From 4ff89e85e74884e8f04edb5c31a94b4323e895e9 Mon Sep 17 00:00:00 2001
From: auth12 <67507608+auth12@users.noreply.github.com>
Date: Wed, 22 Jul 2020 08:40:38 -0700
Subject: Removed wolfssl
---
client/wolfssl/IDE/WIN/README.txt | 70 -
client/wolfssl/IDE/WIN/include.am | 9 -
client/wolfssl/IDE/WIN/test.vcxproj | 277 -
client/wolfssl/IDE/WIN/user_settings.h | 94 -
client/wolfssl/IDE/WIN/wolfssl-fips.sln | 56 -
client/wolfssl/IDE/WIN/wolfssl-fips.vcxproj | 336 -
client/wolfssl/IDE/WIN10/README.txt | 70 -
client/wolfssl/IDE/WIN10/include.am | 9 -
client/wolfssl/IDE/WIN10/test.vcxproj | 277 -
client/wolfssl/IDE/WIN10/user_settings.h | 81 -
client/wolfssl/IDE/WIN10/wolfssl-fips.sln | 56 -
client/wolfssl/IDE/WIN10/wolfssl-fips.vcxproj | 308 -
client/wolfssl/LICENSING | 11 -
client/wolfssl/src/bio.c | 1705 -
client/wolfssl/src/crl.c | 1093 -
client/wolfssl/src/include.am | 531 -
client/wolfssl/src/internal.c | 29412 ------
client/wolfssl/src/keys.c | 3607 -
client/wolfssl/src/ocsp.c | 1100 -
client/wolfssl/src/sniffer.c | 4611 -
client/wolfssl/src/ssl.c | 47707 ----------
client/wolfssl/src/tls.c | 11755 ---
client/wolfssl/src/tls13.c | 9048 --
client/wolfssl/src/wolfio.c | 2375 -
client/wolfssl/wolfcrypt/src/aes.c | 8704 --
client/wolfssl/wolfcrypt/src/aes_asm.S | 1338 -
client/wolfssl/wolfcrypt/src/aes_asm.asm | 1529 -
client/wolfssl/wolfcrypt/src/aes_gcm_asm.S | 8733 --
client/wolfssl/wolfcrypt/src/arc4.c | 149 -
client/wolfssl/wolfcrypt/src/asm.c | 1783 -
client/wolfssl/wolfcrypt/src/asn.c | 17525 ----
client/wolfssl/wolfcrypt/src/async.c | 0
client/wolfssl/wolfcrypt/src/blake2b.c | 452 -
client/wolfssl/wolfcrypt/src/blake2s.c | 446 -
client/wolfssl/wolfcrypt/src/camellia.c | 1644 -
client/wolfssl/wolfcrypt/src/chacha.c | 348 -
client/wolfssl/wolfcrypt/src/chacha20_poly1305.c | 286 -
client/wolfssl/wolfcrypt/src/chacha_asm.S | 1420 -
client/wolfssl/wolfcrypt/src/cmac.c | 215 -
client/wolfssl/wolfcrypt/src/coding.c | 511 -
client/wolfssl/wolfcrypt/src/compress.c | 198 -
client/wolfssl/wolfcrypt/src/cpuid.c | 110 -
client/wolfssl/wolfcrypt/src/cryptocb.c | 648 -
client/wolfssl/wolfcrypt/src/curve25519.c | 513 -
client/wolfssl/wolfcrypt/src/curve448.c | 635 -
client/wolfssl/wolfcrypt/src/des3.c | 1825 -
client/wolfssl/wolfcrypt/src/dh.c | 2491 -
client/wolfssl/wolfcrypt/src/dsa.c | 920 -
client/wolfssl/wolfcrypt/src/ecc.c | 10761 ---
client/wolfssl/wolfcrypt/src/ecc_fp.c | 1 -
client/wolfssl/wolfcrypt/src/ed25519.c | 814 -
client/wolfssl/wolfcrypt/src/ed448.c | 917 -
client/wolfssl/wolfcrypt/src/error.c | 530 -
client/wolfssl/wolfcrypt/src/evp.c | 6595 --
client/wolfssl/wolfcrypt/src/fe_448.c | 2458 -
client/wolfssl/wolfcrypt/src/fe_low_mem.c | 611 -
client/wolfssl/wolfcrypt/src/fe_operations.c | 1438 -
client/wolfssl/wolfcrypt/src/fe_x25519_128.i | 625 -
client/wolfssl/wolfcrypt/src/fe_x25519_asm.S | 16542 ----
client/wolfssl/wolfcrypt/src/fips.c | 0
client/wolfssl/wolfcrypt/src/fips_test.c | 0
client/wolfssl/wolfcrypt/src/fp_mont_small.i | 3874 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_12.i | 147 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_17.i | 187 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_20.i | 210 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_24.i | 243 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_28.i | 275 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_3.i | 61 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_32.i | 321 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_4.i | 83 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_48.i | 435 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_6.i | 99 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_64.i | 563 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_7.i | 107 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_8.i | 115 -
client/wolfssl/wolfcrypt/src/fp_mul_comba_9.i | 123 -
.../wolfssl/wolfcrypt/src/fp_mul_comba_small_set.i | 1268 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_12.i | 177 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_17.i | 227 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_20.i | 257 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_24.i | 297 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_28.i | 337 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_3.i | 73 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_32.i | 377 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_4.i | 97 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_48.i | 537 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_6.i | 117 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_64.i | 697 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_7.i | 127 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_8.i | 137 -
client/wolfssl/wolfcrypt/src/fp_sqr_comba_9.i | 147 -
.../wolfssl/wolfcrypt/src/fp_sqr_comba_small_set.i | 1558 -
client/wolfssl/wolfcrypt/src/ge_448.c | 10780 ---
client/wolfssl/wolfcrypt/src/ge_low_mem.c | 563 -
client/wolfssl/wolfcrypt/src/ge_operations.c | 9803 --
client/wolfssl/wolfcrypt/src/hash.c | 1677 -
client/wolfssl/wolfcrypt/src/hc128.c | 430 -
client/wolfssl/wolfcrypt/src/hmac.c | 1290 -
client/wolfssl/wolfcrypt/src/idea.c | 303 -
client/wolfssl/wolfcrypt/src/include.am | 127 -
client/wolfssl/wolfcrypt/src/integer.c | 5320 --
client/wolfssl/wolfcrypt/src/logging.c | 843 -
client/wolfssl/wolfcrypt/src/md2.c | 162 -
client/wolfssl/wolfcrypt/src/md4.c | 211 -
client/wolfssl/wolfcrypt/src/md5.c | 572 -
client/wolfssl/wolfcrypt/src/memory.c | 1126 -
client/wolfssl/wolfcrypt/src/misc.c | 405 -
client/wolfssl/wolfcrypt/src/pkcs12.c | 2403 -
client/wolfssl/wolfcrypt/src/pkcs7.c | 12523 ---
client/wolfssl/wolfcrypt/src/poly1305.c | 868 -
client/wolfssl/wolfcrypt/src/poly1305_asm.S | 1105 -
.../wolfssl/wolfcrypt/src/port/Espressif/README.md | 109 -
.../wolfcrypt/src/port/Espressif/esp32_aes.c | 299 -
.../wolfcrypt/src/port/Espressif/esp32_mp.c | 514 -
.../wolfcrypt/src/port/Espressif/esp32_sha.c | 434 -
.../wolfcrypt/src/port/Espressif/esp32_util.c | 67 -
.../wolfssl/wolfcrypt/src/port/Renesas/README.md | 176 -
.../wolfcrypt/src/port/Renesas/renesas_tsip_aes.c | 156 -
.../wolfcrypt/src/port/Renesas/renesas_tsip_sha.c | 274 -
.../wolfcrypt/src/port/Renesas/renesas_tsip_util.c | 719 -
.../wolfssl/wolfcrypt/src/port/af_alg/afalg_aes.c | 900 -
.../wolfssl/wolfcrypt/src/port/af_alg/afalg_hash.c | 339 -
.../wolfssl/wolfcrypt/src/port/af_alg/wc_afalg.c | 141 -
.../wolfcrypt/src/port/arm/armv8-32-curve25519.S | 6012 --
.../wolfcrypt/src/port/arm/armv8-32-curve25519.c | 5581 --
.../wolfcrypt/src/port/arm/armv8-32-sha512-asm.S | 5335 --
.../wolfcrypt/src/port/arm/armv8-32-sha512-asm.c | 4783 -
client/wolfssl/wolfcrypt/src/port/arm/armv8-aes.c | 4653 -
.../wolfssl/wolfcrypt/src/port/arm/armv8-chacha.c | 2857 -
.../wolfcrypt/src/port/arm/armv8-curve25519.S | 6715 --
.../wolfcrypt/src/port/arm/armv8-curve25519.c | 6725 --
.../wolfcrypt/src/port/arm/armv8-poly1305.c | 1166 -
.../wolfssl/wolfcrypt/src/port/arm/armv8-sha256.c | 1508 -
.../wolfcrypt/src/port/arm/armv8-sha512-asm.S | 1046 -
.../wolfcrypt/src/port/arm/armv8-sha512-asm.c | 1041 -
.../wolfssl/wolfcrypt/src/port/arm/armv8-sha512.c | 715 -
client/wolfssl/wolfcrypt/src/port/arm/cryptoCell.c | 309 -
.../wolfcrypt/src/port/arm/cryptoCellHash.c | 134 -
client/wolfssl/wolfcrypt/src/port/atmel/README.md | 94 -
client/wolfssl/wolfcrypt/src/port/atmel/atmel.c | 843 -
client/wolfssl/wolfcrypt/src/port/caam/caam_aes.c | 649 -
.../wolfssl/wolfcrypt/src/port/caam/caam_doc.pdf | Bin 1107370 -> 0 bytes
.../wolfssl/wolfcrypt/src/port/caam/caam_driver.c | 1713 -
client/wolfssl/wolfcrypt/src/port/caam/caam_init.c | 289 -
client/wolfssl/wolfcrypt/src/port/caam/caam_sha.c | 397 -
client/wolfssl/wolfcrypt/src/port/cavium/README.md | 3 -
.../wolfcrypt/src/port/cavium/README_Octeon.md | 3 -
.../wolfcrypt/src/port/cavium/cavium_nitrox.c | 0
.../wolfcrypt/src/port/cavium/cavium_octeon_sync.c | 879 -
.../wolfssl/wolfcrypt/src/port/devcrypto/README.md | 43 -
.../wolfcrypt/src/port/devcrypto/devcrypto_aes.c | 384 -
.../wolfcrypt/src/port/devcrypto/devcrypto_hash.c | 248 -
.../wolfcrypt/src/port/devcrypto/wc_devcrypto.c | 167 -
client/wolfssl/wolfcrypt/src/port/intel/README.md | 3 -
.../wolfssl/wolfcrypt/src/port/intel/quickassist.c | 0
.../wolfcrypt/src/port/intel/quickassist_mem.c | 0
.../wolfcrypt/src/port/intel/quickassist_sync.c | 2004 -
.../wolfcrypt/src/port/mynewt/mynewt_port.c | 146 -
client/wolfssl/wolfcrypt/src/port/nrf51.c | 220 -
client/wolfssl/wolfcrypt/src/port/nxp/ksdk_port.c | 1731 -
.../wolfcrypt/src/port/pic32/pic32mz-crypt.c | 804 -
client/wolfssl/wolfcrypt/src/port/st/README.md | 132 -
client/wolfssl/wolfcrypt/src/port/st/stm32.c | 879 -
client/wolfssl/wolfcrypt/src/port/st/stsafe.c | 566 -
client/wolfssl/wolfcrypt/src/port/ti/ti-aes.c | 569 -
client/wolfssl/wolfcrypt/src/port/ti/ti-ccm.c | 94 -
client/wolfssl/wolfcrypt/src/port/ti/ti-des3.c | 204 -
client/wolfssl/wolfcrypt/src/port/ti/ti-hash.c | 338 -
.../wolfssl/wolfcrypt/src/port/xilinx/xil-aesgcm.c | 202 -
.../wolfssl/wolfcrypt/src/port/xilinx/xil-sha3.c | 158 -
client/wolfssl/wolfcrypt/src/pwdbased.c | 795 -
client/wolfssl/wolfcrypt/src/rabbit.c | 342 -
client/wolfssl/wolfcrypt/src/random.c | 2552 -
client/wolfssl/wolfcrypt/src/ripemd.c | 366 -
client/wolfssl/wolfcrypt/src/rsa.c | 4201 -
client/wolfssl/wolfcrypt/src/selftest.c | 0
client/wolfssl/wolfcrypt/src/sha.c | 882 -
client/wolfssl/wolfcrypt/src/sha256.c | 1644 -
client/wolfssl/wolfcrypt/src/sha256_asm.S | 22653 -----
client/wolfssl/wolfcrypt/src/sha3.c | 1216 -
client/wolfssl/wolfcrypt/src/sha512.c | 1225 -
client/wolfssl/wolfcrypt/src/sha512_asm.S | 10741 ---
client/wolfssl/wolfcrypt/src/signature.c | 559 -
client/wolfssl/wolfcrypt/src/sp_arm32.c | 89057 -------------------
client/wolfssl/wolfcrypt/src/sp_arm64.c | 42082 ---------
client/wolfssl/wolfcrypt/src/sp_armthumb.c | 27863 ------
client/wolfssl/wolfcrypt/src/sp_c32.c | 23857 -----
client/wolfssl/wolfcrypt/src/sp_c64.c | 23220 -----
client/wolfssl/wolfcrypt/src/sp_cortexm.c | 25687 ------
client/wolfssl/wolfcrypt/src/sp_dsp32.c | 4908 -
client/wolfssl/wolfcrypt/src/sp_int.c | 2203 -
client/wolfssl/wolfcrypt/src/sp_x86_64.c | 29555 ------
client/wolfssl/wolfcrypt/src/sp_x86_64_asm.S | 41830 ---------
client/wolfssl/wolfcrypt/src/srp.c | 756 -
client/wolfssl/wolfcrypt/src/tfm.c | 5068 --
client/wolfssl/wolfcrypt/src/wc_dsp.c | 327 -
client/wolfssl/wolfcrypt/src/wc_encrypt.c | 660 -
client/wolfssl/wolfcrypt/src/wc_pkcs11.c | 2546 -
client/wolfssl/wolfcrypt/src/wc_port.c | 2276 -
client/wolfssl/wolfcrypt/src/wolfcrypt_first.c | 0
client/wolfssl/wolfcrypt/src/wolfcrypt_last.c | 0
client/wolfssl/wolfcrypt/src/wolfevent.c | 283 -
client/wolfssl/wolfcrypt/src/wolfmath.c | 381 -
client/wolfssl/wolfssl.vcxproj | 373 -
client/wolfssl/wolfssl.vcxproj.user | 4 -
client/wolfssl/wolfssl/callbacks.h | 93 -
client/wolfssl/wolfssl/certs_test.h | 4148 -
client/wolfssl/wolfssl/crl.h | 50 -
client/wolfssl/wolfssl/error-ssl.h | 204 -
client/wolfssl/wolfssl/include.am | 32 -
client/wolfssl/wolfssl/internal.h | 4613 -
client/wolfssl/wolfssl/ocsp.h | 133 -
client/wolfssl/wolfssl/openssl/aes.h | 111 -
client/wolfssl/wolfssl/openssl/asn1.h | 152 -
client/wolfssl/wolfssl/openssl/asn1t.h | 30 -
client/wolfssl/wolfssl/openssl/bio.h | 164 -
client/wolfssl/wolfssl/openssl/bn.h | 226 -
client/wolfssl/wolfssl/openssl/buffer.h | 47 -
client/wolfssl/wolfssl/openssl/conf.h | 48 -
client/wolfssl/wolfssl/openssl/crypto.h | 91 -
client/wolfssl/wolfssl/openssl/des.h | 115 -
client/wolfssl/wolfssl/openssl/dh.h | 115 -
client/wolfssl/wolfssl/openssl/dsa.h | 113 -
client/wolfssl/wolfssl/openssl/ec.h | 323 -
client/wolfssl/wolfssl/openssl/ec25519.h | 44 -
client/wolfssl/wolfssl/openssl/ec448.h | 44 -
client/wolfssl/wolfssl/openssl/ecdh.h | 49 -
client/wolfssl/wolfssl/openssl/ecdsa.h | 75 -
client/wolfssl/wolfssl/openssl/ed25519.h | 47 -
client/wolfssl/wolfssl/openssl/ed448.h | 47 -
client/wolfssl/wolfssl/openssl/engine.h | 8 -
client/wolfssl/wolfssl/openssl/err.h | 55 -
client/wolfssl/wolfssl/openssl/evp.h | 930 -
client/wolfssl/wolfssl/openssl/hmac.h | 100 -
client/wolfssl/wolfssl/openssl/include.am | 52 -
client/wolfssl/wolfssl/openssl/lhash.h | 2 -
client/wolfssl/wolfssl/openssl/md4.h | 62 -
client/wolfssl/wolfssl/openssl/md5.h | 81 -
client/wolfssl/wolfssl/openssl/obj_mac.h | 50 -
client/wolfssl/wolfssl/openssl/objects.h | 63 -
client/wolfssl/wolfssl/openssl/ocsp.h | 82 -
client/wolfssl/wolfssl/openssl/opensslconf.h | 8 -
client/wolfssl/wolfssl/openssl/opensslv.h | 45 -
client/wolfssl/wolfssl/openssl/ossl_typ.h | 32 -
client/wolfssl/wolfssl/openssl/pem.h | 256 -
client/wolfssl/wolfssl/openssl/pkcs12.h | 49 -
client/wolfssl/wolfssl/openssl/pkcs7.h | 79 -
client/wolfssl/wolfssl/openssl/rand.h | 27 -
client/wolfssl/wolfssl/openssl/rc4.h | 59 -
client/wolfssl/wolfssl/openssl/ripemd.h | 58 -
client/wolfssl/wolfssl/openssl/rsa.h | 197 -
client/wolfssl/wolfssl/openssl/sha.h | 203 -
client/wolfssl/wolfssl/openssl/sha3.h | 150 -
client/wolfssl/wolfssl/openssl/ssl.h | 1245 -
client/wolfssl/wolfssl/openssl/ssl23.h | 1 -
client/wolfssl/wolfssl/openssl/stack.h | 59 -
client/wolfssl/wolfssl/openssl/tls1.h | 46 -
client/wolfssl/wolfssl/openssl/ui.h | 2 -
client/wolfssl/wolfssl/openssl/x509.h | 26 -
client/wolfssl/wolfssl/openssl/x509_vfy.h | 45 -
client/wolfssl/wolfssl/openssl/x509v3.h | 116 -
client/wolfssl/wolfssl/options.h | 40 -
client/wolfssl/wolfssl/options.h.in | 40 -
client/wolfssl/wolfssl/sniffer.h | 221 -
client/wolfssl/wolfssl/sniffer_error.h | 137 -
client/wolfssl/wolfssl/sniffer_error.rc | 116 -
client/wolfssl/wolfssl/ssl.h | 3866 -
client/wolfssl/wolfssl/test.h | 3683 -
client/wolfssl/wolfssl/version.h | 40 -
client/wolfssl/wolfssl/version.h.in | 40 -
client/wolfssl/wolfssl/wolfcrypt/aes.h | 437 -
client/wolfssl/wolfssl/wolfcrypt/arc4.h | 68 -
client/wolfssl/wolfssl/wolfcrypt/asn.h | 1470 -
client/wolfssl/wolfssl/wolfcrypt/asn_public.h | 610 -
client/wolfssl/wolfssl/wolfcrypt/async.h | 0
client/wolfssl/wolfssl/wolfcrypt/blake2-impl.h | 155 -
client/wolfssl/wolfssl/wolfcrypt/blake2-int.h | 184 -
client/wolfssl/wolfssl/wolfcrypt/blake2.h | 96 -
client/wolfssl/wolfssl/wolfcrypt/camellia.h | 101 -
client/wolfssl/wolfssl/wolfcrypt/chacha.h | 83 -
.../wolfssl/wolfssl/wolfcrypt/chacha20_poly1305.h | 129 -
client/wolfssl/wolfssl/wolfcrypt/cmac.h | 97 -
client/wolfssl/wolfssl/wolfcrypt/coding.h | 88 -
client/wolfssl/wolfssl/wolfcrypt/compress.h | 58 -
client/wolfssl/wolfssl/wolfcrypt/cpuid.h | 62 -
client/wolfssl/wolfssl/wolfcrypt/cryptocb.h | 299 -
client/wolfssl/wolfssl/wolfcrypt/curve25519.h | 165 -
client/wolfssl/wolfssl/wolfcrypt/curve448.h | 139 -
client/wolfssl/wolfssl/wolfcrypt/des3.h | 158 -
client/wolfssl/wolfssl/wolfcrypt/dh.h | 139 -
client/wolfssl/wolfssl/wolfcrypt/dsa.h | 105 -
client/wolfssl/wolfssl/wolfcrypt/ecc.h | 762 -
client/wolfssl/wolfssl/wolfcrypt/ed25519.h | 169 -
client/wolfssl/wolfssl/wolfcrypt/ed448.h | 160 -
client/wolfssl/wolfssl/wolfcrypt/error-crypt.h | 255 -
client/wolfssl/wolfssl/wolfcrypt/fe_448.h | 116 -
client/wolfssl/wolfssl/wolfcrypt/fe_operations.h | 210 -
client/wolfssl/wolfssl/wolfcrypt/fips.h | 0
client/wolfssl/wolfssl/wolfcrypt/fips_test.h | 59 -
client/wolfssl/wolfssl/wolfcrypt/ge_448.h | 86 -
client/wolfssl/wolfssl/wolfcrypt/ge_operations.h | 113 -
client/wolfssl/wolfssl/wolfcrypt/hash.h | 247 -
client/wolfssl/wolfssl/wolfcrypt/hc128.h | 67 -
client/wolfssl/wolfssl/wolfcrypt/hmac.h | 207 -
client/wolfssl/wolfssl/wolfcrypt/idea.h | 70 -
client/wolfssl/wolfssl/wolfcrypt/include.am | 141 -
client/wolfssl/wolfssl/wolfcrypt/integer.h | 409 -
client/wolfssl/wolfssl/wolfcrypt/logging.h | 194 -
client/wolfssl/wolfssl/wolfcrypt/md2.h | 69 -
client/wolfssl/wolfssl/wolfcrypt/md4.h | 67 -
client/wolfssl/wolfssl/wolfcrypt/md5.h | 130 -
client/wolfssl/wolfssl/wolfcrypt/mem_track.h | 401 -
client/wolfssl/wolfssl/wolfcrypt/memory.h | 233 -
client/wolfssl/wolfssl/wolfcrypt/misc.h | 118 -
client/wolfssl/wolfssl/wolfcrypt/mpi_class.h | 1021 -
client/wolfssl/wolfssl/wolfcrypt/mpi_superclass.h | 96 -
client/wolfssl/wolfssl/wolfcrypt/pkcs11.h | 537 -
client/wolfssl/wolfssl/wolfcrypt/pkcs12.h | 72 -
client/wolfssl/wolfssl/wolfcrypt/pkcs7.h | 504 -
client/wolfssl/wolfssl/wolfcrypt/poly1305.h | 130 -
.../wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h | 152 -
.../wolfcrypt/port/Renesas/renesas-tsip-crypt.h | 153 -
.../wolfssl/wolfcrypt/port/af_alg/afalg_hash.h | 50 -
.../wolfssl/wolfcrypt/port/af_alg/wc_afalg.h | 53 -
.../wolfssl/wolfcrypt/port/arm/cryptoCell.h | 117 -
.../wolfssl/wolfssl/wolfcrypt/port/atmel/atmel.h | 134 -
.../wolfssl/wolfcrypt/port/caam/caam_driver.h | 187 -
.../wolfssl/wolfssl/wolfcrypt/port/caam/wolfcaam.h | 63 -
.../wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h | 88 -
.../wolfssl/wolfcrypt/port/cavium/cavium_nitrox.h | 0
.../wolfcrypt/port/cavium/cavium_octeon_sync.h | 31 -
.../wolfcrypt/port/devcrypto/wc_devcrypto.h | 53 -
.../wolfssl/wolfcrypt/port/intel/quickassist.h | 0
.../wolfssl/wolfcrypt/port/intel/quickassist_mem.h | 0
.../wolfcrypt/port/intel/quickassist_sync.h | 53 -
client/wolfssl/wolfssl/wolfcrypt/port/nrf51.h | 44 -
.../wolfssl/wolfssl/wolfcrypt/port/nxp/ksdk_port.h | 93 -
.../wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h | 224 -
client/wolfssl/wolfssl/wolfcrypt/port/st/stm32.h | 142 -
client/wolfssl/wolfssl/wolfcrypt/port/st/stsafe.h | 99 -
client/wolfssl/wolfssl/wolfcrypt/port/ti/ti-ccm.h | 47 -
client/wolfssl/wolfssl/wolfcrypt/port/ti/ti-hash.h | 63 -
.../wolfssl/wolfcrypt/port/xilinx/xil-sha3.h | 45 -
client/wolfssl/wolfssl/wolfcrypt/pwdbased.h | 77 -
client/wolfssl/wolfssl/wolfcrypt/rabbit.h | 73 -
client/wolfssl/wolfssl/wolfcrypt/random.h | 250 -
client/wolfssl/wolfssl/wolfcrypt/ripemd.h | 67 -
client/wolfssl/wolfssl/wolfcrypt/rsa.h | 371 -
client/wolfssl/wolfssl/wolfcrypt/selftest.h | 48 -
client/wolfssl/wolfssl/wolfcrypt/settings.h | 2195 -
client/wolfssl/wolfssl/wolfcrypt/sha.h | 178 -
client/wolfssl/wolfssl/wolfcrypt/sha256.h | 255 -
client/wolfssl/wolfssl/wolfcrypt/sha3.h | 165 -
client/wolfssl/wolfssl/wolfcrypt/sha512.h | 228 -
client/wolfssl/wolfssl/wolfcrypt/signature.h | 87 -
client/wolfssl/wolfssl/wolfcrypt/sp.h | 154 -
client/wolfssl/wolfssl/wolfcrypt/sp_int.h | 287 -
client/wolfssl/wolfssl/wolfcrypt/srp.h | 311 -
client/wolfssl/wolfssl/wolfcrypt/tfm.h | 839 -
client/wolfssl/wolfssl/wolfcrypt/types.h | 950 -
client/wolfssl/wolfssl/wolfcrypt/visibility.h | 79 -
client/wolfssl/wolfssl/wolfcrypt/wc_encrypt.h | 101 -
client/wolfssl/wolfssl/wolfcrypt/wc_pkcs11.h | 94 -
client/wolfssl/wolfssl/wolfcrypt/wc_port.h | 780 -
client/wolfssl/wolfssl/wolfcrypt/wolfevent.h | 120 -
client/wolfssl/wolfssl/wolfcrypt/wolfmath.h | 97 -
client/wolfssl/wolfssl/wolfio.h | 600 -
367 files changed, 753747 deletions(-)
delete mode 100644 client/wolfssl/IDE/WIN/README.txt
delete mode 100644 client/wolfssl/IDE/WIN/include.am
delete mode 100644 client/wolfssl/IDE/WIN/test.vcxproj
delete mode 100644 client/wolfssl/IDE/WIN/user_settings.h
delete mode 100644 client/wolfssl/IDE/WIN/wolfssl-fips.sln
delete mode 100644 client/wolfssl/IDE/WIN/wolfssl-fips.vcxproj
delete mode 100644 client/wolfssl/IDE/WIN10/README.txt
delete mode 100644 client/wolfssl/IDE/WIN10/include.am
delete mode 100644 client/wolfssl/IDE/WIN10/test.vcxproj
delete mode 100644 client/wolfssl/IDE/WIN10/user_settings.h
delete mode 100644 client/wolfssl/IDE/WIN10/wolfssl-fips.sln
delete mode 100644 client/wolfssl/IDE/WIN10/wolfssl-fips.vcxproj
delete mode 100644 client/wolfssl/LICENSING
delete mode 100644 client/wolfssl/src/bio.c
delete mode 100644 client/wolfssl/src/crl.c
delete mode 100644 client/wolfssl/src/include.am
delete mode 100644 client/wolfssl/src/internal.c
delete mode 100644 client/wolfssl/src/keys.c
delete mode 100644 client/wolfssl/src/ocsp.c
delete mode 100644 client/wolfssl/src/sniffer.c
delete mode 100644 client/wolfssl/src/ssl.c
delete mode 100644 client/wolfssl/src/tls.c
delete mode 100644 client/wolfssl/src/tls13.c
delete mode 100644 client/wolfssl/src/wolfio.c
delete mode 100644 client/wolfssl/wolfcrypt/src/aes.c
delete mode 100644 client/wolfssl/wolfcrypt/src/aes_asm.S
delete mode 100644 client/wolfssl/wolfcrypt/src/aes_asm.asm
delete mode 100644 client/wolfssl/wolfcrypt/src/aes_gcm_asm.S
delete mode 100644 client/wolfssl/wolfcrypt/src/arc4.c
delete mode 100644 client/wolfssl/wolfcrypt/src/asm.c
delete mode 100644 client/wolfssl/wolfcrypt/src/asn.c
delete mode 100644 client/wolfssl/wolfcrypt/src/async.c
delete mode 100644 client/wolfssl/wolfcrypt/src/blake2b.c
delete mode 100644 client/wolfssl/wolfcrypt/src/blake2s.c
delete mode 100644 client/wolfssl/wolfcrypt/src/camellia.c
delete mode 100644 client/wolfssl/wolfcrypt/src/chacha.c
delete mode 100644 client/wolfssl/wolfcrypt/src/chacha20_poly1305.c
delete mode 100644 client/wolfssl/wolfcrypt/src/chacha_asm.S
delete mode 100644 client/wolfssl/wolfcrypt/src/cmac.c
delete mode 100644 client/wolfssl/wolfcrypt/src/coding.c
delete mode 100644 client/wolfssl/wolfcrypt/src/compress.c
delete mode 100644 client/wolfssl/wolfcrypt/src/cpuid.c
delete mode 100644 client/wolfssl/wolfcrypt/src/cryptocb.c
delete mode 100644 client/wolfssl/wolfcrypt/src/curve25519.c
delete mode 100644 client/wolfssl/wolfcrypt/src/curve448.c
delete mode 100644 client/wolfssl/wolfcrypt/src/des3.c
delete mode 100644 client/wolfssl/wolfcrypt/src/dh.c
delete mode 100644 client/wolfssl/wolfcrypt/src/dsa.c
delete mode 100644 client/wolfssl/wolfcrypt/src/ecc.c
delete mode 100644 client/wolfssl/wolfcrypt/src/ecc_fp.c
delete mode 100644 client/wolfssl/wolfcrypt/src/ed25519.c
delete mode 100644 client/wolfssl/wolfcrypt/src/ed448.c
delete mode 100644 client/wolfssl/wolfcrypt/src/error.c
delete mode 100644 client/wolfssl/wolfcrypt/src/evp.c
delete mode 100644 client/wolfssl/wolfcrypt/src/fe_448.c
delete mode 100644 client/wolfssl/wolfcrypt/src/fe_low_mem.c
delete mode 100644 client/wolfssl/wolfcrypt/src/fe_operations.c
delete mode 100644 client/wolfssl/wolfcrypt/src/fe_x25519_128.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fe_x25519_asm.S
delete mode 100644 client/wolfssl/wolfcrypt/src/fips.c
delete mode 100644 client/wolfssl/wolfcrypt/src/fips_test.c
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mont_small.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_12.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_17.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_20.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_24.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_28.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_3.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_32.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_4.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_48.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_6.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_64.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_7.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_8.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_9.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_mul_comba_small_set.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_12.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_17.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_20.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_24.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_28.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_3.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_32.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_4.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_48.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_6.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_64.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_7.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_8.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_9.i
delete mode 100644 client/wolfssl/wolfcrypt/src/fp_sqr_comba_small_set.i
delete mode 100644 client/wolfssl/wolfcrypt/src/ge_448.c
delete mode 100644 client/wolfssl/wolfcrypt/src/ge_low_mem.c
delete mode 100644 client/wolfssl/wolfcrypt/src/ge_operations.c
delete mode 100644 client/wolfssl/wolfcrypt/src/hash.c
delete mode 100644 client/wolfssl/wolfcrypt/src/hc128.c
delete mode 100644 client/wolfssl/wolfcrypt/src/hmac.c
delete mode 100644 client/wolfssl/wolfcrypt/src/idea.c
delete mode 100644 client/wolfssl/wolfcrypt/src/include.am
delete mode 100644 client/wolfssl/wolfcrypt/src/integer.c
delete mode 100644 client/wolfssl/wolfcrypt/src/logging.c
delete mode 100644 client/wolfssl/wolfcrypt/src/md2.c
delete mode 100644 client/wolfssl/wolfcrypt/src/md4.c
delete mode 100644 client/wolfssl/wolfcrypt/src/md5.c
delete mode 100644 client/wolfssl/wolfcrypt/src/memory.c
delete mode 100644 client/wolfssl/wolfcrypt/src/misc.c
delete mode 100644 client/wolfssl/wolfcrypt/src/pkcs12.c
delete mode 100644 client/wolfssl/wolfcrypt/src/pkcs7.c
delete mode 100644 client/wolfssl/wolfcrypt/src/poly1305.c
delete mode 100644 client/wolfssl/wolfcrypt/src/poly1305_asm.S
delete mode 100644 client/wolfssl/wolfcrypt/src/port/Espressif/README.md
delete mode 100644 client/wolfssl/wolfcrypt/src/port/Espressif/esp32_aes.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/Espressif/esp32_mp.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/Espressif/esp32_sha.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/Espressif/esp32_util.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/Renesas/README.md
delete mode 100644 client/wolfssl/wolfcrypt/src/port/Renesas/renesas_tsip_aes.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/Renesas/renesas_tsip_sha.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/Renesas/renesas_tsip_util.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/af_alg/afalg_aes.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/af_alg/afalg_hash.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/af_alg/wc_afalg.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-32-curve25519.S
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-32-curve25519.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-32-sha512-asm.S
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-32-sha512-asm.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-aes.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-chacha.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-curve25519.S
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-curve25519.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-poly1305.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-sha256.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-sha512-asm.S
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-sha512-asm.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/armv8-sha512.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/cryptoCell.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/arm/cryptoCellHash.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/atmel/README.md
delete mode 100644 client/wolfssl/wolfcrypt/src/port/atmel/atmel.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/caam/caam_aes.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/caam/caam_doc.pdf
delete mode 100644 client/wolfssl/wolfcrypt/src/port/caam/caam_driver.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/caam/caam_init.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/caam/caam_sha.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/cavium/README.md
delete mode 100644 client/wolfssl/wolfcrypt/src/port/cavium/README_Octeon.md
delete mode 100644 client/wolfssl/wolfcrypt/src/port/cavium/cavium_nitrox.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/cavium/cavium_octeon_sync.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/devcrypto/README.md
delete mode 100644 client/wolfssl/wolfcrypt/src/port/devcrypto/devcrypto_aes.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/devcrypto/devcrypto_hash.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/devcrypto/wc_devcrypto.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/intel/README.md
delete mode 100644 client/wolfssl/wolfcrypt/src/port/intel/quickassist.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/intel/quickassist_mem.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/intel/quickassist_sync.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/mynewt/mynewt_port.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/nrf51.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/nxp/ksdk_port.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/pic32/pic32mz-crypt.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/st/README.md
delete mode 100644 client/wolfssl/wolfcrypt/src/port/st/stm32.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/st/stsafe.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/ti/ti-aes.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/ti/ti-ccm.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/ti/ti-des3.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/ti/ti-hash.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/xilinx/xil-aesgcm.c
delete mode 100644 client/wolfssl/wolfcrypt/src/port/xilinx/xil-sha3.c
delete mode 100644 client/wolfssl/wolfcrypt/src/pwdbased.c
delete mode 100644 client/wolfssl/wolfcrypt/src/rabbit.c
delete mode 100644 client/wolfssl/wolfcrypt/src/random.c
delete mode 100644 client/wolfssl/wolfcrypt/src/ripemd.c
delete mode 100644 client/wolfssl/wolfcrypt/src/rsa.c
delete mode 100644 client/wolfssl/wolfcrypt/src/selftest.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sha.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sha256.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sha256_asm.S
delete mode 100644 client/wolfssl/wolfcrypt/src/sha3.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sha512.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sha512_asm.S
delete mode 100644 client/wolfssl/wolfcrypt/src/signature.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sp_arm32.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sp_arm64.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sp_armthumb.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sp_c32.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sp_c64.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sp_cortexm.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sp_dsp32.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sp_int.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sp_x86_64.c
delete mode 100644 client/wolfssl/wolfcrypt/src/sp_x86_64_asm.S
delete mode 100644 client/wolfssl/wolfcrypt/src/srp.c
delete mode 100644 client/wolfssl/wolfcrypt/src/tfm.c
delete mode 100644 client/wolfssl/wolfcrypt/src/wc_dsp.c
delete mode 100644 client/wolfssl/wolfcrypt/src/wc_encrypt.c
delete mode 100644 client/wolfssl/wolfcrypt/src/wc_pkcs11.c
delete mode 100644 client/wolfssl/wolfcrypt/src/wc_port.c
delete mode 100644 client/wolfssl/wolfcrypt/src/wolfcrypt_first.c
delete mode 100644 client/wolfssl/wolfcrypt/src/wolfcrypt_last.c
delete mode 100644 client/wolfssl/wolfcrypt/src/wolfevent.c
delete mode 100644 client/wolfssl/wolfcrypt/src/wolfmath.c
delete mode 100644 client/wolfssl/wolfssl.vcxproj
delete mode 100644 client/wolfssl/wolfssl.vcxproj.user
delete mode 100644 client/wolfssl/wolfssl/callbacks.h
delete mode 100644 client/wolfssl/wolfssl/certs_test.h
delete mode 100644 client/wolfssl/wolfssl/crl.h
delete mode 100644 client/wolfssl/wolfssl/error-ssl.h
delete mode 100644 client/wolfssl/wolfssl/include.am
delete mode 100644 client/wolfssl/wolfssl/internal.h
delete mode 100644 client/wolfssl/wolfssl/ocsp.h
delete mode 100644 client/wolfssl/wolfssl/openssl/aes.h
delete mode 100644 client/wolfssl/wolfssl/openssl/asn1.h
delete mode 100644 client/wolfssl/wolfssl/openssl/asn1t.h
delete mode 100644 client/wolfssl/wolfssl/openssl/bio.h
delete mode 100644 client/wolfssl/wolfssl/openssl/bn.h
delete mode 100644 client/wolfssl/wolfssl/openssl/buffer.h
delete mode 100644 client/wolfssl/wolfssl/openssl/conf.h
delete mode 100644 client/wolfssl/wolfssl/openssl/crypto.h
delete mode 100644 client/wolfssl/wolfssl/openssl/des.h
delete mode 100644 client/wolfssl/wolfssl/openssl/dh.h
delete mode 100644 client/wolfssl/wolfssl/openssl/dsa.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ec.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ec25519.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ec448.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ecdh.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ecdsa.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ed25519.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ed448.h
delete mode 100644 client/wolfssl/wolfssl/openssl/engine.h
delete mode 100644 client/wolfssl/wolfssl/openssl/err.h
delete mode 100644 client/wolfssl/wolfssl/openssl/evp.h
delete mode 100644 client/wolfssl/wolfssl/openssl/hmac.h
delete mode 100644 client/wolfssl/wolfssl/openssl/include.am
delete mode 100644 client/wolfssl/wolfssl/openssl/lhash.h
delete mode 100644 client/wolfssl/wolfssl/openssl/md4.h
delete mode 100644 client/wolfssl/wolfssl/openssl/md5.h
delete mode 100644 client/wolfssl/wolfssl/openssl/obj_mac.h
delete mode 100644 client/wolfssl/wolfssl/openssl/objects.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ocsp.h
delete mode 100644 client/wolfssl/wolfssl/openssl/opensslconf.h
delete mode 100644 client/wolfssl/wolfssl/openssl/opensslv.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ossl_typ.h
delete mode 100644 client/wolfssl/wolfssl/openssl/pem.h
delete mode 100644 client/wolfssl/wolfssl/openssl/pkcs12.h
delete mode 100644 client/wolfssl/wolfssl/openssl/pkcs7.h
delete mode 100644 client/wolfssl/wolfssl/openssl/rand.h
delete mode 100644 client/wolfssl/wolfssl/openssl/rc4.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ripemd.h
delete mode 100644 client/wolfssl/wolfssl/openssl/rsa.h
delete mode 100644 client/wolfssl/wolfssl/openssl/sha.h
delete mode 100644 client/wolfssl/wolfssl/openssl/sha3.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ssl.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ssl23.h
delete mode 100644 client/wolfssl/wolfssl/openssl/stack.h
delete mode 100644 client/wolfssl/wolfssl/openssl/tls1.h
delete mode 100644 client/wolfssl/wolfssl/openssl/ui.h
delete mode 100644 client/wolfssl/wolfssl/openssl/x509.h
delete mode 100644 client/wolfssl/wolfssl/openssl/x509_vfy.h
delete mode 100644 client/wolfssl/wolfssl/openssl/x509v3.h
delete mode 100644 client/wolfssl/wolfssl/options.h
delete mode 100644 client/wolfssl/wolfssl/options.h.in
delete mode 100644 client/wolfssl/wolfssl/sniffer.h
delete mode 100644 client/wolfssl/wolfssl/sniffer_error.h
delete mode 100644 client/wolfssl/wolfssl/sniffer_error.rc
delete mode 100644 client/wolfssl/wolfssl/ssl.h
delete mode 100644 client/wolfssl/wolfssl/test.h
delete mode 100644 client/wolfssl/wolfssl/version.h
delete mode 100644 client/wolfssl/wolfssl/version.h.in
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/aes.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/arc4.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/asn.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/asn_public.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/async.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/blake2-impl.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/blake2-int.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/blake2.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/camellia.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/chacha.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/chacha20_poly1305.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/cmac.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/coding.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/compress.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/cpuid.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/cryptocb.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/curve25519.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/curve448.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/des3.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/dh.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/dsa.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/ecc.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/ed25519.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/ed448.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/error-crypt.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/fe_448.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/fe_operations.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/fips.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/fips_test.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/ge_448.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/ge_operations.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/hash.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/hc128.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/hmac.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/idea.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/include.am
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/integer.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/logging.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/md2.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/md4.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/md5.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/mem_track.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/memory.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/misc.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/mpi_class.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/mpi_superclass.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/pkcs11.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/pkcs12.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/pkcs7.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/poly1305.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/af_alg/afalg_hash.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/af_alg/wc_afalg.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/arm/cryptoCell.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/atmel/atmel.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/caam/caam_driver.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/caam/wolfcaam.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/caam/wolfcaam_sha.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/cavium/cavium_nitrox.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/intel/quickassist.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/intel/quickassist_mem.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/intel/quickassist_sync.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/nrf51.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/nxp/ksdk_port.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/st/stm32.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/st/stsafe.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/ti/ti-ccm.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/ti/ti-hash.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/port/xilinx/xil-sha3.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/pwdbased.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/rabbit.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/random.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/ripemd.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/rsa.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/selftest.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/settings.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/sha.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/sha256.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/sha3.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/sha512.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/signature.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/sp.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/sp_int.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/srp.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/tfm.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/types.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/visibility.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/wc_encrypt.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/wc_pkcs11.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/wc_port.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/wolfevent.h
delete mode 100644 client/wolfssl/wolfssl/wolfcrypt/wolfmath.h
delete mode 100644 client/wolfssl/wolfssl/wolfio.h
(limited to 'client')
diff --git a/client/wolfssl/IDE/WIN/README.txt b/client/wolfssl/IDE/WIN/README.txt
deleted file mode 100644
index c748bc5..0000000
--- a/client/wolfssl/IDE/WIN/README.txt
+++ /dev/null
@@ -1,70 +0,0 @@
-# Notes on the wolfssl-fips project
-
-First, if you did not get the FIPS files with your archive, you must contact
-wolfSSL to obtain them.
-
-
-# Building the wolfssl-fips project
-
-The wolfCrypt FIPS library for Windows is a part of the wolfSSL library. It
-must be built as a static library, for the moment.
-
-The library project is built with Whole Program Optimization disabled. This is
-required so that necessary components of the library are not optimized away.
-There are two functions added to the library that are used as markers in
-memory for the in-core memory check of the code. WPO consolidates them into a
-single function. WPO also optimizes away the automatic FIPS entry function.
-
-Each of the source files inside the FIPS boundary defines their own code and
-constant section. The code section names start with ".fipsA$" and the constant
-section names start with ".fipsB$". Each subsection has a letter to organize
-them in a specific order. This specific ordering puts marker functions and
-constants on either end of the boundary so it can be hashed.
-
-
-# In Core Memory Test
-
-The In Core Memory test calculates a checksum (HMAC-SHA256) of the wolfCrypt
-FIPS library code and constant data and compares it with a known value in
-the code.
-
-The Randomized Base Address setting needs to be disabled on the 32-bit builds
-but can be enabled on the 64-bit builds. In the 32-bit mode the addresses
-being different throws off the in-core memory calculation. It looks like in
-64-bit mode the library uses all offsets, so the core hash calculation
-is the same every time.
-
-The "verifyCore" check value in the source fips_test.c needs to be updated when
-building the code. The POS performs this check and the default failure callback
-will print out the calculated checksum. When developing your code, copy this
-value and paste it back into your code in the verifyCore initializer then
-rebuild the code. When statically linking, you may have to recalculate your
-check value when changing your application.
-
-
-# Build Options
-
-The default build options should be the proper default set of options:
-
- * HAVE_FIPS
- * HAVE_THREAD_LS
- * HAVE_AESGCM
- * HAVE_HASHDRBG
- * WOLFSSL_SHA384
- * WOLFSSL_SHA512
- * NO_HC128
- * NO_RC4
- * NO_RABBIT
- * NO_DSA
- * NO_MD4
-
-The "NO" options explicitly disable algorithms that are not allowed in
-FIPS mode.
-
-Additionally one may enable:
-
- * HAVE_ECC
- * OPENSSL_EXTRA
- * WOLFSSL_KEY_GEN
-
-These settings are defined in IDE/WIN/user_settings.h.
diff --git a/client/wolfssl/IDE/WIN/include.am b/client/wolfssl/IDE/WIN/include.am
deleted file mode 100644
index ef20c51..0000000
--- a/client/wolfssl/IDE/WIN/include.am
+++ /dev/null
@@ -1,9 +0,0 @@
-# vim:ft=automake
-# included from Top Level Makefile.am
-# All paths should be given relative to the root
-
-EXTRA_DIST+= IDE/WIN/README.txt
-EXTRA_DIST+= IDE/WIN/test.vcxproj
-EXTRA_DIST+= IDE/WIN/wolfssl-fips.sln
-EXTRA_DIST+= IDE/WIN/wolfssl-fips.vcxproj
-EXTRA_DIST+= IDE/WIN/user_settings.h
diff --git a/client/wolfssl/IDE/WIN/test.vcxproj b/client/wolfssl/IDE/WIN/test.vcxproj
deleted file mode 100644
index 81b3668..0000000
--- a/client/wolfssl/IDE/WIN/test.vcxproj
+++ /dev/null
@@ -1,277 +0,0 @@
-
-
-
-
- Debug
- Win32
-
-
- Debug
- x64
-
-
- DLL Debug
- Win32
-
-
- DLL Debug
- x64
-
-
- DLL Release
- Win32
-
-
- DLL Release
- x64
-
-
- Release
- Win32
-
-
- Release
- x64
-
-
-
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}
- Win32Proj
-
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <_ProjectFileVersion>11.0.61030.0
-
-
- $(SolutionDir)$(Configuration)\$(Platform)\
- $(Configuration)\$(Platform)\obj\
- false
-
-
-
- Disabled
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;_DEBUG;_CONSOLE;HAVE_FIPS;USE_CERT_BUFFERS_2048;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
-
- Level3
- ProgramDatabase
-
-
- true
- Console
- MachineX86
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- false
-
-
-
-
- Disabled
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;_DEBUG;_CONSOLE;HAVE_FIPS;USE_CERT_BUFFERS_2048;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
-
- Level3
- ProgramDatabase
-
-
- true
- Console
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
-
-
-
-
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;NDEBUG;_CONSOLE;HAVE_FIPS;USE_CERT_BUFFERS_2048;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
-
- Level3
- ProgramDatabase
- true
-
-
- true
- Console
- MachineX86
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
- true
- UseLinkTimeCodeGeneration
- false
-
-
-
-
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;NDEBUG;_CONSOLE;HAVE_FIPS;USE_CERT_BUFFERS_2048;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
-
- Level3
- ProgramDatabase
- true
-
-
- true
- Console
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
- true
- UseLinkTimeCodeGeneration
-
-
-
-
- Disabled
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;_DEBUG;_CONSOLE;HAVE_FIPS;USE_CERT_BUFFERS_2048;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;CYASSL_DLL;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
-
- Level3
- ProgramDatabase
- false
-
-
- true
- Console
- MachineX86
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
-
-
-
-
- Disabled
- .\;..\..\;%(AdditionalIncludeDirectories)
- _DEBUG;_CONSOLE;HAVE_FIPS;USE_CERT_BUFFERS_2048;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;CYASSL_DLL;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
-
- Level3
- ProgramDatabase
- false
-
-
- true
- Console
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- false
-
-
-
-
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;NDEBUG;_CONSOLE;HAVE_FIPS;USE_CERT_BUFFERS_2048;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;CYASSL_DLL;%(PreprocessorDefinitions)
- MultiThreadedDLL
-
- Level3
- ProgramDatabase
-
-
- true
- Console
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
- true
- MachineX86
- UseLinkTimeCodeGeneration
- true
-
-
-
-
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;NDEBUG;_CONSOLE;HAVE_FIPS;USE_CERT_BUFFERS_2048;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;CYASSL_DLL;%(PreprocessorDefinitions)
- MultiThreadedDLL
-
- Level3
- ProgramDatabase
-
-
- true
- Console
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
- true
- UseLinkTimeCodeGeneration
- false
-
-
-
-
-
-
-
- {73973223-5ee8-41ca-8e88-1d60e89a237b}
- false
-
-
-
-
-
-
diff --git a/client/wolfssl/IDE/WIN/user_settings.h b/client/wolfssl/IDE/WIN/user_settings.h
deleted file mode 100644
index 5e37e4e..0000000
--- a/client/wolfssl/IDE/WIN/user_settings.h
+++ /dev/null
@@ -1,94 +0,0 @@
-#ifndef _WIN_USER_SETTINGS_H_
-#define _WIN_USER_SETTINGS_H_
-
-/* Verify this is Windows */
-#ifndef _WIN32
-#error This user_settings.h header is only designed for Windows
-#endif
-
-/* Configurations */
-#if defined(HAVE_FIPS)
- /* FIPS */
- #define OPENSSL_EXTRA
- #define HAVE_THREAD_LS
- #define WOLFSSL_KEY_GEN
- #define HAVE_AESGCM
- #define HAVE_HASHDRBG
- #define WOLFSSL_SHA384
- #define WOLFSSL_SHA512
- #define NO_PSK
- #define NO_HC128
- #define NO_RC4
- #define NO_RABBIT
- #define NO_DSA
- #define NO_MD4
-
- #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
- #define WOLFSSL_SHA224
- #define WOLFSSL_SHA3
- #define WC_RSA_PSS
- #define WC_RSA_NO_PADDING
- #define HAVE_ECC
- #define ECC_SHAMIR
- #define HAVE_ECC_CDH
- #define ECC_TIMING_RESISTANT
- #define TFM_TIMING_RESISTANT
- #define WOLFSSL_AES_COUNTER
- #define WOLFSSL_AES_DIRECT
- #define HAVE_AES_ECB
- #define HAVE_AESCCM
- #define WOLFSSL_CMAC
- #define HAVE_HKDF
- #define WOLFSSL_VALIDATE_ECC_IMPORT
- #define WOLFSSL_VALIDATE_FFC_IMPORT
- #define HAVE_FFDHE_Q
- #define WOLFSSL_AESNI
- #define HAVE_INTEL_RDSEED
- #define FORCE_FAILURE_RDSEED
- #endif /* FIPS v2 */
-#else
- /* Enables blinding mode, to prevent timing attacks */
- #define WC_RSA_BLINDING
-
- #if defined(WOLFSSL_LIB)
- /* The lib */
- #define WOLFSSL_SHA512
- #define NO_PSK
- #define NO_WOLFSSL_SERVER
- #define NO_ERROR_STRINGS
- #define NO_OLD_TLS
-
- #define HAVE_EXTENDED_MASTER
- #define HAVE_TLS_EXTENSIONS
-
- #define WOLFSSL_TLS13
- #define HAVE_TLS_EXTENSIONS
-
- #define HAVE_SUPPORTED_CURVES
-
- #define HAVE_ECC
-
- #define HAVE_HKDF
-
- #define HAVE_FFDHE_8192
-
- #define WC_RSA_PSS
-
- #define HAVE_AESGCM
- #define WOLFSSL_SHA384
- #define WOLFSSL_SHA512
-
- #define HAVE_SUPPORTED_CURVES
- #define HAVE_TLS_EXTENSIONS
-
- #define HAVE_ECC
- #define ECC_SHAMIR
- #define ECC_TIMING_RESISTANT
- #else
- /* The servers and clients */
- #define OPENSSL_EXTRA
- #define NO_PSK
- #endif
-#endif /* HAVE_FIPS */
-
-#endif /* _WIN_USER_SETTINGS_H_ */
diff --git a/client/wolfssl/IDE/WIN/wolfssl-fips.sln b/client/wolfssl/IDE/WIN/wolfssl-fips.sln
deleted file mode 100644
index 7f53706..0000000
--- a/client/wolfssl/IDE/WIN/wolfssl-fips.sln
+++ /dev/null
@@ -1,56 +0,0 @@
-
-Microsoft Visual Studio Solution File, Format Version 10.00
-# Visual Studio Express 2012 for Windows Desktop
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wolfssl-fips", "wolfssl-fips.vcxproj", "{73973223-5EE8-41CA-8E88-1D60E89A237B}"
-EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test.vcxproj", "{D04BDF66-664A-4D59-BEAC-8AB2D5809C21}"
-EndProject
-Global
- GlobalSection(SolutionConfigurationPlatforms) = preSolution
- Debug|Win32 = Debug|Win32
- Debug|x64 = Debug|x64
- DLL Debug|Win32 = DLL Debug|Win32
- DLL Debug|x64 = DLL Debug|x64
- DLL Release|Win32 = DLL Release|Win32
- DLL Release|x64 = DLL Release|x64
- Release|Win32 = Release|Win32
- Release|x64 = Release|x64
- EndGlobalSection
- GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Debug|Win32.ActiveCfg = Debug|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Debug|Win32.Build.0 = Debug|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Debug|x64.ActiveCfg = Debug|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Debug|x64.Build.0 = Debug|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Debug|Win32.ActiveCfg = DLL Debug|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Debug|Win32.Build.0 = DLL Debug|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Debug|x64.ActiveCfg = DLL Debug|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Debug|x64.Build.0 = DLL Debug|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Release|Win32.ActiveCfg = DLL Release|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Release|Win32.Build.0 = DLL Release|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Release|x64.ActiveCfg = DLL Release|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Release|x64.Build.0 = DLL Release|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Release|Win32.ActiveCfg = Release|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Release|Win32.Build.0 = Release|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Release|x64.ActiveCfg = Release|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Release|x64.Build.0 = Release|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Debug|Win32.ActiveCfg = Debug|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Debug|Win32.Build.0 = Debug|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Debug|x64.ActiveCfg = Debug|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Debug|x64.Build.0 = Debug|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Debug|Win32.ActiveCfg = DLL Debug|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Debug|Win32.Build.0 = DLL Debug|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Debug|x64.ActiveCfg = DLL Debug|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Debug|x64.Build.0 = DLL Debug|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Release|Win32.ActiveCfg = DLL Release|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Release|Win32.Build.0 = DLL Release|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Release|x64.ActiveCfg = DLL Release|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Release|x64.Build.0 = DLL Release|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Release|Win32.ActiveCfg = Release|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Release|Win32.Build.0 = Release|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Release|x64.ActiveCfg = Release|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Release|x64.Build.0 = Release|x64
- EndGlobalSection
- GlobalSection(SolutionProperties) = preSolution
- HideSolutionNode = FALSE
- EndGlobalSection
-EndGlobal
diff --git a/client/wolfssl/IDE/WIN/wolfssl-fips.vcxproj b/client/wolfssl/IDE/WIN/wolfssl-fips.vcxproj
deleted file mode 100644
index ebdf4fc..0000000
--- a/client/wolfssl/IDE/WIN/wolfssl-fips.vcxproj
+++ /dev/null
@@ -1,336 +0,0 @@
-
-
-
-
- Debug
- Win32
-
-
- Debug
- x64
-
-
- DLL Debug
- Win32
-
-
- DLL Debug
- x64
-
-
- DLL Release
- Win32
-
-
- DLL Release
- x64
-
-
- Release
- Win32
-
-
- Release
- x64
-
-
-
- {73973223-5EE8-41CA-8E88-1D60E89A237B}
- wolfssl-fips
- Win32Proj
-
-
-
- StaticLibrary
- v110
- Unicode
- true
-
-
- DynamicLibrary
- v110
- Unicode
- true
-
-
- StaticLibrary
- v110
- Unicode
- true
-
-
- DynamicLibrary
- v110
- Unicode
- true
-
-
- StaticLibrary
- v110
- Unicode
-
-
- DynamicLibrary
- v110
- Unicode
-
-
- StaticLibrary
- v110
- Unicode
-
-
- DynamicLibrary
- v110
- Unicode
- true
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $(SolutionDir)$(Configuration)\$(Platform)\
- $(Configuration)\$(Platform)\obj\
-
-
-
- Disabled
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
- Level4
- ProgramDatabase
- 4206;4214;4706;%(DisableSpecificWarnings)
-
-
-
-
- Disabled
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;BUILDING_WOLFSSL;CYASSL_DLL;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- true
- EnableFastChecks
- MultiThreadedDebugDLL
- Level4
- ProgramDatabase
- 4206;4214;4706;%(DisableSpecificWarnings)
-
-
- ws2_32.lib;%(AdditionalDependencies)
- 0x5A000000
- false
- false
-
-
-
-
- Disabled
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
- Level4
- ProgramDatabase
- 4206;4214;4706;%(DisableSpecificWarnings)
-
-
-
-
- Disabled
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;BUILDING_WOLFSSL;CYASSL_DLL;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- true
- EnableFastChecks
- MultiThreadedDebugDLL
- Level4
- ProgramDatabase
- 4206;4214;4706;%(DisableSpecificWarnings)
-
-
- ws2_32.lib;%(AdditionalDependencies)
- true
- false
-
-
-
-
- MaxSpeed
- true
- ./;../../;%(AdditionalIncludeDirectories)
- WIN32;HAVE_FIPS;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
- true
- Level3
- ProgramDatabase
- false
-
-
-
-
- MaxSpeed
- true
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;BUILDING_WOLFSSL;CYASSL_DLL;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
- true
- Level3
- ProgramDatabase
-
-
- ws2_32.lib;%(AdditionalDependencies)
- false
- 0x5A000000
-
-
-
-
- MaxSpeed
- true
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
- true
- Level3
- ProgramDatabase
- false
-
-
-
-
- MaxSpeed
- true
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;BUILDING_WOLFSSL;CYASSL_DLL;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
- true
- Level3
- ProgramDatabase
-
-
- ws2_32.lib;%(AdditionalDependencies)
- true
-
-
-
-
- $(IntDir)ctaocrypt\
-
-
- $(IntDir)ctaocrypt\
-
-
- $(IntDir)ctaocrypt\
-
-
- $(IntDir)ctaocrypt\
-
-
- $(IntDir)ctaocrypt\
-
-
- $(IntDir)ctaocrypt\
-
-
- $(IntDir)ctaocrypt\
-
-
- $(IntDir)ctaocrypt\
-
-
- $(IntDir)ctaocrypt\
-
-
- $(IntDir)ctaocrypt\
- false
- false
- false
- false
-
-
- $(IntDir)ctaocrypt\
-
-
- $(IntDir)ctaocrypt\
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- false
- false
- ml64.exe /c /Zi /Fo"$(IntDir)%(Filename).obj" %(Identity)
- ml64.exe /c /Zi /Fo"$(IntDir)%(Filename).obj" %(Identity)
- $(IntDir)%(Filename).obj
- $(IntDir)%(Filename).obj
- false
- false
- ml64.exe /c /Zi /Fo"$(IntDir)%(Filename).obj" %(Identity)
- ml64.exe /c /Zi /Fo"$(IntDir)%(Filename).obj" %(Identity)
- $(IntDir)%(Filename).obj
- $(IntDir)%(Filename).obj
-
-
-
-
-
-
-
-
-
diff --git a/client/wolfssl/IDE/WIN10/README.txt b/client/wolfssl/IDE/WIN10/README.txt
deleted file mode 100644
index c748bc5..0000000
--- a/client/wolfssl/IDE/WIN10/README.txt
+++ /dev/null
@@ -1,70 +0,0 @@
-# Notes on the wolfssl-fips project
-
-First, if you did not get the FIPS files with your archive, you must contact
-wolfSSL to obtain them.
-
-
-# Building the wolfssl-fips project
-
-The wolfCrypt FIPS library for Windows is a part of the wolfSSL library. It
-must be built as a static library, for the moment.
-
-The library project is built with Whole Program Optimization disabled. This is
-required so that necessary components of the library are not optimized away.
-There are two functions added to the library that are used as markers in
-memory for the in-core memory check of the code. WPO consolidates them into a
-single function. WPO also optimizes away the automatic FIPS entry function.
-
-Each of the source files inside the FIPS boundary defines their own code and
-constant section. The code section names start with ".fipsA$" and the constant
-section names start with ".fipsB$". Each subsection has a letter to organize
-them in a specific order. This specific ordering puts marker functions and
-constants on either end of the boundary so it can be hashed.
-
-
-# In Core Memory Test
-
-The In Core Memory test calculates a checksum (HMAC-SHA256) of the wolfCrypt
-FIPS library code and constant data and compares it with a known value in
-the code.
-
-The Randomized Base Address setting needs to be disabled on the 32-bit builds
-but can be enabled on the 64-bit builds. In the 32-bit mode the addresses
-being different throws off the in-core memory calculation. It looks like in
-64-bit mode the library uses all offsets, so the core hash calculation
-is the same every time.
-
-The "verifyCore" check value in the source fips_test.c needs to be updated when
-building the code. The POS performs this check and the default failure callback
-will print out the calculated checksum. When developing your code, copy this
-value and paste it back into your code in the verifyCore initializer then
-rebuild the code. When statically linking, you may have to recalculate your
-check value when changing your application.
-
-
-# Build Options
-
-The default build options should be the proper default set of options:
-
- * HAVE_FIPS
- * HAVE_THREAD_LS
- * HAVE_AESGCM
- * HAVE_HASHDRBG
- * WOLFSSL_SHA384
- * WOLFSSL_SHA512
- * NO_HC128
- * NO_RC4
- * NO_RABBIT
- * NO_DSA
- * NO_MD4
-
-The "NO" options explicitly disable algorithms that are not allowed in
-FIPS mode.
-
-Additionally one may enable:
-
- * HAVE_ECC
- * OPENSSL_EXTRA
- * WOLFSSL_KEY_GEN
-
-These settings are defined in IDE/WIN/user_settings.h.
diff --git a/client/wolfssl/IDE/WIN10/include.am b/client/wolfssl/IDE/WIN10/include.am
deleted file mode 100644
index 3115fb2..0000000
--- a/client/wolfssl/IDE/WIN10/include.am
+++ /dev/null
@@ -1,9 +0,0 @@
-# vim:ft=automake
-# included from Top Level Makefile.am
-# All paths should be given relative to the root
-
-EXTRA_DIST+= IDE/WIN10/README.txt
-EXTRA_DIST+= IDE/WIN10/test.vcxproj
-EXTRA_DIST+= IDE/WIN10/wolfssl-fips.sln
-EXTRA_DIST+= IDE/WIN10/wolfssl-fips.vcxproj
-EXTRA_DIST+= IDE/WIN10/user_settings.h
diff --git a/client/wolfssl/IDE/WIN10/test.vcxproj b/client/wolfssl/IDE/WIN10/test.vcxproj
deleted file mode 100644
index 2aeb273..0000000
--- a/client/wolfssl/IDE/WIN10/test.vcxproj
+++ /dev/null
@@ -1,277 +0,0 @@
-
-
-
-
- Debug
- Win32
-
-
- Debug
- x64
-
-
- DLL Debug
- Win32
-
-
- DLL Debug
- x64
-
-
- DLL Release
- Win32
-
-
- DLL Release
- x64
-
-
- Release
- Win32
-
-
- Release
- x64
-
-
-
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}
- Win32Proj
-
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
- Application
- v110
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <_ProjectFileVersion>11.0.61030.0
-
-
- $(SolutionDir)$(Configuration)\$(Platform)\
- $(Configuration)\$(Platform)\obj\
- false
-
-
-
- Disabled
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;_DEBUG;_CONSOLE;HAVE_FIPS;HAVE_FIPS_VERSION=2;USE_CERT_BUFFERS_2048;USE_CERT_BUFFERS_256;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
-
- Level3
- ProgramDatabase
-
-
- true
- Console
- MachineX86
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- false
-
-
-
-
- Disabled
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;_DEBUG;_CONSOLE;HAVE_FIPS;HAVE_FIPS_VERSION=2;USE_CERT_BUFFERS_2048;USE_CERT_BUFFERS_256;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
-
- Level3
- ProgramDatabase
-
-
- true
- Console
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
-
-
-
-
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;NDEBUG;_CONSOLE;HAVE_FIPS;HAVE_FIPS_VERSION=2;USE_CERT_BUFFERS_2048;USE_CERT_BUFFERS_256;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
-
- Level3
- ProgramDatabase
- true
-
-
- true
- Console
- MachineX86
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
- true
- UseLinkTimeCodeGeneration
- false
-
-
-
-
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;NDEBUG;_CONSOLE;HAVE_FIPS;HAVE_FIPS_VERSION=2;USE_CERT_BUFFERS_2048;USE_CERT_BUFFERS_256;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
-
- Level3
- ProgramDatabase
- true
-
-
- true
- Console
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
- true
- UseLinkTimeCodeGeneration
-
-
-
-
- Disabled
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;_DEBUG;_CONSOLE;HAVE_FIPS;HAVE_FIPS_VERSION=2;USE_CERT_BUFFERS_2048;USE_CERT_BUFFERS_256;WOLFSSL_USER_SETTINGS;WOLFSSL_DLL;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
-
- Level3
- ProgramDatabase
- false
-
-
- true
- Console
- MachineX86
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
-
-
-
-
- Disabled
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;_DEBUG;_CONSOLE;HAVE_FIPS;HAVE_FIPS_VERSION=2;USE_CERT_BUFFERS_2048;USE_CERT_BUFFERS_256;WOLFSSL_USER_SETTINGS;CYASSL_USER_SETTINGS;CYASSL_DLL;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
-
- Level3
- ProgramDatabase
- false
-
-
- true
- Console
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- false
-
-
-
-
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;NDEBUG;_CONSOLE;HAVE_FIPS;HAVE_FIPS_VERSION=2;USE_CERT_BUFFERS_2048;USE_CERT_BUFFERS_256;WOLFSSL_USER_SETTINGS;WOLFSSL_DLL;%(PreprocessorDefinitions)
- MultiThreadedDLL
-
- Level3
- ProgramDatabase
-
-
- true
- Console
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
- true
- MachineX86
- UseLinkTimeCodeGeneration
- true
-
-
-
-
- .\;..\..\;%(AdditionalIncludeDirectories)
- WIN32;NDEBUG;_CONSOLE;HAVE_FIPS;HAVE_FIPS_VERSION=2;USE_CERT_BUFFERS_2048;USE_CERT_BUFFERS_256;WOLFSSL_USER_SETTINGS;WOLFSSL_DLL;%(PreprocessorDefinitions)
- MultiThreadedDLL
-
- Level3
- ProgramDatabase
-
-
- true
- Console
- ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
- true
- true
- UseLinkTimeCodeGeneration
- true
-
-
-
-
-
-
-
- {73973223-5ee8-41ca-8e88-1d60e89a237b}
- false
-
-
-
-
-
-
diff --git a/client/wolfssl/IDE/WIN10/user_settings.h b/client/wolfssl/IDE/WIN10/user_settings.h
deleted file mode 100644
index 4b106cf..0000000
--- a/client/wolfssl/IDE/WIN10/user_settings.h
+++ /dev/null
@@ -1,81 +0,0 @@
-#ifndef _WIN_USER_SETTINGS_H_
-#define _WIN_USER_SETTINGS_H_
-
-/* Verify this is Windows */
-#ifndef _WIN32
-#error This user_settings.h header is only designed for Windows
-#endif
-
-/* Configurations */
-#if defined(HAVE_FIPS)
- /* FIPS */
- #define OPENSSL_EXTRA
- #define HAVE_THREAD_LS
- #define WOLFSSL_KEY_GEN
- #define HAVE_AESGCM
- #define HAVE_HASHDRBG
- #define WOLFSSL_SHA384
- #define WOLFSSL_SHA512
- #define NO_PSK
- #define NO_HC128
- #define NO_RC4
- #define NO_RABBIT
- #define NO_DSA
- #define NO_MD4
-
- #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
- #define WOLFSSL_SHA224
- #define WOLFSSL_SHA3
- #define WC_RSA_PSS
- #define WC_RSA_NO_PADDING
- #define HAVE_ECC
- #define ECC_SHAMIR
- #define HAVE_ECC_CDH
- #define ECC_TIMING_RESISTANT
- #define TFM_TIMING_RESISTANT
- #define WOLFSSL_AES_COUNTER
- #define WOLFSSL_AES_DIRECT
- #define HAVE_AES_ECB
- #define HAVE_AESCCM
- #define WOLFSSL_CMAC
- #define HAVE_HKDF
- #define WOLFSSL_VALIDATE_ECC_IMPORT
- #define WOLFSSL_VALIDATE_FFC_IMPORT
- #define HAVE_FFDHE_Q
- #define WOLFSSL_AESNI
- #define HAVE_INTEL_RDSEED
- #define FORCE_FAILURE_RDSEED
- #endif /* FIPS v2 */
-#else
- /* Enables blinding mode, to prevent timing attacks */
- #define WC_RSA_BLINDING
-
- #if defined(WOLFSSL_LIB)
- /* The lib */
- #define OPENSSL_EXTRA
- #define WOLFSSL_RIPEMD
- #define WOLFSSL_SHA512
- #define NO_PSK
- #define HAVE_EXTENDED_MASTER
- #define WOLFSSL_SNIFFER
- #define HAVE_TLS_EXTENSIONS
- #define HAVE_SECURE_RENEGOTIATION
-
- #define HAVE_AESGCM
- #define WOLFSSL_SHA384
- #define WOLFSSL_SHA512
-
- #define HAVE_SUPPORTED_CURVES
- #define HAVE_TLS_EXTENSIONS
-
- #define HAVE_ECC
- #define ECC_SHAMIR
- #define ECC_TIMING_RESISTANT
- #else
- /* The servers and clients */
- #define OPENSSL_EXTRA
- #define NO_PSK
- #endif
-#endif /* HAVE_FIPS */
-
-#endif /* _WIN_USER_SETTINGS_H_ */
diff --git a/client/wolfssl/IDE/WIN10/wolfssl-fips.sln b/client/wolfssl/IDE/WIN10/wolfssl-fips.sln
deleted file mode 100644
index 7f53706..0000000
--- a/client/wolfssl/IDE/WIN10/wolfssl-fips.sln
+++ /dev/null
@@ -1,56 +0,0 @@
-
-Microsoft Visual Studio Solution File, Format Version 10.00
-# Visual Studio Express 2012 for Windows Desktop
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "wolfssl-fips", "wolfssl-fips.vcxproj", "{73973223-5EE8-41CA-8E88-1D60E89A237B}"
-EndProject
-Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test.vcxproj", "{D04BDF66-664A-4D59-BEAC-8AB2D5809C21}"
-EndProject
-Global
- GlobalSection(SolutionConfigurationPlatforms) = preSolution
- Debug|Win32 = Debug|Win32
- Debug|x64 = Debug|x64
- DLL Debug|Win32 = DLL Debug|Win32
- DLL Debug|x64 = DLL Debug|x64
- DLL Release|Win32 = DLL Release|Win32
- DLL Release|x64 = DLL Release|x64
- Release|Win32 = Release|Win32
- Release|x64 = Release|x64
- EndGlobalSection
- GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Debug|Win32.ActiveCfg = Debug|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Debug|Win32.Build.0 = Debug|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Debug|x64.ActiveCfg = Debug|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Debug|x64.Build.0 = Debug|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Debug|Win32.ActiveCfg = DLL Debug|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Debug|Win32.Build.0 = DLL Debug|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Debug|x64.ActiveCfg = DLL Debug|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Debug|x64.Build.0 = DLL Debug|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Release|Win32.ActiveCfg = DLL Release|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Release|Win32.Build.0 = DLL Release|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Release|x64.ActiveCfg = DLL Release|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.DLL Release|x64.Build.0 = DLL Release|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Release|Win32.ActiveCfg = Release|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Release|Win32.Build.0 = Release|Win32
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Release|x64.ActiveCfg = Release|x64
- {73973223-5EE8-41CA-8E88-1D60E89A237B}.Release|x64.Build.0 = Release|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Debug|Win32.ActiveCfg = Debug|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Debug|Win32.Build.0 = Debug|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Debug|x64.ActiveCfg = Debug|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Debug|x64.Build.0 = Debug|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Debug|Win32.ActiveCfg = DLL Debug|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Debug|Win32.Build.0 = DLL Debug|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Debug|x64.ActiveCfg = DLL Debug|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Debug|x64.Build.0 = DLL Debug|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Release|Win32.ActiveCfg = DLL Release|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Release|Win32.Build.0 = DLL Release|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Release|x64.ActiveCfg = DLL Release|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.DLL Release|x64.Build.0 = DLL Release|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Release|Win32.ActiveCfg = Release|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Release|Win32.Build.0 = Release|Win32
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Release|x64.ActiveCfg = Release|x64
- {D04BDF66-664A-4D59-BEAC-8AB2D5809C21}.Release|x64.Build.0 = Release|x64
- EndGlobalSection
- GlobalSection(SolutionProperties) = preSolution
- HideSolutionNode = FALSE
- EndGlobalSection
-EndGlobal
diff --git a/client/wolfssl/IDE/WIN10/wolfssl-fips.vcxproj b/client/wolfssl/IDE/WIN10/wolfssl-fips.vcxproj
deleted file mode 100644
index 4a1d44f..0000000
--- a/client/wolfssl/IDE/WIN10/wolfssl-fips.vcxproj
+++ /dev/null
@@ -1,308 +0,0 @@
-
-
-
-
- Debug
- Win32
-
-
- Debug
- x64
-
-
- DLL Debug
- Win32
-
-
- DLL Debug
- x64
-
-
- DLL Release
- Win32
-
-
- DLL Release
- x64
-
-
- Release
- Win32
-
-
- Release
- x64
-
-
-
- {73973223-5EE8-41CA-8E88-1D60E89A237B}
- wolfssl-fips
- Win32Proj
-
-
-
- StaticLibrary
- v110
- Unicode
- true
-
-
- DynamicLibrary
- v110
- Unicode
- true
-
-
- StaticLibrary
- v110
- Unicode
- true
-
-
- DynamicLibrary
- v110
- Unicode
- true
-
-
- StaticLibrary
- v110
- Unicode
-
-
- DynamicLibrary
- v110
- Unicode
-
-
- StaticLibrary
- v110
- Unicode
-
-
- DynamicLibrary
- v110
- Unicode
- true
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- $(SolutionDir)$(Configuration)\$(Platform)\
- $(Configuration)\$(Platform)\obj\
-
-
-
- Disabled
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;HAVE_FIPS_VERSION=2;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
- Level4
- ProgramDatabase
- 4206;4214;4706;%(DisableSpecificWarnings)
-
-
-
-
- Disabled
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;HAVE_FIPS_VERSION=2;BUILDING_WOLFSSL;WOLFSSL_DLL;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- true
- EnableFastChecks
- MultiThreadedDebugDLL
- Level4
- ProgramDatabase
- 4206;4214;4706;%(DisableSpecificWarnings)
-
-
- ws2_32.lib;%(AdditionalDependencies)
- 0x5A000000
- false
- false
-
-
-
-
- Disabled
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;HAVE_FIPS_VERSION=2;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- EnableFastChecks
- MultiThreadedDebugDLL
- Level4
- ProgramDatabase
- 4206;4214;4706;%(DisableSpecificWarnings)
-
-
-
-
- Disabled
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;HAVE_FIPS_VERSION=2;BUILDING_WOLFSSL;WOLFSSL_DLL;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- true
- EnableFastChecks
- MultiThreadedDebugDLL
- Level4
- ProgramDatabase
- 4206;4214;4706;%(DisableSpecificWarnings)
-
-
- ws2_32.lib;%(AdditionalDependencies)
- false
- false
-
-
-
-
- MaxSpeed
- true
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;HAVE_FIPS_VERSION=2;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
- true
- Level3
- ProgramDatabase
- false
-
-
-
-
- MaxSpeed
- true
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;HAVE_FIPS_VERSION=2;BUILDING_WOLFSSL;WOLFSSL_DLL;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
- true
- Level3
- ProgramDatabase
-
-
- ws2_32.lib;%(AdditionalDependencies)
- false
- 0x5A000000
-
-
-
-
- MaxSpeed
- true
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;HAVE_FIPS_VERSION=2;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
- true
- Level3
- ProgramDatabase
- false
-
-
-
-
- MaxSpeed
- true
- ./;../../;%(AdditionalIncludeDirectories)
- HAVE_FIPS;HAVE_FIPS_VERSION=2;BUILDING_WOLFSSL;WOLFSSL_DLL;WOLFSSL_USER_SETTINGS;%(PreprocessorDefinitions)
- MultiThreadedDLL
- true
- Level3
- ProgramDatabase
-
-
- ws2_32.lib;%(AdditionalDependencies)
- true
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- false
- false
- false
- false
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- false
- false
- ml64.exe /DHAVE_FIPS /DHAVE_FIPS_VERSION=2 /c /Zi /Fo"$(IntDir)%(Filename).obj" %(Identity)
- ml64.exe /DHAVE_FIPS /DHAVE_FIPS_VERSION=2 /c /Zi /Fo"$(IntDir)%(Filename).obj" %(Identity)
- $(IntDir)%(Filename).obj
- $(IntDir)%(Filename).obj
- false
- false
- ml64.exe /DHAVE_FIPS /DHAVE_FIPS_VERSION=2 /c /Zi /Fo"$(IntDir)%(Filename).obj" %(Identity)
- ml64.exe /DHAVE_FIPS /DHAVE_FIPS_VERSION=2 /c /Zi /Fo"$(IntDir)%(Filename).obj" %(Identity)
- $(IntDir)%(Filename).obj
- $(IntDir)%(Filename).obj
-
-
-
-
-
-
-
-
-
diff --git a/client/wolfssl/LICENSING b/client/wolfssl/LICENSING
deleted file mode 100644
index 9f50165..0000000
--- a/client/wolfssl/LICENSING
+++ /dev/null
@@ -1,11 +0,0 @@
-
-wolfSSL (formerly known as CyaSSL) and wolfCrypt are either licensed for use
-under the GPLv2 or a standard commercial license. For our users who cannot use
-wolfSSL under GPLv2, a commercial license to wolfSSL and wolfCrypt is available.
-Please contact wolfSSL Inc. directly at:
-
-Email: licensing@wolfssl.com
-Phone: +1 425 245-8247
-
-More information can be found on the wolfSSL website at www.wolfssl.com.
-
diff --git a/client/wolfssl/src/bio.c b/client/wolfssl/src/bio.c
deleted file mode 100644
index c134798..0000000
--- a/client/wolfssl/src/bio.c
+++ /dev/null
@@ -1,1705 +0,0 @@
-/* bio.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-#include
-
-#if !defined(WOLFSSL_BIO_INCLUDED)
- #ifndef WOLFSSL_IGNORE_FILE_WARN
- #warning bio.c does not need to be compiled separately from ssl.c
- #endif
-#else
-
-
-/* Helper function to decode a base64 input
- *
- * returns size of resulting buffer on success
- */
-static int wolfSSL_BIO_BASE64_read(WOLFSSL_BIO* bio, void* buf, int len)
-{
- word32 frmtSz = len;
-
- WOLFSSL_ENTER("wolfSSL_BIO_BASE64_read");
-
- if (Base64_Decode((const byte*)buf, (word32)len, (byte*)buf, &frmtSz) !=0) {
- WOLFSSL_MSG("Err doing base64 decode");
- return SSL_FATAL_ERROR;
- }
-
- (void)bio;
- return (int)frmtSz;
-}
-
-
-/* Helper function to read from WOLFSSL_BIO_BIO type
- *
- * returns amount in bytes read on success
- */
-static int wolfSSL_BIO_BIO_read(WOLFSSL_BIO* bio, void* buf, int len)
-{
- int sz;
- char* pt;
-
- sz = wolfSSL_BIO_nread(bio, &pt, len);
-
- if (sz > 0) {
- XMEMCPY(buf, pt, sz);
- }
-
- return sz;
-}
-
-
-/* Handles reading from a memory type BIO and advancing the state.
- *
- * bio WOLFSSL_BIO to read from
- * buf buffer to put data from bio in
- * len amount of data to be read
- *
- * returns size read on success
- */
-static int wolfSSL_BIO_MEMORY_read(WOLFSSL_BIO* bio, void* buf, int len)
-{
- int sz;
- WOLFSSL_ENTER("wolfSSL_BIO_MEMORY_read");
-
- sz = wolfSSL_BIO_pending(bio);
- if (sz > 0) {
- const unsigned char* pt = NULL;
- int memSz;
-
- if (sz > len) {
- sz = len;
- }
- memSz = wolfSSL_BIO_get_mem_data(bio, (void*)&pt);
- if (memSz >= sz && pt != NULL) {
- byte* tmp;
-
- XMEMCPY(buf, (void*)pt, sz);
- if (memSz - sz > 0) {
- tmp = (byte*)XMALLOC(memSz-sz, bio->heap, DYNAMIC_TYPE_OPENSSL);
- if (tmp == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_BIO_ERROR;
- }
- XMEMCPY(tmp, (void*)(pt + sz), memSz - sz);
-
- /* reset internal bio->mem */
- XFREE(bio->ptr, bio->heap, DYNAMIC_TYPE_OPENSSL);
- bio->ptr = tmp;
- bio->num = memSz-sz;
- if (bio->mem_buf != NULL) {
- bio->mem_buf->data = (char*)bio->ptr;
- bio->mem_buf->length = bio->num;
- }
- }
- bio->wrSz -= sz;
- }
- else {
- WOLFSSL_MSG("Issue with getting bio mem pointer");
- return 0;
- }
- }
- else {
- return WOLFSSL_BIO_ERROR;
- }
-
- return sz;
-}
-
-#ifndef WOLFCRYPT_ONLY
-/* Helper function to read from WOLFSSL_BIO_SSL type
- *
- * returns the number of bytes read on success
- */
-static int wolfSSL_BIO_SSL_read(WOLFSSL_BIO* bio, void* buf,
- int len, WOLFSSL_BIO* front)
-{
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_BIO_SSL_read");
-
- /* already got eof, again is error */
- if ((front == NULL) || front->eof)
- return WOLFSSL_FATAL_ERROR;
-
- bio->flags &= ~(WOLFSSL_BIO_FLAG_RETRY); /* default no retry */
- ret = wolfSSL_read((WOLFSSL*)bio->ptr, buf, len);
- if (ret == 0)
- front->eof = 1;
- else if (ret < 0) {
- int err = wolfSSL_get_error((WOLFSSL*)bio->ptr, 0);
- if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) ) {
- front->eof = 1;
- }
- else {
- bio->flags |= WOLFSSL_BIO_FLAG_RETRY; /* should retry */
- }
- }
-
- return ret;
-}
-
-static int wolfSSL_BIO_MD_read(WOLFSSL_BIO* bio, void* buf, int sz)
-{
- int ret = sz;
-
- if (wolfSSL_EVP_MD_CTX_type((WOLFSSL_EVP_MD_CTX*)bio->ptr) == NID_hmac) {
- if (wolfSSL_EVP_DigestSignUpdate((WOLFSSL_EVP_MD_CTX*)bio->ptr, buf,
- sz) != WOLFSSL_SUCCESS)
- {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- else {
- if (wolfSSL_EVP_DigestUpdate((WOLFSSL_EVP_MD_CTX*)bio->ptr, buf, ret)
- != WOLFSSL_SUCCESS) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- return ret;
-}
-#endif /* WOLFCRYPT_ONLY */
-
-
-/* Used to read data from a WOLFSSL_BIO structure
- *
- * bio structure to read data from
- * buf buffer to hold the result
- * len length of buf buffer
- *
- * returns the number of bytes read on success
- */
-int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len)
-{
- int ret = 0;
- WOLFSSL_BIO* front = bio;
- int sz = 0;
-
- WOLFSSL_ENTER("wolfSSL_BIO_read");
-
- /* info cb, abort if user returns <= 0*/
- if (front != NULL && front->infoCb != NULL) {
- ret = (int)front->infoCb(front, WOLFSSL_BIO_CB_READ, (const char*)buf,
- len, 0, 1);
- if (ret <= 0) {
- return ret;
- }
- }
-
- /* start at end of list and work backwards */
- while ((bio != NULL) && (bio->next != NULL)) {
- bio = bio->next;
- }
-
- while (bio != NULL && ret >= 0) {
- /* check for custom read */
- if (bio->method && bio->method->readCb) {
- ret = bio->method->readCb(bio, (char*)buf, len);
- }
-
- /* formatting data */
- if (bio->type == WOLFSSL_BIO_BASE64 && ret > 0 && sz > 0) {
- ret = wolfSSL_BIO_BASE64_read(bio, buf, sz);
- }
-
- /* write BIOs */
- if (bio && bio->type == WOLFSSL_BIO_BIO) {
- ret = wolfSSL_BIO_BIO_read(bio, buf, len);
- }
-
- if (bio && bio->type == WOLFSSL_BIO_MEMORY) {
- ret = wolfSSL_BIO_MEMORY_read(bio, buf, len);
- }
-
- #ifndef NO_FILESYSTEM
- if (bio && bio->type == WOLFSSL_BIO_FILE) {
- ret = (int)XFREAD(buf, 1, len, (XFILE)bio->ptr);
- }
- #endif
-
- #ifndef WOLFCRYPT_ONLY
- if (bio && bio->type == WOLFSSL_BIO_SSL) {
- ret = wolfSSL_BIO_SSL_read(bio, buf, len, front);
- }
-
- /* data passing through BIO MD wrapper */
- if (bio && bio->type == WOLFSSL_BIO_MD && ret > 0) {
- ret = wolfSSL_BIO_MD_read(bio, buf, ret);
- }
- #endif
-
- /* case where front of list is done */
- if (bio == front) {
- break; /* at front of list so be done */
- }
-
- if (ret > 0) {
- sz = ret; /* adjust size for formatting */
- }
-
- /* previous WOLFSSL_BIO in list working towards head of list */
- bio = bio->prev;
- }
-
- /* info cb, user can override return value */
- if (front != NULL && front->infoCb != NULL) {
- ret = (int)front->infoCb(front,
- WOLFSSL_BIO_CB_READ | WOLFSSL_BIO_CB_RETURN,
- (const char*)buf, len, 0, ret);
- }
-
- return ret;
-}
-
-
-/* Converts data into base64 output
- *
- * returns the resulting buffer size on success.
- */
-static int wolfSSL_BIO_BASE64_write(WOLFSSL_BIO* bio, const void* data,
- word32 inLen, byte* out, word32* outLen)
-{
- byte* tmp = NULL;
- int ret = 0;
-
- WOLFSSL_ENTER("wolfSSL_BIO_BASE64_write");
-
- if (bio == NULL || data == NULL || out == NULL || outLen == NULL) {
- return BAD_FUNC_ARG;
- }
-
-#if defined(WOLFSSL_BASE64_ENCODE)
- tmp = (byte*)XMALLOC(*outLen, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmp == NULL) {
- return WOLFSSL_FATAL_ERROR;
- }
-
- if ((bio->flags & WOLFSSL_BIO_FLAG_BASE64_NO_NL) ==
- WOLFSSL_BIO_FLAG_BASE64_NO_NL) {
- if (Base64_Encode_NoNl((const byte*)data, inLen,
- tmp, outLen) < 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- else {
- if (Base64_Encode((const byte*)data, inLen,
- tmp, outLen) < 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
-
- if (ret != WOLFSSL_FATAL_ERROR) {
- ret = (int) inLen;
- XMEMCPY(out, tmp, *outLen);
-
- }
- XFREE(tmp, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#else
- (void)bio;
- (void)data;
- (void)inLen;
- (void)out;
- (void)outLen;
- (void)tmp;
- WOLFSSL_MSG("BASE64 encoding not compiled in");
-#endif
- return ret;
-}
-
-
-#ifndef WOLFCRYPT_ONLY
-/* Helper function for writing to a WOLFSSL_BIO_SSL type
- *
- * returns the amount written in bytes on success
- */
-static int wolfSSL_BIO_SSL_write(WOLFSSL_BIO* bio, const void* data,
- int len, WOLFSSL_BIO* front)
-{
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_BIO_SSL_write");
-
- if (bio->ptr == NULL) {
- return BAD_FUNC_ARG;
- }
-
- bio->flags &= ~(WOLFSSL_BIO_FLAG_RETRY); /* default no retry */
- ret = wolfSSL_write((WOLFSSL*)bio->ptr, data, len);
- if (ret == 0)
- front->eof = 1;
- else if (ret < 0) {
- int err = wolfSSL_get_error((WOLFSSL*)bio->ptr, 0);
- if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) ) {
- front->eof = 1;
- }
- else {
- bio->flags |= WOLFSSL_BIO_FLAG_RETRY; /* should retry */
- }
- }
- return ret;
-}
-#endif /* WOLFCRYPT_ONLY */
-
-
-/* Writes to a WOLFSSL_BIO_BIO type.
- *
- * returns the amount written on success
- */
-static int wolfSSL_BIO_BIO_write(WOLFSSL_BIO* bio, const void* data,
- int len)
-{
- int sz;
- char* buf;
-
- WOLFSSL_ENTER("wolfSSL_BIO_BIO_write");
-
- /* adding in sanity checks for static analysis tools */
- if (bio == NULL || data == NULL) {
- return BAD_FUNC_ARG;
- }
-
- sz = wolfSSL_BIO_nwrite(bio, &buf, len);
-
- /* test space for write */
- if (sz <= 0) {
- WOLFSSL_MSG("No room left to write");
- return sz;
- }
-
- XMEMCPY(buf, data, sz);
-
- return sz;
-}
-
-
-/* for complete compatibility a bio memory write allocs its own memory
- * until the application runs out ....
- *
- * bio structure to hold incoming data
- * data buffer holding the data to be written
- * len length of data buffer
- *
- * returns the amount of data written on success and WOLFSSL_FAILURE or
- * WOLFSSL_BIO_ERROR for failure cases.
- */
-static int wolfSSL_BIO_MEMORY_write(WOLFSSL_BIO* bio, const void* data,
- int len)
-{
- int sz;
- const unsigned char* buf;
-
- WOLFSSL_ENTER("wolfSSL_BIO_MEMORY_write");
-
- if (bio == NULL || data == NULL) {
- return BAD_FUNC_ARG;
- }
-
- sz = wolfSSL_BIO_pending(bio);
- if (sz < 0) {
- WOLFSSL_MSG("Error getting memory data");
- return sz;
- }
-
- if (bio->ptr == NULL) {
- bio->ptr = (byte*)XMALLOC(len, bio->heap, DYNAMIC_TYPE_OPENSSL);
- if (bio->ptr == NULL) {
- WOLFSSL_MSG("Error on malloc");
- return WOLFSSL_FAILURE;
- }
- bio->num = len;
- if (bio->mem_buf != NULL) {
- bio->mem_buf->data = (char*)bio->ptr;
- bio->mem_buf->length = bio->num;
- }
- }
-
- /* check if will fit in current buffer size */
- if (wolfSSL_BIO_get_mem_data(bio, (void*)&buf) < 0) {
- return WOLFSSL_BIO_ERROR;
- }
- if (bio->num < sz + len) {
- bio->ptr = (byte*)XREALLOC(bio->ptr, sz + len, bio->heap,
- DYNAMIC_TYPE_OPENSSL);
- if (bio->ptr == NULL) {
- WOLFSSL_MSG("Error on realloc");
- return WOLFSSL_FAILURE;
- }
- bio->num = sz + len;
- if (bio->mem_buf != NULL) {
- bio->mem_buf->data = (char*)bio->ptr;
- bio->mem_buf->length = bio->num;
- }
- }
-
- XMEMCPY((byte*)bio->ptr + sz, data, len);
- bio->wrSz += len;
-
- return len;
-}
-
-
-#ifndef WOLFCRYPT_ONLY
-/* Helper function for writing to a WOLFSSL_BIO_MD type
- *
- * returns the amount written in bytes on success (0)
- */
-static int wolfSSL_BIO_MD_write(WOLFSSL_BIO* bio, const void* data, int len)
-{
- int ret = 0;
-
- if (bio == NULL || data == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (wolfSSL_EVP_MD_CTX_type((WOLFSSL_EVP_MD_CTX*)bio->ptr) == NID_hmac) {
- if (wolfSSL_EVP_DigestSignUpdate((WOLFSSL_EVP_MD_CTX*)bio->ptr, data,
- len) != WOLFSSL_SUCCESS) {
- ret = WOLFSSL_BIO_ERROR;
- }
- }
- else {
- if (wolfSSL_EVP_DigestUpdate((WOLFSSL_EVP_MD_CTX*)bio->ptr, data, len)
- != WOLFSSL_SUCCESS) {
- ret = WOLFSSL_BIO_ERROR;
- }
- }
- return ret;
-}
-#endif /* WOLFCRYPT_ONLY */
-
-
-/* Writes data to a WOLFSSL_BIO structure
- *
- * bio structure to write to
- * data holds the data to be written
- * len length of data buffer
- *
- * returns the amount written in bytes on success
- */
-int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len)
-{
- int ret = 0;
- int retB64 = 0;
- WOLFSSL_BIO* front = bio;
- void* frmt = NULL;
- word32 frmtSz = 0;
-
- WOLFSSL_ENTER("wolfSSL_BIO_write");
-
- /* info cb, abort if user returns <= 0*/
- if (front != NULL && front->infoCb != NULL) {
- ret = (int)front->infoCb(front, WOLFSSL_BIO_CB_WRITE,
- (const char*)data, len, 0, 1);
- if (ret <= 0) {
- return ret;
- }
- }
-
- while (bio != NULL && ret >= 0) {
- /* check for custom write */
- if (bio->method && bio->method->writeCb) {
- ret = bio->method->writeCb(bio, (const char*)data, len);
- }
-
- /* check for formatting */
- if (bio->type == WOLFSSL_BIO_BASE64) {
-#if defined(WOLFSSL_BASE64_ENCODE)
- word32 sz = 0;
-
- if (bio->flags & WOLFSSL_BIO_FLAG_BASE64_NO_NL) {
- if (Base64_Encode_NoNl((const byte*)data, len, NULL,
- &sz) != LENGTH_ONLY_E) {
- WOLFSSL_MSG("Error with base 64 get length");
- ret = SSL_FATAL_ERROR;
- }
- }
- else {
- if (Base64_Encode((const byte*)data, len, NULL, &sz) !=
- LENGTH_ONLY_E) {
- WOLFSSL_MSG("Error with base 64 get length");
- ret = SSL_FATAL_ERROR;
- }
- }
-
- if (frmt == NULL && sz > 0 && ret != SSL_FATAL_ERROR) {
- frmt = (void*)XMALLOC(sz, front->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (frmt == NULL) {
- WOLFSSL_MSG("Memory error");
- ret = SSL_FATAL_ERROR;
- }
- frmtSz = sz;
- }
- else if (sz > frmtSz) {
- frmt = (void*)XREALLOC(frmt, sz, front->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (frmt == NULL) {
- WOLFSSL_MSG("Memory error");
- ret = SSL_FATAL_ERROR;
- }
- /* since frmt already existed then data should point to knew
- formatted buffer */
- data = frmt;
- len = frmtSz;
- frmtSz = sz;
- }
-#endif /* defined(WOLFSSL_BASE64_ENCODE) */
-
- if (ret >= 0) {
- /* change so that data is formatted buffer */
- retB64 = wolfSSL_BIO_BASE64_write(bio, data, (word32)len,
- (byte*)frmt, &frmtSz);
- data = frmt;
- len = frmtSz;
- }
- }
-
- /* write bios */
- if (bio && bio->type == WOLFSSL_BIO_BIO) {
- ret = wolfSSL_BIO_BIO_write(bio, data, len);
- }
-
- if (bio && bio->type == WOLFSSL_BIO_MEMORY) {
- ret = wolfSSL_BIO_MEMORY_write(bio, data, len);
- }
-
- #ifndef NO_FILESYSTEM
- if (bio && bio->type == WOLFSSL_BIO_FILE) {
- ret = (int)XFWRITE(data, 1, len, (XFILE)bio->ptr);
- }
- #endif
-
- #ifndef WOLFCRYPT_ONLY
- if (bio && bio->type == WOLFSSL_BIO_SSL) {
- /* already got eof, again is error */
- if (front->eof) {
- ret = SSL_FATAL_ERROR;
- }
- else {
- ret = wolfSSL_BIO_SSL_write(bio, data, len, front);
- }
- }
-
- if (bio && bio->type == WOLFSSL_BIO_MD) {
- if (bio->next != NULL) { /* data passing through MD BIO */
- ret = wolfSSL_BIO_MD_write(bio, data, len);
- }
- }
- #endif /* WOLFCRYPT_ONLY */
-
- /* advance to the next bio in list */
- bio = bio->next;
- }
-
- if (frmt != NULL) {
- XFREE(frmt, front->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- /* info cb, user can override return value */
- if (front != NULL && front->infoCb != NULL) {
- ret = (int)front->infoCb(front,
- WOLFSSL_BIO_CB_WRITE | WOLFSSL_BIO_CB_RETURN,
- (const char*)data, 0, 0, ret);
- }
-
- if (retB64 != 0)
- return retB64;
- else
- return ret;
-}
-
-
-/* Wrapper for other BIO type functions, expected to grow as OpenSSL compatibility
- * layer grows.
- *
- * return info. specific to the cmd that is passed in.
- */
-#if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
-long wolfSSL_BIO_ctrl(WOLFSSL_BIO *bio, int cmd, long larg, void *parg)
-{
- long ret;
-
- (void)larg; /* not currently used */
-
- WOLFSSL_ENTER("wolfSSL_BIO_ctrl");
-
- if (bio && bio->method && bio->method->ctrlCb) {
- return bio->method->ctrlCb(bio, cmd, larg, parg);
- }
-
- switch(cmd) {
- case BIO_CTRL_PENDING:
- case BIO_CTRL_WPENDING:
- ret = (long)wolfSSL_BIO_ctrl_pending(bio);
- break;
- case BIO_CTRL_INFO:
- ret = (long)wolfSSL_BIO_get_mem_data(bio, parg);
- break;
- case BIO_CTRL_FLUSH:
- ret = (long)wolfSSL_BIO_flush(bio);
- break;
- case BIO_CTRL_RESET:
- ret = (long)wolfSSL_BIO_reset(bio);
- break;
- default:
- WOLFSSL_MSG("CMD not yet implemented");
- ret = WOLFSSL_FAILURE;
- break;
- }
- return ret;
-}
-#endif
-
-
-/* helper function for wolfSSL_BIO_gets
- * size till a newline is hit
- * returns the number of bytes including the new line character
- */
-static int wolfSSL_getLineLength(char* in, int inSz)
-{
- int i;
-
- for (i = 0; i < inSz; i++) {
- if (in[i] == '\n') {
- return i + 1; /* includes new line character */
- }
- }
-
- return inSz; /* rest of buffer is all one line */
-}
-
-
-/* Gets the next line from bio. Goes until a new line character or end of
- * buffer is reached.
- *
- * bio the structure to read a new line from
- * buf buffer to hold the result
- * sz the size of "buf" buffer
- *
- * returns the size of the result placed in buf on success and a 0 or negative
- * value in an error case.
- */
-int wolfSSL_BIO_gets(WOLFSSL_BIO* bio, char* buf, int sz)
-{
- int ret = WOLFSSL_BIO_UNSET;
-
- WOLFSSL_ENTER("wolfSSL_BIO_gets");
-
- if (bio == NULL || buf == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* not enough space for character plus terminator */
- if (sz <= 1) {
- return 0;
- }
-
- /* info cb, abort if user returns <= 0*/
- if (bio->infoCb != NULL) {
- ret = (int)bio->infoCb(bio, WOLFSSL_BIO_CB_GETS, buf, sz, 0, 1);
- if (ret <= 0) {
- return ret;
- }
- }
-
- /* check if is custom method */
- if (bio->method && bio->method->getsCb) {
- return bio->method->getsCb(bio, buf, sz);
- }
-
- switch (bio->type) {
-#ifndef NO_FILESYSTEM
- case WOLFSSL_BIO_FILE:
- if (((XFILE)bio->ptr) == XBADFILE) {
- return WOLFSSL_BIO_ERROR;
- }
-
- #if defined(MICRIUM) || defined(LSR_FS) || defined(EBSNET)
- WOLFSSL_MSG("XFGETS not ported for this system yet");
- ret = XFGETS(buf, sz, (XFILE)bio->ptr);
- #else
- if (XFGETS(buf, sz, (XFILE)bio->ptr) != NULL) {
- ret = (int)XSTRLEN(buf);
- }
- else {
- ret = WOLFSSL_BIO_ERROR;
- }
- #endif
- break;
-#endif /* NO_FILESYSTEM */
- case WOLFSSL_BIO_MEMORY:
- {
- const byte* c;
- int cSz;
- cSz = wolfSSL_BIO_pending(bio);
- if (cSz == 0) {
- ret = 0; /* Nothing to read */
- buf[0] = '\0';
- break;
- }
-
- if (wolfSSL_BIO_get_mem_data(bio, (void*)&c) <= 0) {
- ret = WOLFSSL_BIO_ERROR;
- break;
- }
-
- cSz = wolfSSL_getLineLength((char*)c, cSz);
- /* check case where line was bigger then buffer and buffer
- * needs end terminator */
- if (cSz >= sz) {
- cSz = sz - 1;
- buf[cSz] = '\0';
- }
- else {
- /* not minus 1 here because placing terminator after
- msg and have checked that sz is large enough */
- buf[cSz] = '\0';
- }
-
- ret = wolfSSL_BIO_MEMORY_read(bio, (void*)buf, cSz);
- /* ret is read after the switch statement */
- break;
- }
- case WOLFSSL_BIO_BIO:
- {
- char* c;
- int cSz;
- cSz = wolfSSL_BIO_nread0(bio, &c);
- if (cSz == 0) {
- ret = 0; /* Nothing to read */
- buf[0] = '\0';
- break;
- }
-
- cSz = wolfSSL_getLineLength(c, cSz);
- /* check case where line was bigger then buffer and buffer
- * needs end terminator */
- if (cSz >= sz) {
- cSz = sz - 1;
- buf[cSz] = '\0';
- }
- else {
- /* not minus 1 here because placing terminator after
- msg and have checked that sz is large enough */
- buf[cSz] = '\0';
- }
-
- ret = wolfSSL_BIO_nread(bio, &c, cSz);
- if (ret > 0 && ret < sz) {
- XMEMCPY(buf, c, ret);
- }
- break;
- }
-
-#ifndef WOLFCRYPT_ONLY
- /* call final on hash */
- case WOLFSSL_BIO_MD:
- if (wolfSSL_EVP_MD_CTX_size((WOLFSSL_EVP_MD_CTX*)bio->ptr) > sz) {
- WOLFSSL_MSG("Output buffer was too small for digest");
- ret = WOLFSSL_FAILURE;
- }
- else {
- unsigned int szOut = 0;
- ret = wolfSSL_EVP_DigestFinal((WOLFSSL_EVP_MD_CTX*)bio->ptr,
- (unsigned char*)buf, &szOut);
- if (ret == WOLFSSL_SUCCESS) {
- ret = szOut;
- }
- }
- break;
-#endif /* WOLFCRYPT_ONLY */
-
- default:
- WOLFSSL_MSG("BIO type not supported yet with wolfSSL_BIO_gets");
- }
-
- /* info cb, user can override return value */
- if (bio->infoCb != NULL) {
- ret = (int)bio->infoCb(bio, WOLFSSL_BIO_CB_GETS | WOLFSSL_BIO_CB_RETURN,
- buf, sz, 0, ret);
- }
-
- return ret;
-}
-
-
-/* Writes a null terminated string to bio.
- *
- * bio the structure to write to
- * buf buffer to holding input string
- *
- * returns the size of the result placed in bio on success and a 0 or negative
- * value in an error case. -2 is returned if the implementation is not
- * supported for the BIO type.
- */
-int wolfSSL_BIO_puts(WOLFSSL_BIO* bio, const char* buf)
-{
- int sz;
-
- if (bio == NULL || buf == NULL) {
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* check if is custom method */
- if (bio->method && bio->method->putsCb) {
- return bio->method->putsCb(bio, buf);
- }
-
- sz = (int)XSTRLEN(buf);
- if (sz <= 0) {
- return WOLFSSL_FATAL_ERROR;
- }
-
- return wolfSSL_BIO_write(bio, buf, sz);
-}
-
-
-/* searches through bio list for a BIO of type "type"
- * returns NULL on failure to find a given type */
-WOLFSSL_BIO* wolfSSL_BIO_find_type(WOLFSSL_BIO* bio, int type)
-{
- WOLFSSL_BIO* local = NULL;
- WOLFSSL_BIO* current;
-
- WOLFSSL_ENTER("wolfSSL_BIO_find_type");
-
- if (bio == NULL) {
- return local;
- }
-
- current = bio;
- while (current != NULL) {
- if (current->type == type) {
- WOLFSSL_MSG("Found matching WOLFSSL_BIO type");
- local = current;
- break;
- }
- current = current->next;
- }
-
- return local;
-}
-
-
-/* returns a pointer to the next WOLFSSL_BIO in the chain on success.
- * If a failure case then NULL is returned */
-WOLFSSL_BIO* wolfSSL_BIO_next(WOLFSSL_BIO* bio)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_next");
-
- if (bio == NULL) {
- WOLFSSL_MSG("Bad argument passed in");
- return NULL;
- }
-
- return bio->next;
-}
-
-/* BIO_wpending returns the number of bytes pending to be written. */
-size_t wolfSSL_BIO_wpending(const WOLFSSL_BIO *bio)
-{
- WOLFSSL_ENTER("BIO_wpending");
-
- if (bio == NULL)
- return 0;
-
- if (bio->type == WOLFSSL_BIO_MEMORY) {
- return bio->wrSz;
- }
-
- /* type BIO_BIO then check paired buffer */
- if (bio->type == WOLFSSL_BIO_BIO && bio->pair != NULL) {
- WOLFSSL_BIO* pair = bio->pair;
- return pair->wrIdx;
- }
-
- return 0;
-}
-
-/* Return the number of pending bytes in read and write buffers */
-size_t wolfSSL_BIO_ctrl_pending(WOLFSSL_BIO *bio)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_ctrl_pending");
- if (bio == NULL) {
- return 0;
- }
-
- if (bio->type == WOLFSSL_BIO_MD) {
- /* MD is a wrapper only get next bio */
- while (bio->next != NULL) {
- bio = bio->next;
- if (bio->type != WOLFSSL_BIO_MD) {
- break;
- }
- }
- }
-
-#ifndef WOLFCRYPT_ONLY
- if (bio->type == WOLFSSL_BIO_SSL && bio->ptr != NULL) {
- return (long)wolfSSL_pending((WOLFSSL*)bio->ptr);
- }
-#endif
-
- if (bio->type == WOLFSSL_BIO_MEMORY) {
- return bio->wrSz;
- }
-
- /* type BIO_BIO then check paired buffer */
- if (bio->type == WOLFSSL_BIO_BIO && bio->pair != NULL) {
- WOLFSSL_BIO* pair = bio->pair;
- if (pair->wrIdx > 0 && pair->wrIdx <= pair->rdIdx) {
- /* in wrap around state where beginning of buffer is being
- * overwritten */
- return pair->wrSz - pair->rdIdx + pair->wrIdx;
- }
- else {
- /* simple case where has not wrapped around */
- return pair->wrIdx - pair->rdIdx;
- }
- }
- return 0;
-}
-
-
-long wolfSSL_BIO_get_mem_ptr(WOLFSSL_BIO *bio, WOLFSSL_BUF_MEM **ptr)
-{
- WOLFSSL_BIO* front = bio;
- long ret = WOLFSSL_FAILURE;
-
- WOLFSSL_ENTER("wolfSSL_BIO_get_mem_ptr");
-
- if (bio == NULL || ptr == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* start at end and work backwards to find a memory BIO in the BIO chain */
- while ((bio != NULL) && (bio->next != NULL)) {
- bio = bio->next;
- }
-
- while (bio != NULL) {
-
- if (bio->type == WOLFSSL_BIO_MEMORY) {
- *ptr = bio->mem_buf;
- ret = WOLFSSL_SUCCESS;
- }
-
- if (bio == front) {
- break;
- }
- bio = bio->prev;
- }
-
- return ret;
-}
-
-WOLFSSL_API long wolfSSL_BIO_int_ctrl(WOLFSSL_BIO *bp, int cmd, long larg, int iarg)
-{
- (void) bp;
- (void) cmd;
- (void) larg;
- (void) iarg;
- WOLFSSL_STUB("BIO_int_ctrl");
- return 0;
-}
-
-
-int wolfSSL_BIO_set_write_buf_size(WOLFSSL_BIO *bio, long size)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_set_write_buf_size");
-
- if (bio == NULL || bio->type != WOLFSSL_BIO_BIO || size < 0) {
- return WOLFSSL_FAILURE;
- }
-
- /* if already in pair then do not change size */
- if (bio->pair != NULL) {
- WOLFSSL_MSG("WOLFSSL_BIO is paired, free from pair before changing");
- return WOLFSSL_FAILURE;
- }
-
- bio->wrSz = (int)size;
- if (bio->wrSz < 0) {
- WOLFSSL_MSG("Unexpected negative size value");
- return WOLFSSL_FAILURE;
- }
-
- if (bio->ptr != NULL) {
- XFREE(bio->ptr, bio->heap, DYNAMIC_TYPE_OPENSSL);
- }
-
- bio->ptr = (byte*)XMALLOC(bio->wrSz, bio->heap, DYNAMIC_TYPE_OPENSSL);
- if (bio->ptr == NULL) {
- WOLFSSL_MSG("Memory allocation error");
- return WOLFSSL_FAILURE;
- }
- bio->num = bio->wrSz;
- bio->wrIdx = 0;
- bio->rdIdx = 0;
- if (bio->mem_buf != NULL) {
- bio->mem_buf->data = (char*)bio->ptr;
- bio->mem_buf->length = bio->num;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* Joins two BIO_BIO types. The write of b1 goes to the read of b2 and vice
- * versa. Creating something similar to a two way pipe.
- * Reading and writing between the two BIOs is not thread safe, they are
- * expected to be used by the same thread. */
-int wolfSSL_BIO_make_bio_pair(WOLFSSL_BIO *b1, WOLFSSL_BIO *b2)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_make_bio_pair");
-
- if (b1 == NULL || b2 == NULL) {
- WOLFSSL_LEAVE("wolfSSL_BIO_make_bio_pair", BAD_FUNC_ARG);
- return WOLFSSL_FAILURE;
- }
-
- /* both are expected to be of type BIO and not already paired */
- if (b1->type != WOLFSSL_BIO_BIO || b2->type != WOLFSSL_BIO_BIO ||
- b1->pair != NULL || b2->pair != NULL) {
- WOLFSSL_MSG("Expected type BIO and not already paired");
- return WOLFSSL_FAILURE;
- }
-
- /* set default write size if not already set */
- if (b1->ptr == NULL && wolfSSL_BIO_set_write_buf_size(b1,
- WOLFSSL_BIO_SIZE) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- if (b2->ptr == NULL && wolfSSL_BIO_set_write_buf_size(b2,
- WOLFSSL_BIO_SIZE) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- b1->pair = b2;
- b2->pair = b1;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_BIO_ctrl_reset_read_request(WOLFSSL_BIO *b)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_ctrl_reset_read_request");
-
- if (b == NULL || b->type == WOLFSSL_BIO_MEMORY) {
- return SSL_FAILURE;
- }
-
- b->readRq = 0;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* Does not advance read index pointer */
-int wolfSSL_BIO_nread0(WOLFSSL_BIO *bio, char **buf)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_nread0");
-
- if (bio == NULL || buf == NULL) {
- WOLFSSL_MSG("NULL argument passed in");
- return 0;
- }
-
- /* if paired read from pair */
- if (bio->pair != NULL) {
- WOLFSSL_BIO* pair = bio->pair;
-
- /* case where have wrapped around write buffer */
- *buf = (char*)pair->ptr + pair->rdIdx;
- if (pair->wrIdx > 0 && pair->rdIdx >= pair->wrIdx) {
- return pair->wrSz - pair->rdIdx;
- }
- else {
- return pair->wrIdx - pair->rdIdx;
- }
- }
-
- return 0;
-}
-
-
-/* similar to wolfSSL_BIO_nread0 but advances the read index */
-int wolfSSL_BIO_nread(WOLFSSL_BIO *bio, char **buf, int num)
-{
- int sz = WOLFSSL_BIO_UNSET;
-
- WOLFSSL_ENTER("wolfSSL_BIO_nread");
-
- if (bio == NULL || buf == NULL) {
- WOLFSSL_MSG("NULL argument passed in");
- return WOLFSSL_FAILURE;
- }
-
- if (bio->type == WOLFSSL_BIO_MEMORY) {
- return SSL_FAILURE;
- }
-
- if (bio->pair != NULL) {
- /* special case if asking to read 0 bytes */
- if (num == 0) {
- *buf = (char*)bio->pair->ptr + bio->pair->rdIdx;
- return 0;
- }
-
- /* get amount able to read and set buffer pointer */
- sz = wolfSSL_BIO_nread0(bio, buf);
- if (sz == 0) {
- return WOLFSSL_BIO_ERROR;
- }
-
- if (num < sz) {
- sz = num;
- }
- bio->pair->rdIdx += sz;
-
- /* check if have read to the end of the buffer and need to reset */
- if (bio->pair->rdIdx == bio->pair->wrSz) {
- bio->pair->rdIdx = 0;
- if (bio->pair->wrIdx == bio->pair->wrSz) {
- bio->pair->wrIdx = 0;
- }
- }
-
- /* check if read up to write index, if so then reset index */
- if (bio->pair->rdIdx == bio->pair->wrIdx) {
- bio->pair->rdIdx = 0;
- bio->pair->wrIdx = 0;
- }
- }
-
- return sz;
-}
-
-
-int wolfSSL_BIO_nwrite(WOLFSSL_BIO *bio, char **buf, int num)
-{
- int sz = WOLFSSL_BIO_UNSET;
-
- WOLFSSL_ENTER("wolfSSL_BIO_nwrite");
-
- if (bio == NULL || buf == NULL) {
- WOLFSSL_MSG("NULL argument passed in");
- return 0;
- }
-
- if (bio->type != WOLFSSL_BIO_BIO) {
- return SSL_FAILURE;
- }
-
- if (bio->pair != NULL) {
- if (num == 0) {
- *buf = (char*)bio->ptr + bio->wrIdx;
- return 0;
- }
-
- if (bio->wrIdx < bio->rdIdx) {
- /* if wrapped around only write up to read index. In this case
- * rdIdx is always greater then wrIdx so sz will not be negative. */
- sz = bio->rdIdx - bio->wrIdx;
- }
- else if (bio->rdIdx > 0 && bio->wrIdx == bio->rdIdx) {
- return WOLFSSL_BIO_ERROR; /* no more room to write */
- }
- else {
- /* write index is past read index so write to end of buffer */
- sz = bio->wrSz - bio->wrIdx;
-
- if (sz <= 0) {
- /* either an error has occurred with write index or it is at the
- * end of the write buffer. */
- if (bio->rdIdx == 0) {
- /* no more room, nothing has been read */
- return WOLFSSL_BIO_ERROR;
- }
-
- bio->wrIdx = 0;
-
- /* check case where read index is not at 0 */
- if (bio->rdIdx > 0) {
- sz = bio->rdIdx; /* can write up to the read index */
- }
- else {
- sz = bio->wrSz; /* no restriction other then buffer size */
- }
- }
- }
-
- if (num < sz) {
- sz = num;
- }
- *buf = (char*)bio->ptr + bio->wrIdx;
- bio->wrIdx += sz;
-
- /* if at the end of the buffer and space for wrap around then set
- * write index back to 0 */
- if (bio->wrIdx == bio->wrSz && bio->rdIdx > 0) {
- bio->wrIdx = 0;
- }
- }
-
- return sz;
-}
-
-
-/* Reset BIO to initial state */
-int wolfSSL_BIO_reset(WOLFSSL_BIO *bio)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_reset");
-
- if (bio == NULL) {
- WOLFSSL_MSG("NULL argument passed in");
- /* -1 is consistent failure even for FILE type */
- return WOLFSSL_BIO_ERROR;
- }
-
- switch (bio->type) {
- #ifndef NO_FILESYSTEM
- case WOLFSSL_BIO_FILE:
- XREWIND((XFILE)bio->ptr);
- return 0;
- #endif
-
- case WOLFSSL_BIO_BIO:
- bio->rdIdx = 0;
- bio->wrIdx = 0;
- return 0;
-
- case WOLFSSL_BIO_MEMORY:
- bio->rdIdx = 0;
- bio->wrIdx = 0;
- bio->wrSz = 0;
- XFREE(bio->ptr, bio->heap, DYNAMIC_TYPE_OPENSSL);
- bio->ptr = NULL;
- bio->num = 0;
- if (bio->mem_buf != NULL) {
- bio->mem_buf->data = (char*)bio->ptr;
- bio->mem_buf->length = bio->num;
- }
- return 0;
-
-#ifndef WOLFCRYPT_ONLY
- case WOLFSSL_BIO_MD:
- if (bio->ptr != NULL) {
- const WOLFSSL_EVP_MD* md =
- wolfSSL_EVP_MD_CTX_md((WOLFSSL_EVP_MD_CTX*)bio->ptr);
- wolfSSL_EVP_MD_CTX_init((WOLFSSL_EVP_MD_CTX*)bio->ptr);
- wolfSSL_EVP_DigestInit((WOLFSSL_EVP_MD_CTX*)bio->ptr, md);
- }
- return 0;
-#endif /* WOLFCRYPT_ONLY */
-
- default:
- WOLFSSL_MSG("Unknown BIO type needs added to reset function");
- }
-
- return WOLFSSL_BIO_ERROR;
-}
-
-#ifndef NO_FILESYSTEM
-long wolfSSL_BIO_set_fp(WOLFSSL_BIO *bio, XFILE fp, int c)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_set_fp");
-
- if (bio == NULL || fp == XBADFILE) {
- WOLFSSL_LEAVE("wolfSSL_BIO_set_fp", BAD_FUNC_ARG);
- return WOLFSSL_FAILURE;
- }
-
- if (bio->type != WOLFSSL_BIO_FILE) {
- return WOLFSSL_FAILURE;
- }
-
- bio->shutdown = (byte)c;
- bio->ptr = (XFILE)fp;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-long wolfSSL_BIO_get_fp(WOLFSSL_BIO *bio, XFILE* fp)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_get_fp");
-
- if (bio == NULL || fp == XBADFILE) {
- return WOLFSSL_FAILURE;
- }
-
- if (bio->type != WOLFSSL_BIO_FILE) {
- return SSL_FAILURE;
- }
-
- *fp = (XFILE)bio->ptr;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* overwrites file */
-int wolfSSL_BIO_write_filename(WOLFSSL_BIO *bio, char *name)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_write_filename");
-
- if (bio == NULL || name == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- if (bio->type == WOLFSSL_BIO_FILE) {
- if (((XFILE)bio->ptr) != XBADFILE && bio->shutdown == BIO_CLOSE) {
- XFCLOSE((XFILE)bio->ptr);
- }
-
- bio->ptr = XFOPEN(name, "w");
- if (((XFILE)bio->ptr) == XBADFILE) {
- return WOLFSSL_FAILURE;
- }
- bio->shutdown = BIO_CLOSE;
-
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-
-int wolfSSL_BIO_seek(WOLFSSL_BIO *bio, int ofs)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_seek");
-
- if (bio == NULL) {
- return -1;
- }
-
- /* offset ofs from beginning of file */
- if (bio->type == WOLFSSL_BIO_FILE &&
- XFSEEK((XFILE)bio->ptr, ofs, SEEK_SET) < 0) {
- return -1;
- }
-
- return 0;
-}
-#endif /* NO_FILESYSTEM */
-
-
-long wolfSSL_BIO_set_mem_eof_return(WOLFSSL_BIO *bio, int v)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_set_mem_eof_return");
-
- if (bio != NULL) {
- bio->eof = v;
- }
-
- return 0;
-}
-
-int wolfSSL_BIO_get_len(WOLFSSL_BIO *bio)
-{
- int len;
-#ifndef NO_FILESYSTEM
- long memSz = 0, curr = 0;
- XFILE file;
-#endif
-
- WOLFSSL_ENTER("wolfSSL_BIO_get_len");
-
- if ((len = wolfSSL_BIO_pending(bio)) > 0) {
- }
-#ifndef NO_FILESYSTEM
- else if (bio->type == WOLFSSL_BIO_FILE) {
- if (wolfSSL_BIO_get_fp(bio, &file) != WOLFSSL_SUCCESS)
- len = BAD_FUNC_ARG;
- if (len == 0) {
- curr = XFTELL(file);
- if (curr < 0) {
- len = WOLFSSL_BAD_FILE;
- }
- if (XFSEEK(file, 0, XSEEK_END) != 0)
- len = WOLFSSL_BAD_FILE;
- }
- if (len == 0) {
- memSz = XFTELL(file);
- if (memSz > MAX_WOLFSSL_FILE_SIZE || memSz < 0)
- len = WOLFSSL_BAD_FILE;
- }
- if (len == 0) {
- memSz -= curr;
- len = (int)memSz;
- if (XFSEEK(file, curr, SEEK_SET) != 0)
- len = WOLFSSL_BAD_FILE;
- }
- }
-#endif
- return len;
-}
-
-
-void wolfSSL_BIO_set_callback(WOLFSSL_BIO *bio, wolf_bio_info_cb callback_func)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_set_callback");
-
- if (bio != NULL) {
- bio->infoCb = callback_func;
- }
-}
-
-
-wolf_bio_info_cb wolfSSL_BIO_get_callback(WOLFSSL_BIO *bio)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_get_callback");
-
- if (bio != NULL) {
- return bio->infoCb;
- }
-
- return NULL;
-}
-
-
-void wolfSSL_BIO_set_callback_arg(WOLFSSL_BIO *bio, char *arg)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_set_callback_arg");
-
- if (bio != NULL) {
- bio->infoArg = arg;
- }
-}
-
-
-char* wolfSSL_BIO_get_callback_arg(const WOLFSSL_BIO *bio)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_get_callback_arg");
-
- if (bio != NULL) {
- return bio->infoArg;
- }
-
- return NULL;
-}
-
-
-/* store a user pointer in the WOLFSSL_BIO structure */
-void wolfSSL_BIO_set_data(WOLFSSL_BIO* bio, void *ptr)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_set_data");
-
- if (bio != NULL) {
- bio->usrCtx = ptr;
- }
-}
-
-
-void* wolfSSL_BIO_get_data(WOLFSSL_BIO* bio)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_get_data");
-
- if (bio != NULL)
- return bio->usrCtx;
-
- WOLFSSL_MSG("WOLFSSL_BIO was null");
- return NULL;
-}
-
-/* If flag is 0 then blocking is set, if 1 then non blocking.
- * Always returns 1
- */
-long wolfSSL_BIO_set_nbio(WOLFSSL_BIO* bio, long on)
-{
- #ifndef WOLFSSL_DTLS
- (void)on;
- #endif
- WOLFSSL_ENTER("wolfSSL_BIO_set_nbio");
-
- switch (bio->type) {
- case WOLFSSL_BIO_SOCKET:
- #ifdef XFCNTL
- {
- int flag = XFCNTL(bio->num, F_GETFL, 0);
- if (on)
- XFCNTL(bio->num, F_SETFL, flag | O_NONBLOCK);
- else
- XFCNTL(bio->num, F_SETFL, flag & ~O_NONBLOCK);
- }
- #endif
- break;
- case WOLFSSL_BIO_SSL:
- #ifdef WOLFSSL_DTLS
- wolfSSL_dtls_set_using_nonblock((WOLFSSL*)bio->ptr, (int)on);
- #endif
- break;
-
- default:
- WOLFSSL_MSG("Unsupported bio type for non blocking");
- break;
- }
-
- return 1;
-}
-
-
-
-/* creates a new custom WOLFSSL_BIO_METHOD */
-WOLFSSL_BIO_METHOD *wolfSSL_BIO_meth_new(int type, const char *name)
-{
- WOLFSSL_BIO_METHOD* meth;
-
- WOLFSSL_ENTER("wolfSSL_BIO_meth_new");
-
- meth = (WOLFSSL_BIO_METHOD*)XMALLOC(sizeof(WOLFSSL_BIO_METHOD), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (meth == NULL) {
- WOLFSSL_MSG("Error allocating memory for WOLFSSL_BIO_METHOD");
- return NULL;
- }
- XMEMSET(meth, 0, sizeof(WOLFSSL_BIO_METHOD));
- meth->type = (byte)type;
- XSTRNCPY(meth->name, name, MAX_BIO_METHOD_NAME - 1);
-
- return meth;
-}
-
-
-void wolfSSL_BIO_meth_free(WOLFSSL_BIO_METHOD *biom)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_meth_free");
- if (biom) {
- XFREE(biom, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-
-int wolfSSL_BIO_meth_set_write(WOLFSSL_BIO_METHOD *biom,
- wolfSSL_BIO_meth_write_cb biom_write)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_meth_set_write");
- if (biom) {
- biom->writeCb = biom_write;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-
-int wolfSSL_BIO_meth_set_read(WOLFSSL_BIO_METHOD *biom,
- wolfSSL_BIO_meth_read_cb biom_read)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_meth_set_read");
- if (biom) {
- biom->readCb = biom_read;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-
-int wolfSSL_BIO_meth_set_puts(WOLFSSL_BIO_METHOD *biom,
- wolfSSL_BIO_meth_puts_cb biom_puts)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_meth_set_puts");
- if (biom) {
- biom->putsCb = biom_puts;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-
-int wolfSSL_BIO_meth_set_gets(WOLFSSL_BIO_METHOD *biom,
- wolfSSL_BIO_meth_gets_cb biom_gets)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_meth_set_gets");
- if (biom) {
- biom->getsCb = biom_gets;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-
-int wolfSSL_BIO_meth_set_ctrl(WOLFSSL_BIO_METHOD *biom,
- wolfSSL_BIO_meth_ctrl_get_cb biom_ctrl)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_meth_set_ctrl");
- if (biom) {
- biom->ctrlCb = biom_ctrl;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-
-int wolfSSL_BIO_meth_set_create(WOLFSSL_BIO_METHOD *biom,
- wolfSSL_BIO_meth_create_cb biom_create)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_meth_set_create");
- if (biom) {
- biom->createCb = biom_create;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-
-int wolfSSL_BIO_meth_set_destroy(WOLFSSL_BIO_METHOD *biom,
- wolfSSL_BIO_meth_destroy_cb biom_destroy)
-{
- WOLFSSL_STUB("wolfSSL_BIO_meth_set_destroy");
- if (biom) {
- biom->freeCb = biom_destroy;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-
-/* this compatibility function can be used for multiple BIO types */
-int wolfSSL_BIO_get_mem_data(WOLFSSL_BIO* bio, void* p)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_get_mem_data");
-
- if (bio == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- if (p) {
- *(byte**)p = (byte*)bio->ptr;
- }
-
- return bio->num;
-}
-
-int wolfSSL_BIO_pending(WOLFSSL_BIO* bio)
-{
- return (int)wolfSSL_BIO_ctrl_pending(bio);
-}
-
-
-int wolfSSL_BIO_flush(WOLFSSL_BIO* bio)
-{
- /* for wolfSSL no flushing needed */
- WOLFSSL_ENTER("BIO_flush");
- (void)bio;
- return 1;
-}
-#endif /* WOLFSSL_BIO_INCLUDED */
diff --git a/client/wolfssl/src/crl.c b/client/wolfssl/src/crl.c
deleted file mode 100644
index a48cf9d..0000000
--- a/client/wolfssl/src/crl.c
+++ /dev/null
@@ -1,1093 +0,0 @@
-/* crl.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
- /* Name change compatibility layer no longer needs included here */
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifndef WOLFCRYPT_ONLY
-#ifdef HAVE_CRL
-
-#include
-#include
-
-#include
-
-#ifdef HAVE_CRL_MONITOR
- #if (defined(__MACH__) || defined(__FreeBSD__) || defined(__linux__))
- static int StopMonitor(int mfd);
- #else
- #error "CRL monitor only currently supported on linux or mach"
- #endif
-#endif /* HAVE_CRL_MONITOR */
-
-
-/* Initialize CRL members */
-int InitCRL(WOLFSSL_CRL* crl, WOLFSSL_CERT_MANAGER* cm)
-{
- WOLFSSL_ENTER("InitCRL");
- if(cm != NULL)
- crl->heap = cm->heap;
- else
- crl->heap = NULL;
- crl->cm = cm;
- crl->crlList = NULL;
- crl->monitors[0].path = NULL;
- crl->monitors[1].path = NULL;
-#ifdef HAVE_CRL_MONITOR
- crl->tid = 0;
- crl->mfd = -1; /* mfd for bsd is kqueue fd, eventfd for linux */
- crl->setup = 0; /* thread setup done predicate */
- if (pthread_cond_init(&crl->cond, 0) != 0) {
- WOLFSSL_MSG("Pthread condition init failed");
- return BAD_COND_E;
- }
-#endif
- if (wc_InitMutex(&crl->crlLock) != 0) {
- WOLFSSL_MSG("Init Mutex failed");
- return BAD_MUTEX_E;
- }
-
- return 0;
-}
-
-
-/* Initialize CRL Entry */
-static int InitCRL_Entry(CRL_Entry* crle, DecodedCRL* dcrl, const byte* buff,
- int verified, void* heap)
-{
- WOLFSSL_ENTER("InitCRL_Entry");
-
- XMEMCPY(crle->issuerHash, dcrl->issuerHash, CRL_DIGEST_SIZE);
- /* XMEMCPY(crle->crlHash, dcrl->crlHash, CRL_DIGEST_SIZE);
- * copy the hash here if needed for optimized comparisons */
- XMEMCPY(crle->lastDate, dcrl->lastDate, MAX_DATE_SIZE);
- XMEMCPY(crle->nextDate, dcrl->nextDate, MAX_DATE_SIZE);
- crle->lastDateFormat = dcrl->lastDateFormat;
- crle->nextDateFormat = dcrl->nextDateFormat;
-
- crle->certs = dcrl->certs; /* take ownsership */
- dcrl->certs = NULL;
- crle->totalCerts = dcrl->totalCerts;
- crle->verified = verified;
- if (!verified) {
- crle->tbsSz = dcrl->sigIndex - dcrl->certBegin;
- crle->signatureSz = dcrl->sigLength;
- crle->signatureOID = dcrl->signatureOID;
- crle->toBeSigned = (byte*)XMALLOC(crle->tbsSz, heap,
- DYNAMIC_TYPE_CRL_ENTRY);
- if (crle->toBeSigned == NULL)
- return -1;
- crle->signature = (byte*)XMALLOC(crle->signatureSz, heap,
- DYNAMIC_TYPE_CRL_ENTRY);
- if (crle->signature == NULL) {
- XFREE(crle->toBeSigned, heap, DYNAMIC_TYPE_CRL_ENTRY);
- return -1;
- }
- XMEMCPY(crle->toBeSigned, buff + dcrl->certBegin, crle->tbsSz);
- XMEMCPY(crle->signature, dcrl->signature, crle->signatureSz);
- #ifndef NO_SKID
- crle->extAuthKeyIdSet = dcrl->extAuthKeyIdSet;
- if (crle->extAuthKeyIdSet)
- XMEMCPY(crle->extAuthKeyId, dcrl->extAuthKeyId, KEYID_SIZE);
- #endif
- }
- else {
- crle->toBeSigned = NULL;
- crle->signature = NULL;
- }
-
- (void)verified;
- (void)heap;
-
- return 0;
-}
-
-
-/* Free all CRL Entry resources */
-static void FreeCRL_Entry(CRL_Entry* crle, void* heap)
-{
- RevokedCert* tmp = crle->certs;
- RevokedCert* next;
-
- WOLFSSL_ENTER("FreeCRL_Entry");
-
- while (tmp) {
- next = tmp->next;
- XFREE(tmp, heap, DYNAMIC_TYPE_REVOKED);
- tmp = next;
- }
- if (crle->signature != NULL)
- XFREE(crle->signature, heap, DYNAMIC_TYPE_REVOKED);
- if (crle->toBeSigned != NULL)
- XFREE(crle->toBeSigned, heap, DYNAMIC_TYPE_REVOKED);
-
- (void)heap;
-}
-
-
-
-/* Free all CRL resources */
-void FreeCRL(WOLFSSL_CRL* crl, int dynamic)
-{
- CRL_Entry* tmp = crl->crlList;
-
- WOLFSSL_ENTER("FreeCRL");
- if (crl->monitors[0].path)
- XFREE(crl->monitors[0].path, crl->heap, DYNAMIC_TYPE_CRL_MONITOR);
-
- if (crl->monitors[1].path)
- XFREE(crl->monitors[1].path, crl->heap, DYNAMIC_TYPE_CRL_MONITOR);
-
- while(tmp) {
- CRL_Entry* next = tmp->next;
- FreeCRL_Entry(tmp, crl->heap);
- XFREE(tmp, crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
- tmp = next;
- }
-
-#ifdef HAVE_CRL_MONITOR
- if (crl->tid != 0) {
- WOLFSSL_MSG("stopping monitor thread");
- if (StopMonitor(crl->mfd) == 0)
- pthread_join(crl->tid, NULL);
- else {
- WOLFSSL_MSG("stop monitor failed");
- }
- }
- pthread_cond_destroy(&crl->cond);
-#endif
- wc_FreeMutex(&crl->crlLock);
- if (dynamic) /* free self */
- XFREE(crl, crl->heap, DYNAMIC_TYPE_CRL);
-}
-
-
-static int CheckCertCRLList(WOLFSSL_CRL* crl, DecodedCert* cert, int *pFoundEntry)
-{
- CRL_Entry* crle;
- int foundEntry = 0;
- int ret = 0;
-
- if (wc_LockMutex(&crl->crlLock) != 0) {
- WOLFSSL_MSG("wc_LockMutex failed");
- return BAD_MUTEX_E;
- }
-
- crle = crl->crlList;
-
- while (crle) {
- if (XMEMCMP(crle->issuerHash, cert->issuerHash, CRL_DIGEST_SIZE) == 0) {
- WOLFSSL_MSG("Found CRL Entry on list");
-
- if (crle->verified == 0) {
- Signer* ca = NULL;
- #ifndef NO_SKID
- byte extAuthKeyId[KEYID_SIZE];
- #endif
- byte issuerHash[CRL_DIGEST_SIZE];
- byte* tbs;
- word32 tbsSz = crle->tbsSz;
- byte* sig = NULL;
- word32 sigSz = crle->signatureSz;
- word32 sigOID = crle->signatureOID;
- SignatureCtx sigCtx;
-
- tbs = (byte*)XMALLOC(tbsSz, crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
- if (tbs == NULL) {
- wc_UnLockMutex(&crl->crlLock);
- return MEMORY_E;
- }
- sig = (byte*)XMALLOC(sigSz, crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
- if (sig == NULL) {
- XFREE(tbs, crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
- wc_UnLockMutex(&crl->crlLock);
- return MEMORY_E;
- }
-
- XMEMCPY(tbs, crle->toBeSigned, tbsSz);
- XMEMCPY(sig, crle->signature, sigSz);
- #ifndef NO_SKID
- XMEMCPY(extAuthKeyId, crle->extAuthKeyId,
- sizeof(extAuthKeyId));
- #endif
- XMEMCPY(issuerHash, crle->issuerHash, sizeof(issuerHash));
-
- wc_UnLockMutex(&crl->crlLock);
-
- #ifndef NO_SKID
- if (crle->extAuthKeyIdSet)
- ca = GetCA(crl->cm, extAuthKeyId);
- if (ca == NULL)
- ca = GetCAByName(crl->cm, issuerHash);
- #else /* NO_SKID */
- ca = GetCA(crl->cm, issuerHash);
- #endif /* NO_SKID */
- if (ca == NULL) {
- XFREE(sig, crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
- XFREE(tbs, crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
- WOLFSSL_MSG("Did NOT find CRL issuer CA");
- return ASN_CRL_NO_SIGNER_E;
- }
-
- ret = VerifyCRL_Signature(&sigCtx, tbs, tbsSz, sig, sigSz,
- sigOID, ca, crl->heap);
-
- XFREE(sig, crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
- XFREE(tbs, crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
-
- if (wc_LockMutex(&crl->crlLock) != 0) {
- WOLFSSL_MSG("wc_LockMutex failed");
- return BAD_MUTEX_E;
- }
-
- crle = crl->crlList;
- while (crle) {
- if (XMEMCMP(crle->issuerHash, cert->issuerHash,
- CRL_DIGEST_SIZE) == 0) {
-
- if (ret == 0)
- crle->verified = 1;
- else
- crle->verified = ret;
-
- XFREE(crle->toBeSigned, crl->heap,
- DYNAMIC_TYPE_CRL_ENTRY);
- crle->toBeSigned = NULL;
- XFREE(crle->signature, crl->heap,
- DYNAMIC_TYPE_CRL_ENTRY);
- crle->signature = NULL;
- break;
- }
- crle = crle->next;
- }
- if (crle == NULL || crle->verified < 0)
- break;
- }
- else if (crle->verified < 0) {
- WOLFSSL_MSG("Cannot use CRL as it didn't verify");
- ret = crle->verified;
- break;
- }
-
- WOLFSSL_MSG("Checking next date validity");
-
- #ifdef WOLFSSL_NO_CRL_NEXT_DATE
- if (crle->nextDateFormat != ASN_OTHER_TYPE)
- #endif
- {
- #ifndef NO_ASN_TIME
- if (!XVALIDATE_DATE(crle->nextDate,crle->nextDateFormat, AFTER)) {
- WOLFSSL_MSG("CRL next date is no longer valid");
- ret = ASN_AFTER_DATE_E;
- }
- #endif
- }
- if (ret == 0) {
- foundEntry = 1;
- }
- break;
- }
- crle = crle->next;
- }
-
- if (foundEntry) {
- RevokedCert* rc = crle->certs;
-
- while (rc) {
- if (rc->serialSz == cert->serialSz &&
- XMEMCMP(rc->serialNumber, cert->serial, rc->serialSz) == 0) {
- WOLFSSL_MSG("Cert revoked");
- ret = CRL_CERT_REVOKED;
- break;
- }
- rc = rc->next;
- }
- }
-
- wc_UnLockMutex(&crl->crlLock);
-
- *pFoundEntry = foundEntry;
-
- return ret;
-}
-
-/* Is the cert ok with CRL, return 0 on success */
-int CheckCertCRL(WOLFSSL_CRL* crl, DecodedCert* cert)
-{
- int foundEntry = 0;
- int ret = 0;
-
- WOLFSSL_ENTER("CheckCertCRL");
-
- ret = CheckCertCRLList(crl, cert, &foundEntry);
-
-#ifdef HAVE_CRL_IO
- if (foundEntry == 0) {
- /* perform embedded lookup */
- if (crl->crlIOCb) {
- ret = crl->crlIOCb(crl, (const char*)cert->extCrlInfo,
- cert->extCrlInfoSz);
- if (ret == WOLFSSL_CBIO_ERR_WANT_READ) {
- ret = WANT_READ;
- }
- else if (ret >= 0) {
- /* try again */
- ret = CheckCertCRLList(crl, cert, &foundEntry);
- }
- }
- }
-#endif
-
- if (foundEntry == 0) {
- WOLFSSL_MSG("Couldn't find CRL for status check");
- ret = CRL_MISSING;
-
- if (crl->cm->cbMissingCRL) {
- char url[256];
-
- WOLFSSL_MSG("Issuing missing CRL callback");
- url[0] = '\0';
- if (cert->extCrlInfo) {
- if (cert->extCrlInfoSz < (int)sizeof(url) -1 ) {
- XMEMCPY(url, cert->extCrlInfo, cert->extCrlInfoSz);
- url[cert->extCrlInfoSz] = '\0';
- }
- else {
- WOLFSSL_MSG("CRL url too long");
- }
- }
-
- crl->cm->cbMissingCRL(url);
- }
- }
-
- return ret;
-}
-
-
-/* Add Decoded CRL, 0 on success */
-static int AddCRL(WOLFSSL_CRL* crl, DecodedCRL* dcrl, const byte* buff,
- int verified)
-{
- CRL_Entry* crle;
-
- WOLFSSL_ENTER("AddCRL");
-
- crle = (CRL_Entry*)XMALLOC(sizeof(CRL_Entry), crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
- if (crle == NULL) {
- WOLFSSL_MSG("alloc CRL Entry failed");
- return -1;
- }
-
- if (InitCRL_Entry(crle, dcrl, buff, verified, crl->heap) < 0) {
- WOLFSSL_MSG("Init CRL Entry failed");
- XFREE(crle, crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
- return -1;
- }
-
- if (wc_LockMutex(&crl->crlLock) != 0) {
- WOLFSSL_MSG("wc_LockMutex failed");
- FreeCRL_Entry(crle, crl->heap);
- XFREE(crle, crl->heap, DYNAMIC_TYPE_CRL_ENTRY);
- return BAD_MUTEX_E;
- }
- crle->next = crl->crlList;
- crl->crlList = crle;
- wc_UnLockMutex(&crl->crlLock);
-
- return 0;
-}
-
-
-/* Load CRL File of type, WOLFSSL_SUCCESS on ok */
-int BufferLoadCRL(WOLFSSL_CRL* crl, const byte* buff, long sz, int type,
- int verify)
-{
- int ret = WOLFSSL_SUCCESS;
- const byte* myBuffer = buff; /* if DER ok, otherwise switch */
- DerBuffer* der = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- DecodedCRL* dcrl;
-#else
- DecodedCRL dcrl[1];
-#endif
-
- WOLFSSL_ENTER("BufferLoadCRL");
-
- if (crl == NULL || buff == NULL || sz == 0)
- return BAD_FUNC_ARG;
-
- if (type == WOLFSSL_FILETYPE_PEM) {
- #ifdef WOLFSSL_PEM_TO_DER
- ret = PemToDer(buff, sz, CRL_TYPE, &der, NULL, NULL, NULL);
- if (ret == 0) {
- myBuffer = der->buffer;
- sz = der->length;
- }
- else {
- WOLFSSL_MSG("Pem to Der failed");
- FreeDer(&der);
- return -1;
- }
- #else
- ret = NOT_COMPILED_IN;
- #endif
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- dcrl = (DecodedCRL*)XMALLOC(sizeof(DecodedCRL), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (dcrl == NULL) {
- FreeDer(&der);
- return MEMORY_E;
- }
-#endif
-
- InitDecodedCRL(dcrl, crl->heap);
- ret = ParseCRL(dcrl, myBuffer, (word32)sz, crl->cm);
- if (ret != 0 && !(ret == ASN_CRL_NO_SIGNER_E && verify == NO_VERIFY)) {
- WOLFSSL_MSG("ParseCRL error");
- }
- else {
- ret = AddCRL(crl, dcrl, myBuffer, ret != ASN_CRL_NO_SIGNER_E);
- if (ret != 0) {
- WOLFSSL_MSG("AddCRL error");
- }
- }
-
- FreeDecodedCRL(dcrl);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(dcrl, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- FreeDer(&der);
-
- return ret ? ret : WOLFSSL_SUCCESS; /* convert 0 to WOLFSSL_SUCCESS */
-}
-
-#if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
-int wolfSSL_X509_STORE_add_crl(WOLFSSL_X509_STORE *store, WOLFSSL_X509_CRL *newcrl)
-{
- CRL_Entry *crle;
- WOLFSSL_CRL *crl;
-
- WOLFSSL_ENTER("wolfSSL_X509_STORE_add_crl");
- if (store == NULL || newcrl == NULL)
- return BAD_FUNC_ARG;
-
- crl = store->crl;
- crle = newcrl->crlList;
-
- if (wc_LockMutex(&crl->crlLock) != 0)
- {
- WOLFSSL_MSG("wc_LockMutex failed");
- return BAD_MUTEX_E;
- }
- crle->next = crl->crlList;
- crl->crlList = crle;
- newcrl->crlList = NULL;
- wc_UnLockMutex(&crl->crlLock);
-
- WOLFSSL_LEAVE("wolfSSL_X509_STORE_add_crl", WOLFSSL_SUCCESS);
-
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-#ifdef HAVE_CRL_MONITOR
-
-
-/* Signal Monitor thread is setup, save status to setup flag, 0 on success */
-static int SignalSetup(WOLFSSL_CRL* crl, int status)
-{
- int ret;
-
- /* signal to calling thread we're setup */
- if (wc_LockMutex(&crl->crlLock) != 0) {
- WOLFSSL_MSG("wc_LockMutex crlLock failed");
- return BAD_MUTEX_E;
- }
-
- crl->setup = status;
- ret = pthread_cond_signal(&crl->cond);
-
- wc_UnLockMutex(&crl->crlLock);
-
- if (ret != 0)
- return BAD_COND_E;
-
- return 0;
-}
-
-
-/* read in new CRL entries and save new list */
-static int SwapLists(WOLFSSL_CRL* crl)
-{
- int ret;
- CRL_Entry* newList;
-#ifdef WOLFSSL_SMALL_STACK
- WOLFSSL_CRL* tmp;
-#else
- WOLFSSL_CRL tmp[1];
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- tmp = (WOLFSSL_CRL*)XMALLOC(sizeof(WOLFSSL_CRL), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmp == NULL)
- return MEMORY_E;
-#endif
-
- if (InitCRL(tmp, crl->cm) < 0) {
- WOLFSSL_MSG("Init tmp CRL failed");
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return -1;
- }
-
- if (crl->monitors[0].path) {
- ret = LoadCRL(tmp, crl->monitors[0].path, WOLFSSL_FILETYPE_PEM, 0);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("PEM LoadCRL on dir change failed");
- FreeCRL(tmp, 0);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return -1;
- }
- }
-
- if (crl->monitors[1].path) {
- ret = LoadCRL(tmp, crl->monitors[1].path, WOLFSSL_FILETYPE_ASN1, 0);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("DER LoadCRL on dir change failed");
- FreeCRL(tmp, 0);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return -1;
- }
- }
-
- if (wc_LockMutex(&crl->crlLock) != 0) {
- WOLFSSL_MSG("wc_LockMutex failed");
- FreeCRL(tmp, 0);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return -1;
- }
-
- newList = tmp->crlList;
-
- /* swap lists */
- tmp->crlList = crl->crlList;
- crl->crlList = newList;
-
- wc_UnLockMutex(&crl->crlLock);
-
- FreeCRL(tmp, 0);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return 0;
-}
-
-
-#if (defined(__MACH__) || defined(__FreeBSD__))
-
-#include
-#include
-#include
-#include
-#include
-
-#ifdef __MACH__
- #define XEVENT_MODE O_EVTONLY
-#elif defined(__FreeBSD__)
- #define XEVENT_MODE EVFILT_VNODE
-#endif
-
-
-/* we need a unique kqueue user filter fd for crl in case user is doing custom
- * events too */
-#ifndef CRL_CUSTOM_FD
- #define CRL_CUSTOM_FD 123456
-#endif
-
-
-/* shutdown monitor thread, 0 on success */
-static int StopMonitor(int mfd)
-{
- struct kevent change;
-
- /* trigger custom shutdown */
- EV_SET(&change, CRL_CUSTOM_FD, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
- if (kevent(mfd, &change, 1, NULL, 0, NULL) < 0) {
- WOLFSSL_MSG("kevent trigger customer event failed");
- return -1;
- }
-
- return 0;
-}
-
-
-/* OS X monitoring */
-static void* DoMonitor(void* arg)
-{
- int fPEM, fDER;
- struct kevent change;
-
- WOLFSSL_CRL* crl = (WOLFSSL_CRL*)arg;
-
- WOLFSSL_ENTER("DoMonitor");
-
- crl->mfd = kqueue();
- if (crl->mfd == -1) {
- WOLFSSL_MSG("kqueue failed");
- SignalSetup(crl, MONITOR_SETUP_E);
- return NULL;
- }
-
- /* listen for custom shutdown event */
- EV_SET(&change, CRL_CUSTOM_FD, EVFILT_USER, EV_ADD, 0, 0, NULL);
- if (kevent(crl->mfd, &change, 1, NULL, 0, NULL) < 0) {
- WOLFSSL_MSG("kevent monitor customer event failed");
- SignalSetup(crl, MONITOR_SETUP_E);
- close(crl->mfd);
- return NULL;
- }
-
- fPEM = -1;
- fDER = -1;
-
- if (crl->monitors[0].path) {
- fPEM = open(crl->monitors[0].path, XEVENT_MODE);
- if (fPEM == -1) {
- WOLFSSL_MSG("PEM event dir open failed");
- SignalSetup(crl, MONITOR_SETUP_E);
- close(crl->mfd);
- return NULL;
- }
- }
-
- if (crl->monitors[1].path) {
- fDER = open(crl->monitors[1].path, XEVENT_MODE);
- if (fDER == -1) {
- WOLFSSL_MSG("DER event dir open failed");
- if (fPEM != -1)
- close(fPEM);
- close(crl->mfd);
- SignalSetup(crl, MONITOR_SETUP_E);
- return NULL;
- }
- }
-
- if (fPEM != -1)
- EV_SET(&change, fPEM, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_ONESHOT,
- NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB, 0, 0);
-
- if (fDER != -1)
- EV_SET(&change, fDER, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_ONESHOT,
- NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_ATTRIB, 0, 0);
-
- /* signal to calling thread we're setup */
- if (SignalSetup(crl, 1) != 0) {
- if (fPEM != -1)
- close(fPEM);
- if (fDER != -1)
- close(fDER);
- close(crl->mfd);
- return NULL;
- }
-
- for (;;) {
- struct kevent event;
- int numEvents = kevent(crl->mfd, &change, 1, &event, 1, NULL);
-
- WOLFSSL_MSG("Got kevent");
-
- if (numEvents == -1) {
- WOLFSSL_MSG("kevent problem, continue");
- continue;
- }
-
- if (event.filter == EVFILT_USER) {
- WOLFSSL_MSG("Got user shutdown event, breaking out");
- break;
- }
-
- if (SwapLists(crl) < 0) {
- WOLFSSL_MSG("SwapLists problem, continue");
- }
- }
-
- if (fPEM != -1)
- close(fPEM);
- if (fDER != -1)
- close(fDER);
-
- close(crl->mfd);
-
- return NULL;
-}
-
-
-#elif defined(__linux__)
-
-#include
-#include
-#include
-#include
-
-
-#ifndef max
- static WC_INLINE int max(int a, int b)
- {
- return a > b ? a : b;
- }
-#endif /* max */
-
-
-/* shutdown monitor thread, 0 on success */
-static int StopMonitor(int mfd)
-{
- word64 w64 = 1;
-
- /* write to our custom event */
- if (write(mfd, &w64, sizeof(w64)) < 0) {
- WOLFSSL_MSG("StopMonitor write failed");
- return -1;
- }
-
- return 0;
-}
-
-
-/* linux monitoring */
-static void* DoMonitor(void* arg)
-{
- int notifyFd;
- int wd = -1;
- WOLFSSL_CRL* crl = (WOLFSSL_CRL*)arg;
-#ifdef WOLFSSL_SMALL_STACK
- char* buff;
-#else
- char buff[8192];
-#endif
-
- WOLFSSL_ENTER("DoMonitor");
-
- crl->mfd = eventfd(0, 0); /* our custom shutdown event */
- if (crl->mfd < 0) {
- WOLFSSL_MSG("eventfd failed");
- SignalSetup(crl, MONITOR_SETUP_E);
- return NULL;
- }
-
- notifyFd = inotify_init();
- if (notifyFd < 0) {
- WOLFSSL_MSG("inotify failed");
- close(crl->mfd);
- SignalSetup(crl, MONITOR_SETUP_E);
- return NULL;
- }
-
- if (crl->monitors[0].path) {
- wd = inotify_add_watch(notifyFd, crl->monitors[0].path, IN_CLOSE_WRITE |
- IN_DELETE);
- if (wd < 0) {
- WOLFSSL_MSG("PEM notify add watch failed");
- close(crl->mfd);
- close(notifyFd);
- SignalSetup(crl, MONITOR_SETUP_E);
- return NULL;
- }
- }
-
- if (crl->monitors[1].path) {
- wd = inotify_add_watch(notifyFd, crl->monitors[1].path, IN_CLOSE_WRITE |
- IN_DELETE);
- if (wd < 0) {
- WOLFSSL_MSG("DER notify add watch failed");
- close(crl->mfd);
- close(notifyFd);
- SignalSetup(crl, MONITOR_SETUP_E);
- return NULL;
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- buff = (char*)XMALLOC(8192, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (buff == NULL)
- return NULL;
-#endif
-
- /* signal to calling thread we're setup */
- if (SignalSetup(crl, 1) != 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
-
- if (wd > 0)
- inotify_rm_watch(notifyFd, wd);
- close(crl->mfd);
- close(notifyFd);
- return NULL;
- }
-
- for (;;) {
- fd_set readfds;
- int result;
- int length;
-
- FD_ZERO(&readfds);
- FD_SET(notifyFd, &readfds);
- FD_SET(crl->mfd, &readfds);
-
- result = select(max(notifyFd, crl->mfd) + 1, &readfds, NULL, NULL,NULL);
-
- WOLFSSL_MSG("Got notify event");
-
- if (result < 0) {
- WOLFSSL_MSG("select problem, continue");
- continue;
- }
-
- if (FD_ISSET(crl->mfd, &readfds)) {
- WOLFSSL_MSG("got custom shutdown event, breaking out");
- break;
- }
-
- length = (int) read(notifyFd, buff, 8192);
- if (length < 0) {
- WOLFSSL_MSG("notify read problem, continue");
- continue;
- }
-
- if (SwapLists(crl) < 0) {
- WOLFSSL_MSG("SwapLists problem, continue");
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- if (wd > 0)
- inotify_rm_watch(notifyFd, wd);
- close(crl->mfd);
- close(notifyFd);
-
- return NULL;
-}
-
-#endif /* MACH or linux */
-
-
-/* Start Monitoring the CRL path(s) in a thread */
-static int StartMonitorCRL(WOLFSSL_CRL* crl)
-{
- int ret = WOLFSSL_SUCCESS;
-
- WOLFSSL_ENTER("StartMonitorCRL");
-
- if (crl == NULL)
- return BAD_FUNC_ARG;
-
- if (crl->tid != 0) {
- WOLFSSL_MSG("Monitor thread already running");
- return ret; /* that's ok, someone already started */
- }
-
- if (pthread_create(&crl->tid, NULL, DoMonitor, crl) != 0) {
- WOLFSSL_MSG("Thread creation error");
- return THREAD_CREATE_E;
- }
-
- /* wait for setup to complete */
- if (wc_LockMutex(&crl->crlLock) != 0) {
- WOLFSSL_MSG("wc_LockMutex crlLock error");
- return BAD_MUTEX_E;
- }
-
- while (crl->setup == 0) {
- if (pthread_cond_wait(&crl->cond, &crl->crlLock) != 0) {
- ret = BAD_COND_E;
- break;
- }
- }
-
- if (crl->setup < 0)
- ret = crl->setup; /* store setup error */
-
- wc_UnLockMutex(&crl->crlLock);
-
- if (ret < 0) {
- WOLFSSL_MSG("DoMonitor setup failure");
- crl->tid = 0; /* thread already done */
- }
-
- return ret;
-}
-
-
-#else /* HAVE_CRL_MONITOR */
-
-#ifndef NO_FILESYSTEM
-
-static int StartMonitorCRL(WOLFSSL_CRL* crl)
-{
- (void)crl;
-
- WOLFSSL_ENTER("StartMonitorCRL");
- WOLFSSL_MSG("Not compiled in");
-
- return NOT_COMPILED_IN;
-}
-
-#endif /* NO_FILESYSTEM */
-
-#endif /* HAVE_CRL_MONITOR */
-
-#if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
-
-/* Load CRL path files of type, WOLFSSL_SUCCESS on ok */
-int LoadCRL(WOLFSSL_CRL* crl, const char* path, int type, int monitor)
-{
- int ret = WOLFSSL_SUCCESS;
- char* name = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- ReadDirCtx* readCtx = NULL;
-#else
- ReadDirCtx readCtx[1];
-#endif
-
- WOLFSSL_ENTER("LoadCRL");
- if (crl == NULL)
- return BAD_FUNC_ARG;
-
-#ifdef WOLFSSL_SMALL_STACK
- readCtx = (ReadDirCtx*)XMALLOC(sizeof(ReadDirCtx), crl->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (readCtx == NULL)
- return MEMORY_E;
-#endif
-
- /* try to load each regular file in path */
- ret = wc_ReadDirFirst(readCtx, path, &name);
- while (ret == 0 && name) {
- int skip = 0;
- if (type == WOLFSSL_FILETYPE_PEM) {
- if (XSTRSTR(name, ".pem") == NULL) {
- WOLFSSL_MSG("not .pem file, skipping");
- skip = 1;
- }
- }
- else {
- if (XSTRSTR(name, ".der") == NULL &&
- XSTRSTR(name, ".crl") == NULL)
- {
- WOLFSSL_MSG("not .der or .crl file, skipping");
- skip = 1;
- }
- }
-
- if (!skip && ProcessFile(NULL, name, type, CRL_TYPE, NULL, 0, crl,
- VERIFY) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("CRL file load failed, continuing");
- }
-
- ret = wc_ReadDirNext(readCtx, path, &name);
- }
- wc_ReadDirClose(readCtx);
- ret = WOLFSSL_SUCCESS; /* load failures not reported, for backwards compat */
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(readCtx, crl->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- if (monitor & WOLFSSL_CRL_MONITOR) {
- word32 pathLen;
- char* pathBuf;
-
- WOLFSSL_MSG("monitor path requested");
-
- pathLen = (word32)XSTRLEN(path);
- pathBuf = (char*)XMALLOC(pathLen+1, crl->heap,DYNAMIC_TYPE_CRL_MONITOR);
- if (pathBuf) {
- XSTRNCPY(pathBuf, path, pathLen+1);
-
- if (type == WOLFSSL_FILETYPE_PEM) {
- /* free old path before setting a new one */
- if (crl->monitors[0].path) {
- XFREE(crl->monitors[0].path, crl->heap,
- DYNAMIC_TYPE_CRL_MONITOR);
- }
- crl->monitors[0].path = pathBuf;
- crl->monitors[0].type = WOLFSSL_FILETYPE_PEM;
- } else {
- /* free old path before setting a new one */
- if (crl->monitors[1].path) {
- XFREE(crl->monitors[1].path, crl->heap,
- DYNAMIC_TYPE_CRL_MONITOR);
- }
- crl->monitors[1].path = pathBuf;
- crl->monitors[1].type = WOLFSSL_FILETYPE_ASN1;
- }
-
- if (monitor & WOLFSSL_CRL_START_MON) {
- WOLFSSL_MSG("start monitoring requested");
-
- ret = StartMonitorCRL(crl);
- }
- }
- else {
- ret = MEMORY_E;
- }
- }
-
- return ret;
-}
-
-#else
-int LoadCRL(WOLFSSL_CRL* crl, const char* path, int type, int monitor)
-{
- (void)crl;
- (void)path;
- (void)type;
- (void)monitor;
-
- /* stub for scenario where file system is not supported */
- return NOT_COMPILED_IN;
-}
-#endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
-
-#endif /* HAVE_CRL */
-#endif /* !WOLFCRYPT_ONLY */
diff --git a/client/wolfssl/src/include.am b/client/wolfssl/src/include.am
deleted file mode 100644
index a7f20de..0000000
--- a/client/wolfssl/src/include.am
+++ /dev/null
@@ -1,531 +0,0 @@
-# vim:ft=automake
-# included from Top Level Makefile.am
-# All paths should be given relative to the root
-
-lib_LTLIBRARIES+= src/libwolfssl.la
-src_libwolfssl_la_SOURCES =
-src_libwolfssl_la_LDFLAGS = ${AM_LDFLAGS} -no-undefined -version-info ${WOLFSSL_LIBRARY_VERSION}
-src_libwolfssl_la_LIBADD = $(LIBM) $(LIB_ADD) $(LIB_STATIC_ADD)
-src_libwolfssl_la_CFLAGS = -DBUILDING_WOLFSSL $(AM_CFLAGS)
-src_libwolfssl_la_CPPFLAGS = -DBUILDING_WOLFSSL $(AM_CPPFLAGS)
-
-# install the packaged IPP libraries
-if BUILD_FAST_RSA
-
-# Link needed IPP libraries
-noinst_SCRIPTS+=IPP_links
-IPP_links:
- @$(IPPLINK)
-
-ippdir = $(libdir)
-ipp_DATA = $(IPPLIBS)
-
-include_HEADERS+=$(IPPHEADERS)
-endif # BUILD_FAST_RSA
-
-if BUILD_FIPS
-
-if BUILD_FIPS_V1
-# fips first file
-src_libwolfssl_la_SOURCES += ctaocrypt/src/wolfcrypt_first.c
-
-src_libwolfssl_la_SOURCES += \
- ctaocrypt/src/hmac.c \
- ctaocrypt/src/random.c \
- ctaocrypt/src/sha256.c
-
-if BUILD_RSA
-src_libwolfssl_la_SOURCES += ctaocrypt/src/rsa.c
-endif
-
-if BUILD_AES
-src_libwolfssl_la_SOURCES += ctaocrypt/src/aes.c
-endif
-
-if BUILD_DES3
-src_libwolfssl_la_SOURCES += ctaocrypt/src/des3.c
-endif
-
-if BUILD_SHA
-src_libwolfssl_la_SOURCES += ctaocrypt/src/sha.c
-endif
-
-if BUILD_SHA512
-src_libwolfssl_la_SOURCES += ctaocrypt/src/sha512.c
-endif
-
-src_libwolfssl_la_SOURCES += ctaocrypt/src/fips.c
-src_libwolfssl_la_SOURCES += ctaocrypt/src/fips_test.c
-
-# fips last file
-src_libwolfssl_la_SOURCES += ctaocrypt/src/wolfcrypt_last.c
-endif
-
-if BUILD_FIPS_V2
-# FIPSv2 first file
-src_libwolfssl_la_SOURCES += \
- wolfcrypt/src/wolfcrypt_first.c
-
-src_libwolfssl_la_SOURCES += \
- wolfcrypt/src/hmac.c \
- wolfcrypt/src/random.c \
- wolfcrypt/src/sha256.c
-
-if BUILD_RSA
-src_libwolfssl_la_SOURCES += wolfcrypt/src/rsa.c
-endif
-
-if BUILD_ECC
-src_libwolfssl_la_SOURCES += wolfcrypt/src/ecc.c
-endif
-
-if BUILD_AES
-src_libwolfssl_la_SOURCES += wolfcrypt/src/aes.c
-endif
-
-if BUILD_AESNI
-src_libwolfssl_la_SOURCES += wolfcrypt/src/aes_asm.S
-if BUILD_INTELASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/aes_gcm_asm.S
-endif
-endif
-
-if BUILD_DES3
-src_libwolfssl_la_SOURCES += wolfcrypt/src/des3.c
-endif
-
-if BUILD_SHA
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha.c
-if BUILD_INTELASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha256_asm.S
-endif
-endif
-
-if BUILD_SHA512
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha512.c
-if BUILD_INTELASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha512_asm.S
-endif
-endif
-
-if BUILD_SHA3
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha3.c
-endif
-
-if BUILD_DH
-src_libwolfssl_la_SOURCES += wolfcrypt/src/dh.c
-endif
-
-if BUILD_CMAC
-src_libwolfssl_la_SOURCES += wolfcrypt/src/cmac.c
-endif
-
-src_libwolfssl_la_SOURCES += wolfcrypt/src/fips.c \
- wolfcrypt/src/fips_test.c
-
-# fips last file
-src_libwolfssl_la_SOURCES += wolfcrypt/src/wolfcrypt_last.c
-endif
-
-if BUILD_FIPS_RAND
-src_libwolfssl_la_SOURCES += \
- wolfcrypt/src/wolfcrypt_first.c \
- wolfcrypt/src/hmac.c \
- wolfcrypt/src/random.c \
- wolfcrypt/src/sha256.c \
- wolfcrypt/src/sha256_asm.S \
- wolfcrypt/src/fips.c \
- wolfcrypt/src/fips_test.c \
- wolfcrypt/src/wolfcrypt_last.c
-endif BUILD_FIPS_RAND
-
-endif BUILD_FIPS
-
-# For wolfRand, exclude everything else.
-if !BUILD_FIPS_RAND
-
-# For FIPSV2, exclude the wolfCrypt files included above.
-# For wolfRand, exclude just a couple files.
-# For old FIPS, keep the wolfCrypt versions of the
-# CtaoCrypt files included above.
-if !BUILD_FIPS_V2
-src_libwolfssl_la_SOURCES += wolfcrypt/src/hmac.c
-endif
-
-# CAVP self test
-if BUILD_SELFTEST
-src_libwolfssl_la_SOURCES += wolfcrypt/src/selftest.c
-endif
-
-endif !BUILD_FIPS_RAND
-
-src_libwolfssl_la_SOURCES += \
- wolfcrypt/src/hash.c \
- wolfcrypt/src/cpuid.c
-
-if !BUILD_FIPS_RAND
-
-if !BUILD_FIPS_V2
-if BUILD_RNG
-src_libwolfssl_la_SOURCES += wolfcrypt/src/random.c
-endif
-endif
-
-if !BUILD_FIPS_V2
-if BUILD_ARMASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-sha256.c
-else
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha256.c
-if BUILD_INTELASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha256_asm.S
-endif
-endif
-endif
-
-if BUILD_AFALG
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/af_alg/afalg_hash.c
-endif
-
-if BUILD_WOLFEVENT
-src_libwolfssl_la_SOURCES += wolfcrypt/src/wolfevent.c
-endif
-
-if BUILD_ASYNCCRYPT
-src_libwolfssl_la_SOURCES += wolfcrypt/src/async.c
-endif
-
-if !BUILD_USER_RSA
-if BUILD_RSA
-if BUILD_FAST_RSA
-src_libwolfssl_la_SOURCES += wolfcrypt/user-crypto/src/rsa.c
-else
-if !BUILD_FIPS_V2
-src_libwolfssl_la_SOURCES += wolfcrypt/src/rsa.c
-endif
-endif
-endif
-endif
-
-if BUILD_SP
-if BUILD_SP_C
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sp_c32.c
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sp_c64.c
-endif
-if BUILD_SP_X86_64
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sp_x86_64.c
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sp_x86_64_asm.S
-endif
-if BUILD_SP_ARM32
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sp_arm32.c
-endif
-if BUILD_SP_ARM_THUMB
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sp_armthumb.c
-endif
-if BUILD_SP_ARM64
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sp_arm64.c
-endif
-if BUILD_SP_INT
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sp_int.c
-endif
-if BUILD_SP_ARM_CORTEX
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sp_cortexm.c
-endif
-endif
-
-if !BUILD_FIPS_V2
-if BUILD_AES
-src_libwolfssl_la_SOURCES += wolfcrypt/src/aes.c
-if BUILD_ARMASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-aes.c
-endif
-if BUILD_AFALG
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/af_alg/afalg_aes.c
-endif
-endif
-endif
-
-if !BUILD_FIPS_V2
-if BUILD_CMAC
-src_libwolfssl_la_SOURCES += wolfcrypt/src/cmac.c
-endif
-endif
-
-if !BUILD_FIPS_V2
-if BUILD_DES3
-src_libwolfssl_la_SOURCES += wolfcrypt/src/des3.c
-endif
-endif
-
-if !BUILD_FIPS_V2
-if BUILD_SHA
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha.c
-endif
-endif
-
-if !BUILD_FIPS_V2
-if BUILD_SHA512
-if BUILD_ARMASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-sha512.c
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-sha512-asm.S
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-32-sha512-asm.S
-else
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha512.c
-if BUILD_INTELASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha512_asm.S
-endif
-endif
-endif
-endif
-
-if !BUILD_FIPS_V2
-if BUILD_SHA3
-src_libwolfssl_la_SOURCES += wolfcrypt/src/sha3.c
-endif
-endif
-
-
-endif !BUILD_FIPS_RAND
-
-src_libwolfssl_la_SOURCES += \
- wolfcrypt/src/logging.c \
- wolfcrypt/src/wc_port.c \
- wolfcrypt/src/error.c
-
-if !BUILD_FIPS_RAND
-src_libwolfssl_la_SOURCES += \
- wolfcrypt/src/wc_encrypt.c \
- wolfcrypt/src/signature.c \
- wolfcrypt/src/wolfmath.c
-endif !BUILD_FIPS_RAND
-
-if BUILD_MEMORY
-src_libwolfssl_la_SOURCES += wolfcrypt/src/memory.c
-endif
-
-if !BUILD_FIPS_RAND
-if !BUILD_FIPS_V2
-if BUILD_DH
-src_libwolfssl_la_SOURCES += wolfcrypt/src/dh.c
-endif
-endif
-
-if BUILD_ASN
-src_libwolfssl_la_SOURCES += wolfcrypt/src/asn.c
-endif
-
-endif !BUILD_FIPS_RAND
-
-if BUILD_CODING
-src_libwolfssl_la_SOURCES += wolfcrypt/src/coding.c
-endif
-
-if !BUILD_FIPS_RAND
-
-if BUILD_POLY1305
-if BUILD_ARMASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-poly1305.c
-endif
-src_libwolfssl_la_SOURCES += wolfcrypt/src/poly1305.c
-if BUILD_INTELASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/poly1305_asm.S
-endif
-endif
-
-if BUILD_RC4
-src_libwolfssl_la_SOURCES += wolfcrypt/src/arc4.c
-endif
-
-if BUILD_MD4
-src_libwolfssl_la_SOURCES += wolfcrypt/src/md4.c
-endif
-
-if BUILD_MD5
-src_libwolfssl_la_SOURCES += wolfcrypt/src/md5.c
-endif
-
-if BUILD_PWDBASED
-src_libwolfssl_la_SOURCES += wolfcrypt/src/pwdbased.c
-src_libwolfssl_la_SOURCES += wolfcrypt/src/pkcs12.c
-endif
-
-if BUILD_DSA
-src_libwolfssl_la_SOURCES += wolfcrypt/src/dsa.c
-endif
-
-if !BUILD_FIPS_V2
-if BUILD_AESNI
-src_libwolfssl_la_SOURCES += wolfcrypt/src/aes_asm.S
-src_libwolfssl_la_SOURCES += wolfcrypt/src/aes_gcm_asm.S
-endif
-endif
-
-if BUILD_CAMELLIA
-src_libwolfssl_la_SOURCES += wolfcrypt/src/camellia.c
-endif
-
-if BUILD_MD2
-src_libwolfssl_la_SOURCES += wolfcrypt/src/md2.c
-endif
-
-if BUILD_RIPEMD
-src_libwolfssl_la_SOURCES += wolfcrypt/src/ripemd.c
-endif
-
-if BUILD_BLAKE2
-src_libwolfssl_la_SOURCES += wolfcrypt/src/blake2b.c
-endif
-if BUILD_BLAKE2S
-src_libwolfssl_la_SOURCES += wolfcrypt/src/blake2s.c
-endif
-
-if BUILD_HC128
-src_libwolfssl_la_SOURCES += wolfcrypt/src/hc128.c
-endif
-
-if BUILD_RABBIT
-src_libwolfssl_la_SOURCES += wolfcrypt/src/rabbit.c
-endif
-
-if BUILD_CHACHA
-if BUILD_ARMASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-chacha.c
-else
-src_libwolfssl_la_SOURCES += wolfcrypt/src/chacha.c
-if BUILD_INTELASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/chacha_asm.S
-endif
-endif
-if BUILD_POLY1305
-src_libwolfssl_la_SOURCES += wolfcrypt/src/chacha20_poly1305.c
-endif
-endif
-
-if !BUILD_INLINE
-src_libwolfssl_la_SOURCES += wolfcrypt/src/misc.c
-endif
-
-if BUILD_FASTMATH
-src_libwolfssl_la_SOURCES += wolfcrypt/src/tfm.c
-endif
-
-if BUILD_SLOWMATH
-src_libwolfssl_la_SOURCES += wolfcrypt/src/integer.c
-endif
-
-if !BUILD_FIPS_V2
-if BUILD_ECC
-src_libwolfssl_la_SOURCES += wolfcrypt/src/ecc.c
-endif
-endif
-
-if BUILD_CURVE25519
-src_libwolfssl_la_SOURCES += wolfcrypt/src/curve25519.c
-endif
-
-if BUILD_ED25519
-src_libwolfssl_la_SOURCES += wolfcrypt/src/ed25519.c
-endif
-
-if BUILD_FEMATH
-if BUILD_CURVE25519_SMALL
-src_libwolfssl_la_SOURCES += wolfcrypt/src/fe_low_mem.c
-else
-if BUILD_INTELASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/fe_x25519_asm.S
-else
-if BUILD_ARMASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-32-curve25519.S
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-curve25519.S
-else
-src_libwolfssl_la_SOURCES += wolfcrypt/src/fe_operations.c
-endif
-endif
-endif
-endif
-
-if BUILD_GEMATH
-if BUILD_ED25519_SMALL
-src_libwolfssl_la_SOURCES += wolfcrypt/src/ge_low_mem.c
-else
-src_libwolfssl_la_SOURCES += wolfcrypt/src/ge_operations.c
-if !BUILD_FEMATH
-if BUILD_INTELASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/fe_x25519_asm.S
-else
-if BUILD_ARMASM
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/arm/armv8-curve25519.S
-else
-src_libwolfssl_la_SOURCES += wolfcrypt/src/fe_operations.c
-endif
-endif
-endif
-endif
-endif
-
-if BUILD_CURVE448
-src_libwolfssl_la_SOURCES += wolfcrypt/src/curve448.c
-endif
-
-if BUILD_ED448
-src_libwolfssl_la_SOURCES += wolfcrypt/src/ed448.c
-endif
-
-if BUILD_FE448
-src_libwolfssl_la_SOURCES += wolfcrypt/src/fe_448.c
-endif
-
-if BUILD_GE448
-src_libwolfssl_la_SOURCES += wolfcrypt/src/ge_448.c
-if !BUILD_FE448
-src_libwolfssl_la_SOURCES += wolfcrypt/src/fe_448.c
-endif
-endif
-
-if BUILD_LIBZ
-src_libwolfssl_la_SOURCES += wolfcrypt/src/compress.c
-endif
-
-if BUILD_PKCS7
-src_libwolfssl_la_SOURCES += wolfcrypt/src/pkcs7.c
-endif
-
-if BUILD_SRP
-src_libwolfssl_la_SOURCES += wolfcrypt/src/srp.c
-endif
-
-if BUILD_IDEA
-src_libwolfssl_la_SOURCES += wolfcrypt/src/idea.c
-endif
-
-if BUILD_AFALG
-src_libwolfssl_la_SOURCES += wolfcrypt/src/port/af_alg/wc_afalg.c
-endif
-
-if !BUILD_CRYPTONLY
-# ssl files
-src_libwolfssl_la_SOURCES += \
- src/internal.c \
- src/wolfio.c \
- src/keys.c \
- src/ssl.c \
- src/tls.c
-
-if BUILD_TLS13
-src_libwolfssl_la_SOURCES += src/tls13.c
-endif
-
-if BUILD_OCSP
-src_libwolfssl_la_SOURCES += src/ocsp.c
-endif
-
-if BUILD_CRL
-src_libwolfssl_la_SOURCES += src/crl.c
-endif
-
-if BUILD_SNIFFER
-src_libwolfssl_la_SOURCES += src/sniffer.c
-endif
-
-endif !BUILD_CRYPTONLY
-
-endif !BUILD_FIPS_RAND
diff --git a/client/wolfssl/src/internal.c b/client/wolfssl/src/internal.c
deleted file mode 100644
index fbdbb13..0000000
--- a/client/wolfssl/src/internal.c
+++ /dev/null
@@ -1,29412 +0,0 @@
-/* internal.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-/*
- * WOLFSSL_SMALL_CERT_VERIFY:
- * Verify the certificate signature without using DecodedCert. Doubles up
- * on some code but allows smaller peak heap memory usage.
- * Cannot be used with WOLFSSL_NONBLOCK_OCSP.
- * WOLFSSL_ALT_CERT_CHAINS:
- * Allows CA's to be presented by peer, but not part of a valid chain.
- * Default wolfSSL behavior is to require validation of all presented peer
- * certificates. This also allows loading intermediate CA's as trusted
- * and ignoring no signer failures for CA's up the chain to root.
- */
-
-
-#ifdef EXTERNAL_OPTS_OPENVPN
-#error EXTERNAL_OPTS_OPENVPN should not be defined\
- when building wolfSSL
-#endif
-
-#ifndef WOLFCRYPT_ONLY
-
-#include
-#include
-#include
-#include
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include
-#endif
-#if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && !defined(NO_SHA)
- #include
-#endif
-
-#ifdef HAVE_LIBZ
- #include "zlib.h"
-#endif
-
-#ifdef HAVE_NTRU
- #include "libntruencrypt/ntru_crypto.h"
-#endif
-
-#if defined(DEBUG_WOLFSSL) || defined(SHOW_SECRETS) || \
- defined(CHACHA_AEAD_TEST) || defined(WOLFSSL_SESSION_EXPORT_DEBUG)
- #ifndef NO_STDIO_FILESYSTEM
- #include
- #endif
-#endif
-
-#ifdef __sun
- #include
-#endif
-
-
-#define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; }
-
-#ifdef _MSC_VER
- /* disable for while(0) cases at the .c level for now */
- #pragma warning(disable:4127)
-#endif
-
-#if defined(WOLFSSL_CALLBACKS) && !defined(LARGE_STATIC_BUFFERS)
- #error \
-WOLFSSL_CALLBACKS needs LARGE_STATIC_BUFFERS, please add LARGE_STATIC_BUFFERS
-#endif
-
-#if defined(HAVE_SECURE_RENEGOTIATION) && defined(HAVE_RENEGOTIATION_INDICATION)
- #error Cannot use both secure-renegotiation and renegotiation-indication
-#endif
-
-#ifndef WOLFSSL_NO_TLS12
-
-#ifndef NO_WOLFSSL_CLIENT
- static int DoHelloVerifyRequest(WOLFSSL* ssl, const byte* input, word32*,
- word32);
- static int DoServerKeyExchange(WOLFSSL* ssl, const byte* input, word32*,
- word32);
- #ifndef NO_CERTS
- static int DoCertificateRequest(WOLFSSL* ssl, const byte* input, word32*,
- word32);
- #endif
- #ifdef HAVE_SESSION_TICKET
- static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32*,
- word32);
- #endif
-#endif
-
-
-#ifndef NO_WOLFSSL_SERVER
- static int DoClientKeyExchange(WOLFSSL* ssl, byte* input, word32*, word32);
- #if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)) && !defined(WOLFSSL_NO_CLIENT_AUTH)
- static int DoCertificateVerify(WOLFSSL* ssl, byte*, word32*, word32);
- #endif
- #ifdef WOLFSSL_DTLS
- static int SendHelloVerifyRequest(WOLFSSL*, const byte*, byte);
- #endif /* WOLFSSL_DTLS */
-#endif
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#ifdef WOLFSSL_DTLS
- static WC_INLINE int DtlsCheckWindow(WOLFSSL* ssl);
- static WC_INLINE int DtlsUpdateWindow(WOLFSSL* ssl);
-#endif
-
-
-enum processReply {
- doProcessInit = 0,
-#ifndef NO_WOLFSSL_SERVER
- runProcessOldClientHello,
-#endif
- getRecordLayerHeader,
- getData,
- verifyEncryptedMessage,
- decryptMessage,
- verifyMessage,
- runProcessingOneMessage
-};
-
-
-#ifndef WOLFSSL_NO_TLS12
-#if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
-
-/* Server random bytes for TLS v1.3 described downgrade protection mechanism. */
-static const byte tls13Downgrade[7] = {
- 0x44, 0x4f, 0x57, 0x4e, 0x47, 0x52, 0x44
-};
-#define TLS13_DOWNGRADE_SZ sizeof(tls13Downgrade)
-
-#endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
-
-#if !defined(NO_OLD_TLS) && !defined(WOLFSSL_AEAD_ONLY)
-static int SSL_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz,
- int padSz, int content, int verify);
-
-#endif
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#ifdef HAVE_QSH
- int QSH_Init(WOLFSSL* ssl);
-#endif
-
-#ifdef WOLFSSL_RENESAS_TSIP_TLS
- int tsip_useable(const WOLFSSL *ssl);
- int tsip_generatePremasterSecret();
- int tsip_generateEncryptPreMasterSecret(WOLFSSL *ssl, byte *out, word32 *outSz);
-#endif
-int IsTLS(const WOLFSSL* ssl)
-{
- if (ssl->version.major == SSLv3_MAJOR && ssl->version.minor >=TLSv1_MINOR)
- return 1;
-
- return 0;
-}
-
-
-int IsAtLeastTLSv1_2(const WOLFSSL* ssl)
-{
- if (ssl->version.major == SSLv3_MAJOR && ssl->version.minor >=TLSv1_2_MINOR)
- return 1;
-#ifdef WOLFSSL_DTLS
- if (ssl->version.major == DTLS_MAJOR && ssl->version.minor <= DTLSv1_2_MINOR)
- return 1;
-#endif
-
- return 0;
-}
-
-int IsAtLeastTLSv1_3(const ProtocolVersion pv)
-{
- return (pv.major == SSLv3_MAJOR && pv.minor >= TLSv1_3_MINOR);
-}
-
-static WC_INLINE int IsEncryptionOn(WOLFSSL* ssl, int isSend)
-{
- (void)isSend;
-
- #ifdef WOLFSSL_DTLS
- /* For DTLS, epoch 0 is always not encrypted. */
- if (ssl->options.dtls && !isSend && ssl->keys.curEpoch == 0)
- return 0;
- #endif /* WOLFSSL_DTLS */
-
-#ifdef WOLFSSL_TLS13
- if (isSend)
- return ssl->encrypt.setup;
- else
- return ssl->decrypt.setup;
-#else
- return ssl->keys.encryptionOn;
-#endif
-}
-
-
-#if defined(WOLFSSL_DTLS) || !defined(WOLFSSL_NO_TLS12)
-/* If SCTP is not enabled returns the state of the dtls option.
- * If SCTP is enabled returns dtls && !sctp. */
-static WC_INLINE int IsDtlsNotSctpMode(WOLFSSL* ssl)
-{
- int result = ssl->options.dtls;
-
- if (result) {
-#ifdef WOLFSSL_SCTP
- result = !ssl->options.dtlsSctp;
-#endif
- }
-
- return result;
-}
-#endif /* DTLS || !WOLFSSL_NO_TLS12 */
-
-
-#ifdef HAVE_QSH
-/* free all structs that where used with QSH */
-static int QSH_FreeAll(WOLFSSL* ssl)
-{
- QSHKey* key = ssl->QSH_Key;
- QSHKey* preKey = NULL;
- QSHSecret* secret = ssl->QSH_secret;
- QSHScheme* list = NULL;
- QSHScheme* preList = NULL;
-
- /* free elements in struct */
- while (key) {
- preKey = key;
- if (key->pri.buffer) {
- ForceZero(key->pri.buffer, key->pri.length);
- XFREE(key->pri.buffer, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- }
- if (key->pub.buffer)
- XFREE(key->pub.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- key = (QSHKey*)key->next;
-
- /* free struct */
- XFREE(preKey, ssl->heap, DYNAMIC_TYPE_QSH);
- }
-
-
- /* free all of peers QSH keys */
- key = ssl->peerQSHKey;
- while (key) {
- preKey = key;
- if (key->pri.buffer) {
- ForceZero(key->pri.buffer, key->pri.length);
- XFREE(key->pri.buffer, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- }
- if (key->pub.buffer)
- XFREE(key->pub.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- key = (QSHKey*)key->next;
-
- /* free struct */
- XFREE(preKey, ssl->heap, DYNAMIC_TYPE_QSH);
- }
- key = NULL;
-
- /* free secret information */
- if (secret) {
- /* free up the QSHScheme list in QSHSecret */
- if (secret->list)
- list = secret->list;
- while (list) {
- preList = list;
- if (list->PK)
- XFREE(list->PK, ssl->heap, DYNAMIC_TYPE_SECRET);
- list = (QSHScheme*)list->next;
- XFREE(preList, ssl->heap, DYNAMIC_TYPE_QSH);
- }
-
- /* free secret buffers */
- if (secret->SerSi) {
- if (secret->SerSi->buffer) {
- /* clear extra secret material that supplemented Master Secret*/
- ForceZero(secret->SerSi->buffer, secret->SerSi->length);
- XFREE(secret->SerSi->buffer, ssl->heap, DYNAMIC_TYPE_SECRET);
- }
- XFREE(secret->SerSi, ssl->heap, DYNAMIC_TYPE_SECRET);
- }
- if (secret->CliSi) {
- if (secret->CliSi->buffer) {
- /* clear extra secret material that supplemented Master Secret*/
- ForceZero(secret->CliSi->buffer, secret->CliSi->length);
- XFREE(secret->CliSi->buffer, ssl->heap, DYNAMIC_TYPE_SECRET);
- }
- XFREE(secret->CliSi, ssl->heap, DYNAMIC_TYPE_SECRET);
- }
- }
- XFREE(secret, ssl->heap, DYNAMIC_TYPE_QSH);
- secret = NULL;
-
- return 0;
-}
-#endif
-
-
-#ifdef HAVE_NTRU
-static WOLFSSL_GLOBAL WC_RNG* rng;
-static WOLFSSL_GLOBAL wolfSSL_Mutex* rngMutex;
-
-static word32 GetEntropy(unsigned char* out, word32 num_bytes)
-{
- int ret = 0;
-
- if (rng == NULL) {
- if ((rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), 0,
- DYNAMIC_TYPE_RNG)) == NULL)
- return DRBG_OUT_OF_MEMORY;
- wc_InitRng(rng);
- }
-
- if (rngMutex == NULL) {
- if ((rngMutex = (wolfSSL_Mutex*)XMALLOC(sizeof(wolfSSL_Mutex), 0,
- DYNAMIC_TYPE_MUTEX)) == NULL)
- return DRBG_OUT_OF_MEMORY;
- wc_InitMutex(rngMutex);
- }
-
- ret |= wc_LockMutex(rngMutex);
- ret |= wc_RNG_GenerateBlock(rng, out, num_bytes);
- ret |= wc_UnLockMutex(rngMutex);
-
- if (ret != 0)
- return DRBG_ENTROPY_FAIL;
-
- return DRBG_OK;
-}
-#endif /* HAVE_NTRU */
-
-#ifdef HAVE_LIBZ
-
- /* alloc user allocs to work with zlib */
- static void* myAlloc(void* opaque, unsigned int item, unsigned int size)
- {
- (void)opaque;
- return XMALLOC(item * size, opaque, DYNAMIC_TYPE_LIBZ);
- }
-
-
- static void myFree(void* opaque, void* memory)
- {
- (void)opaque;
- XFREE(memory, opaque, DYNAMIC_TYPE_LIBZ);
- }
-
-
- /* init zlib comp/decomp streams, 0 on success */
- static int InitStreams(WOLFSSL* ssl)
- {
- ssl->c_stream.zalloc = (alloc_func)myAlloc;
- ssl->c_stream.zfree = (free_func)myFree;
- ssl->c_stream.opaque = (voidpf)ssl->heap;
-
- if (deflateInit(&ssl->c_stream, Z_DEFAULT_COMPRESSION) != Z_OK)
- return ZLIB_INIT_ERROR;
-
- ssl->didStreamInit = 1;
-
- ssl->d_stream.zalloc = (alloc_func)myAlloc;
- ssl->d_stream.zfree = (free_func)myFree;
- ssl->d_stream.opaque = (voidpf)ssl->heap;
-
- if (inflateInit(&ssl->d_stream) != Z_OK) return ZLIB_INIT_ERROR;
-
- return 0;
- }
-
-
- static void FreeStreams(WOLFSSL* ssl)
- {
- if (ssl->didStreamInit) {
- deflateEnd(&ssl->c_stream);
- inflateEnd(&ssl->d_stream);
- }
- }
-
-
- /* compress in to out, return out size or error */
- static int myCompress(WOLFSSL* ssl, byte* in, int inSz, byte* out, int outSz)
- {
- int err;
- int currTotal = (int)ssl->c_stream.total_out;
-
- ssl->c_stream.next_in = in;
- ssl->c_stream.avail_in = inSz;
- ssl->c_stream.next_out = out;
- ssl->c_stream.avail_out = outSz;
-
- err = deflate(&ssl->c_stream, Z_SYNC_FLUSH);
- if (err != Z_OK && err != Z_STREAM_END) return ZLIB_COMPRESS_ERROR;
-
- return (int)ssl->c_stream.total_out - currTotal;
- }
-
-
- /* decompress in to out, return out size or error */
- static int myDeCompress(WOLFSSL* ssl, byte* in,int inSz, byte* out,int outSz)
- {
- int err;
- int currTotal = (int)ssl->d_stream.total_out;
-
- ssl->d_stream.next_in = in;
- ssl->d_stream.avail_in = inSz;
- ssl->d_stream.next_out = out;
- ssl->d_stream.avail_out = outSz;
-
- err = inflate(&ssl->d_stream, Z_SYNC_FLUSH);
- if (err != Z_OK && err != Z_STREAM_END) return ZLIB_DECOMPRESS_ERROR;
-
- return (int)ssl->d_stream.total_out - currTotal;
- }
-
-#endif /* HAVE_LIBZ */
-
-
-#ifdef WOLFSSL_SESSION_EXPORT
-#ifdef WOLFSSL_DTLS
-/* serializes the cipher specs struct for exporting */
-static int ExportCipherSpecState(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
-{
- word32 idx = 0;
- CipherSpecs* specs;
-
- WOLFSSL_ENTER("ExportCipherSpecState");
-
- if (exp == NULL || ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- specs= &(ssl->specs);
-
- if (DTLS_EXPORT_SPC_SZ > len) {
- return BUFFER_E;
- }
-
- XMEMSET(exp, 0, DTLS_EXPORT_SPC_SZ);
-
- c16toa(specs->key_size, exp + idx); idx += OPAQUE16_LEN;
- c16toa(specs->iv_size, exp + idx); idx += OPAQUE16_LEN;
- c16toa(specs->block_size, exp + idx); idx += OPAQUE16_LEN;
- c16toa(specs->aead_mac_size, exp + idx); idx += OPAQUE16_LEN;
- exp[idx++] = specs->bulk_cipher_algorithm;
- exp[idx++] = specs->cipher_type;
- exp[idx++] = specs->mac_algorithm;
- exp[idx++] = specs->kea;
- exp[idx++] = specs->sig_algo;
- exp[idx++] = specs->hash_size;
- exp[idx++] = specs->pad_size;
- exp[idx++] = specs->static_ecdh;
-
- if (idx != DTLS_EXPORT_SPC_SZ) {
- WOLFSSL_MSG("DTLS_EXPORT_SPC_SZ needs updated and export version");
- return DTLS_EXPORT_VER_E;
- }
-
- WOLFSSL_LEAVE("ExportCipherSpecState", idx);
- (void)ver;
- return idx;
-}
-
-
-/* serializes the key struct for exporting */
-static int ExportKeyState(WOLFSSL* ssl, byte* exp, word32 len, byte ver,
- byte small)
-{
- word32 idx = 0;
- byte sz;
- Keys* keys;
-
- WOLFSSL_ENTER("ExportKeyState");
-
- if (exp == NULL || ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- keys = &(ssl->keys);
-
- if (DTLS_EXPORT_MIN_KEY_SZ > len) {
- WOLFSSL_MSG("Buffer not large enough for minimum key struct size");
- return BUFFER_E;
- }
-
- XMEMSET(exp, 0, DTLS_EXPORT_MIN_KEY_SZ);
-
- c32toa(keys->peer_sequence_number_hi, exp + idx); idx += OPAQUE32_LEN;
- c32toa(keys->peer_sequence_number_lo, exp + idx); idx += OPAQUE32_LEN;
- c32toa(keys->sequence_number_hi, exp + idx); idx += OPAQUE32_LEN;
- c32toa(keys->sequence_number_lo, exp + idx); idx += OPAQUE32_LEN;
-
- c16toa(keys->peerSeq[0].nextEpoch, exp + idx); idx += OPAQUE16_LEN;
- c16toa(keys->peerSeq[0].nextSeq_hi, exp + idx); idx += OPAQUE16_LEN;
- c32toa(keys->peerSeq[0].nextSeq_lo, exp + idx); idx += OPAQUE32_LEN;
- c16toa(keys->curEpoch, exp + idx); idx += OPAQUE16_LEN;
- c16toa(keys->curSeq_hi, exp + idx); idx += OPAQUE16_LEN;
- c32toa(keys->curSeq_lo, exp + idx); idx += OPAQUE32_LEN;
- c16toa(keys->peerSeq[0].prevSeq_hi, exp + idx); idx += OPAQUE16_LEN;
- c32toa(keys->peerSeq[0].prevSeq_lo, exp + idx); idx += OPAQUE32_LEN;
-
- c16toa(keys->dtls_peer_handshake_number, exp + idx); idx += OPAQUE16_LEN;
- c16toa(keys->dtls_expected_peer_handshake_number, exp + idx);
- idx += OPAQUE16_LEN;
-
- c16toa(keys->dtls_sequence_number_hi, exp + idx); idx += OPAQUE16_LEN;
- c32toa(keys->dtls_sequence_number_lo, exp + idx); idx += OPAQUE32_LEN;
- c16toa(keys->dtls_prev_sequence_number_hi, exp + idx); idx += OPAQUE16_LEN;
- c32toa(keys->dtls_prev_sequence_number_lo, exp + idx); idx += OPAQUE32_LEN;
- c16toa(keys->dtls_epoch, exp + idx); idx += OPAQUE16_LEN;
- c16toa(keys->dtls_handshake_number, exp + idx); idx += OPAQUE16_LEN;
- c32toa(keys->encryptSz, exp + idx); idx += OPAQUE32_LEN;
- c32toa(keys->padSz, exp + idx); idx += OPAQUE32_LEN;
- exp[idx++] = keys->encryptionOn;
- exp[idx++] = keys->decryptedCur;
-
- /* from here on the buffer needs checked because is variable length that
- * can be larger than DTLS_EXPORT_MIN_KEY_SZ */
- {
- word32 i;
- if ((OPAQUE16_LEN * 2) + idx +
- (2 * (WOLFSSL_DTLS_WINDOW_WORDS * OPAQUE32_LEN)) > len) {
- WOLFSSL_MSG("Buffer not large enough for WOLFSSL_DTLS_WINDOW_WORDS");
- return BUFFER_E;
- }
-
- c16toa(WOLFSSL_DTLS_WINDOW_WORDS, exp + idx); idx += OPAQUE16_LEN;
- for (i = 0; i < WOLFSSL_DTLS_WINDOW_WORDS; i++) {
- c32toa(keys->peerSeq[0].window[i], exp + idx);
- idx += OPAQUE32_LEN;
- }
- c16toa(WOLFSSL_DTLS_WINDOW_WORDS, exp + idx); idx += OPAQUE16_LEN;
- for (i = 0; i < WOLFSSL_DTLS_WINDOW_WORDS; i++) {
- c32toa(keys->peerSeq[0].prevWindow[i], exp + idx);
- idx += OPAQUE32_LEN;
- }
- }
-
- if (idx >= len) {
- WOLFSSL_MSG("Buffer not large enough for truncated hmac flag");
- return BUFFER_E;
- }
-
-#ifdef HAVE_TRUNCATED_HMAC
- sz = ssl->truncated_hmac ? TRUNCATED_HMAC_SZ: ssl->specs.hash_size;
- exp[idx++] = ssl->truncated_hmac;
-#else
- sz = ssl->specs.hash_size;
- exp[idx++] = 0; /* no truncated hmac */
-#endif
-
- sz = (small)? 0: sz;
- if (idx + (sz * 2) + OPAQUE8_LEN > len) {
- WOLFSSL_MSG("Buffer not large enough for MAC secret");
- return BUFFER_E;
- }
-
- exp[idx++] = sz;
- if (sz > 0) {
- #ifndef WOLFSSL_AEAD_ONLY
- XMEMCPY(exp + idx, keys->client_write_MAC_secret, sz); idx += sz;
- XMEMCPY(exp + idx, keys->server_write_MAC_secret, sz); idx += sz;
- #else
- XMEMSET(exp + idx, 0, sz); idx += sz;
- XMEMSET(exp + idx, 0, sz); idx += sz;
- #endif
- }
-
- sz = (small)? 0: ssl->specs.key_size;
- if (idx + (sz * 2) + OPAQUE8_LEN > len) {
- WOLFSSL_MSG("Buffer not large enough for write key");
- return BUFFER_E;
- }
-
- exp[idx++] = sz;
- if (sz > 0) {
- XMEMCPY(exp + idx, keys->client_write_key, sz); idx += sz;
- XMEMCPY(exp + idx, keys->server_write_key, sz); idx += sz;
- }
-
- sz = (small)? 0: ssl->specs.iv_size;
- if (idx + (sz * 2) + OPAQUE8_LEN + AEAD_MAX_EXP_SZ > len) {
- WOLFSSL_MSG("Buffer not large enough for IVs");
- return BUFFER_E;
- }
-
- exp[idx++] = sz;
- if (sz > 0) {
- XMEMCPY(exp + idx, keys->client_write_IV, sz); idx += sz;
- XMEMCPY(exp + idx, keys->server_write_IV, sz); idx += sz;
- }
- XMEMCPY(exp + idx, keys->aead_exp_IV, AEAD_MAX_EXP_SZ);
- idx += AEAD_MAX_EXP_SZ;
-
- sz = (small)? 0: AEAD_MAX_IMP_SZ;
- if (idx + (sz * 2) + OPAQUE8_LEN > len) {
- WOLFSSL_MSG("Buffer not large enough for imp IVs");
- return BUFFER_E;
- }
- exp[idx++] = sz;
- if (sz > 0) {
- XMEMCPY(exp + idx, keys->aead_enc_imp_IV, sz); idx += sz;
- XMEMCPY(exp + idx, keys->aead_dec_imp_IV, sz); idx += sz;
- }
-
- /* DTLS_EXPORT_KEY_SZ is max value. idx size can vary */
- if (idx > DTLS_EXPORT_KEY_SZ) {
- WOLFSSL_MSG("DTLS_EXPORT_KEY_SZ needs updated and export version");
- return DTLS_EXPORT_VER_E;
- }
-
- WOLFSSL_LEAVE("ExportKeyState", idx);
- (void)ver;
- return idx;
-}
-
-static int ImportCipherSpecState(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
-{
- word32 idx = 0;
- CipherSpecs* specs;
-
- WOLFSSL_ENTER("ImportCipherSpecState");
-
- if (exp == NULL || ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- specs= &(ssl->specs);
-
- if (DTLS_EXPORT_SPC_SZ > len) {
- WOLFSSL_MSG("Buffer not large enough for max spec struct size");
- return BUFFER_E;
- }
-
- ato16(exp + idx, &specs->key_size); idx += OPAQUE16_LEN;
- ato16(exp + idx, &specs->iv_size); idx += OPAQUE16_LEN;
- ato16(exp + idx, &specs->block_size); idx += OPAQUE16_LEN;
- ato16(exp + idx, &specs->aead_mac_size); idx += OPAQUE16_LEN;
- specs->bulk_cipher_algorithm = exp[idx++];
- specs->cipher_type = exp[idx++];
- specs->mac_algorithm = exp[idx++];
- specs->kea = exp[idx++];
- specs->sig_algo = exp[idx++];
- specs->hash_size = exp[idx++];
- specs->pad_size = exp[idx++];
- specs->static_ecdh = exp[idx++];
-
- WOLFSSL_LEAVE("ImportCipherSpecState", idx);
- (void)ver;
- return idx;
-}
-
-
-static int ImportKeyState(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
-{
- word32 idx = 0;
- byte sz;
- Keys* keys;
-
- WOLFSSL_ENTER("ImportKeyState");
-
- if (exp == NULL || ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- keys = &(ssl->keys);
-
- /* check minimum length -- includes byte used for size indicators */
- if (len < DTLS_EXPORT_MIN_KEY_SZ) {
- WOLFSSL_MSG("Buffer not large enough for minimum expected size");
- return BUFFER_E;
- }
- ato32(exp + idx, &keys->peer_sequence_number_hi); idx += OPAQUE32_LEN;
- ato32(exp + idx, &keys->peer_sequence_number_lo); idx += OPAQUE32_LEN;
- ato32(exp + idx, &keys->sequence_number_hi); idx += OPAQUE32_LEN;
- ato32(exp + idx, &keys->sequence_number_lo); idx += OPAQUE32_LEN;
-
- ato16(exp + idx, &keys->peerSeq[0].nextEpoch); idx += OPAQUE16_LEN;
- ato16(exp + idx, &keys->peerSeq[0].nextSeq_hi); idx += OPAQUE16_LEN;
- ato32(exp + idx, &keys->peerSeq[0].nextSeq_lo); idx += OPAQUE32_LEN;
- ato16(exp + idx, &keys->curEpoch); idx += OPAQUE16_LEN;
- ato16(exp + idx, &keys->curSeq_hi); idx += OPAQUE16_LEN;
- ato32(exp + idx, &keys->curSeq_lo); idx += OPAQUE32_LEN;
- ato16(exp + idx, &keys->peerSeq[0].prevSeq_hi); idx += OPAQUE16_LEN;
- ato32(exp + idx, &keys->peerSeq[0].prevSeq_lo); idx += OPAQUE32_LEN;
-
- ato16(exp + idx, &keys->dtls_peer_handshake_number); idx += OPAQUE16_LEN;
- ato16(exp + idx, &keys->dtls_expected_peer_handshake_number);
- idx += OPAQUE16_LEN;
-
- ato16(exp + idx, &keys->dtls_sequence_number_hi); idx += OPAQUE16_LEN;
- ato32(exp + idx, &keys->dtls_sequence_number_lo); idx += OPAQUE32_LEN;
- ato16(exp + idx, &keys->dtls_prev_sequence_number_hi); idx += OPAQUE16_LEN;
- ato32(exp + idx, &keys->dtls_prev_sequence_number_lo); idx += OPAQUE32_LEN;
- ato16(exp + idx, &keys->dtls_epoch); idx += OPAQUE16_LEN;
- ato16(exp + idx, &keys->dtls_handshake_number); idx += OPAQUE16_LEN;
- ato32(exp + idx, &keys->encryptSz); idx += OPAQUE32_LEN;
- ato32(exp + idx, &keys->padSz); idx += OPAQUE32_LEN;
- keys->encryptionOn = exp[idx++];
- keys->decryptedCur = exp[idx++];
-
- {
- word16 i, wordCount, wordAdj = 0;
-
- /* do window */
- ato16(exp + idx, &wordCount);
- idx += OPAQUE16_LEN;
-
- if (wordCount > WOLFSSL_DTLS_WINDOW_WORDS) {
- wordCount = WOLFSSL_DTLS_WINDOW_WORDS;
- wordAdj = (WOLFSSL_DTLS_WINDOW_WORDS - wordCount) * sizeof(word32);
- }
-
- XMEMSET(keys->peerSeq[0].window, 0xFF, DTLS_SEQ_SZ);
- for (i = 0; i < wordCount; i++) {
- ato32(exp + idx, &keys->peerSeq[0].window[i]);
- idx += OPAQUE32_LEN;
- }
- idx += wordAdj;
-
- /* do prevWindow */
- ato16(exp + idx, &wordCount);
- idx += OPAQUE16_LEN;
-
- if (wordCount > WOLFSSL_DTLS_WINDOW_WORDS) {
- wordCount = WOLFSSL_DTLS_WINDOW_WORDS;
- wordAdj = (WOLFSSL_DTLS_WINDOW_WORDS - wordCount) * sizeof(word32);
- }
-
- XMEMSET(keys->peerSeq[0].prevWindow, 0xFF, DTLS_SEQ_SZ);
- for (i = 0; i < wordCount; i++) {
- ato32(exp + idx, &keys->peerSeq[0].prevWindow[i]);
- idx += OPAQUE32_LEN;
- }
- idx += wordAdj;
-
- }
-
-#ifdef HAVE_TRUNCATED_HMAC
- ssl->truncated_hmac = exp[idx++];
-#else
- idx++; /* no truncated hmac */
-#endif
- sz = exp[idx++];
-#ifndef WOLFSSL_AEAD_ONLY
- if (sz > sizeof(keys->client_write_MAC_secret) || (sz * 2) + idx > len) {
- WOLFSSL_MSG("Buffer not large enough for MAC import");
- return BUFFER_E;
- }
- if (sz > 0) {
- XMEMCPY(keys->client_write_MAC_secret, exp + idx, sz); idx += sz;
- XMEMCPY(keys->server_write_MAC_secret, exp + idx, sz); idx += sz;
- }
-#else
- if (sz + idx > len) {
- return BUFFER_E;
- }
- idx += sz; idx += sz;
-#endif
-
- sz = exp[idx++];
- if (sz > sizeof(keys->client_write_key) || (sz * 2) + idx > len) {
- WOLFSSL_MSG("Buffer not large enough for key import");
- return BUFFER_E;
- }
- if (sz > 0) {
- XMEMCPY(keys->client_write_key, exp + idx, sz); idx += sz;
- XMEMCPY(keys->server_write_key, exp + idx, sz); idx += sz;
- }
-
- sz = exp[idx++];
- if (sz > sizeof(keys->client_write_IV) || (sz * 2) + idx > len) {
- WOLFSSL_MSG("Buffer not large enough for write IV import");
- return BUFFER_E;
- }
- if (sz > 0) {
- XMEMCPY(keys->client_write_IV, exp + idx, sz); idx += sz;
- XMEMCPY(keys->server_write_IV, exp + idx, sz); idx += sz;
- }
- XMEMCPY(keys->aead_exp_IV, exp + idx, AEAD_MAX_EXP_SZ);
- idx += AEAD_MAX_EXP_SZ;
-
- sz = exp[idx++];
- if (sz > sizeof(keys->aead_enc_imp_IV) || (sz * 2) + idx > len) {
- WOLFSSL_MSG("Buffer not large enough for imp IV import");
- return BUFFER_E;
- }
- if (sz > 0) {
- XMEMCPY(keys->aead_enc_imp_IV, exp + idx, sz); idx += sz;
- XMEMCPY(keys->aead_dec_imp_IV, exp + idx, sz); idx += sz;
- }
-
- WOLFSSL_LEAVE("ImportKeyState", idx);
- (void)ver;
- return idx;
-}
-
-
-/* copy over necessary information from Options struct to buffer
- * On success returns size of buffer used on failure returns a negative value */
-static int dtls_export_new(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
-{
- int idx = 0;
- word16 zero = 0;
- Options* options = &ssl->options;
-
- WOLFSSL_ENTER("dtls_export_new");
-
- if (exp == NULL || options == NULL || len < DTLS_EXPORT_OPT_SZ) {
- return BAD_FUNC_ARG;
- }
-
- XMEMSET(exp, 0, DTLS_EXPORT_OPT_SZ);
-
- /* these options are kept and sent to indicate verify status and strength
- * of handshake */
- exp[idx++] = options->sendVerify;
- exp[idx++] = options->verifyPeer;
- exp[idx++] = options->verifyNone;
- exp[idx++] = options->downgrade;
-#ifndef NO_DH
- c16toa(options->minDhKeySz, exp + idx); idx += OPAQUE16_LEN;
- c16toa(options->maxDhKeySz, exp + idx); idx += OPAQUE16_LEN;
- c16toa(options->dhKeySz, exp + idx); idx += OPAQUE16_LEN;
-#else
- c16toa(zero, exp + idx); idx += OPAQUE16_LEN;
- c16toa(zero, exp + idx); idx += OPAQUE16_LEN;
- c16toa(zero, exp + idx); idx += OPAQUE16_LEN;
-#endif
-#ifndef NO_RSA
- c16toa((word16)(options->minRsaKeySz), exp + idx); idx += OPAQUE16_LEN;
-#else
- c16toa(zero, exp + idx); idx += OPAQUE16_LEN;
-#endif
-#ifdef HAVE_ECC
- c16toa((word16)(options->minEccKeySz), exp + idx); idx += OPAQUE16_LEN;
-#else
- c16toa(zero, exp + idx); idx += OPAQUE16_LEN;
-#endif
-
- /* these options are kept to indicate state and behavior */
-#ifndef NO_PSK
- exp[idx++] = options->havePSK;
-#else
- exp[idx++] = 0;
-#endif
- exp[idx++] = options->sessionCacheOff;
- exp[idx++] = options->sessionCacheFlushOff;
- exp[idx++] = options->side;
- exp[idx++] = options->resuming;
- exp[idx++] = options->haveSessionId;
- exp[idx++] = options->tls;
- exp[idx++] = options->tls1_1;
- exp[idx++] = options->dtls;
- exp[idx++] = options->connReset;
- exp[idx++] = options->isClosed;
- exp[idx++] = options->closeNotify;
- exp[idx++] = options->sentNotify;
- exp[idx++] = options->usingCompression;
- exp[idx++] = options->haveRSA;
- exp[idx++] = options->haveECC;
- exp[idx++] = options->haveDH;
- exp[idx++] = options->haveNTRU;
- exp[idx++] = options->haveQSH;
- exp[idx++] = options->haveECDSAsig;
- exp[idx++] = options->haveStaticECC;
- exp[idx++] = options->havePeerVerify;
- exp[idx++] = options->usingPSK_cipher;
- exp[idx++] = options->usingAnon_cipher;
- exp[idx++] = options->sendAlertState;
- exp[idx++] = options->partialWrite;
- exp[idx++] = options->quietShutdown;
- exp[idx++] = options->groupMessages;
-#ifdef HAVE_POLY1305
- exp[idx++] = options->oldPoly;
-#else
- exp[idx++] = 0;
-#endif
-#ifdef HAVE_ANON
- exp[idx++] = options->haveAnon;
-#else
- exp[idx++] = 0;
-#endif
-#ifdef HAVE_SESSION_TICKET
- exp[idx++] = options->createTicket;
- exp[idx++] = options->useTicket;
-#ifdef WOLFSSL_TLS13
- if (ver > DTLS_EXPORT_VERSION_3) {
- exp[idx++] = options->noTicketTls13;
- }
-#else
- if (ver > DTLS_EXPORT_VERSION_3) {
- exp[idx++] = 0;
- }
-#endif
-#else
- exp[idx++] = 0;
- exp[idx++] = 0;
- if (ver > DTLS_EXPORT_VERSION_3) {
- exp[idx++] = 0;
- }
-#endif
- exp[idx++] = options->processReply;
- exp[idx++] = options->cipherSuite0;
- exp[idx++] = options->cipherSuite;
- exp[idx++] = options->serverState;
- exp[idx++] = options->clientState;
- exp[idx++] = options->handShakeState;
- exp[idx++] = options->handShakeDone;
- exp[idx++] = options->minDowngrade;
- exp[idx++] = options->connectState;
- exp[idx++] = options->acceptState;
- exp[idx++] = options->asyncState;
-
- /* version of connection */
- exp[idx++] = ssl->version.major;
- exp[idx++] = ssl->version.minor;
-
- (void)zero;
-
- /* check if changes were made and notify of need to update export version */
- switch (ver) {
- case DTLS_EXPORT_VERSION_3:
- if (idx != DTLS_EXPORT_OPT_SZ_3) {
- WOLFSSL_MSG("Update DTLS_EXPORT_OPT_SZ and version of export");
- return DTLS_EXPORT_VER_E;
- }
- break;
-
- case DTLS_EXPORT_VERSION:
- if (idx != DTLS_EXPORT_OPT_SZ) {
- WOLFSSL_MSG("Update DTLS_EXPORT_OPT_SZ and version of export");
- return DTLS_EXPORT_VER_E;
- }
- break;
-
- default:
- WOLFSSL_MSG("New version case needs added to wolfSSL export");
- return DTLS_EXPORT_VER_E;
- }
-
- WOLFSSL_LEAVE("dtls_export_new", idx);
-
- return idx;
-}
-
-
-/* copy items from Export struct to Options struct
- * On success returns size of buffer used on failure returns a negative value */
-static int dtls_export_load(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
-{
- int idx = 0;
- Options* options = &ssl->options;
-
- switch (ver) {
- case DTLS_EXPORT_VERSION:
- if (len < DTLS_EXPORT_OPT_SZ) {
- WOLFSSL_MSG("Sanity check on buffer size failed");
- return BAD_FUNC_ARG;
- }
- break;
-
- case DTLS_EXPORT_VERSION_3:
- if (len < DTLS_EXPORT_OPT_SZ_3) {
- WOLFSSL_MSG("Sanity check on buffer size failed");
- return BAD_FUNC_ARG;
- }
- break;
-
- default:
- WOLFSSL_MSG("Export version not supported");
- return BAD_FUNC_ARG;
- }
-
- if (exp == NULL || options == NULL) {
- return BAD_FUNC_ARG;
- }
-
-
- /* these options are kept and sent to indicate verify status and strength
- * of handshake */
- options->sendVerify = exp[idx++];
- options->verifyPeer = exp[idx++];
- options->verifyNone = exp[idx++];
- options->downgrade = exp[idx++];
-#ifndef NO_DH
- ato16(exp + idx, &(options->minDhKeySz)); idx += OPAQUE16_LEN;
- ato16(exp + idx, &(options->maxDhKeySz)); idx += OPAQUE16_LEN;
- ato16(exp + idx, &(options->dhKeySz)); idx += OPAQUE16_LEN;
-#else
- idx += OPAQUE16_LEN;
- idx += OPAQUE16_LEN;
- idx += OPAQUE16_LEN;
-#endif
-#ifndef NO_RSA
- ato16(exp + idx, (word16*)&(options->minRsaKeySz)); idx += OPAQUE16_LEN;
-#else
- idx += OPAQUE16_LEN;
-#endif
-#ifdef HAVE_ECC
- ato16(exp + idx, (word16*)&(options->minEccKeySz)); idx += OPAQUE16_LEN;
-#else
- idx += OPAQUE16_LEN;
-#endif
-
- /* these options are kept to indicate state and behavior */
-#ifndef NO_PSK
- options->havePSK = exp[idx++];
-#else
- idx++;
-#endif
- options->sessionCacheOff = exp[idx++];
- options->sessionCacheFlushOff = exp[idx++];
- options->side = exp[idx++];
- options->resuming = exp[idx++];
- options->haveSessionId = exp[idx++];
- options->tls = exp[idx++];
- options->tls1_1 = exp[idx++];
- options->dtls = exp[idx++];
- options->connReset = exp[idx++];
- options->isClosed = exp[idx++];
- options->closeNotify = exp[idx++];
- options->sentNotify = exp[idx++];
- options->usingCompression = exp[idx++];
- options->haveRSA = exp[idx++];
- options->haveECC = exp[idx++];
- options->haveDH = exp[idx++];
- options->haveNTRU = exp[idx++];
- options->haveQSH = exp[idx++];
- options->haveECDSAsig = exp[idx++];
- options->haveStaticECC = exp[idx++];
- options->havePeerVerify = exp[idx++];
- options->usingPSK_cipher = exp[idx++];
- options->usingAnon_cipher = exp[idx++];
- options->sendAlertState = exp[idx++];
- options->partialWrite = exp[idx++];
- options->quietShutdown = exp[idx++];
- options->groupMessages = exp[idx++];
-#ifdef HAVE_POLY1305
- options->oldPoly = exp[idx++]; /* set when to use old rfc way of poly*/
-#else
- idx++;
-#endif
-#ifdef HAVE_ANON
- options->haveAnon = exp[idx++]; /* User wants to allow Anon suites */
-#else
- idx++;
-#endif
-#ifdef HAVE_SESSION_TICKET
- options->createTicket = exp[idx++]; /* Server to create new Ticket */
- options->useTicket = exp[idx++]; /* Use Ticket not session cache */
-#ifdef WOLFSSL_TLS13
- if (ver > DTLS_EXPORT_VERSION_3) {
- options->noTicketTls13 = exp[idx++];/* Server won't create new Ticket */
- }
-#else
- if (ver > DTLS_EXPORT_VERSION_3) {
- exp[idx++] = 0;
- }
-#endif
-#else
- idx++;
- idx++;
- if (ver > DTLS_EXPORT_VERSION_3) {
- idx++;
- }
-#endif
- options->processReply = exp[idx++];
- options->cipherSuite0 = exp[idx++];
- options->cipherSuite = exp[idx++];
- options->serverState = exp[idx++];
- options->clientState = exp[idx++];
- options->handShakeState = exp[idx++];
- options->handShakeDone = exp[idx++];
- options->minDowngrade = exp[idx++];
- options->connectState = exp[idx++];
- options->acceptState = exp[idx++];
- options->asyncState = exp[idx++];
-
- /* version of connection */
- if (ssl->version.major != exp[idx++] || ssl->version.minor != exp[idx++]) {
- WOLFSSL_MSG("Version mismatch ie DTLS v1 vs v1.2");
- return VERSION_ERROR;
- }
-
- return idx;
-}
-
-#ifndef WOLFSSL_SESSION_EXPORT_NOPEER
-static int ExportPeerInfo(WOLFSSL* ssl, byte* exp, word32 len, byte ver)
-{
- int idx = 0;
- int ipSz = DTLS_EXPORT_IP; /* start as max size */
- int fam = 0;
- word16 port = 0;
- char ip[DTLS_EXPORT_IP];
-
- if (ver != DTLS_EXPORT_VERSION) {
- WOLFSSL_MSG("Export version not supported");
- return BAD_FUNC_ARG;
- }
-
- if (ssl == NULL || exp == NULL || len < sizeof(ip) + 3 * DTLS_EXPORT_LEN) {
- return BAD_FUNC_ARG;
- }
-
- if (ssl->ctx->CBGetPeer == NULL) {
- WOLFSSL_MSG("No get peer call back set");
- return BAD_FUNC_ARG;
- }
- if (ssl->ctx->CBGetPeer(ssl, ip, &ipSz, &port, &fam) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Get peer callback error");
- return SOCKET_ERROR_E;
- }
-
- /* check that ipSz/fam is not negative or too large since user can set cb */
- if (ipSz < 0 || ipSz > DTLS_EXPORT_IP || fam < 0) {
- WOLFSSL_MSG("Bad ipSz or fam returned from get peer callback");
- return SOCKET_ERROR_E;
- }
-
- c16toa((word16)fam, exp + idx); idx += DTLS_EXPORT_LEN;
- c16toa((word16)ipSz, exp + idx); idx += DTLS_EXPORT_LEN;
- XMEMCPY(exp + idx, ip, ipSz); idx += ipSz;
- c16toa(port, exp + idx); idx += DTLS_EXPORT_LEN;
-
- return idx;
-}
-#endif /* !WOLFSSL_SESSION_EXPORT_NOPEER */
-
-
-static int ImportPeerInfo(WOLFSSL* ssl, byte* buf, word32 len, byte ver)
-{
- word16 idx = 0;
- word16 ipSz;
- word16 fam;
- word16 port;
- char ip[DTLS_EXPORT_IP];
-
- if (ver != DTLS_EXPORT_VERSION && ver != DTLS_EXPORT_VERSION_3) {
- WOLFSSL_MSG("Export version not supported");
- return BAD_FUNC_ARG;
- }
-
- if (len == 0) {
- WOLFSSL_MSG("No peer info sent");
- return 0;
- }
-
- if (ssl == NULL || buf == NULL || len < 3 * DTLS_EXPORT_LEN) {
- return BAD_FUNC_ARG;
- }
-
- /* import sin family */
- ato16(buf + idx, &fam); idx += DTLS_EXPORT_LEN;
-
- /* import ip address idx, and ipSz are unsigned but cast for enum */
- ato16(buf + idx, &ipSz); idx += DTLS_EXPORT_LEN;
- if (ipSz >= sizeof(ip) || (word16)(idx + ipSz + DTLS_EXPORT_LEN) > len) {
- return BUFFER_E;
- }
- XMEMSET(ip, 0, sizeof(ip));
- XMEMCPY(ip, buf + idx, ipSz); idx += ipSz;
- ip[ipSz] = '\0'; /* with check that ipSz less than ip this is valid */
- ato16(buf + idx, &port); idx += DTLS_EXPORT_LEN;
-
- /* sanity check for a function to call, then use it to import peer info */
- if (ssl->ctx->CBSetPeer == NULL) {
- WOLFSSL_MSG("No set peer function");
- return BAD_FUNC_ARG;
- }
- if (ssl->ctx->CBSetPeer(ssl, ip, ipSz, port, fam) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error setting peer info");
- return SOCKET_ERROR_E;
- }
-
- return idx;
-}
-
-
-/* WOLFSSL_LOCAL function that serializes the current WOLFSSL session state only
- * buf is used to hold the serialized WOLFSSL struct and sz is the size of buf
- * passed in.
- * On success returns the size of serialized session state.*/
-int wolfSSL_dtls_export_state_internal(WOLFSSL* ssl, byte* buf, word32 sz)
-{
- int ret;
- word32 idx = 0;
- word32 totalLen = 0;
-
- WOLFSSL_ENTER("wolfSSL_dtls_export_state_internal");
-
- if (buf == NULL || ssl == NULL) {
- WOLFSSL_LEAVE("wolfSSL_dtls_export_state_internal", BAD_FUNC_ARG);
- return BAD_FUNC_ARG;
- }
-
- totalLen += DTLS_EXPORT_LEN * 2; /* 2 protocol bytes and 2 length bytes */
- /* each of the following have a 2 byte length before data */
- totalLen += DTLS_EXPORT_LEN + DTLS_EXPORT_MIN_KEY_SZ;
- if (totalLen > sz) {
- WOLFSSL_LEAVE("wolfSSL_dtls_export_state_internal", BUFFER_E);
- return BUFFER_E;
- }
-
- buf[idx++] = (byte)DTLS_EXPORT_STATE_PRO;
- buf[idx++] = ((byte)DTLS_EXPORT_STATE_PRO & 0xF0) |
- ((byte)DTLS_EXPORT_VERSION & 0X0F);
- idx += DTLS_EXPORT_LEN; /* leave room for total length */
-
- /* export keys struct and dtls state -- variable length stored in ret */
- idx += DTLS_EXPORT_LEN; /* leave room for length */
- if ((ret = ExportKeyState(ssl, buf + idx, sz - idx,
- DTLS_EXPORT_VERSION, 1)) < 0) {
- WOLFSSL_LEAVE("wolfSSL_dtls_export_state_internal", ret);
- return ret;
- }
- c16toa((word16)ret, buf + idx - DTLS_EXPORT_LEN); idx += ret;
-
- /* place total length of exported buffer minus 2 bytes protocol/version */
- c16toa((word16)(idx - DTLS_EXPORT_LEN), buf + DTLS_EXPORT_LEN);
-
-#ifdef WOLFSSL_SESSION_EXPORT_DEBUG
- /* if compiled with debug options then print the version, protocol, size */
- {
- char debug[256];
- XSNPRINTF(debug, sizeof(debug), "Exporting DTLS session state\n"
- "\tVersion : %d\n\tProtocol : %02X%01X\n\tLength of: %d\n\n"
- , (int)DTLS_EXPORT_VERSION, buf[0], (buf[1] >> 4), idx - 2);
- WOLFSSL_MSG(debug);
- }
-#endif /* WOLFSSL_SESSION_EXPORT_DEBUG */
-
- WOLFSSL_LEAVE("wolfSSL_dtls_export_state_internal", idx);
- return idx;
-}
-
-
-/* WOLFSSL_LOCAL function that serializes the current WOLFSSL session
- * buf is used to hold the serialized WOLFSSL struct and sz is the size of buf
- * passed in.
- * On success returns the size of serialized session.*/
-int wolfSSL_dtls_export_internal(WOLFSSL* ssl, byte* buf, word32 sz)
-{
- int ret;
- word32 idx = 0;
- word32 totalLen = 0;
-
- WOLFSSL_ENTER("wolfSSL_dtls_export_internal");
-
- if (buf == NULL || ssl == NULL) {
- WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", BAD_FUNC_ARG);
- return BAD_FUNC_ARG;
- }
-
- totalLen += DTLS_EXPORT_LEN * 2; /* 2 protocol bytes and 2 length bytes */
- /* each of the following have a 2 byte length before data */
- totalLen += DTLS_EXPORT_LEN + DTLS_EXPORT_OPT_SZ;
- totalLen += DTLS_EXPORT_LEN + DTLS_EXPORT_KEY_SZ;
- totalLen += DTLS_EXPORT_LEN + DTLS_EXPORT_SPC_SZ;
- totalLen += DTLS_EXPORT_LEN + ssl->buffers.dtlsCtx.peer.sz;
-
- if (totalLen > sz) {
- WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", BUFFER_E);
- return BUFFER_E;
- }
-
- buf[idx++] = (byte)DTLS_EXPORT_PRO;
- buf[idx++] = ((byte)DTLS_EXPORT_PRO & 0xF0) |
- ((byte)DTLS_EXPORT_VERSION & 0X0F);
-
- idx += DTLS_EXPORT_LEN; /* leave spot for length */
-
- c16toa((word16)DTLS_EXPORT_OPT_SZ, buf + idx); idx += DTLS_EXPORT_LEN;
- if ((ret = dtls_export_new(ssl, buf + idx, sz - idx,
- DTLS_EXPORT_VERSION)) < 0) {
- WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", ret);
- return ret;
- }
- idx += ret;
-
- /* export keys struct and dtls state -- variable length stored in ret */
- idx += DTLS_EXPORT_LEN; /* leave room for length */
- if ((ret = ExportKeyState(ssl, buf + idx, sz - idx,
- DTLS_EXPORT_VERSION, 0)) < 0) {
- WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", ret);
- return ret;
- }
- c16toa((word16)ret, buf + idx - DTLS_EXPORT_LEN); idx += ret;
-
- /* export of cipher specs struct */
- c16toa((word16)DTLS_EXPORT_SPC_SZ, buf + idx); idx += DTLS_EXPORT_LEN;
- if ((ret = ExportCipherSpecState(ssl, buf + idx, sz - idx,
- DTLS_EXPORT_VERSION)) < 0) {
- WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", ret);
- return ret;
- }
- idx += ret;
-
- /* export of dtls peer information */
- idx += DTLS_EXPORT_LEN;
-#ifdef WOLFSSL_SESSION_EXPORT_NOPEER
- ret = 0; /* not saving peer port/ip information */
-#else
- if ((ret = ExportPeerInfo(ssl, buf + idx, sz - idx,
- DTLS_EXPORT_VERSION)) < 0) {
- WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", ret);
- return ret;
- }
-#endif
- c16toa(ret, buf + idx - DTLS_EXPORT_LEN);
- idx += ret;
-
- /* place total length of exported buffer minus 2 bytes protocol/version */
- c16toa((word16)(idx - DTLS_EXPORT_LEN), buf + DTLS_EXPORT_LEN);
-
- /* if compiled with debug options then print the version, protocol, size */
-#ifdef WOLFSSL_SESSION_EXPORT_DEBUG
- {
- char debug[256];
- XSNPRINTF(debug, sizeof(debug), "Exporting DTLS session\n"
- "\tVersion : %d\n\tProtocol : %02X%01X\n\tLength of: %d\n\n"
- , (int)DTLS_EXPORT_VERSION, buf[0], (buf[1] >> 4), idx - 2);
- WOLFSSL_MSG(debug);
- }
-#endif /* WOLFSSL_SESSION_EXPORT_DEBUG */
-
- WOLFSSL_LEAVE("wolfSSL_dtls_export_internal", idx);
- return idx;
-}
-
-
-/* On success return amount of buffer consumed */
-int wolfSSL_dtls_import_state_internal(WOLFSSL* ssl, byte* buf, word32 sz)
-{
- word32 idx = 0;
- word16 length = 0;
- int version;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_dtls_import_state_internal");
- /* check at least enough room for protocol and length */
- if (sz < DTLS_EXPORT_LEN * 2 || ssl == NULL) {
- WOLFSSL_LEAVE("wolfSSL_dtls_import_state_internal", BAD_FUNC_ARG);
- return BAD_FUNC_ARG;
- }
-
- if (buf[idx++] != (byte)DTLS_EXPORT_STATE_PRO ||
- (buf[idx] & 0xF0) != ((byte)DTLS_EXPORT_PRO & 0xF0)) {
- WOLFSSL_MSG("Incorrect protocol");
- return BAD_FUNC_ARG;
- }
- version = buf[idx++] & 0x0F;
-
- ato16(buf + idx, &length); idx += DTLS_EXPORT_LEN;
- if (length > sz - DTLS_EXPORT_LEN) { /* subtract 2 for protocol */
- WOLFSSL_MSG("Buffer size sanity check failed");
- return BUFFER_E;
- }
-
-#ifdef WOLFSSL_SESSION_EXPORT_DEBUG
- /* if compiled with debug options then print the version, protocol, size */
- {
- char debug[256];
- XSNPRINTF(debug, sizeof(debug), "Importing DTLS session state\n"
- "\tVersion : %d\n\tProtocol : %02X%01X\n\tLength of: %d\n\n"
- , (int)version, buf[0], (buf[1] >> 4), length);
- WOLFSSL_MSG(debug);
- }
-#endif /* WOLFSSL_SESSION_EXPORT_DEBUG */
-
- /* perform sanity checks and extract Options information used */
- switch (version) {
- case DTLS_EXPORT_VERSION:
- break;
-
- default:
- WOLFSSL_MSG("Bad export state version");
- return BAD_FUNC_ARG;
-
- }
-
- /* perform sanity checks and extract Keys struct */
- if (DTLS_EXPORT_LEN + idx > sz) {
- WOLFSSL_MSG("Import Key struct error");
- return BUFFER_E;
- }
- ato16(buf + idx, &length); idx += DTLS_EXPORT_LEN;
- if (length > DTLS_EXPORT_KEY_SZ || length + idx > sz) {
- WOLFSSL_MSG("Import Key struct error");
- return BUFFER_E;
- }
- if ((ret = ImportKeyState(ssl, buf + idx, length, version)) < 0) {
- WOLFSSL_MSG("Import Key struct error");
- WOLFSSL_LEAVE("wolfSSL_dtls_import_state_internal", ret);
- return ret;
- }
- idx += ret;
-
- WOLFSSL_LEAVE("wolfSSL_dtls_import_state_internal", ret);
- return ret;
-}
-
-
-/* On success return amount of buffer consumed */
-int wolfSSL_dtls_import_internal(WOLFSSL* ssl, byte* buf, word32 sz)
-{
- word32 idx = 0;
- word16 length = 0;
- int version;
- int ret;
- int optSz;
-
- WOLFSSL_ENTER("wolfSSL_dtls_import_internal");
- /* check at least enough room for protocol and length */
- if (sz < DTLS_EXPORT_LEN * 2 || ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* sanity check on protocol ID and size of buffer */
- if (buf[idx++] != (byte)DTLS_EXPORT_PRO ||
- (buf[idx] & 0xF0) != ((byte)DTLS_EXPORT_PRO & 0xF0)) {
- /* don't increment on second idx to next get version */
-
- /* check if importing state only */
- return wolfSSL_dtls_import_state_internal(ssl, buf, sz);
- }
- version = buf[idx++] & 0x0F;
-
- ato16(buf + idx, &length); idx += DTLS_EXPORT_LEN;
- if (length > sz - DTLS_EXPORT_LEN) { /* subtract 2 for protocol */
- return BUFFER_E;
- }
-
- /* if compiled with debug options then print the version, protocol, size */
-#ifdef WOLFSSL_SESSION_EXPORT_DEBUG
- {
- char debug[256];
- XSNPRINTF(debug, sizeof(debug), "Importing DTLS session\n"
- "\tVersion : %d\n\tProtocol : %02X%01X\n\tLength of: %d\n\n"
- , (int)version, buf[0], (buf[1] >> 4), length);
- WOLFSSL_MSG(debug);
- }
-#endif /* WOLFSSL_SESSION_EXPORT_DEBUG */
-
- /* perform sanity checks and extract Options information used */
- switch (version) {
- case DTLS_EXPORT_VERSION:
- optSz = DTLS_EXPORT_OPT_SZ;
- break;
-
- case DTLS_EXPORT_VERSION_3:
- WOLFSSL_MSG("Importing older version 3");
- optSz = DTLS_EXPORT_OPT_SZ_3;
- break;
-
- default:
- WOLFSSL_MSG("Bad export version");
- return BAD_FUNC_ARG;
-
- }
-
- if (DTLS_EXPORT_LEN + optSz + idx > sz) {
- WOLFSSL_MSG("Import Options struct error");
- return BUFFER_E;
- }
- ato16(buf + idx, &length); idx += DTLS_EXPORT_LEN;
- if (length != optSz) {
- WOLFSSL_MSG("Import Options struct error");
- return BUFFER_E;
- }
- if ((ret = dtls_export_load(ssl, buf + idx, length, version)) < 0) {
- WOLFSSL_MSG("Import Options struct error");
- return ret;
- }
- idx += length;
-
- /* perform sanity checks and extract Keys struct */
- if (DTLS_EXPORT_LEN + idx > sz) {
- WOLFSSL_MSG("Import Key struct error");
- return BUFFER_E;
- }
- ato16(buf + idx, &length); idx += DTLS_EXPORT_LEN;
- if (length > DTLS_EXPORT_KEY_SZ || length + idx > sz) {
- WOLFSSL_MSG("Import Key struct error");
- return BUFFER_E;
- }
- if ((ret = ImportKeyState(ssl, buf + idx, length, version)) < 0) {
- WOLFSSL_MSG("Import Key struct error");
- return ret;
- }
- idx += ret;
-
- /* perform sanity checks and extract CipherSpecs struct */
- if (DTLS_EXPORT_LEN + DTLS_EXPORT_SPC_SZ + idx > sz) {
- WOLFSSL_MSG("Import CipherSpecs struct error");
- return BUFFER_E;
- }
- ato16(buf + idx, &length); idx += DTLS_EXPORT_LEN;
- if ( length != DTLS_EXPORT_SPC_SZ) {
- WOLFSSL_MSG("Import CipherSpecs struct error");
- return BUFFER_E;
- }
- if ((ret = ImportCipherSpecState(ssl, buf + idx, length, version)) < 0) {
- WOLFSSL_MSG("Import CipherSpecs struct error");
- return ret;
- }
- idx += ret;
-
- /* perform sanity checks and extract DTLS peer info */
- if (DTLS_EXPORT_LEN + idx > sz) {
- WOLFSSL_MSG("Import DTLS peer info error");
- return BUFFER_E;
- }
- ato16(buf + idx, &length); idx += DTLS_EXPORT_LEN;
- if (idx + length > sz) {
- WOLFSSL_MSG("Import DTLS peer info error");
- return BUFFER_E;
- }
- if ((ret = ImportPeerInfo(ssl, buf + idx, length, version)) < 0) {
- WOLFSSL_MSG("Import Peer Addr error");
- return ret;
- }
- idx += ret;
-
- SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE);
-
- /* set hmac function to use when verifying */
- if (ssl->options.tls == 1 || ssl->options.tls1_1 == 1 ||
- ssl->options.dtls == 1) {
- ssl->hmac = TLS_hmac;
- }
-
- /* make sure is a valid suite used */
- if (wolfSSL_get_cipher(ssl) == NULL) {
- WOLFSSL_MSG("Can not match cipher suite imported");
- return MATCH_SUITE_ERROR;
- }
-
- /* do not allow stream ciphers with DTLS, except for NULL cipher */
- if (ssl->specs.cipher_type == stream &&
- ssl->specs.bulk_cipher_algorithm != wolfssl_cipher_null) {
- WOLFSSL_MSG("Can not import stream ciphers for DTLS");
- return SANITY_CIPHER_E;
- }
-
- return idx;
-}
-#endif /* WOLFSSL_DTLS */
-#endif /* WOLFSSL_SESSION_EXPORT */
-
-
-void InitSSL_Method(WOLFSSL_METHOD* method, ProtocolVersion pv)
-{
- method->version = pv;
- method->side = WOLFSSL_CLIENT_END;
- method->downgrade = 0;
-}
-
-#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
-int InitSSL_Side(WOLFSSL* ssl, word16 side)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- /* set side */
- ssl->options.side = side;
-
- /* reset options that are side specific */
-#ifdef HAVE_NTRU
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- ssl->options.haveNTRU = 1; /* always on client side */
- /* server can turn on by loading key */
- }
-#endif
-#ifdef HAVE_ECC
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- ssl->options.haveECDSAsig = 1; /* always on client side */
- ssl->options.haveECC = 1; /* server turns on with ECC key cert */
- ssl->options.haveStaticECC = 1; /* server can turn on by loading key */
- }
-#elif defined(HAVE_ED25519) || defined(HAVE_ED448)
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- ssl->options.haveECDSAsig = 1; /* always on client side */
- ssl->options.haveECC = 1; /* server turns on with ECC key cert */
- }
-#endif
-
-#if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if ((ssl->ctx->method->version.major == SSLv3_MAJOR) &&
- (ssl->ctx->method->version.minor >= TLSv1_MINOR)) {
- ssl->options.haveEMS = 1;
- }
- #ifdef WOLFSSL_DTLS
- if (ssl->ctx->method->version.major == DTLS_MAJOR)
- ssl->options.haveEMS = 1;
- #endif /* WOLFSSL_DTLS */
- }
-#endif /* HAVE_EXTENDED_MASTER && !NO_WOLFSSL_CLIENT */
-
-#if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
- if (ssl->options.dtls && ssl->options.side == WOLFSSL_SERVER_END) {
- int ret;
- ret = wolfSSL_DTLS_SetCookieSecret(ssl, NULL, 0);
- if (ret != 0) {
- WOLFSSL_MSG("DTLS Cookie Secret error");
- return ret;
- }
- }
-#endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */
-
- return InitSSL_Suites(ssl);
-}
-#endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
-
-/* Initialize SSL context, return 0 on success */
-int InitSSL_Ctx(WOLFSSL_CTX* ctx, WOLFSSL_METHOD* method, void* heap)
-{
- int ret = 0;
-
- XMEMSET(ctx, 0, sizeof(WOLFSSL_CTX));
-
- ctx->method = method;
- ctx->refCount = 1; /* so either CTX_free or SSL_free can release */
- ctx->heap = ctx; /* defaults to self */
- ctx->timeout = WOLFSSL_SESSION_TIMEOUT;
- ctx->minDowngrade = WOLFSSL_MIN_DOWNGRADE; /* current default: TLSv1_MINOR */
-
- if (wc_InitMutex(&ctx->countMutex) < 0) {
- WOLFSSL_MSG("Mutex error on CTX init");
- ctx->err = CTX_INIT_MUTEX_E;
- return BAD_MUTEX_E;
- }
-
-#ifndef NO_DH
- ctx->minDhKeySz = MIN_DHKEY_SZ;
- ctx->maxDhKeySz = MAX_DHKEY_SZ;
-#endif
-#ifndef NO_RSA
- ctx->minRsaKeySz = MIN_RSAKEY_SZ;
-#endif
-#ifdef HAVE_ECC
- ctx->minEccKeySz = MIN_ECCKEY_SZ;
- ctx->eccTempKeySz = ECDHE_SIZE;
-#endif
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- ctx->verifyDepth = MAX_CHAIN_DEPTH;
-#endif
-#ifdef OPENSSL_EXTRA
- ctx->cbioFlag = WOLFSSL_CBIO_NONE;
-#endif
-
-#ifndef WOLFSSL_USER_IO
- #ifdef MICRIUM
- ctx->CBIORecv = MicriumReceive;
- ctx->CBIOSend = MicriumSend;
- #ifdef WOLFSSL_DTLS
- if (method->version.major == DTLS_MAJOR) {
- ctx->CBIORecv = MicriumReceiveFrom;
- ctx->CBIOSend = MicriumSendTo;
- }
- #ifdef WOLFSSL_SESSION_EXPORT
- #error Micrium port does not support DTLS session export yet
- #endif
- #endif
- #elif defined WOLFSSL_UIP
- ctx->CBIORecv = uIPReceive;
- ctx->CBIOSend = uIPSend;
- #ifdef WOLFSSL_DTLS
- if (method->version.major == DTLS_MAJOR) {
- ctx->CBIOSendTo = uIPSendTo;
- ctx->CBIORecvFrom = uIPRecvFrom;
- }
- #endif
- #else
- ctx->CBIORecv = EmbedReceive;
- ctx->CBIOSend = EmbedSend;
- #ifdef WOLFSSL_DTLS
- if (method->version.major == DTLS_MAJOR) {
- ctx->CBIORecv = EmbedReceiveFrom;
- ctx->CBIOSend = EmbedSendTo;
- }
- #ifdef WOLFSSL_SESSION_EXPORT
- ctx->CBGetPeer = EmbedGetPeer;
- ctx->CBSetPeer = EmbedSetPeer;
- #endif
- #endif
- #endif /* MICRIUM */
-#endif /* WOLFSSL_USER_IO */
-
-#ifdef HAVE_NETX
- ctx->CBIORecv = NetX_Receive;
- ctx->CBIOSend = NetX_Send;
-#elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
- ctx->CBIORecv = Mynewt_Receive;
- ctx->CBIOSend = Mynewt_Send;
-#elif defined(WOLFSSL_GNRC)
- ctx->CBIORecv = GNRC_ReceiveFrom;
- ctx->CBIOSend = GNRC_SendTo;
-#endif
-
-#ifdef HAVE_NTRU
- if (method->side == WOLFSSL_CLIENT_END)
- ctx->haveNTRU = 1; /* always on client side */
- /* server can turn on by loading key */
-#endif
-#ifdef HAVE_ECC
- if (method->side == WOLFSSL_CLIENT_END) {
- ctx->haveECDSAsig = 1; /* always on client side */
- ctx->haveECC = 1; /* server turns on with ECC key cert */
- ctx->haveStaticECC = 1; /* server can turn on by loading key */
- }
-#elif defined(HAVE_ED25519) || defined(HAVE_ED448)
- if (method->side == WOLFSSL_CLIENT_END) {
- ctx->haveECDSAsig = 1; /* always on client side */
- ctx->haveECC = 1; /* server turns on with ECC key cert */
- }
-#endif
-
- ctx->devId = INVALID_DEVID;
-
-#if defined(WOLFSSL_DTLS)
- #ifdef WOLFSSL_SCTP
- ctx->dtlsMtuSz = MAX_RECORD_SIZE;
- #elif defined(WOLFSSL_DTLS_MTU)
- ctx->dtlsMtuSz = MAX_MTU;
- #endif
-#endif
-
-#ifndef NO_CERTS
- ctx->cm = wolfSSL_CertManagerNew_ex(heap);
- if (ctx->cm == NULL) {
- WOLFSSL_MSG("Bad Cert Manager New");
- return BAD_CERT_MANAGER_ERROR;
- }
- #ifdef OPENSSL_EXTRA
- /* setup WOLFSSL_X509_STORE */
- ctx->x509_store.cm = ctx->cm;
- #endif
-#endif
-
-#if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
- if (method->side == WOLFSSL_CLIENT_END) {
- if ((method->version.major == SSLv3_MAJOR) &&
- (method->version.minor >= TLSv1_MINOR)) {
-
- ctx->haveEMS = 1;
- }
-#ifdef WOLFSSL_DTLS
- if (method->version.major == DTLS_MAJOR)
- ctx->haveEMS = 1;
-#endif /* WOLFSSL_DTLS */
- }
-#endif /* HAVE_EXTENDED_MASTER && !NO_WOLFSSL_CLIENT */
-
-#if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
- ctx->ticketHint = SESSION_TICKET_HINT_DEFAULT;
-#endif
-
-#ifdef HAVE_WOLF_EVENT
- ret = wolfEventQueue_Init(&ctx->event_queue);
-#endif /* HAVE_WOLF_EVENT */
-
-#ifdef WOLFSSL_EARLY_DATA
- ctx->maxEarlyDataSz = MAX_EARLY_DATA_SZ;
-#endif
-
- ctx->heap = heap; /* wolfSSL_CTX_load_static_memory sets */
- ctx->verifyDepth = MAX_CHAIN_DEPTH;
-
- return ret;
-}
-
-
-/* In case contexts are held in array and don't want to free actual ctx */
-void SSL_CtxResourceFree(WOLFSSL_CTX* ctx)
-{
-#if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && \
- defined(HAVE_TLS_EXTENSIONS) && !defined(NO_WOLFSSL_SERVER)
- int i;
-#endif
-
-#ifdef HAVE_WOLF_EVENT
- wolfEventQueue_Free(&ctx->event_queue);
-#endif /* HAVE_WOLF_EVENT */
-
-#ifdef WOLFSSL_STATIC_MEMORY
- if (ctx->onHeap == 1) {
- XFREE(ctx->method, ctx->heap, DYNAMIC_TYPE_METHOD);
- }
- else {
- XFREE(ctx->method, NULL, DYNAMIC_TYPE_METHOD);
- }
-#else
- XFREE(ctx->method, ctx->heap, DYNAMIC_TYPE_METHOD);
-#endif
- ctx->method = NULL;
- if (ctx->suites) {
- XFREE(ctx->suites, ctx->heap, DYNAMIC_TYPE_SUITES);
- ctx->suites = NULL;
- }
-
-#ifndef NO_DH
- XFREE(ctx->serverDH_G.buffer, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ctx->serverDH_G.buffer = NULL;
- XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ctx->serverDH_P.buffer = NULL;
-#endif /* !NO_DH */
-
-#ifdef SINGLE_THREADED
- if (ctx->rng) {
- wc_FreeRng(ctx->rng);
- XFREE(ctx->rng, ctx->heap, DYNAMIC_TYPE_RNG);
- ctx->rng = NULL;
- }
-#endif /* SINGLE_THREADED */
-
-#ifndef NO_CERTS
- FreeDer(&ctx->privateKey);
- FreeDer(&ctx->certificate);
- #ifdef KEEP_OUR_CERT
- if (ctx->ourCert && ctx->ownOurCert) {
- FreeX509(ctx->ourCert);
- XFREE(ctx->ourCert, ctx->heap, DYNAMIC_TYPE_X509);
- ctx->ourCert = NULL;
- }
- #endif /* KEEP_OUR_CERT */
- FreeDer(&ctx->certChain);
- wolfSSL_CertManagerFree(ctx->cm);
- ctx->cm = NULL;
- #ifdef OPENSSL_EXTRA
- /* ctx->cm was free'd so cm of x509 store should now be NULL */
- if (ctx->x509_store_pt != NULL) {
- ctx->x509_store_pt->cm = NULL;
- }
- wolfSSL_X509_STORE_free(ctx->x509_store_pt);
- while (ctx->ca_names != NULL) {
- WOLFSSL_STACK *next = ctx->ca_names->next;
- wolfSSL_X509_NAME_free(ctx->ca_names->data.name);
- XFREE(ctx->ca_names, NULL, DYNAMIC_TYPE_OPENSSL);
- ctx->ca_names = next;
- }
- #endif
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
- while (ctx->x509Chain != NULL) {
- WOLFSSL_STACK *next = ctx->x509Chain->next;
- wolfSSL_X509_free(ctx->x509Chain->data.x509);
- XFREE(ctx->x509Chain, NULL, DYNAMIC_TYPE_OPENSSL);
- ctx->x509Chain = next;
- }
- #endif
-#endif /* !NO_CERTS */
-
-#ifdef HAVE_TLS_EXTENSIONS
- TLSX_FreeAll(ctx->extensions, ctx->heap);
-
-#ifndef NO_WOLFSSL_SERVER
-#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
- || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
- if (ctx->certOcspRequest) {
- FreeOcspRequest(ctx->certOcspRequest);
- XFREE(ctx->certOcspRequest, ctx->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- }
-#endif
-
-#ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
- for (i = 0; i < MAX_CHAIN_DEPTH; i++) {
- if (ctx->chainOcspRequest[i]) {
- FreeOcspRequest(ctx->chainOcspRequest[i]);
- XFREE(ctx->chainOcspRequest[i], ctx->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- ctx->chainOcspRequest[i] = NULL;
- }
- }
-#endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
-#endif /* !NO_WOLFSSL_SERVER */
-
-#endif /* HAVE_TLS_EXTENSIONS */
-#ifdef OPENSSL_EXTRA
- if(ctx->alpn_cli_protos) {
- XFREE((void *)ctx->alpn_cli_protos, NULL, DYNAMIC_TYPE_OPENSSL);
- ctx->alpn_cli_protos = NULL;
- }
-#endif
-#ifdef WOLFSSL_STATIC_MEMORY
- if (ctx->heap != NULL) {
-#ifdef WOLFSSL_HEAP_TEST
- /* avoid dereferencing a test value */
- if (ctx->heap != (void*)WOLFSSL_HEAP_TEST)
-#endif
- {
- WOLFSSL_HEAP_HINT* hint = (WOLFSSL_HEAP_HINT*)(ctx->heap);
- wc_FreeMutex(&((WOLFSSL_HEAP*)(hint->memory))->memory_mutex);
- }
- }
-#endif /* WOLFSSL_STATIC_MEMORY */
-}
-
-
-void FreeSSL_Ctx(WOLFSSL_CTX* ctx)
-{
- int refCount;
-
- /* decrement CTX reference count */
- if ((refCount = SSL_CTX_RefCount(ctx, -1)) < 0) {
- /* check error state, if mutex error code then mutex init failed but
- * CTX was still malloc'd */
- if (ctx->err == CTX_INIT_MUTEX_E) {
- SSL_CtxResourceFree(ctx);
- XFREE(ctx, ctx->heap, DYNAMIC_TYPE_CTX);
- }
- return;
- }
-
- if (refCount == 0) {
- void* heap = ctx->heap;
- WOLFSSL_MSG("CTX ref count down to 0, doing full free");
- SSL_CtxResourceFree(ctx);
- wc_FreeMutex(&ctx->countMutex);
-#ifdef WOLFSSL_STATIC_MEMORY
- if (ctx->onHeap == 0) {
- heap = NULL;
- }
-#endif
- XFREE(ctx, heap, DYNAMIC_TYPE_CTX);
- (void)heap; /* not used in some builds */
- }
- else {
- (void)ctx;
- WOLFSSL_MSG("CTX ref count not 0 yet, no free");
- }
-}
-
-
-/* Set cipher pointers to null */
-void InitCiphers(WOLFSSL* ssl)
-{
-#ifdef BUILD_ARC4
- ssl->encrypt.arc4 = NULL;
- ssl->decrypt.arc4 = NULL;
-#endif
-#ifdef BUILD_DES3
- ssl->encrypt.des3 = NULL;
- ssl->decrypt.des3 = NULL;
-#endif
-#ifdef BUILD_AES
- ssl->encrypt.aes = NULL;
- ssl->decrypt.aes = NULL;
-#endif
-#ifdef HAVE_CAMELLIA
- ssl->encrypt.cam = NULL;
- ssl->decrypt.cam = NULL;
-#endif
-#ifdef HAVE_HC128
- ssl->encrypt.hc128 = NULL;
- ssl->decrypt.hc128 = NULL;
-#endif
-#ifdef BUILD_RABBIT
- ssl->encrypt.rabbit = NULL;
- ssl->decrypt.rabbit = NULL;
-#endif
-#ifdef HAVE_CHACHA
- ssl->encrypt.chacha = NULL;
- ssl->decrypt.chacha = NULL;
-#endif
-#if defined(HAVE_POLY1305) && defined(HAVE_ONE_TIME_AUTH)
- ssl->auth.poly1305 = NULL;
-#endif
- ssl->encrypt.setup = 0;
- ssl->decrypt.setup = 0;
-#ifdef HAVE_ONE_TIME_AUTH
- ssl->auth.setup = 0;
-#endif
-#ifdef HAVE_IDEA
- ssl->encrypt.idea = NULL;
- ssl->decrypt.idea = NULL;
-#endif
-}
-
-
-/* Free ciphers */
-void FreeCiphers(WOLFSSL* ssl)
-{
- (void)ssl;
-#ifdef BUILD_ARC4
- wc_Arc4Free(ssl->encrypt.arc4);
- wc_Arc4Free(ssl->decrypt.arc4);
- XFREE(ssl->encrypt.arc4, ssl->heap, DYNAMIC_TYPE_CIPHER);
- XFREE(ssl->decrypt.arc4, ssl->heap, DYNAMIC_TYPE_CIPHER);
-#endif
-#ifdef BUILD_DES3
- wc_Des3Free(ssl->encrypt.des3);
- wc_Des3Free(ssl->decrypt.des3);
- XFREE(ssl->encrypt.des3, ssl->heap, DYNAMIC_TYPE_CIPHER);
- XFREE(ssl->decrypt.des3, ssl->heap, DYNAMIC_TYPE_CIPHER);
-#endif
-#if defined(BUILD_AES) || defined(BUILD_AESGCM) /* See: InitKeys() in keys.c
- * on addition of BUILD_AESGCM
- * check (enc->aes, dec->aes) */
- wc_AesFree(ssl->encrypt.aes);
- wc_AesFree(ssl->decrypt.aes);
- #if (defined(BUILD_AESGCM) || defined(HAVE_AESCCM)) && \
- !defined(WOLFSSL_NO_TLS12)
- XFREE(ssl->decrypt.additional, ssl->heap, DYNAMIC_TYPE_AES_BUFFER);
- XFREE(ssl->encrypt.additional, ssl->heap, DYNAMIC_TYPE_AES_BUFFER);
- #endif
- XFREE(ssl->encrypt.aes, ssl->heap, DYNAMIC_TYPE_CIPHER);
- XFREE(ssl->decrypt.aes, ssl->heap, DYNAMIC_TYPE_CIPHER);
-#endif
-#ifdef CIPHER_NONCE
- XFREE(ssl->decrypt.nonce, ssl->heap, DYNAMIC_TYPE_AES_BUFFER);
- XFREE(ssl->encrypt.nonce, ssl->heap, DYNAMIC_TYPE_AES_BUFFER);
-#endif
-#ifdef HAVE_CAMELLIA
- XFREE(ssl->encrypt.cam, ssl->heap, DYNAMIC_TYPE_CIPHER);
- XFREE(ssl->decrypt.cam, ssl->heap, DYNAMIC_TYPE_CIPHER);
-#endif
-#ifdef HAVE_HC128
- XFREE(ssl->encrypt.hc128, ssl->heap, DYNAMIC_TYPE_CIPHER);
- XFREE(ssl->decrypt.hc128, ssl->heap, DYNAMIC_TYPE_CIPHER);
-#endif
-#ifdef BUILD_RABBIT
- XFREE(ssl->encrypt.rabbit, ssl->heap, DYNAMIC_TYPE_CIPHER);
- XFREE(ssl->decrypt.rabbit, ssl->heap, DYNAMIC_TYPE_CIPHER);
-#endif
-#ifdef HAVE_CHACHA
- XFREE(ssl->encrypt.chacha, ssl->heap, DYNAMIC_TYPE_CIPHER);
- XFREE(ssl->decrypt.chacha, ssl->heap, DYNAMIC_TYPE_CIPHER);
-#endif
-#if defined(HAVE_POLY1305) && defined(HAVE_ONE_TIME_AUTH)
- XFREE(ssl->auth.poly1305, ssl->heap, DYNAMIC_TYPE_CIPHER);
-#endif
-#ifdef HAVE_IDEA
- XFREE(ssl->encrypt.idea, ssl->heap, DYNAMIC_TYPE_CIPHER);
- XFREE(ssl->decrypt.idea, ssl->heap, DYNAMIC_TYPE_CIPHER);
-#endif
-#if defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER)
- wc_HmacFree(ssl->encrypt.hmac);
- wc_HmacFree(ssl->decrypt.hmac);
- XFREE(ssl->encrypt.hmac, ssl->heap, DYNAMIC_TYPE_CIPHER);
- XFREE(ssl->decrypt.hmac, ssl->heap, DYNAMIC_TYPE_CIPHER);
-#endif
-}
-
-
-void InitCipherSpecs(CipherSpecs* cs)
-{
- XMEMSET(cs, 0, sizeof(CipherSpecs));
-
- cs->bulk_cipher_algorithm = INVALID_BYTE;
- cs->cipher_type = INVALID_BYTE;
- cs->mac_algorithm = INVALID_BYTE;
- cs->kea = INVALID_BYTE;
- cs->sig_algo = INVALID_BYTE;
-}
-
-#if defined(USE_ECDSA_KEYSZ_HASH_ALGO) || (defined(WOLFSSL_TLS13) && \
- defined(HAVE_ECC))
-static int GetMacDigestSize(byte macAlgo)
-{
- switch (macAlgo) {
- #ifndef NO_SHA
- case sha_mac:
- return WC_SHA_DIGEST_SIZE;
- #endif
- #ifndef NO_SHA256
- case sha256_mac:
- return WC_SHA256_DIGEST_SIZE;
- #endif
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- return WC_SHA384_DIGEST_SIZE;
- #endif
- #ifdef WOLFSSL_SHA512
- case sha512_mac:
- return WC_SHA512_DIGEST_SIZE;
- #endif
- default:
- break;
- }
- return NOT_COMPILED_IN;
-}
-#endif /* USE_ECDSA_KEYSZ_HASH_ALGO */
-
-static WC_INLINE void AddSuiteHashSigAlgo(Suites* suites, byte macAlgo, byte sigAlgo,
- int keySz, word16* inOutIdx)
-{
- int addSigAlgo = 1;
-
-#ifdef USE_ECDSA_KEYSZ_HASH_ALGO
- if (sigAlgo == ecc_dsa_sa_algo) {
- int digestSz = GetMacDigestSize(macAlgo);
- /* do not add sig/algos with digest size larger than key size */
- if (digestSz <= 0 || (keySz > 0 && digestSz > keySz)) {
- addSigAlgo = 0;
- }
- }
-#else
- (void)keySz;
-#endif /* USE_ECDSA_KEYSZ_HASH_ALGO */
-
- if (addSigAlgo) {
-#ifdef WC_RSA_PSS
- if (sigAlgo == rsa_pss_sa_algo) {
- /* RSA PSS is sig then mac */
- suites->hashSigAlgo[*inOutIdx] = sigAlgo;
- *inOutIdx += 1;
- suites->hashSigAlgo[*inOutIdx] = macAlgo;
- *inOutIdx += 1;
- #ifdef WOLFSSL_TLS13
- /* Add the certificate algorithm as well */
- suites->hashSigAlgo[*inOutIdx] = sigAlgo;
- *inOutIdx += 1;
- suites->hashSigAlgo[*inOutIdx] = PSS_RSAE_TO_PSS_PSS(macAlgo);
- *inOutIdx += 1;
- #endif
- }
- else
-#endif
- {
- suites->hashSigAlgo[*inOutIdx] = macAlgo;
- *inOutIdx += 1;
- suites->hashSigAlgo[*inOutIdx] = sigAlgo;
- *inOutIdx += 1;
- }
- }
-}
-
-void InitSuitesHashSigAlgo(Suites* suites, int haveECDSAsig, int haveRSAsig,
- int haveAnon, int tls1_2, int keySz)
-{
- word16 idx = 0;
-
- (void)tls1_2;
- (void)keySz;
-
-#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
- if (haveECDSAsig) {
-#ifdef HAVE_ECC
- #ifdef WOLFSSL_SHA512
- AddSuiteHashSigAlgo(suites, sha512_mac, ecc_dsa_sa_algo, keySz, &idx);
- #endif
- #ifdef WOLFSSL_SHA384
- AddSuiteHashSigAlgo(suites, sha384_mac, ecc_dsa_sa_algo, keySz, &idx);
- #endif
- #ifndef NO_SHA256
- AddSuiteHashSigAlgo(suites, sha256_mac, ecc_dsa_sa_algo, keySz, &idx);
- #endif
- #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
- defined(WOLFSSL_ALLOW_TLS_SHA1))
- AddSuiteHashSigAlgo(suites, sha_mac, ecc_dsa_sa_algo, keySz, &idx);
- #endif
-#endif
- #ifdef HAVE_ED25519
- AddSuiteHashSigAlgo(suites, ED25519_SA_MAJOR, ED25519_SA_MINOR, keySz,
- &idx);
- #endif
- #ifdef HAVE_ED448
- AddSuiteHashSigAlgo(suites, ED448_SA_MAJOR, ED448_SA_MINOR, keySz,
- &idx);
- #endif
- }
-#endif /* HAVE_ECC || HAVE_ED25519 || defined(HAVE_ED448 */
-
- if (haveRSAsig) {
- #ifdef WC_RSA_PSS
- if (tls1_2) {
- #ifdef WOLFSSL_SHA512
- AddSuiteHashSigAlgo(suites, sha512_mac, rsa_pss_sa_algo, keySz,
- &idx);
- #endif
- #ifdef WOLFSSL_SHA384
- AddSuiteHashSigAlgo(suites, sha384_mac, rsa_pss_sa_algo, keySz,
- &idx);
- #endif
- #ifndef NO_SHA256
- AddSuiteHashSigAlgo(suites, sha256_mac, rsa_pss_sa_algo, keySz,
- &idx);
- #endif
- }
- #endif
- #ifdef WOLFSSL_SHA512
- AddSuiteHashSigAlgo(suites, sha512_mac, rsa_sa_algo, keySz, &idx);
- #endif
- #ifdef WOLFSSL_SHA384
- AddSuiteHashSigAlgo(suites, sha384_mac, rsa_sa_algo, keySz, &idx);
- #endif
- #ifndef NO_SHA256
- AddSuiteHashSigAlgo(suites, sha256_mac, rsa_sa_algo, keySz, &idx);
- #endif
- #ifdef WOLFSSL_SHA224
- AddSuiteHashSigAlgo(suites, sha224_mac, rsa_sa_algo, keySz, &idx);
- #endif
- #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
- defined(WOLFSSL_ALLOW_TLS_SHA1))
- AddSuiteHashSigAlgo(suites, sha_mac, rsa_sa_algo, keySz, &idx);
- #endif
- }
-
-#ifdef HAVE_ANON
- if (haveAnon) {
- AddSuiteHashSigAlgo(suites, sha_mac, anonymous_sa_algo, keySz, &idx);
- }
-#endif
-
- (void)haveAnon;
- (void)haveECDSAsig;
- suites->hashSigAlgoSz = idx;
-}
-
-void InitSuites(Suites* suites, ProtocolVersion pv, int keySz, word16 haveRSA,
- word16 havePSK, word16 haveDH, word16 haveNTRU,
- word16 haveECDSAsig, word16 haveECC,
- word16 haveStaticECC, int side)
-{
- word16 idx = 0;
- int tls = pv.major == SSLv3_MAJOR && pv.minor >= TLSv1_MINOR;
- int tls1_2 = pv.major == SSLv3_MAJOR && pv.minor >= TLSv1_2_MINOR;
-#ifdef WOLFSSL_TLS13
- int tls1_3 = IsAtLeastTLSv1_3(pv);
-#endif
- int dtls = 0;
- int haveRSAsig = 1;
-
- (void)tls; /* shut up compiler */
- (void)tls1_2;
- (void)dtls;
- (void)haveDH;
- (void)havePSK;
- (void)haveNTRU;
- (void)haveStaticECC;
- (void)haveECC;
- (void)side;
- (void)haveRSA; /* some builds won't read */
- (void)haveRSAsig; /* non ecc builds won't read */
-
- if (suites == NULL) {
- WOLFSSL_MSG("InitSuites pointer error");
- return;
- }
-
- if (suites->setSuites)
- return; /* trust user settings, don't override */
-
-#ifdef WOLFSSL_TLS13
-#ifdef BUILD_TLS_AES_128_GCM_SHA256
- if (tls1_3) {
- suites->suites[idx++] = TLS13_BYTE;
- suites->suites[idx++] = TLS_AES_128_GCM_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_AES_256_GCM_SHA384
- if (tls1_3) {
- suites->suites[idx++] = TLS13_BYTE;
- suites->suites[idx++] = TLS_AES_256_GCM_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_CHACHA20_POLY1305_SHA256
- if (tls1_3) {
- suites->suites[idx++] = TLS13_BYTE;
- suites->suites[idx++] = TLS_CHACHA20_POLY1305_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_AES_128_CCM_SHA256
- if (tls1_3) {
- suites->suites[idx++] = TLS13_BYTE;
- suites->suites[idx++] = TLS_AES_128_CCM_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_AES_128_CCM_8_SHA256
- if (tls1_3) {
- suites->suites[idx++] = TLS13_BYTE;
- suites->suites[idx++] = TLS_AES_128_CCM_8_SHA256;
- }
-#endif
-
-#ifdef HAVE_NULL_CIPHER
- #ifdef BUILD_TLS_SHA256_SHA256
- if (tls1_3) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_SHA256_SHA256;
- }
- #endif
-
- #ifdef BUILD_TLS_SHA384_SHA384
- if (tls1_3) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_SHA384_SHA384;
- }
- #endif
-#endif
-#endif /* WOLFSSL_TLS13 */
-
-#ifndef WOLFSSL_NO_TLS12
-
-#if !defined(NO_WOLFSSL_SERVER) && !defined(NO_RSA)
- if (side == WOLFSSL_SERVER_END && haveStaticECC) {
- haveRSA = 0; /* can't do RSA with ECDSA key */
- }
-
- if (side == WOLFSSL_SERVER_END && haveECDSAsig) {
- haveRSAsig = 0; /* can't have RSA sig if signed by ECDSA */
- }
-#endif /* !NO_WOLFSSL_SERVER */
-
-#ifdef WOLFSSL_DTLS
- if (pv.major == DTLS_MAJOR) {
- dtls = 1;
- tls = 1;
- /* May be dead assignments dependent upon configuration */
- (void) dtls;
- (void) tls;
- tls1_2 = pv.minor <= DTLSv1_2_MINOR;
- }
-#endif
-
-#ifdef HAVE_RENEGOTIATION_INDICATION
- if (side == WOLFSSL_CLIENT_END) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_EMPTY_RENEGOTIATION_INFO_SCSV;
- }
-#endif
-
-#ifdef BUILD_TLS_QSH
- if (tls) {
- suites->suites[idx++] = QSH_BYTE;
- suites->suites[idx++] = TLS_QSH;
- }
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_AES_256_CBC_SHA
- if (tls && haveNTRU && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_NTRU_RSA_WITH_AES_256_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_AES_128_CBC_SHA
- if (tls && haveNTRU && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_NTRU_RSA_WITH_AES_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_RC4_128_SHA
- if (!dtls && tls && haveNTRU && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_NTRU_RSA_WITH_RC4_128_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA
- if (tls && haveNTRU && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
- if (tls1_2 && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
- if (tls1_2 && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
- if (tls1_2 && haveDH && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_256_GCM_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
- if (tls1_2 && haveDH && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_128_GCM_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_AES_256_GCM_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_AES_128_GCM_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
- if (tls1_2 && haveECC && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
- if (tls1_2 && haveECC && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
- if (tls1_2 && haveRSAsig && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
- if (tls1_2 && haveRSAsig && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
- if (tls1_2 && haveDH && havePSK) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_PSK_WITH_AES_256_GCM_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_DH_anon_WITH_AES_128_CBC_SHA
- if (tls1_2 && haveDH) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DH_anon_WITH_AES_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_DH_anon_WITH_AES_256_GCM_SHA384
- if (tls1_2 && haveDH) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DH_anon_WITH_AES_256_GCM_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
- if (tls1_2 && haveDH && havePSK) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_PSK_WITH_AES_128_GCM_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_GCM_SHA384
- if (tls1_2 && havePSK) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_AES_256_GCM_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_GCM_SHA256
- if (tls1_2 && havePSK) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_AES_128_GCM_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
- if (tls1_2 && haveECC) {
- suites->suites[idx++] = CHACHA_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = CHACHA_BYTE;
- suites->suites[idx++] = TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = CHACHA_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256;
- }
-#endif
-
-/* Place as higher priority for MYSQL */
-#if defined(WOLFSSL_MYSQL_COMPATIBLE)
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
- if (tls && haveDH && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA;
- }
-#endif
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
- if (tls1_2 && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
- if (tls1_2 && haveRSAsig && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
- if (tls1_2 && haveECC && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
- if (tls1_2 && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
- if (tls1_2 && haveRSAsig && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
- if (tls1_2 && haveECC && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
- if (tls && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
- if (tls && haveECC && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
- if (tls && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
- if (tls && haveECC && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
- if (!dtls && tls && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_RC4_128_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
- if (!dtls && tls && haveECC && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_RC4_128_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
- if (tls && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
- if (tls && haveECC && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
- if (tls && haveRSA) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
- if (tls && haveRSAsig && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_RSA_WITH_AES_256_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
- if (tls && haveRSA) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
- if (tls && haveRSAsig && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_RSA_WITH_AES_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_RC4_128_SHA
- if (!dtls && tls && haveRSA) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_RSA_WITH_RC4_128_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_RC4_128_SHA
- if (!dtls && tls && haveRSAsig && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_RSA_WITH_RC4_128_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
- if (tls && haveRSA) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
- if (tls && haveRSAsig && haveStaticECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM
- if (tls1_2 && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_128_CCM;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
- if (tls1_2 && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8
- if (tls1_2 && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_CCM_8
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_AES_128_CCM_8;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_CCM_8
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_AES_256_CCM_8;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveDH && haveRSA)
-#else
- if (tls && haveDH && haveRSA)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveDH && haveRSA)
-#else
- if (tls && haveDH && haveRSA)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA256;
- }
-#endif
-
-/* Place as higher priority for MYSQL testing */
-#if !defined(WOLFSSL_MYSQL_COMPATIBLE)
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
- if (tls && haveDH && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_256_CBC_SHA;
- }
-#endif
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
- if (tls && haveDH && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_AES_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
- if (tls && haveDH && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveRSA)
-#else
- if (tls && haveRSA)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_AES_256_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveRSA)
-#else
- if (tls && haveRSA)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_AES_128_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA
- if (tls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_AES_256_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA
- if (tls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_AES_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256
- if (tls1_2 && haveECC) {
- suites->suites[idx++] = CHACHA_BYTE;
- suites->suites[idx++] =
- TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = CHACHA_BYTE;
- suites->suites[idx++] = TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
- if (tls1_2 && haveRSA) {
- suites->suites[idx++] = CHACHA_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_NULL_SHA
- if (tls && haveECC) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_ECDSA_WITH_NULL_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_NULL_MD5
- if (tls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_NULL_MD5;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_NULL_SHA
- if (tls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_NULL_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_NULL_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveRSA)
-#else
- if (tls && haveRSA)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_NULL_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA
- if (tls && havePSK) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_AES_256_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveDH && havePSK)
-#else
- if (tls && haveDH && havePSK)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_PSK_WITH_AES_256_CBC_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA384
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && havePSK)
-#else
- if (tls && havePSK)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_AES_256_CBC_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveDH && havePSK)
-#else
- if (tls && haveDH && havePSK)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_PSK_WITH_AES_128_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && havePSK)
-#else
- if (tls1 && havePSK)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_AES_128_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA
- if (tls && havePSK) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_AES_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_CCM
- if (tls && haveDH && havePSK) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_DHE_PSK_WITH_AES_128_CCM;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_CCM
- if (tls && haveDH && havePSK) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_DHE_PSK_WITH_AES_256_CCM;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && havePSK)
-#else
- if (tls && havePSK)
-#endif
- {
- suites->suites[idx++] = CHACHA_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_CHACHA20_POLY1305_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && havePSK)
-#else
- if (tls && havePSK)
-#endif
- {
- suites->suites[idx++] = CHACHA_BYTE;
- suites->suites[idx++] = TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && havePSK)
-#else
- if (tls && havePSK)
-#endif
- {
- suites->suites[idx++] = CHACHA_BYTE;
- suites->suites[idx++] = TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && havePSK)
-#else
- if (tls && havePSK)
-#endif
- {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CCM
- if (tls && havePSK) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_AES_128_CCM;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CCM
- if (tls && havePSK) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_AES_256_CCM;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CCM_8
- if (tls && havePSK) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_AES_128_CCM_8;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CCM_8
- if (tls && havePSK) {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_AES_256_CCM_8;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_NULL_SHA384
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveDH && havePSK)
-#else
- if (tls && haveDH && havePSK)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_PSK_WITH_NULL_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_NULL_SHA384
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && havePSK)
-#else
- if (tls && havePSK)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_NULL_SHA384;
- }
-#endif
-
-#ifdef BUILD_TLS_ECDHE_PSK_WITH_NULL_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && havePSK)
-#else
- if (tls && havePSK)
-#endif
- {
- suites->suites[idx++] = ECC_BYTE;
- suites->suites[idx++] = TLS_ECDHE_PSK_WITH_NULL_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_NULL_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveDH && havePSK)
-#else
- if (tls && haveDH && havePSK)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_PSK_WITH_NULL_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_NULL_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && havePSK)
-#else
- if (tls && havePSK)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_NULL_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_NULL_SHA
- if (tls && havePSK) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_PSK_WITH_NULL_SHA;
- }
-#endif
-
-#ifdef BUILD_SSL_RSA_WITH_RC4_128_SHA
- if (!dtls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = SSL_RSA_WITH_RC4_128_SHA;
- }
-#endif
-
-#ifdef BUILD_SSL_RSA_WITH_RC4_128_MD5
- if (!dtls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = SSL_RSA_WITH_RC4_128_MD5;
- }
-#endif
-
-#ifdef BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA
- if (haveRSA ) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = SSL_RSA_WITH_3DES_EDE_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_HC_128_MD5
- if (!dtls && tls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_HC_128_MD5;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_HC_128_SHA
- if (!dtls && tls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_HC_128_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_RABBIT_SHA
- if (!dtls && tls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_RABBIT_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
- if (tls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_CAMELLIA_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
- if (tls && haveDH && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
- if (tls && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_CAMELLIA_256_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA
- if (tls && haveDH && haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveRSA)
-#else
- if (tls && haveRSA)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveDH && haveRSA)
-#else
- if (tls && haveDH && haveRSA)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveRSA)
-#else
- if (tls && haveRSA)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
-#ifndef WOLFSSL_OLDTLS_SHA2_CIPHERSUITES
- if (tls1_2 && haveDH && haveRSA)
-#else
- if (tls && haveDH && haveRSA)
-#endif
- {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256;
- }
-#endif
-
-#ifdef BUILD_SSL_RSA_WITH_IDEA_CBC_SHA
- if (haveRSA) {
- suites->suites[idx++] = CIPHER_BYTE;
- suites->suites[idx++] = SSL_RSA_WITH_IDEA_CBC_SHA;
- }
-#endif
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
- suites->suiteSz = idx;
-
- InitSuitesHashSigAlgo(suites, haveECDSAsig | haveECC, haveRSAsig | haveRSA,
- 0, tls1_2, keySz);
-}
-
-#if !defined(NO_WOLFSSL_SERVER) || !defined(NO_CERTS) || \
- (!defined(NO_WOLFSSL_CLIENT) && (!defined(NO_DH) || defined(HAVE_ECC)))
-
-/* Decode the signature algorithm.
- *
- * input The encoded signature algorithm.
- * hashalgo The hash algorithm.
- * hsType The signature type.
- */
-static WC_INLINE void DecodeSigAlg(const byte* input, byte* hashAlgo, byte* hsType)
-{
- switch (input[0]) {
- case NEW_SA_MAJOR:
- #ifdef HAVE_ED25519
- /* ED25519: 0x0807 */
- if (input[1] == ED25519_SA_MINOR) {
- *hsType = ed25519_sa_algo;
- /* Hash performed as part of sign/verify operation. */
- *hashAlgo = sha512_mac;
- }
- else
- #endif
- #ifdef HAVE_ED448
- /* ED448: 0x0808 */
- if (input[1] == ED448_SA_MINOR) {
- *hsType = ed448_sa_algo;
- /* Hash performed as part of sign/verify operation. */
- *hashAlgo = sha512_mac;
- }
- else
- #endif
- #ifdef WC_RSA_PSS
- /* PSS PSS signatures: 0x080[9-b] */
- if (input[1] >= pss_sha256 && input[1] <= pss_sha512) {
- *hsType = rsa_pss_pss_algo;
- *hashAlgo = PSS_PSS_HASH_TO_MAC(input[1]);
- }
- else
- #endif
- {
- *hsType = input[0];
- *hashAlgo = input[1];
- }
- break;
- default:
- *hashAlgo = input[0];
- *hsType = input[1];
- break;
- }
-}
-#endif /* !NO_WOLFSSL_SERVER || !NO_CERTS */
-
-#ifndef WOLFSSL_NO_TLS12
-#if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
-#if !defined(NO_DH) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448) || (!defined(NO_RSA) && defined(WC_RSA_PSS))
-
-static enum wc_HashType HashAlgoToType(int hashAlgo)
-{
- switch (hashAlgo) {
- #ifdef WOLFSSL_SHA512
- case sha512_mac:
- return WC_HASH_TYPE_SHA512;
- #endif
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- return WC_HASH_TYPE_SHA384;
- #endif
- #ifndef NO_SHA256
- case sha256_mac:
- return WC_HASH_TYPE_SHA256;
- #endif
- #ifdef WOLFSSL_SHA224
- case sha224_mac:
- return WC_HASH_TYPE_SHA224;
- #endif
- #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
- defined(WOLFSSL_ALLOW_TLS_SHA1))
- case sha_mac:
- return WC_HASH_TYPE_SHA;
- #endif
- default:
- WOLFSSL_MSG("Bad hash sig algo");
- break;
- }
-
- return WC_HASH_TYPE_NONE;
-}
-#endif /* !NO_DH || HAVE_ECC || (!NO_RSA && WC_RSA_PSS) */
-#endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#ifndef NO_CERTS
-
-void InitX509Name(WOLFSSL_X509_NAME* name, int dynamicFlag)
-{
- (void)dynamicFlag;
-
- if (name != NULL) {
- name->name = name->staticName;
- name->dynamicName = 0;
- name->sz = 0;
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- XMEMSET(&name->fullName, 0, sizeof(DecodedName));
- XMEMSET(&name->cnEntry, 0, sizeof(WOLFSSL_X509_NAME_ENTRY));
- XMEMSET(&name->extra, 0, sizeof(name->extra));
- name->cnEntry.value = &(name->cnEntry.data); /* point to internal data*/
- name->cnEntry.nid = ASN_COMMON_NAME;
- name->x509 = NULL;
-#endif /* OPENSSL_EXTRA */
- }
-}
-
-
-void FreeX509Name(WOLFSSL_X509_NAME* name, void* heap)
-{
- if (name != NULL) {
- if (name->dynamicName) {
- XFREE(name->name, heap, DYNAMIC_TYPE_SUBJECT_CN);
- name->name = NULL;
- }
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- {
- int i;
- if (name->fullName.fullName != NULL) {
- XFREE(name->fullName.fullName, heap, DYNAMIC_TYPE_X509);
- name->fullName.fullName = NULL;
- }
- for (i = 0; i < MAX_NAME_ENTRIES; i++) {
- /* free ASN1 string data */
- if (name->extra[i].set && name->extra[i].data.data != NULL) {
- XFREE(name->extra[i].data.data, heap, DYNAMIC_TYPE_OPENSSL);
- }
- }
- wolfSSL_ASN1_OBJECT_free(&name->cnEntry.object);
- }
-#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
- }
- (void)heap;
-}
-
-
-/* Initialize wolfSSL X509 type */
-void InitX509(WOLFSSL_X509* x509, int dynamicFlag, void* heap)
-{
- if (x509 == NULL) {
- WOLFSSL_MSG("Null parameter passed in!");
- return;
- }
-
- XMEMSET(x509, 0, sizeof(WOLFSSL_X509));
-
- x509->heap = heap;
- InitX509Name(&x509->issuer, 0);
- InitX509Name(&x509->subject, 0);
- x509->dynamicMemory = (byte)dynamicFlag;
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
- x509->refCount = 1;
- (void)wc_InitMutex(&x509->refMutex);
- #endif
-}
-
-
-/* Free wolfSSL X509 type */
-void FreeX509(WOLFSSL_X509* x509)
-{
- if (x509 == NULL)
- return;
-
- FreeX509Name(&x509->issuer, x509->heap);
- FreeX509Name(&x509->subject, x509->heap);
- if (x509->pubKey.buffer) {
- XFREE(x509->pubKey.buffer, x509->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- x509->pubKey.buffer = NULL;
- }
- FreeDer(&x509->derCert);
- XFREE(x509->sig.buffer, x509->heap, DYNAMIC_TYPE_SIGNATURE);
- x509->sig.buffer = NULL;
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- XFREE(x509->authKeyId, x509->heap, DYNAMIC_TYPE_X509_EXT);
- x509->authKeyId = NULL;
- XFREE(x509->subjKeyId, x509->heap, DYNAMIC_TYPE_X509_EXT);
- x509->subjKeyId = NULL;
- if (x509->authInfo != NULL) {
- XFREE(x509->authInfo, x509->heap, DYNAMIC_TYPE_X509_EXT);
- x509->authInfo = NULL;
- }
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- if (x509->authInfoCaIssuer != NULL) {
- XFREE(x509->authInfoCaIssuer, x509->heap, DYNAMIC_TYPE_X509_EXT);
- }
- if (x509->ext_sk != NULL) {
- wolfSSL_sk_X509_EXTENSION_free(x509->ext_sk);
- }
- #endif /* OPENSSL_ALL || WOLFSSL_QT */
- #ifdef OPENSSL_EXTRA
- /* Free serialNumber that was set by wolfSSL_X509_get_serialNumber */
- if (x509->serialNumber != NULL) {
- wolfSSL_ASN1_INTEGER_free(x509->serialNumber);
- }
- #endif
- if (x509->extKeyUsageSrc != NULL) {
- XFREE(x509->extKeyUsageSrc, x509->heap, DYNAMIC_TYPE_X509_EXT);
- x509->extKeyUsageSrc= NULL;
- }
- #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
- #if defined(OPENSSL_ALL)
- if (x509->algor.algorithm) {
- wolfSSL_ASN1_OBJECT_free(x509->algor.algorithm);
- x509->algor.algorithm = NULL;
- }
- if (x509->key.algor) {
- wolfSSL_X509_ALGOR_free(x509->key.algor);
- x509->key.algor = NULL;
- }
- if (x509->key.pkey) {
- wolfSSL_EVP_PKEY_free(x509->key.pkey);
- x509->key.pkey = NULL;
- }
- #endif /* OPENSSL_ALL */
- if (x509->altNames) {
- FreeAltNames(x509->altNames, x509->heap);
- x509->altNames = NULL;
- }
-
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
- wc_FreeMutex(&x509->refMutex);
- #endif
-}
-
-
-#if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
-#if !defined(WOLFSSL_NO_TLS12)
-/* Encode the signature algorithm into buffer.
- *
- * hashalgo The hash algorithm.
- * hsType The signature type.
- * output The buffer to encode into.
- */
-static WC_INLINE void EncodeSigAlg(byte hashAlgo, byte hsType, byte* output)
-{
- switch (hsType) {
-#ifdef HAVE_ECC
- case ecc_dsa_sa_algo:
- output[0] = hashAlgo;
- output[1] = ecc_dsa_sa_algo;
- break;
-#endif
-#ifdef HAVE_ED25519
- case ed25519_sa_algo:
- output[0] = ED25519_SA_MAJOR;
- output[1] = ED25519_SA_MINOR;
- (void)hashAlgo;
- break;
-#endif
-#ifdef HAVE_ED448
- case ed448_sa_algo:
- output[0] = ED448_SA_MAJOR;
- output[1] = ED448_SA_MINOR;
- (void)hashAlgo;
- break;
-#endif
-#ifndef NO_RSA
- case rsa_sa_algo:
- output[0] = hashAlgo;
- output[1] = rsa_sa_algo;
- break;
- #ifdef WC_RSA_PSS
- /* PSS signatures: 0x080[4-6] */
- case rsa_pss_sa_algo:
- output[0] = rsa_pss_sa_algo;
- output[1] = hashAlgo;
- break;
- #endif
-#endif
- }
- (void)hashAlgo;
- (void)output;
-}
-#endif
-
-#if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_NO_CLIENT_AUTH)
-static void SetDigest(WOLFSSL* ssl, int hashAlgo)
-{
- switch (hashAlgo) {
- #ifndef NO_SHA
- case sha_mac:
- ssl->buffers.digest.buffer = ssl->hsHashes->certHashes.sha;
- ssl->buffers.digest.length = WC_SHA_DIGEST_SIZE;
- break;
- #endif /* !NO_SHA */
- #ifndef NO_SHA256
- case sha256_mac:
- ssl->buffers.digest.buffer = ssl->hsHashes->certHashes.sha256;
- ssl->buffers.digest.length = WC_SHA256_DIGEST_SIZE;
- break;
- #endif /* !NO_SHA256 */
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- ssl->buffers.digest.buffer = ssl->hsHashes->certHashes.sha384;
- ssl->buffers.digest.length = WC_SHA384_DIGEST_SIZE;
- break;
- #endif /* WOLFSSL_SHA384 */
- #ifdef WOLFSSL_SHA512
- case sha512_mac:
- ssl->buffers.digest.buffer = ssl->hsHashes->certHashes.sha512;
- ssl->buffers.digest.length = WC_SHA512_DIGEST_SIZE;
- break;
- #endif /* WOLFSSL_SHA512 */
- } /* switch */
-}
-#endif /* !WOLFSSL_NO_TLS12 && !WOLFSSL_NO_CLIENT_AUTH */
-#endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
-#endif /* !NO_CERTS */
-
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
-static word32 MacSize(WOLFSSL* ssl)
-{
-#ifdef HAVE_TRUNCATED_HMAC
- word32 digestSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
- : ssl->specs.hash_size;
-#else
- word32 digestSz = ssl->specs.hash_size;
-#endif
-
- return digestSz;
-}
-#endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
-
-#ifndef NO_RSA
-#ifndef WOLFSSL_NO_TLS12
-#if !defined(NO_WOLFSSL_SERVER) || (!defined(NO_WOLFSSL_CLIENT) && \
- !defined(WOLFSSL_NO_CLIENT_AUTH))
-static int TypeHash(int hashAlgo)
-{
- switch (hashAlgo) {
- #ifdef WOLFSSL_SHA512
- case sha512_mac:
- return SHA512h;
- #endif
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- return SHA384h;
- #endif
- #ifndef NO_SHA256
- case sha256_mac:
- return SHA256h;
- #endif
- #ifdef WOLFSSL_SHA224
- case sha224_mac:
- return SHA224h;
- #endif
- #ifndef NO_SHA
- case sha_mac:
- return SHAh;
- #endif
- }
-
- return 0;
-}
-#endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#if defined(WC_RSA_PSS)
-int ConvertHashPss(int hashAlgo, enum wc_HashType* hashType, int* mgf)
-{
- switch (hashAlgo) {
- #ifdef WOLFSSL_SHA512
- case sha512_mac:
- *hashType = WC_HASH_TYPE_SHA512;
- if (mgf != NULL)
- *mgf = WC_MGF1SHA512;
- break;
- #endif
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- *hashType = WC_HASH_TYPE_SHA384;
- if (mgf != NULL)
- *mgf = WC_MGF1SHA384;
- break;
- #endif
- #ifndef NO_SHA256
- case sha256_mac:
- *hashType = WC_HASH_TYPE_SHA256;
- if (mgf != NULL)
- *mgf = WC_MGF1SHA256;
- break;
- #endif
- default:
- return BAD_FUNC_ARG;
- }
-
- return 0;
-}
-#endif
-
-#if !defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)
-int RsaSign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out,
- word32* outSz, int sigAlgo, int hashAlgo, RsaKey* key,
- DerBuffer* keyBufInfo)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
- (void)sigAlgo;
- (void)hashAlgo;
-
- WOLFSSL_ENTER("RsaSign");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- if (key) {
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
- }
-#endif
-
-#if defined(WC_RSA_PSS)
- if (sigAlgo == rsa_pss_sa_algo) {
- enum wc_HashType hashType = WC_HASH_TYPE_NONE;
- int mgf = 0;
-
- ret = ConvertHashPss(hashAlgo, &hashType, &mgf);
- if (ret != 0)
- return ret;
-
- #if defined(HAVE_PK_CALLBACKS)
- if (ssl->ctx->RsaPssSignCb) {
- void* ctx = wolfSSL_GetRsaPssSignCtx(ssl);
- ret = ssl->ctx->RsaPssSignCb(ssl, in, inSz, out, outSz,
- TypeHash(hashAlgo), mgf,
- keyBuf, keySz, ctx);
- }
- else
- #endif
- {
- ret = wc_RsaPSS_Sign(in, inSz, out, *outSz, hashType, mgf, key,
- ssl->rng);
- }
- }
- else
-#endif
-#if defined(HAVE_PK_CALLBACKS)
- if (ssl->ctx->RsaSignCb) {
- void* ctx = wolfSSL_GetRsaSignCtx(ssl);
- ret = ssl->ctx->RsaSignCb(ssl, in, inSz, out, outSz, keyBuf, keySz,
- ctx);
- }
- else
-#endif /*HAVE_PK_CALLBACKS */
- ret = wc_RsaSSL_Sign(in, inSz, out, *outSz, key, ssl->rng);
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (key && ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- /* For positive response return in outSz */
- if (ret > 0) {
- *outSz = ret;
- ret = 0;
- }
-
- WOLFSSL_LEAVE("RsaSign", ret);
-
- return ret;
-}
-#endif
-
-int RsaVerify(WOLFSSL* ssl, byte* in, word32 inSz, byte** out, int sigAlgo,
- int hashAlgo, RsaKey* key, buffer* keyBufInfo)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
- (void)sigAlgo;
- (void)hashAlgo;
-
- WOLFSSL_ENTER("RsaVerify");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#if defined(WC_RSA_PSS)
- if (sigAlgo == rsa_pss_sa_algo) {
- enum wc_HashType hashType = WC_HASH_TYPE_NONE;
- int mgf = 0;
-
- ret = ConvertHashPss(hashAlgo, &hashType, &mgf);
- if (ret != 0)
- return ret;
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->RsaPssVerifyCb) {
- void* ctx = wolfSSL_GetRsaPssVerifyCtx(ssl);
- ret = ssl->ctx->RsaPssVerifyCb(ssl, in, inSz, out,
- TypeHash(hashAlgo), mgf,
- keyBuf, keySz, ctx);
- }
- else
-#endif /*HAVE_PK_CALLBACKS */
- ret = wc_RsaPSS_VerifyInline(in, inSz, out, hashType, mgf, key);
- }
- else
-#endif
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->RsaVerifyCb) {
- void* ctx = wolfSSL_GetRsaVerifyCtx(ssl);
- ret = ssl->ctx->RsaVerifyCb(ssl, in, inSz, out, keyBuf, keySz, ctx);
- }
- else
-#endif /*HAVE_PK_CALLBACKS */
- {
- ret = wc_RsaSSL_VerifyInline(in, inSz, out, key);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("RsaVerify", ret);
-
- return ret;
-}
-
-/* Verify RSA signature, 0 on success */
-/* This function is used to check the sign result */
-int VerifyRsaSign(WOLFSSL* ssl, byte* verifySig, word32 sigSz,
- const byte* plain, word32 plainSz, int sigAlgo, int hashAlgo, RsaKey* key,
- DerBuffer* keyBufInfo)
-{
- byte* out = NULL; /* inline result */
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
- (void)sigAlgo;
- (void)hashAlgo;
-
- WOLFSSL_ENTER("VerifyRsaSign");
-
- if (verifySig == NULL || plain == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (sigSz > ENCRYPT_LEN) {
- WOLFSSL_MSG("Signature buffer too big");
- return BUFFER_E;
- }
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- if (key) {
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
- }
-#endif
-
-#if defined(WC_RSA_PSS)
- if (sigAlgo == rsa_pss_sa_algo) {
- enum wc_HashType hashType = WC_HASH_TYPE_NONE;
- int mgf = 0;
-
- ret = ConvertHashPss(hashAlgo, &hashType, &mgf);
- if (ret != 0)
- return ret;
- #ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->RsaPssSignCheckCb) {
- /* The key buffer includes private/public portion,
- but only public is used */
- /* If HSM hardware is checking the signature result you can
- optionally skip the sign check and return 0 */
- /* The ctx here is the RsaSignCtx set using wolfSSL_SetRsaSignCtx */
- void* ctx = wolfSSL_GetRsaPssSignCtx(ssl);
- ret = ssl->ctx->RsaPssSignCheckCb(ssl, verifySig, sigSz, &out,
- TypeHash(hashAlgo), mgf,
- keyBuf, keySz, ctx);
- if (ret > 0) {
- ret = wc_RsaPSS_CheckPadding(plain, plainSz, out, ret,
- hashType);
- if (ret != 0)
- ret = VERIFY_CERT_ERROR;
- }
- }
- else
- #endif /* HAVE_PK_CALLBACKS */
- {
- ret = wc_RsaPSS_VerifyInline(verifySig, sigSz, &out, hashType, mgf,
- key);
- if (ret > 0) {
- #ifdef HAVE_SELFTEST
- ret = wc_RsaPSS_CheckPadding(plain, plainSz, out, ret,
- hashType);
- #else
- ret = wc_RsaPSS_CheckPadding_ex(plain, plainSz, out, ret,
- hashType, -1,
- mp_count_bits(&key->n));
- #endif
- if (ret != 0)
- ret = VERIFY_CERT_ERROR;
- }
- }
-
- }
- else
-#endif /* WC_RSA_PSS */
- {
- #ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->RsaSignCheckCb) {
- /* The key buffer includes private/public portion,
- but only public is used */
- /* If HSM hardware is checking the signature result you can
- optionally skip the sign check and return 0 */
- /* The ctx here is the RsaSignCtx set using wolfSSL_SetRsaSignCtx */
- void* ctx = wolfSSL_GetRsaSignCtx(ssl);
- ret = ssl->ctx->RsaSignCheckCb(ssl, verifySig, sigSz, &out,
- keyBuf, keySz, ctx);
- }
- else
- #endif /* HAVE_PK_CALLBACKS */
- {
- ret = wc_RsaSSL_VerifyInline(verifySig, sigSz, &out, key);
- }
-
- if (ret > 0) {
- if (ret != (int)plainSz || !out ||
- XMEMCMP(plain, out, plainSz) != 0) {
- WOLFSSL_MSG("RSA Signature verification failed");
- ret = RSA_SIGN_FAULT;
- } else {
- ret = 0; /* RSA reset */
- }
- }
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (key && ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("VerifyRsaSign", ret);
-
- return ret;
-}
-
-#ifndef WOLFSSL_NO_TLS12
-
-#if !defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)
-int RsaDec(WOLFSSL* ssl, byte* in, word32 inSz, byte** out, word32* outSz,
- RsaKey* key, DerBuffer* keyBufInfo)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
-
- WOLFSSL_ENTER("RsaDec");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->RsaDecCb) {
- void* ctx = wolfSSL_GetRsaDecCtx(ssl);
- ret = ssl->ctx->RsaDecCb(ssl, in, inSz, out, keyBuf, keySz, ctx);
- }
- else
-#endif /* HAVE_PK_CALLBACKS */
- {
- #ifdef WC_RSA_BLINDING
- ret = wc_RsaSetRNG(key, ssl->rng);
- if (ret != 0)
- return ret;
- #endif
- ret = wc_RsaPrivateDecryptInline(in, inSz, out, key);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- /* For positive response return in outSz */
- if (ret > 0) {
- *outSz = ret;
- ret = 0;
- }
-
- WOLFSSL_LEAVE("RsaDec", ret);
-
- return ret;
-}
-#endif /* !NO_WOLFSSL_SERVER) || !WOLFSSL_NO_CLIENT_AUTH */
-
-int RsaEnc(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out, word32* outSz,
- RsaKey* key, buffer* keyBufInfo)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
-
- WOLFSSL_ENTER("RsaEnc");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->RsaEncCb) {
- void* ctx = wolfSSL_GetRsaEncCtx(ssl);
- ret = ssl->ctx->RsaEncCb(ssl, in, inSz, out, outSz, keyBuf, keySz, ctx);
- }
- else
-#endif /* HAVE_PK_CALLBACKS */
- {
- ret = wc_RsaPublicEncrypt(in, inSz, out, *outSz, key, ssl->rng);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- /* For positive response return in outSz */
- if (ret > 0) {
- *outSz = ret;
- ret = 0;
- }
-
- WOLFSSL_LEAVE("RsaEnc", ret);
-
- return ret;
-}
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#endif /* NO_RSA */
-
-#ifdef HAVE_ECC
-
-int EccSign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out,
- word32* outSz, ecc_key* key, DerBuffer* keyBufInfo)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
-
- WOLFSSL_ENTER("EccSign");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- if (key) {
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
- }
-#endif
-
-#if defined(HAVE_PK_CALLBACKS)
- if (ssl->ctx->EccSignCb) {
- void* ctx = wolfSSL_GetEccSignCtx(ssl);
- ret = ssl->ctx->EccSignCb(ssl, in, inSz, out, outSz, keyBuf,
- keySz, ctx);
- }
- else
-#endif /* HAVE_PK_CALLBACKS */
- {
- ret = wc_ecc_sign_hash(in, inSz, out, outSz, ssl->rng, key);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (key && ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("EccSign", ret);
-
- return ret;
-}
-
-int EccVerify(WOLFSSL* ssl, const byte* in, word32 inSz, const byte* out,
- word32 outSz, ecc_key* key, buffer* keyBufInfo)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
-
- WOLFSSL_ENTER("EccVerify");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->EccVerifyCb) {
- void* ctx = wolfSSL_GetEccVerifyCtx(ssl);
- ret = ssl->ctx->EccVerifyCb(ssl, in, inSz, out, outSz, keyBuf, keySz,
- &ssl->eccVerifyRes, ctx);
- }
- else
-#endif /* HAVE_PK_CALLBACKS */
- {
- ret = wc_ecc_verify_hash(in, inSz, out, outSz, &ssl->eccVerifyRes, key);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
- else
-#endif /* WOLFSSL_ASYNC_CRYPT */
- {
- ret = (ret != 0 || ssl->eccVerifyRes == 0) ? VERIFY_SIGN_ERROR : 0;
- }
-
- WOLFSSL_LEAVE("EccVerify", ret);
-
- return ret;
-}
-
-#ifdef HAVE_PK_CALLBACKS
- /* Gets ECC key for shared secret callback testing
- * Client side: returns peer key
- * Server side: returns private key
- */
- static int EccGetKey(WOLFSSL* ssl, ecc_key** otherKey)
- {
- int ret = NO_PEER_KEY;
- ecc_key* tmpKey = NULL;
-
- if (ssl == NULL || otherKey == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if (ssl->specs.static_ecdh) {
- if (!ssl->peerEccDsaKey || !ssl->peerEccDsaKeyPresent ||
- !ssl->peerEccDsaKey->dp) {
- return NO_PEER_KEY;
- }
- tmpKey = (struct ecc_key*)ssl->peerEccDsaKey;
- }
- else {
- if (!ssl->peerEccKey || !ssl->peerEccKeyPresent ||
- !ssl->peerEccKey->dp) {
- return NO_PEER_KEY;
- }
- tmpKey = (struct ecc_key*)ssl->peerEccKey;
- }
- }
- else if (ssl->options.side == WOLFSSL_SERVER_END) {
- if (ssl->specs.static_ecdh) {
- if (ssl->hsKey == NULL) {
- return NO_PRIVATE_KEY;
- }
- tmpKey = (struct ecc_key*)ssl->hsKey;
- }
- else {
- if (!ssl->eccTempKeyPresent) {
- return NO_PRIVATE_KEY;
- }
- tmpKey = (struct ecc_key*)ssl->eccTempKey;
- }
- }
-
- if (tmpKey) {
- *otherKey = tmpKey;
- ret = 0;
- }
-
- return ret;
- }
-#endif /* HAVE_PK_CALLBACKS */
-
-int EccSharedSecret(WOLFSSL* ssl, ecc_key* priv_key, ecc_key* pub_key,
- byte* pubKeyDer, word32* pubKeySz, byte* out, word32* outlen,
- int side)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- ecc_key* otherKey = NULL;
-#endif
-#ifdef WOLFSSL_ASYNC_CRYPT
- WC_ASYNC_DEV* asyncDev = &priv_key->asyncDev;
-#endif
-
- (void)ssl;
- (void)pubKeyDer;
- (void)pubKeySz;
- (void)side;
-
- WOLFSSL_ENTER("EccSharedSecret");
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->EccSharedSecretCb) {
- ret = EccGetKey(ssl, &otherKey);
- if (ret != 0)
- return ret;
- #ifdef WOLFSSL_ASYNC_CRYPT
- asyncDev = &otherKey->asyncDev;
- #endif
- }
-#endif
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->EccSharedSecretCb) {
- void* ctx = wolfSSL_GetEccSharedSecretCtx(ssl);
- ret = ssl->ctx->EccSharedSecretCb(ssl, otherKey, pubKeyDer,
- pubKeySz, out, outlen, side, ctx);
- }
- else
-#endif
- {
- ret = wc_ecc_shared_secret(priv_key, pub_key, out, outlen);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("EccSharedSecret", ret);
-
- return ret;
-}
-
-int EccMakeKey(WOLFSSL* ssl, ecc_key* key, ecc_key* peer)
-{
- int ret = 0;
- int keySz = 0;
- int ecc_curve = ECC_CURVE_DEF;
-
- WOLFSSL_ENTER("EccMakeKey");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_NONE);
- if (ret != 0)
- return ret;
-#endif
-
- /* get key size */
- if (peer == NULL) {
- keySz = ssl->eccTempKeySz;
- }
- else {
- keySz = peer->dp->size;
- }
-
- /* get curve type */
- if (ssl->ecdhCurveOID > 0) {
- ecc_curve = wc_ecc_get_oid(ssl->ecdhCurveOID, NULL, NULL);
- }
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->EccKeyGenCb) {
- void* ctx = wolfSSL_GetEccKeyGenCtx(ssl);
- ret = ssl->ctx->EccKeyGenCb(ssl, key, keySz, ecc_curve, ctx);
- }
- else
-#endif
- {
- ret = wc_ecc_make_key_ex(ssl->rng, keySz, key, ecc_curve);
- }
-
- /* make sure the curve is set for TLS */
- if (ret == 0 && key->dp) {
- ssl->ecdhCurveOID = key->dp->oidSum;
- #if defined(WOLFSSL_TLS13) || defined(HAVE_FFDHE)
- ssl->namedGroup = 0;
- #endif
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("EccMakeKey", ret);
-
- return ret;
-}
-#endif /* HAVE_ECC */
-
-#ifdef HAVE_ED25519
-/* Check whether the key contains a public key.
- * If not then pull it out of the leaf certificate.
- *
- * ssl SSL/TLS object.
- * returns MEMORY_E when unable to allocate memory, a parsing error, otherwise
- * 0 on success.
- */
-int Ed25519CheckPubKey(WOLFSSL* ssl)
-{
- ed25519_key* key = (ed25519_key*)ssl->hsKey;
- int ret = 0;
-
- /* Public key required for signing. */
- if (!key->pubKeySet) {
- DerBuffer* leaf = ssl->buffers.certificate;
- DecodedCert* cert = (DecodedCert*)XMALLOC(sizeof(*cert),
- ssl->heap, DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- ret = MEMORY_E;
-
- if (ret == 0) {
- InitDecodedCert(cert, leaf->buffer, leaf->length, ssl->heap);
- ret = DecodeToKey(cert, 0);
- }
- if (ret == 0) {
- ret = wc_ed25519_import_public(cert->publicKey, cert->pubKeySize,
- key);
- }
- if (cert != NULL) {
- FreeDecodedCert(cert);
- XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
- }
- }
-
- return ret;
-}
-
-/* Sign the data using EdDSA and key using Ed25519.
- *
- * ssl SSL object.
- * in Data or message to sign.
- * inSz Length of the data.
- * out Buffer to hold signature.
- * outSz On entry, size of the buffer. On exit, the size of the signature.
- * key The private Ed25519 key data.
- * keySz The length of the private key data in bytes.
- * ctx The callback context.
- * returns 0 on success, otherwise the value is an error.
- */
-int Ed25519Sign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out,
- word32* outSz, ed25519_key* key, DerBuffer* keyBufInfo)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
-
- WOLFSSL_ENTER("Ed25519Sign");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#if defined(HAVE_PK_CALLBACKS)
- if (ssl->ctx->Ed25519SignCb) {
- void* ctx = wolfSSL_GetEd25519SignCtx(ssl);
- ret = ssl->ctx->Ed25519SignCb(ssl, in, inSz, out, outSz, keyBuf,
- keySz, ctx);
- }
- else
-#endif /* HAVE_PK_CALLBACKS */
- {
- ret = wc_ed25519_sign_msg(in, inSz, out, outSz, key);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("Ed25519Sign", ret);
-
- return ret;
-}
-
-/* Verify the data using EdDSA and key using Ed25519.
- *
- * ssl SSL object.
- * in Signature data.
- * inSz Length of the signature data in bytes.
- * msg Message to verify.
- * outSz Length of message in bytes.
- * key The public Ed25519 key data.
- * keySz The length of the private key data in bytes.
- * ctx The callback context.
- * returns 0 on success, otherwise the value is an error.
- */
-int Ed25519Verify(WOLFSSL* ssl, const byte* in, word32 inSz, const byte* msg,
- word32 msgSz, ed25519_key* key, buffer* keyBufInfo)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
-
- WOLFSSL_ENTER("Ed25519Verify");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->Ed25519VerifyCb) {
- void* ctx = wolfSSL_GetEd25519VerifyCtx(ssl);
- ret = ssl->ctx->Ed25519VerifyCb(ssl, in, inSz, msg, msgSz, keyBuf,
- keySz, &ssl->eccVerifyRes, ctx);
- }
- else
-#endif /* HAVE_PK_CALLBACKS */
- {
- ret = wc_ed25519_verify_msg(in, inSz, msg, msgSz,
- &ssl->eccVerifyRes, key);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
- else
-#endif /* WOLFSSL_ASYNC_CRYPT */
- {
- ret = (ret != 0 || ssl->eccVerifyRes == 0) ? VERIFY_SIGN_ERROR : 0;
- }
-
- WOLFSSL_LEAVE("Ed25519Verify", ret);
-
- return ret;
-}
-#endif /* HAVE_ED25519 */
-
-#ifndef WOLFSSL_NO_TLS12
-
-#ifdef HAVE_CURVE25519
-#ifdef HAVE_PK_CALLBACKS
- /* Gets X25519 key for shared secret callback testing
- * Client side: returns peer key
- * Server side: returns private key
- */
- static int X25519GetKey(WOLFSSL* ssl, curve25519_key** otherKey)
- {
- int ret = NO_PEER_KEY;
- struct curve25519_key* tmpKey = NULL;
-
- if (ssl == NULL || otherKey == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if (!ssl->peerX25519Key || !ssl->peerX25519KeyPresent ||
- !ssl->peerX25519Key->dp) {
- return NO_PEER_KEY;
- }
- tmpKey = (struct curve25519_key*)ssl->peerX25519Key;
- }
- else if (ssl->options.side == WOLFSSL_SERVER_END) {
- if (!ssl->eccTempKeyPresent) {
- return NO_PRIVATE_KEY;
- }
- tmpKey = (struct curve25519_key*)ssl->eccTempKey;
- }
-
- if (tmpKey) {
- *otherKey = (curve25519_key *)tmpKey;
- ret = 0;
- }
-
- return ret;
- }
-#endif /* HAVE_PK_CALLBACKS */
-
-static int X25519SharedSecret(WOLFSSL* ssl, curve25519_key* priv_key,
- curve25519_key* pub_key, byte* pubKeyDer, word32* pubKeySz,
- byte* out, word32* outlen, int side)
-{
- int ret;
-
- (void)ssl;
- (void)pubKeyDer;
- (void)pubKeySz;
- (void)side;
-
- WOLFSSL_ENTER("X25519SharedSecret");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->X25519SharedSecretCb) {
- curve25519_key* otherKey = NULL;
-
- ret = X25519GetKey(ssl, &otherKey);
- if (ret == 0) {
- void* ctx = wolfSSL_GetX25519SharedSecretCtx(ssl);
- ret = ssl->ctx->X25519SharedSecretCb(ssl, otherKey, pubKeyDer,
- pubKeySz, out, outlen, side, ctx);
- }
- }
- else
-#endif
- {
- ret = wc_curve25519_shared_secret_ex(priv_key, pub_key, out, outlen,
- EC25519_LITTLE_ENDIAN);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &priv_key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("X25519SharedSecret", ret);
-
- return ret;
-}
-
-static int X25519MakeKey(WOLFSSL* ssl, curve25519_key* key,
- curve25519_key* peer)
-{
- int ret = 0;
-
- (void)peer;
-
- WOLFSSL_ENTER("X25519MakeKey");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_NONE);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->X25519KeyGenCb) {
- void* ctx = wolfSSL_GetX25519KeyGenCtx(ssl);
- ret = ssl->ctx->X25519KeyGenCb(ssl, key, CURVE25519_KEYSIZE, ctx);
- }
- else
-#endif
- {
- ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
- }
-
- if (ret == 0) {
- ssl->ecdhCurveOID = ECC_X25519_OID;
- #if defined(WOLFSSL_TLS13) || defined(HAVE_FFDHE)
- ssl->namedGroup = 0;
- #endif
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("X25519MakeKey", ret);
-
- return ret;
-}
-#endif /* HAVE_CURVE25519 */
-
-#ifdef HAVE_ED448
-/* Check whether the key contains a public key.
- * If not then pull it out of the leaf certificate.
- *
- * ssl SSL/TLS object.
- * returns MEMORY_E when unable to allocate memory, a parsing error, otherwise
- * 0 on success.
- */
-int Ed448CheckPubKey(WOLFSSL* ssl)
-{
- ed448_key* key = (ed448_key*)ssl->hsKey;
- int ret = 0;
-
- /* Public key required for signing. */
- if (!key->pubKeySet) {
- DerBuffer* leaf = ssl->buffers.certificate;
- DecodedCert* cert = (DecodedCert*)XMALLOC(sizeof(*cert), ssl->heap,
- DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- ret = MEMORY_E;
-
- if (ret == 0) {
- InitDecodedCert(cert, leaf->buffer, leaf->length, ssl->heap);
- ret = DecodeToKey(cert, 0);
- }
- if (ret == 0) {
- ret = wc_ed448_import_public(cert->publicKey, cert->pubKeySize,
- key);
- }
- if (cert != NULL) {
- FreeDecodedCert(cert);
- XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
- }
- }
-
- return ret;
-}
-
-/* Sign the data using EdDSA and key using Ed448.
- *
- * ssl SSL object.
- * in Data or message to sign.
- * inSz Length of the data.
- * out Buffer to hold signature.
- * outSz On entry, size of the buffer. On exit, the size of the signature.
- * key The private Ed448 key data.
- * keySz The length of the private key data in bytes.
- * ctx The callback context.
- * returns 0 on success, otherwise the value is an error.
- */
-int Ed448Sign(WOLFSSL* ssl, const byte* in, word32 inSz, byte* out,
- word32* outSz, ed448_key* key, DerBuffer* keyBufInfo)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
-
- WOLFSSL_ENTER("Ed448Sign");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#if defined(HAVE_PK_CALLBACKS)
- if (ssl->ctx->Ed448SignCb) {
- void* ctx = wolfSSL_GetEd448SignCtx(ssl);
- ret = ssl->ctx->Ed448SignCb(ssl, in, inSz, out, outSz, keyBuf, keySz,
- ctx);
- }
- else
-#endif /* HAVE_PK_CALLBACKS */
- {
- ret = wc_ed448_sign_msg(in, inSz, out, outSz, key, NULL, 0);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("Ed448Sign", ret);
-
- return ret;
-}
-
-/* Verify the data using EdDSA and key using Ed448.
- *
- * ssl SSL object.
- * in Signature data.
- * inSz Length of the signature data in bytes.
- * msg Message to verify.
- * outSz Length of message in bytes.
- * key The public Ed448 key data.
- * keySz The length of the private key data in bytes.
- * ctx The callback context.
- * returns 0 on success, otherwise the value is an error.
- */
-int Ed448Verify(WOLFSSL* ssl, const byte* in, word32 inSz, const byte* msg,
- word32 msgSz, ed448_key* key, buffer* keyBufInfo)
-{
- int ret;
-#ifdef HAVE_PK_CALLBACKS
- const byte* keyBuf = NULL;
- word32 keySz = 0;
-
- if (keyBufInfo) {
- keyBuf = keyBufInfo->buffer;
- keySz = keyBufInfo->length;
- }
-#endif
-
- (void)ssl;
- (void)keyBufInfo;
-
- WOLFSSL_ENTER("Ed448Verify");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->Ed448VerifyCb) {
- void* ctx = wolfSSL_GetEd448VerifyCtx(ssl);
- ret = ssl->ctx->Ed448VerifyCb(ssl, in, inSz, msg, msgSz, keyBuf, keySz,
- &ssl->eccVerifyRes, ctx);
- }
- else
-#endif /* HAVE_PK_CALLBACKS */
- {
- ret = wc_ed448_verify_msg(in, inSz, msg, msgSz, &ssl->eccVerifyRes, key,
- NULL, 0);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
- else
-#endif /* WOLFSSL_ASYNC_CRYPT */
- {
- ret = (ret != 0 || ssl->eccVerifyRes == 0) ? VERIFY_SIGN_ERROR : 0;
- }
-
- WOLFSSL_LEAVE("Ed448Verify", ret);
-
- return ret;
-}
-#endif /* HAVE_ED448 */
-
-#ifdef HAVE_CURVE448
-#ifdef HAVE_PK_CALLBACKS
- /* Gets X448 key for shared secret callback testing
- * Client side: returns peer key
- * Server side: returns private key
- */
- static int X448GetKey(WOLFSSL* ssl, curve448_key** otherKey)
- {
- int ret = NO_PEER_KEY;
- struct curve448_key* tmpKey = NULL;
-
- if (ssl == NULL || otherKey == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if (!ssl->peerX448Key || !ssl->peerX448KeyPresent) {
- return NO_PEER_KEY;
- }
- tmpKey = (struct curve448_key*)ssl->peerX448Key;
- }
- else if (ssl->options.side == WOLFSSL_SERVER_END) {
- if (!ssl->eccTempKeyPresent) {
- return NO_PRIVATE_KEY;
- }
- tmpKey = (struct curve448_key*)ssl->eccTempKey;
- }
-
- if (tmpKey) {
- *otherKey = (curve448_key *)tmpKey;
- ret = 0;
- }
-
- return ret;
- }
-#endif /* HAVE_PK_CALLBACKS */
-
-static int X448SharedSecret(WOLFSSL* ssl, curve448_key* priv_key,
- curve448_key* pub_key, byte* pubKeyDer,
- word32* pubKeySz, byte* out, word32* outlen,
- int side)
-{
- int ret;
-
- (void)ssl;
- (void)pubKeyDer;
- (void)pubKeySz;
- (void)side;
-
- WOLFSSL_ENTER("X448SharedSecret");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &priv_key->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->X448SharedSecretCb) {
- curve448_key* otherKey = NULL;
-
- ret = X448GetKey(ssl, &otherKey);
- if (ret == 0) {
- void* ctx = wolfSSL_GetX448SharedSecretCtx(ssl);
- ret = ssl->ctx->X448SharedSecretCb(ssl, otherKey, pubKeyDer,
- pubKeySz, out, outlen, side, ctx);
- }
- }
- else
-#endif
- {
- ret = wc_curve448_shared_secret_ex(priv_key, pub_key, out, outlen,
- EC448_LITTLE_ENDIAN);
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &priv_key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("X448SharedSecret", ret);
-
- return ret;
-}
-
-static int X448MakeKey(WOLFSSL* ssl, curve448_key* key, curve448_key* peer)
-{
- int ret = 0;
-
- (void)peer;
-
- WOLFSSL_ENTER("X448MakeKey");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &key->asyncDev, WC_ASYNC_FLAG_NONE);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->X448KeyGenCb) {
- void* ctx = wolfSSL_GetX448KeyGenCtx(ssl);
- ret = ssl->ctx->X448KeyGenCb(ssl, key, CURVE448_KEY_SIZE, ctx);
- }
- else
-#endif
- {
- ret = wc_curve448_make_key(ssl->rng, CURVE448_KEY_SIZE, key);
- }
-
- if (ret == 0) {
- ssl->ecdhCurveOID = ECC_X448_OID;
- #if defined(WOLFSSL_TLS13) || defined(HAVE_FFDHE)
- ssl->namedGroup = 0;
- #endif
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &key->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("X448MakeKey", ret);
-
- return ret;
-}
-#endif /* HAVE_CURVE448 */
-
-#if !defined(NO_CERTS) || !defined(NO_PSK)
-#if !defined(NO_DH)
-
-int DhGenKeyPair(WOLFSSL* ssl, DhKey* dhKey,
- byte* priv, word32* privSz,
- byte* pub, word32* pubSz)
-{
- int ret;
-
- WOLFSSL_ENTER("DhGenKeyPair");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &dhKey->asyncDev, WC_ASYNC_FLAG_NONE);
- if (ret != 0)
- return ret;
-#endif
-
- ret = wc_DhGenerateKeyPair(dhKey, ssl->rng, priv, privSz, pub, pubSz);
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &dhKey->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("DhGenKeyPair", ret);
-
- return ret;
-}
-
-int DhAgree(WOLFSSL* ssl, DhKey* dhKey,
- const byte* priv, word32 privSz,
- const byte* otherPub, word32 otherPubSz,
- byte* agree, word32* agreeSz)
-{
- int ret;
-
- (void)ssl;
-
- WOLFSSL_ENTER("DhAgree");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &dhKey->asyncDev, WC_ASYNC_FLAG_NONE);
- if (ret != 0)
- return ret;
-#endif
-
-#ifdef HAVE_PK_CALLBACKS
- if (ssl->ctx->DhAgreeCb) {
- void* ctx = wolfSSL_GetDhAgreeCtx(ssl);
-
- WOLFSSL_MSG("Calling DhAgree Callback Function");
- ret = ssl->ctx->DhAgreeCb(ssl, dhKey, priv, privSz,
- otherPub, otherPubSz, agree, agreeSz, ctx);
- }
- else
-#endif
- {
-#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- ret = wc_DhCheckPubValue(ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length, otherPub, otherPubSz);
- if (ret != 0) {
- #ifdef OPENSSL_EXTRA
- SendAlert(ssl, alert_fatal, illegal_parameter);
- #endif
- }
- else
-#endif
- {
- ret = wc_DhAgree(dhKey, agree, agreeSz, priv, privSz, otherPub,
- otherPubSz);
- }
- }
-
- /* Handle async pending response */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &dhKey->asyncDev);
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- WOLFSSL_LEAVE("DhAgree", ret);
-
- return ret;
-}
-#endif /* !NO_DH */
-#endif /* !NO_CERTS || !NO_PSK */
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-
-#ifdef HAVE_PK_CALLBACKS
-int wolfSSL_IsPrivatePkSet(WOLFSSL* ssl)
-{
- int pkcbset = 0;
- (void)ssl;
-
-#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \
- !defined(NO_RSA)
- if (0
- #ifdef HAVE_ECC
- || (ssl->ctx->EccSignCb != NULL &&
- ssl->buffers.keyType == ecc_dsa_sa_algo)
- #endif
- #ifdef HAVE_ED25519
- || (ssl->ctx->Ed25519SignCb != NULL &&
- ssl->buffers.keyType == ed25519_sa_algo)
- #endif
- #ifdef HAVE_ED448
- || (ssl->ctx->Ed448SignCb != NULL &&
- ssl->buffers.keyType == ed448_sa_algo)
- #endif
- #ifndef NO_RSA
- || (ssl->ctx->RsaSignCb != NULL && ssl->buffers.keyType == rsa_sa_algo)
- || (ssl->ctx->RsaDecCb != NULL && ssl->buffers.keyType == rsa_kea)
- #ifdef WC_RSA_PSS
- || (ssl->ctx->RsaPssSignCb != NULL &&
- ssl->buffers.keyType == rsa_pss_sa_algo)
- #endif
- #endif
- ) {
- pkcbset = 1;
- }
-#endif
- return pkcbset;
-}
-
-int wolfSSL_CTX_IsPrivatePkSet(WOLFSSL_CTX* ctx)
-{
- int pkcbset = 0;
- (void)ctx;
-#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \
- !defined(NO_RSA)
- if (0
- #ifdef HAVE_ECC
- || ctx->EccSignCb != NULL
- #endif
- #ifdef HAVE_ED25519
- || ctx->Ed25519SignCb != NULL
- #endif
- #ifdef HAVE_ED448
- || ctx->Ed448SignCb != NULL
- #endif
- #ifndef NO_RSA
- || ctx->RsaSignCb != NULL
- || ctx->RsaDecCb != NULL
- #ifdef WC_RSA_PSS
- || ctx->RsaPssSignCb != NULL
- #endif
- #endif
- ) {
- pkcbset = 1;
- }
-#endif
- return pkcbset;
-}
-#endif /* HAVE_PK_CALLBACKS */
-
-
-int InitSSL_Suites(WOLFSSL* ssl)
-{
- int keySz = 0;
- byte havePSK = 0;
- byte haveAnon = 0;
- byte haveRSA = 0;
- byte haveMcast = 0;
-
- (void)haveAnon; /* Squash unused var warnings */
- (void)haveMcast;
-
- if (!ssl)
- return BAD_FUNC_ARG;
-
-#ifndef NO_RSA
- haveRSA = 1;
-#endif
-#ifndef NO_PSK
- havePSK = (byte)ssl->options.havePSK;
-#endif /* NO_PSK */
-#ifdef HAVE_ANON
- haveAnon = (byte)ssl->options.haveAnon;
-#endif /* HAVE_ANON*/
-#ifdef WOLFSSL_MULTICAST
- haveMcast = (byte)ssl->options.haveMcast;
-#endif /* WOLFSSL_MULTICAST */
-
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->options.side == WOLFSSL_SERVER_END)
- ssl->options.maxEarlyDataSz = ssl->ctx->maxEarlyDataSz;
-#endif
-#if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
- ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
- (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
- ssl->options.cacheMessages = ssl->options.side == WOLFSSL_SERVER_END ||
- ssl->buffers.keyType == ed25519_sa_algo ||
- ssl->buffers.keyType == ed448_sa_algo;
-#endif
-
-#ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
-#endif
-
- /* make sure server has DH parms, and add PSK if there, add NTRU too */
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
- }
- else {
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK,
- TRUE, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
- }
-
-#if !defined(NO_CERTS) && !defined(WOLFSSL_SESSION_EXPORT)
- /* make sure server has cert and key unless using PSK, Anon, or
- * Multicast. This should be true even if just switching ssl ctx */
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- !havePSK && !haveAnon && !haveMcast) {
-
- /* server certificate must be loaded */
- if (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer) {
- WOLFSSL_MSG("Server missing certificate");
- return NO_PRIVATE_KEY;
- }
-
- /* allow no private key if using PK callbacks and CB is set */
- #ifdef HAVE_PK_CALLBACKS
- if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) {
- WOLFSSL_MSG("Using PK for server private key");
- }
- else
- #endif
- if (!ssl->buffers.key || !ssl->buffers.key->buffer) {
- WOLFSSL_MSG("Server missing private key");
- return NO_PRIVATE_KEY;
- }
- }
-#endif
-
- return WOLFSSL_SUCCESS;
-}
-
-/* returns new reference count. Arg incr positive=up or negative=down */
-int SSL_CTX_RefCount(WOLFSSL_CTX* ctx, int incr)
-{
- int refCount;
-
- if (ctx == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (wc_LockMutex(&ctx->countMutex) != 0) {
- WOLFSSL_MSG("Couldn't lock CTX count mutex");
- return BAD_MUTEX_E;
- }
-
- ctx->refCount += incr;
- /* make sure refCount is never negative */
- if (ctx->refCount < 0) {
- ctx->refCount = 0;
- }
- refCount = ctx->refCount;
-
- wc_UnLockMutex(&ctx->countMutex);
-
- return refCount;
-}
-
-/* This function inherits a WOLFSSL_CTX's fields into an SSL object.
- It is used during initialization and to switch an ssl's CTX with
- wolfSSL_Set_SSL_CTX. Requires ssl->suites alloc and ssl-arrays with PSK
- unless writeDup is on.
-
- ssl object to initialize
- ctx parent factory
- writeDup flag indicating this is a write dup only
-
- WOLFSSL_SUCCESS return value on success */
-int SetSSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
-{
- int ret;
- byte newSSL;
-
- if (!ssl || !ctx)
- return BAD_FUNC_ARG;
-
-#ifndef SINGLE_THREADED
- if (ssl->suites == NULL && !writeDup)
- return BAD_FUNC_ARG;
-#endif
-
- newSSL = ssl->ctx == NULL; /* Assign after null check */
-
-#ifndef NO_PSK
- if (ctx->server_hint[0] && ssl->arrays == NULL && !writeDup) {
- return BAD_FUNC_ARG; /* needed for copy below */
- }
-#endif
-
- /* decrement previous CTX reference count if exists.
- * This should only happen if switching ctxs!*/
- if (!newSSL) {
- WOLFSSL_MSG("freeing old ctx to decrement reference count. Switching ctx.");
- wolfSSL_CTX_free(ssl->ctx);
- }
-
- /* increment CTX reference count */
- if ((ret = SSL_CTX_RefCount(ctx, 1)) < 0) {
- return ret;
- }
- ret = WOLFSSL_SUCCESS; /* set default ret */
-
- ssl->ctx = ctx; /* only for passing to calls, options could change */
- ssl->version = ctx->method->version;
-
-#ifdef HAVE_ECC
- ssl->eccTempKeySz = ctx->eccTempKeySz;
- ssl->ecdhCurveOID = ctx->ecdhCurveOID;
-#endif
-#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
- ssl->pkCurveOID = ctx->pkCurveOID;
-#endif
-
-#ifdef OPENSSL_EXTRA
- ssl->options.mask = ctx->mask;
- ssl->CBIS = ctx->CBIS;
-#endif
- ssl->timeout = ctx->timeout;
- ssl->verifyCallback = ctx->verifyCallback;
- ssl->options.side = ctx->method->side;
- ssl->options.downgrade = ctx->method->downgrade;
- ssl->options.minDowngrade = ctx->minDowngrade;
-
- ssl->options.haveDH = ctx->haveDH;
- ssl->options.haveNTRU = ctx->haveNTRU;
- ssl->options.haveECDSAsig = ctx->haveECDSAsig;
- ssl->options.haveECC = ctx->haveECC;
- ssl->options.haveStaticECC = ctx->haveStaticECC;
-
-#ifndef NO_PSK
- ssl->options.havePSK = ctx->havePSK;
- ssl->options.client_psk_cb = ctx->client_psk_cb;
- ssl->options.server_psk_cb = ctx->server_psk_cb;
-#ifdef WOLFSSL_TLS13
- ssl->options.client_psk_tls13_cb = ctx->client_psk_tls13_cb;
- ssl->options.server_psk_tls13_cb = ctx->server_psk_tls13_cb;
-#endif
-#endif /* NO_PSK */
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->options.side == WOLFSSL_SERVER_END)
- ssl->options.maxEarlyDataSz = ctx->maxEarlyDataSz;
-#endif
-
-#ifdef HAVE_ANON
- ssl->options.haveAnon = ctx->haveAnon;
-#endif
-#ifndef NO_DH
- ssl->options.minDhKeySz = ctx->minDhKeySz;
- ssl->options.maxDhKeySz = ctx->maxDhKeySz;
-#endif
-#ifndef NO_RSA
- ssl->options.minRsaKeySz = ctx->minRsaKeySz;
-#endif
-#ifdef HAVE_ECC
- ssl->options.minEccKeySz = ctx->minEccKeySz;
-#endif
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- ssl->options.verifyDepth = ctx->verifyDepth;
-#endif
-
- ssl->options.sessionCacheOff = ctx->sessionCacheOff;
- ssl->options.sessionCacheFlushOff = ctx->sessionCacheFlushOff;
-#ifdef HAVE_EXT_CACHE
- ssl->options.internalCacheOff = ctx->internalCacheOff;
-#endif
-
- ssl->options.verifyPeer = ctx->verifyPeer;
- ssl->options.verifyNone = ctx->verifyNone;
- ssl->options.failNoCert = ctx->failNoCert;
- ssl->options.failNoCertxPSK = ctx->failNoCertxPSK;
- ssl->options.sendVerify = ctx->sendVerify;
-
- ssl->options.partialWrite = ctx->partialWrite;
- ssl->options.quietShutdown = ctx->quietShutdown;
- ssl->options.groupMessages = ctx->groupMessages;
-
-#ifndef NO_DH
- #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
- !defined(HAVE_SELFTEST)
- ssl->options.dhKeyTested = ctx->dhKeyTested;
- #endif
- ssl->buffers.serverDH_P = ctx->serverDH_P;
- ssl->buffers.serverDH_G = ctx->serverDH_G;
-#endif
-
-#ifndef NO_CERTS
- /* ctx still owns certificate, certChain, key, dh, and cm */
- ssl->buffers.certificate = ctx->certificate;
- ssl->buffers.certChain = ctx->certChain;
-#ifdef WOLFSSL_TLS13
- ssl->buffers.certChainCnt = ctx->certChainCnt;
-#endif
- ssl->buffers.key = ctx->privateKey;
- ssl->buffers.keyType = ctx->privateKeyType;
- ssl->buffers.keyId = ctx->privateKeyId;
- ssl->buffers.keySz = ctx->privateKeySz;
- ssl->buffers.keyDevId = ctx->privateKeyDevId;
-#endif
-#if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
- ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
- (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
- ssl->options.cacheMessages = ssl->options.side == WOLFSSL_SERVER_END ||
- ssl->buffers.keyType == ed25519_sa_algo ||
- ssl->buffers.keyType == ed448_sa_algo;
-#endif
-
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- ssl->devId = ctx->devId;
-#endif
-
- if (writeDup == 0) {
-#ifndef NO_PSK
- if (ctx->server_hint[0]) { /* set in CTX */
- XSTRNCPY(ssl->arrays->server_hint, ctx->server_hint,
- sizeof(ssl->arrays->server_hint));
- ssl->arrays->server_hint[MAX_PSK_ID_LEN] = '\0'; /* null term */
- }
-#endif /* NO_PSK */
-
- if (ctx->suites) {
-#ifndef SINGLE_THREADED
- *ssl->suites = *ctx->suites;
-#else
- ssl->suites = ctx->suites;
-#endif
- }
- else {
- XMEMSET(ssl->suites, 0, sizeof(Suites));
- }
-
- if (ssl->options.side != WOLFSSL_NEITHER_END) {
- /* Defer initializing suites until accept or connect */
- ret = InitSSL_Suites(ssl);
- }
- } /* writeDup check */
-
-#ifdef WOLFSSL_SESSION_EXPORT
- #ifdef WOLFSSL_DTLS
- ssl->dtls_export = ctx->dtls_export; /* export function for session */
- #endif
-#endif
-
- ssl->CBIORecv = ctx->CBIORecv;
- ssl->CBIOSend = ctx->CBIOSend;
-#ifdef OPENSSL_EXTRA
- ssl->readAhead = ctx->readAhead;
-#endif
- ssl->verifyDepth = ctx->verifyDepth;
-
- return ret;
-}
-
-int InitHandshakeHashes(WOLFSSL* ssl)
-{
- int ret;
-
- /* make sure existing handshake hashes are free'd */
- if (ssl->hsHashes != NULL) {
- FreeHandshakeHashes(ssl);
- }
-
- /* allocate handshake hashes */
- ssl->hsHashes = (HS_Hashes*)XMALLOC(sizeof(HS_Hashes), ssl->heap,
- DYNAMIC_TYPE_HASHES);
- if (ssl->hsHashes == NULL) {
- WOLFSSL_MSG("HS_Hashes Memory error");
- return MEMORY_E;
- }
- XMEMSET(ssl->hsHashes, 0, sizeof(HS_Hashes));
-
-#ifndef NO_OLD_TLS
-#ifndef NO_MD5
- ret = wc_InitMd5_ex(&ssl->hsHashes->hashMd5, ssl->heap, ssl->devId);
- if (ret != 0)
- return ret;
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_Md5SetFlags(&ssl->hsHashes->hashMd5, WC_HASH_FLAG_WILLCOPY);
- #endif
-#endif
-#ifndef NO_SHA
- ret = wc_InitSha_ex(&ssl->hsHashes->hashSha, ssl->heap, ssl->devId);
- if (ret != 0)
- return ret;
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_ShaSetFlags(&ssl->hsHashes->hashSha, WC_HASH_FLAG_WILLCOPY);
- #endif
-#endif
-#endif /* !NO_OLD_TLS */
-#ifndef NO_SHA256
- ret = wc_InitSha256_ex(&ssl->hsHashes->hashSha256, ssl->heap, ssl->devId);
- if (ret != 0)
- return ret;
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_Sha256SetFlags(&ssl->hsHashes->hashSha256, WC_HASH_FLAG_WILLCOPY);
- #endif
-#endif
-#ifdef WOLFSSL_SHA384
- ret = wc_InitSha384_ex(&ssl->hsHashes->hashSha384, ssl->heap, ssl->devId);
- if (ret != 0)
- return ret;
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_Sha384SetFlags(&ssl->hsHashes->hashSha384, WC_HASH_FLAG_WILLCOPY);
- #endif
-#endif
-#ifdef WOLFSSL_SHA512
- ret = wc_InitSha512_ex(&ssl->hsHashes->hashSha512, ssl->heap, ssl->devId);
- if (ret != 0)
- return ret;
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_Sha512SetFlags(&ssl->hsHashes->hashSha512, WC_HASH_FLAG_WILLCOPY);
- #endif
-#endif
-
- return ret;
-}
-
-void FreeHandshakeHashes(WOLFSSL* ssl)
-{
- if (ssl->hsHashes) {
-#ifndef NO_OLD_TLS
- #ifndef NO_MD5
- wc_Md5Free(&ssl->hsHashes->hashMd5);
- #endif
- #ifndef NO_SHA
- wc_ShaFree(&ssl->hsHashes->hashSha);
- #endif
-#endif /* !NO_OLD_TLS */
- #ifndef NO_SHA256
- wc_Sha256Free(&ssl->hsHashes->hashSha256);
- #endif
- #ifdef WOLFSSL_SHA384
- wc_Sha384Free(&ssl->hsHashes->hashSha384);
- #endif
- #ifdef WOLFSSL_SHA512
- wc_Sha512Free(&ssl->hsHashes->hashSha512);
- #endif
- #if (defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
- !defined(WOLFSSL_NO_CLIENT_AUTH)
- if (ssl->hsHashes->messages != NULL) {
- XFREE(ssl->hsHashes->messages, ssl->heap, DYNAMIC_TYPE_HASHES);
- ssl->hsHashes->messages = NULL;
- }
- #endif
-
- XFREE(ssl->hsHashes, ssl->heap, DYNAMIC_TYPE_HASHES);
- ssl->hsHashes = NULL;
- }
-}
-
-
-/* init everything to 0, NULL, default values before calling anything that may
- fail so that destructor has a "good" state to cleanup
-
- ssl object to initialize
- ctx parent factory
- writeDup flag indicating this is a write dup only
-
- 0 on success */
-int InitSSL(WOLFSSL* ssl, WOLFSSL_CTX* ctx, int writeDup)
-{
- int ret;
-
- XMEMSET(ssl, 0, sizeof(WOLFSSL));
-
-#if defined(WOLFSSL_STATIC_MEMORY)
- if (ctx->heap != NULL) {
- WOLFSSL_HEAP_HINT* ssl_hint;
- WOLFSSL_HEAP_HINT* ctx_hint;
-
- /* avoid dereferencing a test value */
- #ifdef WOLFSSL_HEAP_TEST
- if (ctx->heap == (void*)WOLFSSL_HEAP_TEST) {
- ssl->heap = ctx->heap;
- }
- else {
- #endif
- ssl->heap = (WOLFSSL_HEAP_HINT*)XMALLOC(sizeof(WOLFSSL_HEAP_HINT),
- ctx->heap, DYNAMIC_TYPE_SSL);
- if (ssl->heap == NULL) {
- return MEMORY_E;
- }
- XMEMSET(ssl->heap, 0, sizeof(WOLFSSL_HEAP_HINT));
- ssl_hint = ((WOLFSSL_HEAP_HINT*)(ssl->heap));
- ctx_hint = ((WOLFSSL_HEAP_HINT*)(ctx->heap));
-
- /* lock and check IO count / handshake count */
- if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) {
- WOLFSSL_MSG("Bad memory_mutex lock");
- XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
- ssl->heap = NULL; /* free and set to NULL for IO counter */
- return BAD_MUTEX_E;
- }
- if (ctx_hint->memory->maxHa > 0 &&
- ctx_hint->memory->maxHa <= ctx_hint->memory->curHa) {
- WOLFSSL_MSG("At max number of handshakes for static memory");
- wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
- XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
- ssl->heap = NULL; /* free and set to NULL for IO counter */
- return MEMORY_E;
- }
-
- if (ctx_hint->memory->maxIO > 0 &&
- ctx_hint->memory->maxIO <= ctx_hint->memory->curIO) {
- WOLFSSL_MSG("At max number of IO allowed for static memory");
- wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
- XFREE(ssl->heap, ctx->heap, DYNAMIC_TYPE_SSL);
- ssl->heap = NULL; /* free and set to NULL for IO counter */
- return MEMORY_E;
- }
- ctx_hint->memory->curIO++;
- ctx_hint->memory->curHa++;
- ssl_hint->memory = ctx_hint->memory;
- ssl_hint->haFlag = 1;
- wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
-
- /* check if tracking stats */
- if (ctx_hint->memory->flag & WOLFMEM_TRACK_STATS) {
- ssl_hint->stats = (WOLFSSL_MEM_CONN_STATS*)XMALLOC(
- sizeof(WOLFSSL_MEM_CONN_STATS), ctx->heap, DYNAMIC_TYPE_SSL);
- if (ssl_hint->stats == NULL) {
- return MEMORY_E;
- }
- XMEMSET(ssl_hint->stats, 0, sizeof(WOLFSSL_MEM_CONN_STATS));
- }
-
- /* check if using fixed IO buffers */
- if (ctx_hint->memory->flag & WOLFMEM_IO_POOL_FIXED) {
- if (wc_LockMutex(&(ctx_hint->memory->memory_mutex)) != 0) {
- WOLFSSL_MSG("Bad memory_mutex lock");
- return BAD_MUTEX_E;
- }
- if (SetFixedIO(ctx_hint->memory, &(ssl_hint->inBuf)) != 1) {
- wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
- return MEMORY_E;
- }
- if (SetFixedIO(ctx_hint->memory, &(ssl_hint->outBuf)) != 1) {
- wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
- return MEMORY_E;
- }
- if (ssl_hint->outBuf == NULL || ssl_hint->inBuf == NULL) {
- WOLFSSL_MSG("Not enough memory to create fixed IO buffers");
- wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
- return MEMORY_E;
- }
- wc_UnLockMutex(&(ctx_hint->memory->memory_mutex));
- }
- #ifdef WOLFSSL_HEAP_TEST
- }
- #endif
- }
- else {
- ssl->heap = ctx->heap;
- }
-#else
- ssl->heap = ctx->heap; /* carry over user heap without static memory */
-#endif /* WOLFSSL_STATIC_MEMORY */
-
- ssl->buffers.inputBuffer.buffer = ssl->buffers.inputBuffer.staticBuffer;
- ssl->buffers.inputBuffer.bufferSize = STATIC_BUFFER_LEN;
-
- ssl->buffers.outputBuffer.buffer = ssl->buffers.outputBuffer.staticBuffer;
- ssl->buffers.outputBuffer.bufferSize = STATIC_BUFFER_LEN;
-
-#ifdef KEEP_PEER_CERT
- InitX509(&ssl->peerCert, 0, ssl->heap);
-#endif
-
- ssl->rfd = -1; /* set to invalid descriptor */
- ssl->wfd = -1;
- ssl->devId = ctx->devId; /* device for async HW (from wolfAsync_DevOpen) */
-
- ssl->IOCB_ReadCtx = &ssl->rfd; /* prevent invalid pointer access if not */
- ssl->IOCB_WriteCtx = &ssl->wfd; /* correctly set */
-
-#ifdef HAVE_NETX
- ssl->IOCB_ReadCtx = &ssl->nxCtx; /* default NetX IO ctx, same for read */
- ssl->IOCB_WriteCtx = &ssl->nxCtx; /* and write */
-#elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
- ssl->mnCtx = mynewt_ctx_new();
- if(!ssl->mnCtx) {
- return MEMORY_E;
- }
- ssl->IOCB_ReadCtx = ssl->mnCtx; /* default Mynewt IO ctx, same for read */
- ssl->IOCB_WriteCtx = ssl->mnCtx; /* and write */
-#elif defined (WOLFSSL_GNRC)
- ssl->IOCB_ReadCtx = ssl->gnrcCtx;
- ssl->IOCB_WriteCtx = ssl->gnrcCtx;
-#endif
- /* initialize states */
- ssl->options.serverState = NULL_STATE;
- ssl->options.clientState = NULL_STATE;
- ssl->options.connectState = CONNECT_BEGIN;
- ssl->options.acceptState = ACCEPT_BEGIN;
- ssl->options.handShakeState = NULL_STATE;
- ssl->options.processReply = doProcessInit;
- ssl->options.asyncState = TLS_ASYNC_BEGIN;
- ssl->options.buildMsgState = BUILD_MSG_BEGIN;
- ssl->encrypt.state = CIPHER_STATE_BEGIN;
- ssl->decrypt.state = CIPHER_STATE_BEGIN;
-#ifndef NO_DH
- #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
- !defined(HAVE_SELFTEST)
- ssl->options.dhDoKeyTest = 1;
- #endif
-#endif
-
-#ifdef WOLFSSL_DTLS
- #ifdef WOLFSSL_SCTP
- ssl->options.dtlsSctp = ctx->dtlsSctp;
- #endif
- #if defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)
- ssl->dtlsMtuSz = ctx->dtlsMtuSz;
- ssl->dtls_expected_rx = ssl->dtlsMtuSz;
- #else
- ssl->dtls_expected_rx = MAX_MTU;
- #endif
- ssl->dtls_timeout_init = DTLS_TIMEOUT_INIT;
- ssl->dtls_timeout_max = DTLS_TIMEOUT_MAX;
- ssl->dtls_timeout = ssl->dtls_timeout_init;
- ssl->buffers.dtlsCtx.rfd = -1;
- ssl->buffers.dtlsCtx.wfd = -1;
-#endif
-
-#ifndef WOLFSSL_AEAD_ONLY
- #ifndef NO_OLD_TLS
- ssl->hmac = SSL_hmac; /* default to SSLv3 */
- #elif !defined(WOLFSSL_NO_TLS12)
- ssl->hmac = TLS_hmac;
- #endif
-#endif
-
-
- ssl->cipher.ssl = ssl;
-
-#ifdef HAVE_EXTENDED_MASTER
- ssl->options.haveEMS = ctx->haveEMS;
-#endif
- ssl->options.useClientOrder = ctx->useClientOrder;
- ssl->options.mutualAuth = ctx->mutualAuth;
-
-#ifdef WOLFSSL_TLS13
- #ifdef HAVE_SESSION_TICKET
- ssl->options.noTicketTls13 = ctx->noTicketTls13;
- #endif
- ssl->options.noPskDheKe = ctx->noPskDheKe;
- #if defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- ssl->options.postHandshakeAuth = ctx->postHandshakeAuth;
- #endif
-
- if (ctx->numGroups > 0) {
- XMEMCPY(ssl->group, ctx->group, sizeof(*ctx->group) * ctx->numGroups);
- ssl->numGroups = ctx->numGroups;
- }
-#endif
-
-#ifdef HAVE_TLS_EXTENSIONS
-#ifdef HAVE_MAX_FRAGMENT
- ssl->max_fragment = MAX_RECORD_SIZE;
-#endif
-#ifdef HAVE_ALPN
- ssl->alpn_client_list = NULL;
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
- ssl->alpnSelect = ctx->alpnSelect;
- ssl->alpnSelectArg = ctx->alpnSelectArg;
- #endif
-#endif
-#ifdef HAVE_SUPPORTED_CURVES
- ssl->options.userCurves = ctx->userCurves;
-#endif
-#endif /* HAVE_TLS_EXTENSIONS */
-
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- ssl->options.disallowEncThenMac = ctx->disallowEncThenMac;
-#endif
-
- /* default alert state (none) */
- ssl->alert_history.last_rx.code = -1;
- ssl->alert_history.last_rx.level = -1;
- ssl->alert_history.last_tx.code = -1;
- ssl->alert_history.last_tx.level = -1;
-
-#ifdef OPENSSL_EXTRA
- /* copy over application session context ID */
- ssl->sessionCtxSz = ctx->sessionCtxSz;
- XMEMCPY(ssl->sessionCtx, ctx->sessionCtx, ctx->sessionCtxSz);
- ssl->cbioFlag = ctx->cbioFlag;
-
-#endif
-
- InitCiphers(ssl);
- InitCipherSpecs(&ssl->specs);
-
- /* all done with init, now can return errors, call other stuff */
-
- if (!writeDup) {
- /* arrays */
- ssl->arrays = (Arrays*)XMALLOC(sizeof(Arrays), ssl->heap,
- DYNAMIC_TYPE_ARRAYS);
- if (ssl->arrays == NULL) {
- WOLFSSL_MSG("Arrays Memory error");
- return MEMORY_E;
- }
- XMEMSET(ssl->arrays, 0, sizeof(Arrays));
-#if defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER)
- ssl->arrays->preMasterSz = ENCRYPT_LEN;
- ssl->arrays->preMasterSecret = (byte*)XMALLOC(ENCRYPT_LEN, ssl->heap,
- DYNAMIC_TYPE_SECRET);
- if (ssl->arrays->preMasterSecret == NULL) {
- return MEMORY_E;
- }
- XMEMSET(ssl->arrays->preMasterSecret, 0, ENCRYPT_LEN);
-#endif
-
-#ifdef OPENSSL_EXTRA
- if ((ssl->param = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
- sizeof(WOLFSSL_X509_VERIFY_PARAM),
- ssl->heap, DYNAMIC_TYPE_OPENSSL)) == NULL) {
- WOLFSSL_MSG("ssl->param memory error");
- return MEMORY_E;
- }
- XMEMSET(ssl->param, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
-#endif
-
-#ifdef SINGLE_THREADED
- if (ctx->suites == NULL)
-#endif
- {
- /* suites */
- ssl->suites = (Suites*)XMALLOC(sizeof(Suites), ssl->heap,
- DYNAMIC_TYPE_SUITES);
- if (ssl->suites == NULL) {
- WOLFSSL_MSG("Suites Memory error");
- return MEMORY_E;
- }
- #ifdef OPENSSL_ALL
- ssl->suites->stack = NULL;
- #endif
-#ifdef SINGLE_THREADED
- ssl->options.ownSuites = 1;
-#endif
- }
-#ifdef SINGLE_THREADED
- else {
- ssl->options.ownSuites = 0;
- }
-#endif
- }
-
- /* Initialize SSL with the appropriate fields from it's ctx */
- /* requires valid arrays and suites unless writeDup ing */
- if ((ret = SetSSL_CTX(ssl, ctx, writeDup)) != WOLFSSL_SUCCESS)
- return ret;
-
- ssl->options.dtls = ssl->version.major == DTLS_MAJOR;
-
-#ifdef SINGLE_THREADED
- ssl->rng = ctx->rng; /* CTX may have one, if so use it */
-#endif
-
- if (ssl->rng == NULL) {
- /* RNG */
- ssl->rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), ssl->heap,DYNAMIC_TYPE_RNG);
- if (ssl->rng == NULL) {
- WOLFSSL_MSG("RNG Memory error");
- return MEMORY_E;
- }
- XMEMSET(ssl->rng, 0, sizeof(WC_RNG));
- ssl->options.weOwnRng = 1;
-
- /* FIPS RNG API does not accept a heap hint */
-#ifndef HAVE_FIPS
- if ( (ret = wc_InitRng_ex(ssl->rng, ssl->heap, ssl->devId)) != 0) {
- WOLFSSL_MSG("RNG Init error");
- return ret;
- }
-#else
- if ( (ret = wc_InitRng(ssl->rng)) != 0) {
- WOLFSSL_MSG("RNG Init error");
- return ret;
- }
-#endif
- }
-
-#ifdef HAVE_WRITE_DUP
- if (writeDup) {
- /* all done */
- return 0;
- }
-#endif
-
- /* hsHashes */
- ret = InitHandshakeHashes(ssl);
- if (ret != 0)
- return ret;
-
-#if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
- if (ssl->options.dtls && ssl->options.side == WOLFSSL_SERVER_END) {
- ret = wolfSSL_DTLS_SetCookieSecret(ssl, NULL, 0);
- if (ret != 0) {
- WOLFSSL_MSG("DTLS Cookie Secret error");
- return ret;
- }
- }
-#endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */
-
-#ifdef HAVE_SECRET_CALLBACK
- ssl->sessionSecretCb = NULL;
- ssl->sessionSecretCtx = NULL;
-#ifdef WOLFSSL_TLS13
- ssl->tls13SecretCb = NULL;
- ssl->tls13SecretCtx = NULL;
-#endif
-#endif
-
-#ifdef HAVE_SESSION_TICKET
- ssl->session.ticket = ssl->session.staticTicket;
-#endif
-
-#ifdef WOLFSSL_MULTICAST
- if (ctx->haveMcast) {
- int i;
-
- ssl->options.haveMcast = 1;
- ssl->options.mcastID = ctx->mcastID;
-
- /* Force the state to look like handshake has completed. */
- /* Keying material is supplied externally. */
- ssl->options.serverState = SERVER_FINISHED_COMPLETE;
- ssl->options.clientState = CLIENT_FINISHED_COMPLETE;
- ssl->options.connectState = SECOND_REPLY_DONE;
- ssl->options.acceptState = ACCEPT_THIRD_REPLY_DONE;
- ssl->options.handShakeState = HANDSHAKE_DONE;
- ssl->options.handShakeDone = 1;
-
- for (i = 0; i < WOLFSSL_DTLS_PEERSEQ_SZ; i++)
- ssl->keys.peerSeq[i].peerId = INVALID_PEER_ID;
- }
-#endif
-
-#ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- int useSecureReneg = ssl->ctx->useSecureReneg;
- /* use secure renegotiation by default (not recommend) */
- #ifdef WOLFSSL_SECURE_RENEGOTIATION_ON_BY_DEFAULT
- useSecureReneg = 1;
- #endif
- if (useSecureReneg) {
- ret = wolfSSL_UseSecureRenegotiation(ssl);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- }
- }
-#endif /* HAVE_SECURE_RENEGOTIATION */
-
- return 0;
-}
-
-
-/* free use of temporary arrays */
-void FreeArrays(WOLFSSL* ssl, int keep)
-{
- if (ssl->arrays) {
- if (keep) {
- /* keeps session id for user retrieval */
- XMEMCPY(ssl->session.sessionID, ssl->arrays->sessionID, ID_LEN);
- ssl->session.sessionIDSz = ssl->arrays->sessionIDSz;
- }
- if (ssl->arrays->preMasterSecret) {
- XFREE(ssl->arrays->preMasterSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
- ssl->arrays->preMasterSecret = NULL;
- }
- XFREE(ssl->arrays->pendingMsg, ssl->heap, DYNAMIC_TYPE_ARRAYS);
- ssl->arrays->pendingMsg = NULL;
- ForceZero(ssl->arrays, sizeof(Arrays)); /* clear arrays struct */
- }
- XFREE(ssl->arrays, ssl->heap, DYNAMIC_TYPE_ARRAYS);
- ssl->arrays = NULL;
-}
-
-void FreeKey(WOLFSSL* ssl, int type, void** pKey)
-{
- if (ssl && pKey && *pKey) {
- switch (type) {
- #ifndef NO_RSA
- case DYNAMIC_TYPE_RSA:
- wc_FreeRsaKey((RsaKey*)*pKey);
- break;
- #endif /* ! NO_RSA */
- #ifdef HAVE_ECC
- case DYNAMIC_TYPE_ECC:
- wc_ecc_free((ecc_key*)*pKey);
- break;
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case DYNAMIC_TYPE_ED25519:
- wc_ed25519_free((ed25519_key*)*pKey);
- break;
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_CURVE25519
- case DYNAMIC_TYPE_CURVE25519:
- wc_curve25519_free((curve25519_key*)*pKey);
- break;
- #endif /* HAVE_CURVE25519 */
- #ifdef HAVE_ED448
- case DYNAMIC_TYPE_ED448:
- wc_ed448_free((ed448_key*)*pKey);
- break;
- #endif /* HAVE_ED448 */
- #ifdef HAVE_CURVE448
- case DYNAMIC_TYPE_CURVE448:
- wc_curve448_free((curve448_key*)*pKey);
- break;
- #endif /* HAVE_CURVE448 */
- #ifndef NO_DH
- case DYNAMIC_TYPE_DH:
- wc_FreeDhKey((DhKey*)*pKey);
- break;
- #endif /* !NO_DH */
- default:
- break;
- }
- XFREE(*pKey, ssl->heap, type);
-
- /* Reset pointer */
- *pKey = NULL;
- }
-}
-
-int AllocKey(WOLFSSL* ssl, int type, void** pKey)
-{
- int ret = BAD_FUNC_ARG;
- int sz = 0;
-
- if (ssl == NULL || pKey == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* Sanity check key destination */
- if (*pKey != NULL) {
- WOLFSSL_MSG("Key already present!");
- return BAD_STATE_E;
- }
-
- /* Determine size */
- switch (type) {
- #ifndef NO_RSA
- case DYNAMIC_TYPE_RSA:
- sz = sizeof(RsaKey);
- break;
- #endif /* ! NO_RSA */
- #ifdef HAVE_ECC
- case DYNAMIC_TYPE_ECC:
- sz = sizeof(ecc_key);
- break;
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case DYNAMIC_TYPE_ED25519:
- sz = sizeof(ed25519_key);
- break;
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_CURVE25519
- case DYNAMIC_TYPE_CURVE25519:
- sz = sizeof(curve25519_key);
- break;
- #endif /* HAVE_CURVE25519 */
- #ifdef HAVE_ED448
- case DYNAMIC_TYPE_ED448:
- sz = sizeof(ed448_key);
- break;
- #endif /* HAVE_ED448 */
- #ifdef HAVE_CURVE448
- case DYNAMIC_TYPE_CURVE448:
- sz = sizeof(curve448_key);
- break;
- #endif /* HAVE_CURVE448 */
- #ifndef NO_DH
- case DYNAMIC_TYPE_DH:
- sz = sizeof(DhKey);
- break;
- #endif /* !NO_DH */
- default:
- return BAD_FUNC_ARG;
- }
-
- if (sz == 0) {
- return NOT_COMPILED_IN;
- }
-
- /* Allocate memory for key */
- *pKey = XMALLOC(sz, ssl->heap, type);
- if (*pKey == NULL) {
- return MEMORY_E;
- }
-
- /* Initialize key */
- switch (type) {
- #ifndef NO_RSA
- case DYNAMIC_TYPE_RSA:
- ret = wc_InitRsaKey_ex((RsaKey*)*pKey, ssl->heap, ssl->devId);
- break;
- #endif /* ! NO_RSA */
- #ifdef HAVE_ECC
- case DYNAMIC_TYPE_ECC:
- ret = wc_ecc_init_ex((ecc_key*)*pKey, ssl->heap, ssl->devId);
- break;
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case DYNAMIC_TYPE_ED25519:
- wc_ed25519_init((ed25519_key*)*pKey);
- ret = 0;
- break;
- #endif /* HAVE_CURVE25519 */
- #ifdef HAVE_CURVE25519
- case DYNAMIC_TYPE_CURVE25519:
- wc_curve25519_init((curve25519_key*)*pKey);
- ret = 0;
- break;
- #endif /* HAVE_CURVE25519 */
- #ifdef HAVE_ED448
- case DYNAMIC_TYPE_ED448:
- wc_ed448_init((ed448_key*)*pKey);
- ret = 0;
- break;
- #endif /* HAVE_CURVE448 */
- #ifdef HAVE_CURVE448
- case DYNAMIC_TYPE_CURVE448:
- wc_curve448_init((curve448_key*)*pKey);
- ret = 0;
- break;
- #endif /* HAVE_CURVE448 */
- #ifndef NO_DH
- case DYNAMIC_TYPE_DH:
- ret = wc_InitDhKey_ex((DhKey*)*pKey, ssl->heap, ssl->devId);
- break;
- #endif /* !NO_DH */
- default:
- return BAD_FUNC_ARG;
- }
-
- /* On error free handshake key */
- if (ret != 0) {
- FreeKey(ssl, type, pKey);
- }
-
- return ret;
-}
-
-#if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_CURVE25519) || defined(HHAVE_ED448) || defined(HAVE_CURVE448)
-static int ReuseKey(WOLFSSL* ssl, int type, void* pKey)
-{
- int ret = 0;
-
- (void)ssl;
-
- switch (type) {
- #ifndef NO_RSA
- case DYNAMIC_TYPE_RSA:
- wc_FreeRsaKey((RsaKey*)pKey);
- ret = wc_InitRsaKey_ex((RsaKey*)pKey, ssl->heap, ssl->devId);
- break;
- #endif /* ! NO_RSA */
- #ifdef HAVE_ECC
- case DYNAMIC_TYPE_ECC:
- wc_ecc_free((ecc_key*)pKey);
- ret = wc_ecc_init_ex((ecc_key*)pKey, ssl->heap, ssl->devId);
- break;
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case DYNAMIC_TYPE_ED25519:
- wc_ed25519_free((ed25519_key*)pKey);
- ret = wc_ed25519_init((ed25519_key*)pKey);
- break;
- #endif /* HAVE_CURVE25519 */
- #ifdef HAVE_CURVE25519
- case DYNAMIC_TYPE_CURVE25519:
- wc_curve25519_free((curve25519_key*)pKey);
- ret = wc_curve25519_init((curve25519_key*)pKey);
- break;
- #endif /* HAVE_CURVE25519 */
- #ifdef HAVE_ED448
- case DYNAMIC_TYPE_ED448:
- wc_ed448_free((ed448_key*)pKey);
- ret = wc_ed448_init((ed448_key*)pKey);
- break;
- #endif /* HAVE_CURVE448 */
- #ifdef HAVE_CURVE448
- case DYNAMIC_TYPE_CURVE448:
- wc_curve448_free((curve448_key*)pKey);
- ret = wc_curve448_init((curve448_key*)pKey);
- break;
- #endif /* HAVE_CURVE448 */
- #ifndef NO_DH
- case DYNAMIC_TYPE_DH:
- wc_FreeDhKey((DhKey*)pKey);
- ret = wc_InitDhKey_ex((DhKey*)pKey, ssl->heap, ssl->devId);
- break;
- #endif /* !NO_DH */
- default:
- return BAD_FUNC_ARG;
- }
-
- return ret;
-}
-#endif
-
-void FreeKeyExchange(WOLFSSL* ssl)
-{
- /* Cleanup signature buffer */
- if (ssl->buffers.sig.buffer) {
- XFREE(ssl->buffers.sig.buffer, ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- ssl->buffers.sig.buffer = NULL;
- ssl->buffers.sig.length = 0;
- }
-
- /* Cleanup digest buffer */
- if (ssl->buffers.digest.buffer) {
- XFREE(ssl->buffers.digest.buffer, ssl->heap, DYNAMIC_TYPE_DIGEST);
- ssl->buffers.digest.buffer = NULL;
- ssl->buffers.digest.length = 0;
- }
-
- /* Free handshake key */
- FreeKey(ssl, ssl->hsType, &ssl->hsKey);
-
-#ifndef NO_DH
- /* Free temp DH key */
- FreeKey(ssl, DYNAMIC_TYPE_DH, (void**)&ssl->buffers.serverDH_Key);
-#endif
-
- /* Cleanup async */
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ssl->async.freeArgs) {
- ssl->async.freeArgs(ssl, ssl->async.args);
- ssl->async.freeArgs = NULL;
- }
-#endif
-}
-
-
-/* Free up all memory used by Suites structure from WOLFSSL */
-void FreeSuites(WOLFSSL* ssl)
-{
-#ifdef SINGLE_THREADED
- if (ssl->options.ownSuites)
-#endif
- {
- #ifdef OPENSSL_ALL
- wolfSSL_sk_SSL_CIPHER_free(ssl->suites->stack);
- #endif
- XFREE(ssl->suites, ssl->heap, DYNAMIC_TYPE_SUITES);
- }
- ssl->suites = NULL;
-}
-
-
-/* In case holding SSL object in array and don't want to free actual ssl */
-void SSL_ResourceFree(WOLFSSL* ssl)
-{
- /* Note: any resources used during the handshake should be released in the
- * function FreeHandshakeResources(). Be careful with the special cases
- * like the RNG which may optionally be kept for the whole session. (For
- * example with the RNG, it isn't used beyond the handshake except when
- * using stream ciphers where it is retained. */
-
- FreeCiphers(ssl);
- FreeArrays(ssl, 0);
- FreeKeyExchange(ssl);
- if (ssl->options.weOwnRng) {
- wc_FreeRng(ssl->rng);
- XFREE(ssl->rng, ssl->heap, DYNAMIC_TYPE_RNG);
- }
- FreeSuites(ssl);
- FreeHandshakeHashes(ssl);
- XFREE(ssl->buffers.domainName.buffer, ssl->heap, DYNAMIC_TYPE_DOMAIN);
-
- /* clear keys struct after session */
- ForceZero(&ssl->keys, sizeof(Keys));
-
-#ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3) {
- ForceZero(&ssl->clientSecret, sizeof(ssl->clientSecret));
- ForceZero(&ssl->serverSecret, sizeof(ssl->serverSecret));
- }
-#endif
-
-#ifndef NO_DH
- if (ssl->buffers.serverDH_Priv.buffer) {
- ForceZero(ssl->buffers.serverDH_Priv.buffer,
- ssl->buffers.serverDH_Priv.length);
- }
- XFREE(ssl->buffers.serverDH_Priv.buffer, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- XFREE(ssl->buffers.serverDH_Pub.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- /* parameters (p,g) may be owned by ctx */
- if (ssl->buffers.weOwnDH) {
- XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- }
-#endif /* !NO_DH */
-#ifndef NO_CERTS
- ssl->keepCert = 0; /* make sure certificate is free'd */
- wolfSSL_UnloadCertsKeys(ssl);
-#endif
-#ifndef NO_RSA
- FreeKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey);
- ssl->peerRsaKeyPresent = 0;
-#endif
-#ifdef WOLFSSL_RENESAS_TSIP_TLS
- XFREE(ssl->peerTsipEncRsaKeyIndex, ssl->heap, DYNAMIC_TYPE_RSA);
-#endif
- if (ssl->buffers.inputBuffer.dynamicFlag)
- ShrinkInputBuffer(ssl, FORCED_FREE);
- if (ssl->buffers.outputBuffer.dynamicFlag)
- ShrinkOutputBuffer(ssl);
-#if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
- XFREE(ssl->buffers.tls13CookieSecret.buffer, ssl->heap,
- DYNAMIC_TYPE_COOKIE_PWD);
-#endif
-#ifdef WOLFSSL_DTLS
- DtlsMsgPoolReset(ssl);
- if (ssl->dtls_rx_msg_list != NULL) {
- DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
- ssl->dtls_rx_msg_list = NULL;
- ssl->dtls_rx_msg_list_sz = 0;
- }
- XFREE(ssl->buffers.dtlsCtx.peer.sa, ssl->heap, DYNAMIC_TYPE_SOCKADDR);
- ssl->buffers.dtlsCtx.peer.sa = NULL;
-#ifndef NO_WOLFSSL_SERVER
- XFREE(ssl->buffers.dtlsCookieSecret.buffer, ssl->heap,
- DYNAMIC_TYPE_COOKIE_PWD);
-#endif
-#endif /* WOLFSSL_DTLS */
-#ifdef OPENSSL_EXTRA
- if (ssl->biord != ssl->biowr) /* only free write if different */
- wolfSSL_BIO_free(ssl->biowr);
- wolfSSL_BIO_free(ssl->biord); /* always free read bio */
- ssl->biowr = NULL;
- ssl->biord = NULL;
-#endif
-#ifdef HAVE_LIBZ
- FreeStreams(ssl);
-#endif
-#ifdef HAVE_ECC
- FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccKey);
- ssl->peerEccKeyPresent = 0;
- FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey);
- ssl->peerEccDsaKeyPresent = 0;
-#endif
-#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) ||defined(HAVE_CURVE448)
- {
- int dtype = 0;
- #ifdef HAVE_ECC
- dtype = DYNAMIC_TYPE_ECC;
- #endif
- #ifdef HAVE_CURVE25519
- if (ssl->peerX25519KeyPresent
- #ifdef HAVE_ECC
- || ssl->eccTempKeyPresent == DYNAMIC_TYPE_CURVE25519
- #endif /* HAVE_ECC */
- )
- {
- dtype = DYNAMIC_TYPE_CURVE25519;
- }
- #endif /* HAVE_CURVE25519 */
- #ifdef HAVE_CURVE448
- if (ssl->peerX448KeyPresent
- #ifdef HAVE_ECC
- || ssl->eccTempKeyPresent == DYNAMIC_TYPE_CURVE448
- #endif /* HAVE_ECC */
- )
- {
- dtype = DYNAMIC_TYPE_CURVE448;
- }
- #endif /* HAVE_CURVE448 */
- FreeKey(ssl, dtype, (void**)&ssl->eccTempKey);
- ssl->eccTempKeyPresent = 0;
- }
-#endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-#ifdef HAVE_CURVE25519
- FreeKey(ssl, DYNAMIC_TYPE_CURVE25519, (void**)&ssl->peerX25519Key);
- ssl->peerX25519KeyPresent = 0;
-#endif
-#ifdef HAVE_ED25519
- FreeKey(ssl, DYNAMIC_TYPE_ED25519, (void**)&ssl->peerEd25519Key);
- ssl->peerEd25519KeyPresent = 0;
- #ifdef HAVE_PK_CALLBACKS
- if (ssl->buffers.peerEd25519Key.buffer != NULL) {
- XFREE(ssl->buffers.peerEd25519Key.buffer, ssl->heap,
- DYNAMIC_TYPE_ED25519);
- ssl->buffers.peerEd25519Key.buffer = NULL;
- }
- #endif
-#endif
-#ifdef HAVE_CURVE448
- FreeKey(ssl, DYNAMIC_TYPE_CURVE448, (void**)&ssl->peerX448Key);
- ssl->peerX448KeyPresent = 0;
-#endif
-#ifdef HAVE_ED448
- FreeKey(ssl, DYNAMIC_TYPE_ED448, (void**)&ssl->peerEd448Key);
- ssl->peerEd448KeyPresent = 0;
- #ifdef HAVE_PK_CALLBACKS
- if (ssl->buffers.peerEd448Key.buffer != NULL) {
- XFREE(ssl->buffers.peerEd448Key.buffer, ssl->heap,
- DYNAMIC_TYPE_ED448);
- ssl->buffers.peerEd448Key.buffer = NULL;
- }
- #endif
-#endif
-#ifdef HAVE_PK_CALLBACKS
- #ifdef HAVE_ECC
- XFREE(ssl->buffers.peerEccDsaKey.buffer, ssl->heap, DYNAMIC_TYPE_ECC);
- #endif /* HAVE_ECC */
- #ifndef NO_RSA
- XFREE(ssl->buffers.peerRsaKey.buffer, ssl->heap, DYNAMIC_TYPE_RSA);
- #endif /* NO_RSA */
-#endif /* HAVE_PK_CALLBACKS */
-#ifdef HAVE_TLS_EXTENSIONS
- TLSX_FreeAll(ssl->extensions, ssl->heap);
-
-#ifdef HAVE_ALPN
- if (ssl->alpn_client_list != NULL) {
- XFREE(ssl->alpn_client_list, ssl->heap, DYNAMIC_TYPE_ALPN);
- ssl->alpn_client_list = NULL;
- }
-#endif
-#endif /* HAVE_TLS_EXTENSIONS */
-#if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
- if (ssl->mnCtx) {
- mynewt_ctx_clear(ssl->mnCtx);
- ssl->mnCtx = NULL;
- }
-#endif
-#ifdef HAVE_NETX
- if (ssl->nxCtx.nxPacket)
- nx_packet_release(ssl->nxCtx.nxPacket);
-#endif
-#ifdef KEEP_PEER_CERT
- FreeX509(&ssl->peerCert);
-#endif
-
-#ifdef HAVE_SESSION_TICKET
- if (ssl->session.isDynamic) {
- XFREE(ssl->session.ticket, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- ssl->session.ticket = ssl->session.staticTicket;
- ssl->session.isDynamic = 0;
- ssl->session.ticketLen = 0;
- }
-#endif
-#ifdef HAVE_EXT_CACHE
- wolfSSL_SESSION_free(ssl->extSession);
-#endif
-#ifdef HAVE_WRITE_DUP
- if (ssl->dupWrite) {
- FreeWriteDup(ssl);
- }
-#endif
-#ifdef OPENSSL_EXTRA
- if (ssl->param) {
- XFREE(ssl->param, ssl->heap, DYNAMIC_TYPE_OPENSSL);
- }
-#endif
-#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- while (ssl->certReqCtx != NULL) {
- CertReqCtx* curr = ssl->certReqCtx;
- ssl->certReqCtx = curr->next;
- XFREE(curr, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-#endif
-
-#ifdef WOLFSSL_STATIC_MEMORY
- /* check if using fixed io buffers and free them */
- if (ssl->heap != NULL) {
- #ifdef WOLFSSL_HEAP_TEST
- /* avoid dereferencing a test value */
- if (ssl->heap != (void*)WOLFSSL_HEAP_TEST) {
- #endif
- WOLFSSL_HEAP_HINT* ssl_hint = (WOLFSSL_HEAP_HINT*)ssl->heap;
- WOLFSSL_HEAP* ctx_heap;
- void* heap = ssl->ctx ? ssl->ctx->heap : ssl->heap;
-
- ctx_heap = ssl_hint->memory;
- if (wc_LockMutex(&(ctx_heap->memory_mutex)) != 0) {
- WOLFSSL_MSG("Bad memory_mutex lock");
- }
- ctx_heap->curIO--;
- if (FreeFixedIO(ctx_heap, &(ssl_hint->outBuf)) != 1) {
- WOLFSSL_MSG("Error freeing fixed output buffer");
- }
- if (FreeFixedIO(ctx_heap, &(ssl_hint->inBuf)) != 1) {
- WOLFSSL_MSG("Error freeing fixed output buffer");
- }
- if (ssl_hint->haFlag) { /* check if handshake count has been decreased*/
- ctx_heap->curHa--;
- }
- wc_UnLockMutex(&(ctx_heap->memory_mutex));
-
- /* check if tracking stats */
- if (ctx_heap->flag & WOLFMEM_TRACK_STATS) {
- XFREE(ssl_hint->stats, heap, DYNAMIC_TYPE_SSL);
- }
- XFREE(ssl->heap, heap, DYNAMIC_TYPE_SSL);
- #ifdef WOLFSSL_HEAP_TEST
- }
- #endif
- }
-#endif /* WOLFSSL_STATIC_MEMORY */
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- wolfSSL_sk_CIPHER_free(ssl->supportedCiphers);
- wolfSSL_sk_X509_free(ssl->peerCertChain);
-#endif
-}
-
-/* Free any handshake resources no longer needed */
-void FreeHandshakeResources(WOLFSSL* ssl)
-{
-
-#ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation && ssl->secure_renegotiation->enabled) {
- WOLFSSL_MSG("Secure Renegotiation needs to retain handshake resources");
- return;
- }
-#endif
-
- /* input buffer */
- if (ssl->buffers.inputBuffer.dynamicFlag)
- ShrinkInputBuffer(ssl, NO_FORCED_FREE);
-
-#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- if (!ssl->options.tls1_3)
-#endif
- {
- #ifndef OPENSSL_ALL
- /* free suites unless using compatibility layer */
- FreeSuites(ssl);
- #endif
- /* hsHashes */
- FreeHandshakeHashes(ssl);
- }
-
- /* RNG */
- if (ssl->options.tls1_1 == 0
-#ifndef WOLFSSL_AEAD_ONLY
- || ssl->specs.cipher_type == stream
-#endif
-#if defined(WOLFSSL_TLS13)
- #if !defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- || ssl->options.tls1_3
- #elif !defined(HAVE_SESSION_TICKET)
- || (ssl->options.tls1_3 && ssl->options.side == WOLFSSL_SERVER_END)
- #endif
-#endif
- ) {
- if (ssl->options.weOwnRng) {
- wc_FreeRng(ssl->rng);
- XFREE(ssl->rng, ssl->heap, DYNAMIC_TYPE_RNG);
- ssl->rng = NULL;
- ssl->options.weOwnRng = 0;
- }
- }
-
-#ifdef WOLFSSL_DTLS
- /* DTLS_POOL */
- if (ssl->options.dtls) {
- DtlsMsgPoolReset(ssl);
- DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
- ssl->dtls_rx_msg_list = NULL;
- ssl->dtls_rx_msg_list_sz = 0;
- }
-#endif
-
-#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH) && \
- defined(HAVE_SESSION_TICKET)
- if (!ssl->options.tls1_3)
-#endif
- /* arrays */
- if (ssl->options.saveArrays == 0)
- FreeArrays(ssl, 1);
-
-#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- if (!ssl->options.tls1_3 || ssl->options.side == WOLFSSL_CLIENT_END)
-#endif
- {
-#ifndef NO_RSA
- /* peerRsaKey */
- FreeKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey);
- ssl->peerRsaKeyPresent = 0;
-#endif
-#ifdef HAVE_ECC
- FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey);
- ssl->peerEccDsaKeyPresent = 0;
-#endif /* HAVE_ECC */
-#ifdef HAVE_ED25519
- FreeKey(ssl, DYNAMIC_TYPE_ED25519, (void**)&ssl->peerEd25519Key);
- ssl->peerEd25519KeyPresent = 0;
-#endif /* HAVE_ED25519 */
-#ifdef HAVE_ED448
- FreeKey(ssl, DYNAMIC_TYPE_ED448, (void**)&ssl->peerEd448Key);
- ssl->peerEd448KeyPresent = 0;
-#endif /* HAVE_ED448 */
- }
-
-#ifdef HAVE_ECC
- FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccKey);
- ssl->peerEccKeyPresent = 0;
-#endif
-#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
- {
- int dtype;
- #ifdef HAVE_ECC
- dtype = DYNAMIC_TYPE_ECC;
- #endif
- #ifdef HAVE_CURVE25519
- #ifdef HAVE_ECC
- if (ssl->peerX25519KeyPresent ||
- ssl->eccTempKeyPresent == DYNAMIC_TYPE_CURVE25519)
- #endif /* HAVE_ECC */
- {
- dtype = DYNAMIC_TYPE_CURVE25519;
- }
- #endif /* HAVE_CURVE25519 */
- #ifdef HAVE_CURVE448
- #ifdef HAVE_ECC
- if (ssl->peerX448KeyPresent ||
- ssl->eccTempKeyPresent == DYNAMIC_TYPE_CURVE448)
- #endif /* HAVE_ECC */
- {
- dtype = DYNAMIC_TYPE_CURVE448;
- }
- #endif /* HAVE_CURVE448 */
- FreeKey(ssl, dtype, (void**)&ssl->eccTempKey);
- ssl->eccTempKeyPresent = 0;
- }
-#endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-#ifdef HAVE_CURVE25519
- FreeKey(ssl, DYNAMIC_TYPE_CURVE25519, (void**)&ssl->peerX25519Key);
- ssl->peerX25519KeyPresent = 0;
-#endif
-#ifdef HAVE_CURVE448
- FreeKey(ssl, DYNAMIC_TYPE_CURVE448, (void**)&ssl->peerX448Key);
- ssl->peerX448KeyPresent = 0;
-#endif
-
-#ifndef NO_DH
- if (ssl->buffers.serverDH_Priv.buffer) {
- ForceZero(ssl->buffers.serverDH_Priv.buffer,
- ssl->buffers.serverDH_Priv.length);
- }
- XFREE(ssl->buffers.serverDH_Priv.buffer, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- ssl->buffers.serverDH_Priv.buffer = NULL;
- XFREE(ssl->buffers.serverDH_Pub.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ssl->buffers.serverDH_Pub.buffer = NULL;
- /* parameters (p,g) may be owned by ctx */
- if (ssl->buffers.weOwnDH) {
- XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ssl->buffers.serverDH_G.buffer = NULL;
- XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ssl->buffers.serverDH_P.buffer = NULL;
- }
-#endif /* !NO_DH */
-
-#ifndef NO_CERTS
- wolfSSL_UnloadCertsKeys(ssl);
-#endif
-#ifdef HAVE_PK_CALLBACKS
-#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- if (!ssl->options.tls1_3 || ssl->options.side == WOLFSSL_CLIENT_END)
-#endif
- {
- #ifdef HAVE_ECC
- XFREE(ssl->buffers.peerEccDsaKey.buffer, ssl->heap, DYNAMIC_TYPE_ECC);
- ssl->buffers.peerEccDsaKey.buffer = NULL;
- #endif /* HAVE_ECC */
- #ifndef NO_RSA
- XFREE(ssl->buffers.peerRsaKey.buffer, ssl->heap, DYNAMIC_TYPE_RSA);
- ssl->buffers.peerRsaKey.buffer = NULL;
- #endif /* NO_RSA */
- #ifdef HAVE_ED25519
- XFREE(ssl->buffers.peerEd25519Key.buffer, ssl->heap,
- DYNAMIC_TYPE_ED25519);
- ssl->buffers.peerEd25519Key.buffer = NULL;
- #endif
- #ifdef HAVE_ED448
- XFREE(ssl->buffers.peerEd448Key.buffer, ssl->heap, DYNAMIC_TYPE_ED448);
- ssl->buffers.peerEd448Key.buffer = NULL;
- #endif
- }
-#endif /* HAVE_PK_CALLBACKS */
-
-#ifdef HAVE_QSH
- QSH_FreeAll(ssl);
-#endif
-
-#ifdef HAVE_SESSION_TICKET
- if (ssl->session.isDynamic) {
- XFREE(ssl->session.ticket, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- ssl->session.ticket = ssl->session.staticTicket;
- ssl->session.isDynamic = 0;
- ssl->session.ticketLen = 0;
- }
-#endif
-
-#if defined(HAVE_TLS_EXTENSIONS) && !defined(HAVE_SNI) && \
- !defined(HAVE_ALPN) && !defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- /* Some extensions need to be kept for post-handshake querying. */
- TLSX_FreeAll(ssl->extensions, ssl->heap);
- ssl->extensions = NULL;
-#endif
-
-#ifdef WOLFSSL_STATIC_MEMORY
- /* when done with handshake decrement current handshake count */
- if (ssl->heap != NULL) {
- #ifdef WOLFSSL_HEAP_TEST
- /* avoid dereferencing a test value */
- if (ssl->heap != (void*)WOLFSSL_HEAP_TEST) {
- #endif
- WOLFSSL_HEAP_HINT* ssl_hint = (WOLFSSL_HEAP_HINT*)ssl->heap;
- WOLFSSL_HEAP* ctx_heap;
-
- ctx_heap = ssl_hint->memory;
- if (wc_LockMutex(&(ctx_heap->memory_mutex)) != 0) {
- WOLFSSL_MSG("Bad memory_mutex lock");
- }
- ctx_heap->curHa--;
- ssl_hint->haFlag = 0; /* set to zero since handshake has been dec */
- wc_UnLockMutex(&(ctx_heap->memory_mutex));
- #ifdef WOLFSSL_HEAP_TEST
- }
- #endif
- }
-#endif /* WOLFSSL_STATIC_MEMORY */
-}
-
-
-/* heap argument is the heap hint used when creating SSL */
-void FreeSSL(WOLFSSL* ssl, void* heap)
-{
- if (ssl->ctx) {
- FreeSSL_Ctx(ssl->ctx); /* will decrement and free underlying CTX if 0 */
- }
- SSL_ResourceFree(ssl);
- XFREE(ssl, heap, DYNAMIC_TYPE_SSL);
- (void)heap;
-}
-
-#if !defined(NO_OLD_TLS) || defined(WOLFSSL_DTLS) || \
- ((defined(HAVE_CHACHA) || defined(HAVE_AESCCM) || defined(HAVE_AESGCM)) \
- && defined(HAVE_AEAD))
-
-#if defined(WOLFSSL_DTLS) || !defined(WOLFSSL_NO_TLS12)
-static WC_INLINE void GetSEQIncrement(WOLFSSL* ssl, int verify, word32 seq[2])
-{
- if (verify) {
- seq[0] = ssl->keys.peer_sequence_number_hi;
- seq[1] = ssl->keys.peer_sequence_number_lo++;
- if (seq[1] > ssl->keys.peer_sequence_number_lo) {
- /* handle rollover */
- ssl->keys.peer_sequence_number_hi++;
- }
- }
- else {
- seq[0] = ssl->keys.sequence_number_hi;
- seq[1] = ssl->keys.sequence_number_lo++;
- if (seq[1] > ssl->keys.sequence_number_lo) {
- /* handle rollover */
- ssl->keys.sequence_number_hi++;
- }
- }
-}
-#endif /* WOLFSSL_DTLS || !WOLFSSL_NO_TLS12 */
-
-
-#ifdef WOLFSSL_DTLS
-static WC_INLINE void DtlsGetSEQ(WOLFSSL* ssl, int order, word32 seq[2])
-{
- if (order == PREV_ORDER) {
- /* Previous epoch case */
- if (ssl->options.haveMcast) {
- #ifdef WOLFSSL_MULTICAST
- seq[0] = (((word32)ssl->keys.dtls_epoch - 1) << 16) |
- (ssl->options.mcastID << 8) |
- (ssl->keys.dtls_prev_sequence_number_hi & 0xFF);
- #endif
- }
- else
- seq[0] = (((word32)ssl->keys.dtls_epoch - 1) << 16) |
- (ssl->keys.dtls_prev_sequence_number_hi & 0xFFFF);
- seq[1] = ssl->keys.dtls_prev_sequence_number_lo;
- }
- else if (order == PEER_ORDER) {
- if (ssl->options.haveMcast) {
- #ifdef WOLFSSL_MULTICAST
- seq[0] = ((word32)ssl->keys.curEpoch << 16) |
- (ssl->keys.curPeerId << 8) |
- (ssl->keys.curSeq_hi & 0xFF);
- #endif
- }
- else
- seq[0] = ((word32)ssl->keys.curEpoch << 16) |
- (ssl->keys.curSeq_hi & 0xFFFF);
- seq[1] = ssl->keys.curSeq_lo; /* explicit from peer */
- }
- else {
- if (ssl->options.haveMcast) {
- #ifdef WOLFSSL_MULTICAST
- seq[0] = ((word32)ssl->keys.dtls_epoch << 16) |
- (ssl->options.mcastID << 8) |
- (ssl->keys.dtls_sequence_number_hi & 0xFF);
- #endif
- }
- else
- seq[0] = ((word32)ssl->keys.dtls_epoch << 16) |
- (ssl->keys.dtls_sequence_number_hi & 0xFFFF);
- seq[1] = ssl->keys.dtls_sequence_number_lo;
- }
-}
-
-static WC_INLINE void DtlsSEQIncrement(WOLFSSL* ssl, int order)
-{
- word32 seq;
-
- if (order == PREV_ORDER) {
- seq = ssl->keys.dtls_prev_sequence_number_lo++;
- if (seq > ssl->keys.dtls_prev_sequence_number_lo) {
- /* handle rollover */
- ssl->keys.dtls_prev_sequence_number_hi++;
- }
- }
- else if (order == PEER_ORDER) {
- seq = ssl->keys.peer_sequence_number_lo++;
- if (seq > ssl->keys.peer_sequence_number_lo) {
- /* handle rollover */
- ssl->keys.peer_sequence_number_hi++;
- }
- }
- else {
- seq = ssl->keys.dtls_sequence_number_lo++;
- if (seq > ssl->keys.dtls_sequence_number_lo) {
- /* handle rollover */
- ssl->keys.dtls_sequence_number_hi++;
- }
- }
-}
-#endif /* WOLFSSL_DTLS */
-
-#if defined(WOLFSSL_DTLS) || !defined(WOLFSSL_NO_TLS12)
-static WC_INLINE void WriteSEQ(WOLFSSL* ssl, int verifyOrder, byte* out)
-{
- word32 seq[2] = {0, 0};
-
- if (!ssl->options.dtls) {
- GetSEQIncrement(ssl, verifyOrder, seq);
- }
- else {
-#ifdef WOLFSSL_DTLS
- DtlsGetSEQ(ssl, verifyOrder, seq);
-#endif
- }
-
- c32toa(seq[0], out);
- c32toa(seq[1], out + OPAQUE32_LEN);
-}
-#endif /* WOLFSSL_DTLS || !WOLFSSL_NO_TLS12 */
-#endif /* !NO_OLD_TLS || WOLFSSL_DTLS ||
- * ((HAVE_CHACHA || HAVE_AESCCM || HAVE_AESGCM) && HAVE_AEAD) */
-
-#ifdef WOLFSSL_DTLS
-
-/* functions for managing DTLS datagram reordering */
-
-/* Need to allocate space for the handshake message header. The hashing
- * routines assume the message pointer is still within the buffer that
- * has the headers, and will include those headers in the hash. The store
- * routines need to take that into account as well. New will allocate
- * extra space for the headers. */
-DtlsMsg* DtlsMsgNew(word32 sz, void* heap)
-{
- DtlsMsg* msg;
-
- (void)heap;
- msg = (DtlsMsg*)XMALLOC(sizeof(DtlsMsg), heap, DYNAMIC_TYPE_DTLS_MSG);
-
- if (msg != NULL) {
- XMEMSET(msg, 0, sizeof(DtlsMsg));
- msg->buf = (byte*)XMALLOC(sz + DTLS_HANDSHAKE_HEADER_SZ,
- heap, DYNAMIC_TYPE_DTLS_BUFFER);
- if (msg->buf != NULL) {
- msg->sz = sz;
- msg->type = no_shake;
- msg->msg = msg->buf + DTLS_HANDSHAKE_HEADER_SZ;
- }
- else {
- XFREE(msg, heap, DYNAMIC_TYPE_DTLS_MSG);
- msg = NULL;
- }
- }
-
- return msg;
-}
-
-void DtlsMsgDelete(DtlsMsg* item, void* heap)
-{
- (void)heap;
-
- if (item != NULL) {
- DtlsFrag* cur = item->fragList;
- while (cur != NULL) {
- DtlsFrag* next = cur->next;
- XFREE(cur, heap, DYNAMIC_TYPE_DTLS_FRAG);
- cur = next;
- }
- if (item->buf != NULL)
- XFREE(item->buf, heap, DYNAMIC_TYPE_DTLS_BUFFER);
- XFREE(item, heap, DYNAMIC_TYPE_DTLS_MSG);
- }
-}
-
-
-void DtlsMsgListDelete(DtlsMsg* head, void* heap)
-{
- DtlsMsg* next;
- while (head) {
- next = head->next;
- DtlsMsgDelete(head, heap);
- head = next;
- }
-}
-
-
-/* Create a DTLS Fragment from *begin - end, adjust new *begin and bytesLeft */
-static DtlsFrag* CreateFragment(word32* begin, word32 end, const byte* data,
- byte* buf, word32* bytesLeft, void* heap)
-{
- DtlsFrag* newFrag;
- word32 added = end - *begin + 1;
-
- (void)heap;
- newFrag = (DtlsFrag*)XMALLOC(sizeof(DtlsFrag), heap,
- DYNAMIC_TYPE_DTLS_FRAG);
- if (newFrag != NULL) {
- newFrag->next = NULL;
- newFrag->begin = *begin;
- newFrag->end = end;
-
- XMEMCPY(buf + *begin, data, added);
- *bytesLeft -= added;
- *begin = newFrag->end + 1;
- }
-
- return newFrag;
-}
-
-
-int DtlsMsgSet(DtlsMsg* msg, word32 seq, const byte* data, byte type,
- word32 fragOffset, word32 fragSz, void* heap)
-{
- if (msg != NULL && data != NULL && msg->fragSz <= msg->sz &&
- (fragOffset + fragSz) <= msg->sz) {
- DtlsFrag* cur = msg->fragList;
- DtlsFrag* prev = cur;
- DtlsFrag* newFrag;
- word32 bytesLeft = fragSz; /* could be overlapping fragment */
- word32 startOffset = fragOffset;
- word32 added;
-
- msg->seq = seq;
- msg->type = type;
-
- if (fragOffset == 0) {
- XMEMCPY(msg->buf, data - DTLS_HANDSHAKE_HEADER_SZ,
- DTLS_HANDSHAKE_HEADER_SZ);
- c32to24(msg->sz, msg->msg - DTLS_HANDSHAKE_FRAG_SZ);
- }
-
- /* if no message data, just return */
- if (fragSz == 0)
- return 0;
-
- /* if list is empty add full fragment to front */
- if (cur == NULL) {
- newFrag = CreateFragment(&fragOffset, fragOffset + fragSz - 1, data,
- msg->msg, &bytesLeft, heap);
- if (newFrag == NULL)
- return MEMORY_E;
-
- msg->fragSz = fragSz;
- msg->fragList = newFrag;
-
- return 0;
- }
-
- /* add to front if before current front, up to next->begin */
- if (fragOffset < cur->begin) {
- word32 end = fragOffset + fragSz - 1;
-
- if (end >= cur->begin)
- end = cur->begin - 1;
-
- added = end - fragOffset + 1;
- newFrag = CreateFragment(&fragOffset, end, data, msg->msg,
- &bytesLeft, heap);
- if (newFrag == NULL)
- return MEMORY_E;
-
- msg->fragSz += added;
-
- newFrag->next = cur;
- msg->fragList = newFrag;
- }
-
- /* while we have bytes left, try to find a gap to fill */
- while (bytesLeft > 0) {
- /* get previous packet in list */
- while (cur && (fragOffset >= cur->begin)) {
- prev = cur;
- cur = cur->next;
- }
-
- /* don't add duplicate data */
- if (prev->end >= fragOffset) {
- if ( (fragOffset + bytesLeft - 1) <= prev->end)
- return 0;
- fragOffset = prev->end + 1;
- bytesLeft = startOffset + fragSz - fragOffset;
- }
-
- if (cur == NULL)
- /* we're at the end */
- added = bytesLeft;
- else
- /* we're in between two frames */
- added = min(bytesLeft, cur->begin - fragOffset);
-
- /* data already there */
- if (added == 0)
- continue;
-
- newFrag = CreateFragment(&fragOffset, fragOffset + added - 1,
- data + fragOffset - startOffset,
- msg->msg, &bytesLeft, heap);
- if (newFrag == NULL)
- return MEMORY_E;
-
- msg->fragSz += added;
-
- newFrag->next = prev->next;
- prev->next = newFrag;
- }
- }
-
- return 0;
-}
-
-
-DtlsMsg* DtlsMsgFind(DtlsMsg* head, word32 seq)
-{
- while (head != NULL && head->seq != seq) {
- head = head->next;
- }
- return head;
-}
-
-
-void DtlsMsgStore(WOLFSSL* ssl, word32 seq, const byte* data,
- word32 dataSz, byte type, word32 fragOffset, word32 fragSz, void* heap)
-{
- /* See if seq exists in the list. If it isn't in the list, make
- * a new item of size dataSz, copy fragSz bytes from data to msg->msg
- * starting at offset fragOffset, and add fragSz to msg->fragSz. If
- * the seq is in the list and it isn't full, copy fragSz bytes from
- * data to msg->msg starting at offset fragOffset, and add fragSz to
- * msg->fragSz. Insertions take into account data already in the list
- * in case there are overlaps in the handshake message due to retransmit
- * messages. The new item should be inserted into the list in its
- * proper position.
- *
- * 1. Find seq in list, or where seq should go in list. If seq not in
- * list, create new item and insert into list. Either case, keep
- * pointer to item.
- * 2. Copy the data from the message to the stored message where it
- * belongs without overlaps.
- */
-
- DtlsMsg* head = ssl->dtls_rx_msg_list;
-
- if (head != NULL) {
- DtlsMsg* cur = DtlsMsgFind(head, seq);
- if (cur == NULL) {
- cur = DtlsMsgNew(dataSz, heap);
- if (cur != NULL) {
- if (DtlsMsgSet(cur, seq, data, type,
- fragOffset, fragSz, heap) < 0) {
- DtlsMsgDelete(cur, heap);
- }
- else {
- ssl->dtls_rx_msg_list_sz++;
- head = DtlsMsgInsert(head, cur);
- }
- }
- }
- else {
- /* If this fails, the data is just dropped. */
- DtlsMsgSet(cur, seq, data, type, fragOffset, fragSz, heap);
- }
- }
- else {
- head = DtlsMsgNew(dataSz, heap);
- if (DtlsMsgSet(head, seq, data, type, fragOffset, fragSz, heap) < 0) {
- DtlsMsgDelete(head, heap);
- head = NULL;
- }
- else {
- ssl->dtls_rx_msg_list_sz++;
- }
- }
-
- ssl->dtls_rx_msg_list = head;
-}
-
-
-/* DtlsMsgInsert() is an in-order insert. */
-DtlsMsg* DtlsMsgInsert(DtlsMsg* head, DtlsMsg* item)
-{
- if (head == NULL || item->seq < head->seq) {
- item->next = head;
- head = item;
- }
- else if (head->next == NULL) {
- head->next = item;
- }
- else {
- DtlsMsg* cur = head->next;
- DtlsMsg* prev = head;
- while (cur) {
- if (item->seq < cur->seq) {
- item->next = cur;
- prev->next = item;
- break;
- }
- prev = cur;
- cur = cur->next;
- }
- if (cur == NULL) {
- prev->next = item;
- }
- }
-
- return head;
-}
-
-
-/* DtlsMsgPoolSave() adds the message to the end of the stored transmit list. */
-int DtlsMsgPoolSave(WOLFSSL* ssl, const byte* data, word32 dataSz)
-{
- DtlsMsg* item;
- int ret = 0;
-
- WOLFSSL_ENTER("DtlsMsgPoolSave()");
-
- if (ssl->dtls_tx_msg_list_sz > DTLS_POOL_SZ) {
- WOLFSSL_ERROR(DTLS_POOL_SZ_E);
- return DTLS_POOL_SZ_E;
- }
-
- item = DtlsMsgNew(dataSz, ssl->heap);
-
- if (item != NULL) {
- DtlsMsg* cur = ssl->dtls_tx_msg_list;
-
- XMEMCPY(item->buf, data, dataSz);
- item->sz = dataSz;
- item->seq = ssl->keys.dtls_epoch;
-
- if (cur == NULL)
- ssl->dtls_tx_msg_list = item;
- else {
- while (cur->next)
- cur = cur->next;
- cur->next = item;
- }
- ssl->dtls_tx_msg_list_sz++;
- }
- else
- ret = MEMORY_E;
-
- WOLFSSL_LEAVE("DtlsMsgPoolSave()", ret);
- return ret;
-}
-
-
-/* DtlsMsgPoolTimeout() updates the timeout time. */
-int DtlsMsgPoolTimeout(WOLFSSL* ssl)
-{
- int result = -1;
- if (ssl->dtls_timeout < ssl->dtls_timeout_max) {
- ssl->dtls_timeout *= DTLS_TIMEOUT_MULTIPLIER;
- result = 0;
- }
- WOLFSSL_LEAVE("DtlsMsgPoolTimeout()", result);
- return result;
-}
-
-
-/* DtlsMsgPoolReset() deletes the stored transmit list and resets the timeout
- * value. */
-void DtlsMsgPoolReset(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("DtlsMsgPoolReset()");
- if (ssl->dtls_tx_msg_list) {
- DtlsMsgListDelete(ssl->dtls_tx_msg_list, ssl->heap);
- ssl->dtls_tx_msg_list = NULL;
- ssl->dtls_tx_msg = NULL;
- ssl->dtls_tx_msg_list_sz = 0;
- ssl->dtls_timeout = ssl->dtls_timeout_init;
- }
-}
-
-
-int VerifyForDtlsMsgPoolSend(WOLFSSL* ssl, byte type, word32 fragOffset)
-{
- /**
- * only the first message from previous flight should be valid
- * to be used for triggering retransmission of whole DtlsMsgPool.
- * change cipher suite type is not verified here
- */
- return ((fragOffset == 0) &&
- (((ssl->options.side == WOLFSSL_SERVER_END) &&
- ((type == client_hello) ||
- ((ssl->options.verifyPeer) && (type == certificate)) ||
- ((!ssl->options.verifyPeer) && (type == client_key_exchange)))) ||
- ((ssl->options.side == WOLFSSL_CLIENT_END) &&
- (type == server_hello))));
-}
-
-
-/* DtlsMsgPoolSend() will send the stored transmit list. The stored list is
- * updated with new sequence numbers, and will be re-encrypted if needed. */
-int DtlsMsgPoolSend(WOLFSSL* ssl, int sendOnlyFirstPacket)
-{
- int ret = 0;
- DtlsMsg* pool;
-
- WOLFSSL_ENTER("DtlsMsgPoolSend()");
-
- pool = ssl->dtls_tx_msg == NULL ? ssl->dtls_tx_msg_list : ssl->dtls_tx_msg;
-
- if (pool != NULL) {
- if ((ssl->options.side == WOLFSSL_SERVER_END &&
- !(ssl->options.acceptState == SERVER_HELLO_DONE ||
- ssl->options.acceptState == ACCEPT_FINISHED_DONE ||
- ssl->options.acceptState == ACCEPT_THIRD_REPLY_DONE)) ||
- (ssl->options.side == WOLFSSL_CLIENT_END &&
- !(ssl->options.connectState == CLIENT_HELLO_SENT ||
- ssl->options.connectState == HELLO_AGAIN_REPLY ||
- ssl->options.connectState == FINISHED_DONE ||
- ssl->options.connectState == SECOND_REPLY_DONE))) {
-
- WOLFSSL_ERROR(DTLS_RETX_OVER_TX);
- ssl->error = DTLS_RETX_OVER_TX;
- return WOLFSSL_FATAL_ERROR;
- }
-
- while (pool != NULL) {
- if (pool->seq == 0) {
- DtlsRecordLayerHeader* dtls;
- int epochOrder;
-
- dtls = (DtlsRecordLayerHeader*)pool->buf;
- /* If the stored record's epoch is 0, and the currently set
- * epoch is 0, use the "current order" sequence number.
- * If the stored record's epoch is 0 and the currently set
- * epoch is not 0, the stored record is considered a "previous
- * order" sequence number. */
- epochOrder = (ssl->keys.dtls_epoch == 0) ?
- CUR_ORDER : PREV_ORDER;
-
- WriteSEQ(ssl, epochOrder, dtls->sequence_number);
- DtlsSEQIncrement(ssl, epochOrder);
- if ((ret = CheckAvailableSize(ssl, pool->sz)) != 0) {
- WOLFSSL_ERROR(ret);
- return ret;
- }
-
- XMEMCPY(ssl->buffers.outputBuffer.buffer,
- pool->buf, pool->sz);
- ssl->buffers.outputBuffer.idx = 0;
- ssl->buffers.outputBuffer.length = pool->sz;
- }
- else if (pool->seq == ssl->keys.dtls_epoch) {
- byte* input;
- byte* output;
- int inputSz, sendSz;
-
- input = pool->buf;
- inputSz = pool->sz;
- sendSz = inputSz + MAX_MSG_EXTRA;
-
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0) {
- WOLFSSL_ERROR(ret);
- return ret;
- }
-
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
- sendSz = BuildMessage(ssl, output, sendSz, input, inputSz,
- handshake, 0, 0, 0);
- if (sendSz < 0) {
- WOLFSSL_ERROR(BUILD_MSG_ERROR);
- return BUILD_MSG_ERROR;
- }
-
- ssl->buffers.outputBuffer.length += sendSz;
- }
-
- ret = SendBuffered(ssl);
- if (ret < 0) {
- WOLFSSL_ERROR(ret);
- return ret;
- }
-
- /**
- * on server side, retransmission is being triggered only by sending
- * first message of given flight, in order to trigger client
- * to retransmit its whole flight. Sending the whole previous flight
- * could lead to retransmission of previous client flight for each
- * server message from previous flight. Therefore one message should
- * be enough to do the trick.
- */
- if (sendOnlyFirstPacket &&
- ssl->options.side == WOLFSSL_SERVER_END) {
-
- pool = NULL;
- }
- else
- pool = pool->next;
- ssl->dtls_tx_msg = pool;
- }
- }
-
- WOLFSSL_LEAVE("DtlsMsgPoolSend()", ret);
- return ret;
-}
-
-#endif /* WOLFSSL_DTLS */
-
-#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
-
-ProtocolVersion MakeSSLv3(void)
-{
- ProtocolVersion pv;
- pv.major = SSLv3_MAJOR;
- pv.minor = SSLv3_MINOR;
-
- return pv;
-}
-
-#endif /* WOLFSSL_ALLOW_SSLV3 && !NO_OLD_TLS */
-
-
-#ifdef WOLFSSL_DTLS
-
-ProtocolVersion MakeDTLSv1(void)
-{
- ProtocolVersion pv;
- pv.major = DTLS_MAJOR;
- pv.minor = DTLS_MINOR;
-
- return pv;
-}
-
-#ifndef WOLFSSL_NO_TLS12
-
-ProtocolVersion MakeDTLSv1_2(void)
-{
- ProtocolVersion pv;
- pv.major = DTLS_MAJOR;
- pv.minor = DTLSv1_2_MINOR;
-
- return pv;
-}
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#endif /* WOLFSSL_DTLS */
-
-
-#ifndef NO_ASN_TIME
-#if defined(USER_TICKS)
-#if 0
- word32 LowResTimer(void)
- {
- /*
- write your own clock tick function if don't want time(0)
- needs second accuracy but doesn't have to correlated to EPOCH
- */
- }
-#endif
-
-#elif defined(TIME_OVERRIDES)
-
- /* use same asn time overrides unless user wants tick override above */
-
- #ifndef HAVE_TIME_T_TYPE
- typedef long time_t;
- #endif
- extern time_t XTIME(time_t * timer);
-
- word32 LowResTimer(void)
- {
- return (word32) XTIME(0);
- }
-
-#elif defined(USE_WINDOWS_API)
-
- word32 LowResTimer(void)
- {
- static int init = 0;
- static LARGE_INTEGER freq;
- LARGE_INTEGER count;
-
- if (!init) {
- QueryPerformanceFrequency(&freq);
- init = 1;
- }
-
- QueryPerformanceCounter(&count);
-
- return (word32)(count.QuadPart / freq.QuadPart);
- }
-
-#elif defined(HAVE_RTP_SYS)
-
- #include "rtptime.h"
-
- word32 LowResTimer(void)
- {
- return (word32)rtp_get_system_sec();
- }
-
-#elif defined(WOLFSSL_DEOS)
-
- word32 LowResTimer(void)
- {
- const uint32_t systemTickTimeInHz = 1000000 / systemTickInMicroseconds();
- uint32_t *systemTickPtr = systemTickPointer();
-
- return (word32) *systemTickPtr/systemTickTimeInHz;
- }
-
-#elif defined(MICRIUM)
-
- word32 LowResTimer(void)
- {
- OS_TICK ticks = 0;
- OS_ERR err;
-
- ticks = OSTimeGet(&err);
-
- return (word32) (ticks / OSCfg_TickRate_Hz);
- }
-
-
-#elif defined(MICROCHIP_TCPIP_V5)
-
- word32 LowResTimer(void)
- {
- return (word32) (TickGet() / TICKS_PER_SECOND);
- }
-
-
-#elif defined(MICROCHIP_TCPIP)
-
- #if defined(MICROCHIP_MPLAB_HARMONY)
-
- #include
-
- word32 LowResTimer(void)
- {
- return (word32) (SYS_TMR_TickCountGet() /
- SYS_TMR_TickCounterFrequencyGet());
- }
-
- #else
-
- word32 LowResTimer(void)
- {
- return (word32) (SYS_TICK_Get() / SYS_TICK_TicksPerSecondGet());
- }
-
- #endif
-
-#elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
-
- word32 LowResTimer(void)
- {
- TIME_STRUCT mqxTime;
-
- _time_get_elapsed(&mqxTime);
-
- return (word32) mqxTime.SECONDS;
- }
-#elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS)
-
- #include "include/task.h"
-
- unsigned int LowResTimer(void)
- {
- return (unsigned int)(((float)xTaskGetTickCount())/configTICK_RATE_HZ);
- }
-
-#elif defined(FREERTOS)
-
- #include "task.h"
-
- unsigned int LowResTimer(void)
- {
- return (unsigned int)(((float)xTaskGetTickCount())/configTICK_RATE_HZ);
- }
-
-#elif defined(FREESCALE_KSDK_BM)
-
- #include "lwip/sys.h" /* lwIP */
- word32 LowResTimer(void)
- {
- return sys_now()/1000;
- }
-
-#elif defined(WOLFSSL_TIRTOS)
-
- word32 LowResTimer(void)
- {
- return (word32) Seconds_get();
- }
-#elif defined(WOLFSSL_XILINX)
- #include "xrtcpsu.h"
-
- word32 LowResTimer(void)
- {
- XRtcPsu_Config* con;
- XRtcPsu rtc;
-
- con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID);
- if (con != NULL) {
- if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr)
- == XST_SUCCESS) {
- return (word32)XRtcPsu_GetCurrentTime(&rtc);
- }
- else {
- WOLFSSL_MSG("Unable to initialize RTC");
- }
- }
-
- return 0;
- }
-
-#elif defined(WOLFSSL_UTASKER)
-
- word32 LowResTimer(void)
- {
- return (word32)(uTaskerSystemTick / TICK_RESOLUTION);
- }
-
-#elif defined(WOLFSSL_NUCLEUS_1_2)
-
- #define NU_TICKS_PER_SECOND 100
-
- word32 LowResTimer(void)
- {
- /* returns number of 10ms ticks, so 100 ticks/sec */
- return NU_Retrieve_Clock() / NU_TICKS_PER_SECOND;
- }
-#elif defined(WOLFSSL_APACHE_MYNEWT)
-
- #include "os/os_time.h"
- word32 LowResTimer(void)
- {
- word32 now;
- struct os_timeval tv;
- os_gettimeofday(&tv, NULL);
- now = (word32)tv.tv_sec;
- return now;
- }
-
-#elif defined(WOLFSSL_ZEPHYR)
-
- word32 LowResTimer(void)
- {
- return k_uptime_get() / 1000;
- }
-
-#else
- /* Posix style time */
- #if !defined(USER_TIME) && !defined(USE_WOLF_TM)
- #include
- #endif
-
- word32 LowResTimer(void)
- {
- return (word32)XTIME(0);
- }
-#endif
-#endif /* !NO_ASN_TIME */
-#if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
- ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
- (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
-/* Store the message for use with CertificateVerify using EdDSA.
- *
- * ssl SSL/TLS object.
- * data Message to store.
- * sz Size of message to store.
- * returns MEMORY_E if not able to reallocate, otherwise 0.
- */
-static int EdDSA_Update(WOLFSSL* ssl, const byte* data, int sz)
-{
- int ret = 0;
- byte* msgs;
-
- if (ssl->options.cacheMessages) {
- msgs = (byte*)XREALLOC(ssl->hsHashes->messages,
- ssl->hsHashes->length + sz,
- ssl->heap, DYNAMIC_TYPE_HASHES);
- if (msgs == NULL)
- ret = MEMORY_E;
- if (ret == 0) {
- ssl->hsHashes->messages = msgs;
- XMEMCPY(msgs + ssl->hsHashes->length, data, sz);
- ssl->hsHashes->prevLen = ssl->hsHashes->length;
- ssl->hsHashes->length += sz;
- }
- }
-
- return ret;
-}
-#endif /* (HAVE_ED25519 || HAVE_ED448) && !WOLFSSL_NO_CLIENT_AUTH */
-
-#ifndef NO_CERTS
-int HashOutputRaw(WOLFSSL* ssl, const byte* output, int sz)
-{
- int ret = 0;
-
- (void)output;
- (void)sz;
-
- if (ssl->hsHashes == NULL)
- return BAD_FUNC_ARG;
-
-#ifdef HAVE_FUZZER
- if (ssl->fuzzerCb)
- ssl->fuzzerCb(ssl, output, sz, FUZZ_HASH, ssl->fuzzerCtx);
-#endif
-#ifndef NO_OLD_TLS
- #ifndef NO_SHA
- wc_ShaUpdate(&ssl->hsHashes->hashSha, output, sz);
- #endif
- #ifndef NO_MD5
- wc_Md5Update(&ssl->hsHashes->hashMd5, output, sz);
- #endif
-#endif /* NO_OLD_TLS */
-
- if (IsAtLeastTLSv1_2(ssl)) {
- #ifndef NO_SHA256
- ret = wc_Sha256Update(&ssl->hsHashes->hashSha256, output, sz);
- if (ret != 0)
- return ret;
- #endif
- #ifdef WOLFSSL_SHA384
- ret = wc_Sha384Update(&ssl->hsHashes->hashSha384, output, sz);
- if (ret != 0)
- return ret;
- #endif
- #ifdef WOLFSSL_SHA512
- ret = wc_Sha512Update(&ssl->hsHashes->hashSha512, output, sz);
- if (ret != 0)
- return ret;
- #endif
- #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
- ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
- (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
- ret = EdDSA_Update(ssl, output, sz);
- if (ret != 0)
- return ret;
- #endif
- }
-
- return ret;
-}
-#endif /* NO_CERTS */
-
-
-/* add output to md5 and sha handshake hashes, exclude record header */
-int HashOutput(WOLFSSL* ssl, const byte* output, int sz, int ivSz)
-{
- int ret = 0;
- const byte* adj;
-
- if (ssl->hsHashes == NULL)
- return BAD_FUNC_ARG;
-
- adj = output + RECORD_HEADER_SZ + ivSz;
- sz -= RECORD_HEADER_SZ;
-
-#ifdef HAVE_FUZZER
- if (ssl->fuzzerCb)
- ssl->fuzzerCb(ssl, output, sz, FUZZ_HASH, ssl->fuzzerCtx);
-#endif
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- adj += DTLS_RECORD_EXTRA;
- sz -= DTLS_RECORD_EXTRA;
- }
-#endif
-#ifndef NO_OLD_TLS
- #ifndef NO_SHA
- wc_ShaUpdate(&ssl->hsHashes->hashSha, adj, sz);
- #endif
- #ifndef NO_MD5
- wc_Md5Update(&ssl->hsHashes->hashMd5, adj, sz);
- #endif
-#endif
-
- if (IsAtLeastTLSv1_2(ssl)) {
- #ifndef NO_SHA256
- ret = wc_Sha256Update(&ssl->hsHashes->hashSha256, adj, sz);
- if (ret != 0)
- return ret;
- #endif
- #ifdef WOLFSSL_SHA384
- ret = wc_Sha384Update(&ssl->hsHashes->hashSha384, adj, sz);
- if (ret != 0)
- return ret;
- #endif
- #ifdef WOLFSSL_SHA512
- ret = wc_Sha512Update(&ssl->hsHashes->hashSha512, adj, sz);
- if (ret != 0)
- return ret;
- #endif
- #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
- ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
- (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
- ret = EdDSA_Update(ssl, adj, sz);
- if (ret != 0)
- return ret;
- #endif
- }
-
- return ret;
-}
-
-
-/* add input to md5 and sha handshake hashes, include handshake header */
-int HashInput(WOLFSSL* ssl, const byte* input, int sz)
-{
- int ret = 0;
- const byte* adj;
-
- adj = input - HANDSHAKE_HEADER_SZ;
- sz += HANDSHAKE_HEADER_SZ;
-
- (void)adj;
-
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- adj -= DTLS_HANDSHAKE_EXTRA;
- sz += DTLS_HANDSHAKE_EXTRA;
- }
-#endif
-
- if (ssl->hsHashes == NULL) {
- return BAD_FUNC_ARG;
- }
-
-#ifndef NO_OLD_TLS
- #ifndef NO_SHA
- wc_ShaUpdate(&ssl->hsHashes->hashSha, adj, sz);
- #endif
- #ifndef NO_MD5
- wc_Md5Update(&ssl->hsHashes->hashMd5, adj, sz);
- #endif
-#endif
-
- if (IsAtLeastTLSv1_2(ssl)) {
- #ifndef NO_SHA256
- ret = wc_Sha256Update(&ssl->hsHashes->hashSha256, adj, sz);
- if (ret != 0)
- return ret;
- #endif
- #ifdef WOLFSSL_SHA384
- ret = wc_Sha384Update(&ssl->hsHashes->hashSha384, adj, sz);
- if (ret != 0)
- return ret;
- #endif
- #ifdef WOLFSSL_SHA512
- ret = wc_Sha512Update(&ssl->hsHashes->hashSha512, adj, sz);
- if (ret != 0)
- return ret;
- #endif
- #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
- ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
- (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
- ret = EdDSA_Update(ssl, adj, sz);
- if (ret != 0)
- return ret;
- #endif
- }
-
- return ret;
-}
-
-
-/* add record layer header for message */
-static void AddRecordHeader(byte* output, word32 length, byte type, WOLFSSL* ssl)
-{
- RecordLayerHeader* rl;
-
- /* record layer header */
- rl = (RecordLayerHeader*)output;
- if (rl == NULL) {
- return;
- }
- rl->type = type;
- rl->pvMajor = ssl->version.major; /* type and version same in each */
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version)) {
-#ifdef WOLFSSL_TLS13_DRAFT_18
- rl->pvMinor = TLSv1_MINOR;
-#else
- rl->pvMinor = TLSv1_2_MINOR;
-#endif
- }
- else
-#endif
- rl->pvMinor = ssl->version.minor;
-
-#ifdef WOLFSSL_ALTERNATIVE_DOWNGRADE
- if (ssl->options.side == WOLFSSL_CLIENT_END
- && ssl->options.connectState == CONNECT_BEGIN
- && !ssl->options.resuming) {
- rl->pvMinor = ssl->options.downgrade ? ssl->options.minDowngrade
- : ssl->version.minor;
- }
-#endif
-
- if (!ssl->options.dtls) {
- c16toa((word16)length, rl->length);
- }
- else {
-#ifdef WOLFSSL_DTLS
- DtlsRecordLayerHeader* dtls;
-
- /* dtls record layer header extensions */
- dtls = (DtlsRecordLayerHeader*)output;
- WriteSEQ(ssl, 0, dtls->sequence_number);
- c16toa((word16)length, dtls->length);
-#endif
- }
-}
-
-
-#if !defined(WOLFSSL_NO_TLS12) || (defined(HAVE_SESSION_TICKET) && \
- !defined(NO_WOLFSSL_SERVER))
-/* add handshake header for message */
-static void AddHandShakeHeader(byte* output, word32 length,
- word32 fragOffset, word32 fragLength,
- byte type, WOLFSSL* ssl)
-{
- HandShakeHeader* hs;
- (void)fragOffset;
- (void)fragLength;
- (void)ssl;
-
- /* handshake header */
- hs = (HandShakeHeader*)output;
- if (hs == NULL)
- return;
-
- hs->type = type;
- c32to24(length, hs->length); /* type and length same for each */
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- DtlsHandShakeHeader* dtls;
-
- /* dtls handshake header extensions */
- dtls = (DtlsHandShakeHeader*)output;
- c16toa(ssl->keys.dtls_handshake_number++, dtls->message_seq);
- c32to24(fragOffset, dtls->fragment_offset);
- c32to24(fragLength, dtls->fragment_length);
- }
-#endif
-}
-
-/* add both headers for handshake message */
-static void AddHeaders(byte* output, word32 length, byte type, WOLFSSL* ssl)
-{
- word32 lengthAdj = HANDSHAKE_HEADER_SZ;
- word32 outputAdj = RECORD_HEADER_SZ;
-
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- lengthAdj += DTLS_HANDSHAKE_EXTRA;
- outputAdj += DTLS_RECORD_EXTRA;
- }
-#endif
-
- AddRecordHeader(output, length + lengthAdj, handshake, ssl);
- AddHandShakeHeader(output + outputAdj, length, 0, length, type, ssl);
-}
-#endif /* !WOLFSSL_NO_TLS12 || (HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER) */
-
-
-#ifndef WOLFSSL_NO_TLS12
-#if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_SERVER) || \
- !defined(WOLFSSL_NO_CLIENT_AUTH))
-static void AddFragHeaders(byte* output, word32 fragSz, word32 fragOffset,
- word32 length, byte type, WOLFSSL* ssl)
-{
- word32 lengthAdj = HANDSHAKE_HEADER_SZ;
- word32 outputAdj = RECORD_HEADER_SZ;
- (void)fragSz;
-
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- lengthAdj += DTLS_HANDSHAKE_EXTRA;
- outputAdj += DTLS_RECORD_EXTRA;
- }
-#endif
-
- AddRecordHeader(output, fragSz + lengthAdj, handshake, ssl);
- AddHandShakeHeader(output + outputAdj, length, fragOffset, fragSz, type, ssl);
-}
-#endif /* NO_CERTS */
-#endif /* !WOLFSSL_NO_TLS12 */
-
-
-/* return bytes received, -1 on error */
-static int wolfSSLReceive(WOLFSSL* ssl, byte* buf, word32 sz)
-{
- int recvd;
-
- if (ssl->CBIORecv == NULL) {
- WOLFSSL_MSG("Your IO Recv callback is null, please set");
- return -1;
- }
-
-retry:
- recvd = ssl->CBIORecv(ssl, (char *)buf, (int)sz, ssl->IOCB_ReadCtx);
- if (recvd < 0) {
- switch (recvd) {
- case WOLFSSL_CBIO_ERR_GENERAL: /* general/unknown error */
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)
- if (ssl->biord) {
- /* If retry and read flags are set, return WANT_READ */
- if ((ssl->biord->flags & WOLFSSL_BIO_FLAG_READ) &&
- (ssl->biord->flags & WOLFSSL_BIO_FLAG_RETRY)) {
- return WANT_READ;
- }
- }
- #endif
- return -1;
-
- case WOLFSSL_CBIO_ERR_WANT_READ: /* want read, would block */
- return WANT_READ;
-
- case WOLFSSL_CBIO_ERR_CONN_RST: /* connection reset */
- #ifdef USE_WINDOWS_API
- if (ssl->options.dtls) {
- goto retry;
- }
- #endif
- ssl->options.connReset = 1;
- return -1;
-
- case WOLFSSL_CBIO_ERR_ISR: /* interrupt */
- /* see if we got our timeout */
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->toInfoOn) {
- struct itimerval timeout;
- getitimer(ITIMER_REAL, &timeout);
- if (timeout.it_value.tv_sec == 0 &&
- timeout.it_value.tv_usec == 0) {
- XSTRNCPY(ssl->timeoutInfo.timeoutName,
- "recv() timeout", MAX_TIMEOUT_NAME_SZ);
- ssl->timeoutInfo.timeoutName[
- MAX_TIMEOUT_NAME_SZ] = '\0';
-
- WOLFSSL_MSG("Got our timeout");
- return WANT_READ;
- }
- }
- #endif
- goto retry;
-
- case WOLFSSL_CBIO_ERR_CONN_CLOSE: /* peer closed connection */
- ssl->options.isClosed = 1;
- return -1;
-
- case WOLFSSL_CBIO_ERR_TIMEOUT:
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl) &&
- !ssl->options.handShakeDone &&
- DtlsMsgPoolTimeout(ssl) == 0 &&
- DtlsMsgPoolSend(ssl, 0) == 0) {
-
- /* retry read for DTLS during handshake only */
- goto retry;
- }
- #endif
- return -1;
-
- default:
- WOLFSSL_MSG("Unexpected recv return code");
- return recvd;
- }
- }
-
- return recvd;
-}
-
-
-/* Switch dynamic output buffer back to static, buffer is assumed clear */
-void ShrinkOutputBuffer(WOLFSSL* ssl)
-{
- WOLFSSL_MSG("Shrinking output buffer\n");
- XFREE(ssl->buffers.outputBuffer.buffer - ssl->buffers.outputBuffer.offset,
- ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);
- ssl->buffers.outputBuffer.buffer = ssl->buffers.outputBuffer.staticBuffer;
- ssl->buffers.outputBuffer.bufferSize = STATIC_BUFFER_LEN;
- ssl->buffers.outputBuffer.dynamicFlag = 0;
- ssl->buffers.outputBuffer.offset = 0;
-}
-
-
-/* Switch dynamic input buffer back to static, keep any remaining input */
-/* forced free means cleaning up */
-void ShrinkInputBuffer(WOLFSSL* ssl, int forcedFree)
-{
- int usedLength = ssl->buffers.inputBuffer.length -
- ssl->buffers.inputBuffer.idx;
- if (!forcedFree && usedLength > STATIC_BUFFER_LEN)
- return;
-
- WOLFSSL_MSG("Shrinking input buffer\n");
-
- if (!forcedFree && usedLength > 0)
- XMEMCPY(ssl->buffers.inputBuffer.staticBuffer,
- ssl->buffers.inputBuffer.buffer + ssl->buffers.inputBuffer.idx,
- usedLength);
-
- XFREE(ssl->buffers.inputBuffer.buffer - ssl->buffers.inputBuffer.offset,
- ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- ssl->buffers.inputBuffer.buffer = ssl->buffers.inputBuffer.staticBuffer;
- ssl->buffers.inputBuffer.bufferSize = STATIC_BUFFER_LEN;
- ssl->buffers.inputBuffer.dynamicFlag = 0;
- ssl->buffers.inputBuffer.offset = 0;
- ssl->buffers.inputBuffer.idx = 0;
- ssl->buffers.inputBuffer.length = usedLength;
-}
-
-int SendBuffered(WOLFSSL* ssl)
-{
- if (ssl->CBIOSend == NULL) {
- WOLFSSL_MSG("Your IO Send callback is null, please set");
- return SOCKET_ERROR_E;
- }
-
-#ifdef WOLFSSL_DEBUG_TLS
- if (ssl->buffers.outputBuffer.idx == 0) {
- WOLFSSL_MSG("Data to send");
- WOLFSSL_BUFFER(ssl->buffers.outputBuffer.buffer,
- ssl->buffers.outputBuffer.length);
- }
-#endif
-
- while (ssl->buffers.outputBuffer.length > 0) {
- int sent = ssl->CBIOSend(ssl,
- (char*)ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.idx,
- (int)ssl->buffers.outputBuffer.length,
- ssl->IOCB_WriteCtx);
- if (sent < 0) {
- switch (sent) {
-
- case WOLFSSL_CBIO_ERR_WANT_WRITE: /* would block */
- return WANT_WRITE;
-
- case WOLFSSL_CBIO_ERR_CONN_RST: /* connection reset */
- ssl->options.connReset = 1;
- break;
-
- case WOLFSSL_CBIO_ERR_ISR: /* interrupt */
- /* see if we got our timeout */
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->toInfoOn) {
- struct itimerval timeout;
- getitimer(ITIMER_REAL, &timeout);
- if (timeout.it_value.tv_sec == 0 &&
- timeout.it_value.tv_usec == 0) {
- XSTRNCPY(ssl->timeoutInfo.timeoutName,
- "send() timeout", MAX_TIMEOUT_NAME_SZ);
- ssl->timeoutInfo.timeoutName[
- MAX_TIMEOUT_NAME_SZ] = '\0';
-
- WOLFSSL_MSG("Got our timeout");
- return WANT_WRITE;
- }
- }
- #endif
- continue;
-
- case WOLFSSL_CBIO_ERR_CONN_CLOSE: /* epipe / conn closed */
- ssl->options.connReset = 1; /* treat same as reset */
- break;
-
- default:
- return SOCKET_ERROR_E;
- }
-
- return SOCKET_ERROR_E;
- }
-
- if (sent > (int)ssl->buffers.outputBuffer.length) {
- WOLFSSL_MSG("SendBuffered() out of bounds read");
- return SEND_OOB_READ_E;
- }
-
- ssl->buffers.outputBuffer.idx += sent;
- ssl->buffers.outputBuffer.length -= sent;
- }
-
- ssl->buffers.outputBuffer.idx = 0;
-
- if (ssl->buffers.outputBuffer.dynamicFlag)
- ShrinkOutputBuffer(ssl);
-
- return 0;
-}
-
-
-/* Grow the output buffer */
-static WC_INLINE int GrowOutputBuffer(WOLFSSL* ssl, int size)
-{
- byte* tmp;
-#if WOLFSSL_GENERAL_ALIGNMENT > 0
- byte hdrSz = ssl->options.dtls ? DTLS_RECORD_HEADER_SZ :
- RECORD_HEADER_SZ;
- byte align = WOLFSSL_GENERAL_ALIGNMENT;
-#else
- const byte align = WOLFSSL_GENERAL_ALIGNMENT;
-#endif
-
-#if WOLFSSL_GENERAL_ALIGNMENT > 0
- /* the encrypted data will be offset from the front of the buffer by
- the header, if the user wants encrypted alignment they need
- to define their alignment requirement */
-
- if (align) {
- while (align < hdrSz)
- align *= 2;
- }
-#endif
-
- tmp = (byte*)XMALLOC(size + ssl->buffers.outputBuffer.length + align,
- ssl->heap, DYNAMIC_TYPE_OUT_BUFFER);
- WOLFSSL_MSG("growing output buffer\n");
-
- if (tmp == NULL)
- return MEMORY_E;
-
-#if WOLFSSL_GENERAL_ALIGNMENT > 0
- if (align)
- tmp += align - hdrSz;
-#endif
-
-#ifdef WOLFSSL_STATIC_MEMORY
- /* can be from IO memory pool which does not need copy if same buffer */
- if (ssl->buffers.outputBuffer.length &&
- tmp == ssl->buffers.outputBuffer.buffer) {
- ssl->buffers.outputBuffer.bufferSize =
- size + ssl->buffers.outputBuffer.length;
- return 0;
- }
-#endif
-
- if (ssl->buffers.outputBuffer.length)
- XMEMCPY(tmp, ssl->buffers.outputBuffer.buffer,
- ssl->buffers.outputBuffer.length);
-
- if (ssl->buffers.outputBuffer.dynamicFlag)
- XFREE(ssl->buffers.outputBuffer.buffer -
- ssl->buffers.outputBuffer.offset, ssl->heap,
- DYNAMIC_TYPE_OUT_BUFFER);
- ssl->buffers.outputBuffer.dynamicFlag = 1;
-
-#if WOLFSSL_GENERAL_ALIGNMENT > 0
- if (align)
- ssl->buffers.outputBuffer.offset = align - hdrSz;
- else
-#endif
- ssl->buffers.outputBuffer.offset = 0;
-
- ssl->buffers.outputBuffer.buffer = tmp;
- ssl->buffers.outputBuffer.bufferSize = size +
- ssl->buffers.outputBuffer.length;
- return 0;
-}
-
-
-/* Grow the input buffer, should only be to read cert or big app data */
-int GrowInputBuffer(WOLFSSL* ssl, int size, int usedLength)
-{
- byte* tmp;
-#if defined(WOLFSSL_DTLS) || WOLFSSL_GENERAL_ALIGNMENT > 0
- byte align = ssl->options.dtls ? WOLFSSL_GENERAL_ALIGNMENT : 0;
- byte hdrSz = DTLS_RECORD_HEADER_SZ;
-#else
- const byte align = WOLFSSL_GENERAL_ALIGNMENT;
-#endif
-
-#if defined(WOLFSSL_DTLS) || WOLFSSL_GENERAL_ALIGNMENT > 0
- /* the encrypted data will be offset from the front of the buffer by
- the dtls record header, if the user wants encrypted alignment they need
- to define their alignment requirement. in tls we read record header
- to get size of record and put actual data back at front, so don't need */
-
- if (align) {
- while (align < hdrSz)
- align *= 2;
- }
-#endif
-
- if (usedLength < 0 || size < 0) {
- WOLFSSL_MSG("GrowInputBuffer() called with negative number");
- return BAD_FUNC_ARG;
- }
-
- tmp = (byte*)XMALLOC(size + usedLength + align,
- ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- WOLFSSL_MSG("growing input buffer\n");
-
- if (tmp == NULL)
- return MEMORY_E;
-
-#if defined(WOLFSSL_DTLS) || WOLFSSL_GENERAL_ALIGNMENT > 0
- if (align)
- tmp += align - hdrSz;
-#endif
-
-#ifdef WOLFSSL_STATIC_MEMORY
- /* can be from IO memory pool which does not need copy if same buffer */
- if (usedLength && tmp == ssl->buffers.inputBuffer.buffer) {
- ssl->buffers.inputBuffer.bufferSize = size + usedLength;
- ssl->buffers.inputBuffer.idx = 0;
- ssl->buffers.inputBuffer.length = usedLength;
- return 0;
- }
-#endif
-
- if (usedLength)
- XMEMCPY(tmp, ssl->buffers.inputBuffer.buffer +
- ssl->buffers.inputBuffer.idx, usedLength);
-
- if (ssl->buffers.inputBuffer.dynamicFlag)
- XFREE(ssl->buffers.inputBuffer.buffer - ssl->buffers.inputBuffer.offset,
- ssl->heap,DYNAMIC_TYPE_IN_BUFFER);
-
- ssl->buffers.inputBuffer.dynamicFlag = 1;
-#if defined(WOLFSSL_DTLS) || WOLFSSL_GENERAL_ALIGNMENT > 0
- if (align)
- ssl->buffers.inputBuffer.offset = align - hdrSz;
- else
-#endif
- ssl->buffers.inputBuffer.offset = 0;
-
- ssl->buffers.inputBuffer.buffer = tmp;
- ssl->buffers.inputBuffer.bufferSize = size + usedLength;
- ssl->buffers.inputBuffer.idx = 0;
- ssl->buffers.inputBuffer.length = usedLength;
-
- return 0;
-}
-
-
-/* check available size into output buffer, make room if needed */
-int CheckAvailableSize(WOLFSSL *ssl, int size)
-{
- if (size < 0) {
- WOLFSSL_MSG("CheckAvailableSize() called with negative number");
- return BAD_FUNC_ARG;
- }
-
- if (ssl->buffers.outputBuffer.bufferSize - ssl->buffers.outputBuffer.length
- < (word32)size) {
- if (GrowOutputBuffer(ssl, size) < 0)
- return MEMORY_E;
- }
-
- return 0;
-}
-
-
-/* do all verify and sanity checks on record header */
-static int GetRecordHeader(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- RecordLayerHeader* rh, word16 *size)
-{
- if (!ssl->options.dtls) {
-#ifdef HAVE_FUZZER
- if (ssl->fuzzerCb)
- ssl->fuzzerCb(ssl, input + *inOutIdx, RECORD_HEADER_SZ, FUZZ_HEAD,
- ssl->fuzzerCtx);
-#endif
- XMEMCPY(rh, input + *inOutIdx, RECORD_HEADER_SZ);
- *inOutIdx += RECORD_HEADER_SZ;
- ato16(rh->length, size);
- }
- else {
-#ifdef WOLFSSL_DTLS
-#ifdef HAVE_FUZZER
- if (ssl->fuzzerCb)
- ssl->fuzzerCb(ssl, input + *inOutIdx, DTLS_RECORD_HEADER_SZ,
- FUZZ_HEAD, ssl->fuzzerCtx);
-#endif
- /* type and version in same sport */
- XMEMCPY(rh, input + *inOutIdx, ENUM_LEN + VERSION_SZ);
- *inOutIdx += ENUM_LEN + VERSION_SZ;
- ato16(input + *inOutIdx, &ssl->keys.curEpoch);
- *inOutIdx += OPAQUE16_LEN;
- if (ssl->options.haveMcast) {
- #ifdef WOLFSSL_MULTICAST
- ssl->keys.curPeerId = input[*inOutIdx];
- ssl->keys.curSeq_hi = input[*inOutIdx+1];
- #endif
- }
- else
- ato16(input + *inOutIdx, &ssl->keys.curSeq_hi);
- *inOutIdx += OPAQUE16_LEN;
- ato32(input + *inOutIdx, &ssl->keys.curSeq_lo);
- *inOutIdx += OPAQUE32_LEN; /* advance past rest of seq */
- ato16(input + *inOutIdx, size);
- *inOutIdx += LENGTH_SZ;
-#endif
- }
-
-#ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl) && !DtlsCheckWindow(ssl)) {
- WOLFSSL_LEAVE("GetRecordHeader()", SEQUENCE_ERROR);
- return SEQUENCE_ERROR;
- }
-#endif
-
- /* catch version mismatch */
-#ifndef WOLFSSL_TLS13
- if (rh->pvMajor != ssl->version.major || rh->pvMinor != ssl->version.minor)
-#else
- if (rh->pvMajor != ssl->version.major ||
- (rh->pvMinor != ssl->version.minor &&
-#ifdef WOLFSSL_TLS13_DRAFT_18
- (!IsAtLeastTLSv1_3(ssl->version) || rh->pvMinor != TLSv1_MINOR)
-#else
- (!IsAtLeastTLSv1_3(ssl->version) || rh->pvMinor != TLSv1_2_MINOR)
-#endif
- ))
-#endif
- {
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- ssl->options.acceptState < ACCEPT_FIRST_REPLY_DONE)
-
- WOLFSSL_MSG("Client attempting to connect with different version");
- else if (ssl->options.side == WOLFSSL_CLIENT_END &&
- ssl->options.downgrade &&
- ssl->options.connectState < FIRST_REPLY_DONE)
- WOLFSSL_MSG("Server attempting to accept with different version");
- else if (ssl->options.dtls && rh->type == handshake)
- /* Check the DTLS handshake message RH version later. */
- WOLFSSL_MSG("DTLS handshake, skip RH version number check");
- else {
- WOLFSSL_MSG("SSL version error");
- /* send alert per RFC5246 Appendix E. Backward Compatibility */
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
-#ifdef WOLFSSL_MYSQL_COMPATIBLE
- SendAlert(ssl, alert_fatal, wc_protocol_version);
-#else
- SendAlert(ssl, alert_fatal, protocol_version);
-#endif
- }
- return VERSION_ERROR; /* only use requested version */
- }
- }
-
- /* record layer length check */
-#ifdef HAVE_MAX_FRAGMENT
- if (*size > (ssl->max_fragment + MAX_COMP_EXTRA + MAX_MSG_EXTRA)) {
- SendAlert(ssl, alert_fatal, record_overflow);
- return LENGTH_ERROR;
- }
-#else
- if (*size > (MAX_RECORD_SIZE + MAX_COMP_EXTRA + MAX_MSG_EXTRA))
- return LENGTH_ERROR;
-#endif
-
- /* verify record type here as well */
- switch (rh->type) {
- case handshake:
- case change_cipher_spec:
- case application_data:
- case alert:
- break;
- case no_type:
- default:
- WOLFSSL_MSG("Unknown Record Type");
- return UNKNOWN_RECORD_TYPE;
- }
-
- /* haven't decrypted this record yet */
- ssl->keys.decryptedCur = 0;
-
- return 0;
-}
-
-#ifndef WOLFSSL_NO_TLS12
-static int GetHandShakeHeader(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- byte *type, word32 *size, word32 totalSz)
-{
- const byte *ptr = input + *inOutIdx;
- (void)ssl;
-
- *inOutIdx += HANDSHAKE_HEADER_SZ;
- if (*inOutIdx > totalSz)
- return BUFFER_E;
-
- *type = ptr[0];
- c24to32(&ptr[1], size);
-
- return 0;
-}
-#endif
-
-#ifdef WOLFSSL_DTLS
-static int GetDtlsHandShakeHeader(WOLFSSL* ssl, const byte* input,
- word32* inOutIdx, byte *type, word32 *size,
- word32 *fragOffset, word32 *fragSz,
- word32 totalSz)
-{
- word32 idx = *inOutIdx;
-
- *inOutIdx += HANDSHAKE_HEADER_SZ + DTLS_HANDSHAKE_EXTRA;
- if (*inOutIdx > totalSz) {
- WOLFSSL_ERROR(BUFFER_E);
- return BUFFER_E;
- }
-
- *type = input[idx++];
- c24to32(input + idx, size);
- idx += OPAQUE24_LEN;
-
- ato16(input + idx, &ssl->keys.dtls_peer_handshake_number);
- idx += DTLS_HANDSHAKE_SEQ_SZ;
-
- c24to32(input + idx, fragOffset);
- idx += DTLS_HANDSHAKE_FRAG_SZ;
- c24to32(input + idx, fragSz);
-
- if (ssl->curRL.pvMajor != ssl->version.major ||
- ssl->curRL.pvMinor != ssl->version.minor) {
-
- if (*type != client_hello && *type != hello_verify_request) {
- WOLFSSL_ERROR(VERSION_ERROR);
- return VERSION_ERROR;
- }
- else {
- WOLFSSL_MSG("DTLS Handshake ignoring hello or verify version");
- }
- }
- return 0;
-}
-#endif
-
-
-#if !defined(NO_OLD_TLS) || \
- (defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLS_SHA1))
-/* fill with MD5 pad size since biggest required */
-static const byte PAD1[PAD_MD5] =
- { 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
- 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
- 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
- 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
- 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
- 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
- };
-static const byte PAD2[PAD_MD5] =
- { 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
- 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
- 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
- 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
- 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
- 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
- };
-#endif /* !NO_OLD_TLS || (NO_OLD_TLS && WOLFSSL_ALLOW_TLS_SHA1) */
-
-#ifndef NO_OLD_TLS
-
-/* calculate MD5 hash for finished */
-#ifdef WOLFSSL_TI_HASH
-#include
-#endif
-
-static int BuildMD5(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
-{
- int ret;
- byte md5_result[WC_MD5_DIGEST_SIZE];
-#ifdef WOLFSSL_SMALL_STACK
- wc_Md5* md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), ssl->heap, DYNAMIC_TYPE_HASHCTX);
- if (md5 == NULL)
- return MEMORY_E;
-#else
- wc_Md5 md5[1];
-#endif
-
- /* make md5 inner */
- ret = wc_Md5Copy(&ssl->hsHashes->hashMd5, md5);
- if (ret == 0)
- ret = wc_Md5Update(md5, sender, SIZEOF_SENDER);
- if (ret == 0)
- ret = wc_Md5Update(md5, ssl->arrays->masterSecret,SECRET_LEN);
- if (ret == 0)
- ret = wc_Md5Update(md5, PAD1, PAD_MD5);
- if (ret == 0)
- ret = wc_Md5Final(md5, md5_result);
-
- /* make md5 outer */
- if (ret == 0) {
- ret = wc_InitMd5_ex(md5, ssl->heap, ssl->devId);
- if (ret == 0) {
- ret = wc_Md5Update(md5, ssl->arrays->masterSecret,SECRET_LEN);
- if (ret == 0)
- ret = wc_Md5Update(md5, PAD2, PAD_MD5);
- if (ret == 0)
- ret = wc_Md5Update(md5, md5_result, WC_MD5_DIGEST_SIZE);
- if (ret == 0)
- ret = wc_Md5Final(md5, hashes->md5);
- wc_Md5Free(md5);
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(md5, ssl->heap, DYNAMIC_TYPE_HASHCTX);
-#endif
-
- return ret;
-}
-
-
-/* calculate SHA hash for finished */
-static int BuildSHA(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
-{
- int ret;
- byte sha_result[WC_SHA_DIGEST_SIZE];
-#ifdef WOLFSSL_SMALL_STACK
- wc_Sha* sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), ssl->heap, DYNAMIC_TYPE_HASHCTX);
- if (sha == NULL)
- return MEMORY_E;
-#else
- wc_Sha sha[1];
-#endif
- /* make sha inner */
- ret = wc_ShaCopy(&ssl->hsHashes->hashSha, sha); /* Save current position */
- if (ret == 0)
- ret = wc_ShaUpdate(sha, sender, SIZEOF_SENDER);
- if (ret == 0)
- ret = wc_ShaUpdate(sha, ssl->arrays->masterSecret,SECRET_LEN);
- if (ret == 0)
- ret = wc_ShaUpdate(sha, PAD1, PAD_SHA);
- if (ret == 0)
- ret = wc_ShaFinal(sha, sha_result);
-
- /* make sha outer */
- if (ret == 0) {
- ret = wc_InitSha_ex(sha, ssl->heap, ssl->devId);
- if (ret == 0) {
- ret = wc_ShaUpdate(sha, ssl->arrays->masterSecret,SECRET_LEN);
- if (ret == 0)
- ret = wc_ShaUpdate(sha, PAD2, PAD_SHA);
- if (ret == 0)
- ret = wc_ShaUpdate(sha, sha_result, WC_SHA_DIGEST_SIZE);
- if (ret == 0)
- ret = wc_ShaFinal(sha, hashes->sha);
- wc_ShaFree(sha);
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(sha, ssl->heap, DYNAMIC_TYPE_HASHCTX);
-#endif
-
- return ret;
-}
-#endif
-
-#ifndef WOLFSSL_NO_TLS12
-
-/* Finished doesn't support SHA512, not SHA512 cipher suites yet */
-static int BuildFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
-{
- int ret = 0;
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
-#ifndef NO_TLS
- if (ssl->options.tls) {
- ret = BuildTlsFinished(ssl, hashes, sender);
- }
-#endif
-#ifndef NO_OLD_TLS
- if (!ssl->options.tls) {
- ret = BuildMD5(ssl, hashes, sender);
- if (ret == 0) {
- ret = BuildSHA(ssl, hashes, sender);
- }
- }
-#endif
-
- return ret;
-}
-
-#endif /* WOLFSSL_NO_TLS12 */
-
-#if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
- /* cipher requirements */
- enum {
- REQUIRES_RSA,
- REQUIRES_DHE,
- REQUIRES_ECC,
- REQUIRES_ECC_STATIC,
- REQUIRES_PSK,
- REQUIRES_NTRU,
- REQUIRES_RSA_SIG,
- REQUIRES_AEAD
- };
-
-
-
- /* Does this cipher suite (first, second) have the requirement
- an ephemeral key exchange will still require the key for signing
- the key exchange so ECHDE_RSA requires an rsa key thus rsa_kea */
- static int CipherRequires(byte first, byte second, int requirement)
- {
-
- (void)requirement;
-
-#ifndef WOLFSSL_NO_TLS12
-
-#ifdef HAVE_CHACHA
- if (first == CHACHA_BYTE) {
-
- switch (second) {
- case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
- if (requirement == REQUIRES_ECC)
- return 1;
- break;
-
- case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- break;
-
- case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
- if (requirement == REQUIRES_ECC)
- return 1;
- break;
-
- case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- break;
-
-
- case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 :
- if (requirement == REQUIRES_PSK)
- return 1;
- break;
-
- case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 :
- if (requirement == REQUIRES_PSK)
- return 1;
- break;
-
- case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 :
- if (requirement == REQUIRES_PSK)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- break;
- }
-
- if (requirement == REQUIRES_AEAD)
- return 1;
-
- }
-#endif /* HAVE_CHACHA */
-
- /* ECC extensions */
- if (first == ECC_BYTE) {
-
- switch (second) {
-#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
- #ifndef NO_RSA
- case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- if (requirement == REQUIRES_RSA_SIG)
- return 1;
- break;
-
- #ifndef NO_DES3
- case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- if (requirement == REQUIRES_RSA_SIG)
- return 1;
- break;
- #endif /* !NO_DES3 */
-
- #ifndef NO_RC4
- case TLS_ECDHE_RSA_WITH_RC4_128_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_ECDH_RSA_WITH_RC4_128_SHA :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- if (requirement == REQUIRES_RSA_SIG)
- return 1;
- break;
- #endif /* !NO_RC4 */
- #endif /* NO_RSA */
-
- #ifndef NO_DES3
- case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA :
- if (requirement == REQUIRES_ECC)
- return 1;
- break;
-
- case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- break;
- #endif /* !NO_DES3 */
- #ifndef NO_RC4
- case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA :
- if (requirement == REQUIRES_ECC)
- return 1;
- break;
-
- case TLS_ECDH_ECDSA_WITH_RC4_128_SHA :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- break;
- #endif /* !NO_RC4 */
- #ifndef NO_RSA
- case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- if (requirement == REQUIRES_RSA_SIG)
- return 1;
- break;
- #endif /* !NO_RSA */
-
- case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA :
- if (requirement == REQUIRES_ECC)
- return 1;
- break;
-
- case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- break;
-
- case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA :
- if (requirement == REQUIRES_ECC)
- return 1;
- break;
-
- case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- break;
-
- case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 :
- if (requirement == REQUIRES_ECC)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 :
- if (requirement == REQUIRES_ECC)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-#endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-
-#ifndef NO_RSA
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
- case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- if (requirement == REQUIRES_RSA_SIG)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 :
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- if (requirement == REQUIRES_RSA_SIG)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
- #ifdef HAVE_AESCCM
- case TLS_RSA_WITH_AES_128_CCM_8 :
- case TLS_RSA_WITH_AES_256_CCM_8 :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_RSA_SIG)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
- #endif /* HAVE_AESCCM */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
-
- case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 :
- case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 :
- case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 :
- if (requirement == REQUIRES_RSA_SIG)
- return 1;
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- break;
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-#endif /* !NO_RSA */
-
-#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
- case TLS_ECDHE_ECDSA_WITH_AES_128_CCM :
- case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 :
- case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 :
- if (requirement == REQUIRES_ECC)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 :
- case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 :
- if (requirement == REQUIRES_ECC)
- return 1;
- break;
-
- case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 :
- case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 :
- if (requirement == REQUIRES_ECC)
- return 1;
- if (requirement == REQUIRES_ECC_STATIC)
- return 1;
- break;
-#endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-
-#ifndef NO_PSK
- case TLS_PSK_WITH_AES_128_CCM:
- case TLS_PSK_WITH_AES_256_CCM:
- case TLS_PSK_WITH_AES_128_CCM_8:
- case TLS_PSK_WITH_AES_256_CCM_8:
- if (requirement == REQUIRES_PSK)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_DHE_PSK_WITH_AES_128_CCM:
- case TLS_DHE_PSK_WITH_AES_256_CCM:
- if (requirement == REQUIRES_PSK)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-#endif /* !NO_PSK */
-#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
- case TLS_ECDHE_ECDSA_WITH_NULL_SHA :
- if (requirement == REQUIRES_ECC)
- return 1;
- break;
-
- case TLS_ECDHE_PSK_WITH_NULL_SHA256 :
- if (requirement == REQUIRES_PSK)
- return 1;
- break;
-
- case TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 :
- if (requirement == REQUIRES_PSK)
- return 1;
- break;
-#endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-
-#if defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER)
- case TLS_SHA256_SHA256:
- break;
- case TLS_SHA384_SHA384:
- break;
-#endif
-
- default:
- WOLFSSL_MSG("Unsupported cipher suite, CipherRequires ECC");
- return 0;
- } /* switch */
- } /* if */
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
- /* Distinct TLS v1.3 cipher suites with cipher and digest only. */
- if (first == TLS13_BYTE) {
-
- switch (second) {
-#ifdef WOLFSSL_TLS13
- case TLS_AES_128_GCM_SHA256:
- case TLS_AES_256_GCM_SHA384:
- case TLS_CHACHA20_POLY1305_SHA256:
- case TLS_AES_128_CCM_SHA256:
- case TLS_AES_128_CCM_8_SHA256:
- break;
-#endif
-
- default:
- WOLFSSL_MSG("Unsupported cipher suite, CipherRequires "
- "TLS v1.3");
- return 0;
- }
- }
-
-#ifndef WOLFSSL_NO_TLS12
-
- if (first != ECC_BYTE && first != CHACHA_BYTE &&
- first != TLS13_BYTE) { /* normal suites */
- switch (second) {
-
-#ifndef NO_RSA
- #ifndef NO_RC4
- case SSL_RSA_WITH_RC4_128_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case SSL_RSA_WITH_RC4_128_MD5 :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
- #endif /* NO_RC4 */
-
- case SSL_RSA_WITH_3DES_EDE_CBC_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- #ifdef HAVE_NTRU
- case TLS_NTRU_RSA_WITH_RC4_128_SHA :
- if (requirement == REQUIRES_NTRU)
- return 1;
- break;
- #endif /* HAVE_NTRU */
-
- case TLS_RSA_WITH_AES_128_CBC_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_RSA_WITH_AES_128_CBC_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- #ifdef HAVE_NTRU
- case TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA :
- if (requirement == REQUIRES_NTRU)
- return 1;
- break;
- #endif /* HAVE_NTRU */
-
- case TLS_RSA_WITH_AES_256_CBC_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- #ifdef HAVE_NTRU
- case TLS_NTRU_RSA_WITH_AES_128_CBC_SHA :
- if (requirement == REQUIRES_NTRU)
- return 1;
- break;
- #endif /* HAVE_NTRU */
-
- case TLS_RSA_WITH_AES_256_CBC_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_RSA_WITH_NULL_MD5 :
- case TLS_RSA_WITH_NULL_SHA :
- case TLS_RSA_WITH_NULL_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- #ifdef HAVE_NTRU
- case TLS_NTRU_RSA_WITH_AES_256_CBC_SHA :
- if (requirement == REQUIRES_NTRU)
- return 1;
- break;
- #endif /* HAVE_NTRU */
-
- #ifdef HAVE_IDEA
- case SSL_RSA_WITH_IDEA_CBC_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
- #endif /* HAVE_IDEA */
-#endif /* !NO_RSA */
-
-#ifndef NO_PSK
- case TLS_PSK_WITH_AES_128_GCM_SHA256 :
- if (requirement == REQUIRES_PSK)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_PSK_WITH_AES_256_GCM_SHA384 :
- if (requirement == REQUIRES_PSK)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_PSK_WITH_AES_128_CBC_SHA256 :
- case TLS_PSK_WITH_AES_256_CBC_SHA384 :
- case TLS_PSK_WITH_AES_128_CBC_SHA :
- case TLS_PSK_WITH_AES_256_CBC_SHA :
- case TLS_PSK_WITH_NULL_SHA384 :
- case TLS_PSK_WITH_NULL_SHA256 :
- case TLS_PSK_WITH_NULL_SHA :
- if (requirement == REQUIRES_PSK)
- return 1;
- break;
-
- case TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 :
- case TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 :
- if (requirement == REQUIRES_DHE)
- return 1;
- if (requirement == REQUIRES_PSK)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 :
- case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 :
- case TLS_DHE_PSK_WITH_NULL_SHA384 :
- case TLS_DHE_PSK_WITH_NULL_SHA256 :
- if (requirement == REQUIRES_DHE)
- return 1;
- if (requirement == REQUIRES_PSK)
- return 1;
- break;
-#endif /* NO_PSK */
-
-#ifndef NO_RSA
- case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- break;
-
- case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- break;
-
- case TLS_DHE_RSA_WITH_AES_128_CBC_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- break;
-
- case TLS_DHE_RSA_WITH_AES_256_CBC_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- break;
-
-#ifndef NO_HC128
- case TLS_RSA_WITH_HC_128_MD5 :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_RSA_WITH_HC_128_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-#endif /* NO_HC128 */
-
-#ifndef NO_RABBIT
- case TLS_RSA_WITH_RABBIT_SHA :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-#endif /* !NO_RABBIT */
-
- case TLS_RSA_WITH_AES_128_GCM_SHA256 :
- case TLS_RSA_WITH_AES_256_GCM_SHA384 :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
- case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 :
- case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-
-#ifdef HAVE_CAMELLIA
- case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA :
- case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA :
- case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
- case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- break;
-
- case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA :
- case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA :
- case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
- case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_RSA_SIG)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- break;
-#endif /* HAVE_CAMELLIA */
-
- case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
- if (requirement == REQUIRES_RSA)
- return 1;
- if (requirement == REQUIRES_RSA_SIG)
- return 1;
- if (requirement == REQUIRES_DHE)
- return 1;
- break;
-#endif
-#ifdef HAVE_ANON
- case TLS_DH_anon_WITH_AES_128_CBC_SHA :
- if (requirement == REQUIRES_DHE)
- return 1;
- break;
- case TLS_DH_anon_WITH_AES_256_GCM_SHA384:
- if (requirement == REQUIRES_DHE)
- return 1;
- if (requirement == REQUIRES_AEAD)
- return 1;
- break;
-#endif
-#ifdef WOLFSSL_MULTICAST
- case WDM_WITH_NULL_SHA256 :
- break;
-#endif
-
- default:
- WOLFSSL_MSG("Unsupported cipher suite, CipherRequires");
- return 0;
- } /* switch */
- } /* if ECC / Normal suites else */
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
- return 0;
- }
-
-#endif /* !NO_WOLFSSL_SERVER && !NO_WOLFSSL_CLIENT */
-
-
-#ifndef NO_CERTS
-
-
-/* Match names with wildcards, each wildcard can represent a single name
- component or fragment but not multiple names, i.e.,
- *.z.com matches y.z.com but not x.y.z.com
-
- return 1 on success */
-int MatchDomainName(const char* pattern, int len, const char* str)
-{
- int ret = 0;
- char p, s;
-
- if (pattern == NULL || str == NULL || len <= 0)
- return 0;
-
- while (len > 0) {
-
- p = (char)XTOLOWER((unsigned char)*pattern++);
- if (p == '\0')
- break;
-
- if (p == '*') {
- while (--len > 0 &&
- (p = (char)XTOLOWER((unsigned char)*pattern++)) == '*') {
- }
-
- if (len == 0)
- p = '\0';
-
- while ( (s = (char)XTOLOWER((unsigned char) *str)) != '\0') {
- if (s == p)
- break;
- if (s == '.')
- return 0;
- str++;
- }
- }
- else {
- if (p != (char)XTOLOWER((unsigned char) *str))
- return 0;
- }
-
-
- if (len > 0) {
- str++;
- len--;
- }
- }
-
- if (*str == '\0' && len == 0) {
- ret = 1; /* success */
- }
-
- return ret;
-}
-
-
-/* try to find an altName match to domain, return 1 on success */
-int CheckAltNames(DecodedCert* dCert, char* domain)
-{
- int match = 0;
- DNS_entry* altName = NULL;
-
- WOLFSSL_MSG("Checking AltNames");
-
- if (dCert)
- altName = dCert->altNames;
-
- while (altName) {
- WOLFSSL_MSG("\tindividual AltName check");
-
- if (MatchDomainName(altName->name, altName->len, domain)){
- match = 1;
- break;
- }
-
- altName = altName->next;
- }
-
- return match;
-}
-
-#ifdef OPENSSL_EXTRA
-/* Check that alternative names, if they exists, match the domain.
- * Fail if there are wild patterns and they didn't match.
- * Check the common name if no alternative names matched.
- *
- * dCert Decoded cert to get the alternative names from.
- * domain Domain name to compare against.
- * checkCN Whether to check the common name.
- * returns whether there was a problem in matching.
- */
-static int CheckForAltNames(DecodedCert* dCert, char* domain, int* checkCN)
-{
- int match;
- DNS_entry* altName = NULL;
-
- WOLFSSL_MSG("Checking AltNames");
-
- if (dCert)
- altName = dCert->altNames;
-
- *checkCN = altName == NULL;
- match = 0;
- while (altName) {
- WOLFSSL_MSG("\tindividual AltName check");
-
- if (MatchDomainName(altName->name, altName->len, domain)) {
- match = 1;
- *checkCN = 0;
- break;
- }
- /* No matches and wild pattern match failed. */
- else if (altName->name && altName->len >=1 &&
- altName->name[0] == '*' && match == 0) {
- match = -1;
- }
-
- altName = altName->next;
- }
-
- return match != -1;
-}
-
-/* Check the domain name matches the subject alternative name or the subject
- * name.
- *
- * dcert Decoded certificate.
- * domainName The domain name.
- * domainNameLen The length of the domain name.
- * returns DOMAIN_NAME_MISMATCH when no match found and 0 on success.
- */
-int CheckHostName(DecodedCert* dCert, char *domainName, size_t domainNameLen)
-{
- int checkCN;
-
- /* Assume name is NUL terminated. */
- (void)domainNameLen;
-
- if (CheckForAltNames(dCert, domainName, &checkCN) == 0) {
- WOLFSSL_MSG("DomainName match on alt names failed too");
- return DOMAIN_NAME_MISMATCH;
- }
- if (checkCN == 1) {
- if (MatchDomainName(dCert->subjectCN, dCert->subjectCNLen,
- domainName) == 0) {
- WOLFSSL_MSG("DomainName match on common name failed");
- return DOMAIN_NAME_MISMATCH;
- }
- }
-
- return 0;
-}
-
-int CheckIPAddr(DecodedCert* dCert, char* ipasc)
-{
- WOLFSSL_MSG("Checking IPAddr");
-
- return CheckHostName(dCert, ipasc, (size_t)XSTRLEN(ipasc));
-}
-#endif
-
-#ifdef SESSION_CERTS
-static void AddSessionCertToChain(WOLFSSL_X509_CHAIN* chain,
- byte* certBuf, word32 certSz)
-{
- if (chain->count < MAX_CHAIN_DEPTH &&
- certSz < MAX_X509_SIZE) {
- chain->certs[chain->count].length = certSz;
- XMEMCPY(chain->certs[chain->count].buffer, certBuf, certSz);
- chain->count++;
- }
- else {
- WOLFSSL_MSG("Couldn't store chain cert for session");
- }
-}
-#endif
-
-#if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
- defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
-/* Copy parts X509 needs from Decoded cert, 0 on success */
-/* The same DecodedCert cannot be copied to WOLFSSL_X509 twice otherwise the
- * altNames pointers could be free'd by second x509 still active by first */
-int CopyDecodedToX509(WOLFSSL_X509* x509, DecodedCert* dCert)
-{
- int ret = 0;
-
- if (x509 == NULL || dCert == NULL ||
- dCert->subjectCNLen < 0)
- return BAD_FUNC_ARG;
-
- x509->version = dCert->version + 1;
-
- XSTRNCPY(x509->issuer.name, dCert->issuer, ASN_NAME_MAX);
- x509->issuer.name[ASN_NAME_MAX - 1] = '\0';
- x509->issuer.sz = (int)XSTRLEN(x509->issuer.name) + 1;
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- if (dCert->issuerName.fullName != NULL) {
- XMEMCPY(&x509->issuer.fullName,
- &dCert->issuerName, sizeof(DecodedName));
- x509->issuer.fullName.fullName = (char*)XMALLOC(
- dCert->issuerName.fullNameLen, x509->heap,
- DYNAMIC_TYPE_X509);
- if (x509->issuer.fullName.fullName != NULL)
- XMEMCPY(x509->issuer.fullName.fullName,
- dCert->issuerName.fullName, dCert->issuerName.fullNameLen);
- }
- x509->issuer.x509 = x509;
-#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
-
- XSTRNCPY(x509->subject.name, dCert->subject, ASN_NAME_MAX);
- x509->subject.name[ASN_NAME_MAX - 1] = '\0';
- x509->subject.sz = (int)XSTRLEN(x509->subject.name) + 1;
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- if (dCert->subjectName.fullName != NULL) {
- XMEMCPY(&x509->subject.fullName,
- &dCert->subjectName, sizeof(DecodedName));
- x509->subject.fullName.fullName = (char*)XMALLOC(
- dCert->subjectName.fullNameLen, x509->heap, DYNAMIC_TYPE_X509);
- if (x509->subject.fullName.fullName != NULL)
- XMEMCPY(x509->subject.fullName.fullName,
- dCert->subjectName.fullName, dCert->subjectName.fullNameLen);
- }
- x509->subject.x509 = x509;
-#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
- x509->subject.rawLen = min(dCert->subjectRawLen, sizeof(x509->subject.raw));
- XMEMCPY(x509->subject.raw, dCert->subjectRaw, x509->subject.rawLen);
-#ifdef WOLFSSL_CERT_EXT
- x509->issuer.rawLen = min(dCert->issuerRawLen, sizeof(x509->issuer.raw));
- XMEMCPY(x509->issuer.raw, dCert->issuerRaw, x509->issuer.rawLen);
-#endif
-#endif
-
- XMEMCPY(x509->serial, dCert->serial, EXTERNAL_SERIAL_SIZE);
- x509->serialSz = dCert->serialSz;
- if (dCert->subjectCN && dCert->subjectCNLen < ASN_NAME_MAX) {
- XMEMCPY(x509->subjectCN, dCert->subjectCN, dCert->subjectCNLen);
- x509->subjectCN[dCert->subjectCNLen] = '\0';
- }
- else
- x509->subjectCN[0] = '\0';
-
-#ifdef WOLFSSL_SEP
- {
- int minSz = min(dCert->deviceTypeSz, EXTERNAL_SERIAL_SIZE);
- if (minSz > 0) {
- x509->deviceTypeSz = minSz;
- XMEMCPY(x509->deviceType, dCert->deviceType, minSz);
- }
- else
- x509->deviceTypeSz = 0;
- minSz = min(dCert->hwTypeSz, EXTERNAL_SERIAL_SIZE);
- if (minSz > 0) {
- x509->hwTypeSz = minSz;
- XMEMCPY(x509->hwType, dCert->hwType, minSz);
- }
- else
- x509->hwTypeSz = 0;
- minSz = min(dCert->hwSerialNumSz, EXTERNAL_SERIAL_SIZE);
- if (minSz > 0) {
- x509->hwSerialNumSz = minSz;
- XMEMCPY(x509->hwSerialNum, dCert->hwSerialNum, minSz);
- }
- else
- x509->hwSerialNumSz = 0;
- }
-#endif /* WOLFSSL_SEP */
- {
- int minSz;
- if (dCert->beforeDateLen > 0) {
- minSz = min(dCert->beforeDate[1], MAX_DATE_SZ);
- x509->notBefore.type = dCert->beforeDate[0];
- x509->notBefore.length = minSz;
- XMEMCPY(x509->notBefore.data, &dCert->beforeDate[2], minSz);
- }
- else
- x509->notBefore.length = 0;
- if (dCert->afterDateLen > 0) {
- minSz = min(dCert->afterDate[1], MAX_DATE_SZ);
- x509->notAfter.type = dCert->afterDate[0];
- x509->notAfter.length = minSz;
- XMEMCPY(x509->notAfter.data, &dCert->afterDate[2], minSz);
- }
- else
- x509->notAfter.length = 0;
- }
-
- if (dCert->publicKey != NULL && dCert->pubKeySize != 0) {
- x509->pubKey.buffer = (byte*)XMALLOC(
- dCert->pubKeySize, x509->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (x509->pubKey.buffer != NULL) {
- x509->pubKeyOID = dCert->keyOID;
- x509->pubKey.length = dCert->pubKeySize;
- XMEMCPY(x509->pubKey.buffer, dCert->publicKey, dCert->pubKeySize);
- }
- else
- ret = MEMORY_E;
-#if defined(OPENSSL_ALL)
- if (ret == 0) {
- x509->key.pubKeyOID = dCert->keyOID;
-
- if (!x509->key.algor) {
- x509->key.algor = wolfSSL_X509_ALGOR_new();
- } else {
- wolfSSL_ASN1_OBJECT_free(x509->key.algor->algorithm);
- }
- if (!(x509->key.algor->algorithm =
- wolfSSL_OBJ_nid2obj(dCert->keyOID))) {
- ret = PUBLIC_KEY_E;
- }
-
- wolfSSL_EVP_PKEY_free(x509->key.pkey);
- if (!(x509->key.pkey = wolfSSL_d2i_PUBKEY(NULL,
- &dCert->publicKey,
- dCert->pubKeySize))) {
- ret = PUBLIC_KEY_E;
- }
- }
-#endif
- }
-
- if (dCert->signature != NULL && dCert->sigLength != 0 &&
- dCert->sigLength <= MAX_ENCODED_SIG_SZ) {
- x509->sig.buffer = (byte*)XMALLOC(
- dCert->sigLength, x509->heap, DYNAMIC_TYPE_SIGNATURE);
- if (x509->sig.buffer == NULL) {
- ret = MEMORY_E;
- }
- else {
- XMEMCPY(x509->sig.buffer, dCert->signature, dCert->sigLength);
- x509->sig.length = dCert->sigLength;
- x509->sigOID = dCert->signatureOID;
- }
-#if defined(OPENSSL_ALL)
- wolfSSL_ASN1_OBJECT_free(x509->algor.algorithm);
- if (!(x509->algor.algorithm =
- wolfSSL_OBJ_nid2obj(dCert->signatureOID))) {
- ret = PUBLIC_KEY_E;
- }
-#endif
- }
-
- /* store cert for potential retrieval */
- if (AllocDer(&x509->derCert, dCert->maxIdx, CERT_TYPE, x509->heap) == 0) {
- XMEMCPY(x509->derCert->buffer, dCert->source, dCert->maxIdx);
- }
- else {
- ret = MEMORY_E;
- }
-
- x509->altNames = dCert->altNames;
- dCert->weOwnAltNames = 0;
- x509->altNamesNext = x509->altNames; /* index hint */
-
-#if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
- !defined(IGNORE_NAME_CONSTRAINTS)
- /* add copies of alternate emails from dCert to X509 */
- if (dCert->altEmailNames != NULL) {
- DNS_entry* cur = dCert->altEmailNames;
-
- while (cur != NULL) {
- if (cur->type == ASN_RFC822_TYPE) {
- DNS_entry* dnsEntry;
- int strLen = cur->len;
-
- dnsEntry = (DNS_entry*)XMALLOC(sizeof(DNS_entry), x509->heap,
- DYNAMIC_TYPE_ALTNAME);
- if (dnsEntry == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- return MEMORY_E;
- }
-
- dnsEntry->type = ASN_RFC822_TYPE;
- dnsEntry->name = (char*)XMALLOC(strLen + 1, x509->heap,
- DYNAMIC_TYPE_ALTNAME);
- if (dnsEntry->name == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- XFREE(dnsEntry, x509->heap, DYNAMIC_TYPE_ALTNAME);
- return MEMORY_E;
- }
- dnsEntry->len = strLen;
- XMEMCPY(dnsEntry->name, cur->name, strLen);
- dnsEntry->name[strLen] = '\0';
-
- dnsEntry->next = x509->altNames;
- x509->altNames = dnsEntry;
- }
- cur = cur->next;
- }
- }
-#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
-
- x509->isCa = dCert->isCA;
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- x509->pathLength = dCert->pathLength;
- x509->keyUsage = dCert->extKeyUsage;
-
- x509->CRLdistSet = dCert->extCRLdistSet;
- x509->CRLdistCrit = dCert->extCRLdistCrit;
- x509->CRLInfo = dCert->extCrlInfo;
- x509->CRLInfoSz = dCert->extCrlInfoSz;
- x509->authInfoSet = dCert->extAuthInfoSet;
- x509->authInfoCrit = dCert->extAuthInfoCrit;
- if (dCert->extAuthInfo != NULL && dCert->extAuthInfoSz > 0) {
- x509->authInfo = (byte*)XMALLOC(dCert->extAuthInfoSz, x509->heap,
- DYNAMIC_TYPE_X509_EXT);
- if (x509->authInfo != NULL) {
- XMEMCPY(x509->authInfo, dCert->extAuthInfo, dCert->extAuthInfoSz);
- x509->authInfoSz = dCert->extAuthInfoSz;
- }
- else {
- ret = MEMORY_E;
- }
- }
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- if (dCert->extAuthInfoCaIssuer != NULL && dCert->extAuthInfoCaIssuerSz > 0) {
- x509->authInfoCaIssuer = (byte*)XMALLOC(dCert->extAuthInfoCaIssuerSz, x509->heap,
- DYNAMIC_TYPE_X509_EXT);
- if (x509->authInfoCaIssuer != NULL) {
- XMEMCPY(x509->authInfoCaIssuer, dCert->extAuthInfoCaIssuer, dCert->extAuthInfoCaIssuerSz);
- x509->authInfoCaIssuerSz = dCert->extAuthInfoCaIssuerSz;
- }
- else {
- ret = MEMORY_E;
- }
- }
- #endif
- x509->basicConstSet = dCert->extBasicConstSet;
- x509->basicConstCrit = dCert->extBasicConstCrit;
- x509->basicConstPlSet = dCert->pathLengthSet;
- x509->subjAltNameSet = dCert->extSubjAltNameSet;
- x509->subjAltNameCrit = dCert->extSubjAltNameCrit;
- x509->authKeyIdSet = dCert->extAuthKeyIdSet;
- x509->authKeyIdCrit = dCert->extAuthKeyIdCrit;
- if (dCert->extAuthKeyIdSrc != NULL && dCert->extAuthKeyIdSz != 0) {
- x509->authKeyId = (byte*)XMALLOC(dCert->extAuthKeyIdSz, x509->heap,
- DYNAMIC_TYPE_X509_EXT);
- if (x509->authKeyId != NULL) {
- XMEMCPY(x509->authKeyId,
- dCert->extAuthKeyIdSrc, dCert->extAuthKeyIdSz);
- x509->authKeyIdSz = dCert->extAuthKeyIdSz;
- }
- else
- ret = MEMORY_E;
- }
- x509->subjKeyIdSet = dCert->extSubjKeyIdSet;
- x509->subjKeyIdCrit = dCert->extSubjKeyIdCrit;
- if (dCert->extSubjKeyIdSrc != NULL && dCert->extSubjKeyIdSz != 0) {
- x509->subjKeyId = (byte*)XMALLOC(dCert->extSubjKeyIdSz, x509->heap,
- DYNAMIC_TYPE_X509_EXT);
- if (x509->subjKeyId != NULL) {
- XMEMCPY(x509->subjKeyId,
- dCert->extSubjKeyIdSrc, dCert->extSubjKeyIdSz);
- x509->subjKeyIdSz = dCert->extSubjKeyIdSz;
- }
- else
- ret = MEMORY_E;
- }
- x509->keyUsageSet = dCert->extKeyUsageSet;
- x509->keyUsageCrit = dCert->extKeyUsageCrit;
- if (dCert->extExtKeyUsageSrc != NULL && dCert->extExtKeyUsageSz > 0) {
- x509->extKeyUsageSrc = (byte*)XMALLOC(dCert->extExtKeyUsageSz,
- x509->heap, DYNAMIC_TYPE_X509_EXT);
- if (x509->extKeyUsageSrc != NULL) {
- XMEMCPY(x509->extKeyUsageSrc, dCert->extExtKeyUsageSrc,
- dCert->extExtKeyUsageSz);
- x509->extKeyUsageSz = dCert->extExtKeyUsageSz;
- x509->extKeyUsageCrit = dCert->extExtKeyUsageCrit;
- x509->extKeyUsageCount = dCert->extExtKeyUsageCount;
- }
- else {
- ret = MEMORY_E;
- }
- }
- #if defined(WOLFSSL_SEP) || defined(WOLFSSL_QT)
- x509->certPolicySet = dCert->extCertPolicySet;
- x509->certPolicyCrit = dCert->extCertPolicyCrit;
- #endif /* WOLFSSL_SEP || WOLFSSL_QT */
- #ifdef WOLFSSL_CERT_EXT
- {
- int i;
- for (i = 0; i < dCert->extCertPoliciesNb && i < MAX_CERTPOL_NB; i++)
- XMEMCPY(x509->certPolicies[i], dCert->extCertPolicies[i],
- MAX_CERTPOL_SZ);
- x509->certPoliciesNb = dCert->extCertPoliciesNb;
- }
- #endif /* WOLFSSL_CERT_EXT */
-#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
-#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
- x509->pkCurveOID = dCert->pkCurveOID;
-#endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-
- return ret;
-}
-
-#endif /* KEEP_PEER_CERT || SESSION_CERTS */
-
-#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
- (defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && !defined(WOLFSSL_NO_TLS12))
-static int ProcessCSR(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- word32 status_length)
-{
- int ret = 0;
- OcspRequest* request;
-
- #ifdef WOLFSSL_SMALL_STACK
- CertStatus* status;
- OcspResponse* response;
- #else
- CertStatus status[1];
- OcspResponse response[1];
- #endif
-
- do {
- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
- if (ssl->status_request) {
- request = (OcspRequest*)TLSX_CSR_GetRequest(ssl->extensions);
- ssl->status_request = 0;
- break;
- }
- #endif
-
- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
- if (ssl->status_request_v2) {
- request = (OcspRequest*)TLSX_CSR2_GetRequest(ssl->extensions,
- WOLFSSL_CSR2_OCSP, 0);
- ssl->status_request_v2 = 0;
- break;
- }
- #endif
-
- return BUFFER_ERROR;
- } while(0);
-
- if (request == NULL)
- return BAD_CERTIFICATE_STATUS_ERROR; /* not expected */
-
- #ifdef WOLFSSL_SMALL_STACK
- status = (CertStatus*)XMALLOC(sizeof(CertStatus), ssl->heap,
- DYNAMIC_TYPE_OCSP_STATUS);
- response = (OcspResponse*)XMALLOC(sizeof(OcspResponse), ssl->heap,
- DYNAMIC_TYPE_OCSP_REQUEST);
-
- if (status == NULL || response == NULL) {
- if (status)
- XFREE(status, NULL, DYNAMIC_TYPE_OCSP_STATUS);
- if (response)
- XFREE(response, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
-
- return MEMORY_ERROR;
- }
- #endif
-
- InitOcspResponse(response, status, input +*inOutIdx, status_length);
-
- if (OcspResponseDecode(response, ssl->ctx->cm, ssl->heap, 0) != 0)
- ret = BAD_CERTIFICATE_STATUS_ERROR;
- else if (CompareOcspReqResp(request, response) != 0)
- ret = BAD_CERTIFICATE_STATUS_ERROR;
- else if (response->responseStatus != OCSP_SUCCESSFUL)
- ret = BAD_CERTIFICATE_STATUS_ERROR;
- else if (response->status->status == CERT_REVOKED)
- ret = OCSP_CERT_REVOKED;
- else if (response->status->status != CERT_GOOD)
- ret = BAD_CERTIFICATE_STATUS_ERROR;
-
- *inOutIdx += status_length;
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(status, ssl->heap, DYNAMIC_TYPE_OCSP_STATUS);
- XFREE(response, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- #endif
-
- return ret;
-}
-#endif
-
-
-
-#ifdef HAVE_PK_CALLBACKS
-
-#ifdef HAVE_ECC
- static int SigPkCbEccVerify(const unsigned char* sig, unsigned int sigSz,
- const unsigned char* hash, unsigned int hashSz,
- const unsigned char* keyDer, unsigned int keySz,
- int* result, void* ctx)
- {
- int ret = NOT_COMPILED_IN;
- WOLFSSL* ssl = (WOLFSSL*)ctx;
-
- if (ssl && ssl->ctx->EccVerifyCb) {
- ret = ssl->ctx->EccVerifyCb(ssl, sig, sigSz, hash, hashSz,
- keyDer, keySz, result, ssl->EccVerifyCtx);
- }
- return ret;
- }
-#endif
-#ifndef NO_RSA
- static int SigPkCbRsaVerify(unsigned char* sig, unsigned int sigSz,
- unsigned char** out, const unsigned char* keyDer, unsigned int keySz,
- void* ctx)
- {
- int ret = NOT_COMPILED_IN;
- WOLFSSL* ssl = (WOLFSSL*)ctx;
-
- if (ssl && ssl->ctx->RsaVerifyCb) {
- ret = ssl->ctx->RsaVerifyCb(ssl, sig, sigSz, out, keyDer, keySz,
- ssl->RsaVerifyCtx);
- }
- return ret;
- }
-#endif
-
-int InitSigPkCb(WOLFSSL* ssl, SignatureCtx* sigCtx)
-{
- if (ssl == NULL || sigCtx == NULL)
- return BAD_FUNC_ARG;
-
- /* only setup the verify callback if a PK is set */
-#ifdef HAVE_ECC
- if (ssl->ctx->EccVerifyCb) {
- sigCtx->pkCbEcc = SigPkCbEccVerify;
- sigCtx->pkCtxEcc = ssl;
- }
-#endif
-#ifndef NO_RSA
- /* only setup the verify callback if a PK is set */
- if (ssl->ctx->RsaVerifyCb) {
- sigCtx->pkCbRsa = SigPkCbRsaVerify;
- sigCtx->pkCtxRsa = ssl;
- }
-#endif
-
- return 0;
-}
-
-#endif /* HAVE_PK_CALLBACKS */
-
-
-#if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
-static void DoCertFatalAlert(WOLFSSL* ssl, int ret)
-{
- int alertWhy;
- if (ssl == NULL || ret == 0) {
- return;
- }
-
- /* Determine alert reason */
- alertWhy = bad_certificate;
- if (ret == ASN_AFTER_DATE_E || ret == ASN_BEFORE_DATE_E) {
- alertWhy = certificate_expired;
- }
-#if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
- else if (ret == CRL_CERT_REVOKED) {
- alertWhy = certificate_revoked;
- }
-#endif
- else if (ret == NO_PEER_CERT) {
-#ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3) {
- alertWhy = certificate_required;
- }
- else
-#endif
- {
- alertWhy = handshake_failure;
- }
- }
-
- /* send fatal alert and mark connection closed */
- SendAlert(ssl, alert_fatal, alertWhy); /* try to send */
- ssl->options.isClosed = 1;
-}
-
-/* WOLFSSL_ALWAYS_VERIFY_CB: Use verify callback for success or failure cases */
-/* WOLFSSL_VERIFY_CB_ALL_CERTS: Issue callback for all intermediate certificates */
-
-/* Callback is issued for certificate presented in TLS Certificate (11) packet.
- * The intermediates are done first then peer leaf cert last. Use the
- * store->error_depth member to determine index (0=peer, >1 intermediates)
- */
-
-int DoVerifyCallback(WOLFSSL_CERT_MANAGER* cm, WOLFSSL* ssl, int ret,
- ProcPeerCertArgs* args)
-{
- int verify_ok = 0, use_cb = 0;
- void *heap = (ssl != NULL) ? ssl->heap : cm->heap;
-
- /* Determine if verify was okay */
- if (ret == 0) {
- verify_ok = 1;
- }
-
- /* Determine if verify callback should be used */
- if (ret != 0) {
- if ((ssl != NULL) && (!ssl->options.verifyNone)) {
- use_cb = 1; /* always report errors */
- }
- }
-#ifdef WOLFSSL_ALWAYS_VERIFY_CB
- /* always use verify callback on peer leaf cert */
- if (args->certIdx == 0) {
- use_cb = 1;
- }
-#endif
-#ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
- /* perform verify callback on other intermediate certs (not just peer) */
- if (args->certIdx > 0) {
- use_cb = 1;
- }
-#endif
-#if defined(OPENSSL_EXTRA)
- /* perform domain name check on the peer certificate */
- if (args->dCertInit && args->dCert && (ssl != NULL) &&
- ssl->param && ssl->param->hostName[0]) {
- /* If altNames names is present, then subject common name is ignored */
- if (args->dCert->altNames != NULL) {
- if (CheckAltNames(args->dCert, ssl->param->hostName) == 0 ) {
- if (ret == 0) {
- ret = DOMAIN_NAME_MISMATCH;
- }
- }
- }
- else {
- if (args->dCert->subjectCN) {
- if (MatchDomainName(args->dCert->subjectCN,
- args->dCert->subjectCNLen,
- ssl->param->hostName) == 0) {
- if (ret == 0) {
- ret = DOMAIN_NAME_MISMATCH;
- }
- }
- }
- }
- }
-
- /* perform IP address check on the peer certificate */
- if ((args->dCertInit != 0) && (args->dCert != NULL) && (ssl != NULL) &&
- (ssl->param != NULL) && (XSTRLEN(ssl->param->ipasc) > 0)) {
- if (CheckIPAddr(args->dCert, ssl->param->ipasc) != 0) {
- if (ret == 0) {
- ret = IPADDR_MISMATCH;
- }
- }
- }
-#endif
- /* if verify callback has been set */
- if ((use_cb && (ssl != NULL) && ((ssl->verifyCallback != NULL)
- #ifdef OPENSSL_ALL
- || (ssl->ctx->verifyCertCb != NULL)
- #endif
- ))
- #ifndef NO_WOLFSSL_CM_VERIFY
- || (cm->verifyCallback != NULL)
- #endif
- ) {
- int verifyFail = 0;
- #ifdef WOLFSSL_SMALL_STACK
- WOLFSSL_X509_STORE_CTX* store;
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- WOLFSSL_X509* x509;
- #endif
- char* domain = NULL;
- #else
- WOLFSSL_X509_STORE_CTX store[1];
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- WOLFSSL_X509 x509[1];
- #endif
- char domain[ASN_NAME_MAX];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- store = (WOLFSSL_X509_STORE_CTX*)XMALLOC(
- sizeof(WOLFSSL_X509_STORE_CTX), heap, DYNAMIC_TYPE_X509_STORE);
- if (store == NULL) {
- return MEMORY_E;
- }
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), heap,
- DYNAMIC_TYPE_X509);
- if (x509 == NULL) {
- XFREE(store, heap, DYNAMIC_TYPE_X509);
- return MEMORY_E;
- }
- #endif
- domain = (char*)XMALLOC(ASN_NAME_MAX, heap, DYNAMIC_TYPE_STRING);
- if (domain == NULL) {
- XFREE(store, heap, DYNAMIC_TYPE_X509);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- XFREE(x509, heap, DYNAMIC_TYPE_X509);
- #endif
- return MEMORY_E;
- }
- #endif /* WOLFSSL_SMALL_STACK */
-
- XMEMSET(store, 0, sizeof(WOLFSSL_X509_STORE_CTX));
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- XMEMSET(x509, 0, sizeof(WOLFSSL_X509));
- #endif
- domain[0] = '\0';
-
- /* build subject CN as string to return in store */
- if (args->dCertInit && args->dCert && args->dCert->subjectCN) {
- int subjectCNLen = args->dCert->subjectCNLen;
- if (subjectCNLen > ASN_NAME_MAX-1)
- subjectCNLen = ASN_NAME_MAX-1;
- if (subjectCNLen > 0) {
- XMEMCPY(domain, args->dCert->subjectCN, subjectCNLen);
- domain[subjectCNLen] = '\0';
- }
- }
-
- store->error = ret;
- store->error_depth = args->certIdx;
- store->discardSessionCerts = 0;
- store->domain = domain;
- store->userCtx = (ssl != NULL) ? ssl->verifyCbCtx : cm;
- store->certs = args->certs;
- store->totalCerts = args->totalCerts;
- #if defined(HAVE_EX_DATA) || defined(FORTRESS)
- if (wolfSSL_CRYPTO_set_ex_data(&store->ex_data, 0, ssl)
- != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Failed to store ssl context in WOLFSSL_X509_STORE_CTX");
- }
- #endif
-
- if (ssl != NULL) {
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- if (ssl->ctx->x509_store_pt != NULL) {
- store->store = ssl->ctx->x509_store_pt;
- }
- else {
- store->store = &ssl->ctx->x509_store;
- }
- #if defined(OPENSSL_EXTRA)
- store->depth = args->count;
- store->param = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
- sizeof(WOLFSSL_X509_VERIFY_PARAM),
- heap, DYNAMIC_TYPE_OPENSSL);
- if (store->param == NULL) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(domain, heap, DYNAMIC_TYPE_STRING);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- XFREE(x509, heap, DYNAMIC_TYPE_X509);
- #endif
- XFREE(store, heap, DYNAMIC_TYPE_X509_STORE);
- #endif
- return MEMORY_E;
- }
- XMEMSET(store->param, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
- /* Overwrite with non-default param values in SSL */
- if (ssl->param) {
- if (ssl->param->check_time)
- store->param->check_time = ssl->param->check_time;
-
- if (ssl->param->flags)
- store->param->flags = ssl->param->flags;
-
- if (ssl->param->hostName[0])
- XMEMCPY(store->param->hostName, ssl->param->hostName,
- WOLFSSL_HOST_NAME_MAX);
-
- }
- #endif /* defined(OPENSSL_EXTRA) */
- #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)*/
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- #ifdef KEEP_PEER_CERT
- if (args->certIdx == 0) {
- store->current_cert = &ssl->peerCert; /* use existing X509 */
- }
- else
- #endif
- {
- InitX509(x509, 0, heap);
- if (CopyDecodedToX509(x509, args->dCert) == 0) {
- store->current_cert = x509;
- }
- else {
- FreeX509(x509);
- }
- }
- #endif
- #ifdef SESSION_CERTS
- store->sesChain = &ssl->session.chain;
- #endif
- }
- #ifndef NO_WOLFSSL_CM_VERIFY
- /* non-zero return code indicates failure override */
- if ((cm != NULL) && (cm->verifyCallback != NULL)) {
- store->userCtx = cm;
- if (cm->verifyCallback(verify_ok, store)) {
- if (ret != 0) {
- WOLFSSL_MSG("Verify CM callback overriding error!");
- ret = 0;
- }
- }
- else {
- verifyFail = 1;
- }
- }
- #endif
-
- if (ssl != NULL) {
- #ifdef OPENSSL_ALL
- /* non-zero return code indicates failure override */
- if (ssl->ctx->verifyCertCb) {
- if (ssl->ctx->verifyCertCb(store, ssl->ctx->verifyCertCbArg)) {
- if (ret != 0) {
- WOLFSSL_MSG("Verify Cert callback overriding error!");
- ret = 0;
- }
- }
- else {
- verifyFail = 1;
- }
- }
- #endif
-
- /* non-zero return code indicates failure override */
- if (ssl->verifyCallback) {
- if (ssl->verifyCallback(verify_ok, store)) {
- if (ret != 0) {
- WOLFSSL_MSG("Verify callback overriding error!");
- ret = 0;
- }
- }
- else {
- verifyFail = 1;
- }
- }
- }
-
- if (verifyFail) {
- /* induce error if one not present */
- if (ret == 0) {
- ret = VERIFY_CERT_ERROR;
- }
-
- /* mark as verify error */
- args->verifyErr = 1;
- }
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- if (args->certIdx > 0)
- FreeX509(x509);
- #endif
- #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
- wolfSSL_sk_X509_free(store->chain);
- store->chain = NULL;
- #endif
- #ifdef SESSION_CERTS
- if ((ssl != NULL) && (store->discardSessionCerts)) {
- WOLFSSL_MSG("Verify callback requested discard sess certs");
- ssl->session.chain.count = 0;
- #ifdef WOLFSSL_ALT_CERT_CHAINS
- ssl->session.altChain.count = 0;
- #endif
- }
- #endif /* SESSION_CERTS */
-#ifdef OPENSSL_EXTRA
- if ((ssl != NULL) && (store->param)) {
- XFREE(store->param, heap, DYNAMIC_TYPE_OPENSSL);
- }
-#endif
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(domain, heap, DYNAMIC_TYPE_STRING);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- XFREE(x509, heap, DYNAMIC_TYPE_X509);
- #endif
- XFREE(store, heap, DYNAMIC_TYPE_X509_STORE);
- #endif
- }
-
- (void)heap;
-
- return ret;
-}
-
-static void FreeProcPeerCertArgs(WOLFSSL* ssl, void* pArgs)
-{
- ProcPeerCertArgs* args = (ProcPeerCertArgs*)pArgs;
-
- (void)ssl;
-
- if (args->certs) {
- XFREE(args->certs, ssl->heap, DYNAMIC_TYPE_DER);
- args->certs = NULL;
- }
-#ifdef WOLFSSL_TLS13
- if (args->exts) {
- XFREE(args->exts, ssl->heap, DYNAMIC_TYPE_CERT_EXT);
- args->exts = NULL;
- }
-#endif
- if (args->dCert) {
- if (args->dCertInit) {
- FreeDecodedCert(args->dCert);
- args->dCertInit = 0;
- }
- XFREE(args->dCert, ssl->heap, DYNAMIC_TYPE_DCERT);
- args->dCert = NULL;
- }
-}
-
-static int ProcessPeerCertParse(WOLFSSL* ssl, ProcPeerCertArgs* args,
- int certType, int verify, byte** pSubjectHash, int* pAlreadySigner)
-{
- int ret = 0;
- buffer* cert;
- byte* subjectHash = NULL;
- int alreadySigner = 0;
-#ifdef WOLFSSL_SMALL_CERT_VERIFY
- int sigRet = 0;
-#endif
-
- if (ssl == NULL || args == NULL)
- return BAD_FUNC_ARG;
-
- /* check to make sure certificate index is valid */
- if (args->certIdx > args->count)
- return BUFFER_E;
-
- /* check if returning from non-blocking OCSP */
- /* skip this section because cert is already initialized and parsed */
-#ifdef WOLFSSL_NONBLOCK_OCSP
- if (args->lastErr == OCSP_WANT_READ) {
- args->lastErr = 0; /* clear error */
- return 0;
- }
-#endif
-
-#ifdef WOLFSSL_TRUST_PEER_CERT
- /* we have trusted peer */
- if (args->haveTrustPeer) {
- return 0;
- }
-#endif
-
- /* get certificate buffer */
- cert = &args->certs[args->certIdx];
-
-#ifdef WOLFSSL_SMALL_CERT_VERIFY
- if (verify == VERIFY) {
- /* for small cert verify, release decoded cert during signature check to
- reduce peak memory usage */
- if (args->dCert != NULL) {
- if (args->dCertInit) {
- FreeDecodedCert(args->dCert);
- args->dCertInit = 0;
- }
- XFREE(args->dCert, ssl->heap, DYNAMIC_TYPE_DCERT);
- args->dCert = NULL;
- }
-
- /* perform cert parsing and signature check */
- sigRet = CheckCertSignature(cert->buffer, cert->length,
- ssl->heap, ssl->ctx->cm);
- /* fail on errors here after the ParseCertRelative call, so dCert is populated */
-
- /* verify name only in ParseCertRelative below, signature check done */
- verify = VERIFY_NAME;
- }
-#endif /* WOLFSSL_SMALL_CERT_VERIFY */
-
- /* make sure the decoded cert structure is allocated and initialized */
- if (!args->dCertInit
- #ifdef WOLFSSL_SMALL_CERT_VERIFY
- || args->dCert == NULL
- #endif
- ) {
- #ifdef WOLFSSL_SMALL_CERT_VERIFY
- if (args->dCert == NULL) {
- args->dCert = (DecodedCert*)XMALLOC(
- sizeof(DecodedCert), ssl->heap,
- DYNAMIC_TYPE_DCERT);
- if (args->dCert == NULL) {
- return MEMORY_E;
- }
- }
- #endif
-
- InitDecodedCert(args->dCert, cert->buffer, cert->length, ssl->heap);
-
- args->dCertInit = 1;
- args->dCert->sigCtx.devId = ssl->devId;
- #ifdef WOLFSSL_ASYNC_CRYPT
- args->dCert->sigCtx.asyncCtx = ssl;
- #endif
-
- #ifdef HAVE_PK_CALLBACKS
- /* setup the PK callback context */
- ret = InitSigPkCb(ssl, &args->dCert->sigCtx);
- if (ret != 0)
- return ret;
- #endif
- }
-
- /* Parse Certificate */
- ret = ParseCertRelative(args->dCert, certType, verify, ssl->ctx->cm);
- /* perform below checks for date failure cases */
- if (ret == 0 || ret == ASN_BEFORE_DATE_E || ret == ASN_AFTER_DATE_E) {
- /* get subject and determine if already loaded */
- #ifndef NO_SKID
- if (args->dCert->extAuthKeyIdSet)
- subjectHash = args->dCert->extSubjKeyId;
- else
- #endif
- subjectHash = args->dCert->subjectHash;
- alreadySigner = AlreadySigner(ssl->ctx->cm, subjectHash);
- }
-
-#ifdef WOLFSSL_SMALL_CERT_VERIFY
- /* get signature check failures from above */
- if (ret == 0)
- ret = sigRet;
-#endif
-
- if (pSubjectHash)
- *pSubjectHash = subjectHash;
- if (pAlreadySigner)
- *pAlreadySigner = alreadySigner;
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl,
- args->dCert->sigCtx.asyncDev);
- }
-#endif
-
- return ret;
-}
-
-/* Check key sizes for certs. Is redundant check since
- ProcessBuffer also performs this check. */
-static int ProcessPeerCertCheckKey(WOLFSSL* ssl, ProcPeerCertArgs* args)
-{
- int ret = 0;
-
- if (ssl->options.verifyNone) {
- return ret;
- }
-
- switch (args->dCert->keyOID) {
- #ifndef NO_RSA
- case RSAk:
- if (ssl->options.minRsaKeySz < 0 ||
- args->dCert->pubKeySize <
- (word16)ssl->options.minRsaKeySz) {
- WOLFSSL_MSG(
- "RSA key size in cert chain error");
- ret = RSA_KEY_SIZE_E;
- }
- break;
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ECDSAk:
- if (ssl->options.minEccKeySz < 0 ||
- args->dCert->pubKeySize <
- (word16)ssl->options.minEccKeySz) {
- WOLFSSL_MSG(
- "ECC key size in cert chain error");
- ret = ECC_KEY_SIZE_E;
- }
- break;
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ED25519k:
- if (ssl->options.minEccKeySz < 0 ||
- ED25519_KEY_SIZE < (word16)ssl->options.minEccKeySz) {
- WOLFSSL_MSG(
- "ECC key size in cert chain error");
- ret = ECC_KEY_SIZE_E;
- }
- break;
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- case ED448k:
- if (ssl->options.minEccKeySz < 0 ||
- ED448_KEY_SIZE < (word16)ssl->options.minEccKeySz) {
- WOLFSSL_MSG(
- "ECC key size in cert chain error");
- ret = ECC_KEY_SIZE_E;
- }
- break;
- #endif /* HAVE_ED448 */
- default:
- WOLFSSL_MSG("Key size not checked");
- /* key not being checked for size if not in
- switch */
- break;
- }
-
- return ret;
-}
-
-int ProcessPeerCerts(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- word32 totalSz)
-{
- int ret = 0;
-#ifdef WOLFSSL_ASYNC_CRYPT
- ProcPeerCertArgs* args = (ProcPeerCertArgs*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
-#elif defined(WOLFSSL_NONBLOCK_OCSP)
- ProcPeerCertArgs* args = ssl->nonblockarg;
-#elif defined(WOLFSSL_SMALL_STACK)
- ProcPeerCertArgs* args = NULL;
-#else
- ProcPeerCertArgs args[1];
-#endif
- byte* subjectHash = NULL;
- int alreadySigner = 0;
-
- WOLFSSL_ENTER("ProcessPeerCerts");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_ppc;
- }
- else
-#elif defined(WOLFSSL_NONBLOCK_OCSP)
- if (args == NULL) {
- args = (ProcPeerCertArgs*)XMALLOC(
- sizeof(ProcPeerCertArgs), ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (args == NULL) {
- ERROR_OUT(MEMORY_E, exit_ppc);
- }
- }
- if (ssl->nonblockarg == NULL) /* new args */
-#elif defined(WOLFSSL_SMALL_STACK)
- args = (ProcPeerCertArgs*)XMALLOC(
- sizeof(ProcPeerCertArgs), ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (args == NULL) {
- ERROR_OUT(MEMORY_E, exit_ppc);
- }
-#endif
- {
- /* Reset state */
- ret = 0;
- ssl->options.asyncState = TLS_ASYNC_BEGIN;
- XMEMSET(args, 0, sizeof(ProcPeerCertArgs));
- args->idx = *inOutIdx;
- args->begin = *inOutIdx;
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeProcPeerCertArgs;
- #elif defined(WOLFSSL_NONBLOCK_OCSP)
- ssl->nonblockarg = args;
- #endif
- }
-
- switch (ssl->options.asyncState)
- {
- case TLS_ASYNC_BEGIN:
- {
- word32 listSz;
-
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "Certificate");
- if (ssl->toInfoOn)
- AddLateName("Certificate", &ssl->timeoutInfo);
- #endif
-
- #ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3) {
- byte ctxSz;
-
- /* Certificate Request Context */
- if ((args->idx - args->begin) + OPAQUE8_LEN > totalSz)
- ERROR_OUT(BUFFER_ERROR, exit_ppc);
- ctxSz = *(input + args->idx);
- args->idx++;
- if ((args->idx - args->begin) + ctxSz > totalSz)
- ERROR_OUT(BUFFER_ERROR, exit_ppc);
- #ifndef NO_WOLFSSL_CLIENT
- /* Must be empty when received from server. */
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if (ctxSz != 0) {
- ERROR_OUT(INVALID_CERT_CTX_E, exit_ppc);
- }
- }
- #endif
- #ifndef NO_WOLFSSL_SERVER
- /* Must contain value sent in request. */
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- if (ssl->options.handShakeState != HANDSHAKE_DONE &&
- ctxSz != 0) {
- ERROR_OUT(INVALID_CERT_CTX_E, exit_ppc);
- }
- else if (ssl->options.handShakeState == HANDSHAKE_DONE) {
- #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- CertReqCtx* curr = ssl->certReqCtx;
- CertReqCtx* prev = NULL;
- while (curr != NULL) {
- if ((ctxSz == curr->len) &&
- XMEMCMP(&curr->ctx, input + args->idx, ctxSz)
- == 0) {
- if (prev != NULL)
- prev->next = curr->next;
- else
- ssl->certReqCtx = curr->next;
- XFREE(curr, ssl->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- break;
- }
- prev = curr;
- curr = curr->next;
- }
- if (curr == NULL)
- #endif
- ERROR_OUT(INVALID_CERT_CTX_E, exit_ppc);
- }
- }
- #endif
- args->idx += ctxSz;
-
- #ifdef OPENSSL_EXTRA
- /* allocate buffer for cert extensions */
- args->exts = (buffer*)XMALLOC(sizeof(buffer) *
- (ssl->verifyDepth + 1), ssl->heap, DYNAMIC_TYPE_CERT_EXT);
- if (args->exts == NULL) {
- ERROR_OUT(MEMORY_E, exit_ppc);
- }
- #else
- /* allocate buffer for cert extensions */
- args->exts = (buffer*)XMALLOC(sizeof(buffer) * MAX_CHAIN_DEPTH,
- ssl->heap, DYNAMIC_TYPE_CERT_EXT);
- if (args->exts == NULL) {
- ERROR_OUT(MEMORY_E, exit_ppc);
- }
- #endif
- }
- #endif
-
- /* allocate buffer for certs */
- #ifdef OPENSSL_EXTRA
- args->certs = (buffer*)XMALLOC(sizeof(buffer) *
- (ssl->verifyDepth + 1), ssl->heap, DYNAMIC_TYPE_DER);
- if (args->certs == NULL) {
- ERROR_OUT(MEMORY_E, exit_ppc);
- }
- XMEMSET(args->certs, 0, sizeof(buffer) * (ssl->verifyDepth + 1));
- #else
- args->certs = (buffer*)XMALLOC(sizeof(buffer) * MAX_CHAIN_DEPTH,
- ssl->heap, DYNAMIC_TYPE_DER);
- if (args->certs == NULL) {
- ERROR_OUT(MEMORY_E, exit_ppc);
- }
- XMEMSET(args->certs, 0, sizeof(buffer) * MAX_CHAIN_DEPTH);
- #endif /* OPENSSL_EXTRA */
-
- /* Certificate List */
- if ((args->idx - args->begin) + OPAQUE24_LEN > totalSz) {
- ERROR_OUT(BUFFER_ERROR, exit_ppc);
- }
- c24to32(input + args->idx, &listSz);
- args->idx += OPAQUE24_LEN;
- if (listSz > MAX_CERTIFICATE_SZ) {
- ERROR_OUT(BUFFER_ERROR, exit_ppc);
- }
- if ((args->idx - args->begin) + listSz != totalSz) {
- ERROR_OUT(BUFFER_ERROR, exit_ppc);
- }
-
- WOLFSSL_MSG("Loading peer's cert chain");
- /* first put cert chain into buffer so can verify top down
- we're sent bottom up */
- while (listSz) {
- word32 certSz;
-
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- if (args->totalCerts > ssl->verifyDepth) {
- ssl->peerVerifyRet = X509_V_ERR_CERT_CHAIN_TOO_LONG;
- ERROR_OUT(MAX_CHAIN_ERROR, exit_ppc);
- }
- #else
- if (args->totalCerts >= ssl->verifyDepth ||
- args->totalCerts >= MAX_CHAIN_DEPTH) {
- ERROR_OUT(MAX_CHAIN_ERROR, exit_ppc);
- }
- #endif
-
- if ((args->idx - args->begin) + OPAQUE24_LEN > totalSz) {
- ERROR_OUT(BUFFER_ERROR, exit_ppc);
- }
-
- c24to32(input + args->idx, &certSz);
- args->idx += OPAQUE24_LEN;
-
- if ((args->idx - args->begin) + certSz > totalSz) {
- ERROR_OUT(BUFFER_ERROR, exit_ppc);
- }
-
- args->certs[args->totalCerts].length = certSz;
- args->certs[args->totalCerts].buffer = input + args->idx;
-
- #ifdef SESSION_CERTS
- AddSessionCertToChain(&ssl->session.chain,
- input + args->idx, certSz);
- #endif /* SESSION_CERTS */
-
- args->idx += certSz;
- listSz -= certSz + CERT_HEADER_SZ;
-
- #ifdef WOLFSSL_TLS13
- /* Extensions */
- if (ssl->options.tls1_3) {
- word16 extSz;
-
- if ((args->idx - args->begin) + OPAQUE16_LEN > totalSz) {
- ERROR_OUT(BUFFER_ERROR, exit_ppc);
- }
- ato16(input + args->idx, &extSz);
- args->idx += OPAQUE16_LEN;
- if ((args->idx - args->begin) + extSz > totalSz) {
- ERROR_OUT(BUFFER_ERROR, exit_ppc);
- }
- /* Store extension data info for later processing. */
- args->exts[args->totalCerts].length = extSz;
- args->exts[args->totalCerts].buffer = input + args->idx;
- args->idx += extSz;
- listSz -= extSz + OPAQUE16_LEN;
- ret = TLSX_Parse(ssl, args->exts[args->totalCerts].buffer,
- args->exts[args->totalCerts].length, certificate, NULL);
- if (ret < 0) {
- ERROR_OUT(ret, exit_ppc);
- }
- }
- #endif
-
- args->totalCerts++;
- WOLFSSL_MSG("\tPut another cert into chain");
- } /* while (listSz) */
-
- args->count = args->totalCerts;
- args->certIdx = 0; /* select peer cert (first one) */
-
- if (args->count == 0 && ssl->options.mutualAuth &&
- ssl->options.side == WOLFSSL_SERVER_END) {
- ret = NO_PEER_CERT;
- DoCertFatalAlert(ssl, ret);
- }
-
- args->dCertInit = 0;
- #ifndef WOLFSSL_SMALL_CERT_VERIFY
- args->dCert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap,
- DYNAMIC_TYPE_DCERT);
- if (args->dCert == NULL) {
- ERROR_OUT(MEMORY_E, exit_ppc);
- }
- XMEMSET(args->dCert, 0, sizeof(DecodedCert));
- #endif
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_BUILD;
- } /* case TLS_ASYNC_BEGIN */
- FALL_THROUGH;
-
- case TLS_ASYNC_BUILD:
- {
- if (args->count > 0) {
-
- /* check for trusted peer and get untrustedDepth */
- #if defined(WOLFSSL_TRUST_PEER_CERT) || defined(OPENSSL_EXTRA)
- if (args->certIdx == 0) {
- #ifdef WOLFSSL_TRUST_PEER_CERT
- TrustedPeerCert* tp;
- int matchType = WC_MATCH_NAME;
- #endif
-
- ret = ProcessPeerCertParse(ssl, args, CERT_TYPE, NO_VERIFY,
- &subjectHash, &alreadySigner);
- if (ret != 0)
- goto exit_ppc;
-
- #ifdef OPENSSL_EXTRA
- /* Determine untrusted depth */
- if (!alreadySigner && (!args->dCert ||
- !args->dCertInit || !args->dCert->selfSigned)) {
- args->untrustedDepth = 1;
- }
- #endif
-
- #ifdef WOLFSSL_TRUST_PEER_CERT
- #ifndef NO_SKID
- if (args->dCert->extAuthKeyIdSet)
- matchType = WC_MATCH_SKID;
- #endif
- tp = GetTrustedPeer(ssl->ctx->cm, subjectHash, matchType);
- WOLFSSL_MSG("Checking for trusted peer cert");
-
- if (tp && MatchTrustedPeer(tp, args->dCert)) {
- WOLFSSL_MSG("Found matching trusted peer cert");
- args->haveTrustPeer = 1;
- }
- else if (tp == NULL) {
- /* no trusted peer cert */
- WOLFSSL_MSG("No matching trusted peer cert. Checking CAs");
- }
- else {
- WOLFSSL_MSG("Trusted peer cert did not match!");
- }
- if (!args->haveTrustPeer)
- #endif
- {
- /* free cert if not trusted peer */
- FreeDecodedCert(args->dCert);
- args->dCertInit = 0;
- }
- }
- #endif /* WOLFSSL_TRUST_PEER_CERT || OPENSSL_EXTRA */
-
- /* check certificate up to peer's first */
- /* do not verify chain if trusted peer cert found */
- while (args->count > 1
- #ifdef WOLFSSL_TRUST_PEER_CERT
- && !args->haveTrustPeer
- #endif /* WOLFSSL_TRUST_PEER_CERT */
- ) {
- int skipAddCA = 0;
-
- /* select last certificate */
- args->certIdx = args->count - 1;
-
- ret = ProcessPeerCertParse(ssl, args, CERT_TYPE,
- !ssl->options.verifyNone ? VERIFY : NO_VERIFY,
- &subjectHash, &alreadySigner);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- goto exit_ppc;
- #endif
- if (ret == 0) {
- ret = ProcessPeerCertCheckKey(ssl, args);
- }
-
- if (ret == 0 && args->dCert->isCA == 0) {
- WOLFSSL_MSG("Chain cert is not a CA, not adding as one");
- }
- else if (ret == 0 && ssl->options.verifyNone) {
- WOLFSSL_MSG("Chain cert not verified by option, "
- "not adding as CA");
- }
- else if (ret == 0) {
- #ifdef OPENSSL_EXTRA
- if (args->certIdx > args->untrustedDepth) {
- args->untrustedDepth = (char)args->certIdx + 1;
- }
- #endif
-
- if (alreadySigner) {
- WOLFSSL_MSG("Verified CA from chain and already had it");
- }
- }
- else {
- WOLFSSL_MSG("Failed to verify CA from chain");
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- ssl->peerVerifyRet = X509_V_ERR_INVALID_CA;
- #endif
- }
-
- #if defined(HAVE_OCSP) || defined(HAVE_CRL)
- if (ret == 0) {
- int doCrlLookup = 1;
- #ifdef HAVE_OCSP
- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
- if (ssl->status_request_v2) {
- ret = TLSX_CSR2_InitRequests(ssl->extensions,
- args->dCert, 0, ssl->heap);
- }
- else /* skips OCSP and force CRL check */
- #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
- if (ssl->ctx->cm->ocspEnabled &&
- ssl->ctx->cm->ocspCheckAll) {
- WOLFSSL_MSG("Doing Non Leaf OCSP check");
- ret = CheckCertOCSP_ex(ssl->ctx->cm->ocsp,
- args->dCert, NULL, ssl);
- #ifdef WOLFSSL_NONBLOCK_OCSP
- if (ret == OCSP_WANT_READ) {
- args->lastErr = ret;
- goto exit_ppc;
- }
- #endif
- doCrlLookup = (ret == OCSP_CERT_UNKNOWN);
- if (ret != 0) {
- doCrlLookup = 0;
- WOLFSSL_MSG("\tOCSP Lookup not ok");
- }
- }
- #endif /* HAVE_OCSP */
-
- #ifdef HAVE_CRL
- if (ret == 0 && doCrlLookup &&
- ssl->ctx->cm->crlEnabled &&
- ssl->ctx->cm->crlCheckAll) {
- WOLFSSL_MSG("Doing Non Leaf CRL check");
- ret = CheckCertCRL(ssl->ctx->cm->crl, args->dCert);
- #ifdef WOLFSSL_NONBLOCK_OCSP
- if (ret == OCSP_WANT_READ) {
- args->lastErr = ret;
- goto exit_ppc;
- }
- #endif
- if (ret != 0) {
- WOLFSSL_MSG("\tCRL check not ok");
- }
- }
- #endif /* HAVE_CRL */
- (void)doCrlLookup;
- }
- #endif /* HAVE_OCSP || HAVE_CRL */
-
- /* Do verify callback */
- ret = DoVerifyCallback(ssl->ctx->cm, ssl, ret, args);
-
- #ifdef WOLFSSL_ALT_CERT_CHAINS
- /* For alternate cert chain, its okay for a CA cert to fail
- with ASN_NO_SIGNER_E here. The "alternate" certificate
- chain mode only requires that the peer certificate
- validate to a trusted CA */
- if (ret != 0 && args->dCert->isCA) {
- if (ret == ASN_NO_SIGNER_E) {
- if (!ssl->options.usingAltCertChain) {
- WOLFSSL_MSG("Trying alternate cert chain");
- ssl->options.usingAltCertChain = 1;
- }
-
- ret = 0; /* clear error and continue */
- }
-
- /* do not add to certificate manager */
- skipAddCA = 1;
- }
- #endif /* WOLFSSL_ALT_CERT_CHAINS */
-
- /* If valid CA then add to Certificate Manager */
- if (ret == 0 && args->dCert->isCA &&
- !ssl->options.verifyNone && !skipAddCA) {
- buffer* cert = &args->certs[args->certIdx];
-
- /* Is valid CA */
- #if defined(SESSION_CERTS) && defined(WOLFSSL_ALT_CERT_CHAINS)
- /* if using alternate chain, store the cert used */
- if (ssl->options.usingAltCertChain) {
- AddSessionCertToChain(&ssl->session.altChain,
- cert->buffer, cert->length);
- }
- #endif /* SESSION_CERTS && WOLFSSL_ALT_CERT_CHAINS */
- if (!alreadySigner) {
- DerBuffer* add = NULL;
- ret = AllocDer(&add, cert->length, CA_TYPE, ssl->heap);
- if (ret < 0)
- goto exit_ppc;
-
- XMEMCPY(add->buffer, cert->buffer, cert->length);
-
- /* CA already verified above in ParseCertRelative */
- WOLFSSL_MSG("Adding CA from chain");
- ret = AddCA(ssl->ctx->cm, &add, WOLFSSL_CHAIN_CA,
- NO_VERIFY);
- if (ret == WOLFSSL_SUCCESS) {
- ret = 0;
- }
- }
- }
-
- /* Handle error codes */
- if (ret != 0) {
- if (!ssl->options.verifyNone) {
- DoCertFatalAlert(ssl, ret);
- }
- ssl->error = ret; /* Report SSL error */
-
- if (args->lastErr == 0) {
- args->lastErr = ret; /* save error from last time */
- ret = 0; /* reset error */
- }
- }
-
- FreeDecodedCert(args->dCert);
- args->dCertInit = 0;
- args->count--;
- } /* while (count > 0 && !args->haveTrustPeer) */
- } /* if (count > 0) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_ppc;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_DO;
- } /* case TLS_ASYNC_BUILD */
- FALL_THROUGH;
-
- case TLS_ASYNC_DO:
- {
- /* peer's, may not have one if blank client cert sent by TLSv1.2 */
- if (args->count > 0) {
- WOLFSSL_MSG("Verifying Peer's cert");
-
- /* select peer cert (first one) */
- args->certIdx = 0;
-
- ret = ProcessPeerCertParse(ssl, args, CERT_TYPE,
- !ssl->options.verifyNone ? VERIFY : NO_VERIFY,
- &subjectHash, &alreadySigner);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- goto exit_ppc;
- #endif
- if (ret == 0) {
- WOLFSSL_MSG("Verified Peer's cert");
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- ssl->peerVerifyRet = X509_V_OK;
- #endif
- #if defined(SESSION_CERTS) && defined(WOLFSSL_ALT_CERT_CHAINS)
- /* if using alternate chain, store the cert used */
- if (ssl->options.usingAltCertChain) {
- buffer* cert = &args->certs[args->certIdx];
- AddSessionCertToChain(&ssl->session.altChain,
- cert->buffer, cert->length);
- }
- #endif /* SESSION_CERTS && WOLFSSL_ALT_CERT_CHAINS */
-
- /* check if fatal error */
- if (args->verifyErr) {
- args->fatal = 1;
- if (ret == 0) {
- ret = args->lastErr;
- }
- }
- else {
- args->fatal = 0;
- }
- }
- else if (ret == ASN_PARSE_E || ret == BUFFER_E) {
- WOLFSSL_MSG("Got Peer cert ASN PARSE or BUFFER ERROR");
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- SendAlert(ssl, alert_fatal, bad_certificate);
- ssl->peerVerifyRet = X509_V_ERR_CERT_REJECTED;
- #endif
- args->fatal = 1;
- }
- else {
- WOLFSSL_MSG("Failed to verify Peer's cert");
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- ssl->peerVerifyRet = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
- #endif
- if (ssl->verifyCallback) {
- WOLFSSL_MSG(
- "\tCallback override available, will continue");
- /* check if fatal error */
- args->fatal = (args->verifyErr) ? 1 : 0;
- }
- else {
- WOLFSSL_MSG("\tNo callback override available, fatal");
- args->fatal = 1;
- #ifdef OPENSSL_EXTRA
- SendAlert(ssl, alert_fatal, bad_certificate);
- #endif
- }
- }
-
- #ifdef HAVE_SECURE_RENEGOTIATION
- if (args->fatal == 0 && !IsAtLeastTLSv1_3(ssl->version)
- && ssl->secure_renegotiation
- && ssl->secure_renegotiation->enabled) {
-
- if (IsEncryptionOn(ssl, 0)) {
- /* compare against previous time */
- if (ssl->secure_renegotiation->subject_hash_set) {
- if (XMEMCMP(args->dCert->subjectHash,
- ssl->secure_renegotiation->subject_hash,
- KEYID_SIZE) != 0) {
- WOLFSSL_MSG(
- "Peer sent different cert during scr, fatal");
- args->fatal = 1;
- ret = SCR_DIFFERENT_CERT_E;
- }
- }
- }
-
- /* cache peer's hash */
- if (args->fatal == 0) {
- XMEMCPY(ssl->secure_renegotiation->subject_hash,
- args->dCert->subjectHash, KEYID_SIZE);
- ssl->secure_renegotiation->subject_hash_set = 1;
- }
- }
- #endif /* HAVE_SECURE_RENEGOTIATION */
- } /* if (count > 0) */
-
- /* Check for error */
- if (args->fatal && ret != 0) {
- goto exit_ppc;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_VERIFY;
- } /* case TLS_ASYNC_DO */
- FALL_THROUGH;
-
- case TLS_ASYNC_VERIFY:
- {
- if (args->count > 0) {
- #if defined(HAVE_OCSP) || defined(HAVE_CRL)
- if (args->fatal == 0) {
- int doLookup = 1;
-
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
- if (ssl->status_request) {
- args->fatal = TLSX_CSR_InitRequest(ssl->extensions,
- args->dCert, ssl->heap);
- doLookup = 0;
- #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
- if (ssl->options.tls1_3) {
- TLSX* ext = TLSX_Find(ssl->extensions,
- TLSX_STATUS_REQUEST);
- if (ext != NULL) {
- word32 idx = 0;
- CertificateStatusRequest* csr =
- (CertificateStatusRequest*)ext->data;
- ret = ProcessCSR(ssl, csr->response.buffer,
- &idx, csr->response.length);
- if (ret < 0)
- goto exit_ppc;
- }
- }
- #endif
- }
- #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
- if (ssl->status_request_v2) {
- args->fatal = TLSX_CSR2_InitRequests(ssl->extensions,
- args->dCert, 1, ssl->heap);
- doLookup = 0;
- }
- #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
- }
-
- #ifdef HAVE_OCSP
- if (doLookup && ssl->ctx->cm->ocspEnabled) {
- WOLFSSL_MSG("Doing Leaf OCSP check");
- ret = CheckCertOCSP_ex(ssl->ctx->cm->ocsp,
- args->dCert, NULL, ssl);
- #ifdef WOLFSSL_NONBLOCK_OCSP
- if (ret == OCSP_WANT_READ) {
- goto exit_ppc;
- }
- #endif
- doLookup = (ret == OCSP_CERT_UNKNOWN);
- if (ret != 0) {
- WOLFSSL_MSG("\tOCSP Lookup not ok");
- args->fatal = 0;
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- ssl->peerVerifyRet = X509_V_ERR_CERT_REJECTED;
- #endif
- }
- }
- #endif /* HAVE_OCSP */
-
- #ifdef HAVE_CRL
- if (doLookup && ssl->ctx->cm->crlEnabled) {
- WOLFSSL_MSG("Doing Leaf CRL check");
- ret = CheckCertCRL(ssl->ctx->cm->crl, args->dCert);
- #ifdef WOLFSSL_NONBLOCK_OCSP
- if (ret == OCSP_WANT_READ) {
- goto exit_ppc;
- }
- #endif
- if (ret != 0) {
- WOLFSSL_MSG("\tCRL check not ok");
- args->fatal = 0;
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- ssl->peerVerifyRet = X509_V_ERR_CERT_REJECTED;
- #endif
- }
- }
- #endif /* HAVE_CRL */
- (void)doLookup;
- }
- #endif /* HAVE_OCSP || HAVE_CRL */
-
- #ifdef KEEP_PEER_CERT
- if (args->fatal == 0) {
- int copyRet = 0;
-
- #ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation &&
- ssl->secure_renegotiation->enabled) {
- /* free old peer cert */
- FreeX509(&ssl->peerCert);
- }
- #endif
-
- /* set X509 format for peer cert */
- copyRet = CopyDecodedToX509(&ssl->peerCert, args->dCert);
- if (copyRet == MEMORY_E) {
- args->fatal = 1;
- }
- }
- #endif /* KEEP_PEER_CERT */
-
- #ifndef IGNORE_KEY_EXTENSIONS
- #if defined(OPENSSL_EXTRA)
- /* when compatibility layer is turned on and no verify is
- * set then ignore the certificate key extension */
- if (args->dCert->extKeyUsageSet &&
- args->dCert->extKeyUsageCrit == 0 &&
- ssl->options.verifyNone) {
- WOLFSSL_MSG("Not verifying certificate key usage");
- }
- else
- #endif
- if (args->dCert->extKeyUsageSet) {
- if ((ssl->specs.kea == rsa_kea) &&
- (ssl->options.side == WOLFSSL_CLIENT_END) &&
- (args->dCert->extKeyUsage & KEYUSE_KEY_ENCIPHER) == 0) {
- ret = KEYUSE_ENCIPHER_E;
- }
- if ((ssl->specs.sig_algo == rsa_sa_algo ||
- (ssl->specs.sig_algo == ecc_dsa_sa_algo &&
- !ssl->specs.static_ecdh)) &&
- (args->dCert->extKeyUsage & KEYUSE_DIGITAL_SIG) == 0) {
- WOLFSSL_MSG("KeyUse Digital Sig not set");
- ret = KEYUSE_SIGNATURE_E;
- }
- }
-
- #if defined(OPENSSL_EXTRA)
- /* when compatibility layer is turned on and no verify is
- * set then ignore the certificate key extension */
- if (args->dCert->extExtKeyUsageSet &&
- args->dCert->extExtKeyUsageCrit == 0 &&
- ssl->options.verifyNone) {
- WOLFSSL_MSG("Not verifying certificate ext key usage");
- }
- else
- #endif
- if (args->dCert->extExtKeyUsageSet) {
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if ((args->dCert->extExtKeyUsage &
- (EXTKEYUSE_ANY | EXTKEYUSE_SERVER_AUTH)) == 0) {
- WOLFSSL_MSG("ExtKeyUse Server Auth not set");
- ret = EXTKEYUSE_AUTH_E;
- }
- }
- else {
- if ((args->dCert->extExtKeyUsage &
- (EXTKEYUSE_ANY | EXTKEYUSE_CLIENT_AUTH)) == 0) {
- WOLFSSL_MSG("ExtKeyUse Client Auth not set");
- ret = EXTKEYUSE_AUTH_E;
- }
- }
- }
- #endif /* IGNORE_KEY_EXTENSIONS */
-
- if (args->fatal) {
- ssl->error = ret;
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- SendAlert(ssl, alert_fatal, bad_certificate);
- ssl->peerVerifyRet = X509_V_ERR_CERT_REJECTED;
- #endif
- goto exit_ppc;
- }
-
- ssl->options.havePeerCert = 1;
-
- if (!ssl->options.verifyNone && ssl->buffers.domainName.buffer) {
- #ifndef WOLFSSL_ALLOW_NO_CN_IN_SAN
- /* Per RFC 5280 section 4.2.1.6, "Whenever such identities
- * are to be bound into a certificate, the subject
- * alternative name extension MUST be used." */
- if (args->dCert->altNames) {
- if (CheckAltNames(args->dCert,
- (char*)ssl->buffers.domainName.buffer) == 0 ) {
- WOLFSSL_MSG("DomainName match on alt names failed");
- /* try to get peer key still */
- ret = DOMAIN_NAME_MISMATCH;
- }
- }
- else {
- if (MatchDomainName(
- args->dCert->subjectCN,
- args->dCert->subjectCNLen,
- (char*)ssl->buffers.domainName.buffer) == 0) {
- WOLFSSL_MSG("DomainName match on common name failed");
- ret = DOMAIN_NAME_MISMATCH;
- }
- }
- #else /* WOLFSSL_ALL_NO_CN_IN_SAN */
- /* Old behavior. */
- if (MatchDomainName(args->dCert->subjectCN,
- args->dCert->subjectCNLen,
- (char*)ssl->buffers.domainName.buffer) == 0) {
- WOLFSSL_MSG("DomainName match on common name failed");
- if (CheckAltNames(args->dCert,
- (char*)ssl->buffers.domainName.buffer) == 0 ) {
- WOLFSSL_MSG(
- "DomainName match on alt names failed too");
- /* try to get peer key still */
- ret = DOMAIN_NAME_MISMATCH;
- }
- }
- #endif /* WOLFSSL_ALL_NO_CN_IN_SAN */
- }
-
- /* decode peer key */
- switch (args->dCert->keyOID) {
- #ifndef NO_RSA
- case RSAk:
- {
- word32 keyIdx = 0;
- int keyRet = 0;
-
- if (ssl->peerRsaKey == NULL) {
- keyRet = AllocKey(ssl, DYNAMIC_TYPE_RSA,
- (void**)&ssl->peerRsaKey);
- } else if (ssl->peerRsaKeyPresent) {
- keyRet = ReuseKey(ssl, DYNAMIC_TYPE_RSA,
- ssl->peerRsaKey);
- ssl->peerRsaKeyPresent = 0;
- }
-
- if (keyRet != 0 || wc_RsaPublicKeyDecode(
- args->dCert->publicKey, &keyIdx, ssl->peerRsaKey,
- args->dCert->pubKeySize) != 0) {
- ret = PEER_KEY_ERROR;
- }
- else {
- ssl->peerRsaKeyPresent = 1;
- #ifdef WOLFSSL_RENESAS_TSIP_TLS
- /* copy encrypted tsip key index into ssl object */
- if (args->dCert->tsip_encRsaKeyIdx) {
- if (!ssl->peerTsipEncRsaKeyIndex) {
- ssl->peerTsipEncRsaKeyIndex = (byte*)XMALLOC(
- TSIP_TLS_ENCPUBKEY_SZ_BY_CERTVRFY,
- ssl->heap, DYNAMIC_TYPE_RSA);
- if (!ssl->peerTsipEncRsaKeyIndex) {
- args->lastErr = MEMORY_E;
- goto exit_ppc;
- }
- }
-
- XMEMCPY(ssl->peerTsipEncRsaKeyIndex,
- args->dCert->tsip_encRsaKeyIdx,
- TSIP_TLS_ENCPUBKEY_SZ_BY_CERTVRFY);
- }
- #endif
- #ifdef HAVE_PK_CALLBACKS
- #ifndef NO_RSA
- #ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->buffers.peerRsaKey.buffer) {
- XFREE(ssl->buffers.peerRsaKey.buffer,
- ssl->heap, DYNAMIC_TYPE_RSA);
- ssl->buffers.peerRsaKey.buffer = NULL;
- }
- #endif
-
-
- ssl->buffers.peerRsaKey.buffer =
- (byte*)XMALLOC(args->dCert->pubKeySize,
- ssl->heap, DYNAMIC_TYPE_RSA);
- if (ssl->buffers.peerRsaKey.buffer == NULL) {
- ret = MEMORY_ERROR;
- }
- else {
- XMEMCPY(ssl->buffers.peerRsaKey.buffer,
- args->dCert->publicKey,
- args->dCert->pubKeySize);
- ssl->buffers.peerRsaKey.length =
- args->dCert->pubKeySize;
- }
- #endif /* NO_RSA */
- #endif /* HAVE_PK_CALLBACKS */
- }
-
- /* check size of peer RSA key */
- if (ret == 0 && ssl->peerRsaKeyPresent &&
- !ssl->options.verifyNone &&
- wc_RsaEncryptSize(ssl->peerRsaKey)
- < ssl->options.minRsaKeySz) {
- ret = RSA_KEY_SIZE_E;
- WOLFSSL_MSG("Peer RSA key is too small");
- }
- break;
- }
- #endif /* NO_RSA */
- #ifdef HAVE_NTRU
- case NTRUk:
- {
- if (args->dCert->pubKeySize > sizeof(ssl->peerNtruKey)) {
- ret = PEER_KEY_ERROR;
- }
- else {
- XMEMCPY(ssl->peerNtruKey, args->dCert->publicKey,
- args->dCert->pubKeySize);
- ssl->peerNtruKeyLen =
- (word16)args->dCert->pubKeySize;
- ssl->peerNtruKeyPresent = 1;
- }
- break;
- }
- #endif /* HAVE_NTRU */
- #ifdef HAVE_ECC
- case ECDSAk:
- {
- int keyRet = 0;
- word32 idx = 0;
-
- if (ssl->peerEccDsaKey == NULL) {
- /* alloc/init on demand */
- keyRet = AllocKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->peerEccDsaKey);
- } else if (ssl->peerEccDsaKeyPresent) {
- keyRet = ReuseKey(ssl, DYNAMIC_TYPE_ECC,
- ssl->peerEccDsaKey);
- ssl->peerEccDsaKeyPresent = 0;
- }
-
- if (keyRet != 0 ||
- wc_EccPublicKeyDecode(args->dCert->publicKey, &idx,
- ssl->peerEccDsaKey,
- args->dCert->pubKeySize) != 0) {
- ret = PEER_KEY_ERROR;
- }
- else {
- ssl->peerEccDsaKeyPresent = 1;
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.peerEccDsaKey.buffer =
- (byte*)XMALLOC(args->dCert->pubKeySize,
- ssl->heap, DYNAMIC_TYPE_ECC);
- if (ssl->buffers.peerEccDsaKey.buffer == NULL) {
- ERROR_OUT(MEMORY_ERROR, exit_ppc);
- }
- else {
- XMEMCPY(ssl->buffers.peerEccDsaKey.buffer,
- args->dCert->publicKey,
- args->dCert->pubKeySize);
- ssl->buffers.peerEccDsaKey.length =
- args->dCert->pubKeySize;
- }
- #endif /* HAVE_PK_CALLBACKS */
- }
-
- /* check size of peer ECC key */
- if (ret == 0 && ssl->peerEccDsaKeyPresent &&
- !ssl->options.verifyNone &&
- wc_ecc_size(ssl->peerEccDsaKey)
- < ssl->options.minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("Peer ECC key is too small");
- }
- break;
- }
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ED25519k:
- {
- int keyRet = 0;
- if (ssl->peerEd25519Key == NULL) {
- /* alloc/init on demand */
- keyRet = AllocKey(ssl, DYNAMIC_TYPE_ED25519,
- (void**)&ssl->peerEd25519Key);
- } else if (ssl->peerEd25519KeyPresent) {
- keyRet = ReuseKey(ssl, DYNAMIC_TYPE_ED25519,
- ssl->peerEd25519Key);
- ssl->peerEd25519KeyPresent = 0;
- }
-
- if (keyRet != 0 ||
- wc_ed25519_import_public(args->dCert->publicKey,
- args->dCert->pubKeySize,
- ssl->peerEd25519Key)
- != 0) {
- ret = PEER_KEY_ERROR;
- }
- else {
- ssl->peerEd25519KeyPresent = 1;
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.peerEd25519Key.buffer =
- (byte*)XMALLOC(args->dCert->pubKeySize,
- ssl->heap, DYNAMIC_TYPE_ED25519);
- if (ssl->buffers.peerEd25519Key.buffer == NULL) {
- ERROR_OUT(MEMORY_ERROR, exit_ppc);
- }
- else {
- XMEMCPY(ssl->buffers.peerEd25519Key.buffer,
- args->dCert->publicKey,
- args->dCert->pubKeySize);
- ssl->buffers.peerEd25519Key.length =
- args->dCert->pubKeySize;
- }
- #endif /*HAVE_PK_CALLBACKS */
- }
-
- /* check size of peer ECC key */
- if (ret == 0 && ssl->peerEd25519KeyPresent &&
- !ssl->options.verifyNone &&
- ED25519_KEY_SIZE < ssl->options.minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("Peer ECC key is too small");
- }
- break;
- }
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- case ED448k:
- {
- int keyRet = 0;
- if (ssl->peerEd448Key == NULL) {
- /* alloc/init on demand */
- keyRet = AllocKey(ssl, DYNAMIC_TYPE_ED448,
- (void**)&ssl->peerEd448Key);
- } else if (ssl->peerEd448KeyPresent) {
- keyRet = ReuseKey(ssl, DYNAMIC_TYPE_ED448,
- ssl->peerEd448Key);
- ssl->peerEd448KeyPresent = 0;
- }
-
- if (keyRet != 0 ||
- wc_ed448_import_public(args->dCert->publicKey,
- args->dCert->pubKeySize,
- ssl->peerEd448Key) != 0) {
- ret = PEER_KEY_ERROR;
- }
- else {
- ssl->peerEd448KeyPresent = 1;
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.peerEd448Key.buffer =
- (byte*)XMALLOC(args->dCert->pubKeySize,
- ssl->heap, DYNAMIC_TYPE_ED448);
- if (ssl->buffers.peerEd448Key.buffer == NULL) {
- ERROR_OUT(MEMORY_ERROR, exit_ppc);
- }
- else {
- XMEMCPY(ssl->buffers.peerEd448Key.buffer,
- args->dCert->publicKey,
- args->dCert->pubKeySize);
- ssl->buffers.peerEd448Key.length =
- args->dCert->pubKeySize;
- }
- #endif /*HAVE_PK_CALLBACKS */
- }
-
- /* check size of peer ECC key */
- if (ret == 0 && ssl->peerEd448KeyPresent &&
- !ssl->options.verifyNone &&
- ED448_KEY_SIZE < ssl->options.minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("Peer ECC key is too small");
- }
- break;
- }
- #endif /* HAVE_ED448 */
- default:
- break;
- }
-
- /* args->dCert free'd in function cleanup after callback */
- } /* if (count > 0) */
-
- /* Check for error */
- if (args->fatal && ret != 0) {
- goto exit_ppc;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_FINALIZE;
- } /* case TLS_ASYNC_VERIFY */
- FALL_THROUGH;
-
- case TLS_ASYNC_FINALIZE:
- {
- /* load last error */
- if (args->lastErr != 0 && ret == 0) {
- ret = args->lastErr;
- }
-
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- if (args->untrustedDepth > ssl->options.verifyDepth) {
- ssl->peerVerifyRet = X509_V_ERR_CERT_CHAIN_TOO_LONG;
- ret = MAX_CHAIN_ERROR;
- }
- #endif
-
- /* Do verify callback */
- ret = DoVerifyCallback(ssl->ctx->cm, ssl, ret, args);
-
- if (ssl->options.verifyNone &&
- (ret == CRL_MISSING || ret == CRL_CERT_REVOKED)) {
- WOLFSSL_MSG("Ignoring CRL problem based on verify setting");
- ret = ssl->error = 0;
- }
-
- if (ret != 0) {
- if (!ssl->options.verifyNone) {
- DoCertFatalAlert(ssl, ret);
- }
- ssl->error = ret; /* Report SSL error */
- }
-
- if (ret == 0 && ssl->options.side == WOLFSSL_CLIENT_END) {
- ssl->options.serverState = SERVER_CERT_COMPLETE;
- }
-
- if (IsEncryptionOn(ssl, 0)) {
- args->idx += ssl->keys.padSz;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- args->idx += MacSize(ssl);
- #endif
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_END;
- } /* case TLS_ASYNC_FINALIZE */
- FALL_THROUGH;
-
- case TLS_ASYNC_END:
- {
- /* Set final index */
- *inOutIdx = args->idx;
-
- break;
- }
- default:
- ret = INPUT_CASE_ERROR;
- break;
- } /* switch(ssl->options.asyncState) */
-
-exit_ppc:
-
- WOLFSSL_LEAVE("ProcessPeerCerts", ret);
-
-
-#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP)
- if (ret == WC_PENDING_E || ret == OCSP_WANT_READ) {
- /* Mark message as not received so it can process again */
- ssl->msgsReceived.got_certificate = 0;
-
- return ret;
- }
-#endif /* WOLFSSL_ASYNC_CRYPT || WOLFSSL_NONBLOCK_OCSP */
-
- FreeProcPeerCertArgs(ssl, args);
-
-#if defined(WOLFSSL_ASYNC_CRYPT)
-#elif defined(WOLFSSL_NONBLOCK_OCSP)
- XFREE(args, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- ssl->nonblockarg = NULL;
-#elif defined(WOLFSSL_SMALL_STACK)
- XFREE(args, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- FreeKeyExchange(ssl);
-
- return ret;
-}
-#endif
-
-#ifndef WOLFSSL_NO_TLS12
-#if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
-
-/* handle processing of certificate (11) */
-static int DoCertificate(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- word32 size)
-{
- int ret;
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_DO);
- WOLFSSL_ENTER("DoCertificate");
-
-#ifdef SESSION_CERTS
- /* Reset the session cert chain count in case the session resume failed. */
- ssl->session.chain.count = 0;
- #ifdef WOLFSSL_ALT_CERT_CHAINS
- ssl->session.altChain.count = 0;
- #endif
-#endif /* SESSION_CERTS */
-
- ret = ProcessPeerCerts(ssl, input, inOutIdx, size);
-#ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_ERROR || ret == ASN_PARSE_E)
- SendAlert(ssl, alert_fatal, decode_error);
-#endif
-
-#ifdef OPENSSL_EXTRA
- ssl->options.serverState = SERVER_CERT_COMPLETE;
-#endif
-
- WOLFSSL_LEAVE("DoCertificate", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_DO);
-
- return ret;
-}
-
-/* handle processing of certificate_status (22) */
-static int DoCertificateStatus(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- word32 size)
-{
- int ret = 0;
- byte status_type;
- word32 status_length;
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_STATUS_DO);
- WOLFSSL_ENTER("DoCertificateStatus");
-
- if (size < ENUM_LEN + OPAQUE24_LEN)
- return BUFFER_ERROR;
-
- status_type = input[(*inOutIdx)++];
-
- c24to32(input + *inOutIdx, &status_length);
- *inOutIdx += OPAQUE24_LEN;
-
- if (size != ENUM_LEN + OPAQUE24_LEN + status_length)
- return BUFFER_ERROR;
-
- switch (status_type) {
-
- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
- || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
-
- /* WOLFSSL_CSR_OCSP overlaps with WOLFSSL_CSR2_OCSP */
- case WOLFSSL_CSR2_OCSP:
- ret = ProcessCSR(ssl, input, inOutIdx, status_length);
- break;
-
- #endif
-
- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
-
- case WOLFSSL_CSR2_OCSP_MULTI: {
- OcspRequest* request;
- word32 list_length = status_length;
- byte idx = 0;
-
- #ifdef WOLFSSL_SMALL_STACK
- CertStatus* status;
- OcspResponse* response;
- #else
- CertStatus status[1];
- OcspResponse response[1];
- #endif
-
- do {
- if (ssl->status_request_v2) {
- ssl->status_request_v2 = 0;
- break;
- }
-
- return BUFFER_ERROR;
- } while(0);
-
- #ifdef WOLFSSL_SMALL_STACK
- status = (CertStatus*)XMALLOC(sizeof(CertStatus), ssl->heap,
- DYNAMIC_TYPE_OCSP_STATUS);
- response = (OcspResponse*)XMALLOC(sizeof(OcspResponse), ssl->heap,
- DYNAMIC_TYPE_OCSP_REQUEST);
-
- if (status == NULL || response == NULL) {
- if (status)
- XFREE(status, ssl->heap, DYNAMIC_TYPE_OCSP_STATUS);
- if (response)
- XFREE(response, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
-
- return MEMORY_ERROR;
- }
- #endif
-
- while (list_length && ret == 0) {
- if (OPAQUE24_LEN > list_length) {
- ret = BUFFER_ERROR;
- break;
- }
-
- c24to32(input + *inOutIdx, &status_length);
- *inOutIdx += OPAQUE24_LEN;
- list_length -= OPAQUE24_LEN;
-
- if (status_length > list_length) {
- ret = BUFFER_ERROR;
- break;
- }
-
- if (status_length) {
- InitOcspResponse(response, status, input +*inOutIdx,
- status_length);
-
- if ((OcspResponseDecode(response, ssl->ctx->cm, ssl->heap,
- 0) != 0)
- || (response->responseStatus != OCSP_SUCCESSFUL)
- || (response->status->status != CERT_GOOD))
- ret = BAD_CERTIFICATE_STATUS_ERROR;
-
- while (ret == 0) {
- request = (OcspRequest*)TLSX_CSR2_GetRequest(
- ssl->extensions, status_type, idx++);
-
- if (request == NULL)
- ret = BAD_CERTIFICATE_STATUS_ERROR;
- else if (CompareOcspReqResp(request, response) == 0)
- break;
- else if (idx == 1) /* server cert must be OK */
- ret = BAD_CERTIFICATE_STATUS_ERROR;
- }
-
- *inOutIdx += status_length;
- list_length -= status_length;
- }
- }
-
- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
- ssl->status_request_v2 = 0;
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(status, NULL, DYNAMIC_TYPE_OCSP_STATUS);
- XFREE(response, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
- #endif
-
- }
- break;
-
- #endif
-
- default:
- ret = BUFFER_ERROR;
- }
-
- if (ret != 0)
- SendAlert(ssl, alert_fatal, bad_certificate_status_response);
-
- if (IsEncryptionOn(ssl, 0)) {
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead) {
- word32 digestSz = MacSize(ssl);
- if (*inOutIdx + ssl->keys.padSz + digestSz > size)
- return BUFFER_E;
- *inOutIdx += ssl->keys.padSz + digestSz;
- }
- else
- #endif
- {
- if (*inOutIdx + ssl->keys.padSz > size)
- return BUFFER_E;
- *inOutIdx += ssl->keys.padSz;
- }
- }
-
- WOLFSSL_LEAVE("DoCertificateStatus", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_STATUS_DO);
-
- return ret;
-}
-
-#endif
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#endif /* !NO_CERTS */
-
-#ifndef WOLFSSL_NO_TLS12
-
-static int DoHelloRequest(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- word32 size, word32 totalSz)
-{
- (void)input;
-
- WOLFSSL_START(WC_FUNC_HELLO_REQUEST_DO);
- WOLFSSL_ENTER("DoHelloRequest");
-
- if (size) /* must be 0 */
- return BUFFER_ERROR;
-
- if (IsEncryptionOn(ssl, 0)) {
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead) {
- word32 digestSz = MacSize(ssl);
- if (*inOutIdx + ssl->keys.padSz + digestSz > totalSz)
- return BUFFER_E;
- *inOutIdx += ssl->keys.padSz + digestSz;
- }
- else
- #endif
- {
- /* access beyond input + size should be checked against totalSz */
- if (*inOutIdx + ssl->keys.padSz > totalSz)
- return BUFFER_E;
-
- *inOutIdx += ssl->keys.padSz;
- }
- }
-
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- SendAlert(ssl, alert_fatal, unexpected_message); /* try */
- return FATAL_ERROR;
- }
-#ifdef HAVE_SECURE_RENEGOTIATION
- else if (ssl->secure_renegotiation && ssl->secure_renegotiation->enabled) {
- ssl->secure_renegotiation->startScr = 1;
- WOLFSSL_LEAVE("DoHelloRequest", 0);
- WOLFSSL_END(WC_FUNC_HELLO_REQUEST_DO);
- return 0;
- }
-#endif
- else {
- return SendAlert(ssl, alert_warning, no_renegotiation);
- }
-}
-
-
-int DoFinished(WOLFSSL* ssl, const byte* input, word32* inOutIdx, word32 size,
- word32 totalSz, int sniff)
-{
- word32 finishedSz = (ssl->options.tls ? TLS_FINISHED_SZ : FINISHED_SZ);
-
- WOLFSSL_START(WC_FUNC_FINISHED_DO);
- WOLFSSL_ENTER("DoFinished");
-
- if (finishedSz != size)
- return BUFFER_ERROR;
-
- /* check against totalSz */
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead) {
- if (*inOutIdx + size + ssl->keys.padSz + MacSize(ssl) > totalSz)
- return BUFFER_E;
- }
- else
- #endif
- {
- if (*inOutIdx + size + ssl->keys.padSz > totalSz)
- return BUFFER_E;
- }
-
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "Finished");
- if (ssl->toInfoOn) AddLateName("Finished", &ssl->timeoutInfo);
- #endif
-
- if (sniff == NO_SNIFF) {
- if (XMEMCMP(input + *inOutIdx, &ssl->hsHashes->verifyHashes,size) != 0){
- WOLFSSL_MSG("Verify finished error on hashes");
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, decrypt_error);
- #endif
- return VERIFY_FINISHED_ERROR;
- }
- }
-
-#ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation) {
- /* save peer's state */
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- XMEMCPY(ssl->secure_renegotiation->server_verify_data,
- input + *inOutIdx, TLS_FINISHED_SZ);
- else
- XMEMCPY(ssl->secure_renegotiation->client_verify_data,
- input + *inOutIdx, TLS_FINISHED_SZ);
- ssl->secure_renegotiation->verifySet = 1;
- }
-#endif
-
- /* force input exhaustion at ProcessReply consuming padSz */
- *inOutIdx += size + ssl->keys.padSz;
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- *inOutIdx += MacSize(ssl);
-#endif
-
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- ssl->options.serverState = SERVER_FINISHED_COMPLETE;
-#ifdef OPENSSL_EXTRA
- ssl->cbmode = SSL_CB_MODE_WRITE;
- ssl->options.clientState = CLIENT_FINISHED_COMPLETE;
-#endif
- if (!ssl->options.resuming) {
-#ifdef OPENSSL_EXTRA
- if (ssl->CBIS != NULL) {
- ssl->CBIS(ssl, SSL_CB_CONNECT_LOOP, SSL_SUCCESS);
- }
-#endif
- ssl->options.handShakeState = HANDSHAKE_DONE;
- ssl->options.handShakeDone = 1;
- }
- }
- else {
- ssl->options.clientState = CLIENT_FINISHED_COMPLETE;
-#ifdef OPENSSL_EXTRA
- ssl->cbmode = SSL_CB_MODE_READ;
- ssl->options.serverState = SERVER_FINISHED_COMPLETE;
-#endif
- if (ssl->options.resuming) {
-#ifdef OPENSSL_EXTRA
- if (ssl->CBIS != NULL) {
- ssl->CBIS(ssl, SSL_CB_ACCEPT_LOOP, SSL_SUCCESS);
- }
-#endif
- ssl->options.handShakeState = HANDSHAKE_DONE;
- ssl->options.handShakeDone = 1;
- }
- }
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- DtlsMsgPoolReset(ssl);
- }
-#endif
-
- WOLFSSL_LEAVE("DoFinished", 0);
- WOLFSSL_END(WC_FUNC_FINISHED_DO);
-
- return 0;
-}
-
-
-/* Make sure no duplicates, no fast forward, or other problems; 0 on success */
-static int SanityCheckMsgReceived(WOLFSSL* ssl, byte type)
-{
- /* verify not a duplicate, mark received, check state */
- switch (type) {
-
-#ifndef NO_WOLFSSL_CLIENT
- case hello_request:
- if (ssl->msgsReceived.got_hello_request) {
- WOLFSSL_MSG("Duplicate HelloRequest received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_hello_request = 1;
-
- break;
-#endif
-
-#ifndef NO_WOLFSSL_SERVER
- case client_hello:
- if (ssl->msgsReceived.got_client_hello) {
- WOLFSSL_MSG("Duplicate ClientHello received");
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, unexpected_message);
- #endif
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_client_hello = 1;
-
- break;
-#endif
-
-#ifndef NO_WOLFSSL_CLIENT
- case server_hello:
- if (ssl->msgsReceived.got_server_hello) {
- WOLFSSL_MSG("Duplicate ServerHello received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_server_hello = 1;
-
- break;
-#endif
-
-#ifndef NO_WOLFSSL_CLIENT
- case hello_verify_request:
- if (ssl->msgsReceived.got_hello_verify_request) {
- WOLFSSL_MSG("Duplicate HelloVerifyRequest received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_hello_verify_request = 1;
-
- break;
-#endif
-
-#ifndef NO_WOLFSSL_CLIENT
- case session_ticket:
- if (ssl->msgsReceived.got_session_ticket) {
- WOLFSSL_MSG("Duplicate SessionTicket received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_session_ticket = 1;
-
- break;
-#endif
-
- case certificate:
- if (ssl->msgsReceived.got_certificate) {
- WOLFSSL_MSG("Duplicate Certificate received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_certificate = 1;
-
-#ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if ( ssl->msgsReceived.got_server_hello == 0) {
- WOLFSSL_MSG("No ServerHello before Cert");
- return OUT_OF_ORDER_E;
- }
- }
-#endif
-#ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- if ( ssl->msgsReceived.got_client_hello == 0) {
- WOLFSSL_MSG("No ClientHello before Cert");
- return OUT_OF_ORDER_E;
- }
- }
-#endif
- break;
-
-#ifndef NO_WOLFSSL_CLIENT
- case certificate_status:
- if (ssl->msgsReceived.got_certificate_status) {
- WOLFSSL_MSG("Duplicate CertificateSatatus received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_certificate_status = 1;
-
- if (ssl->msgsReceived.got_certificate == 0) {
- WOLFSSL_MSG("No Certificate before CertificateStatus");
- return OUT_OF_ORDER_E;
- }
- if (ssl->msgsReceived.got_server_key_exchange != 0) {
- WOLFSSL_MSG("CertificateStatus after ServerKeyExchange");
- return OUT_OF_ORDER_E;
- }
-
- break;
-#endif
-
-#ifndef NO_WOLFSSL_CLIENT
- case server_key_exchange:
- if (ssl->msgsReceived.got_server_key_exchange) {
- WOLFSSL_MSG("Duplicate ServerKeyExchange received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_server_key_exchange = 1;
-
- if (ssl->msgsReceived.got_server_hello == 0) {
- WOLFSSL_MSG("No ServerHello before ServerKeyExchange");
- return OUT_OF_ORDER_E;
- }
- if (ssl->msgsReceived.got_certificate_status == 0) {
-#ifdef HAVE_CERTIFICATE_STATUS_REQUEST
- if (ssl->status_request) {
- int ret;
-
- WOLFSSL_MSG("No CertificateStatus before ServerKeyExchange");
- if ((ret = TLSX_CSR_ForceRequest(ssl)) != 0)
- return ret;
- }
-#endif
-#ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
- if (ssl->status_request_v2) {
- int ret;
-
- WOLFSSL_MSG("No CertificateStatus before ServerKeyExchange");
- if ((ret = TLSX_CSR2_ForceRequest(ssl)) != 0)
- return ret;
- }
-#endif
- }
-
- break;
-#endif
-
-#ifndef NO_WOLFSSL_CLIENT
- case certificate_request:
- if (ssl->msgsReceived.got_certificate_request) {
- WOLFSSL_MSG("Duplicate CertificateRequest received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_certificate_request = 1;
-
- break;
-#endif
-
-#ifndef NO_WOLFSSL_CLIENT
- case server_hello_done:
- if (ssl->msgsReceived.got_server_hello_done) {
- WOLFSSL_MSG("Duplicate ServerHelloDone received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_server_hello_done = 1;
-
- if (ssl->msgsReceived.got_certificate == 0) {
- if (ssl->specs.kea == psk_kea ||
- ssl->specs.kea == dhe_psk_kea ||
- ssl->specs.kea == ecdhe_psk_kea ||
- ssl->options.usingAnon_cipher) {
- WOLFSSL_MSG("No Cert required");
- } else {
- WOLFSSL_MSG("No Certificate before ServerHelloDone");
- return OUT_OF_ORDER_E;
- }
- }
- if (ssl->msgsReceived.got_server_key_exchange == 0) {
- int pskNoServerHint = 0; /* not required in this case */
-
- #ifndef NO_PSK
- if (ssl->specs.kea == psk_kea &&
- ssl->arrays != NULL &&
- ssl->arrays->server_hint[0] == 0)
- pskNoServerHint = 1;
- #endif
- if (ssl->specs.static_ecdh == 1 ||
- ssl->specs.kea == rsa_kea ||
- ssl->specs.kea == ntru_kea ||
- pskNoServerHint) {
- WOLFSSL_MSG("No KeyExchange required");
- } else {
- WOLFSSL_MSG("No ServerKeyExchange before ServerDone");
- return OUT_OF_ORDER_E;
- }
- }
- break;
-#endif
-
-#ifndef NO_WOLFSSL_SERVER
- case certificate_verify:
- if (ssl->msgsReceived.got_certificate_verify) {
- WOLFSSL_MSG("Duplicate CertificateVerify received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_certificate_verify = 1;
-
- if ( ssl->msgsReceived.got_certificate == 0) {
- WOLFSSL_MSG("No Cert before CertVerify");
- return OUT_OF_ORDER_E;
- }
- break;
-#endif
-
-#ifndef NO_WOLFSSL_SERVER
- case client_key_exchange:
- if (ssl->msgsReceived.got_client_key_exchange) {
- WOLFSSL_MSG("Duplicate ClientKeyExchange received");
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, unexpected_message);
- #endif
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_client_key_exchange = 1;
-
- if (ssl->msgsReceived.got_client_hello == 0) {
- WOLFSSL_MSG("No ClientHello before ClientKeyExchange");
- return OUT_OF_ORDER_E;
- }
- break;
-#endif
-
- case finished:
- if (ssl->msgsReceived.got_finished) {
- WOLFSSL_MSG("Duplicate Finished received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_finished = 1;
-
- if (ssl->msgsReceived.got_change_cipher == 0) {
- WOLFSSL_MSG("Finished received before ChangeCipher");
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, unexpected_message);
- #endif
- return NO_CHANGE_CIPHER_E;
- }
- break;
-
- case change_cipher_hs:
- if (ssl->msgsReceived.got_change_cipher) {
- WOLFSSL_MSG("Duplicate ChangeCipher received");
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, unexpected_message);
- #endif
- return DUPLICATE_MSG_E;
- }
- /* DTLS is going to ignore the CCS message if the client key
- * exchange message wasn't received yet. */
- if (!ssl->options.dtls)
- ssl->msgsReceived.got_change_cipher = 1;
-
-#ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if (!ssl->options.resuming) {
- if (ssl->msgsReceived.got_server_hello_done == 0) {
- WOLFSSL_MSG("No ServerHelloDone before ChangeCipher");
- return OUT_OF_ORDER_E;
- }
- }
- else {
- if (ssl->msgsReceived.got_server_hello == 0) {
- WOLFSSL_MSG("No ServerHello before ChangeCipher on Resume");
- return OUT_OF_ORDER_E;
- }
- }
- #ifdef HAVE_SESSION_TICKET
- if (ssl->expect_session_ticket) {
- WOLFSSL_MSG("Expected session ticket missing");
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- return OUT_OF_ORDER_E;
- #endif
- return SESSION_TICKET_EXPECT_E;
- }
- #endif
- }
-#endif
-#ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- if (!ssl->options.resuming &&
- ssl->msgsReceived.got_client_key_exchange == 0) {
- WOLFSSL_MSG("No ClientKeyExchange before ChangeCipher");
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, unexpected_message);
- #endif
- return OUT_OF_ORDER_E;
- }
- #ifndef NO_CERTS
- if (ssl->options.verifyPeer &&
- ssl->options.havePeerCert) {
-
- if (!ssl->options.havePeerVerify) {
- WOLFSSL_MSG("client didn't send cert verify");
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- return OUT_OF_ORDER_E;
- #endif
- return NO_PEER_VERIFY;
- }
- }
- #endif
- }
-#endif
- if (ssl->options.dtls)
- ssl->msgsReceived.got_change_cipher = 1;
- break;
-
- default:
- WOLFSSL_MSG("Unknown message type");
- return SANITY_MSG_E;
- }
-
- return 0;
-}
-
-
-static int DoHandShakeMsgType(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- byte type, word32 size, word32 totalSz)
-{
- int ret = 0;
- word32 expectedIdx;
-
- WOLFSSL_ENTER("DoHandShakeMsgType");
-
-#ifdef WOLFSSL_TLS13
- if (type == hello_retry_request) {
- return DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size,
- totalSz);
- }
-#endif
-
- /* make sure can read the message */
- if (*inOutIdx + size > totalSz) {
- WOLFSSL_MSG("Incomplete Data");
- return INCOMPLETE_DATA;
- }
-
- expectedIdx = *inOutIdx + size +
- (ssl->keys.encryptionOn ? ssl->keys.padSz : 0);
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead && ssl->keys.encryptionOn)
- expectedIdx += MacSize(ssl);
-#endif
-
-#if !defined(WOLFSSL_NO_SERVER) && \
- defined(HAVE_SECURE_RENEGOTIATION) && \
- defined(HAVE_SERVER_RENEGOTIATION_INFO)
- if (ssl->options.handShakeDone && type == client_hello &&
- ssl->secure_renegotiation &&
- ssl->secure_renegotiation->enabled)
- {
- WOLFSSL_MSG("Reset handshake state");
- XMEMSET(&ssl->msgsReceived, 0, sizeof(MsgsReceived));
- ssl->options.serverState = NULL_STATE;
- ssl->options.clientState = NULL_STATE;
- ssl->options.connectState = CONNECT_BEGIN;
- ssl->options.acceptState = ACCEPT_FIRST_REPLY_DONE;
- ssl->options.handShakeState = NULL_STATE;
- ssl->secure_renegotiation->cache_status = SCR_CACHE_NEEDED;
-
- ret = InitHandshakeHashes(ssl);
- if (ret != 0)
- return ret;
- }
-#endif
-
- /* sanity check msg received */
- if ( (ret = SanityCheckMsgReceived(ssl, type)) != 0) {
- WOLFSSL_MSG("Sanity Check on handshake message type received failed");
- return ret;
- }
-
-#if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- /* add name later, add on record and handshake header part back on */
- if (ssl->toInfoOn) {
- int add = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- AddPacketInfo(ssl, 0, handshake, input + *inOutIdx - add,
- size + add, READ_PROTO, ssl->heap);
- #ifdef WOLFSSL_CALLBACKS
- AddLateRecordHeader(&ssl->curRL, &ssl->timeoutInfo);
- #endif
- }
-#endif
-
- if (ssl->options.handShakeState == HANDSHAKE_DONE && type != hello_request){
- WOLFSSL_MSG("HandShake message after handshake complete");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return OUT_OF_ORDER_E;
- }
-
- if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->options.dtls == 0 &&
- ssl->options.serverState == NULL_STATE && type != server_hello) {
- WOLFSSL_MSG("First server message not server hello");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return OUT_OF_ORDER_E;
- }
-
- if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->options.dtls &&
- type == server_hello_done &&
- ssl->options.serverState < SERVER_HELLO_COMPLETE) {
- WOLFSSL_MSG("Server hello done received before server hello in DTLS");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return OUT_OF_ORDER_E;
- }
-
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- ssl->options.clientState == NULL_STATE && type != client_hello) {
- WOLFSSL_MSG("First client message not client hello");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return OUT_OF_ORDER_E;
- }
-
- /* above checks handshake state */
- /* hello_request not hashed */
- /* Also, skip hashing the client_hello message here for DTLS. It will be
- * hashed later if the DTLS cookie is correct. */
- if (type != hello_request &&
- !(IsDtlsNotSctpMode(ssl) && type == client_hello)
- #ifdef WOLFSSL_ASYNC_CRYPT
- && ssl->error != WC_PENDING_E
- #endif
- #ifdef WOLFSSL_NONBLOCK_OCSP
- && ssl->error != OCSP_WANT_READ
- #endif
- ) {
- ret = HashInput(ssl, input + *inOutIdx, size);
- if (ret != 0) {
- WOLFSSL_MSG("Incomplete handshake hashes");
- return ret;
- }
- }
-
-#ifdef OPENSSL_EXTRA
- if (ssl->CBIS != NULL){
- ssl->cbmode = SSL_CB_MODE_READ;
- ssl->cbtype = type;
- ssl->CBIS(ssl, SSL_CB_ACCEPT_LOOP, SSL_SUCCESS);
- }
-#endif
-
- switch (type) {
-
- case hello_request:
- WOLFSSL_MSG("processing hello request");
- ret = DoHelloRequest(ssl, input, inOutIdx, size, totalSz);
- break;
-
-#ifndef NO_WOLFSSL_CLIENT
- case hello_verify_request:
- WOLFSSL_MSG("processing hello verify request");
- ret = DoHelloVerifyRequest(ssl, input,inOutIdx, size);
- break;
-
- case server_hello:
- WOLFSSL_MSG("processing server hello");
- ret = DoServerHello(ssl, input, inOutIdx, size);
- #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
- ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
- (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
- if (ssl->options.resuming || !IsAtLeastTLSv1_2(ssl) ||
- IsAtLeastTLSv1_3(ssl->version)) {
-
- #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP)
- if (ret != WC_PENDING_E && ret != OCSP_WANT_READ)
- #endif
- {
- ssl->options.cacheMessages = 0;
- if (ssl->hsHashes->messages != NULL) {
- XFREE(ssl->hsHashes->messages, ssl->heap,
- DYNAMIC_TYPE_HASHES);
- ssl->hsHashes->messages = NULL;
- }
- }
- }
- #endif
- break;
-
-#ifndef NO_CERTS
- case certificate_request:
- WOLFSSL_MSG("processing certificate request");
- ret = DoCertificateRequest(ssl, input, inOutIdx, size);
- break;
-#endif
-
- case server_key_exchange:
- WOLFSSL_MSG("processing server key exchange");
- ret = DoServerKeyExchange(ssl, input, inOutIdx, size);
- break;
-
-#ifdef HAVE_SESSION_TICKET
- case session_ticket:
- WOLFSSL_MSG("processing session ticket");
- ret = DoSessionTicket(ssl, input, inOutIdx, size);
- break;
-#endif /* HAVE_SESSION_TICKET */
-#endif
-
-#if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
- !defined(WOLFSSL_NO_CLIENT_AUTH))
- case certificate:
- WOLFSSL_MSG("processing certificate");
- ret = DoCertificate(ssl, input, inOutIdx, size);
- break;
-
- case certificate_status:
- WOLFSSL_MSG("processing certificate status");
- ret = DoCertificateStatus(ssl, input, inOutIdx, size);
- break;
-#endif
-
- case server_hello_done:
- WOLFSSL_MSG("processing server hello done");
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "ServerHelloDone");
- if (ssl->toInfoOn)
- AddLateName("ServerHelloDone", &ssl->timeoutInfo);
- #endif
- ssl->options.serverState = SERVER_HELLODONE_COMPLETE;
- if (IsEncryptionOn(ssl, 0)) {
- *inOutIdx += ssl->keys.padSz;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- *inOutIdx += MacSize(ssl);
- #endif
- }
- if (ssl->options.resuming) {
- WOLFSSL_MSG("Not resuming as thought");
- ssl->options.resuming = 0;
- }
- break;
-
- case finished:
- WOLFSSL_MSG("processing finished");
- ret = DoFinished(ssl, input, inOutIdx, size, totalSz, NO_SNIFF);
- break;
-
-#ifndef NO_WOLFSSL_SERVER
- case client_hello:
- WOLFSSL_MSG("processing client hello");
- ret = DoClientHello(ssl, input, inOutIdx, size);
- #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
- ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
- (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
- if (ssl->options.resuming || !ssl->options.verifyPeer || \
- !IsAtLeastTLSv1_2(ssl) || IsAtLeastTLSv1_3(ssl->version)) {
- #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP)
- if (ret != WC_PENDING_E && ret != OCSP_WANT_READ)
- #endif
- {
- ssl->options.cacheMessages = 0;
- if (ssl->hsHashes->messages != NULL) {
- XFREE(ssl->hsHashes->messages, ssl->heap, DYNAMIC_TYPE_HASHES);
- ssl->hsHashes->messages = NULL;
- }
- }
- }
- #endif
- if (IsEncryptionOn(ssl, 0)) {
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead) {
- word32 digestSz = MacSize(ssl);
- if (*inOutIdx + ssl->keys.padSz + digestSz > totalSz)
- return BUFFER_E;
- *inOutIdx += ssl->keys.padSz + digestSz;
- }
- else
- #endif
- {
- /* access beyond input + size should be checked against totalSz
- */
- if (*inOutIdx + ssl->keys.padSz > totalSz)
- return BUFFER_E;
-
- *inOutIdx += ssl->keys.padSz;
- }
- }
- break;
-
- case client_key_exchange:
- WOLFSSL_MSG("processing client key exchange");
- ret = DoClientKeyExchange(ssl, input, inOutIdx, size);
- break;
-
-#if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)) && !defined(WOLFSSL_NO_CLIENT_AUTH)
- case certificate_verify:
- WOLFSSL_MSG("processing certificate verify");
- ret = DoCertificateVerify(ssl, input, inOutIdx, size);
- break;
-#endif /* (!NO_RSA || ECC || ED25519 || ED448) && !WOLFSSL_NO_CLIENT_AUTH */
-
-#endif /* !NO_WOLFSSL_SERVER */
-
- default:
- WOLFSSL_MSG("Unknown handshake message type");
- ret = UNKNOWN_HANDSHAKE_TYPE;
- break;
- }
- if (ret == 0 && expectedIdx != *inOutIdx) {
- WOLFSSL_MSG("Extra data in handshake message");
- if (!ssl->options.dtls)
- SendAlert(ssl, alert_fatal, decode_error);
- ret = DECODE_E;
- }
-
- if (ret == 0 && ssl->buffers.inputBuffer.dynamicFlag
- #if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP)
- /* do not shrink input for async or non-block */
- && ssl->error != WC_PENDING_E && ssl->error != OCSP_WANT_READ
- #endif
- ) {
- ShrinkInputBuffer(ssl, NO_FORCED_FREE);
- }
-
-#if defined(WOLFSSL_ASYNC_CRYPT) || defined(WOLFSSL_NONBLOCK_OCSP)
- /* if async, offset index so this msg will be processed again */
- if ((ret == WC_PENDING_E || ret == OCSP_WANT_READ) && *inOutIdx > 0) {
- *inOutIdx -= HANDSHAKE_HEADER_SZ;
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- *inOutIdx -= DTLS_HANDSHAKE_EXTRA;
- }
- #endif
- }
-
- /* make sure async error is cleared */
- if (ret == 0 && (ssl->error == WC_PENDING_E || ssl->error == OCSP_WANT_READ)) {
- ssl->error = 0;
- }
-#endif /* WOLFSSL_ASYNC_CRYPT || WOLFSSL_NONBLOCK_OCSP */
-
- WOLFSSL_LEAVE("DoHandShakeMsgType()", ret);
- return ret;
-}
-
-
-static int DoHandShakeMsg(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- word32 totalSz)
-{
- int ret = 0;
- word32 inputLength;
-
- WOLFSSL_ENTER("DoHandShakeMsg()");
-
- if (ssl->arrays == NULL) {
- byte type;
- word32 size;
-
- if (GetHandShakeHeader(ssl,input,inOutIdx,&type, &size, totalSz) != 0)
- return PARSE_ERROR;
-
- ssl->options.handShakeState = type;
-
- return DoHandShakeMsgType(ssl, input, inOutIdx, type, size, totalSz);
- }
-
- inputLength = ssl->buffers.inputBuffer.length - *inOutIdx;
-
- /* If there is a pending fragmented handshake message,
- * pending message size will be non-zero. */
- if (ssl->arrays->pendingMsgSz == 0) {
- byte type;
- word32 size;
-
- if (GetHandShakeHeader(ssl,input, inOutIdx, &type, &size, totalSz) != 0)
- return PARSE_ERROR;
-
- /* Cap the maximum size of a handshake message to something reasonable.
- * By default is the maximum size of a certificate message assuming
- * nine 2048-bit RSA certificates in the chain. */
- if (size > MAX_HANDSHAKE_SZ) {
- WOLFSSL_MSG("Handshake message too large");
- return HANDSHAKE_SIZE_ERROR;
- }
-
- /* size is the size of the certificate message payload */
- if (inputLength - HANDSHAKE_HEADER_SZ < size) {
- ssl->arrays->pendingMsgType = type;
- ssl->arrays->pendingMsgSz = size + HANDSHAKE_HEADER_SZ;
- ssl->arrays->pendingMsg = (byte*)XMALLOC(size + HANDSHAKE_HEADER_SZ,
- ssl->heap,
- DYNAMIC_TYPE_ARRAYS);
- if (ssl->arrays->pendingMsg == NULL)
- return MEMORY_E;
- XMEMCPY(ssl->arrays->pendingMsg,
- input + *inOutIdx - HANDSHAKE_HEADER_SZ,
- inputLength);
- ssl->arrays->pendingMsgOffset = inputLength;
- *inOutIdx += inputLength - HANDSHAKE_HEADER_SZ;
- return 0;
- }
-
- ret = DoHandShakeMsgType(ssl, input, inOutIdx, type, size, totalSz);
- }
- else {
- word32 pendSz =
- ssl->arrays->pendingMsgSz - ssl->arrays->pendingMsgOffset;
-
- /* Catch the case where there may be the remainder of a fragmented
- * handshake message and the next handshake message in the same
- * record. */
- if (inputLength > pendSz)
- inputLength = pendSz;
-
- XMEMCPY(ssl->arrays->pendingMsg + ssl->arrays->pendingMsgOffset,
- input + *inOutIdx, inputLength);
- ssl->arrays->pendingMsgOffset += inputLength;
- *inOutIdx += inputLength;
-
- if (ssl->arrays->pendingMsgOffset == ssl->arrays->pendingMsgSz)
- {
- word32 idx = HANDSHAKE_HEADER_SZ;
- ret = DoHandShakeMsgType(ssl,
- ssl->arrays->pendingMsg,
- &idx, ssl->arrays->pendingMsgType,
- ssl->arrays->pendingMsgSz - idx,
- ssl->arrays->pendingMsgSz);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- /* setup to process fragment again */
- ssl->arrays->pendingMsgOffset -= inputLength;
- *inOutIdx -= inputLength;
- }
- else
- #endif
- {
- XFREE(ssl->arrays->pendingMsg, ssl->heap, DYNAMIC_TYPE_ARRAYS);
- ssl->arrays->pendingMsg = NULL;
- ssl->arrays->pendingMsgSz = 0;
- }
- }
- }
-
- WOLFSSL_LEAVE("DoHandShakeMsg()", ret);
- return ret;
-}
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#ifdef WOLFSSL_DTLS
-
-static WC_INLINE int DtlsCheckWindow(WOLFSSL* ssl)
-{
- word32* window;
- word16 cur_hi, next_hi;
- word32 cur_lo, next_lo, diff;
- int curLT;
- WOLFSSL_DTLS_PEERSEQ* peerSeq = NULL;
-
- if (!ssl->options.haveMcast)
- peerSeq = ssl->keys.peerSeq;
- else {
-#ifdef WOLFSSL_MULTICAST
- WOLFSSL_DTLS_PEERSEQ* p;
- int i;
-
- for (i = 0, p = ssl->keys.peerSeq;
- i < WOLFSSL_DTLS_PEERSEQ_SZ;
- i++, p++) {
-
- if (p->peerId == ssl->keys.curPeerId) {
- peerSeq = p;
- break;
- }
- }
-#endif
- }
-
- if (peerSeq == NULL) {
- WOLFSSL_MSG("Could not find peer sequence");
- return 0;
- }
-
- if (ssl->keys.curEpoch == peerSeq->nextEpoch) {
- next_hi = peerSeq->nextSeq_hi;
- next_lo = peerSeq->nextSeq_lo;
- window = peerSeq->window;
- }
- else if (ssl->keys.curEpoch == peerSeq->nextEpoch - 1) {
- next_hi = peerSeq->prevSeq_hi;
- next_lo = peerSeq->prevSeq_lo;
- window = peerSeq->prevWindow;
- }
- else {
- return 0;
- }
-
- cur_hi = ssl->keys.curSeq_hi;
- cur_lo = ssl->keys.curSeq_lo;
-
- /* If the difference between next and cur is > 2^32, way outside window. */
- if ((cur_hi > next_hi + 1) || (next_hi > cur_hi + 1)) {
- WOLFSSL_MSG("Current record from way too far in the future.");
- return 0;
- }
-
- if (cur_hi == next_hi) {
- curLT = cur_lo < next_lo;
- diff = curLT ? next_lo - cur_lo : cur_lo - next_lo;
- }
- else {
- curLT = cur_hi < next_hi;
- diff = curLT ? cur_lo - next_lo : next_lo - cur_lo;
- }
-
- /* Check to see that the next value is greater than the number of messages
- * trackable in the window, and that the difference between the next
- * expected sequence number and the received sequence number is inside the
- * window. */
- if ((next_hi || next_lo > DTLS_SEQ_BITS) &&
- curLT && (diff > DTLS_SEQ_BITS)) {
-
- WOLFSSL_MSG("Current record sequence number from the past.");
- return 0;
- }
-#ifndef WOLFSSL_DTLS_ALLOW_FUTURE
- else if (!curLT && (diff > DTLS_SEQ_BITS)) {
- WOLFSSL_MSG("Rejecting message too far into the future.");
- return 0;
- }
-#endif
- else if (curLT) {
- word32 idx = diff / DTLS_WORD_BITS;
- word32 newDiff = diff % DTLS_WORD_BITS;
-
- /* verify idx is valid for window array */
- if (idx >= WOLFSSL_DTLS_WINDOW_WORDS) {
- WOLFSSL_MSG("Invalid DTLS windows index");
- return 0;
- }
-
- if (window[idx] & (1 << newDiff)) {
- WOLFSSL_MSG("Current record sequence number already received.");
- return 0;
- }
- }
-
- return 1;
-}
-
-
-#ifdef WOLFSSL_MULTICAST
-static WC_INLINE word32 UpdateHighwaterMark(word32 cur, word32 first,
- word32 second, word32 max)
-{
- word32 newCur = 0;
-
- if (cur < first)
- newCur = first;
- else if (cur < second)
- newCur = second;
- else if (cur < max)
- newCur = max;
-
- return newCur;
-}
-#endif /* WOLFSSL_MULTICAST */
-
-
-static WC_INLINE int DtlsUpdateWindow(WOLFSSL* ssl)
-{
- word32* window;
- word32* next_lo;
- word16* next_hi;
- int curLT;
- word32 cur_lo, diff;
- word16 cur_hi;
- WOLFSSL_DTLS_PEERSEQ* peerSeq = ssl->keys.peerSeq;
-
- cur_hi = ssl->keys.curSeq_hi;
- cur_lo = ssl->keys.curSeq_lo;
-
-#ifdef WOLFSSL_MULTICAST
- if (ssl->options.haveMcast) {
- WOLFSSL_DTLS_PEERSEQ* p;
- int i;
-
- peerSeq = NULL;
- for (i = 0, p = ssl->keys.peerSeq;
- i < WOLFSSL_DTLS_PEERSEQ_SZ;
- i++, p++) {
-
- if (p->peerId == ssl->keys.curPeerId) {
- peerSeq = p;
- break;
- }
- }
-
- if (peerSeq == NULL) {
- WOLFSSL_MSG("Couldn't find that peer ID to update window.");
- return 0;
- }
-
- if (p->highwaterMark && cur_lo >= p->highwaterMark) {
- int cbError = 0;
-
- if (ssl->ctx->mcastHwCb)
- cbError = ssl->ctx->mcastHwCb(p->peerId,
- ssl->ctx->mcastMaxSeq,
- cur_lo, ssl->mcastHwCbCtx);
- if (cbError) {
- WOLFSSL_MSG("Multicast highwater callback returned an error.");
- return MCAST_HIGHWATER_CB_E;
- }
-
- p->highwaterMark = UpdateHighwaterMark(cur_lo,
- ssl->ctx->mcastFirstSeq,
- ssl->ctx->mcastSecondSeq,
- ssl->ctx->mcastMaxSeq);
- }
- }
-#endif
-
- if (ssl->keys.curEpoch == peerSeq->nextEpoch) {
- next_hi = &peerSeq->nextSeq_hi;
- next_lo = &peerSeq->nextSeq_lo;
- window = peerSeq->window;
- }
- else {
- next_hi = &peerSeq->prevSeq_hi;
- next_lo = &peerSeq->prevSeq_lo;
- window = peerSeq->prevWindow;
- }
-
- if (cur_hi == *next_hi) {
- curLT = cur_lo < *next_lo;
- diff = curLT ? *next_lo - cur_lo : cur_lo - *next_lo;
- }
- else {
- curLT = cur_hi < *next_hi;
- diff = curLT ? cur_lo - *next_lo : *next_lo - cur_lo;
- }
-
- if (curLT) {
- word32 idx = diff / DTLS_WORD_BITS;
- word32 newDiff = diff % DTLS_WORD_BITS;
-
- if (idx < WOLFSSL_DTLS_WINDOW_WORDS)
- window[idx] |= (1 << newDiff);
- }
- else {
- if (diff >= DTLS_SEQ_BITS)
- XMEMSET(window, 0, DTLS_SEQ_SZ);
- else {
- word32 idx, newDiff, temp, i;
- word32 oldWindow[WOLFSSL_DTLS_WINDOW_WORDS];
-
- temp = 0;
- diff++;
- idx = diff / DTLS_WORD_BITS;
- newDiff = diff % DTLS_WORD_BITS;
-
- XMEMCPY(oldWindow, window, sizeof(oldWindow));
-
- for (i = 0; i < WOLFSSL_DTLS_WINDOW_WORDS; i++) {
- if (i < idx)
- window[i] = 0;
- else {
- temp |= (oldWindow[i-idx] << newDiff);
- window[i] = temp;
- temp = oldWindow[i-idx] >> (DTLS_WORD_BITS - newDiff - 1);
- }
- }
- }
- window[0] |= 1;
- *next_lo = cur_lo + 1;
- if (*next_lo < cur_lo)
- (*next_hi)++;
- }
-
- return 1;
-}
-
-
-static int DtlsMsgDrain(WOLFSSL* ssl)
-{
- DtlsMsg* item = ssl->dtls_rx_msg_list;
- int ret = 0;
-
- WOLFSSL_ENTER("DtlsMsgDrain()");
-
- /* While there is an item in the store list, and it is the expected
- * message, and it is complete, and there hasn't been an error in the
- * last message... */
- while (item != NULL &&
- ssl->keys.dtls_expected_peer_handshake_number == item->seq &&
- item->fragSz == item->sz &&
- ret == 0) {
- word32 idx = 0;
- ssl->keys.dtls_expected_peer_handshake_number++;
- ret = DoHandShakeMsgType(ssl, item->msg,
- &idx, item->type, item->sz, item->sz);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ssl->keys.dtls_expected_peer_handshake_number--;
- break;
- }
- #endif
- ssl->dtls_rx_msg_list = item->next;
- DtlsMsgDelete(item, ssl->heap);
- item = ssl->dtls_rx_msg_list;
- ssl->dtls_rx_msg_list_sz--;
- }
-
- WOLFSSL_LEAVE("DtlsMsgDrain()", ret);
- return ret;
-}
-
-
-static int DoDtlsHandShakeMsg(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- word32 totalSz)
-{
- byte type;
- word32 size;
- word32 fragOffset, fragSz;
- int ret = 0;
-
- WOLFSSL_ENTER("DoDtlsHandShakeMsg()");
-
- /* process any pending DTLS messages - this flow can happen with async */
- if (ssl->dtls_rx_msg_list != NULL) {
- ret = DtlsMsgDrain(ssl);
- if (ret != 0)
- return ret;
-
- /* if done processing fragment exit with success */
- if (totalSz == *inOutIdx)
- return ret;
- }
-
- /* parse header */
- if (GetDtlsHandShakeHeader(ssl, input, inOutIdx, &type,
- &size, &fragOffset, &fragSz, totalSz) != 0) {
- WOLFSSL_ERROR(PARSE_ERROR);
- return PARSE_ERROR;
- }
-
- /* Cap the maximum size of a handshake message to something reasonable.
- * By default is the maximum size of a certificate message assuming
- * nine 2048-bit RSA certificates in the chain. */
- if (size > MAX_HANDSHAKE_SZ) {
- WOLFSSL_MSG("Handshake message too large");
- return HANDSHAKE_SIZE_ERROR;
- }
-
- /* check that we have complete fragment */
- if (*inOutIdx + fragSz > totalSz) {
- WOLFSSL_ERROR(INCOMPLETE_DATA);
- return INCOMPLETE_DATA;
- }
-
- /* Check the handshake sequence number first. If out of order,
- * add the current message to the list. If the message is in order,
- * but it is a fragment, add the current message to the list, then
- * check the head of the list to see if it is complete, if so, pop
- * it out as the current message. If the message is complete and in
- * order, process it. Check the head of the list to see if it is in
- * order, if so, process it. (Repeat until list exhausted.) If the
- * head is out of order, return for more processing.
- */
- if (ssl->keys.dtls_peer_handshake_number >
- ssl->keys.dtls_expected_peer_handshake_number) {
- /* Current message is out of order. It will get stored in the list.
- * Storing also takes care of defragmentation. If the messages is a
- * client hello, we need to process this out of order; the server
- * is not supposed to keep state, but the second client hello will
- * have a different handshake sequence number than is expected, and
- * the server shouldn't be expecting any particular handshake sequence
- * number. (If the cookie changes multiple times in quick succession,
- * the client could be sending multiple new client hello messages
- * with newer and newer cookies.) */
- if (type != client_hello) {
- if (ssl->dtls_rx_msg_list_sz < DTLS_POOL_SZ) {
- DtlsMsgStore(ssl, ssl->keys.dtls_peer_handshake_number,
- input + *inOutIdx, size, type,
- fragOffset, fragSz, ssl->heap);
- }
- *inOutIdx += fragSz;
- ret = 0;
- }
- else {
- ret = DoHandShakeMsgType(ssl, input, inOutIdx, type, size, totalSz);
- if (ret == 0) {
- ssl->keys.dtls_expected_peer_handshake_number =
- ssl->keys.dtls_peer_handshake_number + 1;
- }
- }
- }
- else if (ssl->keys.dtls_peer_handshake_number <
- ssl->keys.dtls_expected_peer_handshake_number) {
- /* Already saw this message and processed it. It can be ignored. */
- *inOutIdx += fragSz;
- if(type == finished ) {
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead) {
- word32 digestSz = MacSize(ssl);
- if (*inOutIdx + ssl->keys.padSz + digestSz > totalSz)
- return BUFFER_E;
- *inOutIdx += ssl->keys.padSz + digestSz;
- }
- else
- #endif
- {
- if (*inOutIdx + ssl->keys.padSz > totalSz) {
- WOLFSSL_ERROR(BUFFER_E);
- return BUFFER_E;
- }
- *inOutIdx += ssl->keys.padSz;
- }
- }
- if (IsDtlsNotSctpMode(ssl) &&
- VerifyForDtlsMsgPoolSend(ssl, type, fragOffset)) {
-
- ret = DtlsMsgPoolSend(ssl, 0);
- }
- }
- else if (fragSz < size) {
- /* Since this branch is in order, but fragmented, dtls_rx_msg_list will
- * be pointing to the message with this fragment in it. Check it to see
- * if it is completed. */
- if (ssl->dtls_rx_msg_list_sz < DTLS_POOL_SZ) {
- DtlsMsgStore(ssl, ssl->keys.dtls_peer_handshake_number,
- input + *inOutIdx, size, type,
- fragOffset, fragSz, ssl->heap);
- }
- *inOutIdx += fragSz;
- ret = 0;
- if (ssl->dtls_rx_msg_list != NULL &&
- ssl->dtls_rx_msg_list->fragSz >= ssl->dtls_rx_msg_list->sz)
- ret = DtlsMsgDrain(ssl);
- }
- else {
- /* This branch is in order next, and a complete message. */
- ret = DoHandShakeMsgType(ssl, input, inOutIdx, type, size, totalSz);
- if (ret == 0) {
- if (type != client_hello || !IsDtlsNotSctpMode(ssl))
- ssl->keys.dtls_expected_peer_handshake_number++;
- if (ssl->dtls_rx_msg_list != NULL) {
- ret = DtlsMsgDrain(ssl);
- }
- }
- }
-
- WOLFSSL_LEAVE("DoDtlsHandShakeMsg()", ret);
- return ret;
-}
-#endif
-
-#ifndef WOLFSSL_NO_TLS12
-
-#ifdef HAVE_AEAD
-
-#if !defined(NO_PUBLIC_GCM_SET_IV) && \
- (((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))) || \
- (defined(HAVE_POLY1305) && defined(HAVE_CHACHA)))
-static WC_INLINE void AeadIncrementExpIV(WOLFSSL* ssl)
-{
- int i;
- for (i = AEAD_MAX_EXP_SZ-1; i >= 0; i--) {
- if (++ssl->keys.aead_exp_IV[i]) return;
- }
-}
-#endif
-
-
-#if defined(HAVE_POLY1305) && defined(HAVE_CHACHA)
-/* Used for the older version of creating AEAD tags with Poly1305 */
-static int Poly1305TagOld(WOLFSSL* ssl, byte* additional, const byte* out,
- byte* cipher, word16 sz, byte* tag)
-{
- int ret = 0;
- int msglen = (sz - ssl->specs.aead_mac_size);
- word32 keySz = 32;
- byte padding[8]; /* used to temporarily store lengths */
-
-#ifdef CHACHA_AEAD_TEST
- printf("Using old version of poly1305 input.\n");
-#endif
-
- if (msglen < 0)
- return INPUT_CASE_ERROR;
-
- if ((ret = wc_Poly1305SetKey(ssl->auth.poly1305, cipher, keySz)) != 0)
- return ret;
-
- if ((ret = wc_Poly1305Update(ssl->auth.poly1305, additional,
- AEAD_AUTH_DATA_SZ)) != 0)
- return ret;
-
- /* length of additional input plus padding */
- XMEMSET(padding, 0, sizeof(padding));
- padding[0] = AEAD_AUTH_DATA_SZ;
- if ((ret = wc_Poly1305Update(ssl->auth.poly1305, padding,
- sizeof(padding))) != 0)
- return ret;
-
-
- /* add cipher info and then its length */
- XMEMSET(padding, 0, sizeof(padding));
- if ((ret = wc_Poly1305Update(ssl->auth.poly1305, out, msglen)) != 0)
- return ret;
-
- /* 32 bit size of cipher to 64 bit endian */
- padding[0] = msglen & 0xff;
- padding[1] = (msglen >> 8) & 0xff;
- padding[2] = ((word32)msglen >> 16) & 0xff;
- padding[3] = ((word32)msglen >> 24) & 0xff;
- if ((ret = wc_Poly1305Update(ssl->auth.poly1305, padding, sizeof(padding)))
- != 0)
- return ret;
-
- /* generate tag */
- if ((ret = wc_Poly1305Final(ssl->auth.poly1305, tag)) != 0)
- return ret;
-
- return ret;
-}
-
-
-/* When the flag oldPoly is not set this follows RFC7905. When oldPoly is set
- * the implementation follows an older draft for creating the nonce and MAC.
- * The flag oldPoly gets set automatically depending on what cipher suite was
- * negotiated in the handshake. This is able to be done because the IDs for the
- * cipher suites was updated in RFC7905 giving unique values for the older
- * draft in comparison to the more recent RFC.
- *
- * ssl WOLFSSL structure to get cipher and TLS state from
- * out output buffer to hold encrypted data
- * input data to encrypt
- * sz size of input
- *
- * Return 0 on success negative values in error case
- */
-static int ChachaAEADEncrypt(WOLFSSL* ssl, byte* out, const byte* input,
- word16 sz)
-{
- const byte* additionalSrc = input - RECORD_HEADER_SZ;
- int ret = 0;
- word32 msgLen = (sz - ssl->specs.aead_mac_size);
- byte tag[POLY1305_AUTH_SZ];
- byte add[AEAD_AUTH_DATA_SZ];
- byte nonce[CHACHA20_NONCE_SZ];
- byte poly[CHACHA20_256_KEY_SIZE]; /* generated key for poly1305 */
- #ifdef CHACHA_AEAD_TEST
- int i;
- #endif
-
- XMEMSET(tag, 0, sizeof(tag));
- XMEMSET(nonce, 0, sizeof(nonce));
- XMEMSET(poly, 0, sizeof(poly));
- XMEMSET(add, 0, sizeof(add));
-
- /* opaque SEQ number stored for AD */
- WriteSEQ(ssl, CUR_ORDER, add);
-
- if (ssl->options.oldPoly != 0) {
- /* get nonce. SEQ should not be incremented again here */
- XMEMCPY(nonce + CHACHA20_OLD_OFFSET, add, OPAQUE32_LEN * 2);
- }
-
- /* Store the type, version. Unfortunately, they are in
- * the input buffer ahead of the plaintext. */
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- additionalSrc -= DTLS_HANDSHAKE_EXTRA;
- }
- #endif
-
- /* add TLS message size to additional data */
- add[AEAD_AUTH_DATA_SZ - 2] = (msgLen >> 8) & 0xff;
- add[AEAD_AUTH_DATA_SZ - 1] = msgLen & 0xff;
-
- XMEMCPY(add + AEAD_TYPE_OFFSET, additionalSrc, 3);
-
- #ifdef CHACHA_AEAD_TEST
- printf("Encrypt Additional : ");
- for (i = 0; i < AEAD_AUTH_DATA_SZ; i++) {
- printf("%02x", add[i]);
- }
- printf("\n\n");
- printf("input before encryption :\n");
- for (i = 0; i < sz; i++) {
- printf("%02x", input[i]);
- if ((i + 1) % 16 == 0)
- printf("\n");
- }
- printf("\n");
- #endif
-
- if (ssl->options.oldPoly == 0) {
- /* nonce is formed by 4 0x00 byte padded to the left followed by 8 byte
- * record sequence number XORed with client_write_IV/server_write_IV */
- XMEMCPY(nonce, ssl->keys.aead_enc_imp_IV, CHACHA20_IMP_IV_SZ);
- nonce[4] ^= add[0];
- nonce[5] ^= add[1];
- nonce[6] ^= add[2];
- nonce[7] ^= add[3];
- nonce[8] ^= add[4];
- nonce[9] ^= add[5];
- nonce[10] ^= add[6];
- nonce[11] ^= add[7];
- }
-
- /* set the nonce for chacha and get poly1305 key */
- if ((ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 0)) != 0) {
- ForceZero(nonce, CHACHA20_NONCE_SZ);
- return ret;
- }
-
- /* create Poly1305 key using chacha20 keystream */
- if ((ret = wc_Chacha_Process(ssl->encrypt.chacha, poly,
- poly, sizeof(poly))) != 0) {
- ForceZero(nonce, CHACHA20_NONCE_SZ);
- return ret;
- }
-
- /* set the counter after getting poly1305 key */
- if ((ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 1)) != 0) {
- ForceZero(nonce, CHACHA20_NONCE_SZ);
- return ret;
- }
- ForceZero(nonce, CHACHA20_NONCE_SZ); /* done with nonce, clear it */
-
- /* encrypt the plain text */
- if ((ret = wc_Chacha_Process(ssl->encrypt.chacha, out,
- input, msgLen)) != 0) {
- ForceZero(poly, sizeof(poly));
- return ret;
- }
-
- /* get the poly1305 tag using either old padding scheme or more recent */
- if (ssl->options.oldPoly != 0) {
- if ((ret = Poly1305TagOld(ssl, add, (const byte* )out,
- poly, sz, tag)) != 0) {
- ForceZero(poly, sizeof(poly));
- return ret;
- }
- }
- else {
- if ((ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly,
- sizeof(poly))) != 0) {
- ForceZero(poly, sizeof(poly));
- return ret;
- }
- if ((ret = wc_Poly1305_MAC(ssl->auth.poly1305, add,
- sizeof(add), out, msgLen, tag, sizeof(tag))) != 0) {
- ForceZero(poly, sizeof(poly));
- return ret;
- }
- }
- ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */
-
- /* append tag to ciphertext */
- XMEMCPY(out + msgLen, tag, sizeof(tag));
-
- AeadIncrementExpIV(ssl);
-
- #ifdef CHACHA_AEAD_TEST
- printf("mac tag :\n");
- for (i = 0; i < 16; i++) {
- printf("%02x", tag[i]);
- if ((i + 1) % 16 == 0)
- printf("\n");
- }
- printf("\n\noutput after encrypt :\n");
- for (i = 0; i < sz; i++) {
- printf("%02x", out[i]);
- if ((i + 1) % 16 == 0)
- printf("\n");
- }
- printf("\n");
- #endif
-
- return ret;
-}
-
-
-/* When the flag oldPoly is not set this follows RFC7905. When oldPoly is set
- * the implementation follows an older draft for creating the nonce and MAC.
- * The flag oldPoly gets set automatically depending on what cipher suite was
- * negotiated in the handshake. This is able to be done because the IDs for the
- * cipher suites was updated in RFC7905 giving unique values for the older
- * draft in comparison to the more recent RFC.
- *
- * ssl WOLFSSL structure to get cipher and TLS state from
- * plain output buffer to hold decrypted data
- * input data to decrypt
- * sz size of input
- *
- * Return 0 on success negative values in error case
- */
-static int ChachaAEADDecrypt(WOLFSSL* ssl, byte* plain, const byte* input,
- word16 sz)
-{
- byte add[AEAD_AUTH_DATA_SZ];
- byte nonce[CHACHA20_NONCE_SZ];
- byte tag[POLY1305_AUTH_SZ];
- byte poly[CHACHA20_256_KEY_SIZE]; /* generated key for mac */
- int ret = 0;
- int msgLen = (sz - ssl->specs.aead_mac_size);
-
- #ifdef CHACHA_AEAD_TEST
- int i;
- printf("input before decrypt :\n");
- for (i = 0; i < sz; i++) {
- printf("%02x", input[i]);
- if ((i + 1) % 16 == 0)
- printf("\n");
- }
- printf("\n");
- #endif
-
- XMEMSET(tag, 0, sizeof(tag));
- XMEMSET(poly, 0, sizeof(poly));
- XMEMSET(nonce, 0, sizeof(nonce));
- XMEMSET(add, 0, sizeof(add));
-
- /* sequence number field is 64-bits */
- WriteSEQ(ssl, PEER_ORDER, add);
-
- if (ssl->options.oldPoly != 0) {
- /* get nonce, SEQ should not be incremented again here */
- XMEMCPY(nonce + CHACHA20_OLD_OFFSET, add, OPAQUE32_LEN * 2);
- }
-
- /* get AD info */
- /* Store the type, version. */
- add[AEAD_TYPE_OFFSET] = ssl->curRL.type;
- add[AEAD_VMAJ_OFFSET] = ssl->curRL.pvMajor;
- add[AEAD_VMIN_OFFSET] = ssl->curRL.pvMinor;
-
- /* add TLS message size to additional data */
- add[AEAD_AUTH_DATA_SZ - 2] = (msgLen >> 8) & 0xff;
- add[AEAD_AUTH_DATA_SZ - 1] = msgLen & 0xff;
-
- #ifdef CHACHA_AEAD_TEST
- printf("Decrypt Additional : ");
- for (i = 0; i < AEAD_AUTH_DATA_SZ; i++) {
- printf("%02x", add[i]);
- }
- printf("\n\n");
- #endif
-
- if (ssl->options.oldPoly == 0) {
- /* nonce is formed by 4 0x00 byte padded to the left followed by 8 byte
- * record sequence number XORed with client_write_IV/server_write_IV */
- XMEMCPY(nonce, ssl->keys.aead_dec_imp_IV, CHACHA20_IMP_IV_SZ);
- nonce[4] ^= add[0];
- nonce[5] ^= add[1];
- nonce[6] ^= add[2];
- nonce[7] ^= add[3];
- nonce[8] ^= add[4];
- nonce[9] ^= add[5];
- nonce[10] ^= add[6];
- nonce[11] ^= add[7];
- }
-
- /* set nonce and get poly1305 key */
- if ((ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 0)) != 0) {
- ForceZero(nonce, CHACHA20_NONCE_SZ);
- return ret;
- }
-
- /* use chacha20 keystream to get poly1305 key for tag */
- if ((ret = wc_Chacha_Process(ssl->decrypt.chacha, poly,
- poly, sizeof(poly))) != 0) {
- ForceZero(nonce, CHACHA20_NONCE_SZ);
- return ret;
- }
-
- /* set counter after getting poly1305 key */
- if ((ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 1)) != 0) {
- ForceZero(nonce, CHACHA20_NONCE_SZ);
- return ret;
- }
- ForceZero(nonce, CHACHA20_NONCE_SZ); /* done with nonce, clear it */
-
- /* get the tag using Poly1305 */
- if (ssl->options.oldPoly != 0) {
- if ((ret = Poly1305TagOld(ssl, add, input, poly, sz, tag)) != 0) {
- ForceZero(poly, sizeof(poly));
- return ret;
- }
- }
- else {
- if ((ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly,
- sizeof(poly))) != 0) {
- ForceZero(poly, sizeof(poly));
- return ret;
- }
- if ((ret = wc_Poly1305_MAC(ssl->auth.poly1305, add,
- sizeof(add), (byte*)input, msgLen, tag, sizeof(tag))) != 0) {
- ForceZero(poly, sizeof(poly));
- return ret;
- }
- }
- ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */
-
- /* check tag sent along with packet */
- if (ConstantCompare(input + msgLen, tag, ssl->specs.aead_mac_size) != 0) {
- WOLFSSL_MSG("MAC did not match");
- if (!ssl->options.dtls)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- return VERIFY_MAC_ERROR;
- }
-
- /* if the tag was good decrypt message */
- if ((ret = wc_Chacha_Process(ssl->decrypt.chacha, plain,
- input, msgLen)) != 0)
- return ret;
-
- #ifdef CHACHA_AEAD_TEST
- printf("plain after decrypt :\n");
- for (i = 0; i < sz; i++) {
- printf("%02x", plain[i]);
- if ((i + 1) % 16 == 0)
- printf("\n");
- }
- printf("\n");
- #endif
-
- return ret;
-}
-#endif /* HAVE_CHACHA && HAVE_POLY1305 */
-#endif /* HAVE_AEAD */
-
-
-#if defined(BUILD_AESGCM) || defined(HAVE_AESCCM)
-
-#if !defined(NO_GCM_ENCRYPT_EXTRA) && \
- ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
-/* The following type is used to share code between AES-GCM and AES-CCM. */
- typedef int (*AesAuthEncryptFunc)(Aes* aes, byte* out,
- const byte* in, word32 sz,
- byte* iv, word32 ivSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz);
- #define AES_AUTH_ENCRYPT_FUNC AesAuthEncryptFunc
- #define AES_GCM_ENCRYPT wc_AesGcmEncrypt_ex
- #define AES_CCM_ENCRYPT wc_AesCcmEncrypt_ex
-#else
- #define AES_AUTH_ENCRYPT_FUNC wc_AesAuthEncryptFunc
- #define AES_GCM_ENCRYPT wc_AesGcmEncrypt
- #define AES_CCM_ENCRYPT wc_AesCcmEncrypt
-#endif
-
-#endif
-
-
-static WC_INLINE int EncryptDo(WOLFSSL* ssl, byte* out, const byte* input,
- word16 sz, int asyncOkay)
-{
- int ret = 0;
-#ifdef WOLFSSL_ASYNC_CRYPT
- WC_ASYNC_DEV* asyncDev = NULL;
- word32 event_flags = WC_ASYNC_FLAG_CALL_AGAIN;
-#else
- (void)asyncOkay;
-#endif
-
- (void)out;
- (void)input;
- (void)sz;
-
- switch (ssl->specs.bulk_cipher_algorithm) {
- #ifdef BUILD_ARC4
- case wolfssl_rc4:
- wc_Arc4Process(ssl->encrypt.arc4, out, input, sz);
- break;
- #endif
-
- #ifdef BUILD_DES3
- case wolfssl_triple_des:
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- asyncDev = &ssl->encrypt.des3->asyncDev;
- ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags);
- if (ret != 0)
- break;
- #endif
-
- ret = wc_Des3_CbcEncrypt(ssl->encrypt.des3, out, input, sz);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E && asyncOkay) {
- ret = wolfSSL_AsyncPush(ssl, asyncDev);
- }
- #endif
- break;
- #endif
-
- #if defined(BUILD_AES) && defined(HAVE_AES_CBC)
- case wolfssl_aes:
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- asyncDev = &ssl->encrypt.aes->asyncDev;
- ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags);
- if (ret != 0)
- break;
- #endif
- #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- if (tsip_useable(ssl)) {
- ret = wc_tsip_AesCbcEncrypt(ssl->encrypt.aes, out, input, sz);
- } else
- #endif
- ret = wc_AesCbcEncrypt(ssl->encrypt.aes, out, input, sz);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E && asyncOkay) {
- ret = wolfSSL_AsyncPush(ssl, asyncDev);
- }
- #endif
- break;
- #endif
-
- #if defined(BUILD_AESGCM) || defined(HAVE_AESCCM)
- case wolfssl_aes_gcm:
- case wolfssl_aes_ccm:/* GCM AEAD macros use same size as CCM */
- {
- AES_AUTH_ENCRYPT_FUNC aes_auth_fn;
- const byte* additionalSrc;
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- asyncDev = &ssl->encrypt.aes->asyncDev;
- ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags);
- if (ret != 0)
- break;
- #endif
-
- #if defined(BUILD_AESGCM) && defined(HAVE_AESCCM)
- aes_auth_fn = (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm)
- ? AES_GCM_ENCRYPT : AES_CCM_ENCRYPT;
- #elif defined(BUILD_AESGCM)
- aes_auth_fn = AES_GCM_ENCRYPT;
- #else
- aes_auth_fn = AES_CCM_ENCRYPT;
- #endif
- additionalSrc = input - 5;
-
- XMEMSET(ssl->encrypt.additional, 0, AEAD_AUTH_DATA_SZ);
-
- /* sequence number field is 64-bits */
- WriteSEQ(ssl, CUR_ORDER, ssl->encrypt.additional);
-
- /* Store the type, version. Unfortunately, they are in
- * the input buffer ahead of the plaintext. */
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- additionalSrc -= DTLS_HANDSHAKE_EXTRA;
- }
- #endif
- XMEMCPY(ssl->encrypt.additional + AEAD_TYPE_OFFSET,
- additionalSrc, 3);
-
- /* Store the length of the plain text minus the explicit
- * IV length minus the authentication tag size. */
- c16toa(sz - AESGCM_EXP_IV_SZ - ssl->specs.aead_mac_size,
- ssl->encrypt.additional + AEAD_LEN_OFFSET);
-#if !defined(NO_PUBLIC_GCM_SET_IV) && \
- ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)))
- XMEMCPY(ssl->encrypt.nonce,
- ssl->keys.aead_enc_imp_IV, AESGCM_IMP_IV_SZ);
- XMEMCPY(ssl->encrypt.nonce + AESGCM_IMP_IV_SZ,
- ssl->keys.aead_exp_IV, AESGCM_EXP_IV_SZ);
-#endif
- ret = aes_auth_fn(ssl->encrypt.aes,
- out + AESGCM_EXP_IV_SZ, input + AESGCM_EXP_IV_SZ,
- sz - AESGCM_EXP_IV_SZ - ssl->specs.aead_mac_size,
- ssl->encrypt.nonce, AESGCM_NONCE_SZ,
- out + sz - ssl->specs.aead_mac_size,
- ssl->specs.aead_mac_size,
- ssl->encrypt.additional, AEAD_AUTH_DATA_SZ);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E && asyncOkay) {
- ret = wolfSSL_AsyncPush(ssl, asyncDev);
- }
- #endif
-#if !defined(NO_PUBLIC_GCM_SET_IV) && \
- ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
- XMEMCPY(out,
- ssl->encrypt.nonce + AESGCM_IMP_IV_SZ, AESGCM_EXP_IV_SZ);
-#endif
- }
- break;
- #endif /* BUILD_AESGCM || HAVE_AESCCM */
-
- #ifdef HAVE_CAMELLIA
- case wolfssl_camellia:
- ret = wc_CamelliaCbcEncrypt(ssl->encrypt.cam, out, input, sz);
- break;
- #endif
-
- #ifdef HAVE_HC128
- case wolfssl_hc128:
- ret = wc_Hc128_Process(ssl->encrypt.hc128, out, input, sz);
- break;
- #endif
-
- #ifdef BUILD_RABBIT
- case wolfssl_rabbit:
- ret = wc_RabbitProcess(ssl->encrypt.rabbit, out, input, sz);
- break;
- #endif
-
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- case wolfssl_chacha:
- ret = ChachaAEADEncrypt(ssl, out, input, sz);
- break;
- #endif
-
- #ifdef HAVE_NULL_CIPHER
- case wolfssl_cipher_null:
- if (input != out) {
- XMEMMOVE(out, input, sz);
- }
- break;
- #endif
-
- #ifdef HAVE_IDEA
- case wolfssl_idea:
- ret = wc_IdeaCbcEncrypt(ssl->encrypt.idea, out, input, sz);
- break;
- #endif
-
- default:
- WOLFSSL_MSG("wolfSSL Encrypt programming error");
- ret = ENCRYPT_ERROR;
- }
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* if async is not okay, then block */
- if (ret == WC_PENDING_E && !asyncOkay) {
- ret = wc_AsyncWait(ret, asyncDev, event_flags);
- }
-#endif
-
- return ret;
-}
-
-static WC_INLINE int Encrypt(WOLFSSL* ssl, byte* out, const byte* input, word16 sz,
- int asyncOkay)
-{
- int ret = 0;
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ssl->error == WC_PENDING_E) {
- ssl->error = 0; /* clear async */
- }
-#endif
-
- switch (ssl->encrypt.state) {
- case CIPHER_STATE_BEGIN:
- {
- if (ssl->encrypt.setup == 0) {
- WOLFSSL_MSG("Encrypt ciphers not setup");
- return ENCRYPT_ERROR;
- }
-
- #ifdef HAVE_FUZZER
- if (ssl->fuzzerCb)
- ssl->fuzzerCb(ssl, input, sz, FUZZ_ENCRYPT, ssl->fuzzerCtx);
- #endif
-
- #if defined(BUILD_AESGCM) || defined(HAVE_AESCCM)
- /* make sure AES GCM/CCM memory is allocated */
- /* free for these happens in FreeCiphers */
- if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_ccm ||
- ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm) {
- /* make sure auth iv and auth are allocated */
- if (ssl->encrypt.additional == NULL)
- ssl->encrypt.additional = (byte*)XMALLOC(AEAD_AUTH_DATA_SZ,
- ssl->heap, DYNAMIC_TYPE_AES_BUFFER);
- if (ssl->encrypt.nonce == NULL)
- ssl->encrypt.nonce = (byte*)XMALLOC(AESGCM_NONCE_SZ,
- ssl->heap, DYNAMIC_TYPE_AES_BUFFER);
- if (ssl->encrypt.additional == NULL ||
- ssl->encrypt.nonce == NULL) {
- return MEMORY_E;
- }
- }
- #endif /* BUILD_AESGCM || HAVE_AESCCM */
-
- /* Advance state and proceed */
- ssl->encrypt.state = CIPHER_STATE_DO;
- }
- FALL_THROUGH;
-
- case CIPHER_STATE_DO:
- {
- ret = EncryptDo(ssl, out, input, sz, asyncOkay);
-
- /* Advance state */
- ssl->encrypt.state = CIPHER_STATE_END;
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* If pending, then leave and return will resume below */
- if (ret == WC_PENDING_E) {
- return ret;
- }
- #endif
- }
- FALL_THROUGH;
-
- case CIPHER_STATE_END:
- {
- #if defined(BUILD_AESGCM) || defined(HAVE_AESCCM)
- if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_ccm ||
- ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm)
- {
- /* finalize authentication cipher */
-#if !defined(NO_PUBLIC_GCM_SET_IV) && \
- ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)))
- AeadIncrementExpIV(ssl);
-#endif
- if (ssl->encrypt.nonce)
- ForceZero(ssl->encrypt.nonce, AESGCM_NONCE_SZ);
- }
- #endif /* BUILD_AESGCM || HAVE_AESCCM */
- break;
- }
- }
-
- /* Reset state */
- ssl->encrypt.state = CIPHER_STATE_BEGIN;
-
- return ret;
-}
-
-
-static WC_INLINE int DecryptDo(WOLFSSL* ssl, byte* plain, const byte* input,
- word16 sz)
-{
- int ret = 0;
-
- (void)plain;
- (void)input;
- (void)sz;
-
- switch (ssl->specs.bulk_cipher_algorithm)
- {
- #ifdef BUILD_ARC4
- case wolfssl_rc4:
- wc_Arc4Process(ssl->decrypt.arc4, plain, input, sz);
- break;
- #endif
-
- #ifdef BUILD_DES3
- case wolfssl_triple_des:
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.des3->asyncDev,
- WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- break;
- #endif
-
- ret = wc_Des3_CbcDecrypt(ssl->decrypt.des3, plain, input, sz);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.des3->asyncDev);
- }
- #endif
- break;
- #endif
-
- #if defined(BUILD_AES) && defined(HAVE_AES_CBC)
- case wolfssl_aes:
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev,
- WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- break;
- #endif
- #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- if (tsip_useable(ssl)) {
- ret = wc_tsip_AesCbcDecrypt(ssl->decrypt.aes, plain, input, sz);
- } else
- #endif
- ret = wc_AesCbcDecrypt(ssl->decrypt.aes, plain, input, sz);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.aes->asyncDev);
- }
- #endif
- break;
- #endif
-
- #if defined(BUILD_AESGCM) || defined(HAVE_AESCCM)
- case wolfssl_aes_gcm:
- case wolfssl_aes_ccm: /* GCM AEAD macros use same size as CCM */
- {
- wc_AesAuthDecryptFunc aes_auth_fn;
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev,
- WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- break;
- #endif
-
- #if defined(BUILD_AESGCM) && defined(HAVE_AESCCM)
- aes_auth_fn = (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm)
- ? wc_AesGcmDecrypt : wc_AesCcmDecrypt;
- #elif defined(BUILD_AESGCM)
- aes_auth_fn = wc_AesGcmDecrypt;
- #else
- aes_auth_fn = wc_AesCcmDecrypt;
- #endif
-
- XMEMSET(ssl->decrypt.additional, 0, AEAD_AUTH_DATA_SZ);
-
- /* sequence number field is 64-bits */
- WriteSEQ(ssl, PEER_ORDER, ssl->decrypt.additional);
-
- ssl->decrypt.additional[AEAD_TYPE_OFFSET] = ssl->curRL.type;
- ssl->decrypt.additional[AEAD_VMAJ_OFFSET] = ssl->curRL.pvMajor;
- ssl->decrypt.additional[AEAD_VMIN_OFFSET] = ssl->curRL.pvMinor;
-
- c16toa(sz - AESGCM_EXP_IV_SZ - ssl->specs.aead_mac_size,
- ssl->decrypt.additional + AEAD_LEN_OFFSET);
- XMEMCPY(ssl->decrypt.nonce, ssl->keys.aead_dec_imp_IV,
- AESGCM_IMP_IV_SZ);
- XMEMCPY(ssl->decrypt.nonce + AESGCM_IMP_IV_SZ, input,
- AESGCM_EXP_IV_SZ);
- if ((ret = aes_auth_fn(ssl->decrypt.aes,
- plain + AESGCM_EXP_IV_SZ,
- input + AESGCM_EXP_IV_SZ,
- sz - AESGCM_EXP_IV_SZ - ssl->specs.aead_mac_size,
- ssl->decrypt.nonce, AESGCM_NONCE_SZ,
- input + sz - ssl->specs.aead_mac_size,
- ssl->specs.aead_mac_size,
- ssl->decrypt.additional, AEAD_AUTH_DATA_SZ)) < 0) {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl, &ssl->decrypt.aes->asyncDev);
- }
- #endif
- }
- }
- break;
- #endif /* BUILD_AESGCM || HAVE_AESCCM */
-
- #ifdef HAVE_CAMELLIA
- case wolfssl_camellia:
- ret = wc_CamelliaCbcDecrypt(ssl->decrypt.cam, plain, input, sz);
- break;
- #endif
-
- #ifdef HAVE_HC128
- case wolfssl_hc128:
- ret = wc_Hc128_Process(ssl->decrypt.hc128, plain, input, sz);
- break;
- #endif
-
- #ifdef BUILD_RABBIT
- case wolfssl_rabbit:
- ret = wc_RabbitProcess(ssl->decrypt.rabbit, plain, input, sz);
- break;
- #endif
-
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- case wolfssl_chacha:
- ret = ChachaAEADDecrypt(ssl, plain, input, sz);
- break;
- #endif
-
- #ifdef HAVE_NULL_CIPHER
- case wolfssl_cipher_null:
- if (input != plain) {
- XMEMMOVE(plain, input, sz);
- }
- break;
- #endif
-
- #ifdef HAVE_IDEA
- case wolfssl_idea:
- ret = wc_IdeaCbcDecrypt(ssl->decrypt.idea, plain, input, sz);
- break;
- #endif
-
- default:
- WOLFSSL_MSG("wolfSSL Decrypt programming error");
- ret = DECRYPT_ERROR;
- }
-
- return ret;
-}
-
-static WC_INLINE int Decrypt(WOLFSSL* ssl, byte* plain, const byte* input,
- word16 sz)
-{
- int ret = 0;
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->decrypt.state);
- if (ret != WC_NOT_PENDING_E) {
- /* check for still pending */
- if (ret == WC_PENDING_E)
- return ret;
-
- ssl->error = 0; /* clear async */
-
- /* let failures through so CIPHER_STATE_END logic is run */
- }
- else
-#endif
- {
- /* Reset state */
- ret = 0;
- ssl->decrypt.state = CIPHER_STATE_BEGIN;
- }
-
- switch (ssl->decrypt.state) {
- case CIPHER_STATE_BEGIN:
- {
- if (ssl->decrypt.setup == 0) {
- WOLFSSL_MSG("Decrypt ciphers not setup");
- return DECRYPT_ERROR;
- }
-
- #if defined(BUILD_AESGCM) || defined(HAVE_AESCCM)
- /* make sure AES GCM/CCM memory is allocated */
- /* free for these happens in FreeCiphers */
- if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_ccm ||
- ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm) {
- /* make sure auth iv and auth are allocated */
- if (ssl->decrypt.additional == NULL)
- ssl->decrypt.additional = (byte*)XMALLOC(AEAD_AUTH_DATA_SZ,
- ssl->heap, DYNAMIC_TYPE_AES_BUFFER);
- if (ssl->decrypt.nonce == NULL)
- ssl->decrypt.nonce = (byte*)XMALLOC(AESGCM_NONCE_SZ,
- ssl->heap, DYNAMIC_TYPE_AES_BUFFER);
- if (ssl->decrypt.additional == NULL ||
- ssl->decrypt.nonce == NULL) {
- return MEMORY_E;
- }
- }
- #endif /* BUILD_AESGCM || HAVE_AESCCM */
-
- /* Advance state and proceed */
- ssl->decrypt.state = CIPHER_STATE_DO;
- }
- FALL_THROUGH;
- case CIPHER_STATE_DO:
- {
- ret = DecryptDo(ssl, plain, input, sz);
-
- /* Advance state */
- ssl->decrypt.state = CIPHER_STATE_END;
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* If pending, leave and return below */
- if (ret == WC_PENDING_E) {
- return ret;
- }
- #endif
- }
- FALL_THROUGH;
- case CIPHER_STATE_END:
- {
- #if defined(BUILD_AESGCM) || defined(HAVE_AESCCM)
- /* make sure AES GCM/CCM nonce is cleared */
- if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes_ccm ||
- ssl->specs.bulk_cipher_algorithm == wolfssl_aes_gcm) {
- if (ssl->decrypt.nonce)
- ForceZero(ssl->decrypt.nonce, AESGCM_NONCE_SZ);
-
- if (ret < 0)
- ret = VERIFY_MAC_ERROR;
- }
- #endif /* BUILD_AESGCM || HAVE_AESCCM */
- break;
- }
- }
-
- /* Reset state */
- ssl->decrypt.state = CIPHER_STATE_BEGIN;
-
- /* handle mac error case */
- if (ret == VERIFY_MAC_ERROR) {
- if (!ssl->options.dtls)
- SendAlert(ssl, alert_fatal, bad_record_mac);
-
- #ifdef WOLFSSL_DTLS_DROP_STATS
- ssl->macDropCount++;
- #endif /* WOLFSSL_DTLS_DROP_STATS */
- }
-
- return ret;
-}
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-/* Check conditions for a cipher to have an explicit IV.
- *
- * ssl The SSL/TLS object.
- * returns 1 if the cipher in use has an explicit IV and 0 otherwise.
- */
-static WC_INLINE int CipherHasExpIV(WOLFSSL *ssl)
-{
-#ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3)
- return 0;
-#endif
- return (ssl->specs.cipher_type == aead) &&
- (ssl->specs.bulk_cipher_algorithm != wolfssl_chacha);
-}
-
-/* check cipher text size for sanity */
-static int SanityCheckCipherText(WOLFSSL* ssl, word32 encryptSz)
-{
-#ifdef HAVE_TRUNCATED_HMAC
- word32 minLength = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
- : ssl->specs.hash_size;
-#else
- word32 minLength = ssl->specs.hash_size; /* covers stream */
-#endif
-
-#ifndef WOLFSSL_AEAD_ONLY
- if (ssl->specs.cipher_type == block) {
-#ifdef HAVE_ENCRYPT_THEN_MAC
- if (ssl->options.startedETMRead) {
- if ((encryptSz - MacSize(ssl)) % ssl->specs.block_size) {
- WOLFSSL_MSG("Block ciphertext not block size");
- return SANITY_CIPHER_E;
- }
- }
- else
-#endif
- if (encryptSz % ssl->specs.block_size) {
- WOLFSSL_MSG("Block ciphertext not block size");
- return SANITY_CIPHER_E;
- }
-
- minLength++; /* pad byte */
-
- if (ssl->specs.block_size > minLength)
- minLength = ssl->specs.block_size;
-
- if (ssl->options.tls1_1)
- minLength += ssl->specs.block_size; /* explicit IV */
- }
- else
-#endif
- if (ssl->specs.cipher_type == aead) {
- minLength = ssl->specs.aead_mac_size; /* authTag size */
- if (CipherHasExpIV(ssl))
- minLength += AESGCM_EXP_IV_SZ; /* explicit IV */
- }
-
- if (encryptSz < minLength) {
- WOLFSSL_MSG("Ciphertext not minimum size");
- return SANITY_CIPHER_E;
- }
-
- return 0;
-}
-
-
-#ifndef WOLFSSL_AEAD_ONLY
-/* check all length bytes for the pad value, return 0 on success */
-static int PadCheck(const byte* a, byte pad, int length)
-{
- int i;
- int compareSum = 0;
-
- for (i = 0; i < length; i++) {
- compareSum |= a[i] ^ pad;
- }
-
- return compareSum;
-}
-
-
-/* Mask the padding bytes with the expected values.
- * Constant time implementation - does maximum pad size possible.
- *
- * data Message data.
- * sz Size of the message including MAC and padding and padding length.
- * macSz Size of the MAC.
- * returns 0 on success, otherwise failure.
- */
-static byte MaskPadding(const byte* data, int sz, int macSz)
-{
- int i;
- int checkSz = sz - 1;
- byte paddingSz = data[sz - 1];
- byte mask;
- byte good = ctMaskGT(paddingSz, sz - 1 - macSz);
-
- if (checkSz > TLS_MAX_PAD_SZ)
- checkSz = TLS_MAX_PAD_SZ;
-
- for (i = 0; i < checkSz; i++) {
- mask = ctMaskLTE(i, paddingSz);
- good |= mask & (data[sz - 1 - i] ^ paddingSz);
- }
-
- return good;
-}
-
-/* Mask the MAC in the message with the MAC calculated.
- * Constant time implementation - starts looking for MAC where maximum padding
- * size has it.
- *
- * data Message data.
- * sz Size of the message including MAC and padding and padding length.
- * macSz Size of the MAC data.
- * expMac Expected MAC value.
- * returns 0 on success, otherwise failure.
- */
-static byte MaskMac(const byte* data, int sz, int macSz, byte* expMac)
-{
- int i, j;
- unsigned char mac[WC_MAX_DIGEST_SIZE];
- int scanStart = sz - 1 - TLS_MAX_PAD_SZ - macSz;
- int macEnd = sz - 1 - data[sz - 1];
- int macStart = macEnd - macSz;
- int r = 0;
- unsigned char started, notEnded;
- unsigned char good = 0;
-
- scanStart &= ctMaskIntGTE(scanStart, 0);
- macStart &= ctMaskIntGTE(macStart, 0);
-
- /* Div on Intel has different speeds depending on value.
- * Use a bitwise AND or mod a specific value (converted to mul). */
- if ((macSz & (macSz - 1)) == 0)
- r = (macSz - (scanStart - macStart)) & (macSz - 1);
-#ifndef NO_SHA
- else if (macSz == WC_SHA_DIGEST_SIZE)
- r = (macSz - (scanStart - macStart)) % WC_SHA_DIGEST_SIZE;
-#endif
-#ifdef WOLFSSL_SHA384
- else if (macSz == WC_SHA384_DIGEST_SIZE)
- r = (macSz - (scanStart - macStart)) % WC_SHA384_DIGEST_SIZE;
-#endif
-
- XMEMSET(mac, 0, macSz);
- for (i = scanStart; i < sz; i += macSz) {
- for (j = 0; j < macSz && j + i < sz; j++) {
- started = ctMaskGTE(i + j, macStart);
- notEnded = ctMaskLT(i + j, macEnd);
- mac[j] |= started & notEnded & data[i + j];
- }
- }
-
- if ((macSz & (macSz - 1)) == 0) {
- for (i = 0; i < macSz; i++)
- good |= expMac[i] ^ mac[(i + r) & (macSz - 1)];
- }
-#ifndef NO_SHA
- else if (macSz == WC_SHA_DIGEST_SIZE) {
- for (i = 0; i < macSz; i++)
- good |= expMac[i] ^ mac[(i + r) % WC_SHA_DIGEST_SIZE];
- }
-#endif
-#ifdef WOLFSSL_SHA384
- else if (macSz == WC_SHA384_DIGEST_SIZE) {
- for (i = 0; i < macSz; i++)
- good |= expMac[i] ^ mac[(i + r) % WC_SHA384_DIGEST_SIZE];
- }
-#endif
-
- return good;
-}
-
-/* timing resistant pad/verify check, return 0 on success */
-int TimingPadVerify(WOLFSSL* ssl, const byte* input, int padLen, int macSz,
- int pLen, int content)
-{
- byte verify[WC_MAX_DIGEST_SIZE];
- byte good;
- int ret = 0;
-
- good = MaskPadding(input, pLen, macSz);
- /* 4th argument has potential to underflow, ssl->hmac function should
- * either increment the size by (macSz + padLen + 1) before use or check on
- * the size to make sure is valid. */
- ret = ssl->hmac(ssl, verify, input, pLen - macSz - padLen - 1, padLen,
- content, 1);
- good |= MaskMac(input, pLen, ssl->specs.hash_size, verify);
-
- /* Non-zero on failure. */
- good = (byte)~(word32)good;
- good &= good >> 4;
- good &= good >> 2;
- good &= good >> 1;
- /* Make ret negative on masking failure. */
- ret -= 1 - good;
-
- /* Treat any failure as verify MAC error. */
- if (ret != 0)
- ret = VERIFY_MAC_ERROR;
-
- return ret;
-}
-#endif
-
-
-int DoApplicationData(WOLFSSL* ssl, byte* input, word32* inOutIdx)
-{
- word32 msgSz = ssl->keys.encryptSz;
- word32 idx = *inOutIdx;
- int dataSz;
- int ivExtra = 0;
- byte* rawData = input + idx; /* keep current for hmac */
-#ifdef HAVE_LIBZ
- byte decomp[MAX_RECORD_SIZE + MAX_COMP_EXTRA];
-#endif
-
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->options.tls1_3 && ssl->options.handShakeDone == 0) {
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- ssl->earlyData != no_early_data &&
- ssl->options.clientState < CLIENT_FINISHED_COMPLETE) {
- ssl->earlyDataSz += ssl->curSize;
- if (ssl->earlyDataSz <= ssl->options.maxEarlyDataSz) {
- WOLFSSL_MSG("Ignoring EarlyData!");
- *inOutIdx = ssl->buffers.inputBuffer.length;
- return 0;
- }
- WOLFSSL_MSG("Too much EarlyData!");
- }
- }
-#endif
- if (ssl->options.handShakeDone == 0) {
- WOLFSSL_MSG("Received App data before a handshake completed");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return OUT_OF_ORDER_E;
- }
-
-#ifndef WOLFSSL_AEAD_ONLY
- if (ssl->specs.cipher_type == block) {
- if (ssl->options.tls1_1)
- ivExtra = ssl->specs.block_size;
- }
- else
-#endif
- if (ssl->specs.cipher_type == aead) {
- if (CipherHasExpIV(ssl))
- ivExtra = AESGCM_EXP_IV_SZ;
- }
-
- dataSz = msgSz - ivExtra - ssl->keys.padSz;
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- dataSz -= MacSize(ssl);
-#endif
- if (dataSz < 0) {
- WOLFSSL_MSG("App data buffer error, malicious input?");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return BUFFER_ERROR;
- }
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData > early_data_ext) {
- if (ssl->earlyDataSz + dataSz > ssl->options.maxEarlyDataSz) {
- SendAlert(ssl, alert_fatal, unexpected_message);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->earlyDataSz += dataSz;
- }
-#endif
-
- /* read data */
- if (dataSz) {
- int rawSz = dataSz; /* keep raw size for idx adjustment */
-
-#ifdef HAVE_LIBZ
- if (ssl->options.usingCompression) {
- dataSz = myDeCompress(ssl, rawData, dataSz, decomp, sizeof(decomp));
- if (dataSz < 0) return dataSz;
- }
-#endif
- idx += rawSz;
-
- ssl->buffers.clearOutputBuffer.buffer = rawData;
- ssl->buffers.clearOutputBuffer.length = dataSz;
- }
-
- idx += ssl->keys.padSz;
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- idx += MacSize(ssl);
-#endif
-
-#ifdef HAVE_LIBZ
- /* decompress could be bigger, overwrite after verify */
- if (ssl->options.usingCompression)
- XMEMMOVE(rawData, decomp, dataSz);
-#endif
-
- *inOutIdx = idx;
- return 0;
-}
-
-
-/* process alert, return level */
-static int DoAlert(WOLFSSL* ssl, byte* input, word32* inOutIdx, int* type,
- word32 totalSz)
-{
- byte level;
- byte code;
- word32 dataSz = totalSz - *inOutIdx;
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "Alert");
- if (ssl->toInfoOn)
- /* add record header back on to info + alert bytes level/code */
- AddPacketInfo(ssl, "Alert", alert, input + *inOutIdx -
- RECORD_HEADER_SZ, RECORD_HEADER_SZ + ALERT_SIZE,
- READ_PROTO, ssl->heap);
- #endif
-
- if (IsEncryptionOn(ssl, 0)) {
- dataSz -= ssl->keys.padSz;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- dataSz -= MacSize(ssl);
- #endif
- }
-
- /* make sure can read the message */
- if (dataSz != ALERT_SIZE) {
-#ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, unexpected_message);
-#endif
- return BUFFER_E;
- }
-
- level = input[(*inOutIdx)++];
- code = input[(*inOutIdx)++];
- ssl->alert_history.last_rx.code = code;
- ssl->alert_history.last_rx.level = level;
- *type = code;
- if (level == alert_fatal) {
- ssl->options.isClosed = 1; /* Don't send close_notify */
- }
-
- if (++ssl->options.alertCount >= WOLFSSL_ALERT_COUNT_MAX) {
- WOLFSSL_MSG("Alert count exceeded");
-#ifdef WOLFSSL_EXTRA_ALERTS
- if (level != alert_warning || code != close_notify)
- SendAlert(ssl, alert_fatal, unexpected_message);
-#endif
- return ALERT_COUNT_E;
- }
-
- WOLFSSL_MSG("Got alert");
- if (*type == close_notify) {
- WOLFSSL_MSG("\tclose notify");
- ssl->options.closeNotify = 1;
- }
-#ifdef WOLFSSL_TLS13
- if (*type == decode_error) {
- WOLFSSL_MSG("\tdecode error");
- }
- if (*type == illegal_parameter) {
- WOLFSSL_MSG("\tillegal parameter");
- }
-#endif
- WOLFSSL_ERROR(*type);
- if (IsEncryptionOn(ssl, 0)) {
- *inOutIdx += ssl->keys.padSz;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- *inOutIdx += MacSize(ssl);
- #endif
- }
-
- return level;
-}
-
-static int GetInputData(WOLFSSL *ssl, word32 size)
-{
- int in;
- int inSz;
- int maxLength;
- int usedLength;
- int dtlsExtra = 0;
-
-
- /* check max input length */
- usedLength = ssl->buffers.inputBuffer.length - ssl->buffers.inputBuffer.idx;
- maxLength = ssl->buffers.inputBuffer.bufferSize - usedLength;
- inSz = (int)(size - usedLength); /* from last partial read */
-
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- if (size < ssl->dtls_expected_rx)
- dtlsExtra = (int)(ssl->dtls_expected_rx - size);
- inSz = ssl->dtls_expected_rx;
- }
-#endif
-
- /* check that no lengths or size values are negative */
- if (usedLength < 0 || maxLength < 0 || inSz <= 0) {
- return BUFFER_ERROR;
- }
-
- if (inSz > maxLength) {
- if (GrowInputBuffer(ssl, size + dtlsExtra, usedLength) < 0)
- return MEMORY_E;
- }
-
- /* Put buffer data at start if not there */
- if (usedLength > 0 && ssl->buffers.inputBuffer.idx != 0)
- XMEMMOVE(ssl->buffers.inputBuffer.buffer,
- ssl->buffers.inputBuffer.buffer + ssl->buffers.inputBuffer.idx,
- usedLength);
-
- /* remove processed data */
- ssl->buffers.inputBuffer.idx = 0;
- ssl->buffers.inputBuffer.length = usedLength;
-
- /* read data from network */
- do {
- in = wolfSSLReceive(ssl,
- ssl->buffers.inputBuffer.buffer +
- ssl->buffers.inputBuffer.length,
- inSz);
- if (in == WANT_READ)
- return WANT_READ;
-
- if (in < 0)
- return SOCKET_ERROR_E;
-
- if (in > inSz)
- return RECV_OVERFLOW_E;
-
- ssl->buffers.inputBuffer.length += in;
- inSz -= in;
-
- } while (ssl->buffers.inputBuffer.length < size);
-
-#ifdef WOLFSSL_DEBUG_TLS
- if (ssl->buffers.inputBuffer.idx == 0) {
- WOLFSSL_MSG("Data received");
- WOLFSSL_BUFFER(ssl->buffers.inputBuffer.buffer,
- ssl->buffers.inputBuffer.length);
- }
-#endif
-
- return 0;
-}
-
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
-static WC_INLINE int VerifyMacEnc(WOLFSSL* ssl, const byte* input, word32 msgSz,
- int content)
-{
- int ret;
-#ifdef HAVE_TRUNCATED_HMAC
- word32 digestSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
- : ssl->specs.hash_size;
-#else
- word32 digestSz = ssl->specs.hash_size;
-#endif
- byte verify[WC_MAX_DIGEST_SIZE];
-
- WOLFSSL_MSG("Verify MAC of Encrypted Data");
-
- if (msgSz < digestSz) {
- return VERIFY_MAC_ERROR;
- }
-
- ret = ssl->hmac(ssl, verify, input, msgSz - digestSz, -1, content, 1);
- ret |= ConstantCompare(verify, input + msgSz - digestSz, digestSz);
- if (ret != 0) {
- return VERIFY_MAC_ERROR;
- }
-
- return 0;
-}
-#endif
-
-static WC_INLINE int VerifyMac(WOLFSSL* ssl, const byte* input, word32 msgSz,
- int content, word32* padSz)
-{
-#if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
- int ivExtra = 0;
- int ret;
- word32 pad = 0;
- word32 padByte = 0;
-#ifdef HAVE_TRUNCATED_HMAC
- word32 digestSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
- : ssl->specs.hash_size;
-#else
- word32 digestSz = ssl->specs.hash_size;
-#endif
- byte verify[WC_MAX_DIGEST_SIZE];
-
-
- if (ssl->specs.cipher_type == block) {
- if (ssl->options.tls1_1)
- ivExtra = ssl->specs.block_size;
- pad = *(input + msgSz - ivExtra - 1);
- padByte = 1;
-
- if (ssl->options.tls) {
- ret = TimingPadVerify(ssl, input, pad, digestSz, msgSz - ivExtra,
- content);
- if (ret != 0)
- return ret;
- }
- else { /* sslv3, some implementations have bad padding, but don't
- * allow bad read */
- int badPadLen = 0;
- byte dmy[sizeof(WOLFSSL) >= MAX_PAD_SIZE ? 1 : MAX_PAD_SIZE] = {0};
- byte* dummy = sizeof(dmy) < MAX_PAD_SIZE ? (byte*) ssl : dmy;
-
- (void)dmy;
-
- if (pad > (msgSz - digestSz - 1)) {
- WOLFSSL_MSG("Plain Len not long enough for pad/mac");
- pad = 0; /* no bad read */
- badPadLen = 1;
- }
- PadCheck(dummy, (byte)pad, MAX_PAD_SIZE); /* timing only */
- ret = ssl->hmac(ssl, verify, input, msgSz - digestSz - pad - 1, pad,
- content, 1);
- if (ConstantCompare(verify, input + msgSz - digestSz - pad - 1,
- digestSz) != 0)
- return VERIFY_MAC_ERROR;
- if (ret != 0 || badPadLen)
- return VERIFY_MAC_ERROR;
- }
- }
- else if (ssl->specs.cipher_type == stream) {
- ret = ssl->hmac(ssl, verify, input, msgSz - digestSz, -1, content, 1);
- if (ConstantCompare(verify, input + msgSz - digestSz, digestSz) != 0){
- return VERIFY_MAC_ERROR;
- }
- if (ret != 0)
- return VERIFY_MAC_ERROR;
- }
-#endif /* !WOLFSSL_NO_TLS12 && !WOLFSSL_AEAD_ONLY */
-
- if (ssl->specs.cipher_type == aead) {
- *padSz = ssl->specs.aead_mac_size;
- }
-#if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
- else {
- *padSz = digestSz + pad + padByte;
- }
-#endif /* !WOLFSSL_NO_TLS12 && !WOLFSSL_AEAD_ONLY */
-
- (void)input;
- (void)msgSz;
- (void)content;
-
- return 0;
-}
-
-
-/* process input requests, return 0 is done, 1 is call again to complete, and
- negative number is error */
-int ProcessReply(WOLFSSL* ssl)
-{
- int ret = 0, type, readSz;
- int atomicUser = 0;
- word32 startIdx = 0;
-#if defined(WOLFSSL_DTLS)
- int used;
-#endif
-
-#ifdef ATOMIC_USER
- if (ssl->ctx->DecryptVerifyCb)
- atomicUser = 1;
-#endif
-
- if (ssl->error != 0 && ssl->error != WANT_READ && ssl->error != WANT_WRITE
- #ifdef WOLFSSL_ASYNC_CRYPT
- && ssl->error != WC_PENDING_E
- #endif
- #ifdef WOLFSSL_NONBLOCK_OCSP
- && ssl->error != OCSP_WANT_READ
- #endif
- ) {
- WOLFSSL_MSG("ProcessReply retry in error state, not allowed");
- return ssl->error;
- }
-
- for (;;) {
- switch (ssl->options.processReply) {
-
- /* in the WOLFSSL_SERVER case, get the first byte for detecting
- * old client hello */
- case doProcessInit:
-
- readSz = RECORD_HEADER_SZ;
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- readSz = DTLS_RECORD_HEADER_SZ;
- #endif
-
- /* get header or return error */
- if (!ssl->options.dtls) {
- if ((ret = GetInputData(ssl, readSz)) < 0)
- return ret;
- } else {
- #ifdef WOLFSSL_DTLS
- /* read ahead may already have header */
- used = ssl->buffers.inputBuffer.length -
- ssl->buffers.inputBuffer.idx;
- if (used < readSz) {
- if ((ret = GetInputData(ssl, readSz)) < 0)
- return ret;
- }
- #endif
- }
-
-#ifdef OLD_HELLO_ALLOWED
-
- /* see if sending SSLv2 client hello */
- if ( ssl->options.side == WOLFSSL_SERVER_END &&
- ssl->options.clientState == NULL_STATE &&
- ssl->buffers.inputBuffer.buffer[ssl->buffers.inputBuffer.idx]
- != handshake) {
- byte b0, b1;
-
- ssl->options.processReply = runProcessOldClientHello;
-
- /* sanity checks before getting size at front */
- if (ssl->buffers.inputBuffer.buffer[
- ssl->buffers.inputBuffer.idx + OPAQUE16_LEN] != OLD_HELLO_ID) {
- WOLFSSL_MSG("Not a valid old client hello");
- return PARSE_ERROR;
- }
-
- if (ssl->buffers.inputBuffer.buffer[
- ssl->buffers.inputBuffer.idx + OPAQUE24_LEN] != SSLv3_MAJOR &&
- ssl->buffers.inputBuffer.buffer[
- ssl->buffers.inputBuffer.idx + OPAQUE24_LEN] != DTLS_MAJOR) {
- WOLFSSL_MSG("Not a valid version in old client hello");
- return PARSE_ERROR;
- }
-
- /* how many bytes need ProcessOldClientHello */
- b0 =
- ssl->buffers.inputBuffer.buffer[ssl->buffers.inputBuffer.idx++];
- b1 =
- ssl->buffers.inputBuffer.buffer[ssl->buffers.inputBuffer.idx++];
- ssl->curSize = (word16)(((b0 & 0x7f) << 8) | b1);
- }
- else {
- ssl->options.processReply = getRecordLayerHeader;
- continue;
- }
- FALL_THROUGH;
-
- /* in the WOLFSSL_SERVER case, run the old client hello */
- case runProcessOldClientHello:
-
- /* get sz bytes or return error */
- if (!ssl->options.dtls) {
- if ((ret = GetInputData(ssl, ssl->curSize)) < 0)
- return ret;
- } else {
- #ifdef WOLFSSL_DTLS
- /* read ahead may already have */
- used = ssl->buffers.inputBuffer.length -
- ssl->buffers.inputBuffer.idx;
- if (used < ssl->curSize)
- if ((ret = GetInputData(ssl, ssl->curSize)) < 0)
- return ret;
- #endif /* WOLFSSL_DTLS */
- }
-
- ret = ProcessOldClientHello(ssl, ssl->buffers.inputBuffer.buffer,
- &ssl->buffers.inputBuffer.idx,
- ssl->buffers.inputBuffer.length -
- ssl->buffers.inputBuffer.idx,
- ssl->curSize);
- if (ret < 0)
- return ret;
-
- else if (ssl->buffers.inputBuffer.idx ==
- ssl->buffers.inputBuffer.length) {
- ssl->options.processReply = doProcessInit;
- return 0;
- }
-
-#endif /* OLD_HELLO_ALLOWED */
- FALL_THROUGH;
-
- /* get the record layer header */
- case getRecordLayerHeader:
-
- ret = GetRecordHeader(ssl, ssl->buffers.inputBuffer.buffer,
- &ssl->buffers.inputBuffer.idx,
- &ssl->curRL, &ssl->curSize);
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls && ret == SEQUENCE_ERROR) {
- WOLFSSL_MSG("Silently dropping out of order DTLS message");
- ssl->options.processReply = doProcessInit;
- ssl->buffers.inputBuffer.length = 0;
- ssl->buffers.inputBuffer.idx = 0;
-#ifdef WOLFSSL_DTLS_DROP_STATS
- ssl->replayDropCount++;
-#endif /* WOLFSSL_DTLS_DROP_STATS */
-
- if (IsDtlsNotSctpMode(ssl) && ssl->options.dtlsHsRetain) {
- ret = DtlsMsgPoolSend(ssl, 0);
- if (ret != 0)
- return ret;
- }
-
- continue;
- }
-#endif
- if (ret != 0)
- return ret;
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) && IsEncryptionOn(ssl, 0) &&
- ssl->curRL.type != application_data &&
- ssl->curRL.type != change_cipher_spec) {
- SendAlert(ssl, alert_fatal, unexpected_message);
- return PARSE_ERROR;
- }
-#endif
-
- ssl->options.processReply = getData;
- FALL_THROUGH;
-
- /* retrieve record layer data */
- case getData:
-
- /* get sz bytes or return error */
- if (!ssl->options.dtls) {
- if ((ret = GetInputData(ssl, ssl->curSize)) < 0) {
-#ifdef WOLFSSL_EXTRA_ALERTS
- if (ret != WANT_READ)
- SendAlert(ssl, alert_fatal, bad_record_mac);
-#endif
- return ret;
- }
- }
- else {
-#ifdef WOLFSSL_DTLS
- /* read ahead may already have */
- used = ssl->buffers.inputBuffer.length -
- ssl->buffers.inputBuffer.idx;
- if (used < ssl->curSize)
- if ((ret = GetInputData(ssl, ssl->curSize)) < 0)
- return ret;
-#endif
- }
-
- if (IsEncryptionOn(ssl, 0)) {
- int tooLong = 0;
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version)) {
- tooLong = ssl->curSize > MAX_TLS13_ENC_SZ;
- tooLong |= ssl->curSize - ssl->specs.aead_mac_size >
- MAX_TLS13_PLAIN_SZ;
- }
-#endif
-#ifdef WOLFSSL_EXTRA_ALERTS
- if (!IsAtLeastTLSv1_3(ssl->version))
- tooLong = ssl->curSize > MAX_TLS_CIPHER_SZ;
-#endif
- if (tooLong) {
- WOLFSSL_MSG("Encrypted data too long");
-#if defined(WOLFSSL_TLS13) || defined(WOLFSSL_EXTRA_ALERTS)
- SendAlert(ssl, alert_fatal, record_overflow);
-#endif
- return BUFFER_ERROR;
- }
- }
- ssl->keys.padSz = 0;
-
- ssl->options.processReply = verifyEncryptedMessage;
- startIdx = ssl->buffers.inputBuffer.idx; /* in case > 1 msg per */
- FALL_THROUGH;
-
- /* verify digest of encrypted message */
- case verifyEncryptedMessage:
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (IsEncryptionOn(ssl, 0) && ssl->keys.decryptedCur == 0 &&
- !atomicUser && ssl->options.startedETMRead) {
- ret = VerifyMacEnc(ssl, ssl->buffers.inputBuffer.buffer +
- ssl->buffers.inputBuffer.idx,
- ssl->curSize, ssl->curRL.type);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- return ret;
- #endif
- if (ret < 0) {
- WOLFSSL_MSG("VerifyMacEnc failed");
- WOLFSSL_ERROR(ret);
- #ifdef WOLFSSL_DTLS
- /* If in DTLS mode, if the decrypt fails for any
- * reason, pretend the datagram never happened. */
- if (ssl->options.dtls) {
- ssl->options.processReply = doProcessInit;
- ssl->buffers.inputBuffer.idx =
- ssl->buffers.inputBuffer.length;
- #ifdef WOLFSSL_DTLS_DROP_STATS
- ssl->macDropCount++;
- #endif /* WOLFSSL_DTLS_DROP_STATS */
- }
- #endif /* WOLFSSL_DTLS */
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (!ssl->options.dtls)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- #endif
- return DECRYPT_ERROR;
- }
- ssl->keys.encryptSz = ssl->curSize;
- }
-#endif
- ssl->options.processReply = decryptMessage;
- FALL_THROUGH;
-
- /* decrypt message */
- case decryptMessage:
-
-#if !defined(WOLFSSL_TLS13) || defined(WOLFSSL_TLS13_DRAFT_18)
- if (IsEncryptionOn(ssl, 0) && ssl->keys.decryptedCur == 0)
-#else
- if (IsEncryptionOn(ssl, 0) && ssl->keys.decryptedCur == 0 &&
- (!IsAtLeastTLSv1_3(ssl->version) ||
- ssl->curRL.type != change_cipher_spec))
-#endif
- {
- bufferStatic* in = &ssl->buffers.inputBuffer;
-
- ret = SanityCheckCipherText(ssl, ssl->curSize);
- if (ret < 0) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, bad_record_mac);
- #endif
- return ret;
- }
-
- if (atomicUser) {
- #ifdef ATOMIC_USER
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead) {
- ret = ssl->ctx->VerifyDecryptCb(ssl,
- in->buffer + in->idx, in->buffer + in->idx,
- ssl->curSize - MacSize(ssl),
- ssl->curRL.type, 1, &ssl->keys.padSz,
- ssl->DecryptVerifyCtx);
- }
- else
- #endif
- {
- ret = ssl->ctx->DecryptVerifyCb(ssl,
- in->buffer + in->idx,
- in->buffer + in->idx,
- ssl->curSize, ssl->curRL.type, 1,
- &ssl->keys.padSz, ssl->DecryptVerifyCtx);
- }
- #endif /* ATOMIC_USER */
- }
- else {
- if (!ssl->options.tls1_3) {
- #ifndef WOLFSSL_NO_TLS12
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead) {
- word32 digestSz = MacSize(ssl);
- ret = Decrypt(ssl,
- in->buffer + in->idx,
- in->buffer + in->idx,
- ssl->curSize - digestSz);
- ssl->keys.padSz =
- in->buffer[in->idx + ssl->curSize - digestSz - 1];
- ssl->keys.padSz += 1;
- ssl->keys.decryptedCur = 1;
- }
- else
- #endif
- {
- ret = Decrypt(ssl,
- in->buffer + in->idx,
- in->buffer + in->idx,
- ssl->curSize);
- }
- #else
- ret = DECRYPT_ERROR;
- #endif
- }
- else
- {
- #ifdef WOLFSSL_TLS13
- #if defined(WOLFSSL_TLS13_DRAFT_18) || \
- defined(WOLFSSL_TLS13_DRAFT_22) || \
- defined(WOLFSSL_TLS13_DRAFT_23)
- ret = DecryptTls13(ssl,
- in->buffer + in->idx,
- in->buffer + in->idx,
- ssl->curSize, NULL, 0);
- #else
- ret = DecryptTls13(ssl,
- in->buffer + in->idx,
- in->buffer + in->idx,
- ssl->curSize,
- (byte*)&ssl->curRL, RECORD_HEADER_SZ);
- #endif
- #else
- ret = DECRYPT_ERROR;
- #endif /* WOLFSSL_TLS13 */
- }
- }
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- return ret;
- #endif
-
- if (ret >= 0) {
- #ifndef WOLFSSL_NO_TLS12
- /* handle success */
- #ifndef WOLFSSL_AEAD_ONLY
- if (ssl->options.tls1_1 && ssl->specs.cipher_type == block)
- ssl->buffers.inputBuffer.idx += ssl->specs.block_size;
- #endif
- /* go past TLSv1.1 IV */
- if (CipherHasExpIV(ssl))
- ssl->buffers.inputBuffer.idx += AESGCM_EXP_IV_SZ;
- #endif
- }
- else {
- WOLFSSL_MSG("Decrypt failed");
- WOLFSSL_ERROR(ret);
- #ifdef WOLFSSL_EARLY_DATA
- if (ssl->options.tls1_3) {
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- ssl->earlyData != no_early_data &&
- ssl->options.clientState <
- CLIENT_FINISHED_COMPLETE) {
- ssl->earlyDataSz += ssl->curSize;
- if (ssl->earlyDataSz <=
- ssl->options.maxEarlyDataSz) {
- WOLFSSL_MSG("Ignoring EarlyData!");
- if (ssl->keys.peer_sequence_number_lo-- == 0)
- ssl->keys.peer_sequence_number_hi--;
- ssl->options.processReply = doProcessInit;
- ssl->buffers.inputBuffer.idx =
- ssl->buffers.inputBuffer.length;
- return 0;
- }
- WOLFSSL_MSG("Too much EarlyData!");
- }
- SendAlert(ssl, alert_fatal, bad_record_mac);
- }
- #endif
- #ifdef WOLFSSL_DTLS
- /* If in DTLS mode, if the decrypt fails for any
- * reason, pretend the datagram never happened. */
- if (ssl->options.dtls) {
- ssl->options.processReply = doProcessInit;
- ssl->buffers.inputBuffer.idx =
- ssl->buffers.inputBuffer.length;
- #ifdef WOLFSSL_DTLS_DROP_STATS
- ssl->macDropCount++;
- #endif /* WOLFSSL_DTLS_DROP_STATS */
- }
- #endif /* WOLFSSL_DTLS */
- return DECRYPT_ERROR;
- }
- }
-
- ssl->options.processReply = verifyMessage;
- FALL_THROUGH;
-
- /* verify digest of message */
- case verifyMessage:
-
-#if !defined(WOLFSSL_TLS13) || defined(WOLFSSL_TLS13_DRAFT_18)
- if (IsEncryptionOn(ssl, 0) && ssl->keys.decryptedCur == 0)
-#else
- if (IsEncryptionOn(ssl, 0) && ssl->keys.decryptedCur == 0 &&
- (!IsAtLeastTLSv1_3(ssl->version) ||
- ssl->curRL.type != change_cipher_spec))
-#endif
- {
- if (!atomicUser
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- && !ssl->options.startedETMRead
-#endif
- ) {
- ret = VerifyMac(ssl, ssl->buffers.inputBuffer.buffer +
- ssl->buffers.inputBuffer.idx,
- ssl->curSize, ssl->curRL.type,
- &ssl->keys.padSz);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- return ret;
- #endif
- if (ret < 0) {
- WOLFSSL_MSG("VerifyMac failed");
- WOLFSSL_ERROR(ret);
- #ifdef WOLFSSL_DTLS
- /* If in DTLS mode, if the decrypt fails for any
- * reason, pretend the datagram never happened. */
- if (ssl->options.dtls) {
- ssl->options.processReply = doProcessInit;
- ssl->buffers.inputBuffer.idx =
- ssl->buffers.inputBuffer.length;
- #ifdef WOLFSSL_DTLS_DROP_STATS
- ssl->macDropCount++;
- #endif /* WOLFSSL_DTLS_DROP_STATS */
- }
- #endif /* WOLFSSL_DTLS */
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (!ssl->options.dtls)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- #endif
- return DECRYPT_ERROR;
- }
- }
-
- ssl->keys.encryptSz = ssl->curSize;
- ssl->keys.decryptedCur = 1;
-#ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3) {
- word16 i = (word16)(ssl->buffers.inputBuffer.length -
- ssl->keys.padSz);
- /* Remove padding from end of plain text. */
- for (--i; i > ssl->buffers.inputBuffer.idx; i--) {
- if (ssl->buffers.inputBuffer.buffer[i] != 0)
- break;
- }
- /* Get the real content type from the end of the data. */
- ssl->curRL.type = ssl->buffers.inputBuffer.buffer[i];
- ssl->keys.padSz = ssl->buffers.inputBuffer.length - i;
- }
-#endif
- }
-
- ssl->options.processReply = runProcessingOneMessage;
- FALL_THROUGH;
-
- /* the record layer is here */
- case runProcessingOneMessage:
-
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (IsEncryptionOn(ssl, 0) && ssl->options.startedETMRead) {
- if (ssl->buffers.inputBuffer.length - ssl->keys.padSz -
- ssl->buffers.inputBuffer.idx -
- MacSize(ssl) > MAX_PLAINTEXT_SZ) {
- WOLFSSL_MSG("Plaintext too long - Encrypt-Then-MAC");
- #if defined(WOLFSSL_EXTRA_ALERTS)
- SendAlert(ssl, alert_fatal, record_overflow);
- #endif
- return BUFFER_ERROR;
- }
- }
- else
- #endif
- if (ssl->buffers.inputBuffer.length - ssl->keys.padSz -
- ssl->buffers.inputBuffer.idx > MAX_PLAINTEXT_SZ) {
- WOLFSSL_MSG("Plaintext too long");
-#if defined(WOLFSSL_TLS13) || defined(WOLFSSL_EXTRA_ALERTS)
- SendAlert(ssl, alert_fatal, record_overflow);
-#endif
- return BUFFER_ERROR;
- }
-
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- DtlsUpdateWindow(ssl);
- }
- #endif /* WOLFSSL_DTLS */
-
- WOLFSSL_MSG("received record layer msg");
-
- switch (ssl->curRL.type) {
- case handshake :
- /* debugging in DoHandShakeMsg */
- if (ssl->options.dtls) {
-#ifdef WOLFSSL_DTLS
- ret = DoDtlsHandShakeMsg(ssl,
- ssl->buffers.inputBuffer.buffer,
- &ssl->buffers.inputBuffer.idx,
- ssl->buffers.inputBuffer.length);
-#endif
- }
- else if (!IsAtLeastTLSv1_3(ssl->version)) {
-#ifndef WOLFSSL_NO_TLS12
- ret = DoHandShakeMsg(ssl,
- ssl->buffers.inputBuffer.buffer,
- &ssl->buffers.inputBuffer.idx,
- ssl->buffers.inputBuffer.length);
-#else
- ret = BUFFER_ERROR;
-#endif
- }
- else {
-#ifdef WOLFSSL_TLS13
- ret = DoTls13HandShakeMsg(ssl,
- ssl->buffers.inputBuffer.buffer,
- &ssl->buffers.inputBuffer.idx,
- ssl->buffers.inputBuffer.length);
- #ifdef WOLFSSL_EARLY_DATA
- if (ret != 0)
- return ret;
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- ssl->earlyData > early_data_ext &&
- ssl->options.handShakeState == HANDSHAKE_DONE) {
- ssl->earlyData = no_early_data;
- ssl->options.processReply = doProcessInit;
- return ZERO_RETURN;
- }
- #endif
-#else
- ret = BUFFER_ERROR;
-#endif
- }
- if (ret != 0) {
- WOLFSSL_ERROR(ret);
- return ret;
- }
- break;
-
- case change_cipher_spec:
- WOLFSSL_MSG("got CHANGE CIPHER SPEC");
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "ChangeCipher");
- /* add record header back on info */
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "ChangeCipher",
- change_cipher_spec,
- ssl->buffers.inputBuffer.buffer +
- ssl->buffers.inputBuffer.idx - RECORD_HEADER_SZ,
- 1 + RECORD_HEADER_SZ, READ_PROTO, ssl->heap);
- #ifdef WOLFSSL_CALLBACKS
- AddLateRecordHeader(&ssl->curRL, &ssl->timeoutInfo);
- #endif
- }
- #endif
-
-#ifdef WOLFSSL_TLS13
- #ifdef WOLFSSL_TLS13_DRAFT_18
- if (IsAtLeastTLSv1_3(ssl->version)) {
- SendAlert(ssl, alert_fatal, illegal_parameter);
- return UNKNOWN_RECORD_TYPE;
- }
- #else
- if (IsAtLeastTLSv1_3(ssl->version)) {
- word32 i = ssl->buffers.inputBuffer.idx;
- if (ssl->options.handShakeState == HANDSHAKE_DONE) {
- SendAlert(ssl, alert_fatal, unexpected_message);
- return UNKNOWN_RECORD_TYPE;
- }
- if (ssl->curSize != 1 ||
- ssl->buffers.inputBuffer.buffer[i] != 1) {
- SendAlert(ssl, alert_fatal, illegal_parameter);
- return UNKNOWN_RECORD_TYPE;
- }
- ssl->buffers.inputBuffer.idx++;
- break;
- }
- #endif
-#endif
-
-#ifndef WOLFSSL_NO_TLS12
- ret = SanityCheckMsgReceived(ssl, change_cipher_hs);
- if (ret != 0) {
- if (!ssl->options.dtls) {
- return ret;
- }
- else {
- #ifdef WOLFSSL_DTLS
- /* Check for duplicate CCS message in DTLS mode.
- * DTLS allows for duplicate messages, and it should be
- * skipped. Also skip if out of order. */
- if (ret != DUPLICATE_MSG_E && ret != OUT_OF_ORDER_E)
- return ret;
-
- if (IsDtlsNotSctpMode(ssl)) {
- ret = DtlsMsgPoolSend(ssl, 1);
- if (ret != 0)
- return ret;
- }
-
- if (ssl->curSize != 1) {
- WOLFSSL_MSG("Malicious or corrupted"
- " duplicate ChangeCipher msg");
- return LENGTH_ERROR;
- }
- ssl->buffers.inputBuffer.idx++;
- break;
- #endif /* WOLFSSL_DTLS */
- }
- }
-
- if (IsEncryptionOn(ssl, 0) && ssl->options.handShakeDone) {
- ssl->buffers.inputBuffer.idx += ssl->keys.padSz;
- ssl->curSize -= (word16) ssl->buffers.inputBuffer.idx;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead) {
- word32 digestSz = MacSize(ssl);
- ssl->buffers.inputBuffer.idx += digestSz;
- ssl->curSize -= digestSz;
- }
- #endif
- }
-
- if (ssl->curSize != 1) {
- WOLFSSL_MSG("Malicious or corrupted ChangeCipher msg");
- return LENGTH_ERROR;
- }
-
- ssl->buffers.inputBuffer.idx++;
- ssl->keys.encryptionOn = 1;
-
- /* setup decrypt keys for following messages */
- /* XXX This might not be what we want to do when
- * receiving a CCS with multicast. We update the
- * key when the application updates them. */
- if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0)
- return ret;
-
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- ssl->options.startedETMRead = ssl->options.encThenMac;
- #endif
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- WOLFSSL_DTLS_PEERSEQ* peerSeq = ssl->keys.peerSeq;
-#ifdef WOLFSSL_MULTICAST
- if (ssl->options.haveMcast) {
- peerSeq += ssl->keys.curPeerId;
- peerSeq->highwaterMark = UpdateHighwaterMark(0,
- ssl->ctx->mcastFirstSeq,
- ssl->ctx->mcastSecondSeq,
- ssl->ctx->mcastMaxSeq);
- }
-#endif
- peerSeq->nextEpoch++;
- peerSeq->prevSeq_lo = peerSeq->nextSeq_lo;
- peerSeq->prevSeq_hi = peerSeq->nextSeq_hi;
- peerSeq->nextSeq_lo = 0;
- peerSeq->nextSeq_hi = 0;
- XMEMCPY(peerSeq->prevWindow, peerSeq->window,
- DTLS_SEQ_SZ);
- XMEMSET(peerSeq->window, 0, DTLS_SEQ_SZ);
- }
- #endif
-
- #ifdef HAVE_LIBZ
- if (ssl->options.usingCompression)
- if ( (ret = InitStreams(ssl)) != 0)
- return ret;
- #endif
- ret = BuildFinished(ssl, &ssl->hsHashes->verifyHashes,
- ssl->options.side == WOLFSSL_CLIENT_END ?
- server : client);
- if (ret != 0)
- return ret;
-#endif /* !WOLFSSL_NO_TLS12 */
- break;
-
- case application_data:
- WOLFSSL_MSG("got app DATA");
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls && ssl->options.dtlsHsRetain) {
- FreeHandshakeResources(ssl);
- ssl->options.dtlsHsRetain = 0;
- }
- #endif
- #ifdef WOLFSSL_TLS13
- if (ssl->keys.keyUpdateRespond) {
- WOLFSSL_MSG("No KeyUpdate from peer seen");
- return SANITY_MSG_E;
- }
- #endif
- if ((ret = DoApplicationData(ssl,
- ssl->buffers.inputBuffer.buffer,
- &ssl->buffers.inputBuffer.idx))
- != 0) {
- WOLFSSL_ERROR(ret);
- return ret;
- }
- break;
-
- case alert:
- WOLFSSL_MSG("got ALERT!");
- ret = DoAlert(ssl, ssl->buffers.inputBuffer.buffer,
- &ssl->buffers.inputBuffer.idx, &type,
- ssl->buffers.inputBuffer.length);
- if (ret == alert_fatal)
- return FATAL_ERROR;
- else if (ret < 0)
- return ret;
-
- /* catch warnings that are handled as errors */
- if (type == close_notify)
- return ssl->error = ZERO_RETURN;
-
- if (type == decrypt_error)
- return FATAL_ERROR;
- break;
-
- default:
- WOLFSSL_ERROR(UNKNOWN_RECORD_TYPE);
- return UNKNOWN_RECORD_TYPE;
- }
-
- ssl->options.processReply = doProcessInit;
-
- /* input exhausted? */
- if (ssl->buffers.inputBuffer.idx >= ssl->buffers.inputBuffer.length)
- return 0;
-
- /* more messages per record */
- else if ((ssl->buffers.inputBuffer.idx - startIdx) < ssl->curSize) {
- WOLFSSL_MSG("More messages in record");
-
- ssl->options.processReply = runProcessingOneMessage;
-
- if (IsEncryptionOn(ssl, 0)) {
- WOLFSSL_MSG("Bundled encrypted messages, remove middle pad");
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead) {
- word32 digestSz = MacSize(ssl);
- if (ssl->buffers.inputBuffer.idx >=
- ssl->keys.padSz + digestSz) {
- ssl->buffers.inputBuffer.idx -=
- ssl->keys.padSz + digestSz;
- }
- else {
- WOLFSSL_MSG("\tmiddle padding error");
- return FATAL_ERROR;
- }
- }
- else
- #endif
- {
- if (ssl->buffers.inputBuffer.idx >= ssl->keys.padSz) {
- ssl->buffers.inputBuffer.idx -= ssl->keys.padSz;
- }
- else {
- WOLFSSL_MSG("\tmiddle padding error");
- return FATAL_ERROR;
- }
- }
- }
-
- continue;
- }
- /* more records */
- else {
- WOLFSSL_MSG("More records in input");
- ssl->options.processReply = doProcessInit;
- continue;
- }
-
- default:
- WOLFSSL_MSG("Bad process input state, programming error");
- return INPUT_CASE_ERROR;
- }
- }
-}
-
-
-int SendChangeCipher(WOLFSSL* ssl)
-{
- byte *output;
- int sendSz = RECORD_HEADER_SZ + ENUM_LEN;
- int idx = RECORD_HEADER_SZ;
- int ret;
-
- #ifdef OPENSSL_EXTRA
- ssl->cbmode = SSL_CB_MODE_WRITE;
- if (ssl->options.side == WOLFSSL_SERVER_END){
- ssl->options.serverState = SERVER_CHANGECIPHERSPEC_COMPLETE;
- if (ssl->CBIS != NULL)
- ssl->CBIS(ssl, SSL_CB_ACCEPT_LOOP, SSL_SUCCESS);
- }
- else{
- ssl->options.clientState =
- CLIENT_CHANGECIPHERSPEC_COMPLETE;
- if (ssl->CBIS != NULL)
- ssl->CBIS(ssl, SSL_CB_CONNECT_LOOP, SSL_SUCCESS);
- }
- #endif
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- sendSz += DTLS_RECORD_EXTRA;
- idx += DTLS_RECORD_EXTRA;
- }
- #endif
-
- /* are we in scr */
- if (IsEncryptionOn(ssl, 1) && ssl->options.handShakeDone) {
- sendSz += MAX_MSG_EXTRA;
- }
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddRecordHeader(output, 1, change_cipher_spec, ssl);
-
- output[idx] = 1; /* turn it on */
-
- if (IsEncryptionOn(ssl, 1) && ssl->options.handShakeDone) {
- byte input[ENUM_LEN];
- int inputSz = ENUM_LEN;
-
- input[0] = 1; /* turn it on */
- sendSz = BuildMessage(ssl, output, sendSz, input, inputSz,
- change_cipher_spec, 0, 0, 0);
- if (sendSz < 0) {
- return sendSz;
- }
- }
-
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- DtlsSEQIncrement(ssl, CUR_ORDER);
- if ((ret = DtlsMsgPoolSave(ssl, output, sendSz)) != 0)
- return ret;
- }
- #endif
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn) AddPacketName(ssl, "ChangeCipher");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "ChangeCipher", change_cipher_spec, output,
- sendSz, WRITE_PROTO, ssl->heap);
- #endif
- ssl->buffers.outputBuffer.length += sendSz;
-
- if (ssl->options.groupMessages)
- return 0;
- #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_DEBUG_DTLS)
- else if (ssl->options.dtls) {
- /* If using DTLS, force the ChangeCipherSpec message to be in the
- * same datagram as the finished message. */
- return 0;
- }
- #endif
- else
- return SendBuffered(ssl);
-}
-
-
-#if !defined(NO_OLD_TLS) && !defined(WOLFSSL_AEAD_ONLY)
-static int SSL_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz,
- int padLen, int content, int verify)
-{
- byte result[WC_MAX_DIGEST_SIZE];
- word32 digestSz = ssl->specs.hash_size; /* actual sizes */
- word32 padSz = ssl->specs.pad_size;
- int ret = 0;
-
- wc_Md5 md5;
- wc_Sha sha;
-
- /* data */
- byte seq[SEQ_SZ];
- byte conLen[ENUM_LEN + LENGTH_SZ]; /* content & length */
- const byte* macSecret = wolfSSL_GetMacSecret(ssl, verify);
-
- (void)padLen;
-
-#ifdef HAVE_FUZZER
- if (ssl->fuzzerCb)
- ssl->fuzzerCb(ssl, in, sz, FUZZ_HMAC, ssl->fuzzerCtx);
-#endif
-
- XMEMSET(seq, 0, SEQ_SZ);
- conLen[0] = (byte)content;
- c16toa((word16)sz, &conLen[ENUM_LEN]);
- WriteSEQ(ssl, verify, seq);
-
- if (ssl->specs.mac_algorithm == md5_mac) {
- ret = wc_InitMd5_ex(&md5, ssl->heap, ssl->devId);
- if (ret != 0)
- return ret;
-
- /* inner */
- ret = wc_Md5Update(&md5, macSecret, digestSz);
- ret |= wc_Md5Update(&md5, PAD1, padSz);
- ret |= wc_Md5Update(&md5, seq, SEQ_SZ);
- ret |= wc_Md5Update(&md5, conLen, sizeof(conLen));
- /* in buffer */
- ret |= wc_Md5Update(&md5, in, sz);
- if (ret != 0)
- return VERIFY_MAC_ERROR;
- ret = wc_Md5Final(&md5, result);
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* TODO: Make non-blocking */
- if (ret == WC_PENDING_E) {
- ret = wc_AsyncWait(ret, &md5.asyncDev, WC_ASYNC_FLAG_NONE);
- }
- #endif
- if (ret != 0)
- return VERIFY_MAC_ERROR;
-
- /* outer */
- ret = wc_Md5Update(&md5, macSecret, digestSz);
- ret |= wc_Md5Update(&md5, PAD2, padSz);
- ret |= wc_Md5Update(&md5, result, digestSz);
- if (ret != 0)
- return VERIFY_MAC_ERROR;
- ret = wc_Md5Final(&md5, digest);
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* TODO: Make non-blocking */
- if (ret == WC_PENDING_E) {
- ret = wc_AsyncWait(ret, &md5.asyncDev, WC_ASYNC_FLAG_NONE);
- }
- #endif
- if (ret != 0)
- return VERIFY_MAC_ERROR;
-
- wc_Md5Free(&md5);
- }
- else {
- ret = wc_InitSha_ex(&sha, ssl->heap, ssl->devId);
- if (ret != 0)
- return ret;
-
- /* inner */
- ret = wc_ShaUpdate(&sha, macSecret, digestSz);
- ret |= wc_ShaUpdate(&sha, PAD1, padSz);
- ret |= wc_ShaUpdate(&sha, seq, SEQ_SZ);
- ret |= wc_ShaUpdate(&sha, conLen, sizeof(conLen));
- /* in buffer */
- ret |= wc_ShaUpdate(&sha, in, sz);
- if (ret != 0)
- return VERIFY_MAC_ERROR;
- ret = wc_ShaFinal(&sha, result);
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* TODO: Make non-blocking */
- if (ret == WC_PENDING_E) {
- ret = wc_AsyncWait(ret, &sha.asyncDev, WC_ASYNC_FLAG_NONE);
- }
- #endif
- if (ret != 0)
- return VERIFY_MAC_ERROR;
-
- /* outer */
- ret = wc_ShaUpdate(&sha, macSecret, digestSz);
- ret |= wc_ShaUpdate(&sha, PAD2, padSz);
- ret |= wc_ShaUpdate(&sha, result, digestSz);
- if (ret != 0)
- return VERIFY_MAC_ERROR;
- ret = wc_ShaFinal(&sha, digest);
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* TODO: Make non-blocking */
- if (ret == WC_PENDING_E) {
- ret = wc_AsyncWait(ret, &sha.asyncDev, WC_ASYNC_FLAG_NONE);
- }
- #endif
- if (ret != 0)
- return VERIFY_MAC_ERROR;
-
- wc_ShaFree(&sha);
- }
- return 0;
-}
-#endif /* !NO_OLD_TLS && !WOLFSSL_AEAD_ONLY */
-
-
-#ifndef NO_CERTS
-
-#if !defined(NO_MD5) && !defined(NO_OLD_TLS)
-static int BuildMD5_CertVerify(WOLFSSL* ssl, byte* digest)
-{
- int ret;
- byte md5_result[WC_MD5_DIGEST_SIZE];
-#ifdef WOLFSSL_SMALL_STACK
- wc_Md5* md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), ssl->heap, DYNAMIC_TYPE_HASHCTX);
-#else
- wc_Md5 md5[1];
-#endif
-
- /* make md5 inner */
- ret = wc_Md5Copy(&ssl->hsHashes->hashMd5, md5); /* Save current position */
- if (ret == 0)
- ret = wc_Md5Update(md5, ssl->arrays->masterSecret,SECRET_LEN);
- if (ret == 0)
- ret = wc_Md5Update(md5, PAD1, PAD_MD5);
- if (ret == 0)
- ret = wc_Md5Final(md5, md5_result);
-
- /* make md5 outer */
- if (ret == 0) {
- ret = wc_InitMd5_ex(md5, ssl->heap, ssl->devId);
- if (ret == 0) {
- ret = wc_Md5Update(md5, ssl->arrays->masterSecret, SECRET_LEN);
- if (ret == 0)
- ret = wc_Md5Update(md5, PAD2, PAD_MD5);
- if (ret == 0)
- ret = wc_Md5Update(md5, md5_result, WC_MD5_DIGEST_SIZE);
- if (ret == 0)
- ret = wc_Md5Final(md5, digest);
- wc_Md5Free(md5);
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(md5, ssl->heap, DYNAMIC_TYPE_HASHCTX);
-#endif
-
- return ret;
-}
-#endif /* !NO_MD5 && !NO_OLD_TLS */
-
-#if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
- defined(WOLFSSL_ALLOW_TLS_SHA1))
-static int BuildSHA_CertVerify(WOLFSSL* ssl, byte* digest)
-{
- int ret;
- byte sha_result[WC_SHA_DIGEST_SIZE];
-#ifdef WOLFSSL_SMALL_STACK
- wc_Sha* sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), ssl->heap, DYNAMIC_TYPE_HASHCTX);
-#else
- wc_Sha sha[1];
-#endif
-
- /* make sha inner */
- ret = wc_ShaCopy(&ssl->hsHashes->hashSha, sha); /* Save current position */
- if (ret == 0)
- ret = wc_ShaUpdate(sha, ssl->arrays->masterSecret,SECRET_LEN);
- if (ret == 0)
- ret = wc_ShaUpdate(sha, PAD1, PAD_SHA);
- if (ret == 0)
- ret = wc_ShaFinal(sha, sha_result);
-
- /* make sha outer */
- if (ret == 0) {
- ret = wc_InitSha_ex(sha, ssl->heap, ssl->devId);
- if (ret == 0) {
- ret = wc_ShaUpdate(sha, ssl->arrays->masterSecret,SECRET_LEN);
- if (ret == 0)
- ret = wc_ShaUpdate(sha, PAD2, PAD_SHA);
- if (ret == 0)
- ret = wc_ShaUpdate(sha, sha_result, WC_SHA_DIGEST_SIZE);
- if (ret == 0)
- ret = wc_ShaFinal(sha, digest);
- wc_ShaFree(sha);
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(sha, ssl->heap, DYNAMIC_TYPE_HASHCTX);
-#endif
-
- return ret;
-}
-#endif /* !NO_SHA && (!NO_OLD_TLS || WOLFSSL_ALLOW_TLS_SHA1) */
-
-int BuildCertHashes(WOLFSSL* ssl, Hashes* hashes)
-{
- int ret = 0;
-
- (void)hashes;
-
- if (ssl->options.tls) {
- #if !defined(NO_MD5) && !defined(NO_OLD_TLS)
- ret = wc_Md5GetHash(&ssl->hsHashes->hashMd5, hashes->md5);
- if (ret != 0)
- return ret;
- #endif
- #if !defined(NO_SHA)
- ret = wc_ShaGetHash(&ssl->hsHashes->hashSha, hashes->sha);
- if (ret != 0)
- return ret;
- #endif
- if (IsAtLeastTLSv1_2(ssl)) {
- #ifndef NO_SHA256
- ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256,
- hashes->sha256);
- if (ret != 0)
- return ret;
- #endif
- #ifdef WOLFSSL_SHA384
- ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384,
- hashes->sha384);
- if (ret != 0)
- return ret;
- #endif
- #ifdef WOLFSSL_SHA512
- ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512,
- hashes->sha512);
- if (ret != 0)
- return ret;
- #endif
- }
- }
- else {
- #if !defined(NO_MD5) && !defined(NO_OLD_TLS)
- ret = BuildMD5_CertVerify(ssl, hashes->md5);
- if (ret != 0)
- return ret;
- #endif
- #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
- defined(WOLFSSL_ALLOW_TLS_SHA1))
- ret = BuildSHA_CertVerify(ssl, hashes->sha);
- if (ret != 0)
- return ret;
- #endif
- }
-
- return ret;
-}
-
-#endif /* !NO_CERTS */
-
-#ifndef WOLFSSL_NO_TLS12
-/* Persistable BuildMessage arguments */
-typedef struct BuildMsgArgs {
- word32 digestSz;
- word32 sz;
- word32 pad;
- word32 idx;
- word32 headerSz;
- word16 size;
- word32 ivSz; /* TLSv1.1 IV */
- byte* iv;
-} BuildMsgArgs;
-
-static void FreeBuildMsgArgs(WOLFSSL* ssl, void* pArgs)
-{
- BuildMsgArgs* args = (BuildMsgArgs*)pArgs;
-
- (void)ssl;
- (void)args;
-
- if (args->iv) {
- XFREE(args->iv, ssl->heap, DYNAMIC_TYPE_SALT);
- args->iv = NULL;
- }
-}
-#endif
-
-/* Build SSL Message, encrypted */
-int BuildMessage(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
- int inSz, int type, int hashOutput, int sizeOnly, int asyncOkay)
-{
-#ifndef WOLFSSL_NO_TLS12
- int ret = 0;
- BuildMsgArgs* args;
- BuildMsgArgs lcl_args;
-#ifdef WOLFSSL_ASYNC_CRYPT
- args = (BuildMsgArgs*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
-#endif
-#endif
-
- WOLFSSL_ENTER("BuildMessage");
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
-#ifdef WOLFSSL_NO_TLS12
- return BuildTls13Message(ssl, output, outSz, input, inSz, type,
- hashOutput, sizeOnly, asyncOkay);
-#else
-#ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3) {
- return BuildTls13Message(ssl, output, outSz, input, inSz, type,
- hashOutput, sizeOnly, asyncOkay);
- }
-#endif
-
- ret = WC_NOT_PENDING_E;
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (asyncOkay) {
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.buildMsgState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_buildmsg;
- }
- }
- else
-#endif
- {
- args = &lcl_args;
- }
-
- /* Reset state */
- if (ret == WC_NOT_PENDING_E) {
- ret = 0;
- ssl->options.buildMsgState = BUILD_MSG_BEGIN;
- XMEMSET(args, 0, sizeof(BuildMsgArgs));
-
- args->sz = RECORD_HEADER_SZ + inSz;
- args->idx = RECORD_HEADER_SZ;
- args->headerSz = RECORD_HEADER_SZ;
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeBuildMsgArgs;
- #endif
- }
-
- switch (ssl->options.buildMsgState) {
- case BUILD_MSG_BEGIN:
- {
- /* catch mistaken sizeOnly parameter */
- if (!sizeOnly && (output == NULL || input == NULL) ) {
- ERROR_OUT(BAD_FUNC_ARG, exit_buildmsg);
- }
- if (sizeOnly && (output || input) ) {
- WOLFSSL_MSG("BuildMessage w/sizeOnly doesn't need input/output");
- ERROR_OUT(BAD_FUNC_ARG, exit_buildmsg);
- }
-
- ssl->options.buildMsgState = BUILD_MSG_SIZE;
- }
- FALL_THROUGH;
- case BUILD_MSG_SIZE:
- {
- args->digestSz = ssl->specs.hash_size;
- #ifdef HAVE_TRUNCATED_HMAC
- if (ssl->truncated_hmac)
- args->digestSz = min(TRUNCATED_HMAC_SZ, args->digestSz);
- #endif
- args->sz += args->digestSz;
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- args->sz += DTLS_RECORD_EXTRA;
- args->idx += DTLS_RECORD_EXTRA;
- args->headerSz += DTLS_RECORD_EXTRA;
- }
- #endif
-
- #ifndef WOLFSSL_AEAD_ONLY
- if (ssl->specs.cipher_type == block) {
- word32 blockSz = ssl->specs.block_size;
- if (ssl->options.tls1_1) {
- args->ivSz = blockSz;
- args->sz += args->ivSz;
-
- if (args->ivSz > MAX_IV_SZ)
- ERROR_OUT(BUFFER_E, exit_buildmsg);
- }
- args->sz += 1; /* pad byte */
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMWrite) {
- args->pad = (args->sz - args->headerSz -
- args->digestSz) % blockSz;
- }
- else
- #endif
- args->pad = (args->sz - args->headerSz) % blockSz;
- #ifdef OPENSSL_EXTRA
- if(args->pad != 0)
- #endif
- args->pad = blockSz - args->pad;
- args->sz += args->pad;
- }
- #endif /* WOLFSSL_AEAD_ONLY */
-
- #ifdef HAVE_AEAD
- if (ssl->specs.cipher_type == aead) {
- if (ssl->specs.bulk_cipher_algorithm != wolfssl_chacha)
- args->ivSz = AESGCM_EXP_IV_SZ;
-
- args->sz += (args->ivSz + ssl->specs.aead_mac_size - args->digestSz);
- }
- #endif
-
- /* done with size calculations */
- if (sizeOnly)
- goto exit_buildmsg;
-
- if (args->sz > (word32)outSz) {
- WOLFSSL_MSG("Oops, want to write past output buffer size");
- ERROR_OUT(BUFFER_E, exit_buildmsg);
- }
-
- if (args->ivSz > 0) {
- args->iv = (byte*)XMALLOC(args->ivSz, ssl->heap, DYNAMIC_TYPE_SALT);
- if (args->iv == NULL)
- ERROR_OUT(MEMORY_E, exit_buildmsg);
-
- ret = wc_RNG_GenerateBlock(ssl->rng, args->iv, args->ivSz);
- if (ret != 0)
- goto exit_buildmsg;
-
- }
-#if !defined(NO_PUBLIC_GCM_SET_IV) && \
- ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)) && \
- defined(HAVE_AEAD))
- if (ssl->specs.cipher_type == aead) {
- if (ssl->specs.bulk_cipher_algorithm != wolfssl_chacha)
- XMEMCPY(args->iv, ssl->keys.aead_exp_IV, AESGCM_EXP_IV_SZ);
- }
-#endif
-
- args->size = (word16)(args->sz - args->headerSz); /* include mac and digest */
- AddRecordHeader(output, args->size, (byte)type, ssl);
-
- /* write to output */
- if (args->ivSz > 0) {
- XMEMCPY(output + args->idx, args->iv,
- min(args->ivSz, MAX_IV_SZ));
- args->idx += args->ivSz;
- }
- XMEMCPY(output + args->idx, input, inSz);
- args->idx += inSz;
-
- ssl->options.buildMsgState = BUILD_MSG_HASH;
- }
- FALL_THROUGH;
- case BUILD_MSG_HASH:
- {
- if (type == handshake && hashOutput) {
- ret = HashOutput(ssl, output, args->headerSz + inSz, args->ivSz);
- if (ret != 0)
- goto exit_buildmsg;
- }
- #ifndef WOLFSSL_AEAD_ONLY
- if (ssl->specs.cipher_type == block) {
- word32 tmpIdx;
- word32 i;
-
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMWrite)
- tmpIdx = args->idx;
- else
- #endif
- tmpIdx = args->idx + args->digestSz;
-
- for (i = 0; i <= args->pad; i++)
- output[tmpIdx++] = (byte)args->pad; /* pad byte gets pad value */
- }
- #endif
-
- ssl->options.buildMsgState = BUILD_MSG_VERIFY_MAC;
- }
- FALL_THROUGH;
- case BUILD_MSG_VERIFY_MAC:
- {
- /* User Record Layer Callback handling */
- #ifdef ATOMIC_USER
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMWrite) {
- if (ssl->ctx->EncryptMacCb) {
- ret = ssl->ctx->EncryptMacCb(ssl, output + args->idx +
- args->pad + 1, type, 0,
- output + args->headerSz,
- output + args->headerSz,
- args->size - args->digestSz,
- ssl->MacEncryptCtx);
- goto exit_buildmsg;
- }
- }
- else
- #endif
- {
- if (ssl->ctx->MacEncryptCb) {
- ret = ssl->ctx->MacEncryptCb(ssl, output + args->idx,
- output + args->headerSz + args->ivSz, inSz,
- type, 0, output + args->headerSz,
- output + args->headerSz, args->size,
- ssl->MacEncryptCtx);
- goto exit_buildmsg;
- }
- }
- #endif
-
- #ifndef WOLFSSL_AEAD_ONLY
- if (ssl->specs.cipher_type != aead
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- && !ssl->options.startedETMWrite
- #endif
- ) {
- #ifdef HAVE_TRUNCATED_HMAC
- if (ssl->truncated_hmac &&
- ssl->specs.hash_size > args->digestSz) {
- #ifdef WOLFSSL_SMALL_STACK
- byte* hmac;
- #else
- byte hmac[WC_MAX_DIGEST_SIZE];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- hmac = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, ssl->heap,
- DYNAMIC_TYPE_DIGEST);
- if (hmac == NULL)
- ERROR_OUT(MEMORY_E, exit_buildmsg);
- #endif
-
- ret = ssl->hmac(ssl, hmac,
- output + args->headerSz + args->ivSz, inSz,
- -1, type, 0);
- XMEMCPY(output + args->idx, hmac, args->digestSz);
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(hmac, ssl->heap, DYNAMIC_TYPE_DIGEST);
- #endif
- }
- else
- #endif
- {
- ret = ssl->hmac(ssl, output + args->idx, output +
- args->headerSz + args->ivSz, inSz, -1, type, 0);
- }
- }
- #endif /* WOLFSSL_AEAD_ONLY */
- if (ret != 0)
- goto exit_buildmsg;
-
- ssl->options.buildMsgState = BUILD_MSG_ENCRYPT;
- }
- FALL_THROUGH;
- case BUILD_MSG_ENCRYPT:
- {
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMWrite) {
- ret = Encrypt(ssl, output + args->headerSz,
- output + args->headerSz,
- args->size - args->digestSz, asyncOkay);
- }
- else
- #endif
- {
- ret = Encrypt(ssl, output + args->headerSz,
- output + args->headerSz, args->size, asyncOkay);
- }
- if (ret != 0)
- goto exit_buildmsg;
- ssl->options.buildMsgState = BUILD_MSG_ENCRYPTED_VERIFY_MAC;
- }
- FALL_THROUGH;
- case BUILD_MSG_ENCRYPTED_VERIFY_MAC:
- {
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMWrite) {
- WOLFSSL_MSG("Calculate MAC of Encrypted Data");
-
- #ifdef HAVE_TRUNCATED_HMAC
- if (ssl->truncated_hmac &&
- ssl->specs.hash_size > args->digestSz) {
- #ifdef WOLFSSL_SMALL_STACK
- byte* hmac = NULL;
- #else
- byte hmac[WC_MAX_DIGEST_SIZE];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- hmac = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, ssl->heap,
- DYNAMIC_TYPE_DIGEST);
- if (hmac == NULL)
- ERROR_OUT(MEMORY_E, exit_buildmsg);
- #endif
-
- ret = ssl->hmac(ssl, hmac, output + args->headerSz,
- args->ivSz + inSz + args->pad + 1, -1, type,
- 0);
- XMEMCPY(output + args->idx + args->pad + 1, hmac,
- args->digestSz);
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(hmac, ssl->heap, DYNAMIC_TYPE_DIGEST);
- #endif
- }
- else
- #endif
- {
- ret = ssl->hmac(ssl, output + args->idx + args->pad + 1,
- output + args->headerSz,
- args->ivSz + inSz + args->pad + 1, -1, type,
- 0);
- }
- }
- #endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
- }
- }
-
-exit_buildmsg:
-
- WOLFSSL_LEAVE("BuildMessage", ret);
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- return ret;
- }
-#endif
-
- /* make sure build message state is reset */
- ssl->options.buildMsgState = BUILD_MSG_BEGIN;
-
- #ifdef WOLFSSL_DTLS
- if (ret == 0 && ssl->options.dtls)
- DtlsSEQIncrement(ssl, CUR_ORDER);
- #endif
-
- /* return sz on success */
- if (ret == 0)
- ret = args->sz;
-
- /* Final cleanup */
- FreeBuildMsgArgs(ssl, args);
-#ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = NULL;
-#endif
-
- return ret;
-#endif /* !WOLFSSL_NO_TLS12 */
-}
-
-#ifndef WOLFSSL_NO_TLS12
-
-int SendFinished(WOLFSSL* ssl)
-{
- int sendSz,
- finishedSz = ssl->options.tls ? TLS_FINISHED_SZ :
- FINISHED_SZ;
- byte input[FINISHED_SZ + DTLS_HANDSHAKE_HEADER_SZ]; /* max */
- byte *output;
- Hashes* hashes;
- int ret;
- int headerSz = HANDSHAKE_HEADER_SZ;
- int outputSz;
-
- WOLFSSL_START(WC_FUNC_FINISHED_SEND);
- WOLFSSL_ENTER("SendFinished");
-
- /* setup encrypt keys */
- if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0)
- return ret;
-
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- ssl->options.startedETMWrite = ssl->options.encThenMac;
- #endif
-
- /* check for available size */
- outputSz = sizeof(input) + MAX_MSG_EXTRA;
- if ((ret = CheckAvailableSize(ssl, outputSz)) != 0)
- return ret;
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- headerSz += DTLS_HANDSHAKE_EXTRA;
- ssl->keys.dtls_epoch++;
- ssl->keys.dtls_prev_sequence_number_hi =
- ssl->keys.dtls_sequence_number_hi;
- ssl->keys.dtls_prev_sequence_number_lo =
- ssl->keys.dtls_sequence_number_lo;
- ssl->keys.dtls_sequence_number_hi = 0;
- ssl->keys.dtls_sequence_number_lo = 0;
- }
- #endif
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHandShakeHeader(input, finishedSz, 0, finishedSz, finished, ssl);
-
- /* make finished hashes */
- hashes = (Hashes*)&input[headerSz];
- ret = BuildFinished(ssl, hashes,
- ssl->options.side == WOLFSSL_CLIENT_END ? client : server);
- if (ret != 0) return ret;
-
-#ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation) {
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- XMEMCPY(ssl->secure_renegotiation->client_verify_data, hashes,
- TLS_FINISHED_SZ);
- else
- XMEMCPY(ssl->secure_renegotiation->server_verify_data, hashes,
- TLS_FINISHED_SZ);
- }
-#endif
-
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- if ((ret = DtlsMsgPoolSave(ssl, input, headerSz + finishedSz)) != 0)
- return ret;
- }
- #endif
-
- sendSz = BuildMessage(ssl, output, outputSz, input, headerSz + finishedSz,
- handshake, 1, 0, 0);
- if (sendSz < 0)
- return BUILD_MSG_ERROR;
-
- if (!ssl->options.resuming) {
-#ifndef NO_SESSION_CACHE
- AddSession(ssl); /* just try */
-#endif
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- #ifdef OPENSSL_EXTRA
- ssl->options.serverState = SERVER_FINISHED_COMPLETE;
- ssl->cbmode = SSL_CB_MODE_WRITE;
- if (ssl->CBIS != NULL)
- ssl->CBIS(ssl, SSL_CB_HANDSHAKE_DONE, SSL_SUCCESS);
- #endif
- ssl->options.handShakeState = HANDSHAKE_DONE;
- ssl->options.handShakeDone = 1;
- }
- }
- else {
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- #ifdef OPENSSL_EXTRA
- ssl->options.clientState = CLIENT_FINISHED_COMPLETE;
- ssl->cbmode = SSL_CB_MODE_WRITE;
- if (ssl->CBIS != NULL)
- ssl->CBIS(ssl, SSL_CB_HANDSHAKE_DONE, SSL_SUCCESS);
- #endif
- ssl->options.handShakeState = HANDSHAKE_DONE;
- ssl->options.handShakeDone = 1;
- }
- }
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn) AddPacketName(ssl, "Finished");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "Finished", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- #endif
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendFinished", ret);
- WOLFSSL_END(WC_FUNC_FINISHED_SEND);
-
- return ret;
-}
-#endif /* WOLFSSL_NO_TLS12 */
-
-#ifndef NO_WOLFSSL_SERVER
-#if (!defined(WOLFSSL_NO_TLS12) && \
- (defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
- defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2))) || \
- (defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST))
-/* Parses and decodes the certificate then initializes "request". In the case
- * of !ssl->buffers.weOwnCert, ssl->ctx->certOcspRequest gets set to "request".
- *
- * Returns 0 on success
- */
-static int CreateOcspRequest(WOLFSSL* ssl, OcspRequest* request,
- DecodedCert* cert, byte* certData, word32 length)
-{
- int ret;
-
- if (request != NULL)
- XMEMSET(request, 0, sizeof(OcspRequest));
-
- InitDecodedCert(cert, certData, length, ssl->heap);
- /* TODO: Setup async support here */
- ret = ParseCertRelative(cert, CERT_TYPE, VERIFY, ssl->ctx->cm);
- if (ret != 0) {
- WOLFSSL_MSG("ParseCert failed");
- }
- if (ret == 0)
- ret = InitOcspRequest(request, cert, 0, ssl->heap);
- if (ret == 0) {
- /* make sure ctx OCSP request is updated */
- if (!ssl->buffers.weOwnCert) {
- wolfSSL_Mutex* ocspLock = &ssl->ctx->cm->ocsp_stapling->ocspLock;
- if (wc_LockMutex(ocspLock) == 0) {
- if (ssl->ctx->certOcspRequest == NULL)
- ssl->ctx->certOcspRequest = request;
- wc_UnLockMutex(ocspLock);
- }
- }
- }
-
- FreeDecodedCert(cert);
-
- return ret;
-}
-
-
-/* Creates OCSP response and places it in variable "response". Memory
- * management for "buffer* response" is up to the caller.
- *
- * Also creates an OcspRequest in the case that ocspRequest is null or that
- * ssl->buffers.weOwnCert is set. In those cases managing ocspRequest free'ing
- * is up to the caller. NOTE: in OcspCreateRequest ssl->ctx->certOcspRequest can
- * be set to point to "ocspRequest" and it then should not be free'd since
- * wolfSSL_CTX_free will take care of it.
- *
- * Returns 0 on success
- */
-int CreateOcspResponse(WOLFSSL* ssl, OcspRequest** ocspRequest,
- buffer* response)
-{
- int ret = 0;
- OcspRequest* request = NULL;
- byte createdRequest = 0;
-
- if (ssl == NULL || ocspRequest == NULL || response == NULL)
- return BAD_FUNC_ARG;
-
- XMEMSET(response, 0, sizeof(*response));
- request = *ocspRequest;
-
- /* unable to fetch status. skip. */
- if (ssl->ctx->cm == NULL || ssl->ctx->cm->ocspStaplingEnabled == 0)
- return 0;
-
- if (request == NULL || ssl->buffers.weOwnCert) {
- DerBuffer* der = ssl->buffers.certificate;
- #ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert = NULL;
- #else
- DecodedCert cert[1];
- #endif
-
- /* unable to fetch status. skip. */
- if (der->buffer == NULL || der->length == 0)
- return 0;
-
- #ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap,
- DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- return MEMORY_E;
- #endif
- request = (OcspRequest*)XMALLOC(sizeof(OcspRequest), ssl->heap,
- DYNAMIC_TYPE_OCSP_REQUEST);
- if (request == NULL)
- ret = MEMORY_E;
-
- createdRequest = 1;
- if (ret == 0) {
- ret = CreateOcspRequest(ssl, request, cert, der->buffer,
- der->length);
- }
-
- if (ret != 0) {
- XFREE(request, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- request = NULL;
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
- #endif
- }
-
- if (ret == 0) {
- request->ssl = ssl;
- ret = CheckOcspRequest(ssl->ctx->cm->ocsp_stapling, request, response);
-
- /* Suppressing, not critical */
- if (ret == OCSP_CERT_REVOKED ||
- ret == OCSP_CERT_UNKNOWN ||
- ret == OCSP_LOOKUP_FAIL) {
- ret = 0;
- }
- }
-
- /* free request up if error case found otherwise return it */
- if (ret != 0 && createdRequest) {
- FreeOcspRequest(request);
- XFREE(request, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- }
-
- if (ret == 0)
- *ocspRequest = request;
-
- return ret;
-}
-#endif
-#endif /* !NO_WOLFSSL_SERVER */
-
-#ifndef WOLFSSL_NO_TLS12
-
-#ifndef NO_CERTS
-#if !defined(NO_WOLFSSL_SERVER) || !defined(WOLFSSL_NO_CLIENT_AUTH)
-/* handle generation of certificate (11) */
-int SendCertificate(WOLFSSL* ssl)
-{
- int ret = 0;
- word32 certSz, certChainSz, headerSz, listSz, payloadSz;
- word32 length, maxFragment;
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_SEND);
- WOLFSSL_ENTER("SendCertificate");
-
- if (ssl->options.usingPSK_cipher || ssl->options.usingAnon_cipher)
- return 0; /* not needed */
-
- if (ssl->options.sendVerify == SEND_BLANK_CERT) {
- #ifdef OPENSSL_EXTRA
- if (ssl->version.major == SSLv3_MAJOR
- && ssl->version.minor == SSLv3_MINOR){
- SendAlert(ssl, alert_warning, no_certificate);
- return 0;
- } else {
- #endif
- certSz = 0;
- certChainSz = 0;
- headerSz = CERT_HEADER_SZ;
- length = CERT_HEADER_SZ;
- listSz = 0;
- #ifdef OPENSSL_EXTRA
- }
- #endif
- }
- else {
- if (!ssl->buffers.certificate) {
- WOLFSSL_MSG("Send Cert missing certificate buffer");
- return BUFFER_ERROR;
- }
- certSz = ssl->buffers.certificate->length;
- headerSz = 2 * CERT_HEADER_SZ;
- /* list + cert size */
- length = certSz + headerSz;
- listSz = certSz + CERT_HEADER_SZ;
-
- /* may need to send rest of chain, already has leading size(s) */
- if (certSz && ssl->buffers.certChain) {
- certChainSz = ssl->buffers.certChain->length;
- length += certChainSz;
- listSz += certChainSz;
- }
- else
- certChainSz = 0;
- }
-
- payloadSz = length;
-
- if (ssl->fragOffset != 0)
- length -= (ssl->fragOffset + headerSz);
-
- maxFragment = MAX_RECORD_SIZE;
-
- if (ssl->options.dtls) {
- #ifdef WOLFSSL_DTLS
- /* The 100 bytes is used to account for the UDP and IP headers.
- It can also include the record padding and MAC if the
- SendCertificate is called for a secure renegotiation. */
- maxFragment = MAX_MTU - DTLS_RECORD_HEADER_SZ
- - DTLS_HANDSHAKE_HEADER_SZ - 100;
- #endif /* WOLFSSL_DTLS */
- }
-
- maxFragment = wolfSSL_GetMaxRecordSize(ssl, maxFragment);
-
- while (length > 0 && ret == 0) {
- byte* output = NULL;
- word32 fragSz = 0;
- word32 i = RECORD_HEADER_SZ;
- int sendSz = RECORD_HEADER_SZ;
-
- if (!ssl->options.dtls) {
- if (ssl->fragOffset == 0) {
- if (headerSz + certSz + certChainSz <=
- maxFragment - HANDSHAKE_HEADER_SZ) {
-
- fragSz = headerSz + certSz + certChainSz;
- }
- else {
- fragSz = maxFragment - HANDSHAKE_HEADER_SZ;
- }
- sendSz += fragSz + HANDSHAKE_HEADER_SZ;
- i += HANDSHAKE_HEADER_SZ;
- }
- else {
- fragSz = min(length, maxFragment);
- sendSz += fragSz;
- }
-
- if (IsEncryptionOn(ssl, 1))
- sendSz += MAX_MSG_EXTRA;
- }
- else {
- #ifdef WOLFSSL_DTLS
- fragSz = min(length, maxFragment);
- sendSz += fragSz + DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA
- + HANDSHAKE_HEADER_SZ;
- i += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA
- + HANDSHAKE_HEADER_SZ;
- #endif
- }
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- if (ssl->fragOffset == 0) {
- if (!ssl->options.dtls) {
- AddFragHeaders(output, fragSz, 0, payloadSz, certificate, ssl);
- if (!IsEncryptionOn(ssl, 1))
- HashOutputRaw(ssl, output + RECORD_HEADER_SZ,
- HANDSHAKE_HEADER_SZ);
- }
- else {
- #ifdef WOLFSSL_DTLS
- AddHeaders(output, payloadSz, certificate, ssl);
- if (!IsEncryptionOn(ssl, 1))
- HashOutputRaw(ssl,
- output + RECORD_HEADER_SZ + DTLS_RECORD_EXTRA,
- HANDSHAKE_HEADER_SZ + DTLS_HANDSHAKE_EXTRA);
- /* Adding the headers increments these, decrement them for
- * actual message header. */
- ssl->keys.dtls_handshake_number--;
- AddFragHeaders(output, fragSz, 0, payloadSz, certificate, ssl);
- ssl->keys.dtls_handshake_number--;
- #endif /* WOLFSSL_DTLS */
- }
-
- /* list total */
- c32to24(listSz, output + i);
- if (!IsEncryptionOn(ssl, 1))
- HashOutputRaw(ssl, output + i, CERT_HEADER_SZ);
- i += CERT_HEADER_SZ;
- length -= CERT_HEADER_SZ;
- fragSz -= CERT_HEADER_SZ;
- if (certSz) {
- c32to24(certSz, output + i);
- if (!IsEncryptionOn(ssl, 1))
- HashOutputRaw(ssl, output + i, CERT_HEADER_SZ);
- i += CERT_HEADER_SZ;
- length -= CERT_HEADER_SZ;
- fragSz -= CERT_HEADER_SZ;
-
- if (!IsEncryptionOn(ssl, 1)) {
- HashOutputRaw(ssl, ssl->buffers.certificate->buffer, certSz);
- if (certChainSz)
- HashOutputRaw(ssl, ssl->buffers.certChain->buffer,
- certChainSz);
- }
- }
- }
- else {
- if (!ssl->options.dtls) {
- AddRecordHeader(output, fragSz, handshake, ssl);
- }
- else {
- #ifdef WOLFSSL_DTLS
- AddFragHeaders(output, fragSz, ssl->fragOffset + headerSz,
- payloadSz, certificate, ssl);
- ssl->keys.dtls_handshake_number--;
- #endif /* WOLFSSL_DTLS */
- }
- }
-
- /* member */
- if (certSz && ssl->fragOffset < certSz) {
- word32 copySz = min(certSz - ssl->fragOffset, fragSz);
- XMEMCPY(output + i,
- ssl->buffers.certificate->buffer + ssl->fragOffset, copySz);
- i += copySz;
- ssl->fragOffset += copySz;
- length -= copySz;
- fragSz -= copySz;
- }
- if (certChainSz && fragSz) {
- word32 copySz = min(certChainSz + certSz - ssl->fragOffset, fragSz);
- XMEMCPY(output + i,
- ssl->buffers.certChain->buffer + ssl->fragOffset - certSz,
- copySz);
- i += copySz;
- ssl->fragOffset += copySz;
- length -= copySz;
- }
-
- if (IsEncryptionOn(ssl, 1)) {
- byte* input = NULL;
- int inputSz = i - RECORD_HEADER_SZ; /* build msg adds rec hdr */
-
- if (inputSz < 0) {
- WOLFSSL_MSG("Send Cert bad inputSz");
- return BUFFER_E;
- }
-
- if (inputSz > 0) { /* clang thinks could be zero, let's help */
- input = (byte*)XMALLOC(inputSz, ssl->heap,
- DYNAMIC_TYPE_IN_BUFFER);
- if (input == NULL)
- return MEMORY_E;
- XMEMCPY(input, output + RECORD_HEADER_SZ, inputSz);
- }
-
- sendSz = BuildMessage(ssl, output, sendSz, input, inputSz,
- handshake, 1, 0, 0);
-
- if (inputSz > 0)
- XFREE(input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
-
- if (sendSz < 0)
- return sendSz;
- }
- else {
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- DtlsSEQIncrement(ssl, CUR_ORDER);
- #endif
- }
-
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- if ((ret = DtlsMsgPoolSave(ssl, output, sendSz)) != 0)
- return ret;
- }
- #endif
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "Certificate");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "Certificate", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- #endif
-
- ssl->buffers.outputBuffer.length += sendSz;
- if (!ssl->options.groupMessages)
- ret = SendBuffered(ssl);
- }
-
- if (ret != WANT_WRITE) {
- /* Clean up the fragment offset. */
- ssl->fragOffset = 0;
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- ssl->keys.dtls_handshake_number++;
- #endif
- if (ssl->options.side == WOLFSSL_SERVER_END){
- ssl->options.serverState = SERVER_CERT_COMPLETE;
- }
- }
-
- WOLFSSL_LEAVE("SendCertificate", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_SEND);
-
- return ret;
-}
-#endif /* !NO_WOLFSSL_SERVER || !WOLFSSL_NO_CLIENT_AUTH */
-
-/* handle generation of certificate_request (13) */
-int SendCertificateRequest(WOLFSSL* ssl)
-{
- byte *output;
- int ret;
- int sendSz;
- word32 i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- word32 dnLen = 0;
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
- WOLF_STACK_OF(WOLFSSL_X509_NAME)* names;
-#endif
-
- int typeTotal = 1; /* only 1 for now */
- int reqSz = ENUM_LEN + typeTotal + REQ_HEADER_SZ; /* add auth later */
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_SEND);
- WOLFSSL_ENTER("SendCertificateRequest");
-
- if (IsAtLeastTLSv1_2(ssl))
- reqSz += LENGTH_SZ + ssl->suites->hashSigAlgoSz;
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
- /* Certificate Authorities */
- names = ssl->ctx->ca_names;
- while (names != NULL) {
- byte seq[MAX_SEQ_SZ];
-
- /* 16-bit length | SEQ | Len | DER of name */
- dnLen += OPAQUE16_LEN + SetSequence(names->data.name->rawLen, seq) +
- names->data.name->rawLen;
- names = names->next;
- }
- reqSz += dnLen;
-#endif
-
- if (ssl->options.usingPSK_cipher || ssl->options.usingAnon_cipher)
- return 0; /* not needed */
-
- sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + reqSz;
-
- if (!ssl->options.dtls) {
- if (IsEncryptionOn(ssl, 1))
- sendSz += MAX_MSG_EXTRA;
- }
- else {
- #ifdef WOLFSSL_DTLS
- sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- i += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- #endif
- }
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHeaders(output, reqSz, certificate_request, ssl);
-
- /* write to output */
- output[i++] = (byte)typeTotal; /* # of types */
-#ifdef HAVE_ECC
- if ((ssl->options.cipherSuite0 == ECC_BYTE ||
- ssl->options.cipherSuite0 == CHACHA_BYTE) &&
- ssl->specs.sig_algo == ecc_dsa_sa_algo) {
- output[i++] = ecdsa_sign;
- } else
-#endif /* HAVE_ECC */
- {
- output[i++] = rsa_sign;
- }
-
- /* supported hash/sig */
- if (IsAtLeastTLSv1_2(ssl)) {
- c16toa(ssl->suites->hashSigAlgoSz, &output[i]);
- i += OPAQUE16_LEN;
-
- XMEMCPY(&output[i],
- ssl->suites->hashSigAlgo, ssl->suites->hashSigAlgoSz);
- i += ssl->suites->hashSigAlgoSz;
- }
-
- /* Certificate Authorities */
- c16toa((word16)dnLen, &output[i]); /* auth's */
- i += REQ_HEADER_SZ;
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
- names = ssl->ctx->ca_names;
- while (names != NULL) {
- byte seq[MAX_SEQ_SZ];
-
- c16toa((word16)names->data.name->rawLen +
- SetSequence(names->data.name->rawLen, seq), &output[i]);
- i += OPAQUE16_LEN;
- i += SetSequence(names->data.name->rawLen, output + i);
- XMEMCPY(output + i, names->data.name->raw, names->data.name->rawLen);
- i += names->data.name->rawLen;
- names = names->next;
- }
-#endif
- (void)i;
-
- if (IsEncryptionOn(ssl, 1)) {
- byte* input;
- int inputSz = i - RECORD_HEADER_SZ; /* build msg adds rec hdr */
-
- input = (byte*)XMALLOC(inputSz, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- if (input == NULL)
- return MEMORY_E;
-
- XMEMCPY(input, output + RECORD_HEADER_SZ, inputSz);
- sendSz = BuildMessage(ssl, output, sendSz, input, inputSz,
- handshake, 1, 0, 0);
- XFREE(input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
-
- if (sendSz < 0)
- return sendSz;
- } else {
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- DtlsSEQIncrement(ssl, CUR_ORDER);
- if (IsDtlsNotSctpMode(ssl)) {
- if ((ret = DtlsMsgPoolSave(ssl, output, sendSz)) != 0)
- return ret;
- }
- #endif
- ret = HashOutput(ssl, output, sendSz, 0);
- if (ret != 0)
- return ret;
- }
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "CertificateRequest");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "CertificateRequest", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- #endif
- ssl->buffers.outputBuffer.length += sendSz;
- if (ssl->options.groupMessages)
- ret = 0;
- else
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendCertificateRequest", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND);
-
- return ret;
-}
-
-#ifndef NO_WOLFSSL_SERVER
-#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
- || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
-static int BuildCertificateStatus(WOLFSSL* ssl, byte type, buffer* status,
- byte count)
-{
- byte* output = NULL;
- word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- word32 length = ENUM_LEN;
- int sendSz = 0;
- int ret = 0;
- int i = 0;
-
- WOLFSSL_ENTER("BuildCertificateStatus");
-
- switch (type) {
- case WOLFSSL_CSR2_OCSP_MULTI:
- length += OPAQUE24_LEN;
- FALL_THROUGH; /* followed by */
-
- case WOLFSSL_CSR2_OCSP:
- for (i = 0; i < count; i++)
- length += OPAQUE24_LEN + status[i].length;
- break;
-
- default:
- return 0;
- }
-
- sendSz = idx + length;
-
- if (ssl->keys.encryptionOn)
- sendSz += MAX_MSG_EXTRA;
-
- if ((ret = CheckAvailableSize(ssl, sendSz)) == 0) {
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHeaders(output, length, certificate_status, ssl);
-
- output[idx++] = type;
-
- if (type == WOLFSSL_CSR2_OCSP_MULTI) {
- c32to24(length - (ENUM_LEN + OPAQUE24_LEN), output + idx);
- idx += OPAQUE24_LEN;
- }
-
- for (i = 0; i < count; i++) {
- c32to24(status[i].length, output + idx);
- idx += OPAQUE24_LEN;
-
- XMEMCPY(output + idx, status[i].buffer, status[i].length);
- idx += status[i].length;
- }
-
- if (IsEncryptionOn(ssl, 1)) {
- byte* input;
- int inputSz = idx - RECORD_HEADER_SZ;
-
- input = (byte*)XMALLOC(inputSz, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- if (input == NULL)
- return MEMORY_E;
-
- XMEMCPY(input, output + RECORD_HEADER_SZ, inputSz);
- sendSz = BuildMessage(ssl, output, sendSz, input, inputSz,
- handshake, 1, 0, 0);
- XFREE(input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
-
- if (sendSz < 0)
- ret = sendSz;
- }
- else {
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- DtlsSEQIncrement(ssl, CUR_ORDER);
- #endif
- ret = HashOutput(ssl, output, sendSz, 0);
- }
-
- #ifdef WOLFSSL_DTLS
- if (ret == 0 && IsDtlsNotSctpMode(ssl))
- ret = DtlsMsgPoolSave(ssl, output, sendSz);
- #endif
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ret == 0 && ssl->hsInfoOn)
- AddPacketName(ssl, "CertificateStatus");
- if (ret == 0 && ssl->toInfoOn)
- AddPacketInfo(ssl, "CertificateStatus", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- #endif
-
- if (ret == 0) {
- ssl->buffers.outputBuffer.length += sendSz;
- if (!ssl->options.groupMessages)
- ret = SendBuffered(ssl);
- }
- }
-
- WOLFSSL_LEAVE("BuildCertificateStatus", ret);
- return ret;
-}
-#endif
-#endif /* NO_WOLFSSL_SERVER */
-
-/* handle generation of certificate_status (22) */
-int SendCertificateStatus(WOLFSSL* ssl)
-{
- int ret = 0;
- byte status_type = 0;
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_STATUS_SEND);
- WOLFSSL_ENTER("SendCertificateStatus");
-
- (void) ssl;
-
-#ifdef HAVE_CERTIFICATE_STATUS_REQUEST
- status_type = ssl->status_request;
-#endif
-
-#ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
- status_type = status_type ? status_type : ssl->status_request_v2;
-#endif
-
- switch (status_type) {
-
- #ifndef NO_WOLFSSL_SERVER
- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
- || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
- /* case WOLFSSL_CSR_OCSP: */
- case WOLFSSL_CSR2_OCSP:
- {
- OcspRequest* request = ssl->ctx->certOcspRequest;
- buffer response;
-
- ret = CreateOcspResponse(ssl, &request, &response);
-
- /* if a request was successfully created and not stored in
- * ssl->ctx then free it */
- if (ret == 0 && request != ssl->ctx->certOcspRequest) {
- FreeOcspRequest(request);
- XFREE(request, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- request = NULL;
- }
-
- if (ret == 0 && response.buffer) {
- ret = BuildCertificateStatus(ssl, status_type, &response, 1);
-
- XFREE(response.buffer, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- response.buffer = NULL;
- }
- break;
- }
-
- #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
- /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
-
- #if defined HAVE_CERTIFICATE_STATUS_REQUEST_V2
- case WOLFSSL_CSR2_OCSP_MULTI:
- {
- OcspRequest* request = ssl->ctx->certOcspRequest;
- buffer responses[1 + MAX_CHAIN_DEPTH];
- int i = 0;
-
- XMEMSET(responses, 0, sizeof(responses));
-
- ret = CreateOcspResponse(ssl, &request, &responses[0]);
-
- /* if a request was successfully created and not stored in
- * ssl->ctx then free it */
- if (ret == 0 && request != ssl->ctx->certOcspRequest) {
- FreeOcspRequest(request);
- XFREE(request, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- request = NULL;
- }
-
- if (ret == 0 && (!ssl->ctx->chainOcspRequest[0]
- || ssl->buffers.weOwnCertChain)) {
- buffer der;
- word32 idx = 0;
- #ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert;
- #else
- DecodedCert cert[1];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap,
- DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- return MEMORY_E;
- #endif
- request = (OcspRequest*)XMALLOC(sizeof(OcspRequest), ssl->heap,
- DYNAMIC_TYPE_OCSP_REQUEST);
- if (request == NULL) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
- #endif
- return MEMORY_E;
- }
-
- while (idx + OPAQUE24_LEN < ssl->buffers.certChain->length) {
- c24to32(ssl->buffers.certChain->buffer + idx, &der.length);
- idx += OPAQUE24_LEN;
-
- der.buffer = ssl->buffers.certChain->buffer + idx;
- idx += der.length;
-
- if (idx > ssl->buffers.certChain->length)
- break;
-
- ret = CreateOcspRequest(ssl, request, cert, der.buffer,
- der.length);
- if (ret == 0) {
- request->ssl = ssl;
- ret = CheckOcspRequest(ssl->ctx->cm->ocsp_stapling,
- request, &responses[i + 1]);
-
- /* Suppressing, not critical */
- if (ret == OCSP_CERT_REVOKED ||
- ret == OCSP_CERT_UNKNOWN ||
- ret == OCSP_LOOKUP_FAIL) {
- ret = 0;
- }
-
-
- i++;
- FreeOcspRequest(request);
- }
- }
-
- XFREE(request, ssl->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
- #endif
- }
- else {
- while (ret == 0 &&
- NULL != (request = ssl->ctx->chainOcspRequest[i])) {
- request->ssl = ssl;
- ret = CheckOcspRequest(ssl->ctx->cm->ocsp_stapling,
- request, &responses[++i]);
-
- /* Suppressing, not critical */
- if (ret == OCSP_CERT_REVOKED ||
- ret == OCSP_CERT_UNKNOWN ||
- ret == OCSP_LOOKUP_FAIL) {
- ret = 0;
- }
- }
- }
-
- if (responses[0].buffer) {
- if (ret == 0) {
- ret = BuildCertificateStatus(ssl, status_type, responses,
- (byte)i + 1);
- }
-
- for (i = 0; i < 1 + MAX_CHAIN_DEPTH; i++) {
- if (responses[i].buffer) {
- XFREE(responses[i].buffer, ssl->heap,
- DYNAMIC_TYPE_OCSP_REQUEST);
- }
- }
- }
-
- break;
- }
- #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
- #endif /* NO_WOLFSSL_SERVER */
-
- default:
- break;
- }
-
- WOLFSSL_LEAVE("SendCertificateStatus", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_STATUS_SEND);
-
- return ret;
-}
-
-#endif /* !NO_CERTS */
-
-#endif /* WOLFSSL_NO_TLS12 */
-
-
-/* If secure renegotiation is disabled, this will always return false.
- * Otherwise it checks to see if we are currently renegotiating. */
-static WC_INLINE int IsSCR(WOLFSSL* ssl)
-{
-#ifndef HAVE_SECURE_RENEGOTIATION
- (void)ssl;
-#else /* HAVE_SECURE_RENEGOTIATION */
- if (ssl->secure_renegotiation &&
- ssl->secure_renegotiation->enabled &&
- ssl->options.handShakeState != HANDSHAKE_DONE)
- return 1;
-#endif /* HAVE_SECURE_RENEGOTIATION */
- return 0;
-}
-
-
-int SendData(WOLFSSL* ssl, const void* data, int sz)
-{
- int sent = 0, /* plainText size */
- sendSz,
- ret,
- dtlsExtra = 0;
- int groupMsgs = 0;
-
- if (ssl->error == WANT_WRITE
- #ifdef WOLFSSL_ASYNC_CRYPT
- || ssl->error == WC_PENDING_E
- #endif
- ) {
- ssl->error = 0;
- }
-
- /* don't allow write after decrypt or mac error */
- if (ssl->error == VERIFY_MAC_ERROR || ssl->error == DECRYPT_ERROR) {
- /* For DTLS allow these possible errors and allow the session
- to continue despite them */
- if (ssl->options.dtls) {
- ssl->error = 0;
- }
- else {
- WOLFSSL_MSG("Not allowing write after decrypt or mac error");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data) {
- if (ssl->options.handShakeState == HANDSHAKE_DONE) {
- WOLFSSL_MSG("handshake complete, trying to send early data");
- return BUILD_MSG_ERROR;
- }
- #ifdef WOLFSSL_EARLY_DATA_GROUP
- groupMsgs = 1;
- #endif
- }
- else
-#endif
- if (ssl->options.handShakeState != HANDSHAKE_DONE && !IsSCR(ssl)) {
- int err;
- WOLFSSL_MSG("handshake not complete, trying to finish");
- if ( (err = wolfSSL_negotiate(ssl)) != WOLFSSL_SUCCESS) {
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* if async would block return WANT_WRITE */
- if (ssl->error == WC_PENDING_E) {
- return WOLFSSL_CBIO_ERR_WANT_WRITE;
- }
- #endif
- return err;
- }
- }
-
- /* last time system socket output buffer was full, try again to send */
- if (!groupMsgs && ssl->buffers.outputBuffer.length > 0) {
- WOLFSSL_MSG("output buffer was full, trying to send again");
- if ( (ssl->error = SendBuffered(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- if (ssl->error == SOCKET_ERROR_E && (ssl->options.connReset ||
- ssl->options.isClosed)) {
- ssl->error = SOCKET_PEER_CLOSED_E;
- WOLFSSL_ERROR(ssl->error);
- return 0; /* peer reset or closed */
- }
- return ssl->error;
- }
- else {
- /* advance sent to previous sent + plain size just sent */
- sent = ssl->buffers.prevSent + ssl->buffers.plainSz;
- WOLFSSL_MSG("sent write buffered data");
-
- if (sent > sz) {
- WOLFSSL_MSG("error: write() after WANT_WRITE with short size");
- return ssl->error = BAD_FUNC_ARG;
- }
- }
- }
-
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- dtlsExtra = DTLS_RECORD_EXTRA;
- }
-#endif
-
- for (;;) {
- int len;
- byte* out;
- byte* sendBuffer = (byte*)data + sent; /* may switch on comp */
- int buffSz; /* may switch on comp */
- int outputSz;
-#ifdef HAVE_LIBZ
- byte comp[MAX_RECORD_SIZE + MAX_COMP_EXTRA];
-#endif
-
- if (sent == sz) break;
-
- len = wolfSSL_GetMaxRecordSize(ssl, sz - sent);
-
-#ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- len = min(len, MAX_UDP_SIZE);
- }
-#endif
- buffSz = len;
-
- /* check for available size */
- outputSz = len + COMP_EXTRA + dtlsExtra + MAX_MSG_EXTRA;
- if ((ret = CheckAvailableSize(ssl, outputSz)) != 0)
- return ssl->error = ret;
-
- /* get output buffer */
- out = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
-#ifdef HAVE_LIBZ
- if (ssl->options.usingCompression) {
- buffSz = myCompress(ssl, sendBuffer, buffSz, comp, sizeof(comp));
- if (buffSz < 0) {
- return buffSz;
- }
- sendBuffer = comp;
- }
-#endif
- if (!ssl->options.tls1_3) {
- sendSz = BuildMessage(ssl, out, outputSz, sendBuffer, buffSz,
- application_data, 0, 0, 1);
- }
- else {
-#ifdef WOLFSSL_TLS13
- sendSz = BuildTls13Message(ssl, out, outputSz, sendBuffer, buffSz,
- application_data, 0, 0, 1);
-#else
- sendSz = BUFFER_ERROR;
-#endif
- }
- if (sendSz < 0) {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (sendSz == WC_PENDING_E)
- ssl->error = sendSz;
- #endif
- return BUILD_MSG_ERROR;
- }
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- if ( (ssl->error = SendBuffered(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- /* store for next call if WANT_WRITE or user embedSend() that
- doesn't present like WANT_WRITE */
- ssl->buffers.plainSz = len;
- ssl->buffers.prevSent = sent;
- if (ssl->error == SOCKET_ERROR_E && (ssl->options.connReset ||
- ssl->options.isClosed)) {
- ssl->error = SOCKET_PEER_CLOSED_E;
- WOLFSSL_ERROR(ssl->error);
- return 0; /* peer reset or closed */
- }
- return ssl->error;
- }
-
- sent += len;
-
- /* only one message per attempt */
- if (ssl->options.partialWrite == 1) {
- WOLFSSL_MSG("Partial Write on, only sending one record");
- break;
- }
- }
-
- return sent;
-}
-
-/* process input data */
-int ReceiveData(WOLFSSL* ssl, byte* output, int sz, int peek)
-{
- int size;
-
- WOLFSSL_ENTER("ReceiveData()");
-
- /* reset error state */
- if (ssl->error == WANT_READ
- #ifdef WOLFSSL_ASYNC_CRYPT
- || ssl->error == WC_PENDING_E
- #endif
- ) {
- ssl->error = 0;
- }
-
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- /* In DTLS mode, we forgive some errors and allow the session
- * to continue despite them. */
- if (ssl->error == VERIFY_MAC_ERROR || ssl->error == DECRYPT_ERROR)
- ssl->error = 0;
- }
-#endif /* WOLFSSL_DTLS */
-
- if (ssl->error != 0 && ssl->error != WANT_WRITE) {
- WOLFSSL_MSG("User calling wolfSSL_read in error state, not allowed");
- return ssl->error;
- }
-
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data) {
- }
- else
-#endif
- if (ssl->options.handShakeState != HANDSHAKE_DONE) {
- int err;
- WOLFSSL_MSG("Handshake not complete, trying to finish");
- if ( (err = wolfSSL_negotiate(ssl)) != WOLFSSL_SUCCESS) {
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* if async would block return WANT_WRITE */
- if (ssl->error == WC_PENDING_E) {
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- #endif
- return err;
- }
- }
-
-#ifdef HAVE_SECURE_RENEGOTIATION
-startScr:
- if (ssl->secure_renegotiation && ssl->secure_renegotiation->startScr) {
- int err;
- WOLFSSL_MSG("Need to start scr, server requested");
- if ( (err = wolfSSL_Rehandshake(ssl)) != WOLFSSL_SUCCESS)
- return err;
- ssl->secure_renegotiation->startScr = 0; /* only start once */
- }
-#endif
-
- while (ssl->buffers.clearOutputBuffer.length == 0) {
- if ( (ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- if (ssl->error == ZERO_RETURN) {
- WOLFSSL_MSG("Zero return, no more data coming");
- return 0; /* no more data coming */
- }
- if (ssl->error == SOCKET_ERROR_E) {
- if (ssl->options.connReset || ssl->options.isClosed) {
- WOLFSSL_MSG("Peer reset or closed, connection done");
- ssl->error = SOCKET_PEER_CLOSED_E;
- WOLFSSL_ERROR(ssl->error);
- return 0; /* peer reset or closed */
- }
- }
- return ssl->error;
- }
- #ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation &&
- ssl->secure_renegotiation->startScr) {
- goto startScr;
- }
- #endif
- }
-
- if (sz < (int)ssl->buffers.clearOutputBuffer.length)
- size = sz;
- else
- size = ssl->buffers.clearOutputBuffer.length;
-
- XMEMCPY(output, ssl->buffers.clearOutputBuffer.buffer, size);
-
- if (peek == 0) {
- ssl->buffers.clearOutputBuffer.length -= size;
- ssl->buffers.clearOutputBuffer.buffer += size;
- }
-
- if (ssl->buffers.clearOutputBuffer.length == 0 &&
- ssl->buffers.inputBuffer.dynamicFlag)
- ShrinkInputBuffer(ssl, NO_FORCED_FREE);
-
- WOLFSSL_LEAVE("ReceiveData()", size);
- return size;
-}
-
-
-/* send alert message */
-int SendAlert(WOLFSSL* ssl, int severity, int type)
-{
- byte input[ALERT_SIZE];
- byte *output;
- int sendSz;
- int ret;
- int outputSz;
- int dtlsExtra = 0;
-
- WOLFSSL_ENTER("SendAlert");
-
-#ifdef HAVE_WRITE_DUP
- if (ssl->dupWrite && ssl->dupSide == READ_DUP_SIDE) {
- int notifyErr = 0;
-
- WOLFSSL_MSG("Read dup side cannot write alerts, notifying sibling");
-
- if (type == close_notify) {
- notifyErr = ZERO_RETURN;
- } else if (severity == alert_fatal) {
- notifyErr = FATAL_ERROR;
- }
-
- if (notifyErr != 0) {
- return NotifyWriteSide(ssl, notifyErr);
- }
-
- return 0;
- }
-#endif
-
- /* if sendalert is called again for nonblocking */
- if (ssl->options.sendAlertState != 0) {
- ret = SendBuffered(ssl);
- if (ret == 0)
- ssl->options.sendAlertState = 0;
- return ret;
- }
-
- #ifdef OPENSSL_EXTRA
- if (ssl->CBIS != NULL) {
- ssl->CBIS(ssl, SSL_CB_ALERT, type);
- }
- #endif
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- dtlsExtra = DTLS_RECORD_EXTRA;
- #endif
-
- /* check for available size */
- outputSz = ALERT_SIZE + MAX_MSG_EXTRA + dtlsExtra;
- if ((ret = CheckAvailableSize(ssl, outputSz)) != 0)
- return ret;
-
- /* Check output buffer */
- if (ssl->buffers.outputBuffer.buffer == NULL)
- return BUFFER_E;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- input[0] = (byte)severity;
- input[1] = (byte)type;
- ssl->alert_history.last_tx.code = type;
- ssl->alert_history.last_tx.level = severity;
- if (severity == alert_fatal) {
- ssl->options.isClosed = 1; /* Don't send close_notify */
- }
-
- /* only send encrypted alert if handshake actually complete, otherwise
- other side may not be able to handle it */
- if (IsEncryptionOn(ssl, 1) && (IsAtLeastTLSv1_3(ssl->version) ||
- ssl->options.handShakeDone)) {
- sendSz = BuildMessage(ssl, output, outputSz, input, ALERT_SIZE, alert,
- 0, 0, 0);
- }
- else {
-
- AddRecordHeader(output, ALERT_SIZE, alert, ssl);
- output += RECORD_HEADER_SZ;
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- output += DTLS_RECORD_EXTRA;
- #endif
- XMEMCPY(output, input, ALERT_SIZE);
-
- sendSz = RECORD_HEADER_SZ + ALERT_SIZE;
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- sendSz += DTLS_RECORD_EXTRA;
- #endif
- }
- if (sendSz < 0)
- return BUILD_MSG_ERROR;
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "Alert");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "Alert", alert, output, sendSz, WRITE_PROTO,
- ssl->heap);
- #endif
-
- ssl->buffers.outputBuffer.length += sendSz;
- ssl->options.sendAlertState = 1;
-
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendAlert", ret);
-
- return ret;
-}
-
-const char* wolfSSL_ERR_reason_error_string(unsigned long e)
-{
-#ifdef NO_ERROR_STRINGS
-
- (void)e;
- return "no support for error strings built in";
-
-#else
-
- int error = (int)e;
-
- /* pass to wolfCrypt */
- if (error < MAX_CODE_E && error > MIN_CODE_E) {
- return wc_GetErrorString(error);
- }
-
- switch (error) {
-
-#ifdef WOLFSSL_WPAS
- case 0 :
- return "ok";
-#endif
-
- case UNSUPPORTED_SUITE :
- return "unsupported cipher suite";
-
- case INPUT_CASE_ERROR :
- return "input state error";
-
- case PREFIX_ERROR :
- return "bad index to key rounds";
-
- case MEMORY_ERROR :
- return "out of memory";
-
- case VERIFY_FINISHED_ERROR :
- return "verify problem on finished";
-
- case VERIFY_MAC_ERROR :
- return "verify mac problem";
-
- case PARSE_ERROR :
- return "parse error on header";
-
- case SIDE_ERROR :
- return "wrong client/server type";
-
- case NO_PEER_CERT :
- return "peer didn't send cert";
-
- case UNKNOWN_HANDSHAKE_TYPE :
- return "weird handshake type";
-
- case SOCKET_ERROR_E :
- return "error state on socket";
-
- case SOCKET_NODATA :
- return "expected data, not there";
-
- case INCOMPLETE_DATA :
- return "don't have enough data to complete task";
-
- case UNKNOWN_RECORD_TYPE :
- return "unknown type in record hdr";
-
- case DECRYPT_ERROR :
- return "error during decryption";
-
- case FATAL_ERROR :
- return "received alert fatal error";
-
- case ENCRYPT_ERROR :
- return "error during encryption";
-
- case FREAD_ERROR :
- return "fread problem";
-
- case NO_PEER_KEY :
- return "need peer's key";
-
- case NO_PRIVATE_KEY :
- return "need the private key";
-
- case NO_DH_PARAMS :
- return "server missing DH params";
-
- case RSA_PRIVATE_ERROR :
- return "error during rsa priv op";
-
- case MATCH_SUITE_ERROR :
- return "can't match cipher suite";
-
- case COMPRESSION_ERROR :
- return "compression mismatch error";
-
- case BUILD_MSG_ERROR :
- return "build message failure";
-
- case BAD_HELLO :
- return "client hello malformed";
-
- case DOMAIN_NAME_MISMATCH :
- return "peer subject name mismatch";
-
- case IPADDR_MISMATCH :
- return "peer ip address mismatch";
-
- case WANT_READ :
- case WOLFSSL_ERROR_WANT_READ :
- return "non-blocking socket wants data to be read";
-
- case NOT_READY_ERROR :
- return "handshake layer not ready yet, complete first";
-
- case VERSION_ERROR :
- return "record layer version error";
-
- case WANT_WRITE :
- case WOLFSSL_ERROR_WANT_WRITE :
- return "non-blocking socket write buffer full";
-
- case BUFFER_ERROR :
- return "malformed buffer input error";
-
- case VERIFY_CERT_ERROR :
- return "verify problem on certificate";
-
- case VERIFY_SIGN_ERROR :
- return "verify problem based on signature";
-
- case CLIENT_ID_ERROR :
- return "psk client identity error";
-
- case SERVER_HINT_ERROR:
- return "psk server hint error";
-
- case PSK_KEY_ERROR:
- return "psk key callback error";
-
- case NTRU_KEY_ERROR:
- return "NTRU key error";
-
- case NTRU_DRBG_ERROR:
- return "NTRU drbg error";
-
- case NTRU_ENCRYPT_ERROR:
- return "NTRU encrypt error";
-
- case NTRU_DECRYPT_ERROR:
- return "NTRU decrypt error";
-
- case GETTIME_ERROR:
- return "gettimeofday() error";
-
- case GETITIMER_ERROR:
- return "getitimer() error";
-
- case SIGACT_ERROR:
- return "sigaction() error";
-
- case SETITIMER_ERROR:
- return "setitimer() error";
-
- case LENGTH_ERROR:
- return "record layer length error";
-
- case PEER_KEY_ERROR:
- return "cant decode peer key";
-
- case ZERO_RETURN:
- case WOLFSSL_ERROR_ZERO_RETURN:
- return "peer sent close notify alert";
-
- case ECC_CURVETYPE_ERROR:
- return "Bad ECC Curve Type or unsupported";
-
- case ECC_CURVE_ERROR:
- return "Bad ECC Curve or unsupported";
-
- case ECC_PEERKEY_ERROR:
- return "Bad ECC Peer Key";
-
- case ECC_MAKEKEY_ERROR:
- return "ECC Make Key failure";
-
- case ECC_EXPORT_ERROR:
- return "ECC Export Key failure";
-
- case ECC_SHARED_ERROR:
- return "ECC DHE shared failure";
-
- case NOT_CA_ERROR:
- return "Not a CA by basic constraint error";
-
- case HTTP_TIMEOUT:
- return "HTTP timeout for OCSP or CRL req";
-
- case BAD_CERT_MANAGER_ERROR:
- return "Bad Cert Manager error";
-
- case OCSP_CERT_REVOKED:
- return "OCSP Cert revoked";
-
- case CRL_CERT_REVOKED:
- return "CRL Cert revoked";
-
- case CRL_MISSING:
- return "CRL missing, not loaded";
-
- case MONITOR_SETUP_E:
- return "CRL monitor setup error";
-
- case THREAD_CREATE_E:
- return "Thread creation problem";
-
- case OCSP_NEED_URL:
- return "OCSP need URL";
-
- case OCSP_CERT_UNKNOWN:
- return "OCSP Cert unknown";
-
- case OCSP_LOOKUP_FAIL:
- return "OCSP Responder lookup fail";
-
- case MAX_CHAIN_ERROR:
- return "Maximum Chain Depth Exceeded";
-
- case COOKIE_ERROR:
- return "DTLS Cookie Error";
-
- case SEQUENCE_ERROR:
- return "DTLS Sequence Error";
-
- case SUITES_ERROR:
- return "Suites Pointer Error";
-
- case OUT_OF_ORDER_E:
- return "Out of order message, fatal";
-
- case BAD_KEA_TYPE_E:
- return "Bad KEA type found";
-
- case SANITY_CIPHER_E:
- return "Sanity check on ciphertext failed";
-
- case RECV_OVERFLOW_E:
- return "Receive callback returned more than requested";
-
- case GEN_COOKIE_E:
- return "Generate Cookie Error";
-
- case NO_PEER_VERIFY:
- return "Need peer certificate verify Error";
-
- case FWRITE_ERROR:
- return "fwrite Error";
-
- case CACHE_MATCH_ERROR:
- return "Cache restore header match Error";
-
- case UNKNOWN_SNI_HOST_NAME_E:
- return "Unrecognized host name Error";
-
- case UNKNOWN_MAX_FRAG_LEN_E:
- return "Unrecognized max frag len Error";
-
- case KEYUSE_SIGNATURE_E:
- return "Key Use digitalSignature not set Error";
-
- case KEYUSE_ENCIPHER_E:
- return "Key Use keyEncipherment not set Error";
-
- case EXTKEYUSE_AUTH_E:
- return "Ext Key Use server/client auth not set Error";
-
- case SEND_OOB_READ_E:
- return "Send Callback Out of Bounds Read Error";
-
- case SECURE_RENEGOTIATION_E:
- return "Invalid Renegotiation Error";
-
- case SESSION_TICKET_LEN_E:
- return "Session Ticket Too Long Error";
-
- case SESSION_TICKET_EXPECT_E:
- return "Session Ticket Error";
-
- case SESSION_SECRET_CB_E:
- return "Session Secret Callback Error";
-
- case NO_CHANGE_CIPHER_E:
- return "Finished received from peer before Change Cipher Error";
-
- case SANITY_MSG_E:
- return "Sanity Check on message order Error";
-
- case DUPLICATE_MSG_E:
- return "Duplicate HandShake message Error";
-
- case SNI_UNSUPPORTED:
- return "Protocol version does not support SNI Error";
-
- case SOCKET_PEER_CLOSED_E:
- return "Peer closed underlying transport Error";
-
- case BAD_TICKET_KEY_CB_SZ:
- return "Bad user session ticket key callback Size Error";
-
- case BAD_TICKET_MSG_SZ:
- return "Bad session ticket message Size Error";
-
- case BAD_TICKET_ENCRYPT:
- return "Bad user ticket callback encrypt Error";
-
- case DH_KEY_SIZE_E:
- return "DH key too small Error";
-
- case SNI_ABSENT_ERROR:
- return "No Server Name Indication extension Error";
-
- case RSA_SIGN_FAULT:
- return "RSA Signature Fault Error";
-
- case HANDSHAKE_SIZE_ERROR:
- return "Handshake message too large Error";
-
- case UNKNOWN_ALPN_PROTOCOL_NAME_E:
- return "Unrecognized protocol name Error";
-
- case BAD_CERTIFICATE_STATUS_ERROR:
- return "Bad Certificate Status Message Error";
-
- case OCSP_INVALID_STATUS:
- return "Invalid OCSP Status Error";
-
- case OCSP_WANT_READ:
- return "OCSP nonblock wants read";
-
- case RSA_KEY_SIZE_E:
- return "RSA key too small";
-
- case ECC_KEY_SIZE_E:
- return "ECC key too small";
-
- case DTLS_EXPORT_VER_E:
- return "Version needs updated after code change or version mismatch";
-
- case INPUT_SIZE_E:
- return "Input size too large Error";
-
- case CTX_INIT_MUTEX_E:
- return "Initialize ctx mutex error";
-
- case EXT_MASTER_SECRET_NEEDED_E:
- return "Extended Master Secret must be enabled to resume EMS session";
-
- case DTLS_POOL_SZ_E:
- return "Maximum DTLS pool size exceeded";
-
- case DECODE_E:
- return "Decode handshake message error";
-
- case WRITE_DUP_READ_E:
- return "Write dup write side can't read error";
-
- case WRITE_DUP_WRITE_E:
- return "Write dup read side can't write error";
-
- case INVALID_CERT_CTX_E:
- return "Certificate context does not match request or not empty";
-
- case BAD_KEY_SHARE_DATA:
- return "The Key Share data contains group that wasn't in Client Hello";
-
- case MISSING_HANDSHAKE_DATA:
- return "The handshake message is missing required data";
-
- case BAD_BINDER:
- return "Binder value does not match value server calculated";
-
- case EXT_NOT_ALLOWED:
- return "Extension type not allowed in handshake message type";
-
- case INVALID_PARAMETER:
- return "The security parameter is invalid";
-
- case UNSUPPORTED_EXTENSION:
- return "TLS Extension not requested by the client";
-
- case PRF_MISSING:
- return "Pseudo-random function is not enabled";
-
- case KEY_SHARE_ERROR:
- return "Key share extension did not contain a valid named group";
-
- case POST_HAND_AUTH_ERROR:
- return "Client will not do post handshake authentication";
-
- case HRR_COOKIE_ERROR:
- return "Cookie does not match one sent in HelloRetryRequest";
-
- case MCAST_HIGHWATER_CB_E:
- return "Multicast highwater callback returned error";
-
- case ALERT_COUNT_E:
- return "Alert Count exceeded error";
-
- case EXT_MISSING:
- return "Required TLS extension missing";
-
- case DTLS_RETX_OVER_TX:
- return "DTLS interrupting flight transmit with retransmit";
-
- case DH_PARAMS_NOT_FFDHE_E:
- return "Server DH parameters were not from the FFDHE set as required";
-
- case TCA_INVALID_ID_TYPE:
- return "TLS Extension Trusted CA ID type invalid";
-
- case TCA_ABSENT_ERROR:
- return "TLS Extension Trusted CA ID response absent";
-
- case TSIP_MAC_DIGSZ_E:
- return "TSIP MAC size invalid, must be sized for SHA-1 or SHA-256";
-
- case CLIENT_CERT_CB_ERROR:
- return "Error importing client cert or key from callback";
-
- case SSL_SHUTDOWN_ALREADY_DONE_E:
- return "Shutdown has already occurred";
-
- case TLS13_SECRET_CB_E:
- return "TLS1.3 Secret Callback Error";
-
- default :
- return "unknown error number";
- }
-
-#endif /* NO_ERROR_STRINGS */
-}
-
-void SetErrorString(int error, char* str)
-{
- XSTRNCPY(str, wolfSSL_ERR_reason_error_string(error), WOLFSSL_MAX_ERROR_SZ);
- str[WOLFSSL_MAX_ERROR_SZ-1] = 0;
-}
-
-#ifndef NO_ERROR_STRINGS
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- #define SUITE_INFO(x,y,z,w,v,u) {(x),(y),(z),(w),(v),(u)}
- #else
- #define SUITE_INFO(x,y,z,w,v,u) {(x),(y),(z),(w)}
- #endif
-#else
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- #define SUITE_INFO(x,y,z,w,v,u) {(x),(z),(w),(v),(u)}
- #else
- #define SUITE_INFO(x,y,z,w,v,u) {(x),(z),(w)}
- #endif
-#endif
-
-static const CipherSuiteInfo cipher_names[] =
-{
-
-#ifdef BUILD_TLS_AES_128_GCM_SHA256
- SUITE_INFO("TLS13-AES128-GCM-SHA256","TLS_AES_128_GCM_SHA256",TLS13_BYTE,TLS_AES_128_GCM_SHA256, TLSv1_3_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_AES_256_GCM_SHA384
- SUITE_INFO("TLS13-AES256-GCM-SHA384","TLS_AES_256_GCM_SHA384",TLS13_BYTE,TLS_AES_256_GCM_SHA384, TLSv1_3_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_CHACHA20_POLY1305_SHA256
- SUITE_INFO("TLS13-CHACHA20-POLY1305-SHA256","TLS_CHACHA20_POLY1305_SHA256",TLS13_BYTE,TLS_CHACHA20_POLY1305_SHA256, TLSv1_3_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_AES_128_CCM_SHA256
- SUITE_INFO("TLS13-AES128-CCM-SHA256","TLS_AES_128_CCM_SHA256",TLS13_BYTE,TLS_AES_128_CCM_SHA256, TLSv1_3_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_AES_128_CCM_8_SHA256
- SUITE_INFO("TLS13-AES128-CCM-8-SHA256","TLS_AES_128_CCM_8_SHA256",TLS13_BYTE,TLS_AES_128_CCM_8_SHA256,TLSv1_3_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_SHA256_SHA256
- SUITE_INFO("TLS13-SHA256-SHA256","TLS_SHA256_SHA256",ECC_BYTE,TLS_SHA256_SHA256,TLSv1_3_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_SHA384_SHA384
- SUITE_INFO("TLS13-SHA384-SHA384","TLS_SHA384_SHA384",ECC_BYTE,TLS_SHA384_SHA384,TLSv1_3_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifndef WOLFSSL_NO_TLS12
-
-#ifdef BUILD_SSL_RSA_WITH_RC4_128_SHA
- SUITE_INFO("RC4-SHA","SSL_RSA_WITH_RC4_128_SHA",CIPHER_BYTE,SSL_RSA_WITH_RC4_128_SHA,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_SSL_RSA_WITH_RC4_128_MD5
- SUITE_INFO("RC4-MD5","SSL_RSA_WITH_RC4_128_MD5",CIPHER_BYTE,SSL_RSA_WITH_RC4_128_MD5,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA
- SUITE_INFO("DES-CBC3-SHA","SSL_RSA_WITH_3DES_EDE_CBC_SHA",CIPHER_BYTE,SSL_RSA_WITH_3DES_EDE_CBC_SHA,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA
- SUITE_INFO("AES128-SHA","TLS_RSA_WITH_AES_128_CBC_SHA",CIPHER_BYTE,TLS_RSA_WITH_AES_128_CBC_SHA,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA
- SUITE_INFO("AES256-SHA","TLS_RSA_WITH_AES_256_CBC_SHA",CIPHER_BYTE,TLS_RSA_WITH_AES_256_CBC_SHA,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_NULL_MD5
- SUITE_INFO("NULL-MD5","TLS_RSA_WITH_NULL_MD5",CIPHER_BYTE,TLS_RSA_WITH_NULL_MD5,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_NULL_SHA
- SUITE_INFO("NULL-SHA","TLS_RSA_WITH_NULL_SHA",CIPHER_BYTE,TLS_RSA_WITH_NULL_SHA,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_NULL_SHA256
- SUITE_INFO("NULL-SHA256","TLS_RSA_WITH_NULL_SHA256",CIPHER_BYTE,TLS_RSA_WITH_NULL_SHA256,TLSv1_2_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
- SUITE_INFO("DHE-RSA-AES128-SHA","TLS_DHE_RSA_WITH_AES_128_CBC_SHA",CIPHER_BYTE,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
- SUITE_INFO("DHE-RSA-AES256-SHA","TLS_DHE_RSA_WITH_AES_256_CBC_SHA",CIPHER_BYTE,TLS_DHE_RSA_WITH_AES_256_CBC_SHA,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
- SUITE_INFO("DHE-PSK-AES256-GCM-SHA384","TLS_DHE_PSK_WITH_AES_256_GCM_SHA384",CIPHER_BYTE,TLS_DHE_PSK_WITH_AES_256_GCM_SHA384,TLSv1_2_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
- SUITE_INFO("DHE-PSK-AES128-GCM-SHA256","TLS_DHE_PSK_WITH_AES_128_GCM_SHA256",CIPHER_BYTE,TLS_DHE_PSK_WITH_AES_128_GCM_SHA256,TLSv1_2_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_GCM_SHA384
- SUITE_INFO("PSK-AES256-GCM-SHA384","TLS_PSK_WITH_AES_256_GCM_SHA384",CIPHER_BYTE,TLS_PSK_WITH_AES_256_GCM_SHA384,TLSv1_2_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_GCM_SHA256
- SUITE_INFO("PSK-AES128-GCM-SHA256","TLS_PSK_WITH_AES_128_GCM_SHA256",CIPHER_BYTE,TLS_PSK_WITH_AES_128_GCM_SHA256,TLSv1_2_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
- SUITE_INFO("DHE-PSK-AES256-CBC-SHA384","TLS_DHE_PSK_WITH_AES_256_CBC_SHA384",CIPHER_BYTE,TLS_DHE_PSK_WITH_AES_256_CBC_SHA384,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
- SUITE_INFO("DHE-PSK-AES128-CBC-SHA256","TLS_DHE_PSK_WITH_AES_128_CBC_SHA256",CIPHER_BYTE,TLS_DHE_PSK_WITH_AES_128_CBC_SHA256,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA384
- SUITE_INFO("PSK-AES256-CBC-SHA384","TLS_PSK_WITH_AES_256_CBC_SHA384",CIPHER_BYTE,TLS_PSK_WITH_AES_256_CBC_SHA384,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA256
- SUITE_INFO("PSK-AES128-CBC-SHA256","TLS_PSK_WITH_AES_128_CBC_SHA256",CIPHER_BYTE,TLS_PSK_WITH_AES_128_CBC_SHA256,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA
- SUITE_INFO("PSK-AES128-CBC-SHA","TLS_PSK_WITH_AES_128_CBC_SHA",CIPHER_BYTE,TLS_PSK_WITH_AES_128_CBC_SHA,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA
- SUITE_INFO("PSK-AES256-CBC-SHA","TLS_PSK_WITH_AES_256_CBC_SHA",CIPHER_BYTE,TLS_PSK_WITH_AES_256_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_CCM
- SUITE_INFO("DHE-PSK-AES128-CCM","TLS_DHE_PSK_WITH_AES_128_CCM",ECC_BYTE,TLS_DHE_PSK_WITH_AES_128_CCM,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_CCM
- SUITE_INFO("DHE-PSK-AES256-CCM","TLS_DHE_PSK_WITH_AES_256_CCM",ECC_BYTE,TLS_DHE_PSK_WITH_AES_256_CCM,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CCM
- SUITE_INFO("PSK-AES128-CCM","TLS_PSK_WITH_AES_128_CCM",ECC_BYTE,TLS_PSK_WITH_AES_128_CCM,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CCM
- SUITE_INFO("PSK-AES256-CCM","TLS_PSK_WITH_AES_256_CCM",ECC_BYTE,TLS_PSK_WITH_AES_256_CCM,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CCM_8
- SUITE_INFO("PSK-AES128-CCM-8","TLS_PSK_WITH_AES_128_CCM_8",ECC_BYTE,TLS_PSK_WITH_AES_128_CCM_8,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CCM_8
- SUITE_INFO("PSK-AES256-CCM-8","TLS_PSK_WITH_AES_256_CCM_8",ECC_BYTE,TLS_PSK_WITH_AES_256_CCM_8,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_NULL_SHA384
- SUITE_INFO("DHE-PSK-NULL-SHA384","TLS_DHE_PSK_WITH_NULL_SHA384",CIPHER_BYTE,TLS_DHE_PSK_WITH_NULL_SHA384,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_NULL_SHA256
- SUITE_INFO("DHE-PSK-NULL-SHA256","TLS_DHE_PSK_WITH_NULL_SHA256",CIPHER_BYTE,TLS_DHE_PSK_WITH_NULL_SHA256,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_NULL_SHA384
- SUITE_INFO("PSK-NULL-SHA384","TLS_PSK_WITH_NULL_SHA384",CIPHER_BYTE,TLS_PSK_WITH_NULL_SHA384,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_NULL_SHA256
- SUITE_INFO("PSK-NULL-SHA256","TLS_PSK_WITH_NULL_SHA256",CIPHER_BYTE,TLS_PSK_WITH_NULL_SHA256,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_NULL_SHA
- SUITE_INFO("PSK-NULL-SHA","TLS_PSK_WITH_NULL_SHA",CIPHER_BYTE,TLS_PSK_WITH_NULL_SHA,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_HC_128_MD5
- SUITE_INFO("HC128-MD5","TLS_RSA_WITH_HC_128_MD5",CIPHER_BYTE,TLS_RSA_WITH_HC_128_MD5,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_HC_128_SHA
- SUITE_INFO("HC128-SHA","TLS_RSA_WITH_HC_128_SHA",CIPHER_BYTE,TLS_RSA_WITH_HC_128_SHA,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_RABBIT_SHA
- SUITE_INFO("RABBIT-SHA","TLS_RSA_WITH_RABBIT_SHA",CIPHER_BYTE,TLS_RSA_WITH_RABBIT_SHA,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_RC4_128_SHA
- SUITE_INFO("NTRU-RC4-SHA","TLS_NTRU_RSA_WITH_RC4_128_SHA",CIPHER_BYTE,TLS_NTRU_RSA_WITH_RC4_128_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA
- SUITE_INFO("NTRU-DES-CBC3-SHA","TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA",CIPHER_BYTE,TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_AES_128_CBC_SHA
- SUITE_INFO("NTRU-AES128-SHA","TLS_NTRU_RSA_WITH_AES_128_CBC_SHA",CIPHER_BYTE,TLS_NTRU_RSA_WITH_AES_128_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_AES_256_CBC_SHA
- SUITE_INFO("NTRU-AES256-SHA","TLS_NTRU_RSA_WITH_AES_256_CBC_SHA",CIPHER_BYTE,TLS_NTRU_RSA_WITH_AES_256_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_CCM_8
- SUITE_INFO("AES128-CCM-8","TLS_RSA_WITH_AES_128_CCM_8",ECC_BYTE,TLS_RSA_WITH_AES_128_CCM_8, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_CCM_8
- SUITE_INFO("AES256-CCM-8","TLS_RSA_WITH_AES_256_CCM_8",ECC_BYTE,TLS_RSA_WITH_AES_256_CCM_8, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM
- SUITE_INFO("ECDHE-ECDSA-AES128-CCM","TLS_ECDHE_ECDSA_WITH_AES_128_CCM",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_AES_128_CCM, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
- SUITE_INFO("ECDHE-ECDSA-AES128-CCM-8","TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8
- SUITE_INFO("ECDHE-ECDSA-AES256-CCM-8","TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
- SUITE_INFO("ECDHE-RSA-AES128-SHA","TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA",ECC_BYTE,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
- SUITE_INFO("ECDHE-RSA-AES256-SHA","TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA",ECC_BYTE,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
- SUITE_INFO("ECDHE-ECDSA-AES128-SHA","TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
- SUITE_INFO("ECDHE-ECDSA-AES256-SHA","TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_RC4_128_SHA
- SUITE_INFO("ECDHE-RSA-RC4-SHA","TLS_ECDHE_RSA_WITH_RC4_128_SHA",ECC_BYTE,TLS_ECDHE_RSA_WITH_RC4_128_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
- SUITE_INFO("ECDHE-RSA-DES-CBC3-SHA","TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",ECC_BYTE,TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
- SUITE_INFO("ECDHE-ECDSA-RC4-SHA","TLS_ECDHE_ECDSA_WITH_RC4_128_SHA",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
- SUITE_INFO("ECDHE-ECDSA-DES-CBC3-SHA","TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA256
- SUITE_INFO("AES128-SHA256","TLS_RSA_WITH_AES_128_CBC_SHA256",CIPHER_BYTE,TLS_RSA_WITH_AES_128_CBC_SHA256, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA256
- SUITE_INFO("AES256-SHA256","TLS_RSA_WITH_AES_256_CBC_SHA256",CIPHER_BYTE,TLS_RSA_WITH_AES_256_CBC_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
- SUITE_INFO("DHE-RSA-AES128-SHA256","TLS_DHE_RSA_WITH_AES_128_CBC_SHA256",CIPHER_BYTE,TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
- SUITE_INFO("DHE-RSA-AES256-SHA256","TLS_DHE_RSA_WITH_AES_256_CBC_SHA256",CIPHER_BYTE,TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
- SUITE_INFO("ECDH-RSA-AES128-SHA","TLS_ECDH_RSA_WITH_AES_128_CBC_SHA",ECC_BYTE,TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
- SUITE_INFO("ECDH-RSA-AES256-SHA","TLS_ECDH_RSA_WITH_AES_256_CBC_SHA",ECC_BYTE,TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
- SUITE_INFO("ECDH-ECDSA-AES128-SHA","TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA",ECC_BYTE,TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
- SUITE_INFO("ECDH-ECDSA-AES256-SHA","TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA",ECC_BYTE,TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_RC4_128_SHA
- SUITE_INFO("ECDH-RSA-RC4-SHA","TLS_ECDH_RSA_WITH_RC4_128_SHA",ECC_BYTE,TLS_ECDH_RSA_WITH_RC4_128_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
- SUITE_INFO("ECDH-RSA-DES-CBC3-SHA","TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA",ECC_BYTE,TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
- SUITE_INFO("ECDH-ECDSA-RC4-SHA","TLS_ECDH_ECDSA_WITH_RC4_128_SHA",ECC_BYTE,TLS_ECDH_ECDSA_WITH_RC4_128_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
- SUITE_INFO("ECDH-ECDSA-DES-CBC3-SHA","TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA",ECC_BYTE,TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256
- SUITE_INFO("AES128-GCM-SHA256","TLS_RSA_WITH_AES_128_GCM_SHA256",CIPHER_BYTE,TLS_RSA_WITH_AES_128_GCM_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384
- SUITE_INFO("AES256-GCM-SHA384","TLS_RSA_WITH_AES_256_GCM_SHA384",CIPHER_BYTE,TLS_RSA_WITH_AES_256_GCM_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
- SUITE_INFO("DHE-RSA-AES128-GCM-SHA256","TLS_DHE_RSA_WITH_AES_128_GCM_SHA256",CIPHER_BYTE,TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
- SUITE_INFO("DHE-RSA-AES256-GCM-SHA384","TLS_DHE_RSA_WITH_AES_256_GCM_SHA384",CIPHER_BYTE,TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
- SUITE_INFO("ECDHE-RSA-AES128-GCM-SHA256","TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",ECC_BYTE,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
- SUITE_INFO("ECDHE-RSA-AES256-GCM-SHA384","TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384",ECC_BYTE,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
- SUITE_INFO("ECDHE-ECDSA-AES128-GCM-SHA256","TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
- SUITE_INFO("ECDHE-ECDSA-AES256-GCM-SHA384","TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
- SUITE_INFO("ECDH-RSA-AES128-GCM-SHA256","TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256",ECC_BYTE,TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
- SUITE_INFO("ECDH-RSA-AES256-GCM-SHA384","TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384",ECC_BYTE,TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
- SUITE_INFO("ECDH-ECDSA-AES128-GCM-SHA256","TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256",ECC_BYTE,TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
- SUITE_INFO("ECDH-ECDSA-AES256-GCM-SHA384","TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384",ECC_BYTE,TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
- SUITE_INFO("CAMELLIA128-SHA","TLS_RSA_WITH_CAMELLIA_128_CBC_SHA",CIPHER_BYTE,TLS_RSA_WITH_CAMELLIA_128_CBC_SHA,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
- SUITE_INFO("DHE-RSA-CAMELLIA128-SHA","TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA",CIPHER_BYTE,TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
- SUITE_INFO("CAMELLIA256-SHA","TLS_RSA_WITH_CAMELLIA_256_CBC_SHA",CIPHER_BYTE,TLS_RSA_WITH_CAMELLIA_256_CBC_SHA,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA
- SUITE_INFO("DHE-RSA-CAMELLIA256-SHA","TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA",CIPHER_BYTE,TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
- SUITE_INFO("CAMELLIA128-SHA256","TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256",CIPHER_BYTE,TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
- SUITE_INFO("DHE-RSA-CAMELLIA128-SHA256","TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256",CIPHER_BYTE,TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
- SUITE_INFO("CAMELLIA256-SHA256","TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256",CIPHER_BYTE,TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
- SUITE_INFO("DHE-RSA-CAMELLIA256-SHA256","TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256",CIPHER_BYTE,TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
- SUITE_INFO("ECDHE-RSA-AES128-SHA256","TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256",ECC_BYTE,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
- SUITE_INFO("ECDHE-ECDSA-AES128-SHA256","TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
- SUITE_INFO("ECDH-RSA-AES128-SHA256","TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256",ECC_BYTE,TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
- SUITE_INFO("ECDH-ECDSA-AES128-SHA256","TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256",ECC_BYTE,TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
- SUITE_INFO("ECDHE-RSA-AES256-SHA384","TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384",ECC_BYTE,TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
- SUITE_INFO("ECDHE-ECDSA-AES256-SHA384","TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
- SUITE_INFO("ECDH-RSA-AES256-SHA384","TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384",ECC_BYTE,TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
- SUITE_INFO("ECDH-ECDSA-AES256-SHA384","TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384",ECC_BYTE,TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
- SUITE_INFO("ECDHE-RSA-CHACHA20-POLY1305","TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256",CHACHA_BYTE,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
- SUITE_INFO("ECDHE-ECDSA-CHACHA20-POLY1305","TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256",CHACHA_BYTE,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
- SUITE_INFO("DHE-RSA-CHACHA20-POLY1305","TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256",CHACHA_BYTE,TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
- SUITE_INFO("ECDHE-RSA-CHACHA20-POLY1305-OLD","TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256",CHACHA_BYTE,TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256
- SUITE_INFO("ECDHE-ECDSA-CHACHA20-POLY1305-OLD","TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256",CHACHA_BYTE,TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
- SUITE_INFO("DHE-RSA-CHACHA20-POLY1305-OLD","TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256",CHACHA_BYTE,TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DH_anon_WITH_AES_128_CBC_SHA
- SUITE_INFO("ADH-AES128-SHA","TLS_DH_anon_WITH_AES_128_CBC_SHA",CIPHER_BYTE,TLS_DH_anon_WITH_AES_128_CBC_SHA, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DH_anon_WITH_AES_256_GCM_SHA384
- SUITE_INFO("ADH-AES256-GCM-SHA384","TLS_DH_anon_WITH_AES_256_GCM_SHA384",CIPHER_BYTE,TLS_DH_anon_WITH_AES_256_GCM_SHA384, TLSv1_2_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_QSH
- SUITE_INFO("QSH","TLS_QSH",QSH_BYTE,TLS_QSH, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef HAVE_RENEGOTIATION_INDICATION
- SUITE_INFO("RENEGOTIATION-INFO","TLS_EMPTY_RENEGOTIATION_INFO_SCSV",CIPHER_BYTE,TLS_EMPTY_RENEGOTIATION_INFO_SCSV,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_SSL_RSA_WITH_IDEA_CBC_SHA
- SUITE_INFO("IDEA-CBC-SHA","SSL_RSA_WITH_IDEA_CBC_SHA",CIPHER_BYTE,SSL_RSA_WITH_IDEA_CBC_SHA,SSLv3_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_NULL_SHA
- SUITE_INFO("ECDHE-ECDSA-NULL-SHA","TLS_ECDHE_ECDSA_WITH_NULL_SHA",ECC_BYTE,TLS_ECDHE_ECDSA_WITH_NULL_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_PSK_WITH_NULL_SHA256
- SUITE_INFO("ECDHE-PSK-NULL-SHA256","TLS_ECDHE_PSK_WITH_NULL_SHA256",ECC_BYTE,TLS_ECDHE_PSK_WITH_NULL_SHA256,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
- SUITE_INFO("ECDHE-PSK-AES128-CBC-SHA256","TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256",ECC_BYTE,TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256,TLSv1_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256
- SUITE_INFO("PSK-CHACHA20-POLY1305","TLS_PSK_WITH_CHACHA20_POLY1305_SHA256",CHACHA_BYTE,TLS_PSK_WITH_CHACHA20_POLY1305_SHA256,TLSv1_2_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256
- SUITE_INFO("ECDHE-PSK-CHACHA20-POLY1305","TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256",CHACHA_BYTE,TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256,TLSv1_2_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256
- SUITE_INFO("DHE-PSK-CHACHA20-POLY1305","TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256",CHACHA_BYTE,TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256,TLSv1_2_MINOR,SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
- SUITE_INFO("EDH-RSA-DES-CBC3-SHA","TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA",CIPHER_BYTE,TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA, TLSv1_MINOR, SSLv3_MAJOR),
-#endif
-
-#ifdef BUILD_WDM_WITH_NULL_SHA256
- SUITE_INFO("WDM-NULL-SHA256","WDM_WITH_NULL_SHA256",CIPHER_BYTE,WDM_WITH_NULL_SHA256, TLSv1_3_MINOR, SSLv3_MAJOR)
-#endif
-
-#endif /* WOLFSSL_NO_TLS12 */
-};
-
-
-/* returns the cipher_names array */
-const CipherSuiteInfo* GetCipherNames(void)
-{
- return cipher_names;
-}
-
-
-/* returns the number of elements in the cipher_names array */
-int GetCipherNamesSize(void)
-{
- return (int)(sizeof(cipher_names) / sizeof(CipherSuiteInfo));
-}
-
-
-const char* GetCipherNameInternal(const byte cipherSuite0, const byte cipherSuite)
-{
- int i;
- const char* nameInternal = "None";
-
- for (i = 0; i < GetCipherNamesSize(); i++) {
- if ((cipher_names[i].cipherSuite0 == cipherSuite0) &&
- (cipher_names[i].cipherSuite == cipherSuite)) {
- nameInternal = cipher_names[i].name;
- break;
- }
- }
- return nameInternal;
-}
-
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
-const char* GetCipherKeaStr(char n[][MAX_SEGMENT_SZ]) {
- const char* keaStr = NULL;
- const char *n0,*n1,*n2,*n3,*n4;
- n0 = n[0];
- n1 = n[1];
- n2 = n[2];
- n3 = n[3];
- n4 = n[4];
-
- if (XSTRNCMP(n0,"ECDHE",5) == 0 && XSTRNCMP(n1,"PSK",3) == 0)
- keaStr = "ECDHEPSK";
- else if (XSTRNCMP(n0,"ECDH",4) == 0)
- keaStr = "ECDH";
- else if (XSTRNCMP(n0,"DHE",3) == 0 && XSTRNCMP(n1,"PSK",3) == 0)
- keaStr = "DHEPSK";
- else if (XSTRNCMP(n0,"DHE",3) == 0)
- keaStr = "DH";
- else if (XSTRNCMP(n0,"RSA",3) == 0 && XSTRNCMP(n1,"PSK",3) == 0)
- keaStr = "RSAPSK";
- else if (XSTRNCMP(n0,"SRP",3) == 0)
- keaStr = "SRP";
- else if (XSTRNCMP(n0,"PSK",3) == 0)
- keaStr = "PSK";
- else if (XSTRNCMP(n0,"EDH",3) == 0)
- keaStr = "EDH";
- else if ((XSTRNCMP(n1,"SHA",3) == 0) || (XSTRNCMP(n2,"SHA",3) == 0) ||
- (XSTRNCMP(n3,"SHA",3) == 0) || (XSTRNCMP(n4,"SHA",3) == 0) ||
- (XSTRNCMP(n2,"RSA",3) == 0) || (XSTRNCMP(n0,"AES128",6) == 0) ||
- (XSTRNCMP(n0,"AES256",6) == 0) || (XSTRNCMP(n1,"MD5",3) == 0))
- keaStr = "RSA";
- else
- keaStr = "unknown";
-
- return keaStr;
-}
-
-const char* GetCipherAuthStr(char n[][MAX_SEGMENT_SZ]) {
-
- const char* authStr = NULL;
- const char *n0,*n1,*n2;
- n0 = n[0];
- n1 = n[1];
- n2 = n[2];
-
- if ((XSTRNCMP(n0,"AES128",6) == 0) || (XSTRNCMP(n0,"AES256",6) == 0) ||
- ((XSTRNCMP(n0,"TLS13",5) == 0) && ((XSTRNCMP(n1,"AES128",6) == 0) ||
- (XSTRNCMP(n1,"AES256",6) == 0) || (XSTRNCMP(n1,"CHACHA20",8) == 0))) ||
- (XSTRNCMP(n0,"RSA",3) == 0) || (XSTRNCMP(n1,"RSA",3) == 0) ||
- (XSTRNCMP(n1,"SHA",3) == 0) || (XSTRNCMP(n2,"SHA",3) == 0) ||
- (XSTRNCMP(n1,"MD5",3) == 0))
- authStr = "RSA";
- else if (XSTRNCMP(n0,"PSK",3) == 0 || XSTRNCMP(n1,"PSK",3) == 0)
- authStr = "PSK";
- else if (XSTRNCMP(n0,"SRP",3) == 0 && XSTRNCMP(n1,"AES",3) == 0)
- authStr = "SRP";
- else if (XSTRNCMP(n1,"ECDSA",5) == 0)
- authStr = "ECDSA";
- else
- authStr = "unknown";
-
- return authStr;
-}
-
-const char* GetCipherEncStr(char n[][MAX_SEGMENT_SZ]) {
- const char* encStr = NULL;
- const char *n0,*n1,*n2,*n3;
- n0 = n[0];
- n1 = n[1];
- n2 = n[2];
- n3 = n[3];
-
- if ((XSTRNCMP(n0,"AES256",6) == 0 && XSTRNCMP(n1,"GCM",3) == 0) ||
- (XSTRNCMP(n1,"AES256",6) == 0 && XSTRNCMP(n2,"GCM",3) == 0) ||
- (XSTRNCMP(n2,"AES256",6) == 0 && XSTRNCMP(n3,"GCM",3) == 0))
- encStr = "AESGCM(256)";
-
- else if ((XSTRNCMP(n0,"AES128",6) == 0 && XSTRNCMP(n1,"GCM",3) == 0) ||
- (XSTRNCMP(n1,"AES128",6) == 0 && XSTRNCMP(n2,"GCM",3) == 0) ||
- (XSTRNCMP(n2,"AES128",6) == 0 && XSTRNCMP(n3,"GCM",3) == 0))
- encStr = "AESGCM(128)";
-
- else if ((XSTRNCMP(n0,"AES128",6) == 0 && XSTRNCMP(n1,"CCM",3) == 0) ||
- (XSTRNCMP(n1,"AES128",6) == 0 && XSTRNCMP(n2,"CCM",3) == 0) ||
- (XSTRNCMP(n2,"AES128",6) == 0 && XSTRNCMP(n3,"CCM",3) == 0))
- encStr = "AESCCM(128)";
-
- else if ((XSTRNCMP(n0,"AES128",6) == 0) ||
- (XSTRNCMP(n1,"AES128",6) == 0) ||
- (XSTRNCMP(n2,"AES128",6) == 0) ||
- (XSTRNCMP(n1,"AES",3) == 0 && XSTRNCMP(n2,"128",3) == 0) ||
- (XSTRNCMP(n2,"AES",3) == 0 && XSTRNCMP(n3,"128",3) == 0))
- encStr = "AES(128)";
-
- else if ((XSTRNCMP(n0,"AES256",6) == 0) ||
- (XSTRNCMP(n1,"AES256",6) == 0) ||
- (XSTRNCMP(n2,"AES256",6) == 0) ||
- (XSTRNCMP(n1,"AES",3) == 0 && XSTRNCMP(n2,"256",3) == 0) ||
- (XSTRNCMP(n2,"AES",3) == 0 && XSTRNCMP(n3,"256",3) == 0))
- encStr = "AES(256)";
-
- else if ((XSTRNCMP(n0,"CAMELLIA256",11) == 0) ||
- (XSTRNCMP(n2,"CAMELLIA256",11) == 0))
- encStr = "CAMELLIA(256)";
- else if ((XSTRNCMP(n0,"CAMELLIA128",11) == 0) ||
- (XSTRNCMP(n2,"CAMELLIA128",11) == 0))
- encStr = "CAMELLIA(128)";
- else if ((XSTRNCMP(n0,"RC4",3) == 0) || (XSTRNCMP(n2,"RC4",3) == 0))
- encStr = "RC4";
- else if (((XSTRNCMP(n0,"DES",3) == 0) || (XSTRNCMP(n2,"DES",3) == 0)) &&
- ((XSTRNCMP(n1,"CBC3",4) == 0) || (XSTRNCMP(n3,"CBC3",4) == 0)))
- encStr = "3DES";
- else if ((XSTRNCMP(n1,"CHACHA20",8) == 0 && XSTRNCMP(n2,"POLY1305",8) == 0) ||
- (XSTRNCMP(n2,"CHACHA20",8) == 0 && XSTRNCMP(n3,"POLY1305",8) == 0))
- encStr = "CHACHA20/POLY1305(256)";
- else if ((XSTRNCMP(n0,"NULL",4) == 0) || (XSTRNCMP(n1,"NULL",4) == 0) ||
- (XSTRNCMP(n2,"NULL",4) == 0) ||
- ((XSTRNCMP(n0,"TLS13",5) == 0) && (XSTRNCMP(n3,"",0) == 0)))
- encStr = "None";
- else if ((XSTRNCMP(n0,"IDEA",4) == 0))
- encStr = "IDEA";
- else if ((XSTRNCMP(n0,"RABBIT",4) == 0))
- encStr = "RABBIT";
- else if ((XSTRNCMP(n0,"HC128",5) == 0))
- encStr = "HC128";
- else
- encStr = "unknown";
-
- return encStr;
-}
-
-/* Returns the MAC string of a cipher or "unknown" on failure */
-const char* GetCipherMacStr(char n[][MAX_SEGMENT_SZ]) {
-
- const char* macStr = NULL;
- const char *n1,*n2,*n3,*n4;
- n1 = n[1];
- n2 = n[2];
- n3 = n[3];
- n4 = n[4];
-
- if ((XSTRNCMP(n4,"SHA256",6) == 0) || (XSTRNCMP(n3,"SHA256",6) == 0) ||
- (XSTRNCMP(n2,"SHA256",6) == 0) || (XSTRNCMP(n1,"SHA256",6) == 0))
- macStr = "SHA256";
- else if ((XSTRNCMP(n4,"SHA384",6) == 0) ||
- (XSTRNCMP(n3,"SHA384",6) == 0) ||
- (XSTRNCMP(n2,"SHA384",6) == 0) ||
- (XSTRNCMP(n1,"SHA384",6) == 0))
- macStr = "SHA384";
- else if ((XSTRNCMP(n4,"SHA",3) == 0) || (XSTRNCMP(n3,"SHA",3) == 0) ||
- (XSTRNCMP(n2,"SHA",3) == 0) || (XSTRNCMP(n1,"SHA",3) == 0) ||
- (XSTRNCMP(n1,"MD5",3) == 0))
- macStr = "SHA1";
- else if ((XSTRNCMP(n3,"GCM",3) == 0) ||
- (XSTRNCMP(n1,"CCM",3) == 0) ||
- (XSTRNCMP(n2,"CCM",3) == 0) || (XSTRNCMP(n3,"CCM",3) == 0) ||
- (XSTRNCMP(n1,"CHACHA20",8) == 0 && XSTRNCMP(n2,"POLY1305",8) == 0) ||
- (XSTRNCMP(n2,"CHACHA20",8) == 0 && XSTRNCMP(n3,"POLY1305",8) == 0))
- macStr = "AEAD";
- else
- macStr = "unknown";
-
- return macStr;
-}
-
-/* Returns the number of bits based on the cipher enc string, or 0 on failure */
-int SetCipherBits(const char* enc) {
- int ret = WOLFSSL_FAILURE;
-
- if ((XSTRNCMP(enc,"AESGCM(256)",11) == 0) ||
- (XSTRNCMP(enc,"AES(256)",8) == 0) ||
- (XSTRNCMP(enc,"CAMELLIA(256)",13) == 0) ||
- (XSTRNCMP(enc,"CHACHA20/POLY1305(256)",22) == 0))
- ret = 256;
- else if
- ((XSTRNCMP(enc,"3DES",4) == 0))
- ret = 168;
- else if
- ((XSTRNCMP(enc,"AESGCM(128)",11) == 0) ||
- (XSTRNCMP(enc,"AES(128)",8) == 0) ||
- (XSTRNCMP(enc,"CAMELLIA(128)",13) == 0) ||
- (XSTRNCMP(enc,"IDEA",4) == 0) ||
- (XSTRNCMP(enc,"RC4",3) == 0))
- ret = 128;
- else if
- ((XSTRNCMP(enc,"DES",3) == 0))
- ret = 56;
-
- return ret;
-}
-#endif /* WOLFSSL_QT || OPENSSL_ALL */
-
-const char* GetCipherNameIana(const byte cipherSuite0, const byte cipherSuite)
-{
-#ifndef NO_ERROR_STRINGS
- int i;
- const char* nameIana = "NONE";
-
- for (i = 0; i < GetCipherNamesSize(); i++) {
- if ((cipher_names[i].cipherSuite0 == cipherSuite0) &&
- (cipher_names[i].cipherSuite == cipherSuite)) {
- nameIana = cipher_names[i].name_iana;
- break;
- }
- }
- return nameIana;
-#else
- (void)cipherSuite0;
- (void)cipherSuite;
- return NULL;
-#endif
-}
-
-const char* wolfSSL_get_cipher_name_internal(WOLFSSL* ssl)
-{
- if (ssl == NULL) {
- return NULL;
- }
-
- return GetCipherNameInternal(ssl->options.cipherSuite0, ssl->options.cipherSuite);
-}
-
-const char* wolfSSL_get_cipher_name_iana(WOLFSSL* ssl)
-{
- if (ssl == NULL) {
- return NULL;
- }
-
- return GetCipherNameIana(ssl->options.cipherSuite0, ssl->options.cipherSuite);
-}
-
-int GetCipherSuiteFromName(const char* name, byte* cipherSuite0,
- byte* cipherSuite)
-{
- int ret = BAD_FUNC_ARG;
- int i;
- unsigned long len = (unsigned long)XSTRLEN(name);
-
- for (i = 0; i < GetCipherNamesSize(); i++) {
- if (XSTRNCMP(name, cipher_names[i].name, len) == 0) {
- *cipherSuite0 = cipher_names[i].cipherSuite0;
- *cipherSuite = cipher_names[i].cipherSuite;
- ret = 0;
- break;
- }
- }
-
- return ret;
-}
-
-/**
-Set the enabled cipher suites.
-
-@param [out] suites Suites structure.
-@param [in] list List of cipher suites, only supports full name from
- cipher_names[] delimited by ':'.
-
-@return true on success, else false.
-*/
-int SetCipherList(WOLFSSL_CTX* ctx, Suites* suites, const char* list)
-{
- int ret = 0;
- int idx = 0;
- int haveRSAsig = 0;
- int haveECDSAsig = 0;
- int haveAnon = 0;
- const int suiteSz = GetCipherNamesSize();
- char* next = (char*)list;
-
- if (suites == NULL || list == NULL) {
- WOLFSSL_MSG("SetCipherList parameter error");
- return 0;
- }
-
- if (next[0] == 0 || XSTRNCMP(next, "ALL", 3) == 0 ||
- XSTRNCMP(next, "DEFAULT", 7) == 0)
- return 1; /* wolfSSL default */
-
- do {
- char* current = next;
- char name[MAX_SUITE_NAME + 1];
- int i;
- word32 length;
-
- next = XSTRSTR(next, ":");
- length = min(sizeof(name), !next ? (word32)XSTRLEN(current) /* last */
- : (word32)(next - current));
-
- XSTRNCPY(name, current, length);
- name[(length == sizeof(name)) ? length - 1 : length] = 0;
-
- for (i = 0; i < suiteSz; i++) {
- if (XSTRNCMP(name, cipher_names[i].name, sizeof(name)) == 0
- #ifndef NO_ERROR_STRINGS
- || XSTRNCMP(name, cipher_names[i].name_iana, sizeof(name)) == 0
- #endif
- ) {
- #ifdef WOLFSSL_DTLS
- /* don't allow stream ciphers with DTLS */
- if (ctx->method->version.major == DTLS_MAJOR) {
- if (XSTRSTR(name, "RC4") ||
- XSTRSTR(name, "HC128") ||
- XSTRSTR(name, "RABBIT"))
- {
- WOLFSSL_MSG("Stream ciphers not supported with DTLS");
- continue;
- }
-
- }
- #endif /* WOLFSSL_DTLS */
-
- if (idx + 1 >= WOLFSSL_MAX_SUITE_SZ) {
- WOLFSSL_MSG("WOLFSSL_MAX_SUITE_SZ set too low");
- return 0; /* suites buffer not large enough, error out */
- }
-
- suites->suites[idx++] = cipher_names[i].cipherSuite0;
- suites->suites[idx++] = cipher_names[i].cipherSuite;
- /* The suites are either ECDSA, RSA, PSK, or Anon. The RSA
- * suites don't necessarily have RSA in the name. */
- #ifdef WOLFSSL_TLS13
- if (cipher_names[i].cipherSuite0 == TLS13_BYTE ||
- (cipher_names[i].cipherSuite0 == ECC_BYTE &&
- (cipher_names[i].cipherSuite == TLS_SHA256_SHA256 ||
- cipher_names[i].cipherSuite == TLS_SHA384_SHA384))) {
- #ifndef NO_RSA
- haveRSAsig = 1;
- #endif
- #if defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)
- haveECDSAsig = 1;
- #endif
- }
- else
- #endif
- #if defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)
- if ((haveECDSAsig == 0) && XSTRSTR(name, "ECDSA"))
- haveECDSAsig = 1;
- else
- #endif
- #ifdef HAVE_ANON
- if (XSTRSTR(name, "ADH"))
- haveAnon = 1;
- else
- #endif
- if (haveRSAsig == 0
- #ifndef NO_PSK
- && (XSTRSTR(name, "PSK") == NULL)
- #endif
- ) {
- haveRSAsig = 1;
- }
-
- ret = 1; /* found at least one */
- break;
- }
- }
- }
- while (next++); /* ++ needed to skip ':' */
-
- if (ret) {
- int keySz = 0;
- #ifndef NO_CERTS
- keySz = ctx->privateKeySz;
- #endif
- suites->setSuites = 1;
- suites->suiteSz = (word16)idx;
- InitSuitesHashSigAlgo(suites, haveECDSAsig, haveRSAsig, haveAnon, 1,
- keySz);
- }
-
- (void)ctx;
-
- return ret;
-}
-
-
-#if !defined(NO_WOLFSSL_SERVER) || !defined(NO_CERTS)
-int PickHashSigAlgo(WOLFSSL* ssl, const byte* hashSigAlgo, word32 hashSigAlgoSz)
-{
- word32 i;
- int ret = MATCH_SUITE_ERROR;
-
- ssl->suites->sigAlgo = ssl->specs.sig_algo;
-
- /* set defaults */
- if (IsAtLeastTLSv1_3(ssl->version)) {
- ssl->suites->hashAlgo = sha256_mac;
- #ifndef NO_CERTS
- ssl->suites->sigAlgo = ssl->buffers.keyType;
- #endif
- }
-#ifndef WOLFSSL_NO_TLS12
- else if (IsAtLeastTLSv1_2(ssl)) {
- #ifdef WOLFSSL_ALLOW_TLS_SHA1
- ssl->suites->hashAlgo = sha_mac;
- #else
- ssl->suites->hashAlgo = sha256_mac;
- #endif
- }
- else {
- ssl->suites->hashAlgo = sha_mac;
- }
-#endif
-
- if (hashSigAlgoSz == 0)
- return 0;
-
- /* i+1 since peek a byte ahead for type */
- for (i = 0; (i+1) < hashSigAlgoSz; i += HELLO_EXT_SIGALGO_SZ) {
- byte hashAlgo = 0, sigAlgo = 0;
-
- DecodeSigAlg(&hashSigAlgo[i], &hashAlgo, &sigAlgo);
- #ifdef HAVE_ED25519
- if (ssl->pkCurveOID == ECC_ED25519_OID) {
- if (sigAlgo != ed25519_sa_algo)
- continue;
- if (sigAlgo == ed25519_sa_algo &&
- ssl->suites->sigAlgo == ecc_dsa_sa_algo) {
- ssl->suites->sigAlgo = sigAlgo;
- ssl->suites->hashAlgo = sha512_mac;
- ret = 0;
- break;
- }
- }
- #endif
- #ifdef HAVE_ED448
- if (ssl->pkCurveOID == ECC_ED448_OID) {
- if (sigAlgo != ed448_sa_algo)
- continue;
-
- if (sigAlgo == ed448_sa_algo &&
- ssl->suites->sigAlgo == ecc_dsa_sa_algo) {
- ssl->suites->sigAlgo = sigAlgo;
- ssl->suites->hashAlgo = sha512_mac;
- ret = 0;
- break;
- }
- }
- #endif
- #if defined(WOLFSSL_TLS13) && defined(HAVE_ECC)
- if (IsAtLeastTLSv1_3(ssl->version) && sigAlgo == ssl->suites->sigAlgo &&
- sigAlgo == ecc_dsa_sa_algo) {
-
- int digestSz = GetMacDigestSize(hashAlgo);
- if (digestSz <= 0)
- continue;
-
- /* TLS 1.3 signature algorithms for ECDSA match hash length with
- * key size.
- */
- if (digestSz != ssl->buffers.keySz)
- continue;
-
- ssl->suites->hashAlgo = hashAlgo;
- ssl->suites->sigAlgo = sigAlgo;
- ret = 0;
- break; /* done selected sig/hash algorithms */
- }
- else
- #endif
- /* For ECDSA the `USE_ECDSA_KEYSZ_HASH_ALGO` build option will choose a hash
- * algorithm that matches the ephemeral ECDHE key size or the next highest
- * available. This workaround resolves issue with some peer's that do not
- * properly support scenarios such as a P-256 key hashed with SHA512.
- */
- #if defined(HAVE_ECC) && defined(USE_ECDSA_KEYSZ_HASH_ALGO)
- if (sigAlgo == ssl->suites->sigAlgo && sigAlgo == ecc_dsa_sa_algo) {
- int digestSz = GetMacDigestSize(hashAlgo);
- if (digestSz <= 0)
- continue;
-
- /* For ecc_dsa_sa_algo, pick hash algo that is curve size unless
- algorithm in not compiled in, then choose next highest */
- if (digestSz == ssl->eccTempKeySz) {
- ssl->suites->hashAlgo = hashAlgo;
- ssl->suites->sigAlgo = sigAlgo;
- #if defined(WOLFSSL_TLS13) || defined(HAVE_FFDHE)
- ssl->namedGroup = 0;
- #endif
- ret = 0;
- break; /* done selected sig/hash algorithms */
- }
- /* not strong enough, so keep checking hashSigAlso list */
- if (digestSz < ssl->eccTempKeySz)
- continue;
-
- /* mark as highest and check remainder of hashSigAlgo list */
- ssl->suites->hashAlgo = hashAlgo;
- ssl->suites->sigAlgo = sigAlgo;
- ret = 0;
- }
- else
- #endif
- #ifdef WC_RSA_PSS
- if (IsAtLeastTLSv1_3(ssl->version) &&
- ssl->suites->sigAlgo == rsa_sa_algo &&
- sigAlgo != rsa_pss_sa_algo) {
- continue;
- }
- else if (sigAlgo == ssl->suites->sigAlgo ||
- (sigAlgo == rsa_pss_sa_algo &&
- (ssl->suites->sigAlgo == rsa_sa_algo)))
- #else
- if (sigAlgo == ssl->suites->sigAlgo)
- #endif
- {
- /* pick highest available between both server and client */
- switch (hashAlgo) {
- case sha_mac:
- #ifdef WOLFSSL_SHA224
- case sha224_mac:
- #endif
- #ifndef NO_SHA256
- case sha256_mac:
- #endif
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- #endif
- #ifdef WOLFSSL_SHA512
- case sha512_mac:
- #endif
- /* not strong enough, so keep checking hashSigAlso list */
- if (hashAlgo < ssl->suites->hashAlgo) {
- ret = 0;
- continue;
- }
- /* mark as highest and check remainder of hashSigAlgo list */
- ssl->suites->hashAlgo = hashAlgo;
- ssl->suites->sigAlgo = sigAlgo;
- break;
- default:
- continue;
- }
- ret = 0;
- break;
- }
-#if defined(WOLFSSL_TLS13)
- else if (ssl->specs.sig_algo == 0 && IsAtLeastTLSv1_3(ssl->version)) {
- }
-#endif
- else if (ssl->specs.sig_algo == 0)
- {
- ssl->suites->hashAlgo = ssl->specs.mac_algorithm;
- ret = 0;
- }
- }
-
- return ret;
-}
-#endif /* !defined(NO_WOLFSSL_SERVER) || !defined(NO_CERTS) */
-
-#if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
-
- /* Initialize HandShakeInfo */
- void InitHandShakeInfo(HandShakeInfo* info, WOLFSSL* ssl)
- {
- int i;
-
- info->ssl = ssl;
- info->cipherName[0] = 0;
- for (i = 0; i < MAX_PACKETS_HANDSHAKE; i++)
- info->packetNames[i][0] = 0;
- info->numberPackets = 0;
- info->negotiationError = 0;
- }
-
- /* Set Final HandShakeInfo parameters */
- void FinishHandShakeInfo(HandShakeInfo* info)
- {
- int i;
- int sz = GetCipherNamesSize();
-
- for (i = 0; i < sz; i++)
- if (info->ssl->options.cipherSuite ==
- (byte)cipher_names[i].cipherSuite) {
- if (info->ssl->options.cipherSuite0 == ECC_BYTE)
- continue; /* ECC suites at end */
- XSTRNCPY(info->cipherName, cipher_names[i].name, MAX_CIPHERNAME_SZ);
- info->cipherName[MAX_CIPHERNAME_SZ] = '\0';
- break;
- }
-
- /* error max and min are negative numbers */
- if (info->ssl->error <= MIN_PARAM_ERR && info->ssl->error >= MAX_PARAM_ERR)
- info->negotiationError = info->ssl->error;
- }
-
-
- /* Add name to info packet names, increase packet name count */
- void AddPacketName(WOLFSSL* ssl, const char* name)
- {
- #ifdef WOLFSSL_CALLBACKS
- HandShakeInfo* info = &ssl->handShakeInfo;
- if (info->numberPackets < MAX_PACKETS_HANDSHAKE) {
- char* packetName = info->packetNames[info->numberPackets];
- XSTRNCPY(packetName, name, MAX_PACKETNAME_SZ);
- packetName[MAX_PACKETNAME_SZ] = '\0';
- info->numberPackets++;
- }
- #endif
- (void)ssl;
- (void)name;
- }
-
-
- #ifdef WOLFSSL_CALLBACKS
- /* Initialize TimeoutInfo */
- void InitTimeoutInfo(TimeoutInfo* info)
- {
- int i;
-
- info->timeoutName[0] = 0;
- info->flags = 0;
-
- for (i = 0; i < MAX_PACKETS_HANDSHAKE; i++) {
- info->packets[i].packetName[0] = 0;
- info->packets[i].timestamp.tv_sec = 0;
- info->packets[i].timestamp.tv_usec = 0;
- info->packets[i].bufferValue = 0;
- info->packets[i].valueSz = 0;
- }
- info->numberPackets = 0;
- info->timeoutValue.tv_sec = 0;
- info->timeoutValue.tv_usec = 0;
- }
-
-
- /* Free TimeoutInfo */
- void FreeTimeoutInfo(TimeoutInfo* info, void* heap)
- {
- int i;
- (void)heap;
- for (i = 0; i < MAX_PACKETS_HANDSHAKE; i++)
- if (info->packets[i].bufferValue) {
- XFREE(info->packets[i].bufferValue, heap, DYNAMIC_TYPE_INFO);
- info->packets[i].bufferValue = 0;
- }
-
- }
-
- /* Add packet name to previously added packet info */
- void AddLateName(const char* name, TimeoutInfo* info)
- {
- /* make sure we have a valid previous one */
- if (info->numberPackets > 0 && info->numberPackets <
- MAX_PACKETS_HANDSHAKE) {
- char* packetName = info->packets[info->numberPackets-1].packetName;
- XSTRNCPY(packetName, name, MAX_PACKETNAME_SZ);
- packetName[MAX_PACKETNAME_SZ] = '\0';
- }
- }
-
- /* Add record header to previously added packet info */
- void AddLateRecordHeader(const RecordLayerHeader* rl, TimeoutInfo* info)
- {
- /* make sure we have a valid previous one */
- if (info->numberPackets > 0 && info->numberPackets <
- MAX_PACKETS_HANDSHAKE) {
- if (info->packets[info->numberPackets - 1].bufferValue)
- XMEMCPY(info->packets[info->numberPackets - 1].bufferValue, rl,
- RECORD_HEADER_SZ);
- else
- XMEMCPY(info->packets[info->numberPackets - 1].value, rl,
- RECORD_HEADER_SZ);
- }
- }
-
- #endif /* WOLFSSL_CALLBACKS */
-
-
- /* Add PacketInfo to TimeoutInfo
- *
- * ssl WOLFSSL structure sending or receiving packet
- * name name of packet being sent
- * type type of packet being sent
- * data data bing sent with packet
- * sz size of data buffer
- * written 1 if this packet is being written to wire, 0 if being read
- * heap custom heap to use for mallocs/frees
- */
- void AddPacketInfo(WOLFSSL* ssl, const char* name, int type,
- const byte* data, int sz, int written, void* heap)
- {
- #ifdef WOLFSSL_CALLBACKS
- TimeoutInfo* info = &ssl->timeoutInfo;
-
- if (info->numberPackets < (MAX_PACKETS_HANDSHAKE - 1)) {
- WOLFSSL_TIMEVAL currTime;
-
- /* may add name after */
- if (name) {
- char* packetName = info->packets[info->numberPackets].packetName;
- XSTRNCPY(packetName, name, MAX_PACKETNAME_SZ);
- packetName[MAX_PACKETNAME_SZ] = '\0';
- }
-
- /* add data, put in buffer if bigger than static buffer */
- info->packets[info->numberPackets].valueSz = sz;
- if (sz < MAX_VALUE_SZ)
- XMEMCPY(info->packets[info->numberPackets].value, data, sz);
- else {
- info->packets[info->numberPackets].bufferValue =
- (byte*)XMALLOC(sz, heap, DYNAMIC_TYPE_INFO);
- if (!info->packets[info->numberPackets].bufferValue)
- /* let next alloc catch, just don't fill, not fatal here */
- info->packets[info->numberPackets].valueSz = 0;
- else
- XMEMCPY(info->packets[info->numberPackets].bufferValue,
- data, sz);
- }
- gettimeofday(&currTime, 0);
- info->packets[info->numberPackets].timestamp.tv_sec =
- currTime.tv_sec;
- info->packets[info->numberPackets].timestamp.tv_usec =
- currTime.tv_usec;
- info->numberPackets++;
- }
- #endif /* WOLFSSL_CALLBACKS */
- #ifdef OPENSSL_EXTRA
- if (ssl->protoMsgCb != NULL && sz > RECORD_HEADER_SZ) {
- /* version from hex to dec 16 is 16^1, 256 from 16^2 and
- 4096 from 16^3 */
- int version = (ssl->version.minor & 0X0F) +
- (ssl->version.minor & 0xF0) * 16 +
- (ssl->version.major & 0X0F) * 256 +
- (ssl->version.major & 0xF0) * 4096;
-
- ssl->protoMsgCb(written, version, type,
- (const void *)(data + RECORD_HEADER_SZ),
- (size_t)(sz - RECORD_HEADER_SZ),
- ssl, ssl->protoMsgCtx);
- }
- #endif /* OPENSSL_EXTRA */
- (void)written;
- (void)name;
- (void)heap;
- (void)type;
- (void)ssl;
- }
-
-#endif /* WOLFSSL_CALLBACKS */
-
-#if !defined(NO_CERTS)
-
-/* Decode the private key - RSA/ECC/Ed25519/Ed448 - and creates a key object.
- * The signature type is set as well.
- * The maximum length of a signature is returned.
- *
- * ssl The SSL/TLS object.
- * length The length of a signature.
- * returns 0 on success, otherwise failure.
- */
-int DecodePrivateKey(WOLFSSL *ssl, word16* length)
-{
- int ret = BAD_FUNC_ARG;
- int keySz;
- word32 idx;
-
-#ifdef HAVE_PK_CALLBACKS
- /* allow no private key if using PK callbacks and CB is set */
- if (wolfSSL_IsPrivatePkSet(ssl)) {
- *length = GetPrivateKeySigSize(ssl);
- return 0;
- }
- else
-#endif
-
- /* make sure private key exists */
- if (ssl->buffers.key == NULL || ssl->buffers.key->buffer == NULL) {
- WOLFSSL_MSG("Private key missing!");
- ERROR_OUT(NO_PRIVATE_KEY, exit_dpk);
- }
-
-#ifdef HAVE_PKCS11
- if (ssl->buffers.keyDevId != INVALID_DEVID && ssl->buffers.keyId) {
- if (ssl->buffers.keyType == rsa_sa_algo)
- ssl->hsType = DYNAMIC_TYPE_RSA;
- else if (ssl->buffers.keyType == ecc_dsa_sa_algo)
- ssl->hsType = DYNAMIC_TYPE_ECC;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_dpk;
- }
-
- if (ssl->buffers.keyType == rsa_sa_algo) {
- #ifndef NO_RSA
- ret = wc_InitRsaKey_Id((RsaKey*)ssl->hsKey,
- ssl->buffers.key->buffer, ssl->buffers.key->length,
- ssl->heap, ssl->buffers.keyDevId);
- if (ret == 0) {
- if (ssl->buffers.keySz < ssl->options.minRsaKeySz) {
- WOLFSSL_MSG("RSA key size too small");
- ERROR_OUT(RSA_KEY_SIZE_E, exit_dpk);
- }
-
- /* Return the maximum signature length. */
- *length = (word16)ssl->buffers.keySz;
- }
- #else
- ret = NOT_COMPILED_IN;
- #endif
- }
- else if (ssl->buffers.keyType == ecc_dsa_sa_algo) {
- #ifdef HAVE_ECC
- ret = wc_ecc_init_id((ecc_key*)ssl->hsKey, ssl->buffers.key->buffer,
- ssl->buffers.key->length, ssl->heap,
- ssl->buffers.keyDevId);
- if (ret == 0) {
- if (ssl->buffers.keySz < ssl->options.minEccKeySz) {
- WOLFSSL_MSG("ECC key size too small");
- ERROR_OUT(ECC_KEY_SIZE_E, exit_dpk);
- }
-
- /* Return the maximum signature length. */
- *length = (word16)wc_ecc_sig_size_calc(ssl->buffers.keySz);
- }
- #else
- ret = NOT_COMPILED_IN;
- #endif
- }
- goto exit_dpk;
- }
-#endif
-
-#ifndef NO_RSA
- if (ssl->buffers.keyType == rsa_sa_algo || ssl->buffers.keyType == 0) {
- ssl->hsType = DYNAMIC_TYPE_RSA;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_dpk;
- }
-
- WOLFSSL_MSG("Trying RSA private key");
-
- /* Set start of data to beginning of buffer. */
- idx = 0;
- /* Decode the key assuming it is an RSA private key. */
- ret = wc_RsaPrivateKeyDecode(ssl->buffers.key->buffer, &idx,
- (RsaKey*)ssl->hsKey, ssl->buffers.key->length);
- if (ret == 0) {
- WOLFSSL_MSG("Using RSA private key");
-
- /* It worked so check it meets minimum key size requirements. */
- keySz = wc_RsaEncryptSize((RsaKey*)ssl->hsKey);
- if (keySz < 0) { /* check if keySz has error case */
- ERROR_OUT(keySz, exit_dpk);
- }
-
- if (keySz < ssl->options.minRsaKeySz) {
- WOLFSSL_MSG("RSA key size too small");
- ERROR_OUT(RSA_KEY_SIZE_E, exit_dpk);
- }
-
- /* Return the maximum signature length. */
- *length = (word16)keySz;
-
- goto exit_dpk;
- }
- }
-#endif /* !NO_RSA */
-
-#ifdef HAVE_ECC
-#ifndef NO_RSA
- FreeKey(ssl, ssl->hsType, (void**)&ssl->hsKey);
-#endif /* !NO_RSA */
-
- if (ssl->buffers.keyType == ecc_dsa_sa_algo || ssl->buffers.keyType == 0) {
- ssl->hsType = DYNAMIC_TYPE_ECC;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_dpk;
- }
-
- #ifndef NO_RSA
- WOLFSSL_MSG("Trying ECC private key, RSA didn't work");
- #else
- WOLFSSL_MSG("Trying ECC private key");
- #endif
-
- /* Set start of data to beginning of buffer. */
- idx = 0;
- /* Decode the key assuming it is an ECC private key. */
- ret = wc_EccPrivateKeyDecode(ssl->buffers.key->buffer, &idx,
- (ecc_key*)ssl->hsKey,
- ssl->buffers.key->length);
- if (ret == 0) {
- WOLFSSL_MSG("Using ECC private key");
-
- /* Check it meets the minimum ECC key size requirements. */
- keySz = wc_ecc_size((ecc_key*)ssl->hsKey);
- if (keySz < ssl->options.minEccKeySz) {
- WOLFSSL_MSG("ECC key size too small");
- ERROR_OUT(ECC_KEY_SIZE_E, exit_dpk);
- }
-
- /* Return the maximum signature length. */
- *length = (word16)wc_ecc_sig_size((ecc_key*)ssl->hsKey);
-
- goto exit_dpk;
- }
- }
-#endif
-#ifdef HAVE_ED25519
- #if !defined(NO_RSA) || defined(HAVE_ECC)
- FreeKey(ssl, ssl->hsType, (void**)&ssl->hsKey);
- #endif
-
- if (ssl->buffers.keyType == ed25519_sa_algo || ssl->buffers.keyType == 0) {
- ssl->hsType = DYNAMIC_TYPE_ED25519;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_dpk;
- }
-
- #ifdef HAVE_ECC
- WOLFSSL_MSG("Trying ED25519 private key, ECC didn't work");
- #elif !defined(NO_RSA)
- WOLFSSL_MSG("Trying ED25519 private key, RSA didn't work");
- #else
- WOLFSSL_MSG("Trying ED25519 private key");
- #endif
-
- /* Set start of data to beginning of buffer. */
- idx = 0;
- /* Decode the key assuming it is an ED25519 private key. */
- ret = wc_Ed25519PrivateKeyDecode(ssl->buffers.key->buffer, &idx,
- (ed25519_key*)ssl->hsKey,
- ssl->buffers.key->length);
- if (ret == 0) {
- WOLFSSL_MSG("Using ED25519 private key");
-
- /* Check it meets the minimum ECC key size requirements. */
- if (ED25519_KEY_SIZE < ssl->options.minEccKeySz) {
- WOLFSSL_MSG("ED25519 key size too small");
- ERROR_OUT(ECC_KEY_SIZE_E, exit_dpk);
- }
-
- /* Return the maximum signature length. */
- *length = ED25519_SIG_SIZE;
-
- goto exit_dpk;
- }
- }
-#endif /* HAVE_ED25519 */
-#ifdef HAVE_ED448
- #if !defined(NO_RSA) || defined(HAVE_ECC)
- FreeKey(ssl, ssl->hsType, (void**)&ssl->hsKey);
- #endif
-
- if (ssl->buffers.keyType == ed448_sa_algo || ssl->buffers.keyType == 0) {
- ssl->hsType = DYNAMIC_TYPE_ED448;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_dpk;
- }
-
- #ifdef HAVE_ED25519
- WOLFSSL_MSG("Trying ED448 private key, ED25519 didn't work");
- #elif defined(HAVE_ECC)
- WOLFSSL_MSG("Trying ED448 private key, ECC didn't work");
- #elif !defined(NO_RSA)
- WOLFSSL_MSG("Trying ED448 private key, RSA didn't work");
- #else
- WOLFSSL_MSG("Trying ED447 private key");
- #endif
-
- /* Set start of data to beginning of buffer. */
- idx = 0;
- /* Decode the key assuming it is an ED448 private key. */
- ret = wc_Ed448PrivateKeyDecode(ssl->buffers.key->buffer, &idx,
- (ed448_key*)ssl->hsKey,
- ssl->buffers.key->length);
- if (ret == 0) {
- WOLFSSL_MSG("Using ED448 private key");
-
- /* Check it meets the minimum ECC key size requirements. */
- if (ED448_KEY_SIZE < ssl->options.minEccKeySz) {
- WOLFSSL_MSG("ED448 key size too small");
- ERROR_OUT(ECC_KEY_SIZE_E, exit_dpk);
- }
-
- /* Return the maximum signature length. */
- *length = ED448_SIG_SIZE;
-
- goto exit_dpk;
- }
- }
-#endif /* HAVE_ED448 */
-
- (void)idx;
- (void)keySz;
- (void)length;
-exit_dpk:
- return ret;
-}
-
-#endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
-
-/* client only parts */
-#ifndef NO_WOLFSSL_CLIENT
-
-#ifndef WOLFSSL_NO_TLS12
-
- /* handle generation of client_hello (1) */
- int SendClientHello(WOLFSSL* ssl)
- {
- byte *output;
- word32 length, idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- int sendSz;
- int idSz = ssl->options.resuming
- ? ssl->session.sessionIDSz
- : 0;
- int ret;
- word16 extSz = 0;
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version))
- return SendTls13ClientHello(ssl);
-#endif
-
- WOLFSSL_START(WC_FUNC_CLIENT_HELLO_SEND);
- WOLFSSL_ENTER("SendClientHello");
-
- if (ssl->suites == NULL) {
- WOLFSSL_MSG("Bad suites pointer in SendClientHello");
- return SUITES_ERROR;
- }
-
-#ifdef HAVE_SESSION_TICKET
- if (ssl->options.resuming && ssl->session.ticketLen > 0) {
- SessionTicket* ticket;
-
- ticket = TLSX_SessionTicket_Create(0, ssl->session.ticket,
- ssl->session.ticketLen, ssl->heap);
- if (ticket == NULL) return MEMORY_E;
-
- ret = TLSX_UseSessionTicket(&ssl->extensions, ticket, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) {
- TLSX_SessionTicket_Free(ticket, ssl->heap);
- return ret;
- }
-
- idSz = 0;
- }
-#endif
- length = VERSION_SZ + RAN_LEN
- + idSz + ENUM_LEN
- + ssl->suites->suiteSz + SUITE_LEN
- + COMP_LEN + ENUM_LEN;
-
-#ifdef HAVE_TLS_EXTENSIONS
- /* auto populate extensions supported unless user defined */
- if ((ret = TLSX_PopulateExtensions(ssl, 0)) != 0)
- return ret;
- #ifdef HAVE_QSH
- if (QSH_Init(ssl) != 0)
- return MEMORY_E;
- #endif
- extSz = 0;
- ret = TLSX_GetRequestSize(ssl, client_hello, &extSz);
- if (ret != 0)
- return ret;
- length += extSz;
-#else
- if (IsAtLeastTLSv1_2(ssl) && ssl->suites->hashSigAlgoSz)
- extSz += HELLO_EXT_SZ + HELLO_EXT_SIGALGO_SZ
- + ssl->suites->hashSigAlgoSz;
-#ifdef HAVE_EXTENDED_MASTER
- if (ssl->options.haveEMS)
- extSz += HELLO_EXT_SZ;
-#endif
- if (extSz != 0)
- length += extSz + HELLO_EXT_SZ_SZ;
-#endif
- sendSz = length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ;
-
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- length += ENUM_LEN; /* cookie */
- if (ssl->arrays->cookieSz != 0) length += ssl->arrays->cookieSz;
- sendSz = length + DTLS_HANDSHAKE_HEADER_SZ + DTLS_RECORD_HEADER_SZ;
- idx += DTLS_HANDSHAKE_EXTRA + DTLS_RECORD_EXTRA;
- }
-#endif
-
- if (IsEncryptionOn(ssl, 1))
- sendSz += MAX_MSG_EXTRA;
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHeaders(output, length, client_hello, ssl);
-
- /* client hello, first version */
- output[idx++] = ssl->version.major;
- output[idx++] = ssl->version.minor;
- ssl->chVersion = ssl->version; /* store in case changed */
-
- /* then random */
- if (ssl->options.connectState == CONNECT_BEGIN) {
- ret = wc_RNG_GenerateBlock(ssl->rng, output + idx, RAN_LEN);
- if (ret != 0)
- return ret;
-
- /* store random */
- XMEMCPY(ssl->arrays->clientRandom, output + idx, RAN_LEN);
- } else {
-#ifdef WOLFSSL_DTLS
- /* send same random on hello again */
- XMEMCPY(output + idx, ssl->arrays->clientRandom, RAN_LEN);
-#endif
- }
- idx += RAN_LEN;
-
- /* then session id */
- output[idx++] = (byte)idSz;
- if (idSz) {
- XMEMCPY(output + idx, ssl->session.sessionID,
- ssl->session.sessionIDSz);
- idx += ssl->session.sessionIDSz;
- }
-
- /* then DTLS cookie */
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- byte cookieSz = ssl->arrays->cookieSz;
-
- output[idx++] = cookieSz;
- if (cookieSz) {
- XMEMCPY(&output[idx], ssl->arrays->cookie, cookieSz);
- idx += cookieSz;
- }
- }
-#endif
- /* then cipher suites */
- c16toa(ssl->suites->suiteSz, output + idx);
- idx += OPAQUE16_LEN;
- XMEMCPY(output + idx, &ssl->suites->suites, ssl->suites->suiteSz);
- idx += ssl->suites->suiteSz;
-
- /* last, compression */
- output[idx++] = COMP_LEN;
- if (ssl->options.usingCompression)
- output[idx++] = ZLIB_COMPRESSION;
- else
- output[idx++] = NO_COMPRESSION;
-
-#ifdef HAVE_TLS_EXTENSIONS
- extSz = 0;
- ret = TLSX_WriteRequest(ssl, output + idx, client_hello, &extSz);
- if (ret != 0)
- return ret;
- idx += extSz;
-
- (void)idx; /* suppress analyzer warning, keep idx current */
-#else
- if (extSz != 0) {
- c16toa(extSz, output + idx);
- idx += HELLO_EXT_SZ_SZ;
-
- if (IsAtLeastTLSv1_2(ssl)) {
- if (ssl->suites->hashSigAlgoSz) {
- word16 i;
- /* extension type */
- c16toa(HELLO_EXT_SIG_ALGO, output + idx);
- idx += HELLO_EXT_TYPE_SZ;
- /* extension data length */
- c16toa(HELLO_EXT_SIGALGO_SZ + ssl->suites->hashSigAlgoSz,
- output + idx);
- idx += HELLO_EXT_SZ_SZ;
- /* sig algos length */
- c16toa(ssl->suites->hashSigAlgoSz, output + idx);
- idx += HELLO_EXT_SIGALGO_SZ;
- for (i=0; i < ssl->suites->hashSigAlgoSz; i++, idx++) {
- output[idx] = ssl->suites->hashSigAlgo[i];
- }
- }
- }
-#ifdef HAVE_EXTENDED_MASTER
- if (ssl->options.haveEMS) {
- c16toa(HELLO_EXT_EXTMS, output + idx);
- idx += HELLO_EXT_TYPE_SZ;
- c16toa(0, output + idx);
- idx += HELLO_EXT_SZ_SZ;
- }
-#endif
- }
-#endif
-
- if (IsEncryptionOn(ssl, 1)) {
- byte* input;
- int inputSz = idx - RECORD_HEADER_SZ; /* build msg adds rec hdr */
-
- input = (byte*)XMALLOC(inputSz, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- if (input == NULL)
- return MEMORY_E;
-
- XMEMCPY(input, output + RECORD_HEADER_SZ, inputSz);
- sendSz = BuildMessage(ssl, output, sendSz, input, inputSz,
- handshake, 1, 0, 0);
- XFREE(input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
-
- if (sendSz < 0)
- return sendSz;
- } else {
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- if ((ret = DtlsMsgPoolSave(ssl, output, sendSz)) != 0)
- return ret;
- }
- if (ssl->options.dtls)
- DtlsSEQIncrement(ssl, CUR_ORDER);
- #endif
- ret = HashOutput(ssl, output, sendSz, 0);
- if (ret != 0)
- return ret;
- }
-
- ssl->options.clientState = CLIENT_HELLO_COMPLETE;
-#ifdef OPENSSL_EXTRA
- ssl->cbmode = SSL_CB_MODE_WRITE;
- if (ssl->CBIS != NULL)
- ssl->CBIS(ssl, SSL_CB_CONNECT_LOOP, SSL_SUCCESS);
-#endif
-
-#if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "ClientHello", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
-#endif
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendClientHello", ret);
- WOLFSSL_END(WC_FUNC_CLIENT_HELLO_SEND);
-
- return ret;
- }
-
-
- /* handle processing of DTLS hello_verify_request (3) */
- static int DoHelloVerifyRequest(WOLFSSL* ssl, const byte* input,
- word32* inOutIdx, word32 size)
- {
- ProtocolVersion pv;
- byte cookieSz;
- word32 begin = *inOutIdx;
-
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "HelloVerifyRequest");
- if (ssl->toInfoOn) AddLateName("HelloVerifyRequest", &ssl->timeoutInfo);
-#endif
-
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- DtlsMsgPoolReset(ssl);
- }
-#endif
-
- if (OPAQUE16_LEN + OPAQUE8_LEN > size)
- return BUFFER_ERROR;
-
- XMEMCPY(&pv, input + *inOutIdx, OPAQUE16_LEN);
- *inOutIdx += OPAQUE16_LEN;
-
- if (pv.major != DTLS_MAJOR ||
- (pv.minor != DTLS_MINOR && pv.minor != DTLSv1_2_MINOR))
- return VERSION_ERROR;
-
- cookieSz = input[(*inOutIdx)++];
-
- if (cookieSz) {
- if ((*inOutIdx - begin) + cookieSz > size)
- return BUFFER_ERROR;
-
-#ifdef WOLFSSL_DTLS
- if (cookieSz <= MAX_COOKIE_LEN) {
- XMEMCPY(ssl->arrays->cookie, input + *inOutIdx, cookieSz);
- ssl->arrays->cookieSz = cookieSz;
- }
-#endif
- *inOutIdx += cookieSz;
- }
-
- ssl->options.serverState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
- return 0;
- }
-
-
- static WC_INLINE int DSH_CheckSessionId(WOLFSSL* ssl)
- {
- int ret = 0;
-
-#ifdef HAVE_SECRET_CALLBACK
- /* If a session secret callback exists, we are using that
- * key instead of the saved session key. */
- ret = ret || (ssl->sessionSecretCb != NULL);
-#endif
-
-#ifdef HAVE_SESSION_TICKET
- /* server may send blank ticket which may not be expected to indicate
- * existing one ok but will also be sending a new one */
- ret = ret || (ssl->session.ticketLen > 0);
-#endif
-
- ret = ret ||
- (ssl->options.haveSessionId && XMEMCMP(ssl->arrays->sessionID,
- ssl->session.sessionID, ID_LEN) == 0);
-
- return ret;
- }
-
- /* Check the version in the received message is valid and set protocol
- * version to use.
- *
- * ssl The SSL/TLS object.
- * pv The protocol version from the packet.
- * returns 0 on success, otherwise failure.
- */
- int CheckVersion(WOLFSSL *ssl, ProtocolVersion pv)
- {
- #ifdef WOLFSSL_TLS13_DRAFT
- if (pv.major == TLS_DRAFT_MAJOR) {
- pv.major = SSLv3_MAJOR;
- pv.minor = TLSv1_3_MINOR;
- }
- #endif
-
- #ifdef OPENSSL_EXTRA
- if (ssl->CBIS != NULL) {
- ssl->CBIS(ssl, SSL_CB_HANDSHAKE_START, SSL_SUCCESS);
- }
- #endif
-
- if (pv.minor > ssl->version.minor) {
- WOLFSSL_MSG("Server using higher version, fatal error");
- return VERSION_ERROR;
- }
- if (pv.minor < ssl->version.minor) {
- WOLFSSL_MSG("server using lower version");
-
- /* Check for downgrade attack. */
- if (!ssl->options.downgrade) {
- WOLFSSL_MSG("\tno downgrade allowed, fatal error");
- return VERSION_ERROR;
- }
- if (pv.minor < ssl->options.minDowngrade) {
- WOLFSSL_MSG("\tversion below minimum allowed, fatal error");
- return VERSION_ERROR;
- }
-
- #ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation &&
- ssl->secure_renegotiation->enabled &&
- ssl->options.handShakeDone) {
- WOLFSSL_MSG("Server changed version during scr");
- return VERSION_ERROR;
- }
- #endif
-
- /* Checks made - OK to downgrade. */
- if (pv.minor == SSLv3_MINOR) {
- /* turn off tls */
- WOLFSSL_MSG("\tdowngrading to SSLv3");
- ssl->options.tls = 0;
- ssl->options.tls1_1 = 0;
- ssl->version.minor = SSLv3_MINOR;
- }
- else if (pv.minor == TLSv1_MINOR) {
- /* turn off tls 1.1+ */
- WOLFSSL_MSG("\tdowngrading to TLSv1");
- ssl->options.tls1_1 = 0;
- ssl->version.minor = TLSv1_MINOR;
- }
- else if (pv.minor == TLSv1_1_MINOR) {
- WOLFSSL_MSG("\tdowngrading to TLSv1.1");
- ssl->version.minor = TLSv1_1_MINOR;
- }
- else if (pv.minor == TLSv1_2_MINOR) {
- WOLFSSL_MSG(" downgrading to TLSv1.2");
- ssl->version.minor = TLSv1_2_MINOR;
- }
- }
-
-#ifdef OPENSSL_EXTRA
- /* check if option is set to not allow the current version
- * set from either wolfSSL_set_options or wolfSSL_CTX_set_options */
- if (!ssl->options.dtls && ssl->options.downgrade &&
- ssl->options.mask > 0) {
- if (ssl->version.minor == TLSv1_2_MINOR &&
- (ssl->options.mask & SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2) {
- WOLFSSL_MSG("\tOption set to not allow TLSv1.2, Downgrading");
- ssl->version.minor = TLSv1_1_MINOR;
- }
- if (ssl->version.minor == TLSv1_1_MINOR &&
- (ssl->options.mask & SSL_OP_NO_TLSv1_1) == SSL_OP_NO_TLSv1_1) {
- WOLFSSL_MSG("\tOption set to not allow TLSv1.1, Downgrading");
- ssl->options.tls1_1 = 0;
- ssl->version.minor = TLSv1_MINOR;
- }
- if (ssl->version.minor == TLSv1_MINOR &&
- (ssl->options.mask & SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1) {
- WOLFSSL_MSG("\tOption set to not allow TLSv1, Downgrading");
- ssl->options.tls = 0;
- ssl->options.tls1_1 = 0;
- ssl->version.minor = SSLv3_MINOR;
- }
- if (ssl->version.minor == SSLv3_MINOR &&
- (ssl->options.mask & SSL_OP_NO_SSLv3) == SSL_OP_NO_SSLv3) {
- WOLFSSL_MSG("\tError, option set to not allow SSLv3");
- return VERSION_ERROR;
- }
-
- if (ssl->version.minor < ssl->options.minDowngrade) {
- WOLFSSL_MSG("\tversion below minimum allowed, fatal error");
- return VERSION_ERROR;
- }
- }
-#endif
-
- return 0;
- }
-
- /* handle processing of server_hello (2) */
- int DoServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- word32 helloSz)
- {
- byte cs0; /* cipher suite bytes 0, 1 */
- byte cs1;
- ProtocolVersion pv;
- byte compression;
- word32 i = *inOutIdx;
- word32 begin = i;
- int ret;
-
- WOLFSSL_START(WC_FUNC_SERVER_HELLO_DO);
- WOLFSSL_ENTER("DoServerHello");
-
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "ServerHello");
- if (ssl->toInfoOn) AddLateName("ServerHello", &ssl->timeoutInfo);
-#endif
-
- /* protocol version, random and session id length check */
- if (OPAQUE16_LEN + RAN_LEN + OPAQUE8_LEN > helloSz)
- return BUFFER_ERROR;
-
- /* protocol version */
- XMEMCPY(&pv, input + i, OPAQUE16_LEN);
- i += OPAQUE16_LEN;
-
- ret = CheckVersion(ssl, pv);
- if (ret != 0)
- return ret;
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(pv)) {
- byte type = server_hello;
- return DoTls13ServerHello(ssl, input, inOutIdx, helloSz, &type);
- }
-#endif
-
- /* random */
- XMEMCPY(ssl->arrays->serverRandom, input + i, RAN_LEN);
- i += RAN_LEN;
-
- /* session id */
- ssl->arrays->sessionIDSz = input[i++];
-
- if (ssl->arrays->sessionIDSz > ID_LEN) {
- WOLFSSL_MSG("Invalid session ID size");
- ssl->arrays->sessionIDSz = 0;
- return BUFFER_ERROR;
- }
- else if (ssl->arrays->sessionIDSz) {
- if ((i - begin) + ssl->arrays->sessionIDSz > helloSz)
- return BUFFER_ERROR;
-
- XMEMCPY(ssl->arrays->sessionID, input + i,
- ssl->arrays->sessionIDSz);
- i += ssl->arrays->sessionIDSz;
- ssl->options.haveSessionId = 1;
- }
-
-
- /* suite and compression */
- if ((i - begin) + OPAQUE16_LEN + OPAQUE8_LEN > helloSz)
- return BUFFER_ERROR;
-
- cs0 = input[i++];
- cs1 = input[i++];
-
-#ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation && ssl->secure_renegotiation->enabled &&
- ssl->options.handShakeDone) {
- if (ssl->options.cipherSuite0 != cs0 ||
- ssl->options.cipherSuite != cs1) {
- WOLFSSL_MSG("Server changed cipher suite during scr");
- return MATCH_SUITE_ERROR;
- }
- }
-#endif
-
- ssl->options.cipherSuite0 = cs0;
- ssl->options.cipherSuite = cs1;
- compression = input[i++];
-
-#ifndef WOLFSSL_NO_STRICT_CIPHER_SUITE
- {
- word32 idx, found = 0;
- /* confirm server_hello cipher suite is one sent in client_hello */
- for (idx = 0; idx < ssl->suites->suiteSz; idx += 2) {
- if (ssl->suites->suites[idx] == cs0 &&
- ssl->suites->suites[idx+1] == cs1) {
- found = 1;
- break;
- }
- }
- if (!found) {
- WOLFSSL_MSG("ServerHello did not use cipher suite from ClientHello");
- return MATCH_SUITE_ERROR;
- }
- }
-#endif /* !WOLFSSL_NO_STRICT_CIPHER_SUITE */
-
- if (compression != NO_COMPRESSION && !ssl->options.usingCompression) {
- WOLFSSL_MSG("Server forcing compression w/o support");
- return COMPRESSION_ERROR;
- }
-
- if (compression != ZLIB_COMPRESSION && ssl->options.usingCompression) {
- WOLFSSL_MSG("Server refused compression, turning off");
- ssl->options.usingCompression = 0; /* turn off if server refused */
- }
-
- *inOutIdx = i;
-
-#ifdef HAVE_TLS_EXTENSIONS
- if ( (i - begin) < helloSz) {
- if (TLSX_SupportExtensions(ssl)) {
- word16 totalExtSz;
-
- if ((i - begin) + OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
-
- ato16(&input[i], &totalExtSz);
- i += OPAQUE16_LEN;
-
- if ((i - begin) + totalExtSz > helloSz)
- return BUFFER_ERROR;
-
- if ((ret = TLSX_Parse(ssl, (byte *) input + i, totalExtSz,
- server_hello, NULL)))
- return ret;
-
- i += totalExtSz;
- *inOutIdx = i;
- }
- else
- *inOutIdx = begin + helloSz; /* skip extensions */
- }
- else
- ssl->options.haveEMS = 0; /* If no extensions, no EMS */
-#else
- {
- int allowExt = 0;
- byte pendingEMS = 0;
-
- if ( (i - begin) < helloSz) {
- if (ssl->version.major == SSLv3_MAJOR &&
- ssl->version.minor >= TLSv1_MINOR) {
-
- allowExt = 1;
- }
-#ifdef WOLFSSL_DTLS
- if (ssl->version.major == DTLS_MAJOR)
- allowExt = 1;
-#endif
-
- if (allowExt) {
- word16 totalExtSz;
-
- if ((i - begin) + OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
-
- ato16(&input[i], &totalExtSz);
- i += OPAQUE16_LEN;
-
- if ((i - begin) + totalExtSz > helloSz)
- return BUFFER_ERROR;
-
- while (totalExtSz) {
- word16 extId, extSz;
-
- if (OPAQUE16_LEN + OPAQUE16_LEN > totalExtSz)
- return BUFFER_ERROR;
-
- ato16(&input[i], &extId);
- i += OPAQUE16_LEN;
- ato16(&input[i], &extSz);
- i += OPAQUE16_LEN;
-
- if (OPAQUE16_LEN + OPAQUE16_LEN + extSz > totalExtSz)
- return BUFFER_ERROR;
-
- if (extId == HELLO_EXT_EXTMS)
- pendingEMS = 1;
- else
- i += extSz;
-
- totalExtSz -= OPAQUE16_LEN + OPAQUE16_LEN + extSz;
- }
-
- *inOutIdx = i;
- }
- else
- *inOutIdx = begin + helloSz; /* skip extensions */
- }
-
- if (!pendingEMS && ssl->options.haveEMS)
- ssl->options.haveEMS = 0;
- }
-#endif
-
- ssl->options.serverState = SERVER_HELLO_COMPLETE;
-
- if (IsEncryptionOn(ssl, 0)) {
- *inOutIdx += ssl->keys.padSz;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMWrite &&
- ssl->specs.cipher_type == block) {
- *inOutIdx += MacSize(ssl);
- }
- #endif
- }
-
-#ifdef HAVE_SECRET_CALLBACK
- if (ssl->sessionSecretCb != NULL) {
- int secretSz = SECRET_LEN;
- ret = ssl->sessionSecretCb(ssl, ssl->session.masterSecret,
- &secretSz, ssl->sessionSecretCtx);
- if (ret != 0 || secretSz != SECRET_LEN)
- return SESSION_SECRET_CB_E;
- }
-#endif /* HAVE_SECRET_CALLBACK */
-
- ret = CompleteServerHello(ssl);
-
- WOLFSSL_LEAVE("DoServerHello", ret);
- WOLFSSL_END(WC_FUNC_SERVER_HELLO_DO);
-
- return ret;
- }
-
-#ifdef WOLFSSL_TLS13
- /* returns 1 if able to do TLS 1.3 otherwise 0 */
- static int TLSv1_3_Capable(WOLFSSL* ssl)
- {
- #ifndef WOLFSSL_TLS13
- return 0;
- #else
- int ret = 0;
-
- if (IsAtLeastTLSv1_3(ssl->ctx->method->version)) {
- ret = 1;
- }
-
- #ifdef OPENSSL_EXTRA
- if ((wolfSSL_get_options(ssl) & SSL_OP_NO_TLSv1_3)) {
- /* option set at run time to disable TLS 1.3 */
- ret = 0;
- }
- #endif
- return ret;
- #endif
- }
-#endif /* WOLFSSL_TLS13 */
-
- int CompleteServerHello(WOLFSSL* ssl)
- {
- int ret;
-
- if (!ssl->options.resuming) {
- byte* down = ssl->arrays->serverRandom + RAN_LEN -
- TLS13_DOWNGRADE_SZ - 1;
- byte vers = ssl->arrays->serverRandom[RAN_LEN - 1];
- #ifdef WOLFSSL_TLS13
- if (TLSv1_3_Capable(ssl)) {
- /* TLS v1.3 capable client not allowed to downgrade when
- * connecting to TLS v1.3 capable server unless cipher suite
- * demands it.
- */
- if (XMEMCMP(down, tls13Downgrade, TLS13_DOWNGRADE_SZ) == 0 &&
- (vers == 0 || vers == 1)) {
- SendAlert(ssl, alert_fatal, illegal_parameter);
- return VERSION_ERROR;
- }
- }
- else
- #endif
- if (ssl->ctx->method->version.major == SSLv3_MAJOR &&
- ssl->ctx->method->version.minor == TLSv1_2_MINOR) {
- /* TLS v1.2 capable client not allowed to downgrade when
- * connecting to TLS v1.2 capable server.
- */
- if (XMEMCMP(down, tls13Downgrade, TLS13_DOWNGRADE_SZ) == 0 &&
- vers == 0) {
- SendAlert(ssl, alert_fatal, illegal_parameter);
- return VERSION_ERROR;
- }
- }
- }
- else {
- if (DSH_CheckSessionId(ssl)) {
- if (SetCipherSpecs(ssl) == 0) {
-
- XMEMCPY(ssl->arrays->masterSecret,
- ssl->session.masterSecret, SECRET_LEN);
- #ifdef NO_OLD_TLS
- ret = DeriveTlsKeys(ssl);
- #else
- ret = -1; /* default value */
- #ifndef NO_TLS
- if (ssl->options.tls)
- ret = DeriveTlsKeys(ssl);
- #endif
- if (!ssl->options.tls)
- ret = DeriveKeys(ssl);
- #endif /* NO_OLD_TLS */
- ssl->options.serverState = SERVER_HELLODONE_COMPLETE;
-
- return ret;
- }
- else {
- WOLFSSL_MSG("Unsupported cipher suite, DoServerHello");
- return UNSUPPORTED_SUITE;
- }
- }
- else {
- WOLFSSL_MSG("Server denied resumption attempt");
- ssl->options.resuming = 0; /* server denied resumption try */
- }
- }
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- DtlsMsgPoolReset(ssl);
- }
- #endif
-
- return SetCipherSpecs(ssl);
- }
-
-#endif /* WOLFSSL_NO_TLS12 */
-
-
- /* Make sure client setup is valid for this suite, true on success */
- int VerifyClientSuite(WOLFSSL* ssl)
- {
- int havePSK = 0;
- byte first = ssl->options.cipherSuite0;
- byte second = ssl->options.cipherSuite;
-
- WOLFSSL_ENTER("VerifyClientSuite");
-
- #ifndef NO_PSK
- havePSK = ssl->options.havePSK;
- #endif
-
- if (CipherRequires(first, second, REQUIRES_PSK)) {
- WOLFSSL_MSG("Requires PSK");
- if (havePSK == 0) {
- WOLFSSL_MSG("Don't have PSK");
- return 0;
- }
- }
-
- return 1; /* success */
- }
-
-#ifndef WOLFSSL_NO_TLS12
-
-#ifndef NO_CERTS
- /* handle processing of certificate_request (13) */
- static int DoCertificateRequest(WOLFSSL* ssl, const byte* input, word32*
- inOutIdx, word32 size)
- {
- word16 len;
- word32 begin = *inOutIdx;
- #ifdef OPENSSL_EXTRA
- int ret;
- WOLFSSL_X509* x509 = NULL;
- WOLFSSL_EVP_PKEY* pkey = NULL;
- #endif
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_DO);
- WOLFSSL_ENTER("DoCertificateRequest");
-
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "CertificateRequest");
- if (ssl->toInfoOn)
- AddLateName("CertificateRequest", &ssl->timeoutInfo);
- #endif
-
- if (OPAQUE8_LEN > size)
- return BUFFER_ERROR;
-
- len = input[(*inOutIdx)++];
-
- if ((*inOutIdx - begin) + len > size)
- return BUFFER_ERROR;
-
- /* types, read in here */
- *inOutIdx += len;
-
- /* signature and hash signature algorithm */
- if (IsAtLeastTLSv1_2(ssl)) {
- if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
- return BUFFER_ERROR;
-
- ato16(input + *inOutIdx, &len);
- *inOutIdx += OPAQUE16_LEN;
-
- if ((*inOutIdx - begin) + len > size)
- return BUFFER_ERROR;
-
- if (PickHashSigAlgo(ssl, input + *inOutIdx, len) != 0 &&
- ssl->buffers.certificate &&
- ssl->buffers.certificate->buffer) {
- #ifdef HAVE_PK_CALLBACKS
- if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) {
- WOLFSSL_MSG("Using PK for client private key");
- return INVALID_PARAMETER;
- }
- #endif
- if (ssl->buffers.key && ssl->buffers.key->buffer) {
- return INVALID_PARAMETER;
- }
- }
- *inOutIdx += len;
- #ifdef WC_RSA_PSS
- ssl->pssAlgo = 0;
- if (ssl->suites->sigAlgo == rsa_pss_sa_algo)
- ssl->pssAlgo |= 1 << ssl->suites->hashAlgo;
- #endif
- }
-
- /* authorities */
- if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
- return BUFFER_ERROR;
-
- ato16(input + *inOutIdx, &len);
- *inOutIdx += OPAQUE16_LEN;
-
- if ((*inOutIdx - begin) + len > size)
- return BUFFER_ERROR;
-
- while (len) {
- word16 dnSz;
-
- if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
- return BUFFER_ERROR;
-
- ato16(input + *inOutIdx, &dnSz);
- *inOutIdx += OPAQUE16_LEN;
-
- if ((*inOutIdx - begin) + dnSz > size)
- return BUFFER_ERROR;
-
- *inOutIdx += dnSz;
- len -= OPAQUE16_LEN + dnSz;
- }
-
- #ifdef OPENSSL_EXTRA
- /* call client cert callback if no cert has been loaded */
- if ((ssl->ctx->CBClientCert != NULL) &&
- (!ssl->buffers.certificate || !ssl->buffers.certificate->buffer)) {
-
- ret = ssl->ctx->CBClientCert(ssl, &x509, &pkey);
- if (ret == 1) {
- if ((wolfSSL_use_certificate(ssl, x509) != WOLFSSL_SUCCESS) ||
- (wolfSSL_use_PrivateKey(ssl, pkey) != WOLFSSL_SUCCESS)) {
- return CLIENT_CERT_CB_ERROR;
- }
- wolfSSL_X509_free(x509);
- wolfSSL_EVP_PKEY_free(pkey);
-
- } else if (ret < 0) {
- return WOLFSSL_ERROR_WANT_X509_LOOKUP;
- }
- }
- #endif
-
- /* don't send client cert or cert verify if user hasn't provided
- cert and private key */
- if (ssl->buffers.certificate && ssl->buffers.certificate->buffer) {
- #ifdef HAVE_PK_CALLBACKS
- if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) {
- WOLFSSL_MSG("Using PK for client private key");
- ssl->options.sendVerify = SEND_CERT;
- }
- #endif
- if (ssl->buffers.key && ssl->buffers.key->buffer) {
- ssl->options.sendVerify = SEND_CERT;
- }
- }
- #ifdef OPENSSL_EXTRA
- else
- #else
- else if (IsTLS(ssl))
- #endif
- {
- ssl->options.sendVerify = SEND_BLANK_CERT;
- }
-
- if (IsEncryptionOn(ssl, 0)) {
- *inOutIdx += ssl->keys.padSz;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- *inOutIdx += MacSize(ssl);
- #endif
- }
-
- WOLFSSL_LEAVE("DoCertificateRequest", 0);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_DO);
-
- return 0;
- }
-#endif /* !NO_CERTS */
-
-
-#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
-
- static int CheckCurveId(int tlsCurveId)
- {
- int ret = ECC_CURVE_ERROR;
-
- switch (tlsCurveId) {
- #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP160R1: return ECC_SECP160R1_OID;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_SECPR2
- case WOLFSSL_ECC_SECP160R2: return ECC_SECP160R2_OID;
- #endif /* HAVE_ECC_SECPR2 */
- #ifdef HAVE_ECC_KOBLITZ
- case WOLFSSL_ECC_SECP160K1: return ECC_SECP160K1_OID;
- #endif /* HAVE_ECC_KOBLITZ */
- #endif
- #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP192R1: return ECC_SECP192R1_OID;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_KOBLITZ
- case WOLFSSL_ECC_SECP192K1: return ECC_SECP192K1_OID;
- #endif /* HAVE_ECC_KOBLITZ */
- #endif
- #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP224R1: return ECC_SECP224R1_OID;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_KOBLITZ
- case WOLFSSL_ECC_SECP224K1: return ECC_SECP224K1_OID;
- #endif /* HAVE_ECC_KOBLITZ */
- #endif
- #ifdef HAVE_CURVE25519
- case WOLFSSL_ECC_X25519: return ECC_X25519_OID;
- #endif
- #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP256R1: return ECC_SECP256R1_OID;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_KOBLITZ
- case WOLFSSL_ECC_SECP256K1: return ECC_SECP256K1_OID;
- #endif /* HAVE_ECC_KOBLITZ */
- #ifdef HAVE_ECC_BRAINPOOL
- case WOLFSSL_ECC_BRAINPOOLP256R1: return ECC_BRAINPOOLP256R1_OID;
- #endif /* HAVE_ECC_BRAINPOOL */
- #endif
- #ifdef HAVE_CURVE448
- case WOLFSSL_ECC_X448: return ECC_X448_OID;
- #endif
- #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP384R1: return ECC_SECP384R1_OID;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_BRAINPOOL
- case WOLFSSL_ECC_BRAINPOOLP384R1: return ECC_BRAINPOOLP384R1_OID;
- #endif /* HAVE_ECC_BRAINPOOL */
- #endif
- #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
- #ifdef HAVE_ECC_BRAINPOOL
- case WOLFSSL_ECC_BRAINPOOLP512R1: return ECC_BRAINPOOLP512R1_OID;
- #endif /* HAVE_ECC_BRAINPOOL */
- #endif
- #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP521R1: return ECC_SECP521R1_OID;
- #endif /* !NO_ECC_SECP */
- #endif
- }
-
- return ret;
- }
-
-#endif /* HAVE_ECC */
-
-/* Persistable DoServerKeyExchange arguments */
-typedef struct DskeArgs {
- byte* output; /* not allocated */
-#if !defined(NO_DH) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)
- byte* verifySig;
-#endif
- word32 idx;
- word32 begin;
-#if !defined(NO_DH) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)
- word16 verifySigSz;
-#endif
- word16 sigSz;
- byte sigAlgo;
- byte hashAlgo;
-#if !defined(NO_RSA) && defined(WC_RSA_PSS)
- int bits;
-#endif
-} DskeArgs;
-
-static void FreeDskeArgs(WOLFSSL* ssl, void* pArgs)
-{
- DskeArgs* args = (DskeArgs*)pArgs;
-
- (void)ssl;
- (void)args;
-
-#if !defined(NO_DH) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)
- if (args->verifySig) {
- XFREE(args->verifySig, ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- args->verifySig = NULL;
- }
-#endif
-}
-
-#ifndef NO_DH
-static int GetDhPublicKey(WOLFSSL* ssl, const byte* input, word32 size,
- DskeArgs* args)
-{
- int ret = 0;
- word16 length;
-#ifdef HAVE_FFDHE
- const DhParams* params = NULL;
- int group = 0;
-#endif
-
- ssl->buffers.weOwnDH = 1;
-
- ssl->buffers.serverDH_P.buffer = NULL;
- ssl->buffers.serverDH_G.buffer = NULL;
- ssl->buffers.serverDH_Pub.buffer = NULL;
-
- /* p */
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_gdpk);
- }
-
- ato16(input + args->idx, &length);
- args->idx += OPAQUE16_LEN;
-
- if ((args->idx - args->begin) + length > size) {
- ERROR_OUT(BUFFER_ERROR, exit_gdpk);
- }
-
- if (length < ssl->options.minDhKeySz) {
- WOLFSSL_MSG("Server using a DH key that is too small");
- SendAlert(ssl, alert_fatal, handshake_failure);
- ERROR_OUT(DH_KEY_SIZE_E, exit_gdpk);
- }
- if (length > ssl->options.maxDhKeySz) {
- WOLFSSL_MSG("Server using a DH key that is too big");
- SendAlert(ssl, alert_fatal, handshake_failure);
- ERROR_OUT(DH_KEY_SIZE_E, exit_gdpk);
- }
-
- ssl->buffers.serverDH_P.buffer =
- (byte*)XMALLOC(length, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (ssl->buffers.serverDH_P.buffer) {
- ssl->buffers.serverDH_P.length = length;
- }
- else {
- ERROR_OUT(MEMORY_ERROR, exit_gdpk);
- }
-
- XMEMCPY(ssl->buffers.serverDH_P.buffer, input + args->idx,
- length);
- args->idx += length;
-
- ssl->options.dhKeySz = length;
-
- /* g */
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_gdpk);
- }
-
- ato16(input + args->idx, &length);
- args->idx += OPAQUE16_LEN;
-
- if ((args->idx - args->begin) + length > size) {
- ERROR_OUT(BUFFER_ERROR, exit_gdpk);
- }
-
- ssl->buffers.serverDH_G.buffer =
- (byte*)XMALLOC(length, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (ssl->buffers.serverDH_G.buffer) {
- ssl->buffers.serverDH_G.length = length;
- }
- else {
- ERROR_OUT(MEMORY_ERROR, exit_gdpk);
- }
-
- XMEMCPY(ssl->buffers.serverDH_G.buffer, input + args->idx,
- length);
- args->idx += length;
-
- /* pub */
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_gdpk);
- }
-
- ato16(input + args->idx, &length);
- args->idx += OPAQUE16_LEN;
-
- if ((args->idx - args->begin) + length > size) {
- ERROR_OUT(BUFFER_ERROR, exit_gdpk);
- }
-
- ssl->buffers.serverDH_Pub.buffer =
- (byte*)XMALLOC(length, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (ssl->buffers.serverDH_Pub.buffer) {
- ssl->buffers.serverDH_Pub.length = length;
- }
- else {
- ERROR_OUT(MEMORY_ERROR, exit_gdpk);
- }
-
- XMEMCPY(ssl->buffers.serverDH_Pub.buffer, input + args->idx,
- length);
- args->idx += length;
-
-#ifdef HAVE_FFDHE
- switch (ssl->options.dhKeySz) {
- #ifdef HAVE_FFDHE_2048
- case 2048/8:
- params = wc_Dh_ffdhe2048_Get();
- group = WOLFSSL_FFDHE_2048;
- break;
- #endif
- #ifdef HAVE_FFDHE_3072
- case 3072/8:
- params = wc_Dh_ffdhe3072_Get();
- group = WOLFSSL_FFDHE_3072;
- break;
- #endif
- #ifdef HAVE_FFDHE_4096
- case 4096/8:
- params = wc_Dh_ffdhe4096_Get();
- group = WOLFSSL_FFDHE_4096;
- break;
- #endif
- #ifdef HAVE_FFDHE_6144
- case 6144/8:
- params = wc_Dh_ffdhe6144_Get();
- group = WOLFSSL_FFDHE_6144;
- break;
- #endif
- #ifdef HAVE_FFDHE_8192
- case 8192/8:
- params = wc_Dh_ffdhe8192_Get();
- group = WOLFSSL_FFDHE_8192;
- break;
- #endif
- default:
- break;
- }
-
- if (params == NULL || params->g_len != ssl->buffers.serverDH_G.length ||
- (XMEMCMP(ssl->buffers.serverDH_G.buffer, params->g,
- params->g_len) != 0) ||
- (XMEMCMP(ssl->buffers.serverDH_P.buffer, params->p,
- params->p_len) != 0)) {
- WOLFSSL_MSG("Server not using FFDHE parameters");
- #ifdef WOLFSSL_REQUIRE_FFDHE
- SendAlert(ssl, alert_fatal, handshake_failure);
- ERROR_OUT(DH_PARAMS_NOT_FFDHE_E, exit_gdpk);
- #endif
- }
- else {
- ssl->namedGroup = group;
- #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
- !defined(HAVE_SELFTEST)
- ssl->options.dhDoKeyTest = 0;
- #endif
- }
-#endif /* HAVE_FFDHE */
-
-exit_gdpk:
- return ret;
-}
-#endif
-
-/* handle processing of server_key_exchange (12) */
-static int DoServerKeyExchange(WOLFSSL* ssl, const byte* input,
- word32* inOutIdx, word32 size)
-{
- int ret = 0;
-#ifdef WOLFSSL_ASYNC_CRYPT
- DskeArgs* args = (DskeArgs*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
-#else
- DskeArgs args[1];
-#endif
-
- (void)input;
- (void)size;
-
- WOLFSSL_START(WC_FUNC_SERVER_KEY_EXCHANGE_DO);
- WOLFSSL_ENTER("DoServerKeyExchange");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_dske;
- }
- else
-#endif
- {
- /* Reset state */
- ret = 0;
- ssl->options.asyncState = TLS_ASYNC_BEGIN;
- XMEMSET(args, 0, sizeof(DskeArgs));
- args->idx = *inOutIdx;
- args->begin = *inOutIdx;
- args->sigAlgo = ssl->specs.sig_algo;
- args->hashAlgo = sha_mac;
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeDskeArgs;
- #endif
- }
-
- switch(ssl->options.asyncState)
- {
- case TLS_ASYNC_BEGIN:
- {
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "ServerKeyExchange");
- if (ssl->toInfoOn)
- AddLateName("ServerKeyExchange", &ssl->timeoutInfo);
- #endif
-
- switch(ssl->specs.kea)
- {
- #ifndef NO_PSK
- case psk_kea:
- {
- int srvHintLen;
- word16 length;
-
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- ato16(input + args->idx, &length);
- args->idx += OPAQUE16_LEN;
-
- if ((args->idx - args->begin) + length > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- /* get PSK server hint from the wire */
- srvHintLen = min(length, MAX_PSK_ID_LEN);
- XMEMCPY(ssl->arrays->server_hint, input + args->idx,
- srvHintLen);
- ssl->arrays->server_hint[srvHintLen] = '\0'; /* null term */
- args->idx += length;
- break;
- }
- #endif /* !NO_PSK */
- #ifndef NO_DH
- case diffie_hellman_kea:
- {
- ret = GetDhPublicKey(ssl, input, size, args);
- if (ret != 0)
- goto exit_dske;
- break;
- }
- #endif /* !NO_DH */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- byte b;
- #ifdef HAVE_ECC
- int curveId;
- #endif
- int curveOid;
- word16 length;
-
- if ((args->idx - args->begin) + ENUM_LEN + OPAQUE16_LEN +
- OPAQUE8_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- b = input[args->idx++];
- if (b != named_curve) {
- ERROR_OUT(ECC_CURVETYPE_ERROR, exit_dske);
- }
-
- args->idx += 1; /* curve type, eat leading 0 */
- b = input[args->idx++];
- if ((curveOid = CheckCurveId(b)) < 0) {
- ERROR_OUT(ECC_CURVE_ERROR, exit_dske);
- }
- ssl->ecdhCurveOID = curveOid;
- #if defined(WOLFSSL_TLS13) || defined(HAVE_FFDHE)
- ssl->namedGroup = 0;
- #endif
-
- length = input[args->idx++];
- if ((args->idx - args->begin) + length > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- if (ssl->peerX25519Key == NULL) {
- ret = AllocKey(ssl, DYNAMIC_TYPE_CURVE25519,
- (void**)&ssl->peerX25519Key);
- if (ret != 0) {
- goto exit_dske;
- }
- } else if (ssl->peerX25519KeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_CURVE25519,
- ssl->peerX25519Key);
- ssl->peerX25519KeyPresent = 0;
- if (ret != 0) {
- goto exit_dske;
- }
- }
-
- if ((ret = wc_curve25519_check_public(
- input + args->idx, length,
- EC25519_LITTLE_ENDIAN)) != 0) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_E)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret == ECC_OUT_OF_RANGE_E)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- else {
- SendAlert(ssl, alert_fatal, illegal_parameter);
- }
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dske);
- }
-
- if (wc_curve25519_import_public_ex(input + args->idx,
- length, ssl->peerX25519Key,
- EC25519_LITTLE_ENDIAN) != 0) {
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dske);
- }
-
- args->idx += length;
- ssl->peerX25519KeyPresent = 1;
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- if (ssl->peerX448Key == NULL) {
- ret = AllocKey(ssl, DYNAMIC_TYPE_CURVE448,
- (void**)&ssl->peerX448Key);
- if (ret != 0) {
- goto exit_dske;
- }
- } else if (ssl->peerX448KeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_CURVE448,
- ssl->peerX448Key);
- ssl->peerX448KeyPresent = 0;
- if (ret != 0) {
- goto exit_dske;
- }
- }
-
- if ((ret = wc_curve448_check_public(
- input + args->idx, length,
- EC448_LITTLE_ENDIAN)) != 0) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_E)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret == ECC_OUT_OF_RANGE_E)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- else {
- SendAlert(ssl, alert_fatal, illegal_parameter);
- }
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dske);
- }
-
- if (wc_curve448_import_public_ex(input + args->idx,
- length, ssl->peerX448Key,
- EC448_LITTLE_ENDIAN) != 0) {
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dske);
- }
-
- args->idx += length;
- ssl->peerX448KeyPresent = 1;
- break;
- }
- #endif
- #ifdef HAVE_ECC
- if (ssl->peerEccKey == NULL) {
- ret = AllocKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->peerEccKey);
- if (ret != 0) {
- goto exit_dske;
- }
- } else if (ssl->peerEccKeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_ECC, ssl->peerEccKey);
- ssl->peerEccKeyPresent = 0;
- if (ret != 0) {
- goto exit_dske;
- }
- }
-
- curveId = wc_ecc_get_oid(curveOid, NULL, NULL);
- if (wc_ecc_import_x963_ex(input + args->idx, length,
- ssl->peerEccKey, curveId) != 0) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, illegal_parameter);
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dske);
- }
-
- args->idx += length;
- ssl->peerEccKeyPresent = 1;
- #endif
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- int srvHintLen;
- word16 length;
-
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- ato16(input + args->idx, &length);
- args->idx += OPAQUE16_LEN;
-
- if ((args->idx - args->begin) + length > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- /* get PSK server hint from the wire */
- srvHintLen = min(length, MAX_PSK_ID_LEN);
- XMEMCPY(ssl->arrays->server_hint, input + args->idx,
- srvHintLen);
- ssl->arrays->server_hint[srvHintLen] = '\0'; /* null term */
- args->idx += length;
-
- ret = GetDhPublicKey(ssl, input, size, args);
- if (ret != 0)
- goto exit_dske;
- break;
- }
- #endif /* !NO_DH && !NO_PSK */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- byte b;
- int curveOid, curveId;
- int srvHintLen;
- word16 length;
-
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- ato16(input + args->idx, &length);
- args->idx += OPAQUE16_LEN;
-
- if ((args->idx - args->begin) + length > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- /* get PSK server hint from the wire */
- srvHintLen = min(length, MAX_PSK_ID_LEN);
- XMEMCPY(ssl->arrays->server_hint, input + args->idx,
- srvHintLen);
- ssl->arrays->server_hint[srvHintLen] = '\0'; /* null term */
-
- args->idx += length;
-
- if ((args->idx - args->begin) + ENUM_LEN + OPAQUE16_LEN +
- OPAQUE8_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- /* Check curve name and ID */
- b = input[args->idx++];
- if (b != named_curve) {
- ERROR_OUT(ECC_CURVETYPE_ERROR, exit_dske);
- }
-
- args->idx += 1; /* curve type, eat leading 0 */
- b = input[args->idx++];
- if ((curveOid = CheckCurveId(b)) < 0) {
- ERROR_OUT(ECC_CURVE_ERROR, exit_dske);
- }
-
- length = input[args->idx++];
- if ((args->idx - args->begin) + length > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- if (ssl->peerX25519Key == NULL) {
- ret = AllocKey(ssl, DYNAMIC_TYPE_CURVE25519,
- (void**)&ssl->peerX25519Key);
- if (ret != 0) {
- goto exit_dske;
- }
- } else if (ssl->peerEccKeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_CURVE25519,
- ssl->peerX25519Key);
- ssl->peerX25519KeyPresent = 0;
- if (ret != 0) {
- goto exit_dske;
- }
- }
-
- if ((ret = wc_curve25519_check_public(
- input + args->idx, length,
- EC25519_LITTLE_ENDIAN)) != 0) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_E)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret == ECC_OUT_OF_RANGE_E)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- else {
- SendAlert(ssl, alert_fatal, illegal_parameter);
- }
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dske);
- }
-
- if (wc_curve25519_import_public_ex(input + args->idx,
- length, ssl->peerX25519Key,
- EC25519_LITTLE_ENDIAN) != 0) {
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dske);
- }
-
- args->idx += length;
- ssl->peerX25519KeyPresent = 1;
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- if (ssl->peerX448Key == NULL) {
- ret = AllocKey(ssl, DYNAMIC_TYPE_CURVE448,
- (void**)&ssl->peerX448Key);
- if (ret != 0) {
- goto exit_dske;
- }
- } else if (ssl->peerEccKeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_CURVE448,
- ssl->peerX448Key);
- ssl->peerX448KeyPresent = 0;
- if (ret != 0) {
- goto exit_dske;
- }
- }
-
- if ((ret = wc_curve448_check_public(
- input + args->idx, length,
- EC448_LITTLE_ENDIAN)) != 0) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_E)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret == ECC_OUT_OF_RANGE_E)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- else {
- SendAlert(ssl, alert_fatal, illegal_parameter);
- }
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dske);
- }
-
- if (wc_curve448_import_public_ex(input + args->idx,
- length, ssl->peerX448Key,
- EC448_LITTLE_ENDIAN) != 0) {
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dske);
- }
-
- args->idx += length;
- ssl->peerX448KeyPresent = 1;
- break;
- }
- #endif
-
- if (ssl->peerEccKey == NULL) {
- ret = AllocKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->peerEccKey);
- if (ret != 0) {
- goto exit_dske;
- }
- } else if (ssl->peerEccKeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_ECC, ssl->peerEccKey);
- ssl->peerEccKeyPresent = 0;
- if (ret != 0) {
- goto exit_dske;
- }
- }
-
- curveId = wc_ecc_get_oid(curveOid, NULL, NULL);
- if (wc_ecc_import_x963_ex(input + args->idx, length,
- ssl->peerEccKey, curveId) != 0) {
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dske);
- }
-
- args->idx += length;
- ssl->peerEccKeyPresent = 1;
- break;
- }
- #endif /* (HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448) && !NO_PSK */
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_dske;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_BUILD;
- } /* case TLS_ASYNC_BEGIN */
- FALL_THROUGH;
-
- case TLS_ASYNC_BUILD:
- {
- switch(ssl->specs.kea)
- {
- case psk_kea:
- case dhe_psk_kea:
- case ecdhe_psk_kea:
- {
- /* Nothing to do in this sub-state */
- break;
- }
-
- case diffie_hellman_kea:
- case ecc_diffie_hellman_kea:
- {
- #if defined(NO_DH) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) \
- && !defined(HAVE_ED448)
- ERROR_OUT(NOT_COMPILED_IN, exit_dske);
- #else
- enum wc_HashType hashType;
- word16 verifySz;
-
- if (ssl->options.usingAnon_cipher) {
- break;
- }
-
- verifySz = (word16)(args->idx - args->begin);
- if (verifySz > MAX_DH_SZ) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- if (IsAtLeastTLSv1_2(ssl)) {
- if ((args->idx - args->begin) + ENUM_LEN + ENUM_LEN >
- size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- DecodeSigAlg(&input[args->idx], &args->hashAlgo,
- &args->sigAlgo);
- args->idx += 2;
- hashType = HashAlgoToType(args->hashAlgo);
- if (hashType == WC_HASH_TYPE_NONE) {
- ERROR_OUT(ALGO_ID_E, exit_dske);
- }
- } else {
- /* only using sha and md5 for rsa */
- #ifndef NO_OLD_TLS
- hashType = WC_HASH_TYPE_SHA;
- if (args->sigAlgo == rsa_sa_algo) {
- hashType = WC_HASH_TYPE_MD5_SHA;
- }
- #else
- ERROR_OUT(ALGO_ID_E, exit_dske);
- #endif
- }
-
- /* signature */
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- ato16(input + args->idx, &args->verifySigSz);
- args->idx += OPAQUE16_LEN;
-
- if ((args->idx - args->begin) + args->verifySigSz > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
-
- /* buffer for signature */
- ssl->buffers.sig.buffer = (byte*)XMALLOC(SEED_LEN + verifySz,
- ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- if (ssl->buffers.sig.buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_dske);
- }
- ssl->buffers.sig.length = SEED_LEN + verifySz;
-
- /* build message to hash */
- XMEMCPY(ssl->buffers.sig.buffer,
- ssl->arrays->clientRandom, RAN_LEN);
- XMEMCPY(&ssl->buffers.sig.buffer[RAN_LEN],
- ssl->arrays->serverRandom, RAN_LEN);
- XMEMCPY(&ssl->buffers.sig.buffer[RAN_LEN * 2],
- input + args->begin, verifySz); /* message */
-
- if (args->sigAlgo != ed25519_sa_algo) {
- int digest_sz = wc_HashGetDigestSize(hashType);
- if (digest_sz <= 0) {
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
- ssl->buffers.digest.length = (unsigned int)digest_sz;
-
- /* buffer for hash */
- ssl->buffers.digest.buffer = (byte*)XMALLOC(
- ssl->buffers.digest.length, ssl->heap,
- DYNAMIC_TYPE_DIGEST);
- if (ssl->buffers.digest.buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_dske);
- }
-
- /* Perform hash */
- ret = wc_Hash(hashType, ssl->buffers.sig.buffer,
- ssl->buffers.sig.length,
- ssl->buffers.digest.buffer,
- ssl->buffers.digest.length);
- if (ret != 0) {
- goto exit_dske;
- }
- }
-
- switch (args->sigAlgo)
- {
- #ifndef NO_RSA
- #ifdef WC_RSA_PSS
- case rsa_pss_sa_algo:
- #endif
- case rsa_sa_algo:
- {
- if (ssl->peerRsaKey == NULL ||
- !ssl->peerRsaKeyPresent) {
- ERROR_OUT(NO_PEER_KEY, exit_dske);
- }
- break;
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ecc_dsa_sa_algo:
- {
- if (!ssl->peerEccDsaKeyPresent) {
- ERROR_OUT(NO_PEER_KEY, exit_dske);
- }
- break;
- }
- #endif /* HAVE_ECC */
- #if defined(HAVE_ED25519)
- case ed25519_sa_algo:
- {
- if (!ssl->peerEd25519KeyPresent) {
- ERROR_OUT(NO_PEER_KEY, exit_dske);
- }
- break;
- }
- #endif /* HAVE_ED25519 */
- #if defined(HAVE_ED448)
- case ed448_sa_algo:
- {
- if (!ssl->peerEd448KeyPresent) {
- ERROR_OUT(NO_PEER_KEY, exit_dske);
- }
- break;
- }
- #endif /* HAVE_ED448 */
-
- default:
- ret = ALGO_ID_E;
- } /* switch (args->sigAlgo) */
-
- #endif /* NO_DH && !HAVE_ECC && !HAVE_ED25519 && !HAVE_ED448 */
- break;
- }
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_dske;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_DO;
- } /* case TLS_ASYNC_BUILD */
- FALL_THROUGH;
-
- case TLS_ASYNC_DO:
- {
- switch(ssl->specs.kea)
- {
- case psk_kea:
- case dhe_psk_kea:
- case ecdhe_psk_kea:
- {
- /* Nothing to do in this sub-state */
- break;
- }
-
- case diffie_hellman_kea:
- case ecc_diffie_hellman_kea:
- {
- #if defined(NO_DH) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) \
- && !defined(HAVE_ED448)
- ERROR_OUT(NOT_COMPILED_IN, exit_dske);
- #else
- if (ssl->options.usingAnon_cipher) {
- break;
- }
-
- if (args->verifySig == NULL) {
- args->verifySig = (byte*)XMALLOC(args->verifySigSz,
- ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- if (args->verifySig == NULL) {
- ERROR_OUT(MEMORY_E, exit_dske);
- }
- XMEMCPY(args->verifySig, input + args->idx,
- args->verifySigSz);
- }
-
- switch (args->sigAlgo)
- {
- #ifndef NO_RSA
- #ifdef WC_RSA_PSS
- case rsa_pss_sa_algo:
- #endif
- case rsa_sa_algo:
- {
- ret = RsaVerify(ssl,
- args->verifySig, args->verifySigSz,
- &args->output,
- args->sigAlgo, args->hashAlgo,
- ssl->peerRsaKey,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerRsaKey
- #else
- NULL
- #endif
- );
-
- if (ret >= 0) {
- args->sigSz = (word16)ret;
- #ifdef WC_RSA_PSS
- args->bits = mp_count_bits(&ssl->peerRsaKey->n);
- #endif
- ret = 0;
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret != WC_PENDING_E)
- #endif
- {
- /* peerRsaKey */
- FreeKey(ssl, DYNAMIC_TYPE_RSA,
- (void**)&ssl->peerRsaKey);
- ssl->peerRsaKeyPresent = 0;
- }
- break;
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ecc_dsa_sa_algo:
- {
- ret = EccVerify(ssl,
- args->verifySig, args->verifySigSz,
- ssl->buffers.digest.buffer,
- ssl->buffers.digest.length,
- ssl->peerEccDsaKey,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerEccDsaKey
- #else
- NULL
- #endif
- );
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret != WC_PENDING_E)
- #endif
- {
- /* peerEccDsaKey */
- FreeKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->peerEccDsaKey);
- ssl->peerEccDsaKeyPresent = 0;
- }
- break;
- }
- #endif /* HAVE_ECC */
- #if defined(HAVE_ED25519)
- case ed25519_sa_algo:
- {
- ret = Ed25519Verify(ssl,
- args->verifySig, args->verifySigSz,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length,
- ssl->peerEd25519Key,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerEd25519Key
- #else
- NULL
- #endif
- );
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret != WC_PENDING_E)
- #endif
- {
- /* peerEccDsaKey */
- FreeKey(ssl, DYNAMIC_TYPE_ED25519,
- (void**)&ssl->peerEd25519Key);
- ssl->peerEd25519KeyPresent = 0;
- }
- break;
- }
- #endif /* HAVE_ED25519 */
- #if defined(HAVE_ED448)
- case ed448_sa_algo:
- {
- ret = Ed448Verify(ssl,
- args->verifySig, args->verifySigSz,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length,
- ssl->peerEd448Key,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerEd448Key
- #else
- NULL
- #endif
- );
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret != WC_PENDING_E)
- #endif
- {
- /* peerEccDsaKey */
- FreeKey(ssl, DYNAMIC_TYPE_ED448,
- (void**)&ssl->peerEd448Key);
- ssl->peerEd448KeyPresent = 0;
- }
- break;
- }
- #endif /* HAVE_ED448 */
-
- default:
- ret = ALGO_ID_E;
- } /* switch (sigAlgo) */
- #endif /* NO_DH && !HAVE_ECC && !HAVE_ED25519 && !HAVE_ED448 */
- break;
- }
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_dske;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_VERIFY;
- } /* case TLS_ASYNC_DO */
- FALL_THROUGH;
-
- case TLS_ASYNC_VERIFY:
- {
- switch(ssl->specs.kea)
- {
- case psk_kea:
- case dhe_psk_kea:
- case ecdhe_psk_kea:
- {
- /* Nothing to do in this sub-state */
- break;
- }
-
- case diffie_hellman_kea:
- case ecc_diffie_hellman_kea:
- {
- #if defined(NO_DH) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) \
- && !defined(HAVE_ED448)
- ERROR_OUT(NOT_COMPILED_IN, exit_dske);
- #else
- if (ssl->options.usingAnon_cipher) {
- break;
- }
-
- /* increment index after verify is done */
- args->idx += args->verifySigSz;
-
- switch(args->sigAlgo)
- {
- #ifndef NO_RSA
- #ifdef WC_RSA_PSS
- case rsa_pss_sa_algo:
- #ifdef HAVE_SELFTEST
- ret = wc_RsaPSS_CheckPadding(
- ssl->buffers.digest.buffer,
- ssl->buffers.digest.length,
- args->output, args->sigSz,
- HashAlgoToType(args->hashAlgo));
- #else
- ret = wc_RsaPSS_CheckPadding_ex(
- ssl->buffers.digest.buffer,
- ssl->buffers.digest.length,
- args->output, args->sigSz,
- HashAlgoToType(args->hashAlgo),
- -1, args->bits);
- #endif
- if (ret != 0)
- return ret;
- break;
- #endif
- case rsa_sa_algo:
- {
- if (IsAtLeastTLSv1_2(ssl)) {
- #ifdef WOLFSSL_SMALL_STACK
- byte* encodedSig;
- #else
- byte encodedSig[MAX_ENCODED_SIG_SZ];
- #endif
- word32 encSigSz;
-
- #ifdef WOLFSSL_SMALL_STACK
- encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ,
- ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- if (encodedSig == NULL) {
- ERROR_OUT(MEMORY_E, exit_dske);
- }
- #endif
-
- encSigSz = wc_EncodeSignature(encodedSig,
- ssl->buffers.digest.buffer,
- ssl->buffers.digest.length,
- TypeHash(args->hashAlgo));
- if (encSigSz != args->sigSz || !args->output ||
- XMEMCMP(args->output, encodedSig,
- min(encSigSz, MAX_ENCODED_SIG_SZ)) != 0) {
- ret = VERIFY_SIGN_ERROR;
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(encodedSig, ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- #endif
- if (ret != 0) {
- goto exit_dske;
- }
- }
- else if (args->sigSz != FINISHED_SZ ||
- !args->output ||
- XMEMCMP(args->output,
- ssl->buffers.digest.buffer,
- FINISHED_SZ) != 0) {
- ERROR_OUT(VERIFY_SIGN_ERROR, exit_dske);
- }
- break;
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ecc_dsa_sa_algo:
- /* Nothing to do in this algo */
- break;
- #endif /* HAVE_ECC */
- #if defined(HAVE_ED25519)
- case ed25519_sa_algo:
- /* Nothing to do in this algo */
- break;
- #endif /* HAVE_ED25519 */
- #if defined(HAVE_ED448)
- case ed448_sa_algo:
- /* Nothing to do in this algo */
- break;
- #endif /* HAVE_ED448 */
- default:
- ret = ALGO_ID_E;
- } /* switch (sigAlgo) */
- #endif /* NO_DH && !HAVE_ECC && !HAVE_ED25519 && !HAVE_ED448 */
- break;
- }
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_dske;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_FINALIZE;
- } /* case TLS_ASYNC_VERIFY */
- FALL_THROUGH;
-
- case TLS_ASYNC_FINALIZE:
- {
- if (IsEncryptionOn(ssl, 0)) {
- args->idx += ssl->keys.padSz;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- args->idx += MacSize(ssl);
- #endif
- }
-
- /* QSH extensions */
- #ifdef HAVE_QSH
- if (ssl->peerQSHKeyPresent) {
- word16 name;
- int qshSz;
-
- /* extension name */
- ato16(input + args->idx, &name);
- args->idx += OPAQUE16_LEN;
-
- if (name == TLSX_QUANTUM_SAFE_HYBRID) {
- /* if qshSz is larger than 0 it is the length of
- buffer used */
- if ((qshSz = TLSX_QSHCipher_Parse(ssl, input + args->idx,
- size, 0)) < 0) {
- ERROR_OUT(qshSz, exit_dske);
- }
- args->idx += qshSz;
- }
- else {
- /* unknown extension sent server ignored handshake */
- ERROR_OUT(BUFFER_ERROR, exit_dske);
- }
- }
- #endif
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_END;
- } /* case TLS_ASYNC_FINALIZE */
- FALL_THROUGH;
-
- case TLS_ASYNC_END:
- {
- /* return index */
- *inOutIdx = args->idx;
-
- ssl->options.serverState = SERVER_KEYEXCHANGE_COMPLETE;
- break;
- }
- default:
- ret = INPUT_CASE_ERROR;
- } /* switch(ssl->options.asyncState) */
-
-exit_dske:
-
- WOLFSSL_LEAVE("DoServerKeyExchange", ret);
- WOLFSSL_END(WC_FUNC_SERVER_KEY_EXCHANGE_DO);
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* Handle async operation */
- if (ret == WC_PENDING_E) {
- /* Mark message as not received so it can process again */
- ssl->msgsReceived.got_server_key_exchange = 0;
-
- return ret;
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- /* Final cleanup */
- FreeDskeArgs(ssl, args);
- FreeKeyExchange(ssl);
-
- return ret;
-}
-
-
-#ifdef HAVE_QSH
-
-#ifdef HAVE_NTRU
-/* Encrypt a byte array using ntru
- key a struct containing the public key to use
- bufIn array to be encrypted
- inSz size of bufIn array
- bufOut cipher text out
- outSz will be set to the new size of cipher text
- */
-static int NtruSecretEncrypt(QSHKey* key, byte* bufIn, word32 inSz,
- byte* bufOut, word16* outSz)
-{
- int ret;
- DRBG_HANDLE drbg;
-
- /* sanity checks on input arguments */
- if (key == NULL || bufIn == NULL || bufOut == NULL || outSz == NULL)
- return BAD_FUNC_ARG;
-
- if (key->pub.buffer == NULL)
- return BAD_FUNC_ARG;
-
- switch (key->name) {
- case WOLFSSL_NTRU_EESS439:
- case WOLFSSL_NTRU_EESS593:
- case WOLFSSL_NTRU_EESS743:
- break;
- default:
- WOLFSSL_MSG("Unknown QSH encryption key!");
- return -1;
- }
-
- /* set up ntru drbg */
- ret = ntru_crypto_drbg_external_instantiate(GetEntropy, &drbg);
- if (ret != DRBG_OK)
- return NTRU_DRBG_ERROR;
-
- /* encrypt the byte array */
- ret = ntru_crypto_ntru_encrypt(drbg, key->pub.length, key->pub.buffer,
- inSz, bufIn, outSz, bufOut);
- ntru_crypto_drbg_uninstantiate(drbg);
- if (ret != NTRU_OK)
- return NTRU_ENCRYPT_ERROR;
-
- return ret;
-}
-
-/* Decrypt a byte array using ntru
- key a struct containing the private key to use
- bufIn array to be decrypted
- inSz size of bufIn array
- bufOut plain text out
- outSz will be set to the new size of plain text
- */
-
-static int NtruSecretDecrypt(QSHKey* key, byte* bufIn, word32 inSz,
- byte* bufOut, word16* outSz)
-{
- int ret;
- DRBG_HANDLE drbg;
-
- /* sanity checks on input arguments */
- if (key == NULL || bufIn == NULL || bufOut == NULL || outSz == NULL)
- return BAD_FUNC_ARG;
-
- if (key->pri.buffer == NULL)
- return BAD_FUNC_ARG;
-
- switch (key->name) {
- case WOLFSSL_NTRU_EESS439:
- case WOLFSSL_NTRU_EESS593:
- case WOLFSSL_NTRU_EESS743:
- break;
- default:
- WOLFSSL_MSG("Unknown QSH decryption key!");
- return -1;
- }
-
-
- /* set up drbg */
- ret = ntru_crypto_drbg_external_instantiate(GetEntropy, &drbg);
- if (ret != DRBG_OK)
- return NTRU_DRBG_ERROR;
-
- /* decrypt cipher text */
- ret = ntru_crypto_ntru_decrypt(key->pri.length, key->pri.buffer,
- inSz, bufIn, outSz, bufOut);
- ntru_crypto_drbg_uninstantiate(drbg);
- if (ret != NTRU_OK)
- return NTRU_ENCRYPT_ERROR;
-
- return ret;
-}
-#endif /* HAVE_NTRU */
-
-int QSH_Init(WOLFSSL* ssl)
-{
- /* check so not initializing twice when running DTLS */
- if (ssl->QSH_secret != NULL)
- return 0;
-
- /* malloc memory for holding generated secret information */
- if ((ssl->QSH_secret = (QSHSecret*)XMALLOC(sizeof(QSHSecret), ssl->heap,
- DYNAMIC_TYPE_QSH)) == NULL)
- return MEMORY_E;
-
- ssl->QSH_secret->CliSi = (buffer*)XMALLOC(sizeof(buffer), ssl->heap,
- DYNAMIC_TYPE_SECRET);
- if (ssl->QSH_secret->CliSi == NULL)
- return MEMORY_E;
-
- ssl->QSH_secret->SerSi = (buffer*)XMALLOC(sizeof(buffer), ssl->heap,
- DYNAMIC_TYPE_SECRET);
- if (ssl->QSH_secret->SerSi == NULL)
- return MEMORY_E;
-
- /* initialize variables */
- ssl->QSH_secret->list = NULL;
- ssl->QSH_secret->CliSi->length = 0;
- ssl->QSH_secret->CliSi->buffer = NULL;
- ssl->QSH_secret->SerSi->length = 0;
- ssl->QSH_secret->SerSi->buffer = NULL;
-
- return 0;
-}
-
-
-static int QSH_Encrypt(QSHKey* key, byte* in, word32 szIn,
- byte* out, word32* szOut)
-{
- int ret = 0;
- word16 size = *szOut;
-
- (void)in;
- (void)szIn;
- (void)out;
- (void)szOut;
-
- WOLFSSL_MSG("Encrypting QSH key material");
-
- switch (key->name) {
- #ifdef HAVE_NTRU
- case WOLFSSL_NTRU_EESS439:
- case WOLFSSL_NTRU_EESS593:
- case WOLFSSL_NTRU_EESS743:
- ret = NtruSecretEncrypt(key, in, szIn, out, &size);
- break;
- #endif
- default:
- WOLFSSL_MSG("Unknown QSH encryption key!");
- return -1;
- }
-
- *szOut = size;
-
- return ret;
-}
-
-
-/* Decrypt using Quantum Safe Handshake algorithms */
-int QSH_Decrypt(QSHKey* key, byte* in, word32 szIn, byte* out, word16* szOut)
-{
- int ret = 0;
- word16 size = *szOut;
-
- (void)in;
- (void)szIn;
- (void)out;
- (void)szOut;
-
- WOLFSSL_MSG("Decrypting QSH key material");
-
- switch (key->name) {
- #ifdef HAVE_NTRU
- case WOLFSSL_NTRU_EESS439:
- case WOLFSSL_NTRU_EESS593:
- case WOLFSSL_NTRU_EESS743:
- ret = NtruSecretDecrypt(key, in, szIn, out, &size);
- break;
- #endif
- default:
- WOLFSSL_MSG("Unknown QSH decryption key!");
- return -1;
- }
-
- *szOut = size;
-
- return ret;
-}
-
-
-/* Get the max cipher text for corresponding encryption scheme
- (encrypting 48 or max plain text whichever is smaller)
- */
-static word32 QSH_MaxSecret(QSHKey* key)
-{
- int ret = 0;
-#ifdef HAVE_NTRU
- byte isNtru = 0;
- word16 inSz = 48;
- word16 outSz;
- DRBG_HANDLE drbg = 0;
- byte bufIn[48];
-#endif
-
- if (key == NULL || key->pub.length == 0)
- return 0;
-
- switch(key->name) {
-#ifdef HAVE_NTRU
- case WOLFSSL_NTRU_EESS439:
- isNtru = 1;
- break;
- case WOLFSSL_NTRU_EESS593:
- isNtru = 1;
- break;
- case WOLFSSL_NTRU_EESS743:
- isNtru = 1;
- break;
-#endif
- default:
- WOLFSSL_MSG("Unknown QSH encryption scheme size!");
- return 0;
- }
-
-#ifdef HAVE_NTRU
- if (isNtru) {
- ret = ntru_crypto_drbg_external_instantiate(GetEntropy, &drbg);
- if (ret != DRBG_OK)
- return NTRU_DRBG_ERROR;
- ret = ntru_crypto_ntru_encrypt(drbg, key->pub.length,
- key->pub.buffer, inSz, bufIn, &outSz, NULL);
- if (ret != NTRU_OK) {
- return NTRU_ENCRYPT_ERROR;
- }
- ntru_crypto_drbg_uninstantiate(drbg);
- ret = outSz;
- }
-#endif
-
- return ret;
-}
-
-/* Generate the secret byte material for pms
- returns length on success and -1 on fail
- */
-static int QSH_GenerateSerCliSecret(WOLFSSL* ssl, byte isServer)
-{
- int sz = 0;
- int plainSz = 48; /* lesser of 48 and max plain text able to encrypt */
- int offset = 0;
- word32 tmpSz = 0;
- buffer* buf;
- QSHKey* current;
- QSHScheme* schmPre = NULL;
- QSHScheme* schm = NULL;
-
- if (ssl == NULL)
- return -1;
-
- WOLFSSL_MSG("Generating QSH secret key material");
-
- current = ssl->peerQSHKey;
- /* get size of buffer needed */
- while (current) {
- if (current->pub.length != 0) {
- sz += plainSz;
- }
- current = (QSHKey*)current->next;
- }
-
- /* allocate memory for buffer */
- if (isServer) {
- buf = ssl->QSH_secret->SerSi;
- }
- else {
- buf = ssl->QSH_secret->CliSi;
- }
- buf->length = sz;
- buf->buffer = (byte*)XMALLOC(sz, ssl->heap, DYNAMIC_TYPE_SECRET);
- if (buf->buffer == NULL) {
- WOLFSSL_ERROR(MEMORY_E);
- }
-
- /* create secret information */
- sz = 0;
- current = ssl->peerQSHKey;
- while (current) {
- schm = (QSHScheme*)XMALLOC(sizeof(QSHScheme), ssl->heap,
- DYNAMIC_TYPE_QSH);
- if (schm == NULL)
- return MEMORY_E;
-
- /* initialize variables */
- schm->name = 0;
- schm->PK = NULL;
- schm->PKLen = 0;
- schm->next = NULL;
- if (ssl->QSH_secret->list == NULL) {
- ssl->QSH_secret->list = schm;
- }
- else {
- if (schmPre)
- schmPre->next = schm;
- }
-
- tmpSz = QSH_MaxSecret(current);
-
- if ((schm->PK = (byte*)XMALLOC(tmpSz, ssl->heap,
- DYNAMIC_TYPE_SECRET)) == NULL)
- return -1;
-
- /* store info for writing extension */
- schm->name = current->name;
-
- /* no key to use for encryption */
- if (tmpSz == 0) {
- current = (QSHKey*)current->next;
- continue;
- }
-
- if (wc_RNG_GenerateBlock(ssl->rng, buf->buffer + offset, plainSz)
- != 0) {
- return -1;
- }
- if (QSH_Encrypt(current, buf->buffer + offset, plainSz, schm->PK,
- &tmpSz) != 0) {
- return -1;
- }
- schm->PKLen = tmpSz;
-
- sz += tmpSz;
- offset += plainSz;
- schmPre = schm;
- current = (QSHKey*)current->next;
- }
-
- return sz;
-}
-
-
-static word32 QSH_KeyGetSize(WOLFSSL* ssl)
-{
- word32 sz = 0;
- QSHKey* current;
-
- if (ssl == NULL)
- return -1;
-
- current = ssl->peerQSHKey;
- sz += OPAQUE16_LEN; /* type of extension ie 0x00 0x18 */
- sz += OPAQUE24_LEN;
- /* get size of buffer needed */
- while (current) {
- sz += OPAQUE16_LEN; /* scheme id */
- sz += OPAQUE16_LEN; /* encrypted key len*/
- sz += QSH_MaxSecret(current);
- current = (QSHKey*)current->next;
- }
-
- return sz;
-}
-
-
-/* handle QSH key Exchange
- return 0 on success
- */
-static word32 QSH_KeyExchangeWrite(WOLFSSL* ssl, byte isServer)
-{
- int ret = 0;
-
- WOLFSSL_ENTER("QSH KeyExchange");
-
- ret = QSH_GenerateSerCliSecret(ssl, isServer);
- if (ret < 0)
- return MEMORY_E;
-
- return 0;
-}
-
-#endif /* HAVE_QSH */
-
-
-typedef struct SckeArgs {
- byte* output; /* not allocated */
- byte* encSecret;
- byte* input;
- word32 encSz;
- word32 length;
- int sendSz;
- int inputSz;
-} SckeArgs;
-
-static void FreeSckeArgs(WOLFSSL* ssl, void* pArgs)
-{
- SckeArgs* args = (SckeArgs*)pArgs;
-
- (void)ssl;
-
- if (args->encSecret) {
- XFREE(args->encSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
- args->encSecret = NULL;
- }
- if (args->input) {
- XFREE(args->input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- args->input = NULL;
- }
-}
-
-/* handle generation client_key_exchange (16) */
-int SendClientKeyExchange(WOLFSSL* ssl)
-{
- int ret = 0;
-#ifdef WOLFSSL_ASYNC_CRYPT
- SckeArgs* args = (SckeArgs*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
-#else
- SckeArgs args[1];
-#endif
-
- WOLFSSL_START(WC_FUNC_CLIENT_KEY_EXCHANGE_SEND);
- WOLFSSL_ENTER("SendClientKeyExchange");
-
-#ifdef OPENSSL_EXTRA
- ssl->options.clientState = CLIENT_KEYEXCHANGE_COMPLETE;
- ssl->cbmode = SSL_CB_MODE_WRITE;
- if (ssl->CBIS != NULL)
- ssl->CBIS(ssl, SSL_CB_CONNECT_LOOP, SSL_SUCCESS);
-#endif
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_scke;
- }
- else
-#endif
- {
- /* Reset state */
- ret = 0;
- ssl->options.asyncState = TLS_ASYNC_BEGIN;
- XMEMSET(args, 0, sizeof(SckeArgs));
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeSckeArgs;
- #endif
- }
-
- switch(ssl->options.asyncState)
- {
- case TLS_ASYNC_BEGIN:
- {
- switch (ssl->specs.kea) {
- #ifndef NO_RSA
- case rsa_kea:
- if (ssl->peerRsaKey == NULL ||
- ssl->peerRsaKeyPresent == 0) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
- break;
- #endif
- #ifndef NO_DH
- case diffie_hellman_kea:
- if (ssl->buffers.serverDH_P.buffer == NULL ||
- ssl->buffers.serverDH_G.buffer == NULL ||
- ssl->buffers.serverDH_Pub.buffer == NULL) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
- break;
- #endif /* NO_DH */
- #ifndef NO_PSK
- case psk_kea:
- /* sanity check that PSK client callback has been set */
- if (ssl->options.client_psk_cb == NULL) {
- WOLFSSL_MSG("No client PSK callback set");
- ERROR_OUT(PSK_KEY_ERROR, exit_scke);
- }
- break;
- #endif /* NO_PSK */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- if (ssl->buffers.serverDH_P.buffer == NULL ||
- ssl->buffers.serverDH_G.buffer == NULL ||
- ssl->buffers.serverDH_Pub.buffer == NULL) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
-
- /* sanity check that PSK client callback has been set */
- if (ssl->options.client_psk_cb == NULL) {
- WOLFSSL_MSG("No client PSK callback set");
- ERROR_OUT(PSK_KEY_ERROR, exit_scke);
- }
- break;
- #endif /* !NO_DH && !NO_PSK */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- /* sanity check that PSK client callback has been set */
- if (ssl->options.client_psk_cb == NULL) {
- WOLFSSL_MSG("No client PSK callback set");
- ERROR_OUT(PSK_KEY_ERROR, exit_scke);
- }
-
- #ifdef HAVE_CURVE25519
- if (ssl->peerX25519KeyPresent) {
- /* Check client ECC public key */
- if (!ssl->peerX25519Key || !ssl->peerX25519Key->dp) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
-
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->X25519SharedSecretCb != NULL) {
- break;
- }
- #endif
-
- /* create private key */
- ssl->hsType = DYNAMIC_TYPE_CURVE25519;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_scke;
- }
-
- ret = X25519MakeKey(ssl, (curve25519_key*)ssl->hsKey,
- ssl->peerX25519Key);
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->peerX448KeyPresent) {
- /* Check client ECC public key */
- if (!ssl->peerX448Key) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
-
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->X448SharedSecretCb != NULL) {
- break;
- }
- #endif
-
- /* create private key */
- ssl->hsType = DYNAMIC_TYPE_CURVE448;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_scke;
- }
-
- ret = X448MakeKey(ssl, (curve448_key*)ssl->hsKey,
- ssl->peerX448Key);
- break;
- }
- #endif
- /* Check client ECC public key */
- if (!ssl->peerEccKey || !ssl->peerEccKeyPresent ||
- !ssl->peerEccKey->dp) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
-
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->EccSharedSecretCb != NULL) {
- break;
- }
- #endif
-
- /* create ephemeral private key */
- ssl->hsType = DYNAMIC_TYPE_ECC;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_scke;
- }
-
- ret = EccMakeKey(ssl, (ecc_key*)ssl->hsKey, ssl->peerEccKey);
-
- break;
- #endif /* (HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448) && !NO_PSK */
- #ifdef HAVE_NTRU
- case ntru_kea:
- if (ssl->peerNtruKeyPresent == 0) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
- break;
- #endif /* HAVE_NTRU */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- #ifdef HAVE_ECC
- ecc_key* peerKey;
- #endif
-
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- if (ssl->ctx->X25519SharedSecretCb != NULL)
- break;
- }
- else
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- if (ssl->ctx->X448SharedSecretCb != NULL)
- break;
- }
- else
- #endif
- if (ssl->ctx->EccSharedSecretCb != NULL) {
- break;
- }
- #endif /* HAVE_PK_CALLBACKS */
-
- #ifdef HAVE_CURVE25519
- if (ssl->peerX25519KeyPresent) {
- if (!ssl->peerX25519Key || !ssl->peerX25519Key->dp) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
-
- /* create private key */
- ssl->hsType = DYNAMIC_TYPE_CURVE25519;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_scke;
- }
-
- ret = X25519MakeKey(ssl, (curve25519_key*)ssl->hsKey,
- ssl->peerX25519Key);
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->peerX448KeyPresent) {
- if (!ssl->peerX448Key) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
-
- /* create private key */
- ssl->hsType = DYNAMIC_TYPE_CURVE448;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_scke;
- }
-
- ret = X448MakeKey(ssl, (curve448_key*)ssl->hsKey,
- ssl->peerX448Key);
- break;
- }
- #endif
- #ifdef HAVE_ECC
- if (ssl->specs.static_ecdh) {
- /* Note: EccDsa is really fixed Ecc key here */
- if (!ssl->peerEccDsaKey || !ssl->peerEccDsaKeyPresent) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
- peerKey = ssl->peerEccDsaKey;
- }
- else {
- if (!ssl->peerEccKey || !ssl->peerEccKeyPresent) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
- peerKey = ssl->peerEccKey;
- }
- if (peerKey == NULL) {
- ERROR_OUT(NO_PEER_KEY, exit_scke);
- }
-
- /* create ephemeral private key */
- ssl->hsType = DYNAMIC_TYPE_ECC;
- ret = AllocKey(ssl, ssl->hsType, &ssl->hsKey);
- if (ret != 0) {
- goto exit_scke;
- }
-
- ret = EccMakeKey(ssl, (ecc_key*)ssl->hsKey, peerKey);
- #endif
-
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_scke;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_BUILD;
- } /* case TLS_ASYNC_BEGIN */
- FALL_THROUGH;
-
- case TLS_ASYNC_BUILD:
- {
- args->encSz = MAX_ENCRYPT_SZ;
- args->encSecret = (byte*)XMALLOC(args->encSz, ssl->heap,
- DYNAMIC_TYPE_SECRET);
- if (args->encSecret == NULL) {
- ERROR_OUT(MEMORY_E, exit_scke);
- }
- if (ssl->arrays->preMasterSecret == NULL) {
- ssl->arrays->preMasterSz = ENCRYPT_LEN;
- ssl->arrays->preMasterSecret = (byte*)XMALLOC(ENCRYPT_LEN,
- ssl->heap, DYNAMIC_TYPE_SECRET);
- if (ssl->arrays->preMasterSecret == NULL) {
- ERROR_OUT(MEMORY_E, exit_scke);
- }
- XMEMSET(ssl->arrays->preMasterSecret, 0, ENCRYPT_LEN);
- }
-
- switch(ssl->specs.kea)
- {
- #ifndef NO_RSA
- case rsa_kea:
- {
- /* build PreMasterSecret with RNG data */
- #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- if (tsip_useable(ssl)) {
- ret = tsip_generatePremasterSecret(
- &ssl->arrays->preMasterSecret[VERSION_SZ],
- ENCRYPT_LEN - VERSION_SZ);
- } else {
- #endif
- ret = wc_RNG_GenerateBlock(ssl->rng,
- &ssl->arrays->preMasterSecret[VERSION_SZ],
- SECRET_LEN - VERSION_SZ);
- #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- }
- #endif
- if (ret != 0) {
- goto exit_scke;
- }
-
- ssl->arrays->preMasterSecret[0] = ssl->chVersion.major;
- ssl->arrays->preMasterSecret[1] = ssl->chVersion.minor;
-
- ssl->arrays->preMasterSz = SECRET_LEN;
-
- break;
- }
- #endif /* !NO_RSA */
- #ifndef NO_DH
- case diffie_hellman_kea:
- {
- ssl->buffers.sig.length = ENCRYPT_LEN;
- ssl->buffers.sig.buffer = (byte*)XMALLOC(ENCRYPT_LEN,
- ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- if (ssl->buffers.sig.buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_scke);
- }
-
- ret = AllocKey(ssl, DYNAMIC_TYPE_DH,
- (void**)&ssl->buffers.serverDH_Key);
- if (ret != 0) {
- goto exit_scke;
- }
-
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
- !defined(WOLFSSL_OLD_PRIME_CHECK)
- if (ssl->options.dhDoKeyTest &&
- !ssl->options.dhKeyTested)
- {
- ret = wc_DhSetCheckKey(ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length,
- ssl->buffers.serverDH_G.buffer,
- ssl->buffers.serverDH_G.length,
- NULL, 0, 0, ssl->rng);
- if (ret != 0) {
- goto exit_scke;
- }
- ssl->options.dhKeyTested = 1;
- }
- else
- #endif
- {
- ret = wc_DhSetKey(ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length,
- ssl->buffers.serverDH_G.buffer,
- ssl->buffers.serverDH_G.length);
- if (ret != 0) {
- goto exit_scke;
- }
- }
-
- /* for DH, encSecret is Yc, agree is pre-master */
- ret = DhGenKeyPair(ssl, ssl->buffers.serverDH_Key,
- ssl->buffers.sig.buffer, (word32*)&ssl->buffers.sig.length,
- args->encSecret, &args->encSz);
-
- /* set the max agree result size */
- ssl->arrays->preMasterSz = ENCRYPT_LEN;
- break;
- }
- #endif /* !NO_DH */
- #ifndef NO_PSK
- case psk_kea:
- {
- byte* pms = ssl->arrays->preMasterSecret;
- ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
- ssl->arrays->server_hint, ssl->arrays->client_identity,
- MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
- if (ssl->arrays->psk_keySz == 0 ||
- ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
- ERROR_OUT(PSK_KEY_ERROR, exit_scke);
- }
- ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0'; /* null term */
- args->encSz = (word32)XSTRLEN(ssl->arrays->client_identity);
- if (args->encSz > MAX_PSK_ID_LEN) {
- ERROR_OUT(CLIENT_ID_ERROR, exit_scke);
- }
- XMEMCPY(args->encSecret, ssl->arrays->client_identity,
- args->encSz);
-
- /* make psk pre master secret */
- /* length of key + length 0s + length of key + key */
- c16toa((word16)ssl->arrays->psk_keySz, pms);
- pms += OPAQUE16_LEN;
- XMEMSET(pms, 0, ssl->arrays->psk_keySz);
- pms += ssl->arrays->psk_keySz;
- c16toa((word16)ssl->arrays->psk_keySz, pms);
- pms += OPAQUE16_LEN;
- XMEMCPY(pms, ssl->arrays->psk_key, ssl->arrays->psk_keySz);
- ssl->arrays->preMasterSz = (ssl->arrays->psk_keySz * 2) +
- (2 * OPAQUE16_LEN);
- ForceZero(ssl->arrays->psk_key, ssl->arrays->psk_keySz);
- ssl->arrays->psk_keySz = 0; /* No further need */
- break;
- }
- #endif /* !NO_PSK */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- word32 esSz = 0;
- args->output = args->encSecret;
-
- ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
- ssl->arrays->server_hint, ssl->arrays->client_identity,
- MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
- if (ssl->arrays->psk_keySz == 0 ||
- ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
- ERROR_OUT(PSK_KEY_ERROR, exit_scke);
- }
- ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0'; /* null term */
- esSz = (word32)XSTRLEN(ssl->arrays->client_identity);
-
- if (esSz > MAX_PSK_ID_LEN) {
- ERROR_OUT(CLIENT_ID_ERROR, exit_scke);
- }
-
- ssl->buffers.sig.length = ENCRYPT_LEN;
- ssl->buffers.sig.buffer = (byte*)XMALLOC(ENCRYPT_LEN,
- ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- if (ssl->buffers.sig.buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_scke);
- }
-
- c16toa((word16)esSz, args->output);
- args->output += OPAQUE16_LEN;
- XMEMCPY(args->output, ssl->arrays->client_identity, esSz);
- args->output += esSz;
- args->encSz = esSz + OPAQUE16_LEN;
-
- args->length = 0;
-
- ret = AllocKey(ssl, DYNAMIC_TYPE_DH,
- (void**)&ssl->buffers.serverDH_Key);
- if (ret != 0) {
- goto exit_scke;
- }
-
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
- !defined(WOLFSSL_OLD_PRIME_CHECK)
- if (ssl->options.dhDoKeyTest &&
- !ssl->options.dhKeyTested)
- {
- ret = wc_DhSetCheckKey(ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length,
- ssl->buffers.serverDH_G.buffer,
- ssl->buffers.serverDH_G.length,
- NULL, 0, 0, ssl->rng);
- if (ret != 0) {
- goto exit_scke;
- }
- ssl->options.dhKeyTested = 1;
- }
- else
- #endif
- {
- ret = wc_DhSetKey(ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length,
- ssl->buffers.serverDH_G.buffer,
- ssl->buffers.serverDH_G.length);
- if (ret != 0) {
- goto exit_scke;
- }
- }
-
- /* for DH, encSecret is Yc, agree is pre-master */
- ret = DhGenKeyPair(ssl, ssl->buffers.serverDH_Key,
- ssl->buffers.sig.buffer, (word32*)&ssl->buffers.sig.length,
- args->output + OPAQUE16_LEN, &args->length);
- break;
- }
- #endif /* !NO_DH && !NO_PSK */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- word32 esSz = 0;
- args->output = args->encSecret;
-
- /* Send PSK client identity */
- ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
- ssl->arrays->server_hint, ssl->arrays->client_identity,
- MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
- if (ssl->arrays->psk_keySz == 0 ||
- ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
- ERROR_OUT(PSK_KEY_ERROR, exit_scke);
- }
- ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0'; /* null term */
- esSz = (word32)XSTRLEN(ssl->arrays->client_identity);
- if (esSz > MAX_PSK_ID_LEN) {
- ERROR_OUT(CLIENT_ID_ERROR, exit_scke);
- }
-
- /* place size and identity in output buffer sz:identity */
- c16toa((word16)esSz, args->output);
- args->output += OPAQUE16_LEN;
- XMEMCPY(args->output, ssl->arrays->client_identity, esSz);
- args->output += esSz;
- args->encSz = esSz + OPAQUE16_LEN;
-
- /* length is used for public key size */
- args->length = MAX_ENCRYPT_SZ;
-
- /* Create shared ECC key leaving room at the beginning
- of buffer for size of shared key. */
- ssl->arrays->preMasterSz = ENCRYPT_LEN - OPAQUE16_LEN;
-
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->X25519SharedSecretCb != NULL) {
- break;
- }
- #endif
-
- ret = wc_curve25519_export_public_ex(
- (curve25519_key*)ssl->hsKey,
- args->output + OPAQUE8_LEN, &args->length,
- EC25519_LITTLE_ENDIAN);
- if (ret != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
- }
-
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->X448SharedSecretCb != NULL) {
- break;
- }
- #endif
-
- ret = wc_curve448_export_public_ex(
- (curve448_key*)ssl->hsKey,
- args->output + OPAQUE8_LEN, &args->length,
- EC448_LITTLE_ENDIAN);
- if (ret != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
- }
-
- break;
- }
- #endif
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->EccSharedSecretCb != NULL) {
- break;
- }
- #endif
-
- /* Place ECC key in output buffer, leaving room for size */
- ret = wc_ecc_export_x963((ecc_key*)ssl->hsKey,
- args->output + OPAQUE8_LEN, &args->length);
- if (ret != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
- }
-
- break;
- }
- #endif /* (HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448) && !NO_PSK */
- #ifdef HAVE_NTRU
- case ntru_kea:
- {
- ret = wc_RNG_GenerateBlock(ssl->rng,
- ssl->arrays->preMasterSecret, SECRET_LEN);
- if (ret != 0) {
- goto exit_scke;
- }
-
- ssl->arrays->preMasterSz = SECRET_LEN;
- args->encSz = MAX_ENCRYPT_SZ;
- break;
- }
- #endif /* HAVE_NTRU */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- ssl->arrays->preMasterSz = ENCRYPT_LEN;
-
- #ifdef HAVE_CURVE25519
- if (ssl->hsType == DYNAMIC_TYPE_CURVE25519) {
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->X25519SharedSecretCb != NULL) {
- break;
- }
- #endif
-
- ret = wc_curve25519_export_public_ex(
- (curve25519_key*)ssl->hsKey,
- args->encSecret + OPAQUE8_LEN, &args->encSz,
- EC25519_LITTLE_ENDIAN);
- if (ret != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
- }
-
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->hsType == DYNAMIC_TYPE_CURVE448) {
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->X448SharedSecretCb != NULL) {
- break;
- }
- #endif
-
- ret = wc_curve448_export_public_ex(
- (curve448_key*)ssl->hsKey,
- args->encSecret + OPAQUE8_LEN, &args->encSz,
- EC448_LITTLE_ENDIAN);
- if (ret != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
- }
-
- break;
- }
- #endif
- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->EccSharedSecretCb != NULL) {
- break;
- }
- #endif
-
- /* Place ECC key in buffer, leaving room for size */
- ret = wc_ecc_export_x963((ecc_key*)ssl->hsKey,
- args->encSecret + OPAQUE8_LEN, &args->encSz);
- if (ret != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_scke);
- }
- #endif /* HAVE_ECC */
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_scke;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_DO;
- } /* case TLS_ASYNC_BUILD */
- FALL_THROUGH;
-
- case TLS_ASYNC_DO:
- {
- switch(ssl->specs.kea)
- {
- #ifndef NO_RSA
- case rsa_kea:
- {
- #if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- if (tsip_useable(ssl) &&
- wc_RsaEncryptSize(ssl->peerRsaKey) == 256) {
- ret = tsip_generateEncryptPreMasterSecret(ssl,
- args->encSecret,
- &args->encSz);
-
- } else
- #endif
- ret = RsaEnc(ssl,
- ssl->arrays->preMasterSecret, SECRET_LEN,
- args->encSecret, &args->encSz,
- ssl->peerRsaKey,
- #if defined(HAVE_PK_CALLBACKS)
- &ssl->buffers.peerRsaKey
- #else
- NULL
- #endif
- );
-
- break;
- }
- #endif /* !NO_RSA */
- #ifndef NO_DH
- case diffie_hellman_kea:
- {
- ret = DhAgree(ssl, ssl->buffers.serverDH_Key,
- ssl->buffers.sig.buffer, ssl->buffers.sig.length,
- ssl->buffers.serverDH_Pub.buffer,
- ssl->buffers.serverDH_Pub.length,
- ssl->arrays->preMasterSecret,
- &ssl->arrays->preMasterSz);
- break;
- }
- #endif /* !NO_DH */
- #ifndef NO_PSK
- case psk_kea:
- {
- break;
- }
- #endif /* !NO_PSK */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- ret = DhAgree(ssl, ssl->buffers.serverDH_Key,
- ssl->buffers.sig.buffer, ssl->buffers.sig.length,
- ssl->buffers.serverDH_Pub.buffer,
- ssl->buffers.serverDH_Pub.length,
- ssl->arrays->preMasterSecret + OPAQUE16_LEN,
- &ssl->arrays->preMasterSz);
- break;
- }
- #endif /* !NO_DH && !NO_PSK */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- #ifdef HAVE_CURVE25519
- if (ssl->peerX25519KeyPresent) {
- ret = X25519SharedSecret(ssl,
- (curve25519_key*)ssl->hsKey, ssl->peerX25519Key,
- args->output + OPAQUE8_LEN, &args->length,
- ssl->arrays->preMasterSecret + OPAQUE16_LEN,
- &ssl->arrays->preMasterSz,
- WOLFSSL_CLIENT_END
- );
- if (!ssl->specs.static_ecdh
- #ifdef WOLFSSL_ASYNC_CRYPT
- && ret != WC_PENDING_E
- #endif
- ) {
- FreeKey(ssl, DYNAMIC_TYPE_CURVE25519,
- (void**)&ssl->peerX25519Key);
- ssl->peerX25519KeyPresent = 0;
- }
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->peerX448KeyPresent) {
- ret = X448SharedSecret(ssl,
- (curve448_key*)ssl->hsKey, ssl->peerX448Key,
- args->output + OPAQUE8_LEN, &args->length,
- ssl->arrays->preMasterSecret + OPAQUE16_LEN,
- &ssl->arrays->preMasterSz,
- WOLFSSL_CLIENT_END
- );
- if (!ssl->specs.static_ecdh
- #ifdef WOLFSSL_ASYNC_CRYPT
- && ret != WC_PENDING_E
- #endif
- ) {
- FreeKey(ssl, DYNAMIC_TYPE_CURVE448,
- (void**)&ssl->peerX448Key);
- ssl->peerX448KeyPresent = 0;
- }
- break;
- }
- #endif
- ret = EccSharedSecret(ssl,
- (ecc_key*)ssl->hsKey, ssl->peerEccKey,
- args->output + OPAQUE8_LEN, &args->length,
- ssl->arrays->preMasterSecret + OPAQUE16_LEN,
- &ssl->arrays->preMasterSz,
- WOLFSSL_CLIENT_END
- );
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret != WC_PENDING_E)
- #endif
- {
- FreeKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->peerEccKey);
- ssl->peerEccKeyPresent = 0;
- }
- break;
- }
- #endif /* (HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448) && !NO_PSK */
- #ifdef HAVE_NTRU
- case ntru_kea:
- {
- word32 rc;
- word16 tmpEncSz = (word16)args->encSz;
- DRBG_HANDLE drbg;
-
- rc = ntru_crypto_drbg_external_instantiate(GetEntropy, &drbg);
- if (rc != DRBG_OK) {
- ERROR_OUT(NTRU_DRBG_ERROR, exit_scke);
- }
- rc = ntru_crypto_ntru_encrypt(drbg, ssl->peerNtruKeyLen,
- ssl->peerNtruKey,
- ssl->arrays->preMasterSz,
- ssl->arrays->preMasterSecret,
- &tmpEncSz,
- args->encSecret);
- args->encSz = tmpEncSz;
- ntru_crypto_drbg_uninstantiate(drbg);
- if (rc != NTRU_OK) {
- ERROR_OUT(NTRU_ENCRYPT_ERROR, exit_scke);
- }
- ret = 0;
- break;
- }
- #endif /* HAVE_NTRU */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- #ifdef HAVE_ECC
- ecc_key* peerKey;
- #endif
-
- #ifdef HAVE_CURVE25519
- if (ssl->peerX25519KeyPresent) {
- ret = X25519SharedSecret(ssl,
- (curve25519_key*)ssl->hsKey, ssl->peerX25519Key,
- args->encSecret + OPAQUE8_LEN, &args->encSz,
- ssl->arrays->preMasterSecret,
- &ssl->arrays->preMasterSz,
- WOLFSSL_CLIENT_END
- );
- if (!ssl->specs.static_ecdh
- #ifdef WOLFSSL_ASYNC_CRYPT
- && ret != WC_PENDING_E
- #endif
- ) {
- FreeKey(ssl, DYNAMIC_TYPE_CURVE25519,
- (void**)&ssl->peerX25519Key);
- ssl->peerX25519KeyPresent = 0;
- }
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->peerX448KeyPresent) {
- ret = X448SharedSecret(ssl,
- (curve448_key*)ssl->hsKey, ssl->peerX448Key,
- args->encSecret + OPAQUE8_LEN, &args->encSz,
- ssl->arrays->preMasterSecret,
- &ssl->arrays->preMasterSz,
- WOLFSSL_CLIENT_END
- );
- if (!ssl->specs.static_ecdh
- #ifdef WOLFSSL_ASYNC_CRYPT
- && ret != WC_PENDING_E
- #endif
- ) {
- FreeKey(ssl, DYNAMIC_TYPE_CURVE448,
- (void**)&ssl->peerX448Key);
- ssl->peerX448KeyPresent = 0;
- }
- break;
- }
- #endif
- #ifdef HAVE_ECC
- peerKey = (ssl->specs.static_ecdh) ?
- ssl->peerEccDsaKey : ssl->peerEccKey;
-
- ret = EccSharedSecret(ssl,
- (ecc_key*)ssl->hsKey, peerKey,
- args->encSecret + OPAQUE8_LEN, &args->encSz,
- ssl->arrays->preMasterSecret,
- &ssl->arrays->preMasterSz,
- WOLFSSL_CLIENT_END
- );
- if (!ssl->specs.static_ecdh
- #ifdef WOLFSSL_ASYNC_CRYPT
- && ret != WC_PENDING_E
- #endif
- && !ssl->options.keepResources) {
- FreeKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->peerEccKey);
- ssl->peerEccKeyPresent = 0;
- }
- #endif
-
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_scke;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_VERIFY;
- } /* case TLS_ASYNC_DO */
- FALL_THROUGH;
-
- case TLS_ASYNC_VERIFY:
- {
- switch(ssl->specs.kea)
- {
- #ifndef NO_RSA
- case rsa_kea:
- {
- break;
- }
- #endif /* !NO_RSA */
- #ifndef NO_DH
- case diffie_hellman_kea:
- {
- break;
- }
- #endif /* !NO_DH */
- #ifndef NO_PSK
- case psk_kea:
- {
- break;
- }
- #endif /* !NO_PSK */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- byte* pms = ssl->arrays->preMasterSecret;
-
- /* validate args */
- if (args->output == NULL || args->length == 0) {
- ERROR_OUT(BAD_FUNC_ARG, exit_scke);
- }
-
- c16toa((word16)args->length, args->output);
- args->encSz += args->length + OPAQUE16_LEN;
- c16toa((word16)ssl->arrays->preMasterSz, pms);
- ssl->arrays->preMasterSz += OPAQUE16_LEN;
- pms += ssl->arrays->preMasterSz;
-
- /* make psk pre master secret */
- /* length of key + length 0s + length of key + key */
- c16toa((word16)ssl->arrays->psk_keySz, pms);
- pms += OPAQUE16_LEN;
- XMEMCPY(pms, ssl->arrays->psk_key, ssl->arrays->psk_keySz);
- ssl->arrays->preMasterSz +=
- ssl->arrays->psk_keySz + OPAQUE16_LEN;
- ForceZero(ssl->arrays->psk_key, ssl->arrays->psk_keySz);
- ssl->arrays->psk_keySz = 0; /* No further need */
- break;
- }
- #endif /* !NO_DH && !NO_PSK */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- byte* pms = ssl->arrays->preMasterSecret;
-
- /* validate args */
- if (args->output == NULL || args->length > ENCRYPT_LEN) {
- ERROR_OUT(BAD_FUNC_ARG, exit_scke);
- }
-
- /* place size of public key in output buffer */
- *args->output = (byte)args->length;
- args->encSz += args->length + OPAQUE8_LEN;
-
- /* Create pre master secret is the concatenation of
- eccSize + eccSharedKey + pskSize + pskKey */
- c16toa((word16)ssl->arrays->preMasterSz, pms);
- ssl->arrays->preMasterSz += OPAQUE16_LEN;
- pms += ssl->arrays->preMasterSz;
-
- c16toa((word16)ssl->arrays->psk_keySz, pms);
- pms += OPAQUE16_LEN;
- XMEMCPY(pms, ssl->arrays->psk_key, ssl->arrays->psk_keySz);
- ssl->arrays->preMasterSz +=
- ssl->arrays->psk_keySz + OPAQUE16_LEN;
-
- ForceZero(ssl->arrays->psk_key, ssl->arrays->psk_keySz);
- ssl->arrays->psk_keySz = 0; /* No further need */
- break;
- }
- #endif /* (HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448) && !NO_PSK */
- #ifdef HAVE_NTRU
- case ntru_kea:
- {
- break;
- }
- #endif /* HAVE_NTRU */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- /* place size of public key in buffer */
- *args->encSecret = (byte)args->encSz;
- args->encSz += OPAQUE8_LEN;
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_scke;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_FINALIZE;
- } /* case TLS_ASYNC_VERIFY */
- FALL_THROUGH;
-
- case TLS_ASYNC_FINALIZE:
- {
- word32 tlsSz = 0;
- word32 idx = 0;
-
- #ifdef HAVE_QSH
- word32 qshSz = 0;
- if (ssl->peerQSHKeyPresent) {
- qshSz = QSH_KeyGetSize(ssl);
- }
- #endif
-
- if (ssl->options.tls || ssl->specs.kea == diffie_hellman_kea) {
- tlsSz = 2;
- }
-
- if (ssl->specs.kea == ecc_diffie_hellman_kea ||
- ssl->specs.kea == dhe_psk_kea ||
- ssl->specs.kea == ecdhe_psk_kea) { /* always off */
- tlsSz = 0;
- }
-
- idx = HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ;
- args->sendSz = args->encSz + tlsSz + idx;
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- idx += DTLS_HANDSHAKE_EXTRA + DTLS_RECORD_EXTRA;
- args->sendSz += DTLS_HANDSHAKE_EXTRA + DTLS_RECORD_EXTRA;
- }
- #endif
-
- if (IsEncryptionOn(ssl, 1)) {
- args->sendSz += MAX_MSG_EXTRA;
- }
-
- #ifdef HAVE_QSH
- args->encSz += qshSz;
- args->sendSz += qshSz;
- #endif
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) {
- goto exit_scke;
- }
-
- /* get output buffer */
- args->output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- #ifdef HAVE_QSH
- if (ssl->peerQSHKeyPresent) {
- byte idxSave = idx;
- idx = args->sendSz - qshSz;
-
- if (QSH_KeyExchangeWrite(ssl, 0) != 0) {
- ERROR_OUT(MEMORY_E, exit_scke);
- }
-
- /* extension type */
- c16toa(TLSX_QUANTUM_SAFE_HYBRID, args->output + idx);
- idx += OPAQUE16_LEN;
-
- /* write to output and check amount written */
- if (TLSX_QSHPK_Write(ssl->QSH_secret->list,
- args->output + idx) > qshSz - OPAQUE16_LEN) {
- ERROR_OUT(MEMORY_E, exit_scke);
- }
-
- idx = idxSave;
- }
- #endif
-
- AddHeaders(args->output, args->encSz + tlsSz, client_key_exchange, ssl);
-
- #ifdef HAVE_QSH
- if (ssl->peerQSHKeyPresent) {
- args->encSz -= qshSz;
- }
- #endif
- if (tlsSz) {
- c16toa((word16)args->encSz, &args->output[idx]);
- idx += OPAQUE16_LEN;
- }
- XMEMCPY(args->output + idx, args->encSecret, args->encSz);
- idx += args->encSz;
-
- if (IsEncryptionOn(ssl, 1)) {
- args->inputSz = idx - RECORD_HEADER_SZ; /* buildmsg adds rechdr */
- args->input = (byte*)XMALLOC(args->inputSz, ssl->heap,
- DYNAMIC_TYPE_IN_BUFFER);
- if (args->input == NULL) {
- ERROR_OUT(MEMORY_E, exit_scke);
- }
-
- XMEMCPY(args->input, args->output + RECORD_HEADER_SZ,
- args->inputSz);
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_END;
- } /* case TLS_ASYNC_FINALIZE */
- FALL_THROUGH;
-
- case TLS_ASYNC_END:
- {
- if (IsEncryptionOn(ssl, 1)) {
- ret = BuildMessage(ssl, args->output, args->sendSz,
- args->input, args->inputSz, handshake, 1, 0, 0);
- XFREE(args->input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- args->input = NULL; /* make sure its not double free'd on cleanup */
-
- if (ret >= 0) {
- args->sendSz = ret;
- ret = 0;
- }
- }
- else {
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- DtlsSEQIncrement(ssl, CUR_ORDER);
- #endif
- ret = HashOutput(ssl, args->output, args->sendSz, 0);
- }
-
- if (ret != 0) {
- goto exit_scke;
- }
-
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- if ((ret = DtlsMsgPoolSave(ssl, args->output, args->sendSz)) != 0) {
- goto exit_scke;
- }
- }
- #endif
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "ClientKeyExchange");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "ClientKeyExchange", handshake,
- args->output, args->sendSz, WRITE_PROTO, ssl->heap);
- #endif
-
- ssl->buffers.outputBuffer.length += args->sendSz;
-
- if (!ssl->options.groupMessages) {
- ret = SendBuffered(ssl);
- }
- if (ret == 0 || ret == WANT_WRITE) {
- int tmpRet = MakeMasterSecret(ssl);
- if (tmpRet != 0) {
- ret = tmpRet; /* save WANT_WRITE unless more serious */
- }
- ssl->options.clientState = CLIENT_KEYEXCHANGE_COMPLETE;
- }
- break;
- }
- default:
- ret = INPUT_CASE_ERROR;
- } /* switch(ssl->options.asyncState) */
-
-exit_scke:
-
- WOLFSSL_LEAVE("SendClientKeyExchange", ret);
- WOLFSSL_END(WC_FUNC_CLIENT_KEY_EXCHANGE_SEND);
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* Handle async operation */
- if (ret == WC_PENDING_E)
- return ret;
-#endif
-
- /* No further need for PMS */
- if (ssl->arrays->preMasterSecret != NULL) {
- ForceZero(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
- }
- ssl->arrays->preMasterSz = 0;
-
- /* Final cleanup */
- FreeSckeArgs(ssl, args);
- FreeKeyExchange(ssl);
-
- return ret;
-}
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#ifndef NO_CERTS
-
-#ifdef HAVE_PK_CALLBACKS
- int GetPrivateKeySigSize(WOLFSSL* ssl)
- {
- int sigSz = 0;
-
- if (ssl == NULL)
- return 0;
-
- switch (ssl->buffers.keyType) {
- #ifndef NO_RSA
- #ifdef WC_RSA_PSS
- case rsa_pss_sa_algo:
- #endif
- case rsa_sa_algo:
- sigSz = ssl->buffers.keySz;
- ssl->hsType = DYNAMIC_TYPE_RSA;
- break;
- #endif
- #ifdef HAVE_ECC
- case ecc_dsa_sa_algo:
- sigSz = wc_ecc_sig_size_calc(ssl->buffers.keySz);
- ssl->hsType = DYNAMIC_TYPE_ECC;
- break;
- #endif
- #ifdef HAVE_ED25519
- case ed25519_sa_algo:
- sigSz = ED25519_SIG_SIZE; /* fixed known value */
- ssl->hsType = DYNAMIC_TYPE_ED25519;
- break;
- #endif
- #ifdef HAVE_ED448
- case ed448_sa_algo:
- sigSz = ED448_SIG_SIZE; /* fixed known value */
- ssl->hsType = DYNAMIC_TYPE_ED448;
- break;
- #endif
- default:
- break;
- }
- return sigSz;
- }
-#endif /* HAVE_PK_CALLBACKS */
-
-#ifndef WOLFSSL_NO_TLS12
-
-#ifndef WOLFSSL_NO_CLIENT_AUTH
-typedef struct ScvArgs {
- byte* output; /* not allocated */
-#ifndef NO_RSA
- byte* verifySig;
-#endif
- byte* verify; /* not allocated */
- byte* input;
- word32 idx;
- word32 extraSz;
- word32 sigSz;
- int sendSz;
- int inputSz;
- word16 length;
- byte sigAlgo;
-} ScvArgs;
-
-static void FreeScvArgs(WOLFSSL* ssl, void* pArgs)
-{
- ScvArgs* args = (ScvArgs*)pArgs;
-
- (void)ssl;
-
-#ifndef NO_RSA
- if (args->verifySig) {
- XFREE(args->verifySig, ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- args->verifySig = NULL;
- }
-#endif
- if (args->input) {
- XFREE(args->input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- args->input = NULL;
- }
-}
-
-/* handle generation of certificate_verify (15) */
-int SendCertificateVerify(WOLFSSL* ssl)
-{
- int ret = 0;
-#ifdef WOLFSSL_ASYNC_CRYPT
- ScvArgs* args = (ScvArgs*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
-#else
- ScvArgs args[1];
-#endif
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_SEND);
- WOLFSSL_ENTER("SendCertificateVerify");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_scv;
- }
- else
-#endif
- {
- /* Reset state */
- ret = 0;
- ssl->options.asyncState = TLS_ASYNC_BEGIN;
- XMEMSET(args, 0, sizeof(ScvArgs));
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeScvArgs;
- #endif
- }
-
- switch(ssl->options.asyncState)
- {
- case TLS_ASYNC_BEGIN:
- {
- if (ssl->options.sendVerify == SEND_BLANK_CERT) {
- return 0; /* sent blank cert, can't verify */
- }
-
- args->sendSz = MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA;
- if (IsEncryptionOn(ssl, 1)) {
- args->sendSz += MAX_MSG_EXTRA;
- }
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) {
- goto exit_scv;
- }
-
- /* get output buffer */
- args->output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_BUILD;
- } /* case TLS_ASYNC_BEGIN */
- FALL_THROUGH;
-
- case TLS_ASYNC_BUILD:
- {
- ret = BuildCertHashes(ssl, &ssl->hsHashes->certHashes);
- if (ret != 0) {
- goto exit_scv;
- }
-
- if (ssl->buffers.key == NULL) {
- #ifdef HAVE_PK_CALLBACKS
- if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx))
- args->length = GetPrivateKeySigSize(ssl);
- else
- #endif
- ERROR_OUT(NO_PRIVATE_KEY, exit_scv);
- }
- else {
- /* Decode private key. */
- ret = DecodePrivateKey(ssl, &args->length);
- if (ret != 0) {
- goto exit_scv;
- }
- }
-
- if (args->length == 0) {
- ERROR_OUT(NO_PRIVATE_KEY, exit_scv);
- }
-
- /* idx is used to track verify pointer offset to output */
- args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- args->verify = &args->output[RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ];
- args->extraSz = 0; /* tls 1.2 hash/sig */
-
- /* build encoded signature buffer */
- ssl->buffers.sig.length = MAX_ENCODED_SIG_SZ;
- ssl->buffers.sig.buffer = (byte*)XMALLOC(ssl->buffers.sig.length,
- ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- if (ssl->buffers.sig.buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_scv);
- }
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- args->verify += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- }
- #endif
-
- #ifndef NO_OLD_TLS
- #ifndef NO_SHA
- /* old tls default */
- SetDigest(ssl, sha_mac);
- #endif
- #else
- #ifndef NO_SHA256
- /* new tls default */
- SetDigest(ssl, sha256_mac);
- #endif
- #endif /* !NO_OLD_TLS */
-
- if (ssl->hsType == DYNAMIC_TYPE_RSA) {
- #ifdef WC_RSA_PSS
- if (IsAtLeastTLSv1_2(ssl) &&
- (ssl->pssAlgo & (1 << ssl->suites->hashAlgo))) {
- args->sigAlgo = rsa_pss_sa_algo;
- }
- else
- #endif
- args->sigAlgo = rsa_sa_algo;
- }
- else if (ssl->hsType == DYNAMIC_TYPE_ECC)
- args->sigAlgo = ecc_dsa_sa_algo;
- else if (ssl->hsType == DYNAMIC_TYPE_ED25519)
- args->sigAlgo = ed25519_sa_algo;
- else if (ssl->hsType == DYNAMIC_TYPE_ED448)
- args->sigAlgo = ed448_sa_algo;
-
- if (IsAtLeastTLSv1_2(ssl)) {
- EncodeSigAlg(ssl->suites->hashAlgo, args->sigAlgo,
- args->verify);
- args->extraSz = HASH_SIG_SIZE;
- SetDigest(ssl, ssl->suites->hashAlgo);
- }
- #ifndef NO_OLD_TLS
- else {
- /* if old TLS load MD5 and SHA hash as value to sign */
- XMEMCPY(ssl->buffers.sig.buffer,
- (byte*)ssl->hsHashes->certHashes.md5, FINISHED_SZ);
- }
- #endif
-
- #ifndef NO_RSA
- if (args->sigAlgo == rsa_sa_algo) {
- ssl->buffers.sig.length = FINISHED_SZ;
- args->sigSz = ENCRYPT_LEN;
-
- if (IsAtLeastTLSv1_2(ssl)) {
- ssl->buffers.sig.length = wc_EncodeSignature(
- ssl->buffers.sig.buffer, ssl->buffers.digest.buffer,
- ssl->buffers.digest.length,
- TypeHash(ssl->suites->hashAlgo));
- }
-
- /* prepend hdr */
- c16toa(args->length, args->verify + args->extraSz);
- }
- #ifdef WC_RSA_PSS
- else if (args->sigAlgo == rsa_pss_sa_algo) {
- XMEMCPY(ssl->buffers.sig.buffer, ssl->buffers.digest.buffer,
- ssl->buffers.digest.length);
- ssl->buffers.sig.length = ssl->buffers.digest.length;
- args->sigSz = ENCRYPT_LEN;
-
- /* prepend hdr */
- c16toa(args->length, args->verify + args->extraSz);
- }
- #endif
- #endif /* !NO_RSA */
- #if defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)
- if (args->sigAlgo == ed25519_sa_algo) {
- ret = Ed25519CheckPubKey(ssl);
- if (ret != 0)
- goto exit_scv;
- }
- #endif /* HAVE_ED25519 && !NO_ED25519_CLIENT_AUTH */
- #if defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)
- if (args->sigAlgo == ed448_sa_algo) {
- ret = Ed448CheckPubKey(ssl);
- if (ret != 0)
- goto exit_scv;
- }
- #endif /* HAVE_ED448 && !NO_ED448_CLIENT_AUTH */
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_DO;
- } /* case TLS_ASYNC_BUILD */
- FALL_THROUGH;
-
- case TLS_ASYNC_DO:
- {
- #ifdef HAVE_ECC
- if (ssl->hsType == DYNAMIC_TYPE_ECC) {
- ecc_key* key = (ecc_key*)ssl->hsKey;
-
- ret = EccSign(ssl,
- ssl->buffers.digest.buffer, ssl->buffers.digest.length,
- ssl->buffers.sig.buffer, (word32*)&ssl->buffers.sig.length,
- key,
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.key
- #else
- NULL
- #endif
- );
- }
- #endif /* HAVE_ECC */
- #if defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)
- if (ssl->hsType == DYNAMIC_TYPE_ED25519) {
- ed25519_key* key = (ed25519_key*)ssl->hsKey;
-
- ret = Ed25519Sign(ssl,
- ssl->hsHashes->messages, ssl->hsHashes->length,
- ssl->buffers.sig.buffer, (word32*)&ssl->buffers.sig.length,
- key,
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.key
- #else
- NULL
- #endif
- );
- }
- #endif /* HAVE_ED25519 && !NO_ED25519_CLIENT_AUTH */
- #if defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)
- if (ssl->hsType == DYNAMIC_TYPE_ED448) {
- ed448_key* key = (ed448_key*)ssl->hsKey;
-
- ret = Ed448Sign(ssl,
- ssl->hsHashes->messages, ssl->hsHashes->length,
- ssl->buffers.sig.buffer, (word32*)&ssl->buffers.sig.length,
- key,
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.key
- #else
- NULL
- #endif
- );
- }
- #endif /* HAVE_ED448 && !NO_ED448_CLIENT_AUTH */
- #ifndef NO_RSA
- if (ssl->hsType == DYNAMIC_TYPE_RSA) {
- RsaKey* key = (RsaKey*)ssl->hsKey;
-
- /* restore verify pointer */
- args->verify = &args->output[args->idx];
-
- ret = RsaSign(ssl,
- ssl->buffers.sig.buffer, ssl->buffers.sig.length,
- args->verify + args->extraSz + VERIFY_HEADER, &args->sigSz,
- args->sigAlgo, ssl->suites->hashAlgo, key,
- ssl->buffers.key
- );
- }
- #endif /* !NO_RSA */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_scv;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_VERIFY;
- } /* case TLS_ASYNC_DO */
- FALL_THROUGH;
-
- case TLS_ASYNC_VERIFY:
- {
- /* restore verify pointer */
- args->verify = &args->output[args->idx];
-
- switch (ssl->hsType) {
- #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
- #ifdef HAVE_ECC
- case DYNAMIC_TYPE_ECC:
- #endif
- #ifdef HAVE_ED25519
- case DYNAMIC_TYPE_ED25519:
- #endif
- #ifdef HAVE_ED448
- case DYNAMIC_TYPE_ED448:
- #endif
- args->length = (word16)ssl->buffers.sig.length;
- /* prepend hdr */
- c16toa(args->length, args->verify + args->extraSz);
- XMEMCPY(args->verify + args->extraSz + VERIFY_HEADER,
- ssl->buffers.sig.buffer, ssl->buffers.sig.length);
- break;
- #endif
- #ifndef NO_RSA
- case DYNAMIC_TYPE_RSA:
- {
- RsaKey* key = (RsaKey*)ssl->hsKey;
-
- if (args->verifySig == NULL) {
- args->verifySig = (byte*)XMALLOC(args->sigSz, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (args->verifySig == NULL) {
- ERROR_OUT(MEMORY_E, exit_scv);
- }
- XMEMCPY(args->verifySig, args->verify + args->extraSz +
- VERIFY_HEADER, args->sigSz);
- }
-
- /* check for signature faults */
- ret = VerifyRsaSign(ssl,
- args->verifySig, args->sigSz,
- ssl->buffers.sig.buffer, ssl->buffers.sig.length,
- args->sigAlgo, ssl->suites->hashAlgo, key,
- ssl->buffers.key
- );
- break;
- }
- #endif /* !NO_RSA */
- default:
- break;
- }
-
- /* Check for error */
- if (ret != 0) {
- goto exit_scv;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_FINALIZE;
- } /* case TLS_ASYNC_VERIFY */
- FALL_THROUGH;
-
- case TLS_ASYNC_FINALIZE:
- {
- if (args->output == NULL) {
- ERROR_OUT(BUFFER_ERROR, exit_scv);
- }
- AddHeaders(args->output, (word32)args->length + args->extraSz +
- VERIFY_HEADER, certificate_verify, ssl);
-
- args->sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ +
- (word32)args->length + args->extraSz + VERIFY_HEADER;
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- }
- #endif
-
- if (IsEncryptionOn(ssl, 1)) {
- args->inputSz = args->sendSz - RECORD_HEADER_SZ;
- /* build msg adds rec hdr */
- args->input = (byte*)XMALLOC(args->inputSz, ssl->heap,
- DYNAMIC_TYPE_IN_BUFFER);
- if (args->input == NULL) {
- ERROR_OUT(MEMORY_E, exit_scv);
- }
-
- XMEMCPY(args->input, args->output + RECORD_HEADER_SZ,
- args->inputSz);
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_END;
- } /* case TLS_ASYNC_FINALIZE */
- FALL_THROUGH;
-
- case TLS_ASYNC_END:
- {
- if (IsEncryptionOn(ssl, 1)) {
- ret = BuildMessage(ssl, args->output,
- MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA,
- args->input, args->inputSz, handshake,
- 1, 0, 1);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- goto exit_scv;
- #endif
-
- XFREE(args->input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- args->input = NULL; /* make sure its not double free'd on cleanup */
-
- if (ret >= 0) {
- args->sendSz = ret;
- ret = 0;
- }
- }
- else {
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- DtlsSEQIncrement(ssl, CUR_ORDER);
- #endif
- ret = HashOutput(ssl, args->output, args->sendSz, 0);
- }
-
- if (ret != 0) {
- goto exit_scv;
- }
-
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- ret = DtlsMsgPoolSave(ssl, args->output, args->sendSz);
- }
- #endif
-
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "CertificateVerify");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "CertificateVerify", handshake,
- args->output, args->sendSz, WRITE_PROTO, ssl->heap);
- #endif
-
- ssl->buffers.outputBuffer.length += args->sendSz;
-
- if (!ssl->options.groupMessages) {
- ret = SendBuffered(ssl);
- }
- break;
- }
- default:
- ret = INPUT_CASE_ERROR;
- } /* switch(ssl->options.asyncState) */
-
-exit_scv:
-
- WOLFSSL_LEAVE("SendCertificateVerify", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_SEND);
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* Handle async operation */
- if (ret == WC_PENDING_E) {
- return ret;
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- /* Digest is not allocated, so do this to prevent free */
- ssl->buffers.digest.buffer = NULL;
- ssl->buffers.digest.length = 0;
-
- /* Final cleanup */
- FreeScvArgs(ssl, args);
- FreeKeyExchange(ssl);
-
- return ret;
-}
-#endif /* WOLFSSL_NO_CLIENT_AUTH */
-
-#endif /* WOLFSSL_NO_TLS12 */
-
-#endif /* NO_CERTS */
-
-
-#ifdef HAVE_SESSION_TICKET
-int SetTicket(WOLFSSL* ssl, const byte* ticket, word32 length)
-{
- /* Free old dynamic ticket if we already had one */
- if (ssl->session.isDynamic) {
- XFREE(ssl->session.ticket, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- ssl->session.ticket = ssl->session.staticTicket;
- ssl->session.isDynamic = 0;
- }
-
- if (length > sizeof(ssl->session.staticTicket)) {
- byte* sessionTicket =
- (byte*)XMALLOC(length, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- if (sessionTicket == NULL)
- return MEMORY_E;
- ssl->session.ticket = sessionTicket;
- ssl->session.isDynamic = 1;
- }
- ssl->session.ticketLen = (word16)length;
-
- if (length > 0) {
- XMEMCPY(ssl->session.ticket, ticket, length);
- if (ssl->session_ticket_cb != NULL) {
- ssl->session_ticket_cb(ssl,
- ssl->session.ticket, ssl->session.ticketLen,
- ssl->session_ticket_ctx);
- }
- /* Create a fake sessionID based on the ticket, this will
- * supersede the existing session cache info. */
- ssl->options.haveSessionId = 1;
-#ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3) {
- XMEMCPY(ssl->session.sessionID,
- ssl->session.ticket + length - ID_LEN, ID_LEN);
- }
- else
-#endif
- XMEMCPY(ssl->arrays->sessionID,
- ssl->session.ticket + length - ID_LEN, ID_LEN);
- }
-
- return 0;
-}
-
-#ifndef WOLFSSL_NO_TLS12
-
-/* handle processing of session_ticket (4) */
-static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- word32 size)
-{
- word32 begin = *inOutIdx;
- word32 lifetime;
- word16 length;
- int ret;
-
- if (ssl->expect_session_ticket == 0) {
- WOLFSSL_MSG("Unexpected session ticket");
- return SESSION_TICKET_EXPECT_E;
- }
-
- if (OPAQUE32_LEN > size)
- return BUFFER_ERROR;
-
- ato32(input + *inOutIdx, &lifetime);
- *inOutIdx += OPAQUE32_LEN;
-
- if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
- return BUFFER_ERROR;
-
- ato16(input + *inOutIdx, &length);
- *inOutIdx += OPAQUE16_LEN;
-
- if ((*inOutIdx - begin) + length > size)
- return BUFFER_ERROR;
-
- if ((ret = SetTicket(ssl, input + *inOutIdx, length)) != 0)
- return ret;
- *inOutIdx += length;
- if (length > 0) {
- ssl->timeout = lifetime;
-#ifndef NO_SESSION_CACHE
- AddSession(ssl);
-#endif
- }
-
- if (IsEncryptionOn(ssl, 0)) {
- *inOutIdx += ssl->keys.padSz;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- *inOutIdx += MacSize(ssl);
- #endif
- }
-
- ssl->expect_session_ticket = 0;
-
- return 0;
-}
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#endif /* HAVE_SESSION_TICKET */
-
-#endif /* NO_WOLFSSL_CLIENT */
-
-#ifdef HAVE_ECC
- /* returns the WOLFSSL_* version of the curve from the OID sum */
- word16 GetCurveByOID(int oidSum) {
- switch(oidSum) {
- #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case ECC_SECP160R1_OID:
- return WOLFSSL_ECC_SECP160R1;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_SECPR2
- case ECC_SECP160R2_OID:
- return WOLFSSL_ECC_SECP160R2;
- #endif /* HAVE_ECC_SECPR2 */
- #ifdef HAVE_ECC_KOBLITZ
- case ECC_SECP160K1_OID:
- return WOLFSSL_ECC_SECP160K1;
- #endif /* HAVE_ECC_KOBLITZ */
- #endif
- #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case ECC_SECP192R1_OID:
- return WOLFSSL_ECC_SECP192R1;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_KOBLITZ
- case ECC_SECP192K1_OID:
- return WOLFSSL_ECC_SECP192K1;
- #endif /* HAVE_ECC_KOBLITZ */
- #endif
- #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case ECC_SECP224R1_OID:
- return WOLFSSL_ECC_SECP224R1;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_KOBLITZ
- case ECC_SECP224K1_OID:
- return WOLFSSL_ECC_SECP224K1;
- #endif /* HAVE_ECC_KOBLITZ */
- #endif
- #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case ECC_SECP256R1_OID:
- return WOLFSSL_ECC_SECP256R1;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_KOBLITZ
- case ECC_SECP256K1_OID:
- return WOLFSSL_ECC_SECP256K1;
- #endif /* HAVE_ECC_KOBLITZ */
- #ifdef HAVE_ECC_BRAINPOOL
- case ECC_BRAINPOOLP256R1_OID:
- return WOLFSSL_ECC_BRAINPOOLP256R1;
- #endif /* HAVE_ECC_BRAINPOOL */
- #endif
- #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case ECC_SECP384R1_OID:
- return WOLFSSL_ECC_SECP384R1;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_BRAINPOOL
- case ECC_BRAINPOOLP384R1_OID:
- return WOLFSSL_ECC_BRAINPOOLP384R1;
- #endif /* HAVE_ECC_BRAINPOOL */
- #endif
- #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
- #ifdef HAVE_ECC_BRAINPOOL
- case ECC_BRAINPOOLP512R1_OID:
- return WOLFSSL_ECC_BRAINPOOLP512R1;
- #endif /* HAVE_ECC_BRAINPOOL */
- #endif
- #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case ECC_SECP521R1_OID:
- return WOLFSSL_ECC_SECP521R1;
- #endif /* !NO_ECC_SECP */
- #endif
- default:
- WOLFSSL_MSG("Curve OID not compiled in or implemented");
- return 0;
- }
- }
-#endif /* HAVE_ECC */
-
-
-#ifndef NO_WOLFSSL_SERVER
-
-#ifndef WOLFSSL_NO_TLS12
-
- /* handle generation of server_hello (2) */
- int SendServerHello(WOLFSSL* ssl)
- {
- int ret;
- byte *output;
- word16 length;
- word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- int sendSz;
- byte sessIdSz = ID_LEN;
- byte echoId = 0; /* ticket echo id flag */
- byte cacheOff = 0; /* session cache off flag */
-
- WOLFSSL_START(WC_FUNC_SERVER_HELLO_SEND);
- WOLFSSL_ENTER("SendServerHello");
-
- length = VERSION_SZ + RAN_LEN
- + ID_LEN + ENUM_LEN
- + SUITE_LEN
- + ENUM_LEN;
-
-#ifdef HAVE_TLS_EXTENSIONS
- ret = TLSX_GetResponseSize(ssl, server_hello, &length);
- if (ret != 0)
- return ret;
- #ifdef HAVE_SESSION_TICKET
- if (ssl->options.useTicket) {
- /* echo session id sz can be 0,32 or bogus len in between */
- sessIdSz = ssl->arrays->sessionIDSz;
- if (sessIdSz > ID_LEN) {
- WOLFSSL_MSG("Bad bogus session id len");
- return BUFFER_ERROR;
- }
- if (!IsAtLeastTLSv1_3(ssl->version))
- length -= (ID_LEN - sessIdSz); /* adjust ID_LEN assumption */
- echoId = 1;
- }
- #endif /* HAVE_SESSION_TICKET */
-#else
- if (ssl->options.haveEMS) {
- length += HELLO_EXT_SZ_SZ + HELLO_EXT_SZ;
- }
-#endif
-
- /* is the session cache off at build or runtime */
-#ifdef NO_SESSION_CACHE
- cacheOff = 1;
-#else
- if (ssl->options.sessionCacheOff == 1) {
- cacheOff = 1;
- }
-#endif
-
- /* if no session cache don't send a session ID unless we're echoing
- * an ID as part of session tickets */
- if (echoId == 0 && cacheOff == 1) {
- length -= ID_LEN; /* adjust ID_LEN assumption */
- sessIdSz = 0;
- }
-
- sendSz = length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ;
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- /* Server Hello should use the same sequence number as the
- * Client Hello. */
- ssl->keys.dtls_sequence_number_hi = ssl->keys.curSeq_hi;
- ssl->keys.dtls_sequence_number_lo = ssl->keys.curSeq_lo;
- idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- }
- #endif /* WOLFSSL_DTLS */
-
- if (IsEncryptionOn(ssl, 1))
- sendSz += MAX_MSG_EXTRA;
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHeaders(output, length, server_hello, ssl);
-
- /* now write to output */
- /* first version */
- output[idx++] = (byte)ssl->version.major;
- output[idx++] = (byte)ssl->version.minor;
-
- /* then random and session id */
- if (!ssl->options.resuming) {
- /* generate random part and session id */
- ret = wc_RNG_GenerateBlock(ssl->rng, output + idx,
- RAN_LEN + sizeof(sessIdSz) + sessIdSz);
- if (ret != 0)
- return ret;
-
-#ifdef WOLFSSL_TLS13
- if (TLSv1_3_Capable(ssl)) {
- /* TLS v1.3 capable server downgraded. */
- XMEMCPY(output + idx + RAN_LEN - (TLS13_DOWNGRADE_SZ + 1),
- tls13Downgrade, TLS13_DOWNGRADE_SZ);
- output[idx + RAN_LEN - 1] = (byte)IsAtLeastTLSv1_2(ssl);
- }
- else
-#endif
- if (ssl->ctx->method->version.major == SSLv3_MAJOR &&
- ssl->ctx->method->version.minor == TLSv1_2_MINOR &&
- !IsAtLeastTLSv1_2(ssl)) {
- /* TLS v1.2 capable server downgraded. */
- XMEMCPY(output + idx + RAN_LEN - (TLS13_DOWNGRADE_SZ + 1),
- tls13Downgrade, TLS13_DOWNGRADE_SZ);
- output[idx + RAN_LEN - 1] = 0;
- }
-
- /* store info in SSL for later */
- XMEMCPY(ssl->arrays->serverRandom, output + idx, RAN_LEN);
- idx += RAN_LEN;
- output[idx++] = sessIdSz;
- XMEMCPY(ssl->arrays->sessionID, output + idx, sessIdSz);
- ssl->arrays->sessionIDSz = sessIdSz;
- }
- else {
- /* If resuming, use info from SSL */
- XMEMCPY(output + idx, ssl->arrays->serverRandom, RAN_LEN);
- idx += RAN_LEN;
- output[idx++] = sessIdSz;
- XMEMCPY(output + idx, ssl->arrays->sessionID, sessIdSz);
- }
- idx += sessIdSz;
-
-#ifdef SHOW_SECRETS
- {
- int j;
- printf("server random: ");
- for (j = 0; j < RAN_LEN; j++)
- printf("%02x", ssl->arrays->serverRandom[j]);
- printf("\n");
- }
-#endif
-
- /* then cipher suite */
- output[idx++] = ssl->options.cipherSuite0;
- output[idx++] = ssl->options.cipherSuite;
-
- /* then compression */
- if (ssl->options.usingCompression)
- output[idx++] = ZLIB_COMPRESSION;
- else
- output[idx++] = NO_COMPRESSION;
-
- /* last, extensions */
-#ifdef HAVE_TLS_EXTENSIONS
- {
- word16 offset = 0;
- ret = TLSX_WriteResponse(ssl, output + idx, server_hello, &offset);
- if (ret != 0)
- return ret;
- idx += offset;
- }
-#else
-#ifdef HAVE_EXTENDED_MASTER
- if (ssl->options.haveEMS) {
- c16toa(HELLO_EXT_SZ, output + idx);
- idx += HELLO_EXT_SZ_SZ;
-
- c16toa(HELLO_EXT_EXTMS, output + idx);
- idx += HELLO_EXT_TYPE_SZ;
- c16toa(0, output + idx);
- /*idx += HELLO_EXT_SZ_SZ;*/
- /* idx is not used after this point. uncomment the line above
- * if adding any more extensions in the future. */
- }
-#endif
-#endif
-
- if (IsEncryptionOn(ssl, 1)) {
- byte* input;
- int inputSz = idx - RECORD_HEADER_SZ; /* build msg adds rec hdr */
-
- input = (byte*)XMALLOC(inputSz, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- if (input == NULL)
- return MEMORY_E;
-
- XMEMCPY(input, output + RECORD_HEADER_SZ, inputSz);
- sendSz = BuildMessage(ssl, output, sendSz, input, inputSz,
- handshake, 1, 0, 0);
- XFREE(input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
-
- if (sendSz < 0)
- return sendSz;
- } else {
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- DtlsSEQIncrement(ssl, CUR_ORDER);
- #endif
- ret = HashOutput(ssl, output, sendSz, 0);
- if (ret != 0)
- return ret;
- }
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "ServerHello");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "ServerHello", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- #endif
-
- ssl->options.serverState = SERVER_HELLO_COMPLETE;
- ssl->buffers.outputBuffer.length += sendSz;
-
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- if ((ret = DtlsMsgPoolSave(ssl, output, sendSz)) != 0)
- return ret;
- }
- #endif
-
- if (ssl->options.groupMessages)
- ret = 0;
- else
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendServerHello", ret);
- WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND);
-
- return ret;
- }
-
-
-#if defined(HAVE_ECC)
-
- static byte SetCurveId(ecc_key* key)
- {
- if (key == NULL || key->dp == NULL) {
- WOLFSSL_MSG("SetCurveId: Invalid key!");
- return 0;
- }
-
- return (byte)GetCurveByOID(key->dp->oidSum);
- }
-
-#endif /* HAVE_ECC */
-
- typedef struct SskeArgs {
- byte* output; /* not allocated */
- #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \
- (!defined(NO_DH) && !defined(NO_RSA))
- byte* sigDataBuf;
- #endif
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
- byte* exportBuf;
- #endif
- #ifndef NO_RSA
- byte* verifySig;
- #endif
- byte* input;
- word32 idx;
- word32 tmpSigSz;
- word32 length;
- word32 sigSz;
- #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \
- (!defined(NO_DH) && !defined(NO_RSA))
- word32 sigDataSz;
- #endif
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
- word32 exportSz;
- #endif
- #ifdef HAVE_QSH
- word32 qshSz;
- #endif
- int sendSz;
- int inputSz;
- } SskeArgs;
-
- static void FreeSskeArgs(WOLFSSL* ssl, void* pArgs)
- {
- SskeArgs* args = (SskeArgs*)pArgs;
-
- (void)ssl;
-
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
- if (args->exportBuf) {
- XFREE(args->exportBuf, ssl->heap, DYNAMIC_TYPE_DER);
- args->exportBuf = NULL;
- }
- #endif
- #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \
- (!defined(NO_DH) && !defined(NO_RSA))
- if (args->sigDataBuf) {
- XFREE(args->sigDataBuf, ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- args->sigDataBuf = NULL;
- }
- #endif
- #ifndef NO_RSA
- if (args->verifySig) {
- XFREE(args->verifySig, ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- args->verifySig = NULL;
- }
- #endif
- (void)args;
- }
-
- /* handle generation of server_key_exchange (12) */
- int SendServerKeyExchange(WOLFSSL* ssl)
- {
- int ret;
- #ifdef WOLFSSL_ASYNC_CRYPT
- SskeArgs* args = (SskeArgs*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
- #else
- SskeArgs args[1];
- #endif
-
- WOLFSSL_START(WC_FUNC_SERVER_KEY_EXCHANGE_SEND);
- WOLFSSL_ENTER("SendServerKeyExchange");
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_sske;
- }
- else
- #endif
- {
- /* Reset state */
- ret = 0;
- ssl->options.asyncState = TLS_ASYNC_BEGIN;
- XMEMSET(args, 0, sizeof(SskeArgs));
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeSskeArgs;
- #endif
- }
-
- switch(ssl->options.asyncState)
- {
- case TLS_ASYNC_BEGIN:
- {
- #ifdef HAVE_QSH
- if (ssl->peerQSHKeyPresent && ssl->options.haveQSH) {
- args->qshSz = QSH_KeyGetSize(ssl);
- }
- #endif
-
- /* Do some checks / debug msgs */
- switch(ssl->specs.kea)
- {
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- WOLFSSL_MSG("Using ephemeral ECDH PSK");
- break;
- }
- #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && !NO_PSK */
- #if defined(HAVE_ECC)
- case ecc_diffie_hellman_kea:
- {
- if (ssl->specs.static_ecdh) {
- WOLFSSL_MSG("Using Static ECDH, not sending "
- "ServerKeyExchange");
- ERROR_OUT(0, exit_sske);
- }
-
- WOLFSSL_MSG("Using ephemeral ECDH");
- break;
- }
- #endif /* HAVE_ECC */
- }
-
- /* Preparing keys */
- switch(ssl->specs.kea)
- {
- #ifndef NO_PSK
- case psk_kea:
- {
- /* Nothing to do in this sub-state */
- break;
- }
- #endif /* !NO_PSK */
- #if !defined(NO_DH) && (!defined(NO_PSK) || !defined(NO_RSA))
- #if !defined(NO_PSK)
- case dhe_psk_kea:
- #endif
- #if !defined(NO_RSA)
- case diffie_hellman_kea:
- #endif
- {
- /* Allocate DH key buffers and generate key */
- if (ssl->buffers.serverDH_P.buffer == NULL ||
- ssl->buffers.serverDH_G.buffer == NULL) {
- ERROR_OUT(NO_DH_PARAMS, exit_sske);
- }
-
- if (ssl->buffers.serverDH_Pub.buffer == NULL) {
- /* Free'd in SSL_ResourceFree and FreeHandshakeResources */
- ssl->buffers.serverDH_Pub.buffer = (byte*)XMALLOC(
- ssl->buffers.serverDH_P.length + OPAQUE16_LEN,
- ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (ssl->buffers.serverDH_Pub.buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
- }
-
- if (ssl->buffers.serverDH_Priv.buffer == NULL) {
- /* Free'd in SSL_ResourceFree and FreeHandshakeResources */
- ssl->buffers.serverDH_Priv.buffer = (byte*)XMALLOC(
- ssl->buffers.serverDH_P.length + OPAQUE16_LEN,
- ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- if (ssl->buffers.serverDH_Priv.buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
- }
-
- ssl->options.dhKeySz =
- (word16)ssl->buffers.serverDH_P.length;
-
- ret = AllocKey(ssl, DYNAMIC_TYPE_DH,
- (void**)&ssl->buffers.serverDH_Key);
- if (ret != 0) {
- goto exit_sske;
- }
-
- #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
- !defined(HAVE_FIPS) && \
- !defined(HAVE_SELFTEST)
- if (ssl->options.dhDoKeyTest &&
- !ssl->options.dhKeyTested)
- {
- ret = wc_DhSetCheckKey(
- ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length,
- ssl->buffers.serverDH_G.buffer,
- ssl->buffers.serverDH_G.length,
- NULL, 0, 0, ssl->rng);
- if (ret != 0) {
- goto exit_sske;
- }
- ssl->options.dhKeyTested = 1;
- }
- else
- #endif
- {
- ret = wc_DhSetKey(ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length,
- ssl->buffers.serverDH_G.buffer,
- ssl->buffers.serverDH_G.length);
- if (ret != 0) {
- goto exit_sske;
- }
- }
-
- ret = DhGenKeyPair(ssl, ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_Priv.buffer,
- (word32*)&ssl->buffers.serverDH_Priv.length,
- ssl->buffers.serverDH_Pub.buffer,
- (word32*)&ssl->buffers.serverDH_Pub.length);
- break;
- }
- #endif /* !NO_DH && (!NO_PSK || !NO_RSA) */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- /* Fall through to create temp ECC key */
- #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && !NO_PSK */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- /* need ephemeral key now, create it if missing */
- if (ssl->eccTempKey == NULL) {
- /* alloc/init on demand */
- ret = AllocKey(ssl, DYNAMIC_TYPE_CURVE25519,
- (void**)&ssl->eccTempKey);
- if (ret != 0) {
- goto exit_sske;
- }
- }
-
- if (ssl->eccTempKeyPresent == 0) {
- ret = X25519MakeKey(ssl,
- (curve25519_key*)ssl->eccTempKey, NULL);
- if (ret == 0 || ret == WC_PENDING_E) {
- ssl->eccTempKeyPresent =
- DYNAMIC_TYPE_CURVE25519;
- }
- }
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- /* need ephemeral key now, create it if missing */
- if (ssl->eccTempKey == NULL) {
- /* alloc/init on demand */
- ret = AllocKey(ssl, DYNAMIC_TYPE_CURVE448,
- (void**)&ssl->eccTempKey);
- if (ret != 0) {
- goto exit_sske;
- }
- }
-
- if (ssl->eccTempKeyPresent == 0) {
- ret = X448MakeKey(ssl,
- (curve448_key*)ssl->eccTempKey, NULL);
- if (ret == 0 || ret == WC_PENDING_E) {
- ssl->eccTempKeyPresent =
- DYNAMIC_TYPE_CURVE448;
- }
- }
- break;
- }
- #endif
- #ifdef HAVE_ECC
- /* need ephemeral key now, create it if missing */
- if (ssl->eccTempKey == NULL) {
- /* alloc/init on demand */
- ret = AllocKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->eccTempKey);
- if (ret != 0) {
- goto exit_sske;
- }
- }
-
- if (ssl->eccTempKeyPresent == 0) {
- ret = EccMakeKey(ssl, ssl->eccTempKey, NULL);
- if (ret == 0 || ret == WC_PENDING_E) {
- ssl->eccTempKeyPresent = DYNAMIC_TYPE_ECC;
- }
- }
- #endif
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
- default:
- /* Skip ServerKeyExchange */
- goto exit_sske;
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_sske;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_BUILD;
- } /* case TLS_ASYNC_BEGIN */
- FALL_THROUGH;
-
- case TLS_ASYNC_BUILD:
- {
- #if (!defined(NO_DH) && !defined(NO_RSA)) || (defined(HAVE_ECC) || \
- defined(HAVE_CURVE25519) || defined(HAVE_CURVE448))
- word32 preSigSz, preSigIdx;
- #endif
-
- switch(ssl->specs.kea)
- {
- #ifndef NO_PSK
- case psk_kea:
- {
- args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
-
- if (ssl->arrays->server_hint[0] == 0) {
- ERROR_OUT(0, exit_sske); /* don't send */
- }
-
- /* include size part */
- args->length = (word32)XSTRLEN(ssl->arrays->server_hint);
- if (args->length > MAX_PSK_ID_LEN) {
- ERROR_OUT(SERVER_HINT_ERROR, exit_sske);
- }
-
- args->length += HINT_LEN_SZ;
- args->sendSz = args->length + HANDSHAKE_HEADER_SZ +
- RECORD_HEADER_SZ;
-
- #ifdef HAVE_QSH
- args->length += args->qshSz;
- args->sendSz += args->qshSz;
- #endif
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- }
- #endif
-
- if (IsEncryptionOn(ssl, 1)) {
- args->sendSz += MAX_MSG_EXTRA;
- }
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) {
- goto exit_sske;
- }
-
- /* get output buffer */
- args->output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHeaders(args->output, args->length,
- server_key_exchange, ssl);
-
- /* key data */
- #ifdef HAVE_QSH
- c16toa((word16)(args->length - args->qshSz -
- HINT_LEN_SZ), args->output + args->idx);
- #else
- c16toa((word16)(args->length - HINT_LEN_SZ),
- args->output + args->idx);
- #endif
-
- args->idx += HINT_LEN_SZ;
- XMEMCPY(args->output + args->idx,
- ssl->arrays->server_hint,
- args->length - HINT_LEN_SZ);
- break;
- }
- #endif /* !NO_PSK */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- word32 hintLen;
-
- args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- args->length = LENGTH_SZ * 3 + /* p, g, pub */
- ssl->buffers.serverDH_P.length +
- ssl->buffers.serverDH_G.length +
- ssl->buffers.serverDH_Pub.length;
-
- /* include size part */
- hintLen = (word32)XSTRLEN(ssl->arrays->server_hint);
- if (hintLen > MAX_PSK_ID_LEN) {
- ERROR_OUT(SERVER_HINT_ERROR, exit_sske);
- }
- args->length += hintLen + HINT_LEN_SZ;
- args->sendSz = args->length + HANDSHAKE_HEADER_SZ +
- RECORD_HEADER_SZ;
-
- #ifdef HAVE_QSH
- args->length += args->qshSz;
- args->sendSz += args->qshSz;
- #endif
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- }
- #endif
-
- if (IsEncryptionOn(ssl, 1)) {
- args->sendSz += MAX_MSG_EXTRA;
- }
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) {
- goto exit_sske;
- }
-
- /* get output buffer */
- args->output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHeaders(args->output, args->length,
- server_key_exchange, ssl);
-
- /* key data */
- c16toa((word16)hintLen, args->output + args->idx);
- args->idx += HINT_LEN_SZ;
- XMEMCPY(args->output + args->idx,
- ssl->arrays->server_hint, hintLen);
- args->idx += hintLen;
-
- /* add p, g, pub */
- c16toa((word16)ssl->buffers.serverDH_P.length,
- args->output + args->idx);
- args->idx += LENGTH_SZ;
- XMEMCPY(args->output + args->idx,
- ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length);
- args->idx += ssl->buffers.serverDH_P.length;
-
- /* g */
- c16toa((word16)ssl->buffers.serverDH_G.length,
- args->output + args->idx);
- args->idx += LENGTH_SZ;
- XMEMCPY(args->output + args->idx,
- ssl->buffers.serverDH_G.buffer,
- ssl->buffers.serverDH_G.length);
- args->idx += ssl->buffers.serverDH_G.length;
-
- /* pub */
- c16toa((word16)ssl->buffers.serverDH_Pub.length,
- args->output + args->idx);
- args->idx += LENGTH_SZ;
- XMEMCPY(args->output + args->idx,
- ssl->buffers.serverDH_Pub.buffer,
- ssl->buffers.serverDH_Pub.length);
- /* No need to update idx, since sizes are already set */
- /* args->idx += ssl->buffers.serverDH_Pub.length; */
- break;
- }
- #endif /* !defined(NO_DH) && !defined(NO_PSK) */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- word32 hintLen;
-
- /* curve type, named curve, length(1) */
- args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- args->length = ENUM_LEN + CURVE_LEN + ENUM_LEN;
-
- args->exportSz = MAX_EXPORT_ECC_SZ;
- args->exportBuf = (byte*)XMALLOC(args->exportSz,
- ssl->heap, DYNAMIC_TYPE_DER);
- if (args->exportBuf == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- if (wc_curve25519_export_public_ex(
- (curve25519_key*)ssl->eccTempKey,
- args->exportBuf, &args->exportSz,
- EC25519_LITTLE_ENDIAN) != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_sske);
- }
- }
- else
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- if (wc_curve448_export_public_ex(
- (curve448_key*)ssl->eccTempKey,
- args->exportBuf, &args->exportSz,
- EC448_LITTLE_ENDIAN) != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_sske);
- }
- }
- else
- #endif
- {
- if (wc_ecc_export_x963(ssl->eccTempKey,
- args->exportBuf, &args->exportSz) != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_sske);
- }
- }
- args->length += args->exportSz;
-
- /* include size part */
- hintLen = (word32)XSTRLEN(ssl->arrays->server_hint);
- if (hintLen > MAX_PSK_ID_LEN) {
- ERROR_OUT(SERVER_HINT_ERROR, exit_sske);
- }
- args->length += hintLen + HINT_LEN_SZ;
- args->sendSz = args->length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ;
-
- #ifdef HAVE_QSH
- args->length += args->qshSz;
- args->sendSz += args->qshSz;
- #endif
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- }
- #endif
-
- if (IsEncryptionOn(ssl, 1)) {
- args->sendSz += MAX_MSG_EXTRA;
- }
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) {
- goto exit_sske;
- }
-
- /* get output buffer */
- args->output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* key data */
- c16toa((word16)hintLen, args->output + args->idx);
- args->idx += HINT_LEN_SZ;
- XMEMCPY(args->output + args->idx,
- ssl->arrays->server_hint, hintLen);
- args->idx += hintLen;
-
- /* ECC key exchange data */
- args->output[args->idx++] = named_curve;
- args->output[args->idx++] = 0x00; /* leading zero */
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID)
- args->output[args->idx++] = WOLFSSL_ECC_X25519;
- else
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID)
- args->output[args->idx++] = WOLFSSL_ECC_X448;
- else
- #endif
- {
- #ifdef HAVE_ECC
- args->output[args->idx++] =
- SetCurveId(ssl->eccTempKey);
- #endif
- }
- args->output[args->idx++] = (byte)args->exportSz;
- XMEMCPY(args->output + args->idx, args->exportBuf,
- args->exportSz);
- break;
- }
- #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && !NO_PSK */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- enum wc_HashType hashType;
-
- /* curve type, named curve, length(1) */
- args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- args->length = ENUM_LEN + CURVE_LEN + ENUM_LEN;
-
- /* Export temp ECC key and add to length */
- args->exportSz = MAX_EXPORT_ECC_SZ;
- args->exportBuf = (byte*)XMALLOC(args->exportSz,
- ssl->heap, DYNAMIC_TYPE_DER);
- if (args->exportBuf == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- if (wc_curve25519_export_public_ex(
- (curve25519_key*)ssl->eccTempKey,
- args->exportBuf, &args->exportSz,
- EC25519_LITTLE_ENDIAN) != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_sske);
- }
- }
- else
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- if (wc_curve448_export_public_ex(
- (curve448_key*)ssl->eccTempKey,
- args->exportBuf, &args->exportSz,
- EC448_LITTLE_ENDIAN) != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_sske);
- }
- }
- else
- #endif
- {
- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
- if (wc_ecc_export_x963(ssl->eccTempKey,
- args->exportBuf, &args->exportSz) != 0) {
- ERROR_OUT(ECC_EXPORT_ERROR, exit_sske);
- }
- #endif
- }
- args->length += args->exportSz;
-
- preSigSz = args->length;
- preSigIdx = args->idx;
-
- if (ssl->buffers.key == NULL) {
- #ifdef HAVE_PK_CALLBACKS
- if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) {
- args->tmpSigSz = GetPrivateKeySigSize(ssl);
- if (args->tmpSigSz == 0) {
- ERROR_OUT(NO_PRIVATE_KEY, exit_sske);
- }
- }
- else
- #endif
- ERROR_OUT(NO_PRIVATE_KEY, exit_sske);
- }
- else {
- switch(ssl->suites->sigAlgo) {
- #ifndef NO_RSA
- #ifdef WC_RSA_PSS
- case rsa_pss_sa_algo:
- #endif
- case rsa_sa_algo:
- {
- word16 keySz;
-
- ssl->buffers.keyType = rsa_sa_algo;
- ret = DecodePrivateKey(ssl, &keySz);
- if (ret != 0) {
- goto exit_sske;
- }
-
- args->tmpSigSz = (word32)keySz;
- break;
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ecc_dsa_sa_algo:
- {
- word16 keySz;
-
- ssl->buffers.keyType = ecc_dsa_sa_algo;
- ret = DecodePrivateKey(ssl, &keySz);
- if (ret != 0) {
- goto exit_sske;
- }
- /* worst case estimate */
- args->tmpSigSz = keySz;
- break;
- }
- #endif
- #ifdef HAVE_ED25519
- case ed25519_sa_algo:
- {
- word16 keySz;
-
- ssl->buffers.keyType = ed25519_sa_algo;
- ret = DecodePrivateKey(ssl, &keySz);
- if (ret != 0) {
- goto exit_sske;
- }
-
- /* worst case estimate */
- args->tmpSigSz = ED25519_SIG_SIZE;
- break;
- }
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- case ed448_sa_algo:
- {
- word16 keySz;
-
- ssl->buffers.keyType = ed448_sa_algo;
- ret = DecodePrivateKey(ssl, &keySz);
- if (ret != 0) {
- goto exit_sske;
- }
-
- /* worst case estimate */
- args->tmpSigSz = ED448_SIG_SIZE;
- break;
- }
- #endif /* HAVE_ED448 */
- default:
- ERROR_OUT(ALGO_ID_E, exit_sske); /* unsupported type */
- } /* switch(ssl->specs.sig_algo) */
- }
-
- /* sig length */
- args->length += LENGTH_SZ;
- args->length += args->tmpSigSz;
-
- if (IsAtLeastTLSv1_2(ssl)) {
- args->length += HASH_SIG_SIZE;
- }
-
- args->sendSz = args->length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ;
-
- #ifdef HAVE_QSH
- args->length += args->qshSz;
- args->sendSz += args->qshSz;
- #endif
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- preSigIdx = args->idx;
- }
- #endif
- if (IsEncryptionOn(ssl, 1)) {
- args->sendSz += MAX_MSG_EXTRA;
- }
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) {
- goto exit_sske;
- }
-
- /* get output buffer */
- args->output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* record and message headers will be added below, when we're sure
- of the sig length */
-
- /* key exchange data */
- args->output[args->idx++] = named_curve;
- args->output[args->idx++] = 0x00; /* leading zero */
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID)
- args->output[args->idx++] = WOLFSSL_ECC_X25519;
- else
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID)
- args->output[args->idx++] = WOLFSSL_ECC_X448;
- else
- #endif
- {
- #ifdef HAVE_ECC
- args->output[args->idx++] =
- SetCurveId(ssl->eccTempKey);
- #endif
- }
- args->output[args->idx++] = (byte)args->exportSz;
- XMEMCPY(args->output + args->idx, args->exportBuf, args->exportSz);
- args->idx += args->exportSz;
-
- /* Determine hash type */
- if (IsAtLeastTLSv1_2(ssl)) {
- EncodeSigAlg(ssl->suites->hashAlgo,
- ssl->suites->sigAlgo,
- &args->output[args->idx]);
- args->idx += 2;
-
- hashType = HashAlgoToType(ssl->suites->hashAlgo);
- if (hashType == WC_HASH_TYPE_NONE) {
- ERROR_OUT(ALGO_ID_E, exit_sske);
- }
-
- } else {
- /* only using sha and md5 for rsa */
- #ifndef NO_OLD_TLS
- hashType = WC_HASH_TYPE_SHA;
- if (ssl->suites->sigAlgo == rsa_sa_algo) {
- hashType = WC_HASH_TYPE_MD5_SHA;
- }
- #else
- ERROR_OUT(ALGO_ID_E, exit_sske);
- #endif
- }
-
- /* Signature length will be written later, when we're sure what it is */
-
- #ifdef HAVE_FUZZER
- if (ssl->fuzzerCb) {
- ssl->fuzzerCb(ssl, args->output + preSigIdx,
- preSigSz, FUZZ_SIGNATURE, ssl->fuzzerCtx);
- }
- #endif
-
- /* Assemble buffer to hash for signature */
- args->sigDataSz = RAN_LEN + RAN_LEN + preSigSz;
- args->sigDataBuf = (byte*)XMALLOC(args->sigDataSz,
- ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- if (args->sigDataBuf == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
- XMEMCPY(args->sigDataBuf, ssl->arrays->clientRandom,
- RAN_LEN);
- XMEMCPY(args->sigDataBuf+RAN_LEN,
- ssl->arrays->serverRandom, RAN_LEN);
- XMEMCPY(args->sigDataBuf+RAN_LEN+RAN_LEN,
- args->output + preSigIdx, preSigSz);
-
- if (ssl->suites->sigAlgo != ed25519_sa_algo &&
- ssl->suites->sigAlgo != ed448_sa_algo) {
- ssl->buffers.sig.length =
- wc_HashGetDigestSize(hashType);
- if ((int)ssl->buffers.sig.length < 0) {
- ERROR_OUT(HASH_TYPE_E, exit_sske);
- }
- ssl->buffers.sig.buffer = (byte*)XMALLOC(
- ssl->buffers.sig.length,
- ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- if (ssl->buffers.sig.buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
-
- /* Perform hash */
- ret = wc_Hash(hashType, args->sigDataBuf,
- args->sigDataSz,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length);
- if (ret != 0) {
- goto exit_sske;
- }
- }
-
- args->sigSz = args->tmpSigSz;
-
- /* Sign hash to create signature */
- switch (ssl->suites->sigAlgo)
- {
- #ifndef NO_RSA
- case rsa_sa_algo:
- {
- /* For TLS 1.2 re-encode signature */
- if (IsAtLeastTLSv1_2(ssl)) {
- byte* encodedSig = (byte*)XMALLOC(
- MAX_ENCODED_SIG_SZ, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (encodedSig == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
-
- ssl->buffers.sig.length =
- wc_EncodeSignature(encodedSig,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length,
- TypeHash(ssl->suites->hashAlgo));
-
- /* Replace sig buffer with new one */
- XFREE(ssl->buffers.sig.buffer, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- ssl->buffers.sig.buffer = encodedSig;
- }
-
- /* write sig size here */
- c16toa((word16)args->sigSz,
- args->output + args->idx);
- args->idx += LENGTH_SZ;
- break;
- }
- #ifdef WC_RSA_PSS
- case rsa_pss_sa_algo:
- /* write sig size here */
- c16toa((word16)args->sigSz,
- args->output + args->idx);
- args->idx += LENGTH_SZ;
- break;
- #endif
- #endif /* !NO_RSA */
- case ecc_dsa_sa_algo:
- {
- break;
- }
- #ifdef HAVE_ED25519
- case ed25519_sa_algo:
- ret = Ed25519CheckPubKey(ssl);
- if (ret != 0)
- goto exit_sske;
- break;
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- case ed448_sa_algo:
- ret = Ed448CheckPubKey(ssl);
- if (ret != 0)
- goto exit_sske;
- break;
- #endif /* HAVE_ED448 */
- } /* switch(ssl->specs.sig_algo) */
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
- #if !defined(NO_DH) && !defined(NO_RSA)
- case diffie_hellman_kea:
- {
- enum wc_HashType hashType;
-
- args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- args->length = LENGTH_SZ * 3; /* p, g, pub */
- args->length += ssl->buffers.serverDH_P.length +
- ssl->buffers.serverDH_G.length +
- ssl->buffers.serverDH_Pub.length;
-
- preSigIdx = args->idx;
- preSigSz = args->length;
-
- if (!ssl->options.usingAnon_cipher) {
- word16 keySz;
-
- /* sig length */
- args->length += LENGTH_SZ;
-
- if (ssl->buffers.key == NULL) {
- #ifdef HAVE_PK_CALLBACKS
- if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx))
- keySz = (word32)GetPrivateKeySigSize(ssl);
- else
- #endif
- ERROR_OUT(NO_PRIVATE_KEY, exit_sske);
- }
- else
- {
- if (ssl->buffers.keyType == 0)
- ssl->buffers.keyType = rsa_sa_algo;
- ret = DecodePrivateKey(ssl, &keySz);
- if (ret != 0) {
- goto exit_sske;
- }
- }
-
- if (keySz == 0) { /* test if keySz has error */
- ERROR_OUT(keySz, exit_sske);
- }
-
- args->tmpSigSz = (word32)keySz;
- args->length += args->tmpSigSz;
-
- if (IsAtLeastTLSv1_2(ssl)) {
- args->length += HASH_SIG_SIZE;
- }
- }
-
- args->sendSz = args->length + HANDSHAKE_HEADER_SZ +
- RECORD_HEADER_SZ;
-
- #ifdef HAVE_QSH
- args->length += args->qshSz;
- args->sendSz += args->qshSz;
- #endif
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- args->sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- args->idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- preSigIdx = args->idx;
- }
- #endif
-
- if (IsEncryptionOn(ssl, 1)) {
- args->sendSz += MAX_MSG_EXTRA;
- }
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) {
- goto exit_sske;
- }
-
- /* get output buffer */
- args->output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHeaders(args->output, args->length,
- server_key_exchange, ssl);
-
- /* add p, g, pub */
- c16toa((word16)ssl->buffers.serverDH_P.length,
- args->output + args->idx);
- args->idx += LENGTH_SZ;
- XMEMCPY(args->output + args->idx,
- ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length);
- args->idx += ssl->buffers.serverDH_P.length;
-
- /* g */
- c16toa((word16)ssl->buffers.serverDH_G.length,
- args->output + args->idx);
- args->idx += LENGTH_SZ;
- XMEMCPY(args->output + args->idx,
- ssl->buffers.serverDH_G.buffer,
- ssl->buffers.serverDH_G.length);
- args->idx += ssl->buffers.serverDH_G.length;
-
- /* pub */
- c16toa((word16)ssl->buffers.serverDH_Pub.length,
- args->output + args->idx);
- args->idx += LENGTH_SZ;
- XMEMCPY(args->output + args->idx,
- ssl->buffers.serverDH_Pub.buffer,
- ssl->buffers.serverDH_Pub.length);
- args->idx += ssl->buffers.serverDH_Pub.length;
-
- #ifdef HAVE_FUZZER
- if (ssl->fuzzerCb) {
- ssl->fuzzerCb(ssl, args->output + preSigIdx,
- preSigSz, FUZZ_SIGNATURE, ssl->fuzzerCtx);
- }
- #endif
-
- if (ssl->options.usingAnon_cipher) {
- break;
- }
-
- /* Determine hash type */
- if (IsAtLeastTLSv1_2(ssl)) {
- EncodeSigAlg(ssl->suites->hashAlgo,
- ssl->suites->sigAlgo,
- &args->output[args->idx]);
- args->idx += 2;
-
- hashType = HashAlgoToType(ssl->suites->hashAlgo);
- if (hashType == WC_HASH_TYPE_NONE) {
- ERROR_OUT(ALGO_ID_E, exit_sske);
- }
- } else {
- /* only using sha and md5 for rsa */
- #ifndef NO_OLD_TLS
- hashType = WC_HASH_TYPE_SHA;
- if (ssl->suites->sigAlgo == rsa_sa_algo) {
- hashType = WC_HASH_TYPE_MD5_SHA;
- }
- #else
- ERROR_OUT(ALGO_ID_E, exit_sske);
- #endif
- }
-
- /* signature size */
- c16toa((word16)args->tmpSigSz, args->output + args->idx);
- args->idx += LENGTH_SZ;
-
- /* Assemble buffer to hash for signature */
- args->sigDataSz = RAN_LEN + RAN_LEN + preSigSz;
- args->sigDataBuf = (byte*)XMALLOC(args->sigDataSz,
- ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- if (args->sigDataBuf == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
- XMEMCPY(args->sigDataBuf, ssl->arrays->clientRandom,
- RAN_LEN);
- XMEMCPY(args->sigDataBuf+RAN_LEN,
- ssl->arrays->serverRandom, RAN_LEN);
- XMEMCPY(args->sigDataBuf+RAN_LEN+RAN_LEN,
- args->output + preSigIdx, preSigSz);
-
- if (ssl->suites->sigAlgo != ed25519_sa_algo &&
- ssl->suites->sigAlgo != ed448_sa_algo) {
- ssl->buffers.sig.length =
- wc_HashGetDigestSize(hashType);
- ssl->buffers.sig.buffer = (byte*)XMALLOC(
- ssl->buffers.sig.length, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (ssl->buffers.sig.buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
-
- /* Perform hash */
- ret = wc_Hash(hashType, args->sigDataBuf,
- args->sigDataSz,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length);
- if (ret != 0) {
- goto exit_sske;
- }
- }
-
- args->sigSz = args->tmpSigSz;
-
- /* Sign hash to create signature */
- switch (ssl->suites->sigAlgo)
- {
- #ifndef NO_RSA
- case rsa_sa_algo:
- {
- /* For TLS 1.2 re-encode signature */
- if (IsAtLeastTLSv1_2(ssl)) {
- byte* encodedSig = (byte*)XMALLOC(
- MAX_ENCODED_SIG_SZ, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (encodedSig == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
-
- ssl->buffers.sig.length =
- wc_EncodeSignature(encodedSig,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length,
- TypeHash(ssl->suites->hashAlgo));
-
- /* Replace sig buffer with new one */
- XFREE(ssl->buffers.sig.buffer, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- ssl->buffers.sig.buffer = encodedSig;
- }
- break;
- }
- #endif /* NO_RSA */
- } /* switch (ssl->suites->sigAlgo) */
- break;
- }
- #endif /* !defined(NO_DH) && !defined(NO_RSA) */
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_sske;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_DO;
- } /* case TLS_ASYNC_BUILD */
- FALL_THROUGH;
-
- case TLS_ASYNC_DO:
- {
- switch(ssl->specs.kea)
- {
- #ifndef NO_PSK
- case psk_kea:
- {
- break;
- }
- #endif /* !NO_PSK */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- break;
- }
- #endif /* !defined(NO_DH) && !defined(NO_PSK) */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- break;
- }
- #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && !NO_PSK */
- #if defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)
- case ecc_diffie_hellman_kea:
- {
- /* Sign hash to create signature */
- switch (ssl->suites->sigAlgo)
- {
- #ifndef NO_RSA
- #ifdef WC_RSA_PSS
- case rsa_pss_sa_algo:
- #endif
- case rsa_sa_algo:
- {
- RsaKey* key = (RsaKey*)ssl->hsKey;
-
- ret = RsaSign(ssl,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length,
- args->output + args->idx,
- &args->sigSz,
- ssl->suites->sigAlgo, ssl->suites->hashAlgo,
- key,
- ssl->buffers.key
- );
- break;
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ecc_dsa_sa_algo:
- {
- ecc_key* key = (ecc_key*)ssl->hsKey;
-
- ret = EccSign(ssl,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length,
- args->output + LENGTH_SZ + args->idx,
- &args->sigSz,
- key,
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.key
- #else
- NULL
- #endif
- );
- break;
- }
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ed25519_sa_algo:
- {
- ed25519_key* key = (ed25519_key*)ssl->hsKey;
-
- ret = Ed25519Sign(ssl,
- args->sigDataBuf, args->sigDataSz,
- args->output + LENGTH_SZ + args->idx,
- &args->sigSz,
- key,
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.key
- #else
- NULL
- #endif
- );
- break;
- }
- #endif
- #ifdef HAVE_ED448
- case ed448_sa_algo:
- {
- ed448_key* key = (ed448_key*)ssl->hsKey;
-
- ret = Ed448Sign(ssl,
- args->sigDataBuf, args->sigDataSz,
- args->output + LENGTH_SZ + args->idx,
- &args->sigSz,
- key,
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.key
- #else
- NULL
- #endif
- );
- break;
- }
- #endif
- } /* switch(ssl->specs.sig_algo) */
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
- #if !defined(NO_DH) && !defined(NO_RSA)
- case diffie_hellman_kea:
- {
- /* Sign hash to create signature */
- switch (ssl->suites->sigAlgo)
- {
- #ifndef NO_RSA
- #ifdef WC_RSA_PSS
- case rsa_pss_sa_algo:
- #endif
- case rsa_sa_algo:
- {
- RsaKey* key = (RsaKey*)ssl->hsKey;
-
- if (ssl->options.usingAnon_cipher) {
- break;
- }
-
- ret = RsaSign(ssl,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length,
- args->output + args->idx,
- &args->sigSz,
- ssl->suites->sigAlgo, ssl->suites->hashAlgo,
- key,
- ssl->buffers.key
- );
- break;
- }
- #endif /* NO_RSA */
- } /* switch (ssl->suites->sigAlgo) */
-
- break;
- }
- #endif /* !defined(NO_DH) && !defined(NO_RSA) */
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_sske;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_VERIFY;
- } /* case TLS_ASYNC_DO */
- FALL_THROUGH;
-
- case TLS_ASYNC_VERIFY:
- {
- switch(ssl->specs.kea)
- {
- #ifndef NO_PSK
- case psk_kea:
- {
- /* Nothing to do in this sub-state */
- break;
- }
- #endif /* !NO_PSK */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- /* Nothing to do in this sub-state */
- break;
- }
- #endif /* !defined(NO_DH) && !defined(NO_PSK) */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- /* Nothing to do in this sub-state */
- break;
- }
- #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && !NO_PSK */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- switch(ssl->suites->sigAlgo)
- {
- #ifndef NO_RSA
- #ifdef WC_RSA_PSS
- case rsa_pss_sa_algo:
- #endif
- case rsa_sa_algo:
- {
- RsaKey* key = (RsaKey*)ssl->hsKey;
-
- if (args->verifySig == NULL) {
- if (args->sigSz == 0) {
- ERROR_OUT(BAD_COND_E, exit_sske);
- }
- args->verifySig = (byte*)XMALLOC(
- args->sigSz, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (!args->verifySig) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
- XMEMCPY(args->verifySig,
- args->output + args->idx, args->sigSz);
- }
-
- /* check for signature faults */
- ret = VerifyRsaSign(ssl,
- args->verifySig, args->sigSz,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length,
- ssl->suites->sigAlgo, ssl->suites->hashAlgo,
- key, ssl->buffers.key
- );
- break;
- }
- #endif
- case ecc_dsa_sa_algo:
- #ifdef HAVE_ED25519
- case ed25519_sa_algo:
- #endif
- #ifdef HAVE_ED448
- case ed448_sa_algo:
- #endif
- {
- /* Now that we know the real sig size, write it. */
- c16toa((word16)args->sigSz,
- args->output + args->idx);
-
- /* And adjust length and sendSz from estimates */
- args->length += args->sigSz - args->tmpSigSz;
- args->sendSz += args->sigSz - args->tmpSigSz;
- break;
- }
- default:
- ERROR_OUT(ALGO_ID_E, exit_sske); /* unsupported type */
- } /* switch(ssl->specs.sig_algo) */
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
- #if !defined(NO_DH) && !defined(NO_RSA)
- case diffie_hellman_kea:
- {
- switch (ssl->suites->sigAlgo)
- {
- #ifndef NO_RSA
- #ifndef WC_RSA_PSS
- case rsa_pss_sa_algo:
- #endif
- case rsa_sa_algo:
- {
- RsaKey* key = (RsaKey*)ssl->hsKey;
-
- if (ssl->options.usingAnon_cipher) {
- break;
- }
-
- if (args->verifySig == NULL) {
- if (args->sigSz == 0) {
- ERROR_OUT(BAD_COND_E, exit_sske);
- }
- args->verifySig = (byte*)XMALLOC(
- args->sigSz, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (!args->verifySig) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
- XMEMCPY(args->verifySig,
- args->output + args->idx, args->sigSz);
- }
-
- /* check for signature faults */
- ret = VerifyRsaSign(ssl,
- args->verifySig, args->sigSz,
- ssl->buffers.sig.buffer,
- ssl->buffers.sig.length,
- ssl->suites->sigAlgo, ssl->suites->hashAlgo,
- key, ssl->buffers.key
- );
- break;
- }
- #endif
- } /* switch (ssl->suites->sigAlgo) */
- break;
- }
- #endif /* !defined(NO_DH) && !defined(NO_RSA) */
- } /* switch(ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_sske;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_FINALIZE;
- } /* case TLS_ASYNC_VERIFY */
- FALL_THROUGH;
-
- case TLS_ASYNC_FINALIZE:
- {
- #ifdef HAVE_QSH
- if (ssl->peerQSHKeyPresent) {
- if (args->qshSz > 0) {
- args->idx = args->sendSz - args->qshSz;
- if (QSH_KeyExchangeWrite(ssl, 1) != 0) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
-
- /* extension type */
- c16toa(TLSX_QUANTUM_SAFE_HYBRID,
- args->output + args->idx);
- args->idx += OPAQUE16_LEN;
-
- /* write to output and check amount written */
- if (TLSX_QSHPK_Write(ssl->QSH_secret->list,
- args->output + args->idx) >
- args->qshSz - OPAQUE16_LEN) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
- }
- }
- #endif
-
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- if (ssl->specs.kea == ecdhe_psk_kea ||
- ssl->specs.kea == ecc_diffie_hellman_kea) {
- /* Check output to make sure it was set */
- if (args->output) {
- AddHeaders(args->output, args->length,
- server_key_exchange, ssl);
- }
- else {
- ERROR_OUT(BUFFER_ERROR, exit_sske);
- }
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-
- if (IsEncryptionOn(ssl, 1)) {
- args->inputSz = args->length + HANDSHAKE_HEADER_SZ;
- /* buildmsg adds rechdr */
- args->input = (byte*)XMALLOC(args->inputSz, ssl->heap,
- DYNAMIC_TYPE_IN_BUFFER);
- if (args->input == NULL) {
- ERROR_OUT(MEMORY_E, exit_sske);
- }
-
- if (args->output == NULL) {
- ERROR_OUT(BUFFER_ERROR, exit_sske);
- }
-
- XMEMCPY(args->input, args->output + RECORD_HEADER_SZ,
- args->inputSz);
- ret = BuildMessage(ssl, args->output, args->sendSz,
- args->input, args->inputSz, handshake, 1, 0, 0);
- XFREE(args->input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- args->input = NULL;
- /* make sure its not double free'd on cleanup */
-
- if (ret >= 0) {
- args->sendSz = ret;
- ret = 0;
- }
- }
- else {
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- if ((ret = DtlsMsgPoolSave(ssl,
- args->output, args->sendSz)) != 0) {
- goto exit_sske;
- }
- }
-
- if (ssl->options.dtls)
- DtlsSEQIncrement(ssl, CUR_ORDER);
- #endif
-
- ret = HashOutput(ssl, args->output, args->sendSz, 0);
- if (ret != 0) {
- goto exit_sske;
- }
- }
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn) {
- AddPacketName(ssl, "ServerKeyExchange");
- }
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "ServerKeyExchange", handshake,
- args->output, args->sendSz, WRITE_PROTO, ssl->heap);
- }
- #endif
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_END;
- } /* case TLS_ASYNC_FINALIZE */
- FALL_THROUGH;
-
- case TLS_ASYNC_END:
- {
- ssl->buffers.outputBuffer.length += args->sendSz;
- if (!ssl->options.groupMessages) {
- ret = SendBuffered(ssl);
- }
-
- ssl->options.serverState = SERVER_KEYEXCHANGE_COMPLETE;
- break;
- }
- default:
- ret = INPUT_CASE_ERROR;
- } /* switch(ssl->options.asyncState) */
-
- exit_sske:
-
- WOLFSSL_LEAVE("SendServerKeyExchange", ret);
- WOLFSSL_END(WC_FUNC_SERVER_KEY_EXCHANGE_SEND);
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* Handle async operation */
- if (ret == WC_PENDING_E)
- return ret;
- #endif /* WOLFSSL_ASYNC_CRYPT */
-
- /* Final cleanup */
- FreeSskeArgs(ssl, args);
- FreeKeyExchange(ssl);
-
- return ret;
- }
-
-#if defined(HAVE_SERVER_RENEGOTIATION_INFO) || defined(HAVE_FALLBACK_SCSV) || \
- defined(OPENSSL_ALL)
-
- /* search suites for specific one, idx on success, negative on error */
-#ifndef WOLFSSL_TLS13
- static
-#endif
- int FindSuite(Suites* suites, byte first, byte second)
- {
- int i;
-
- if (suites == NULL || suites->suiteSz == 0) {
- WOLFSSL_MSG("Suites pointer error or suiteSz 0");
- return SUITES_ERROR;
- }
-
- for (i = 0; i < suites->suiteSz-1; i += SUITE_LEN) {
- if (suites->suites[i] == first &&
- suites->suites[i+1] == second )
- return i;
- }
-
- return MATCH_SUITE_ERROR;
- }
-
-#endif
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
- /* Make sure server cert/key are valid for this suite, true on success */
- static int VerifyServerSuite(WOLFSSL* ssl, word16 idx)
- {
- int haveRSA = !ssl->options.haveStaticECC;
- int havePSK = 0;
- byte first;
- byte second;
-
- WOLFSSL_ENTER("VerifyServerSuite");
-
- if (ssl->suites == NULL) {
- WOLFSSL_MSG("Suites pointer error");
- return 0;
- }
-
- first = ssl->suites->suites[idx];
- second = ssl->suites->suites[idx+1];
-
- #ifndef NO_PSK
- havePSK = ssl->options.havePSK;
- #endif
-
- if (ssl->options.haveNTRU)
- haveRSA = 0;
-
- if (CipherRequires(first, second, REQUIRES_RSA)) {
- WOLFSSL_MSG("Requires RSA");
- if (haveRSA == 0) {
- WOLFSSL_MSG("Don't have RSA");
- return 0;
- }
- }
-
- if (CipherRequires(first, second, REQUIRES_DHE)) {
- WOLFSSL_MSG("Requires DHE");
- if (ssl->options.haveDH == 0) {
- WOLFSSL_MSG("Don't have DHE");
- return 0;
- }
- }
-
- if (CipherRequires(first, second, REQUIRES_ECC)) {
- WOLFSSL_MSG("Requires ECC");
- if (ssl->options.haveECC == 0) {
- WOLFSSL_MSG("Don't have ECC");
- return 0;
- }
- }
-
- if (CipherRequires(first, second, REQUIRES_ECC_STATIC)) {
- WOLFSSL_MSG("Requires static ECC");
- if (ssl->options.haveStaticECC == 0) {
- WOLFSSL_MSG("Don't have static ECC");
- return 0;
- }
- }
-
- if (CipherRequires(first, second, REQUIRES_PSK)) {
- WOLFSSL_MSG("Requires PSK");
- if (havePSK == 0) {
- WOLFSSL_MSG("Don't have PSK");
- return 0;
- }
- }
-
- if (CipherRequires(first, second, REQUIRES_NTRU)) {
- WOLFSSL_MSG("Requires NTRU");
- if (ssl->options.haveNTRU == 0) {
- WOLFSSL_MSG("Don't have NTRU");
- return 0;
- }
- }
-
- if (CipherRequires(first, second, REQUIRES_RSA_SIG)) {
- WOLFSSL_MSG("Requires RSA Signature");
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- ssl->options.haveECDSAsig == 1) {
- WOLFSSL_MSG("Don't have RSA Signature");
- return 0;
- }
- }
-
-#if !defined(WOLFSSL_OLDTLS_AEAD_CIPHERSUITES)
- if (CipherRequires(first, second, REQUIRES_AEAD)) {
- WOLFSSL_MSG("Requires AEAD");
- if (ssl->version.major == SSLv3_MAJOR &&
- ssl->version.minor < TLSv1_2_MINOR) {
- WOLFSSL_MSG("Version of SSL does not support AEAD ciphers");
- return 0;
- }
-
- }
-#endif
-
-#if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES)
- if (!TLSX_ValidateSupportedCurves(ssl, first, second)) {
- WOLFSSL_MSG("Don't have matching curves");
- return 0;
- }
-#endif
-
- /* ECCDHE is always supported if ECC on */
-
-#ifdef HAVE_QSH
- /* need to negotiate a classic suite in addition to TLS_QSH */
- if (first == QSH_BYTE && second == TLS_QSH) {
- if (TLSX_SupportExtensions(ssl)) {
- ssl->options.haveQSH = 1; /* matched TLS_QSH */
- }
- else {
- WOLFSSL_MSG("Version of SSL connection does not support "
- "TLS_QSH");
- }
- return 0;
- }
-#endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) &&
- ssl->options.side == WOLFSSL_SERVER_END) {
- /* Try to establish a key share. */
- int ret = TLSX_KeyShare_Establish(ssl);
- if (ret == KEY_SHARE_ERROR)
- ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE;
- else if (ret != 0)
- return 0;
- }
- else if (first == TLS13_BYTE || (first == ECC_BYTE &&
- (second == TLS_SHA256_SHA256 || second == TLS_SHA384_SHA384))) {
- /* Can't negotiate TLS 1.3 cipher suites with lower protocol
- * version. */
- return 0;
- }
-#endif
-
- return 1;
- }
-
-#ifndef NO_WOLFSSL_SERVER
- static int CompareSuites(WOLFSSL* ssl, Suites* peerSuites, word16 i,
- word16 j)
- {
- if (ssl->suites->suites[i] == peerSuites->suites[j] &&
- ssl->suites->suites[i+1] == peerSuites->suites[j+1] ) {
-
- if (VerifyServerSuite(ssl, i)) {
- int result;
- WOLFSSL_MSG("Verified suite validity");
- ssl->options.cipherSuite0 = ssl->suites->suites[i];
- ssl->options.cipherSuite = ssl->suites->suites[i+1];
- result = SetCipherSpecs(ssl);
- if (result == 0) {
- result = PickHashSigAlgo(ssl, peerSuites->hashSigAlgo,
- peerSuites->hashSigAlgoSz);
- }
- return result;
- }
- else {
- WOLFSSL_MSG("Could not verify suite validity, continue");
- }
- }
-
- return MATCH_SUITE_ERROR;
- }
-
- int MatchSuite(WOLFSSL* ssl, Suites* peerSuites)
- {
- int ret;
- word16 i, j;
-
- WOLFSSL_ENTER("MatchSuite");
-
- /* & 0x1 equivalent % 2 */
- if (peerSuites->suiteSz == 0 || peerSuites->suiteSz & 0x1)
- return BUFFER_ERROR;
-
- if (ssl->suites == NULL)
- return SUITES_ERROR;
-
- if (!ssl->options.useClientOrder) {
- /* Server order */
- for (i = 0; i < ssl->suites->suiteSz; i += 2) {
- for (j = 0; j < peerSuites->suiteSz; j += 2) {
- ret = CompareSuites(ssl, peerSuites, i, j);
- if (ret != MATCH_SUITE_ERROR)
- return ret;
- }
- }
- }
- else {
- /* Client order */
- for (j = 0; j < peerSuites->suiteSz; j += 2) {
- for (i = 0; i < ssl->suites->suiteSz; i += 2) {
- ret = CompareSuites(ssl, peerSuites, i, j);
- if (ret != MATCH_SUITE_ERROR)
- return ret;
- }
- }
- }
-
- return MATCH_SUITE_ERROR;
- }
-#endif
-
-#ifdef OLD_HELLO_ALLOWED
-
- /* process old style client hello, deprecate? */
- int ProcessOldClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- word32 inSz, word16 sz)
- {
- word32 idx = *inOutIdx;
- word16 sessionSz;
- word16 randomSz;
- word16 i, j;
- ProtocolVersion pv;
- Suites clSuites;
- int ret = -1;
-
- (void)inSz;
- WOLFSSL_MSG("Got old format client hello");
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "ClientHello");
- if (ssl->toInfoOn)
- AddLateName("ClientHello", &ssl->timeoutInfo);
-#endif
-
- /* manually hash input since different format */
-#ifndef NO_OLD_TLS
-#ifndef NO_MD5
- wc_Md5Update(&ssl->hsHashes->hashMd5, input + idx, sz);
-#endif
-#ifndef NO_SHA
- wc_ShaUpdate(&ssl->hsHashes->hashSha, input + idx, sz);
-#endif
-#endif
-#ifndef NO_SHA256
- if (IsAtLeastTLSv1_2(ssl)) {
- int shaRet = wc_Sha256Update(&ssl->hsHashes->hashSha256,
- input + idx, sz);
- if (shaRet != 0)
- return shaRet;
- }
-#endif
-
- /* does this value mean client_hello? */
- idx++;
-
- /* version */
- pv.major = input[idx++];
- pv.minor = input[idx++];
- ssl->chVersion = pv; /* store */
-
- if (ssl->version.minor > pv.minor) {
- byte haveRSA = 0;
- byte havePSK = 0;
- int keySz = 0;
-
- if (!ssl->options.downgrade) {
- WOLFSSL_MSG("Client trying to connect with lesser version");
- return VERSION_ERROR;
- }
- if (pv.minor < ssl->options.minDowngrade) {
- WOLFSSL_MSG("\tversion below minimum allowed, fatal error");
- return VERSION_ERROR;
- }
- if (pv.minor == SSLv3_MINOR) {
- /* turn off tls */
- WOLFSSL_MSG("\tdowngrading to SSLv3");
- ssl->options.tls = 0;
- ssl->options.tls1_1 = 0;
- ssl->version.minor = SSLv3_MINOR;
- }
- else if (pv.minor == TLSv1_MINOR) {
- WOLFSSL_MSG("\tdowngrading to TLSv1");
- /* turn off tls 1.1+ */
- ssl->options.tls1_1 = 0;
- ssl->version.minor = TLSv1_MINOR;
- }
- else if (pv.minor == TLSv1_1_MINOR) {
- WOLFSSL_MSG("\tdowngrading to TLSv1.1");
- ssl->version.minor = TLSv1_1_MINOR;
- }
- else if (pv.minor == TLSv1_2_MINOR) {
- WOLFSSL_MSG(" downgrading to TLSv1.2");
- ssl->version.minor = TLSv1_2_MINOR;
- }
-#ifndef NO_RSA
- haveRSA = 1;
-#endif
-#ifndef NO_PSK
- havePSK = ssl->options.havePSK;
-#endif
-#ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
-#endif
-
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
- }
-
- /* suite size */
- ato16(&input[idx], &clSuites.suiteSz);
- idx += OPAQUE16_LEN;
-
- if (clSuites.suiteSz > WOLFSSL_MAX_SUITE_SZ)
- return BUFFER_ERROR;
- clSuites.hashSigAlgoSz = 0;
-
- /* session size */
- ato16(&input[idx], &sessionSz);
- idx += OPAQUE16_LEN;
-
- if (sessionSz > ID_LEN)
- return BUFFER_ERROR;
-
- /* random size */
- ato16(&input[idx], &randomSz);
- idx += OPAQUE16_LEN;
-
- if (randomSz > RAN_LEN)
- return BUFFER_ERROR;
-
- /* suites */
- for (i = 0, j = 0; i < clSuites.suiteSz; i += 3) {
- byte first = input[idx++];
- if (!first) { /* implicit: skip sslv2 type */
- XMEMCPY(&clSuites.suites[j], &input[idx], SUITE_LEN);
- j += SUITE_LEN;
- }
- idx += SUITE_LEN;
- }
- clSuites.suiteSz = j;
-
- /* session id */
- if (sessionSz) {
- XMEMCPY(ssl->arrays->sessionID, input + idx, sessionSz);
- ssl->arrays->sessionIDSz = (byte)sessionSz;
- idx += sessionSz;
- ssl->options.resuming = 1;
- }
-
- /* random */
- if (randomSz < RAN_LEN)
- XMEMSET(ssl->arrays->clientRandom, 0, RAN_LEN - randomSz);
- XMEMCPY(&ssl->arrays->clientRandom[RAN_LEN - randomSz], input + idx,
- randomSz);
- idx += randomSz;
-
- if (ssl->options.usingCompression)
- ssl->options.usingCompression = 0; /* turn off */
-
- ssl->options.clientState = CLIENT_HELLO_COMPLETE;
- ssl->cbmode = SSL_CB_MODE_WRITE;
- *inOutIdx = idx;
-
- ssl->options.haveSessionId = 1;
- /* DoClientHello uses same resume code */
- if (ssl->options.resuming) { /* let's try */
- WOLFSSL_SESSION* session = GetSession(ssl,
- ssl->arrays->masterSecret, 1);
- #ifdef HAVE_SESSION_TICKET
- if (ssl->options.useTicket == 1) {
- session = &ssl->session;
- }
- #endif
-
- if (!session) {
- WOLFSSL_MSG("Session lookup for resume failed");
- ssl->options.resuming = 0;
- } else {
- #ifdef HAVE_EXT_CACHE
- wolfSSL_SESSION_free(session);
- #endif
- if (MatchSuite(ssl, &clSuites) < 0) {
- WOLFSSL_MSG("Unsupported cipher suite, OldClientHello");
- return UNSUPPORTED_SUITE;
- }
-
- ret = wc_RNG_GenerateBlock(ssl->rng, ssl->arrays->serverRandom,
- RAN_LEN);
- if (ret != 0)
- return ret;
-
- #ifdef NO_OLD_TLS
- ret = DeriveTlsKeys(ssl);
- #else
- #ifndef NO_TLS
- if (ssl->options.tls)
- ret = DeriveTlsKeys(ssl);
- #endif
- if (!ssl->options.tls)
- ret = DeriveKeys(ssl);
- #endif
- ssl->options.clientState = CLIENT_KEYEXCHANGE_COMPLETE;
-
- return ret;
- }
- }
-
- ret = MatchSuite(ssl, &clSuites);
- if (ret != 0)return ret;
- return SanityCheckMsgReceived(ssl, client_hello);
- }
-
-#endif /* OLD_HELLO_ALLOWED */
-
-#ifndef WOLFSSL_NO_TLS12
-
- int HandleTlsResumption(WOLFSSL* ssl, int bogusID, Suites* clSuites)
- {
- int ret = 0;
- WOLFSSL_SESSION* session;
-
- (void)bogusID;
-
- session = GetSession(ssl, ssl->arrays->masterSecret, 1);
- #ifdef HAVE_SESSION_TICKET
- if (ssl->options.useTicket == 1) {
- session = &ssl->session;
- } else if (bogusID == 1 && ssl->options.rejectTicket == 0) {
- WOLFSSL_MSG("Bogus session ID without session ticket");
- return BUFFER_ERROR;
- }
- #endif
-
- if (!session) {
- WOLFSSL_MSG("Session lookup for resume failed");
- ssl->options.resuming = 0;
- }
- else if (session->haveEMS != ssl->options.haveEMS) {
- /* RFC 7627, 5.3, server-side */
- /* if old sess didn't have EMS, but new does, full handshake */
- if (!session->haveEMS && ssl->options.haveEMS) {
- WOLFSSL_MSG("Attempting to resume a session that didn't "
- "use EMS with a new session with EMS. Do full "
- "handshake.");
- ssl->options.resuming = 0;
- }
- /* if old sess used EMS, but new doesn't, MUST abort */
- else if (session->haveEMS && !ssl->options.haveEMS) {
- WOLFSSL_MSG("Trying to resume a session with EMS without "
- "using EMS");
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, handshake_failure);
- #endif
- return EXT_MASTER_SECRET_NEEDED_E;
- }
- #ifdef HAVE_EXT_CACHE
- wolfSSL_SESSION_free(session);
- #endif
- }
- else {
- #ifndef NO_RESUME_SUITE_CHECK
- int j;
-
- /* Check client suites include the one in session */
- for (j = 0; j < clSuites->suiteSz; j += 2) {
- if (clSuites->suites[j] == session->cipherSuite0 &&
- clSuites->suites[j+1] == session->cipherSuite) {
- break;
- }
- }
- if (j == clSuites->suiteSz) {
- WOLFSSL_MSG("Prev session's cipher suite not in ClientHello");
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, illegal_parameter);
- #endif
- return UNSUPPORTED_SUITE;
- }
- #endif
-
- #ifdef HAVE_EXT_CACHE
- wolfSSL_SESSION_free(session);
- #endif
- if (MatchSuite(ssl, clSuites) < 0) {
- WOLFSSL_MSG("Unsupported cipher suite, ClientHello");
- return UNSUPPORTED_SUITE;
- }
-
- ret = wc_RNG_GenerateBlock(ssl->rng, ssl->arrays->serverRandom,
- RAN_LEN);
- if (ret != 0)
- return ret;
-
- #ifdef NO_OLD_TLS
- ret = DeriveTlsKeys(ssl);
- #else
- #ifndef NO_TLS
- if (ssl->options.tls)
- ret = DeriveTlsKeys(ssl);
- #endif
- if (!ssl->options.tls)
- ret = DeriveKeys(ssl);
- #endif
- ssl->options.clientState = CLIENT_KEYEXCHANGE_COMPLETE;
- }
-
- return ret;
- }
-
-
- /* handle processing of client_hello (1) */
- int DoClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- word32 helloSz)
- {
- byte b;
- byte bogusID = 0; /* flag for a bogus session id */
- ProtocolVersion pv;
- Suites clSuites;
- word32 i = *inOutIdx;
- word32 begin = i;
- int ret = 0;
-#ifdef WOLFSSL_DTLS
- Hmac cookieHmac;
- byte peerCookie[MAX_COOKIE_LEN];
- byte peerCookieSz = 0;
- byte cookieType;
- byte cookieSz = 0;
-
- XMEMSET(&cookieHmac, 0, sizeof(Hmac));
-#endif /* WOLFSSL_DTLS */
-
- WOLFSSL_START(WC_FUNC_CLIENT_HELLO_DO);
- WOLFSSL_ENTER("DoClientHello");
-
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello");
- if (ssl->toInfoOn) AddLateName("ClientHello", &ssl->timeoutInfo);
-#endif
- /* protocol version, random and session id length check */
- if (OPAQUE16_LEN + RAN_LEN + OPAQUE8_LEN > helloSz)
- return BUFFER_ERROR;
-
- /* protocol version */
- XMEMCPY(&pv, input + i, OPAQUE16_LEN);
- ssl->chVersion = pv; /* store */
-#ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- #if defined(NO_SHA) && defined(NO_SHA256)
- #error "DTLS needs either SHA or SHA-256"
- #endif /* NO_SHA && NO_SHA256 */
-
- #if !defined(NO_SHA) && defined(NO_SHA256)
- cookieType = WC_SHA;
- cookieSz = WC_SHA_DIGEST_SIZE;
- #endif /* NO_SHA */
- #ifndef NO_SHA256
- cookieType = WC_SHA256;
- cookieSz = WC_SHA256_DIGEST_SIZE;
- #endif /* NO_SHA256 */
- ret = wc_HmacSetKey(&cookieHmac, cookieType,
- ssl->buffers.dtlsCookieSecret.buffer,
- ssl->buffers.dtlsCookieSecret.length);
- if (ret != 0) return ret;
- ret = wc_HmacUpdate(&cookieHmac,
- (const byte*)ssl->buffers.dtlsCtx.peer.sa,
- ssl->buffers.dtlsCtx.peer.sz);
- if (ret != 0) return ret;
- ret = wc_HmacUpdate(&cookieHmac, input + i, OPAQUE16_LEN);
- if (ret != 0) return ret;
- }
-#endif /* WOLFSSL_DTLS */
- i += OPAQUE16_LEN;
-
- /* Legacy protocol version cannot negotiate TLS 1.3 or higher. */
- if (pv.major == SSLv3_MAJOR && pv.minor >= TLSv1_3_MINOR)
- pv.minor = TLSv1_2_MINOR;
-
- if ((!ssl->options.dtls && ssl->version.minor > pv.minor) ||
- (ssl->options.dtls && ssl->version.minor != DTLS_MINOR
- && ssl->version.minor != DTLSv1_2_MINOR && pv.minor != DTLS_MINOR
- && pv.minor != DTLSv1_2_MINOR)) {
-
- word16 haveRSA = 0;
- word16 havePSK = 0;
- int keySz = 0;
-
- if (!ssl->options.downgrade) {
- WOLFSSL_MSG("Client trying to connect with lesser version");
- return VERSION_ERROR;
- }
- if (pv.minor < ssl->options.minDowngrade) {
- WOLFSSL_MSG("\tversion below minimum allowed, fatal error");
- return VERSION_ERROR;
- }
-
- if (pv.minor == SSLv3_MINOR) {
- /* turn off tls */
- WOLFSSL_MSG("\tdowngrading to SSLv3");
- ssl->options.tls = 0;
- ssl->options.tls1_1 = 0;
- ssl->version.minor = SSLv3_MINOR;
- }
- else if (pv.minor == TLSv1_MINOR) {
- /* turn off tls 1.1+ */
- WOLFSSL_MSG("\tdowngrading to TLSv1");
- ssl->options.tls1_1 = 0;
- ssl->version.minor = TLSv1_MINOR;
- }
- else if (pv.minor == TLSv1_1_MINOR) {
- WOLFSSL_MSG("\tdowngrading to TLSv1.1");
- ssl->version.minor = TLSv1_1_MINOR;
- }
- else if (pv.minor == TLSv1_2_MINOR) {
- WOLFSSL_MSG(" downgrading to TLSv1.2");
- ssl->version.minor = TLSv1_2_MINOR;
- }
-#ifndef NO_RSA
- haveRSA = 1;
-#endif
-#ifndef NO_PSK
- havePSK = ssl->options.havePSK;
-#endif
-#ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
-#endif
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
- }
-
-#ifdef OPENSSL_EXTRA
- /* check if option is set to not allow the current version
- * set from either wolfSSL_set_options or wolfSSL_CTX_set_options */
- if (!ssl->options.dtls && ssl->options.downgrade &&
- ssl->options.mask > 0) {
- int reset = 0;
- if (ssl->version.minor == TLSv1_2_MINOR &&
- (ssl->options.mask & SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2) {
- WOLFSSL_MSG("\tOption set to not allow TLSv1.2, Downgrading");
- ssl->version.minor = TLSv1_1_MINOR;
- reset = 1;
- }
- if (ssl->version.minor == TLSv1_1_MINOR &&
- (ssl->options.mask & SSL_OP_NO_TLSv1_1) == SSL_OP_NO_TLSv1_1) {
- WOLFSSL_MSG("\tOption set to not allow TLSv1.1, Downgrading");
- ssl->options.tls1_1 = 0;
- ssl->version.minor = TLSv1_MINOR;
- reset = 1;
- }
- if (ssl->version.minor == TLSv1_MINOR &&
- (ssl->options.mask & SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1) {
- WOLFSSL_MSG("\tOption set to not allow TLSv1, Downgrading");
- ssl->options.tls = 0;
- ssl->options.tls1_1 = 0;
- ssl->version.minor = SSLv3_MINOR;
- reset = 1;
- }
- if (ssl->version.minor == SSLv3_MINOR &&
- (ssl->options.mask & SSL_OP_NO_SSLv3) == SSL_OP_NO_SSLv3) {
- WOLFSSL_MSG("\tError, option set to not allow SSLv3");
- return VERSION_ERROR;
- }
-
- if (ssl->version.minor < ssl->options.minDowngrade) {
- WOLFSSL_MSG("\tversion below minimum allowed, fatal error");
- return VERSION_ERROR;
- }
-
- if (reset) {
- word16 haveRSA = 0;
- word16 havePSK = 0;
- int keySz = 0;
-
- #ifndef NO_RSA
- haveRSA = 1;
- #endif
- #ifndef NO_PSK
- havePSK = ssl->options.havePSK;
- #endif
- #ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
- #endif
-
- /* reset cipher suites to account for TLS version change */
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
- }
- }
-#endif
-
- /* random */
- XMEMCPY(ssl->arrays->clientRandom, input + i, RAN_LEN);
-#ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- ret = wc_HmacUpdate(&cookieHmac, input + i, RAN_LEN);
- if (ret != 0) return ret;
- }
-#endif /* WOLFSSL_DTLS */
- i += RAN_LEN;
-
-#ifdef SHOW_SECRETS
- {
- int j;
- printf("client random: ");
- for (j = 0; j < RAN_LEN; j++)
- printf("%02x", ssl->arrays->clientRandom[j]);
- printf("\n");
- }
-#endif
-
- /* session id */
- b = input[i++];
-
-#ifdef HAVE_SESSION_TICKET
- if (b > 0 && b < ID_LEN) {
- bogusID = 1;
- WOLFSSL_MSG("Client sent bogus session id, let's allow for echo");
- }
-#endif
-
- if (b == ID_LEN || bogusID) {
- if ((i - begin) + b > helloSz)
- return BUFFER_ERROR;
-
- XMEMCPY(ssl->arrays->sessionID, input + i, b);
-#ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- ret = wc_HmacUpdate(&cookieHmac, input + i - 1, b + 1);
- if (ret != 0) return ret;
- }
-#endif /* WOLFSSL_DTLS */
- ssl->arrays->sessionIDSz = b;
- i += b;
- ssl->options.resuming = 1; /* client wants to resume */
- WOLFSSL_MSG("Client wants to resume session");
- }
- else if (b) {
- WOLFSSL_MSG("Invalid session ID size");
- return BUFFER_ERROR; /* session ID nor 0 neither 32 bytes long */
- }
-
- #ifdef WOLFSSL_DTLS
- /* cookie */
- if (ssl->options.dtls) {
-
- if ((i - begin) + OPAQUE8_LEN > helloSz)
- return BUFFER_ERROR;
-
- peerCookieSz = input[i++];
-
- if (peerCookieSz) {
- if (peerCookieSz > MAX_COOKIE_LEN)
- return BUFFER_ERROR;
-
- if ((i - begin) + peerCookieSz > helloSz)
- return BUFFER_ERROR;
-
- XMEMCPY(peerCookie, input + i, peerCookieSz);
-
- i += peerCookieSz;
- }
- }
- #endif
-
- /* suites */
- if ((i - begin) + OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
-
- ato16(&input[i], &clSuites.suiteSz);
- i += OPAQUE16_LEN;
-
- /* suites and compression length check */
- if ((i - begin) + clSuites.suiteSz + OPAQUE8_LEN > helloSz)
- return BUFFER_ERROR;
-
- if (clSuites.suiteSz > WOLFSSL_MAX_SUITE_SZ)
- return BUFFER_ERROR;
-
- XMEMCPY(clSuites.suites, input + i, clSuites.suiteSz);
-
-#ifdef HAVE_SERVER_RENEGOTIATION_INFO
- /* check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV suite */
- if (FindSuite(&clSuites, 0, TLS_EMPTY_RENEGOTIATION_INFO_SCSV) >= 0) {
- TLSX* extension;
-
- /* check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV suite */
- ret = TLSX_AddEmptyRenegotiationInfo(&ssl->extensions, ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
-
- extension = TLSX_Find(ssl->extensions, TLSX_RENEGOTIATION_INFO);
- if (extension) {
- ssl->secure_renegotiation =
- (SecureRenegotiation*)extension->data;
- ssl->secure_renegotiation->enabled = 1;
- }
- }
-#endif /* HAVE_SERVER_RENEGOTIATION_INFO */
-#if defined(HAVE_FALLBACK_SCSV) || defined(OPENSSL_ALL)
- /* check for TLS_FALLBACK_SCSV suite */
- if (FindSuite(&clSuites, TLS_FALLBACK_SCSV, 0) >= 0) {
- WOLFSSL_MSG("Found Fallback SCSV");
- if (ssl->ctx->method->version.minor > pv.minor) {
- WOLFSSL_MSG("Client trying to connect with lesser version");
- SendAlert(ssl, alert_fatal, inappropriate_fallback);
- return VERSION_ERROR;
- }
- }
-#endif
-
-#ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- ret = wc_HmacUpdate(&cookieHmac,
- input + i - OPAQUE16_LEN,
- clSuites.suiteSz + OPAQUE16_LEN);
- if (ret != 0) return ret;
- }
-#endif /* WOLFSSL_DTLS */
- i += clSuites.suiteSz;
- clSuites.hashSigAlgoSz = 0;
-
- /* compression length */
- b = input[i++];
-
- if ((i - begin) + b > helloSz)
- return BUFFER_ERROR;
-
- if (b == 0) {
- WOLFSSL_MSG("No compression types in list");
-#ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, decode_error);
-#endif
- return COMPRESSION_ERROR;
- }
-
-#ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- byte newCookie[MAX_COOKIE_LEN];
-
- ret = wc_HmacUpdate(&cookieHmac, input + i - 1, b + 1);
- if (ret != 0) return ret;
- ret = wc_HmacFinal(&cookieHmac, newCookie);
- if (ret != 0) return ret;
-
- /* If a cookie callback is set, call it to overwrite the cookie.
- * This should be deprecated. The code now calculates the cookie
- * using an HMAC as expected. */
- if (ssl->ctx->CBIOCookie != NULL &&
- ssl->ctx->CBIOCookie(ssl, newCookie, cookieSz,
- ssl->IOCB_CookieCtx) != cookieSz) {
- return COOKIE_ERROR;
- }
-
- /* Check the cookie, see if we progress the state machine. */
- if (peerCookieSz != cookieSz ||
- XMEMCMP(peerCookie, newCookie, cookieSz) != 0) {
-
- /* Send newCookie to client in a HelloVerifyRequest message
- * and let the state machine alone. */
- ssl->msgsReceived.got_client_hello = 0;
- ssl->keys.dtls_handshake_number = 0;
- ssl->keys.dtls_expected_peer_handshake_number = 0;
- *inOutIdx += helloSz;
- return SendHelloVerifyRequest(ssl, newCookie, cookieSz);
- }
-
- /* This was skipped in the DTLS case so we could handle the hello
- * verify request. */
- ret = HashInput(ssl, input + *inOutIdx, helloSz);
- if (ret != 0) return ret;
- }
-#endif /* WOLFSSL_DTLS */
-
- {
- /* compression match types */
- int matchNo = 0;
- int matchZlib = 0;
-
- while (b--) {
- byte comp = input[i++];
-
- if (comp == NO_COMPRESSION) {
- matchNo = 1;
- }
- if (comp == ZLIB_COMPRESSION) {
- matchZlib = 1;
- }
- }
-
- if (ssl->options.usingCompression == 0 && matchNo) {
- WOLFSSL_MSG("Matched No Compression");
- } else if (ssl->options.usingCompression && matchZlib) {
- WOLFSSL_MSG("Matched zlib Compression");
- } else if (ssl->options.usingCompression && matchNo) {
- WOLFSSL_MSG("Could only match no compression, turning off");
- ssl->options.usingCompression = 0; /* turn off */
- } else {
- WOLFSSL_MSG("Could not match compression");
-#ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, illegal_parameter);
-#endif
- return COMPRESSION_ERROR;
- }
- }
-
- *inOutIdx = i;
-
- /* tls extensions */
- if ((i - begin) < helloSz) {
-#ifdef HAVE_TLS_EXTENSIONS
- #ifdef HAVE_QSH
- QSH_Init(ssl);
- #endif
- if (TLSX_SupportExtensions(ssl))
-#else
- if (IsAtLeastTLSv1_2(ssl))
-#endif
- {
- /* Process the hello extension. Skip unsupported. */
- word16 totalExtSz;
-
-#ifdef HAVE_TLS_EXTENSIONS
- /* auto populate extensions supported unless user defined */
- if ((ret = TLSX_PopulateExtensions(ssl, 1)) != 0)
- return ret;
-#endif
-
- if ((i - begin) + OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
-
- ato16(&input[i], &totalExtSz);
- i += OPAQUE16_LEN;
-
- if ((i - begin) + totalExtSz > helloSz)
- return BUFFER_ERROR;
-
-#ifdef HAVE_TLS_EXTENSIONS
- /* tls extensions */
- if ((ret = TLSX_Parse(ssl, (byte *) input + i, totalExtSz,
- client_hello, &clSuites)))
- return ret;
- #ifdef WOLFSSL_TLS13
- if (TLSX_Find(ssl->extensions,
- TLSX_SUPPORTED_VERSIONS) != NULL) {
- WOLFSSL_MSG(
- "Client attempting to connect with higher version");
- return VERSION_ERROR;
- }
- #endif
- #if defined(OPENSSL_ALL) || defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
- if((ret=SNI_Callback(ssl)))
- return ret;
- ssl->options.side = WOLFSSL_SERVER_END;
- #endif
-
- i += totalExtSz;
-#else
- while (totalExtSz) {
- word16 extId, extSz;
-
- if (OPAQUE16_LEN + OPAQUE16_LEN > totalExtSz)
- return BUFFER_ERROR;
-
- ato16(&input[i], &extId);
- i += OPAQUE16_LEN;
- ato16(&input[i], &extSz);
- i += OPAQUE16_LEN;
-
- if (OPAQUE16_LEN + OPAQUE16_LEN + extSz > totalExtSz)
- return BUFFER_ERROR;
-
- if (extId == HELLO_EXT_SIG_ALGO) {
- word16 hashSigAlgoSz;
-
- ato16(&input[i], &hashSigAlgoSz);
- i += OPAQUE16_LEN;
-
- if (OPAQUE16_LEN + hashSigAlgoSz > extSz)
- return BUFFER_ERROR;
-
- clSuites.hashSigAlgoSz = hashSigAlgoSz;
- if (clSuites.hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
- WOLFSSL_MSG("ClientHello SigAlgo list exceeds max, "
- "truncating");
- clSuites.hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
- }
-
- XMEMCPY(clSuites.hashSigAlgo, &input[i],
- clSuites.hashSigAlgoSz);
-
- i += hashSigAlgoSz;
- }
-#ifdef HAVE_EXTENDED_MASTER
- else if (extId == HELLO_EXT_EXTMS)
- ssl->options.haveEMS = 1;
-#endif
- else
- i += extSz;
-
- totalExtSz -= OPAQUE16_LEN + OPAQUE16_LEN + extSz;
- }
-#endif
- *inOutIdx = i;
- }
- else
- *inOutIdx = begin + helloSz; /* skip extensions */
- }
-
- ssl->options.clientState = CLIENT_HELLO_COMPLETE;
- ssl->options.haveSessionId = 1;
-
- /* ProcessOld uses same resume code */
- if (ssl->options.resuming) {
- ret = HandleTlsResumption(ssl, bogusID, &clSuites);
- if (ret != 0)
- return ret;
-
- #ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation &&
- ssl->secure_renegotiation->enabled &&
- IsEncryptionOn(ssl, 0))
- ssl->secure_renegotiation->startScr = 1;
- #endif
-
- if (ssl->options.clientState == CLIENT_KEYEXCHANGE_COMPLETE) {
- WOLFSSL_LEAVE("DoClientHello", ret);
- WOLFSSL_END(WC_FUNC_CLIENT_HELLO_DO);
-
- return ret;
- }
- }
-
-#if defined(HAVE_TLS_EXTENSIONS) && defined(HAVE_DH_DEFAULT_PARAMS)
- #if defined(HAVE_FFDHE) && defined(HAVE_SUPPORTED_CURVES)
- if (TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS) != NULL) {
- /* Set FFDHE parameters or clear DHE parameters if FFDH parameters
- * present and no matches in the server's list. */
- ret = TLSX_SupportedFFDHE_Set(ssl);
- if (ret != 0)
- return ret;
- }
- #endif
-#endif
-
- ret = MatchSuite(ssl, &clSuites);
-#ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_ERROR)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret < 0)
- SendAlert(ssl, alert_fatal, handshake_failure);
-#endif
-
-#ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation && ssl->secure_renegotiation->enabled &&
- IsEncryptionOn(ssl, 0)) {
- ssl->secure_renegotiation->startScr = 1;
- }
-#endif
- WOLFSSL_LEAVE("DoClientHello", ret);
- WOLFSSL_END(WC_FUNC_CLIENT_HELLO_DO);
-
- return ret;
- }
-
-
-#if (!defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)) && !defined(WOLFSSL_NO_CLIENT_AUTH)
-
- typedef struct DcvArgs {
- byte* output; /* not allocated */
- word32 sendSz;
- word16 sz;
- word32 sigSz;
- word32 idx;
- word32 begin;
- byte hashAlgo;
- byte sigAlgo;
- } DcvArgs;
-
- static void FreeDcvArgs(WOLFSSL* ssl, void* pArgs)
- {
- DcvArgs* args = (DcvArgs*)pArgs;
-
- (void)ssl;
- (void)args;
- }
-
- /* handle processing of certificate_verify (15) */
- static int DoCertificateVerify(WOLFSSL* ssl, byte* input,
- word32* inOutIdx, word32 size)
- {
- int ret = 0;
- #ifdef WOLFSSL_ASYNC_CRYPT
- DcvArgs* args = (DcvArgs*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
- #else
- DcvArgs args[1];
- #endif
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_DO);
- WOLFSSL_ENTER("DoCertificateVerify");
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_dcv;
- }
- else
- #endif
- {
- /* Reset state */
- ret = 0;
- ssl->options.asyncState = TLS_ASYNC_BEGIN;
- XMEMSET(args, 0, sizeof(DcvArgs));
- args->hashAlgo = sha_mac;
- args->sigAlgo = anonymous_sa_algo;
- args->idx = *inOutIdx;
- args->begin = *inOutIdx;
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeDcvArgs;
- #endif
- }
-
- switch(ssl->options.asyncState)
- {
- case TLS_ASYNC_BEGIN:
- {
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "CertificateVerify");
- if (ssl->toInfoOn)
- AddLateName("CertificateVerify", &ssl->timeoutInfo);
- #endif
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_BUILD;
- } /* case TLS_ASYNC_BEGIN */
- FALL_THROUGH;
-
- case TLS_ASYNC_BUILD:
- {
- if (IsAtLeastTLSv1_2(ssl)) {
- if ((args->idx - args->begin) + ENUM_LEN + ENUM_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcv);
- }
-
- DecodeSigAlg(&input[args->idx], &args->hashAlgo,
- &args->sigAlgo);
- args->idx += 2;
- }
- #ifndef NO_RSA
- else if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0)
- args->sigAlgo = rsa_sa_algo;
- #endif
- #ifdef HAVE_ECC
- else if (ssl->peerEccDsaKeyPresent)
- args->sigAlgo = ecc_dsa_sa_algo;
- #endif
- #if defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)
- else if (ssl->peerEd25519KeyPresent)
- args->sigAlgo = ed25519_sa_algo;
- #endif /* HAVE_ED25519 && !NO_ED25519_CLIENT_AUTH */
- #if defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)
- else if (ssl->peerEd448KeyPresent)
- args->sigAlgo = ed448_sa_algo;
- #endif /* HAVE_ED448 && !NO_ED448_CLIENT_AUTH */
-
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcv);
- }
-
- ato16(input + args->idx, &args->sz);
- args->idx += OPAQUE16_LEN;
-
- if ((args->idx - args->begin) + args->sz > size ||
- args->sz > ENCRYPT_LEN) {
- ERROR_OUT(BUFFER_ERROR, exit_dcv);
- }
-
- #ifdef HAVE_ECC
- if (ssl->peerEccDsaKeyPresent) {
-
- WOLFSSL_MSG("Doing ECC peer cert verify");
-
- /* make sure a default is defined */
- #if !defined(NO_SHA)
- SetDigest(ssl, sha_mac);
- #elif !defined(NO_SHA256)
- SetDigest(ssl, sha256_mac);
- #elif defined(WOLFSSL_SHA384)
- SetDigest(ssl, sha384_mac);
- #elif defined(WOLFSSL_SHA512)
- SetDigest(ssl, sha512_mac);
- #else
- #error No digest enabled for ECC sig verify
- #endif
-
- if (IsAtLeastTLSv1_2(ssl)) {
- if (args->sigAlgo != ecc_dsa_sa_algo) {
- WOLFSSL_MSG("Oops, peer sent ECC key but not in verify");
- }
-
- SetDigest(ssl, args->hashAlgo);
- }
- }
- #endif /* HAVE_ECC */
- #if defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)
- if (ssl->peerEd25519KeyPresent) {
- WOLFSSL_MSG("Doing ED25519 peer cert verify");
- if (IsAtLeastTLSv1_2(ssl) &&
- args->sigAlgo != ed25519_sa_algo) {
- WOLFSSL_MSG(
- "Oops, peer sent ED25519 key but not in verify");
- }
- }
- #endif /* HAVE_ED25519 && !NO_ED25519_CLIENT_AUTH */
- #if defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)
- if (ssl->peerEd448KeyPresent) {
- WOLFSSL_MSG("Doing ED448 peer cert verify");
- if (IsAtLeastTLSv1_2(ssl) &&
- args->sigAlgo != ed448_sa_algo) {
- WOLFSSL_MSG(
- "Oops, peer sent ED448 key but not in verify");
- }
- }
- #endif /* HAVE_ED448 && !NO_ED448_CLIENT_AUTH */
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_DO;
- } /* case TLS_ASYNC_BUILD */
- FALL_THROUGH;
-
- case TLS_ASYNC_DO:
- {
- #ifndef NO_RSA
- if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) {
- WOLFSSL_MSG("Doing RSA peer cert verify");
-
- ret = RsaVerify(ssl,
- input + args->idx,
- args->sz,
- &args->output,
- args->sigAlgo, args->hashAlgo,
- ssl->peerRsaKey,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerRsaKey
- #else
- NULL
- #endif
- );
- if (ret >= 0) {
- if (args->sigAlgo == rsa_sa_algo)
- args->sendSz = ret;
- else {
- args->sigSz = ret;
- args->sendSz = ssl->buffers.digest.length;
- }
- ret = 0;
- }
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- if (ssl->peerEccDsaKeyPresent) {
- WOLFSSL_MSG("Doing ECC peer cert verify");
-
- ret = EccVerify(ssl,
- input + args->idx, args->sz,
- ssl->buffers.digest.buffer, ssl->buffers.digest.length,
- ssl->peerEccDsaKey,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerEccDsaKey
- #else
- NULL
- #endif
- );
- }
- #endif /* HAVE_ECC */
- #if defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)
- if (ssl->peerEd25519KeyPresent) {
- WOLFSSL_MSG("Doing Ed25519 peer cert verify");
-
- ret = Ed25519Verify(ssl,
- input + args->idx, args->sz,
- ssl->hsHashes->messages, ssl->hsHashes->prevLen,
- ssl->peerEd25519Key,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerEd25519Key
- #else
- NULL
- #endif
- );
- }
- #endif /* HAVE_ED25519 && !NO_ED25519_CLIENT_AUTH */
- #if defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)
- if (ssl->peerEd448KeyPresent) {
- WOLFSSL_MSG("Doing Ed448 peer cert verify");
-
- ret = Ed448Verify(ssl,
- input + args->idx, args->sz,
- ssl->hsHashes->messages, ssl->hsHashes->prevLen,
- ssl->peerEd448Key,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerEd448Key
- #else
- NULL
- #endif
- );
- }
- #endif /* HAVE_ED448 && !NO_ED448_CLIENT_AUTH */
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* handle async pending */
- if (ret == WC_PENDING_E)
- goto exit_dcv;
- #endif
-
- /* Check for error */
- if (ret != 0) {
- ret = SIG_VERIFY_E;
- goto exit_dcv;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_VERIFY;
- } /* case TLS_ASYNC_DO */
- FALL_THROUGH;
-
- case TLS_ASYNC_VERIFY:
- {
- #ifndef NO_RSA
- if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) {
- if (IsAtLeastTLSv1_2(ssl)) {
- #ifdef WC_RSA_PSS
- if (args->sigAlgo == rsa_pss_sa_algo) {
- SetDigest(ssl, args->hashAlgo);
-
- #ifdef HAVE_SELFTEST
- ret = wc_RsaPSS_CheckPadding(
- ssl->buffers.digest.buffer,
- ssl->buffers.digest.length,
- args->output, args->sigSz,
- HashAlgoToType(args->hashAlgo));
- #else
- ret = wc_RsaPSS_CheckPadding_ex(
- ssl->buffers.digest.buffer,
- ssl->buffers.digest.length,
- args->output, args->sigSz,
- HashAlgoToType(args->hashAlgo), -1,
- mp_count_bits(&ssl->peerRsaKey->n));
- #endif
- if (ret != 0) {
- ret = SIG_VERIFY_E;
- goto exit_dcv;
- }
- }
- else
- #endif
- {
- #ifdef WOLFSSL_SMALL_STACK
- byte* encodedSig;
- #else
- byte encodedSig[MAX_ENCODED_SIG_SZ];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ,
- ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- if (encodedSig == NULL) {
- ERROR_OUT(MEMORY_E, exit_dcv);
- }
- #endif
-
- if (args->sigAlgo != rsa_sa_algo) {
- WOLFSSL_MSG("Oops, peer sent RSA key but not "
- "in verify");
- }
-
- SetDigest(ssl, args->hashAlgo);
-
- args->sigSz = wc_EncodeSignature(encodedSig,
- ssl->buffers.digest.buffer,
- ssl->buffers.digest.length,
- TypeHash(args->hashAlgo));
-
- if (args->sendSz != args->sigSz || !args->output ||
- XMEMCMP(args->output, encodedSig,
- min(args->sigSz, MAX_ENCODED_SIG_SZ)) != 0) {
- ret = VERIFY_CERT_ERROR;
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(encodedSig, ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- #endif
- }
- }
- else {
- if (args->sendSz != FINISHED_SZ || !args->output ||
- XMEMCMP(args->output,
- &ssl->hsHashes->certHashes, FINISHED_SZ) != 0) {
- ret = VERIFY_CERT_ERROR;
- }
- }
- }
- #endif /* !NO_RSA */
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_FINALIZE;
- } /* case TLS_ASYNC_VERIFY */
- FALL_THROUGH;
-
- case TLS_ASYNC_FINALIZE:
- {
- if (IsEncryptionOn(ssl, 0)) {
- args->idx += ssl->keys.padSz;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- args->idx += MacSize(ssl);
- #endif
- }
-
- ssl->options.havePeerVerify = 1;
-
- /* Set final index */
- args->idx += args->sz;
- *inOutIdx = args->idx;
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_END;
- } /* case TLS_ASYNC_FINALIZE */
-
- case TLS_ASYNC_END:
- {
- break;
- }
- default:
- ret = INPUT_CASE_ERROR;
- } /* switch(ssl->options.asyncState) */
-
- exit_dcv:
-
- WOLFSSL_LEAVE("DoCertificateVerify", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_DO);
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* Handle async operation */
- if (ret == WC_PENDING_E) {
- /* Mark message as not received so it can process again */
- ssl->msgsReceived.got_certificate_verify = 0;
-
- return ret;
- }
- #endif /* WOLFSSL_ASYNC_CRYPT */
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_ERROR)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret == SIG_VERIFY_E)
- SendAlert(ssl, alert_fatal, decrypt_error);
- else if (ret != 0)
- SendAlert(ssl, alert_fatal, bad_certificate);
- #endif
- /* Digest is not allocated, so do this to prevent free */
- ssl->buffers.digest.buffer = NULL;
- ssl->buffers.digest.length = 0;
-
- /* Final cleanup */
- FreeDcvArgs(ssl, args);
- FreeKeyExchange(ssl);
-
- return ret;
- }
-
-#endif /* (!NO_RSA || ECC || ED25519 || ED448) && !WOLFSSL_NO_CLIENT_AUTH */
-
- /* handle generation of server_hello_done (14) */
- int SendServerHelloDone(WOLFSSL* ssl)
- {
- byte* output;
- int sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- int ret;
-
- WOLFSSL_START(WC_FUNC_SERVER_HELLO_DONE_SEND);
- WOLFSSL_ENTER("SendServerHelloDone");
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls)
- sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- #endif
-
- if (IsEncryptionOn(ssl, 1))
- sendSz += MAX_MSG_EXTRA;
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHeaders(output, 0, server_hello_done, ssl);
-
- if (IsEncryptionOn(ssl, 1)) {
- byte* input;
- int inputSz = HANDSHAKE_HEADER_SZ; /* build msg adds rec hdr */
-
- input = (byte*)XMALLOC(inputSz, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- if (input == NULL)
- return MEMORY_E;
-
- XMEMCPY(input, output + RECORD_HEADER_SZ, inputSz);
- sendSz = BuildMessage(ssl, output, sendSz, input, inputSz,
- handshake, 1, 0, 0);
- XFREE(input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
-
- if (sendSz < 0)
- return sendSz;
- } else {
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- if ((ret = DtlsMsgPoolSave(ssl, output, sendSz)) != 0)
- return ret;
- }
- if (ssl->options.dtls)
- DtlsSEQIncrement(ssl, CUR_ORDER);
- #endif
- ret = HashOutput(ssl, output, sendSz, 0);
- if (ret != 0)
- return ret;
- }
-
- #if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "ServerHelloDone");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "ServerHelloDone", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- #endif
- ssl->options.serverState = SERVER_HELLODONE_COMPLETE;
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendServerHelloDone", ret);
- WOLFSSL_END(WC_FUNC_SERVER_HELLO_DONE_SEND);
-
- return ret;
- }
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#ifdef HAVE_SESSION_TICKET
-
-#define WOLFSSL_TICKET_FIXED_SZ (WOLFSSL_TICKET_NAME_SZ + \
- WOLFSSL_TICKET_IV_SZ + WOLFSSL_TICKET_MAC_SZ + LENGTH_SZ)
-#define WOLFSSL_TICKET_ENC_SZ (SESSION_TICKET_LEN - WOLFSSL_TICKET_FIXED_SZ)
-
- /* our ticket format */
- typedef struct InternalTicket {
- ProtocolVersion pv; /* version when ticket created */
- byte suite[SUITE_LEN]; /* cipher suite when created */
- byte msecret[SECRET_LEN]; /* master secret */
- word32 timestamp; /* born on */
- word16 haveEMS; /* have extended master secret */
-#ifdef WOLFSSL_TLS13
- word32 ageAdd; /* Obfuscation of age */
- word16 namedGroup; /* Named group used */
- #ifndef WOLFSSL_TLS13_DRAFT_18
- TicketNonce ticketNonce; /* Ticket nonce */
- #endif
- #ifdef WOLFSSL_EARLY_DATA
- word32 maxEarlyDataSz; /* Max size of early data */
- #endif
-#endif
- } InternalTicket;
-
- /* fit within SESSION_TICKET_LEN */
- typedef struct ExternalTicket {
- byte key_name[WOLFSSL_TICKET_NAME_SZ]; /* key context name */
- byte iv[WOLFSSL_TICKET_IV_SZ]; /* this ticket's iv */
- byte enc_len[LENGTH_SZ]; /* encrypted length */
- byte enc_ticket[WOLFSSL_TICKET_ENC_SZ]; /* encrypted internal ticket */
- byte mac[WOLFSSL_TICKET_MAC_SZ]; /* total mac */
- /* !! if add to structure, add to TICKET_FIXED_SZ !! */
- } ExternalTicket;
-
- /* create a new session ticket, 0 on success */
- int CreateTicket(WOLFSSL* ssl)
- {
- InternalTicket it;
- ExternalTicket* et = (ExternalTicket*)ssl->session.ticket;
- int encLen;
- int ret;
- byte zeros[WOLFSSL_TICKET_MAC_SZ]; /* biggest cmp size */
-
- XMEMSET(&it, 0, sizeof(it));
-
- /* build internal */
- it.pv.major = ssl->version.major;
- it.pv.minor = ssl->version.minor;
-
- it.suite[0] = ssl->options.cipherSuite0;
- it.suite[1] = ssl->options.cipherSuite;
-
- #ifdef WOLFSSL_EARLY_DATA
- it.maxEarlyDataSz = ssl->options.maxEarlyDataSz;
- #endif
-
- if (!ssl->options.tls1_3) {
- XMEMCPY(it.msecret, ssl->arrays->masterSecret, SECRET_LEN);
- c32toa(LowResTimer(), (byte*)&it.timestamp);
- it.haveEMS = ssl->options.haveEMS;
- }
- else {
-#ifdef WOLFSSL_TLS13
- /* Client adds to ticket age to obfuscate. */
- ret = wc_RNG_GenerateBlock(ssl->rng, (byte*)&it.ageAdd,
- sizeof(it.ageAdd));
- if (ret != 0)
- return BAD_TICKET_ENCRYPT;
- ssl->session.ticketAdd = it.ageAdd;
- it.namedGroup = ssl->session.namedGroup;
- it.timestamp = TimeNowInMilliseconds();
- /* Resumption master secret. */
- XMEMCPY(it.msecret, ssl->session.masterSecret, SECRET_LEN);
- #ifndef WOLFSSL_TLS13_DRAFT_18
- XMEMCPY(&it.ticketNonce, &ssl->session.ticketNonce,
- sizeof(TicketNonce));
- #endif
-#endif
- }
-
- /* build external */
- XMEMCPY(et->enc_ticket, &it, sizeof(InternalTicket));
-
- /* encrypt */
- encLen = WOLFSSL_TICKET_ENC_SZ; /* max size user can use */
- if (ssl->ctx->ticketEncCb == NULL) {
- ret = WOLFSSL_TICKET_RET_FATAL;
- }
- else {
- ret = ssl->ctx->ticketEncCb(ssl, et->key_name, et->iv, et->mac, 1,
- et->enc_ticket, sizeof(InternalTicket),
- &encLen, ssl->ctx->ticketEncCtx);
- }
- if (ret == WOLFSSL_TICKET_RET_OK) {
- if (encLen < (int)sizeof(InternalTicket) ||
- encLen > WOLFSSL_TICKET_ENC_SZ) {
- WOLFSSL_MSG("Bad user ticket encrypt size");
- return BAD_TICKET_KEY_CB_SZ;
- }
-
- /* sanity checks on encrypt callback */
-
- /* internal ticket can't be the same if encrypted */
- if (XMEMCMP(et->enc_ticket, &it, sizeof(InternalTicket)) == 0) {
- WOLFSSL_MSG("User ticket encrypt didn't encrypt");
- return BAD_TICKET_ENCRYPT;
- }
-
- XMEMSET(zeros, 0, sizeof(zeros));
-
- /* name */
- if (XMEMCMP(et->key_name, zeros, WOLFSSL_TICKET_NAME_SZ) == 0) {
- WOLFSSL_MSG("User ticket encrypt didn't set name");
- return BAD_TICKET_ENCRYPT;
- }
-
- /* iv */
- if (XMEMCMP(et->iv, zeros, WOLFSSL_TICKET_IV_SZ) == 0) {
- WOLFSSL_MSG("User ticket encrypt didn't set iv");
- return BAD_TICKET_ENCRYPT;
- }
-
- /* mac */
- if (XMEMCMP(et->mac, zeros, WOLFSSL_TICKET_MAC_SZ) == 0) {
- WOLFSSL_MSG("User ticket encrypt didn't set mac");
- return BAD_TICKET_ENCRYPT;
- }
-
- /* set size */
- c16toa((word16)encLen, et->enc_len);
- ssl->session.ticketLen = (word16)(encLen + WOLFSSL_TICKET_FIXED_SZ);
- if (encLen < WOLFSSL_TICKET_ENC_SZ) {
- /* move mac up since whole enc buffer not used */
- XMEMMOVE(et->enc_ticket +encLen, et->mac,WOLFSSL_TICKET_MAC_SZ);
- }
- }
-
- return ret;
- }
-
-
- /* Parse ticket sent by client, returns callback return value */
- int DoClientTicket(WOLFSSL* ssl, const byte* input, word32 len)
- {
- ExternalTicket* et;
- InternalTicket* it;
- int ret;
- int outLen;
- word16 inLen;
-
- WOLFSSL_START(WC_FUNC_TICKET_DO);
- WOLFSSL_ENTER("DoClientTicket");
-
- if (len > SESSION_TICKET_LEN ||
- len < (word32)(sizeof(InternalTicket) + WOLFSSL_TICKET_FIXED_SZ)) {
- return BAD_TICKET_MSG_SZ;
- }
-
- et = (ExternalTicket*)input;
- it = (InternalTicket*)et->enc_ticket;
-
- /* decrypt */
- ato16(et->enc_len, &inLen);
- if (inLen > (word16)(len - WOLFSSL_TICKET_FIXED_SZ)) {
- return BAD_TICKET_MSG_SZ;
- }
- outLen = inLen; /* may be reduced by user padding */
-
- if (ssl->ctx->ticketEncCb == NULL) {
- ret = WOLFSSL_TICKET_RET_FATAL;
- }
- else {
- ret = ssl->ctx->ticketEncCb(ssl, et->key_name, et->iv,
- et->enc_ticket + inLen, 0,
- et->enc_ticket, inLen, &outLen,
- ssl->ctx->ticketEncCtx);
- }
- if (ret == WOLFSSL_TICKET_RET_FATAL || ret < 0) return ret;
- if (outLen > (int)inLen || outLen < (int)sizeof(InternalTicket)) {
- WOLFSSL_MSG("Bad user ticket decrypt len");
- return BAD_TICKET_KEY_CB_SZ;
- }
-
- /* get master secret */
- if (ret == WOLFSSL_TICKET_RET_OK || ret == WOLFSSL_TICKET_RET_CREATE) {
- if (ssl->version.minor < it->pv.minor) {
- WOLFSSL_MSG("Ticket has greater version");
- return VERSION_ERROR;
- }
- else if (ssl->version.minor > it->pv.minor) {
- if (!ssl->options.downgrade) {
- WOLFSSL_MSG("Ticket has lesser version");
- return VERSION_ERROR;
- }
-
- WOLFSSL_MSG("Downgrading protocol due to ticket");
-
- if (it->pv.minor < ssl->options.minDowngrade)
- return VERSION_ERROR;
- ssl->version.minor = it->pv.minor;
- }
-
-
- if (!IsAtLeastTLSv1_3(ssl->version)) {
- XMEMCPY(ssl->arrays->masterSecret, it->msecret, SECRET_LEN);
- /* Copy the haveExtendedMasterSecret property from the ticket to
- * the saved session, so the property may be checked later. */
- ssl->session.haveEMS = it->haveEMS;
- #ifndef NO_RESUME_SUITE_CHECK
- ssl->session.cipherSuite0 = it->suite[0];
- ssl->session.cipherSuite = it->suite[1];
- #endif
- }
- else {
-#ifdef WOLFSSL_TLS13
- /* Restore information to renegotiate. */
- ssl->session.ticketSeen = it->timestamp;
- ssl->session.ticketAdd = it->ageAdd;
- ssl->session.cipherSuite0 = it->suite[0];
- ssl->session.cipherSuite = it->suite[1];
- #ifdef WOLFSSL_EARLY_DATA
- ssl->session.maxEarlyDataSz = it->maxEarlyDataSz;
- #endif
- /* Resumption master secret. */
- XMEMCPY(ssl->session.masterSecret, it->msecret, SECRET_LEN);
- #ifndef WOLFSSL_TLS13_DRAFT_18
- XMEMCPY(&ssl->session.ticketNonce, &it->ticketNonce,
- sizeof(TicketNonce));
- #endif
- ssl->session.namedGroup = it->namedGroup;
-#endif
- }
- }
-
- WOLFSSL_LEAVE("DoClientTicket", ret);
- WOLFSSL_END(WC_FUNC_TICKET_DO);
-
- return ret;
- }
-
-
- /* send Session Ticket */
- int SendTicket(WOLFSSL* ssl)
- {
- byte* output;
- int ret;
- int sendSz;
- word32 length = SESSION_HINT_SZ + LENGTH_SZ;
- word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
-
- WOLFSSL_START(WC_FUNC_TICKET_SEND);
- WOLFSSL_ENTER("SendTicket");
-
- if (ssl->options.createTicket) {
- ret = CreateTicket(ssl);
- if (ret != 0) return ret;
- }
-
- length += ssl->session.ticketLen;
- sendSz = length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ;
-
- if (!ssl->options.dtls) {
- if (IsEncryptionOn(ssl, 1) && ssl->options.handShakeDone)
- sendSz += MAX_MSG_EXTRA;
- }
- else {
- #ifdef WOLFSSL_DTLS
- sendSz += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- idx += DTLS_RECORD_EXTRA + DTLS_HANDSHAKE_EXTRA;
- #endif
- }
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHeaders(output, length, session_ticket, ssl);
-
- /* hint */
- c32toa(ssl->ctx->ticketHint, output + idx);
- idx += SESSION_HINT_SZ;
-
- /* length */
- c16toa(ssl->session.ticketLen, output + idx);
- idx += LENGTH_SZ;
-
- /* ticket */
- XMEMCPY(output + idx, ssl->session.ticket, ssl->session.ticketLen);
- idx += ssl->session.ticketLen;
-
- if (IsEncryptionOn(ssl, 1) && ssl->options.handShakeDone) {
- byte* input;
- int inputSz = idx - RECORD_HEADER_SZ; /* build msg adds rec hdr */
-
- input = (byte*)XMALLOC(inputSz, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- if (input == NULL)
- return MEMORY_E;
-
- XMEMCPY(input, output + RECORD_HEADER_SZ, inputSz);
- sendSz = BuildMessage(ssl, output, sendSz, input, inputSz,
- handshake, 1, 0, 0);
- XFREE(input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
-
- if (sendSz < 0)
- return sendSz;
- }
- else {
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- if ((ret = DtlsMsgPoolSave(ssl, output, sendSz)) != 0)
- return ret;
-
- DtlsSEQIncrement(ssl, CUR_ORDER);
- }
- #endif
- ret = HashOutput(ssl, output, sendSz, 0);
- if (ret != 0)
- return ret;
- }
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendTicket", ret);
- WOLFSSL_END(WC_FUNC_TICKET_SEND);
-
- return ret;
- }
-
-#endif /* HAVE_SESSION_TICKET */
-
-#ifndef WOLFSSL_NO_TLS12
-
-#if defined(HAVE_SECURE_RENEGOTIATION) && \
- defined(HAVE_SERVER_RENEGOTIATION_INFO) && \
- !defined(WOLFSSL_NO_SERVER)
-
- /* handle generation of server's hello_request (0) */
- int SendHelloRequest(WOLFSSL* ssl)
- {
- byte* output;
- int sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- int ret;
-
- WOLFSSL_START(WC_FUNC_HELLO_REQUEST_SEND);
- WOLFSSL_ENTER("SendHelloRequest");
-
- if (IsEncryptionOn(ssl, 1))
- sendSz += MAX_MSG_EXTRA;
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- AddHeaders(output, 0, hello_request, ssl);
-
- if (IsEncryptionOn(ssl, 1)) {
- byte* input;
- int inputSz = HANDSHAKE_HEADER_SZ; /* build msg adds rec hdr */
-
- input = (byte*)XMALLOC(inputSz, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
- if (input == NULL)
- return MEMORY_E;
-
- XMEMCPY(input, output + RECORD_HEADER_SZ, inputSz);
- sendSz = BuildMessage(ssl, output, sendSz, input, inputSz,
- handshake, 0, 0, 0);
- XFREE(input, ssl->heap, DYNAMIC_TYPE_IN_BUFFER);
-
- if (sendSz < 0)
- return sendSz;
- }
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendHelloRequest", ret);
- WOLFSSL_END(WC_FUNC_HELLO_REQUEST_SEND);
-
- return ret;
- }
-
-#endif /* HAVE_SECURE_RENEGOTIATION && HAVE_SERVER_RENEGOTIATION_INFO */
-
-#ifdef WOLFSSL_DTLS
- /* handle generation of DTLS hello_verify_request (3) */
- static int SendHelloVerifyRequest(WOLFSSL* ssl,
- const byte* cookie, byte cookieSz)
- {
- byte* output;
- int length = VERSION_SZ + ENUM_LEN + cookieSz;
- int idx = DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ;
- int sendSz = length + idx;
- int ret;
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* Hello Verify Request should use the same sequence number as the
- * Client Hello. */
- ssl->keys.dtls_sequence_number_hi = ssl->keys.curSeq_hi;
- ssl->keys.dtls_sequence_number_lo = ssl->keys.curSeq_lo;
- AddHeaders(output, length, hello_verify_request, ssl);
-
-#ifdef OPENSSL_EXTRA
- output[idx++] = DTLS_MAJOR;
- output[idx++] = DTLS_MINOR;
-#else
- output[idx++] = ssl->version.major;
- output[idx++] = ssl->version.minor;
-#endif
-
- output[idx++] = cookieSz;
- if (cookie == NULL || cookieSz == 0)
- return COOKIE_ERROR;
-
- XMEMCPY(output + idx, cookie, cookieSz);
-
-#if defined(WOLFSSL_CALLBACKS) || defined(OPENSSL_EXTRA)
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "HelloVerifyRequest");
- if (ssl->toInfoOn)
- AddPacketInfo(ssl, "HelloVerifyRequest", handshake, output,
- sendSz, WRITE_PROTO, ssl->heap);
-#endif
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- return SendBuffered(ssl);
- }
-#endif /* WOLFSSL_DTLS */
-
- typedef struct DckeArgs {
- byte* output; /* not allocated */
- word32 length;
- word32 idx;
- word32 begin;
- word32 sigSz;
- #ifndef NO_RSA
- int lastErr;
- #endif
- } DckeArgs;
-
- static void FreeDckeArgs(WOLFSSL* ssl, void* pArgs)
- {
- DckeArgs* args = (DckeArgs*)pArgs;
-
- (void)ssl;
- (void)args;
- }
-
- /* handle processing client_key_exchange (16) */
- static int DoClientKeyExchange(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- word32 size)
- {
- int ret;
- #ifdef WOLFSSL_ASYNC_CRYPT
- DckeArgs* args = (DckeArgs*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
- #else
- DckeArgs args[1];
- #endif
-
- (void)size;
- (void)input;
-
- WOLFSSL_START(WC_FUNC_CLIENT_KEY_EXCHANGE_DO);
- WOLFSSL_ENTER("DoClientKeyExchange");
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_dcke;
- }
- else
- #endif /* WOLFSSL_ASYNC_CRYPT */
- {
- /* Reset state */
- ret = 0;
- ssl->options.asyncState = TLS_ASYNC_BEGIN;
- XMEMSET(args, 0, sizeof(DckeArgs));
- args->idx = *inOutIdx;
- args->begin = *inOutIdx;
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeDckeArgs;
- #endif
- }
-
- /* Do Client Key Exchange State Machine */
- switch(ssl->options.asyncState)
- {
- case TLS_ASYNC_BEGIN:
- {
- /* Sanity checks */
- if (ssl->options.side != WOLFSSL_SERVER_END) {
- WOLFSSL_MSG("Client received client keyexchange, attack?");
- WOLFSSL_ERROR(ssl->error = SIDE_ERROR);
- ERROR_OUT(WOLFSSL_FATAL_ERROR, exit_dcke);
- }
-
- if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) {
- WOLFSSL_MSG("Client sending keyexchange at wrong time");
- SendAlert(ssl, alert_fatal, unexpected_message);
- ERROR_OUT(OUT_OF_ORDER_E, exit_dcke);
- }
-
- #ifndef NO_CERTS
- if (ssl->options.verifyPeer && ssl->options.failNoCert) {
- if (!ssl->options.havePeerCert) {
- WOLFSSL_MSG("client didn't present peer cert");
- ERROR_OUT(NO_PEER_CERT, exit_dcke);
- }
- }
-
- if (ssl->options.verifyPeer && ssl->options.failNoCertxPSK) {
- if (!ssl->options.havePeerCert &&
- !ssl->options.usingPSK_cipher) {
- WOLFSSL_MSG("client didn't present peer cert");
- return NO_PEER_CERT;
- }
- }
- #endif /* !NO_CERTS */
-
- #if defined(WOLFSSL_CALLBACKS)
- if (ssl->hsInfoOn) {
- AddPacketName(ssl, "ClientKeyExchange");
- }
- if (ssl->toInfoOn) {
- AddLateName("ClientKeyExchange", &ssl->timeoutInfo);
- }
- #endif
-
- if (ssl->arrays->preMasterSecret == NULL) {
- ssl->arrays->preMasterSz = ENCRYPT_LEN;
- ssl->arrays->preMasterSecret = (byte*)XMALLOC(ENCRYPT_LEN,
- ssl->heap, DYNAMIC_TYPE_SECRET);
- if (ssl->arrays->preMasterSecret == NULL) {
- ERROR_OUT(MEMORY_E, exit_dcke);
- }
- XMEMSET(ssl->arrays->preMasterSecret, 0, ENCRYPT_LEN);
- }
-
- switch (ssl->specs.kea) {
- #ifndef NO_RSA
- case rsa_kea:
- {
- break;
- } /* rsa_kea */
- #endif /* !NO_RSA */
- #ifndef NO_PSK
- case psk_kea:
- {
- /* sanity check that PSK server callback has been set */
- if (ssl->options.server_psk_cb == NULL) {
- WOLFSSL_MSG("No server PSK callback set");
- ERROR_OUT(PSK_KEY_ERROR, exit_dcke);
- }
- break;
- }
- #endif /* !NO_PSK */
- #ifdef HAVE_NTRU
- case ntru_kea:
- {
- /* make sure private key exists */
- if (ssl->buffers.key == NULL ||
- ssl->buffers.key->buffer == NULL) {
- ERROR_OUT(NO_PRIVATE_KEY, exit_dcke);
- }
- break;
- }
- #endif /* HAVE_NTRU */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
- #ifndef NO_DH
- case diffie_hellman_kea:
- {
- break;
- }
- #endif /* !NO_DH */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- /* sanity check that PSK server callback has been set */
- if (ssl->options.server_psk_cb == NULL) {
- WOLFSSL_MSG("No server PSK callback set");
- ERROR_OUT(PSK_KEY_ERROR, exit_dcke);
- }
- break;
- }
- #endif /* !NO_DH && !NO_PSK */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- /* sanity check that PSK server callback has been set */
- if (ssl->options.server_psk_cb == NULL) {
- WOLFSSL_MSG("No server PSK callback set");
- ERROR_OUT(PSK_KEY_ERROR, exit_dcke);
- }
- break;
- }
- #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && !NO_PSK */
- default:
- WOLFSSL_MSG("Bad kea type");
- ret = BAD_KEA_TYPE_E;
- } /* switch (ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_dcke;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_BUILD;
- } /* TLS_ASYNC_BEGIN */
- FALL_THROUGH;
-
- case TLS_ASYNC_BUILD:
- {
- switch (ssl->specs.kea) {
- #ifndef NO_RSA
- case rsa_kea:
- {
- word16 keySz;
-
- ssl->buffers.keyType = rsa_sa_algo;
- ret = DecodePrivateKey(ssl, &keySz);
- if (ret != 0) {
- goto exit_dcke;
- }
- args->length = (word32)keySz;
- ssl->arrays->preMasterSz = SECRET_LEN;
-
- if (ssl->options.tls) {
- word16 check;
-
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- ato16(input + args->idx, &check);
- args->idx += OPAQUE16_LEN;
-
- if ((word32)check != args->length) {
- WOLFSSL_MSG("RSA explicit size doesn't match");
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, bad_record_mac);
- #endif
- ERROR_OUT(RSA_PRIVATE_ERROR, exit_dcke);
- }
- }
-
- if ((args->idx - args->begin) + args->length > size) {
- WOLFSSL_MSG("RSA message too big");
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- /* pre-load PreMasterSecret with RNG data */
- ret = wc_RNG_GenerateBlock(ssl->rng,
- &ssl->arrays->preMasterSecret[VERSION_SZ],
- SECRET_LEN - VERSION_SZ);
- if (ret != 0) {
- goto exit_dcke;
- }
-
- args->output = NULL;
- break;
- } /* rsa_kea */
- #endif /* !NO_RSA */
- #ifndef NO_PSK
- case psk_kea:
- {
- byte* pms = ssl->arrays->preMasterSecret;
- word16 ci_sz;
-
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- ato16(input + args->idx, &ci_sz);
- args->idx += OPAQUE16_LEN;
-
- if (ci_sz > MAX_PSK_ID_LEN) {
- ERROR_OUT(CLIENT_ID_ERROR, exit_dcke);
- }
-
- if ((args->idx - args->begin) + ci_sz > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- XMEMCPY(ssl->arrays->client_identity,
- input + args->idx, ci_sz);
- args->idx += ci_sz;
-
- ssl->arrays->client_identity[ci_sz] = '\0'; /* null term */
- ssl->arrays->psk_keySz = ssl->options.server_psk_cb(ssl,
- ssl->arrays->client_identity, ssl->arrays->psk_key,
- MAX_PSK_KEY_LEN);
-
- if (ssl->arrays->psk_keySz == 0 ||
- ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
- ERROR_OUT(PSK_KEY_ERROR, exit_dcke);
- }
-
- /* make psk pre master secret */
- /* length of key + length 0s + length of key + key */
- c16toa((word16) ssl->arrays->psk_keySz, pms);
- pms += OPAQUE16_LEN;
-
- XMEMSET(pms, 0, ssl->arrays->psk_keySz);
- pms += ssl->arrays->psk_keySz;
-
- c16toa((word16) ssl->arrays->psk_keySz, pms);
- pms += OPAQUE16_LEN;
-
- XMEMCPY(pms, ssl->arrays->psk_key, ssl->arrays->psk_keySz);
- ssl->arrays->preMasterSz =
- (ssl->arrays->psk_keySz * 2) + (OPAQUE16_LEN * 2);
- break;
- }
- #endif /* !NO_PSK */
- #ifdef HAVE_NTRU
- case ntru_kea:
- {
- word16 cipherLen;
- word16 plainLen = ENCRYPT_LEN;
-
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- ato16(input + args->idx, &cipherLen);
- args->idx += OPAQUE16_LEN;
-
- if (cipherLen > MAX_NTRU_ENCRYPT_SZ) {
- ERROR_OUT(NTRU_KEY_ERROR, exit_dcke);
- }
-
- if ((args->idx - args->begin) + cipherLen > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- if (NTRU_OK != ntru_crypto_ntru_decrypt(
- (word16) ssl->buffers.key->length,
- ssl->buffers.key->buffer, cipherLen,
- input + args->idx, &plainLen,
- ssl->arrays->preMasterSecret)) {
- ERROR_OUT(NTRU_DECRYPT_ERROR, exit_dcke);
- }
-
- if (plainLen != SECRET_LEN) {
- ERROR_OUT(NTRU_DECRYPT_ERROR, exit_dcke);
- }
-
- args->idx += cipherLen;
- ssl->arrays->preMasterSz = plainLen;
- break;
- }
- #endif /* HAVE_NTRU */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- #ifdef HAVE_ECC
- ecc_key* private_key = ssl->eccTempKey;
-
- /* handle static private key */
- if (ssl->specs.static_ecdh &&
- ssl->ecdhCurveOID != ECC_X25519_OID &&
- ssl->ecdhCurveOID != ECC_X448_OID) {
- word16 keySz;
-
- ssl->buffers.keyType = ecc_dsa_sa_algo;
- ret = DecodePrivateKey(ssl, &keySz);
- if (ret != 0) {
- goto exit_dcke;
- }
- private_key = (ecc_key*)ssl->hsKey;
- }
- #endif
-
- /* import peer ECC key */
- if ((args->idx - args->begin) + OPAQUE8_LEN > size) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, decode_error);
- #endif
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- args->length = input[args->idx++];
-
- if ((args->idx - args->begin) + args->length > size) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, decode_error);
- #endif
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->X25519SharedSecretCb != NULL) {
- break;
- }
- #endif
- if (ssl->peerX25519Key == NULL) {
- /* alloc/init on demand */
- ret = AllocKey(ssl, DYNAMIC_TYPE_CURVE25519,
- (void**)&ssl->peerX25519Key);
- if (ret != 0) {
- goto exit_dcke;
- }
- } else if (ssl->peerX25519KeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_CURVE25519,
- ssl->peerX25519Key);
- ssl->peerX25519KeyPresent = 0;
- if (ret != 0) {
- goto exit_dcke;
- }
- }
-
- if ((ret = wc_curve25519_check_public(
- input + args->idx, args->length,
- EC25519_LITTLE_ENDIAN)) != 0) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_E)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret == ECC_OUT_OF_RANGE_E)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- else {
- SendAlert(ssl, alert_fatal,
- illegal_parameter);
- }
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dcke);
- }
-
- if (wc_curve25519_import_public_ex(
- input + args->idx, args->length,
- ssl->peerX25519Key,
- EC25519_LITTLE_ENDIAN)) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, illegal_parameter);
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dcke);
- }
-
- ssl->arrays->preMasterSz = CURVE25519_KEYSIZE;
-
- ssl->peerX25519KeyPresent = 1;
-
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->X448SharedSecretCb != NULL) {
- break;
- }
- #endif
- if (ssl->peerX448Key == NULL) {
- /* alloc/init on demand */
- ret = AllocKey(ssl, DYNAMIC_TYPE_CURVE448,
- (void**)&ssl->peerX448Key);
- if (ret != 0) {
- goto exit_dcke;
- }
- } else if (ssl->peerX448KeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_CURVE448,
- ssl->peerX448Key);
- ssl->peerX448KeyPresent = 0;
- if (ret != 0) {
- goto exit_dcke;
- }
- }
-
- if ((ret = wc_curve448_check_public(
- input + args->idx, args->length,
- EC448_LITTLE_ENDIAN)) != 0) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_E)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret == ECC_OUT_OF_RANGE_E)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- else {
- SendAlert(ssl, alert_fatal,
- illegal_parameter);
- }
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dcke);
- }
-
- if (wc_curve448_import_public_ex(
- input + args->idx, args->length,
- ssl->peerX448Key,
- EC448_LITTLE_ENDIAN)) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, illegal_parameter);
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dcke);
- }
-
- ssl->arrays->preMasterSz = CURVE448_KEY_SIZE;
-
- ssl->peerX448KeyPresent = 1;
-
- break;
- }
- #endif
- #ifdef HAVE_ECC
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->EccSharedSecretCb != NULL) {
- break;
- }
- #endif
-
- if (!ssl->specs.static_ecdh &&
- ssl->eccTempKeyPresent == 0) {
- WOLFSSL_MSG("Ecc ephemeral key not made correctly");
- ERROR_OUT(ECC_MAKEKEY_ERROR, exit_dcke);
- }
-
- if (ssl->peerEccKey == NULL) {
- /* alloc/init on demand */
- ret = AllocKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->peerEccKey);
- if (ret != 0) {
- goto exit_dcke;
- }
- } else if (ssl->peerEccKeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_ECC,
- ssl->peerEccKey);
- ssl->peerEccKeyPresent = 0;
- if (ret != 0) {
- goto exit_dcke;
- }
- }
-
- if (wc_ecc_import_x963_ex(input + args->idx,
- args->length, ssl->peerEccKey,
- private_key->dp->id)) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, illegal_parameter);
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dcke);
- }
-
- ssl->arrays->preMasterSz = private_key->dp->size;
-
- ssl->peerEccKeyPresent = 1;
- #endif /* HAVE_ECC */
-
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
- #ifndef NO_DH
- case diffie_hellman_kea:
- {
- word16 clientPubSz;
-
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- ato16(input + args->idx, &clientPubSz);
- args->idx += OPAQUE16_LEN;
-
- if ((args->idx - args->begin) + clientPubSz > size) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- SendAlert(ssl, alert_fatal, decode_error);
- #endif
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- args->sigSz = clientPubSz;
-
- ret = AllocKey(ssl, DYNAMIC_TYPE_DH,
- (void**)&ssl->buffers.serverDH_Key);
- if (ret != 0) {
- goto exit_dcke;
- }
-
- ret = wc_DhSetKey(ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length,
- ssl->buffers.serverDH_G.buffer,
- ssl->buffers.serverDH_G.length);
-
- /* set the max agree result size */
- ssl->arrays->preMasterSz = ENCRYPT_LEN;
- break;
- }
- #endif /* !NO_DH */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- word16 clientSz;
-
- /* Read in the PSK hint */
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- ato16(input + args->idx, &clientSz);
- args->idx += OPAQUE16_LEN;
- if (clientSz > MAX_PSK_ID_LEN) {
- ERROR_OUT(CLIENT_ID_ERROR, exit_dcke);
- }
-
- if ((args->idx - args->begin) + clientSz > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- XMEMCPY(ssl->arrays->client_identity, input + args->idx,
- clientSz);
- args->idx += clientSz;
- ssl->arrays->client_identity[clientSz] = '\0'; /* null term */
-
- /* Read in the DHE business */
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- ato16(input + args->idx, &clientSz);
- args->idx += OPAQUE16_LEN;
-
- if ((args->idx - args->begin) + clientSz > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- args->sigSz = clientSz;
-
- ret = AllocKey(ssl, DYNAMIC_TYPE_DH,
- (void**)&ssl->buffers.serverDH_Key);
- if (ret != 0) {
- goto exit_dcke;
- }
-
- ret = wc_DhSetKey(ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_P.buffer,
- ssl->buffers.serverDH_P.length,
- ssl->buffers.serverDH_G.buffer,
- ssl->buffers.serverDH_G.length);
-
- break;
- }
- #endif /* !NO_DH && !NO_PSK */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- word16 clientSz;
-
- /* Read in the PSK hint */
- if ((args->idx - args->begin) + OPAQUE16_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- ato16(input + args->idx, &clientSz);
- args->idx += OPAQUE16_LEN;
- if (clientSz > MAX_PSK_ID_LEN) {
- ERROR_OUT(CLIENT_ID_ERROR, exit_dcke);
- }
- if ((args->idx - args->begin) + clientSz > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- XMEMCPY(ssl->arrays->client_identity,
- input + args->idx, clientSz);
- args->idx += clientSz;
- ssl->arrays->client_identity[clientSz] = '\0'; /* null term */
-
- /* import peer ECC key */
- if ((args->idx - args->begin) + OPAQUE8_LEN > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- args->length = input[args->idx++];
-
- if ((args->idx - args->begin) + args->length > size) {
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
-
- args->sigSz = ENCRYPT_LEN - OPAQUE16_LEN;
-
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->X25519SharedSecretCb != NULL) {
- break;
- }
- #endif
-
- if (ssl->eccTempKeyPresent == 0) {
- WOLFSSL_MSG(
- "X25519 ephemeral key not made correctly");
- ERROR_OUT(ECC_MAKEKEY_ERROR, exit_dcke);
- }
-
- if (ssl->peerX25519Key == NULL) {
- /* alloc/init on demand */
- ret = AllocKey(ssl, DYNAMIC_TYPE_CURVE25519,
- (void**)&ssl->peerX25519Key);
- if (ret != 0) {
- goto exit_dcke;
- }
- } else if (ssl->peerX25519KeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_CURVE25519,
- ssl->peerX25519Key);
- ssl->peerX25519KeyPresent = 0;
- if (ret != 0) {
- goto exit_dcke;
- }
- }
-
- if ((ret = wc_curve25519_check_public(
- input + args->idx, args->length,
- EC25519_LITTLE_ENDIAN)) != 0) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_E)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret == ECC_OUT_OF_RANGE_E)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- else {
- SendAlert(ssl, alert_fatal,
- illegal_parameter);
- }
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dcke);
- }
-
- if (wc_curve25519_import_public_ex(
- input + args->idx, args->length,
- ssl->peerX25519Key,
- EC25519_LITTLE_ENDIAN)) {
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dcke);
- }
-
- ssl->peerX25519KeyPresent = 1;
-
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->X448SharedSecretCb != NULL) {
- break;
- }
- #endif
-
- if (ssl->eccTempKeyPresent == 0) {
- WOLFSSL_MSG(
- "X448 ephemeral key not made correctly");
- ERROR_OUT(ECC_MAKEKEY_ERROR, exit_dcke);
- }
-
- if (ssl->peerX448Key == NULL) {
- /* alloc/init on demand */
- ret = AllocKey(ssl, DYNAMIC_TYPE_CURVE448,
- (void**)&ssl->peerX448Key);
- if (ret != 0) {
- goto exit_dcke;
- }
- } else if (ssl->peerX448KeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_CURVE448,
- ssl->peerX448Key);
- ssl->peerX448KeyPresent = 0;
- if (ret != 0) {
- goto exit_dcke;
- }
- }
-
- if ((ret = wc_curve448_check_public(
- input + args->idx, args->length,
- EC448_LITTLE_ENDIAN)) != 0) {
- #ifdef WOLFSSL_EXTRA_ALERTS
- if (ret == BUFFER_E)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret == ECC_OUT_OF_RANGE_E)
- SendAlert(ssl, alert_fatal, bad_record_mac);
- else {
- SendAlert(ssl, alert_fatal,
- illegal_parameter);
- }
- #endif
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dcke);
- }
-
- if (wc_curve448_import_public_ex(
- input + args->idx, args->length,
- ssl->peerX448Key,
- EC448_LITTLE_ENDIAN)) {
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dcke);
- }
-
- ssl->peerX448KeyPresent = 1;
-
- break;
- }
- #endif
- #ifdef HAVE_PK_CALLBACKS
- /* if callback then use it for shared secret */
- if (ssl->ctx->EccSharedSecretCb != NULL) {
- break;
- }
- #endif
-
- if (ssl->eccTempKeyPresent == 0) {
- WOLFSSL_MSG("Ecc ephemeral key not made correctly");
- ERROR_OUT(ECC_MAKEKEY_ERROR, exit_dcke);
- }
-
- if (ssl->peerEccKey == NULL) {
- /* alloc/init on demand */
- ret = AllocKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->peerEccKey);
- if (ret != 0) {
- goto exit_dcke;
- }
- }
- else if (ssl->peerEccKeyPresent) {
- ret = ReuseKey(ssl, DYNAMIC_TYPE_ECC,
- ssl->peerEccKey);
- ssl->peerEccKeyPresent = 0;
- if (ret != 0) {
- goto exit_dcke;
- }
- }
- if (wc_ecc_import_x963_ex(input + args->idx,
- args->length, ssl->peerEccKey,
- ssl->eccTempKey->dp->id)) {
- ERROR_OUT(ECC_PEERKEY_ERROR, exit_dcke);
- }
-
- ssl->peerEccKeyPresent = 1;
- break;
- }
- #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && !NO_PSK */
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch (ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_dcke;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_DO;
- } /* TLS_ASYNC_BUILD */
- FALL_THROUGH;
-
- case TLS_ASYNC_DO:
- {
- switch (ssl->specs.kea) {
- #ifndef NO_RSA
- case rsa_kea:
- {
- RsaKey* key = (RsaKey*)ssl->hsKey;
-
- ret = RsaDec(ssl,
- input + args->idx,
- args->length,
- &args->output,
- &args->sigSz,
- key,
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.key
- #else
- NULL
- #endif
- );
-
- /* Errors that can occur here that should be
- * indistinguishable:
- * RSA_BUFFER_E, RSA_PAD_E and RSA_PRIVATE_ERROR
- */
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- goto exit_dcke;
- #endif
- if (ret == BAD_FUNC_ARG)
- goto exit_dcke;
-
- args->lastErr = ret - (SECRET_LEN - args->sigSz);
- ret = 0;
- break;
- } /* rsa_kea */
- #endif /* !NO_RSA */
- #ifndef NO_PSK
- case psk_kea:
- {
- break;
- }
- #endif /* !NO_PSK */
- #ifdef HAVE_NTRU
- case ntru_kea:
- {
- break;
- }
- #endif /* HAVE_NTRU */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- void* private_key = ssl->eccTempKey;
- (void)private_key;
-
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- ret = X25519SharedSecret(ssl,
- (curve25519_key*)private_key,
- ssl->peerX25519Key,
- input + args->idx, &args->length,
- ssl->arrays->preMasterSecret,
- &ssl->arrays->preMasterSz,
- WOLFSSL_SERVER_END
- );
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- ret = X448SharedSecret(ssl,
- (curve448_key*)private_key,
- ssl->peerX448Key,
- input + args->idx, &args->length,
- ssl->arrays->preMasterSecret,
- &ssl->arrays->preMasterSz,
- WOLFSSL_SERVER_END
- );
- break;
- }
- #endif
- #ifdef HAVE_ECC
- if (ssl->specs.static_ecdh) {
- private_key = ssl->hsKey;
- }
-
- /* Generate shared secret */
- ret = EccSharedSecret(ssl,
- (ecc_key*)private_key, ssl->peerEccKey,
- input + args->idx, &args->length,
- ssl->arrays->preMasterSecret,
- &ssl->arrays->preMasterSz,
- WOLFSSL_SERVER_END
- );
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret != WC_PENDING_E)
- #endif
- {
- FreeKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->peerEccKey);
- ssl->peerEccKeyPresent = 0;
- }
- #endif
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
- #ifndef NO_DH
- case diffie_hellman_kea:
- {
- ret = DhAgree(ssl, ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_Priv.buffer,
- ssl->buffers.serverDH_Priv.length,
- input + args->idx,
- (word16)args->sigSz,
- ssl->arrays->preMasterSecret,
- &ssl->arrays->preMasterSz);
- break;
- }
- #endif /* !NO_DH */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- ret = DhAgree(ssl, ssl->buffers.serverDH_Key,
- ssl->buffers.serverDH_Priv.buffer,
- ssl->buffers.serverDH_Priv.length,
- input + args->idx,
- (word16)args->sigSz,
- ssl->arrays->preMasterSecret + OPAQUE16_LEN,
- &ssl->arrays->preMasterSz);
- break;
- }
- #endif /* !NO_DH && !NO_PSK */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- #ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID) {
- ret = X25519SharedSecret(ssl,
- (curve25519_key*)ssl->eccTempKey,
- ssl->peerX25519Key,
- input + args->idx, &args->length,
- ssl->arrays->preMasterSecret + OPAQUE16_LEN,
- &args->sigSz,
- WOLFSSL_SERVER_END
- );
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret != WC_PENDING_E)
- #endif
- {
- FreeKey(ssl, DYNAMIC_TYPE_CURVE25519,
- (void**)&ssl->peerX25519Key);
- ssl->peerX25519KeyPresent = 0;
- }
- break;
- }
- #endif
- #ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID) {
- ret = X448SharedSecret(ssl,
- (curve448_key*)ssl->eccTempKey,
- ssl->peerX448Key,
- input + args->idx, &args->length,
- ssl->arrays->preMasterSecret + OPAQUE16_LEN,
- &args->sigSz,
- WOLFSSL_SERVER_END
- );
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret != WC_PENDING_E)
- #endif
- {
- FreeKey(ssl, DYNAMIC_TYPE_CURVE448,
- (void**)&ssl->peerX448Key);
- ssl->peerX448KeyPresent = 0;
- }
- break;
- }
- #endif
- /* Generate shared secret */
- ret = EccSharedSecret(ssl,
- ssl->eccTempKey, ssl->peerEccKey,
- input + args->idx, &args->length,
- ssl->arrays->preMasterSecret + OPAQUE16_LEN,
- &args->sigSz,
- WOLFSSL_SERVER_END
- );
- if (!ssl->specs.static_ecdh
- #ifdef WOLFSSL_ASYNC_CRYPT
- && ret != WC_PENDING_E
- #endif
- ) {
- FreeKey(ssl, DYNAMIC_TYPE_ECC,
- (void**)&ssl->peerEccKey);
- ssl->peerEccKeyPresent = 0;
- }
- break;
- }
- #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && !NO_PSK */
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch (ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_dcke;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_VERIFY;
- } /* TLS_ASYNC_DO */
- FALL_THROUGH;
-
- case TLS_ASYNC_VERIFY:
- {
- switch (ssl->specs.kea) {
- #ifndef NO_RSA
- case rsa_kea:
- {
- byte mask;
- int i;
-
- /* Add the signature length to idx */
- args->idx += args->length;
-
- #ifdef DEBUG_WOLFSSL
- /* check version (debug warning message only) */
- if (args->output != NULL) {
- if (args->output[0] != ssl->chVersion.major ||
- args->output[1] != ssl->chVersion.minor) {
- WOLFSSL_MSG("preMasterSecret version mismatch");
- }
- }
- #endif
-
- /* RFC5246 7.4.7.1:
- * Treat incorrectly formatted message blocks and/or
- * mismatched version numbers in a manner
- * indistinguishable from correctly formatted RSA blocks
- */
-
- ret = args->lastErr;
- args->lastErr = 0; /* reset */
- /* On error 'ret' will be negative - top bit set */
- mask = ((unsigned int)ret >>
- ((sizeof(ret) * 8) - 1)) - 1;
-
- /* build PreMasterSecret */
- ssl->arrays->preMasterSecret[0] = ssl->chVersion.major;
- ssl->arrays->preMasterSecret[1] = ssl->chVersion.minor;
-
- if (args->output != NULL) {
- /* Use random secret on error */
- for (i = VERSION_SZ; i < SECRET_LEN; i++) {
- ssl->arrays->preMasterSecret[i] =
- ctMaskSel(mask, args->output[i],
- ssl->arrays->preMasterSecret[i]);
- }
- }
- /* preMasterSecret has RNG and version set
- * return proper length and ignore error
- * error will be caught as decryption error
- */
- args->sigSz = SECRET_LEN;
- ret = 0;
- break;
- } /* rsa_kea */
- #endif /* !NO_RSA */
- #ifndef NO_PSK
- case psk_kea:
- {
- break;
- }
- #endif /* !NO_PSK */
- #ifdef HAVE_NTRU
- case ntru_kea:
- {
- break;
- }
- #endif /* HAVE_NTRU */
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- case ecc_diffie_hellman_kea:
- {
- /* skip past the imported peer key */
- args->idx += args->length;
- break;
- }
- #endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
- #ifndef NO_DH
- case diffie_hellman_kea:
- {
- args->idx += (word16)args->sigSz;
- break;
- }
- #endif /* !NO_DH */
- #if !defined(NO_DH) && !defined(NO_PSK)
- case dhe_psk_kea:
- {
- byte* pms = ssl->arrays->preMasterSecret;
- word16 clientSz = (word16)args->sigSz;
-
- args->idx += clientSz;
- c16toa((word16)ssl->arrays->preMasterSz, pms);
- ssl->arrays->preMasterSz += OPAQUE16_LEN;
- pms += ssl->arrays->preMasterSz;
-
- /* Use the PSK hint to look up the PSK and add it to the
- * preMasterSecret here. */
- ssl->arrays->psk_keySz = ssl->options.server_psk_cb(ssl,
- ssl->arrays->client_identity, ssl->arrays->psk_key,
- MAX_PSK_KEY_LEN);
-
- if (ssl->arrays->psk_keySz == 0 ||
- ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
- ERROR_OUT(PSK_KEY_ERROR, exit_dcke);
- }
-
- c16toa((word16) ssl->arrays->psk_keySz, pms);
- pms += OPAQUE16_LEN;
-
- XMEMCPY(pms, ssl->arrays->psk_key,
- ssl->arrays->psk_keySz);
- ssl->arrays->preMasterSz += ssl->arrays->psk_keySz +
- OPAQUE16_LEN;
- break;
- }
- #endif /* !NO_DH && !NO_PSK */
- #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && !defined(NO_PSK)
- case ecdhe_psk_kea:
- {
- byte* pms = ssl->arrays->preMasterSecret;
- word16 clientSz = (word16)args->sigSz;
-
- /* skip past the imported peer key */
- args->idx += args->length;
-
- /* Add preMasterSecret */
- c16toa(clientSz, pms);
- ssl->arrays->preMasterSz = OPAQUE16_LEN + clientSz;
- pms += ssl->arrays->preMasterSz;
-
- /* Use the PSK hint to look up the PSK and add it to the
- * preMasterSecret here. */
- ssl->arrays->psk_keySz = ssl->options.server_psk_cb(ssl,
- ssl->arrays->client_identity, ssl->arrays->psk_key,
- MAX_PSK_KEY_LEN);
-
- if (ssl->arrays->psk_keySz == 0 ||
- ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
- ERROR_OUT(PSK_KEY_ERROR, exit_dcke);
- }
-
- c16toa((word16) ssl->arrays->psk_keySz, pms);
- pms += OPAQUE16_LEN;
-
- XMEMCPY(pms, ssl->arrays->psk_key, ssl->arrays->psk_keySz);
- ssl->arrays->preMasterSz +=
- ssl->arrays->psk_keySz + OPAQUE16_LEN;
- break;
- }
- #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && !NO_PSK */
- default:
- ret = BAD_KEA_TYPE_E;
- } /* switch (ssl->specs.kea) */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_dcke;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_FINALIZE;
- } /* TLS_ASYNC_VERIFY */
- FALL_THROUGH;
-
- case TLS_ASYNC_FINALIZE:
- {
- if (IsEncryptionOn(ssl, 0)) {
- args->idx += ssl->keys.padSz;
- #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.startedETMRead)
- args->idx += MacSize(ssl);
- #endif
- }
-
- #ifdef HAVE_QSH
- word16 name;
-
- if (ssl->options.haveQSH) {
- /* extension name */
- ato16(input + args->idx, &name);
- args->idx += OPAQUE16_LEN;
-
- if (name == TLSX_QUANTUM_SAFE_HYBRID) {
- int qshSz;
- /* if qshSz is larger than 0 it is the
- length of buffer used */
- if ((qshSz = TLSX_QSHCipher_Parse(ssl,
- input + args->idx,
- size - args->idx + args->begin, 1)) < 0) {
- ERROR_OUT(qshSz, exit_dcke);
- }
- args->idx += qshSz;
- }
- else {
- /* unknown extension sent client ignored handshake */
- ERROR_OUT(BUFFER_ERROR, exit_dcke);
- }
- }
- #endif /* HAVE_QSH */
- ret = MakeMasterSecret(ssl);
-
- /* Check for error */
- if (ret != 0) {
- goto exit_dcke;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_END;
- } /* TLS_ASYNC_FINALIZE */
- FALL_THROUGH;
-
- case TLS_ASYNC_END:
- {
- /* Set final index */
- *inOutIdx = args->idx;
-
- ssl->options.clientState = CLIENT_KEYEXCHANGE_COMPLETE;
- #ifndef NO_CERTS
- if (ssl->options.verifyPeer) {
- ret = BuildCertHashes(ssl, &ssl->hsHashes->certHashes);
- }
- #endif
- break;
- } /* TLS_ASYNC_END */
- default:
- ret = INPUT_CASE_ERROR;
- } /* switch(ssl->options.asyncState) */
-
- exit_dcke:
-
- WOLFSSL_LEAVE("DoClientKeyExchange", ret);
- WOLFSSL_END(WC_FUNC_CLIENT_KEY_EXCHANGE_DO);
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* Handle async operation */
- if (ret == WC_PENDING_E) {
- /* Mark message as not received so it can process again */
- ssl->msgsReceived.got_client_key_exchange = 0;
-
- return ret;
- }
- #endif /* WOLFSSL_ASYNC_CRYPT */
-
- /* Cleanup PMS */
- if (ssl->arrays->preMasterSecret != NULL) {
- ForceZero(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
- }
- ssl->arrays->preMasterSz = 0;
-
- /* Final cleanup */
- FreeDckeArgs(ssl, args);
- FreeKeyExchange(ssl);
-
- return ret;
- }
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#if defined(OPENSSL_ALL) || defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
- defined(WOLFSSL_HAPROXY)
- int SNI_Callback(WOLFSSL* ssl)
- {
- /* Stunnel supports a custom sni callback to switch an SSL's ctx
- * when SNI is received. Call it now if exists */
- if(ssl && ssl->ctx && ssl->ctx->sniRecvCb) {
- WOLFSSL_MSG("Calling custom sni callback");
- if(ssl->ctx->sniRecvCb(ssl, NULL, ssl->ctx->sniRecvCbArg)
- == alert_fatal) {
- WOLFSSL_MSG("Error in custom sni callback. Fatal alert");
- SendAlert(ssl, alert_fatal, unrecognized_name);
- return FATAL_ERROR;
- }
- }
- return 0;
- }
-#endif /* OPENSSL_ALL || HAVE_STUNNEL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
-
-#endif /* NO_WOLFSSL_SERVER */
-
-
-#ifdef WOLFSSL_ASYNC_CRYPT
-int wolfSSL_AsyncPop(WOLFSSL* ssl, byte* state)
-{
- int ret = 0;
- WC_ASYNC_DEV* asyncDev;
- WOLF_EVENT* event;
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* check for pending async */
- asyncDev = ssl->async.dev;
- if (asyncDev) {
- /* grab event pointer */
- event = &asyncDev->event;
-
- ret = wolfAsync_EventPop(event, WOLF_EVENT_TYPE_ASYNC_WOLFSSL);
- if (ret != WC_NOT_PENDING_E && ret != WC_PENDING_E) {
-
- /* advance key share state if doesn't need called again */
- if (state && (asyncDev->event.flags & WC_ASYNC_FLAG_CALL_AGAIN) == 0) {
- (*state)++;
- }
-
- /* clear event */
- XMEMSET(&asyncDev->event, 0, sizeof(WOLF_EVENT));
-
- /* clear async dev */
- ssl->async.dev = NULL;
- }
- }
- else {
- ret = WC_NOT_PENDING_E;
- }
-
- WOLFSSL_LEAVE("wolfSSL_AsyncPop", ret);
-
- return ret;
-}
-
-int wolfSSL_AsyncInit(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev, word32 flags)
-{
- int ret;
- WOLF_EVENT* event;
-
- if (ssl == NULL || asyncDev == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* grab event pointer */
- event = &asyncDev->event;
-
- /* init event */
- ret = wolfAsync_EventInit(event, WOLF_EVENT_TYPE_ASYNC_WOLFSSL, ssl, flags);
-
- WOLFSSL_LEAVE("wolfSSL_AsyncInit", ret);
-
- return ret;
-}
-
-int wolfSSL_AsyncPush(WOLFSSL* ssl, WC_ASYNC_DEV* asyncDev)
-{
- int ret;
- WOLF_EVENT* event;
-
- if (ssl == NULL || asyncDev == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* grab event pointer */
- event = &asyncDev->event;
-
- /* store reference to active async operation */
- ssl->async.dev = asyncDev;
-
- /* place event into queue */
- ret = wolfAsync_EventQueuePush(&ssl->ctx->event_queue, event);
-
- /* success means return WC_PENDING_E */
- if (ret == 0) {
- ret = WC_PENDING_E;
- }
-
- WOLFSSL_LEAVE("wolfSSL_AsyncPush", ret);
-
- return ret;
-}
-
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
-
-/* return the max record size */
-int wolfSSL_GetMaxRecordSize(WOLFSSL* ssl, int maxFragment)
-{
- (void) ssl; /* Avoid compiler warnings */
-
- if (maxFragment > MAX_RECORD_SIZE) {
- maxFragment = MAX_RECORD_SIZE;
- }
-
-#ifdef HAVE_MAX_FRAGMENT
- if ((ssl->max_fragment != 0) && ((word16)maxFragment > ssl->max_fragment)) {
- maxFragment = ssl->max_fragment;
- }
-#endif /* HAVE_MAX_FRAGMENT */
-#ifdef WOLFSSL_DTLS
- if ((ssl->options.dtls) && (maxFragment > MAX_UDP_SIZE)) {
- maxFragment = MAX_UDP_SIZE;
- }
-#endif
-
- return maxFragment;
-}
-
-
-#undef ERROR_OUT
-
-#endif /* WOLFCRYPT_ONLY */
diff --git a/client/wolfssl/src/keys.c b/client/wolfssl/src/keys.c
deleted file mode 100644
index 55b2d9b..0000000
--- a/client/wolfssl/src/keys.c
+++ /dev/null
@@ -1,3607 +0,0 @@
-/* keys.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-/* Name change compatibility layer no longer needs to be included here */
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifndef WOLFCRYPT_ONLY
-
-#include
-#include
-#if defined(SHOW_SECRETS) || defined(CHACHA_AEAD_TEST)
- #ifndef NO_STDIO_FILESYSTEM
- #include
- #endif
-#endif
-
-#if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- int tsip_useable(const WOLFSSL *ssl);
-#endif
-int SetCipherSpecs(WOLFSSL* ssl)
-{
-#ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- /* server side verified before SetCipherSpecs call */
- if (VerifyClientSuite(ssl) != 1) {
- WOLFSSL_MSG("SetCipherSpecs() client has an unusable suite");
- return UNSUPPORTED_SUITE;
- }
- }
-#endif /* NO_WOLFSSL_CLIENT */
-
- /* Chacha extensions, 0xcc */
- if (ssl->options.cipherSuite0 == CHACHA_BYTE) {
-
- switch (ssl->options.cipherSuite) {
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
- case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
- ssl->specs.bulk_cipher_algorithm = wolfssl_chacha;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CHACHA20_256_KEY_SIZE;
- ssl->specs.block_size = CHACHA20_BLOCK_SIZE;
- ssl->specs.iv_size = CHACHA20_IV_SIZE;
- ssl->specs.aead_mac_size = POLY1305_AUTH_SZ;
- ssl->options.oldPoly = 1; /* use old poly1305 padding */
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256
- case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
- ssl->specs.bulk_cipher_algorithm = wolfssl_chacha;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CHACHA20_256_KEY_SIZE;
- ssl->specs.block_size = CHACHA20_BLOCK_SIZE;
- ssl->specs.iv_size = CHACHA20_IV_SIZE;
- ssl->specs.aead_mac_size = POLY1305_AUTH_SZ;
- ssl->options.oldPoly = 1; /* use old poly1305 padding */
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256
- case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
- ssl->specs.bulk_cipher_algorithm = wolfssl_chacha;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CHACHA20_256_KEY_SIZE;
- ssl->specs.block_size = CHACHA20_BLOCK_SIZE;
- ssl->specs.iv_size = CHACHA20_IV_SIZE;
- ssl->specs.aead_mac_size = POLY1305_AUTH_SZ;
- ssl->options.oldPoly = 1; /* use old poly1305 padding */
-
- break;
-#endif
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
- case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
- ssl->specs.bulk_cipher_algorithm = wolfssl_chacha;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CHACHA20_256_KEY_SIZE;
- ssl->specs.block_size = CHACHA20_BLOCK_SIZE;
- ssl->specs.iv_size = CHACHA20_IV_SIZE;
- ssl->specs.aead_mac_size = POLY1305_AUTH_SZ;
- ssl->options.oldPoly = 0; /* use recent padding RFC */
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
- case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256:
- ssl->specs.bulk_cipher_algorithm = wolfssl_chacha;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CHACHA20_256_KEY_SIZE;
- ssl->specs.block_size = CHACHA20_BLOCK_SIZE;
- ssl->specs.iv_size = CHACHA20_IV_SIZE;
- ssl->specs.aead_mac_size = POLY1305_AUTH_SZ;
- ssl->options.oldPoly = 0; /* use recent padding RFC */
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256
- case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
- ssl->specs.bulk_cipher_algorithm = wolfssl_chacha;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CHACHA20_256_KEY_SIZE;
- ssl->specs.block_size = CHACHA20_BLOCK_SIZE;
- ssl->specs.iv_size = CHACHA20_IV_SIZE;
- ssl->specs.aead_mac_size = POLY1305_AUTH_SZ;
- ssl->options.oldPoly = 0; /* use recent padding RFC */
-
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_CHACHA20_POLY1305_SHA256
- case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
- ssl->specs.bulk_cipher_algorithm = wolfssl_chacha;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CHACHA20_256_KEY_SIZE;
- ssl->specs.block_size = CHACHA20_BLOCK_SIZE;
- ssl->specs.iv_size = CHACHA20_IV_SIZE;
- ssl->specs.aead_mac_size = POLY1305_AUTH_SZ;
-
- ssl->options.oldPoly = 0; /* use recent padding RFC */
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256
- case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
- ssl->specs.bulk_cipher_algorithm = wolfssl_chacha;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecdhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CHACHA20_256_KEY_SIZE;
- ssl->specs.block_size = CHACHA20_BLOCK_SIZE;
- ssl->specs.iv_size = CHACHA20_IV_SIZE;
- ssl->specs.aead_mac_size = POLY1305_AUTH_SZ;
-
- ssl->options.oldPoly = 0; /* use recent padding RFC */
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256
- case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
- ssl->specs.bulk_cipher_algorithm = wolfssl_chacha;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = dhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CHACHA20_256_KEY_SIZE;
- ssl->specs.block_size = CHACHA20_BLOCK_SIZE;
- ssl->specs.iv_size = CHACHA20_IV_SIZE;
- ssl->specs.aead_mac_size = POLY1305_AUTH_SZ;
-
- ssl->options.oldPoly = 0; /* use recent padding RFC */
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
- default:
- WOLFSSL_MSG("Unsupported cipher suite, SetCipherSpecs ChaCha");
- return UNSUPPORTED_SUITE;
- }
- }
-
- /* ECC extensions, AES-CCM or TLS 1.3 Integrity-only */
- if (ssl->options.cipherSuite0 == ECC_BYTE) {
-
- switch (ssl->options.cipherSuite) {
-
-#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
- case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
- case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
- case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA
- case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_triple_des;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = DES3_KEY_SIZE;
- ssl->specs.block_size = DES_BLOCK_SIZE;
- ssl->specs.iv_size = DES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_RC4_128_SHA
- case TLS_ECDHE_RSA_WITH_RC4_128_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_rc4;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = RC4_KEY_SIZE;
- ssl->specs.iv_size = 0;
- ssl->specs.block_size = 0;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
- case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
- case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
- case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_PSK_WITH_NULL_SHA256
- case TLS_ECDHE_PSK_WITH_NULL_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecdhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = 0;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = 0;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256
- case TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecdhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#endif /* HAVE_ECC || HAVE_CURVE25519 || HAVE_CURVE448 */
-
-#if defined(HAVE_ECC) || (defined(HAVE_CURVE25519) && defined(HAVE_ED25519)) \
- || (defined(HAVE_CURVE448) && defined(HAVE_ED448))
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256
- case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384
- case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA
- case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_triple_des;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = DES3_KEY_SIZE;
- ssl->specs.block_size = DES_BLOCK_SIZE;
- ssl->specs.iv_size = DES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_RC4_128_SHA
- case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_rc4;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = RC4_KEY_SIZE;
- ssl->specs.iv_size = 0;
- ssl->specs.block_size = 0;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
- case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
- case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
- case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
- case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM
- case TLS_ECDHE_ECDSA_WITH_AES_128_CCM :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_16_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8
- case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_8_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8
- case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_8_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDHE_ECDSA_WITH_NULL_SHA
- case TLS_ECDHE_ECDSA_WITH_NULL_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = 0;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = 0;
-
- break;
-#endif
-
-#endif /* HAVE_ECC || (CURVE25519 && ED25519) || (CURVE448 && ED448) */
-
-#if defined(HAVE_ECC)
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256
- case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256
- case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384
- case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384
- case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA
- case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA
- case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_triple_des;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = DES3_KEY_SIZE;
- ssl->specs.block_size = DES_BLOCK_SIZE;
- ssl->specs.iv_size = DES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_RC4_128_SHA
- case TLS_ECDH_RSA_WITH_RC4_128_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_rc4;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = RC4_KEY_SIZE;
- ssl->specs.iv_size = 0;
- ssl->specs.block_size = 0;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA
- case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_triple_des;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = DES3_KEY_SIZE;
- ssl->specs.block_size = DES_BLOCK_SIZE;
- ssl->specs.iv_size = DES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_RC4_128_SHA
- case TLS_ECDH_ECDSA_WITH_RC4_128_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_rc4;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = RC4_KEY_SIZE;
- ssl->specs.iv_size = 0;
- ssl->specs.block_size = 0;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA
- case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA
- case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA
- case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256
- case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384
- case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256
- case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384
- case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = ecc_diffie_hellman_kea;
- ssl->specs.sig_algo = ecc_dsa_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 1;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#endif /* HAVE_ECC */
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_CCM_8
- case TLS_RSA_WITH_AES_128_CCM_8 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_8_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_CCM_8
- case TLS_RSA_WITH_AES_256_CCM_8 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_8_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CCM_8
- case TLS_PSK_WITH_AES_128_CCM_8 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_8_AUTH_SZ;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CCM_8
- case TLS_PSK_WITH_AES_256_CCM_8 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_8_AUTH_SZ;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CCM
- case TLS_PSK_WITH_AES_128_CCM :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_16_AUTH_SZ;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CCM
- case TLS_PSK_WITH_AES_256_CCM :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_16_AUTH_SZ;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_CCM
- case TLS_DHE_PSK_WITH_AES_128_CCM :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = dhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_16_AUTH_SZ;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_CCM
- case TLS_DHE_PSK_WITH_AES_256_CCM :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = dhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_CCM_16_AUTH_SZ;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#if defined(WOLFSSL_TLS13) && defined(HAVE_NULL_CIPHER)
- #ifdef BUILD_TLS_SHA256_SHA256
- case TLS_SHA256_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = 0;
- ssl->specs.sig_algo = 0;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = WC_SHA256_DIGEST_SIZE / 2;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = HMAC_NONCE_SZ;
- ssl->specs.aead_mac_size = WC_SHA256_DIGEST_SIZE;
-
- break;
- #endif
-
- #ifdef BUILD_TLS_SHA384_SHA384
- case TLS_SHA384_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = 0;
- ssl->specs.sig_algo = 0;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = WC_SHA384_DIGEST_SIZE / 2;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = HMAC_NONCE_SZ;
- ssl->specs.aead_mac_size = WC_SHA384_DIGEST_SIZE;
-
- break;
- #endif
-#endif
-
- default:
- WOLFSSL_MSG("Unsupported cipher suite, SetCipherSpecs ECC");
- return UNSUPPORTED_SUITE;
- } /* switch */
- } /* if */
-
- /* TLSi v1.3 cipher suites, 0x13 */
- if (ssl->options.cipherSuite0 == TLS13_BYTE) {
- switch (ssl->options.cipherSuite) {
-
-#ifdef WOLFSSL_TLS13
- #ifdef BUILD_TLS_AES_128_GCM_SHA256
- case TLS_AES_128_GCM_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = 0;
- ssl->specs.sig_algo = 0;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_NONCE_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
- #endif
-
- #ifdef BUILD_TLS_AES_256_GCM_SHA384
- case TLS_AES_256_GCM_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = 0;
- ssl->specs.sig_algo = 0;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_NONCE_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
- #endif
-
- #ifdef BUILD_TLS_CHACHA20_POLY1305_SHA256
- case TLS_CHACHA20_POLY1305_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_chacha;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = 0;
- ssl->specs.sig_algo = 0;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CHACHA20_256_KEY_SIZE;
- ssl->specs.block_size = CHACHA20_BLOCK_SIZE;
- ssl->specs.iv_size = CHACHA20_IV_SIZE;
- ssl->specs.aead_mac_size = POLY1305_AUTH_SZ;
- ssl->options.oldPoly = 0; /* use recent padding RFC */
-
- break;
- #endif
-
- #ifdef BUILD_TLS_AES_128_CCM_SHA256
- case TLS_AES_128_CCM_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = 0;
- ssl->specs.sig_algo = 0;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_NONCE_SZ;
- ssl->specs.aead_mac_size = AES_CCM_16_AUTH_SZ;
-
- break;
- #endif
-
- #ifdef BUILD_TLS_AES_128_CCM_8_SHA256
- case TLS_AES_128_CCM_8_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_ccm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = 0;
- ssl->specs.sig_algo = 0;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_NONCE_SZ;
- ssl->specs.aead_mac_size = AES_CCM_8_AUTH_SZ;
-
- break;
- #endif
-#endif /* WOLFSSL_TLS13 */
- }
- }
-
- if (ssl->options.cipherSuite0 != ECC_BYTE &&
- ssl->options.cipherSuite0 != CHACHA_BYTE &&
- ssl->options.cipherSuite0 != TLS13_BYTE) { /* normal suites */
- switch (ssl->options.cipherSuite) {
-
-#ifdef BUILD_SSL_RSA_WITH_RC4_128_SHA
- case SSL_RSA_WITH_RC4_128_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_rc4;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = RC4_KEY_SIZE;
- ssl->specs.iv_size = 0;
- ssl->specs.block_size = 0;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_RC4_128_SHA
- case TLS_NTRU_RSA_WITH_RC4_128_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_rc4;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ntru_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = RC4_KEY_SIZE;
- ssl->specs.iv_size = 0;
- ssl->specs.block_size = 0;
-
- break;
-#endif
-
-#ifdef BUILD_SSL_RSA_WITH_RC4_128_MD5
- case SSL_RSA_WITH_RC4_128_MD5 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_rc4;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = md5_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_MD5_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_MD5;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = RC4_KEY_SIZE;
- ssl->specs.iv_size = 0;
- ssl->specs.block_size = 0;
-
- break;
-#endif
-
-#ifdef BUILD_SSL_RSA_WITH_3DES_EDE_CBC_SHA
- case SSL_RSA_WITH_3DES_EDE_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_triple_des;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = DES3_KEY_SIZE;
- ssl->specs.block_size = DES_BLOCK_SIZE;
- ssl->specs.iv_size = DES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA
- case TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_triple_des;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ntru_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = DES3_KEY_SIZE;
- ssl->specs.block_size = DES_BLOCK_SIZE;
- ssl->specs.iv_size = DES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA
- case TLS_RSA_WITH_AES_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_CBC_SHA256
- case TLS_RSA_WITH_AES_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_NULL_MD5
- case TLS_RSA_WITH_NULL_MD5 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = md5_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_MD5_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_MD5;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = 0;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = 0;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_NULL_SHA
- case TLS_RSA_WITH_NULL_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = 0;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = 0;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_NULL_SHA256
- case TLS_RSA_WITH_NULL_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = 0;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = 0;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_AES_128_CBC_SHA
- case TLS_NTRU_RSA_WITH_AES_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ntru_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA
- case TLS_RSA_WITH_AES_256_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_CBC_SHA256
- case TLS_RSA_WITH_AES_256_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_NTRU_RSA_WITH_AES_256_CBC_SHA
- case TLS_NTRU_RSA_WITH_AES_256_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = ntru_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_GCM_SHA256
- case TLS_PSK_WITH_AES_128_GCM_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_GCM_SHA384
- case TLS_PSK_WITH_AES_256_GCM_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DH_anon_WITH_AES_256_GCM_SHA384
- case TLS_DH_anon_WITH_AES_256_GCM_SHA384:
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- ssl->options.usingAnon_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_GCM_SHA256
- case TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = dhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_GCM_SHA384
- case TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = dhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA256
- case TLS_PSK_WITH_AES_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA384
- case TLS_PSK_WITH_AES_256_CBC_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_128_CBC_SHA256
- case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = dhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_AES_256_CBC_SHA384
- case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = dhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_128_CBC_SHA
- case TLS_PSK_WITH_AES_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_AES_256_CBC_SHA
- case TLS_PSK_WITH_AES_256_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_NULL_SHA256
- case TLS_PSK_WITH_NULL_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = 0;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = 0;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_NULL_SHA384
- case TLS_PSK_WITH_NULL_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = 0;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = 0;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_PSK_WITH_NULL_SHA
- case TLS_PSK_WITH_NULL_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = 0;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = 0;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_NULL_SHA256
- case TLS_DHE_PSK_WITH_NULL_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = dhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = 0;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = 0;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_PSK_WITH_NULL_SHA384
- case TLS_DHE_PSK_WITH_NULL_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = dhe_psk_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = 0;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = 0;
-
- ssl->options.usingPSK_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
- case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA
- case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_triple_des;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = DES3_KEY_SIZE;
- ssl->specs.block_size = DES_BLOCK_SIZE;
- ssl->specs.iv_size = DES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
- case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
- case TLS_DHE_RSA_WITH_AES_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_CBC_SHA
- case TLS_DHE_RSA_WITH_AES_256_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_HC_128_MD5
- case TLS_RSA_WITH_HC_128_MD5 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_hc128;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = md5_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_MD5_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_MD5;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = HC_128_KEY_SIZE;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = HC_128_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_HC_128_SHA
- case TLS_RSA_WITH_HC_128_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_hc128;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = HC_128_KEY_SIZE;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = HC_128_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_RABBIT_SHA
- case TLS_RSA_WITH_RABBIT_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_rabbit;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = RABBIT_KEY_SIZE;
- ssl->specs.block_size = 0;
- ssl->specs.iv_size = RABBIT_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_128_GCM_SHA256
- case TLS_RSA_WITH_AES_128_GCM_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_AES_256_GCM_SHA384
- case TLS_RSA_WITH_AES_256_GCM_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
- case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_AES_256_GCM_SHA384
- case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes_gcm;
- ssl->specs.cipher_type = aead;
- ssl->specs.mac_algorithm = sha384_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA384_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_256_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AESGCM_IMP_IV_SZ;
- ssl->specs.aead_mac_size = AES_GCM_AUTH_SZ;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA
- case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_camellia;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CAMELLIA_128_KEY_SIZE;
- ssl->specs.block_size = CAMELLIA_BLOCK_SIZE;
- ssl->specs.iv_size = CAMELLIA_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA
- case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_camellia;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CAMELLIA_256_KEY_SIZE;
- ssl->specs.block_size = CAMELLIA_BLOCK_SIZE;
- ssl->specs.iv_size = CAMELLIA_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256
- case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_camellia;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CAMELLIA_128_KEY_SIZE;
- ssl->specs.block_size = CAMELLIA_BLOCK_SIZE;
- ssl->specs.iv_size = CAMELLIA_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256
- case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_camellia;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CAMELLIA_256_KEY_SIZE;
- ssl->specs.block_size = CAMELLIA_BLOCK_SIZE;
- ssl->specs.iv_size = CAMELLIA_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA
- case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_camellia;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CAMELLIA_128_KEY_SIZE;
- ssl->specs.block_size = CAMELLIA_BLOCK_SIZE;
- ssl->specs.iv_size = CAMELLIA_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA
- case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_camellia;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CAMELLIA_256_KEY_SIZE;
- ssl->specs.block_size = CAMELLIA_BLOCK_SIZE;
- ssl->specs.iv_size = CAMELLIA_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256
- case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_camellia;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CAMELLIA_128_KEY_SIZE;
- ssl->specs.block_size = CAMELLIA_BLOCK_SIZE;
- ssl->specs.iv_size = CAMELLIA_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256
- case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_camellia;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = CAMELLIA_256_KEY_SIZE;
- ssl->specs.block_size = CAMELLIA_BLOCK_SIZE;
- ssl->specs.iv_size = CAMELLIA_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_TLS_DH_anon_WITH_AES_128_CBC_SHA
- case TLS_DH_anon_WITH_AES_128_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_aes;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = diffie_hellman_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = AES_128_KEY_SIZE;
- ssl->specs.block_size = AES_BLOCK_SIZE;
- ssl->specs.iv_size = AES_IV_SIZE;
-
- ssl->options.usingAnon_cipher = 1;
- break;
-#endif
-
-#ifdef BUILD_SSL_RSA_WITH_IDEA_CBC_SHA
- case SSL_RSA_WITH_IDEA_CBC_SHA :
- ssl->specs.bulk_cipher_algorithm = wolfssl_idea;
- ssl->specs.cipher_type = block;
- ssl->specs.mac_algorithm = sha_mac;
- ssl->specs.kea = rsa_kea;
- ssl->specs.sig_algo = rsa_sa_algo;
- ssl->specs.hash_size = WC_SHA_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
- ssl->specs.static_ecdh = 0;
- ssl->specs.key_size = IDEA_KEY_SIZE;
- ssl->specs.block_size = IDEA_BLOCK_SIZE;
- ssl->specs.iv_size = IDEA_IV_SIZE;
-
- break;
-#endif
-
-#ifdef BUILD_WDM_WITH_NULL_SHA256
- case WDM_WITH_NULL_SHA256 :
- ssl->specs.bulk_cipher_algorithm = wolfssl_cipher_null;
- ssl->specs.cipher_type = stream;
- ssl->specs.mac_algorithm = sha256_mac;
- ssl->specs.kea = no_kea;
- ssl->specs.sig_algo = anonymous_sa_algo;
- ssl->specs.hash_size = WC_SHA256_DIGEST_SIZE;
- ssl->specs.pad_size = PAD_SHA;
-
- break;
-#endif
-
- default:
- WOLFSSL_MSG("Unsupported cipher suite, SetCipherSpecs");
- return UNSUPPORTED_SUITE;
- } /* switch */
- } /* if ECC / Normal suites else */
-
- /* set TLS if it hasn't been turned off */
- if (ssl->version.major == 3 && ssl->version.minor >= 1) {
-#ifndef NO_TLS
- ssl->options.tls = 1;
- #if !defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_AEAD_ONLY)
- ssl->hmac = TLS_hmac;
- #endif
- if (ssl->version.minor >= 2) {
- ssl->options.tls1_1 = 1;
- if (ssl->version.minor >= 4)
- ssl->options.tls1_3 = 1;
- }
-#endif
- }
-
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (IsAtLeastTLSv1_3(ssl->version) || ssl->specs.cipher_type != block)
- ssl->options.encThenMac = 0;
-#endif
-
-#if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_AEAD_ONLY)
- if (ssl->options.dtls)
- ssl->hmac = TLS_hmac;
-#endif
-
- return 0;
-}
-
-
-enum KeyStuff {
- MASTER_ROUNDS = 3,
- PREFIX = 3, /* up to three letters for master prefix */
- KEY_PREFIX = 9 /* up to 9 prefix letters for key rounds */
-
-
-};
-
-#ifndef NO_OLD_TLS
-/* true or false, zero for error */
-static int SetPrefix(byte* sha_input, int idx)
-{
- switch (idx) {
- case 0:
- XMEMCPY(sha_input, "A", 1);
- break;
- case 1:
- XMEMCPY(sha_input, "BB", 2);
- break;
- case 2:
- XMEMCPY(sha_input, "CCC", 3);
- break;
- case 3:
- XMEMCPY(sha_input, "DDDD", 4);
- break;
- case 4:
- XMEMCPY(sha_input, "EEEEE", 5);
- break;
- case 5:
- XMEMCPY(sha_input, "FFFFFF", 6);
- break;
- case 6:
- XMEMCPY(sha_input, "GGGGGGG", 7);
- break;
- case 7:
- XMEMCPY(sha_input, "HHHHHHHH", 8);
- break;
- case 8:
- XMEMCPY(sha_input, "IIIIIIIII", 9);
- break;
- default:
- WOLFSSL_MSG("Set Prefix error, bad input");
- return 0;
- }
- return 1;
-}
-#endif
-
-
-static int SetKeys(Ciphers* enc, Ciphers* dec, Keys* keys, CipherSpecs* specs,
- int side, void* heap, int devId, WC_RNG* rng, int tls13)
-{
- (void)rng;
- (void)tls13;
-
-#ifdef BUILD_ARC4
- if (specs->bulk_cipher_algorithm == wolfssl_rc4) {
- word32 sz = specs->key_size;
- if (enc && enc->arc4 == NULL) {
- enc->arc4 = (Arc4*)XMALLOC(sizeof(Arc4), heap, DYNAMIC_TYPE_CIPHER);
- if (enc->arc4 == NULL)
- return MEMORY_E;
- }
- if (dec && dec->arc4 == NULL) {
- dec->arc4 = (Arc4*)XMALLOC(sizeof(Arc4), heap, DYNAMIC_TYPE_CIPHER);
- if (dec->arc4 == NULL)
- return MEMORY_E;
- }
-
- if (enc) {
- if (wc_Arc4Init(enc->arc4, heap, devId) != 0) {
- WOLFSSL_MSG("Arc4Init failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
- if (dec) {
- if (wc_Arc4Init(dec->arc4, heap, devId) != 0) {
- WOLFSSL_MSG("Arc4Init failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
-
- if (side == WOLFSSL_CLIENT_END) {
- if (enc)
- wc_Arc4SetKey(enc->arc4, keys->client_write_key, sz);
- if (dec)
- wc_Arc4SetKey(dec->arc4, keys->server_write_key, sz);
- }
- else {
- if (enc)
- wc_Arc4SetKey(enc->arc4, keys->server_write_key, sz);
- if (dec)
- wc_Arc4SetKey(dec->arc4, keys->client_write_key, sz);
- }
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif /* BUILD_ARC4 */
-
-
-#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- /* Check that the max implicit iv size is suffecient */
- #if (AEAD_MAX_IMP_SZ < 12) /* CHACHA20_IMP_IV_SZ */
- #error AEAD_MAX_IMP_SZ is too small for ChaCha20
- #endif
- #if (MAX_WRITE_IV_SZ < 12) /* CHACHA20_IMP_IV_SZ */
- #error MAX_WRITE_IV_SZ is too small for ChaCha20
- #endif
-
- if (specs->bulk_cipher_algorithm == wolfssl_chacha) {
- int chachaRet;
- if (enc && enc->chacha == NULL)
- enc->chacha =
- (ChaCha*)XMALLOC(sizeof(ChaCha), heap, DYNAMIC_TYPE_CIPHER);
- if (enc && enc->chacha == NULL)
- return MEMORY_E;
- if (dec && dec->chacha == NULL)
- dec->chacha =
- (ChaCha*)XMALLOC(sizeof(ChaCha), heap, DYNAMIC_TYPE_CIPHER);
- if (dec && dec->chacha == NULL)
- return MEMORY_E;
- if (side == WOLFSSL_CLIENT_END) {
- if (enc) {
- chachaRet = wc_Chacha_SetKey(enc->chacha, keys->client_write_key,
- specs->key_size);
- XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
- CHACHA20_IMP_IV_SZ);
- if (chachaRet != 0) return chachaRet;
- }
- if (dec) {
- chachaRet = wc_Chacha_SetKey(dec->chacha, keys->server_write_key,
- specs->key_size);
- XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
- CHACHA20_IMP_IV_SZ);
- if (chachaRet != 0) return chachaRet;
- }
- }
- else {
- if (enc) {
- chachaRet = wc_Chacha_SetKey(enc->chacha, keys->server_write_key,
- specs->key_size);
- XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
- CHACHA20_IMP_IV_SZ);
- if (chachaRet != 0) return chachaRet;
- }
- if (dec) {
- chachaRet = wc_Chacha_SetKey(dec->chacha, keys->client_write_key,
- specs->key_size);
- XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
- CHACHA20_IMP_IV_SZ);
- if (chachaRet != 0) return chachaRet;
- }
- }
-
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif /* HAVE_CHACHA && HAVE_POLY1305 */
-
-
-#ifdef HAVE_HC128
- /* check that buffer sizes are sufficient */
- #if (MAX_WRITE_IV_SZ < 16) /* HC_128_IV_SIZE */
- #error MAX_WRITE_IV_SZ too small for HC128
- #endif
-
- if (specs->bulk_cipher_algorithm == wolfssl_hc128) {
- int hcRet;
- if (enc && enc->hc128 == NULL)
- enc->hc128 =
- (HC128*)XMALLOC(sizeof(HC128), heap, DYNAMIC_TYPE_CIPHER);
- if (enc && enc->hc128 == NULL)
- return MEMORY_E;
- if (dec && dec->hc128 == NULL)
- dec->hc128 =
- (HC128*)XMALLOC(sizeof(HC128), heap, DYNAMIC_TYPE_CIPHER);
- if (dec && dec->hc128 == NULL)
- return MEMORY_E;
- if (side == WOLFSSL_CLIENT_END) {
- if (enc) {
- hcRet = wc_Hc128_SetKey(enc->hc128, keys->client_write_key,
- keys->client_write_IV);
- if (hcRet != 0) return hcRet;
- }
- if (dec) {
- hcRet = wc_Hc128_SetKey(dec->hc128, keys->server_write_key,
- keys->server_write_IV);
- if (hcRet != 0) return hcRet;
- }
- }
- else {
- if (enc) {
- hcRet = wc_Hc128_SetKey(enc->hc128, keys->server_write_key,
- keys->server_write_IV);
- if (hcRet != 0) return hcRet;
- }
- if (dec) {
- hcRet = wc_Hc128_SetKey(dec->hc128, keys->client_write_key,
- keys->client_write_IV);
- if (hcRet != 0) return hcRet;
- }
- }
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif /* HAVE_HC128 */
-
-#ifdef BUILD_RABBIT
- /* check that buffer sizes are sufficient */
- #if (MAX_WRITE_IV_SZ < 8) /* RABBIT_IV_SIZE */
- #error MAX_WRITE_IV_SZ too small for RABBIT
- #endif
-
- if (specs->bulk_cipher_algorithm == wolfssl_rabbit) {
- int rabRet;
- if (enc && enc->rabbit == NULL)
- enc->rabbit =
- (Rabbit*)XMALLOC(sizeof(Rabbit), heap, DYNAMIC_TYPE_CIPHER);
- if (enc && enc->rabbit == NULL)
- return MEMORY_E;
- if (dec && dec->rabbit == NULL)
- dec->rabbit =
- (Rabbit*)XMALLOC(sizeof(Rabbit), heap, DYNAMIC_TYPE_CIPHER);
- if (dec && dec->rabbit == NULL)
- return MEMORY_E;
- if (side == WOLFSSL_CLIENT_END) {
- if (enc) {
- rabRet = wc_RabbitSetKey(enc->rabbit, keys->client_write_key,
- keys->client_write_IV);
- if (rabRet != 0) return rabRet;
- }
- if (dec) {
- rabRet = wc_RabbitSetKey(dec->rabbit, keys->server_write_key,
- keys->server_write_IV);
- if (rabRet != 0) return rabRet;
- }
- }
- else {
- if (enc) {
- rabRet = wc_RabbitSetKey(enc->rabbit, keys->server_write_key,
- keys->server_write_IV);
- if (rabRet != 0) return rabRet;
- }
- if (dec) {
- rabRet = wc_RabbitSetKey(dec->rabbit, keys->client_write_key,
- keys->client_write_IV);
- if (rabRet != 0) return rabRet;
- }
- }
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif /* BUILD_RABBIT */
-
-#ifdef BUILD_DES3
- /* check that buffer sizes are sufficient */
- #if (MAX_WRITE_IV_SZ < 8) /* DES_IV_SIZE */
- #error MAX_WRITE_IV_SZ too small for 3DES
- #endif
-
- if (specs->bulk_cipher_algorithm == wolfssl_triple_des) {
- int desRet = 0;
-
- if (enc) {
- if (enc->des3 == NULL)
- enc->des3 = (Des3*)XMALLOC(sizeof(Des3), heap, DYNAMIC_TYPE_CIPHER);
- if (enc->des3 == NULL)
- return MEMORY_E;
- XMEMSET(enc->des3, 0, sizeof(Des3));
- }
- if (dec) {
- if (dec->des3 == NULL)
- dec->des3 = (Des3*)XMALLOC(sizeof(Des3), heap, DYNAMIC_TYPE_CIPHER);
- if (dec->des3 == NULL)
- return MEMORY_E;
- XMEMSET(dec->des3, 0, sizeof(Des3));
- }
-
- if (enc) {
- if (wc_Des3Init(enc->des3, heap, devId) != 0) {
- WOLFSSL_MSG("Des3Init failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
- if (dec) {
- if (wc_Des3Init(dec->des3, heap, devId) != 0) {
- WOLFSSL_MSG("Des3Init failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
-
- if (side == WOLFSSL_CLIENT_END) {
- if (enc) {
- desRet = wc_Des3_SetKey(enc->des3, keys->client_write_key,
- keys->client_write_IV, DES_ENCRYPTION);
- if (desRet != 0) return desRet;
- }
- if (dec) {
- desRet = wc_Des3_SetKey(dec->des3, keys->server_write_key,
- keys->server_write_IV, DES_DECRYPTION);
- if (desRet != 0) return desRet;
- }
- }
- else {
- if (enc) {
- desRet = wc_Des3_SetKey(enc->des3, keys->server_write_key,
- keys->server_write_IV, DES_ENCRYPTION);
- if (desRet != 0) return desRet;
- }
- if (dec) {
- desRet = wc_Des3_SetKey(dec->des3, keys->client_write_key,
- keys->client_write_IV, DES_DECRYPTION);
- if (desRet != 0) return desRet;
- }
- }
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif /* BUILD_DES3 */
-
-#ifdef BUILD_AES
- /* check that buffer sizes are sufficient */
- #if (MAX_WRITE_IV_SZ < 16) /* AES_IV_SIZE */
- #error MAX_WRITE_IV_SZ too small for AES
- #endif
-
- if (specs->bulk_cipher_algorithm == wolfssl_aes) {
- int aesRet = 0;
-
- if (enc) {
- if (enc->aes == NULL)
- enc->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
- if (enc->aes == NULL)
- return MEMORY_E;
- XMEMSET(enc->aes, 0, sizeof(Aes));
- }
- if (dec) {
- if (dec->aes == NULL)
- dec->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
- if (dec->aes == NULL)
- return MEMORY_E;
- XMEMSET(dec->aes, 0, sizeof(Aes));
- }
- if (enc) {
- if (wc_AesInit(enc->aes, heap, devId) != 0) {
- WOLFSSL_MSG("AesInit failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
- if (dec) {
- if (wc_AesInit(dec->aes, heap, devId) != 0) {
- WOLFSSL_MSG("AesInit failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
-
- if (side == WOLFSSL_CLIENT_END) {
- if (enc) {
- aesRet = wc_AesSetKey(enc->aes, keys->client_write_key,
- specs->key_size, keys->client_write_IV,
- AES_ENCRYPTION);
- if (aesRet != 0) return aesRet;
- }
- if (dec) {
- aesRet = wc_AesSetKey(dec->aes, keys->server_write_key,
- specs->key_size, keys->server_write_IV,
- AES_DECRYPTION);
- if (aesRet != 0) return aesRet;
- }
- }
- else {
- if (enc) {
- aesRet = wc_AesSetKey(enc->aes, keys->server_write_key,
- specs->key_size, keys->server_write_IV,
- AES_ENCRYPTION);
- if (aesRet != 0) return aesRet;
- }
- if (dec) {
- aesRet = wc_AesSetKey(dec->aes, keys->client_write_key,
- specs->key_size, keys->client_write_IV,
- AES_DECRYPTION);
- if (aesRet != 0) return aesRet;
- }
- }
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif /* BUILD_AES */
-
-#ifdef BUILD_AESGCM
- /* check that buffer sizes are sufficient */
- #if (AEAD_MAX_IMP_SZ < 4) /* AESGCM_IMP_IV_SZ */
- #error AEAD_MAX_IMP_SZ too small for AESGCM
- #endif
- #if (AEAD_MAX_EXP_SZ < 8) /* AESGCM_EXP_IV_SZ */
- #error AEAD_MAX_EXP_SZ too small for AESGCM
- #endif
- #if (MAX_WRITE_IV_SZ < 4) /* AESGCM_IMP_IV_SZ */
- #error MAX_WRITE_IV_SZ too small for AESGCM
- #endif
-
- if (specs->bulk_cipher_algorithm == wolfssl_aes_gcm) {
- int gcmRet;
-
- if (enc) {
- if (enc->aes == NULL)
- enc->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
- if (enc->aes == NULL)
- return MEMORY_E;
- XMEMSET(enc->aes, 0, sizeof(Aes));
- }
- if (dec) {
- if (dec->aes == NULL)
- dec->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
- if (dec->aes == NULL)
- return MEMORY_E;
- XMEMSET(dec->aes, 0, sizeof(Aes));
- }
-
- if (enc) {
- if (wc_AesInit(enc->aes, heap, devId) != 0) {
- WOLFSSL_MSG("AesInit failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
- if (dec) {
- if (wc_AesInit(dec->aes, heap, devId) != 0) {
- WOLFSSL_MSG("AesInit failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
-
- if (side == WOLFSSL_CLIENT_END) {
- if (enc) {
- gcmRet = wc_AesGcmSetKey(enc->aes, keys->client_write_key,
- specs->key_size);
- if (gcmRet != 0) return gcmRet;
- XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
- AEAD_MAX_IMP_SZ);
-#if !defined(NO_PUBLIC_GCM_SET_IV) && \
- ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
- if (!tls13) {
- gcmRet = wc_AesGcmSetIV(enc->aes, AESGCM_NONCE_SZ,
- keys->client_write_IV, AESGCM_IMP_IV_SZ, rng);
- if (gcmRet != 0) return gcmRet;
- }
-#endif
- }
- if (dec) {
- gcmRet = wc_AesGcmSetKey(dec->aes, keys->server_write_key,
- specs->key_size);
- if (gcmRet != 0) return gcmRet;
- XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
- AEAD_MAX_IMP_SZ);
- }
- }
- else {
- if (enc) {
- gcmRet = wc_AesGcmSetKey(enc->aes, keys->server_write_key,
- specs->key_size);
- if (gcmRet != 0) return gcmRet;
- XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
- AEAD_MAX_IMP_SZ);
-#if !defined(NO_PUBLIC_GCM_SET_IV) && \
- ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
- if (!tls13) {
- gcmRet = wc_AesGcmSetIV(enc->aes, AESGCM_NONCE_SZ,
- keys->server_write_IV, AESGCM_IMP_IV_SZ, rng);
- if (gcmRet != 0) return gcmRet;
- }
-#endif
- }
- if (dec) {
- gcmRet = wc_AesGcmSetKey(dec->aes, keys->client_write_key,
- specs->key_size);
- if (gcmRet != 0) return gcmRet;
- XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
- AEAD_MAX_IMP_SZ);
- }
- }
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif /* BUILD_AESGCM */
-
-#ifdef HAVE_AESCCM
- /* check that buffer sizes are sufficient (CCM is same size as GCM) */
- #if (AEAD_MAX_IMP_SZ < 4) /* AESGCM_IMP_IV_SZ */
- #error AEAD_MAX_IMP_SZ too small for AESCCM
- #endif
- #if (AEAD_MAX_EXP_SZ < 8) /* AESGCM_EXP_IV_SZ */
- #error AEAD_MAX_EXP_SZ too small for AESCCM
- #endif
- #if (MAX_WRITE_IV_SZ < 4) /* AESGCM_IMP_IV_SZ */
- #error MAX_WRITE_IV_SZ too small for AESCCM
- #endif
-
- if (specs->bulk_cipher_algorithm == wolfssl_aes_ccm) {
- int CcmRet;
-
- if (enc) {
- if (enc->aes == NULL)
- enc->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
- if (enc->aes == NULL)
- return MEMORY_E;
- XMEMSET(enc->aes, 0, sizeof(Aes));
- }
- if (dec) {
- if (dec->aes == NULL)
- dec->aes = (Aes*)XMALLOC(sizeof(Aes), heap, DYNAMIC_TYPE_CIPHER);
- if (dec->aes == NULL)
- return MEMORY_E;
- XMEMSET(dec->aes, 0, sizeof(Aes));
- }
-
- if (enc) {
- if (wc_AesInit(enc->aes, heap, devId) != 0) {
- WOLFSSL_MSG("AesInit failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
- if (dec) {
- if (wc_AesInit(dec->aes, heap, devId) != 0) {
- WOLFSSL_MSG("AesInit failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
-
- if (side == WOLFSSL_CLIENT_END) {
- if (enc) {
- CcmRet = wc_AesCcmSetKey(enc->aes, keys->client_write_key,
- specs->key_size);
- if (CcmRet != 0) {
- return CcmRet;
- }
- XMEMCPY(keys->aead_enc_imp_IV, keys->client_write_IV,
- AEAD_MAX_IMP_SZ);
-#if !defined(NO_PUBLIC_CCM_SET_NONCE) && \
- ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
- if (!tls13) {
- CcmRet = wc_AesCcmSetNonce(enc->aes, keys->client_write_IV,
- AEAD_MAX_IMP_SZ);
- if (CcmRet != 0) return CcmRet;
- }
-#endif
- }
- if (dec) {
- CcmRet = wc_AesCcmSetKey(dec->aes, keys->server_write_key,
- specs->key_size);
- if (CcmRet != 0) {
- return CcmRet;
- }
- XMEMCPY(keys->aead_dec_imp_IV, keys->server_write_IV,
- AEAD_MAX_IMP_SZ);
- }
- }
- else {
- if (enc) {
- CcmRet = wc_AesCcmSetKey(enc->aes, keys->server_write_key,
- specs->key_size);
- if (CcmRet != 0) {
- return CcmRet;
- }
- XMEMCPY(keys->aead_enc_imp_IV, keys->server_write_IV,
- AEAD_MAX_IMP_SZ);
-#if !defined(NO_PUBLIC_CCM_SET_NONCE) && \
- ((!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)))
- if (!tls13) {
- CcmRet = wc_AesCcmSetNonce(enc->aes, keys->server_write_IV,
- AEAD_MAX_IMP_SZ);
- if (CcmRet != 0) return CcmRet;
- }
-#endif
- }
- if (dec) {
- CcmRet = wc_AesCcmSetKey(dec->aes, keys->client_write_key,
- specs->key_size);
- if (CcmRet != 0) {
- return CcmRet;
- }
- XMEMCPY(keys->aead_dec_imp_IV, keys->client_write_IV,
- AEAD_MAX_IMP_SZ);
- }
- }
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif /* HAVE_AESCCM */
-
-#ifdef HAVE_CAMELLIA
- /* check that buffer sizes are sufficient */
- #if (MAX_WRITE_IV_SZ < 16) /* CAMELLIA_IV_SIZE */
- #error MAX_WRITE_IV_SZ too small for CAMELLIA
- #endif
-
- if (specs->bulk_cipher_algorithm == wolfssl_camellia) {
- int camRet;
-
- if (enc && enc->cam == NULL)
- enc->cam =
- (Camellia*)XMALLOC(sizeof(Camellia), heap, DYNAMIC_TYPE_CIPHER);
- if (enc && enc->cam == NULL)
- return MEMORY_E;
-
- if (dec && dec->cam == NULL)
- dec->cam =
- (Camellia*)XMALLOC(sizeof(Camellia), heap, DYNAMIC_TYPE_CIPHER);
- if (dec && dec->cam == NULL)
- return MEMORY_E;
-
- if (side == WOLFSSL_CLIENT_END) {
- if (enc) {
- camRet = wc_CamelliaSetKey(enc->cam, keys->client_write_key,
- specs->key_size, keys->client_write_IV);
- if (camRet != 0) return camRet;
- }
- if (dec) {
- camRet = wc_CamelliaSetKey(dec->cam, keys->server_write_key,
- specs->key_size, keys->server_write_IV);
- if (camRet != 0) return camRet;
- }
- }
- else {
- if (enc) {
- camRet = wc_CamelliaSetKey(enc->cam, keys->server_write_key,
- specs->key_size, keys->server_write_IV);
- if (camRet != 0) return camRet;
- }
- if (dec) {
- camRet = wc_CamelliaSetKey(dec->cam, keys->client_write_key,
- specs->key_size, keys->client_write_IV);
- if (camRet != 0) return camRet;
- }
- }
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif /* HAVE_CAMELLIA */
-
-#ifdef HAVE_IDEA
- /* check that buffer sizes are sufficient */
- #if (MAX_WRITE_IV_SZ < 8) /* IDEA_IV_SIZE */
- #error MAX_WRITE_IV_SZ too small for IDEA
- #endif
-
- if (specs->bulk_cipher_algorithm == wolfssl_idea) {
- int ideaRet;
-
- if (enc && enc->idea == NULL)
- enc->idea = (Idea*)XMALLOC(sizeof(Idea), heap, DYNAMIC_TYPE_CIPHER);
- if (enc && enc->idea == NULL)
- return MEMORY_E;
-
- if (dec && dec->idea == NULL)
- dec->idea = (Idea*)XMALLOC(sizeof(Idea), heap, DYNAMIC_TYPE_CIPHER);
- if (dec && dec->idea == NULL)
- return MEMORY_E;
-
- if (side == WOLFSSL_CLIENT_END) {
- if (enc) {
- ideaRet = wc_IdeaSetKey(enc->idea, keys->client_write_key,
- specs->key_size, keys->client_write_IV,
- IDEA_ENCRYPTION);
- if (ideaRet != 0) return ideaRet;
- }
- if (dec) {
- ideaRet = wc_IdeaSetKey(dec->idea, keys->server_write_key,
- specs->key_size, keys->server_write_IV,
- IDEA_DECRYPTION);
- if (ideaRet != 0) return ideaRet;
- }
- }
- else {
- if (enc) {
- ideaRet = wc_IdeaSetKey(enc->idea, keys->server_write_key,
- specs->key_size, keys->server_write_IV,
- IDEA_ENCRYPTION);
- if (ideaRet != 0) return ideaRet;
- }
- if (dec) {
- ideaRet = wc_IdeaSetKey(dec->idea, keys->client_write_key,
- specs->key_size, keys->client_write_IV,
- IDEA_DECRYPTION);
- if (ideaRet != 0) return ideaRet;
- }
- }
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif /* HAVE_IDEA */
-
-#ifdef HAVE_NULL_CIPHER
- if (specs->bulk_cipher_algorithm == wolfssl_cipher_null) {
- #ifdef WOLFSSL_TLS13
- if (tls13) {
- int hmacRet;
- int hashType = WC_HASH_TYPE_NONE;
-
- switch (specs->mac_algorithm) {
- case sha256_mac:
- hashType = WC_SHA256;
- break;
- case sha384_mac:
- hashType = WC_SHA384;
- break;
- default:
- break;
- }
-
- if (enc && enc->hmac == NULL) {
- enc->hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap,
- DYNAMIC_TYPE_CIPHER);
- if (enc->hmac == NULL)
- return MEMORY_E;
- }
-
- if (dec && dec->hmac == NULL) {
- dec->hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap,
- DYNAMIC_TYPE_CIPHER);
- if (dec->hmac == NULL)
- return MEMORY_E;
- }
-
- if (enc) {
- if (wc_HmacInit(enc->hmac, heap, devId) != 0) {
- WOLFSSL_MSG("HmacInit failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
- if (dec) {
- if (wc_HmacInit(dec->hmac, heap, devId) != 0) {
- WOLFSSL_MSG("HmacInit failed in SetKeys");
- return ASYNC_INIT_E;
- }
- }
-
- if (side == WOLFSSL_CLIENT_END) {
- if (enc) {
- hmacRet = wc_HmacSetKey(enc->hmac, hashType,
- keys->client_write_key, specs->key_size);
- if (hmacRet != 0) return hmacRet;
- }
- if (dec) {
- hmacRet = wc_HmacSetKey(dec->hmac, hashType,
- keys->server_write_key, specs->key_size);
- if (hmacRet != 0) return hmacRet;
- }
- }
- else {
- if (enc) {
- hmacRet = wc_HmacSetKey(enc->hmac, hashType,
- keys->server_write_key, specs->key_size);
- if (hmacRet != 0) return hmacRet;
- }
- if (dec) {
- hmacRet = wc_HmacSetKey(dec->hmac, hashType,
- keys->client_write_key, specs->key_size);
- if (hmacRet != 0) return hmacRet;
- }
- }
- }
- #endif
- if (enc)
- enc->setup = 1;
- if (dec)
- dec->setup = 1;
- }
-#endif
-
- if (enc) {
- keys->sequence_number_hi = 0;
- keys->sequence_number_lo = 0;
- }
- if (dec) {
- keys->peer_sequence_number_hi = 0;
- keys->peer_sequence_number_lo = 0;
- }
- (void)side;
- (void)heap;
- (void)enc;
- (void)dec;
- (void)specs;
- (void)devId;
-
- return 0;
-}
-
-
-#ifdef HAVE_ONE_TIME_AUTH
-/* set one time authentication keys */
-static int SetAuthKeys(OneTimeAuth* authentication, Keys* keys,
- CipherSpecs* specs, void* heap, int devId)
-{
-
-#ifdef HAVE_POLY1305
- /* set up memory space for poly1305 */
- if (authentication && authentication->poly1305 == NULL)
- authentication->poly1305 =
- (Poly1305*)XMALLOC(sizeof(Poly1305), heap, DYNAMIC_TYPE_CIPHER);
- if (authentication && authentication->poly1305 == NULL)
- return MEMORY_E;
- if (authentication)
- authentication->setup = 1;
-#endif
- (void)authentication;
- (void)heap;
- (void)keys;
- (void)specs;
- (void)devId;
-
- return 0;
-}
-#endif /* HAVE_ONE_TIME_AUTH */
-
-#ifdef HAVE_SECURE_RENEGOTIATION
-/* function name is for cache_status++
- * This function was added because of error incrementing enum type when
- * compiling with a C++ compiler.
- */
-static void CacheStatusPP(SecureRenegotiation* cache)
-{
- switch (cache->cache_status) {
- case SCR_CACHE_NULL:
- cache->cache_status = SCR_CACHE_NEEDED;
- break;
-
- case SCR_CACHE_NEEDED:
- cache->cache_status = SCR_CACHE_COPY;
- break;
-
- case SCR_CACHE_COPY:
- cache->cache_status = SCR_CACHE_PARTIAL;
- break;
-
- case SCR_CACHE_PARTIAL:
- cache->cache_status = SCR_CACHE_COMPLETE;
- break;
-
- case SCR_CACHE_COMPLETE:
- WOLFSSL_MSG("SCR Cache state Complete");
- break;
-
- default:
- WOLFSSL_MSG("Unknown cache state!!");
- }
-}
-#endif /* HAVE_SECURE_RENEGOTIATION */
-
-
-/* Set wc_encrypt/wc_decrypt or both sides of key setup
- * note: use wc_encrypt to avoid shadowing global encrypt
- * declared in unistd.h
- */
-int SetKeysSide(WOLFSSL* ssl, enum encrypt_side side)
-{
- int ret, copy = 0;
- Ciphers* wc_encrypt = NULL;
- Ciphers* wc_decrypt = NULL;
- Keys* keys = &ssl->keys;
-
- (void)copy;
-
-#ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation && ssl->secure_renegotiation->cache_status) {
- keys = &ssl->secure_renegotiation->tmp_keys;
- copy = 1;
- }
-#endif /* HAVE_SECURE_RENEGOTIATION */
-
- switch (side) {
- case ENCRYPT_SIDE_ONLY:
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Provisioning ENCRYPT key");
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- WOLFSSL_BUFFER(ssl->keys.client_write_key, ssl->specs.key_size);
- }
- else {
- WOLFSSL_BUFFER(ssl->keys.server_write_key, ssl->specs.key_size);
- }
-#endif
- wc_encrypt = &ssl->encrypt;
- break;
-
- case DECRYPT_SIDE_ONLY:
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Provisioning DECRYPT key");
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- WOLFSSL_BUFFER(ssl->keys.server_write_key, ssl->specs.key_size);
- }
- else {
- WOLFSSL_BUFFER(ssl->keys.client_write_key, ssl->specs.key_size);
- }
-#endif
- wc_decrypt = &ssl->decrypt;
- break;
-
- case ENCRYPT_AND_DECRYPT_SIDE:
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Provisioning ENCRYPT key");
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- WOLFSSL_BUFFER(ssl->keys.client_write_key, ssl->specs.key_size);
- }
- else {
- WOLFSSL_BUFFER(ssl->keys.server_write_key, ssl->specs.key_size);
- }
- WOLFSSL_MSG("Provisioning DECRYPT key");
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- WOLFSSL_BUFFER(ssl->keys.server_write_key, ssl->specs.key_size);
- }
- else {
- WOLFSSL_BUFFER(ssl->keys.client_write_key, ssl->specs.key_size);
- }
-#endif
- wc_encrypt = &ssl->encrypt;
- wc_decrypt = &ssl->decrypt;
- break;
-
- default:
- return BAD_FUNC_ARG;
- }
-
-#ifdef HAVE_ONE_TIME_AUTH
- if (!ssl->auth.setup && ssl->specs.bulk_cipher_algorithm == wolfssl_chacha){
- ret = SetAuthKeys(&ssl->auth, keys, &ssl->specs, ssl->heap, ssl->devId);
- if (ret != 0)
- return ret;
- }
-#endif
-
-#if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- /* check if keys for TSIP has been created */
- if (tsip_useable(ssl) == 1)
- ret = 0;
- else
-#endif
- ret = SetKeys(wc_encrypt, wc_decrypt, keys, &ssl->specs, ssl->options.side,
- ssl->heap, ssl->devId, ssl->rng, ssl->options.tls1_3);
-
-#ifdef HAVE_SECURE_RENEGOTIATION
- if (copy) {
- int clientCopy = 0;
-
- if (ssl->options.side == WOLFSSL_CLIENT_END && wc_encrypt)
- clientCopy = 1;
- else if (ssl->options.side == WOLFSSL_SERVER_END && wc_decrypt)
- clientCopy = 1;
-
- if (clientCopy) {
- #ifndef WOLFSSL_AEAD_ONLY
- XMEMCPY(ssl->keys.client_write_MAC_secret,
- keys->client_write_MAC_secret, WC_MAX_DIGEST_SIZE);
- #endif
- XMEMCPY(ssl->keys.client_write_key,
- keys->client_write_key, AES_256_KEY_SIZE);
- XMEMCPY(ssl->keys.client_write_IV,
- keys->client_write_IV, MAX_WRITE_IV_SZ);
- } else {
- #ifndef WOLFSSL_AEAD_ONLY
- XMEMCPY(ssl->keys.server_write_MAC_secret,
- keys->server_write_MAC_secret, WC_MAX_DIGEST_SIZE);
- #endif
- XMEMCPY(ssl->keys.server_write_key,
- keys->server_write_key, AES_256_KEY_SIZE);
- XMEMCPY(ssl->keys.server_write_IV,
- keys->server_write_IV, MAX_WRITE_IV_SZ);
- }
- if (wc_encrypt) {
- ssl->keys.sequence_number_hi = keys->sequence_number_hi;
- ssl->keys.sequence_number_lo = keys->sequence_number_lo;
- #ifdef HAVE_AEAD
- if (ssl->specs.cipher_type == aead) {
- /* Initialize the AES-GCM/CCM explicit IV to a zero. */
- XMEMCPY(ssl->keys.aead_exp_IV, keys->aead_exp_IV,
- AEAD_MAX_EXP_SZ);
-
- /* Initialize encrypt implicit IV by encrypt side */
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- XMEMCPY(ssl->keys.aead_enc_imp_IV,
- keys->client_write_IV, AEAD_MAX_IMP_SZ);
- } else {
- XMEMCPY(ssl->keys.aead_enc_imp_IV,
- keys->server_write_IV, AEAD_MAX_IMP_SZ);
- }
- }
- #endif
- }
- if (wc_decrypt) {
- ssl->keys.peer_sequence_number_hi = keys->peer_sequence_number_hi;
- ssl->keys.peer_sequence_number_lo = keys->peer_sequence_number_lo;
- #ifdef HAVE_AEAD
- if (ssl->specs.cipher_type == aead) {
- /* Initialize decrypt implicit IV by decrypt side */
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- XMEMCPY(ssl->keys.aead_dec_imp_IV,
- keys->client_write_IV, AEAD_MAX_IMP_SZ);
- } else {
- XMEMCPY(ssl->keys.aead_dec_imp_IV,
- keys->server_write_IV, AEAD_MAX_IMP_SZ);
- }
- }
- #endif
- }
- CacheStatusPP(ssl->secure_renegotiation);
- }
-#endif /* HAVE_SECURE_RENEGOTIATION */
-
- return ret;
-}
-
-
-/* TLS can call too */
-int StoreKeys(WOLFSSL* ssl, const byte* keyData, int side)
-{
- int sz, i = 0;
- Keys* keys = &ssl->keys;
-
-#ifdef HAVE_SECURE_RENEGOTIATION
- if (ssl->secure_renegotiation && ssl->secure_renegotiation->cache_status ==
- SCR_CACHE_NEEDED) {
- keys = &ssl->secure_renegotiation->tmp_keys;
- CacheStatusPP(ssl->secure_renegotiation);
- }
-#endif /* HAVE_SECURE_RENEGOTIATION */
-
-#ifdef WOLFSSL_MULTICAST
- if (ssl->options.haveMcast) {
- /* Use the same keys for encrypt and decrypt. */
- if (ssl->specs.cipher_type != aead) {
- sz = ssl->specs.hash_size;
- #ifndef WOLFSSL_AEAD_ONLY
- XMEMCPY(keys->client_write_MAC_secret,&keyData[i], sz);
- XMEMCPY(keys->server_write_MAC_secret,&keyData[i], sz);
- #endif
- i += sz;
- }
- sz = ssl->specs.key_size;
- XMEMCPY(keys->client_write_key, &keyData[i], sz);
- XMEMCPY(keys->server_write_key, &keyData[i], sz);
- i += sz;
-
- sz = ssl->specs.iv_size;
- XMEMCPY(keys->client_write_IV, &keyData[i], sz);
- XMEMCPY(keys->server_write_IV, &keyData[i], sz);
-
-#ifdef HAVE_AEAD
- if (ssl->specs.cipher_type == aead) {
- /* Initialize the AES-GCM/CCM explicit IV to a zero. */
- XMEMSET(keys->aead_exp_IV, 0, AEAD_MAX_EXP_SZ);
- }
-#endif /* HAVE_AEAD */
-
- return 0;
- }
-#endif /* WOLFSSL_MULTICAST */
-
- if (ssl->specs.cipher_type != aead) {
- sz = ssl->specs.hash_size;
- if (side & PROVISION_CLIENT) {
- #ifndef WOLFSSL_AEAD_ONLY
- XMEMCPY(keys->client_write_MAC_secret,&keyData[i], sz);
- #endif
- i += sz;
- }
- if (side & PROVISION_SERVER) {
- #ifndef WOLFSSL_AEAD_ONLY
- XMEMCPY(keys->server_write_MAC_secret,&keyData[i], sz);
- #endif
- i += sz;
- }
- }
- sz = ssl->specs.key_size;
- if (side & PROVISION_CLIENT) {
- XMEMCPY(keys->client_write_key, &keyData[i], sz);
- i += sz;
- }
- if (side & PROVISION_SERVER) {
- XMEMCPY(keys->server_write_key, &keyData[i], sz);
- i += sz;
- }
-
- sz = ssl->specs.iv_size;
- if (side & PROVISION_CLIENT) {
- XMEMCPY(keys->client_write_IV, &keyData[i], sz);
- i += sz;
- }
- if (side & PROVISION_SERVER)
- XMEMCPY(keys->server_write_IV, &keyData[i], sz);
-
-#ifdef HAVE_AEAD
- if (ssl->specs.cipher_type == aead) {
- /* Initialize the AES-GCM/CCM explicit IV to a zero. */
- XMEMSET(keys->aead_exp_IV, 0, AEAD_MAX_EXP_SZ);
- }
-#endif
-
- return 0;
-}
-
-#ifndef NO_OLD_TLS
-int DeriveKeys(WOLFSSL* ssl)
-{
- int length = 2 * ssl->specs.hash_size +
- 2 * ssl->specs.key_size +
- 2 * ssl->specs.iv_size;
- int rounds = (length + WC_MD5_DIGEST_SIZE - 1 ) / WC_MD5_DIGEST_SIZE, i;
- int ret = 0;
-
-#ifdef WOLFSSL_SMALL_STACK
- byte* shaOutput;
- byte* md5Input;
- byte* shaInput;
- byte* keyData;
- wc_Md5* md5;
- wc_Sha* sha;
-#else
- byte shaOutput[WC_SHA_DIGEST_SIZE];
- byte md5Input[SECRET_LEN + WC_SHA_DIGEST_SIZE];
- byte shaInput[KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN];
- byte keyData[KEY_PREFIX * WC_MD5_DIGEST_SIZE];
- wc_Md5 md5[1];
- wc_Sha sha[1];
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- shaOutput = (byte*)XMALLOC(WC_SHA_DIGEST_SIZE,
- NULL, DYNAMIC_TYPE_TMP_BUFFER);
- md5Input = (byte*)XMALLOC(SECRET_LEN + WC_SHA_DIGEST_SIZE,
- NULL, DYNAMIC_TYPE_TMP_BUFFER);
- shaInput = (byte*)XMALLOC(KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN,
- NULL, DYNAMIC_TYPE_TMP_BUFFER);
- keyData = (byte*)XMALLOC(KEY_PREFIX * WC_MD5_DIGEST_SIZE,
- NULL, DYNAMIC_TYPE_TMP_BUFFER);
- md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if (shaOutput == NULL || md5Input == NULL || shaInput == NULL ||
- keyData == NULL || md5 == NULL || sha == NULL) {
- if (shaOutput) XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (md5Input) XFREE(md5Input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (shaInput) XFREE(shaInput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (keyData) XFREE(keyData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (md5) XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (sha) XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- return MEMORY_E;
- }
-#endif
-
- ret = wc_InitMd5(md5);
- if (ret == 0) {
- ret = wc_InitSha(sha);
- }
- if (ret == 0) {
- XMEMCPY(md5Input, ssl->arrays->masterSecret, SECRET_LEN);
-
- for (i = 0; i < rounds; ++i) {
- int j = i + 1;
- int idx = j;
-
- if (!SetPrefix(shaInput, i)) {
- ret = PREFIX_ERROR;
- break;
- }
-
- XMEMCPY(shaInput + idx, ssl->arrays->masterSecret, SECRET_LEN);
- idx += SECRET_LEN;
- XMEMCPY(shaInput + idx, ssl->arrays->serverRandom, RAN_LEN);
- idx += RAN_LEN;
- XMEMCPY(shaInput + idx, ssl->arrays->clientRandom, RAN_LEN);
- if (ret == 0) {
- ret = wc_ShaUpdate(sha, shaInput,
- (KEY_PREFIX + SECRET_LEN + 2 * RAN_LEN) - KEY_PREFIX + j);
- }
- if (ret == 0) {
- ret = wc_ShaFinal(sha, shaOutput);
- }
-
- XMEMCPY(md5Input + SECRET_LEN, shaOutput, WC_SHA_DIGEST_SIZE);
- if (ret == 0) {
- ret = wc_Md5Update(md5, md5Input, SECRET_LEN + WC_SHA_DIGEST_SIZE);
- }
- if (ret == 0) {
- ret = wc_Md5Final(md5, keyData + i * WC_MD5_DIGEST_SIZE);
- }
- }
-
- if (ret == 0)
- ret = StoreKeys(ssl, keyData, PROVISION_CLIENT_SERVER);
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(md5Input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(shaInput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(keyData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return ret;
-}
-
-
-static int CleanPreMaster(WOLFSSL* ssl)
-{
- int i, ret, sz = ssl->arrays->preMasterSz;
-
- for (i = 0; i < sz; i++)
- ssl->arrays->preMasterSecret[i] = 0;
-
- ret = wc_RNG_GenerateBlock(ssl->rng, ssl->arrays->preMasterSecret, sz);
- if (ret != 0)
- return ret;
-
- for (i = 0; i < sz; i++)
- ssl->arrays->preMasterSecret[i] = 0;
-
- XFREE(ssl->arrays->preMasterSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
- ssl->arrays->preMasterSecret = NULL;
- ssl->arrays->preMasterSz = 0;
-
- return 0;
-}
-
-
-/* Create and store the master secret see page 32, 6.1 */
-static int MakeSslMasterSecret(WOLFSSL* ssl)
-{
- int i, ret;
- word32 idx;
- word32 pmsSz = ssl->arrays->preMasterSz;
-
-#ifdef WOLFSSL_SMALL_STACK
- byte* shaOutput;
- byte* md5Input;
- byte* shaInput;
- wc_Md5* md5;
- wc_Sha* sha;
-#else
- byte shaOutput[WC_SHA_DIGEST_SIZE];
- byte md5Input[ENCRYPT_LEN + WC_SHA_DIGEST_SIZE];
- byte shaInput[PREFIX + ENCRYPT_LEN + 2 * RAN_LEN];
- wc_Md5 md5[1];
- wc_Sha sha[1];
-#endif
-
-#ifdef SHOW_SECRETS
- {
- word32 j;
- printf("pre master secret: ");
- for (j = 0; j < pmsSz; j++)
- printf("%02x", ssl->arrays->preMasterSecret[j]);
- printf("\n");
- }
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- shaOutput = (byte*)XMALLOC(WC_SHA_DIGEST_SIZE,
- NULL, DYNAMIC_TYPE_TMP_BUFFER);
- md5Input = (byte*)XMALLOC(ENCRYPT_LEN + WC_SHA_DIGEST_SIZE,
- NULL, DYNAMIC_TYPE_TMP_BUFFER);
- shaInput = (byte*)XMALLOC(PREFIX + ENCRYPT_LEN + 2 * RAN_LEN,
- NULL, DYNAMIC_TYPE_TMP_BUFFER);
- md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if (shaOutput == NULL || md5Input == NULL || shaInput == NULL ||
- md5 == NULL || sha == NULL) {
- if (shaOutput) XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (md5Input) XFREE(md5Input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (shaInput) XFREE(shaInput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (md5) XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (sha) XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- return MEMORY_E;
- }
-#endif
-
- ret = wc_InitMd5(md5);
- if (ret == 0) {
- ret = wc_InitSha(sha);
- }
- if (ret == 0) {
- XMEMCPY(md5Input, ssl->arrays->preMasterSecret, pmsSz);
-
- for (i = 0; i < MASTER_ROUNDS; ++i) {
- byte prefix[KEY_PREFIX]; /* only need PREFIX bytes but static */
- if (!SetPrefix(prefix, i)) { /* analysis thinks will overrun */
- ret = PREFIX_ERROR;
- break;
- }
-
- idx = 0;
- XMEMCPY(shaInput, prefix, i + 1);
- idx += i + 1;
-
- XMEMCPY(shaInput + idx, ssl->arrays->preMasterSecret, pmsSz);
- idx += pmsSz;
- XMEMCPY(shaInput + idx, ssl->arrays->clientRandom, RAN_LEN);
- idx += RAN_LEN;
- XMEMCPY(shaInput + idx, ssl->arrays->serverRandom, RAN_LEN);
- idx += RAN_LEN;
- if (ret == 0) {
- ret = wc_ShaUpdate(sha, shaInput, idx);
- }
- if (ret == 0) {
- ret = wc_ShaFinal(sha, shaOutput);
- }
- idx = pmsSz; /* preSz */
- XMEMCPY(md5Input + idx, shaOutput, WC_SHA_DIGEST_SIZE);
- idx += WC_SHA_DIGEST_SIZE;
- if (ret == 0) {
- ret = wc_Md5Update(md5, md5Input, idx);
- }
- if (ret == 0) {
- ret = wc_Md5Final(md5,
- &ssl->arrays->masterSecret[i * WC_MD5_DIGEST_SIZE]);
- }
- }
-
-#ifdef SHOW_SECRETS
- {
- word32 j;
- printf("master secret: ");
- for (j = 0; j < SECRET_LEN; j++)
- printf("%02x", ssl->arrays->masterSecret[j]);
- printf("\n");
- }
-#endif
-
- if (ret == 0)
- ret = DeriveKeys(ssl);
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(shaOutput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(md5Input, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(shaInput, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- if (ret == 0)
- ret = CleanPreMaster(ssl);
- else
- CleanPreMaster(ssl);
-
- return ret;
-}
-#endif
-
-
-/* Master wrapper, doesn't use SSL stack space in TLS mode */
-int MakeMasterSecret(WOLFSSL* ssl)
-{
- /* append secret to premaster : premaster | SerSi | CliSi */
-#ifdef HAVE_QSH
- word32 offset = 0;
-
- if (ssl->peerQSHKeyPresent) {
- offset += ssl->arrays->preMasterSz;
- ssl->arrays->preMasterSz += ssl->QSH_secret->CliSi->length +
- ssl->QSH_secret->SerSi->length;
- /* test and set flag if QSH has been used */
- if (ssl->QSH_secret->CliSi->length > 0 ||
- ssl->QSH_secret->SerSi->length > 0)
- ssl->isQSH = 1;
-
- /* append secrets to the premaster */
- if (ssl->QSH_secret->SerSi != NULL) {
- XMEMCPY(ssl->arrays->preMasterSecret + offset,
- ssl->QSH_secret->SerSi->buffer, ssl->QSH_secret->SerSi->length);
- }
- offset += ssl->QSH_secret->SerSi->length;
- if (ssl->QSH_secret->CliSi != NULL) {
- XMEMCPY(ssl->arrays->preMasterSecret + offset,
- ssl->QSH_secret->CliSi->buffer, ssl->QSH_secret->CliSi->length);
- }
-
- /* show secret SerSi and CliSi */
- #ifdef SHOW_SECRETS
- {
- word32 j;
- printf("QSH generated secret material\n");
- printf("SerSi : ");
- for (j = 0; j < ssl->QSH_secret->SerSi->length; j++) {
- printf("%02x", ssl->QSH_secret->SerSi->buffer[j]);
- }
- printf("\n");
- printf("CliSi : ");
- for (j = 0; j < ssl->QSH_secret->CliSi->length; j++) {
- printf("%02x", ssl->QSH_secret->CliSi->buffer[j]);
- }
- printf("\n");
- }
- #endif
- }
-#endif
-
-#ifndef NO_OLD_TLS
- if (ssl->options.tls) return MakeTlsMasterSecret(ssl);
- return MakeSslMasterSecret(ssl);
-#elif !defined(WOLFSSL_NO_TLS12)
- return MakeTlsMasterSecret(ssl);
-#else
- (void)ssl;
- return 0;
-#endif
-}
-
-#endif /* WOLFCRYPT_ONLY */
diff --git a/client/wolfssl/src/ocsp.c b/client/wolfssl/src/ocsp.c
deleted file mode 100644
index b5a2719..0000000
--- a/client/wolfssl/src/ocsp.c
+++ /dev/null
@@ -1,1100 +0,0 @@
-/* ocsp.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
- /* Name change compatibility layer no longer needs to be included here */
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifndef WOLFCRYPT_ONLY
-#ifdef HAVE_OCSP
-
-#include
-#include
-#include
-
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include
-#endif
-
-
-int InitOCSP(WOLFSSL_OCSP* ocsp, WOLFSSL_CERT_MANAGER* cm)
-{
- WOLFSSL_ENTER("InitOCSP");
-
- ForceZero(ocsp, sizeof(WOLFSSL_OCSP));
-
- if (wc_InitMutex(&ocsp->ocspLock) != 0)
- return BAD_MUTEX_E;
-
- ocsp->cm = cm;
-
- return 0;
-}
-
-
-static int InitOcspEntry(OcspEntry* entry, OcspRequest* request)
-{
- WOLFSSL_ENTER("InitOcspEntry");
-
- ForceZero(entry, sizeof(OcspEntry));
-
- XMEMCPY(entry->issuerHash, request->issuerHash, OCSP_DIGEST_SIZE);
- XMEMCPY(entry->issuerKeyHash, request->issuerKeyHash, OCSP_DIGEST_SIZE);
-
- return 0;
-}
-
-
-static void FreeOcspEntry(OcspEntry* entry, void* heap)
-{
- CertStatus *status, *next;
-
- WOLFSSL_ENTER("FreeOcspEntry");
-
- for (status = entry->status; status; status = next) {
- next = status->next;
-
- if (status->rawOcspResponse)
- XFREE(status->rawOcspResponse, heap, DYNAMIC_TYPE_OCSP_STATUS);
-
- XFREE(status, heap, DYNAMIC_TYPE_OCSP_STATUS);
- }
-
- (void)heap;
-}
-
-
-void FreeOCSP(WOLFSSL_OCSP* ocsp, int dynamic)
-{
- OcspEntry *entry, *next;
-
- WOLFSSL_ENTER("FreeOCSP");
-
- for (entry = ocsp->ocspList; entry; entry = next) {
- next = entry->next;
- FreeOcspEntry(entry, ocsp->cm->heap);
- XFREE(entry, ocsp->cm->heap, DYNAMIC_TYPE_OCSP_ENTRY);
- }
-
- wc_FreeMutex(&ocsp->ocspLock);
-
- if (dynamic)
- XFREE(ocsp, ocsp->cm->heap, DYNAMIC_TYPE_OCSP);
-
-}
-
-
-static int xstat2err(int st)
-{
- switch (st) {
- case CERT_GOOD:
- return 0;
- case CERT_REVOKED:
- return OCSP_CERT_REVOKED;
- default:
- return OCSP_CERT_UNKNOWN;
- }
-}
-
-int CheckCertOCSP_ex(WOLFSSL_OCSP* ocsp, DecodedCert* cert, buffer* responseBuffer, WOLFSSL* ssl)
-{
- int ret = OCSP_LOOKUP_FAIL;
-
-#ifdef WOLFSSL_SMALL_STACK
- OcspRequest* ocspRequest;
-#else
- OcspRequest ocspRequest[1];
-#endif
-
- WOLFSSL_ENTER("CheckCertOCSP");
-
-
-#ifdef WOLFSSL_SMALL_STACK
- ocspRequest = (OcspRequest*)XMALLOC(sizeof(OcspRequest), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (ocspRequest == NULL) {
- WOLFSSL_LEAVE("CheckCertOCSP", MEMORY_ERROR);
- return MEMORY_E;
- }
-#endif
-
- if (InitOcspRequest(ocspRequest, cert, ocsp->cm->ocspSendNonce,
- ocsp->cm->heap) == 0) {
- ocspRequest->ssl = ssl;
- ret = CheckOcspRequest(ocsp, ocspRequest, responseBuffer);
-
- FreeOcspRequest(ocspRequest);
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(ocspRequest, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- WOLFSSL_LEAVE("CheckCertOCSP", ret);
- return ret;
-}
-int CheckCertOCSP(WOLFSSL_OCSP* ocsp, DecodedCert* cert, buffer* responseBuffer)
-{
- return CheckCertOCSP_ex(ocsp, cert, responseBuffer, NULL);
-}
-
-static int GetOcspEntry(WOLFSSL_OCSP* ocsp, OcspRequest* request,
- OcspEntry** entry)
-{
- WOLFSSL_ENTER("GetOcspEntry");
-
- *entry = NULL;
-
- if (wc_LockMutex(&ocsp->ocspLock) != 0) {
- WOLFSSL_LEAVE("CheckCertOCSP", BAD_MUTEX_E);
- return BAD_MUTEX_E;
- }
-
- for (*entry = ocsp->ocspList; *entry; *entry = (*entry)->next)
- if (XMEMCMP((*entry)->issuerHash, request->issuerHash,
- OCSP_DIGEST_SIZE) == 0
- && XMEMCMP((*entry)->issuerKeyHash, request->issuerKeyHash,
- OCSP_DIGEST_SIZE) == 0)
- break;
-
- if (*entry == NULL) {
- *entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry),
- ocsp->cm->heap, DYNAMIC_TYPE_OCSP_ENTRY);
- if (*entry) {
- InitOcspEntry(*entry, request);
- (*entry)->next = ocsp->ocspList;
- ocsp->ocspList = *entry;
- }
- }
-
- wc_UnLockMutex(&ocsp->ocspLock);
-
- return *entry ? 0 : MEMORY_ERROR;
-}
-
-
-/* Mallocs responseBuffer->buffer and is up to caller to free on success
- *
- * Returns OCSP status
- */
-static int GetOcspStatus(WOLFSSL_OCSP* ocsp, OcspRequest* request,
- OcspEntry* entry, CertStatus** status, buffer* responseBuffer)
-{
- int ret = OCSP_INVALID_STATUS;
-
- WOLFSSL_ENTER("GetOcspStatus");
-
- *status = NULL;
-
- if (wc_LockMutex(&ocsp->ocspLock) != 0) {
- WOLFSSL_LEAVE("CheckCertOCSP", BAD_MUTEX_E);
- return BAD_MUTEX_E;
- }
-
- for (*status = entry->status; *status; *status = (*status)->next)
- if ((*status)->serialSz == request->serialSz
- && !XMEMCMP((*status)->serial, request->serial, (*status)->serialSz))
- break;
-
- if (responseBuffer && *status && !(*status)->rawOcspResponse) {
- /* force fetching again */
- ret = OCSP_INVALID_STATUS;
- }
- else if (*status) {
-#ifndef NO_ASN_TIME
- if (XVALIDATE_DATE((*status)->thisDate,
- (*status)->thisDateFormat, BEFORE)
- && ((*status)->nextDate[0] != 0)
- && XVALIDATE_DATE((*status)->nextDate,
- (*status)->nextDateFormat, AFTER))
-#endif
- {
- ret = xstat2err((*status)->status);
-
- if (responseBuffer) {
- responseBuffer->buffer = (byte*)XMALLOC(
- (*status)->rawOcspResponseSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if (responseBuffer->buffer) {
- responseBuffer->length = (*status)->rawOcspResponseSz;
- XMEMCPY(responseBuffer->buffer,
- (*status)->rawOcspResponse,
- (*status)->rawOcspResponseSz);
- }
- }
- }
- }
-
- wc_UnLockMutex(&ocsp->ocspLock);
-
- return ret;
-}
-
-/* Check that the response for validity. Store result in status.
- *
- * ocsp Context object for OCSP status.
- * response OCSP response message data.
- * responseSz Length of OCSP response message data.
- * reponseBuffer Buffer object to return the response with.
- * status The certificate status object.
- * entry The OCSP entry for this certificate.
- * returns OCSP_LOOKUP_FAIL when the response is bad and 0 otherwise.
- */
-WOLFSSL_LOCAL int CheckOcspResponse(WOLFSSL_OCSP *ocsp, byte *response, int responseSz,
- WOLFSSL_BUFFER_INFO *responseBuffer, CertStatus *status,
- OcspEntry *entry, OcspRequest *ocspRequest)
-{
-#ifdef WOLFSSL_SMALL_STACK
- CertStatus* newStatus;
- OcspResponse* ocspResponse;
-#else
- CertStatus newStatus[1];
- OcspResponse ocspResponse[1];
-#endif
- int ret;
- int validated = 0; /* ocsp validation flag */
-
-#ifdef WOLFSSL_SMALL_STACK
- newStatus = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- ocspResponse = (OcspResponse*)XMALLOC(sizeof(OcspResponse), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
-
- if (newStatus == NULL || ocspResponse == NULL) {
- if (newStatus) XFREE(newStatus, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (ocspResponse) XFREE(ocspResponse, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- WOLFSSL_LEAVE("CheckCertOCSP", MEMORY_ERROR);
- return MEMORY_E;
- }
-#endif
- XMEMSET(newStatus, 0, sizeof(CertStatus));
-
- InitOcspResponse(ocspResponse, newStatus, response, responseSz);
- ret = OcspResponseDecode(ocspResponse, ocsp->cm, ocsp->cm->heap, 0);
- if (ret != 0) {
- ocsp->error = ret;
- WOLFSSL_LEAVE("OcspResponseDecode failed", ocsp->error);
- goto end;
- }
-
- if (ocspResponse->responseStatus != OCSP_SUCCESSFUL) {
- WOLFSSL_MSG("OcspResponse status bad");
- goto end;
- }
- if (ocspRequest != NULL) {
- ret = CompareOcspReqResp(ocspRequest, ocspResponse);
- if (ret != 0) {
- goto end;
- }
- }
-
- if (responseBuffer) {
- responseBuffer->buffer = (byte*)XMALLOC(responseSz, ocsp->cm->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
-
- if (responseBuffer->buffer) {
- responseBuffer->length = responseSz;
- XMEMCPY(responseBuffer->buffer, response, responseSz);
- }
- }
-
- ret = xstat2err(ocspResponse->status->status);
- if (ret == 0) {
- validated = 1;
- }
-
- if (wc_LockMutex(&ocsp->ocspLock) != 0) {
- ret = BAD_MUTEX_E;
- goto end;
- }
-
- if (status != NULL) {
- if (status->rawOcspResponse) {
- XFREE(status->rawOcspResponse, ocsp->cm->heap,
- DYNAMIC_TYPE_OCSP_STATUS);
- }
-
- /* Replace existing certificate entry with updated */
- newStatus->next = status->next;
- XMEMCPY(status, newStatus, sizeof(CertStatus));
- }
- else {
- /* Save new certificate entry */
- status = (CertStatus*)XMALLOC(sizeof(CertStatus),
- ocsp->cm->heap, DYNAMIC_TYPE_OCSP_STATUS);
- if (status != NULL) {
- XMEMCPY(status, newStatus, sizeof(CertStatus));
- status->next = entry->status;
- entry->status = status;
- entry->totalStatus++;
- }
- }
-
- if (status && responseBuffer && responseBuffer->buffer) {
- status->rawOcspResponse = (byte*)XMALLOC(responseBuffer->length,
- ocsp->cm->heap,
- DYNAMIC_TYPE_OCSP_STATUS);
-
- if (status->rawOcspResponse) {
- status->rawOcspResponseSz = responseBuffer->length;
- XMEMCPY(status->rawOcspResponse, responseBuffer->buffer,
- responseBuffer->length);
- }
- }
-
- wc_UnLockMutex(&ocsp->ocspLock);
-
-end:
- if (ret == 0 && validated == 1) {
- WOLFSSL_MSG("New OcspResponse validated");
- } else if (ret != OCSP_CERT_REVOKED) {
- ret = OCSP_LOOKUP_FAIL;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(newStatus, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(ocspResponse, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return ret;
-}
-
-/* 0 on success */
-int CheckOcspRequest(WOLFSSL_OCSP* ocsp, OcspRequest* ocspRequest,
- buffer* responseBuffer)
-{
- OcspEntry* entry = NULL;
- CertStatus* status = NULL;
- byte* request = NULL;
- int requestSz = 2048;
- int responseSz = 0;
- byte* response = NULL;
- const char* url = NULL;
- int urlSz = 0;
- int ret = -1;
- WOLFSSL* ssl;
- void* ioCtx;
-
- WOLFSSL_ENTER("CheckOcspRequest");
-
- if (ocsp == NULL || ocspRequest == NULL)
- return BAD_FUNC_ARG;
-
- if (responseBuffer) {
- responseBuffer->buffer = NULL;
- responseBuffer->length = 0;
- }
-
- ret = GetOcspEntry(ocsp, ocspRequest, &entry);
- if (ret != 0)
- return ret;
-
- ret = GetOcspStatus(ocsp, ocspRequest, entry, &status, responseBuffer);
- if (ret != OCSP_INVALID_STATUS)
- return ret;
-
- /* get SSL and IOCtx */
- ssl = (WOLFSSL*)ocspRequest->ssl;
- ioCtx = (ssl && ssl->ocspIOCtx != NULL) ?
- ssl->ocspIOCtx : ocsp->cm->ocspIOCtx;
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
- if (ocsp->statusCb != NULL && ssl != NULL) {
- ret = ocsp->statusCb(ssl, ioCtx);
- if (ret == 0) {
- ret = wolfSSL_get_ocsp_response(ssl, &response);
- ret = CheckOcspResponse(ocsp, response, ret, responseBuffer, status,
- entry, NULL);
- if (response != NULL)
- XFREE(response, NULL, DYNAMIC_TYPE_OPENSSL);
- return ret;
- }
- WOLFSSL_LEAVE("CheckOcspRequest", ocsp->error);
- return OCSP_LOOKUP_FAIL;
- }
-#endif
-
- if (ocsp->cm->ocspUseOverrideURL) {
- url = ocsp->cm->ocspOverrideURL;
- if (url != NULL && url[0] != '\0')
- urlSz = (int)XSTRLEN(url);
- else
- return OCSP_NEED_URL;
- }
- else if (ocspRequest->urlSz != 0 && ocspRequest->url != NULL) {
- url = (const char *)ocspRequest->url;
- urlSz = ocspRequest->urlSz;
- }
- else {
- /* cert doesn't have extAuthInfo, assuming CERT_GOOD */
- return 0;
- }
-
- request = (byte*)XMALLOC(requestSz, ocsp->cm->heap, DYNAMIC_TYPE_OCSP);
- if (request == NULL) {
- WOLFSSL_LEAVE("CheckCertOCSP", MEMORY_ERROR);
- if (responseBuffer) {
- XFREE(responseBuffer->buffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- responseBuffer->buffer = NULL;
- }
- return MEMORY_ERROR;
- }
-
- requestSz = EncodeOcspRequest(ocspRequest, request, requestSz);
- if (requestSz > 0 && ocsp->cm->ocspIOCb) {
- responseSz = ocsp->cm->ocspIOCb(ioCtx, url, urlSz,
- request, requestSz, &response);
- }
- if (responseSz == WOLFSSL_CBIO_ERR_WANT_READ) {
- ret = OCSP_WANT_READ;
- }
-
- XFREE(request, ocsp->cm->heap, DYNAMIC_TYPE_OCSP);
-
- if (responseSz >= 0 && response) {
- ret = CheckOcspResponse(ocsp, response, responseSz, responseBuffer, status,
- entry, ocspRequest);
- }
-
- if (response != NULL && ocsp->cm->ocspRespFreeCb)
- ocsp->cm->ocspRespFreeCb(ioCtx, response);
-
- /* Keep responseBuffer in the case of getting to response check. Caller
- * should free responseBuffer after checking OCSP return value in "ret" */
- WOLFSSL_LEAVE("CheckOcspRequest", ret);
- return ret;
-}
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
- defined(WOLFSSL_APACHE_HTTPD)
-
-int wolfSSL_OCSP_resp_find_status(WOLFSSL_OCSP_BASICRESP *bs,
- WOLFSSL_OCSP_CERTID* id, int* status, int* reason,
- WOLFSSL_ASN1_TIME** revtime, WOLFSSL_ASN1_TIME** thisupd,
- WOLFSSL_ASN1_TIME** nextupd)
-{
- if (bs == NULL || id == NULL)
- return WOLFSSL_FAILURE;
-
- /* Only supporting one certificate status in asn.c. */
- if (CompareOcspReqResp(id, bs) != 0)
- return WOLFSSL_FAILURE;
-
- if (status != NULL)
- *status = bs->status->status;
- if (thisupd != NULL)
- *thisupd = &bs->status->thisDateParsed;
- if (nextupd != NULL)
- *nextupd = &bs->status->nextDateParsed;
-
- /* TODO: Not needed for Nginx. */
- if (reason != NULL)
- *reason = 0;
- if (revtime != NULL)
- *revtime = NULL;
-
- return WOLFSSL_SUCCESS;
-}
-
-const char *wolfSSL_OCSP_cert_status_str(long s)
-{
- switch (s) {
- case CERT_GOOD:
- return "good";
- case CERT_REVOKED:
- return "revoked";
- case CERT_UNKNOWN:
- return "unknown";
- default:
- return "(UNKNOWN)";
- }
-}
-
-int wolfSSL_OCSP_check_validity(WOLFSSL_ASN1_TIME* thisupd,
- WOLFSSL_ASN1_TIME* nextupd, long sec, long maxsec)
-{
- (void)thisupd;
- (void)nextupd;
- (void)sec;
- (void)maxsec;
- /* Dates validated in DecodeSingleResponse. */
- return WOLFSSL_SUCCESS;
-}
-
-void wolfSSL_OCSP_CERTID_free(WOLFSSL_OCSP_CERTID* certId)
-{
- FreeOcspRequest(certId);
- XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
-}
-
-WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_cert_to_id(
- const WOLFSSL_EVP_MD *dgst, const WOLFSSL_X509 *subject,
- const WOLFSSL_X509 *issuer)
-{
- WOLFSSL_OCSP_CERTID* certId;
- DecodedCert cert;
- WOLFSSL_CERT_MANAGER* cm;
- int ret;
- DerBuffer* derCert = NULL;
-
- (void)dgst;
-
- cm = wolfSSL_CertManagerNew();
- if (cm == NULL)
- return NULL;
-
- ret = AllocDer(&derCert, issuer->derCert->length,
- issuer->derCert->type, NULL);
- if (ret == 0) {
- /* AddCA() frees the buffer. */
- XMEMCPY(derCert->buffer, issuer->derCert->buffer,
- issuer->derCert->length);
- AddCA(cm, &derCert, WOLFSSL_USER_CA, 1);
- }
-
- certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(WOLFSSL_OCSP_CERTID), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (certId != NULL) {
- InitDecodedCert(&cert, subject->derCert->buffer,
- subject->derCert->length, NULL);
- if (ParseCertRelative(&cert, CERT_TYPE, VERIFY_OCSP, cm) != 0) {
- XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
- certId = NULL;
- }
- else {
- ret = InitOcspRequest(certId, &cert, 0, NULL);
- if (ret != 0) {
- XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
- certId = NULL;
- }
- }
- FreeDecodedCert(&cert);
- }
-
- wolfSSL_CertManagerFree(cm);
-
- return certId;
-}
-
-void wolfSSL_OCSP_BASICRESP_free(WOLFSSL_OCSP_BASICRESP* basicResponse)
-{
- wolfSSL_OCSP_RESPONSE_free(basicResponse);
-}
-
-/* Signature verified in DecodeBasicOcspResponse.
- * But no store available to verify certificate. */
-int wolfSSL_OCSP_basic_verify(WOLFSSL_OCSP_BASICRESP *bs,
- WOLF_STACK_OF(WOLFSSL_X509) *certs, WOLFSSL_X509_STORE *st, unsigned long flags)
-{
- DecodedCert cert;
- int ret = WOLFSSL_SUCCESS;
-
- (void)certs;
-
- if (flags & OCSP_NOVERIFY)
- return WOLFSSL_SUCCESS;
-
-#ifdef OPENSSL_EXTRA
- if (bs->verifyError != OCSP_VERIFY_ERROR_NONE)
- return WOLFSSL_FAILURE;
-#endif
-
- InitDecodedCert(&cert, bs->cert, bs->certSz, NULL);
- if (ParseCertRelative(&cert, CERT_TYPE, VERIFY, st->cm) < 0)
- ret = WOLFSSL_FAILURE;
- FreeDecodedCert(&cert);
-
- return ret;
-}
-
-void wolfSSL_OCSP_RESPONSE_free(OcspResponse* response)
-{
- if (response->status != NULL)
- XFREE(response->status, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (response->source != NULL)
- XFREE(response->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(response, NULL, DYNAMIC_TYPE_OPENSSL);
-}
-
-OcspResponse* wolfSSL_d2i_OCSP_RESPONSE_bio(WOLFSSL_BIO* bio,
- OcspResponse** response)
-{
- byte* data;
- byte* p;
- int len;
- int dataAlloced = 0;
- OcspResponse* ret = NULL;
-
- if (bio == NULL)
- return NULL;
-
- if (bio->type == WOLFSSL_BIO_MEMORY) {
- len = wolfSSL_BIO_get_mem_data(bio, &data);
- if (len <= 0 || data == NULL) {
- return NULL;
- }
- }
-#ifndef NO_FILESYSTEM
- else if (bio->type == WOLFSSL_BIO_FILE) {
- long fcur;
- long flen;
-
- if (bio->ptr == NULL)
- return NULL;
-
- fcur = XFTELL((XFILE)bio->ptr);
- if (fcur < 0)
- return NULL;
- if(XFSEEK((XFILE)bio->ptr, 0, SEEK_END) != 0)
- return NULL;
- flen = XFTELL((XFILE)bio->ptr);
- if (flen < 0)
- return NULL;
- if (XFSEEK((XFILE)bio->ptr, fcur, SEEK_SET) != 0)
- return NULL;
-
- /* check calculated length */
- fcur = flen - fcur;
- if (fcur > MAX_WOLFSSL_FILE_SIZE || fcur <= 0)
- return NULL;
-
- data = (byte*)XMALLOC(fcur, 0, DYNAMIC_TYPE_TMP_BUFFER);
- if (data == NULL)
- return NULL;
- dataAlloced = 1;
-
- len = wolfSSL_BIO_read(bio, (char *)data, (int)flen);
- }
-#endif
- else
- return NULL;
-
- if (len > 0) {
- p = data;
- ret = wolfSSL_d2i_OCSP_RESPONSE(response, (const unsigned char **)&p,
- len);
- }
-
- if (dataAlloced)
- XFREE(data, 0, DYNAMIC_TYPE_TMP_BUFFER);
-
- return ret;
-}
-
-OcspResponse* wolfSSL_d2i_OCSP_RESPONSE(OcspResponse** response,
- const unsigned char** data, int len)
-{
- OcspResponse *resp = NULL;
- word32 idx = 0;
- int length = 0;
-
- if (data == NULL)
- return NULL;
-
- if (response != NULL)
- resp = *response;
- if (resp == NULL) {
- resp = (OcspResponse*)XMALLOC(sizeof(OcspResponse), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (resp == NULL)
- return NULL;
- XMEMSET(resp, 0, sizeof(OcspResponse));
- }
-
- resp->source = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (resp->source == NULL) {
- XFREE(resp, NULL, DYNAMIC_TYPE_OPENSSL);
- return NULL;
- }
- resp->status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (resp->status == NULL) {
- XFREE(resp->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(resp, NULL, DYNAMIC_TYPE_OPENSSL);
- return NULL;
- }
-
- XMEMCPY(resp->source, *data, len);
- resp->maxIdx = len;
-
- if (OcspResponseDecode(resp, NULL, NULL, 1) != 0) {
- wolfSSL_OCSP_RESPONSE_free(resp);
- return NULL;
- }
-
- if (GetSequence(*data, &idx, &length, len) >= 0)
- (*data) += idx + length;
-
- return resp;
-}
-
-int wolfSSL_i2d_OCSP_RESPONSE(OcspResponse* response,
- unsigned char** data)
-{
- if (data == NULL)
- return response->maxIdx;
-
- XMEMCPY(*data, response->source, response->maxIdx);
- return response->maxIdx;
-}
-
-int wolfSSL_OCSP_response_status(OcspResponse *response)
-{
- return response->responseStatus;
-}
-
-const char *wolfSSL_OCSP_response_status_str(long s)
-{
- switch (s) {
- case OCSP_SUCCESSFUL:
- return "successful";
- case OCSP_MALFORMED_REQUEST:
- return "malformedrequest";
- case OCSP_INTERNAL_ERROR:
- return "internalerror";
- case OCSP_TRY_LATER:
- return "trylater";
- case OCSP_SIG_REQUIRED:
- return "sigrequired";
- case OCSP_UNAUTHROIZED:
- return "unauthorized";
- default:
- return "(UNKNOWN)";
- }
-}
-
-WOLFSSL_OCSP_BASICRESP* wolfSSL_OCSP_response_get1_basic(OcspResponse* response)
-{
- WOLFSSL_OCSP_BASICRESP* bs;
-
- bs = (WOLFSSL_OCSP_BASICRESP*)XMALLOC(sizeof(WOLFSSL_OCSP_BASICRESP), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (bs == NULL)
- return NULL;
-
- XMEMCPY(bs, response, sizeof(OcspResponse));
- bs->status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- bs->source = (byte*)XMALLOC(bs->maxIdx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (bs->status == NULL || bs->source == NULL) {
- if (bs->status) XFREE(bs->status, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (bs->source) XFREE(bs->source, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_OCSP_RESPONSE_free(bs);
- bs = NULL;
- }
- else {
- XMEMCPY(bs->status, response->status, sizeof(CertStatus));
- XMEMCPY(bs->source, response->source, response->maxIdx);
- }
- return bs;
-}
-
-OcspRequest* wolfSSL_OCSP_REQUEST_new(void)
-{
- OcspRequest* request;
-
- request = (OcspRequest*)XMALLOC(sizeof(OcspRequest), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (request != NULL)
- XMEMSET(request, 0, sizeof(OcspRequest));
-
- return request;
-}
-
-void wolfSSL_OCSP_REQUEST_free(OcspRequest* request)
-{
- FreeOcspRequest(request);
- XFREE(request, NULL, DYNAMIC_TYPE_OPENSSL);
-}
-
-int wolfSSL_i2d_OCSP_REQUEST(OcspRequest* request, unsigned char** data)
-{
- int size;
-
- size = EncodeOcspRequest(request, NULL, 0);
- if (size <= 0 || data == NULL)
- return size;
-
- return EncodeOcspRequest(request, *data, size);
-}
-
-WOLFSSL_OCSP_ONEREQ* wolfSSL_OCSP_request_add0_id(OcspRequest *req,
- WOLFSSL_OCSP_CERTID *cid)
-{
- if (req == NULL || cid == NULL)
- return NULL;
-
- FreeOcspRequest(req);
- XMEMCPY(req, cid, sizeof(OcspRequest));
-
- if (cid->serial != NULL) {
- req->serial = (byte*)XMALLOC(cid->serialSz, NULL,
- DYNAMIC_TYPE_OCSP_REQUEST);
- req->url = (byte*)XMALLOC(cid->urlSz, NULL, DYNAMIC_TYPE_OCSP_REQUEST);
- if (req->serial == NULL || req->url == NULL) {
- FreeOcspRequest(req);
- return NULL;
- }
-
- XMEMCPY(req->serial, cid->serial, cid->serialSz);
- XMEMCPY(req->url, cid->url, cid->urlSz);
- }
-
- wolfSSL_OCSP_REQUEST_free(cid);
-
- return req;
-}
-
-WOLFSSL_OCSP_CERTID* wolfSSL_OCSP_CERTID_dup(WOLFSSL_OCSP_CERTID* id)
-{
- WOLFSSL_OCSP_CERTID* certId;
-
- if (id == NULL) {
- return NULL;
- }
-
- certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(WOLFSSL_OCSP_CERTID),
- id->heap, DYNAMIC_TYPE_OPENSSL);
- if (certId) {
- XMEMCPY(certId, id, sizeof(WOLFSSL_OCSP_CERTID));
- }
- return certId;
-}
-#endif
-
-#if defined(OPENSSL_ALL) || defined(APACHE_HTTPD)
-int wolfSSL_i2d_OCSP_REQUEST_bio(WOLFSSL_BIO* out,
- WOLFSSL_OCSP_REQUEST *req)
-{
- int size = -1;
- unsigned char* data = NULL;
-
- WOLFSSL_ENTER("wolfSSL_i2d_OCSP_REQUEST_bio");
- if (out == NULL || req == NULL)
- return WOLFSSL_FAILURE;
-
- size = wolfSSL_i2d_OCSP_REQUEST(req, NULL);
- if (size > 0) {
- data = (unsigned char*) XMALLOC(size, out->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- if (data != NULL) {
- size = wolfSSL_i2d_OCSP_REQUEST(req, &data);
- }
-
- if (size <= 0) {
- XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_BIO_write(out, data, size) == (int)size) {
- XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_SUCCESS;
- }
-
- XFREE(data, out->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
-}
-#endif /* OPENSSL_ALL || APACHE_HTTPD */
-
-#ifdef OPENSSL_EXTRA
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_OCSP_REQUEST_add_ext(OcspRequest* req, WOLFSSL_X509_EXTENSION* ext,
- int idx)
-{
- WOLFSSL_STUB("wolfSSL_OCSP_REQUEST_add_ext");
- (void)req;
- (void)ext;
- (void)idx;
- return WOLFSSL_FATAL_ERROR;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-OcspResponse* wolfSSL_OCSP_response_create(int status,
- WOLFSSL_OCSP_BASICRESP* bs)
-{
- WOLFSSL_STUB("wolfSSL_OCSP_response_create");
- (void)status;
- (void)bs;
- return NULL;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-const char* wolfSSL_OCSP_crl_reason_str(long s)
-{
- WOLFSSL_STUB("wolfSSL_OCSP_crl_reason_str");
- (void)s;
- return NULL;
-}
-#endif
-
-/* Returns elements of an OCSP_CERTID struct. Currently only supports
- * returning the serial number, and returns an error if user requests
- * any of name, pmd, and/or keyHash.
- * Return 1 on success, 0 on failure */
-int wolfSSL_OCSP_id_get0_info(WOLFSSL_ASN1_STRING **name,
- WOLFSSL_ASN1_OBJECT **pmd, WOLFSSL_ASN1_STRING **keyHash,
- WOLFSSL_ASN1_INTEGER **serial, WOLFSSL_OCSP_CERTID *cid)
-{
- int i = 0;
- WOLFSSL_ASN1_INTEGER* ser;
-
- WOLFSSL_ENTER("wolfSSL_OCSP_id_get0_info");
-
- if (cid == NULL)
- return 0;
-
- /* build up ASN1_INTEGER for serial */
- if (serial != NULL) {
- ser = wolfSSL_ASN1_INTEGER_new();
- if (ser == NULL)
- return 0;
-
- if (cid->serialSz > (WOLFSSL_ASN1_INTEGER_MAX - 2)) {
- /* allocate data buffer, +2 for type and length */
- ser->data = (unsigned char*)XMALLOC(cid->serialSz + 2, NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (ser->data == NULL) {
- wolfSSL_ASN1_INTEGER_free(ser);
- return 0;
- }
- ser->dataMax = cid->serialSz + 2;
- ser->isDynamic = 1;
- } else {
- /* Use array instead of dynamic memory */
- ser->data = ser->intData;
- ser->dataMax = WOLFSSL_ASN1_INTEGER_MAX;
- }
-
- #ifdef WOLFSSL_QT
- /* Serial number starts at 0 index of ser->data */
- XMEMCPY(&ser->data[i], cid->serial, cid->serialSz);
- ser->length = cid->serialSz;
- #else
- ser->data[i++] = ASN_INTEGER;
- i += SetLength(cid->serialSz, ser->data + i);
- XMEMCPY(&ser->data[i], cid->serial, cid->serialSz);
- #endif
-
- cid->serialInt = ser;
- *serial = cid->serialInt;
- }
-
- /* Not needed for Apache, return error if user is requesting */
- if (name != NULL || pmd != NULL || keyHash != NULL) {
- if (name != NULL)
- *name = NULL;
-
- if (pmd != NULL)
- *pmd = NULL;
-
- if (keyHash != NULL)
- *keyHash = NULL;
- return 0;
- }
-
- return 1;
-}
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_OCSP_request_add1_nonce(OcspRequest* req, unsigned char* val,
- int sz)
-{
- WOLFSSL_STUB("wolfSSL_OCSP_request_add1_nonce");
- (void)req;
- (void)val;
- (void)sz;
- return WOLFSSL_FATAL_ERROR;
-}
-#endif
-
-/* Returns result of OCSP nonce comparison. Return values:
- * 1 - nonces are both present and equal
- * 2 - both nonces are absent
- * 3 - nonce only present in response
- * -1 - nonce only present in request
- * 0 - both nonces present and equal
- */
-int wolfSSL_OCSP_check_nonce(OcspRequest* req, WOLFSSL_OCSP_BASICRESP* bs)
-{
- byte* reqNonce = NULL;
- byte* rspNonce = NULL;
- int reqNonceSz = 0;
- int rspNonceSz = 0;
-
- WOLFSSL_ENTER("wolfSSL_OCSP_check_nonce");
-
- if (req != NULL) {
- reqNonce = req->nonce;
- reqNonceSz = req->nonceSz;
- }
-
- if (bs != NULL) {
- rspNonce = bs->nonce;
- rspNonceSz = bs->nonceSz;
- }
-
- /* nonce absent in both req and rsp */
- if (reqNonce == NULL && rspNonce == NULL)
- return 2;
-
- /* nonce present in rsp only */
- if (reqNonce == NULL && rspNonce != NULL)
- return 3;
-
- /* nonce present in req only */
- if (reqNonce != NULL && rspNonce == NULL)
- return -1;
-
- /* nonces are present and equal, return 1. Extra NULL check for fixing
- scan-build warning. */
- if (reqNonceSz == rspNonceSz && reqNonce && rspNonce) {
- if (XMEMCMP(reqNonce, rspNonce, reqNonceSz) == 0)
- return 1;
- }
-
- /* nonces are present but not equal */
- return 0;
-}
-#endif /* OPENSSL_EXTRA */
-
-#else /* HAVE_OCSP */
-
-
-#ifdef _MSC_VER
- /* 4206 warning for blank file */
- #pragma warning(disable: 4206)
-#endif
-
-
-#endif /* HAVE_OCSP */
-#endif /* WOLFCRYPT_ONLY */
diff --git a/client/wolfssl/src/sniffer.c b/client/wolfssl/src/sniffer.c
deleted file mode 100644
index 9087148..0000000
--- a/client/wolfssl/src/sniffer.c
+++ /dev/null
@@ -1,4611 +0,0 @@
-/* sniffer.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifndef WOLFCRYPT_ONLY
-#ifdef WOLFSSL_SNIFFER
-
-#include
-#include
-
-#ifndef _WIN32
- #include
-#else
- #include
-#endif
-
-#ifdef _WIN32
- #define SNPRINTF _snprintf
-#else
- #define SNPRINTF snprintf
-#endif
-
-#include
-#include
-#include
-#include
-#include
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include
-#endif
-
-#ifdef WOLF_CRYPTO_CB
- #include
- #ifdef HAVE_INTEL_QA_SYNC
- #include
- #endif
- #ifdef HAVE_CAVIUM_OCTEON_SYNC
- #include
- #endif
-#endif
-
-
-#ifndef WOLFSSL_SNIFFER_TIMEOUT
- #define WOLFSSL_SNIFFER_TIMEOUT 900
- /* Cache unclosed Sessions for 15 minutes since last used */
-#endif
-
-/* Misc constants */
-enum {
- MAX_SERVER_ADDRESS = 128, /* maximum server address length */
- MAX_SERVER_NAME = 128, /* maximum server name length */
- MAX_ERROR_LEN = 80, /* maximum error length */
- ETHER_IF_ADDR_LEN = 6, /* ethernet interface address length */
- LOCAL_IF_ADDR_LEN = 4, /* localhost interface address length, !windows */
- TCP_PROTO = 6, /* TCP_PROTOCOL */
- IP_HDR_SZ = 20, /* IPv4 header length, min */
- IP6_HDR_SZ = 40, /* IPv6 header length, min */
- TCP_HDR_SZ = 20, /* TCP header length, min */
- IPV4 = 4, /* IP version 4 */
- IPV6 = 6, /* IP version 6 */
- TCP_PROTOCOL = 6, /* TCP Protocol id */
- NO_NEXT_HEADER = 59, /* IPv6 no headers follow */
- TRACE_MSG_SZ = 80, /* Trace Message buffer size */
- HASH_SIZE = 499, /* Session Hash Table Rows */
- PSEUDO_HDR_SZ = 12, /* TCP Pseudo Header size in bytes */
- FATAL_ERROR_STATE = 1, /* SnifferSession fatal error state */
- TICKET_HINT_LEN = 4, /* Session Ticket Hint length */
- EXT_TYPE_SZ = 2, /* Extension length */
- MAX_INPUT_SZ = MAX_RECORD_SIZE + COMP_EXTRA + MAX_MSG_EXTRA +
- MTU_EXTRA, /* Max input sz of reassembly */
- EXT_MASTER_SECRET = 0x17, /* Extended Master Secret Extension ID */
- TICKET_EXT_ID = 0x23 /* Session Ticket Extension ID */
-};
-
-
-#ifdef _WIN32
-
-static HMODULE dllModule; /* for error string resources */
-
-BOOL APIENTRY DllMain( HMODULE hModule,
- DWORD ul_reason_for_call,
- LPVOID lpReserved
- )
-{
- static int didInit = 0;
-
- switch (ul_reason_for_call)
- {
- case DLL_PROCESS_ATTACH:
- if (didInit == 0) {
- dllModule = hModule;
- ssl_InitSniffer();
- didInit = 1;
- }
- break;
- case DLL_THREAD_ATTACH:
- break;
- case DLL_THREAD_DETACH:
- break;
- case DLL_PROCESS_DETACH:
- if (didInit) {
- ssl_FreeSniffer();
- didInit = 0;
- }
- break;
- }
- return TRUE;
-}
-
-#endif /* _WIN32 */
-
-
-static WOLFSSL_GLOBAL int TraceOn = 0; /* Trace is off by default */
-static WOLFSSL_GLOBAL FILE* TraceFile = 0;
-
-
-/* windows uses .rc table for this */
-#ifndef _WIN32
-
-static const char* const msgTable[] =
-{
- /* 1 */
- "Out of Memory",
- "New SSL Sniffer Server Registered",
- "Checking IP Header",
- "SSL Sniffer Server Not Registered",
- "Checking TCP Header",
-
- /* 6 */
- "SSL Sniffer Server Port Not Registered",
- "RSA Private Decrypt Error",
- "RSA Private Decode Error",
- "Set Cipher Spec Error",
- "Server Hello Input Malformed",
-
- /* 11 */
- "Couldn't Resume Session Error",
- "Server Did Resumption",
- "Client Hello Input Malformed",
- "Client Trying to Resume",
- "Handshake Input Malformed",
-
- /* 16 */
- "Got Hello Verify msg",
- "Got Server Hello msg",
- "Got Cert Request msg",
- "Got Server Key Exchange msg",
- "Got Cert msg",
-
- /* 21 */
- "Got Server Hello Done msg",
- "Got Finished msg",
- "Got Client Hello msg",
- "Got Client Key Exchange msg",
- "Got Cert Verify msg",
-
- /* 26 */
- "Got Unknown Handshake msg",
- "New SSL Sniffer Session created",
- "Couldn't create new SSL",
- "Got a Packet to decode",
- "No data present",
-
- /* 31 */
- "Session Not Found",
- "Got an Old Client Hello msg",
- "Old Client Hello Input Malformed",
- "Old Client Hello OK",
- "Bad Old Client Hello",
-
- /* 36 */
- "Bad Record Header",
- "Record Header Input Malformed",
- "Got a HandShake msg",
- "Bad HandShake msg",
- "Got a Change Cipher Spec msg",
-
- /* 41 */
- "Got Application Data msg",
- "Bad Application Data",
- "Got an Alert msg",
- "Another msg to Process",
- "Removing Session From Table",
-
- /* 46 */
- "Bad Key File",
- "Wrong IP Version",
- "Wrong Protocol type",
- "Packet Short for header processing",
- "Got Unknown Record Type",
-
- /* 51 */
- "Can't Open Trace File",
- "Session in Fatal Error State",
- "Partial SSL record received",
- "Buffer Error, malformed input",
- "Added to Partial Input",
-
- /* 56 */
- "Received a Duplicate Packet",
- "Received an Out of Order Packet",
- "Received an Overlap Duplicate Packet",
- "Received an Overlap Reassembly Begin Duplicate Packet",
- "Received an Overlap Reassembly End Duplicate Packet",
-
- /* 61 */
- "Missed the Client Hello Entirely",
- "Got Hello Request msg",
- "Got Session Ticket msg",
- "Bad Input",
- "Bad Decrypt Type",
-
- /* 66 */
- "Bad Finished Message Processing",
- "Bad Compression Type",
- "Bad DeriveKeys Error",
- "Saw ACK for Missing Packet Error",
- "Bad Decrypt Operation",
-
- /* 71 */
- "Decrypt Keys Not Set Up",
- "Late Key Load Error",
- "Got Certificate Status msg",
- "RSA Key Missing Error",
- "Secure Renegotiation Not Supported",
-
- /* 76 */
- "Get Session Stats Failure",
- "Reassembly Buffer Size Exceeded",
- "Dropping Lost Fragment",
- "Dropping Partial Record",
- "Clear ACK Fault",
-
- /* 81 */
- "Bad Decrypt Size",
- "Extended Master Secret Hash Error",
- "Handshake Message Split Across TLS Records",
- "ECC Private Decode Error",
- "ECC Public Decode Error",
-
- /* 86 */
- "Watch callback not set",
- "Watch hash failed",
- "Watch callback failed",
- "Bad Certificate Message",
- "Store data callback not set",
-
- /* 91 */
- "No data destination Error",
- "Store data callback failed",
- "Loading chain input"
-};
-
-
-/* *nix version uses table above */
-static void GetError(int idx, char* str)
-{
- XSTRNCPY(str, msgTable[idx - 1], MAX_ERROR_LEN-1);
- str[MAX_ERROR_LEN-1] = '\0';
-}
-
-
-#else /* _WIN32 */
-
-
-/* Windows version uses .rc table */
-static void GetError(int idx, char* buffer)
-{
- if (!LoadStringA(dllModule, idx, buffer, MAX_ERROR_LEN))
- buffer[0] = 0;
-}
-
-
-#endif /* _WIN32 */
-
-
-/* Packet Buffer for reassembly list and ready list */
-typedef struct PacketBuffer {
- word32 begin; /* relative sequence begin */
- word32 end; /* relative sequence end */
- byte* data; /* actual data */
- struct PacketBuffer* next; /* next on reassembly list or ready list */
-} PacketBuffer;
-
-
-#ifdef HAVE_SNI
-
-/* NamedKey maps a SNI name to a specific private key */
-typedef struct NamedKey {
- char name[MAX_SERVER_NAME]; /* server DNS name */
- word32 nameSz; /* size of server DNS name */
- byte* key; /* DER private key */
- word32 keySz; /* size of DER private key */
- struct NamedKey* next; /* for list */
-} NamedKey;
-
-#endif
-
-
-typedef struct IpAddrInfo {
- int version;
- union {
- word32 ip4;
- byte ip6[16];
- };
-} IpAddrInfo;
-
-
-/* Sniffer Server holds info for each server/port monitored */
-typedef struct SnifferServer {
- SSL_CTX* ctx; /* SSL context */
- char address[MAX_SERVER_ADDRESS]; /* passed in server address */
- IpAddrInfo server; /* network order address */
- int port; /* server port */
-#ifdef HAVE_SNI
- NamedKey* namedKeys; /* mapping of names and keys */
- wolfSSL_Mutex namedKeysMutex; /* mutex for namedKey list */
-#endif
- struct SnifferServer* next; /* for list */
-} SnifferServer;
-
-
-/* Session Flags */
-typedef struct Flags {
- byte side; /* which end is current packet headed */
- byte serverCipherOn; /* indicates whether cipher is active */
- byte clientCipherOn; /* indicates whether cipher is active */
- byte resuming; /* did this session come from resumption */
- byte cached; /* have we cached this session yet */
- byte clientHello; /* processed client hello yet, for SSLv2 */
- byte finCount; /* get both FINs before removing */
- byte fatalError; /* fatal error state */
- byte cliAckFault; /* client acked unseen data from server */
- byte srvAckFault; /* server acked unseen data from client */
- byte cliSkipPartial; /* client skips partial data to catch up */
- byte srvSkipPartial; /* server skips partial data to catch up */
-#ifdef HAVE_EXTENDED_MASTER
- byte expectEms; /* expect extended master secret */
-#endif
-} Flags;
-
-
-/* Out of Order FIN capture */
-typedef struct FinCaputre {
- word32 cliFinSeq; /* client relative sequence FIN 0 is no */
- word32 srvFinSeq; /* server relative sequence FIN, 0 is no */
- byte cliCounted; /* did we count yet, detects duplicates */
- byte srvCounted; /* did we count yet, detects duplicates */
-} FinCaputre;
-
-
-typedef struct HsHashes {
-#ifndef NO_OLD_TLS
-#ifndef NO_SHA
- wc_Sha hashSha;
-#endif
-#ifndef NO_MD5
- wc_Md5 hashMd5;
-#endif
-#endif
-#ifndef NO_SHA256
- wc_Sha256 hashSha256;
-#endif
-#ifdef WOLFSSL_SHA384
- wc_Sha384 hashSha384;
-#endif
-} HsHashes;
-
-
-/* Sniffer Session holds info for each client/server SSL/TLS session */
-typedef struct SnifferSession {
- SnifferServer* context; /* server context */
- SSL* sslServer; /* SSL server side decode */
- SSL* sslClient; /* SSL client side decode */
- IpAddrInfo server; /* server address in network byte order */
- IpAddrInfo client; /* client address in network byte order */
- word16 srvPort; /* server port */
- word16 cliPort; /* client port */
- word32 cliSeqStart; /* client start sequence */
- word32 srvSeqStart; /* server start sequence */
- word32 cliExpected; /* client expected sequence (relative) */
- word32 srvExpected; /* server expected sequence (relative) */
- FinCaputre finCaputre; /* retain out of order FIN s */
- Flags flags; /* session flags */
- time_t lastUsed; /* last used ticks */
- word32 keySz; /* size of the private key */
- PacketBuffer* cliReassemblyList; /* client out of order packets */
- PacketBuffer* srvReassemblyList; /* server out of order packets */
- word32 cliReassemblyMemory; /* client packet memory used */
- word32 srvReassemblyMemory; /* server packet memory used */
- struct SnifferSession* next; /* for hash table list */
- byte* ticketID; /* mac ID of session ticket */
-#ifdef HAVE_SNI
- const char* sni; /* server name indication */
-#endif
-#ifdef HAVE_EXTENDED_MASTER
- HsHashes* hash;
-#endif
-} SnifferSession;
-
-
-/* Sniffer Server List and mutex */
-static WOLFSSL_GLOBAL SnifferServer* ServerList = 0;
-static WOLFSSL_GLOBAL wolfSSL_Mutex ServerListMutex;
-
-
-/* Session Hash Table, mutex, and count */
-static WOLFSSL_GLOBAL SnifferSession* SessionTable[HASH_SIZE];
-static WOLFSSL_GLOBAL wolfSSL_Mutex SessionMutex;
-static WOLFSSL_GLOBAL int SessionCount = 0;
-
-/* Recovery of missed data switches and stats */
-static WOLFSSL_GLOBAL wolfSSL_Mutex RecoveryMutex; /* for stats */
-static WOLFSSL_GLOBAL int RecoveryEnabled = 0; /* global switch */
-static WOLFSSL_GLOBAL int MaxRecoveryMemory = -1;
- /* per session max recovery memory */
-static WOLFSSL_GLOBAL word32 MissedDataSessions = 0;
- /* # of sessions with missed data */
-
-/* Connection Info Callback */
-static WOLFSSL_GLOBAL SSLConnCb ConnectionCb;
-static WOLFSSL_GLOBAL void* ConnectionCbCtx = NULL;
-
-#ifdef WOLFSSL_SNIFFER_STATS
-/* Sessions Statistics */
-static WOLFSSL_GLOBAL SSLStats SnifferStats;
-static WOLFSSL_GLOBAL wolfSSL_Mutex StatsMutex;
-#endif
-
-#ifdef WOLFSSL_SNIFFER_WATCH
-/* Watch Key Callback */
-static WOLFSSL_GLOBAL SSLWatchCb WatchCb;
-static WOLFSSL_GLOBAL void* WatchCbCtx = NULL;
-#endif
-
-#ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
-/* Store Data Callback */
-static WOLFSSL_GLOBAL SSLStoreDataCb StoreDataCb;
-#endif
-
-
-static void UpdateMissedDataSessions(void)
-{
- wc_LockMutex(&RecoveryMutex);
- MissedDataSessions += 1;
- wc_UnLockMutex(&RecoveryMutex);
-}
-
-
-#ifdef WOLFSSL_SNIFFER_STATS
-#define LOCK_STAT() do { wc_LockMutex(&StatsMutex); } while (0)
-#define UNLOCK_STAT() do { wc_UnLockMutex(&StatsMutex); } while (0)
-#define NOLOCK_ADD_TO_STAT(x,y) do { TraceStat(#x, y); x += y; } while (0)
-#define NOLOCK_INC_STAT(x) NOLOCK_ADD_TO_STAT(x,1)
-#define ADD_TO_STAT(x,y) do { LOCK_STAT(); \
- NOLOCK_ADD_TO_STAT(x,y); UNLOCK_STAT(); } while (0)
-#define INC_STAT(x) do { LOCK_STAT(); \
- NOLOCK_INC_STAT(x); UNLOCK_STAT(); } while (0)
-#endif
-
-
-#ifdef WOLF_CRYPTO_CB
- static WOLFSSL_GLOBAL int CryptoDeviceId = INVALID_DEVID;
-#endif
-
-
-/* Initialize overall Sniffer */
-void ssl_InitSniffer(void)
-{
- wolfSSL_Init();
- wc_InitMutex(&ServerListMutex);
- wc_InitMutex(&SessionMutex);
- wc_InitMutex(&RecoveryMutex);
-#ifdef WOLFSSL_SNIFFER_STATS
- XMEMSET(&SnifferStats, 0, sizeof(SSLStats));
- wc_InitMutex(&StatsMutex);
-#endif
-#ifdef WOLF_CRYPTO_CB
- #ifdef HAVE_INTEL_QA_SYNC
- CryptoDeviceId = wc_CryptoCb_InitIntelQa();
- if (INVALID_DEVID == CryptoDeviceId) {
- printf("Couldn't init the Intel QA\n");
- }
- #endif
- #ifdef HAVE_CAVIUM_OCTEON_SYNC
- CryptoDeviceId = wc_CryptoCb_InitOcteon();
- if (INVALID_DEVID == CryptoDeviceId) {
- printf("Couldn't init the Intel QA\n");
- }
- #endif
-#endif
-}
-
-
-#ifdef HAVE_SNI
-
-/* Free Named Key and the zero out the private key it holds */
-static void FreeNamedKey(NamedKey* in)
-{
- if (in) {
- if (in->key) {
- ForceZero(in->key, in->keySz);
- XFREE(in->key, NULL, DYNAMIC_TYPE_X509);
- }
- XFREE(in, NULL, DYNAMIC_TYPE_SNIFFER_NAMED_KEY);
- }
-}
-
-
-static void FreeNamedKeyList(NamedKey* in)
-{
- NamedKey* next;
-
- while (in) {
- next = in->next;
- FreeNamedKey(in);
- in = next;
- }
-}
-
-#endif
-
-
-/* Free Sniffer Server's resources/self */
-static void FreeSnifferServer(SnifferServer* srv)
-{
- if (srv) {
-#ifdef HAVE_SNI
- wc_LockMutex(&srv->namedKeysMutex);
- FreeNamedKeyList(srv->namedKeys);
- wc_UnLockMutex(&srv->namedKeysMutex);
- wc_FreeMutex(&srv->namedKeysMutex);
-#endif
- SSL_CTX_free(srv->ctx);
- }
- XFREE(srv, NULL, DYNAMIC_TYPE_SNIFFER_SERVER);
-}
-
-
-/* free PacketBuffer's resources/self */
-static void FreePacketBuffer(PacketBuffer* del)
-{
- if (del) {
- XFREE(del->data, NULL, DYNAMIC_TYPE_SNIFFER_PB_BUFFER);
- XFREE(del, NULL, DYNAMIC_TYPE_SNIFFER_PB);
- }
-}
-
-
-/* remove PacketBuffer List */
-static void FreePacketList(PacketBuffer* in)
-{
- if (in) {
- PacketBuffer* del;
- PacketBuffer* packet = in;
-
- while (packet) {
- del = packet;
- packet = packet->next;
- FreePacketBuffer(del);
- }
- }
-}
-
-
-/* Free Sniffer Session's resources/self */
-static void FreeSnifferSession(SnifferSession* session)
-{
- if (session) {
- SSL_free(session->sslClient);
- SSL_free(session->sslServer);
-
- FreePacketList(session->cliReassemblyList);
- FreePacketList(session->srvReassemblyList);
-
- XFREE(session->ticketID, NULL, DYNAMIC_TYPE_SNIFFER_TICKET_ID);
-#ifdef HAVE_EXTENDED_MASTER
- XFREE(session->hash, NULL, DYNAMIC_TYPE_HASHES);
-#endif
- }
- XFREE(session, NULL, DYNAMIC_TYPE_SNIFFER_SESSION);
-}
-
-
-/* Free overall Sniffer */
-void ssl_FreeSniffer(void)
-{
- SnifferServer* srv;
- SnifferServer* removeServer;
- SnifferSession* session;
- SnifferSession* removeSession;
- int i;
-
- wc_LockMutex(&ServerListMutex);
- wc_LockMutex(&SessionMutex);
-
- srv = ServerList;
- while (srv) {
- removeServer = srv;
- srv = srv->next;
- FreeSnifferServer(removeServer);
- }
-
- for (i = 0; i < HASH_SIZE; i++) {
- session = SessionTable[i];
- while (session) {
- removeSession = session;
- session = session->next;
- FreeSnifferSession(removeSession);
- }
- }
-
- wc_UnLockMutex(&SessionMutex);
- wc_UnLockMutex(&ServerListMutex);
-
- wc_FreeMutex(&RecoveryMutex);
- wc_FreeMutex(&SessionMutex);
- wc_FreeMutex(&ServerListMutex);
-
-#ifdef WOLF_CRYPTO_CB
-#ifdef HAVE_INTEL_QA_SYNC
- wc_CryptoCb_CleanupIntelQa(&CryptoDeviceId);
-#endif
-#ifdef HAVE_CAVIUM_OCTEON_SYNC
- wc_CryptoCb_CleanupOcteon(&CryptoDeviceId);
-#endif
-#endif
-
- if (TraceFile) {
- TraceOn = 0;
- fclose(TraceFile);
- TraceFile = NULL;
- }
-
- wolfSSL_Cleanup();
-}
-
-
-#ifdef HAVE_EXTENDED_MASTER
-
-static int HashInit(HsHashes* hash)
-{
- int ret = 0;
-
- XMEMSET(hash, 0, sizeof(HsHashes));
-
-#ifndef NO_OLD_TLS
-#ifndef NO_SHA
- if (ret == 0)
- ret = wc_InitSha(&hash->hashSha);
-#endif
-#ifndef NO_MD5
- if (ret == 0) {
- ret = wc_InitMd5(&hash->hashMd5);
- }
-#endif
-#endif
-#ifndef NO_SHA256
- if (ret == 0)
- ret = wc_InitSha256(&hash->hashSha256);
-#endif
-#ifdef WOLFSSL_SHA384
- if (ret == 0)
- ret = wc_InitSha384(&hash->hashSha384);
-#endif
-
- return ret;
-}
-
-
-static int HashUpdate(HsHashes* hash, const byte* input, int sz)
-{
- int ret = 0;
-
- input -= HANDSHAKE_HEADER_SZ;
- sz += HANDSHAKE_HEADER_SZ;
-
-#ifndef NO_OLD_TLS
-#ifndef NO_SHA
- if (ret == 0)
- ret = wc_ShaUpdate(&hash->hashSha, input, sz);
-#endif
-#ifndef NO_MD5
- if (ret == 0) {
- ret = wc_Md5Update(&hash->hashMd5, input, sz);
- }
-#endif
-#endif
-#ifndef NO_SHA256
- if (ret == 0)
- ret = wc_Sha256Update(&hash->hashSha256, input, sz);
-#endif
-#ifdef WOLFSSL_SHA384
- if (ret == 0)
- ret = wc_Sha384Update(&hash->hashSha384, input, sz);
-#endif
-
- return ret;
-}
-
-
-static int HashCopy(HS_Hashes* d, HsHashes* s)
-{
-#ifndef NO_OLD_TLS
-#ifndef NO_SHA
- XMEMCPY(&d->hashSha, &s->hashSha, sizeof(wc_Sha));
-#endif
-#ifndef NO_MD5
- XMEMCPY(&d->hashMd5, &s->hashMd5, sizeof(wc_Md5));
-#endif
-#endif
-
-#ifndef NO_SHA256
- XMEMCPY(&d->hashSha256, &s->hashSha256, sizeof(wc_Sha256));
-#endif
-#ifdef WOLFSSL_SHA384
- XMEMCPY(&d->hashSha384, &s->hashSha384, sizeof(wc_Sha384));
-#endif
-
- return 0;
-}
-
-#endif
-
-
-/* Initialize a SnifferServer */
-static void InitSnifferServer(SnifferServer* sniffer)
-{
- XMEMSET(sniffer, 0, sizeof(SnifferServer));
-}
-
-
-/* Initialize session flags */
-static void InitFlags(Flags* flags)
-{
- XMEMSET(flags, 0, sizeof(Flags));
-}
-
-
-/* Initialize FIN Capture */
-static void InitFinCapture(FinCaputre* cap)
-{
- XMEMSET(cap, 0, sizeof(FinCaputre));
-}
-
-
-/* Initialize a Sniffer Session */
-static void InitSession(SnifferSession* session)
-{
- XMEMSET(session, 0, sizeof(SnifferSession));
- InitFlags(&session->flags);
- InitFinCapture(&session->finCaputre);
-}
-
-
-/* IP Info from IP Header */
-typedef struct IpInfo {
- int length; /* length of this header */
- int total; /* total length of fragment */
- IpAddrInfo src; /* network order source address */
- IpAddrInfo dst; /* network order destination address */
-} IpInfo;
-
-
-/* TCP Info from TCP Header */
-typedef struct TcpInfo {
- int srcPort; /* source port */
- int dstPort; /* source port */
- int length; /* length of this header */
- word32 sequence; /* sequence number */
- word32 ackNumber; /* ack number */
- byte fin; /* FIN set */
- byte rst; /* RST set */
- byte syn; /* SYN set */
- byte ack; /* ACK set */
-} TcpInfo;
-
-
-/* Tcp Pseudo Header for Checksum calculation */
-typedef struct TcpPseudoHdr {
- word32 src; /* source address */
- word32 dst; /* destination address */
- byte rsv; /* reserved, always 0 */
- byte protocol; /* IP protocol */
- word16 length; /* tcp header length + data length (doesn't include */
- /* pseudo header length) network order */
-} TcpPseudoHdr;
-
-
-/* Password Setting Callback */
-static int SetPassword(char* passwd, int sz, int rw, void* userdata)
-{
- (void)rw;
- XSTRNCPY(passwd, (const char*)userdata, sz);
- return (int)XSTRLEN((const char*)userdata);
-}
-
-
-/* Ethernet Header */
-typedef struct EthernetHdr {
- byte dst[ETHER_IF_ADDR_LEN]; /* destination host address */
- byte src[ETHER_IF_ADDR_LEN]; /* source host address */
- word16 type; /* IP, ARP, etc */
-} EthernetHdr;
-
-
-/* IPv4 Header */
-typedef struct IpHdr {
- byte ver_hl; /* version/header length */
- byte tos; /* type of service */
- word16 length; /* total length */
- word16 id; /* identification */
- word16 offset; /* fragment offset field */
- byte ttl; /* time to live */
- byte protocol; /* protocol */
- word16 sum; /* checksum */
- word32 src; /* source address */
- word32 dst; /* destination address */
-} IpHdr;
-
-
-/* IPv6 Header */
-typedef struct Ip6Hdr {
- byte ver_hl; /* version/traffic class high */
- byte tc_fl; /* traffic class low/flow label high */
- word16 fl; /* flow label low */
- word16 length; /* payload length */
- byte next_header; /* next header (6 for TCP, any other skip) */
- byte hl; /* hop limit */
- byte src[16]; /* source address */
- byte dst[16]; /* destination address */
-} Ip6Hdr;
-
-
-/* IPv6 extension header */
-typedef struct Ip6ExtHdr {
- byte next_header; /* next header (6 for TCP, any other skip) */
- byte length; /* length in 8-octet units - 1 */
- byte reserved[6];
-} Ip6ExtHdr;
-
-
-#define IP_HL(ip) ( (((ip)->ver_hl) & 0x0f) * 4)
-#define IP_V(ip) ( ((ip)->ver_hl) >> 4)
-
-/* TCP Header */
-typedef struct TcpHdr {
- word16 srcPort; /* source port */
- word16 dstPort; /* destination port */
- word32 sequence; /* sequence number */
- word32 ack; /* acknowledgment number */
- byte offset; /* data offset, reserved */
- byte flags; /* option flags */
- word16 window; /* window */
- word16 sum; /* checksum */
- word16 urgent; /* urgent pointer */
-} TcpHdr;
-
-#define TCP_LEN(tcp) ( (((tcp)->offset & 0xf0) >> 4) * 4)
-#define TCP_FIN 0x01
-#define TCP_SYN 0x02
-#define TCP_RST 0x04
-#define TCP_ACK 0x10
-
-
-
-
-
-/* Use platform specific GetError to write to trace file if tracing */
-static void Trace(int idx)
-{
- if (TraceOn) {
- char myBuffer[MAX_ERROR_LEN];
- GetError(idx, myBuffer);
- fprintf(TraceFile, "\t%s\n", myBuffer);
-#ifdef DEBUG_SNIFFER
- fprintf(stderr, "\t%s\n", myBuffer);
-#endif
- }
-}
-
-
-/* Show TimeStamp for beginning of packet Trace */
-static void TraceHeader(void)
-{
- if (TraceOn) {
- time_t ticks = time(NULL);
- fprintf(TraceFile, "\n%s", ctime(&ticks));
- }
-}
-
-
-/* Show Set Server info for Trace */
-static void TraceSetServer(const char* srv, int port, const char* keyFile)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tTrying to install a new Sniffer Server with\n");
- fprintf(TraceFile, "\tserver: %s, port: %d, keyFile: %s\n", srv, port,
- keyFile);
- }
-}
-
-
-#ifdef HAVE_SNI
-
-/* Show Set Named Server info for Trace */
-static void TraceSetNamedServer(const char* name,
- const char* srv, int port, const char* keyFile)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tTrying to install a new Sniffer Server with\n");
- fprintf(TraceFile, "\tname: %s, server: %s, port: %d, keyFile: %s\n",
- name, srv, port, keyFile);
- }
-}
-
-#endif
-
-
-/* Trace got packet number */
-static void TracePacket(void)
-{
- if (TraceOn) {
- static word32 packetNumber = 0;
- fprintf(TraceFile, "\tGot a Packet to decode, packet %u\n",
- ++packetNumber);
- }
-}
-
-
-/* Convert network byte order address into human readable */
-static const char* IpToS(int version, void* src, char* dst)
-{
- return inet_ntop(version, src, dst, TRACE_MSG_SZ);
-}
-
-
-/* Show destination and source address from Ip Hdr for packet Trace */
-static void TraceIP(IpHdr* iphdr)
-{
- if (TraceOn) {
- char src[TRACE_MSG_SZ];
- char dst[TRACE_MSG_SZ];
- fprintf(TraceFile, "\tdst:%s src:%s\n",
- IpToS(AF_INET, &iphdr->dst, dst),
- IpToS(AF_INET, &iphdr->src, src));
- }
-}
-
-
-/* Show destination and source address from Ip6Hdr for packet Trace */
-static void TraceIP6(Ip6Hdr* iphdr)
-{
- if (TraceOn) {
- char src[TRACE_MSG_SZ];
- char dst[TRACE_MSG_SZ];
- fprintf(TraceFile, "\tdst: %s src: %s\n",
- IpToS(AF_INET6, iphdr->dst, dst),
- IpToS(AF_INET6, iphdr->src, src));
- }
-}
-
-
-/* Show destination and source port from Tcp Hdr for packet Trace */
-static void TraceTcp(TcpHdr* tcphdr)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tdstPort:%u srcPort:%u\n", ntohs(tcphdr->dstPort),
- ntohs(tcphdr->srcPort));
- }
-}
-
-
-/* Show sequence and payload length for Trace */
-static void TraceSequence(word32 seq, int len)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tSequence:%u, payload length:%d\n", seq, len);
- }
-}
-
-
-/* Show sequence and payload length for Trace */
-static void TraceAck(word32 ack, word32 expected)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tAck:%u Expected:%u\n", ack, expected);
- }
-}
-
-
-/* Show relative expected and relative received sequences */
-static void TraceRelativeSequence(word32 expected, word32 got)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tExpected sequence:%u, received sequence:%u\n",
- expected, got);
- }
-}
-
-
-/* Show server sequence startup from SYN */
-static void TraceServerSyn(word32 seq)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tServer SYN, Sequence Start:%u\n", seq);
- }
-}
-
-
-/* Show client sequence startup from SYN */
-static void TraceClientSyn(word32 seq)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tClient SYN, Sequence Start:%u\n", seq);
- }
-}
-
-
-/* Show client FIN capture */
-static void TraceClientFin(word32 finSeq, word32 relSeq)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tClient FIN capture:%u, current SEQ:%u\n",
- finSeq, relSeq);
- }
-}
-
-
-/* Show server FIN capture */
-static void TraceServerFin(word32 finSeq, word32 relSeq)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tServer FIN capture:%u, current SEQ:%u\n",
- finSeq, relSeq);
- }
-}
-
-
-/* Show number of SSL data bytes decoded, could be 0 (ok) */
-static void TraceGotData(int bytes)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\t%d bytes of SSL App data processed\n", bytes);
- }
-}
-
-
-/* Show bytes added to old SSL App data */
-static void TraceAddedData(int newBytes, int existingBytes)
-{
- if (TraceOn) {
- fprintf(TraceFile,
- "\t%d bytes added to %d existing bytes in User Buffer\n",
- newBytes, existingBytes);
- }
-}
-
-
-/* Show Stale Session */
-static void TraceStaleSession(void)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tFound a stale session\n");
- }
-}
-
-
-/* Show Finding Stale Sessions */
-static void TraceFindingStale(void)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tTrying to find Stale Sessions\n");
- }
-}
-
-
-/* Show Removed Session */
-static void TraceRemovedSession(void)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tRemoved it\n");
- }
-}
-
-
-/* Show SSLInfo if provided and is valid. */
-static void TraceSessionInfo(SSLInfo* sslInfo)
-{
- if (TraceOn) {
- if (sslInfo != NULL && sslInfo->isValid) {
- fprintf(TraceFile,
- "\tver:(%u %u) suiteId:(%02x %02x) suiteName:(%s) "
- #ifdef HAVE_SNI
- "sni:(%s) "
- #endif
- "keySize:(%u)\n",
- sslInfo->protocolVersionMajor,
- sslInfo->protocolVersionMinor,
- sslInfo->serverCipherSuite0,
- sslInfo->serverCipherSuite,
- sslInfo->serverCipherSuiteName,
- #ifdef HAVE_SNI
- sslInfo->serverNameIndication,
- #endif
- sslInfo->keySize);
- }
- }
-}
-
-
-#ifdef WOLFSSL_SNIFFER_STATS
-
-/* Show value added to a named statistic. */
-static void TraceStat(const char* name, int add)
-{
- if (TraceOn) {
- fprintf(TraceFile, "\tAdding %d to %s\n", add, name);
- }
-}
-
-#endif
-
-
-/* Set user error string */
-static void SetError(int idx, char* error, SnifferSession* session, int fatal)
-{
- GetError(idx, error);
- Trace(idx);
- if (session && fatal == FATAL_ERROR_STATE)
- session->flags.fatalError = 1;
-}
-
-
-/* Compare IpAddrInfo structs */
-static WC_INLINE int MatchAddr(IpAddrInfo l, IpAddrInfo r)
-{
- if (l.version == r.version) {
- if (l.version == IPV4)
- return (l.ip4 == r.ip4);
- else if (l.version == IPV6)
- return (0 == XMEMCMP(l.ip6, r.ip6, sizeof(l.ip6)));
- }
- return 0;
-}
-
-
-#ifndef WOLFSSL_SNIFFER_WATCH
-
-/* See if this IPV4 network order address has been registered */
-/* return 1 is true, 0 is false */
-static int IsServerRegistered(word32 addr)
-{
- int ret = 0; /* false */
- SnifferServer* sniffer;
-
- wc_LockMutex(&ServerListMutex);
-
- sniffer = ServerList;
- while (sniffer) {
- if (sniffer->server.ip4 == addr) {
- ret = 1;
- break;
- }
- sniffer = sniffer->next;
- }
-
- wc_UnLockMutex(&ServerListMutex);
-
- return ret;
-}
-
-
-/* See if this port has been registered to watch */
-/* See if this IPV4 network order address has been registered */
-/* return 1 is true, 0 is false */
-static int IsServerRegistered6(byte* addr)
-{
- int ret = 0; /* false */
- SnifferServer* sniffer;
-
- wc_LockMutex(&ServerListMutex);
-
- sniffer = ServerList;
- while (sniffer) {
- if (sniffer->server.version == IPV6 &&
- 0 == XMEMCMP(sniffer->server.ip6, addr, sizeof(sniffer->server.ip6))) {
- ret = 1;
- break;
- }
- sniffer = sniffer->next;
- }
-
- wc_UnLockMutex(&ServerListMutex);
-
- return ret;
-}
-
-
-/* See if this port has been registered to watch */
-/* return 1 is true, 0 is false */
-static int IsPortRegistered(word32 port)
-{
- int ret = 0; /* false */
- SnifferServer* sniffer;
-
- wc_LockMutex(&ServerListMutex);
-
- sniffer = ServerList;
- while (sniffer) {
- if (sniffer->port == (int)port) {
- ret = 1;
- break;
- }
- sniffer = sniffer->next;
- }
-
- wc_UnLockMutex(&ServerListMutex);
-
- return ret;
-}
-
-#endif
-
-
-/* Get SnifferServer from IP and Port */
-static SnifferServer* GetSnifferServer(IpInfo* ipInfo, TcpInfo* tcpInfo)
-{
- SnifferServer* sniffer;
-
- wc_LockMutex(&ServerListMutex);
-
- sniffer = ServerList;
-
-#ifndef WOLFSSL_SNIFFER_WATCH
- while (sniffer) {
- if (sniffer->port == tcpInfo->srcPort &&
- MatchAddr(sniffer->server, ipInfo->src))
- break;
- if (sniffer->port == tcpInfo->dstPort &&
- MatchAddr(sniffer->server, ipInfo->dst))
- break;
-
- sniffer = sniffer->next;
- }
-#else
- (void)ipInfo;
- (void)tcpInfo;
-#endif
-
- wc_UnLockMutex(&ServerListMutex);
-
- return sniffer;
-}
-
-
-/* Hash the Session Info, return hash row */
-static word32 SessionHash(IpInfo* ipInfo, TcpInfo* tcpInfo)
-{
- word32 hash = 1;
-
- if (ipInfo->src.version == IPV4) {
- hash *= ipInfo->src.ip4 * ipInfo->dst.ip4;
- }
- else if (ipInfo->src.version == IPV6) {
- word32* x;
- word32 y;
- x = (word32*)ipInfo->src.ip6;
- y = x[0] ^ x[1] ^ x[2] ^ x[3];
- hash *= y;
- x = (word32*)ipInfo->dst.ip6;
- y = x[0] ^ x[1] ^ x[2] ^ x[3];
- hash *= y;
- }
- hash *= tcpInfo->srcPort * tcpInfo->dstPort;
-
- return hash % HASH_SIZE;
-}
-
-
-/* Get Existing SnifferSession from IP and Port */
-static SnifferSession* GetSnifferSession(IpInfo* ipInfo, TcpInfo* tcpInfo)
-{
- SnifferSession* session;
- time_t currTime = time(NULL);
- word32 row = SessionHash(ipInfo, tcpInfo);
-
- assert(row <= HASH_SIZE);
-
- wc_LockMutex(&SessionMutex);
-
- session = SessionTable[row];
- while (session) {
- if (MatchAddr(session->server, ipInfo->src) &&
- MatchAddr(session->client, ipInfo->dst) &&
- session->srvPort == tcpInfo->srcPort &&
- session->cliPort == tcpInfo->dstPort)
- break;
-
- if (MatchAddr(session->client, ipInfo->src) &&
- MatchAddr(session->server, ipInfo->dst) &&
- session->cliPort == tcpInfo->srcPort &&
- session->srvPort == tcpInfo->dstPort)
- break;
-
- session = session->next;
- }
-
- if (session)
- session->lastUsed= currTime; /* keep session alive, remove stale will */
- /* leave alone */
- wc_UnLockMutex(&SessionMutex);
-
- /* determine side */
- if (session) {
- if (MatchAddr(ipInfo->dst, session->server) &&
- tcpInfo->dstPort == session->srvPort) {
-
- session->flags.side = WOLFSSL_SERVER_END;
- }
- else {
- session->flags.side = WOLFSSL_CLIENT_END;
- }
- }
-
- return session;
-}
-
-
-#if defined(HAVE_SNI) || defined(WOLFSSL_SNIFFER_WATCH)
-
-static int LoadKeyFile(byte** keyBuf, word32* keyBufSz,
- const char* keyFile, int typeKey,
- const char* password)
-{
- byte* loadBuf;
- long fileSz = 0;
- XFILE file;
- int ret;
-
- if (keyBuf == NULL || keyBufSz == NULL || keyFile == NULL) {
- return -1;
- }
-
- file = XFOPEN(keyFile, "rb");
- if (file == XBADFILE) return -1;
- if(XFSEEK(file, 0, XSEEK_END) != 0) {
- XFCLOSE(file);
- return -1;
- }
- fileSz = XFTELL(file);
- if (fileSz > MAX_WOLFSSL_FILE_SIZE || fileSz < 0) {
- XFCLOSE(file);
- return -1;
- }
- XREWIND(file);
-
- loadBuf = (byte*)XMALLOC(fileSz, NULL, DYNAMIC_TYPE_FILE);
- if (loadBuf == NULL) {
- XFCLOSE(file);
- return -1;
- }
-
- ret = (int)XFREAD(loadBuf, 1, fileSz, file);
- XFCLOSE(file);
-
- if (ret != fileSz) {
- XFREE(loadBuf, NULL, DYNAMIC_TYPE_FILE);
- return -1;
- }
-
- if (typeKey == WOLFSSL_FILETYPE_PEM) {
- byte* saveBuf = (byte*)XMALLOC(fileSz, NULL, DYNAMIC_TYPE_X509);
- int saveBufSz = 0;
-
- ret = -1;
- if (saveBuf != NULL) {
- saveBufSz = wc_KeyPemToDer(loadBuf, (int)fileSz,
- saveBuf, (int)fileSz, password);
- if (saveBufSz < 0) {
- saveBufSz = 0;
- XFREE(saveBuf, NULL, DYNAMIC_TYPE_X509);
- saveBuf = NULL;
- }
- else
- ret = 0;
- }
-
- ForceZero(loadBuf, (word32)fileSz);
- XFREE(loadBuf, NULL, DYNAMIC_TYPE_FILE);
-
- if (saveBuf) {
- *keyBuf = saveBuf;
- *keyBufSz = (word32)saveBufSz;
- }
- }
- else {
- *keyBuf = loadBuf;
- *keyBufSz = (word32)fileSz;
- }
-
- if (ret < 0) {
- return -1;
- }
-
- return ret;
-}
-
-#endif
-
-
-#ifdef WOLFSSL_SNIFFER_WATCH
-
-static int CreateWatchSnifferServer(char* error)
-{
- SnifferServer* sniffer;
-
- sniffer = (SnifferServer*)XMALLOC(sizeof(SnifferServer), NULL,
- DYNAMIC_TYPE_SNIFFER_SERVER);
- if (sniffer == NULL) {
- SetError(MEMORY_STR, error, NULL, 0);
- return -1;
- }
- InitSnifferServer(sniffer);
- sniffer->ctx = SSL_CTX_new(TLSv1_2_client_method());
- if (!sniffer->ctx) {
- SetError(MEMORY_STR, error, NULL, 0);
- FreeSnifferServer(sniffer);
- return -1;
- }
-#ifdef WOLF_CRYPTO_CB
- if (CryptoDeviceId != INVALID_DEVID)
- wolfSSL_CTX_SetDevId(sniffer->ctx, CryptoDeviceId);
-#endif
- ServerList = sniffer;
-
- return 0;
-}
-
-#endif
-
-
-static int SetNamedPrivateKey(const char* name, const char* address, int port,
- const char* keyFile, int typeKey, const char* password, char* error)
-{
- SnifferServer* sniffer;
- int ret;
- int type = (typeKey == FILETYPE_PEM) ? WOLFSSL_FILETYPE_PEM :
- WOLFSSL_FILETYPE_ASN1;
- int isNew = 0;
- IpAddrInfo serverIp;
-
-#ifdef HAVE_SNI
- NamedKey* namedKey = NULL;
-#endif
-
- (void)name;
-#ifdef HAVE_SNI
- if (name != NULL) {
- namedKey = (NamedKey*)XMALLOC(sizeof(NamedKey),
- NULL, DYNAMIC_TYPE_SNIFFER_NAMED_KEY);
- if (namedKey == NULL) {
- SetError(MEMORY_STR, error, NULL, 0);
- return -1;
- }
- XMEMSET(namedKey, 0, sizeof(NamedKey));
-
- namedKey->nameSz = (word32)XSTRLEN(name);
- if (namedKey->nameSz > sizeof(namedKey->name)-1)
- namedKey->nameSz = sizeof(namedKey->name)-1;
- XSTRNCPY(namedKey->name, name, namedKey->nameSz);
- namedKey->name[MAX_SERVER_NAME-1] = '\0';
-
- ret = LoadKeyFile(&namedKey->key, &namedKey->keySz,
- keyFile, type, password);
- if (ret < 0) {
- SetError(KEY_FILE_STR, error, NULL, 0);
- FreeNamedKey(namedKey);
- return -1;
- }
- }
-#endif
-
- serverIp.version = IPV4;
- serverIp.ip4 = inet_addr(address);
- if (serverIp.ip4 == INADDR_NONE) {
- if (inet_pton(AF_INET6, address, serverIp.ip6) == 1) {
- serverIp.version = IPV6;
- }
- }
- sniffer = ServerList;
- while (sniffer != NULL &&
- (!MatchAddr(sniffer->server, serverIp) || sniffer->port != port)) {
- sniffer = sniffer->next;
- }
-
- if (sniffer == NULL) {
- isNew = 1;
- sniffer = (SnifferServer*)XMALLOC(sizeof(SnifferServer),
- NULL, DYNAMIC_TYPE_SNIFFER_SERVER);
- if (sniffer == NULL) {
- SetError(MEMORY_STR, error, NULL, 0);
-#ifdef HAVE_SNI
- FreeNamedKey(namedKey);
-#endif
- return -1;
- }
- InitSnifferServer(sniffer);
-
- XSTRNCPY(sniffer->address, address, MAX_SERVER_ADDRESS-1);
- sniffer->address[MAX_SERVER_ADDRESS-1] = '\0';
- sniffer->server = serverIp;
- sniffer->port = port;
-
- sniffer->ctx = SSL_CTX_new(TLSv1_2_client_method());
- if (!sniffer->ctx) {
- SetError(MEMORY_STR, error, NULL, 0);
-#ifdef HAVE_SNI
- FreeNamedKey(namedKey);
-#endif
- FreeSnifferServer(sniffer);
- return -1;
- }
- }
-
- if (name == NULL) {
- if (password) {
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- SSL_CTX_set_default_passwd_cb(sniffer->ctx, SetPassword);
- SSL_CTX_set_default_passwd_cb_userdata(
- sniffer->ctx, (void*)password);
- #endif
- }
- ret = SSL_CTX_use_PrivateKey_file(sniffer->ctx, keyFile, type);
- if (ret != WOLFSSL_SUCCESS) {
- SetError(KEY_FILE_STR, error, NULL, 0);
- if (isNew)
- FreeSnifferServer(sniffer);
- return -1;
- }
- #ifdef WOLF_CRYPTO_CB
- wolfSSL_CTX_SetDevId(sniffer->ctx, CryptoDeviceId);
- #endif
- }
-#ifdef HAVE_SNI
- else {
- wc_LockMutex(&sniffer->namedKeysMutex);
- namedKey->next = sniffer->namedKeys;
- sniffer->namedKeys = namedKey;
- wc_UnLockMutex(&sniffer->namedKeysMutex);
- }
-#endif
-
- if (isNew) {
- sniffer->next = ServerList;
- ServerList = sniffer;
- }
-
- return 0;
-}
-
-
-#ifdef HAVE_SNI
-
-/* Sets the private key for a specific name, server and port */
-/* returns 0 on success, -1 on error */
-int ssl_SetNamedPrivateKey(const char* name,
- const char* address, int port,
- const char* keyFile, int typeKey,
- const char* password, char* error)
-{
- int ret;
-
- TraceHeader();
- TraceSetNamedServer(name, address, port, keyFile);
-
- wc_LockMutex(&ServerListMutex);
- ret = SetNamedPrivateKey(name, address, port, keyFile,
- typeKey, password, error);
- wc_UnLockMutex(&ServerListMutex);
-
- if (ret == 0)
- Trace(NEW_SERVER_STR);
-
- return ret;
-}
-
-#endif
-
-
-/* Sets the private key for a specific server and port */
-/* returns 0 on success, -1 on error */
-int ssl_SetPrivateKey(const char* address, int port, const char* keyFile,
- int typeKey, const char* password, char* error)
-{
- int ret;
-
- TraceHeader();
- TraceSetServer(address, port, keyFile);
-
- wc_LockMutex(&ServerListMutex);
- ret = SetNamedPrivateKey(NULL, address, port, keyFile,
- typeKey, password, error);
- wc_UnLockMutex(&ServerListMutex);
-
- if (ret == 0)
- Trace(NEW_SERVER_STR);
-
- return ret;
-}
-
-
-/* Check IP Header for IPV6, TCP, and a registered server address */
-/* returns 0 on success, -1 on error */
-static int CheckIp6Hdr(Ip6Hdr* iphdr, IpInfo* info, int length, char* error)
-{
- int version = IP_V(iphdr);
- int exthdrsz = IP6_HDR_SZ;
-
- TraceIP6(iphdr);
- Trace(IP_CHECK_STR);
-
- if (version != IPV6) {
- SetError(BAD_IPVER_STR, error, NULL, 0);
- return -1;
- }
-
- /* Here, we need to move onto next header if not TCP. */
- if (iphdr->next_header != TCP_PROTOCOL) {
- Ip6ExtHdr* exthdr = (Ip6ExtHdr*)((byte*)iphdr + IP6_HDR_SZ);
- do {
- int hdrsz = (exthdr->length + 1) * 8;
- if (hdrsz > length - exthdrsz) {
- SetError(PACKET_HDR_SHORT_STR, error, NULL, 0);
- return -1;
- }
- exthdrsz += hdrsz;
- exthdr = (Ip6ExtHdr*)((byte*)exthdr + hdrsz);
- }
- while (exthdr->next_header != TCP_PROTOCOL &&
- exthdr->next_header != NO_NEXT_HEADER);
- }
-
-#ifndef WOLFSSL_SNIFFER_WATCH
- if (!IsServerRegistered6(iphdr->src) && !IsServerRegistered6(iphdr->dst)) {
- SetError(SERVER_NOT_REG_STR, error, NULL, 0);
- return -1;
- }
-#endif
-
- info->length = exthdrsz;
- info->total = ntohs(iphdr->length) + info->length;
- /* IPv6 doesn't include its own header size in the length like v4. */
- info->src.version = IPV6;
- XMEMCPY(info->src.ip6, iphdr->src, sizeof(info->src.ip6));
- info->dst.version = IPV6;
- XMEMCPY(info->dst.ip6, iphdr->dst, sizeof(info->dst.ip6));
-
- return 0;
-}
-
-
-/* Check IP Header for IPV4, TCP, and a registered server address */
-/* If header IPv6, pass to CheckIp6Hdr(). */
-/* returns 0 on success, -1 on error */
-static int CheckIpHdr(IpHdr* iphdr, IpInfo* info, int length, char* error)
-{
- int version = IP_V(iphdr);
-
- if (version == IPV6)
- return CheckIp6Hdr((Ip6Hdr*)iphdr, info, length, error);
-
- TraceIP(iphdr);
- Trace(IP_CHECK_STR);
-
- if (version != IPV4) {
- SetError(BAD_IPVER_STR, error, NULL, 0);
- return -1;
- }
-
- if (iphdr->protocol != TCP_PROTOCOL) {
- SetError(BAD_PROTO_STR, error, NULL, 0);
- return -1;
- }
-
-#ifndef WOLFSSL_SNIFFER_WATCH
- if (!IsServerRegistered(iphdr->src) && !IsServerRegistered(iphdr->dst)) {
- SetError(SERVER_NOT_REG_STR, error, NULL, 0);
- return -1;
- }
-#endif
-
- info->length = IP_HL(iphdr);
- info->total = ntohs(iphdr->length);
- info->src.version = IPV4;
- info->src.ip4 = iphdr->src;
- info->dst.version = IPV4;
- info->dst.ip4 = iphdr->dst;
-
- if (info->total == 0)
- info->total = length; /* reassembled may be off */
-
- return 0;
-}
-
-
-/* Check TCP Header for a registered port */
-/* returns 0 on success, -1 on error */
-static int CheckTcpHdr(TcpHdr* tcphdr, TcpInfo* info, char* error)
-{
- TraceTcp(tcphdr);
- Trace(TCP_CHECK_STR);
- info->srcPort = ntohs(tcphdr->srcPort);
- info->dstPort = ntohs(tcphdr->dstPort);
- info->length = TCP_LEN(tcphdr);
- info->sequence = ntohl(tcphdr->sequence);
- info->fin = tcphdr->flags & TCP_FIN;
- info->rst = tcphdr->flags & TCP_RST;
- info->syn = tcphdr->flags & TCP_SYN;
- info->ack = tcphdr->flags & TCP_ACK;
- if (info->ack)
- info->ackNumber = ntohl(tcphdr->ack);
-
-#ifndef WOLFSSL_SNIFFER_WATCH
- if (!IsPortRegistered(info->srcPort) && !IsPortRegistered(info->dstPort)) {
- SetError(SERVER_PORT_NOT_REG_STR, error, NULL, 0);
- return -1;
- }
-#else
- (void)error;
-#endif
-
- return 0;
-}
-
-
-/* Decode Record Layer Header */
-static int GetRecordHeader(const byte* input, RecordLayerHeader* rh, int* size)
-{
- XMEMCPY(rh, input, RECORD_HEADER_SZ);
- *size = (rh->length[0] << 8) | rh->length[1];
-
- if (*size > (MAX_RECORD_SIZE + COMP_EXTRA + MAX_MSG_EXTRA))
- return LENGTH_ERROR;
-
- return 0;
-}
-
-
-/* Copies the session's information to the provided sslInfo. Skip copy if
- * SSLInfo is not provided. */
-static void CopySessionInfo(SnifferSession* session, SSLInfo* sslInfo)
-{
- if (NULL != sslInfo) {
- XMEMSET(sslInfo, 0, sizeof(SSLInfo));
-
- /* Pass back Session Info after we have processed the Server Hello. */
- if (0 != session->sslServer->options.cipherSuite) {
- const char* pCipher;
-
- sslInfo->isValid = 1;
- sslInfo->protocolVersionMajor = session->sslServer->version.major;
- sslInfo->protocolVersionMinor = session->sslServer->version.minor;
- sslInfo->serverCipherSuite0 =
- session->sslServer->options.cipherSuite0;
- sslInfo->serverCipherSuite =
- session->sslServer->options.cipherSuite;
-
- pCipher = wolfSSL_get_cipher(session->sslServer);
- if (NULL != pCipher) {
- XSTRNCPY((char*)sslInfo->serverCipherSuiteName, pCipher,
- sizeof(sslInfo->serverCipherSuiteName));
- sslInfo->serverCipherSuiteName
- [sizeof(sslInfo->serverCipherSuiteName) - 1] = '\0';
- }
- sslInfo->keySize = session->keySz;
- #ifdef HAVE_SNI
- if (NULL != session->sni) {
- XSTRNCPY((char*)sslInfo->serverNameIndication,
- session->sni, sizeof(sslInfo->serverNameIndication));
- sslInfo->serverNameIndication
- [sizeof(sslInfo->serverNameIndication) - 1] = '\0';
- }
- #endif
- TraceSessionInfo(sslInfo);
- }
- }
-}
-
-
-/* Call the session connection start callback. */
-static void CallConnectionCb(SnifferSession* session)
-{
- if (ConnectionCb != NULL) {
- SSLInfo info;
- CopySessionInfo(session, &info);
- ConnectionCb((const void*)session, &info, ConnectionCbCtx);
- }
-}
-
-
-/* Process Client Key Exchange, RSA or static ECDH */
-static int ProcessClientKeyExchange(const byte* input, int* sslBytes,
- SnifferSession* session, char* error)
-{
- word32 idx = 0;
- int tryEcc = 0;
- int ret;
-
- if (session->sslServer->buffers.key == NULL ||
- session->sslServer->buffers.key->buffer == NULL ||
- session->sslServer->buffers.key->length == 0) {
-
- SetError(RSA_KEY_MISSING_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- {
- RsaKey key;
- int length;
-
- ret = wc_InitRsaKey(&key, 0);
- if (ret == 0) {
- ret = wc_RsaPrivateKeyDecode(
- session->sslServer->buffers.key->buffer,
- &idx, &key, session->sslServer->buffers.key->length);
- if (ret != 0) {
- tryEcc = 1;
- #ifndef HAVE_ECC
- SetError(RSA_DECODE_STR, error, session, FATAL_ERROR_STATE);
- #else
- /* If we can do ECC, this isn't fatal. Not loading an ECC
- * key will be fatal, though. */
- SetError(RSA_DECODE_STR, error, session, 0);
- #endif
- }
- }
-
- if (ret == 0) {
- length = wc_RsaEncryptSize(&key);
- if (IsTLS(session->sslServer)) {
- input += 2; /* tls pre length */
- }
-
- if (length > *sslBytes) {
- SetError(PARTIAL_INPUT_STR, error, session, FATAL_ERROR_STATE);
- ret = -1;
- }
- }
-
- #ifdef WC_RSA_BLINDING
- if (ret == 0) {
- ret = wc_RsaSetRNG(&key, session->sslServer->rng);
- if (ret != 0) {
- SetError(RSA_DECRYPT_STR, error, session, FATAL_ERROR_STATE);
- }
- }
- #endif
-
- if (ret == 0) {
- session->keySz = length * WOLFSSL_BIT_SIZE;
- /* length is the key size in bytes */
- session->sslServer->arrays->preMasterSz = SECRET_LEN;
-
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- ret = wc_AsyncWait(ret, &key.asyncDev,
- WC_ASYNC_FLAG_CALL_AGAIN);
- #endif
- if (ret >= 0) {
- ret = wc_RsaPrivateDecrypt(input, length,
- session->sslServer->arrays->preMasterSecret,
- session->sslServer->arrays->preMasterSz, &key);
- }
- } while (ret == WC_PENDING_E);
-
- if (ret != SECRET_LEN) {
- SetError(RSA_DECRYPT_STR, error, session, FATAL_ERROR_STATE);
- }
- }
-
- wc_FreeRsaKey(&key);
- }
-
- if (tryEcc) {
-#ifdef HAVE_ECC
- ecc_key key;
- ecc_key pubKey;
- int length, keyInit = 0, pubKeyInit = 0;
-
- idx = 0;
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- keyInit = 1;
- ret = wc_ecc_init(&pubKey);
- }
- if (ret == 0) {
- pubKeyInit = 1;
- ret = wc_EccPrivateKeyDecode(
- session->sslServer->buffers.key->buffer,
- &idx, &key, session->sslServer->buffers.key->length);
- if (ret != 0) {
- SetError(ECC_DECODE_STR, error, session, FATAL_ERROR_STATE);
- }
- }
-
- if (ret == 0) {
- length = wc_ecc_size(&key) * 2 + 1;
- /* The length should be 2 times the key size (x and y), plus 1
- * for the type byte. */
- if (IsTLS(session->sslServer)) {
- input += 1; /* Don't include the TLS length for the key. */
- }
-
- if (length + 1 > *sslBytes) {
- SetError(PARTIAL_INPUT_STR,
- error, session, FATAL_ERROR_STATE);
- ret = -1;
- }
- }
-
- if (ret == 0) {
- ret = wc_ecc_import_x963_ex(input, length, &pubKey, ECC_CURVE_DEF);
- if (ret != 0) {
- SetError(ECC_PUB_DECODE_STR, error, session, FATAL_ERROR_STATE);
- }
- }
-
- if (ret == 0) {
- session->keySz = ((length - 1) / 2) * WOLFSSL_BIT_SIZE;
- /* Length is in bytes. Subtract 1 for the ECC key type. Divide
- * by two as the key is in (x,y) coordinates, where x and y are
- * the same size, the key size. Convert from bytes to bits. */
- session->sslServer->arrays->preMasterSz = ENCRYPT_LEN;
-
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- ret = wc_AsyncWait(ret, &key.asyncDev,
- WC_ASYNC_FLAG_CALL_AGAIN);
- #endif
- if (ret >= 0) {
- ret = wc_ecc_shared_secret(&key, &pubKey,
- session->sslServer->arrays->preMasterSecret,
- &session->sslServer->arrays->preMasterSz);
- }
- } while (ret == WC_PENDING_E);
- }
-
-#ifdef WOLFSSL_SNIFFER_STATS
- if (ret != 0)
- INC_STAT(SnifferStats.sslKeyFails);
-#endif
-
- if (keyInit)
- wc_ecc_free(&key);
- if (pubKeyInit)
- wc_ecc_free(&pubKey);
-#endif
- }
-
- /* store for client side as well */
- XMEMCPY(session->sslClient->arrays->preMasterSecret,
- session->sslServer->arrays->preMasterSecret,
- session->sslServer->arrays->preMasterSz);
- session->sslClient->arrays->preMasterSz =
- session->sslServer->arrays->preMasterSz;
-
- #ifdef SHOW_SECRETS
- {
- word32 i;
- printf("pre master secret: ");
- for (i = 0; i < session->sslServer->arrays->preMasterSz; i++)
- printf("%02x", session->sslServer->arrays->preMasterSecret[i]);
- printf("\n");
- }
- #endif
-
- if (SetCipherSpecs(session->sslServer) != 0) {
- SetError(BAD_CIPHER_SPEC_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- if (SetCipherSpecs(session->sslClient) != 0) {
- SetError(BAD_CIPHER_SPEC_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- ret = MakeMasterSecret(session->sslServer);
- ret += MakeMasterSecret(session->sslClient);
- ret += SetKeysSide(session->sslServer, ENCRYPT_AND_DECRYPT_SIDE);
- ret += SetKeysSide(session->sslClient, ENCRYPT_AND_DECRYPT_SIDE);
-
- if (ret != 0) {
- SetError(BAD_DERIVE_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
-#ifdef SHOW_SECRETS
- {
- int i;
- printf("server master secret: ");
- for (i = 0; i < SECRET_LEN; i++)
- printf("%02x", session->sslServer->arrays->masterSecret[i]);
- printf("\n");
-
- printf("client master secret: ");
- for (i = 0; i < SECRET_LEN; i++)
- printf("%02x", session->sslClient->arrays->masterSecret[i]);
- printf("\n");
-
- printf("server suite = %d\n", session->sslServer->options.cipherSuite);
- printf("client suite = %d\n", session->sslClient->options.cipherSuite);
- }
-#endif
-
- CallConnectionCb(session);
-
- return ret;
-}
-
-
-/* Process Session Ticket */
-static int ProcessSessionTicket(const byte* input, int* sslBytes,
- SnifferSession* session, char* error)
-{
- word16 len;
-
- /* make sure can read through hint and len */
- if (TICKET_HINT_LEN + LENGTH_SZ > *sslBytes) {
- SetError(BAD_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- input += TICKET_HINT_LEN; /* skip over hint */
- *sslBytes -= TICKET_HINT_LEN;
-
- len = (word16)((input[0] << 8) | input[1]);
- input += LENGTH_SZ;
- *sslBytes -= LENGTH_SZ;
-
- /* make sure can read through ticket */
- if (len > *sslBytes || len < ID_LEN) {
- SetError(BAD_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- /* store session with macID as sessionID */
- session->sslServer->options.haveSessionId = 1;
- XMEMCPY(session->sslServer->arrays->sessionID, input + len - ID_LEN,ID_LEN);
-
- return 0;
-}
-
-
-/* Process Server Hello */
-static int ProcessServerHello(int msgSz, const byte* input, int* sslBytes,
- SnifferSession* session, char* error)
-{
- ProtocolVersion pv;
- byte b, b0;
- int toRead = VERSION_SZ + RAN_LEN + ENUM_LEN;
- int doResume = 0;
- int initialBytes = *sslBytes;
-
- (void)msgSz;
- (void)initialBytes;
-
- /* make sure we didn't miss ClientHello */
- if (session->flags.clientHello == 0) {
- SetError(MISSED_CLIENT_HELLO_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- /* make sure can read through session len */
- if (toRead > *sslBytes) {
- SetError(SERVER_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- XMEMCPY(&pv, input, VERSION_SZ);
- input += VERSION_SZ;
- *sslBytes -= VERSION_SZ;
-
- session->sslServer->version = pv;
- session->sslClient->version = pv;
-
- XMEMCPY(session->sslServer->arrays->serverRandom, input, RAN_LEN);
- XMEMCPY(session->sslClient->arrays->serverRandom, input, RAN_LEN);
- input += RAN_LEN;
- *sslBytes -= RAN_LEN;
-
- b = *input++;
- *sslBytes -= 1;
-
- /* make sure can read through compression */
- if ( (b + SUITE_LEN + ENUM_LEN) > *sslBytes) {
- SetError(SERVER_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- if (b) {
- XMEMCPY(session->sslServer->arrays->sessionID, input, ID_LEN);
- session->sslServer->options.haveSessionId = 1;
- }
- input += b;
- *sslBytes -= b;
-
- /* cipher suite */
- b0 = *input++; /* first byte, ECC or not */
- session->sslServer->options.cipherSuite0 = b0;
- session->sslClient->options.cipherSuite0 = b0;
- b = *input++;
- session->sslServer->options.cipherSuite = b;
- session->sslClient->options.cipherSuite = b;
- *sslBytes -= SUITE_LEN;
-
-#ifdef WOLFSSL_SNIFFER_STATS
- {
- const CipherSuiteInfo* suites = GetCipherNames();
- int suitesSz = GetCipherNamesSize();
- int match = 0;
-
- while (suitesSz) {
- if (b0 == suites->cipherSuite0 && b == suites->cipherSuite) {
- match = 1;
- break;
- }
- suites++;
- suitesSz--;
- }
- if (!match)
- INC_STAT(SnifferStats.sslCiphersUnsupported);
- }
-#endif /* WOLFSSL_SNIFFER_STATS */
-
- /* compression */
- b = *input++;
- *sslBytes -= ENUM_LEN;
-
- if (b) {
- SetError(BAD_COMPRESSION_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
-#ifdef HAVE_EXTENDED_MASTER
- /* extensions */
- if ((initialBytes - *sslBytes) < msgSz) {
- word16 len;
-
- /* skip extensions until extended master secret */
- /* make sure can read len */
- if (SUITE_LEN > *sslBytes) {
- SetError(SERVER_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- len = (word16)((input[0] << 8) | input[1]);
- input += SUITE_LEN;
- *sslBytes -= SUITE_LEN;
- /* make sure can read through all extensions */
- if (len > *sslBytes) {
- SetError(SERVER_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- while (len >= EXT_TYPE_SZ + LENGTH_SZ) {
- byte extType[EXT_TYPE_SZ];
- word16 extLen;
-
- extType[0] = input[0];
- extType[1] = input[1];
- input += EXT_TYPE_SZ;
- *sslBytes -= EXT_TYPE_SZ;
-
- extLen = (word16)((input[0] << 8) | input[1]);
- input += LENGTH_SZ;
- *sslBytes -= LENGTH_SZ;
-
- /* make sure can read through individual extension */
- if (extLen > *sslBytes) {
- SetError(SERVER_HELLO_INPUT_STR, error, session,
- FATAL_ERROR_STATE);
- return -1;
- }
-
- if (extType[0] == 0x00 && extType[1] == EXT_MASTER_SECRET) {
- session->flags.expectEms = 1;
- }
-
- input += extLen;
- *sslBytes -= extLen;
- len -= extLen + EXT_TYPE_SZ + LENGTH_SZ;
- }
- }
-
- if (!session->flags.expectEms) {
- XFREE(session->hash, NULL, DYNAMIC_TYPE_HASHES);
- session->hash = NULL;
- }
-#endif
-
- if (session->sslServer->options.haveSessionId) {
- if (XMEMCMP(session->sslServer->arrays->sessionID,
- session->sslClient->arrays->sessionID, ID_LEN) == 0)
- doResume = 1;
- }
- else if (session->sslClient->options.haveSessionId == 0 &&
- session->sslServer->options.haveSessionId == 0 &&
- session->ticketID)
- doResume = 1;
-
- if (session->ticketID && doResume) {
- /* use ticketID to retrieve from session, prefer over sessionID */
- XMEMCPY(session->sslServer->arrays->sessionID,session->ticketID,ID_LEN);
- session->sslServer->options.haveSessionId = 1; /* may not have
- actual sessionID */
- }
-
- if (doResume ) {
- int ret = 0;
- SSL_SESSION* resume = GetSession(session->sslServer,
- session->sslServer->arrays->masterSecret, 0);
- if (resume == NULL) {
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslResumeMisses);
-#endif
- SetError(BAD_SESSION_RESUME_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- /* make sure client has master secret too */
- XMEMCPY(session->sslClient->arrays->masterSecret,
- session->sslServer->arrays->masterSecret, SECRET_LEN);
- session->flags.resuming = 1;
-
- Trace(SERVER_DID_RESUMPTION_STR);
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslResumedConns);
- INC_STAT(SnifferStats.sslResumptionValid);
-#endif
- if (SetCipherSpecs(session->sslServer) != 0) {
- SetError(BAD_CIPHER_SPEC_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- if (SetCipherSpecs(session->sslClient) != 0) {
- SetError(BAD_CIPHER_SPEC_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- if (session->sslServer->options.tls) {
- ret = DeriveTlsKeys(session->sslServer);
- ret += DeriveTlsKeys(session->sslClient);
- }
- else {
- ret = DeriveKeys(session->sslServer);
- ret += DeriveKeys(session->sslClient);
- }
- ret += SetKeysSide(session->sslServer, ENCRYPT_AND_DECRYPT_SIDE);
- ret += SetKeysSide(session->sslClient, ENCRYPT_AND_DECRYPT_SIDE);
-
- if (ret != 0) {
- SetError(BAD_DERIVE_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- }
- else {
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslStandardConns);
-#endif
- }
-#ifdef SHOW_SECRETS
- {
- int i;
- printf("cipher suite = 0x%02x\n",
- session->sslServer->options.cipherSuite);
- printf("server random: ");
- for (i = 0; i < RAN_LEN; i++)
- printf("%02x", session->sslServer->arrays->serverRandom[i]);
- printf("\n");
- }
-#endif
- return 0;
-}
-
-
-/* Process normal Client Hello */
-static int ProcessClientHello(const byte* input, int* sslBytes,
- SnifferSession* session, char* error)
-{
- byte bLen;
- word16 len;
- int toRead = VERSION_SZ + RAN_LEN + ENUM_LEN;
-
-#ifdef HAVE_SNI
- {
- byte name[MAX_SERVER_NAME];
- word32 nameSz = sizeof(name);
- int ret;
-
- ret = wolfSSL_SNI_GetFromBuffer(
- input - HANDSHAKE_HEADER_SZ - RECORD_HEADER_SZ,
- *sslBytes + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ,
- WOLFSSL_SNI_HOST_NAME, name, &nameSz);
-
- if (ret == WOLFSSL_SUCCESS) {
- NamedKey* namedKey;
-
- if (nameSz > sizeof(name) - 1)
- nameSz = sizeof(name) - 1;
- name[nameSz] = 0;
- wc_LockMutex(&session->context->namedKeysMutex);
- namedKey = session->context->namedKeys;
- while (namedKey != NULL) {
- if (nameSz == namedKey->nameSz &&
- XSTRNCMP((char*)name, namedKey->name, nameSz) == 0) {
- if (wolfSSL_use_PrivateKey_buffer(session->sslServer,
- namedKey->key, namedKey->keySz,
- WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS) {
- wc_UnLockMutex(&session->context->namedKeysMutex);
- SetError(CLIENT_HELLO_LATE_KEY_STR, error, session,
- FATAL_ERROR_STATE);
- return -1;
- }
- session->sni = namedKey->name;
- break;
- }
- else
- namedKey = namedKey->next;
- }
- wc_UnLockMutex(&session->context->namedKeysMutex);
- }
- }
-#endif
-
- session->flags.clientHello = 1; /* don't process again */
-
- /* make sure can read up to session len */
- if (toRead > *sslBytes) {
- SetError(CLIENT_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- /* skip, get negotiated one from server hello */
- input += VERSION_SZ;
- *sslBytes -= VERSION_SZ;
-
- XMEMCPY(session->sslServer->arrays->clientRandom, input, RAN_LEN);
- XMEMCPY(session->sslClient->arrays->clientRandom, input, RAN_LEN);
-
- input += RAN_LEN;
- *sslBytes -= RAN_LEN;
-
- /* store session in case trying to resume */
- bLen = *input++;
- *sslBytes -= ENUM_LEN;
- if (bLen) {
- if (ID_LEN > *sslBytes) {
- SetError(CLIENT_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- Trace(CLIENT_RESUME_TRY_STR);
- XMEMCPY(session->sslClient->arrays->sessionID, input, ID_LEN);
- session->sslClient->options.haveSessionId = 1;
- }
-#ifdef SHOW_SECRETS
- {
- int i;
- printf("client random: ");
- for (i = 0; i < RAN_LEN; i++)
- printf("%02x", session->sslServer->arrays->clientRandom[i]);
- printf("\n");
- }
-#endif
-
- input += bLen;
- *sslBytes -= bLen;
-
- /* skip cipher suites */
- /* make sure can read len */
- if (SUITE_LEN > *sslBytes) {
- SetError(CLIENT_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- len = (word16)((input[0] << 8) | input[1]);
- input += SUITE_LEN;
- *sslBytes -= SUITE_LEN;
- /* make sure can read suites + comp len */
- if (len + ENUM_LEN > *sslBytes) {
- SetError(CLIENT_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- input += len;
- *sslBytes -= len;
-
- /* skip compression */
- bLen = *input++;
- *sslBytes -= ENUM_LEN;
- /* make sure can read len */
- if (bLen > *sslBytes) {
- SetError(CLIENT_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- input += bLen;
- *sslBytes -= bLen;
-
- if (*sslBytes == 0) {
- /* no extensions */
- return 0;
- }
-
- /* skip extensions until session ticket */
- /* make sure can read len */
- if (SUITE_LEN > *sslBytes) {
- SetError(CLIENT_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- len = (word16)((input[0] << 8) | input[1]);
- input += SUITE_LEN;
- *sslBytes -= SUITE_LEN;
- /* make sure can read through all extensions */
- if (len > *sslBytes) {
- SetError(CLIENT_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- while (len >= EXT_TYPE_SZ + LENGTH_SZ) {
- byte extType[EXT_TYPE_SZ];
- word16 extLen;
-
- extType[0] = input[0];
- extType[1] = input[1];
- input += EXT_TYPE_SZ;
- *sslBytes -= EXT_TYPE_SZ;
-
- extLen = (word16)((input[0] << 8) | input[1]);
- input += LENGTH_SZ;
- *sslBytes -= LENGTH_SZ;
-
- /* make sure can read through individual extension */
- if (extLen > *sslBytes) {
- SetError(CLIENT_HELLO_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- if (extType[0] == 0x00 && extType[1] == TICKET_EXT_ID) {
-
- /* make sure can read through ticket if there is a non blank one */
- if (extLen && extLen < ID_LEN) {
- SetError(CLIENT_HELLO_INPUT_STR, error, session,
- FATAL_ERROR_STATE);
- return -1;
- }
-
- if (extLen) {
- if (session->ticketID == 0) {
- session->ticketID = (byte*)XMALLOC(ID_LEN,
- NULL, DYNAMIC_TYPE_SNIFFER_TICKET_ID);
- if (session->ticketID == 0) {
- SetError(MEMORY_STR, error, session,
- FATAL_ERROR_STATE);
- return -1;
- }
- }
- XMEMCPY(session->ticketID, input + extLen - ID_LEN, ID_LEN);
- }
- }
-
- input += extLen;
- *sslBytes -= extLen;
- len -= extLen + EXT_TYPE_SZ + LENGTH_SZ;
- }
-
- return 0;
-}
-
-
-#ifdef WOLFSSL_SNIFFER_WATCH
-
-/* Process Certificate */
-static int ProcessCertificate(const byte* input, int* sslBytes,
- SnifferSession* session, char* error)
-{
- Sha256 sha;
- const byte* certChain;
- word32 certChainSz;
- word32 certSz;
- int ret;
- byte digest[SHA256_DIGEST_SIZE];
-
- /* If the receiver is the server, this is the client certificate message,
- * and it should be ignored at this point. */
- if (session->flags.side == WOLFSSL_SERVER_END)
- return 0;
-
- if (WatchCb == NULL) {
- SetError(WATCH_CB_MISSING_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- if (*sslBytes < CERT_HEADER_SZ) {
- SetError(BAD_CERT_MSG_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- ato24(input, &certChainSz);
- *sslBytes -= CERT_HEADER_SZ;
- input += CERT_HEADER_SZ;
-
- if (*sslBytes < (int)certChainSz) {
- SetError(BAD_CERT_MSG_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- certChain = input;
-
- ato24(input, &certSz);
- input += OPAQUE24_LEN;
- if (*sslBytes < (int)certSz) {
- SetError(BAD_CERT_MSG_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- *sslBytes -= certChainSz;
-
- ret = wc_InitSha256(&sha);
- if (ret == 0)
- ret = wc_Sha256Update(&sha, input, certSz);
- if (ret == 0)
- ret = wc_Sha256Final(&sha, digest);
- if (ret != 0) {
- SetError(WATCH_HASH_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- ret = WatchCb((void*)session, digest, sizeof(digest),
- certChain, certChainSz, WatchCbCtx, error);
- if (ret != 0) {
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslKeysUnmatched);
-#endif
- SetError(WATCH_FAIL_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- else {
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslKeyMatches);
-#endif
- }
-
- return 0;
-}
-
-#endif
-
-
-/* Process Finished */
-static int ProcessFinished(const byte* input, int size, int* sslBytes,
- SnifferSession* session, char* error)
-{
- SSL* ssl;
- word32 inOutIdx = 0;
- int ret;
-
- if (session->flags.side == WOLFSSL_SERVER_END)
- ssl = session->sslServer;
- else
- ssl = session->sslClient;
-
- ret = DoFinished(ssl, input, &inOutIdx, (word32) size, (word32) *sslBytes,
- SNIFF);
- *sslBytes -= (int)inOutIdx;
-
- if (ret < 0) {
- SetError(BAD_FINISHED_MSG, error, session, FATAL_ERROR_STATE);
- return ret;
- }
-
- if (ret == 0 && session->flags.cached == 0) {
- if (session->sslServer->options.haveSessionId) {
- WOLFSSL_SESSION* sess = GetSession(session->sslServer, NULL, 0);
- if (sess == NULL) {
- AddSession(session->sslServer); /* don't re add */
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslResumptionInserts);
-#endif
- }
- session->flags.cached = 1;
- }
- }
-
- /* If receiving a finished message from one side, free the resources
- * from the other side's tracker. */
- if (session->flags.side == WOLFSSL_SERVER_END)
- FreeHandshakeResources(session->sslClient);
- else
- FreeHandshakeResources(session->sslServer);
-
- return ret;
-}
-
-
-/* Process HandShake input */
-static int DoHandShake(const byte* input, int* sslBytes,
- SnifferSession* session, char* error)
-{
- byte type;
- int size;
- int ret = 0;
- int startBytes;
-
- if (*sslBytes < HANDSHAKE_HEADER_SZ) {
- SetError(HANDSHAKE_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- type = input[0];
- size = (input[1] << 16) | (input[2] << 8) | input[3];
-
- input += HANDSHAKE_HEADER_SZ;
- *sslBytes -= HANDSHAKE_HEADER_SZ;
- startBytes = *sslBytes;
-
- if (*sslBytes < size) {
- Trace(SPLIT_HANDSHAKE_MSG_STR);
- *sslBytes = 0;
- return ret;
- }
-
- /* A session's arrays are released when the handshake is completed. */
- if (session->sslServer->arrays == NULL &&
- session->sslClient->arrays == NULL) {
-
- SetError(NO_SECURE_RENEGOTIATION, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
-#ifdef HAVE_EXTENDED_MASTER
- if (session->hash) {
- if (HashUpdate(session->hash, input, size) != 0) {
- SetError(EXTENDED_MASTER_HASH_STR, error,
- session, FATAL_ERROR_STATE);
- return -1;
- }
- }
-#endif
-
- switch (type) {
- case hello_verify_request:
- Trace(GOT_HELLO_VERIFY_STR);
- break;
- case hello_request:
- Trace(GOT_HELLO_REQUEST_STR);
- break;
- case session_ticket:
- Trace(GOT_SESSION_TICKET_STR);
- ret = ProcessSessionTicket(input, sslBytes, session, error);
- break;
- case server_hello:
- Trace(GOT_SERVER_HELLO_STR);
- ret = ProcessServerHello(size, input, sslBytes, session, error);
- break;
- case certificate_request:
- Trace(GOT_CERT_REQ_STR);
- break;
- case server_key_exchange:
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslEphemeralMisses);
-#endif
- Trace(GOT_SERVER_KEY_EX_STR);
- /* can't know temp key passively */
- SetError(BAD_CIPHER_SPEC_STR, error, session, FATAL_ERROR_STATE);
- ret = -1;
- break;
- case certificate:
- Trace(GOT_CERT_STR);
- if (session->flags.side == WOLFSSL_SERVER_END) {
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslClientAuthConns);
-#endif
- }
-#ifdef WOLFSSL_SNIFFER_WATCH
- ret = ProcessCertificate(input, sslBytes, session, error);
-#endif
- break;
- case server_hello_done:
- Trace(GOT_SERVER_HELLO_DONE_STR);
- break;
- case finished:
- Trace(GOT_FINISHED_STR);
- ret = ProcessFinished(input, size, sslBytes, session, error);
- break;
- case client_hello:
- Trace(GOT_CLIENT_HELLO_STR);
- ret = ProcessClientHello(input, sslBytes, session, error);
- break;
- case client_key_exchange:
- Trace(GOT_CLIENT_KEY_EX_STR);
-#ifdef HAVE_EXTENDED_MASTER
- if (session->flags.expectEms && session->hash != NULL) {
- if (HashCopy(session->sslServer->hsHashes,
- session->hash) == 0 &&
- HashCopy(session->sslClient->hsHashes,
- session->hash) == 0) {
-
- session->sslServer->options.haveEMS = 1;
- session->sslClient->options.haveEMS = 1;
- }
- else {
- SetError(EXTENDED_MASTER_HASH_STR, error,
- session, FATAL_ERROR_STATE);
- ret = -1;
- }
- XMEMSET(session->hash, 0, sizeof(HsHashes));
- XFREE(session->hash, NULL, DYNAMIC_TYPE_HASHES);
- session->hash = NULL;
- }
- else {
- session->sslServer->options.haveEMS = 0;
- session->sslClient->options.haveEMS = 0;
- }
-#endif
- if (ret == 0)
- ret = ProcessClientKeyExchange(input, sslBytes, session, error);
- break;
- case certificate_verify:
- Trace(GOT_CERT_VER_STR);
- break;
- case certificate_status:
- Trace(GOT_CERT_STATUS_STR);
- break;
- default:
- SetError(GOT_UNKNOWN_HANDSHAKE_STR, error, session, 0);
- return -1;
- }
-
- *sslBytes = startBytes - size; /* actual bytes of full process */
-
- return ret;
-}
-
-
-/* Decrypt input into plain output, 0 on success */
-static int Decrypt(SSL* ssl, byte* output, const byte* input, word32 sz)
-{
- int ret = 0;
-
- (void)output;
- (void)input;
- (void)sz;
-
- switch (ssl->specs.bulk_cipher_algorithm) {
- #ifdef BUILD_ARC4
- case wolfssl_rc4:
- wc_Arc4Process(ssl->decrypt.arc4, output, input, sz);
- break;
- #endif
-
- #ifdef BUILD_DES3
- case wolfssl_triple_des:
- ret = wc_Des3_CbcDecrypt(ssl->decrypt.des3, output, input, sz);
- break;
- #endif
-
- #ifdef BUILD_AES
- case wolfssl_aes:
- ret = wc_AesCbcDecrypt(ssl->decrypt.aes, output, input, sz);
- break;
- #endif
-
- #ifdef HAVE_HC128
- case wolfssl_hc128:
- wc_Hc128_Process(ssl->decrypt.hc128, output, input, sz);
- break;
- #endif
-
- #ifdef BUILD_RABBIT
- case wolfssl_rabbit:
- wc_RabbitProcess(ssl->decrypt.rabbit, output, input, sz);
- break;
- #endif
-
- #ifdef HAVE_CAMELLIA
- case wolfssl_camellia:
- wc_CamelliaCbcDecrypt(ssl->decrypt.cam, output, input, sz);
- break;
- #endif
-
- #ifdef HAVE_IDEA
- case wolfssl_idea:
- wc_IdeaCbcDecrypt(ssl->decrypt.idea, output, input, sz);
- break;
- #endif
-
- #ifdef HAVE_AESGCM
- case wolfssl_aes_gcm:
- if (sz >= (word32)(AESGCM_EXP_IV_SZ + ssl->specs.aead_mac_size))
- {
- /* scratch buffer, sniffer ignores auth tag*/
- byte authTag[WOLFSSL_MIN_AUTH_TAG_SZ];
-
- byte nonce[AESGCM_NONCE_SZ];
- XMEMCPY(nonce, ssl->keys.aead_dec_imp_IV, AESGCM_IMP_IV_SZ);
- XMEMCPY(nonce + AESGCM_IMP_IV_SZ, input, AESGCM_EXP_IV_SZ);
-
- if (wc_AesGcmEncrypt(ssl->decrypt.aes,
- output,
- input + AESGCM_EXP_IV_SZ,
- sz - AESGCM_EXP_IV_SZ - ssl->specs.aead_mac_size,
- nonce, AESGCM_NONCE_SZ,
- authTag, sizeof(authTag),
- NULL, 0) < 0) {
- Trace(BAD_DECRYPT);
- ret = -1;
- }
- ForceZero(nonce, AESGCM_NONCE_SZ);
- }
- else {
- Trace(BAD_DECRYPT_SIZE);
- ret = -1;
- }
- break;
- #endif
-
- #ifdef HAVE_NULL_CIPHER
- case wolfssl_cipher_null:
- XMEMCPY(output, input, sz);
- break;
- #endif
-
- default:
- Trace(BAD_DECRYPT_TYPE);
- ret = -1;
- break;
- }
-
- return ret;
-}
-
-
-/* Decrypt input message into output, adjust output steam if needed */
-static const byte* DecryptMessage(SSL* ssl, const byte* input, word32 sz,
- byte* output, int* error, int* advance)
-{
- int ivExtra = 0;
-
- int ret = Decrypt(ssl, output, input, sz);
- if (ret != 0) {
- *error = ret;
- return NULL;
- }
- ssl->keys.encryptSz = sz;
- if (ssl->options.tls1_1 && ssl->specs.cipher_type == block) {
- output += ssl->specs.block_size; /* go past TLSv1.1 IV */
- ivExtra = ssl->specs.block_size;
- *advance = ssl->specs.block_size;
- }
-
- if (ssl->specs.cipher_type == aead) {
- *advance = ssl->specs.aead_mac_size;
- ssl->keys.padSz = ssl->specs.aead_mac_size;
- }
- else
- ssl->keys.padSz = ssl->specs.hash_size;
-
- if (ssl->specs.cipher_type == block)
- ssl->keys.padSz += *(output + sz - ivExtra - 1) + 1;
-
- return output;
-}
-
-
-/* remove session from table, use rowHint if no info (means we have a lock) */
-static void RemoveSession(SnifferSession* session, IpInfo* ipInfo,
- TcpInfo* tcpInfo, word32 rowHint)
-{
- SnifferSession* previous = 0;
- SnifferSession* current;
- word32 row = rowHint;
- int haveLock = 0;
-
- if (ipInfo && tcpInfo)
- row = SessionHash(ipInfo, tcpInfo);
- else
- haveLock = 1;
-
- assert(row <= HASH_SIZE);
- Trace(REMOVE_SESSION_STR);
-
- if (!haveLock)
- wc_LockMutex(&SessionMutex);
-
- current = SessionTable[row];
-
- while (current) {
- if (current == session) {
- if (previous)
- previous->next = current->next;
- else
- SessionTable[row] = current->next;
- FreeSnifferSession(session);
- TraceRemovedSession();
- break;
- }
- previous = current;
- current = current->next;
- }
-
- if (!haveLock)
- wc_UnLockMutex(&SessionMutex);
-}
-
-
-/* Remove stale sessions from the Session Table, have a lock */
-static void RemoveStaleSessions(void)
-{
- word32 i;
- SnifferSession* session;
-
- for (i = 0; i < HASH_SIZE; i++) {
- session = SessionTable[i];
- while (session) {
- SnifferSession* next = session->next;
- if (time(NULL) >= session->lastUsed + WOLFSSL_SNIFFER_TIMEOUT) {
- TraceStaleSession();
- RemoveSession(session, NULL, NULL, i);
- }
- session = next;
- }
- }
-}
-
-
-/* Create a new Sniffer Session */
-static SnifferSession* CreateSession(IpInfo* ipInfo, TcpInfo* tcpInfo,
- char* error)
-{
- SnifferSession* session = 0;
- int row;
-
- Trace(NEW_SESSION_STR);
- /* create a new one */
- session = (SnifferSession*)XMALLOC(sizeof(SnifferSession),
- NULL, DYNAMIC_TYPE_SNIFFER_SESSION);
- if (session == NULL) {
- SetError(MEMORY_STR, error, NULL, 0);
- return 0;
- }
- InitSession(session);
-#ifdef HAVE_EXTENDED_MASTER
- {
- HsHashes* newHash = (HsHashes*)XMALLOC(sizeof(HsHashes),
- NULL, DYNAMIC_TYPE_HASHES);
- if (newHash == NULL) {
- SetError(MEMORY_STR, error, NULL, 0);
- XFREE(session, NULL, DYNAMIC_TYPE_SNIFFER_SESSION);
- return 0;
- }
- if (HashInit(newHash) != 0) {
- SetError(EXTENDED_MASTER_HASH_STR, error, NULL, 0);
- XFREE(session, NULL, DYNAMIC_TYPE_SNIFFER_SESSION);
- return 0;
- }
- session->hash = newHash;
- }
-#endif
- session->server = ipInfo->dst;
- session->client = ipInfo->src;
- session->srvPort = (word16)tcpInfo->dstPort;
- session->cliPort = (word16)tcpInfo->srcPort;
- session->cliSeqStart = tcpInfo->sequence;
- session->cliExpected = 1; /* relative */
- session->lastUsed= time(NULL);
- session->keySz = 0;
-#ifdef HAVE_SNI
- session->sni = NULL;
-#endif
-
- session->context = GetSnifferServer(ipInfo, tcpInfo);
- if (session->context == NULL) {
- SetError(SERVER_NOT_REG_STR, error, NULL, 0);
- XFREE(session, NULL, DYNAMIC_TYPE_SNIFFER_SESSION);
- return 0;
- }
-
- session->sslServer = SSL_new(session->context->ctx);
- if (session->sslServer == NULL) {
- SetError(BAD_NEW_SSL_STR, error, session, FATAL_ERROR_STATE);
- XFREE(session, NULL, DYNAMIC_TYPE_SNIFFER_SESSION);
- return 0;
- }
- session->sslClient = SSL_new(session->context->ctx);
- if (session->sslClient == NULL) {
- SSL_free(session->sslServer);
- session->sslServer = 0;
-
- SetError(BAD_NEW_SSL_STR, error, session, FATAL_ERROR_STATE);
- XFREE(session, NULL, DYNAMIC_TYPE_SNIFFER_SESSION);
- return 0;
- }
- /* put server back into server mode */
- session->sslServer->options.side = WOLFSSL_SERVER_END;
-
- row = SessionHash(ipInfo, tcpInfo);
-
- /* add it to the session table */
- wc_LockMutex(&SessionMutex);
-
- session->next = SessionTable[row];
- SessionTable[row] = session;
-
- SessionCount++;
-
- if ( (SessionCount % HASH_SIZE) == 0) {
- TraceFindingStale();
- RemoveStaleSessions();
- }
-
- wc_UnLockMutex(&SessionMutex);
-
- /* CreateSession is called in response to a SYN packet, we know this
- * is headed to the server. Also we know the server is one we care
- * about as we've passed the GetSnifferServer() successfully. */
- session->flags.side = WOLFSSL_SERVER_END;
-
- return session;
-}
-
-
-#ifdef OLD_HELLO_ALLOWED
-
-/* Process Old Client Hello Input */
-static int DoOldHello(SnifferSession* session, const byte* sslFrame,
- int* rhSize, int* sslBytes, char* error)
-{
- const byte* input = sslFrame;
- byte b0, b1;
- word32 idx = 0;
- int ret;
-
- Trace(GOT_OLD_CLIENT_HELLO_STR);
- session->flags.clientHello = 1; /* don't process again */
- b0 = *input++;
- b1 = *input++;
- *sslBytes -= 2;
- *rhSize = ((b0 & 0x7f) << 8) | b1;
-
- if (*rhSize > *sslBytes) {
- SetError(OLD_CLIENT_INPUT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- ret = ProcessOldClientHello(session->sslServer, input, &idx, *sslBytes,
- (word16)*rhSize);
- if (ret < 0 && ret != MATCH_SUITE_ERROR) {
- SetError(BAD_OLD_CLIENT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- Trace(OLD_CLIENT_OK_STR);
- XMEMCPY(session->sslClient->arrays->clientRandom,
- session->sslServer->arrays->clientRandom, RAN_LEN);
-
- *sslBytes -= *rhSize;
- return 0;
-}
-
-#endif /* OLD_HELLO_ALLOWED */
-
-
-#if 0
-/* Calculate the TCP checksum, see RFC 1071 */
-/* return 0 for success, -1 on error */
-/* can be called from decode() with
- TcpChecksum(&ipInfo, &tcpInfo, sslBytes, packet + ipInfo.length);
- could also add a 64bit version if type available and using this
-*/
-int TcpChecksum(IpInfo* ipInfo, TcpInfo* tcpInfo, int dataLen,
- const byte* packet)
-{
- TcpPseudoHdr pseudo;
- int count = PSEUDO_HDR_SZ;
- const word16* data = (word16*)&pseudo;
- word32 sum = 0;
- word16 checksum;
-
- pseudo.src = ipInfo->src;
- pseudo.dst = ipInfo->dst;
- pseudo.rsv = 0;
- pseudo.protocol = TCP_PROTO;
- pseudo.length = htons(tcpInfo->length + dataLen);
-
- /* pseudo header sum */
- while (count >= 2) {
- sum += *data++;
- count -= 2;
- }
-
- count = tcpInfo->length + dataLen;
- data = (word16*)packet;
-
- /* main sum */
- while (count > 1) {
- sum += *data++;
- count -=2;
- }
-
- /* get left-over, if any */
- packet = (byte*)data;
- if (count > 0) {
- sum += *packet;
- }
-
- /* fold 32bit sum into 16 bits */
- while (sum >> 16)
- sum = (sum & 0xffff) + (sum >> 16);
-
- checksum = (word16)~sum;
- /* checksum should now equal 0, since included already calcd checksum */
- /* field, but tcp checksum offloading could negate calculation */
- if (checksum == 0)
- return 0;
- return -1;
-}
-#endif
-
-
-/* Check IP and TCP headers, set payload */
-/* returns 0 on success, -1 on error */
-static int CheckHeaders(IpInfo* ipInfo, TcpInfo* tcpInfo, const byte* packet,
- int length, const byte** sslFrame, int* sslBytes, char* error)
-{
- TraceHeader();
- TracePacket();
-
- /* ip header */
- if (length < IP_HDR_SZ) {
- SetError(PACKET_HDR_SHORT_STR, error, NULL, 0);
- return -1;
- }
- if (CheckIpHdr((IpHdr*)packet, ipInfo, length, error) != 0)
- return -1;
-
- /* tcp header */
- if (length < (ipInfo->length + TCP_HDR_SZ)) {
- SetError(PACKET_HDR_SHORT_STR, error, NULL, 0);
- return -1;
- }
- if (CheckTcpHdr((TcpHdr*)(packet + ipInfo->length), tcpInfo, error) != 0)
- return -1;
-
- /* setup */
- *sslFrame = packet + ipInfo->length + tcpInfo->length;
- if (*sslFrame > packet + length) {
- SetError(PACKET_HDR_SHORT_STR, error, NULL, 0);
- return -1;
- }
- /* We only care about the data in the TCP/IP record. There may be extra
- * data after the IP record for the FCS for Ethernet. */
- *sslBytes = (int)(packet + ipInfo->total - *sslFrame);
-
- return 0;
-}
-
-
-/* Create or Find existing session */
-/* returns 0 on success (continue), -1 on error, 1 on success (end) */
-static int CheckSession(IpInfo* ipInfo, TcpInfo* tcpInfo, int sslBytes,
- SnifferSession** session, char* error)
-{
- /* create a new SnifferSession on client SYN */
- if (tcpInfo->syn && !tcpInfo->ack) {
- TraceClientSyn(tcpInfo->sequence);
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslEncryptedConns);
-#endif
- *session = CreateSession(ipInfo, tcpInfo, error);
- if (*session == NULL) {
- *session = GetSnifferSession(ipInfo, tcpInfo);
- /* already had existing, so OK */
- if (*session)
- return 1;
-
- SetError(MEMORY_STR, error, NULL, 0);
- return -1;
- }
- return 1;
- }
- /* get existing sniffer session */
- else {
- *session = GetSnifferSession(ipInfo, tcpInfo);
- if (*session == NULL) {
- /* don't worry about extraneous RST or duplicate FINs */
- if (tcpInfo->fin || tcpInfo->rst)
- return 1;
- /* don't worry about duplicate ACKs either */
- if (sslBytes == 0 && tcpInfo->ack)
- return 1;
-
-#ifdef WOLFSSL_SNIFFER_STATS
- LOCK_STAT();
- NOLOCK_INC_STAT(SnifferStats.sslDecryptedPackets);
- NOLOCK_ADD_TO_STAT(SnifferStats.sslDecryptedBytes, sslBytes);
- UNLOCK_STAT();
-#endif
-
- SetError(BAD_SESSION_STR, error, NULL, 0);
- return -1;
- }
- }
- return 0;
-}
-
-
-/* Create a Packet Buffer from *begin - end, adjust new *begin and bytesLeft */
-static PacketBuffer* CreateBuffer(word32* begin, word32 end, const byte* data,
- int* bytesLeft)
-{
- PacketBuffer* pb;
-
- int added = end - *begin + 1;
- assert(*begin <= end);
-
- pb = (PacketBuffer*)XMALLOC(sizeof(PacketBuffer),
- NULL, DYNAMIC_TYPE_SNIFFER_PB);
- if (pb == NULL) return NULL;
-
- pb->next = 0;
- pb->begin = *begin;
- pb->end = end;
- pb->data = (byte*)XMALLOC(added, NULL, DYNAMIC_TYPE_SNIFFER_PB_BUFFER);
-
- if (pb->data == NULL) {
- XFREE(pb, NULL, DYNAMIC_TYPE_SNIFFER_PB);
- return NULL;
- }
- XMEMCPY(pb->data, data, added);
-
- *bytesLeft -= added;
- *begin = pb->end + 1;
-
- return pb;
-}
-
-
-/* Add sslFrame to Reassembly List */
-/* returns 1 (end) on success, -1, on error */
-static int AddToReassembly(byte from, word32 seq, const byte* sslFrame,
- int sslBytes, SnifferSession* session, char* error)
-{
- PacketBuffer* add;
- PacketBuffer** front = (from == WOLFSSL_SERVER_END) ?
- &session->cliReassemblyList: &session->srvReassemblyList;
- PacketBuffer* curr = *front;
- PacketBuffer* prev = curr;
-
- word32* reassemblyMemory = (from == WOLFSSL_SERVER_END) ?
- &session->cliReassemblyMemory : &session->srvReassemblyMemory;
- word32 startSeq = seq;
- word32 added;
- int bytesLeft = sslBytes; /* could be overlapping fragment */
-
- /* if list is empty add full frame to front */
- if (!curr) {
- if (MaxRecoveryMemory != -1 &&
- (int)(*reassemblyMemory + sslBytes) > MaxRecoveryMemory) {
- SetError(REASSEMBLY_MAX_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- add = CreateBuffer(&seq, seq + sslBytes - 1, sslFrame, &bytesLeft);
- if (add == NULL) {
- SetError(MEMORY_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- *front = add;
- *reassemblyMemory += sslBytes;
- return 1;
- }
-
- /* add to front if before current front, up to next->begin */
- if (seq < curr->begin) {
- word32 end = seq + sslBytes - 1;
-
- if (end >= curr->begin)
- end = curr->begin - 1;
-
- if (MaxRecoveryMemory -1 &&
- (int)(*reassemblyMemory + sslBytes) > MaxRecoveryMemory) {
- SetError(REASSEMBLY_MAX_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- add = CreateBuffer(&seq, end, sslFrame, &bytesLeft);
- if (add == NULL) {
- SetError(MEMORY_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- add->next = curr;
- *front = add;
- *reassemblyMemory += sslBytes;
- }
-
- /* while we have bytes left, try to find a gap to fill */
- while (bytesLeft > 0) {
- /* get previous packet in list */
- while (curr && (seq >= curr->begin)) {
- prev = curr;
- curr = curr->next;
- }
-
- /* don't add duplicate data */
- if (prev->end >= seq) {
- if ( (seq + bytesLeft - 1) <= prev->end)
- return 1;
- seq = prev->end + 1;
- bytesLeft = startSeq + sslBytes - seq;
- }
-
- if (!curr)
- /* we're at the end */
- added = bytesLeft;
- else
- /* we're in between two frames */
- added = min((word32)bytesLeft, curr->begin - seq);
-
- /* data already there */
- if (added == 0)
- continue;
-
- if (MaxRecoveryMemory != -1 &&
- (int)(*reassemblyMemory + added) > MaxRecoveryMemory) {
- SetError(REASSEMBLY_MAX_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- add = CreateBuffer(&seq, seq + added - 1, &sslFrame[seq - startSeq],
- &bytesLeft);
- if (add == NULL) {
- SetError(MEMORY_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- add->next = prev->next;
- prev->next = add;
- *reassemblyMemory += added;
- }
- return 1;
-}
-
-
-/* Add out of order FIN capture */
-/* returns 1 for success (end) */
-static int AddFinCapture(SnifferSession* session, word32 sequence)
-{
- if (session->flags.side == WOLFSSL_SERVER_END) {
- if (session->finCaputre.cliCounted == 0)
- session->finCaputre.cliFinSeq = sequence;
- }
- else {
- if (session->finCaputre.srvCounted == 0)
- session->finCaputre.srvFinSeq = sequence;
- }
- return 1;
-}
-
-
-/* Adjust incoming sequence based on side */
-/* returns 0 on success (continue), -1 on error, 1 on success (end) */
-static int AdjustSequence(TcpInfo* tcpInfo, SnifferSession* session,
- int* sslBytes, const byte** sslFrame, char* error)
-{
- word32 seqStart = (session->flags.side == WOLFSSL_SERVER_END) ?
- session->cliSeqStart :session->srvSeqStart;
- word32 real = tcpInfo->sequence - seqStart;
- word32* expected = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->cliExpected : &session->srvExpected;
- PacketBuffer* reassemblyList = (session->flags.side == WOLFSSL_SERVER_END) ?
- session->cliReassemblyList : session->srvReassemblyList;
- byte skipPartial = (session->flags.side == WOLFSSL_SERVER_END) ?
- session->flags.srvSkipPartial :
- session->flags.cliSkipPartial;
-
- /* handle rollover of sequence */
- if (tcpInfo->sequence < seqStart)
- real = 0xffffffffU - seqStart + tcpInfo->sequence;
-
- TraceRelativeSequence(*expected, real);
-
- if (real < *expected) {
- Trace(DUPLICATE_STR);
- if (real + *sslBytes > *expected) {
- int overlap = *expected - real;
- Trace(OVERLAP_DUPLICATE_STR);
-
- /* adjust to expected, remove duplicate */
- *sslFrame += overlap;
- *sslBytes -= overlap;
-
- /* The following conditional block is duplicated below. It is the
- * same action but for a different setup case. If changing this
- * block be sure to also update the block below. */
- if (reassemblyList) {
- word32 newEnd = *expected + *sslBytes;
-
- if (newEnd > reassemblyList->begin) {
- Trace(OVERLAP_REASSEMBLY_BEGIN_STR);
-
- /* remove bytes already on reassembly list */
- *sslBytes -= newEnd - reassemblyList->begin;
- }
- if (newEnd > reassemblyList->end) {
- Trace(OVERLAP_REASSEMBLY_END_STR);
-
- /* may be past reassembly list end (could have more on list)
- so try to add what's past the front->end */
- AddToReassembly(session->flags.side, reassemblyList->end +1,
- *sslFrame + reassemblyList->end - *expected + 1,
- newEnd - reassemblyList->end, session, error);
- }
- }
- }
- else
- return 1;
- }
- else if (real > *expected) {
- Trace(OUT_OF_ORDER_STR);
- if (*sslBytes > 0) {
- int addResult = AddToReassembly(session->flags.side, real,
- *sslFrame, *sslBytes, session, error);
- if (skipPartial) {
- *sslBytes = 0;
- return 0;
- }
- else
- return addResult;
- }
- else if (tcpInfo->fin)
- return AddFinCapture(session, real);
- }
- else if (*sslBytes > 0) {
- if (skipPartial) {
- AddToReassembly(session->flags.side, real,
- *sslFrame, *sslBytes, session, error);
- *expected += *sslBytes;
- *sslBytes = 0;
- if (tcpInfo->fin)
- *expected += 1;
- return 0;
- }
- /* The following conditional block is duplicated above. It is the
- * same action but for a different setup case. If changing this
- * block be sure to also update the block above. */
- else if (reassemblyList) {
- word32 newEnd = *expected + *sslBytes;
-
- if (newEnd > reassemblyList->begin) {
- Trace(OVERLAP_REASSEMBLY_BEGIN_STR);
-
- /* remove bytes already on reassembly list */
- *sslBytes -= newEnd - reassemblyList->begin;
- }
- if (newEnd > reassemblyList->end) {
- Trace(OVERLAP_REASSEMBLY_END_STR);
-
- /* may be past reassembly list end (could have more on list)
- so try to add what's past the front->end */
- AddToReassembly(session->flags.side, reassemblyList->end +1,
- *sslFrame + reassemblyList->end - *expected + 1,
- newEnd - reassemblyList->end, session, error);
- }
- }
- }
- /* got expected sequence */
- *expected += *sslBytes;
- if (tcpInfo->fin)
- *expected += 1;
-
- return 0;
-}
-
-
-static int FindNextRecordInAssembly(SnifferSession* session,
- const byte** sslFrame, int* sslBytes,
- const byte** end, char* error)
-{
- PacketBuffer** front = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->cliReassemblyList :
- &session->srvReassemblyList;
- PacketBuffer* curr = *front;
- PacketBuffer* prev = NULL;
- byte* skipPartial = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->flags.srvSkipPartial :
- &session->flags.cliSkipPartial;
- word32* reassemblyMemory = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->cliReassemblyMemory :
- &session->srvReassemblyMemory;
- SSL* ssl = (session->flags.side == WOLFSSL_SERVER_END) ?
- session->sslServer :
- session->sslClient;
- ProtocolVersion pv = ssl->version;
- word32* expected = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->cliExpected :
- &session->srvExpected;
-
- while (curr != NULL) {
- *expected = curr->end + 1;
-
- if (curr->data[0] == application_data &&
- curr->data[1] == pv.major &&
- curr->data[2] == pv.minor) {
-
- if (ssl->buffers.inputBuffer.length > 0)
- Trace(DROPPING_PARTIAL_RECORD);
-
- *sslBytes = curr->end - curr->begin + 1;
- if ( (word32)*sslBytes > ssl->buffers.inputBuffer.bufferSize) {
- if (GrowInputBuffer(ssl, *sslBytes, 0) < 0) {
- SetError(MEMORY_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- }
-
- XMEMCPY(ssl->buffers.inputBuffer.buffer, curr->data, *sslBytes);
-
- *front = curr->next;
- *reassemblyMemory -= *sslBytes;
- FreePacketBuffer(curr);
-
- ssl->buffers.inputBuffer.length = *sslBytes;
- *sslFrame = ssl->buffers.inputBuffer.buffer;
- *end = *sslFrame + *sslBytes;
- *skipPartial = 0;
-
- return 0;
- }
- else if (ssl->specs.cipher_type == block) {
- if (ssl->specs.bulk_cipher_algorithm == wolfssl_aes) {
-#ifdef BUILD_AES
- wc_AesSetIV(ssl->decrypt.aes,
- curr->data + curr->end - curr->begin
- - ssl->specs.block_size + 1);
-#endif
- }
- else if (ssl->specs.bulk_cipher_algorithm == wolfssl_triple_des) {
-#ifdef BUILD_DES3
- wc_Des3_SetIV(ssl->decrypt.des3,
- curr->data + curr->end - curr->begin
- - ssl->specs.block_size + 1);
-#endif
- }
- }
-
- Trace(DROPPING_LOST_FRAG_STR);
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslDecodeFails);
-#endif
- prev = curr;
- curr = curr->next;
- *reassemblyMemory -= (prev->end - prev->begin + 1);
- FreePacketBuffer(prev);
- }
-
- *front = curr;
-
- return 0;
-}
-
-
-static int FixSequence(TcpInfo* tcpInfo, SnifferSession* session)
-{
- word32* expected = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->srvExpected : &session->cliExpected;
- PacketBuffer* list = (session->flags.side == WOLFSSL_SERVER_END) ?
- session->srvReassemblyList :
- session->cliReassemblyList;
- byte* skipPartial = (session->flags.side != WOLFSSL_SERVER_END) ?
- &session->flags.srvSkipPartial :
- &session->flags.cliSkipPartial;
-
- *skipPartial = 1;
- if (list != NULL)
- *expected = list->begin;
- else {
- word32 seqStart = (session->flags.side == WOLFSSL_SERVER_END) ?
- session->srvSeqStart : session->cliSeqStart;
- word32 real = tcpInfo->ackNumber - seqStart;
-
- *expected = real;
- }
-
- return 1;
-}
-
-
-/* Check latest ack number for missing packets
- return 0 ok, <0 on error */
-static int CheckAck(TcpInfo* tcpInfo, SnifferSession* session)
-{
- if (tcpInfo->ack) {
- word32 seqStart = (session->flags.side == WOLFSSL_SERVER_END) ?
- session->srvSeqStart :session->cliSeqStart;
- word32 real = tcpInfo->ackNumber - seqStart;
- word32 expected = (session->flags.side == WOLFSSL_SERVER_END) ?
- session->srvExpected : session->cliExpected;
-
- /* handle rollover of sequence */
- if (tcpInfo->ackNumber < seqStart)
- real = 0xffffffffU - seqStart + tcpInfo->ackNumber;
-
- TraceAck(real, expected);
-
- if (real > expected)
- return -1; /* we missed a packet, ACKing data we never saw */
- }
- return 0;
-}
-
-
-/* Check TCP Sequence status */
-/* returns 0 on success (continue), -1 on error, 1 on success (end) */
-static int CheckSequence(IpInfo* ipInfo, TcpInfo* tcpInfo,
- SnifferSession* session, int* sslBytes,
- const byte** sslFrame, char* error)
-{
- int actualLen;
- byte* ackFault = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->flags.cliAckFault :
- &session->flags.srvAckFault;
-
- /* init SEQ from server to client */
- if (tcpInfo->syn && tcpInfo->ack) {
- session->srvSeqStart = tcpInfo->sequence;
- session->srvExpected = 1;
- TraceServerSyn(tcpInfo->sequence);
- return 1;
- }
-
- /* adjust potential ethernet trailer */
- actualLen = ipInfo->total - ipInfo->length - tcpInfo->length;
- if (*sslBytes > actualLen) {
- *sslBytes = actualLen;
- }
-
- TraceSequence(tcpInfo->sequence, *sslBytes);
- if (CheckAck(tcpInfo, session) < 0) {
- if (!RecoveryEnabled) {
- UpdateMissedDataSessions();
- SetError(ACK_MISSED_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- else {
- SetError(ACK_MISSED_STR, error, session, 0);
- if (*ackFault == 0) {
- *ackFault = 1;
- UpdateMissedDataSessions();
- }
- return FixSequence(tcpInfo, session);
- }
- }
-
- if (*ackFault) {
- Trace(CLEAR_ACK_FAULT);
- *ackFault = 0;
- }
-
- return AdjustSequence(tcpInfo, session, sslBytes, sslFrame, error);
-}
-
-
-/* Check Status before record processing */
-/* returns 0 on success (continue), -1 on error, 1 on success (end) */
-static int CheckPreRecord(IpInfo* ipInfo, TcpInfo* tcpInfo,
- const byte** sslFrame, SnifferSession** session,
- int* sslBytes, const byte** end,
- void* vChain, word32 chainSz, char* error)
-{
- word32 length;
- SSL* ssl = ((*session)->flags.side == WOLFSSL_SERVER_END) ?
- (*session)->sslServer : (*session)->sslClient;
- byte skipPartial = ((*session)->flags.side == WOLFSSL_SERVER_END) ?
- (*session)->flags.srvSkipPartial :
- (*session)->flags.cliSkipPartial;
- /* remove SnifferSession on 2nd FIN or RST */
- if (tcpInfo->fin || tcpInfo->rst) {
- /* flag FIN and RST */
- if (tcpInfo->fin)
- (*session)->flags.finCount += 1;
- else if (tcpInfo->rst)
- (*session)->flags.finCount += 2;
-
- if ((*session)->flags.finCount >= 2) {
- RemoveSession(*session, ipInfo, tcpInfo, 0);
- *session = NULL;
- return 1;
- }
- }
-
- if ((*session)->flags.fatalError == FATAL_ERROR_STATE) {
- SetError(FATAL_ERROR_STR, error, NULL, 0);
- return -1;
- }
-
- if (skipPartial) {
- if (FindNextRecordInAssembly(*session,
- sslFrame, sslBytes, end, error) < 0) {
- return -1;
- }
- }
-
- if (*sslBytes == 0) {
- Trace(NO_DATA_STR);
- return 1;
- }
-
- /* if current partial data, add to end of partial */
- /* if skipping, the data is already at the end of partial */
- if ( !skipPartial && (length = ssl->buffers.inputBuffer.length) ) {
- Trace(PARTIAL_ADD_STR);
-
- if ( (*sslBytes + length) > ssl->buffers.inputBuffer.bufferSize) {
- if (GrowInputBuffer(ssl, *sslBytes, length) < 0) {
- SetError(MEMORY_STR, error, *session, FATAL_ERROR_STATE);
- return -1;
- }
- }
- if (vChain == NULL) {
- XMEMCPY(&ssl->buffers.inputBuffer.buffer[length],
- *sslFrame, *sslBytes);
- *sslBytes += length;
- ssl->buffers.inputBuffer.length = *sslBytes;
- *sslFrame = ssl->buffers.inputBuffer.buffer;
- *end = *sslFrame + *sslBytes;
- }
- }
-
- if (vChain != NULL) {
-#ifdef WOLFSSL_SNIFFER_CHAIN_INPUT
- struct iovec* chain = (struct iovec*)vChain;
- word32 i, offset, headerSz, qty, remainder;
-
- Trace(CHAIN_INPUT_STR);
- headerSz = (word32)*sslFrame - (word32)chain[0].iov_base;
- remainder = *sslBytes;
-
- if ( (*sslBytes + length) > ssl->buffers.inputBuffer.bufferSize) {
- if (GrowInputBuffer(ssl, *sslBytes, length) < 0) {
- SetError(MEMORY_STR, error, *session, FATAL_ERROR_STATE);
- return -1;
- }
- }
-
- qty = min(*sslBytes, (word32)chain[0].iov_len - headerSz);
- XMEMCPY(&ssl->buffers.inputBuffer.buffer[length],
- (byte*)chain[0].iov_base + headerSz, qty);
- offset = length;
- for (i = 1; i < chainSz; i++) {
- offset += qty;
- remainder -= qty;
-
- if (chain[i].iov_len > remainder)
- qty = remainder;
- else
- qty = (word32)chain[i].iov_len;
- XMEMCPY(ssl->buffers.inputBuffer.buffer + offset,
- chain[i].iov_base, qty);
- }
-
- *sslBytes += length;
- ssl->buffers.inputBuffer.length = *sslBytes;
- *sslFrame = ssl->buffers.inputBuffer.buffer;
- *end = *sslFrame + *sslBytes;
-#endif
- (void)chainSz;
- }
-
- if ((*session)->flags.clientHello == 0 && **sslFrame != handshake) {
- /* Sanity check the packet for an old style client hello. */
- int rhSize = (((*sslFrame)[0] & 0x7f) << 8) | ((*sslFrame)[1]);
-
- if ((rhSize <= (*sslBytes - 2)) &&
- (*sslFrame)[2] == OLD_HELLO_ID && (*sslFrame)[3] == SSLv3_MAJOR) {
-#ifdef OLD_HELLO_ALLOWED
- int ret = DoOldHello(*session, *sslFrame, &rhSize, sslBytes, error);
- if (ret < 0)
- return -1; /* error already set */
- if (*sslBytes <= 0)
- return 1;
-#endif
- }
- else {
-#ifdef STARTTLS_ALLOWED
- if (ssl->buffers.inputBuffer.dynamicFlag) {
- ssl->buffers.inputBuffer.length = 0;
- ShrinkInputBuffer(ssl, NO_FORCED_FREE);
- }
- return 1;
-#endif
- }
- }
-
- return 0;
-}
-
-
-/* See if input on the reassembly list is ready for consuming */
-/* returns 1 for TRUE, 0 for FALSE */
-static int HaveMoreInput(SnifferSession* session, const byte** sslFrame,
- int* sslBytes, const byte** end, char* error)
-{
- /* sequence and reassembly based on from, not to */
- int moreInput = 0;
- PacketBuffer** front = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->cliReassemblyList : &session->srvReassemblyList;
- word32* expected = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->cliExpected : &session->srvExpected;
- /* buffer is on receiving end */
- word32* length = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->sslServer->buffers.inputBuffer.length :
- &session->sslClient->buffers.inputBuffer.length;
- byte** myBuffer = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->sslServer->buffers.inputBuffer.buffer :
- &session->sslClient->buffers.inputBuffer.buffer;
- word32* bufferSize = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->sslServer->buffers.inputBuffer.bufferSize :
- &session->sslClient->buffers.inputBuffer.bufferSize;
- SSL* ssl = (session->flags.side == WOLFSSL_SERVER_END) ?
- session->sslServer : session->sslClient;
- word32* reassemblyMemory = (session->flags.side == WOLFSSL_SERVER_END) ?
- &session->cliReassemblyMemory : &session->srvReassemblyMemory;
-
- while (*front && ((*front)->begin == *expected) ) {
- word32 room = *bufferSize - *length;
- word32 packetLen = (*front)->end - (*front)->begin + 1;
-
- if (packetLen > room && *bufferSize < MAX_INPUT_SZ) {
- if (GrowInputBuffer(ssl, packetLen, *length) < 0) {
- SetError(MEMORY_STR, error, session, FATAL_ERROR_STATE);
- return 0;
- }
- room = *bufferSize - *length; /* bufferSize is now bigger */
- }
-
- if (packetLen <= room) {
- PacketBuffer* del = *front;
- byte* buf = *myBuffer;
-
- XMEMCPY(&buf[*length], (*front)->data, packetLen);
- *length += packetLen;
- *expected += packetLen;
-
- /* remove used packet */
- *front = (*front)->next;
-
- *reassemblyMemory -= packetLen;
- FreePacketBuffer(del);
-
- moreInput = 1;
- }
- else
- break;
- }
- if (moreInput) {
- *sslFrame = *myBuffer;
- *sslBytes = *length;
- *end = *myBuffer + *length;
- }
- return moreInput;
-}
-
-
-
-/* Process Message(s) from sslFrame */
-/* return Number of bytes on success, 0 for no data yet, and -1 on error */
-static int ProcessMessage(const byte* sslFrame, SnifferSession* session,
- int sslBytes, byte** data, const byte* end,
- void* ctx, char* error)
-{
- const byte* sslBegin = sslFrame;
- const byte* recordEnd; /* end of record indicator */
- const byte* inRecordEnd; /* indicator from input stream not decrypt */
- RecordLayerHeader rh;
- int rhSize = 0;
- int ret;
- int errCode = 0;
- int decoded = 0; /* bytes stored for user in data */
- int notEnough; /* notEnough bytes yet flag */
- int decrypted = 0; /* was current msg decrypted */
- SSL* ssl = (session->flags.side == WOLFSSL_SERVER_END) ?
- session->sslServer : session->sslClient;
-doMessage:
- notEnough = 0;
- if (sslBytes < 0) {
- SetError(PACKET_HDR_SHORT_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- if (sslBytes >= RECORD_HEADER_SZ) {
- if (GetRecordHeader(sslFrame, &rh, &rhSize) != 0) {
- SetError(BAD_RECORD_HDR_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- }
- else
- notEnough = 1;
-
- if (notEnough || rhSize > (sslBytes - RECORD_HEADER_SZ)) {
- /* don't have enough input yet to process full SSL record */
- Trace(PARTIAL_INPUT_STR);
-
- /* store partial if not there already or we advanced */
- if (ssl->buffers.inputBuffer.length == 0 || sslBegin != sslFrame) {
- if (sslBytes > (int)ssl->buffers.inputBuffer.bufferSize) {
- if (GrowInputBuffer(ssl, sslBytes, 0) < 0) {
- SetError(MEMORY_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- }
- XMEMMOVE(ssl->buffers.inputBuffer.buffer, sslFrame, sslBytes);
- ssl->buffers.inputBuffer.length = sslBytes;
- }
- if (HaveMoreInput(session, &sslFrame, &sslBytes, &end, error))
- goto doMessage;
- return decoded;
- }
- sslFrame += RECORD_HEADER_SZ;
- sslBytes -= RECORD_HEADER_SZ;
- recordEnd = sslFrame + rhSize; /* may have more than one record */
- inRecordEnd = recordEnd;
-
- /* decrypt if needed */
- if ((session->flags.side == WOLFSSL_SERVER_END &&
- session->flags.serverCipherOn)
- || (session->flags.side == WOLFSSL_CLIENT_END &&
- session->flags.clientCipherOn)) {
- int ivAdvance = 0; /* TLSv1.1 advance amount */
- if (ssl->decrypt.setup != 1) {
- SetError(DECRYPT_KEYS_NOT_SETUP, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- if (CheckAvailableSize(ssl, rhSize) < 0) {
- SetError(MEMORY_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- sslFrame = DecryptMessage(ssl, sslFrame, rhSize,
- ssl->buffers.outputBuffer.buffer, &errCode,
- &ivAdvance);
- recordEnd = sslFrame - ivAdvance + rhSize; /* sslFrame moved so
- should recordEnd */
- decrypted = 1;
-
-#ifdef WOLFSSL_SNIFFER_STATS
- if (errCode != 0) {
- INC_STAT(SnifferStats.sslKeyFails);
- }
- else {
- LOCK_STAT();
- NOLOCK_INC_STAT(SnifferStats.sslDecryptedPackets);
- NOLOCK_ADD_TO_STAT(SnifferStats.sslDecryptedBytes, sslBytes);
- UNLOCK_STAT();
- }
-#endif
- if (errCode != 0) {
- SetError(BAD_DECRYPT, error, session, FATAL_ERROR_STATE);
- return -1;
- }
- }
-
-doPart:
-
- switch ((enum ContentType)rh.type) {
- case handshake:
- {
- int startIdx = sslBytes;
- int used;
-
- Trace(GOT_HANDSHAKE_STR);
- ret = DoHandShake(sslFrame, &sslBytes, session, error);
- if (ret != 0) {
- if (session->flags.fatalError == 0)
- SetError(BAD_HANDSHAKE_STR, error, session,
- FATAL_ERROR_STATE);
- return -1;
- }
-
- /* DoHandShake now fully decrements sslBytes to remaining */
- used = startIdx - sslBytes;
- sslFrame += used;
- if (decrypted)
- sslFrame += ssl->keys.padSz;
- }
- break;
- case change_cipher_spec:
- if (session->flags.side == WOLFSSL_SERVER_END)
- session->flags.serverCipherOn = 1;
- else
- session->flags.clientCipherOn = 1;
- Trace(GOT_CHANGE_CIPHER_STR);
- ssl->options.handShakeState = HANDSHAKE_DONE;
- ssl->options.handShakeDone = 1;
-
- sslFrame += 1;
- sslBytes -= 1;
-
- break;
- case application_data:
- Trace(GOT_APP_DATA_STR);
- {
- word32 inOutIdx = 0;
-
- ret = DoApplicationData(ssl, (byte*)sslFrame, &inOutIdx);
- if (ret == 0) {
- ret = ssl->buffers.clearOutputBuffer.length;
- TraceGotData(ret);
- if (ret) { /* may be blank message */
- if (data != NULL) {
- byte* tmpData; /* don't leak on realloc free */
- /* add an extra byte at end of allocation in case
- * user wants to null terminate plaintext */
- tmpData = (byte*)XREALLOC(*data, decoded + ret + 1,
- NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmpData == NULL) {
- ForceZero(*data, decoded);
- XFREE(*data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- *data = NULL;
- SetError(MEMORY_STR, error, session,
- FATAL_ERROR_STATE);
- return -1;
- }
- *data = tmpData;
- XMEMCPY(*data + decoded,
- ssl->buffers.clearOutputBuffer.buffer, ret);
- }
- else {
-#ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
- if (StoreDataCb) {
- const byte* buf;
- word32 offset = 0;
- word32 bufSz;
- int stored;
-
- buf = ssl->buffers.clearOutputBuffer.buffer;
- bufSz = ssl->buffers.clearOutputBuffer.length;
- do {
- stored = StoreDataCb(buf, bufSz, offset,
- ctx);
- if (stored <= 0) {
- return -1;
- }
- offset += stored;
- } while (offset < bufSz);
- }
- else {
- SetError(STORE_DATA_CB_MISSING_STR, error,
- session, FATAL_ERROR_STATE);
- return -1;
- }
-#else
- (void)ctx;
- SetError(NO_DATA_DEST_STR, error, session,
- FATAL_ERROR_STATE);
- return -1;
-#endif
- }
- TraceAddedData(ret, decoded);
- decoded += ret;
- ssl->buffers.clearOutputBuffer.length = 0;
- }
- }
- else {
- SetError(BAD_APP_DATA_STR, error,session,FATAL_ERROR_STATE);
- return -1;
- }
- if (ssl->buffers.outputBuffer.dynamicFlag)
- ShrinkOutputBuffer(ssl);
-
- sslFrame += inOutIdx;
- sslBytes -= inOutIdx;
- }
- break;
- case alert:
- Trace(GOT_ALERT_STR);
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslAlerts);
-#endif
- sslFrame += rhSize;
- sslBytes -= rhSize;
- break;
- case no_type:
- default:
- SetError(GOT_UNKNOWN_RECORD_STR, error, session, FATAL_ERROR_STATE);
- return -1;
- }
-
- /* do we have another msg in record ? */
- if (sslFrame < recordEnd) {
- Trace(ANOTHER_MSG_STR);
- goto doPart;
- }
-
- /* back to input stream instead of potential decrypt buffer */
- recordEnd = inRecordEnd;
-
- /* do we have more records ? */
- if (recordEnd < end) {
- Trace(ANOTHER_MSG_STR);
- sslFrame = recordEnd;
- sslBytes = (int)(end - recordEnd);
- goto doMessage;
- }
-
- /* clear used input */
- ssl->buffers.inputBuffer.length = 0;
-
- /* could have more input ready now */
- if (HaveMoreInput(session, &sslFrame, &sslBytes, &end, error))
- goto doMessage;
-
- if (ssl->buffers.inputBuffer.dynamicFlag)
- ShrinkInputBuffer(ssl, NO_FORCED_FREE);
-
- return decoded;
-}
-
-
-/* See if we need to process any pending FIN captures */
-/* Return 0=normal, else = session removed */
-static int CheckFinCapture(IpInfo* ipInfo, TcpInfo* tcpInfo,
- SnifferSession* session)
-{
- int ret = 0;
- if (session->finCaputre.cliFinSeq && session->finCaputre.cliFinSeq <=
- session->cliExpected) {
- if (session->finCaputre.cliCounted == 0) {
- session->flags.finCount += 1;
- session->finCaputre.cliCounted = 1;
- TraceClientFin(session->finCaputre.cliFinSeq, session->cliExpected);
- }
- }
-
- if (session->finCaputre.srvFinSeq && session->finCaputre.srvFinSeq <=
- session->srvExpected) {
- if (session->finCaputre.srvCounted == 0) {
- session->flags.finCount += 1;
- session->finCaputre.srvCounted = 1;
- TraceServerFin(session->finCaputre.srvFinSeq, session->srvExpected);
- }
- }
-
- if (session->flags.finCount >= 2) {
- RemoveSession(session, ipInfo, tcpInfo, 0);
- ret = 1;
- }
- return ret;
-}
-
-
-/* If session is in fatal error state free resources now
- return true if removed, 0 otherwise */
-static int RemoveFatalSession(IpInfo* ipInfo, TcpInfo* tcpInfo,
- SnifferSession* session, char* error)
-{
- if (session && session->flags.fatalError == FATAL_ERROR_STATE) {
- RemoveSession(session, ipInfo, tcpInfo, 0);
- SetError(FATAL_ERROR_STR, error, NULL, 0);
- return 1;
- }
- return 0;
-}
-
-
-/* Passes in an IP/TCP packet for decoding (ethernet/localhost frame) removed */
-/* returns Number of bytes on success, 0 for no data yet, and -1 on error */
-static int ssl_DecodePacketInternal(const byte* packet, int length,
- void* vChain, word32 chainSz,
- byte** data, SSLInfo* sslInfo,
- void* ctx, char* error)
-{
- TcpInfo tcpInfo;
- IpInfo ipInfo;
- const byte* sslFrame;
- const byte* end;
- int sslBytes; /* ssl bytes unconsumed */
- int ret;
- SnifferSession* session = 0;
-
-#ifdef WOLFSSL_SNIFFER_CHAIN_INPUT
- if (packet == NULL && vChain != NULL) {
- struct iovec* chain = (struct iovec*)vChain;
- word32 i;
-
- length = 0;
- for (i = 0; i < chainSz; i++)
- length += chain[i].iov_len;
- packet = (const byte*)chain[0].iov_base;
- }
-#endif
-
- if (CheckHeaders(&ipInfo, &tcpInfo, packet, length, &sslFrame, &sslBytes,
- error) != 0)
- return -1;
-
- end = sslFrame + sslBytes;
-
- ret = CheckSession(&ipInfo, &tcpInfo, sslBytes, &session, error);
- if (RemoveFatalSession(&ipInfo, &tcpInfo, session, error)) return -1;
- else if (ret == -1) return -1;
- else if (ret == 1) {
-#ifdef WOLFSSL_SNIFFER_STATS
- if (sslBytes > 0) {
- LOCK_STAT();
- NOLOCK_INC_STAT(SnifferStats.sslEncryptedPackets);
- NOLOCK_ADD_TO_STAT(SnifferStats.sslEncryptedBytes, sslBytes);
- UNLOCK_STAT();
- }
- else
- INC_STAT(SnifferStats.sslDecryptedPackets);
-#endif
- return 0; /* done for now */
- }
-
- ret = CheckSequence(&ipInfo, &tcpInfo, session, &sslBytes, &sslFrame,error);
- if (RemoveFatalSession(&ipInfo, &tcpInfo, session, error)) return -1;
- else if (ret == -1) return -1;
- else if (ret == 1) {
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslDecryptedPackets);
-#endif
- return 0; /* done for now */
- }
-
- ret = CheckPreRecord(&ipInfo, &tcpInfo, &sslFrame, &session, &sslBytes,
- &end, vChain, chainSz, error);
- if (RemoveFatalSession(&ipInfo, &tcpInfo, session, error)) return -1;
- else if (ret == -1) return -1;
- else if (ret == 1) {
-#ifdef WOLFSSL_SNIFFER_STATS
- INC_STAT(SnifferStats.sslDecryptedPackets);
-#endif
- return 0; /* done for now */
- }
-
-#ifdef WOLFSSL_SNIFFER_STATS
- if (sslBytes > 0) {
- LOCK_STAT();
- NOLOCK_INC_STAT(SnifferStats.sslEncryptedPackets);
- NOLOCK_ADD_TO_STAT(SnifferStats.sslEncryptedBytes, sslBytes);
- UNLOCK_STAT();
- }
- else
- INC_STAT(SnifferStats.sslDecryptedPackets);
-#endif
-
- ret = ProcessMessage(sslFrame, session, sslBytes, data, end, ctx, error);
- if (RemoveFatalSession(&ipInfo, &tcpInfo, session, error)) return -1;
- if (CheckFinCapture(&ipInfo, &tcpInfo, session) == 0) {
- CopySessionInfo(session, sslInfo);
- }
-
- return ret;
-}
-
-
-/* Passes in an IP/TCP packet for decoding (ethernet/localhost frame) removed */
-/* returns Number of bytes on success, 0 for no data yet, and -1 on error */
-/* Also returns Session Info if available */
-int ssl_DecodePacketWithSessionInfo(const unsigned char* packet, int length,
- unsigned char** data, SSLInfo* sslInfo, char* error)
-{
- return ssl_DecodePacketInternal(packet, length, NULL, 0, data, sslInfo,
- NULL, error);
-}
-
-
-/* Passes in an IP/TCP packet for decoding (ethernet/localhost frame) removed */
-/* returns Number of bytes on success, 0 for no data yet, and -1 on error */
-int ssl_DecodePacket(const byte* packet, int length, byte** data, char* error)
-{
- return ssl_DecodePacketInternal(packet, length, NULL, 0, data, NULL, NULL,
- error);
-}
-
-
-#ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
-
-int ssl_DecodePacketWithSessionInfoStoreData(const unsigned char* packet,
- int length, void* ctx, SSLInfo* sslInfo, char* error)
-{
- return ssl_DecodePacketInternal(packet, length, NULL, 0, NULL, sslInfo,
- ctx, error);
-}
-
-#endif
-
-
-#ifdef WOLFSSL_SNIFFER_CHAIN_INPUT
-
-int ssl_DecodePacketWithChain(void* vChain, word32 chainSz, byte** data,
- char* error)
-{
- return ssl_DecodePacketInternal(NULL, 0, vChain, chainSz, data, NULL, NULL,
- error);
-}
-
-#endif
-
-
-#if defined(WOLFSSL_SNIFFER_CHAIN_INPUT) && \
- defined(WOLFSSL_SNIFFER_STORE_DATA_CB)
-
-int ssl_DecodePacketWithChainSessionInfoStoreData(void* vChain, word32 chainSz,
- void* ctx, SSLInfo* sslInfo, char* error)
-{
- return ssl_DecodePacketInternal(NULL, 0, vChain, chainSz, NULL, sslInfo,
- ctx, error);
-}
-
-#endif
-
-
-/* Deallocator for the decoded data buffer. */
-/* returns 0 on success, -1 on error */
-int ssl_FreeDecodeBuffer(byte** data, char* error)
-{
- return ssl_FreeZeroDecodeBuffer(data, 0, error);
-}
-
-
-/* Deallocator for the decoded data buffer, zeros out buffer. */
-/* returns 0 on success, -1 on error */
-int ssl_FreeZeroDecodeBuffer(byte** data, int sz, char* error)
-{
- (void)error;
-
- if (sz < 0) {
- return -1;
- }
-
- if (data != NULL) {
- ForceZero(*data, (word32)sz);
- XFREE(*data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- *data = NULL;
- }
-
- return 0;
-}
-
-
-/* Enables (if traceFile)/ Disables debug tracing */
-/* returns 0 on success, -1 on error */
-int ssl_Trace(const char* traceFile, char* error)
-{
- if (traceFile) {
- /* Don't try to reopen the file */
- if (TraceFile == NULL) {
- TraceFile = fopen(traceFile, "a");
- if (!TraceFile) {
- SetError(BAD_TRACE_FILE_STR, error, NULL, 0);
- return -1;
- }
- TraceOn = 1;
- }
- }
- else
- TraceOn = 0;
-
- return 0;
-}
-
-
-/* Enables/Disables Recovery of missed data if later packets allow
- * maxMemory is number of bytes to use for reassembly buffering per session,
- * -1 means unlimited
- * returns 0 on success, -1 on error */
-int ssl_EnableRecovery(int onOff, int maxMemory, char* error)
-{
- (void)error;
-
- RecoveryEnabled = onOff;
- if (onOff)
- MaxRecoveryMemory = maxMemory;
-
- return 0;
-}
-
-
-
-#ifdef WOLFSSL_SESSION_STATS
-
-int ssl_GetSessionStats(unsigned int* active, unsigned int* total,
- unsigned int* peak, unsigned int* maxSessions,
- unsigned int* missedData, unsigned int* reassemblyMem,
- char* error)
-{
- int ret;
-
- if (missedData) {
- wc_LockMutex(&RecoveryMutex);
- *missedData = MissedDataSessions;
- wc_UnLockMutex(&RecoveryMutex);
- }
-
- if (reassemblyMem) {
- SnifferSession* session;
- int i;
-
- *reassemblyMem = 0;
- wc_LockMutex(&SessionMutex);
- for (i = 0; i < HASH_SIZE; i++) {
- session = SessionTable[i];
- while (session) {
- *reassemblyMem += session->cliReassemblyMemory;
- *reassemblyMem += session->srvReassemblyMemory;
- session = session->next;
- }
- }
- wc_UnLockMutex(&SessionMutex);
- }
-
- ret = wolfSSL_get_session_stats(active, total, peak, maxSessions);
-
- if (ret == WOLFSSL_SUCCESS)
- return 0;
- else {
- SetError(BAD_SESSION_STATS, error, NULL, 0);
- return -1;
- }
-}
-
-#endif
-
-
-
-int ssl_SetConnectionCb(SSLConnCb cb)
-{
- ConnectionCb = cb;
- return 0;
-}
-
-
-
-int ssl_SetConnectionCtx(void* ctx)
-{
- ConnectionCbCtx = ctx;
- return 0;
-}
-
-
-#ifdef WOLFSSL_SNIFFER_STATS
-
-/* Resets the statistics tracking global structure.
- * returns 0 on success, -1 on error */
-int ssl_ResetStatistics(void)
-{
- wc_LockMutex(&StatsMutex);
- XMEMSET(&SnifferStats, 0, sizeof(SSLStats));
- wc_UnLockMutex(&StatsMutex);
- return 0;
-}
-
-
-/* Copies the SSL statistics into the provided stats record.
- * returns 0 on success, -1 on error */
-int ssl_ReadStatistics(SSLStats* stats)
-{
- if (stats == NULL)
- return -1;
-
- LOCK_STAT();
- XMEMCPY(stats, &SnifferStats, sizeof(SSLStats));
- UNLOCK_STAT();
- return 0;
-}
-
-/* Copies the SSL statistics into the provided stats record then
- * resets the statistics tracking global structure.
- * returns 0 on success, -1 on error */
-int ssl_ReadResetStatistics(SSLStats* stats)
-{
- if (stats == NULL)
- return -1;
-
- LOCK_STAT();
- XMEMCPY(stats, &SnifferStats, sizeof(SSLStats));
- XMEMSET(&SnifferStats, 0, sizeof(SSLStats));
- UNLOCK_STAT();
- return 0;
-}
-
-#endif /* WOLFSSL_SNIFFER_STATS */
-
-
-#ifdef WOLFSSL_SNIFFER_WATCH
-
-int ssl_SetWatchKeyCallback_ex(SSLWatchCb cb, int devId, char* error)
-{
- (void)devId;
- WatchCb = cb;
- return CreateWatchSnifferServer(error);
-}
-
-
-int ssl_SetWatchKeyCallback(SSLWatchCb cb, char* error)
-{
- WatchCb = cb;
- return CreateWatchSnifferServer(error);
-}
-
-
-int ssl_SetWatchKeyCtx(void* ctx, char* error)
-{
- (void)error;
- WatchCbCtx = ctx;
- return 0;
-}
-
-
-int ssl_SetWatchKey_buffer(void* vSniffer, const byte* key, word32 keySz,
- int keyType, char* error)
-{
- SnifferSession* sniffer;
- int ret;
-
- if (vSniffer == NULL) {
- return -1;
- }
- if (key == NULL || keySz == 0) {
- return -1;
- }
-
- sniffer = (SnifferSession*)vSniffer;
- /* Remap the keyType from what the user can use to
- * what wolfSSL_use_PrivateKey_buffer expects. */
- keyType = (keyType == FILETYPE_PEM) ? WOLFSSL_FILETYPE_PEM :
- WOLFSSL_FILETYPE_ASN1;
-
- ret = wolfSSL_use_PrivateKey_buffer(sniffer->sslServer,
- key, keySz, keyType);
- if (ret != WOLFSSL_SUCCESS) {
- SetError(KEY_FILE_STR, error, sniffer, FATAL_ERROR_STATE);
- return -1;
- }
-
- return 0;
-}
-
-
-int ssl_SetWatchKey_file(void* vSniffer, const char* keyFile, int keyType,
- const char* password, char* error)
-{
- byte* keyBuf = NULL;
- word32 keyBufSz = 0;
- int ret;
-
- if (vSniffer == NULL) {
- return -1;
- }
- if (keyFile == NULL) {
- return -1;
- }
-
- /* Remap the keyType from what the user can use to
- * what LoadKeyFile expects. */
- keyType = (keyType == FILETYPE_PEM) ? WOLFSSL_FILETYPE_PEM :
- WOLFSSL_FILETYPE_ASN1;
-
- ret = LoadKeyFile(&keyBuf, &keyBufSz, keyFile, keyType, password);
- if (ret < 0) {
- SetError(KEY_FILE_STR, error, NULL, 0);
- XFREE(keyBuf, NULL, DYNAMIC_TYPE_X509);
- return -1;
- }
-
- ret = ssl_SetWatchKey_buffer(vSniffer, keyBuf, keyBufSz, FILETYPE_DER,
- error);
- XFREE(keyBuf, NULL, DYNAMIC_TYPE_X509);
-
- return ret;
-}
-
-#endif /* WOLFSSL_SNIFFER_WATCH */
-
-
-#ifdef WOLFSSL_SNIFFER_STORE_DATA_CB
-
-int ssl_SetStoreDataCallback(SSLStoreDataCb cb)
-{
- StoreDataCb = cb;
- return 0;
-}
-
-#endif /* WOLFSSL_SNIFFER_STORE_DATA_CB */
-
-#endif /* WOLFSSL_SNIFFER */
-#endif /* WOLFCRYPT_ONLY */
diff --git a/client/wolfssl/src/ssl.c b/client/wolfssl/src/ssl.c
deleted file mode 100644
index 58569f3..0000000
--- a/client/wolfssl/src/ssl.c
+++ /dev/null
@@ -1,47707 +0,0 @@
-/* ssl.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-#if defined(OPENSSL_EXTRA) && !defined(_WIN32)
- /* turn on GNU extensions for XVASPRINTF with wolfSSL_BIO_printf */
- #undef _GNU_SOURCE
- #define _GNU_SOURCE
-#endif
-
-#if !defined(WOLFCRYPT_ONLY) || defined(OPENSSL_EXTRA) || \
- defined(OPENSSL_EXTRA_X509_SMALL)
-
-#ifdef HAVE_ERRNO_H
- #include
-#endif
-
-#include
-#include
-#include
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include
-#endif
-
-
-#if !defined(WOLFSSL_ALLOW_NO_SUITES) && !defined(WOLFCRYPT_ONLY)
- #if defined(NO_DH) && !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA) \
- && !defined(WOLFSSL_STATIC_DH) && !defined(WOLFSSL_STATIC_PSK) \
- && !defined(HAVE_ED25519) && !defined(HAVE_ED448)
- #error "No cipher suites defined because DH disabled, ECC disabled, and no static suites defined. Please see top of README"
- #endif
- #ifdef WOLFSSL_CERT_GEN
- /* need access to Cert struct for creating certificate */
- #include
- #endif
-#endif
-
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
- defined(HAVE_WEBSERVER) || defined(WOLFSSL_KEY_GEN)
- #include
- /* openssl headers end, wolfssl internal headers next */
-#endif
-
-#include
-
-#ifndef NO_RSA
- #include
-#endif
-
-#ifdef OPENSSL_EXTRA
- /* openssl headers begin */
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- /* openssl headers end, wolfssl internal headers next */
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #if defined(OPENSSL_ALL) || defined(HAVE_STUNNEL)
- #include
- #endif /* WITH_STUNNEL */
- #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
- #include
- #endif
- #if defined(WOLFCRYPT_HAVE_SRP) && !defined(NO_SHA256) \
- && !defined(WC_NO_RNG)
- #include
- #endif
- #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
- #include
- #endif
- #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
- #include
- #endif /* OPENSSL_ALL && HAVE_PKCS7 */
-#endif
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- int SetIndividualInternal(WOLFSSL_BIGNUM* bn, mp_int* mpi);
- int SetIndividualExternal(WOLFSSL_BIGNUM** bn, mp_int* mpi);
- int oid2nid(word32 oid, int grp);
-#endif
-
-#if defined(WOLFSSL_QT)
- #include
-#endif
-
-#ifdef NO_ASN
- #include
-#endif
-#endif /* !WOLFCRYPT_ONLY || OPENSSL_EXTRA */
-
-#define WOLFSSL_EVP_INCLUDED
-#include "wolfcrypt/src/evp.c"
-
-#ifdef OPENSSL_EXTRA
-/* Global pointer to constant BN on */
-static WOLFSSL_BIGNUM* bn_one = NULL;
-#endif
-
-#ifndef WOLFCRYPT_ONLY
-
-#if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
-const WOLF_EC_NIST_NAME kNistCurves[] = {
- {XSTR_SIZEOF("P-192"), "P-192", NID_X9_62_prime192v1},
- {XSTR_SIZEOF("P-256"), "P-256", NID_X9_62_prime256v1},
- {XSTR_SIZEOF("P-112"), "P-112", NID_secp112r1},
- {XSTR_SIZEOF("P-112-2"), "P-112-2", NID_secp112r2},
- {XSTR_SIZEOF("P-128"), "P-128", NID_secp128r1},
- {XSTR_SIZEOF("P-128-2"), "P-128-2", NID_secp128r2},
- {XSTR_SIZEOF("P-160"), "P-160", NID_secp160r1},
- {XSTR_SIZEOF("P-160-2"), "P-160-2", NID_secp160r2},
- {XSTR_SIZEOF("P-224"), "P-224", NID_secp224r1},
- {XSTR_SIZEOF("P-384"), "P-384", NID_secp384r1},
- {XSTR_SIZEOF("P-521"), "P-521", NID_secp521r1},
- {XSTR_SIZEOF("K-160"), "K-160", NID_secp160k1},
- {XSTR_SIZEOF("K-192"), "K-192", NID_secp192k1},
- {XSTR_SIZEOF("K-224"), "K-224", NID_secp224k1},
- {XSTR_SIZEOF("K-256"), "K-256", NID_secp256k1},
- {XSTR_SIZEOF("B-160"), "B-160", NID_brainpoolP160r1},
- {XSTR_SIZEOF("B-192"), "B-192", NID_brainpoolP192r1},
- {XSTR_SIZEOF("B-224"), "B-224", NID_brainpoolP224r1},
- {XSTR_SIZEOF("B-256"), "B-256", NID_brainpoolP256r1},
- {XSTR_SIZEOF("B-320"), "B-320", NID_brainpoolP320r1},
- {XSTR_SIZEOF("B-384"), "B-384", NID_brainpoolP384r1},
- {XSTR_SIZEOF("B-512"), "B-512", NID_brainpoolP512r1},
- {0, NULL, 0},
-};
-#endif
-
-#if defined(WOLFSSL_RENESAS_TSIP_TLS)
- /* for root ca verification */
-int tsip_tls_RootCertVerify(const byte *cert, word32 cert_len,
- word32 key_n_start, word32 key_n_len,
- word32 key_e_start, word32 key_e_len,
- word32 cm_row);
-byte tsip_rootCAverified( );
-#endif
-
-#ifdef WOLFSSL_SESSION_EXPORT
-#ifdef WOLFSSL_DTLS
-int wolfSSL_dtls_import(WOLFSSL* ssl, unsigned char* buf, unsigned int sz)
-{
- WOLFSSL_ENTER("wolfSSL_session_import");
-
- if (ssl == NULL || buf == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* sanity checks on buffer and protocol are done in internal function */
- return wolfSSL_dtls_import_internal(ssl, buf, sz);
-}
-
-
-/* Sets the function to call for serializing the session. This function is
- * called right after the handshake is completed. */
-int wolfSSL_CTX_dtls_set_export(WOLFSSL_CTX* ctx, wc_dtls_export func)
-{
-
- WOLFSSL_ENTER("wolfSSL_CTX_dtls_set_export");
-
- /* purposefully allow func to be NULL */
- if (ctx == NULL) {
- return BAD_FUNC_ARG;
- }
-
- ctx->dtls_export = func;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* Sets the function in WOLFSSL struct to call for serializing the session. This
- * function is called right after the handshake is completed. */
-int wolfSSL_dtls_set_export(WOLFSSL* ssl, wc_dtls_export func)
-{
-
- WOLFSSL_ENTER("wolfSSL_dtls_set_export");
-
- /* purposefully allow func to be NULL */
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- ssl->dtls_export = func;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* This function allows for directly serializing a session rather than using
- * callbacks. It has less overhead by removing a temporary buffer and gives
- * control over when the session gets serialized. When using callbacks the
- * session is always serialized immediately after the handshake is finished.
- *
- * buf is the argument to contain the serialized session
- * sz is the size of the buffer passed in
- * ssl is the WOLFSSL struct to serialize
- * returns the size of serialized session on success, 0 on no action, and
- * negative value on error */
-int wolfSSL_dtls_export(WOLFSSL* ssl, unsigned char* buf, unsigned int* sz)
-{
- WOLFSSL_ENTER("wolfSSL_dtls_export");
-
- if (ssl == NULL || sz == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (buf == NULL) {
- *sz = MAX_EXPORT_BUFFER;
- return 0;
- }
-
- /* if not DTLS do nothing */
- if (!ssl->options.dtls) {
- WOLFSSL_MSG("Currently only DTLS export is supported");
- return 0;
- }
-
- /* copy over keys, options, and dtls state struct */
- return wolfSSL_dtls_export_internal(ssl, buf, *sz);
-}
-
-
-/* This function is similar to wolfSSL_dtls_export but only exports the portion
- * of the WOLFSSL structure related to the state of the connection, i.e. peer
- * sequence number, epoch, AEAD state etc.
- *
- * buf is the argument to contain the serialized state, if null then set "sz" to
- * buffer size required
- * sz is the size of the buffer passed in
- * ssl is the WOLFSSL struct to serialize
- * returns the size of serialized session on success, 0 on no action, and
- * negative value on error */
-int wolfSSL_dtls_export_state_only(WOLFSSL* ssl, unsigned char* buf,
- unsigned int* sz)
-{
- WOLFSSL_ENTER("wolfSSL_dtls_export_state_only");
-
- if (ssl == NULL || sz == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (buf == NULL) {
- *sz = MAX_EXPORT_STATE_BUFFER;
- return 0;
- }
-
- /* if not DTLS do nothing */
- if (!ssl->options.dtls) {
- WOLFSSL_MSG("Currently only DTLS export state is supported");
- return 0;
- }
-
- /* copy over keys, options, and dtls state struct */
- return wolfSSL_dtls_export_state_internal(ssl, buf, *sz);
-}
-
-
-/* returns 0 on success */
-int wolfSSL_send_session(WOLFSSL* ssl)
-{
- int ret;
- byte* buf;
- word16 bufSz = MAX_EXPORT_BUFFER;
-
- WOLFSSL_ENTER("wolfSSL_send_session");
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- buf = (byte*)XMALLOC(bufSz, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (buf == NULL) {
- return MEMORY_E;
- }
-
- /* if not DTLS do nothing */
- if (!ssl->options.dtls) {
- XFREE(buf, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- WOLFSSL_MSG("Currently only DTLS export is supported");
- return 0;
- }
-
- /* copy over keys, options, and dtls state struct */
- ret = wolfSSL_dtls_export_internal(ssl, buf, bufSz);
- if (ret < 0) {
- XFREE(buf, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
-
- /* if no error ret has size of buffer */
- ret = ssl->dtls_export(ssl, buf, ret, NULL);
- if (ret != WOLFSSL_SUCCESS) {
- XFREE(buf, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
-
- XFREE(buf, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return 0;
-}
-#endif /* WOLFSSL_DTLS */
-#endif /* WOLFSSL_SESSION_EXPORT */
-
-/* prevent multiple mutex initializations */
-static volatile WOLFSSL_GLOBAL int initRefCount = 0;
-static WOLFSSL_GLOBAL wolfSSL_Mutex count_mutex; /* init ref count mutex */
-
-/* Create a new WOLFSSL_CTX struct and return the pointer to created struct.
- WOLFSSL_METHOD pointer passed in is given to ctx to manage.
- This function frees the passed in WOLFSSL_METHOD struct on failure and on
- success is freed when ctx is freed.
- */
-WOLFSSL_CTX* wolfSSL_CTX_new_ex(WOLFSSL_METHOD* method, void* heap)
-{
- WOLFSSL_CTX* ctx = NULL;
-
- WOLFSSL_ENTER("wolfSSL_CTX_new_ex");
-
- if (initRefCount == 0) {
- /* user no longer forced to call Init themselves */
- int ret = wolfSSL_Init();
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("wolfSSL_Init failed");
- WOLFSSL_LEAVE("WOLFSSL_CTX_new", 0);
- if (method != NULL) {
- XFREE(method, heap, DYNAMIC_TYPE_METHOD);
- }
- return NULL;
- }
- }
-
- if (method == NULL)
- return ctx;
-
- ctx = (WOLFSSL_CTX*) XMALLOC(sizeof(WOLFSSL_CTX), heap, DYNAMIC_TYPE_CTX);
- if (ctx) {
- int ret;
-
- ret = InitSSL_Ctx(ctx, method, heap);
- #ifdef WOLFSSL_STATIC_MEMORY
- if (heap != NULL) {
- ctx->onHeap = 1; /* free the memory back to heap when done */
- }
- #endif
- if (ret < 0) {
- WOLFSSL_MSG("Init CTX failed");
- wolfSSL_CTX_free(ctx);
- ctx = NULL;
- }
-#if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
- && !defined(NO_SHA256) && !defined(WC_NO_RNG)
- else {
- ctx->srp = (Srp*) XMALLOC(sizeof(Srp), heap, DYNAMIC_TYPE_SRP);
- if (ctx->srp == NULL){
- WOLFSSL_MSG("Init CTX failed");
- wolfSSL_CTX_free(ctx);
- return NULL;
- }
- XMEMSET(ctx->srp, 0, sizeof(Srp));
- }
-#endif
- }
- else {
- WOLFSSL_MSG("Alloc CTX failed, method freed");
- XFREE(method, heap, DYNAMIC_TYPE_METHOD);
- }
-
-
- WOLFSSL_LEAVE("WOLFSSL_CTX_new", 0);
- return ctx;
-}
-
-
-WOLFSSL_ABI
-WOLFSSL_CTX* wolfSSL_CTX_new(WOLFSSL_METHOD* method)
-{
-#ifdef WOLFSSL_HEAP_TEST
- /* if testing the heap hint then set top level CTX to have test value */
- return wolfSSL_CTX_new_ex(method, (void*)WOLFSSL_HEAP_TEST);
-#else
- return wolfSSL_CTX_new_ex(method, NULL);
-#endif
-}
-
-#ifdef OPENSSL_EXTRA
-/* increases CTX reference count to track proper time to "free" */
-int wolfSSL_CTX_up_ref(WOLFSSL_CTX* ctx)
-{
- int refCount = SSL_CTX_RefCount(ctx, 1);
- return ((refCount > 1) ? 1 : 0);
-}
-#endif
-
-WOLFSSL_ABI
-void wolfSSL_CTX_free(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_ENTER("SSL_CTX_free");
- if (ctx) {
-#if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
-&& !defined(NO_SHA256) && !defined(WC_NO_RNG)
- if (ctx->srp != NULL){
- if (ctx->srp_password != NULL){
- XFREE(ctx->srp_password, ctx->heap, DYNAMIC_TYPE_SRP);
- ctx->srp_password = NULL;
- }
- wc_SrpTerm(ctx->srp);
- XFREE(ctx->srp, ctx->heap, DYNAMIC_TYPE_SRP);
- ctx->srp = NULL;
- }
-#endif
- FreeSSL_Ctx(ctx);
- }
-
- WOLFSSL_LEAVE("SSL_CTX_free", 0);
-}
-
-
-#ifdef HAVE_ENCRYPT_THEN_MAC
-/**
- * Sets whether Encrypt-Then-MAC extension can be negotiated against context.
- * The default value: enabled.
- *
- * ctx SSL/TLS context.
- * set Whether to allow or not: 1 is allow and 0 is disallow.
- * returns WOLFSSL_SUCCESS
- */
-int wolfSSL_CTX_AllowEncryptThenMac(WOLFSSL_CTX *ctx, int set)
-{
- ctx->disallowEncThenMac = !set;
- return WOLFSSL_SUCCESS;
-}
-
-/**
- * Sets whether Encrypt-Then-MAC extension can be negotiated against context.
- * The default value comes from context.
- *
- * ctx SSL/TLS context.
- * set Whether to allow or not: 1 is allow and 0 is disallow.
- * returns WOLFSSL_SUCCESS
- */
-int wolfSSL_AllowEncryptThenMac(WOLFSSL *ssl, int set)
-{
- ssl->options.disallowEncThenMac = !set;
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-#ifdef SINGLE_THREADED
-/* no locking in single threaded mode, allow a CTX level rng to be shared with
- * WOLFSSL objects, WOLFSSL_SUCCESS on ok */
-int wolfSSL_CTX_new_rng(WOLFSSL_CTX* ctx)
-{
- WC_RNG* rng;
- int ret;
-
- if (ctx == NULL) {
- return BAD_FUNC_ARG;
- }
-
- rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), ctx->heap, DYNAMIC_TYPE_RNG);
- if (rng == NULL) {
- return MEMORY_E;
- }
-
-#ifndef HAVE_FIPS
- ret = wc_InitRng_ex(rng, ctx->heap, ctx->devId);
-#else
- ret = wc_InitRng(rng);
-#endif
- if (ret != 0) {
- XFREE(rng, ctx->heap, DYNAMIC_TYPE_RNG);
- return ret;
- }
-
- ctx->rng = rng;
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-
-WOLFSSL_ABI
-WOLFSSL* wolfSSL_new(WOLFSSL_CTX* ctx)
-{
- WOLFSSL* ssl = NULL;
- int ret = 0;
-
- (void)ret;
- WOLFSSL_ENTER("SSL_new");
-
- if (ctx == NULL)
- return ssl;
-
- ssl = (WOLFSSL*) XMALLOC(sizeof(WOLFSSL), ctx->heap, DYNAMIC_TYPE_SSL);
- if (ssl)
- if ( (ret = InitSSL(ssl, ctx, 0)) < 0) {
- FreeSSL(ssl, ctx->heap);
- ssl = 0;
- }
-
- WOLFSSL_LEAVE("SSL_new", ret);
- return ssl;
-}
-
-
-WOLFSSL_ABI
-void wolfSSL_free(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("SSL_free");
- if (ssl)
- FreeSSL(ssl, ssl->ctx->heap);
- WOLFSSL_LEAVE("SSL_free", 0);
-}
-
-
-int wolfSSL_is_server(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
- return ssl->options.side == WOLFSSL_SERVER_END;
-}
-
-#ifdef HAVE_WRITE_DUP
-
-/*
- * Release resources around WriteDup object
- *
- * ssl WOLFSSL object
- *
- * no return, destruction so make best attempt
-*/
-void FreeWriteDup(WOLFSSL* ssl)
-{
- int doFree = 0;
-
- WOLFSSL_ENTER("FreeWriteDup");
-
- if (ssl->dupWrite) {
- if (wc_LockMutex(&ssl->dupWrite->dupMutex) == 0) {
- ssl->dupWrite->dupCount--;
- if (ssl->dupWrite->dupCount == 0) {
- doFree = 1;
- } else {
- WOLFSSL_MSG("WriteDup count not zero, no full free");
- }
- wc_UnLockMutex(&ssl->dupWrite->dupMutex);
- }
- }
-
- if (doFree) {
- WOLFSSL_MSG("Doing WriteDup full free, count to zero");
- wc_FreeMutex(&ssl->dupWrite->dupMutex);
- XFREE(ssl->dupWrite, ssl->heap, DYNAMIC_TYPE_WRITEDUP);
- }
-}
-
-
-/*
- * duplicate existing ssl members into dup needed for writing
- *
- * dup write only WOLFSSL
- * ssl existing WOLFSSL
- *
- * 0 on success
-*/
-static int DupSSL(WOLFSSL* dup, WOLFSSL* ssl)
-{
- /* shared dupWrite setup */
- ssl->dupWrite = (WriteDup*)XMALLOC(sizeof(WriteDup), ssl->heap,
- DYNAMIC_TYPE_WRITEDUP);
- if (ssl->dupWrite == NULL) {
- return MEMORY_E;
- }
- XMEMSET(ssl->dupWrite, 0, sizeof(WriteDup));
-
- if (wc_InitMutex(&ssl->dupWrite->dupMutex) != 0) {
- XFREE(ssl->dupWrite, ssl->heap, DYNAMIC_TYPE_WRITEDUP);
- ssl->dupWrite = NULL;
- return BAD_MUTEX_E;
- }
- ssl->dupWrite->dupCount = 2; /* both sides have a count to start */
- dup->dupWrite = ssl->dupWrite; /* each side uses */
-
- /* copy write parts over to dup writer */
- XMEMCPY(&dup->specs, &ssl->specs, sizeof(CipherSpecs));
- XMEMCPY(&dup->options, &ssl->options, sizeof(Options));
- XMEMCPY(&dup->keys, &ssl->keys, sizeof(Keys));
- XMEMCPY(&dup->encrypt, &ssl->encrypt, sizeof(Ciphers));
- /* dup side now owns encrypt/write ciphers */
- XMEMSET(&ssl->encrypt, 0, sizeof(Ciphers));
-
- dup->IOCB_WriteCtx = ssl->IOCB_WriteCtx;
- dup->wfd = ssl->wfd;
- dup->wflags = ssl->wflags;
- dup->hmac = ssl->hmac;
-#ifdef HAVE_TRUNCATED_HMAC
- dup->truncated_hmac = ssl->truncated_hmac;
-#endif
-
- /* unique side dup setup */
- dup->dupSide = WRITE_DUP_SIDE;
- ssl->dupSide = READ_DUP_SIDE;
-
- return 0;
-}
-
-
-/*
- * duplicate a WOLFSSL object post handshake for writing only
- * turn existing object into read only. Allows concurrent access from two
- * different threads.
- *
- * ssl existing WOLFSSL object
- *
- * return dup'd WOLFSSL object on success
-*/
-WOLFSSL* wolfSSL_write_dup(WOLFSSL* ssl)
-{
- WOLFSSL* dup = NULL;
- int ret = 0;
-
- (void)ret;
- WOLFSSL_ENTER("wolfSSL_write_dup");
-
- if (ssl == NULL) {
- return ssl;
- }
-
- if (ssl->options.handShakeDone == 0) {
- WOLFSSL_MSG("wolfSSL_write_dup called before handshake complete");
- return NULL;
- }
-
- if (ssl->dupWrite) {
- WOLFSSL_MSG("wolfSSL_write_dup already called once");
- return NULL;
- }
-
- dup = (WOLFSSL*) XMALLOC(sizeof(WOLFSSL), ssl->ctx->heap, DYNAMIC_TYPE_SSL);
- if (dup) {
- if ( (ret = InitSSL(dup, ssl->ctx, 1)) < 0) {
- FreeSSL(dup, ssl->ctx->heap);
- dup = NULL;
- } else if ( (ret = DupSSL(dup, ssl)) < 0) {
- FreeSSL(dup, ssl->ctx->heap);
- dup = NULL;
- }
- }
-
- WOLFSSL_LEAVE("wolfSSL_write_dup", ret);
-
- return dup;
-}
-
-
-/*
- * Notify write dup side of fatal error or close notify
- *
- * ssl WOLFSSL object
- * err Notify err
- *
- * 0 on success
-*/
-int NotifyWriteSide(WOLFSSL* ssl, int err)
-{
- int ret;
-
- WOLFSSL_ENTER("NotifyWriteSide");
-
- ret = wc_LockMutex(&ssl->dupWrite->dupMutex);
- if (ret == 0) {
- ssl->dupWrite->dupErr = err;
- ret = wc_UnLockMutex(&ssl->dupWrite->dupMutex);
- }
-
- return ret;
-}
-
-
-#endif /* HAVE_WRITE_DUP */
-
-
-#ifdef HAVE_POLY1305
-/* set if to use old poly 1 for yes 0 to use new poly */
-int wolfSSL_use_old_poly(WOLFSSL* ssl, int value)
-{
- (void)ssl;
- (void)value;
-
-#ifndef WOLFSSL_NO_TLS12
- WOLFSSL_ENTER("SSL_use_old_poly");
- WOLFSSL_MSG("Warning SSL connection auto detects old/new and this function"
- "is depreciated");
- ssl->options.oldPoly = (word16)value;
- WOLFSSL_LEAVE("SSL_use_old_poly", 0);
-#endif
- return 0;
-}
-#endif
-
-
-WOLFSSL_ABI
-int wolfSSL_set_fd(WOLFSSL* ssl, int fd)
-{
- int ret;
-
- WOLFSSL_ENTER("SSL_set_fd");
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- ret = wolfSSL_set_read_fd(ssl, fd);
- if (ret == WOLFSSL_SUCCESS) {
- ret = wolfSSL_set_write_fd(ssl, fd);
- }
-
- return ret;
-}
-
-
-int wolfSSL_set_read_fd(WOLFSSL* ssl, int fd)
-{
- WOLFSSL_ENTER("SSL_set_read_fd");
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- ssl->rfd = fd; /* not used directly to allow IO callbacks */
- ssl->IOCB_ReadCtx = &ssl->rfd;
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- ssl->IOCB_ReadCtx = &ssl->buffers.dtlsCtx;
- ssl->buffers.dtlsCtx.rfd = fd;
- }
- #endif
-
- WOLFSSL_LEAVE("SSL_set_read_fd", WOLFSSL_SUCCESS);
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_set_write_fd(WOLFSSL* ssl, int fd)
-{
- WOLFSSL_ENTER("SSL_set_write_fd");
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- ssl->wfd = fd; /* not used directly to allow IO callbacks */
- ssl->IOCB_WriteCtx = &ssl->wfd;
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- ssl->IOCB_WriteCtx = &ssl->buffers.dtlsCtx;
- ssl->buffers.dtlsCtx.wfd = fd;
- }
- #endif
-
- WOLFSSL_LEAVE("SSL_set_write_fd", WOLFSSL_SUCCESS);
- return WOLFSSL_SUCCESS;
-}
-
-
-/**
- * Get the name of cipher at priority level passed in.
- */
-char* wolfSSL_get_cipher_list(int priority)
-{
- const CipherSuiteInfo* ciphers = GetCipherNames();
-
- if (priority >= GetCipherNamesSize() || priority < 0) {
- return 0;
- }
-
- return (char*)ciphers[priority].name;
-}
-
-
-/**
- * Get the name of cipher at priority level passed in.
- */
-char* wolfSSL_get_cipher_list_ex(WOLFSSL* ssl, int priority)
-{
-
- if (ssl == NULL) {
- return NULL;
- }
- else {
- const char* cipher;
-
- if ((cipher = wolfSSL_get_cipher_name_internal(ssl)) != NULL) {
- if (priority == 0) {
- return (char*)cipher;
- }
- else {
- return NULL;
- }
- }
- else {
- return wolfSSL_get_cipher_list(priority);
- }
- }
-}
-
-
-int wolfSSL_get_ciphers(char* buf, int len)
-{
- const CipherSuiteInfo* ciphers = GetCipherNames();
- int ciphersSz = GetCipherNamesSize();
- int i;
- int cipherNameSz;
-
- if (buf == NULL || len <= 0)
- return BAD_FUNC_ARG;
-
- /* Add each member to the buffer delimited by a : */
- for (i = 0; i < ciphersSz; i++) {
- cipherNameSz = (int)XSTRLEN(ciphers[i].name);
- if (cipherNameSz + 1 < len) {
- XSTRNCPY(buf, ciphers[i].name, len);
- buf += cipherNameSz;
-
- if (i < ciphersSz - 1)
- *buf++ = ':';
- *buf = 0;
-
- len -= cipherNameSz + 1;
- }
- else
- return BUFFER_E;
- }
- return WOLFSSL_SUCCESS;
-}
-
-
-#ifndef NO_ERROR_STRINGS
-/* places a list of all supported cipher suites in TLS_* format into "buf"
- * return WOLFSSL_SUCCESS on success */
-int wolfSSL_get_ciphers_iana(char* buf, int len)
-{
- const CipherSuiteInfo* ciphers = GetCipherNames();
- int ciphersSz = GetCipherNamesSize();
- int i;
- int cipherNameSz;
-
- if (buf == NULL || len <= 0)
- return BAD_FUNC_ARG;
-
- /* Add each member to the buffer delimited by a : */
- for (i = 0; i < ciphersSz; i++) {
- cipherNameSz = (int)XSTRLEN(ciphers[i].name_iana);
- if (cipherNameSz + 1 < len) {
- XSTRNCPY(buf, ciphers[i].name_iana, len);
- buf += cipherNameSz;
-
- if (i < ciphersSz - 1)
- *buf++ = ':';
- *buf = 0;
-
- len -= cipherNameSz + 1;
- }
- else
- return BUFFER_E;
- }
- return WOLFSSL_SUCCESS;
-}
-#endif /* NO_ERROR_STRINGS */
-
-
-const char* wolfSSL_get_shared_ciphers(WOLFSSL* ssl, char* buf, int len)
-{
- const char* cipher;
-
- if (ssl == NULL)
- return NULL;
-
- cipher = wolfSSL_get_cipher_name_iana(ssl);
- len = min(len, (int)(XSTRLEN(cipher) + 1));
- XMEMCPY(buf, cipher, len);
- return buf;
-}
-
-int wolfSSL_get_fd(const WOLFSSL* ssl)
-{
- int fd = -1;
- WOLFSSL_ENTER("SSL_get_fd");
- if (ssl) {
- fd = ssl->rfd;
- }
- WOLFSSL_LEAVE("SSL_get_fd", fd);
- return fd;
-}
-
-
-int wolfSSL_dtls(WOLFSSL* ssl)
-{
- int dtlsOpt = 0;
- if (ssl)
- dtlsOpt = ssl->options.dtls;
- return dtlsOpt;
-}
-
-#if !defined(NO_CERTS)
-/* Set whether mutual authentication is required for connections.
- * Server side only.
- *
- * ctx The SSL/TLS CTX object.
- * req 1 to indicate required and 0 when not.
- * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and
- * 0 on success.
- */
-int wolfSSL_CTX_mutual_auth(WOLFSSL_CTX* ctx, int req)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
- if (ctx->method->side == WOLFSSL_CLIENT_END)
- return SIDE_ERROR;
-
- ctx->mutualAuth = (byte)req;
-
- return 0;
-}
-
-/* Set whether mutual authentication is required for the connection.
- * Server side only.
- *
- * ssl The SSL/TLS object.
- * req 1 to indicate required and 0 when not.
- * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3,
- * SIDE_ERROR when not a client and 0 on success.
- */
-int wolfSSL_mutual_auth(WOLFSSL* ssl, int req)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
- if (ssl->options.side == WOLFSSL_SERVER_END)
- return SIDE_ERROR;
-
- ssl->options.mutualAuth = (word16)req;
-
- return 0;
-}
-#endif /* NO_CERTS */
-
-#ifndef WOLFSSL_LEANPSK
-int wolfSSL_dtls_set_peer(WOLFSSL* ssl, void* peer, unsigned int peerSz)
-{
-#ifdef WOLFSSL_DTLS
- void* sa;
-
- if (ssl == NULL)
- return WOLFSSL_FAILURE;
-
- sa = (void*)XMALLOC(peerSz, ssl->heap, DYNAMIC_TYPE_SOCKADDR);
- if (sa != NULL) {
- if (ssl->buffers.dtlsCtx.peer.sa != NULL) {
- XFREE(ssl->buffers.dtlsCtx.peer.sa,ssl->heap,DYNAMIC_TYPE_SOCKADDR);
- ssl->buffers.dtlsCtx.peer.sa = NULL;
- }
- XMEMCPY(sa, peer, peerSz);
- ssl->buffers.dtlsCtx.peer.sa = sa;
- ssl->buffers.dtlsCtx.peer.sz = peerSz;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-#else
- (void)ssl;
- (void)peer;
- (void)peerSz;
- return WOLFSSL_NOT_IMPLEMENTED;
-#endif
-}
-
-int wolfSSL_dtls_get_peer(WOLFSSL* ssl, void* peer, unsigned int* peerSz)
-{
-#ifdef WOLFSSL_DTLS
- if (ssl == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- if (peer != NULL && peerSz != NULL
- && *peerSz >= ssl->buffers.dtlsCtx.peer.sz
- && ssl->buffers.dtlsCtx.peer.sa != NULL) {
- *peerSz = ssl->buffers.dtlsCtx.peer.sz;
- XMEMCPY(peer, ssl->buffers.dtlsCtx.peer.sa, *peerSz);
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-#else
- (void)ssl;
- (void)peer;
- (void)peerSz;
- return WOLFSSL_NOT_IMPLEMENTED;
-#endif
-}
-
-
-#if defined(WOLFSSL_SCTP) && defined(WOLFSSL_DTLS)
-
-int wolfSSL_CTX_dtls_set_sctp(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_dtls_set_sctp()");
-
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- ctx->dtlsSctp = 1;
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_dtls_set_sctp(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_dtls_set_sctp()");
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ssl->options.dtlsSctp = 1;
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* WOLFSSL_DTLS && WOLFSSL_SCTP */
-
-#if (defined(WOLFSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)) && \
- defined(WOLFSSL_DTLS)
-
-int wolfSSL_CTX_dtls_set_mtu(WOLFSSL_CTX* ctx, word16 newMtu)
-{
- if (ctx == NULL || newMtu > MAX_RECORD_SIZE)
- return BAD_FUNC_ARG;
-
- ctx->dtlsMtuSz = newMtu;
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_dtls_set_mtu(WOLFSSL* ssl, word16 newMtu)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- if (newMtu > MAX_RECORD_SIZE) {
- ssl->error = BAD_FUNC_ARG;
- return WOLFSSL_FAILURE;
- }
-
- ssl->dtlsMtuSz = newMtu;
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* WOLFSSL_DTLS && (WOLFSSL_SCTP || WOLFSSL_DTLS_MTU) */
-
-
-#ifdef WOLFSSL_DTLS_DROP_STATS
-
-int wolfSSL_dtls_get_drop_stats(WOLFSSL* ssl,
- word32* macDropCount, word32* replayDropCount)
-{
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_dtls_get_drop_stats()");
-
- if (ssl == NULL)
- ret = BAD_FUNC_ARG;
- else {
- ret = WOLFSSL_SUCCESS;
- if (macDropCount != NULL)
- *macDropCount = ssl->macDropCount;
- if (replayDropCount != NULL)
- *replayDropCount = ssl->replayDropCount;
- }
-
- WOLFSSL_LEAVE("wolfSSL_dtls_get_drop_stats()", ret);
- return ret;
-}
-
-#endif /* WOLFSSL_DTLS_DROP_STATS */
-
-
-#if defined(WOLFSSL_MULTICAST)
-
-int wolfSSL_CTX_mcast_set_member_id(WOLFSSL_CTX* ctx, word16 id)
-{
- int ret = 0;
-
- WOLFSSL_ENTER("wolfSSL_CTX_mcast_set_member_id()");
-
- if (ctx == NULL || id > 255)
- ret = BAD_FUNC_ARG;
-
- if (ret == 0) {
- ctx->haveEMS = 0;
- ctx->haveMcast = 1;
- ctx->mcastID = (byte)id;
-#ifndef WOLFSSL_USER_IO
- ctx->CBIORecv = EmbedReceiveFromMcast;
-#endif /* WOLFSSL_USER_IO */
-
- ret = WOLFSSL_SUCCESS;
- }
- WOLFSSL_LEAVE("wolfSSL_CTX_mcast_set_member_id()", ret);
- return ret;
-}
-
-int wolfSSL_mcast_get_max_peers(void)
-{
- return WOLFSSL_MULTICAST_PEERS;
-}
-
-#ifdef WOLFSSL_DTLS
-static WC_INLINE word32 UpdateHighwaterMark(word32 cur, word32 first,
- word32 second, word32 max)
-{
- word32 newCur = 0;
-
- if (cur < first)
- newCur = first;
- else if (cur < second)
- newCur = second;
- else if (cur < max)
- newCur = max;
-
- return newCur;
-}
-#endif /* WOLFSSL_DTLS */
-
-
-int wolfSSL_set_secret(WOLFSSL* ssl, word16 epoch,
- const byte* preMasterSecret, word32 preMasterSz,
- const byte* clientRandom, const byte* serverRandom,
- const byte* suite)
-{
- int ret = 0;
-
- WOLFSSL_ENTER("wolfSSL_set_secret()");
-
- if (ssl == NULL || preMasterSecret == NULL ||
- preMasterSz == 0 || preMasterSz > ENCRYPT_LEN ||
- clientRandom == NULL || serverRandom == NULL || suite == NULL) {
-
- ret = BAD_FUNC_ARG;
- }
-
- if (ret == 0) {
- XMEMCPY(ssl->arrays->preMasterSecret, preMasterSecret, preMasterSz);
- ssl->arrays->preMasterSz = preMasterSz;
- XMEMCPY(ssl->arrays->clientRandom, clientRandom, RAN_LEN);
- XMEMCPY(ssl->arrays->serverRandom, serverRandom, RAN_LEN);
- ssl->options.cipherSuite0 = suite[0];
- ssl->options.cipherSuite = suite[1];
-
- ret = SetCipherSpecs(ssl);
- }
-
- if (ret == 0)
- ret = MakeTlsMasterSecret(ssl);
-
- if (ret == 0) {
- ssl->keys.encryptionOn = 1;
- ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE);
- }
-
- if (ret == 0) {
- if (ssl->options.dtls) {
- #ifdef WOLFSSL_DTLS
- WOLFSSL_DTLS_PEERSEQ* peerSeq;
- int i;
-
- ssl->keys.dtls_epoch = epoch;
- for (i = 0, peerSeq = ssl->keys.peerSeq;
- i < WOLFSSL_DTLS_PEERSEQ_SZ;
- i++, peerSeq++) {
-
- peerSeq->nextEpoch = epoch;
- peerSeq->prevSeq_lo = peerSeq->nextSeq_lo;
- peerSeq->prevSeq_hi = peerSeq->nextSeq_hi;
- peerSeq->nextSeq_lo = 0;
- peerSeq->nextSeq_hi = 0;
- XMEMCPY(peerSeq->prevWindow, peerSeq->window, DTLS_SEQ_SZ);
- XMEMSET(peerSeq->window, 0, DTLS_SEQ_SZ);
- peerSeq->highwaterMark = UpdateHighwaterMark(0,
- ssl->ctx->mcastFirstSeq,
- ssl->ctx->mcastSecondSeq,
- ssl->ctx->mcastMaxSeq);
- }
- #else
- (void)epoch;
- #endif
- }
- FreeHandshakeResources(ssl);
- ret = WOLFSSL_SUCCESS;
- }
- else {
- if (ssl)
- ssl->error = ret;
- ret = WOLFSSL_FATAL_ERROR;
- }
- WOLFSSL_LEAVE("wolfSSL_set_secret()", ret);
- return ret;
-}
-
-
-#ifdef WOLFSSL_DTLS
-
-int wolfSSL_mcast_peer_add(WOLFSSL* ssl, word16 peerId, int remove)
-{
- WOLFSSL_DTLS_PEERSEQ* p = NULL;
- int ret = WOLFSSL_SUCCESS;
- int i;
-
- WOLFSSL_ENTER("wolfSSL_mcast_peer_add()");
- if (ssl == NULL || peerId > 255)
- return BAD_FUNC_ARG;
-
- if (!remove) {
- /* Make sure it isn't already present, while keeping the first
- * open spot. */
- for (i = 0; i < WOLFSSL_DTLS_PEERSEQ_SZ; i++) {
- if (ssl->keys.peerSeq[i].peerId == INVALID_PEER_ID)
- p = &ssl->keys.peerSeq[i];
- if (ssl->keys.peerSeq[i].peerId == peerId) {
- WOLFSSL_MSG("Peer ID already in multicast peer list.");
- p = NULL;
- }
- }
-
- if (p != NULL) {
- XMEMSET(p, 0, sizeof(WOLFSSL_DTLS_PEERSEQ));
- p->peerId = peerId;
- p->highwaterMark = UpdateHighwaterMark(0,
- ssl->ctx->mcastFirstSeq,
- ssl->ctx->mcastSecondSeq,
- ssl->ctx->mcastMaxSeq);
- }
- else {
- WOLFSSL_MSG("No room in peer list.");
- ret = -1;
- }
- }
- else {
- for (i = 0; i < WOLFSSL_DTLS_PEERSEQ_SZ; i++) {
- if (ssl->keys.peerSeq[i].peerId == peerId)
- p = &ssl->keys.peerSeq[i];
- }
-
- if (p != NULL) {
- p->peerId = INVALID_PEER_ID;
- }
- else {
- WOLFSSL_MSG("Peer not found in list.");
- }
- }
-
- WOLFSSL_LEAVE("wolfSSL_mcast_peer_add()", ret);
- return ret;
-}
-
-
-/* If peerId is in the list of peers and its last sequence number is non-zero,
- * return 1, otherwise return 0. */
-int wolfSSL_mcast_peer_known(WOLFSSL* ssl, unsigned short peerId)
-{
- int known = 0;
- int i;
-
- WOLFSSL_ENTER("wolfSSL_mcast_peer_known()");
-
- if (ssl == NULL || peerId > 255) {
- return BAD_FUNC_ARG;
- }
-
- for (i = 0; i < WOLFSSL_DTLS_PEERSEQ_SZ; i++) {
- if (ssl->keys.peerSeq[i].peerId == peerId) {
- if (ssl->keys.peerSeq[i].nextSeq_hi ||
- ssl->keys.peerSeq[i].nextSeq_lo) {
-
- known = 1;
- }
- break;
- }
- }
-
- WOLFSSL_LEAVE("wolfSSL_mcast_peer_known()", known);
- return known;
-}
-
-
-int wolfSSL_CTX_mcast_set_highwater_cb(WOLFSSL_CTX* ctx, word32 maxSeq,
- word32 first, word32 second,
- CallbackMcastHighwater cb)
-{
- if (ctx == NULL || (second && first > second) ||
- first > maxSeq || second > maxSeq || cb == NULL) {
-
- return BAD_FUNC_ARG;
- }
-
- ctx->mcastHwCb = cb;
- ctx->mcastFirstSeq = first;
- ctx->mcastSecondSeq = second;
- ctx->mcastMaxSeq = maxSeq;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_mcast_set_highwater_ctx(WOLFSSL* ssl, void* ctx)
-{
- if (ssl == NULL || ctx == NULL)
- return BAD_FUNC_ARG;
-
- ssl->mcastHwCbCtx = ctx;
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* WOLFSSL_DTLS */
-
-#endif /* WOLFSSL_MULTICAST */
-
-
-#endif /* WOLFSSL_LEANPSK */
-
-
-/* return underlying connect or accept, WOLFSSL_SUCCESS on ok */
-int wolfSSL_negotiate(WOLFSSL* ssl)
-{
- int err = WOLFSSL_FATAL_ERROR;
-
- WOLFSSL_ENTER("wolfSSL_negotiate");
-#ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version))
- err = wolfSSL_accept_TLSv13(ssl);
- else
-#endif
- err = wolfSSL_accept(ssl);
- }
-#endif
-
-#ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version))
- err = wolfSSL_connect_TLSv13(ssl);
- else
-#endif
- err = wolfSSL_connect(ssl);
- }
-#endif
-
- (void)ssl;
-
- WOLFSSL_LEAVE("wolfSSL_negotiate", err);
-
- return err;
-}
-
-
-WOLFSSL_ABI
-WC_RNG* wolfSSL_GetRNG(WOLFSSL* ssl)
-{
- if (ssl) {
- return ssl->rng;
- }
-
- return NULL;
-}
-
-
-#ifndef WOLFSSL_LEANPSK
-/* object size based on build */
-int wolfSSL_GetObjectSize(void)
-{
-#ifdef SHOW_SIZES
- printf("sizeof suites = %lu\n", (unsigned long)sizeof(Suites));
- printf("sizeof ciphers(2) = %lu\n", (unsigned long)sizeof(Ciphers));
-#ifndef NO_RC4
- printf("\tsizeof arc4 = %lu\n", (unsigned long)sizeof(Arc4));
-#endif
- printf("\tsizeof aes = %lu\n", (unsigned long)sizeof(Aes));
-#ifndef NO_DES3
- printf("\tsizeof des3 = %lu\n", (unsigned long)sizeof(Des3));
-#endif
-#ifndef NO_RABBIT
- printf("\tsizeof rabbit = %lu\n", (unsigned long)sizeof(Rabbit));
-#endif
-#ifdef HAVE_CHACHA
- printf("\tsizeof chacha = %lu\n", (unsigned long)sizeof(ChaCha));
-#endif
- printf("sizeof cipher specs = %lu\n", (unsigned long)sizeof(CipherSpecs));
- printf("sizeof keys = %lu\n", (unsigned long)sizeof(Keys));
- printf("sizeof Hashes(2) = %lu\n", (unsigned long)sizeof(Hashes));
-#ifndef NO_MD5
- printf("\tsizeof MD5 = %lu\n", (unsigned long)sizeof(wc_Md5));
-#endif
-#ifndef NO_SHA
- printf("\tsizeof SHA = %lu\n", (unsigned long)sizeof(wc_Sha));
-#endif
-#ifdef WOLFSSL_SHA224
- printf("\tsizeof SHA224 = %lu\n", (unsigned long)sizeof(wc_Sha224));
-#endif
-#ifndef NO_SHA256
- printf("\tsizeof SHA256 = %lu\n", (unsigned long)sizeof(wc_Sha256));
-#endif
-#ifdef WOLFSSL_SHA384
- printf("\tsizeof SHA384 = %lu\n", (unsigned long)sizeof(wc_Sha384));
-#endif
-#ifdef WOLFSSL_SHA384
- printf("\tsizeof SHA512 = %lu\n", (unsigned long)sizeof(wc_Sha512));
-#endif
- printf("sizeof Buffers = %lu\n", (unsigned long)sizeof(Buffers));
- printf("sizeof Options = %lu\n", (unsigned long)sizeof(Options));
- printf("sizeof Arrays = %lu\n", (unsigned long)sizeof(Arrays));
-#ifndef NO_RSA
- printf("sizeof RsaKey = %lu\n", (unsigned long)sizeof(RsaKey));
-#endif
-#ifdef HAVE_ECC
- printf("sizeof ecc_key = %lu\n", (unsigned long)sizeof(ecc_key));
-#endif
- printf("sizeof WOLFSSL_CIPHER = %lu\n", (unsigned long)sizeof(WOLFSSL_CIPHER));
- printf("sizeof WOLFSSL_SESSION = %lu\n", (unsigned long)sizeof(WOLFSSL_SESSION));
- printf("sizeof WOLFSSL = %lu\n", (unsigned long)sizeof(WOLFSSL));
- printf("sizeof WOLFSSL_CTX = %lu\n", (unsigned long)sizeof(WOLFSSL_CTX));
-#endif
-
- return sizeof(WOLFSSL);
-}
-
-int wolfSSL_CTX_GetObjectSize(void)
-{
- return sizeof(WOLFSSL_CTX);
-}
-
-int wolfSSL_METHOD_GetObjectSize(void)
-{
- return sizeof(WOLFSSL_METHOD);
-}
-#endif
-
-
-#ifdef WOLFSSL_STATIC_MEMORY
-
-int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx, wolfSSL_method_func method,
- unsigned char* buf, unsigned int sz,
- int flag, int max)
-{
- WOLFSSL_HEAP* heap;
- WOLFSSL_HEAP_HINT* hint;
- word32 idx = 0;
-
- if (ctx == NULL || buf == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (*ctx == NULL && method == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (*ctx == NULL || (*ctx)->heap == NULL) {
- if (sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT) > sz - idx) {
- return BUFFER_E; /* not enough memory for structures */
- }
- heap = (WOLFSSL_HEAP*)buf;
- idx += sizeof(WOLFSSL_HEAP);
- if (wolfSSL_init_memory_heap(heap) != 0) {
- return WOLFSSL_FAILURE;
- }
- hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
- idx += sizeof(WOLFSSL_HEAP_HINT);
- XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
- hint->memory = heap;
-
- if (*ctx && (*ctx)->heap == NULL) {
- (*ctx)->heap = (void*)hint;
- }
- }
- else {
-#ifdef WOLFSSL_HEAP_TEST
- /* do not load in memory if test has been set */
- if ((*ctx)->heap == (void*)WOLFSSL_HEAP_TEST) {
- return WOLFSSL_SUCCESS;
- }
-#endif
- hint = (WOLFSSL_HEAP_HINT*)((*ctx)->heap);
- heap = hint->memory;
- }
-
- if (wolfSSL_load_static_memory(buf + idx, sz - idx, flag, heap) != 1) {
- WOLFSSL_MSG("Error partitioning memory");
- return WOLFSSL_FAILURE;
- }
-
- /* create ctx if needed */
- if (*ctx == NULL) {
- *ctx = wolfSSL_CTX_new_ex(method(hint), hint);
- if (*ctx == NULL) {
- WOLFSSL_MSG("Error creating ctx");
- return WOLFSSL_FAILURE;
- }
- }
-
- /* determine what max applies too */
- if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
- heap->maxIO = max;
- }
- else { /* general memory used in handshakes */
- heap->maxHa = max;
- }
-
- heap->flag |= flag;
-
- (void)max;
- (void)method;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats)
-{
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
- WOLFSSL_ENTER("wolfSSL_is_static_memory");
-
- /* fill out statistics if wanted and WOLFMEM_TRACK_STATS flag */
- if (mem_stats != NULL && ssl->heap != NULL) {
- WOLFSSL_HEAP_HINT* hint = ((WOLFSSL_HEAP_HINT*)(ssl->heap));
- WOLFSSL_HEAP* heap = hint->memory;
- if (heap->flag & WOLFMEM_TRACK_STATS && hint->stats != NULL) {
- XMEMCPY(mem_stats, hint->stats, sizeof(WOLFSSL_MEM_CONN_STATS));
- }
- }
-
- return (ssl->heap) ? 1 : 0;
-}
-
-
-int wolfSSL_CTX_is_static_memory(WOLFSSL_CTX* ctx, WOLFSSL_MEM_STATS* mem_stats)
-{
- if (ctx == NULL) {
- return BAD_FUNC_ARG;
- }
- WOLFSSL_ENTER("wolfSSL_CTX_is_static_memory");
-
- /* fill out statistics if wanted */
- if (mem_stats != NULL && ctx->heap != NULL) {
- WOLFSSL_HEAP* heap = ((WOLFSSL_HEAP_HINT*)(ctx->heap))->memory;
- if (wolfSSL_GetMemStats(heap, mem_stats) != 1) {
- return MEMORY_E;
- }
- }
-
- return (ctx->heap) ? 1 : 0;
-}
-
-#endif /* WOLFSSL_STATIC_MEMORY */
-
-
-/* return max record layer size plaintext input size */
-int wolfSSL_GetMaxOutputSize(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_GetMaxOutputSize");
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- if (ssl->options.handShakeState != HANDSHAKE_DONE) {
- WOLFSSL_MSG("Handshake not complete yet");
- return BAD_FUNC_ARG;
- }
-
- return wolfSSL_GetMaxRecordSize(ssl, OUTPUT_RECORD_SIZE);
-}
-
-
-/* return record layer size of plaintext input size */
-int wolfSSL_GetOutputSize(WOLFSSL* ssl, int inSz)
-{
- int maxSize;
-
- WOLFSSL_ENTER("wolfSSL_GetOutputSize");
-
- if (inSz < 0)
- return BAD_FUNC_ARG;
-
- maxSize = wolfSSL_GetMaxOutputSize(ssl);
- if (maxSize < 0)
- return maxSize; /* error */
- if (inSz > maxSize)
- return INPUT_SIZE_E;
-
- return BuildMessage(ssl, NULL, 0, NULL, inSz, application_data, 0, 1, 0);
-}
-
-
-#ifdef HAVE_ECC
-int wolfSSL_CTX_SetMinEccKey_Sz(WOLFSSL_CTX* ctx, short keySz)
-{
- if (ctx == NULL || keySz < 0 || keySz % 8 != 0) {
- WOLFSSL_MSG("Key size must be divisible by 8 or ctx was null");
- return BAD_FUNC_ARG;
- }
-
- ctx->minEccKeySz = keySz / 8;
-#ifndef NO_CERTS
- ctx->cm->minEccKeySz = keySz / 8;
-#endif
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_SetMinEccKey_Sz(WOLFSSL* ssl, short keySz)
-{
- if (ssl == NULL || keySz < 0 || keySz % 8 != 0) {
- WOLFSSL_MSG("Key size must be divisible by 8 or ssl was null");
- return BAD_FUNC_ARG;
- }
-
- ssl->options.minEccKeySz = keySz / 8;
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* !NO_RSA */
-
-#ifndef NO_RSA
-int wolfSSL_CTX_SetMinRsaKey_Sz(WOLFSSL_CTX* ctx, short keySz)
-{
- if (ctx == NULL || keySz < 0 || keySz % 8 != 0) {
- WOLFSSL_MSG("Key size must be divisible by 8 or ctx was null");
- return BAD_FUNC_ARG;
- }
-
- ctx->minRsaKeySz = keySz / 8;
- ctx->cm->minRsaKeySz = keySz / 8;
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_SetMinRsaKey_Sz(WOLFSSL* ssl, short keySz)
-{
- if (ssl == NULL || keySz < 0 || keySz % 8 != 0) {
- WOLFSSL_MSG("Key size must be divisible by 8 or ssl was null");
- return BAD_FUNC_ARG;
- }
-
- ssl->options.minRsaKeySz = keySz / 8;
- return WOLFSSL_SUCCESS;
-}
-#endif /* !NO_RSA */
-
-#ifndef NO_DH
-/* server Diffie-Hellman parameters, WOLFSSL_SUCCESS on ok */
-int wolfSSL_SetTmpDH(WOLFSSL* ssl, const unsigned char* p, int pSz,
- const unsigned char* g, int gSz)
-{
- WOLFSSL_ENTER("wolfSSL_SetTmpDH");
-
- if (ssl == NULL || p == NULL || g == NULL)
- return BAD_FUNC_ARG;
-
- if ((word16)pSz < ssl->options.minDhKeySz)
- return DH_KEY_SIZE_E;
- if ((word16)pSz > ssl->options.maxDhKeySz)
- return DH_KEY_SIZE_E;
-
- /* this function is for server only */
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- return SIDE_ERROR;
-
- #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
- !defined(HAVE_SELFTEST)
- ssl->options.dhKeyTested = 0;
- ssl->options.dhDoKeyTest = 1;
- #endif
-
- if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
- XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ssl->buffers.serverDH_P.buffer = NULL;
- }
- if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
- XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ssl->buffers.serverDH_G.buffer = NULL;
- }
-
- ssl->buffers.weOwnDH = 1; /* SSL owns now */
- ssl->buffers.serverDH_P.buffer = (byte*)XMALLOC(pSz, ssl->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (ssl->buffers.serverDH_P.buffer == NULL)
- return MEMORY_E;
-
- ssl->buffers.serverDH_G.buffer = (byte*)XMALLOC(gSz, ssl->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (ssl->buffers.serverDH_G.buffer == NULL) {
- XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ssl->buffers.serverDH_P.buffer = NULL;
- return MEMORY_E;
- }
-
- ssl->buffers.serverDH_P.length = pSz;
- ssl->buffers.serverDH_G.length = gSz;
-
- XMEMCPY(ssl->buffers.serverDH_P.buffer, p, pSz);
- XMEMCPY(ssl->buffers.serverDH_G.buffer, g, gSz);
-
- ssl->options.haveDH = 1;
-
- if (ssl->options.side != WOLFSSL_NEITHER_END) {
- word16 havePSK;
- word16 haveRSA;
- int keySz = 0;
-
- #ifndef NO_PSK
- havePSK = ssl->options.havePSK;
- #else
- havePSK = 0;
- #endif
- #ifdef NO_RSA
- haveRSA = 0;
- #else
- haveRSA = 1;
- #endif
- #ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
- #endif
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
- }
-
- WOLFSSL_LEAVE("wolfSSL_SetTmpDH", 0);
-
- return WOLFSSL_SUCCESS;
-}
-
-
-#if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
- !defined(HAVE_SELFTEST)
-/* Enables or disables the session's DH key prime test. */
-int wolfSSL_SetEnableDhKeyTest(WOLFSSL* ssl, int enable)
-{
- WOLFSSL_ENTER("wolfSSL_SetEnableDhKeyTest");
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- if (!enable)
- ssl->options.dhDoKeyTest = 0;
- else
- ssl->options.dhDoKeyTest = 1;
-
- WOLFSSL_LEAVE("wolfSSL_SetEnableDhKeyTest", WOLFSSL_SUCCESS);
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-
-/* server ctx Diffie-Hellman parameters, WOLFSSL_SUCCESS on ok */
-int wolfSSL_CTX_SetTmpDH(WOLFSSL_CTX* ctx, const unsigned char* p, int pSz,
- const unsigned char* g, int gSz)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_SetTmpDH");
- if (ctx == NULL || p == NULL || g == NULL) return BAD_FUNC_ARG;
-
- if ((word16)pSz < ctx->minDhKeySz)
- return DH_KEY_SIZE_E;
- if ((word16)pSz > ctx->maxDhKeySz)
- return DH_KEY_SIZE_E;
-
- #if !defined(WOLFSSL_OLD_PRIME_CHECK) && !defined(HAVE_FIPS) && \
- !defined(HAVE_SELFTEST)
- {
- DhKey checkKey;
- WC_RNG rng;
- int error, freeKey = 0;
-
- error = wc_InitRng(&rng);
- if (!error)
- error = wc_InitDhKey(&checkKey);
- if (!error) {
- freeKey = 1;
- error = wc_DhSetCheckKey(&checkKey,
- p, pSz, g, gSz, NULL, 0, 0, &rng);
- }
- if (freeKey)
- wc_FreeDhKey(&checkKey);
- wc_FreeRng(&rng);
- if (error)
- return error;
-
- ctx->dhKeyTested = 1;
- }
- #endif
-
- XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ctx->serverDH_P.buffer = NULL;
- XFREE(ctx->serverDH_G.buffer, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ctx->serverDH_G.buffer = NULL;
-
- ctx->serverDH_P.buffer = (byte*)XMALLOC(pSz, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (ctx->serverDH_P.buffer == NULL)
- return MEMORY_E;
-
- ctx->serverDH_G.buffer = (byte*)XMALLOC(gSz, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (ctx->serverDH_G.buffer == NULL) {
- XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- ctx->serverDH_P.buffer = NULL;
- return MEMORY_E;
- }
-
- ctx->serverDH_P.length = pSz;
- ctx->serverDH_G.length = gSz;
-
- XMEMCPY(ctx->serverDH_P.buffer, p, pSz);
- XMEMCPY(ctx->serverDH_G.buffer, g, gSz);
-
- ctx->haveDH = 1;
-
- WOLFSSL_LEAVE("wolfSSL_CTX_SetTmpDH", 0);
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_CTX_SetMinDhKey_Sz(WOLFSSL_CTX* ctx, word16 keySz)
-{
- if (ctx == NULL || keySz > 16000 || keySz % 8 != 0)
- return BAD_FUNC_ARG;
-
- ctx->minDhKeySz = keySz / 8;
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_SetMinDhKey_Sz(WOLFSSL* ssl, word16 keySz)
-{
- if (ssl == NULL || keySz > 16000 || keySz % 8 != 0)
- return BAD_FUNC_ARG;
-
- ssl->options.minDhKeySz = keySz / 8;
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_CTX_SetMaxDhKey_Sz(WOLFSSL_CTX* ctx, word16 keySz)
-{
- if (ctx == NULL || keySz > 16000 || keySz % 8 != 0)
- return BAD_FUNC_ARG;
-
- ctx->maxDhKeySz = keySz / 8;
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_SetMaxDhKey_Sz(WOLFSSL* ssl, word16 keySz)
-{
- if (ssl == NULL || keySz > 16000 || keySz % 8 != 0)
- return BAD_FUNC_ARG;
-
- ssl->options.maxDhKeySz = keySz / 8;
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_GetDhKey_Sz(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return (ssl->options.dhKeySz * 8);
-}
-
-#endif /* !NO_DH */
-
-
-WOLFSSL_ABI
-int wolfSSL_write(WOLFSSL* ssl, const void* data, int sz)
-{
- int ret;
-
- WOLFSSL_ENTER("SSL_write()");
-
- if (ssl == NULL || data == NULL || sz < 0)
- return BAD_FUNC_ARG;
-
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data && (ret = wolfSSL_negotiate(ssl)) < 0) {
- ssl->error = ret;
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->earlyData = no_early_data;
-#endif
-
-#ifdef HAVE_WRITE_DUP
- { /* local variable scope */
- int dupErr = 0; /* local copy */
-
- ret = 0;
-
- if (ssl->dupWrite && ssl->dupSide == READ_DUP_SIDE) {
- WOLFSSL_MSG("Read dup side cannot write");
- return WRITE_DUP_WRITE_E;
- }
- if (ssl->dupWrite) {
- if (wc_LockMutex(&ssl->dupWrite->dupMutex) != 0) {
- return BAD_MUTEX_E;
- }
- dupErr = ssl->dupWrite->dupErr;
- ret = wc_UnLockMutex(&ssl->dupWrite->dupMutex);
- }
-
- if (ret != 0) {
- ssl->error = ret; /* high priority fatal error */
- return WOLFSSL_FATAL_ERROR;
- }
- if (dupErr != 0) {
- WOLFSSL_MSG("Write dup error from other side");
- ssl->error = dupErr;
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif
-
-#ifdef HAVE_ERRNO_H
- errno = 0;
-#endif
-
- #ifdef OPENSSL_EXTRA
- if (ssl->CBIS != NULL) {
- ssl->CBIS(ssl, SSL_CB_WRITE, SSL_SUCCESS);
- ssl->cbmode = SSL_CB_WRITE;
- }
- #endif
- ret = SendData(ssl, data, sz);
-
- WOLFSSL_LEAVE("SSL_write()", ret);
-
- if (ret < 0)
- return WOLFSSL_FATAL_ERROR;
- else
- return ret;
-}
-
-static int wolfSSL_read_internal(WOLFSSL* ssl, void* data, int sz, int peek)
-{
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_read_internal()");
-
- if (ssl == NULL || data == NULL || sz < 0)
- return BAD_FUNC_ARG;
-
-#ifdef HAVE_WRITE_DUP
- if (ssl->dupWrite && ssl->dupSide == WRITE_DUP_SIDE) {
- WOLFSSL_MSG("Write dup side cannot read");
- return WRITE_DUP_READ_E;
- }
-#endif
-
-#ifdef HAVE_ERRNO_H
- errno = 0;
-#endif
-
-#ifdef WOLFSSL_DTLS
- if (ssl->options.dtls) {
- ssl->dtls_expected_rx = max(sz + 100, MAX_MTU);
-#ifdef WOLFSSL_SCTP
- if (ssl->options.dtlsSctp)
-#endif
-#if defined(WOLLSSL_SCTP) || defined(WOLFSSL_DTLS_MTU)
- ssl->dtls_expected_rx = max(ssl->dtls_expected_rx, ssl->dtlsMtuSz);
-#endif
- }
-#endif
-
- sz = wolfSSL_GetMaxRecordSize(ssl, sz);
-
- ret = ReceiveData(ssl, (byte*)data, sz, peek);
-
-#ifdef HAVE_WRITE_DUP
- if (ssl->dupWrite) {
- if (ssl->error != 0 && ssl->error != WANT_READ
- #ifdef WOLFSSL_ASYNC_CRYPT
- && ssl->error != WC_PENDING_E
- #endif
- ) {
- int notifyErr;
-
- WOLFSSL_MSG("Notifying write side of fatal read error");
- notifyErr = NotifyWriteSide(ssl, ssl->error);
- if (notifyErr < 0) {
- ret = ssl->error = notifyErr;
- }
- }
- }
-#endif
-
- WOLFSSL_LEAVE("wolfSSL_read_internal()", ret);
-
- if (ret < 0)
- return WOLFSSL_FATAL_ERROR;
- else
- return ret;
-}
-
-
-int wolfSSL_peek(WOLFSSL* ssl, void* data, int sz)
-{
- WOLFSSL_ENTER("wolfSSL_peek()");
-
- return wolfSSL_read_internal(ssl, data, sz, TRUE);
-}
-
-
-WOLFSSL_ABI
-int wolfSSL_read(WOLFSSL* ssl, void* data, int sz)
-{
- WOLFSSL_ENTER("wolfSSL_read()");
-
- #ifdef OPENSSL_EXTRA
- if (ssl->CBIS != NULL) {
- ssl->CBIS(ssl, SSL_CB_READ, SSL_SUCCESS);
- ssl->cbmode = SSL_CB_READ;
- }
- #endif
- return wolfSSL_read_internal(ssl, data, sz, FALSE);
-}
-
-
-#ifdef WOLFSSL_MULTICAST
-
-int wolfSSL_mcast_read(WOLFSSL* ssl, word16* id, void* data, int sz)
-{
- int ret = 0;
-
- WOLFSSL_ENTER("wolfSSL_mcast_read()");
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ret = wolfSSL_read_internal(ssl, data, sz, FALSE);
- if (ssl->options.dtls && ssl->options.haveMcast && id != NULL)
- *id = ssl->keys.curPeerId;
- return ret;
-}
-
-#endif /* WOLFSSL_MULTICAST */
-
-
-/* helpers to set the device id, WOLFSSL_SUCCESS on ok */
-WOLFSSL_ABI
-int wolfSSL_SetDevId(WOLFSSL* ssl, int devId)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ssl->devId = devId;
-
- return WOLFSSL_SUCCESS;
-}
-
-WOLFSSL_ABI
-int wolfSSL_CTX_SetDevId(WOLFSSL_CTX* ctx, int devId)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- ctx->devId = devId;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* helpers to get device id and heap */
-WOLFSSL_ABI
-int wolfSSL_CTX_GetDevId(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
-{
- int devId = INVALID_DEVID;
- if (ctx != NULL)
- devId = ctx->devId;
- else if (ssl != NULL)
- devId = ssl->devId;
- return devId;
-}
-void* wolfSSL_CTX_GetHeap(WOLFSSL_CTX* ctx, WOLFSSL* ssl)
-{
- void* heap = NULL;
- if (ctx != NULL)
- heap = ctx->heap;
- else if (ssl != NULL)
- heap = ssl->heap;
- return heap;
-}
-
-
-#ifdef HAVE_SNI
-
-WOLFSSL_ABI
-int wolfSSL_UseSNI(WOLFSSL* ssl, byte type, const void* data, word16 size)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return TLSX_UseSNI(&ssl->extensions, type, data, size, ssl->heap);
-}
-
-
-WOLFSSL_ABI
-int wolfSSL_CTX_UseSNI(WOLFSSL_CTX* ctx, byte type, const void* data,
- word16 size)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- return TLSX_UseSNI(&ctx->extensions, type, data, size, ctx->heap);
-}
-
-#ifndef NO_WOLFSSL_SERVER
-
-void wolfSSL_SNI_SetOptions(WOLFSSL* ssl, byte type, byte options)
-{
- if (ssl && ssl->extensions)
- TLSX_SNI_SetOptions(ssl->extensions, type, options);
-}
-
-
-void wolfSSL_CTX_SNI_SetOptions(WOLFSSL_CTX* ctx, byte type, byte options)
-{
- if (ctx && ctx->extensions)
- TLSX_SNI_SetOptions(ctx->extensions, type, options);
-}
-
-
-byte wolfSSL_SNI_Status(WOLFSSL* ssl, byte type)
-{
- return TLSX_SNI_Status(ssl ? ssl->extensions : NULL, type);
-}
-
-
-word16 wolfSSL_SNI_GetRequest(WOLFSSL* ssl, byte type, void** data)
-{
- if (data)
- *data = NULL;
-
- if (ssl && ssl->extensions)
- return TLSX_SNI_GetRequest(ssl->extensions, type, data);
-
- return 0;
-}
-
-
-int wolfSSL_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
- byte type, byte* sni, word32* inOutSz)
-{
- if (clientHello && helloSz > 0 && sni && inOutSz && *inOutSz > 0)
- return TLSX_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz);
-
- return BAD_FUNC_ARG;
-}
-
-#endif /* NO_WOLFSSL_SERVER */
-
-#endif /* HAVE_SNI */
-
-
-#ifdef HAVE_TRUSTED_CA
-
-WOLFSSL_API int wolfSSL_UseTrustedCA(WOLFSSL* ssl, byte type,
- const byte* certId, word32 certIdSz)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- if (type == WOLFSSL_TRUSTED_CA_PRE_AGREED) {
- if (certId != NULL || certIdSz != 0)
- return BAD_FUNC_ARG;
- }
- else if (type == WOLFSSL_TRUSTED_CA_X509_NAME) {
- if (certId == NULL || certIdSz == 0)
- return BAD_FUNC_ARG;
- }
- #ifndef NO_SHA
- else if (type == WOLFSSL_TRUSTED_CA_KEY_SHA1 ||
- type == WOLFSSL_TRUSTED_CA_CERT_SHA1) {
- if (certId == NULL || certIdSz != WC_SHA_DIGEST_SIZE)
- return BAD_FUNC_ARG;
- }
- #endif
- else
- return BAD_FUNC_ARG;
-
- return TLSX_UseTrustedCA(&ssl->extensions,
- type, certId, certIdSz, ssl->heap);
-}
-
-#endif /* HAVE_TRUSTED_CA */
-
-
-#ifdef HAVE_MAX_FRAGMENT
-#ifndef NO_WOLFSSL_CLIENT
-
-int wolfSSL_UseMaxFragment(WOLFSSL* ssl, byte mfl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
-#ifdef WOLFSSL_ALLOW_MAX_FRAGMENT_ADJUST
- /* The following is a non-standard way to reconfigure the max packet size
- post-handshake for wolfSSL_write/woflSSL_read */
- if (ssl->options.handShakeState == HANDSHAKE_DONE) {
- switch (mfl) {
- case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
- case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
- case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
- case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
- case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
- case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
- default: ssl->max_fragment = MAX_RECORD_SIZE; break;
- }
- return WOLFSSL_SUCCESS;
- }
-#endif /* WOLFSSL_MAX_FRAGMENT_ADJUST */
-
- /* This call sets the max fragment TLS extension, which gets sent to server.
- The server_hello response is what sets the `ssl->max_fragment` in
- TLSX_MFL_Parse */
- return TLSX_UseMaxFragment(&ssl->extensions, mfl, ssl->heap);
-}
-
-
-int wolfSSL_CTX_UseMaxFragment(WOLFSSL_CTX* ctx, byte mfl)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- return TLSX_UseMaxFragment(&ctx->extensions, mfl, ctx->heap);
-}
-
-#endif /* NO_WOLFSSL_CLIENT */
-#endif /* HAVE_MAX_FRAGMENT */
-
-#ifdef HAVE_TRUNCATED_HMAC
-#ifndef NO_WOLFSSL_CLIENT
-
-int wolfSSL_UseTruncatedHMAC(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
-}
-
-
-int wolfSSL_CTX_UseTruncatedHMAC(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- return TLSX_UseTruncatedHMAC(&ctx->extensions, ctx->heap);
-}
-
-#endif /* NO_WOLFSSL_CLIENT */
-#endif /* HAVE_TRUNCATED_HMAC */
-
-#ifdef HAVE_CERTIFICATE_STATUS_REQUEST
-
-int wolfSSL_UseOCSPStapling(WOLFSSL* ssl, byte status_type, byte options)
-{
- if (ssl == NULL || ssl->options.side != WOLFSSL_CLIENT_END)
- return BAD_FUNC_ARG;
-
- return TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
- options, NULL, ssl->heap, ssl->devId);
-}
-
-
-int wolfSSL_CTX_UseOCSPStapling(WOLFSSL_CTX* ctx, byte status_type,
- byte options)
-{
- if (ctx == NULL || ctx->method->side != WOLFSSL_CLIENT_END)
- return BAD_FUNC_ARG;
-
- return TLSX_UseCertificateStatusRequest(&ctx->extensions, status_type,
- options, NULL, ctx->heap, ctx->devId);
-}
-
-#endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
-
-#ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
-
-int wolfSSL_UseOCSPStaplingV2(WOLFSSL* ssl, byte status_type, byte options)
-{
- if (ssl == NULL || ssl->options.side != WOLFSSL_CLIENT_END)
- return BAD_FUNC_ARG;
-
- return TLSX_UseCertificateStatusRequestV2(&ssl->extensions, status_type,
- options, ssl->heap, ssl->devId);
-}
-
-
-int wolfSSL_CTX_UseOCSPStaplingV2(WOLFSSL_CTX* ctx, byte status_type,
- byte options)
-{
- if (ctx == NULL || ctx->method->side != WOLFSSL_CLIENT_END)
- return BAD_FUNC_ARG;
-
- return TLSX_UseCertificateStatusRequestV2(&ctx->extensions, status_type,
- options, ctx->heap, ctx->devId);
-}
-
-#endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
-
-/* Elliptic Curves */
-#if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT)
-
-int wolfSSL_UseSupportedCurve(WOLFSSL* ssl, word16 name)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- switch (name) {
- case WOLFSSL_ECC_SECP160K1:
- case WOLFSSL_ECC_SECP160R1:
- case WOLFSSL_ECC_SECP160R2:
- case WOLFSSL_ECC_SECP192K1:
- case WOLFSSL_ECC_SECP192R1:
- case WOLFSSL_ECC_SECP224K1:
- case WOLFSSL_ECC_SECP224R1:
- case WOLFSSL_ECC_SECP256K1:
- case WOLFSSL_ECC_SECP256R1:
- case WOLFSSL_ECC_SECP384R1:
- case WOLFSSL_ECC_SECP521R1:
- case WOLFSSL_ECC_BRAINPOOLP256R1:
- case WOLFSSL_ECC_BRAINPOOLP384R1:
- case WOLFSSL_ECC_BRAINPOOLP512R1:
- case WOLFSSL_ECC_X25519:
- case WOLFSSL_ECC_X448:
-
- case WOLFSSL_FFDHE_2048:
- case WOLFSSL_FFDHE_3072:
- case WOLFSSL_FFDHE_4096:
- case WOLFSSL_FFDHE_6144:
- case WOLFSSL_FFDHE_8192:
- break;
-
- default:
- return BAD_FUNC_ARG;
- }
-
- ssl->options.userCurves = 1;
-
- return TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap);
-}
-
-
-int wolfSSL_CTX_UseSupportedCurve(WOLFSSL_CTX* ctx, word16 name)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- switch (name) {
- case WOLFSSL_ECC_SECP160K1:
- case WOLFSSL_ECC_SECP160R1:
- case WOLFSSL_ECC_SECP160R2:
- case WOLFSSL_ECC_SECP192K1:
- case WOLFSSL_ECC_SECP192R1:
- case WOLFSSL_ECC_SECP224K1:
- case WOLFSSL_ECC_SECP224R1:
- case WOLFSSL_ECC_SECP256K1:
- case WOLFSSL_ECC_SECP256R1:
- case WOLFSSL_ECC_SECP384R1:
- case WOLFSSL_ECC_SECP521R1:
- case WOLFSSL_ECC_BRAINPOOLP256R1:
- case WOLFSSL_ECC_BRAINPOOLP384R1:
- case WOLFSSL_ECC_BRAINPOOLP512R1:
- case WOLFSSL_ECC_X25519:
- case WOLFSSL_ECC_X448:
- case WOLFSSL_FFDHE_2048:
- case WOLFSSL_FFDHE_3072:
- case WOLFSSL_FFDHE_4096:
- case WOLFSSL_FFDHE_6144:
- case WOLFSSL_FFDHE_8192:
- break;
-
- default:
- return BAD_FUNC_ARG;
- }
-
- ctx->userCurves = 1;
-
- return TLSX_UseSupportedCurve(&ctx->extensions, name, ctx->heap);
-}
-
-#endif /* HAVE_SUPPORTED_CURVES && !NO_WOLFSSL_CLIENT */
-
-/* QSH quantum safe handshake */
-#ifdef HAVE_QSH
-/* returns 1 if QSH has been used 0 otherwise */
-int wolfSSL_isQSH(WOLFSSL* ssl)
-{
- /* if no ssl struct than QSH was not used */
- if (ssl == NULL)
- return 0;
-
- return ssl->isQSH;
-}
-
-
-int wolfSSL_UseSupportedQSH(WOLFSSL* ssl, word16 name)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- switch (name) {
- #ifdef HAVE_NTRU
- case WOLFSSL_NTRU_EESS439:
- case WOLFSSL_NTRU_EESS593:
- case WOLFSSL_NTRU_EESS743:
- break;
- #endif
- default:
- return BAD_FUNC_ARG;
- }
-
- ssl->user_set_QSHSchemes = 1;
-
- return TLSX_UseQSHScheme(&ssl->extensions, name, NULL, 0, ssl->heap);
-}
-
-#ifndef NO_WOLFSSL_CLIENT
- /* user control over sending client public key in hello
- when flag = 1 will send keys if flag is 0 or function is not called
- then will not send keys in the hello extension
- return 0 on success
- */
- int wolfSSL_UseClientQSHKeys(WOLFSSL* ssl, unsigned char flag)
- {
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ssl->sendQSHKeys = flag;
-
- return 0;
- }
-#endif /* NO_WOLFSSL_CLIENT */
-#endif /* HAVE_QSH */
-
-/* Application-Layer Protocol Negotiation */
-#ifdef HAVE_ALPN
-
-WOLFSSL_ABI
-int wolfSSL_UseALPN(WOLFSSL* ssl, char *protocol_name_list,
- word32 protocol_name_listSz, byte options)
-{
- char *list, *ptr, *token[WOLFSSL_MAX_ALPN_NUMBER]={NULL};
- word16 len;
- int idx = 0;
- int ret = WOLFSSL_FAILURE;
-
- WOLFSSL_ENTER("wolfSSL_UseALPN");
-
- if (ssl == NULL || protocol_name_list == NULL)
- return BAD_FUNC_ARG;
-
- if (protocol_name_listSz > (WOLFSSL_MAX_ALPN_NUMBER *
- WOLFSSL_MAX_ALPN_PROTO_NAME_LEN +
- WOLFSSL_MAX_ALPN_NUMBER)) {
- WOLFSSL_MSG("Invalid arguments, protocol name list too long");
- return BAD_FUNC_ARG;
- }
-
- if (!(options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) &&
- !(options & WOLFSSL_ALPN_FAILED_ON_MISMATCH)) {
- WOLFSSL_MSG("Invalid arguments, options not supported");
- return BAD_FUNC_ARG;
- }
-
-
- list = (char *)XMALLOC(protocol_name_listSz+1, ssl->heap,
- DYNAMIC_TYPE_ALPN);
- if (list == NULL) {
- WOLFSSL_MSG("Memory failure");
- return MEMORY_ERROR;
- }
-
- XSTRNCPY(list, protocol_name_list, protocol_name_listSz);
- list[protocol_name_listSz] = '\0';
-
- /* read all protocol name from the list */
- token[idx] = XSTRTOK(list, ",", &ptr);
- while (idx < WOLFSSL_MAX_ALPN_NUMBER && token[idx] != NULL)
- token[++idx] = XSTRTOK(NULL, ",", &ptr);
-
- /* add protocol name list in the TLS extension in reverse order */
- while ((idx--) > 0) {
- len = (word16)XSTRLEN(token[idx]);
-
- ret = TLSX_UseALPN(&ssl->extensions, token[idx], len, options,
- ssl->heap);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("TLSX_UseALPN failure");
- break;
- }
- }
-
- XFREE(list, ssl->heap, DYNAMIC_TYPE_ALPN);
-
- return ret;
-}
-
-int wolfSSL_ALPN_GetProtocol(WOLFSSL* ssl, char **protocol_name, word16 *size)
-{
- return TLSX_ALPN_GetRequest(ssl ? ssl->extensions : NULL,
- (void **)protocol_name, size);
-}
-
-int wolfSSL_ALPN_GetPeerProtocol(WOLFSSL* ssl, char **list, word16 *listSz)
-{
- if (list == NULL || listSz == NULL)
- return BAD_FUNC_ARG;
-
- if (ssl->alpn_client_list == NULL)
- return BUFFER_ERROR;
-
- *listSz = (word16)XSTRLEN(ssl->alpn_client_list);
- if (*listSz == 0)
- return BUFFER_ERROR;
-
- *list = (char *)XMALLOC((*listSz)+1, ssl->heap, DYNAMIC_TYPE_TLSX);
- if (*list == NULL)
- return MEMORY_ERROR;
-
- XSTRNCPY(*list, ssl->alpn_client_list, (*listSz)+1);
- (*list)[*listSz] = 0;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* used to free memory allocated by wolfSSL_ALPN_GetPeerProtocol */
-int wolfSSL_ALPN_FreePeerProtocol(WOLFSSL* ssl, char **list)
-{
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- XFREE(*list, ssl->heap, DYNAMIC_TYPE_TLSX);
- *list = NULL;
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* HAVE_ALPN */
-
-/* Secure Renegotiation */
-#ifdef HAVE_SECURE_RENEGOTIATION
-
-/* user is forcing ability to use secure renegotiation, we discourage it */
-int wolfSSL_UseSecureRenegotiation(WOLFSSL* ssl)
-{
- int ret = BAD_FUNC_ARG;
-
- if (ssl)
- ret = TLSX_UseSecureRenegotiation(&ssl->extensions, ssl->heap);
-
- if (ret == WOLFSSL_SUCCESS) {
- TLSX* extension = TLSX_Find(ssl->extensions, TLSX_RENEGOTIATION_INFO);
-
- if (extension)
- ssl->secure_renegotiation = (SecureRenegotiation*)extension->data;
- }
-
- return ret;
-}
-
-int wolfSSL_CTX_UseSecureRenegotiation(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- ctx->useSecureReneg = 1;
- return WOLFSSL_SUCCESS;
-}
-
-
-/* do a secure renegotiation handshake, user forced, we discourage */
-static int _Rehandshake(WOLFSSL* ssl)
-{
- int ret;
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- if (ssl->secure_renegotiation == NULL) {
- WOLFSSL_MSG("Secure Renegotiation not forced on by user");
- return SECURE_RENEGOTIATION_E;
- }
-
- if (ssl->secure_renegotiation->enabled == 0) {
- WOLFSSL_MSG("Secure Renegotiation not enabled at extension level");
- return SECURE_RENEGOTIATION_E;
- }
-
- /* If the client started the renegotiation, the server will already
- * have processed the client's hello. */
- if (ssl->options.side != WOLFSSL_SERVER_END ||
- ssl->options.acceptState != ACCEPT_FIRST_REPLY_DONE) {
-
- if (ssl->options.handShakeState != HANDSHAKE_DONE) {
- WOLFSSL_MSG("Can't renegotiate until previous handshake complete");
- return SECURE_RENEGOTIATION_E;
- }
-
-#ifndef NO_FORCE_SCR_SAME_SUITE
- /* force same suite */
- if (ssl->suites) {
- ssl->suites->suiteSz = SUITE_LEN;
- ssl->suites->suites[0] = ssl->options.cipherSuite0;
- ssl->suites->suites[1] = ssl->options.cipherSuite;
- }
-#endif
-
- /* reset handshake states */
- ssl->options.sendVerify = 0;
- ssl->options.serverState = NULL_STATE;
- ssl->options.clientState = NULL_STATE;
- ssl->options.connectState = CONNECT_BEGIN;
- ssl->options.acceptState = ACCEPT_BEGIN_RENEG;
- ssl->options.handShakeState = NULL_STATE;
- ssl->options.processReply = 0; /* TODO, move states in internal.h */
-
- XMEMSET(&ssl->msgsReceived, 0, sizeof(ssl->msgsReceived));
-
- ssl->secure_renegotiation->cache_status = SCR_CACHE_NEEDED;
-
-#if !defined(NO_WOLFSSL_SERVER) && defined(HAVE_SERVER_RENEGOTIATION_INFO)
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- ret = SendHelloRequest(ssl);
- if (ret != 0) {
- ssl->error = ret;
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif /* NO_WOLFSSL_SERVER && HAVE_SERVER_RENEGOTIATION_INFO */
-
- ret = InitHandshakeHashes(ssl);
- if (ret != 0) {
- ssl->error = ret;
- return WOLFSSL_FATAL_ERROR;
- }
- }
- ret = wolfSSL_negotiate(ssl);
- ssl->secure_rene_count++;
- return ret;
-}
-
-
-/* do a secure renegotiation handshake, user forced, we discourage */
-int wolfSSL_Rehandshake(WOLFSSL* ssl)
-{
- int ret = WOLFSSL_SUCCESS;
- WOLFSSL_ENTER("wolfSSL_Rehandshake");
-
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- /* Reset option to send certificate verify. */
- ssl->options.sendVerify = 0;
- }
- else {
- /* Reset resuming flag to do full secure handshake. */
- ssl->options.resuming = 0;
- #ifdef HAVE_SESSION_TICKET
- /* Clearing the ticket. */
- ret = wolfSSL_UseSessionTicket(ssl);
- #endif
- }
-
- if (ret == WOLFSSL_SUCCESS)
- ret = _Rehandshake(ssl);
-
- return ret;
-}
-
-
-#ifndef NO_WOLFSSL_CLIENT
-
-/* do a secure resumption handshake, user forced, we discourage */
-int wolfSSL_SecureResume(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_SecureResume");
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- ssl->error = SIDE_ERROR;
- return SSL_FATAL_ERROR;
- }
-
- return _Rehandshake(ssl);
-}
-
-#endif /* NO_WOLFSSL_CLIENT */
-
-long wolfSSL_SSL_get_secure_renegotiation_support(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_SSL_get_secure_renegotiation_support");
-
- if (!ssl || !ssl->secure_renegotiation)
- return WOLFSSL_FAILURE;
- return ssl->secure_renegotiation->enabled;
-}
-
-#endif /* HAVE_SECURE_RENEGOTIATION */
-
-/* Session Ticket */
-#if !defined(NO_WOLFSSL_SERVER) && defined(HAVE_SESSION_TICKET)
-/* WOLFSSL_SUCCESS on ok */
-int wolfSSL_CTX_set_TicketEncCb(WOLFSSL_CTX* ctx, SessionTicketEncCb cb)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- ctx->ticketEncCb = cb;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* set hint interval, WOLFSSL_SUCCESS on ok */
-int wolfSSL_CTX_set_TicketHint(WOLFSSL_CTX* ctx, int hint)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- ctx->ticketHint = hint;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* set user context, WOLFSSL_SUCCESS on ok */
-int wolfSSL_CTX_set_TicketEncCtx(WOLFSSL_CTX* ctx, void* userCtx)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- ctx->ticketEncCtx = userCtx;
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* !defined(NO_WOLFSSL_CLIENT) && defined(HAVE_SESSION_TICKET) */
-
-/* Session Ticket */
-#if !defined(NO_WOLFSSL_CLIENT) && defined(HAVE_SESSION_TICKET)
-int wolfSSL_UseSessionTicket(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
-}
-
-int wolfSSL_CTX_UseSessionTicket(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- return TLSX_UseSessionTicket(&ctx->extensions, NULL, ctx->heap);
-}
-
-WOLFSSL_API int wolfSSL_get_SessionTicket(WOLFSSL* ssl,
- byte* buf, word32* bufSz)
-{
- if (ssl == NULL || buf == NULL || bufSz == NULL || *bufSz == 0)
- return BAD_FUNC_ARG;
-
- if (ssl->session.ticketLen <= *bufSz) {
- XMEMCPY(buf, ssl->session.ticket, ssl->session.ticketLen);
- *bufSz = ssl->session.ticketLen;
- }
- else
- *bufSz = 0;
-
- return WOLFSSL_SUCCESS;
-}
-
-WOLFSSL_API int wolfSSL_set_SessionTicket(WOLFSSL* ssl, const byte* buf,
- word32 bufSz)
-{
- if (ssl == NULL || (buf == NULL && bufSz > 0))
- return BAD_FUNC_ARG;
-
- if (bufSz > 0) {
- /* Ticket will fit into static ticket */
- if(bufSz <= SESSION_TICKET_LEN) {
- if (ssl->session.isDynamic) {
- XFREE(ssl->session.ticket, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- ssl->session.isDynamic = 0;
- ssl->session.ticket = ssl->session.staticTicket;
- }
- } else { /* Ticket requires dynamic ticket storage */
- if (ssl->session.ticketLen < bufSz) { /* is dyn buffer big enough */
- if(ssl->session.isDynamic)
- XFREE(ssl->session.ticket, ssl->heap,
- DYNAMIC_TYPE_SESSION_TICK);
- ssl->session.ticket = (byte*)XMALLOC(bufSz, ssl->heap,
- DYNAMIC_TYPE_SESSION_TICK);
- if(!ssl->session.ticket) {
- ssl->session.ticket = ssl->session.staticTicket;
- ssl->session.isDynamic = 0;
- return MEMORY_ERROR;
- }
- ssl->session.isDynamic = 1;
- }
- }
- XMEMCPY(ssl->session.ticket, buf, bufSz);
- }
- ssl->session.ticketLen = (word16)bufSz;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-WOLFSSL_API int wolfSSL_set_SessionTicket_cb(WOLFSSL* ssl,
- CallbackSessionTicket cb, void* ctx)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ssl->session_ticket_cb = cb;
- ssl->session_ticket_ctx = ctx;
-
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-
-#ifdef HAVE_EXTENDED_MASTER
-#ifndef NO_WOLFSSL_CLIENT
-
-int wolfSSL_CTX_DisableExtendedMasterSecret(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- ctx->haveEMS = 0;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_DisableExtendedMasterSecret(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ssl->options.haveEMS = 0;
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif
-#endif
-
-
-#ifndef WOLFSSL_LEANPSK
-
-int wolfSSL_send(WOLFSSL* ssl, const void* data, int sz, int flags)
-{
- int ret;
- int oldFlags;
-
- WOLFSSL_ENTER("wolfSSL_send()");
-
- if (ssl == NULL || data == NULL || sz < 0)
- return BAD_FUNC_ARG;
-
- oldFlags = ssl->wflags;
-
- ssl->wflags = flags;
- ret = wolfSSL_write(ssl, data, sz);
- ssl->wflags = oldFlags;
-
- WOLFSSL_LEAVE("wolfSSL_send()", ret);
-
- return ret;
-}
-
-
-int wolfSSL_recv(WOLFSSL* ssl, void* data, int sz, int flags)
-{
- int ret;
- int oldFlags;
-
- WOLFSSL_ENTER("wolfSSL_recv()");
-
- if (ssl == NULL || data == NULL || sz < 0)
- return BAD_FUNC_ARG;
-
- oldFlags = ssl->rflags;
-
- ssl->rflags = flags;
- ret = wolfSSL_read(ssl, data, sz);
- ssl->rflags = oldFlags;
-
- WOLFSSL_LEAVE("wolfSSL_recv()", ret);
-
- return ret;
-}
-#endif
-
-
-/* WOLFSSL_SUCCESS on ok */
-WOLFSSL_ABI
-int wolfSSL_shutdown(WOLFSSL* ssl)
-{
- int ret = WOLFSSL_FATAL_ERROR;
- WOLFSSL_ENTER("SSL_shutdown()");
-
- if (ssl == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- if (ssl->options.quietShutdown) {
- WOLFSSL_MSG("quiet shutdown, no close notify sent");
- ret = WOLFSSL_SUCCESS;
- }
- else {
- /* try to send close notify, not an error if can't */
- if (!ssl->options.isClosed && !ssl->options.connReset &&
- !ssl->options.sentNotify) {
- ssl->error = SendAlert(ssl, alert_warning, close_notify);
- if (ssl->error < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.sentNotify = 1; /* don't send close_notify twice */
- if (ssl->options.closeNotify)
- ret = WOLFSSL_SUCCESS;
- else {
- ret = WOLFSSL_SHUTDOWN_NOT_DONE;
- WOLFSSL_LEAVE("SSL_shutdown()", ret);
- return ret;
- }
- }
-
-#ifdef WOLFSSL_SHUTDOWNONCE
- if (ssl->options.isClosed || ssl->options.connReset) {
- /* Shutdown has already occurred.
- * Caller is free to ignore this error. */
- return SSL_SHUTDOWN_ALREADY_DONE_E;
- }
-#endif
-
- /* call wolfSSL_shutdown again for bidirectional shutdown */
- if (ssl->options.sentNotify && !ssl->options.closeNotify) {
- ret = ProcessReply(ssl);
- if (ret == ZERO_RETURN) {
- /* simulate OpenSSL behavior */
- ssl->error = WOLFSSL_ERROR_SYSCALL;
- ret = WOLFSSL_SUCCESS;
- } else if (ssl->error == WOLFSSL_ERROR_NONE) {
- ret = WOLFSSL_SHUTDOWN_NOT_DONE;
- } else {
- WOLFSSL_ERROR(ssl->error);
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
-
-#ifdef OPENSSL_EXTRA
- /* reset WOLFSSL structure state for possible re-use */
- if (ret == WOLFSSL_SUCCESS) {
- if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("could not clear WOLFSSL");
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
-#endif
-
- WOLFSSL_LEAVE("SSL_shutdown()", ret);
-
- return ret;
-}
-
-
-/* get current error state value */
-int wolfSSL_state(WOLFSSL* ssl)
-{
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- return ssl->error;
-}
-
-
-WOLFSSL_ABI
-int wolfSSL_get_error(WOLFSSL* ssl, int ret)
-{
- WOLFSSL_ENTER("SSL_get_error");
-
- if (ret > 0)
- return WOLFSSL_ERROR_NONE;
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- WOLFSSL_LEAVE("SSL_get_error", ssl->error);
-
- /* make sure converted types are handled in SetErrorString() too */
- if (ssl->error == WANT_READ)
- return WOLFSSL_ERROR_WANT_READ; /* convert to OpenSSL type */
- else if (ssl->error == WANT_WRITE)
- return WOLFSSL_ERROR_WANT_WRITE; /* convert to OpenSSL type */
- else if (ssl->error == ZERO_RETURN)
- return WOLFSSL_ERROR_ZERO_RETURN; /* convert to OpenSSL type */
- return ssl->error;
-}
-
-
-/* retrieve alert history, WOLFSSL_SUCCESS on ok */
-int wolfSSL_get_alert_history(WOLFSSL* ssl, WOLFSSL_ALERT_HISTORY *h)
-{
- if (ssl && h) {
- *h = ssl->alert_history;
- }
- return WOLFSSL_SUCCESS;
-}
-
-#ifdef OPENSSL_EXTRA
-/* returns SSL_WRITING, SSL_READING or SSL_NOTHING */
-int wolfSSL_want(WOLFSSL* ssl)
-{
- int rw_state = SSL_NOTHING;
- if (ssl) {
- if (ssl->error == WANT_READ)
- rw_state = SSL_READING;
- else if (ssl->error == WANT_WRITE)
- rw_state = SSL_WRITING;
- }
- return rw_state;
-}
-#endif
-
-/* return TRUE if current error is want read */
-int wolfSSL_want_read(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("SSL_want_read");
- if (ssl->error == WANT_READ)
- return 1;
-
- return 0;
-}
-
-
-/* return TRUE if current error is want write */
-int wolfSSL_want_write(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("SSL_want_write");
- if (ssl->error == WANT_WRITE)
- return 1;
-
- return 0;
-}
-
-
-char* wolfSSL_ERR_error_string(unsigned long errNumber, char* data)
-{
- static wcchar msg = "Please supply a buffer for error string";
-
- WOLFSSL_ENTER("ERR_error_string");
- if (data) {
- SetErrorString((int)errNumber, data);
- return data;
- }
-
- return (char*)msg;
-}
-
-
-void wolfSSL_ERR_error_string_n(unsigned long e, char* buf, unsigned long len)
-{
- WOLFSSL_ENTER("wolfSSL_ERR_error_string_n");
- if (len >= WOLFSSL_MAX_ERROR_SZ)
- wolfSSL_ERR_error_string(e, buf);
- else {
- char tmp[WOLFSSL_MAX_ERROR_SZ];
-
- WOLFSSL_MSG("Error buffer too short, truncating");
- if (len) {
- wolfSSL_ERR_error_string(e, tmp);
- XMEMCPY(buf, tmp, len-1);
- buf[len-1] = '\0';
- }
- }
-}
-
-
-/* don't free temporary arrays at end of handshake */
-void wolfSSL_KeepArrays(WOLFSSL* ssl)
-{
- if (ssl)
- ssl->options.saveArrays = 1;
-}
-
-
-/* user doesn't need temporary arrays anymore, Free */
-void wolfSSL_FreeArrays(WOLFSSL* ssl)
-{
- if (ssl && ssl->options.handShakeState == HANDSHAKE_DONE) {
- ssl->options.saveArrays = 0;
- FreeArrays(ssl, 1);
- }
-}
-
-/* Set option to indicate that the resources are not to be freed after
- * handshake.
- *
- * ssl The SSL/TLS object.
- * returns BAD_FUNC_ARG when ssl is NULL and 0 on success.
- */
-int wolfSSL_KeepHandshakeResources(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ssl->options.keepResources = 1;
-
- return 0;
-}
-
-/* Free the handshake resources after handshake.
- *
- * ssl The SSL/TLS object.
- * returns BAD_FUNC_ARG when ssl is NULL and 0 on success.
- */
-int wolfSSL_FreeHandshakeResources(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- FreeHandshakeResources(ssl);
-
- return 0;
-}
-
-/* Use the client's order of preference when matching cipher suites.
- *
- * ssl The SSL/TLS context object.
- * returns BAD_FUNC_ARG when ssl is NULL and 0 on success.
- */
-int wolfSSL_CTX_UseClientSuites(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- ctx->useClientOrder = 1;
-
- return 0;
-}
-
-/* Use the client's order of preference when matching cipher suites.
- *
- * ssl The SSL/TLS object.
- * returns BAD_FUNC_ARG when ssl is NULL and 0 on success.
- */
-int wolfSSL_UseClientSuites(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ssl->options.useClientOrder = 1;
-
- return 0;
-}
-
-const byte* wolfSSL_GetMacSecret(WOLFSSL* ssl, int verify)
-{
-#ifndef WOLFSSL_AEAD_ONLY
- if (ssl == NULL)
- return NULL;
-
- if ( (ssl->options.side == WOLFSSL_CLIENT_END && !verify) ||
- (ssl->options.side == WOLFSSL_SERVER_END && verify) )
- return ssl->keys.client_write_MAC_secret;
- else
- return ssl->keys.server_write_MAC_secret;
-#else
- (void)ssl;
- (void)verify;
-
- return NULL;
-#endif
-}
-
-
-#ifdef ATOMIC_USER
-
-void wolfSSL_CTX_SetMacEncryptCb(WOLFSSL_CTX* ctx, CallbackMacEncrypt cb)
-{
- if (ctx)
- ctx->MacEncryptCb = cb;
-}
-
-
-void wolfSSL_SetMacEncryptCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->MacEncryptCtx = ctx;
-}
-
-
-void* wolfSSL_GetMacEncryptCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->MacEncryptCtx;
-
- return NULL;
-}
-
-
-void wolfSSL_CTX_SetDecryptVerifyCb(WOLFSSL_CTX* ctx, CallbackDecryptVerify cb)
-{
- if (ctx)
- ctx->DecryptVerifyCb = cb;
-}
-
-
-void wolfSSL_SetDecryptVerifyCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->DecryptVerifyCtx = ctx;
-}
-
-
-void* wolfSSL_GetDecryptVerifyCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->DecryptVerifyCtx;
-
- return NULL;
-}
-
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
-/**
- * Set the callback, against the context, that encrypts then MACs.
- *
- * ctx SSL/TLS context.
- * cb Callback function to use with Encrypt-Then-MAC.
- */
-void wolfSSL_CTX_SetEncryptMacCb(WOLFSSL_CTX* ctx, CallbackEncryptMac cb)
-{
- if (ctx)
- ctx->EncryptMacCb = cb;
-}
-
-/**
- * Set the context to use with callback that encrypts then MACs.
- *
- * ssl SSL/TLS object.
- * ctx Callback function's context.
- */
-void wolfSSL_SetEncryptMacCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->EncryptMacCtx = ctx;
-}
-
-/**
- * Get the context being used with callback that encrypts then MACs.
- *
- * ssl SSL/TLS object.
- * returns callback function's context or NULL if SSL/TLS object is NULL.
- */
-void* wolfSSL_GetEncryptMacCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->EncryptMacCtx;
-
- return NULL;
-}
-
-
-/**
- * Set the callback, against the context, that MAC verifies then decrypts.
- *
- * ctx SSL/TLS context.
- * cb Callback function to use with Encrypt-Then-MAC.
- */
-void wolfSSL_CTX_SetVerifyDecryptCb(WOLFSSL_CTX* ctx, CallbackVerifyDecrypt cb)
-{
- if (ctx)
- ctx->VerifyDecryptCb = cb;
-}
-
-/**
- * Set the context to use with callback that MAC verifies then decrypts.
- *
- * ssl SSL/TLS object.
- * ctx Callback function's context.
- */
-void wolfSSL_SetVerifyDecryptCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->VerifyDecryptCtx = ctx;
-}
-
-/**
- * Get the context being used with callback that MAC verifies then decrypts.
- *
- * ssl SSL/TLS object.
- * returns callback function's context or NULL if SSL/TLS object is NULL.
- */
-void* wolfSSL_GetVerifyDecryptCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->VerifyDecryptCtx;
-
- return NULL;
-}
-#endif /* HAVE_ENCRYPT_THEN_MAC !WOLFSSL_AEAD_ONLY */
-
-
-
-const byte* wolfSSL_GetClientWriteKey(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->keys.client_write_key;
-
- return NULL;
-}
-
-
-const byte* wolfSSL_GetClientWriteIV(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->keys.client_write_IV;
-
- return NULL;
-}
-
-
-const byte* wolfSSL_GetServerWriteKey(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->keys.server_write_key;
-
- return NULL;
-}
-
-
-const byte* wolfSSL_GetServerWriteIV(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->keys.server_write_IV;
-
- return NULL;
-}
-
-int wolfSSL_GetKeySize(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->specs.key_size;
-
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_GetIVSize(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->specs.iv_size;
-
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_GetBulkCipher(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->specs.bulk_cipher_algorithm;
-
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_GetCipherType(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
-#ifndef WOLFSSL_AEAD_ONLY
- if (ssl->specs.cipher_type == block)
- return WOLFSSL_BLOCK_TYPE;
- if (ssl->specs.cipher_type == stream)
- return WOLFSSL_STREAM_TYPE;
-#endif
- if (ssl->specs.cipher_type == aead)
- return WOLFSSL_AEAD_TYPE;
-
- return -1;
-}
-
-
-int wolfSSL_GetCipherBlockSize(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return ssl->specs.block_size;
-}
-
-
-int wolfSSL_GetAeadMacSize(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return ssl->specs.aead_mac_size;
-}
-
-
-int wolfSSL_IsTLSv1_1(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- if (ssl->options.tls1_1)
- return 1;
-
- return 0;
-}
-
-
-int wolfSSL_GetSide(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->options.side;
-
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_GetHmacSize(WOLFSSL* ssl)
-{
- /* AEAD ciphers don't have HMAC keys */
- if (ssl)
- return (ssl->specs.cipher_type != aead) ? ssl->specs.hash_size : 0;
-
- return BAD_FUNC_ARG;
-}
-
-#endif /* ATOMIC_USER */
-
-#ifndef NO_CERTS
-
-WOLFSSL_CERT_MANAGER* wolfSSL_CTX_GetCertManager(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_CERT_MANAGER* cm = NULL;
- if (ctx)
- cm = ctx->cm;
- return cm;
-}
-
-WOLFSSL_CERT_MANAGER* wolfSSL_CertManagerNew_ex(void* heap)
-{
- WOLFSSL_CERT_MANAGER* cm;
-
- WOLFSSL_ENTER("wolfSSL_CertManagerNew");
-
- cm = (WOLFSSL_CERT_MANAGER*) XMALLOC(sizeof(WOLFSSL_CERT_MANAGER), heap,
- DYNAMIC_TYPE_CERT_MANAGER);
- if (cm) {
- XMEMSET(cm, 0, sizeof(WOLFSSL_CERT_MANAGER));
-
- if (wc_InitMutex(&cm->caLock) != 0) {
- WOLFSSL_MSG("Bad mutex init");
- wolfSSL_CertManagerFree(cm);
- return NULL;
- }
-
- #ifdef WOLFSSL_TRUST_PEER_CERT
- if (wc_InitMutex(&cm->tpLock) != 0) {
- WOLFSSL_MSG("Bad mutex init");
- wolfSSL_CertManagerFree(cm);
- return NULL;
- }
- #endif
-
- /* set default minimum key size allowed */
- #ifndef NO_RSA
- cm->minRsaKeySz = MIN_RSAKEY_SZ;
- #endif
- #ifdef HAVE_ECC
- cm->minEccKeySz = MIN_ECCKEY_SZ;
- #endif
- cm->heap = heap;
- }
-
- return cm;
-}
-
-
-WOLFSSL_CERT_MANAGER* wolfSSL_CertManagerNew(void)
-{
- return wolfSSL_CertManagerNew_ex(NULL);
-}
-
-
-void wolfSSL_CertManagerFree(WOLFSSL_CERT_MANAGER* cm)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerFree");
-
- if (cm) {
- #ifdef HAVE_CRL
- if (cm->crl)
- FreeCRL(cm->crl, 1);
- #endif
- #ifdef HAVE_OCSP
- if (cm->ocsp)
- FreeOCSP(cm->ocsp, 1);
- XFREE(cm->ocspOverrideURL, cm->heap, DYNAMIC_TYPE_URL);
- #if !defined(NO_WOLFSSL_SERVER) && \
- (defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
- defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2))
- if (cm->ocsp_stapling)
- FreeOCSP(cm->ocsp_stapling, 1);
- #endif
- #endif
- FreeSignerTable(cm->caTable, CA_TABLE_SIZE, cm->heap);
- wc_FreeMutex(&cm->caLock);
-
- #ifdef WOLFSSL_TRUST_PEER_CERT
- FreeTrustedPeerTable(cm->tpTable, TP_TABLE_SIZE, cm->heap);
- wc_FreeMutex(&cm->tpLock);
- #endif
-
- XFREE(cm, cm->heap, DYNAMIC_TYPE_CERT_MANAGER);
- }
-
-}
-
-#if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
-#if defined(WOLFSSL_SIGNER_DER_CERT)
-/******************************************************************************
-* wolfSSL_CertManagerGetCerts - retrieve stack of X509 certificates in a
-* certificate manager (CM).
-*
-* RETURNS:
-* returns stack of X509 certs on success, otherwise returns a NULL.
-*/
-WOLFSSL_STACK* wolfSSL_CertManagerGetCerts(WOLFSSL_CERT_MANAGER* cm)
-{
- WOLFSSL_STACK* sk = NULL;
- Signer* signers = NULL;
- word32 row = 0;
- DecodedCert* dCert = NULL;
- WOLFSSL_X509* x509 = NULL;
- int found = 0;
-
- if (cm == NULL)
- return NULL;
-
- sk = wolfSSL_sk_X509_new();
-
- if (sk == NULL) {
- return NULL;
- }
-
- if (wc_LockMutex(&cm->caLock) != 0) {
- goto error_init;
- }
-
- for (row = 0; row < CA_TABLE_SIZE; row++) {
- signers = cm->caTable[row];
- while (signers && signers->derCert && signers->derCert->buffer) {
-
- dCert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), cm->heap,
- DYNAMIC_TYPE_DCERT);
- if (dCert == NULL) {
- goto error;
- }
-
- XMEMSET(dCert, 0, sizeof(DecodedCert));
-
- InitDecodedCert(dCert, signers->derCert->buffer,
- signers->derCert->length, cm->heap);
-
- /* Parse Certificate */
- if (ParseCert(dCert, CERT_TYPE, NO_VERIFY, cm)) {
- goto error;
- }
-
- x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), cm->heap,
- DYNAMIC_TYPE_X509);
-
- if (x509 == NULL) {
- goto error;
- }
-
- InitX509(x509, 1, NULL);
-
- if (CopyDecodedToX509(x509, dCert) == 0) {
-
- if (wolfSSL_sk_X509_push(sk, x509) != SSL_SUCCESS) {
- WOLFSSL_MSG("Unable to load x509 into stack");
- FreeX509(x509);
- XFREE(x509, cm->heap, DYNAMIC_TYPE_X509);
- goto error;
- }
- }
- else {
- goto error;
- }
-
- found = 1;
-
- signers = signers->next;
-
- FreeDecodedCert(dCert);
- XFREE(dCert, cm->heap, DYNAMIC_TYPE_DCERT);
- dCert = NULL;
- }
- }
- wc_UnLockMutex(&cm->caLock);
-
- if (!found) {
- goto error_init;
- }
-
- return sk;
-
-error:
- wc_UnLockMutex(&cm->caLock);
-
-error_init:
-
- if (dCert) {
- FreeDecodedCert(dCert);
- XFREE(dCert, cm->heap, DYNAMIC_TYPE_DCERT);
- }
-
- if (sk)
- wolfSSL_sk_X509_free(sk);
-
- return NULL;
-}
-#endif /* WOLFSSL_SIGNER_DER_CERT */
-
-/******************************************************************************
-* wolfSSL_X509_STORE_GetCerts - retrieve stack of X509 in a certificate store ctx
-*
-* This API can be used in SSL verify callback function to view cert chain
-* See examples/client/client.c and myVerify() function in test.h
-*
-* RETURNS:
-* returns stack of X509 certs on success, otherwise returns a NULL.
-*/
-WOLFSSL_STACK* wolfSSL_X509_STORE_GetCerts(WOLFSSL_X509_STORE_CTX* s)
-{
- int certIdx = 0;
- WOLFSSL_BUFFER_INFO* cert = NULL;
- DecodedCert* dCert = NULL;
- WOLFSSL_X509* x509 = NULL;
- WOLFSSL_STACK* sk = NULL;
- int found = 0;
-
- if (s == NULL) {
- return NULL;
- }
-
- sk = wolfSSL_sk_X509_new();
-
- if (sk == NULL) {
- return NULL;
- }
-
- for (certIdx = s->totalCerts - 1; certIdx >= 0; certIdx--) {
- /* get certificate buffer */
- cert = &s->certs[certIdx];
-
- dCert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, DYNAMIC_TYPE_DCERT);
-
- if (dCert == NULL) {
- goto error;
- }
- XMEMSET(dCert, 0, sizeof(DecodedCert));
-
- InitDecodedCert(dCert, cert->buffer, cert->length, NULL);
-
- /* Parse Certificate */
- if (ParseCert(dCert, CERT_TYPE, NO_VERIFY, NULL)){
- goto error;
- }
- x509 = wolfSSL_X509_new();
-
- if (x509 == NULL) {
- goto error;
- }
- InitX509(x509, 1, NULL);
-
- if (CopyDecodedToX509(x509, dCert) == 0) {
-
- if (wolfSSL_sk_X509_push(sk, x509) != SSL_SUCCESS) {
- WOLFSSL_MSG("Unable to load x509 into stack");
- wolfSSL_X509_free(x509);
- goto error;
- }
- }
- else {
- goto error;
- }
- found = 1;
-
- FreeDecodedCert(dCert);
- XFREE(dCert, NULL, DYNAMIC_TYPE_DCERT);
- dCert = NULL;
- }
-
- if (!found) {
- wolfSSL_sk_X509_free(sk);
- sk = NULL;
- }
- return sk;
-
-error:
- if (dCert) {
- FreeDecodedCert(dCert);
- XFREE(dCert, NULL, DYNAMIC_TYPE_DCERT);
- }
-
- if (sk)
- wolfSSL_sk_X509_free(sk);
-
- return NULL;
-}
-#endif /* OPENSSL_EXTRA && !NO_FILESYSTEM */
-
-/* Unload the CA signer list */
-int wolfSSL_CertManagerUnloadCAs(WOLFSSL_CERT_MANAGER* cm)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerUnloadCAs");
-
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- if (wc_LockMutex(&cm->caLock) != 0)
- return BAD_MUTEX_E;
-
- FreeSignerTable(cm->caTable, CA_TABLE_SIZE, cm->heap);
-
- wc_UnLockMutex(&cm->caLock);
-
-
- return WOLFSSL_SUCCESS;
-}
-
-
-#ifdef WOLFSSL_TRUST_PEER_CERT
-int wolfSSL_CertManagerUnload_trust_peers(WOLFSSL_CERT_MANAGER* cm)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerUnload_trust_peers");
-
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- if (wc_LockMutex(&cm->tpLock) != 0)
- return BAD_MUTEX_E;
-
- FreeTrustedPeerTable(cm->tpTable, TP_TABLE_SIZE, cm->heap);
-
- wc_UnLockMutex(&cm->tpLock);
-
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* WOLFSSL_TRUST_PEER_CERT */
-
-#endif /* NO_CERTS */
-
-#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
-
-void wolfSSL_ERR_print_errors_fp(XFILE fp, int err)
-{
- char data[WOLFSSL_MAX_ERROR_SZ + 1];
-
- WOLFSSL_ENTER("wolfSSL_ERR_print_errors_fp");
- SetErrorString(err, data);
- fprintf(fp, "%s", data);
-}
-
-#if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
-void wolfSSL_ERR_dump_errors_fp(XFILE fp)
-{
- wc_ERR_print_errors_fp(fp);
-}
-
-void wolfSSL_ERR_print_errors_cb (int (*cb)(const char *str, size_t len,
- void *u), void *u)
-{
- wc_ERR_print_errors_cb(cb, u);
-}
-#endif
-#endif
-
-WOLFSSL_ABI
-int wolfSSL_pending(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("SSL_pending");
- return ssl->buffers.clearOutputBuffer.length;
-}
-
-
-#ifndef WOLFSSL_LEANPSK
-/* turn on handshake group messages for context */
-int wolfSSL_CTX_set_group_messages(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- ctx->groupMessages = 1;
-
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_CLIENT
-/* connect enough to get peer cert chain */
-int wolfSSL_connect_cert(WOLFSSL* ssl)
-{
- int ret;
-
- if (ssl == NULL)
- return WOLFSSL_FAILURE;
-
- ssl->options.certOnly = 1;
- ret = wolfSSL_connect(ssl);
- ssl->options.certOnly = 0;
-
- return ret;
-}
-#endif
-
-
-#ifndef WOLFSSL_LEANPSK
-/* turn on handshake group messages for ssl object */
-int wolfSSL_set_group_messages(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ssl->options.groupMessages = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* make minVersion the internal equivalent SSL version */
-static int SetMinVersionHelper(byte* minVersion, int version)
-{
-#ifdef NO_TLS
- (void)minVersion;
-#endif
-
- switch (version) {
-#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
- case WOLFSSL_SSLV3:
- *minVersion = SSLv3_MINOR;
- break;
-#endif
-
-#ifndef NO_TLS
- #ifndef NO_OLD_TLS
- #ifdef WOLFSSL_ALLOW_TLSV10
- case WOLFSSL_TLSV1:
- *minVersion = TLSv1_MINOR;
- break;
- #endif
-
- case WOLFSSL_TLSV1_1:
- *minVersion = TLSv1_1_MINOR;
- break;
- #endif
- #ifndef WOLFSSL_NO_TLS12
- case WOLFSSL_TLSV1_2:
- *minVersion = TLSv1_2_MINOR;
- break;
- #endif
-#endif
- #ifdef WOLFSSL_TLS13
- case WOLFSSL_TLSV1_3:
- *minVersion = TLSv1_3_MINOR;
- break;
- #endif
-
- default:
- WOLFSSL_MSG("Bad function argument");
- return BAD_FUNC_ARG;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* Set minimum downgrade version allowed, WOLFSSL_SUCCESS on ok */
-WOLFSSL_ABI
-int wolfSSL_CTX_SetMinVersion(WOLFSSL_CTX* ctx, int version)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_SetMinVersion");
-
- if (ctx == NULL) {
- WOLFSSL_MSG("Bad function argument");
- return BAD_FUNC_ARG;
- }
-
- return SetMinVersionHelper(&ctx->minDowngrade, version);
-}
-
-
-/* Set minimum downgrade version allowed, WOLFSSL_SUCCESS on ok */
-int wolfSSL_SetMinVersion(WOLFSSL* ssl, int version)
-{
- WOLFSSL_ENTER("wolfSSL_SetMinVersion");
-
- if (ssl == NULL) {
- WOLFSSL_MSG("Bad function argument");
- return BAD_FUNC_ARG;
- }
-
- return SetMinVersionHelper(&ssl->options.minDowngrade, version);
-}
-
-
-/* Function to get version as WOLFSSL_ enum value for wolfSSL_SetVersion */
-int wolfSSL_GetVersion(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- if (ssl->version.major == SSLv3_MAJOR) {
- switch (ssl->version.minor) {
- case SSLv3_MINOR :
- return WOLFSSL_SSLV3;
- case TLSv1_MINOR :
- return WOLFSSL_TLSV1;
- case TLSv1_1_MINOR :
- return WOLFSSL_TLSV1_1;
- case TLSv1_2_MINOR :
- return WOLFSSL_TLSV1_2;
- case TLSv1_3_MINOR :
- return WOLFSSL_TLSV1_3;
- default:
- break;
- }
- }
-
- return VERSION_ERROR;
-}
-
-int wolfSSL_SetVersion(WOLFSSL* ssl, int version)
-{
- word16 haveRSA = 1;
- word16 havePSK = 0;
- int keySz = 0;
-
- WOLFSSL_ENTER("wolfSSL_SetVersion");
-
- if (ssl == NULL) {
- WOLFSSL_MSG("Bad function argument");
- return BAD_FUNC_ARG;
- }
-
- switch (version) {
-#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
- case WOLFSSL_SSLV3:
- ssl->version = MakeSSLv3();
- break;
-#endif
-
-#ifndef NO_TLS
- #ifndef NO_OLD_TLS
- #ifdef WOLFSSL_ALLOW_TLSV10
- case WOLFSSL_TLSV1:
- ssl->version = MakeTLSv1();
- break;
- #endif
-
- case WOLFSSL_TLSV1_1:
- ssl->version = MakeTLSv1_1();
- break;
- #endif
- #ifndef WOLFSSL_NO_TLS12
- case WOLFSSL_TLSV1_2:
- ssl->version = MakeTLSv1_2();
- break;
- #endif
-#endif
-#ifdef WOLFSSL_TLS13
- case WOLFSSL_TLSV1_3:
- ssl->version = MakeTLSv1_3();
- break;
-
-#endif
-
- default:
- WOLFSSL_MSG("Bad function argument");
- return BAD_FUNC_ARG;
- }
-
- #ifdef NO_RSA
- haveRSA = 0;
- #endif
- #ifndef NO_PSK
- havePSK = ssl->options.havePSK;
- #endif
- #ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
- #endif
-
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* !leanpsk */
-
-
-#if !defined(NO_CERTS) || !defined(NO_SESSION_CACHE)
-
-/* Make a work from the front of random hash */
-static WC_INLINE word32 MakeWordFromHash(const byte* hashID)
-{
- return ((word32)hashID[0] << 24) | ((word32)hashID[1] << 16) |
- (hashID[2] << 8) | hashID[3];
-}
-
-#endif /* !NO_CERTS || !NO_SESSION_CACHE */
-
-
-#ifndef NO_CERTS
-
-/* hash is the SHA digest of name, just use first 32 bits as hash */
-static WC_INLINE word32 HashSigner(const byte* hash)
-{
- return MakeWordFromHash(hash) % CA_TABLE_SIZE;
-}
-
-
-/* does CA already exist on signer list */
-int AlreadySigner(WOLFSSL_CERT_MANAGER* cm, byte* hash)
-{
- Signer* signers;
- int ret = 0;
- word32 row;
-
- if (cm == NULL || hash == NULL) {
- return ret;
- }
-
- row = HashSigner(hash);
-
- if (wc_LockMutex(&cm->caLock) != 0) {
- return ret;
- }
- signers = cm->caTable[row];
- while (signers) {
- byte* subjectHash;
-
- #ifndef NO_SKID
- subjectHash = signers->subjectKeyIdHash;
- #else
- subjectHash = signers->subjectNameHash;
- #endif
-
- if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
- ret = 1; /* success */
- break;
- }
- signers = signers->next;
- }
- wc_UnLockMutex(&cm->caLock);
-
- return ret;
-}
-
-
-#ifdef WOLFSSL_TRUST_PEER_CERT
-/* hash is the SHA digest of name, just use first 32 bits as hash */
-static WC_INLINE word32 TrustedPeerHashSigner(const byte* hash)
-{
- return MakeWordFromHash(hash) % TP_TABLE_SIZE;
-}
-
-/* does trusted peer already exist on signer list */
-int AlreadyTrustedPeer(WOLFSSL_CERT_MANAGER* cm, byte* hash)
-{
- TrustedPeerCert* tp;
- int ret = 0;
- word32 row = TrustedPeerHashSigner(hash);
-
- if (wc_LockMutex(&cm->tpLock) != 0)
- return ret;
- tp = cm->tpTable[row];
- while (tp) {
- byte* subjectHash;
- #ifndef NO_SKID
- subjectHash = tp->subjectKeyIdHash;
- #else
- subjectHash = tp->subjectNameHash;
- #endif
- if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
- ret = 1;
- break;
- }
- tp = tp->next;
- }
- wc_UnLockMutex(&cm->tpLock);
-
- return ret;
-}
-
-
-/* return Trusted Peer if found, otherwise NULL
- type is what to match on
- */
-TrustedPeerCert* GetTrustedPeer(void* vp, byte* hash, int type)
-{
- WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
- TrustedPeerCert* ret = NULL;
- TrustedPeerCert* tp = NULL;
- word32 row;
-
- if (cm == NULL || hash == NULL)
- return NULL;
-
- row = TrustedPeerHashSigner(hash);
-
- if (wc_LockMutex(&cm->tpLock) != 0)
- return ret;
-
- tp = cm->tpTable[row];
- while (tp) {
- byte* subjectHash;
- switch (type) {
- #ifndef NO_SKID
- case WC_MATCH_SKID:
- subjectHash = tp->subjectKeyIdHash;
- break;
- #endif
- case WC_MATCH_NAME:
- subjectHash = tp->subjectNameHash;
- break;
- default:
- WOLFSSL_MSG("Unknown search type");
- wc_UnLockMutex(&cm->tpLock);
- return NULL;
- }
- if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
- ret = tp;
- break;
- }
- tp = tp->next;
- }
- wc_UnLockMutex(&cm->tpLock);
-
- return ret;
-}
-
-
-int MatchTrustedPeer(TrustedPeerCert* tp, DecodedCert* cert)
-{
- if (tp == NULL || cert == NULL)
- return BAD_FUNC_ARG;
-
- /* subject key id or subject hash has been compared when searching
- tpTable for the cert from function GetTrustedPeer */
-
- /* compare signatures */
- if (tp->sigLen == cert->sigLength) {
- if (XMEMCMP(tp->sig, cert->signature, cert->sigLength)) {
- return WOLFSSL_FAILURE;
- }
- }
- else {
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* WOLFSSL_TRUST_PEER_CERT */
-
-
-/* return CA if found, otherwise NULL */
-Signer* GetCA(void* vp, byte* hash)
-{
- WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
- Signer* ret = NULL;
- Signer* signers;
- word32 row = HashSigner(hash);
-
- if (cm == NULL)
- return NULL;
-
- if (wc_LockMutex(&cm->caLock) != 0)
- return ret;
-
- signers = cm->caTable[row];
- while (signers) {
- byte* subjectHash;
- #ifndef NO_SKID
- subjectHash = signers->subjectKeyIdHash;
- #else
- subjectHash = signers->subjectNameHash;
- #endif
- if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
- ret = signers;
- break;
- }
- signers = signers->next;
- }
- wc_UnLockMutex(&cm->caLock);
-
- return ret;
-}
-
-
-#ifndef NO_SKID
-/* return CA if found, otherwise NULL. Walk through hash table. */
-Signer* GetCAByName(void* vp, byte* hash)
-{
- WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
- Signer* ret = NULL;
- Signer* signers;
- word32 row;
-
- if (cm == NULL)
- return NULL;
-
- if (wc_LockMutex(&cm->caLock) != 0)
- return ret;
-
- for (row = 0; row < CA_TABLE_SIZE && ret == NULL; row++) {
- signers = cm->caTable[row];
- while (signers && ret == NULL) {
- if (XMEMCMP(hash, signers->subjectNameHash,
- SIGNER_DIGEST_SIZE) == 0) {
- ret = signers;
- }
- signers = signers->next;
- }
- }
- wc_UnLockMutex(&cm->caLock);
-
- return ret;
-}
-#endif
-
-
-#ifdef WOLFSSL_TRUST_PEER_CERT
-/* add a trusted peer cert to linked list */
-int AddTrustedPeer(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int verify)
-{
- int ret, row;
- TrustedPeerCert* peerCert;
- DecodedCert* cert;
- DerBuffer* der = *pDer;
- byte* subjectHash = NULL;
-
- WOLFSSL_MSG("Adding a Trusted Peer Cert");
-
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), cm->heap,
- DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- return MEMORY_E;
-
- InitDecodedCert(cert, der->buffer, der->length, cm->heap);
- if ((ret = ParseCert(cert, TRUSTED_PEER_TYPE, verify, cm)) != 0) {
- FreeDecodedCert(cert);
- XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
- return ret;
- }
- WOLFSSL_MSG("\tParsed new trusted peer cert");
-
- peerCert = (TrustedPeerCert*)XMALLOC(sizeof(TrustedPeerCert), cm->heap,
- DYNAMIC_TYPE_CERT);
- if (peerCert == NULL) {
- FreeDecodedCert(cert);
- XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
- return MEMORY_E;
- }
- XMEMSET(peerCert, 0, sizeof(TrustedPeerCert));
-
-#ifndef NO_SKID
- if (cert->extAuthKeyIdSet) {
- subjectHash = cert->extSubjKeyId;
- }
- else {
- subjectHash = cert->subjectHash;
- }
-#else
- subjectHash = cert->subjectHash;
-#endif
-
- #ifndef IGNORE_NAME_CONSTRAINTS
- if (peerCert->permittedNames)
- FreeNameSubtrees(peerCert->permittedNames, cm->heap);
- if (peerCert->excludedNames)
- FreeNameSubtrees(peerCert->excludedNames, cm->heap);
- #endif
-
- if (AlreadyTrustedPeer(cm, subjectHash)) {
- WOLFSSL_MSG("\tAlready have this CA, not adding again");
- FreeTrustedPeer(peerCert, cm->heap);
- (void)ret;
- }
- else {
- /* add trusted peer signature */
- peerCert->sigLen = cert->sigLength;
- peerCert->sig = XMALLOC(cert->sigLength, cm->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (peerCert->sig == NULL) {
- FreeDecodedCert(cert);
- XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
- FreeTrustedPeer(peerCert, cm->heap);
- return MEMORY_E;
- }
- XMEMCPY(peerCert->sig, cert->signature, cert->sigLength);
-
- /* add trusted peer name */
- peerCert->nameLen = cert->subjectCNLen;
- peerCert->name = cert->subjectCN;
- #ifndef IGNORE_NAME_CONSTRAINTS
- peerCert->permittedNames = cert->permittedNames;
- peerCert->excludedNames = cert->excludedNames;
- #endif
-
- /* add SKID when available and hash of name */
- #ifndef NO_SKID
- XMEMCPY(peerCert->subjectKeyIdHash, cert->extSubjKeyId,
- SIGNER_DIGEST_SIZE);
- #endif
- XMEMCPY(peerCert->subjectNameHash, cert->subjectHash,
- SIGNER_DIGEST_SIZE);
- peerCert->next = NULL; /* If Key Usage not set, all uses valid. */
- cert->subjectCN = 0;
- #ifndef IGNORE_NAME_CONSTRAINTS
- cert->permittedNames = NULL;
- cert->excludedNames = NULL;
- #endif
-
- #ifndef NO_SKID
- if (cert->extAuthKeyIdSet) {
- row = TrustedPeerHashSigner(peerCert->subjectKeyIdHash);
- }
- else {
- row = TrustedPeerHashSigner(peerCert->subjectNameHash);
- }
- #else
- row = TrustedPeerHashSigner(peerCert->subjectNameHash);
- #endif
-
- if (wc_LockMutex(&cm->tpLock) == 0) {
- peerCert->next = cm->tpTable[row];
- cm->tpTable[row] = peerCert; /* takes ownership */
- wc_UnLockMutex(&cm->tpLock);
- }
- else {
- WOLFSSL_MSG("\tTrusted Peer Cert Mutex Lock failed");
- FreeDecodedCert(cert);
- XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
- FreeTrustedPeer(peerCert, cm->heap);
- return BAD_MUTEX_E;
- }
- }
-
- WOLFSSL_MSG("\tFreeing parsed trusted peer cert");
- FreeDecodedCert(cert);
- XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
- WOLFSSL_MSG("\tFreeing der trusted peer cert");
- FreeDer(&der);
- WOLFSSL_MSG("\t\tOK Freeing der trusted peer cert");
- WOLFSSL_LEAVE("AddTrustedPeer", ret);
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* WOLFSSL_TRUST_PEER_CERT */
-
-
-/* owns der, internal now uses too */
-/* type flag ids from user or from chain received during verify
- don't allow chain ones to be added w/o isCA extension */
-int AddCA(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int type, int verify)
-{
- int ret;
- Signer* signer = NULL;
- word32 row;
- byte* subjectHash;
-#ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert = NULL;
-#else
- DecodedCert cert[1];
-#endif
- DerBuffer* der = *pDer;
-
- WOLFSSL_MSG("Adding a CA");
-
- if (cm == NULL) {
- FreeDer(pDer);
- return BAD_FUNC_ARG;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
- DYNAMIC_TYPE_DCERT);
- if (cert == NULL) {
- FreeDer(pDer);
- return MEMORY_E;
- }
-#endif
-
- InitDecodedCert(cert, der->buffer, der->length, cm->heap);
- ret = ParseCert(cert, CA_TYPE, verify, cm);
- WOLFSSL_MSG("\tParsed new CA");
-
-#ifndef NO_SKID
- subjectHash = cert->extSubjKeyId;
-#else
- subjectHash = cert->subjectHash;
-#endif
-
- /* check CA key size */
- if (verify) {
- switch (cert->keyOID) {
- #ifndef NO_RSA
- case RSAk:
- if (cm->minRsaKeySz < 0 ||
- cert->pubKeySize < (word16)cm->minRsaKeySz) {
- ret = RSA_KEY_SIZE_E;
- WOLFSSL_MSG("\tCA RSA key size error");
- }
- break;
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ECDSAk:
- if (cm->minEccKeySz < 0 ||
- cert->pubKeySize < (word16)cm->minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("\tCA ECC key size error");
- }
- break;
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ED25519k:
- if (cm->minEccKeySz < 0 ||
- ED25519_KEY_SIZE < (word16)cm->minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("\tCA ECC key size error");
- }
- break;
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- case ED448k:
- if (cm->minEccKeySz < 0 ||
- ED448_KEY_SIZE < (word16)cm->minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("\tCA ECC key size error");
- }
- break;
- #endif /* HAVE_ED448 */
-
- default:
- WOLFSSL_MSG("\tNo key size check done on CA");
- break; /* no size check if key type is not in switch */
- }
- }
-
- if (ret == 0 && cert->isCA == 0 && type != WOLFSSL_USER_CA) {
- WOLFSSL_MSG("\tCan't add as CA if not actually one");
- ret = NOT_CA_ERROR;
- }
-#ifndef ALLOW_INVALID_CERTSIGN
- else if (ret == 0 && cert->isCA == 1 && type != WOLFSSL_USER_CA &&
- !cert->selfSigned && (cert->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0) {
- /* Intermediate CA certs are required to have the keyCertSign
- * extension set. User loaded root certs are not. */
- WOLFSSL_MSG("\tDoesn't have key usage certificate signing");
- ret = NOT_CA_ERROR;
- }
-#endif
- else if (ret == 0 && AlreadySigner(cm, subjectHash)) {
- WOLFSSL_MSG("\tAlready have this CA, not adding again");
- (void)ret;
- }
- else if (ret == 0) {
- /* take over signer parts */
- signer = MakeSigner(cm->heap);
- if (!signer)
- ret = MEMORY_ERROR;
- }
- if (ret == 0 && signer != NULL) {
- #ifdef WOLFSSL_SIGNER_DER_CERT
- ret = AllocDer(&signer->derCert, der->length, der->type, NULL);
- }
- if (ret == 0 && signer != NULL) {
- XMEMCPY(signer->derCert->buffer, der->buffer, der->length);
- #endif
- signer->keyOID = cert->keyOID;
- if (cert->pubKeyStored) {
- signer->publicKey = cert->publicKey;
- signer->pubKeySize = cert->pubKeySize;
- }
- if (cert->subjectCNStored) {
- signer->nameLen = cert->subjectCNLen;
- signer->name = cert->subjectCN;
- }
- signer->pathLength = cert->pathLength;
- signer->maxPathLen = cert->maxPathLen;
- signer->pathLengthSet = cert->pathLengthSet;
- signer->selfSigned = cert->selfSigned;
- #ifndef IGNORE_NAME_CONSTRAINTS
- signer->permittedNames = cert->permittedNames;
- signer->excludedNames = cert->excludedNames;
- #endif
- #ifndef NO_SKID
- XMEMCPY(signer->subjectKeyIdHash, cert->extSubjKeyId,
- SIGNER_DIGEST_SIZE);
- #endif
- XMEMCPY(signer->subjectNameHash, cert->subjectHash,
- SIGNER_DIGEST_SIZE);
- #ifdef HAVE_OCSP
- XMEMCPY(signer->subjectKeyHash, cert->subjectKeyHash,
- KEYID_SIZE);
- #endif
- signer->keyUsage = cert->extKeyUsageSet ? cert->extKeyUsage
- : 0xFFFF;
- signer->next = NULL; /* If Key Usage not set, all uses valid. */
- cert->publicKey = 0; /* in case lock fails don't free here. */
- cert->subjectCN = 0;
- #ifndef IGNORE_NAME_CONSTRAINTS
- cert->permittedNames = NULL;
- cert->excludedNames = NULL;
- #endif
-
- #ifndef NO_SKID
- row = HashSigner(signer->subjectKeyIdHash);
- #else
- row = HashSigner(signer->subjectNameHash);
- #endif
-
- if (wc_LockMutex(&cm->caLock) == 0) {
- signer->next = cm->caTable[row];
- cm->caTable[row] = signer; /* takes ownership */
- wc_UnLockMutex(&cm->caLock);
- if (cm->caCacheCallback)
- cm->caCacheCallback(der->buffer, (int)der->length, type);
- }
- else {
- WOLFSSL_MSG("\tCA Mutex Lock failed");
- ret = BAD_MUTEX_E;
- FreeSigner(signer, cm->heap);
- }
- }
-#if defined(WOLFSSL_RENESAS_TSIP_TLS)
- /* Verify CA by TSIP so that generated tsip key is going to be able to */
- /* be used for peer's cert verification */
- /* TSIP is only able to handle USER CA, and only one CA. */
- /* Therefore, it doesn't need to call TSIP again if there is already */
- /* verified CA. */
- if ( ret == 0 && signer != NULL ) {
- signer->cm_idx = row;
- if (type == WOLFSSL_USER_CA && tsip_rootCAverified() == 0 ) {
- if ((ret = tsip_tls_RootCertVerify(cert->source, cert->maxIdx,
- cert->sigCtx.pubkey_n_start, cert->sigCtx.pubkey_n_len - 1,
- cert->sigCtx.pubkey_e_start, cert->sigCtx.pubkey_e_len - 1,
- row/* cm index */))
- != 0)
- WOLFSSL_MSG("tsip_tls_RootCertVerify() failed");
- else
- WOLFSSL_MSG("tsip_tls_RootCertVerify() succeed");
- }
- }
-#endif
- WOLFSSL_MSG("\tFreeing Parsed CA");
- FreeDecodedCert(cert);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
-#endif
- WOLFSSL_MSG("\tFreeing der CA");
- FreeDer(pDer);
- WOLFSSL_MSG("\t\tOK Freeing der CA");
-
- WOLFSSL_LEAVE("AddCA", ret);
-
- return ret == 0 ? WOLFSSL_SUCCESS : ret;
-}
-
-#endif /* !NO_CERTS */
-
-
-#ifndef NO_SESSION_CACHE
-
- /* basic config gives a cache with 33 sessions, adequate for clients and
- embedded servers
-
- TITAN_SESSION_CACHE allows just over 2 million sessions, for servers
- with titanic amounts of memory with long session ID timeouts and high
- levels of traffic.
-
- HUGE_SESSION_CACHE yields 65,791 sessions, for servers under heavy load,
- allows over 13,000 new sessions per minute or over 200 new sessions per
- second
-
- BIG_SESSION_CACHE yields 20,027 sessions
-
- MEDIUM_SESSION_CACHE allows 1055 sessions, adequate for servers that
- aren't under heavy load, basically allows 200 new sessions per minute
-
- SMALL_SESSION_CACHE only stores 6 sessions, good for embedded clients
- or systems where the default of nearly 3kB is too much RAM, this define
- uses less than 500 bytes RAM
-
- default SESSION_CACHE stores 33 sessions (no XXX_SESSION_CACHE defined)
- */
- #if defined(TITAN_SESSION_CACHE)
- #define SESSIONS_PER_ROW 31
- #define SESSION_ROWS 64937
- #elif defined(HUGE_SESSION_CACHE)
- #define SESSIONS_PER_ROW 11
- #define SESSION_ROWS 5981
- #elif defined(BIG_SESSION_CACHE)
- #define SESSIONS_PER_ROW 7
- #define SESSION_ROWS 2861
- #elif defined(MEDIUM_SESSION_CACHE)
- #define SESSIONS_PER_ROW 5
- #define SESSION_ROWS 211
- #elif defined(SMALL_SESSION_CACHE)
- #define SESSIONS_PER_ROW 2
- #define SESSION_ROWS 3
- #else
- #define SESSIONS_PER_ROW 3
- #define SESSION_ROWS 11
- #endif
-
- typedef struct SessionRow {
- int nextIdx; /* where to place next one */
- int totalCount; /* sessions ever on this row */
- WOLFSSL_SESSION Sessions[SESSIONS_PER_ROW];
- } SessionRow;
-
- static WOLFSSL_GLOBAL SessionRow SessionCache[SESSION_ROWS];
-
- #if defined(WOLFSSL_SESSION_STATS) && defined(WOLFSSL_PEAK_SESSIONS)
- static WOLFSSL_GLOBAL word32 PeakSessions;
- #endif
-
- static WOLFSSL_GLOBAL wolfSSL_Mutex session_mutex; /* SessionCache mutex */
-
- #ifndef NO_CLIENT_CACHE
-
- typedef struct ClientSession {
- word16 serverRow; /* SessionCache Row id */
- word16 serverIdx; /* SessionCache Idx (column) */
- } ClientSession;
-
- typedef struct ClientRow {
- int nextIdx; /* where to place next one */
- int totalCount; /* sessions ever on this row */
- ClientSession Clients[SESSIONS_PER_ROW];
- } ClientRow;
-
- static WOLFSSL_GLOBAL ClientRow ClientCache[SESSION_ROWS];
- /* Client Cache */
- /* uses session mutex */
- #endif /* NO_CLIENT_CACHE */
-
-#endif /* NO_SESSION_CACHE */
-
-WOLFSSL_ABI
-int wolfSSL_Init(void)
-{
- WOLFSSL_ENTER("wolfSSL_Init");
-
- if (initRefCount == 0) {
- /* Initialize crypto for use with TLS connection */
- if (wolfCrypt_Init() != 0) {
- WOLFSSL_MSG("Bad wolfCrypt Init");
- return WC_INIT_E;
- }
-
-#ifdef OPENSSL_EXTRA
- if (wolfSSL_RAND_seed(NULL, 0) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("wolfSSL_RAND_Seed failed");
- return WC_INIT_E;
- }
-#endif
-
-#ifndef NO_SESSION_CACHE
- if (wc_InitMutex(&session_mutex) != 0) {
- WOLFSSL_MSG("Bad Init Mutex session");
- return BAD_MUTEX_E;
- }
-#endif
- if (wc_InitMutex(&count_mutex) != 0) {
- WOLFSSL_MSG("Bad Init Mutex count");
- return BAD_MUTEX_E;
- }
- }
-
- if (wc_LockMutex(&count_mutex) != 0) {
- WOLFSSL_MSG("Bad Lock Mutex count");
- return BAD_MUTEX_E;
- }
-
- initRefCount++;
- wc_UnLockMutex(&count_mutex);
-
- return WOLFSSL_SUCCESS;
-}
-
-
-
-#ifndef NO_CERTS
-
-/* process user cert chain to pass during the handshake */
-static int ProcessUserChain(WOLFSSL_CTX* ctx, const unsigned char* buff,
- long sz, int format, int type, WOLFSSL* ssl,
- long* used, EncryptedInfo* info, int verify)
-{
- int ret = 0;
- void* heap = wolfSSL_CTX_GetHeap(ctx, ssl);
-#ifdef WOLFSSL_TLS13
- int cnt = 0;
-#endif
-
- if ((type == CA_TYPE) && (ctx == NULL)) {
- WOLFSSL_MSG("Need context for CA load");
- return BAD_FUNC_ARG;
- }
-
- /* we may have a user cert chain, try to consume */
- if ((type == CERT_TYPE || type == CA_TYPE) && (info->consumed < sz)) {
- #ifdef WOLFSSL_SMALL_STACK
- byte staticBuffer[1]; /* force heap usage */
- #else
- byte staticBuffer[FILE_BUFFER_SIZE]; /* tmp chain buffer */
- #endif
- byte* chainBuffer = staticBuffer;
- int dynamicBuffer = 0;
- word32 bufferSz;
- long consumed = info->consumed;
- word32 idx = 0;
- int gotOne = 0;
-
- /* Calculate max possible size, including max headers */
- bufferSz = (word32)(sz - consumed) + (CERT_HEADER_SZ * MAX_CHAIN_DEPTH);
- if (bufferSz > sizeof(staticBuffer)) {
- WOLFSSL_MSG("Growing Tmp Chain Buffer");
- /* will shrink to actual size */
- chainBuffer = (byte*)XMALLOC(bufferSz, heap, DYNAMIC_TYPE_FILE);
- if (chainBuffer == NULL) {
- return MEMORY_E;
- }
- dynamicBuffer = 1;
- }
-
- WOLFSSL_MSG("Processing Cert Chain");
- while (consumed < sz) {
- DerBuffer* part = NULL;
- word32 remain = (word32)(sz - consumed);
- info->consumed = 0;
-
- if (format == WOLFSSL_FILETYPE_PEM) {
- #ifdef WOLFSSL_PEM_TO_DER
- ret = PemToDer(buff + consumed, remain, type, &part,
- heap, info, NULL);
- #else
- ret = NOT_COMPILED_IN;
- #endif
- }
- else {
- int length = remain;
- if (format == WOLFSSL_FILETYPE_ASN1) {
- /* get length of der (read sequence) */
- word32 inOutIdx = 0;
- if (GetSequence(buff + consumed, &inOutIdx, &length,
- remain) < 0) {
- ret = ASN_NO_PEM_HEADER;
- }
- length += inOutIdx; /* include leading sequence */
- }
- info->consumed = length;
- if (ret == 0) {
- ret = AllocDer(&part, length, type, heap);
- if (ret == 0) {
- XMEMCPY(part->buffer, buff + consumed, length);
- }
- }
- }
- if (ret == 0) {
- gotOne = 1;
-#ifdef WOLFSSL_TLS13
- cnt++;
-#endif
- if ((idx + part->length + CERT_HEADER_SZ) > bufferSz) {
- WOLFSSL_MSG(" Cert Chain bigger than buffer");
- ret = BUFFER_E;
- }
- else {
- c32to24(part->length, &chainBuffer[idx]);
- idx += CERT_HEADER_SZ;
- XMEMCPY(&chainBuffer[idx], part->buffer, part->length);
- idx += part->length;
- consumed += info->consumed;
- if (used)
- *used += info->consumed;
- }
-
- /* add CA's to certificate manager */
- if (type == CA_TYPE) {
- /* verify CA unless user set to no verify */
- ret = AddCA(ctx->cm, &part, WOLFSSL_USER_CA, verify);
- gotOne = 0; /* don't exit loop for CA type */
- }
- }
-
- FreeDer(&part);
-
- if (ret == ASN_NO_PEM_HEADER && gotOne) {
- WOLFSSL_MSG("We got one good cert, so stuff at end ok");
- break;
- }
-
- if (ret < 0) {
- WOLFSSL_MSG(" Error in Cert in Chain");
- if (dynamicBuffer)
- XFREE(chainBuffer, heap, DYNAMIC_TYPE_FILE);
- return ret;
- }
- WOLFSSL_MSG(" Consumed another Cert in Chain");
- }
- WOLFSSL_MSG("Finished Processing Cert Chain");
-
- /* only retain actual size used */
- ret = 0;
- if (idx > 0) {
- if (ssl) {
- if (ssl->buffers.weOwnCertChain) {
- FreeDer(&ssl->buffers.certChain);
- }
- ret = AllocDer(&ssl->buffers.certChain, idx, type, heap);
- if (ret == 0) {
- XMEMCPY(ssl->buffers.certChain->buffer, chainBuffer,
- idx);
- ssl->buffers.weOwnCertChain = 1;
- }
- #ifdef WOLFSSL_TLS13
- ssl->buffers.certChainCnt = cnt;
- #endif
- } else if (ctx) {
- FreeDer(&ctx->certChain);
- ret = AllocDer(&ctx->certChain, idx, type, heap);
- if (ret == 0) {
- XMEMCPY(ctx->certChain->buffer, chainBuffer, idx);
- }
- #ifdef WOLFSSL_TLS13
- ctx->certChainCnt = cnt;
- #endif
- }
- }
-
- if (dynamicBuffer)
- XFREE(chainBuffer, heap, DYNAMIC_TYPE_FILE);
- }
-
- return ret;
-}
-
-static int ProcessBufferTryDecode(WOLFSSL_CTX* ctx, WOLFSSL* ssl, DerBuffer* der,
- int* keySz, word32* idx, int* resetSuites, int* keyFormat, void* heap, int devId)
-{
- int ret = 0;
-
- (void)heap;
- (void)devId;
-
- if (ctx == NULL && ssl == NULL)
- ret = BAD_FUNC_ARG;
- if (!der || !keySz || !idx || !resetSuites || !keyFormat)
- ret = BAD_FUNC_ARG;
-
-#ifndef NO_RSA
- if (ret == 0 && (*keyFormat == 0 || *keyFormat == RSAk)) {
- /* make sure RSA key can be used */
- #ifdef WOLFSSL_SMALL_STACK
- RsaKey* key;
- #else
- RsaKey key[1];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- key = (RsaKey*)XMALLOC(sizeof(RsaKey), heap, DYNAMIC_TYPE_RSA);
- if (key == NULL)
- return MEMORY_E;
- #endif
-
- ret = wc_InitRsaKey_ex(key, heap, devId);
- if (ret == 0) {
- *idx = 0;
- if (wc_RsaPrivateKeyDecode(der->buffer, idx, key, der->length)
- != 0) {
- #if !defined(HAVE_ECC) && !defined(HAVE_ED25519) && \
- !defined(HAVE_ED448)
- WOLFSSL_MSG("RSA decode failed and ECC/ED25519/ED448 not "
- "enabled to try");
- ret = WOLFSSL_BAD_FILE;
- #endif
- }
- else {
- /* check that the size of the RSA key is enough */
- int minRsaSz = ssl ? ssl->options.minRsaKeySz :
- ctx->minRsaKeySz;
- *keySz = wc_RsaEncryptSize((RsaKey*)key);
- if (*keySz < minRsaSz) {
- ret = RSA_KEY_SIZE_E;
- WOLFSSL_MSG("Private Key size too small");
- }
-
- if (ssl) {
- ssl->buffers.keyType = rsa_sa_algo;
- ssl->buffers.keySz = *keySz;
- }
- else {
- ctx->privateKeyType = rsa_sa_algo;
- ctx->privateKeySz = *keySz;
- }
-
- *keyFormat = RSAk;
-
- if (ssl && ssl->options.side == WOLFSSL_SERVER_END) {
- ssl->options.haveStaticECC = 0;
- *resetSuites = 1;
- }
- }
-
- wc_FreeRsaKey(key);
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(key, heap, DYNAMIC_TYPE_RSA);
- #endif
- }
-#endif
-#ifdef HAVE_ECC
- if (ret == 0 && (*keyFormat == 0 || *keyFormat == ECDSAk)) {
- /* make sure ECC key can be used */
- #ifdef WOLFSSL_SMALL_STACK
- ecc_key* key;
- #else
- ecc_key key[1];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- key = (ecc_key*)XMALLOC(sizeof(ecc_key), heap, DYNAMIC_TYPE_ECC);
- if (key == NULL)
- return MEMORY_E;
- #endif
-
- if (wc_ecc_init_ex(key, heap, devId) == 0) {
- *idx = 0;
- if (wc_EccPrivateKeyDecode(der->buffer, idx, key,
- der->length) == 0) {
- /* check for minimum ECC key size and then free */
- int minKeySz = ssl ? ssl->options.minEccKeySz :
- ctx->minEccKeySz;
- *keySz = wc_ecc_size(key);
- if (*keySz < minKeySz) {
- WOLFSSL_MSG("ECC private key too small");
- ret = ECC_KEY_SIZE_E;
- }
-
- *keyFormat = ECDSAk;
- if (ssl) {
- ssl->options.haveStaticECC = 1;
- ssl->buffers.keyType = ecc_dsa_sa_algo;
- ssl->buffers.keySz = *keySz;
- }
- else {
- ctx->haveStaticECC = 1;
- ctx->privateKeyType = ecc_dsa_sa_algo;
- ctx->privateKeySz = *keySz;
- }
-
- if (ssl && ssl->options.side == WOLFSSL_SERVER_END) {
- *resetSuites = 1;
- }
- }
-
- wc_ecc_free(key);
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(key, heap, DYNAMIC_TYPE_ECC);
- #endif
- }
-#endif /* HAVE_ECC */
-#ifdef HAVE_ED25519
- if (ret == 0 && (*keyFormat == 0 || *keyFormat == ED25519k)) {
- /* make sure Ed25519 key can be used */
- #ifdef WOLFSSL_SMALL_STACK
- ed25519_key* key;
- #else
- ed25519_key key[1];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- key = (ed25519_key*)XMALLOC(sizeof(ed25519_key), heap,
- DYNAMIC_TYPE_ED25519);
- if (key == NULL)
- return MEMORY_E;
- #endif
-
- ret = wc_ed25519_init(key);
- if (ret == 0) {
- *idx = 0;
- if (wc_Ed25519PrivateKeyDecode(der->buffer, idx, key,
- der->length) == 0) {
- /* check for minimum key size and then free */
- int minKeySz = ssl ? ssl->options.minEccKeySz :
- ctx->minEccKeySz;
- *keySz = ED25519_KEY_SIZE;
- if (*keySz < minKeySz) {
- WOLFSSL_MSG("ED25519 private key too small");
- ret = ECC_KEY_SIZE_E;
- }
- if (ret == 0) {
- if (ssl) {
- ssl->buffers.keyType = ed25519_sa_algo;
- ssl->buffers.keySz = *keySz;
- }
- else if (ctx) {
- ctx->privateKeyType = ed25519_sa_algo;
- ctx->privateKeySz = *keySz;
- }
-
- *keyFormat = ED25519k;
- if (ssl && ssl->options.side == WOLFSSL_SERVER_END) {
- *resetSuites = 1;
- }
- }
- }
-
- wc_ed25519_free(key);
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(key, heap, DYNAMIC_TYPE_ED25519);
- #endif
- }
-#endif /* HAVE_ED25519 */
-#ifdef HAVE_ED448
- if (ret == 0 && (*keyFormat == 0 || *keyFormat == ED448k)) {
- /* make sure Ed448 key can be used */
- #ifdef WOLFSSL_SMALL_STACK
- ed448_key* key = NULL;
- #else
- ed448_key key[1];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- key = (ed448_key*)XMALLOC(sizeof(ed448_key), heap, DYNAMIC_TYPE_ED448);
- if (key == NULL)
- return MEMORY_E;
- #endif
-
- ret = wc_ed448_init(key);
- if (ret == 0) {
- *idx = 0;
- if (wc_Ed448PrivateKeyDecode(der->buffer, idx, key,
- der->length) != 0) {
- ret = WOLFSSL_BAD_FILE;
- }
-
- if (ret == 0) {
- /* check for minimum key size and then free */
- int minKeySz = ssl ? ssl->options.minEccKeySz :
- ctx->minEccKeySz;
- *keySz = ED448_KEY_SIZE;
- if (*keySz < minKeySz) {
- WOLFSSL_MSG("ED448 private key too small");
- ret = ECC_KEY_SIZE_E;
- }
- }
- if (ret == 0) {
- if (ssl) {
- ssl->buffers.keyType = ed448_sa_algo;
- ssl->buffers.keySz = *keySz;
- }
- else if (ctx) {
- ctx->privateKeyType = ed448_sa_algo;
- ctx->privateKeySz = *keySz;
- }
-
- *keyFormat = ED448k;
- if (ssl && ssl->options.side == WOLFSSL_SERVER_END) {
- *resetSuites = 1;
- }
- }
-
- wc_ed448_free(key);
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(key, heap, DYNAMIC_TYPE_ED448);
- #endif
- }
-#endif /* HAVE_ED448 */
- return ret;
-}
-
-/* process the buffer buff, length sz, into ctx of format and type
- used tracks bytes consumed, userChain specifies a user cert chain
- to pass during the handshake */
-int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
- long sz, int format, int type, WOLFSSL* ssl,
- long* used, int userChain, int verify)
-{
- DerBuffer* der = NULL; /* holds DER or RAW (for NTRU) */
- int ret = 0;
- int done = 0;
- int keyFormat = 0;
- int resetSuites = 0;
- void* heap = wolfSSL_CTX_GetHeap(ctx, ssl);
- int devId = wolfSSL_CTX_GetDevId(ctx, ssl);
- word32 idx = 0;
- int keySz = 0;
-#if (defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)) || \
- defined(HAVE_PKCS8)
- word32 algId = 0;
-#endif
-#ifdef WOLFSSL_SMALL_STACK
- EncryptedInfo* info = NULL;
-#else
- EncryptedInfo info[1];
-#endif
-
- (void)devId;
- (void)idx;
- (void)keySz;
-
- if (used)
- *used = sz; /* used bytes default to sz, PEM chain may shorten*/
-
- /* check args */
- if (format != WOLFSSL_FILETYPE_ASN1 && format != WOLFSSL_FILETYPE_PEM
- && format != WOLFSSL_FILETYPE_RAW)
- return WOLFSSL_BAD_FILETYPE;
-
- if (ctx == NULL && ssl == NULL)
- return BAD_FUNC_ARG;
-
-#ifdef WOLFSSL_SMALL_STACK
- info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), heap,
- DYNAMIC_TYPE_ENCRYPTEDINFO);
- if (info == NULL)
- return MEMORY_E;
-#endif
-
- XMEMSET(info, 0, sizeof(EncryptedInfo));
-#if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)
- if (ctx) {
- info->passwd_cb = ctx->passwd_cb;
- info->passwd_userdata = ctx->passwd_userdata;
- }
-#endif
-
- if (format == WOLFSSL_FILETYPE_PEM) {
- #ifdef WOLFSSL_PEM_TO_DER
- ret = PemToDer(buff, sz, type, &der, heap, info, &keyFormat);
- #else
- ret = NOT_COMPILED_IN;
- #endif
- }
- else {
- /* ASN1 (DER) or RAW (NTRU) */
- int length = (int)sz;
- if (format == WOLFSSL_FILETYPE_ASN1) {
- /* get length of der (read sequence or octet string) */
- word32 inOutIdx = 0;
- if (GetSequence(buff, &inOutIdx, &length, (word32)sz) >= 0) {
- length += inOutIdx; /* include leading sequence */
- }
- /* get length using octect string (allowed for private key types) */
- else if (type == PRIVATEKEY_TYPE &&
- GetOctetString(buff, &inOutIdx, &length, (word32)sz) >= 0) {
- length += inOutIdx; /* include leading oct string */
- }
- else {
- ret = ASN_PARSE_E;
- }
- }
-
- info->consumed = length;
-
- if (ret == 0) {
- ret = AllocDer(&der, (word32)length, type, heap);
- if (ret == 0) {
- XMEMCPY(der->buffer, buff, length);
- }
-
- #ifdef HAVE_PKCS8
- /* if private key try and remove PKCS8 header */
- if (type == PRIVATEKEY_TYPE) {
- if ((ret = ToTraditional_ex(der->buffer, der->length, &algId)) > 0) {
- /* Found PKCS8 header */
- /* ToTraditional_ex moves buff and returns adjusted length */
- der->length = ret;
- }
- ret = 0; /* failures should be ignored */
- }
- #endif
- }
- }
-
- if (used) {
- *used = info->consumed;
- }
-
- /* process user chain */
- if (ret >= 0) {
- /* Chain should have server cert first, then intermediates, then root.
- * First certificate in chain is processed below after ProcessUserChain
- * and is loaded into ssl->buffers.certificate.
- * Remainder are processed using ProcessUserChain and are loaded into
- * ssl->buffers.certChain. */
- if (userChain) {
- ret = ProcessUserChain(ctx, buff, sz, format, type, ssl, used, info,
- verify);
- }
- }
-
- /* info is only used for private key with DER or PEM, so free now */
- if (ret < 0 || type != PRIVATEKEY_TYPE || format == WOLFSSL_FILETYPE_RAW) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO);
- #endif
- }
-
- /* check for error */
- if (ret < 0) {
- FreeDer(&der);
- done = 1;
- }
-
- if (done == 1) {
- /* No operation, just skip the next section */
- }
- /* Handle DER owner */
- else if (type == CA_TYPE) {
- if (ctx == NULL) {
- WOLFSSL_MSG("Need context for CA load");
- FreeDer(&der);
- return BAD_FUNC_ARG;
- }
- /* verify CA unless user set to no verify */
- ret = AddCA(ctx->cm, &der, WOLFSSL_USER_CA, verify);
- done = 1;
- }
-#ifdef WOLFSSL_TRUST_PEER_CERT
- else if (type == TRUSTED_PEER_TYPE) {
- if (ctx == NULL) {
- WOLFSSL_MSG("Need context for trusted peer cert load");
- FreeDer(&der);
- return BAD_FUNC_ARG;
- }
- /* add trusted peer cert */
- ret = AddTrustedPeer(ctx->cm, &der, !ctx->verifyNone);
- done = 1;
- }
-#endif /* WOLFSSL_TRUST_PEER_CERT */
- else if (type == CERT_TYPE) {
- if (ssl) {
- /* Make sure previous is free'd */
- if (ssl->buffers.weOwnCert) {
- FreeDer(&ssl->buffers.certificate);
- #ifdef KEEP_OUR_CERT
- FreeX509(ssl->ourCert);
- if (ssl->ourCert) {
- XFREE(ssl->ourCert, ssl->heap, DYNAMIC_TYPE_X509);
- ssl->ourCert = NULL;
- }
- #endif
- }
- ssl->buffers.certificate = der;
- #ifdef KEEP_OUR_CERT
- ssl->keepCert = 1; /* hold cert for ssl lifetime */
- #endif
- ssl->buffers.weOwnCert = 1;
- }
- else if (ctx) {
- FreeDer(&ctx->certificate); /* Make sure previous is free'd */
- #ifdef KEEP_OUR_CERT
- if (ctx->ourCert) {
- if (ctx->ownOurCert) {
- FreeX509(ctx->ourCert);
- XFREE(ctx->ourCert, ctx->heap, DYNAMIC_TYPE_X509);
- }
- ctx->ourCert = NULL;
- }
- #endif
- ctx->certificate = der;
- }
- }
- else if (type == PRIVATEKEY_TYPE) {
- if (ssl) {
- /* Make sure previous is free'd */
- if (ssl->buffers.weOwnKey) {
- FreeDer(&ssl->buffers.key);
- }
- ssl->buffers.key = der;
- ssl->buffers.weOwnKey = 1;
- }
- else if (ctx) {
- FreeDer(&ctx->privateKey);
- ctx->privateKey = der;
- }
- }
- else {
- FreeDer(&der);
- return WOLFSSL_BAD_CERTTYPE;
- }
-
- if (done == 1) {
- /* No operation, just skip the next section */
- }
- else if (type == PRIVATEKEY_TYPE && format != WOLFSSL_FILETYPE_RAW) {
- #if defined(WOLFSSL_ENCRYPTED_KEYS) || defined(HAVE_PKCS8)
- keyFormat = algId;
- #endif
-
- ret = ProcessBufferTryDecode(ctx, ssl, der, &keySz, &idx, &resetSuites,
- &keyFormat, heap, devId);
-
- #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_PWDBASED)
- /* for WOLFSSL_FILETYPE_PEM, PemToDer manages the decryption */
- /* If private key type PKCS8 header wasn't already removed (algoId == 0) */
- if ((ret != 0 || keyFormat == 0)
- && format != WOLFSSL_FILETYPE_PEM && info->passwd_cb && algId == 0)
- {
- int passwordSz = NAME_SZ;
- #ifndef WOLFSSL_SMALL_STACK
- char password[NAME_SZ];
- #else
- char* password = (char*)XMALLOC(passwordSz, heap, DYNAMIC_TYPE_STRING);
- if (password == NULL) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO);
- #endif
- FreeDer(&der);
- return MEMORY_E;
- }
- #endif
- /* get password */
- ret = info->passwd_cb(password, passwordSz, PEM_PASS_READ,
- info->passwd_userdata);
- if (ret >= 0) {
- passwordSz = ret;
-
- /* PKCS8 decrypt */
- ret = ToTraditionalEnc(der->buffer, der->length,
- password, passwordSz, &algId);
- if (ret >= 0) {
- der->length = ret;
- }
- /* ignore failures and try parsing as unencrypted */
-
- ForceZero(password, passwordSz);
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(password, heap, DYNAMIC_TYPE_STRING);
- #endif
- ret = ProcessBufferTryDecode(ctx, ssl, der, &keySz, &idx,
- &resetSuites, &keyFormat, heap, devId);
- }
- #endif /* WOLFSSL_ENCRYPTED_KEYS && !NO_PWDBASED */
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(info, heap, DYNAMIC_TYPE_ENCRYPTEDINFO);
- #endif
-
- if (ret != 0)
- return ret;
- if (keyFormat == 0)
- return WOLFSSL_BAD_FILE;
-
- (void)devId;
- }
- else if (type == CERT_TYPE) {
- #ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert;
- #else
- DecodedCert cert[1];
- #endif
- #ifdef HAVE_PK_CALLBACKS
- int keyType = 0;
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), heap,
- DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- return MEMORY_E;
- #endif
-
- WOLFSSL_MSG("Checking cert signature type");
- InitDecodedCert(cert, der->buffer, der->length, heap);
-
- if (DecodeToKey(cert, 0) < 0) {
- WOLFSSL_MSG("Decode to key failed");
- FreeDecodedCert(cert);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, heap, DYNAMIC_TYPE_DCERT);
- #endif
- return WOLFSSL_BAD_FILE;
- }
-
- if (ssl && ssl->options.side == WOLFSSL_SERVER_END) {
- resetSuites = 1;
- }
- if (ssl && ssl->ctx->haveECDSAsig) {
- WOLFSSL_MSG("SSL layer setting cert, CTX had ECDSA, turning off");
- ssl->options.haveECDSAsig = 0; /* may turn back on next */
- }
-
- switch (cert->signatureOID) {
- case CTC_SHAwECDSA:
- case CTC_SHA256wECDSA:
- case CTC_SHA384wECDSA:
- case CTC_SHA512wECDSA:
- WOLFSSL_MSG("ECDSA cert signature");
- if (ssl)
- ssl->options.haveECDSAsig = 1;
- else if (ctx)
- ctx->haveECDSAsig = 1;
- break;
- case CTC_ED25519:
- WOLFSSL_MSG("ED25519 cert signature");
- if (ssl)
- ssl->options.haveECDSAsig = 1;
- else if (ctx)
- ctx->haveECDSAsig = 1;
- break;
- case CTC_ED448:
- WOLFSSL_MSG("ED448 cert signature");
- if (ssl)
- ssl->options.haveECDSAsig = 1;
- else if (ctx)
- ctx->haveECDSAsig = 1;
- break;
- default:
- WOLFSSL_MSG("Not ECDSA cert signature");
- break;
- }
-
- #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
- if (ssl) {
- ssl->pkCurveOID = cert->pkCurveOID;
- #ifndef WC_STRICT_SIG
- if (cert->keyOID == ECDSAk) {
- ssl->options.haveECC = 1;
- }
- #ifdef HAVE_ED25519
- else if (cert->keyOID == ED25519k) {
- ssl->options.haveECC = 1;
- }
- #endif
- #ifdef HAVE_ED448
- else if (cert->keyOID == ED448k) {
- ssl->options.haveECC = 1;
- }
- #endif
- #else
- ssl->options.haveECC = ssl->options.haveECDSAsig;
- #endif
- }
- else if (ctx) {
- ctx->pkCurveOID = cert->pkCurveOID;
- #ifndef WC_STRICT_SIG
- if (cert->keyOID == ECDSAk) {
- ctx->haveECC = 1;
- }
- #ifdef HAVE_ED25519
- else if (cert->keyOID == ED25519k) {
- ctx->haveECC = 1;
- }
- #endif
- #ifdef HAVE_ED448
- else if (cert->keyOID == ED448k) {
- ctx->haveECC = 1;
- }
- #endif
- #else
- ctx->haveECC = ctx->haveECDSAsig;
- #endif
- }
- #endif
-
- /* check key size of cert unless specified not to */
- switch (cert->keyOID) {
- #ifndef NO_RSA
- case RSAk:
- #ifdef HAVE_PK_CALLBACKS
- keyType = rsa_sa_algo;
- #endif
- #ifdef HAVE_PKCS11
- if (ctx) {
- ctx->privateKeyType = rsa_sa_algo;
- }
- else {
- ssl->buffers.keyType = rsa_sa_algo;
- }
- #endif
- /* Determine RSA key size by parsing public key */
- idx = 0;
- ret = wc_RsaPublicKeyDecode_ex(cert->publicKey, &idx,
- cert->pubKeySize, NULL, (word32*)&keySz, NULL, NULL);
- if (ret < 0)
- break;
-
- if (ssl && !ssl->options.verifyNone) {
- if (ssl->options.minRsaKeySz < 0 ||
- keySz < (int)ssl->options.minRsaKeySz) {
- ret = RSA_KEY_SIZE_E;
- WOLFSSL_MSG("Certificate RSA key size too small");
- }
- }
- else if (ctx && !ctx->verifyNone) {
- if (ctx->minRsaKeySz < 0 ||
- keySz < (int)ctx->minRsaKeySz) {
- ret = RSA_KEY_SIZE_E;
- WOLFSSL_MSG("Certificate RSA key size too small");
- }
- }
- break;
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ECDSAk:
- #ifdef HAVE_PK_CALLBACKS
- keyType = ecc_dsa_sa_algo;
- #endif
- #ifdef HAVE_PKCS11
- if (ctx) {
- ctx->privateKeyType = ecc_dsa_sa_algo;
- }
- else {
- ssl->buffers.keyType = ecc_dsa_sa_algo;
- }
- #endif
- /* Determine ECC key size based on curve */
- keySz = wc_ecc_get_curve_size_from_id(
- wc_ecc_get_oid(cert->pkCurveOID, NULL, NULL));
-
- if (ssl && !ssl->options.verifyNone) {
- if (ssl->options.minEccKeySz < 0 ||
- keySz < (int)ssl->options.minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("Certificate ECC key size error");
- }
- }
- else if (ctx && !ctx->verifyNone) {
- if (ctx->minEccKeySz < 0 ||
- keySz < (int)ctx->minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("Certificate ECC key size error");
- }
- }
- break;
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ED25519k:
- #ifdef HAVE_PK_CALLBACKS
- keyType = ed25519_sa_algo;
- #endif
- #ifdef HAVE_PKCS11
- if (ctx) {
- ctx->privateKeyType = ed25519_sa_algo;
- }
- else {
- ssl->buffers.keyType = ed25519_sa_algo;
- }
- #endif
- /* ED25519 is fixed key size */
- keySz = ED25519_KEY_SIZE;
- if (ssl && !ssl->options.verifyNone) {
- if (ssl->options.minEccKeySz < 0 ||
- keySz < (int)ssl->options.minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("Certificate Ed key size error");
- }
- }
- else if (ctx && !ctx->verifyNone) {
- if (ctx->minEccKeySz < 0 ||
- keySz < (int)ctx->minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("Certificate ECC key size error");
- }
- }
- break;
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- case ED448k:
- #ifdef HAVE_PK_CALLBACKS
- keyType = ed448_sa_algo;
- #endif
- #ifdef HAVE_PKCS11
- if (ctx) {
- ctx->privateKeyType = ed448_sa_algo;
- }
- else {
- ssl->buffers.keyType = ed448_sa_algo;
- }
- #endif
- /* ED448 is fixed key size */
- keySz = ED448_KEY_SIZE;
- if (ssl && !ssl->options.verifyNone) {
- if (ssl->options.minEccKeySz < 0 ||
- keySz < (int)ssl->options.minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("Certificate Ed key size error");
- }
- }
- else if (ctx && !ctx->verifyNone) {
- if (ctx->minEccKeySz < 0 ||
- keySz < (int)ctx->minEccKeySz) {
- ret = ECC_KEY_SIZE_E;
- WOLFSSL_MSG("Certificate ECC key size error");
- }
- }
- break;
- #endif /* HAVE_ED448 */
-
- default:
- WOLFSSL_MSG("No key size check done on certificate");
- break; /* do no check if not a case for the key */
- }
-
- #ifdef HAVE_PK_CALLBACKS
- if (ssl && ssl->buffers.keyType == 0) {
- ssl->buffers.keyType = keyType;
- ssl->buffers.keySz = keySz;
- }
- else if (ctx && ctx->privateKeyType == 0) {
- ctx->privateKeyType = keyType;
- ctx->privateKeySz = keySz;
- }
- #endif
-
- FreeDecodedCert(cert);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, heap, DYNAMIC_TYPE_DCERT);
- #endif
-
- if (ret != 0) {
- done = 1;
- }
- }
-
- if (done == 1) {
- #ifndef NO_WOLFSSL_CM_VERIFY
- if ((type == CA_TYPE) || (type == CERT_TYPE)) {
- /* Call to over-ride status */
- if ((ctx != NULL) && (ctx->cm != NULL) &&
- (ctx->cm->verifyCallback != NULL)) {
- ret = CM_VerifyBuffer_ex(ctx->cm, buff,
- sz, format, (ret == WOLFSSL_SUCCESS ? 0 : ret));
- }
- }
- #endif /* NO_WOLFSSL_CM_VERIFY */
-
- return ret;
- }
-
-
- if (ssl && resetSuites) {
- word16 havePSK = 0;
- word16 haveRSA = 0;
-
- #ifndef NO_PSK
- if (ssl->options.havePSK) {
- havePSK = 1;
- }
- #endif
- #ifndef NO_RSA
- haveRSA = 1;
- #endif
- #ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
- #endif
-
- /* let's reset suites */
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA,
- havePSK, ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* CA PEM file for verification, may have multiple/chain certs to process */
-static int ProcessChainBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
- long sz, int format, int type, WOLFSSL* ssl, int verify)
-{
- long used = 0;
- int ret = 0;
- int gotOne = 0;
-
- WOLFSSL_MSG("Processing CA PEM file");
- while (used < sz) {
- long consumed = 0;
-
- ret = ProcessBuffer(ctx, buff + used, sz - used, format, type, ssl,
- &consumed, 0, verify);
-
- if (ret < 0) {
-#if defined(WOLFSSL_WPAS) && defined(HAVE_CRL)
- DerBuffer* der = NULL;
- EncryptedInfo info;
-
- WOLFSSL_MSG("Trying a CRL");
- if (PemToDer(buff + used, sz - used, CRL_TYPE, &der, NULL, &info,
- NULL) == 0) {
- WOLFSSL_MSG(" Processed a CRL");
- wolfSSL_CertManagerLoadCRLBuffer(ctx->cm, der->buffer,
- der->length, WOLFSSL_FILETYPE_ASN1);
- FreeDer(&der);
- used += info.consumed;
- continue;
- }
-#endif
-
- if (consumed > 0) { /* Made progress in file */
- WOLFSSL_ERROR(ret);
- WOLFSSL_MSG("CA Parse failed, with progress in file.");
- WOLFSSL_MSG("Search for other certs in file");
- }
- else {
- WOLFSSL_MSG("CA Parse failed, no progress in file.");
- WOLFSSL_MSG("Do not continue search for other certs in file");
- break;
- }
- }
- else {
- WOLFSSL_MSG(" Processed a CA");
- gotOne = 1;
- }
- used += consumed;
- }
-
- if (gotOne) {
- WOLFSSL_MSG("Processed at least one valid CA. Other stuff OK");
- return WOLFSSL_SUCCESS;
- }
- return ret;
-}
-
-
-static WC_INLINE WOLFSSL_METHOD* cm_pick_method(void)
-{
- #ifndef NO_WOLFSSL_CLIENT
- #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3)
- return wolfSSLv3_client_method();
- #elif !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
- return wolfTLSv1_client_method();
- #elif !defined(NO_OLD_TLS)
- return wolfTLSv1_1_client_method();
- #elif !defined(WOLFSSL_NO_TLS12)
- return wolfTLSv1_2_client_method();
- #elif defined(WOLFSSL_TLS13)
- return wolfTLSv1_3_client_method();
- #else
- return NULL;
- #endif
- #elif !defined(NO_WOLFSSL_SERVER)
- #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3)
- return wolfSSLv3_server_method();
- #elif !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
- return wolfTLSv1_server_method();
- #elif !defined(NO_OLD_TLS)
- return wolfTLSv1_1_server_method();
- #elif !defined(WOLFSSL_NO_TLS12)
- return wolfTLSv1_2_server_method();
- #elif defined(WOLFSSL_TLS13)
- return wolfTLSv1_3_server_method();
- #else
- return NULL;
- #endif
- #else
- return NULL;
- #endif
-}
-
-
-/* like load verify locations, 1 for success, < 0 for error */
-int wolfSSL_CertManagerLoadCABuffer(WOLFSSL_CERT_MANAGER* cm,
- const unsigned char* in, long sz, int format)
-{
- int ret = WOLFSSL_FATAL_ERROR;
- WOLFSSL_CTX* tmp;
-
- WOLFSSL_ENTER("wolfSSL_CertManagerLoadCABuffer");
-
- if (cm == NULL) {
- WOLFSSL_MSG("No CertManager error");
- return ret;
- }
- tmp = wolfSSL_CTX_new(cm_pick_method());
-
- if (tmp == NULL) {
- WOLFSSL_MSG("CTX new failed");
- return ret;
- }
-
- /* for tmp use */
- wolfSSL_CertManagerFree(tmp->cm);
- tmp->cm = cm;
-
- ret = wolfSSL_CTX_load_verify_buffer(tmp, in, sz, format);
-
- /* don't loose our good one */
- tmp->cm = NULL;
- wolfSSL_CTX_free(tmp);
-
- return ret;
-}
-
-#ifdef HAVE_CRL
-
-int wolfSSL_CertManagerLoadCRLBuffer(WOLFSSL_CERT_MANAGER* cm,
- const unsigned char* buff, long sz, int type)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerLoadCRLBuffer");
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- if (cm->crl == NULL) {
- if (wolfSSL_CertManagerEnableCRL(cm, 0) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Enable CRL failed");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- return BufferLoadCRL(cm->crl, buff, sz, type, VERIFY);
-}
-
-int wolfSSL_CertManagerFreeCRL(WOLFSSL_CERT_MANAGER* cm)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerFreeCRL");
- if (cm == NULL)
- return BAD_FUNC_ARG;
- if (cm->crl != NULL){
- FreeCRL(cm->crl, 1);
- cm->crl = NULL;
- }
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_CTX_LoadCRLBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
- long sz, int type)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_LoadCRLBuffer");
-
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- return wolfSSL_CertManagerLoadCRLBuffer(ctx->cm, buff, sz, type);
-}
-
-
-int wolfSSL_LoadCRLBuffer(WOLFSSL* ssl, const unsigned char* buff,
- long sz, int type)
-{
- WOLFSSL_ENTER("wolfSSL_LoadCRLBuffer");
-
- if (ssl == NULL || ssl->ctx == NULL)
- return BAD_FUNC_ARG;
-
- return wolfSSL_CertManagerLoadCRLBuffer(ssl->ctx->cm, buff, sz, type);
-}
-
-
-#endif /* HAVE_CRL */
-
-/* turn on CRL if off and compiled in, set options */
-int wolfSSL_CertManagerEnableCRL(WOLFSSL_CERT_MANAGER* cm, int options)
-{
- int ret = WOLFSSL_SUCCESS;
-
- (void)options;
-
- WOLFSSL_ENTER("wolfSSL_CertManagerEnableCRL");
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- #ifdef HAVE_CRL
- if (cm->crl == NULL) {
- cm->crl = (WOLFSSL_CRL*)XMALLOC(sizeof(WOLFSSL_CRL), cm->heap,
- DYNAMIC_TYPE_CRL);
- if (cm->crl == NULL)
- return MEMORY_E;
-
- if (InitCRL(cm->crl, cm) != 0) {
- WOLFSSL_MSG("Init CRL failed");
- FreeCRL(cm->crl, 1);
- cm->crl = NULL;
- return WOLFSSL_FAILURE;
- }
-
- #ifdef HAVE_CRL_IO
- cm->crl->crlIOCb = EmbedCrlLookup;
- #endif
- }
-
- cm->crlEnabled = 1;
- if (options & WOLFSSL_CRL_CHECKALL)
- cm->crlCheckAll = 1;
- #else
- ret = NOT_COMPILED_IN;
- #endif
-
- return ret;
-}
-
-
-int wolfSSL_CertManagerDisableCRL(WOLFSSL_CERT_MANAGER* cm)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerDisableCRL");
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- cm->crlEnabled = 0;
-
- return WOLFSSL_SUCCESS;
-}
-
-#ifndef NO_WOLFSSL_CM_VERIFY
-void wolfSSL_CertManagerSetVerify(WOLFSSL_CERT_MANAGER* cm, VerifyCallback vc)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerSetVerify");
- if (cm == NULL)
- return;
-
- cm->verifyCallback = vc;
-}
-#endif /* NO_WOLFSSL_CM_VERIFY */
-
-/* Verify the certificate, WOLFSSL_SUCCESS for ok, < 0 for error */
-int CM_VerifyBuffer_ex(WOLFSSL_CERT_MANAGER* cm, const byte* buff,
- long sz, int format, int err_val)
-{
- int ret = 0;
- DerBuffer* der = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert;
-#else
- DecodedCert cert[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_CertManagerVerifyBuffer");
-
-#ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), cm->heap,
- DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- return MEMORY_E;
-#endif
-
- if (format == WOLFSSL_FILETYPE_PEM) {
-#ifdef WOLFSSL_PEM_TO_DER
- ret = PemToDer(buff, sz, CERT_TYPE, &der, cm->heap, NULL, NULL);
- if (ret != 0) {
- FreeDer(&der);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
- #endif
- return ret;
- }
- InitDecodedCert(cert, der->buffer, der->length, cm->heap);
-#else
- ret = NOT_COMPILED_IN;
-#endif
- }
- else {
- InitDecodedCert(cert, (byte*)buff, (word32)sz, cm->heap);
- }
-
- if (ret == 0)
- ret = ParseCertRelative(cert, CERT_TYPE, 1, cm);
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- /* ret needs to be self-singer error for Qt compat */
- if (ret == ASN_NO_SIGNER_E && cert->selfSigned)
- ret = ASN_SELF_SIGNED_E;
-#endif
-
-#ifdef HAVE_CRL
- if (ret == 0 && cm->crlEnabled)
- ret = CheckCertCRL(cm->crl, cert);
-#endif
-
-#ifndef NO_WOLFSSL_CM_VERIFY
- /* if verify callback has been set */
- if (cm->verifyCallback) {
- buffer certBuf;
- #ifdef WOLFSSL_SMALL_STACK
- ProcPeerCertArgs* args;
- args = (ProcPeerCertArgs*)XMALLOC(
- sizeof(ProcPeerCertArgs), cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (args == NULL) {
- XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
- return MEMORY_E;
- }
- #else
- ProcPeerCertArgs args[1];
- #endif
-
- certBuf.buffer = (byte*)buff;
- certBuf.length = (unsigned int)sz;
- XMEMSET(args, 0, sizeof(ProcPeerCertArgs));
-
- args->totalCerts = 1;
- args->certs = &certBuf;
- args->dCert = cert;
- args->dCertInit = 1;
-
- if (err_val != 0) {
- ret = err_val;
- }
- ret = DoVerifyCallback(cm, NULL, ret, args);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(args, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- }
-#else
- (void)err_val;
-#endif
-
- FreeDecodedCert(cert);
- FreeDer(&der);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, cm->heap, DYNAMIC_TYPE_DCERT);
-#endif
-
- return ret == 0 ? WOLFSSL_SUCCESS : ret;
-}
-
-/* Verify the certificate, WOLFSSL_SUCCESS for ok, < 0 for error */
-int wolfSSL_CertManagerVerifyBuffer(WOLFSSL_CERT_MANAGER* cm, const byte* buff,
- long sz, int format)
-{
- return CM_VerifyBuffer_ex(cm, buff, sz, format, 0);
-}
-/* turn on OCSP if off and compiled in, set options */
-int wolfSSL_CertManagerEnableOCSP(WOLFSSL_CERT_MANAGER* cm, int options)
-{
- int ret = WOLFSSL_SUCCESS;
-
- (void)options;
-
- WOLFSSL_ENTER("wolfSSL_CertManagerEnableOCSP");
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- #ifdef HAVE_OCSP
- if (cm->ocsp == NULL) {
- cm->ocsp = (WOLFSSL_OCSP*)XMALLOC(sizeof(WOLFSSL_OCSP), cm->heap,
- DYNAMIC_TYPE_OCSP);
- if (cm->ocsp == NULL)
- return MEMORY_E;
-
- if (InitOCSP(cm->ocsp, cm) != 0) {
- WOLFSSL_MSG("Init OCSP failed");
- FreeOCSP(cm->ocsp, 1);
- cm->ocsp = NULL;
- return WOLFSSL_FAILURE;
- }
- }
- cm->ocspEnabled = 1;
- if (options & WOLFSSL_OCSP_URL_OVERRIDE)
- cm->ocspUseOverrideURL = 1;
- if (options & WOLFSSL_OCSP_NO_NONCE)
- cm->ocspSendNonce = 0;
- else
- cm->ocspSendNonce = 1;
- if (options & WOLFSSL_OCSP_CHECKALL)
- cm->ocspCheckAll = 1;
- #ifndef WOLFSSL_USER_IO
- cm->ocspIOCb = EmbedOcspLookup;
- cm->ocspRespFreeCb = EmbedOcspRespFree;
- cm->ocspIOCtx = cm->heap;
- #endif /* WOLFSSL_USER_IO */
- #else
- ret = NOT_COMPILED_IN;
- #endif
-
- return ret;
-}
-
-
-int wolfSSL_CertManagerDisableOCSP(WOLFSSL_CERT_MANAGER* cm)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerDisableOCSP");
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- cm->ocspEnabled = 0;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* turn on OCSP Stapling if off and compiled in, set options */
-int wolfSSL_CertManagerEnableOCSPStapling(WOLFSSL_CERT_MANAGER* cm)
-{
- int ret = WOLFSSL_SUCCESS;
-
- WOLFSSL_ENTER("wolfSSL_CertManagerEnableOCSPStapling");
-
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
-#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
- || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
- #ifndef NO_WOLFSSL_SERVER
- if (cm->ocsp_stapling == NULL) {
- cm->ocsp_stapling = (WOLFSSL_OCSP*)XMALLOC(sizeof(WOLFSSL_OCSP),
- cm->heap, DYNAMIC_TYPE_OCSP);
- if (cm->ocsp_stapling == NULL)
- return MEMORY_E;
-
- if (InitOCSP(cm->ocsp_stapling, cm) != 0) {
- WOLFSSL_MSG("Init OCSP failed");
- FreeOCSP(cm->ocsp_stapling, 1);
- cm->ocsp_stapling = NULL;
- return WOLFSSL_FAILURE;
- }
- }
-
- #ifndef WOLFSSL_USER_IO
- cm->ocspIOCb = EmbedOcspLookup;
- cm->ocspRespFreeCb = EmbedOcspRespFree;
- cm->ocspIOCtx = cm->heap;
- #endif /* WOLFSSL_USER_IO */
- #endif /* NO_WOLFSSL_SERVER */
- cm->ocspStaplingEnabled = 1;
-#else
- ret = NOT_COMPILED_IN;
-#endif
-
- return ret;
-}
-
-int wolfSSL_CertManagerDisableOCSPStapling(WOLFSSL_CERT_MANAGER* cm)
-{
- int ret = WOLFSSL_SUCCESS;
-
- WOLFSSL_ENTER("wolfSSL_CertManagerDisableOCSPStapling");
-
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
-#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
- || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
- cm->ocspStaplingEnabled = 0;
-#else
- ret = NOT_COMPILED_IN;
-#endif
- return ret;
-}
-
-#ifdef HAVE_OCSP
-/* check CRL if enabled, WOLFSSL_SUCCESS */
-int wolfSSL_CertManagerCheckOCSP(WOLFSSL_CERT_MANAGER* cm, byte* der, int sz)
-{
- int ret;
-#ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert = NULL;
-#else
- DecodedCert cert[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_CertManagerCheckOCSP");
-
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- if (cm->ocspEnabled == 0)
- return WOLFSSL_SUCCESS;
-
-#ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- return MEMORY_E;
-#endif
-
- InitDecodedCert(cert, der, sz, NULL);
-
- if ((ret = ParseCertRelative(cert, CERT_TYPE, VERIFY_OCSP, cm)) != 0) {
- WOLFSSL_MSG("ParseCert failed");
- }
- else if ((ret = CheckCertOCSP(cm->ocsp, cert, NULL)) != 0) {
- WOLFSSL_MSG("CheckCertOCSP failed");
- }
-
- FreeDecodedCert(cert);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
-#endif
-
- return ret == 0 ? WOLFSSL_SUCCESS : ret;
-}
-
-WOLFSSL_API int wolfSSL_CertManagerCheckOCSPResponse(WOLFSSL_CERT_MANAGER *cm,
- byte *response, int responseSz, buffer *responseBuffer,
- CertStatus *status, OcspEntry *entry, OcspRequest *ocspRequest)
-{
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_CertManagerCheckOCSP_Staple");
- if (cm == NULL || response == NULL)
- return BAD_FUNC_ARG;
- if (cm->ocspEnabled == 0)
- return WOLFSSL_SUCCESS;
-
- ret = CheckOcspResponse(cm->ocsp, response, responseSz, responseBuffer, status,
- entry, ocspRequest);
-
- return ret == 0 ? WOLFSSL_SUCCESS : ret;
-}
-
-int wolfSSL_CertManagerSetOCSPOverrideURL(WOLFSSL_CERT_MANAGER* cm,
- const char* url)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerSetOCSPOverrideURL");
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- XFREE(cm->ocspOverrideURL, cm->heap, DYNAMIC_TYPE_URL);
- if (url != NULL) {
- int urlSz = (int)XSTRLEN(url) + 1;
- cm->ocspOverrideURL = (char*)XMALLOC(urlSz, cm->heap, DYNAMIC_TYPE_URL);
- if (cm->ocspOverrideURL != NULL) {
- XMEMCPY(cm->ocspOverrideURL, url, urlSz);
- }
- else
- return MEMORY_E;
- }
- else
- cm->ocspOverrideURL = NULL;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_CertManagerSetOCSP_Cb(WOLFSSL_CERT_MANAGER* cm,
- CbOCSPIO ioCb, CbOCSPRespFree respFreeCb, void* ioCbCtx)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerSetOCSP_Cb");
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- cm->ocspIOCb = ioCb;
- cm->ocspRespFreeCb = respFreeCb;
- cm->ocspIOCtx = ioCbCtx;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_EnableOCSP(WOLFSSL* ssl, int options)
-{
- WOLFSSL_ENTER("wolfSSL_EnableOCSP");
- if (ssl)
- return wolfSSL_CertManagerEnableOCSP(ssl->ctx->cm, options);
- else
- return BAD_FUNC_ARG;
-}
-
-int wolfSSL_DisableOCSP(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_DisableOCSP");
- if (ssl)
- return wolfSSL_CertManagerDisableOCSP(ssl->ctx->cm);
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_EnableOCSPStapling(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_EnableOCSPStapling");
- if (ssl)
- return wolfSSL_CertManagerEnableOCSPStapling(ssl->ctx->cm);
- else
- return BAD_FUNC_ARG;
-}
-
-int wolfSSL_DisableOCSPStapling(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_DisableOCSPStapling");
- if (ssl)
- return wolfSSL_CertManagerDisableOCSPStapling(ssl->ctx->cm);
- else
- return BAD_FUNC_ARG;
-}
-
-int wolfSSL_SetOCSP_OverrideURL(WOLFSSL* ssl, const char* url)
-{
- WOLFSSL_ENTER("wolfSSL_SetOCSP_OverrideURL");
- if (ssl)
- return wolfSSL_CertManagerSetOCSPOverrideURL(ssl->ctx->cm, url);
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_SetOCSP_Cb(WOLFSSL* ssl,
- CbOCSPIO ioCb, CbOCSPRespFree respFreeCb, void* ioCbCtx)
-{
- WOLFSSL_ENTER("wolfSSL_SetOCSP_Cb");
- if (ssl) {
- ssl->ocspIOCtx = ioCbCtx; /* use SSL specific ioCbCtx */
- return wolfSSL_CertManagerSetOCSP_Cb(ssl->ctx->cm,
- ioCb, respFreeCb, NULL);
- }
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_CTX_EnableOCSP(WOLFSSL_CTX* ctx, int options)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_EnableOCSP");
- if (ctx)
- return wolfSSL_CertManagerEnableOCSP(ctx->cm, options);
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_CTX_DisableOCSP(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_DisableOCSP");
- if (ctx)
- return wolfSSL_CertManagerDisableOCSP(ctx->cm);
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_CTX_SetOCSP_OverrideURL(WOLFSSL_CTX* ctx, const char* url)
-{
- WOLFSSL_ENTER("wolfSSL_SetOCSP_OverrideURL");
- if (ctx)
- return wolfSSL_CertManagerSetOCSPOverrideURL(ctx->cm, url);
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_CTX_SetOCSP_Cb(WOLFSSL_CTX* ctx, CbOCSPIO ioCb,
- CbOCSPRespFree respFreeCb, void* ioCbCtx)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_SetOCSP_Cb");
- if (ctx)
- return wolfSSL_CertManagerSetOCSP_Cb(ctx->cm, ioCb,
- respFreeCb, ioCbCtx);
- else
- return BAD_FUNC_ARG;
-}
-
-#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
- || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
-int wolfSSL_CTX_EnableOCSPStapling(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_EnableOCSPStapling");
- if (ctx)
- return wolfSSL_CertManagerEnableOCSPStapling(ctx->cm);
- else
- return BAD_FUNC_ARG;
-}
-
-int wolfSSL_CTX_DisableOCSPStapling(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_DisableOCSPStapling");
- if (ctx)
- return wolfSSL_CertManagerDisableOCSPStapling(ctx->cm);
- else
- return BAD_FUNC_ARG;
-}
-#endif /* HAVE_CERTIFICATE_STATUS_REQUEST || HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
-
-#endif /* HAVE_OCSP */
-
-/* macro to get verify settings for AddCA */
-#define GET_VERIFY_SETTING_CTX(ctx) \
- (ctx && ctx->verifyNone ? NO_VERIFY : VERIFY)
-#define GET_VERIFY_SETTING_SSL(ssl) \
- (ssl && ssl->options.verifyNone ? NO_VERIFY : VERIFY)
-
-#ifndef NO_FILESYSTEM
-
-/* process a file with name fname into ctx of format and type
- userChain specifies a user certificate chain to pass during handshake */
-int ProcessFile(WOLFSSL_CTX* ctx, const char* fname, int format, int type,
- WOLFSSL* ssl, int userChain, WOLFSSL_CRL* crl, int verify)
-{
-#ifdef WOLFSSL_SMALL_STACK
- byte staticBuffer[1]; /* force heap usage */
-#else
- byte staticBuffer[FILE_BUFFER_SIZE];
-#endif
- byte* myBuffer = staticBuffer;
- int dynamic = 0;
- int ret;
- long sz = 0;
- XFILE file;
- void* heapHint = wolfSSL_CTX_GetHeap(ctx, ssl);
- const char* header = NULL;
- const char* footer = NULL;
-
- (void)crl;
- (void)heapHint;
-
- if (fname == NULL) return WOLFSSL_BAD_FILE;
-
- file = XFOPEN(fname, "rb");
- if (file == XBADFILE) return WOLFSSL_BAD_FILE;
- if (XFSEEK(file, 0, XSEEK_END) != 0) {
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
- sz = XFTELL(file);
- XREWIND(file);
-
- if (sz > MAX_WOLFSSL_FILE_SIZE || sz <= 0) {
- WOLFSSL_MSG("ProcessFile file size error");
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
-
- if (sz > (long)sizeof(staticBuffer)) {
- WOLFSSL_MSG("Getting dynamic buffer");
- myBuffer = (byte*)XMALLOC(sz, heapHint, DYNAMIC_TYPE_FILE);
- if (myBuffer == NULL) {
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
- dynamic = 1;
- }
-
- if ( (ret = (int)XFREAD(myBuffer, 1, sz, file)) != sz)
- ret = WOLFSSL_BAD_FILE;
- else {
- /* Try to detect type by parsing cert header and footer */
- if (type == DETECT_CERT_TYPE) {
- if (wc_PemGetHeaderFooter(CA_TYPE, &header, &footer) == 0 &&
- (XSTRNSTR((char*)myBuffer, header, (int)sz) != NULL)) {
- type = CA_TYPE;
- }
-#ifdef HAVE_CRL
- else if (wc_PemGetHeaderFooter(CRL_TYPE, &header, &footer) == 0 &&
- (XSTRNSTR((char*)myBuffer, header, (int)sz) != NULL)) {
- type = CRL_TYPE;
- }
-#endif
- else if (wc_PemGetHeaderFooter(CERT_TYPE, &header, &footer) == 0 &&
- (XSTRNSTR((char*)myBuffer, header, (int)sz) != NULL)) {
- type = CERT_TYPE;
- }
- else {
- WOLFSSL_MSG("Failed to detect certificate type");
- if (dynamic)
- XFREE(myBuffer, heapHint, DYNAMIC_TYPE_FILE);
- XFCLOSE(file);
- return WOLFSSL_BAD_CERTTYPE;
- }
- }
- if ((type == CA_TYPE || type == TRUSTED_PEER_TYPE)
- && format == WOLFSSL_FILETYPE_PEM) {
- ret = ProcessChainBuffer(ctx, myBuffer, sz, format, type, ssl,
- verify);
- }
-#ifdef HAVE_CRL
- else if (type == CRL_TYPE)
- ret = BufferLoadCRL(crl, myBuffer, sz, format, verify);
-#endif
- else
- ret = ProcessBuffer(ctx, myBuffer, sz, format, type, ssl, NULL,
- userChain, verify);
- }
-
- XFCLOSE(file);
- if (dynamic)
- XFREE(myBuffer, heapHint, DYNAMIC_TYPE_FILE);
-
- return ret;
-}
-
-/* loads file then loads each file in path, no c_rehash */
-int wolfSSL_CTX_load_verify_locations_ex(WOLFSSL_CTX* ctx, const char* file,
- const char* path, word32 flags)
-{
- int ret = WOLFSSL_SUCCESS;
-#ifndef NO_WOLFSSL_DIR
- int fileRet;
- int successCount = 0;
- int failCount = 0;
-#endif
- int verify;
-
- WOLFSSL_MSG("wolfSSL_CTX_load_verify_locations_ex");
-
- if (ctx == NULL || (file == NULL && path == NULL)) {
- return WOLFSSL_FAILURE;
- }
-
- verify = GET_VERIFY_SETTING_CTX(ctx);
- if (flags & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY)
- verify = VERIFY_SKIP_DATE;
-
- if (file) {
- ret = ProcessFile(ctx, file, WOLFSSL_FILETYPE_PEM, CA_TYPE, NULL, 0,
- NULL, verify);
-#ifndef NO_WOLFSSL_DIR
- if (ret == WOLFSSL_SUCCESS)
- successCount++;
-#endif
- }
-
- if (ret == WOLFSSL_SUCCESS && path) {
-#ifndef NO_WOLFSSL_DIR
- char* name = NULL;
- #ifdef WOLFSSL_SMALL_STACK
- ReadDirCtx* readCtx;
- readCtx = (ReadDirCtx*)XMALLOC(sizeof(ReadDirCtx), ctx->heap,
- DYNAMIC_TYPE_DIRCTX);
- if (readCtx == NULL)
- return MEMORY_E;
- #else
- ReadDirCtx readCtx[1];
- #endif
-
- /* try to load each regular file in path */
- fileRet = wc_ReadDirFirst(readCtx, path, &name);
- while (fileRet == 0 && name) {
- WOLFSSL_MSG(name); /* log file name */
- ret = ProcessFile(ctx, name, WOLFSSL_FILETYPE_PEM, CA_TYPE,
- NULL, 0, NULL, verify);
- if (ret != WOLFSSL_SUCCESS) {
- /* handle flags for ignoring errors, skipping expired certs or
- by PEM certificate header error */
- if ( (flags & WOLFSSL_LOAD_FLAG_IGNORE_ERR) ||
- ((flags & WOLFSSL_LOAD_FLAG_PEM_CA_ONLY) &&
- (ret == ASN_NO_PEM_HEADER))) {
- /* Do not fail here if a certificate fails to load,
- continue to next file */
- ret = WOLFSSL_SUCCESS;
- }
- else {
- WOLFSSL_ERROR(ret);
- WOLFSSL_MSG("Load CA file failed, continuing");
- failCount++;
- }
- }
- else {
- successCount++;
- }
- fileRet = wc_ReadDirNext(readCtx, path, &name);
- }
- wc_ReadDirClose(readCtx);
-
- /* pass directory read failure to response code */
- if (fileRet != WC_READDIR_NOFILE) {
- ret = fileRet;
- }
- /* report failure if no files were loaded or there were failures */
- else if (successCount == 0 || failCount > 0) {
- /* use existing error code if exists */
- if (ret == WOLFSSL_SUCCESS)
- ret = WOLFSSL_FAILURE;
- }
- else {
- ret = WOLFSSL_SUCCESS;
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(readCtx, ctx->heap, DYNAMIC_TYPE_DIRCTX);
- #endif
-#else
- ret = NOT_COMPILED_IN;
- (void)flags;
-#endif
- }
-
- return ret;
-}
-
-WOLFSSL_ABI
-int wolfSSL_CTX_load_verify_locations(WOLFSSL_CTX* ctx, const char* file,
- const char* path)
-{
- return wolfSSL_CTX_load_verify_locations_ex(ctx, file, path,
- WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS);
-}
-
-
-#ifdef WOLFSSL_TRUST_PEER_CERT
-/* Used to specify a peer cert to match when connecting
- ctx : the ctx structure to load in peer cert
- file: the string name of cert file
- type: type of format such as PEM/DER
- */
-int wolfSSL_CTX_trust_peer_cert(WOLFSSL_CTX* ctx, const char* file, int type)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_trust_peer_cert");
-
- if (ctx == NULL || file == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- return ProcessFile(ctx, file, type, TRUSTED_PEER_TYPE, NULL, 0, NULL,
- GET_VERIFY_SETTING_CTX(ctx));
-}
-#endif /* WOLFSSL_TRUST_PEER_CERT */
-
-
-/* Verify the certificate, WOLFSSL_SUCCESS for ok, < 0 for error */
-int wolfSSL_CertManagerVerify(WOLFSSL_CERT_MANAGER* cm, const char* fname,
- int format)
-{
- int ret = WOLFSSL_FATAL_ERROR;
-#ifdef WOLFSSL_SMALL_STACK
- byte staticBuffer[1]; /* force heap usage */
-#else
- byte staticBuffer[FILE_BUFFER_SIZE];
-#endif
- byte* myBuffer = staticBuffer;
- int dynamic = 0;
- long sz = 0;
- XFILE file = XFOPEN(fname, "rb");
-
- WOLFSSL_ENTER("wolfSSL_CertManagerVerify");
-
- if (file == XBADFILE) return WOLFSSL_BAD_FILE;
- if(XFSEEK(file, 0, XSEEK_END) != 0) {
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
- sz = XFTELL(file);
- XREWIND(file);
-
- if (sz > MAX_WOLFSSL_FILE_SIZE || sz <= 0) {
- WOLFSSL_MSG("CertManagerVerify file size error");
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
-
- if (sz > (long)sizeof(staticBuffer)) {
- WOLFSSL_MSG("Getting dynamic buffer");
- myBuffer = (byte*) XMALLOC(sz, cm->heap, DYNAMIC_TYPE_FILE);
- if (myBuffer == NULL) {
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
- dynamic = 1;
- }
-
- if ( (ret = (int)XFREAD(myBuffer, 1, sz, file)) != sz)
- ret = WOLFSSL_BAD_FILE;
- else
- ret = wolfSSL_CertManagerVerifyBuffer(cm, myBuffer, sz, format);
-
- XFCLOSE(file);
- if (dynamic)
- XFREE(myBuffer, cm->heap, DYNAMIC_TYPE_FILE);
-
- return ret;
-}
-
-
-/* like load verify locations, 1 for success, < 0 for error */
-int wolfSSL_CertManagerLoadCA(WOLFSSL_CERT_MANAGER* cm, const char* file,
- const char* path)
-{
- int ret = WOLFSSL_FATAL_ERROR;
- WOLFSSL_CTX* tmp;
-
- WOLFSSL_ENTER("wolfSSL_CertManagerLoadCA");
-
- if (cm == NULL) {
- WOLFSSL_MSG("No CertManager error");
- return ret;
- }
- tmp = wolfSSL_CTX_new(cm_pick_method());
-
- if (tmp == NULL) {
- WOLFSSL_MSG("CTX new failed");
- return ret;
- }
-
- /* for tmp use */
- wolfSSL_CertManagerFree(tmp->cm);
- tmp->cm = cm;
-
- ret = wolfSSL_CTX_load_verify_locations(tmp, file, path);
-
- /* don't lose our good one */
- tmp->cm = NULL;
- wolfSSL_CTX_free(tmp);
-
- return ret;
-}
-
-#ifndef NO_CHECK_PRIVATE_KEY
-/* Check private against public in certificate for match
- *
- * ctx WOLFSSL_CTX structure to check private key in
- *
- * Returns SSL_SUCCESS on good private key and SSL_FAILURE if miss matched. */
-int wolfSSL_CTX_check_private_key(const WOLFSSL_CTX* ctx)
-{
-#ifdef WOLFSSL_SMALL_STACK
- DecodedCert* der = NULL;
-#else
- DecodedCert der[1];
-#endif
- word32 size;
- byte* buff;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_CTX_check_private_key");
-
- if (ctx == NULL) {
- return WOLFSSL_FAILURE;
- }
-
-#ifndef NO_CERTS
-#ifdef WOLFSSL_SMALL_STACK
- der = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, DYNAMIC_TYPE_DCERT);
- if (der == NULL)
- return MEMORY_E;
-#endif
-
- size = ctx->certificate->length;
- buff = ctx->certificate->buffer;
- InitDecodedCert(der, buff, size, ctx->heap);
- if (ParseCertRelative(der, CERT_TYPE, NO_VERIFY, NULL) != 0) {
- FreeDecodedCert(der);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(der, NULL, DYNAMIC_TYPE_DCERT);
- #endif
- return WOLFSSL_FAILURE;
- }
-
- size = ctx->privateKey->length;
- buff = ctx->privateKey->buffer;
- ret = wc_CheckPrivateKey(buff, size, der);
- FreeDecodedCert(der);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(der, NULL, DYNAMIC_TYPE_DCERT);
-#endif
-
- if (ret == 1) {
- return WOLFSSL_SUCCESS;
- }
- else {
- return WOLFSSL_FAILURE;
- }
-#else
- WOLFSSL_MSG("NO_CERTS is defined, can not check private key");
- return WOLFSSL_FAILURE;
-#endif
-}
-#endif /* !NO_CHECK_PRIVATE_KEY */
-
-
-#ifdef HAVE_CRL
-
-/* check CRL if enabled, WOLFSSL_SUCCESS */
-int wolfSSL_CertManagerCheckCRL(WOLFSSL_CERT_MANAGER* cm, byte* der, int sz)
-{
- int ret = 0;
-#ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert = NULL;
-#else
- DecodedCert cert[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_CertManagerCheckCRL");
-
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- if (cm->crlEnabled == 0)
- return WOLFSSL_SUCCESS;
-
-#ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- return MEMORY_E;
-#endif
-
- InitDecodedCert(cert, der, sz, NULL);
-
- if ((ret = ParseCertRelative(cert, CERT_TYPE, VERIFY_CRL, cm)) != 0) {
- WOLFSSL_MSG("ParseCert failed");
- }
- else if ((ret = CheckCertCRL(cm->crl, cert)) != 0) {
- WOLFSSL_MSG("CheckCertCRL failed");
- }
-
- FreeDecodedCert(cert);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
-#endif
-
- return ret == 0 ? WOLFSSL_SUCCESS : ret;
-}
-
-
-int wolfSSL_CertManagerSetCRL_Cb(WOLFSSL_CERT_MANAGER* cm, CbMissingCRL cb)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerSetCRL_Cb");
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- cm->cbMissingCRL = cb;
-
- return WOLFSSL_SUCCESS;
-}
-
-#ifdef HAVE_CRL_IO
-int wolfSSL_CertManagerSetCRL_IOCb(WOLFSSL_CERT_MANAGER* cm, CbCrlIO cb)
-{
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- cm->crl->crlIOCb = cb;
-
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-int wolfSSL_CertManagerLoadCRL(WOLFSSL_CERT_MANAGER* cm, const char* path,
- int type, int monitor)
-{
- WOLFSSL_ENTER("wolfSSL_CertManagerLoadCRL");
- if (cm == NULL)
- return BAD_FUNC_ARG;
-
- if (cm->crl == NULL) {
- if (wolfSSL_CertManagerEnableCRL(cm, 0) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Enable CRL failed");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- return LoadCRL(cm->crl, path, type, monitor);
-}
-
-int wolfSSL_EnableCRL(WOLFSSL* ssl, int options)
-{
- WOLFSSL_ENTER("wolfSSL_EnableCRL");
- if (ssl)
- return wolfSSL_CertManagerEnableCRL(ssl->ctx->cm, options);
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_DisableCRL(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_DisableCRL");
- if (ssl)
- return wolfSSL_CertManagerDisableCRL(ssl->ctx->cm);
- else
- return BAD_FUNC_ARG;
-}
-
-int wolfSSL_LoadCRL(WOLFSSL* ssl, const char* path, int type, int monitor)
-{
- WOLFSSL_ENTER("wolfSSL_LoadCRL");
- if (ssl)
- return wolfSSL_CertManagerLoadCRL(ssl->ctx->cm, path, type, monitor);
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_SetCRL_Cb(WOLFSSL* ssl, CbMissingCRL cb)
-{
- WOLFSSL_ENTER("wolfSSL_SetCRL_Cb");
- if (ssl)
- return wolfSSL_CertManagerSetCRL_Cb(ssl->ctx->cm, cb);
- else
- return BAD_FUNC_ARG;
-}
-
-#ifdef HAVE_CRL_IO
-int wolfSSL_SetCRL_IOCb(WOLFSSL* ssl, CbCrlIO cb)
-{
- WOLFSSL_ENTER("wolfSSL_SetCRL_Cb");
- if (ssl)
- return wolfSSL_CertManagerSetCRL_IOCb(ssl->ctx->cm, cb);
- else
- return BAD_FUNC_ARG;
-}
-#endif
-
-int wolfSSL_CTX_EnableCRL(WOLFSSL_CTX* ctx, int options)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_EnableCRL");
- if (ctx)
- return wolfSSL_CertManagerEnableCRL(ctx->cm, options);
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_CTX_DisableCRL(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_DisableCRL");
- if (ctx)
- return wolfSSL_CertManagerDisableCRL(ctx->cm);
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_CTX_LoadCRL(WOLFSSL_CTX* ctx, const char* path,
- int type, int monitor)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_LoadCRL");
- if (ctx)
- return wolfSSL_CertManagerLoadCRL(ctx->cm, path, type, monitor);
- else
- return BAD_FUNC_ARG;
-}
-
-
-int wolfSSL_CTX_SetCRL_Cb(WOLFSSL_CTX* ctx, CbMissingCRL cb)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_SetCRL_Cb");
- if (ctx)
- return wolfSSL_CertManagerSetCRL_Cb(ctx->cm, cb);
- else
- return BAD_FUNC_ARG;
-}
-
-#ifdef HAVE_CRL_IO
-int wolfSSL_CTX_SetCRL_IOCb(WOLFSSL_CTX* ctx, CbCrlIO cb)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_SetCRL_IOCb");
- if (ctx)
- return wolfSSL_CertManagerSetCRL_IOCb(ctx->cm, cb);
- else
- return BAD_FUNC_ARG;
-}
-#endif
-
-
-#endif /* HAVE_CRL */
-
-
-#ifdef WOLFSSL_DER_LOAD
-
-/* Add format parameter to allow DER load of CA files */
-int wolfSSL_CTX_der_load_verify_locations(WOLFSSL_CTX* ctx, const char* file,
- int format)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_der_load_verify_locations");
- if (ctx == NULL || file == NULL)
- return WOLFSSL_FAILURE;
-
- if (ProcessFile(ctx, file, format, CA_TYPE, NULL, 0, NULL,
- GET_VERIFY_SETTING_CTX(ctx)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-#endif /* WOLFSSL_DER_LOAD */
-
-
-
-WOLFSSL_ABI
-int wolfSSL_CTX_use_certificate_file(WOLFSSL_CTX* ctx, const char* file,
- int format)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_file");
-
- if (ProcessFile(ctx, file, format, CERT_TYPE, NULL, 0, NULL,
- GET_VERIFY_SETTING_CTX(ctx)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-
-WOLFSSL_ABI
-int wolfSSL_CTX_use_PrivateKey_file(WOLFSSL_CTX* ctx, const char* file,
- int format)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_file");
-
- if (ProcessFile(ctx, file, format, PRIVATEKEY_TYPE, NULL, 0, NULL,
- GET_VERIFY_SETTING_CTX(ctx)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-
-/* Sets the max chain depth when verifying a certificate chain. Default depth
- * is set to MAX_CHAIN_DEPTH.
- *
- * ctx WOLFSSL_CTX structure to set depth in
- * depth max depth
- */
-void wolfSSL_CTX_set_verify_depth(WOLFSSL_CTX *ctx, int depth) {
- WOLFSSL_ENTER("wolfSSL_CTX_set_verify_depth");
-
- if (ctx == NULL || depth < 0 || depth > MAX_CHAIN_DEPTH) {
- WOLFSSL_MSG("Bad depth argument, too large or less than 0");
- return;
- }
-
- ctx->verifyDepth = (byte)depth;
-}
-
-
-/* get cert chaining depth using ssl struct */
-long wolfSSL_get_verify_depth(WOLFSSL* ssl)
-{
- if(ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-#ifndef OPENSSL_EXTRA
- return MAX_CHAIN_DEPTH;
-#else
- return ssl->options.verifyDepth;
-#endif
-}
-
-
-/* get cert chaining depth using ctx struct */
-long wolfSSL_CTX_get_verify_depth(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL) {
- return BAD_FUNC_ARG;
- }
-#ifndef OPENSSL_EXTRA
- return MAX_CHAIN_DEPTH;
-#else
- return ctx->verifyDepth;
-#endif
-}
-
-
-WOLFSSL_ABI
-int wolfSSL_CTX_use_certificate_chain_file(WOLFSSL_CTX* ctx, const char* file)
-{
- /* process up to MAX_CHAIN_DEPTH plus subject cert */
- WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_file");
-
- if (ProcessFile(ctx, file, WOLFSSL_FILETYPE_PEM, CERT_TYPE, NULL, 1, NULL,
- GET_VERIFY_SETTING_CTX(ctx)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-
-int wolfSSL_CTX_use_certificate_chain_file_format(WOLFSSL_CTX* ctx,
- const char* file, int format)
-{
- /* process up to MAX_CHAIN_DEPTH plus subject cert */
- WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_file_format");
-
- if (ProcessFile(ctx, file, format, CERT_TYPE, NULL, 1, NULL,
- GET_VERIFY_SETTING_CTX(ctx)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-
-#ifndef NO_DH
-
-/* server Diffie-Hellman parameters */
-static int wolfSSL_SetTmpDH_file_wrapper(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
- const char* fname, int format)
-{
-#ifdef WOLFSSL_SMALL_STACK
- byte staticBuffer[1]; /* force heap usage */
-#else
- byte staticBuffer[FILE_BUFFER_SIZE];
-#endif
- byte* myBuffer = staticBuffer;
- int dynamic = 0;
- int ret;
- long sz = 0;
- XFILE file;
-
- if (ctx == NULL || fname == NULL)
- return BAD_FUNC_ARG;
-
- file = XFOPEN(fname, "rb");
- if (file == XBADFILE) return WOLFSSL_BAD_FILE;
- if(XFSEEK(file, 0, XSEEK_END) != 0) {
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
- sz = XFTELL(file);
- XREWIND(file);
-
- if (sz > MAX_WOLFSSL_FILE_SIZE || sz <= 0) {
- WOLFSSL_MSG("SetTmpDH file size error");
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
-
- if (sz > (long)sizeof(staticBuffer)) {
- WOLFSSL_MSG("Getting dynamic buffer");
- myBuffer = (byte*) XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
- if (myBuffer == NULL) {
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
- dynamic = 1;
- }
-
- if ( (ret = (int)XFREAD(myBuffer, 1, sz, file)) != sz)
- ret = WOLFSSL_BAD_FILE;
- else {
- if (ssl)
- ret = wolfSSL_SetTmpDH_buffer(ssl, myBuffer, sz, format);
- else
- ret = wolfSSL_CTX_SetTmpDH_buffer(ctx, myBuffer, sz, format);
- }
-
- XFCLOSE(file);
- if (dynamic)
- XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
-
- return ret;
-}
-
-/* server Diffie-Hellman parameters */
-int wolfSSL_SetTmpDH_file(WOLFSSL* ssl, const char* fname, int format)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return wolfSSL_SetTmpDH_file_wrapper(ssl->ctx, ssl, fname, format);
-}
-
-
-/* server Diffie-Hellman parameters */
-int wolfSSL_CTX_SetTmpDH_file(WOLFSSL_CTX* ctx, const char* fname, int format)
-{
- return wolfSSL_SetTmpDH_file_wrapper(ctx, NULL, fname, format);
-}
-
-#endif /* NO_DH */
-
-#endif /* NO_FILESYSTEM */
-
-#ifdef OPENSSL_EXTRA
-/* put SSL type in extra for now, not very common */
-
-/* Converts a DER format key read from "bio" to a PKCS8 structure.
- *
- * bio input bio to read DER from
- * pkey If not NULL then this pointer will be overwritten with a new PKCS8
- * structure.
- *
- * returns a WOLFSSL_PKCS8_PRIV_KEY_INFO pointer on success and NULL in fail
- * case.
- */
-WOLFSSL_PKCS8_PRIV_KEY_INFO* wolfSSL_d2i_PKCS8_PKEY_bio(WOLFSSL_BIO* bio,
- WOLFSSL_PKCS8_PRIV_KEY_INFO** pkey)
-{
- WOLFSSL_PKCS8_PRIV_KEY_INFO* pkcs8 = NULL;
-#ifdef WOLFSSL_PEM_TO_DER
- unsigned char* mem = NULL;
- int memSz;
- int keySz;
- word32 algId;
-
- WOLFSSL_MSG("wolfSSL_d2i_PKCS8_PKEY_bio()");
-
- if (bio == NULL) {
- return NULL;
- }
-
- if ((memSz = wolfSSL_BIO_get_mem_data(bio, &mem)) < 0) {
- return NULL;
- }
-
- if ((keySz = wc_KeyPemToDer(mem, memSz, mem, memSz, NULL)) < 0) {
- WOLFSSL_MSG("Not PEM format");
- keySz = memSz;
- if ((keySz = ToTraditional_ex((byte*)mem, (word32)keySz, &algId)) < 0) {
- return NULL;
- }
- }
-
- pkcs8 = wolfSSL_EVP_PKEY_new();
- if (pkcs8 == NULL) {
- return NULL;
- }
-
- pkcs8->pkey.ptr = (char*)XMALLOC(keySz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- if (pkcs8->pkey.ptr == NULL) {
- wolfSSL_EVP_PKEY_free(pkcs8);
- return NULL;
- }
- XMEMCPY(pkcs8->pkey.ptr, mem, keySz);
- pkcs8->pkey_sz = keySz;
-
- if (pkey != NULL) {
- *pkey = pkcs8;
- }
-#else
- (void)bio;
- (void)pkey;
-#endif /* WOLFSSL_PEM_TO_DER */
-
- return pkcs8;
-}
-
-
-/* expecting DER format public key
- *
- * bio input bio to read DER from
- * out If not NULL then this pointer will be overwritten with a new
- * WOLFSSL_EVP_PKEY pointer
- *
- * returns a WOLFSSL_EVP_PKEY pointer on success and NULL in fail case.
- */
-WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY_bio(WOLFSSL_BIO* bio,
- WOLFSSL_EVP_PKEY** out)
-{
- unsigned char* mem;
- long memSz;
- WOLFSSL_EVP_PKEY* pkey = NULL;
-
- WOLFSSL_ENTER("wolfSSL_d2i_PUBKEY_bio()");
-
- if (bio == NULL) {
- return NULL;
- }
- (void)out;
-
- memSz = wolfSSL_BIO_pending(bio);
- if (memSz <= 0) {
- return NULL;
- }
-
- mem = (unsigned char*)XMALLOC(memSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (mem == NULL) {
- return NULL;
- }
-
- if (wolfSSL_BIO_read(bio, mem, (int)memSz) == memSz) {
- pkey = wolfSSL_d2i_PUBKEY(NULL, (const unsigned char**)&mem, memSz);
- if (out != NULL && pkey != NULL) {
- *out = pkey;
- }
- }
-
- XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return pkey;
-}
-
-
-
-/* Converts a DER encoded public key to a WOLFSSL_EVP_PKEY structure.
- *
- * out pointer to new WOLFSSL_EVP_PKEY structure. Can be NULL
- * in DER buffer to convert
- * inSz size of in buffer
- *
- * returns a pointer to a new WOLFSSL_EVP_PKEY structure on success and NULL
- * on fail
- */
-WOLFSSL_EVP_PKEY* wolfSSL_d2i_PUBKEY(WOLFSSL_EVP_PKEY** out,
- const unsigned char** in, long inSz)
-{
- WOLFSSL_EVP_PKEY* pkey = NULL;
- const unsigned char* mem;
- long memSz = inSz;
-
- WOLFSSL_ENTER("wolfSSL_d2i_PUBKEY");
-
- if (in == NULL || inSz < 0) {
- WOLFSSL_MSG("Bad argument");
- return NULL;
- }
- mem = *in;
-
- #if !defined(NO_RSA)
- {
- RsaKey rsa;
- word32 keyIdx = 0;
-
- /* test if RSA key */
- if (wc_InitRsaKey(&rsa, NULL) == 0 &&
- wc_RsaPublicKeyDecode(mem, &keyIdx, &rsa, (word32)memSz) == 0) {
- wc_FreeRsaKey(&rsa);
- pkey = wolfSSL_EVP_PKEY_new();
- if (pkey != NULL) {
- pkey->pkey_sz = keyIdx;
- pkey->pkey.ptr = (char*)XMALLOC(memSz, NULL,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (pkey->pkey.ptr == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
- XMEMCPY(pkey->pkey.ptr, mem, keyIdx);
- pkey->type = EVP_PKEY_RSA;
- if (out != NULL) {
- *out = pkey;
- }
-
- pkey->ownRsa = 1;
- pkey->rsa = wolfSSL_RSA_new();
- if (pkey->rsa == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
-
- if (wolfSSL_RSA_LoadDer_ex(pkey->rsa,
- (const unsigned char*)pkey->pkey.ptr,
- pkey->pkey_sz, WOLFSSL_RSA_LOAD_PUBLIC) != 1) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
-
- return pkey;
- }
- }
- wc_FreeRsaKey(&rsa);
- }
- #endif /* NO_RSA */
-
- #ifdef HAVE_ECC
- {
- word32 keyIdx = 0;
- ecc_key ecc;
-
- if (wc_ecc_init(&ecc) == 0 &&
- wc_EccPublicKeyDecode(mem, &keyIdx, &ecc, (word32)memSz) == 0) {
- wc_ecc_free(&ecc);
- pkey = wolfSSL_EVP_PKEY_new();
- if (pkey != NULL) {
- pkey->pkey_sz = keyIdx;
- pkey->pkey.ptr = (char*)XMALLOC(keyIdx, NULL,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (pkey->pkey.ptr == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
- XMEMCPY(pkey->pkey.ptr, mem, keyIdx);
- pkey->type = EVP_PKEY_EC;
- if (out != NULL) {
- *out = pkey;
- }
-
- pkey->ownEcc = 1;
- pkey->ecc = wolfSSL_EC_KEY_new();
- if (pkey->ecc == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
-
- if (wolfSSL_EC_KEY_LoadDer_ex(pkey->ecc,
- (const unsigned char*)pkey->pkey.ptr,
- pkey->pkey_sz, WOLFSSL_EC_KEY_LOAD_PUBLIC) != 1) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
-
- return pkey;
- }
- }
- wc_ecc_free(&ecc);
- }
- #endif /* HAVE_ECC */
-
- #if !defined(NO_DSA)
- {
- DsaKey dsa;
- word32 keyIdx = 0;
-
- /* test if DSA key */
- if (wc_InitDsaKey(&dsa) == 0 &&
- wc_DsaPublicKeyDecode(mem, &keyIdx, &dsa, (word32)memSz) == 0) {
- wc_FreeDsaKey(&dsa);
- pkey = wolfSSL_EVP_PKEY_new();
-
- if (pkey != NULL) {
- pkey->pkey_sz = keyIdx;
- pkey->pkey.ptr = (char*)XMALLOC(memSz, NULL,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (pkey->pkey.ptr == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
- XMEMCPY(pkey->pkey.ptr, mem, keyIdx);
- pkey->type = EVP_PKEY_DSA;
- if (out != NULL) {
- *out = pkey;
- }
-
- pkey->ownDsa = 1;
- pkey->dsa = wolfSSL_DSA_new();
- if (pkey->dsa == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
-
-
- if (wolfSSL_DSA_LoadDer_ex(pkey->dsa,
- (const unsigned char*)pkey->pkey.ptr,
- pkey->pkey_sz, WOLFSSL_DSA_LOAD_PUBLIC) != 1) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
-
- return pkey;
- }
- }
- wc_FreeDsaKey(&dsa);
- }
- #endif /* NO_DSA */
-
- #if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL))
- {
- DhKey dh;
- word32 keyIdx = 0;
-
- /* test if DH key */
- if (wc_InitDhKey(&dh) == 0 &&
- wc_DhKeyDecode(mem, &keyIdx, &dh, (word32)memSz) == 0) {
- wc_FreeDhKey(&dh);
- pkey = wolfSSL_EVP_PKEY_new();
-
- if (pkey != NULL) {
- pkey->pkey_sz = (int)memSz;
- pkey->pkey.ptr = (char*)XMALLOC(memSz, NULL,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (pkey->pkey.ptr == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
- XMEMCPY(pkey->pkey.ptr, mem, memSz);
- pkey->type = EVP_PKEY_DH;
- if (out != NULL) {
- *out = pkey;
- }
-
- pkey->ownDh = 1;
- pkey->dh = wolfSSL_DH_new();
- if (pkey->dh == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
-
- if (wolfSSL_DH_LoadDer(pkey->dh,
- (const unsigned char*)pkey->pkey.ptr,
- pkey->pkey_sz) != WOLFSSL_SUCCESS) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
-
- return pkey;
- }
- }
- wc_FreeDhKey(&dh);
- }
- #endif /* !NO_DH && (WOLFSSL_QT || OPENSSL_ALL) */
-
- return pkey;
-}
-
-
-/* Reads in a DER format key. If PKCS8 headers are found they are stripped off.
- *
- * type type of key
- * out newly created WOLFSSL_EVP_PKEY structure
- * in pointer to input key DER
- * inSz size of in buffer
- *
- * On success a non null pointer is returned and the pointer in is advanced the
- * same number of bytes read.
- */
-WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey(int type, WOLFSSL_EVP_PKEY** out,
- const unsigned char **in, long inSz)
-{
- WOLFSSL_EVP_PKEY* local;
- word32 idx = 0;
- int ret;
- word32 algId;
-
- WOLFSSL_ENTER("wolfSSL_d2i_PrivateKey");
-
- if (in == NULL || inSz < 0) {
- WOLFSSL_MSG("Bad argument");
- return NULL;
- }
-
- /* Check if input buffer has PKCS8 header. In the case that it does not
- * have a PKCS8 header then do not error out. */
- if ((ret = ToTraditionalInline_ex((const byte*)(*in), &idx, (word32)inSz,
- &algId)) > 0) {
- WOLFSSL_MSG("Found and removed PKCS8 header");
- }
- else {
- if (ret != ASN_PARSE_E) {
- WOLFSSL_MSG("Unexpected error with trying to remove PKCS8 header");
- return NULL;
- }
- }
-
- if (out != NULL && *out != NULL) {
- wolfSSL_EVP_PKEY_free(*out);
- *out = NULL;
- }
- local = wolfSSL_EVP_PKEY_new();
- if (local == NULL) {
- return NULL;
- }
-
- /* sanity check on idx before use */
- if ((int)idx > inSz) {
- WOLFSSL_MSG("Issue with index pointer");
- wolfSSL_EVP_PKEY_free(local);
- local = NULL;
- return NULL;
- }
-
- local->type = type;
- local->pkey_sz = (int)inSz - idx;
- local->pkey.ptr = (char*)XMALLOC(inSz - idx, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- if (local->pkey.ptr == NULL) {
- wolfSSL_EVP_PKEY_free(local);
- local = NULL;
- return NULL;
- }
- else {
- XMEMCPY(local->pkey.ptr, *in + idx, inSz - idx);
- }
-
- switch (type) {
-#ifndef NO_RSA
- case EVP_PKEY_RSA:
- local->ownRsa = 1;
- local->rsa = wolfSSL_RSA_new();
- if (local->rsa == NULL) {
- wolfSSL_EVP_PKEY_free(local);
- return NULL;
- }
- if (wolfSSL_RSA_LoadDer_ex(local->rsa,
- (const unsigned char*)local->pkey.ptr, local->pkey_sz,
- WOLFSSL_RSA_LOAD_PRIVATE) != SSL_SUCCESS) {
- wolfSSL_EVP_PKEY_free(local);
- return NULL;
- }
- break;
-#endif /* NO_RSA */
-#ifdef HAVE_ECC
- case EVP_PKEY_EC:
- local->ownEcc = 1;
- local->ecc = wolfSSL_EC_KEY_new();
- if (local->ecc == NULL) {
- wolfSSL_EVP_PKEY_free(local);
- return NULL;
- }
- if (wolfSSL_EC_KEY_LoadDer(local->ecc,
- (const unsigned char*)local->pkey.ptr, local->pkey_sz)
- != SSL_SUCCESS) {
- wolfSSL_EVP_PKEY_free(local);
- return NULL;
- }
- break;
-#endif /* HAVE_ECC */
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
-#ifndef NO_DSA
- case EVP_PKEY_DSA:
- local->ownDsa = 1;
- local->dsa = wolfSSL_DSA_new();
- if (local->dsa == NULL) {
- wolfSSL_EVP_PKEY_free(local);
- return NULL;
- }
- if (wolfSSL_DSA_LoadDer(local->dsa,
- (const unsigned char*)local->pkey.ptr, local->pkey_sz)
- != SSL_SUCCESS) {
- wolfSSL_EVP_PKEY_free(local);
- return NULL;
- }
- break;
-#endif /* NO_DSA */
-#ifndef NO_DH
- case EVP_PKEY_DH:
- local->ownDh = 1;
- local->dh = wolfSSL_DH_new();
- if (local->dh == NULL) {
- wolfSSL_EVP_PKEY_free(local);
- return NULL;
- }
- if (wolfSSL_DH_LoadDer(local->dh,
- (const unsigned char*)local->pkey.ptr, local->pkey_sz)
- != SSL_SUCCESS) {
- wolfSSL_EVP_PKEY_free(local);
- return NULL;
- }
- break;
-#endif /* HAVE_DH */
-#endif /* WOLFSSL_QT || OPENSSL_ALL */
- default:
- WOLFSSL_MSG("Unsupported key type");
- wolfSSL_EVP_PKEY_free(local);
- return NULL;
- }
-
- /* advance pointer with success */
- if (local != NULL) {
- if ((idx + local->pkey_sz) <= (word32)inSz) {
- *in = *in + idx + local->pkey_sz;
- }
-
- if (out != NULL) {
- *out = local;
- }
- }
-
- return local;
-}
-
-#ifndef NO_CERTS
-
-int wolfSSL_check_private_key(const WOLFSSL* ssl)
-{
- DecodedCert der;
- word32 size;
- byte* buff;
- int ret;
-
- if (ssl == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- size = ssl->buffers.certificate->length;
- buff = ssl->buffers.certificate->buffer;
- InitDecodedCert(&der, buff, size, ssl->heap);
-#ifdef HAVE_PK_CALLBACKS
- ret = InitSigPkCb((WOLFSSL*)ssl, &der.sigCtx);
- if (ret != 0) {
- FreeDecodedCert(&der);
- return ret;
- }
-#endif
-
- if (ParseCertRelative(&der, CERT_TYPE, NO_VERIFY, NULL) != 0) {
- FreeDecodedCert(&der);
- return WOLFSSL_FAILURE;
- }
-
- size = ssl->buffers.key->length;
- buff = ssl->buffers.key->buffer;
- ret = wc_CheckPrivateKey(buff, size, &der);
- FreeDecodedCert(&der);
- return ret;
-}
-
-#if defined(OPENSSL_ALL)
-/* Returns the number of X509V3 extensions in X509 object, or 0 on failure */
-int wolfSSL_X509_get_ext_count(const WOLFSSL_X509* passedCert)
-{
- int extCount = 0;
- int length = 0;
- int outSz = 0;
- const byte* rawCert;
- int sz = 0;
- word32 idx = 0;
- DecodedCert cert;
- const byte* input;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_ext_count()");
- if (passedCert == NULL) {
- WOLFSSL_MSG("\tNot passed a certificate");
- return WOLFSSL_FAILURE;
- }
-
- rawCert = wolfSSL_X509_get_der((WOLFSSL_X509*)passedCert, &outSz);
- if (rawCert == NULL) {
- WOLFSSL_MSG("\tpassedCert has no internal DerBuffer set.");
- return WOLFSSL_FAILURE;
- }
- InitDecodedCert(&cert, rawCert, (word32)outSz, 0);
-
- if (ParseCert(&cert, CA_TYPE, NO_VERIFY, NULL) < 0) {
- WOLFSSL_MSG("\tCertificate parsing failed");
- return WOLFSSL_FAILURE;
- }
-
- input = cert.extensions;
- sz = cert.extensionsSz;
-
- if (input == NULL || sz == 0) {
- WOLFSSL_MSG("\tsz or input NULL error");
- FreeDecodedCert(&cert);
- return WOLFSSL_FAILURE;
- }
-
- if (input[idx++] != ASN_EXTENSIONS) {
- WOLFSSL_MSG("\tfail: should be an EXTENSIONS");
- FreeDecodedCert(&cert);
- return WOLFSSL_FAILURE;
- }
-
- if (GetLength(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: invalid length");
- FreeDecodedCert(&cert);
- return WOLFSSL_FAILURE;
- }
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE (1)");
- FreeDecodedCert(&cert);
- return WOLFSSL_FAILURE;
- }
-
- while (idx < (word32)sz) {
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE");
- FreeDecodedCert(&cert);
- return WOLFSSL_FAILURE;
- }
- idx += length;
- extCount++;
- }
- FreeDecodedCert(&cert);
- return extCount;
-}
-
-/* Creates and returns pointer to a new X509_EXTENSION object in memory */
-WOLFSSL_X509_EXTENSION* wolfSSL_X509_EXTENSION_new(void)
-{
- WOLFSSL_ENTER("wolfSSL_X509_EXTENSION_new");
-
- WOLFSSL_X509_EXTENSION* newExt;
- newExt = (WOLFSSL_X509_EXTENSION*)XMALLOC(sizeof(WOLFSSL_X509_EXTENSION),
- NULL, DYNAMIC_TYPE_X509_EXT);
- if (newExt == NULL)
- return NULL;
- XMEMSET(newExt, 0, sizeof(WOLFSSL_X509_EXTENSION));
-
- return newExt;
-}
-
-void wolfSSL_X509_EXTENSION_free(WOLFSSL_X509_EXTENSION* x)
-{
- WOLFSSL_ASN1_STRING asn1;
- WOLFSSL_ENTER("wolfSSL_X509_EXTENSION_free");
- if (x == NULL)
- return;
-
- if (x->obj != NULL)
- wolfSSL_ASN1_OBJECT_free(x->obj);
-
- asn1 = x->value;
- if (asn1.length > 0 && asn1.data != NULL && asn1.isDynamic)
- XFREE(asn1.data, NULL, DYNAMIC_TYPE_OPENSSL);
-
- wolfSSL_sk_free(x->ext_sk);
-
- XFREE(x, NULL, DYNAMIC_TYPE_X509_EXT);
-}
-
-/* Creates and returns a new WOLFSSL_X509_EXTENSION stack. */
-WOLFSSL_STACK* wolfSSL_sk_new_x509_ext(void)
-{
- WOLFSSL_STACK* sk;
- WOLFSSL_ENTER("wolfSSL_sk_new_x509_ext");
-
- sk = wolfSSL_sk_new_null();
- if (sk) {
- sk->type = STACK_TYPE_X509_EXT;
- }
- return sk;
-}
-
-/* return 1 on success 0 on fail */
-int wolfSSL_sk_X509_EXTENSION_push(WOLFSSL_STACK* sk,WOLFSSL_X509_EXTENSION* ext)
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_X509_EXTENSION_push");
-
- if (sk == NULL || ext == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* no previous values in stack */
- if (sk->data.ext == NULL) {
- sk->data.ext = ext;
- sk->num += 1;
- return WOLFSSL_SUCCESS;
- }
-
- /* stack already has value(s) create a new node and add more */
- node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_X509);
- if (node == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
- XMEMSET(node, 0, sizeof(WOLFSSL_STACK));
-
- /* push new obj onto head of stack */
- node->data.ext = sk->data.ext;
- node->next = sk->next;
- node->type = sk->type;
- sk->next = node;
- sk->data.ext = ext;
- sk->num += 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Free the structure for X509_EXTENSION stack
- *
- * sk stack to free nodes in
- */
-void wolfSSL_sk_X509_EXTENSION_free(WOLFSSL_STACK* sk)
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_X509_EXTENSION_free");
-
- if (sk == NULL) {
- return;
- }
-
- /* parse through stack freeing each node */
- node = sk->next;
- while ((node != NULL) && (sk->num > 1)) {
- WOLFSSL_STACK* tmp = node;
- node = node->next;
-
- wolfSSL_X509_EXTENSION_free(tmp->data.ext);
- XFREE(tmp, NULL, DYNAMIC_TYPE_X509);
- sk->num -= 1;
- }
-
- /* free head of stack */
- if (sk->num == 1) {
- wolfSSL_X509_EXTENSION_free(sk->data.ext);
- }
- XFREE(sk, NULL, DYNAMIC_TYPE_X509);
-}
-
-int wolfSSL_ASN1_BIT_STRING_set_bit(WOLFSSL_ASN1_BIT_STRING* str, int pos,
- int val)
-{
- int bytes_cnt, bit;
- byte* temp;
-
- if (!str || (val != 0 && val != 1) || pos < 0) {
- return WOLFSSL_FAILURE;
- }
-
- bytes_cnt = pos/8;
- bit = 1<<(7-(pos%8));
-
- if (bytes_cnt+1 > str->length) {
- if (!(temp = (byte*)XREALLOC(str->data, bytes_cnt+1, NULL,
- DYNAMIC_TYPE_OPENSSL))) {
- return WOLFSSL_FAILURE;
- }
- XMEMSET(temp+str->length, 0, bytes_cnt+1 - str->length);
- str->data = temp;
- str->length = bytes_cnt+1;
- }
-
- str->data[bytes_cnt] &= ~bit;
- str->data[bytes_cnt] |= val ? bit : 0;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Gets the X509_EXTENSION* ext based on it's location in WOLFSSL_X509* x509.
- *
- * x509 : The X509 structure to look for the extension.
- * loc : Location of the extension. If the extension is found at the given
- * location, a new X509_EXTENSION structure is populated with extension-specific
- * data based on the extension type.
-
- * Returns NULL on error or pointer to X509_EXTENSION structure containing the
- * extension. The returned X509_EXTENSION should not be free'd by caller.
- * The returned X509_EXTENSION is pushed onto a stack inside the x509 argument.
- * This is later free'd when x509 is free'd.
- *
- * NOTE: for unknown extension NIDs, a X509_EXTENSION is populated with the
- * extension oid as the ASN1_OBJECT (QT compatibility)
- */
-WOLFSSL_X509_EXTENSION* wolfSSL_X509_get_ext(const WOLFSSL_X509* x509, int loc)
-{
- WOLFSSL_X509_EXTENSION* ext = NULL;
- WOLFSSL_ENTER("wolfSSL_X509_get_ext");
- if (x509 == NULL)
- return NULL;
-
- ext = wolfSSL_X509_set_ext((WOLFSSL_X509*) x509, loc);
- return ext;
-}
-
-/* Pushes a new X509_EXTENSION* ext onto the stack inside WOLFSSL_X509* x509.
- * This is currently a helper function for wolfSSL_X509_get_ext
- * Caller does not free the returned WOLFSSL_X509_EXTENSION*
- */
-WOLFSSL_X509_EXTENSION* wolfSSL_X509_set_ext(WOLFSSL_X509* x509, int loc)
-{
- int extCount = 0, length = 0, outSz = 0, sz = 0, ret = 0;
- int objSz = 0, isSet = 0;
- const byte* rawCert;
- const byte* input;
- byte* oidBuf;
- word32 oid, idx = 0, tmpIdx = 0;
- WOLFSSL_X509_EXTENSION* ext = NULL;
- WOLFSSL_ASN1_INTEGER* a;
- WOLFSSL_STACK* sk;
- DecodedCert cert;
-
- WOLFSSL_ENTER("wolfSSL_X509_set_ext");
-
- if(x509 == NULL){
- WOLFSSL_MSG("\tNot passed a certificate");
- return NULL;
- }
-
- if(loc <0 || (loc > wolfSSL_X509_get_ext_count(x509))){
- WOLFSSL_MSG("\tBad location argument");
- return NULL;
- }
-
- ext = wolfSSL_X509_EXTENSION_new();
- if (ext == NULL) {
- WOLFSSL_MSG("\tX509_EXTENSION_new() failed");
- return NULL;
- }
-
- rawCert = wolfSSL_X509_get_der((WOLFSSL_X509*)x509, &outSz);
- if (rawCert == NULL) {
- WOLFSSL_MSG("\tX509_get_der() failed");
- wolfSSL_X509_EXTENSION_free(ext);
- return NULL;
- }
-
- InitDecodedCert( &cert, rawCert, (word32)outSz, 0);
-
- if (ParseCert(&cert, CA_TYPE, NO_VERIFY, NULL) < 0) {
- WOLFSSL_MSG("\tCertificate parsing failed");
- wolfSSL_X509_EXTENSION_free(ext);
- return NULL;
- }
-
- input = cert.extensions;
- sz = cert.extensionsSz;
-
- if (input == NULL || sz == 0) {
- WOLFSSL_MSG("\tfail: should be an EXTENSIONS");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
-
- if (input[idx++] != ASN_EXTENSIONS) {
- WOLFSSL_MSG("\tfail: should be an EXTENSIONS");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
-
- if (GetLength(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: invalid length");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE (1)");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
-
- while (idx < (word32)sz) {
- oid = 0;
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
-
- tmpIdx = idx;
- ret = GetObjectId(input, &idx, &oid, oidCertExtType, sz);
- if (ret < 0) {
- WOLFSSL_MSG("\tfail: OBJECT ID");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- idx = tmpIdx;
-
- /* Continue while loop until extCount == loc or idx > sz */
- if (extCount != loc) {
- idx += length;
- extCount++;
- continue;
- }
- /* extCount == loc. Now get the extension. */
- /* Check if extension has been set */
- isSet = wolfSSL_X509_ext_isSet_by_NID((WOLFSSL_X509*)x509, oid);
- ext->obj = wolfSSL_OBJ_nid2obj(oid);
- if (ext->obj == NULL) {
- WOLFSSL_MSG("\tfail: Invalid OBJECT");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- ext->obj->nid = oid;
-
- switch (oid) {
- case BASIC_CA_OID:
- if (!isSet)
- break;
- /* Set pathlength */
- a = wolfSSL_ASN1_INTEGER_new();
- if (a == NULL) {
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- a->length = x509->pathLength;
-
- /* Save ASN1_INTEGER in x509 extension */
- ext->obj->pathlen = a;
-
- ext->obj->ca = x509->isCa;
- ext->crit = x509->basicConstCrit;
- break;
-
- case AUTH_INFO_OID:
- if (!isSet)
- break;
-
- /* Create a stack to hold both the caIssuer and ocsp objects
- in X509_EXTENSION structure */
- sk = (WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)*)XMALLOC(
- sizeof(WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)),
- NULL, DYNAMIC_TYPE_ASN1);
- if (sk == NULL) {
- WOLFSSL_MSG("Failed to malloc stack");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- XMEMSET(sk, 0, sizeof(WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)));
- sk->type = STACK_TYPE_OBJ;
-
- /* Add CaIssuers object to stack */
- if (x509->authInfoCaIssuer != NULL &&
- x509->authInfoCaIssuerSz > 0)
- {
- WOLFSSL_ASN1_OBJECT* obj;
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Error creating ASN1 object");
- wolfSSL_sk_ASN1_OBJECT_free(sk);
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- obj->obj = (byte*)x509->authInfoCaIssuer;
- obj->objSz = x509->authInfoCaIssuerSz;
- obj->grp = oidCertAuthInfoType;
- obj->nid = AIA_CA_ISSUER_OID;
-
- ret = wolfSSL_sk_ASN1_OBJECT_push(sk, obj);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error pushing ASN1 object onto stack");
- wolfSSL_ASN1_OBJECT_free(obj);
- wolfSSL_sk_ASN1_OBJECT_free(sk);
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- }
-
- /* Add OCSP object to stack */
- if (x509->authInfo != NULL &&
- x509->authInfoSz > 0)
- {
- WOLFSSL_ASN1_OBJECT* obj;
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Error creating ASN1 object");
- wolfSSL_sk_ASN1_OBJECT_free(sk);
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- obj->obj = x509->authInfo;
- obj->objSz = x509->authInfoSz;
- obj->grp = oidCertAuthInfoType;
- obj->nid = AIA_OCSP_OID;
-
- ret = wolfSSL_sk_ASN1_OBJECT_push(sk, obj);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error pushing ASN1 object onto stack");
- wolfSSL_ASN1_OBJECT_free(obj);
- wolfSSL_sk_ASN1_OBJECT_free(sk);
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- }
- ext->ext_sk = sk;
- ext->crit = x509->authInfoCrit;
- break;
-
- case AUTH_KEY_OID:
- if (!isSet)
- break;
-
- ret = wolfSSL_ASN1_STRING_set(&ext->value, x509->authKeyId,
- x509->authKeyIdSz);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ASN1_STRING_set() failed");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- ext->crit = x509->authKeyIdCrit;
- break;
-
- case SUBJ_KEY_OID:
- if (!isSet)
- break;
-
- ret = wolfSSL_ASN1_STRING_set(&ext->value, x509->subjKeyId,
- x509->subjKeyIdSz);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ASN1_STRING_set() failed");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- ext->crit = x509->subjKeyIdCrit;
- break;
-
- case CERT_POLICY_OID:
- if (!isSet)
- break;
- ext->crit = x509->certPolicyCrit;
- break;
-
- case KEY_USAGE_OID:
- if (!isSet)
- break;
-
- ret = wolfSSL_ASN1_STRING_set(&ext->value,
- (byte*)&(x509->keyUsage), sizeof(word16));
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ASN1_STRING_set() failed");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- ext->crit = x509->keyUsageCrit;
- break;
-
- case EXT_KEY_USAGE_OID:
- if (!isSet)
- break;
- ext->crit = x509->keyUsageCrit;
- break;
-
- case CRL_DIST_OID:
- if (!isSet)
- break;
- ext->crit = x509->CRLdistCrit;
- break;
-
- case ALT_NAMES_OID:
- {
- WOLFSSL_GENERAL_NAME* gn = NULL;
- DNS_entry* dns = NULL;
- if (!isSet)
- break;
-
- sk = (WOLF_STACK_OF(WOLFSSL_GENERAL_NAME)*)XMALLOC(
- sizeof(WOLF_STACK_OF(WOLFSSL_GENERAL_NAME)), NULL,
- DYNAMIC_TYPE_ASN1);
- if (sk == NULL) {
- return NULL;
- }
- XMEMSET(sk, 0, sizeof(WOLF_STACK_OF(WOLFSSL_GENERAL_NAME)));
- sk->type = STACK_TYPE_GEN_NAME;
-
- if (x509->subjAltNameSet && x509->altNames != NULL) {
- /* alt names are DNS_entry structs */
- dns = x509->altNames;
- /* Currently only support GEN_DNS type */
- while (dns != NULL) {
- gn = wolfSSL_GENERAL_NAME_new();
- if (gn == NULL) {
- WOLFSSL_MSG("Error creating GENERAL_NAME");
- wolfSSL_sk_free(sk);
- return NULL;
- }
-
- gn->type = dns->type;
- gn->d.ia5->length = dns->len;
- if (wolfSSL_ASN1_STRING_set(gn->d.ia5, dns->name,
- gn->d.ia5->length) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ASN1_STRING_set failed");
- wolfSSL_GENERAL_NAME_free(gn);
- wolfSSL_sk_free(sk);
- return NULL;
- }
-
- dns = dns->next;
- /* last dns in list add at end of function */
- if (dns != NULL) {
- if (wolfSSL_sk_GENERAL_NAME_push(sk, gn) !=
- WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error pushing onto stack");
- wolfSSL_GENERAL_NAME_free(gn);
- wolfSSL_sk_free(sk);
- sk = NULL;
- }
- }
- }
- if (wolfSSL_sk_GENERAL_NAME_push(sk,gn) !=
- WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error pushing onto stack");
- wolfSSL_GENERAL_NAME_free(gn);
- wolfSSL_sk_free(sk);
- sk = NULL;
- }
- }
- ext->ext_sk = sk;
- ext->crit = x509->subjAltNameCrit;
- break;
- }
-
- default:
- WOLFSSL_MSG("Unknown extension type found, parsing OID");
- /* If the extension type is not recognized/supported,
- set the ASN1_OBJECT in the extension with the
- parsed oid for access in later function calls */
-
- /* Get OID from input */
- if (GetASNObjectId(input, &idx, &length, sz) != 0) {
- WOLFSSL_MSG("Failed to Get ASN Object Id");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- oidBuf = (byte*)XMALLOC(length+1+MAX_LENGTH_SZ, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (oidBuf == NULL) {
- WOLFSSL_MSG("Failed to malloc tmp buffer");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- oidBuf[0] = ASN_OBJECT_ID;
- objSz++;
- objSz += SetLength(length, oidBuf + 1);
- objSz += length;
-
- /* Set object size and reallocate space in object buffer */
- ext->obj->objSz = objSz;
- if(((ext->obj->dynamic & WOLFSSL_ASN1_DYNAMIC_DATA) != 0) ||
- (ext->obj->obj == NULL)) {
- ext->obj->obj =(byte*)XREALLOC((byte*)ext->obj->obj,
- ext->obj->objSz,
- NULL,DYNAMIC_TYPE_ASN1);
- if (ext->obj->obj == NULL) {
- wolfSSL_ASN1_OBJECT_free(ext->obj);
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- ext->obj->dynamic |= WOLFSSL_ASN1_DYNAMIC_DATA;
- } else {
- ext->obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA;
- }
- /* Get OID from input and copy to ASN1_OBJECT buffer */
- XMEMCPY(oidBuf+2, input+idx, length);
- XMEMCPY((byte*)ext->obj->obj, oidBuf, ext->obj->objSz);
- XFREE(oidBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- oidBuf = NULL;
- ext->obj->grp = oidCertExtType;
- ext->crit = 0;
-
- /* Get extension data and copy as ASN1_STRING */
- tmpIdx = idx + length;
- if ((tmpIdx >= (word32)sz) || (input[tmpIdx++] != ASN_OCTET_STRING)) {
- WOLFSSL_MSG("Error decoding unknown extension data");
- wolfSSL_ASN1_OBJECT_free(ext->obj);
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
-
- if (GetLength(input, &tmpIdx, &length, sz) <= 0) {
- WOLFSSL_MSG("Error: Invalid Input Length.");
- wolfSSL_ASN1_OBJECT_free(ext->obj);
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- ext->value.data = (char*)XMALLOC(length, NULL, DYNAMIC_TYPE_ASN1);
- ext->value.isDynamic = 1;
- if (ext->value.data == NULL) {
- WOLFSSL_MSG("Failed to malloc ASN1_STRING data");
- wolfSSL_X509_EXTENSION_free(ext);
- FreeDecodedCert(&cert);
- return NULL;
- }
- XMEMCPY(ext->value.data,input+tmpIdx,length);
- ext->value.length = length;
- } /* switch(oid) */
-
- break; /* Got the Extension. Now exit while loop. */
-
- } /* while(idx < sz) */
-
- /* Store the new extension in a stack inside x509
- * The extensions on the stack are free'd internally when FreeX509 is called
- */
- if (x509->ext_sk == NULL)
- x509->ext_sk = wolfSSL_sk_new_x509_ext();
- if (x509->ext_sk != NULL)
- wolfSSL_sk_X509_EXTENSION_push(x509->ext_sk, ext);
-
- FreeDecodedCert(&cert);
- return ext;
-}
-
-/* Return 0 on success and 1 on failure. Copies ext data to bio, using indent
- * to pad the output. flag is ignored. */
-int wolfSSL_X509V3_EXT_print(WOLFSSL_BIO *out, WOLFSSL_X509_EXTENSION *ext,
- unsigned long flag, int indent)
-{
- ASN1_OBJECT* obj;
- ASN1_STRING* str;
- int nid;
- const int sz = CTC_NAME_SIZE*2;
- int rc = WOLFSSL_FAILURE;
- char tmp[CTC_NAME_SIZE*2];
- WOLFSSL_ENTER("wolfSSL_X509V3_EXT_print");
-
- if ((out == NULL) || (ext == NULL)) {
- WOLFSSL_MSG("NULL parameter error");
- return rc;
- }
-
- obj = wolfSSL_X509_EXTENSION_get_object(ext);
- if (obj == NULL) {
- WOLFSSL_MSG("Error getting ASN1_OBJECT from X509_EXTENSION");
- return rc;
- }
-
- str = wolfSSL_X509_EXTENSION_get_data(ext);
- if (obj == NULL) {
- WOLFSSL_MSG("Error getting ASN1_STRING from X509_EXTENSION");
- return rc;
- }
-
- /* Print extension based on the type */
- nid = wolfSSL_OBJ_obj2nid(obj);
- switch (nid) {
- case BASIC_CA_OID:
- {
- char isCa[] = "TRUE";
- char notCa[] = "FALSE";
- XSNPRINTF(tmp, sz, "%*sCA:%s", indent, "",
- obj->ca ? isCa : notCa);
- break;
- }
- case ALT_NAMES_OID:
- {
- WOLFSSL_STACK* sk;
- char* val;
- int len;
- tmp[0] = '\0'; /* Make sure tmp is null-terminated */
-
- sk = ext->ext_sk;
- while (sk != NULL) {
- if (sk->type == STACK_TYPE_GEN_NAME && sk->data.gn) {
- /* str is GENERAL_NAME for subject alternative name ext */
- str = sk->data.gn->d.ia5;
- len = str->length + 2; /* + 2 for NULL char and "," */
- if (len > sz) {
- WOLFSSL_MSG("len greater than buffer size");
- return rc;
- }
-
- val = (char*)XMALLOC(len + indent, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (val == NULL) {
- WOLFSSL_MSG("Memory error");
- return rc;
- }
- if (sk->next)
- XSNPRINTF(val, len, "%*s%s, ", indent, "", str->strData);
- else
- XSNPRINTF(val, len, "%*s%s", indent, "", str->strData);
-
- XSTRNCAT(tmp, val, len);
- XFREE(val, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- sk = sk->next;
- }
- break;
- }
- case AUTH_KEY_OID:
- case SUBJ_KEY_OID:
- {
- char* asn1str;
- asn1str = wolfSSL_i2s_ASN1_STRING(NULL, str);
- XSNPRINTF(tmp, sz, "%*s%s", indent, "", asn1str);
- XFREE(asn1str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- break;
- }
- case AUTH_INFO_OID:
- case CERT_POLICY_OID:
- case CRL_DIST_OID:
- case KEY_USAGE_OID:
- WOLFSSL_MSG("X509V3_EXT_print not yet implemented for ext type");
- break;
-
- default:
- XSNPRINTF(tmp, sz, "%*s%s", indent, "", str->strData);
- }
-
- if (wolfSSL_BIO_write(out, tmp, (int)XSTRLEN(tmp)) == (int)XSTRLEN(tmp)) {
- rc = WOLFSSL_SUCCESS;
- }
- (void) flag;
-
- return rc;
-}
-
-/* Returns crit flag in X509_EXTENSION object */
-int wolfSSL_X509_EXTENSION_get_critical(const WOLFSSL_X509_EXTENSION* ex)
-{
- WOLFSSL_ENTER("wolfSSL_X509_EXTENSION_get_critical");
- if (ex == NULL)
- return BAD_FUNC_ARG;
- return ex->crit;
-}
-
-/* Creates v3_ext_method for a given X509v3 extension
- *
- * ex : The X509_EXTENSION used to create v3_ext_method. If the extension is
- * not NULL, get the NID of the extension object and populate the
- * extension type-specific X509V3_EXT_* function(s) in v3_ext_method.
- *
- * Returns NULL on error or pointer to the v3_ext_method populated with extension
- * type-specific X509V3_EXT_* function(s).
- *
- * NOTE: NID_subject_key_identifier is currently the only extension implementing
- * the X509V3_EXT_* functions, as it is the only type called directly by QT. The
- * other extension types return a pointer to a v3_ext_method struct that contains
- * only the NID.
- */
-const WOLFSSL_v3_ext_method* wolfSSL_X509V3_EXT_get(WOLFSSL_X509_EXTENSION* ex)
-{
- int nid;
- WOLFSSL_v3_ext_method method;
-
- WOLFSSL_ENTER("wolfSSL_X509V3_EXT_get");
- if ((ex == NULL) || (ex->obj == NULL)) {
- WOLFSSL_MSG("Passed an invalid X509_EXTENSION*");
- return NULL;
- }
- /* Initialize all methods to NULL */
- method.d2i = NULL;
- method.i2v = NULL;
- method.i2s = NULL;
- method.i2r = NULL;
-
- nid = ex->obj->nid;
- if (nid <= 0) {
- WOLFSSL_MSG("Failed to get nid from passed extension object");
- return NULL;
- }
-
- switch (nid) {
- case NID_basic_constraints:
- break;
- case NID_subject_key_identifier:
- method.i2s = (X509V3_EXT_I2S)wolfSSL_i2s_ASN1_STRING;
- break;
- case NID_key_usage:
- WOLFSSL_MSG("i2v function not yet implemented for Key Usage");
- break;
- case NID_authority_key_identifier:
- WOLFSSL_MSG("i2v function not yet implemented for Auth Key Id");
- break;
- case NID_info_access:
- WOLFSSL_MSG("i2v function not yet implemented for Info Access");
- break;
- case NID_ext_key_usage:
- WOLFSSL_MSG("i2v function not yet implemented for Ext Key Usage");
- break;
- case NID_certificate_policies:
- WOLFSSL_MSG("r2i function not yet implemented for Cert Policies");
- break;
- case NID_crl_distribution_points:
- WOLFSSL_MSG("r2i function not yet implemented for CRL Dist Points");
- break;
- default:
- /* If extension type is unknown, return NULL -- QT makes call to
- X509_EXTENSION_get_data() if there is no v3_ext_method */
- WOLFSSL_MSG("X509V3_EXT_get(): Unknown extension type found");
- return NULL;
- }
-
- method.ext_nid = nid;
- ex->ext_method = method;
-
- return (const WOLFSSL_v3_ext_method*)&ex->ext_method;
-}
-
-/* Parses and returns an x509v3 extension internal structure.
- *
- * ext : The X509_EXTENSION for parsing internal structure. If extension is
- * not NULL, get the NID of the extension object and create a new
- * extension-specific internal structure based on the extension type.
- *
- * Returns NULL on error or if NID is not found, otherwise returns a pointer to
- * the extension type-specific X509_EXTENSION internal structure.
- * Return is expected to be free'd by caller.
- */
-void* wolfSSL_X509V3_EXT_d2i(WOLFSSL_X509_EXTENSION* ext)
-{
- const WOLFSSL_v3_ext_method* method;
- int ret;
- WOLFSSL_ASN1_OBJECT* object;
- WOLFSSL_BASIC_CONSTRAINTS* bc;
- WOLFSSL_AUTHORITY_KEYID* akey;
- WOLFSSL_ASN1_STRING* asn1String, *newString;
- WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
- WOLFSSL_STACK* sk;
-
- WOLFSSL_ENTER("wolfSSL_X509V3_EXT_d2i");
-
- if(ext == NULL) {
- WOLFSSL_MSG("Bad function Argument");
- return NULL;
- }
-
- /* extract extension info */
- method = wolfSSL_X509V3_EXT_get(ext);
- if (method == NULL) {
- WOLFSSL_MSG("wolfSSL_X509V3_EXT_get error");
- return NULL;
- }
- object = wolfSSL_X509_EXTENSION_get_object(ext);
- if (object == NULL) {
- WOLFSSL_MSG("X509_EXTENSION_get_object failed");
- return NULL;
- }
-
- /* Return pointer to proper internal structure based on NID */
- switch (object->type) {
- /* basicConstraints */
- case (NID_basic_constraints):
- WOLFSSL_MSG("basicConstraints");
- /* Allocate new BASIC_CONSTRAINTS structure */
- bc = (WOLFSSL_BASIC_CONSTRAINTS*)
- XMALLOC(sizeof(WOLFSSL_BASIC_CONSTRAINTS), NULL,
- DYNAMIC_TYPE_X509_EXT);
- if (bc == NULL) {
- WOLFSSL_MSG("Failed to malloc basic constraints");
- return NULL;
- }
- /* Copy pathlen and CA into BASIC_CONSTRAINTS from object */
- bc->ca = object->ca;
- if (object->pathlen->length > 0) {
- bc->pathlen = wolfSSL_ASN1_INTEGER_dup(object->pathlen);
- if (bc->pathlen == NULL) {
- WOLFSSL_MSG("Failed to duplicate ASN1_INTEGER");
- XFREE(bc, NULL, DYNAMIC_TYPE_X509_EXT);
- return NULL;
- }
- }
- else
- bc->pathlen = NULL;
- return bc;
-
- /* subjectKeyIdentifier */
- case (NID_subject_key_identifier):
- WOLFSSL_MSG("subjectKeyIdentifier");
- asn1String = wolfSSL_X509_EXTENSION_get_data(ext);
- if (asn1String == NULL) {
- WOLFSSL_MSG("X509_EXTENSION_get_data() failed");
- return NULL;
- }
- newString = wolfSSL_ASN1_STRING_new();
- if (newString == NULL) {
- WOLFSSL_MSG("Failed to malloc ASN1_STRING");
- return NULL;
- }
- ret = wolfSSL_ASN1_STRING_set(newString, asn1String->data,
- asn1String->length);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ASN1_STRING_set() failed");
- wolfSSL_ASN1_STRING_free(newString);
- return NULL;
- };
- newString->type = asn1String->type;
- return newString;
-
- /* authorityKeyIdentifier */
- case (NID_authority_key_identifier):
- WOLFSSL_MSG("AuthorityKeyIdentifier");
-
- akey = (WOLFSSL_AUTHORITY_KEYID*)
- XMALLOC(sizeof(WOLFSSL_AUTHORITY_KEYID), NULL,
- DYNAMIC_TYPE_X509_EXT);
- if (akey == NULL) {
- WOLFSSL_MSG("Failed to malloc authority key id");
- return NULL;
- }
-
- akey->keyid = wolfSSL_ASN1_STRING_new();
- if (akey->keyid == NULL) {
- WOLFSSL_MSG("ASN1_STRING_new() failed");
- wolfSSL_AUTHORITY_KEYID_free(akey);
- return NULL;
- }
-
- asn1String = wolfSSL_X509_EXTENSION_get_data(ext);
- if (asn1String == NULL) {
- WOLFSSL_MSG("X509_EXTENSION_get_data() failed");
- wolfSSL_AUTHORITY_KEYID_free(akey);
- return NULL;
- }
-
- ret = wolfSSL_ASN1_STRING_set(akey->keyid, asn1String->data,
- asn1String->length);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ASN1_STRING_set() failed");
- wolfSSL_AUTHORITY_KEYID_free(akey);
- return NULL;
- };
- akey->keyid->type = asn1String->type;
-
- /* For now, set issuer and serial to NULL. This may need to be
- updated for future use */
- akey->issuer = NULL;
- akey->serial = NULL;
- return akey;
-
- /* keyUsage */
- case (NID_key_usage):
- WOLFSSL_MSG("keyUsage");
- /* This may need to be updated for future use. The i2v method for
- keyUsage is not currently set. For now, return the ASN1_STRING
- representation of KeyUsage bit string */
- asn1String = wolfSSL_X509_EXTENSION_get_data(ext);
- if (asn1String == NULL) {
- WOLFSSL_MSG("X509_EXTENSION_get_data() failed");
- return NULL;
- }
- newString = wolfSSL_ASN1_STRING_new();
- if (newString == NULL) {
- WOLFSSL_MSG("Failed to malloc ASN1_STRING");
- return NULL;
- }
- ret = wolfSSL_ASN1_STRING_set(newString, asn1String->data,
- asn1String->length);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ASN1_STRING_set() failed");
- wolfSSL_ASN1_STRING_free(newString);
- return NULL;
- };
- newString->type = asn1String->type;
- return newString;
-
- /* extKeyUsage */
- case (NID_ext_key_usage):
- WOLFSSL_MSG("extKeyUsage not supported yet");
- return NULL;
-
- /* certificatePolicies */
- case (NID_certificate_policies):
- WOLFSSL_MSG("certificatePolicies not supported yet");
- return NULL;
-
- /* cRLDistributionPoints */
- case (NID_crl_distribution_points):
- WOLFSSL_MSG("cRLDistributionPoints not supported yet");
- return NULL;
-
- /* authorityInfoAccess */
- case (NID_info_access):
- WOLFSSL_MSG("AuthorityInfoAccess");
-
- sk = ext->ext_sk;
- if (sk == NULL) {
- WOLFSSL_MSG("ACCESS_DESCRIPTION stack NULL");
- return NULL;
- }
-
- /* AUTHORITY_INFO_ACCESS is a stack of ACCESS_DESCRIPTION entries */
- aia = wolfSSL_sk_new_null();
- if (aia == NULL) {
- WOLFSSL_MSG("Failed to malloc AUTHORITY_INFO_ACCESS");
- return NULL;
- }
- aia->type = STACK_TYPE_ACCESS_DESCRIPTION;
-
- while (sk) {
- WOLFSSL_ACCESS_DESCRIPTION* ad;
- WOLFSSL_ASN1_OBJECT* aiaEntry;
-
- if (sk->type != STACK_TYPE_OBJ) {
- sk = sk->next;
- continue;
- }
-
- aiaEntry = sk->data.obj;
-
- /* ACCESS_DESCRIPTION has two members, method and location.
- Method: ASN1_OBJECT as either AIA_OCSP_OID or AIA_CA_ISSUER_OID
- Location: GENERAL_NAME structure containing the URI. */
-
- ad = (WOLFSSL_ACCESS_DESCRIPTION*)
- XMALLOC(sizeof(WOLFSSL_ACCESS_DESCRIPTION), NULL,
- DYNAMIC_TYPE_X509_EXT);
- if (ad == NULL) {
- WOLFSSL_MSG("Failed to malloc ACCESS_DESCRIPTION");
- XFREE(aia, NULL, DYNAMIC_TYPE_X509_EXT);
- return NULL;
- }
- XMEMSET(ad, 0, sizeof(WOLFSSL_ACCESS_DESCRIPTION));
-
- /* Create new ASN1_OBJECT from oid */
- ad->method = wolfSSL_OBJ_nid2obj(aiaEntry->nid);
- if (ad->method == NULL) {
- WOLFSSL_MSG("OBJ_nid2obj() failed");
- XFREE(aia, NULL, DYNAMIC_TYPE_X509_EXT);
- XFREE(ad, NULL, DYNAMIC_TYPE_X509_EXT);
- return NULL;
- }
-
- /* Allocate memory for GENERAL NAME */
- ad->location = (WOLFSSL_GENERAL_NAME*)
- XMALLOC(sizeof(WOLFSSL_GENERAL_NAME), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (ad->location == NULL) {
- WOLFSSL_MSG("Failed to malloc GENERAL_NAME");
- wolfSSL_ASN1_OBJECT_free(ad->method);
- XFREE(aia, NULL, DYNAMIC_TYPE_X509_EXT);
- XFREE(ad, NULL, DYNAMIC_TYPE_X509_EXT);
- return NULL;
- }
- XMEMSET(ad->location, 0, sizeof(WOLFSSL_GENERAL_NAME));
- ad->location->type = GEN_URI;
- ad->location->d.uniformResourceIdentifier =
- wolfSSL_ASN1_STRING_new();
- /* Set the URI in GENERAL_NAME */
- ret = wolfSSL_ASN1_STRING_set(
- ad->location->d.uniformResourceIdentifier,
- aiaEntry->obj, aiaEntry->objSz);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ASN1_STRING_set() failed");
- wolfSSL_ASN1_OBJECT_free(ad->method);
- XFREE(aia, NULL, DYNAMIC_TYPE_X509_EXT);
- XFREE(ad, NULL, DYNAMIC_TYPE_X509_EXT);
- return NULL;
- }
- /* Push to AUTHORITY_INFO_ACCESS stack */
- ret = wolfSSL_sk_ACCESS_DESCRIPTION_push(aia, ad);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error pushing ASN1 AD onto stack");
- wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
- wolfSSL_ASN1_OBJECT_free(ad->method);
- XFREE(aia, NULL, DYNAMIC_TYPE_X509_EXT);
- XFREE(ad, NULL, DYNAMIC_TYPE_X509_EXT);
- return NULL;
- }
-
- sk = sk->next;
- }
- return aia;
-
- default:
- WOLFSSL_MSG("Extension NID not in table, returning NULL");
- break;
- }
- return NULL;
-}
-
-/* Looks for the extension matching the passed in nid
- *
- * x509 : certificate to get parse through for extension.
- * nid : Extension OID to be found.
- * lastPos : Start search from extension after lastPos.
- * Set to -1 to search from index 0.
- * return >= 0 If successful the extension index is returned.
- * return -1 If extension is not found or error is encountered.
- */
-int wolfSSL_X509_get_ext_by_NID(const WOLFSSL_X509* x509, int nid, int lastPos)
-{
- int extCount = 0, length = 0, outSz = 0, sz = 0, ret = 0;
- int isSet = 0, found = 0, loc;
- const byte* rawCert;
- const byte* input;
- word32 oid, idx = 0, tmpIdx = 0;
- DecodedCert cert;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_ext_by_NID");
-
- if(x509 == NULL){
- WOLFSSL_MSG("\tNot passed a certificate");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if(lastPos < -1 || (lastPos > (wolfSSL_X509_get_ext_count(x509) - 1))){
- WOLFSSL_MSG("\tBad location argument");
- return WOLFSSL_FATAL_ERROR;
- }
-
- loc = lastPos + 1;
-
- rawCert = wolfSSL_X509_get_der((WOLFSSL_X509*)x509, &outSz);
- if (rawCert == NULL) {
- WOLFSSL_MSG("\tX509_get_der() failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- InitDecodedCert( &cert, rawCert, (word32)outSz, 0);
-
- if (ParseCert(&cert, CA_TYPE, NO_VERIFY, NULL) < 0) {
- WOLFSSL_MSG("\tCertificate parsing failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- input = cert.extensions;
- sz = cert.extensionsSz;
-
- if (input == NULL || sz == 0) {
- WOLFSSL_MSG("\tfail: should be an EXTENSIONS");
- FreeDecodedCert(&cert);
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (input[idx++] != ASN_EXTENSIONS) {
- WOLFSSL_MSG("\tfail: should be an EXTENSIONS");
- FreeDecodedCert(&cert);
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (GetLength(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: invalid length");
- FreeDecodedCert(&cert);
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE (1)");
- FreeDecodedCert(&cert);
- return WOLFSSL_FATAL_ERROR;
- }
-
- while (idx < (word32)sz) {
- oid = 0;
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE");
- FreeDecodedCert(&cert);
- return WOLFSSL_FATAL_ERROR;
- }
-
- tmpIdx = idx;
- ret = GetObjectId(input, &idx, &oid, oidCertExtType, sz);
- if (ret < 0) {
- WOLFSSL_MSG("\tfail: OBJECT ID");
- FreeDecodedCert(&cert);
- return WOLFSSL_FATAL_ERROR;
- }
- idx = tmpIdx;
-
- if (extCount >= loc) {
- /* extCount >= loc. Now check if extension has been set */
- isSet = wolfSSL_X509_ext_isSet_by_NID((WOLFSSL_X509*)x509, oid);
-
- if (isSet && ((word32)nid == oid)) {
- found = 1;
- break;
- }
- }
-
- idx += length;
- extCount++;
- } /* while(idx < sz) */
-
- FreeDecodedCert(&cert);
-
- return found ? extCount : WOLFSSL_FATAL_ERROR;
-}
-
-
-#endif /* OPENSSL_ALL */
-
-WOLFSSL_ASN1_BIT_STRING* wolfSSL_ASN1_BIT_STRING_new(void)
-{
- WOLFSSL_ASN1_BIT_STRING* str;
-
- str = (WOLFSSL_ASN1_BIT_STRING*)XMALLOC(sizeof(WOLFSSL_ASN1_BIT_STRING),
- NULL, DYNAMIC_TYPE_OPENSSL);
- if (str) {
- XMEMSET(str, 0, sizeof(WOLFSSL_ASN1_BIT_STRING));
- }
- return str;
-}
-
-void wolfSSL_ASN1_BIT_STRING_free(WOLFSSL_ASN1_BIT_STRING* str)
-{
- if (str) {
- if (str->data) {
- XFREE(str->data, NULL, DYNAMIC_TYPE_OPENSSL);
- str->data = NULL;
- }
- XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-int wolfSSL_ASN1_BIT_STRING_get_bit(const WOLFSSL_ASN1_BIT_STRING* str, int i)
-{
- if (!str || !str->data || str->length <= (i/8) || i < 0) {
- return WOLFSSL_FAILURE;
- }
-
- return (str->data[i/8] & (1<<(7-(i%8)))) ? 1 : 0;
-}
-
-/* Looks for the extension matching the passed in nid
- *
- * c : if not null then is set to status value -2 if multiple occurrences
- * of the extension are found, -1 if not found, 0 if found and not
- * critical, and 1 if found and critical.
- * nid : Extension OID to be found.
- * idx : if NULL return first extension found match, otherwise start search at
- * idx location and set idx to the location of extension returned.
- * returns NULL or a pointer to an WOLFSSL_ASN1_BIT_STRING (for KEY_USAGE_OID)
- * or WOLFSSL_STACK (for other)
- * holding extension structure
- *
- * NOTE code for decoding extensions is in asn.c DecodeCertExtensions --
- * use already decoded extension in this function to avoid decoding twice.
- * Currently we do not make use of idx since getting pre decoded extensions.
- */
-void* wolfSSL_X509_get_ext_d2i(const WOLFSSL_X509* x509, int nid, int* c,
- int* idx)
-{
- void* ret = NULL;
- WOLFSSL_STACK* sk = NULL;
- WOLFSSL_ASN1_OBJECT* obj = NULL;
- WOLFSSL_GENERAL_NAME* gn = NULL;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_ext_d2i");
-
- if (x509 == NULL) {
- return NULL;
- }
-
- if (c != NULL) {
- *c = -1; /* default to not found */
- }
-
- switch (nid) {
- case BASIC_CA_OID:
- if (x509->basicConstSet) {
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
- return NULL;
- }
- if (c != NULL) {
- *c = x509->basicConstCrit;
- }
- obj->type = BASIC_CA_OID;
- obj->grp = oidCertExtType;
- obj->nid = nid;
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
- defined(WOLFSSL_APACHE_HTTPD)
- obj->ca = x509->isCa;
- #endif
- }
- else {
- WOLFSSL_MSG("No Basic Constraint set");
- }
- return obj;
-
- case ALT_NAMES_OID:
- {
- DNS_entry* dns = NULL;
- /* Malloc GENERAL_NAME stack */
- sk = (WOLF_STACK_OF(WOLFSSL_GENERAL_NAME)*)XMALLOC(
- sizeof(WOLF_STACK_OF(WOLFSSL_GENERAL_NAME)), NULL,
- DYNAMIC_TYPE_ASN1);
- if (sk == NULL) {
- return NULL;
- }
- XMEMSET(sk, 0, sizeof(WOLF_STACK_OF(WOLFSSL_GENERAL_NAME)));
- sk->type = STACK_TYPE_GEN_NAME;
-
- if (x509->subjAltNameSet && x509->altNames != NULL) {
- /* alt names are DNS_entry structs */
- if (c != NULL) {
- if (x509->altNames->next != NULL) {
- *c = -2; /* more then one found */
- }
- else {
- *c = x509->subjAltNameCrit;
- }
- }
-
- dns = x509->altNames;
- /* Currently only support GEN_DNS type */
- while (dns != NULL) {
- gn = wolfSSL_GENERAL_NAME_new();
- if (gn == NULL) {
- WOLFSSL_MSG("Error creating GENERAL_NAME");
- wolfSSL_sk_free(sk);
- return NULL;
- }
-
- gn->type = dns->type;
- gn->d.ia5->length = dns->len;
- if (wolfSSL_ASN1_STRING_set(gn->d.ia5, dns->name,
- gn->d.ia5->length) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ASN1_STRING_set failed");
- wolfSSL_GENERAL_NAME_free(gn);
- wolfSSL_sk_free(sk);
- return NULL;
- }
-
- dns = dns->next;
- /* last dns in list add at end of function */
- if (dns != NULL) {
- if (wolfSSL_sk_GENERAL_NAME_push(sk, gn) !=
- WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error pushing ASN1 object onto stack");
- wolfSSL_GENERAL_NAME_free(gn);
- wolfSSL_sk_free(sk);
- sk = NULL;
- }
- }
- }
- }
- else {
- WOLFSSL_MSG("No Alt Names set");
- }
-
- break;
- }
-
- case CRL_DIST_OID:
- if (x509->CRLdistSet && x509->CRLInfo != NULL) {
- if (c != NULL) {
- *c = x509->CRLdistCrit;
- }
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
- return NULL;
- }
- obj->type = CRL_DIST_OID;
- obj->grp = oidCertExtType;
- obj->obj = x509->CRLInfo;
- obj->objSz = x509->CRLInfoSz;
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA ;
- }
- else {
- WOLFSSL_MSG("No CRL dist set");
- }
- break;
-
- case AUTH_INFO_OID:
- if (x509->authInfoSet && x509->authInfo != NULL) {
- if (c != NULL) {
- *c = x509->authInfoCrit;
- }
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
- return NULL;
- }
- obj->type = AUTH_INFO_OID;
- obj->grp = oidCertExtType;
- obj->obj = x509->authInfo;
- obj->objSz = x509->authInfoSz;
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA;
- }
- else {
- WOLFSSL_MSG("No Auth Info set");
- }
- break;
-
- case AUTH_KEY_OID:
- if (x509->authKeyIdSet) {
- if (c != NULL) {
- *c = x509->authKeyIdCrit;
- }
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
- return NULL;
- }
- obj->type = AUTH_KEY_OID;
- obj->grp = oidCertExtType;
- obj->obj = x509->authKeyId;
- obj->objSz = x509->authKeyIdSz;
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA;
- }
- else {
- WOLFSSL_MSG("No Auth Key set");
- }
- break;
-
- case SUBJ_KEY_OID:
- if (x509->subjKeyIdSet) {
- if (c != NULL) {
- *c = x509->subjKeyIdCrit;
- }
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
- return NULL;
- }
- obj->type = SUBJ_KEY_OID;
- obj->grp = oidCertExtType;
- obj->obj = x509->subjKeyId;
- obj->objSz = x509->subjKeyIdSz;
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA;
- }
- else {
- WOLFSSL_MSG("No Subject Key set");
- }
- break;
-
- case CERT_POLICY_OID:
- {
- #ifdef WOLFSSL_CERT_EXT
- int i;
-
- if (x509->certPoliciesNb > 0) {
- if (c != NULL) {
- if (x509->certPoliciesNb > 1) {
- *c = -2;
- }
- else {
- *c = 0;
- }
- }
-
- sk = wolfSSL_sk_new_asn1_obj();
- if (sk == NULL) {
- return NULL;
- }
-
- for (i = 0; i < x509->certPoliciesNb - 1; i++) {
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
- wolfSSL_sk_ASN1_OBJECT_free(sk);
- return NULL;
- }
- obj->type = CERT_POLICY_OID;
- obj->grp = oidCertExtType;
- obj->obj = (byte*)(x509->certPolicies[i]);
- obj->objSz = MAX_CERTPOL_SZ;
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA;
- if (wolfSSL_sk_ASN1_OBJECT_push(sk, obj)
- != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error pushing ASN1 object onto stack");
- wolfSSL_ASN1_OBJECT_free(obj);
- wolfSSL_sk_ASN1_OBJECT_free(sk);
- sk = NULL;
- }
- }
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
- wolfSSL_sk_ASN1_OBJECT_free(sk);
- return NULL;
- }
- obj->type = CERT_POLICY_OID;
- obj->grp = oidCertExtType;
- obj->obj = (byte*)(x509->certPolicies[i]);
- obj->objSz = MAX_CERTPOL_SZ;
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA;
- }
- else {
- WOLFSSL_MSG("No Cert Policy set");
- }
- #elif defined(WOLFSSL_SEP)
- if (x509->certPolicySet) {
- if (c != NULL) {
- *c = x509->certPolicyCrit;
- }
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
- return NULL;
- }
- obj->type = CERT_POLICY_OID;
- obj->grp = oidCertExtType;
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- }
- else {
- WOLFSSL_MSG("No Cert Policy set");
- }
- #else
- WOLFSSL_MSG("wolfSSL not built with WOLFSSL_SEP or WOLFSSL_CERT_EXT");
- #endif
- break;
- }
- case KEY_USAGE_OID:
- {
- WOLFSSL_ASN1_BIT_STRING* bit_str = NULL;
- if (x509->keyUsageSet) {
- if (c != NULL) {
- *c = x509->keyUsageCrit;
- }
-
- bit_str = wolfSSL_ASN1_BIT_STRING_new();
- if (bit_str == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_BIT_STRING struct");
- return NULL;
- }
-
- bit_str->type = KEY_USAGE_OID;
- bit_str->flags = 0;
- bit_str->length = sizeof(word16);
- bit_str->data = (byte*)XMALLOC(bit_str->length, NULL, DYNAMIC_TYPE_OPENSSL);
- if (bit_str->data == NULL) {
- wolfSSL_ASN1_BIT_STRING_free(bit_str);
- return NULL;
- }
- XMEMCPY(bit_str->data, &x509->keyUsage, bit_str->length);
- }
- else {
- WOLFSSL_MSG("No Key Usage set");
- }
- /* don't add stack of and return bit string directly */
- return bit_str;
- }
- case INHIBIT_ANY_OID:
- WOLFSSL_MSG("INHIBIT ANY extension not supported");
- break;
-
- case EXT_KEY_USAGE_OID:
- if (x509->extKeyUsageSrc != NULL) {
- if (c != NULL) {
- if (x509->extKeyUsageCount > 1) {
- *c = -2;
- }
- else {
- *c = x509->extKeyUsageCrit;
- }
- }
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
- return NULL;
- }
- obj->type = EXT_KEY_USAGE_OID;
- obj->grp = oidCertExtType;
- obj->obj = x509->extKeyUsageSrc;
- obj->objSz = x509->extKeyUsageSz;
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA;
- }
- else {
- WOLFSSL_MSG("No Extended Key Usage set");
- }
- break;
-
- case NAME_CONS_OID:
- WOLFSSL_MSG("Name Constraint OID extension not supported");
- break;
-
- case PRIV_KEY_USAGE_PERIOD_OID:
- WOLFSSL_MSG("Private Key Usage Period extension not supported");
- break;
-
- case SUBJECT_INFO_ACCESS:
- WOLFSSL_MSG("Subject Info Access extension not supported");
- break;
-
- case POLICY_MAP_OID:
- WOLFSSL_MSG("Policy Map extension not supported");
- break;
-
- case POLICY_CONST_OID:
- WOLFSSL_MSG("Policy Constraint extension not supported");
- break;
-
- case ISSUE_ALT_NAMES_OID:
- WOLFSSL_MSG("Issue Alt Names extension not supported");
- break;
-
- case TLS_FEATURE_OID:
- WOLFSSL_MSG("TLS Feature extension not supported");
- break;
-
- default:
- WOLFSSL_MSG("Unsupported/Unknown extension OID");
- }
-
- /* make sure stack of is allocated */
- if ((obj || gn) && sk == NULL) {
- sk = wolfSSL_sk_new_asn1_obj();
- if (sk == NULL) {
- goto err;
- }
- }
- if (obj && wolfSSL_sk_ASN1_OBJECT_push(sk, obj) == WOLFSSL_SUCCESS) {
- /* obj pushed successfully on stack */
- }
- else if (gn && wolfSSL_sk_GENERAL_NAME_push(sk, gn) == WOLFSSL_SUCCESS) {
- /* gn pushed successfully on stack */
- }
- else {
- /* Nothing to push or push failed */
- WOLFSSL_MSG("Error pushing ASN1_OBJECT or GENERAL_NAME object onto stack "
- "or nothing to push.");
- goto err;
- }
- ret = sk;
-
- (void)idx;
-
- return ret;
-
-err:
- if (obj) {
- wolfSSL_ASN1_OBJECT_free(obj);
- }
- if (gn) {
- wolfSSL_GENERAL_NAME_free(gn);
- }
- if (sk) {
- wolfSSL_sk_ASN1_OBJECT_free(sk);
- }
- return NULL;
-}
-
-
-int wolfSSL_X509_add_altname(WOLFSSL_X509* x509, const char* name, int type)
-{
- DNS_entry* newAltName = NULL;
- char* nameCopy = NULL;
- word32 nameSz;
-
- if (x509 == NULL)
- return WOLFSSL_FAILURE;
-
- if (name == NULL)
- return WOLFSSL_SUCCESS;
-
- nameSz = (word32)XSTRLEN(name);
- if (nameSz == 0)
- return WOLFSSL_SUCCESS;
-
- newAltName = (DNS_entry*)XMALLOC(sizeof(DNS_entry),
- x509->heap, DYNAMIC_TYPE_ALTNAME);
- if (newAltName == NULL)
- return WOLFSSL_FAILURE;
-
- nameCopy = (char*)XMALLOC(nameSz + 1, x509->heap, DYNAMIC_TYPE_ALTNAME);
- if (nameCopy == NULL) {
- XFREE(newAltName, x509->heap, DYNAMIC_TYPE_ALTNAME);
- return WOLFSSL_FAILURE;
- }
-
- XMEMCPY(nameCopy, name, nameSz + 1);
-
- newAltName->next = x509->altNames;
- newAltName->type = type;
- newAltName->len = nameSz;
- newAltName->name = nameCopy;
- x509->altNames = newAltName;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_X509_add_ext(WOLFSSL_X509 *x509, WOLFSSL_X509_EXTENSION *ext, int loc)
-{
- WOLFSSL_STUB("wolfSSL_X509_add_ext");
- (void)x509;
- (void)ext;
- (void)loc;
- return WOLFSSL_FAILURE;
-}
-
-/* currently LHASH is not implemented (and not needed for Apache port) */
-WOLFSSL_X509_EXTENSION* wolfSSL_X509V3_EXT_conf_nid(
- WOLF_LHASH_OF(CONF_VALUE)* conf, WOLFSSL_X509V3_CTX* ctx, int nid,
- char* value)
-{
- WOLFSSL_STUB("wolfSSL_X509V3_EXT_conf_nid");
-
- if (conf != NULL) {
- WOLFSSL_MSG("Handling LHASH not implemented yet");
- return NULL;
- }
-
- (void)conf;
- (void)ctx;
- (void)nid;
- (void)value;
- return NULL;
-}
-
-void wolfSSL_X509V3_set_ctx_nodb(WOLFSSL_X509V3_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_X509V3_set_ctx_nodb");
- (void)ctx;
-}
-#endif /* !NO_WOLFSSL_STUB */
-
-/* Returns pointer to ASN1_OBJECT from an X509_EXTENSION object */
-WOLFSSL_ASN1_OBJECT* wolfSSL_X509_EXTENSION_get_object \
- (WOLFSSL_X509_EXTENSION* ext)
-{
- WOLFSSL_ENTER("wolfSSL_X509_EXTENSION_get_object");
- if(ext == NULL)
- return NULL;
- return ext->obj;
-}
-
-/* Returns pointer to ASN1_STRING in X509_EXTENSION object */
-WOLFSSL_ASN1_STRING* wolfSSL_X509_EXTENSION_get_data(WOLFSSL_X509_EXTENSION* ext)
-{
- WOLFSSL_ENTER("wolfSSL_X509_EXTENSION_get_data");
- if (ext == NULL)
- return NULL;
- return &ext->value;
-}
-
-#if !defined(NO_PWDBASED)
-int wolfSSL_X509_digest(const WOLFSSL_X509* x509, const WOLFSSL_EVP_MD* digest,
- unsigned char* buf, unsigned int* len)
-{
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_X509_digest");
-
- if (x509 == NULL || digest == NULL) {
- WOLFSSL_MSG("Null argument found");
- return WOLFSSL_FAILURE;
- }
-
- if (x509->derCert == NULL) {
- WOLFSSL_MSG("No DER certificate stored in X509");
- return WOLFSSL_FAILURE;
- }
-
- ret = wolfSSL_EVP_Digest(x509->derCert->buffer, x509->derCert->length, buf,
- len, digest, NULL);
- WOLFSSL_LEAVE("wolfSSL_X509_digest", ret);
- return ret;
-}
-#endif
-
-int wolfSSL_use_PrivateKey(WOLFSSL* ssl, WOLFSSL_EVP_PKEY* pkey)
-{
- WOLFSSL_ENTER("wolfSSL_use_PrivateKey");
- if (ssl == NULL || pkey == NULL ) {
- return WOLFSSL_FAILURE;
- }
-
- return wolfSSL_use_PrivateKey_buffer(ssl, (unsigned char*)pkey->pkey.ptr,
- pkey->pkey_sz, WOLFSSL_FILETYPE_ASN1);
-}
-
-
-int wolfSSL_use_PrivateKey_ASN1(int pri, WOLFSSL* ssl, const unsigned char* der,
- long derSz)
-{
- WOLFSSL_ENTER("wolfSSL_use_PrivateKey_ASN1");
- if (ssl == NULL || der == NULL ) {
- return WOLFSSL_FAILURE;
- }
-
- (void)pri; /* type of private key */
- return wolfSSL_use_PrivateKey_buffer(ssl, der, derSz, WOLFSSL_FILETYPE_ASN1);
-}
-/******************************************************************************
-* wolfSSL_CTX_use_PrivateKey_ASN1 - loads a private key buffer into the SSL ctx
-*
-* RETURNS:
-* returns WOLFSSL_SUCCESS on success, otherwise returns WOLFSSL_FAILURE
-*/
-
-int wolfSSL_CTX_use_PrivateKey_ASN1(int pri, WOLFSSL_CTX* ctx,
- unsigned char* der, long derSz)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_ASN1");
- if (ctx == NULL || der == NULL ) {
- return WOLFSSL_FAILURE;
- }
-
- (void)pri; /* type of private key */
- return wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, derSz, WOLFSSL_FILETYPE_ASN1);
-}
-
-
-#ifndef NO_RSA
-int wolfSSL_use_RSAPrivateKey_ASN1(WOLFSSL* ssl, unsigned char* der, long derSz)
-{
- WOLFSSL_ENTER("wolfSSL_use_RSAPrivateKey_ASN1");
- if (ssl == NULL || der == NULL ) {
- return WOLFSSL_FAILURE;
- }
-
- return wolfSSL_use_PrivateKey_buffer(ssl, der, derSz, WOLFSSL_FILETYPE_ASN1);
-}
-#endif
-
-int wolfSSL_use_certificate(WOLFSSL* ssl, WOLFSSL_X509* x509)
-{
- long idx;
-
- WOLFSSL_ENTER("wolfSSL_use_certificate");
- if (x509 != NULL && ssl != NULL && x509->derCert != NULL) {
- if (ProcessBuffer(NULL, x509->derCert->buffer, x509->derCert->length,
- WOLFSSL_FILETYPE_ASN1, CERT_TYPE, ssl, &idx, 0,
- GET_VERIFY_SETTING_SSL(ssl)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
- }
-
- (void)idx;
- return WOLFSSL_FAILURE;
-}
-
-#endif /* NO_CERTS */
-
-#endif /* OPENSSL_EXTRA */
-
-#ifndef NO_CERTS
-int wolfSSL_use_certificate_ASN1(WOLFSSL* ssl, const unsigned char* der,
- int derSz)
-{
- long idx;
-
- WOLFSSL_ENTER("wolfSSL_use_certificate_ASN1");
- if (der != NULL && ssl != NULL) {
- if (ProcessBuffer(NULL, der, derSz, WOLFSSL_FILETYPE_ASN1, CERT_TYPE,
- ssl, &idx, 0, GET_VERIFY_SETTING_SSL(ssl)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
- }
-
- (void)idx;
- return WOLFSSL_FAILURE;
-}
-
-#ifndef NO_FILESYSTEM
-
-WOLFSSL_ABI
-int wolfSSL_use_certificate_file(WOLFSSL* ssl, const char* file, int format)
-{
- WOLFSSL_ENTER("wolfSSL_use_certificate_file");
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (ProcessFile(ssl->ctx, file, format, CERT_TYPE,
- ssl, 0, NULL, GET_VERIFY_SETTING_SSL(ssl)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-
-WOLFSSL_ABI
-int wolfSSL_use_PrivateKey_file(WOLFSSL* ssl, const char* file, int format)
-{
- WOLFSSL_ENTER("wolfSSL_use_PrivateKey_file");
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (ProcessFile(ssl->ctx, file, format, PRIVATEKEY_TYPE,
- ssl, 0, NULL, GET_VERIFY_SETTING_SSL(ssl)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-
-WOLFSSL_ABI
-int wolfSSL_use_certificate_chain_file(WOLFSSL* ssl, const char* file)
-{
- /* process up to MAX_CHAIN_DEPTH plus subject cert */
- WOLFSSL_ENTER("wolfSSL_use_certificate_chain_file");
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (ProcessFile(ssl->ctx, file, WOLFSSL_FILETYPE_PEM, CERT_TYPE,
- ssl, 1, NULL, GET_VERIFY_SETTING_SSL(ssl)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-int wolfSSL_use_certificate_chain_file_format(WOLFSSL* ssl, const char* file,
- int format)
-{
- /* process up to MAX_CHAIN_DEPTH plus subject cert */
- WOLFSSL_ENTER("wolfSSL_use_certificate_chain_file_format");
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (ProcessFile(ssl->ctx, file, format, CERT_TYPE, ssl, 1,
- NULL, GET_VERIFY_SETTING_SSL(ssl)) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-#endif /* !NO_FILESYSTEM */
-#endif /* !NO_CERTS */
-
-#ifdef HAVE_ECC
-
-/* Set Temp CTX EC-DHE size in octets, should be 20 - 66 for 160 - 521 bit */
-int wolfSSL_CTX_SetTmpEC_DHE_Sz(WOLFSSL_CTX* ctx, word16 sz)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- if (sz == 0) {
- /* applies only to ECDSA */
- if (ctx->privateKeyType != ecc_dsa_sa_algo)
- return WOLFSSL_SUCCESS;
-
- if (ctx->privateKeySz == 0) {
- WOLFSSL_MSG("Must set private key/cert first");
- return BAD_FUNC_ARG;
- }
-
- sz = (word16)ctx->privateKeySz;
- }
-
- /* check size */
- if (sz < ECC_MINSIZE || sz > ECC_MAXSIZE)
- return BAD_FUNC_ARG;
-
- ctx->eccTempKeySz = sz;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* Set Temp SSL EC-DHE size in octets, should be 20 - 66 for 160 - 521 bit */
-int wolfSSL_SetTmpEC_DHE_Sz(WOLFSSL* ssl, word16 sz)
-{
- if (ssl == NULL || sz < ECC_MINSIZE || sz > ECC_MAXSIZE)
- return BAD_FUNC_ARG;
-
- ssl->eccTempKeySz = sz;
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* HAVE_ECC */
-
-
-#ifdef OPENSSL_EXTRA
-
-#ifndef NO_FILESYSTEM
-int wolfSSL_CTX_use_RSAPrivateKey_file(WOLFSSL_CTX* ctx,const char* file,
- int format)
-{
- WOLFSSL_ENTER("SSL_CTX_use_RSAPrivateKey_file");
-
- return wolfSSL_CTX_use_PrivateKey_file(ctx, file, format);
-}
-
-
-int wolfSSL_use_RSAPrivateKey_file(WOLFSSL* ssl, const char* file, int format)
-{
- WOLFSSL_ENTER("wolfSSL_use_RSAPrivateKey_file");
-
- return wolfSSL_use_PrivateKey_file(ssl, file, format);
-}
-#endif /* NO_FILESYSTEM */
-
-
-/* Copies the master secret over to out buffer. If outSz is 0 returns the size
- * of master secret.
- *
- * ses : a session from completed TLS/SSL handshake
- * out : buffer to hold copy of master secret
- * outSz : size of out buffer
- * returns : number of bytes copied into out buffer on success
- * less then or equal to 0 is considered a failure case
- */
-int wolfSSL_SESSION_get_master_key(const WOLFSSL_SESSION* ses,
- unsigned char* out, int outSz)
-{
- int size;
-
- if (outSz == 0) {
- return SECRET_LEN;
- }
-
- if (ses == NULL || out == NULL || outSz < 0) {
- return 0;
- }
-
- if (outSz > SECRET_LEN) {
- size = SECRET_LEN;
- }
- else {
- size = outSz;
- }
-
- XMEMCPY(out, ses->masterSecret, size);
- return size;
-}
-
-
-int wolfSSL_SESSION_get_master_key_length(const WOLFSSL_SESSION* ses)
-{
- (void)ses;
- return SECRET_LEN;
-}
-
-#endif /* OPENSSL_EXTRA */
-
-#ifndef NO_FILESYSTEM
-#ifdef HAVE_NTRU
-
-int wolfSSL_CTX_use_NTRUPrivateKey_file(WOLFSSL_CTX* ctx, const char* file)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_use_NTRUPrivateKey_file");
-
- if (ctx == NULL)
- return WOLFSSL_FAILURE;
-
- if (ProcessFile(ctx, file, WOLFSSL_FILETYPE_RAW, PRIVATEKEY_TYPE, NULL, 0,
- NULL, GET_VERIFY_SETTING_CTX(ctx)) == WOLFSSL_SUCCESS) {
- ctx->haveNTRU = 1;
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-#endif /* HAVE_NTRU */
-
-
-#endif /* NO_FILESYSTEM */
-
-
-void wolfSSL_CTX_set_verify(WOLFSSL_CTX* ctx, int mode, VerifyCallback vc)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_set_verify");
- if (ctx == NULL)
- return;
-
- if (mode & WOLFSSL_VERIFY_PEER) {
- ctx->verifyPeer = 1;
- ctx->verifyNone = 0; /* in case previously set */
- }
-
- if (mode == WOLFSSL_VERIFY_NONE) {
- ctx->verifyNone = 1;
- ctx->verifyPeer = 0; /* in case previously set */
- }
-
- if (mode & WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT) {
- ctx->failNoCert = 1;
- }
-
- if (mode & WOLFSSL_VERIFY_FAIL_EXCEPT_PSK) {
- ctx->failNoCert = 0; /* fail on all is set to fail on PSK */
- ctx->failNoCertxPSK = 1;
- }
-
- ctx->verifyCallback = vc;
-}
-
-#ifdef OPENSSL_ALL
-void wolfSSL_CTX_set_cert_verify_callback(WOLFSSL_CTX* ctx,
- CertVerifyCallback cb, void* arg)
-{
- WOLFSSL_ENTER("SSL_CTX_set_cert_verify_callback");
- if (ctx == NULL)
- return;
-
- ctx->verifyCertCb = cb;
- ctx->verifyCertCbArg = arg;
-}
-#endif
-
-
-void wolfSSL_set_verify(WOLFSSL* ssl, int mode, VerifyCallback vc)
-{
- WOLFSSL_ENTER("wolfSSL_set_verify");
- if (ssl == NULL)
- return;
-
- if (mode & WOLFSSL_VERIFY_PEER) {
- ssl->options.verifyPeer = 1;
- ssl->options.verifyNone = 0; /* in case previously set */
- }
-
- if (mode == WOLFSSL_VERIFY_NONE) {
- ssl->options.verifyNone = 1;
- ssl->options.verifyPeer = 0; /* in case previously set */
- }
-
- if (mode & WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT)
- ssl->options.failNoCert = 1;
-
- if (mode & WOLFSSL_VERIFY_FAIL_EXCEPT_PSK) {
- ssl->options.failNoCert = 0; /* fail on all is set to fail on PSK */
- ssl->options.failNoCertxPSK = 1;
- }
-
- ssl->verifyCallback = vc;
-}
-
-void wolfSSL_set_verify_result(WOLFSSL *ssl, long v)
-{
- WOLFSSL_ENTER("wolfSSL_set_verify_result");
-
- if (ssl == NULL)
- return;
-
-#ifdef OPENSSL_ALL
- ssl->verifyCallbackResult = v;
-#else
- (void)v;
- WOLFSSL_STUB("wolfSSL_set_verify_result");
-#endif
-}
-
-/* store user ctx for verify callback */
-void wolfSSL_SetCertCbCtx(WOLFSSL* ssl, void* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_SetCertCbCtx");
- if (ssl)
- ssl->verifyCbCtx = ctx;
-}
-
-
-/* store context CA Cache addition callback */
-void wolfSSL_CTX_SetCACb(WOLFSSL_CTX* ctx, CallbackCACache cb)
-{
- if (ctx && ctx->cm)
- ctx->cm->caCacheCallback = cb;
-}
-
-
-#if defined(PERSIST_CERT_CACHE)
-
-#if !defined(NO_FILESYSTEM)
-
-/* Persist cert cache to file */
-int wolfSSL_CTX_save_cert_cache(WOLFSSL_CTX* ctx, const char* fname)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_save_cert_cache");
-
- if (ctx == NULL || fname == NULL)
- return BAD_FUNC_ARG;
-
- return CM_SaveCertCache(ctx->cm, fname);
-}
-
-
-/* Persist cert cache from file */
-int wolfSSL_CTX_restore_cert_cache(WOLFSSL_CTX* ctx, const char* fname)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_restore_cert_cache");
-
- if (ctx == NULL || fname == NULL)
- return BAD_FUNC_ARG;
-
- return CM_RestoreCertCache(ctx->cm, fname);
-}
-
-#endif /* NO_FILESYSTEM */
-
-/* Persist cert cache to memory */
-int wolfSSL_CTX_memsave_cert_cache(WOLFSSL_CTX* ctx, void* mem,
- int sz, int* used)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_memsave_cert_cache");
-
- if (ctx == NULL || mem == NULL || used == NULL || sz <= 0)
- return BAD_FUNC_ARG;
-
- return CM_MemSaveCertCache(ctx->cm, mem, sz, used);
-}
-
-
-/* Restore cert cache from memory */
-int wolfSSL_CTX_memrestore_cert_cache(WOLFSSL_CTX* ctx, const void* mem, int sz)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_memrestore_cert_cache");
-
- if (ctx == NULL || mem == NULL || sz <= 0)
- return BAD_FUNC_ARG;
-
- return CM_MemRestoreCertCache(ctx->cm, mem, sz);
-}
-
-
-/* get how big the the cert cache save buffer needs to be */
-int wolfSSL_CTX_get_cert_cache_memsize(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_get_cert_cache_memsize");
-
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- return CM_GetCertCacheMemSize(ctx->cm);
-}
-
-#endif /* PERSIST_CERT_CACHE */
-#endif /* !NO_CERTS */
-
-
-#ifndef NO_SESSION_CACHE
-
-WOLFSSL_ABI
-WOLFSSL_SESSION* wolfSSL_get_session(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("SSL_get_session");
- if (ssl)
- return GetSession(ssl, 0, 1);
-
- return NULL;
-}
-
-
-WOLFSSL_ABI
-int wolfSSL_set_session(WOLFSSL* ssl, WOLFSSL_SESSION* session)
-{
- WOLFSSL_ENTER("SSL_set_session");
- if (session)
- return SetSession(ssl, session);
-
- return WOLFSSL_FAILURE;
-}
-
-
-#ifndef NO_CLIENT_CACHE
-
-/* Associate client session with serverID, find existing or store for saving
- if newSession flag on, don't reuse existing session
- WOLFSSL_SUCCESS on ok */
-int wolfSSL_SetServerID(WOLFSSL* ssl, const byte* id, int len, int newSession)
-{
- WOLFSSL_SESSION* session = NULL;
-
- WOLFSSL_ENTER("wolfSSL_SetServerID");
-
- if (ssl == NULL || id == NULL || len <= 0)
- return BAD_FUNC_ARG;
-
- if (newSession == 0) {
- session = GetSessionClient(ssl, id, len);
- if (session) {
- if (SetSession(ssl, session) != WOLFSSL_SUCCESS) {
- #ifdef HAVE_EXT_CACHE
- wolfSSL_SESSION_free(session);
- #endif
- WOLFSSL_MSG("SetSession failed");
- session = NULL;
- }
- }
- }
-
- if (session == NULL) {
- WOLFSSL_MSG("Valid ServerID not cached already");
-
- ssl->session.idLen = (word16)min(SERVER_ID_LEN, (word32)len);
- XMEMCPY(ssl->session.serverID, id, ssl->session.idLen);
- }
- #ifdef HAVE_EXT_CACHE
- else
- wolfSSL_SESSION_free(session);
- #endif
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* NO_CLIENT_CACHE */
-
-#if defined(PERSIST_SESSION_CACHE)
-
-/* for persistence, if changes to layout need to increment and modify
- save_session_cache() and restore_session_cache and memory versions too */
-#define WOLFSSL_CACHE_VERSION 2
-
-/* Session Cache Header information */
-typedef struct {
- int version; /* cache layout version id */
- int rows; /* session rows */
- int columns; /* session columns */
- int sessionSz; /* sizeof WOLFSSL_SESSION */
-} cache_header_t;
-
-/* current persistence layout is:
-
- 1) cache_header_t
- 2) SessionCache
- 3) ClientCache
-
- update WOLFSSL_CACHE_VERSION if change layout for the following
- PERSISTENT_SESSION_CACHE functions
-*/
-
-
-/* get how big the the session cache save buffer needs to be */
-int wolfSSL_get_session_cache_memsize(void)
-{
- int sz = (int)(sizeof(SessionCache) + sizeof(cache_header_t));
-
- #ifndef NO_CLIENT_CACHE
- sz += (int)(sizeof(ClientCache));
- #endif
-
- return sz;
-}
-
-
-/* Persist session cache to memory */
-int wolfSSL_memsave_session_cache(void* mem, int sz)
-{
- int i;
- cache_header_t cache_header;
- SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
-#ifndef NO_CLIENT_CACHE
- ClientRow* clRow;
-#endif
-
- WOLFSSL_ENTER("wolfSSL_memsave_session_cache");
-
- if (sz < wolfSSL_get_session_cache_memsize()) {
- WOLFSSL_MSG("Memory buffer too small");
- return BUFFER_E;
- }
-
- cache_header.version = WOLFSSL_CACHE_VERSION;
- cache_header.rows = SESSION_ROWS;
- cache_header.columns = SESSIONS_PER_ROW;
- cache_header.sessionSz = (int)sizeof(WOLFSSL_SESSION);
- XMEMCPY(mem, &cache_header, sizeof(cache_header));
-
- if (wc_LockMutex(&session_mutex) != 0) {
- WOLFSSL_MSG("Session cache mutex lock failed");
- return BAD_MUTEX_E;
- }
-
- for (i = 0; i < cache_header.rows; ++i)
- XMEMCPY(row++, SessionCache + i, sizeof(SessionRow));
-
-#ifndef NO_CLIENT_CACHE
- clRow = (ClientRow*)row;
- for (i = 0; i < cache_header.rows; ++i)
- XMEMCPY(clRow++, ClientCache + i, sizeof(ClientRow));
-#endif
-
- wc_UnLockMutex(&session_mutex);
-
- WOLFSSL_LEAVE("wolfSSL_memsave_session_cache", WOLFSSL_SUCCESS);
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* Restore the persistent session cache from memory */
-int wolfSSL_memrestore_session_cache(const void* mem, int sz)
-{
- int i;
- cache_header_t cache_header;
- SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
-#ifndef NO_CLIENT_CACHE
- ClientRow* clRow;
-#endif
-
- WOLFSSL_ENTER("wolfSSL_memrestore_session_cache");
-
- if (sz < wolfSSL_get_session_cache_memsize()) {
- WOLFSSL_MSG("Memory buffer too small");
- return BUFFER_E;
- }
-
- XMEMCPY(&cache_header, mem, sizeof(cache_header));
- if (cache_header.version != WOLFSSL_CACHE_VERSION ||
- cache_header.rows != SESSION_ROWS ||
- cache_header.columns != SESSIONS_PER_ROW ||
- cache_header.sessionSz != (int)sizeof(WOLFSSL_SESSION)) {
-
- WOLFSSL_MSG("Session cache header match failed");
- return CACHE_MATCH_ERROR;
- }
-
- if (wc_LockMutex(&session_mutex) != 0) {
- WOLFSSL_MSG("Session cache mutex lock failed");
- return BAD_MUTEX_E;
- }
-
- for (i = 0; i < cache_header.rows; ++i)
- XMEMCPY(SessionCache + i, row++, sizeof(SessionRow));
-
-#ifndef NO_CLIENT_CACHE
- clRow = (ClientRow*)row;
- for (i = 0; i < cache_header.rows; ++i)
- XMEMCPY(ClientCache + i, clRow++, sizeof(ClientRow));
-#endif
-
- wc_UnLockMutex(&session_mutex);
-
- WOLFSSL_LEAVE("wolfSSL_memrestore_session_cache", WOLFSSL_SUCCESS);
-
- return WOLFSSL_SUCCESS;
-}
-
-#if !defined(NO_FILESYSTEM)
-
-/* Persist session cache to file */
-/* doesn't use memsave because of additional memory use */
-int wolfSSL_save_session_cache(const char *fname)
-{
- XFILE file;
- int ret;
- int rc = WOLFSSL_SUCCESS;
- int i;
- cache_header_t cache_header;
-
- WOLFSSL_ENTER("wolfSSL_save_session_cache");
-
- file = XFOPEN(fname, "w+b");
- if (file == XBADFILE) {
- WOLFSSL_MSG("Couldn't open session cache save file");
- return WOLFSSL_BAD_FILE;
- }
- cache_header.version = WOLFSSL_CACHE_VERSION;
- cache_header.rows = SESSION_ROWS;
- cache_header.columns = SESSIONS_PER_ROW;
- cache_header.sessionSz = (int)sizeof(WOLFSSL_SESSION);
-
- /* cache header */
- ret = (int)XFWRITE(&cache_header, sizeof cache_header, 1, file);
- if (ret != 1) {
- WOLFSSL_MSG("Session cache header file write failed");
- XFCLOSE(file);
- return FWRITE_ERROR;
- }
-
- if (wc_LockMutex(&session_mutex) != 0) {
- WOLFSSL_MSG("Session cache mutex lock failed");
- XFCLOSE(file);
- return BAD_MUTEX_E;
- }
-
- /* session cache */
- for (i = 0; i < cache_header.rows; ++i) {
- ret = (int)XFWRITE(SessionCache + i, sizeof(SessionRow), 1, file);
- if (ret != 1) {
- WOLFSSL_MSG("Session cache member file write failed");
- rc = FWRITE_ERROR;
- break;
- }
- }
-
-#ifndef NO_CLIENT_CACHE
- /* client cache */
- for (i = 0; i < cache_header.rows; ++i) {
- ret = (int)XFWRITE(ClientCache + i, sizeof(ClientRow), 1, file);
- if (ret != 1) {
- WOLFSSL_MSG("Client cache member file write failed");
- rc = FWRITE_ERROR;
- break;
- }
- }
-#endif /* NO_CLIENT_CACHE */
-
- wc_UnLockMutex(&session_mutex);
-
- XFCLOSE(file);
- WOLFSSL_LEAVE("wolfSSL_save_session_cache", rc);
-
- return rc;
-}
-
-
-/* Restore the persistent session cache from file */
-/* doesn't use memstore because of additional memory use */
-int wolfSSL_restore_session_cache(const char *fname)
-{
- XFILE file;
- int rc = WOLFSSL_SUCCESS;
- int ret;
- int i;
- cache_header_t cache_header;
-
- WOLFSSL_ENTER("wolfSSL_restore_session_cache");
-
- file = XFOPEN(fname, "rb");
- if (file == XBADFILE) {
- WOLFSSL_MSG("Couldn't open session cache save file");
- return WOLFSSL_BAD_FILE;
- }
- /* cache header */
- ret = (int)XFREAD(&cache_header, sizeof cache_header, 1, file);
- if (ret != 1) {
- WOLFSSL_MSG("Session cache header file read failed");
- XFCLOSE(file);
- return FREAD_ERROR;
- }
- if (cache_header.version != WOLFSSL_CACHE_VERSION ||
- cache_header.rows != SESSION_ROWS ||
- cache_header.columns != SESSIONS_PER_ROW ||
- cache_header.sessionSz != (int)sizeof(WOLFSSL_SESSION)) {
-
- WOLFSSL_MSG("Session cache header match failed");
- XFCLOSE(file);
- return CACHE_MATCH_ERROR;
- }
-
- if (wc_LockMutex(&session_mutex) != 0) {
- WOLFSSL_MSG("Session cache mutex lock failed");
- XFCLOSE(file);
- return BAD_MUTEX_E;
- }
-
- /* session cache */
- for (i = 0; i < cache_header.rows; ++i) {
- ret = (int)XFREAD(SessionCache + i, sizeof(SessionRow), 1, file);
- if (ret != 1) {
- WOLFSSL_MSG("Session cache member file read failed");
- XMEMSET(SessionCache, 0, sizeof SessionCache);
- rc = FREAD_ERROR;
- break;
- }
- }
-
-#ifndef NO_CLIENT_CACHE
- /* client cache */
- for (i = 0; i < cache_header.rows; ++i) {
- ret = (int)XFREAD(ClientCache + i, sizeof(ClientRow), 1, file);
- if (ret != 1) {
- WOLFSSL_MSG("Client cache member file read failed");
- XMEMSET(ClientCache, 0, sizeof ClientCache);
- rc = FREAD_ERROR;
- break;
- }
- }
-
-#endif /* NO_CLIENT_CACHE */
-
- wc_UnLockMutex(&session_mutex);
-
- XFCLOSE(file);
- WOLFSSL_LEAVE("wolfSSL_restore_session_cache", rc);
-
- return rc;
-}
-
-#endif /* !NO_FILESYSTEM */
-#endif /* PERSIST_SESSION_CACHE */
-#endif /* NO_SESSION_CACHE */
-
-
-void wolfSSL_load_error_strings(void) /* compatibility only */
-{}
-
-
-int wolfSSL_library_init(void)
-{
- WOLFSSL_ENTER("SSL_library_init");
- if (wolfSSL_Init() == WOLFSSL_SUCCESS)
- return WOLFSSL_SUCCESS;
- else
- return WOLFSSL_FATAL_ERROR;
-}
-
-
-#ifdef HAVE_SECRET_CALLBACK
-
-int wolfSSL_set_session_secret_cb(WOLFSSL* ssl, SessionSecretCb cb, void* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_set_session_secret_cb");
- if (ssl == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- ssl->sessionSecretCb = cb;
- ssl->sessionSecretCtx = ctx;
- /* If using a pre-set key, assume session resumption. */
- ssl->session.sessionIDSz = 0;
- ssl->options.resuming = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif
-
-
-#ifndef NO_SESSION_CACHE
-
-/* on by default if built in but allow user to turn off */
-WOLFSSL_ABI
-long wolfSSL_CTX_set_session_cache_mode(WOLFSSL_CTX* ctx, long mode)
-{
- WOLFSSL_ENTER("SSL_CTX_set_session_cache_mode");
- if (mode == WOLFSSL_SESS_CACHE_OFF)
- ctx->sessionCacheOff = 1;
-
- if ((mode & WOLFSSL_SESS_CACHE_NO_AUTO_CLEAR) != 0)
- ctx->sessionCacheFlushOff = 1;
-
-#ifdef HAVE_EXT_CACHE
- if ((mode & WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE) != 0)
- ctx->internalCacheOff = 1;
-#endif
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* NO_SESSION_CACHE */
-
-
-#if !defined(NO_CERTS)
-#if defined(PERSIST_CERT_CACHE)
-
-
-#define WOLFSSL_CACHE_CERT_VERSION 1
-
-typedef struct {
- int version; /* cache cert layout version id */
- int rows; /* hash table rows, CA_TABLE_SIZE */
- int columns[CA_TABLE_SIZE]; /* columns per row on list */
- int signerSz; /* sizeof Signer object */
-} CertCacheHeader;
-
-/* current cert persistence layout is:
-
- 1) CertCacheHeader
- 2) caTable
-
- update WOLFSSL_CERT_CACHE_VERSION if change layout for the following
- PERSIST_CERT_CACHE functions
-*/
-
-
-/* Return memory needed to persist this signer, have lock */
-static WC_INLINE int GetSignerMemory(Signer* signer)
-{
- int sz = sizeof(signer->pubKeySize) + sizeof(signer->keyOID)
- + sizeof(signer->nameLen) + sizeof(signer->subjectNameHash);
-
-#if !defined(NO_SKID)
- sz += (int)sizeof(signer->subjectKeyIdHash);
-#endif
-
- /* add dynamic bytes needed */
- sz += signer->pubKeySize;
- sz += signer->nameLen;
-
- return sz;
-}
-
-
-/* Return memory needed to persist this row, have lock */
-static WC_INLINE int GetCertCacheRowMemory(Signer* row)
-{
- int sz = 0;
-
- while (row) {
- sz += GetSignerMemory(row);
- row = row->next;
- }
-
- return sz;
-}
-
-
-/* get the size of persist cert cache, have lock */
-static WC_INLINE int GetCertCacheMemSize(WOLFSSL_CERT_MANAGER* cm)
-{
- int sz;
- int i;
-
- sz = sizeof(CertCacheHeader);
-
- for (i = 0; i < CA_TABLE_SIZE; i++)
- sz += GetCertCacheRowMemory(cm->caTable[i]);
-
- return sz;
-}
-
-
-/* Store cert cache header columns with number of items per list, have lock */
-static WC_INLINE void SetCertHeaderColumns(WOLFSSL_CERT_MANAGER* cm, int* columns)
-{
- int i;
- Signer* row;
-
- for (i = 0; i < CA_TABLE_SIZE; i++) {
- int count = 0;
- row = cm->caTable[i];
-
- while (row) {
- ++count;
- row = row->next;
- }
- columns[i] = count;
- }
-}
-
-
-/* Restore whole cert row from memory, have lock, return bytes consumed,
- < 0 on error, have lock */
-static WC_INLINE int RestoreCertRow(WOLFSSL_CERT_MANAGER* cm, byte* current,
- int row, int listSz, const byte* end)
-{
- int idx = 0;
-
- if (listSz < 0) {
- WOLFSSL_MSG("Row header corrupted, negative value");
- return PARSE_ERROR;
- }
-
- while (listSz) {
- Signer* signer;
- byte* publicKey;
- byte* start = current + idx; /* for end checks on this signer */
- int minSz = sizeof(signer->pubKeySize) + sizeof(signer->keyOID) +
- sizeof(signer->nameLen) + sizeof(signer->subjectNameHash);
- #ifndef NO_SKID
- minSz += (int)sizeof(signer->subjectKeyIdHash);
- #endif
-
- if (start + minSz > end) {
- WOLFSSL_MSG("Would overread restore buffer");
- return BUFFER_E;
- }
- signer = MakeSigner(cm->heap);
- if (signer == NULL)
- return MEMORY_E;
-
- /* pubKeySize */
- XMEMCPY(&signer->pubKeySize, current + idx, sizeof(signer->pubKeySize));
- idx += (int)sizeof(signer->pubKeySize);
-
- /* keyOID */
- XMEMCPY(&signer->keyOID, current + idx, sizeof(signer->keyOID));
- idx += (int)sizeof(signer->keyOID);
-
- /* pulicKey */
- if (start + minSz + signer->pubKeySize > end) {
- WOLFSSL_MSG("Would overread restore buffer");
- FreeSigner(signer, cm->heap);
- return BUFFER_E;
- }
- publicKey = (byte*)XMALLOC(signer->pubKeySize, cm->heap,
- DYNAMIC_TYPE_KEY);
- if (publicKey == NULL) {
- FreeSigner(signer, cm->heap);
- return MEMORY_E;
- }
-
- XMEMCPY(publicKey, current + idx, signer->pubKeySize);
- signer->publicKey = publicKey;
- idx += signer->pubKeySize;
-
- /* nameLen */
- XMEMCPY(&signer->nameLen, current + idx, sizeof(signer->nameLen));
- idx += (int)sizeof(signer->nameLen);
-
- /* name */
- if (start + minSz + signer->pubKeySize + signer->nameLen > end) {
- WOLFSSL_MSG("Would overread restore buffer");
- FreeSigner(signer, cm->heap);
- return BUFFER_E;
- }
- signer->name = (char*)XMALLOC(signer->nameLen, cm->heap,
- DYNAMIC_TYPE_SUBJECT_CN);
- if (signer->name == NULL) {
- FreeSigner(signer, cm->heap);
- return MEMORY_E;
- }
-
- XMEMCPY(signer->name, current + idx, signer->nameLen);
- idx += signer->nameLen;
-
- /* subjectNameHash */
- XMEMCPY(signer->subjectNameHash, current + idx, SIGNER_DIGEST_SIZE);
- idx += SIGNER_DIGEST_SIZE;
-
- #ifndef NO_SKID
- /* subjectKeyIdHash */
- XMEMCPY(signer->subjectKeyIdHash, current + idx,SIGNER_DIGEST_SIZE);
- idx += SIGNER_DIGEST_SIZE;
- #endif
-
- signer->next = cm->caTable[row];
- cm->caTable[row] = signer;
-
- --listSz;
- }
-
- return idx;
-}
-
-
-/* Store whole cert row into memory, have lock, return bytes added */
-static WC_INLINE int StoreCertRow(WOLFSSL_CERT_MANAGER* cm, byte* current, int row)
-{
- int added = 0;
- Signer* list = cm->caTable[row];
-
- while (list) {
- XMEMCPY(current + added, &list->pubKeySize, sizeof(list->pubKeySize));
- added += (int)sizeof(list->pubKeySize);
-
- XMEMCPY(current + added, &list->keyOID, sizeof(list->keyOID));
- added += (int)sizeof(list->keyOID);
-
- XMEMCPY(current + added, list->publicKey, list->pubKeySize);
- added += list->pubKeySize;
-
- XMEMCPY(current + added, &list->nameLen, sizeof(list->nameLen));
- added += (int)sizeof(list->nameLen);
-
- XMEMCPY(current + added, list->name, list->nameLen);
- added += list->nameLen;
-
- XMEMCPY(current + added, list->subjectNameHash, SIGNER_DIGEST_SIZE);
- added += SIGNER_DIGEST_SIZE;
-
- #ifndef NO_SKID
- XMEMCPY(current + added, list->subjectKeyIdHash,SIGNER_DIGEST_SIZE);
- added += SIGNER_DIGEST_SIZE;
- #endif
-
- list = list->next;
- }
-
- return added;
-}
-
-
-/* Persist cert cache to memory, have lock */
-static WC_INLINE int DoMemSaveCertCache(WOLFSSL_CERT_MANAGER* cm,
- void* mem, int sz)
-{
- int realSz;
- int ret = WOLFSSL_SUCCESS;
- int i;
-
- WOLFSSL_ENTER("DoMemSaveCertCache");
-
- realSz = GetCertCacheMemSize(cm);
- if (realSz > sz) {
- WOLFSSL_MSG("Mem output buffer too small");
- ret = BUFFER_E;
- }
- else {
- byte* current;
- CertCacheHeader hdr;
-
- hdr.version = WOLFSSL_CACHE_CERT_VERSION;
- hdr.rows = CA_TABLE_SIZE;
- SetCertHeaderColumns(cm, hdr.columns);
- hdr.signerSz = (int)sizeof(Signer);
-
- XMEMCPY(mem, &hdr, sizeof(CertCacheHeader));
- current = (byte*)mem + sizeof(CertCacheHeader);
-
- for (i = 0; i < CA_TABLE_SIZE; ++i)
- current += StoreCertRow(cm, current, i);
- }
-
- return ret;
-}
-
-
-#if !defined(NO_FILESYSTEM)
-
-/* Persist cert cache to file */
-int CM_SaveCertCache(WOLFSSL_CERT_MANAGER* cm, const char* fname)
-{
- XFILE file;
- int rc = WOLFSSL_SUCCESS;
- int memSz;
- byte* mem;
-
- WOLFSSL_ENTER("CM_SaveCertCache");
-
- file = XFOPEN(fname, "w+b");
- if (file == XBADFILE) {
- WOLFSSL_MSG("Couldn't open cert cache save file");
- return WOLFSSL_BAD_FILE;
- }
-
- if (wc_LockMutex(&cm->caLock) != 0) {
- WOLFSSL_MSG("wc_LockMutex on caLock failed");
- XFCLOSE(file);
- return BAD_MUTEX_E;
- }
-
- memSz = GetCertCacheMemSize(cm);
- mem = (byte*)XMALLOC(memSz, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (mem == NULL) {
- WOLFSSL_MSG("Alloc for tmp buffer failed");
- rc = MEMORY_E;
- } else {
- rc = DoMemSaveCertCache(cm, mem, memSz);
- if (rc == WOLFSSL_SUCCESS) {
- int ret = (int)XFWRITE(mem, memSz, 1, file);
- if (ret != 1) {
- WOLFSSL_MSG("Cert cache file write failed");
- rc = FWRITE_ERROR;
- }
- }
- XFREE(mem, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- wc_UnLockMutex(&cm->caLock);
- XFCLOSE(file);
-
- return rc;
-}
-
-
-/* Restore cert cache from file */
-int CM_RestoreCertCache(WOLFSSL_CERT_MANAGER* cm, const char* fname)
-{
- XFILE file;
- int rc = WOLFSSL_SUCCESS;
- int ret;
- int memSz;
- byte* mem;
-
- WOLFSSL_ENTER("CM_RestoreCertCache");
-
- file = XFOPEN(fname, "rb");
- if (file == XBADFILE) {
- WOLFSSL_MSG("Couldn't open cert cache save file");
- return WOLFSSL_BAD_FILE;
- }
-
- if(XFSEEK(file, 0, XSEEK_END) != 0) {
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
- memSz = (int)XFTELL(file);
- XREWIND(file);
-
- if (memSz > MAX_WOLFSSL_FILE_SIZE || memSz <= 0) {
- WOLFSSL_MSG("CM_RestoreCertCache file size error");
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
-
- mem = (byte*)XMALLOC(memSz, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (mem == NULL) {
- WOLFSSL_MSG("Alloc for tmp buffer failed");
- XFCLOSE(file);
- return MEMORY_E;
- }
-
- ret = (int)XFREAD(mem, memSz, 1, file);
- if (ret != 1) {
- WOLFSSL_MSG("Cert file read error");
- rc = FREAD_ERROR;
- } else {
- rc = CM_MemRestoreCertCache(cm, mem, memSz);
- if (rc != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Mem restore cert cache failed");
- }
- }
-
- XFREE(mem, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFCLOSE(file);
-
- return rc;
-}
-
-#endif /* NO_FILESYSTEM */
-
-
-/* Persist cert cache to memory */
-int CM_MemSaveCertCache(WOLFSSL_CERT_MANAGER* cm, void* mem, int sz, int* used)
-{
- int ret = WOLFSSL_SUCCESS;
-
- WOLFSSL_ENTER("CM_MemSaveCertCache");
-
- if (wc_LockMutex(&cm->caLock) != 0) {
- WOLFSSL_MSG("wc_LockMutex on caLock failed");
- return BAD_MUTEX_E;
- }
-
- ret = DoMemSaveCertCache(cm, mem, sz);
- if (ret == WOLFSSL_SUCCESS)
- *used = GetCertCacheMemSize(cm);
-
- wc_UnLockMutex(&cm->caLock);
-
- return ret;
-}
-
-
-/* Restore cert cache from memory */
-int CM_MemRestoreCertCache(WOLFSSL_CERT_MANAGER* cm, const void* mem, int sz)
-{
- int ret = WOLFSSL_SUCCESS;
- int i;
- CertCacheHeader* hdr = (CertCacheHeader*)mem;
- byte* current = (byte*)mem + sizeof(CertCacheHeader);
- byte* end = (byte*)mem + sz; /* don't go over */
-
- WOLFSSL_ENTER("CM_MemRestoreCertCache");
-
- if (current > end) {
- WOLFSSL_MSG("Cert Cache Memory buffer too small");
- return BUFFER_E;
- }
-
- if (hdr->version != WOLFSSL_CACHE_CERT_VERSION ||
- hdr->rows != CA_TABLE_SIZE ||
- hdr->signerSz != (int)sizeof(Signer)) {
-
- WOLFSSL_MSG("Cert Cache Memory header mismatch");
- return CACHE_MATCH_ERROR;
- }
-
- if (wc_LockMutex(&cm->caLock) != 0) {
- WOLFSSL_MSG("wc_LockMutex on caLock failed");
- return BAD_MUTEX_E;
- }
-
- FreeSignerTable(cm->caTable, CA_TABLE_SIZE, cm->heap);
-
- for (i = 0; i < CA_TABLE_SIZE; ++i) {
- int added = RestoreCertRow(cm, current, i, hdr->columns[i], end);
- if (added < 0) {
- WOLFSSL_MSG("RestoreCertRow error");
- ret = added;
- break;
- }
- current += added;
- }
-
- wc_UnLockMutex(&cm->caLock);
-
- return ret;
-}
-
-
-/* get how big the the cert cache save buffer needs to be */
-int CM_GetCertCacheMemSize(WOLFSSL_CERT_MANAGER* cm)
-{
- int sz;
-
- WOLFSSL_ENTER("CM_GetCertCacheMemSize");
-
- if (wc_LockMutex(&cm->caLock) != 0) {
- WOLFSSL_MSG("wc_LockMutex on caLock failed");
- return BAD_MUTEX_E;
- }
-
- sz = GetCertCacheMemSize(cm);
-
- wc_UnLockMutex(&cm->caLock);
-
- return sz;
-}
-
-#endif /* PERSIST_CERT_CACHE */
-#endif /* NO_CERTS */
-
-#ifdef OPENSSL_EXTRA
-
-
-/* removes all cipher suites from the list that contain "toRemove"
- * returns the new list size on success
- */
-static int wolfSSL_remove_ciphers(char* list, int sz, const char* toRemove)
-{
- int idx = 0;
- char* next = (char*)list;
- int totalSz = sz;
-
- if (list == NULL) {
- return 0;
- }
-
- do {
- char* current = next;
- char name[MAX_SUITE_NAME + 1];
- word32 length;
-
- next = XSTRSTR(next, ":");
- length = min(sizeof(name), !next ? (word32)XSTRLEN(current) /* last */
- : (word32)(next - current));
-
- XSTRNCPY(name, current, length);
- name[(length == sizeof(name)) ? length - 1 : length] = 0;
-
- if (XSTRSTR(name, toRemove)) {
- XMEMMOVE(list + idx, list + idx + length, totalSz - (idx + length));
- totalSz -= length;
- list[totalSz] = '\0';
- next = current;
- }
- else {
- idx += length;
- }
- } while (next++); /* ++ needed to skip ':' */
-
- return totalSz;
-}
-
-/* parse some bulk lists like !eNULL / !aNULL
- *
- * returns WOLFSSL_SUCCESS on success and sets the cipher suite list
- */
-static int wolfSSL_parse_cipher_list(WOLFSSL_CTX* ctx, Suites* suites,
- const char* list)
-{
- int ret = 0;
- const int suiteSz = GetCipherNamesSize();
- char* next = (char*)list;
- const CipherSuiteInfo* names = GetCipherNames();
- char* localList = NULL;
- int sz = 0;
-
- if (suites == NULL || list == NULL) {
- WOLFSSL_MSG("NULL argument");
- return WOLFSSL_FAILURE;
- }
-
- /* does list contain eNULL or aNULL? */
- if (XSTRSTR(list, "aNULL") || XSTRSTR(list, "eNULL")) {
- do {
- char* current = next;
- char name[MAX_SUITE_NAME + 1];
- int i;
- word32 length;
-
- next = XSTRSTR(next, ":");
- length = min(sizeof(name), !next ? (word32)XSTRLEN(current) /*last*/
- : (word32)(next - current));
-
- XSTRNCPY(name, current, length);
- name[(length == sizeof(name)) ? length - 1 : length] = 0;
-
- /* check for "not" case */
- if (name[0] == '!' && suiteSz > 0) {
- /* populate list with all suites if not already created */
- if (localList == NULL) {
- for (i = 0; i < suiteSz; i++) {
- sz += (int)XSTRLEN(names[i].name) + 2;
- }
- localList = (char*)XMALLOC(sz, ctx->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (localList == NULL) {
- return WOLFSSL_FAILURE;
- }
- wolfSSL_get_ciphers(localList, sz);
- sz = (int)XSTRLEN(localList);
- }
-
- if (XSTRSTR(name, "eNULL")) {
- wolfSSL_remove_ciphers(localList, sz, "-NULL");
- }
- }
- }
- while (next++); /* ++ needed to skip ':' */
-
- ret = SetCipherList(ctx, suites, localList);
- XFREE(localList, ctx->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return (ret)? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
- }
- else {
- return (SetCipherList(ctx, suites, list)) ? WOLFSSL_SUCCESS :
- WOLFSSL_FAILURE;
- }
-}
-
-#endif
-
-
-int wolfSSL_CTX_set_cipher_list(WOLFSSL_CTX* ctx, const char* list)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_set_cipher_list");
-
- if (ctx == NULL)
- return WOLFSSL_FAILURE;
-
- /* alloc/init on demand only */
- if (ctx->suites == NULL) {
- ctx->suites = (Suites*)XMALLOC(sizeof(Suites), ctx->heap,
- DYNAMIC_TYPE_SUITES);
- if (ctx->suites == NULL) {
- WOLFSSL_MSG("Memory alloc for Suites failed");
- return WOLFSSL_FAILURE;
- }
- XMEMSET(ctx->suites, 0, sizeof(Suites));
- }
-
-#ifdef OPENSSL_EXTRA
- return wolfSSL_parse_cipher_list(ctx, ctx->suites, list);
-#else
- return (SetCipherList(ctx, ctx->suites, list)) ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
-#endif
-}
-
-
-int wolfSSL_set_cipher_list(WOLFSSL* ssl, const char* list)
-{
- WOLFSSL_ENTER("wolfSSL_set_cipher_list");
-#ifdef SINGLE_THREADED
- if (ssl->ctx->suites == ssl->suites) {
- ssl->suites = (Suites*)XMALLOC(sizeof(Suites), ssl->heap,
- DYNAMIC_TYPE_SUITES);
- if (ssl->suites == NULL) {
- WOLFSSL_MSG("Suites Memory error");
- return MEMORY_E;
- }
- ssl->options.ownSuites = 1;
- }
-#endif
-
-#ifdef OPENSSL_EXTRA
- return wolfSSL_parse_cipher_list(ssl->ctx, ssl->suites, list);
-#else
- return (SetCipherList(ssl->ctx, ssl->suites, list)) ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
-#endif
-}
-
-
-int wolfSSL_dtls_get_using_nonblock(WOLFSSL* ssl)
-{
- int useNb = 0;
-
- if (ssl == NULL)
- return WOLFSSL_FAILURE;
-
- WOLFSSL_ENTER("wolfSSL_dtls_get_using_nonblock");
- if (ssl->options.dtls) {
-#ifdef WOLFSSL_DTLS
- useNb = ssl->options.dtlsUseNonblock;
-#endif
- }
- else {
- WOLFSSL_MSG("wolfSSL_dtls_get_using_nonblock() is "
- "DEPRECATED for non-DTLS use.");
- }
- return useNb;
-}
-
-
-#ifndef WOLFSSL_LEANPSK
-
-void wolfSSL_dtls_set_using_nonblock(WOLFSSL* ssl, int nonblock)
-{
- (void)nonblock;
-
- WOLFSSL_ENTER("wolfSSL_dtls_set_using_nonblock");
-
- if (ssl == NULL)
- return;
-
- if (ssl->options.dtls) {
-#ifdef WOLFSSL_DTLS
- ssl->options.dtlsUseNonblock = (nonblock != 0);
-#endif
- }
- else {
- WOLFSSL_MSG("wolfSSL_dtls_set_using_nonblock() is "
- "DEPRECATED for non-DTLS use.");
- }
-}
-
-
-#ifdef WOLFSSL_DTLS
-
-int wolfSSL_dtls_get_current_timeout(WOLFSSL* ssl)
-{
- int timeout = 0;
- if (ssl)
- timeout = ssl->dtls_timeout;
-
- WOLFSSL_LEAVE("wolfSSL_dtls_get_current_timeout()", timeout);
- return timeout;
-}
-
-int wolfSSL_DTLSv1_get_timeout(WOLFSSL* ssl, WOLFSSL_TIMEVAL* timeleft)
-{
- if (ssl && timeleft) {
- XMEMSET(timeleft, 0, sizeof(WOLFSSL_TIMEVAL));
- timeleft->tv_sec = ssl->dtls_timeout;
- }
- return 0;
-}
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_DTLSv1_handle_timeout(WOLFSSL* ssl)
-{
- WOLFSSL_STUB("SSL_DTLSv1_handle_timeout");
- (void)ssl;
- return 0;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_DTLSv1_set_initial_timeout_duration(WOLFSSL* ssl, word32 duration_ms)
-{
- WOLFSSL_STUB("SSL_DTLSv1_set_initial_timeout_duration");
- (void)ssl;
- (void)duration_ms;
-}
-#endif
-
-/* user may need to alter init dtls recv timeout, WOLFSSL_SUCCESS on ok */
-int wolfSSL_dtls_set_timeout_init(WOLFSSL* ssl, int timeout)
-{
- if (ssl == NULL || timeout < 0)
- return BAD_FUNC_ARG;
-
- if (timeout > ssl->dtls_timeout_max) {
- WOLFSSL_MSG("Can't set dtls timeout init greater than dtls timeout max");
- return BAD_FUNC_ARG;
- }
-
- ssl->dtls_timeout_init = timeout;
- ssl->dtls_timeout = timeout;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* user may need to alter max dtls recv timeout, WOLFSSL_SUCCESS on ok */
-int wolfSSL_dtls_set_timeout_max(WOLFSSL* ssl, int timeout)
-{
- if (ssl == NULL || timeout < 0)
- return BAD_FUNC_ARG;
-
- if (timeout < ssl->dtls_timeout_init) {
- WOLFSSL_MSG("Can't set dtls timeout max less than dtls timeout init");
- return BAD_FUNC_ARG;
- }
-
- ssl->dtls_timeout_max = timeout;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_dtls_got_timeout(WOLFSSL* ssl)
-{
- int result = WOLFSSL_SUCCESS;
- WOLFSSL_ENTER("wolfSSL_dtls_got_timeout()");
-
- if (ssl == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- if (!ssl->options.handShakeDone &&
- (DtlsMsgPoolTimeout(ssl) < 0 || DtlsMsgPoolSend(ssl, 0) < 0)) {
-
- result = WOLFSSL_FATAL_ERROR;
- }
-
- WOLFSSL_LEAVE("wolfSSL_dtls_got_timeout()", result);
- return result;
-}
-
-
-/* retransmit all the saves messages, WOLFSSL_SUCCESS on ok */
-int wolfSSL_dtls_retransmit(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_dtls_retransmit()");
-
- if (ssl == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- if (!ssl->options.handShakeDone) {
- int result = DtlsMsgPoolSend(ssl, 0);
- if (result < 0) {
- ssl->error = result;
- WOLFSSL_ERROR(result);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- return 0;
-}
-
-#endif /* DTLS */
-#endif /* LEANPSK */
-
-
-#if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
-
-/* Not an SSL function, return 0 for success, error code otherwise */
-/* Prereq: ssl's RNG needs to be initialized. */
-int wolfSSL_DTLS_SetCookieSecret(WOLFSSL* ssl,
- const byte* secret, word32 secretSz)
-{
- int ret = 0;
-
- WOLFSSL_ENTER("wolfSSL_DTLS_SetCookieSecret");
-
- if (ssl == NULL) {
- WOLFSSL_MSG("need a SSL object");
- return BAD_FUNC_ARG;
- }
-
- if (secret != NULL && secretSz == 0) {
- WOLFSSL_MSG("can't have a new secret without a size");
- return BAD_FUNC_ARG;
- }
-
- /* If secretSz is 0, use the default size. */
- if (secretSz == 0)
- secretSz = COOKIE_SECRET_SZ;
-
- if (secretSz != ssl->buffers.dtlsCookieSecret.length) {
- byte* newSecret;
-
- if (ssl->buffers.dtlsCookieSecret.buffer != NULL) {
- ForceZero(ssl->buffers.dtlsCookieSecret.buffer,
- ssl->buffers.dtlsCookieSecret.length);
- XFREE(ssl->buffers.dtlsCookieSecret.buffer,
- ssl->heap, DYNAMIC_TYPE_NONE);
- }
-
- newSecret = (byte*)XMALLOC(secretSz, ssl->heap,DYNAMIC_TYPE_COOKIE_PWD);
- if (newSecret == NULL) {
- ssl->buffers.dtlsCookieSecret.buffer = NULL;
- ssl->buffers.dtlsCookieSecret.length = 0;
- WOLFSSL_MSG("couldn't allocate new cookie secret");
- return MEMORY_ERROR;
- }
- ssl->buffers.dtlsCookieSecret.buffer = newSecret;
- ssl->buffers.dtlsCookieSecret.length = secretSz;
- }
-
- /* If the supplied secret is NULL, randomly generate a new secret. */
- if (secret == NULL) {
- ret = wc_RNG_GenerateBlock(ssl->rng,
- ssl->buffers.dtlsCookieSecret.buffer, secretSz);
- }
- else
- XMEMCPY(ssl->buffers.dtlsCookieSecret.buffer, secret, secretSz);
-
- WOLFSSL_LEAVE("wolfSSL_DTLS_SetCookieSecret", 0);
- return ret;
-}
-
-#endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */
-
-
-/* EITHER SIDE METHODS */
-#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- WOLFSSL_METHOD* wolfSSLv23_method(void)
- {
- return wolfSSLv23_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfSSLv23_method_ex(void* heap)
- {
- WOLFSSL_METHOD* m = NULL;
- WOLFSSL_ENTER("SSLv23_method");
- #if !defined(NO_WOLFSSL_CLIENT)
- m = wolfSSLv23_client_method_ex(heap);
- #elif !defined(NO_WOLFSSL_SERVER)
- m = wolfSSLv23_server_method_ex(heap);
- #endif
- if (m != NULL) {
- m->side = WOLFSSL_NEITHER_END;
- }
-
- return m;
- }
-
- #ifdef WOLFSSL_ALLOW_SSLV3
- WOLFSSL_METHOD* wolfSSLv3_method(void)
- {
- return wolfSSLv3_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfSSLv3_method_ex(void* heap)
- {
- WOLFSSL_METHOD* m = NULL;
- WOLFSSL_ENTER("SSLv3_method");
- #if !defined(NO_WOLFSSL_CLIENT)
- m = wolfSSLv3_client_method_ex(heap);
- #elif !defined(NO_WOLFSSL_SERVER)
- m = wolfSSLv3_server_method_ex(heap);
- #endif
- if (m != NULL) {
- m->side = WOLFSSL_NEITHER_END;
- }
-
- return m;
- }
- #endif
-#endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
-
-/* client only parts */
-#ifndef NO_WOLFSSL_CLIENT
-
- #ifdef OPENSSL_EXTRA
- WOLFSSL_METHOD* wolfSSLv2_client_method(void)
- {
- WOLFSSL_STUB("wolfSSLv2_client_method");
- return NULL;
- }
- #endif
-
- #ifdef WOLFSSL_ALLOW_SSLV3
- WOLFSSL_METHOD* wolfSSLv3_client_method(void)
- {
- return wolfSSLv3_client_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfSSLv3_client_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("SSLv3_client_method_ex");
- if (method)
- InitSSL_Method(method, MakeSSLv3());
- return method;
- }
- #endif /* WOLFSSL_ALLOW_SSLV3 */
-
-
- WOLFSSL_METHOD* wolfSSLv23_client_method(void)
- {
- return wolfSSLv23_client_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfSSLv23_client_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("SSLv23_client_method_ex");
- if (method) {
- #if !defined(NO_SHA256) || defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
- #if defined(WOLFSSL_TLS13)
- InitSSL_Method(method, MakeTLSv1_3());
- #elif !defined(WOLFSSL_NO_TLS12)
- InitSSL_Method(method, MakeTLSv1_2());
- #elif !defined(NO_OLD_TLS)
- InitSSL_Method(method, MakeTLSv1_1());
- #endif
- #else
- #ifndef NO_OLD_TLS
- InitSSL_Method(method, MakeTLSv1_1());
- #endif
- #endif
- #if !defined(NO_OLD_TLS) || defined(WOLFSSL_TLS13)
- method->downgrade = 1;
- #endif
- }
- return method;
- }
-
-
- #if defined(WOLFSSL_DTLS) || !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS) || \
- defined(WOLFSSL_ALLOW_SSLV3)
- /* If SCTP is not enabled returns the state of the dtls option.
- * If SCTP is enabled returns dtls && !sctp. */
- static WC_INLINE int IsDtlsNotSctpMode(WOLFSSL* ssl)
- {
- int result = ssl->options.dtls;
-
- if (result) {
- #ifdef WOLFSSL_SCTP
- result = !ssl->options.dtlsSctp;
- #endif
- }
-
- return result;
- }
- #endif /* WOLFSSL_DTLS || !WOLFSSL_NO_TLS12 || !NO_OLD_TLS */
-
-
- /* please see note at top of README if you get an error from connect */
- WOLFSSL_ABI
- int wolfSSL_connect(WOLFSSL* ssl)
- {
- #if !(defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS) && defined(WOLFSSL_TLS13))
- int neededState;
- #endif
-
- WOLFSSL_ENTER("SSL_connect()");
-
- #ifdef HAVE_ERRNO_H
- errno = 0;
- #endif
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (ssl->options.side == WOLFSSL_NEITHER_END) {
- ssl->error = InitSSL_Side(ssl, WOLFSSL_CLIENT_END);
- if (ssl->error != WOLFSSL_SUCCESS) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->error = 0; /* expected to be zero here */
- }
-
- #ifdef OPENSSL_EXTRA
- if (ssl->CBIS != NULL) {
- ssl->CBIS(ssl, SSL_ST_CONNECT, SSL_SUCCESS);
- ssl->cbmode = SSL_CB_WRITE;
- }
- #endif
- #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
-
- #if defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS) && defined(WOLFSSL_TLS13)
- return wolfSSL_connect_TLSv13(ssl);
- #else
- #ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3)
- return wolfSSL_connect_TLSv13(ssl);
- #endif
-
- if (ssl->options.side != WOLFSSL_CLIENT_END) {
- WOLFSSL_ERROR(ssl->error = SIDE_ERROR);
- return WOLFSSL_FATAL_ERROR;
- }
-
- #ifdef WOLFSSL_DTLS
- if (ssl->version.major == DTLS_MAJOR) {
- ssl->options.dtls = 1;
- ssl->options.tls = 1;
- ssl->options.tls1_1 = 1;
- }
- #endif
-
- if (ssl->buffers.outputBuffer.length > 0
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* do not send buffered or advance state if last error was an
- async pending operation */
- && ssl->error != WC_PENDING_E
- #endif
- ) {
- if ( (ssl->error = SendBuffered(ssl)) == 0) {
- /* fragOffset is non-zero when sending fragments. On the last
- * fragment, fragOffset is zero again, and the state can be
- * advanced. */
- if (ssl->fragOffset == 0) {
- ssl->options.connectState++;
- WOLFSSL_MSG("connect state: "
- "Advanced from last buffered fragment send");
- }
- else {
- WOLFSSL_MSG("connect state: "
- "Not advanced, more fragments to send");
- }
- }
- else {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- switch (ssl->options.connectState) {
-
- case CONNECT_BEGIN :
- /* always send client hello first */
- if ( (ssl->error = SendClientHello(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.connectState = CLIENT_HELLO_SENT;
- WOLFSSL_MSG("connect state: CLIENT_HELLO_SENT");
- FALL_THROUGH;
-
- case CLIENT_HELLO_SENT :
- neededState = ssl->options.resuming ? SERVER_FINISHED_COMPLETE :
- SERVER_HELLODONE_COMPLETE;
- #ifdef WOLFSSL_DTLS
- /* In DTLS, when resuming, we can go straight to FINISHED,
- * or do a cookie exchange and then skip to FINISHED, assume
- * we need the cookie exchange first. */
- if (IsDtlsNotSctpMode(ssl))
- neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
- #endif
- /* get response */
- while (ssl->options.serverState < neededState) {
- #ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3)
- return wolfSSL_connect_TLSv13(ssl);
- #endif
- if ( (ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- /* if resumption failed, reset needed state */
- else if (neededState == SERVER_FINISHED_COMPLETE)
- if (!ssl->options.resuming) {
- if (!IsDtlsNotSctpMode(ssl))
- neededState = SERVER_HELLODONE_COMPLETE;
- else
- neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
- }
- }
-
- ssl->options.connectState = HELLO_AGAIN;
- WOLFSSL_MSG("connect state: HELLO_AGAIN");
- FALL_THROUGH;
-
- case HELLO_AGAIN :
- if (ssl->options.certOnly)
- return WOLFSSL_SUCCESS;
-
- #ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3)
- return wolfSSL_connect_TLSv13(ssl);
- #endif
-
- #ifdef WOLFSSL_DTLS
- if (ssl->options.serverState ==
- SERVER_HELLOVERIFYREQUEST_COMPLETE) {
- if (IsDtlsNotSctpMode(ssl)) {
- /* re-init hashes, exclude first hello and verify request */
- if ((ssl->error = InitHandshakeHashes(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- if ( (ssl->error = SendClientHello(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
- }
- #endif
-
- ssl->options.connectState = HELLO_AGAIN_REPLY;
- WOLFSSL_MSG("connect state: HELLO_AGAIN_REPLY");
- FALL_THROUGH;
-
- case HELLO_AGAIN_REPLY :
- #ifdef WOLFSSL_DTLS
- if (IsDtlsNotSctpMode(ssl)) {
- neededState = ssl->options.resuming ?
- SERVER_FINISHED_COMPLETE : SERVER_HELLODONE_COMPLETE;
-
- /* get response */
- while (ssl->options.serverState < neededState) {
- if ( (ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- /* if resumption failed, reset needed state */
- if (neededState == SERVER_FINISHED_COMPLETE) {
- if (!ssl->options.resuming)
- neededState = SERVER_HELLODONE_COMPLETE;
- }
- }
- }
- #endif
-
- ssl->options.connectState = FIRST_REPLY_DONE;
- WOLFSSL_MSG("connect state: FIRST_REPLY_DONE");
- FALL_THROUGH;
-
- case FIRST_REPLY_DONE :
- #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CLIENT_AUTH)
- #ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3)
- return wolfSSL_connect_TLSv13(ssl);
- #endif
- if (ssl->options.sendVerify) {
- if ( (ssl->error = SendCertificate(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- WOLFSSL_MSG("sent: certificate");
- }
-
- #endif
- ssl->options.connectState = FIRST_REPLY_FIRST;
- WOLFSSL_MSG("connect state: FIRST_REPLY_FIRST");
- FALL_THROUGH;
-
- case FIRST_REPLY_FIRST :
- #ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3)
- return wolfSSL_connect_TLSv13(ssl);
- #endif
- if (!ssl->options.resuming) {
- if ( (ssl->error = SendClientKeyExchange(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- WOLFSSL_MSG("sent: client key exchange");
- }
-
- ssl->options.connectState = FIRST_REPLY_SECOND;
- WOLFSSL_MSG("connect state: FIRST_REPLY_SECOND");
- FALL_THROUGH;
-
- case FIRST_REPLY_SECOND :
- #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_CLIENT_AUTH)
- if (ssl->options.sendVerify) {
- if ( (ssl->error = SendCertificateVerify(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- WOLFSSL_MSG("sent: certificate verify");
- }
- #endif /* !NO_CERTS && !WOLFSSL_NO_CLIENT_AUTH */
- ssl->options.connectState = FIRST_REPLY_THIRD;
- WOLFSSL_MSG("connect state: FIRST_REPLY_THIRD");
- FALL_THROUGH;
-
- case FIRST_REPLY_THIRD :
- if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- WOLFSSL_MSG("sent: change cipher spec");
- ssl->options.connectState = FIRST_REPLY_FOURTH;
- WOLFSSL_MSG("connect state: FIRST_REPLY_FOURTH");
- FALL_THROUGH;
-
- case FIRST_REPLY_FOURTH :
- if ( (ssl->error = SendFinished(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- WOLFSSL_MSG("sent: finished");
- ssl->options.connectState = FINISHED_DONE;
- WOLFSSL_MSG("connect state: FINISHED_DONE");
- FALL_THROUGH;
-
- case FINISHED_DONE :
- /* get response */
- while (ssl->options.serverState < SERVER_FINISHED_COMPLETE)
- if ( (ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
-
- ssl->options.connectState = SECOND_REPLY_DONE;
- WOLFSSL_MSG("connect state: SECOND_REPLY_DONE");
- FALL_THROUGH;
-
- case SECOND_REPLY_DONE:
-#ifndef NO_HANDSHAKE_DONE_CB
- if (ssl->hsDoneCb) {
- int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx);
- if (cbret < 0) {
- ssl->error = cbret;
- WOLFSSL_MSG("HandShake Done Cb don't continue error");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif /* NO_HANDSHAKE_DONE_CB */
-
- if (!ssl->options.dtls) {
- if (!ssl->options.keepResources) {
- FreeHandshakeResources(ssl);
- }
- }
-#ifdef WOLFSSL_DTLS
- else {
- ssl->options.dtlsHsRetain = 1;
- }
-#endif /* WOLFSSL_DTLS */
-
- WOLFSSL_LEAVE("SSL_connect()", WOLFSSL_SUCCESS);
- return WOLFSSL_SUCCESS;
-
- default:
- WOLFSSL_MSG("Unknown connect state ERROR");
- return WOLFSSL_FATAL_ERROR; /* unknown connect state */
- }
- #endif /* !WOLFSSL_NO_TLS12 */
- }
-
-#endif /* NO_WOLFSSL_CLIENT */
-
-
-/* server only parts */
-#ifndef NO_WOLFSSL_SERVER
-
- #ifdef OPENSSL_EXTRA
- WOLFSSL_METHOD* wolfSSLv2_server_method(void)
- {
- WOLFSSL_STUB("wolfSSLv2_server_method");
- return 0;
- }
- #endif
-
- #ifdef WOLFSSL_ALLOW_SSLV3
- WOLFSSL_METHOD* wolfSSLv3_server_method(void)
- {
- return wolfSSLv3_server_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfSSLv3_server_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("SSLv3_server_method_ex");
- if (method) {
- InitSSL_Method(method, MakeSSLv3());
- method->side = WOLFSSL_SERVER_END;
- }
- return method;
- }
- #endif /* WOLFSSL_ALLOW_SSLV3 */
-
- WOLFSSL_METHOD* wolfSSLv23_server_method(void)
- {
- return wolfSSLv23_server_method_ex(NULL);
- }
-
- WOLFSSL_METHOD* wolfSSLv23_server_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("SSLv23_server_method_ex");
- if (method) {
- #if !defined(NO_SHA256) || defined(WOLFSSL_SHA384) || defined(WOLFSSL_SHA512)
- #ifdef WOLFSSL_TLS13
- InitSSL_Method(method, MakeTLSv1_3());
- #elif !defined(WOLFSSL_NO_TLS12)
- InitSSL_Method(method, MakeTLSv1_2());
- #elif !defined(NO_OLD_TLS)
- InitSSL_Method(method, MakeTLSv1_1());
- #endif
- #else
- #ifndef NO_OLD_TLS
- InitSSL_Method(method, MakeTLSv1_1());
- #else
- #error Must have SHA256, SHA384 or SHA512 enabled for TLS 1.2
- #endif
- #endif
- #if !defined(NO_OLD_TLS) || defined(WOLFSSL_TLS13)
- method->downgrade = 1;
- #endif
- method->side = WOLFSSL_SERVER_END;
- }
- return method;
- }
-
-
- int wolfSSL_accept(WOLFSSL* ssl)
- {
-#if !(defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS) && defined(WOLFSSL_TLS13))
- word16 havePSK = 0;
- word16 haveAnon = 0;
- word16 haveMcast = 0;
-#endif
-
- if (ssl == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (ssl->options.side == WOLFSSL_NEITHER_END) {
- WOLFSSL_MSG("Setting WOLFSSL_SSL to be server side");
- ssl->error = InitSSL_Side(ssl, WOLFSSL_SERVER_END);
- if (ssl->error != WOLFSSL_SUCCESS) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->error = 0; /* expected to be zero here */
- }
- #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
-
-#if defined(WOLFSSL_NO_TLS12) && defined(NO_OLD_TLS) && defined(WOLFSSL_TLS13)
- return wolfSSL_accept_TLSv13(ssl);
-#else
- #ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3)
- return wolfSSL_accept_TLSv13(ssl);
- #endif
- WOLFSSL_ENTER("SSL_accept()");
-
- #ifdef HAVE_ERRNO_H
- errno = 0;
- #endif
-
- #ifndef NO_PSK
- havePSK = ssl->options.havePSK;
- #endif
- (void)havePSK;
-
- #ifdef HAVE_ANON
- haveAnon = ssl->options.haveAnon;
- #endif
- (void)haveAnon;
-
- #ifdef WOLFSSL_MULTICAST
- haveMcast = ssl->options.haveMcast;
- #endif
- (void)haveMcast;
-
- if (ssl->options.side != WOLFSSL_SERVER_END) {
- WOLFSSL_ERROR(ssl->error = SIDE_ERROR);
- return WOLFSSL_FATAL_ERROR;
- }
-
- #ifndef NO_CERTS
- /* in case used set_accept_state after init */
- /* allow no private key if using PK callbacks and CB is set */
- if (!havePSK && !haveAnon && !haveMcast) {
- if (!ssl->buffers.certificate ||
- !ssl->buffers.certificate->buffer) {
-
- WOLFSSL_MSG("accept error: server cert required");
- WOLFSSL_ERROR(ssl->error = NO_PRIVATE_KEY);
- return WOLFSSL_FATAL_ERROR;
- }
-
- #ifdef HAVE_PK_CALLBACKS
- if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) {
- WOLFSSL_MSG("Using PK for server private key");
- }
- else
- #endif
- if (!ssl->buffers.key || !ssl->buffers.key->buffer) {
- WOLFSSL_MSG("accept error: server key required");
- WOLFSSL_ERROR(ssl->error = NO_PRIVATE_KEY);
- return WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
-
- #ifdef WOLFSSL_DTLS
- if (ssl->version.major == DTLS_MAJOR) {
- ssl->options.dtls = 1;
- ssl->options.tls = 1;
- ssl->options.tls1_1 = 1;
- }
- #endif
-
- if (ssl->buffers.outputBuffer.length > 0
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* do not send buffered or advance state if last error was an
- async pending operation */
- && ssl->error != WC_PENDING_E
- #endif
- ) {
- if ( (ssl->error = SendBuffered(ssl)) == 0) {
- /* fragOffset is non-zero when sending fragments. On the last
- * fragment, fragOffset is zero again, and the state can be
- * advanced. */
- if (ssl->fragOffset == 0) {
- ssl->options.acceptState++;
- WOLFSSL_MSG("accept state: "
- "Advanced from last buffered fragment send");
- }
- else {
- WOLFSSL_MSG("accept state: "
- "Not advanced, more fragments to send");
- }
- }
- else {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- switch (ssl->options.acceptState) {
-
- case ACCEPT_BEGIN :
-#ifdef HAVE_SECURE_RENEGOTIATION
- case ACCEPT_BEGIN_RENEG:
-#endif
- /* get response */
- while (ssl->options.clientState < CLIENT_HELLO_COMPLETE)
- if ( (ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
-#ifdef WOLFSSL_TLS13
- ssl->options.acceptState = ACCEPT_CLIENT_HELLO_DONE;
- WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE");
- FALL_THROUGH;
-
- case ACCEPT_CLIENT_HELLO_DONE :
- if (ssl->options.tls1_3) {
- return wolfSSL_accept_TLSv13(ssl);
- }
-#endif
- ssl->options.acceptState = ACCEPT_FIRST_REPLY_DONE;
- WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE");
- FALL_THROUGH;
-
- case ACCEPT_FIRST_REPLY_DONE :
- if ( (ssl->error = SendServerHello(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.acceptState = SERVER_HELLO_SENT;
- WOLFSSL_MSG("accept state SERVER_HELLO_SENT");
- FALL_THROUGH;
-
- case SERVER_HELLO_SENT :
- #ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3) {
- return wolfSSL_accept_TLSv13(ssl);
- }
- #endif
- #ifndef NO_CERTS
- if (!ssl->options.resuming)
- if ( (ssl->error = SendCertificate(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- #endif
- ssl->options.acceptState = CERT_SENT;
- WOLFSSL_MSG("accept state CERT_SENT");
- FALL_THROUGH;
-
- case CERT_SENT :
- #ifndef NO_CERTS
- if (!ssl->options.resuming)
- if ( (ssl->error = SendCertificateStatus(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- #endif
- ssl->options.acceptState = CERT_STATUS_SENT;
- WOLFSSL_MSG("accept state CERT_STATUS_SENT");
- FALL_THROUGH;
-
- case CERT_STATUS_SENT :
- #ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3) {
- return wolfSSL_accept_TLSv13(ssl);
- }
- #endif
- if (!ssl->options.resuming)
- if ( (ssl->error = SendServerKeyExchange(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.acceptState = KEY_EXCHANGE_SENT;
- WOLFSSL_MSG("accept state KEY_EXCHANGE_SENT");
- FALL_THROUGH;
-
- case KEY_EXCHANGE_SENT :
- #ifndef NO_CERTS
- if (!ssl->options.resuming) {
- if (ssl->options.verifyPeer) {
- if ( (ssl->error = SendCertificateRequest(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
- }
- #endif
- ssl->options.acceptState = CERT_REQ_SENT;
- WOLFSSL_MSG("accept state CERT_REQ_SENT");
- FALL_THROUGH;
-
- case CERT_REQ_SENT :
- if (!ssl->options.resuming)
- if ( (ssl->error = SendServerHelloDone(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.acceptState = SERVER_HELLO_DONE;
- WOLFSSL_MSG("accept state SERVER_HELLO_DONE");
- FALL_THROUGH;
-
- case SERVER_HELLO_DONE :
- if (!ssl->options.resuming) {
- while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
- if ( (ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
- ssl->options.acceptState = ACCEPT_SECOND_REPLY_DONE;
- WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE");
- FALL_THROUGH;
-
- case ACCEPT_SECOND_REPLY_DONE :
-#ifdef HAVE_SESSION_TICKET
- if (ssl->options.createTicket) {
- if ( (ssl->error = SendTicket(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif /* HAVE_SESSION_TICKET */
- ssl->options.acceptState = TICKET_SENT;
- WOLFSSL_MSG("accept state TICKET_SENT");
- FALL_THROUGH;
-
- case TICKET_SENT:
- if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.acceptState = CHANGE_CIPHER_SENT;
- WOLFSSL_MSG("accept state CHANGE_CIPHER_SENT");
- FALL_THROUGH;
-
- case CHANGE_CIPHER_SENT :
- if ( (ssl->error = SendFinished(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
-
- ssl->options.acceptState = ACCEPT_FINISHED_DONE;
- WOLFSSL_MSG("accept state ACCEPT_FINISHED_DONE");
- FALL_THROUGH;
-
- case ACCEPT_FINISHED_DONE :
- if (ssl->options.resuming)
- while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
- if ( (ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
-
- ssl->options.acceptState = ACCEPT_THIRD_REPLY_DONE;
- WOLFSSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE");
- FALL_THROUGH;
-
- case ACCEPT_THIRD_REPLY_DONE :
-#ifndef NO_HANDSHAKE_DONE_CB
- if (ssl->hsDoneCb) {
- int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx);
- if (cbret < 0) {
- ssl->error = cbret;
- WOLFSSL_MSG("HandShake Done Cb don't continue error");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif /* NO_HANDSHAKE_DONE_CB */
-
- if (!ssl->options.dtls) {
- if (!ssl->options.keepResources) {
- FreeHandshakeResources(ssl);
- }
- }
-#ifdef WOLFSSL_DTLS
- else {
- ssl->options.dtlsHsRetain = 1;
- }
-#endif /* WOLFSSL_DTLS */
-
-#ifdef WOLFSSL_SESSION_EXPORT
- if (ssl->dtls_export) {
- if ((ssl->error = wolfSSL_send_session(ssl)) != 0) {
- WOLFSSL_MSG("Export DTLS session error");
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif
-
- WOLFSSL_LEAVE("SSL_accept()", WOLFSSL_SUCCESS);
- return WOLFSSL_SUCCESS;
-
- default :
- WOLFSSL_MSG("Unknown accept state ERROR");
- return WOLFSSL_FATAL_ERROR;
- }
-#endif /* !WOLFSSL_NO_TLS12 */
- }
-
-#endif /* NO_WOLFSSL_SERVER */
-
-
-#ifndef NO_HANDSHAKE_DONE_CB
-
-int wolfSSL_SetHsDoneCb(WOLFSSL* ssl, HandShakeDoneCb cb, void* user_ctx)
-{
- WOLFSSL_ENTER("wolfSSL_SetHsDoneCb");
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ssl->hsDoneCb = cb;
- ssl->hsDoneCtx = user_ctx;
-
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* NO_HANDSHAKE_DONE_CB */
-
-WOLFSSL_ABI
-int wolfSSL_Cleanup(void)
-{
- int ret = WOLFSSL_SUCCESS;
- int release = 0;
-
- WOLFSSL_ENTER("wolfSSL_Cleanup");
-
- if (initRefCount == 0)
- return ret; /* possibly no init yet, but not failure either way */
-
- if (wc_LockMutex(&count_mutex) != 0) {
- WOLFSSL_MSG("Bad Lock Mutex count");
- return BAD_MUTEX_E;
- }
-
- release = initRefCount-- == 1;
- if (initRefCount < 0)
- initRefCount = 0;
-
- wc_UnLockMutex(&count_mutex);
-
- if (!release)
- return ret;
-
-#ifdef OPENSSL_EXTRA
- if (bn_one) {
- wolfSSL_BN_free(bn_one);
- bn_one = NULL;
- }
-#endif
-
-#ifndef NO_SESSION_CACHE
- if (wc_FreeMutex(&session_mutex) != 0)
- ret = BAD_MUTEX_E;
-#endif
- if (wc_FreeMutex(&count_mutex) != 0)
- ret = BAD_MUTEX_E;
-
-#ifdef OPENSSL_EXTRA
- wolfSSL_RAND_Cleanup();
-#endif
-
- if (wolfCrypt_Cleanup() != 0) {
- WOLFSSL_MSG("Error with wolfCrypt_Cleanup call");
- ret = WC_CLEANUP_E;
- }
-
- return ret;
-}
-
-
-#ifndef NO_SESSION_CACHE
-
-
-/* some session IDs aren't random after all, let's make them random */
-static WC_INLINE word32 HashSession(const byte* sessionID, word32 len, int* error)
-{
- byte digest[WC_MAX_DIGEST_SIZE];
-
-#ifndef NO_MD5
- *error = wc_Md5Hash(sessionID, len, digest);
-#elif !defined(NO_SHA)
- *error = wc_ShaHash(sessionID, len, digest);
-#elif !defined(NO_SHA256)
- *error = wc_Sha256Hash(sessionID, len, digest);
-#else
- #error "We need a digest to hash the session IDs"
-#endif
-
- return *error == 0 ? MakeWordFromHash(digest) : 0; /* 0 on failure */
-}
-
-
-WOLFSSL_ABI
-void wolfSSL_flush_sessions(WOLFSSL_CTX* ctx, long tm)
-{
- /* static table now, no flushing needed */
- (void)ctx;
- (void)tm;
-}
-
-
-/* set ssl session timeout in seconds */
-WOLFSSL_ABI
-int wolfSSL_set_timeout(WOLFSSL* ssl, unsigned int to)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- if (to == 0)
- to = WOLFSSL_SESSION_TIMEOUT;
- ssl->timeout = to;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* set ctx session timeout in seconds */
-WOLFSSL_ABI
-int wolfSSL_CTX_set_timeout(WOLFSSL_CTX* ctx, unsigned int to)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- if (to == 0)
- to = WOLFSSL_SESSION_TIMEOUT;
- ctx->timeout = to;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-#ifndef NO_CLIENT_CACHE
-
-/* Get Session from Client cache based on id/len, return NULL on failure */
-WOLFSSL_SESSION* GetSessionClient(WOLFSSL* ssl, const byte* id, int len)
-{
- WOLFSSL_SESSION* ret = NULL;
- word32 row;
- int idx;
- int count;
- int error = 0;
-
- WOLFSSL_ENTER("GetSessionClient");
-
- if (ssl->ctx->sessionCacheOff)
- return NULL;
-
- if (ssl->options.side == WOLFSSL_SERVER_END)
- return NULL;
-
- len = min(SERVER_ID_LEN, (word32)len);
-
-#ifdef HAVE_EXT_CACHE
- if (ssl->ctx->get_sess_cb != NULL) {
- int copy = 0;
- ret = ssl->ctx->get_sess_cb(ssl, (byte*)id, len, ©);
- if (ret != NULL)
- return ret;
- }
-
- if (ssl->ctx->internalCacheOff)
- return NULL;
-#endif
-
- row = HashSession(id, len, &error) % SESSION_ROWS;
- if (error != 0) {
- WOLFSSL_MSG("Hash session failed");
- return NULL;
- }
-
- if (wc_LockMutex(&session_mutex) != 0) {
- WOLFSSL_MSG("Lock session mutex failed");
- return NULL;
- }
-
- /* start from most recently used */
- count = min((word32)ClientCache[row].totalCount, SESSIONS_PER_ROW);
- idx = ClientCache[row].nextIdx - 1;
- if (idx < 0)
- idx = SESSIONS_PER_ROW - 1; /* if back to front, the previous was end */
-
- for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
- WOLFSSL_SESSION* current;
- ClientSession clSess;
-
- if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
- WOLFSSL_MSG("Bad idx");
- break;
- }
-
- clSess = ClientCache[row].Clients[idx];
-
- current = &SessionCache[clSess.serverRow].Sessions[clSess.serverIdx];
- if (XMEMCMP(current->serverID, id, len) == 0) {
- WOLFSSL_MSG("Found a serverid match for client");
- if (LowResTimer() < (current->bornOn + current->timeout)) {
- WOLFSSL_MSG("Session valid");
- ret = current;
- break;
- } else {
- WOLFSSL_MSG("Session timed out"); /* could have more for id */
- }
- } else {
- WOLFSSL_MSG("ServerID not a match from client table");
- }
- }
-
- wc_UnLockMutex(&session_mutex);
-
- return ret;
-}
-
-#endif /* NO_CLIENT_CACHE */
-
-/* Restore the master secret and session information for certificates.
- *
- * ssl The SSL/TLS object.
- * session The cached session to restore.
- * masterSecret The master secret from the cached session.
- * restoreSessionCerts Restoring session certificates is required.
- */
-static WC_INLINE void RestoreSession(WOLFSSL* ssl, WOLFSSL_SESSION* session,
- byte* masterSecret, byte restoreSessionCerts)
-{
- (void)ssl;
- (void)restoreSessionCerts;
-
- if (masterSecret)
- XMEMCPY(masterSecret, session->masterSecret, SECRET_LEN);
-#ifdef SESSION_CERTS
- /* If set, we should copy the session certs into the ssl object
- * from the session we are returning so we can resume */
- if (restoreSessionCerts) {
- ssl->session.chain = session->chain;
- ssl->session.version = session->version;
- #ifdef NO_RESUME_SUITE_CHECK
- ssl->session.cipherSuite0 = session->cipherSuite0;
- ssl->session.cipherSuite = session->cipherSuite;
- #endif
- }
-#endif /* SESSION_CERTS */
-#if !defined(NO_RESUME_SUITE_CHECK) || \
- (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET))
- ssl->session.cipherSuite0 = session->cipherSuite0;
- ssl->session.cipherSuite = session->cipherSuite;
-#endif
-}
-
-WOLFSSL_SESSION* GetSession(WOLFSSL* ssl, byte* masterSecret,
- byte restoreSessionCerts)
-{
- WOLFSSL_SESSION* ret = 0;
- const byte* id = NULL;
- word32 row;
- int idx;
- int count;
- int error = 0;
-
- (void) restoreSessionCerts;
-
- if (ssl->options.sessionCacheOff)
- return NULL;
-
- if (ssl->options.haveSessionId == 0)
- return NULL;
-
-#ifdef HAVE_SESSION_TICKET
- if (ssl->options.side == WOLFSSL_SERVER_END && ssl->options.useTicket == 1)
- return NULL;
-#endif
-
- if (!ssl->options.tls1_3 && ssl->arrays != NULL)
- id = ssl->arrays->sessionID;
- else
- id = ssl->session.sessionID;
-
-#ifdef HAVE_EXT_CACHE
- if (ssl->ctx->get_sess_cb != NULL) {
- int copy = 0;
- /* Attempt to retrieve the session from the external cache. */
- ret = ssl->ctx->get_sess_cb(ssl, (byte*)id, ID_LEN, ©);
- if (ret != NULL) {
- RestoreSession(ssl, ret, masterSecret, restoreSessionCerts);
- return ret;
- }
- }
-
- if (ssl->ctx->internalCacheOff)
- return NULL;
-#endif
-
- row = HashSession(id, ID_LEN, &error) % SESSION_ROWS;
- if (error != 0) {
- WOLFSSL_MSG("Hash session failed");
- return NULL;
- }
-
- if (wc_LockMutex(&session_mutex) != 0)
- return 0;
-
- /* start from most recently used */
- count = min((word32)SessionCache[row].totalCount, SESSIONS_PER_ROW);
- idx = SessionCache[row].nextIdx - 1;
- if (idx < 0)
- idx = SESSIONS_PER_ROW - 1; /* if back to front, the previous was end */
-
- for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
- WOLFSSL_SESSION* current;
-
- if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
- WOLFSSL_MSG("Bad idx");
- break;
- }
-
- current = &SessionCache[row].Sessions[idx];
- if (XMEMCMP(current->sessionID, id, ID_LEN) == 0) {
- WOLFSSL_MSG("Found a session match");
- if (LowResTimer() < (current->bornOn + current->timeout)) {
- WOLFSSL_MSG("Session valid");
- ret = current;
- RestoreSession(ssl, ret, masterSecret, restoreSessionCerts);
- } else {
- WOLFSSL_MSG("Session timed out");
- }
- break; /* no more sessionIDs whether valid or not that match */
- } else {
- WOLFSSL_MSG("SessionID not a match at this idx");
- }
- }
-
- wc_UnLockMutex(&session_mutex);
-
- return ret;
-}
-
-
-static int GetDeepCopySession(WOLFSSL* ssl, WOLFSSL_SESSION* copyFrom)
-{
- WOLFSSL_SESSION* copyInto = &ssl->session;
- void* tmpBuff = NULL;
- int ticketLen = 0;
- int doDynamicCopy = 0;
- int ret = WOLFSSL_SUCCESS;
-
- (void)ticketLen;
- (void)doDynamicCopy;
- (void)tmpBuff;
-
- if (!ssl || !copyFrom)
- return BAD_FUNC_ARG;
-
-#ifdef HAVE_SESSION_TICKET
- /* Free old dynamic ticket if we had one to avoid leak */
- if (copyInto->isDynamic) {
- XFREE(copyInto->ticket, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- copyInto->ticket = copyInto->staticTicket;
- copyInto->isDynamic = 0;
- }
-#endif
-
- if (wc_LockMutex(&session_mutex) != 0)
- return BAD_MUTEX_E;
-
-#ifdef HAVE_SESSION_TICKET
- /* Size of ticket to alloc if needed; Use later for alloc outside lock */
- doDynamicCopy = copyFrom->isDynamic;
- ticketLen = copyFrom->ticketLen;
-#endif
-
- *copyInto = *copyFrom;
-
- /* Default ticket to non dynamic. This will avoid crash if we fail below */
-#ifdef HAVE_SESSION_TICKET
- copyInto->ticket = copyInto->staticTicket;
- copyInto->isDynamic = 0;
-#endif
-
-#ifndef NO_RESUME_SUITE_CHECK
- copyInto->cipherSuite0 = copyFrom->cipherSuite0;
- copyInto->cipherSuite = copyFrom->cipherSuite;
-#endif
-
- if (wc_UnLockMutex(&session_mutex) != 0) {
- return BAD_MUTEX_E;
- }
-
-#ifdef HAVE_SESSION_TICKET
-#ifdef WOLFSSL_TLS13
- if (wc_LockMutex(&session_mutex) != 0) {
- XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- return BAD_MUTEX_E;
- }
-
-#ifdef NO_RESUME_SUITE_CHECK
- copyInto->cipherSuite0 = copyFrom->cipherSuite0;
- copyInto->cipherSuite = copyFrom->cipherSuite;
-#endif
- copyInto->namedGroup = copyFrom->namedGroup;
- copyInto->ticketSeen = copyFrom->ticketSeen;
- copyInto->ticketAdd = copyFrom->ticketAdd;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- XMEMCPY(©Into->ticketNonce, ©From->ticketNonce,
- sizeof(TicketNonce));
-#endif
-#ifdef WOLFSSL_EARLY_DATA
- copyInto->maxEarlyDataSz = copyFrom->maxEarlyDataSz;
-#endif
- XMEMCPY(copyInto->masterSecret, copyFrom->masterSecret, SECRET_LEN);
-
- if (wc_UnLockMutex(&session_mutex) != 0) {
- if (ret == WOLFSSL_SUCCESS)
- ret = BAD_MUTEX_E;
- }
-#endif
- /* If doing dynamic copy, need to alloc outside lock, then inside a lock
- * confirm the size still matches and memcpy */
- if (doDynamicCopy) {
- tmpBuff = (byte*)XMALLOC(ticketLen, ssl->heap,
- DYNAMIC_TYPE_SESSION_TICK);
- if (!tmpBuff)
- return MEMORY_ERROR;
-
- if (wc_LockMutex(&session_mutex) != 0) {
- XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- return BAD_MUTEX_E;
- }
-
- if ((word16)ticketLen != copyFrom->ticketLen) {
- /* Another thread modified the ssl-> session ticket during alloc.
- * Treat as error, since ticket different than when copy requested */
- ret = VAR_STATE_CHANGE_E;
- }
-
- if (ret == WOLFSSL_SUCCESS) {
- copyInto->ticket = (byte*)tmpBuff;
- copyInto->isDynamic = 1;
- XMEMCPY(copyInto->ticket, copyFrom->ticket, ticketLen);
- }
- } else {
- /* Need to ensure ticket pointer gets updated to own buffer
- * and is not pointing to buff of session copied from */
- copyInto->ticket = copyInto->staticTicket;
- }
-
- if (doDynamicCopy) {
- if (wc_UnLockMutex(&session_mutex) != 0) {
- if (ret == WOLFSSL_SUCCESS)
- ret = BAD_MUTEX_E;
- }
- }
-
- if (ret != WOLFSSL_SUCCESS) {
- /* cleanup */
- if (tmpBuff)
- XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- copyInto->ticket = copyInto->staticTicket;
- copyInto->isDynamic = 0;
- }
-#endif /* HAVE_SESSION_TICKET */
- return ret;
-}
-
-
-int SetSession(WOLFSSL* ssl, WOLFSSL_SESSION* session)
-{
- if (ssl->options.sessionCacheOff)
- return WOLFSSL_FAILURE;
-
-#ifdef OPENSSL_EXTRA
- /* check for application context id */
- if (ssl->sessionCtxSz > 0) {
- if (XMEMCMP(ssl->sessionCtx, session->sessionCtx, ssl->sessionCtxSz)) {
- /* context id did not match! */
- WOLFSSL_MSG("Session context did not match");
- return SSL_FAILURE;
- }
- }
-#endif /* OPENSSL_EXTRA */
-
- if (LowResTimer() < (session->bornOn + session->timeout)) {
- int ret = GetDeepCopySession(ssl, session);
- if (ret == WOLFSSL_SUCCESS) {
- ssl->options.resuming = 1;
-
-#if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
- defined(HAVE_SESSION_TICKET))
- ssl->version = session->version;
-#endif
-#if defined(SESSION_CERTS) || !defined(NO_RESUME_SUITE_CHECK) || \
- (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET))
- ssl->options.cipherSuite0 = session->cipherSuite0;
- ssl->options.cipherSuite = session->cipherSuite;
-#endif
- }
-
- return ret;
- }
- return WOLFSSL_FAILURE; /* session timed out */
-}
-
-
-#ifdef WOLFSSL_SESSION_STATS
-static int get_locked_session_stats(word32* active, word32* total,
- word32* peak);
-#endif
-
-int AddSession(WOLFSSL* ssl)
-{
- word32 row = 0;
- word32 idx = 0;
- int error = 0;
-#ifdef HAVE_SESSION_TICKET
- byte* tmpBuff = NULL;
- int ticLen = 0;
-#endif
- WOLFSSL_SESSION* session;
- int i;
- int overwrite = 0;
-
- if (ssl->options.sessionCacheOff)
- return 0;
-
- if (ssl->options.haveSessionId == 0)
- return 0;
-
-#ifdef HAVE_SESSION_TICKET
- if (ssl->options.side == WOLFSSL_SERVER_END && ssl->options.useTicket == 1)
- return 0;
-#endif
-
-#ifdef HAVE_SESSION_TICKET
- ticLen = ssl->session.ticketLen;
- /* Alloc Memory here so if Malloc fails can exit outside of lock */
- if(ticLen > SESSION_TICKET_LEN) {
- tmpBuff = (byte*)XMALLOC(ticLen, ssl->heap,
- DYNAMIC_TYPE_SESSION_TICK);
- if(!tmpBuff)
- return MEMORY_E;
- }
-#endif
-
-#ifdef HAVE_EXT_CACHE
- if (ssl->options.internalCacheOff) {
- /* Create a new session object to be stored. */
- session = (WOLFSSL_SESSION*)XMALLOC(sizeof(WOLFSSL_SESSION), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (session == NULL) {
-#ifdef HAVE_SESSION_TICKET
- XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
-#endif
- return MEMORY_E;
- }
- XMEMSET(session, 0, sizeof(WOLFSSL_SESSION));
- session->isAlloced = 1;
- }
- else
-#endif
- {
- /* Use the session object in the cache for external cache if required.
- */
-#if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
- if (ssl->options.tls1_3) {
- row = HashSession(ssl->session.sessionID, ID_LEN, &error) %
- SESSION_ROWS;
- }
- else
-#endif
- {
- row = HashSession(ssl->arrays->sessionID, ID_LEN, &error) %
- SESSION_ROWS;
- }
- if (error != 0) {
- WOLFSSL_MSG("Hash session failed");
-#ifdef HAVE_SESSION_TICKET
- XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
-#endif
- return error;
- }
-
- if (wc_LockMutex(&session_mutex) != 0) {
-#ifdef HAVE_SESSION_TICKET
- XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
-#endif
- return BAD_MUTEX_E;
- }
-
- for (i=0; ioptions.tls1_3) {
- if (XMEMCMP(ssl->session.sessionID, SessionCache[row].Sessions[i].sessionID, ID_LEN) == 0) {
- WOLFSSL_MSG("Session already exists. Overwriting.");
- overwrite = 1;
- idx = i;
- break;
- }
- }
- else {
- if (XMEMCMP(ssl->arrays->sessionID, SessionCache[row].Sessions[i].sessionID, ID_LEN) == 0) {
- WOLFSSL_MSG("Session already exists. Overwriting.");
- overwrite = 1;
- idx = i;
- break;
- }
- }
- }
-
- if (!overwrite) {
- idx = SessionCache[row].nextIdx++;
- }
-#ifdef SESSION_INDEX
- ssl->sessionIndex = (row << SESSIDX_ROW_SHIFT) | idx;
-#endif
- session = &SessionCache[row].Sessions[idx];
- }
-
- if (!ssl->options.tls1_3)
- XMEMCPY(session->masterSecret, ssl->arrays->masterSecret, SECRET_LEN);
- else
- XMEMCPY(session->masterSecret, ssl->session.masterSecret, SECRET_LEN);
- session->haveEMS = ssl->options.haveEMS;
-#if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
- if (ssl->options.tls1_3) {
- XMEMCPY(session->sessionID, ssl->session.sessionID, ID_LEN);
- session->sessionIDSz = ID_LEN;
- }
- else
-#endif
- {
- XMEMCPY(session->sessionID, ssl->arrays->sessionID, ID_LEN);
- session->sessionIDSz = ssl->arrays->sessionIDSz;
- }
-
-#ifdef OPENSSL_EXTRA
- /* If using compatibility layer then check for and copy over session context
- * id. */
- if (ssl->sessionCtxSz > 0 && ssl->sessionCtxSz < ID_LEN) {
- XMEMCPY(session->sessionCtx, ssl->sessionCtx, ssl->sessionCtxSz);
- }
-#endif
-
- session->timeout = ssl->timeout;
- session->bornOn = LowResTimer();
-
-#ifdef HAVE_SESSION_TICKET
- /* Check if another thread modified ticket since alloc */
- if ((word16)ticLen != ssl->session.ticketLen) {
- error = VAR_STATE_CHANGE_E;
- }
-
- if (error == 0) {
- /* Cleanup cache row's old Dynamic buff if exists */
- if(session->isDynamic) {
- XFREE(session->ticket, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- session->ticket = NULL;
- }
-
- /* If too large to store in static buffer, use dyn buffer */
- if (ticLen > SESSION_TICKET_LEN) {
- session->ticket = tmpBuff;
- session->isDynamic = 1;
- } else {
- session->ticket = session->staticTicket;
- session->isDynamic = 0;
- }
-
- session->ticketLen = (word16)ticLen;
- XMEMCPY(session->ticket, ssl->session.ticket, ticLen);
- } else { /* cleanup, reset state */
- session->ticket = session->staticTicket;
- session->isDynamic = 0;
- session->ticketLen = 0;
- if (tmpBuff) {
- XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
- tmpBuff = NULL;
- }
- }
-#endif
-
-#ifdef SESSION_CERTS
- if (error == 0) {
- if (!overwrite || ssl->session.chain.count > 0) {
- /*
- * If we are overwriting and no certs present in ssl->session.chain
- * then keep the old chain.
- */
- session->chain.count = ssl->session.chain.count;
- XMEMCPY(session->chain.certs, ssl->session.chain.certs,
- sizeof(x509_buffer) * session->chain.count);
- }
- }
-#endif /* SESSION_CERTS */
-#if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
- defined(HAVE_SESSION_TICKET))
- if (error == 0) {
- session->version = ssl->version;
- }
-#endif /* SESSION_CERTS || (WOLFSSL_TLS13 & HAVE_SESSION_TICKET) */
-#if defined(SESSION_CERTS) || !defined(NO_RESUME_SUITE_CHECK) || \
- (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET))
- if (error == 0) {
- session->cipherSuite0 = ssl->options.cipherSuite0;
- session->cipherSuite = ssl->options.cipherSuite;
- }
-#endif
-#if defined(WOLFSSL_TLS13)
- if (error == 0) {
- session->namedGroup = ssl->session.namedGroup;
- }
-#endif
-#if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
- if (error == 0) {
- session->ticketSeen = ssl->session.ticketSeen;
- session->ticketAdd = ssl->session.ticketAdd;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- XMEMCPY(&session->ticketNonce, &ssl->session.ticketNonce,
- sizeof(TicketNonce));
-#endif
- #ifdef WOLFSSL_EARLY_DATA
- session->maxEarlyDataSz = ssl->session.maxEarlyDataSz;
- #endif
- }
-#endif /* WOLFSSL_TLS13 && HAVE_SESSION_TICKET */
-#ifdef HAVE_EXT_CACHE
- if (!ssl->options.internalCacheOff)
-#endif
- {
- if (error == 0) {
- SessionCache[row].totalCount++;
- if (SessionCache[row].nextIdx == SESSIONS_PER_ROW)
- SessionCache[row].nextIdx = 0;
- }
- }
-#ifndef NO_CLIENT_CACHE
- if (error == 0) {
- if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->session.idLen) {
- word32 clientRow, clientIdx;
-
- WOLFSSL_MSG("Adding client cache entry");
-
- session->idLen = ssl->session.idLen;
- XMEMCPY(session->serverID, ssl->session.serverID,
- ssl->session.idLen);
-
-#ifdef HAVE_EXT_CACHE
- if (!ssl->options.internalCacheOff)
-#endif
- {
- clientRow = HashSession(ssl->session.serverID,
- ssl->session.idLen, &error) % SESSION_ROWS;
- if (error != 0) {
- WOLFSSL_MSG("Hash session failed");
- } else {
- clientIdx = ClientCache[clientRow].nextIdx++;
-
- ClientCache[clientRow].Clients[clientIdx].serverRow =
- (word16)row;
- ClientCache[clientRow].Clients[clientIdx].serverIdx =
- (word16)idx;
-
- ClientCache[clientRow].totalCount++;
- if (ClientCache[clientRow].nextIdx == SESSIONS_PER_ROW)
- ClientCache[clientRow].nextIdx = 0;
- }
- }
- }
- else
- session->idLen = 0;
- }
-#endif /* NO_CLIENT_CACHE */
-
-#if defined(WOLFSSL_SESSION_STATS) && defined(WOLFSSL_PEAK_SESSIONS)
-#ifdef HAVE_EXT_CACHE
- if (!ssl->options.internalCacheOff)
-#endif
- {
- if (error == 0) {
- word32 active = 0;
-
- error = get_locked_session_stats(&active, NULL, NULL);
- if (error == WOLFSSL_SUCCESS) {
- error = 0; /* back to this function ok */
-
- if (active > PeakSessions)
- PeakSessions = active;
- }
- }
- }
-#endif /* defined(WOLFSSL_SESSION_STATS) && defined(WOLFSSL_PEAK_SESSIONS) */
-
-#ifdef HAVE_EXT_CACHE
- if (!ssl->options.internalCacheOff)
-#endif
- {
- if (wc_UnLockMutex(&session_mutex) != 0)
- return BAD_MUTEX_E;
- }
-
-#ifdef HAVE_EXT_CACHE
- if (error == 0 && ssl->ctx->new_sess_cb != NULL)
- ssl->ctx->new_sess_cb(ssl, session);
- if (ssl->options.internalCacheOff)
- wolfSSL_SESSION_free(session);
-#endif
-
- return error;
-}
-
-
-#ifdef SESSION_INDEX
-
-int wolfSSL_GetSessionIndex(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_GetSessionIndex");
- WOLFSSL_LEAVE("wolfSSL_GetSessionIndex", ssl->sessionIndex);
- return ssl->sessionIndex;
-}
-
-
-int wolfSSL_GetSessionAtIndex(int idx, WOLFSSL_SESSION* session)
-{
- int row, col, result = WOLFSSL_FAILURE;
-
- WOLFSSL_ENTER("wolfSSL_GetSessionAtIndex");
-
- row = idx >> SESSIDX_ROW_SHIFT;
- col = idx & SESSIDX_IDX_MASK;
-
- if (wc_LockMutex(&session_mutex) != 0) {
- return BAD_MUTEX_E;
- }
-
- if (row < SESSION_ROWS &&
- col < (int)min(SessionCache[row].totalCount, SESSIONS_PER_ROW)) {
- XMEMCPY(session,
- &SessionCache[row].Sessions[col], sizeof(WOLFSSL_SESSION));
- result = WOLFSSL_SUCCESS;
- }
-
- if (wc_UnLockMutex(&session_mutex) != 0)
- result = BAD_MUTEX_E;
-
- WOLFSSL_LEAVE("wolfSSL_GetSessionAtIndex", result);
- return result;
-}
-
-#endif /* SESSION_INDEX */
-
-#if defined(SESSION_CERTS)
-
-WOLFSSL_X509_CHAIN* wolfSSL_SESSION_get_peer_chain(WOLFSSL_SESSION* session)
-{
- WOLFSSL_X509_CHAIN* chain = NULL;
-
- WOLFSSL_ENTER("wolfSSL_SESSION_get_peer_chain");
- if (session)
- chain = &session->chain;
-
- WOLFSSL_LEAVE("wolfSSL_SESSION_get_peer_chain", chain ? 1 : 0);
- return chain;
-}
-
-
-#ifdef OPENSSL_EXTRA
-/* gets the peer certificate associated with the session passed in
- * returns null on failure, the caller should not free the returned pointer */
-WOLFSSL_X509* wolfSSL_SESSION_get0_peer(WOLFSSL_SESSION* session)
-{
- WOLFSSL_ENTER("wolfSSL_SESSION_get_peer_chain");
- if (session) {
- int count;
-
- count = wolfSSL_get_chain_count(&session->chain);
- if (count < 1 || count >= MAX_CHAIN_DEPTH) {
- WOLFSSL_MSG("bad count found");
- return NULL;
- }
-
- if (session->peer == NULL) {
- session->peer = wolfSSL_get_chain_X509(&session->chain, 0);
- }
- return session->peer;
- }
- WOLFSSL_MSG("No session passed in");
-
- return NULL;
-}
-#endif /* OPENSSL_EXTRA */
-#endif /* SESSION_INDEX && SESSION_CERTS */
-
-
-#ifdef WOLFSSL_SESSION_STATS
-
-/* requires session_mutex lock held, WOLFSSL_SUCCESS on ok */
-static int get_locked_session_stats(word32* active, word32* total, word32* peak)
-{
- int result = WOLFSSL_SUCCESS;
- int i;
- int count;
- int idx;
- word32 now = 0;
- word32 seen = 0;
- word32 ticks = LowResTimer();
-
- (void)peak;
-
- WOLFSSL_ENTER("get_locked_session_stats");
-
- for (i = 0; i < SESSION_ROWS; i++) {
- seen += SessionCache[i].totalCount;
-
- if (active == NULL)
- continue; /* no need to calculate what we can't set */
-
- count = min((word32)SessionCache[i].totalCount, SESSIONS_PER_ROW);
- idx = SessionCache[i].nextIdx - 1;
- if (idx < 0)
- idx = SESSIONS_PER_ROW - 1; /* if back to front previous was end */
-
- for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
- if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
- WOLFSSL_MSG("Bad idx");
- break;
- }
-
- /* if not expired then good */
- if (ticks < (SessionCache[i].Sessions[idx].bornOn +
- SessionCache[i].Sessions[idx].timeout) ) {
- now++;
- }
- }
- }
-
- if (active)
- *active = now;
-
- if (total)
- *total = seen;
-
-#ifdef WOLFSSL_PEAK_SESSIONS
- if (peak)
- *peak = PeakSessions;
-#endif
-
- WOLFSSL_LEAVE("get_locked_session_stats", result);
-
- return result;
-}
-
-
-/* return WOLFSSL_SUCCESS on ok */
-int wolfSSL_get_session_stats(word32* active, word32* total, word32* peak,
- word32* maxSessions)
-{
- int result = WOLFSSL_SUCCESS;
-
- WOLFSSL_ENTER("wolfSSL_get_session_stats");
-
- if (maxSessions) {
- *maxSessions = SESSIONS_PER_ROW * SESSION_ROWS;
-
- if (active == NULL && total == NULL && peak == NULL)
- return result; /* we're done */
- }
-
- /* user must provide at least one query value */
- if (active == NULL && total == NULL && peak == NULL)
- return BAD_FUNC_ARG;
-
- if (wc_LockMutex(&session_mutex) != 0) {
- return BAD_MUTEX_E;
- }
-
- result = get_locked_session_stats(active, total, peak);
-
- if (wc_UnLockMutex(&session_mutex) != 0)
- result = BAD_MUTEX_E;
-
- WOLFSSL_LEAVE("wolfSSL_get_session_stats", result);
-
- return result;
-}
-
-#endif /* WOLFSSL_SESSION_STATS */
-
-
- #ifdef PRINT_SESSION_STATS
-
- /* WOLFSSL_SUCCESS on ok */
- int wolfSSL_PrintSessionStats(void)
- {
- word32 totalSessionsSeen = 0;
- word32 totalSessionsNow = 0;
- word32 peak = 0;
- word32 maxSessions = 0;
- int i;
- int ret;
- double E; /* expected freq */
- double chiSquare = 0;
-
- ret = wolfSSL_get_session_stats(&totalSessionsNow, &totalSessionsSeen,
- &peak, &maxSessions);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- printf("Total Sessions Seen = %d\n", totalSessionsSeen);
- printf("Total Sessions Now = %d\n", totalSessionsNow);
-#ifdef WOLFSSL_PEAK_SESSIONS
- printf("Peak Sessions = %d\n", peak);
-#endif
- printf("Max Sessions = %d\n", maxSessions);
-
- E = (double)totalSessionsSeen / SESSION_ROWS;
-
- for (i = 0; i < SESSION_ROWS; i++) {
- double diff = SessionCache[i].totalCount - E;
- diff *= diff; /* square */
- diff /= E; /* normalize */
-
- chiSquare += diff;
- }
- printf(" chi-square = %5.1f, d.f. = %d\n", chiSquare,
- SESSION_ROWS - 1);
- #if (SESSION_ROWS == 11)
- printf(" .05 p value = 18.3, chi-square should be less\n");
- #elif (SESSION_ROWS == 211)
- printf(".05 p value = 244.8, chi-square should be less\n");
- #elif (SESSION_ROWS == 5981)
- printf(".05 p value = 6161.0, chi-square should be less\n");
- #elif (SESSION_ROWS == 3)
- printf(".05 p value = 6.0, chi-square should be less\n");
- #elif (SESSION_ROWS == 2861)
- printf(".05 p value = 2985.5, chi-square should be less\n");
- #endif
- printf("\n");
-
- return ret;
- }
-
- #endif /* SESSION_STATS */
-
-#else /* NO_SESSION_CACHE */
-
-/* No session cache version */
-WOLFSSL_SESSION* GetSession(WOLFSSL* ssl, byte* masterSecret,
- byte restoreSessionCerts)
-{
- (void)ssl;
- (void)masterSecret;
- (void)restoreSessionCerts;
-
- return NULL;
-}
-
-#endif /* NO_SESSION_CACHE */
-
-
-/* call before SSL_connect, if verifying will add name check to
- date check and signature check */
-WOLFSSL_ABI
-int wolfSSL_check_domain_name(WOLFSSL* ssl, const char* dn)
-{
- WOLFSSL_ENTER("wolfSSL_check_domain_name");
-
- if (ssl == NULL || dn == NULL) {
- WOLFSSL_MSG("Bad function argument: NULL");
- return WOLFSSL_FAILURE;
- }
-
- if (ssl->buffers.domainName.buffer)
- XFREE(ssl->buffers.domainName.buffer, ssl->heap, DYNAMIC_TYPE_DOMAIN);
-
- ssl->buffers.domainName.length = (word32)XSTRLEN(dn);
- ssl->buffers.domainName.buffer = (byte*)XMALLOC(
- ssl->buffers.domainName.length + 1, ssl->heap, DYNAMIC_TYPE_DOMAIN);
-
- if (ssl->buffers.domainName.buffer) {
- unsigned char* domainName = ssl->buffers.domainName.buffer;
- XMEMCPY(domainName, dn, ssl->buffers.domainName.length);
- domainName[ssl->buffers.domainName.length] = '\0';
- return WOLFSSL_SUCCESS;
- }
- else {
- ssl->error = MEMORY_ERROR;
- return WOLFSSL_FAILURE;
- }
-}
-
-
-/* turn on wolfSSL zlib compression
- returns WOLFSSL_SUCCESS for success, else error (not built in)
-*/
-int wolfSSL_set_compression(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_set_compression");
- (void)ssl;
-#ifdef HAVE_LIBZ
- ssl->options.usingCompression = 1;
- return WOLFSSL_SUCCESS;
-#else
- return NOT_COMPILED_IN;
-#endif
-}
-
-
-#ifndef USE_WINDOWS_API
- #ifndef NO_WRITEV
-
- /* simulate writev semantics, doesn't actually do block at a time though
- because of SSL_write behavior and because front adds may be small */
- int wolfSSL_writev(WOLFSSL* ssl, const struct iovec* iov, int iovcnt)
- {
- #ifdef WOLFSSL_SMALL_STACK
- byte staticBuffer[1]; /* force heap usage */
- #else
- byte staticBuffer[FILE_BUFFER_SIZE];
- #endif
- byte* myBuffer = staticBuffer;
- int dynamic = 0;
- int sending = 0;
- int idx = 0;
- int i;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_writev");
-
- for (i = 0; i < iovcnt; i++)
- sending += (int)iov[i].iov_len;
-
- if (sending > (int)sizeof(staticBuffer)) {
- myBuffer = (byte*)XMALLOC(sending, ssl->heap,
- DYNAMIC_TYPE_WRITEV);
- if (!myBuffer)
- return MEMORY_ERROR;
-
- dynamic = 1;
- }
-
- for (i = 0; i < iovcnt; i++) {
- XMEMCPY(&myBuffer[idx], iov[i].iov_base, iov[i].iov_len);
- idx += (int)iov[i].iov_len;
- }
-
- ret = wolfSSL_write(ssl, myBuffer, sending);
-
- if (dynamic)
- XFREE(myBuffer, ssl->heap, DYNAMIC_TYPE_WRITEV);
-
- return ret;
- }
- #endif
-#endif
-
-
-#ifdef WOLFSSL_CALLBACKS
-
- typedef struct itimerval Itimerval;
-
- /* don't keep calling simple functions while setting up timer and signals
- if no inlining these are the next best */
-
- #define AddTimes(a, b, c) \
- do { \
- c.tv_sec = a.tv_sec + b.tv_sec; \
- c.tv_usec = a.tv_usec + b.tv_usec; \
- if (c.tv_usec >= 1000000) { \
- c.tv_sec++; \
- c.tv_usec -= 1000000; \
- } \
- } while (0)
-
-
- #define SubtractTimes(a, b, c) \
- do { \
- c.tv_sec = a.tv_sec - b.tv_sec; \
- c.tv_usec = a.tv_usec - b.tv_usec; \
- if (c.tv_usec < 0) { \
- c.tv_sec--; \
- c.tv_usec += 1000000; \
- } \
- } while (0)
-
- #define CmpTimes(a, b, cmp) \
- ((a.tv_sec == b.tv_sec) ? \
- (a.tv_usec cmp b.tv_usec) : \
- (a.tv_sec cmp b.tv_sec)) \
-
-
- /* do nothing handler */
- static void myHandler(int signo)
- {
- (void)signo;
- return;
- }
-
-
- static int wolfSSL_ex_wrapper(WOLFSSL* ssl, HandShakeCallBack hsCb,
- TimeoutCallBack toCb, WOLFSSL_TIMEVAL timeout)
- {
- int ret = WOLFSSL_FATAL_ERROR;
- int oldTimerOn = 0; /* was timer already on */
- WOLFSSL_TIMEVAL startTime;
- WOLFSSL_TIMEVAL endTime;
- WOLFSSL_TIMEVAL totalTime;
- Itimerval myTimeout;
- Itimerval oldTimeout; /* if old timer adjust from total time to reset */
- struct sigaction act, oact;
-
- #define ERR_OUT(x) { ssl->hsInfoOn = 0; ssl->toInfoOn = 0; return x; }
-
- if (hsCb) {
- ssl->hsInfoOn = 1;
- InitHandShakeInfo(&ssl->handShakeInfo, ssl);
- }
- if (toCb) {
- ssl->toInfoOn = 1;
- InitTimeoutInfo(&ssl->timeoutInfo);
-
- if (gettimeofday(&startTime, 0) < 0)
- ERR_OUT(GETTIME_ERROR);
-
- /* use setitimer to simulate getitimer, init 0 myTimeout */
- myTimeout.it_interval.tv_sec = 0;
- myTimeout.it_interval.tv_usec = 0;
- myTimeout.it_value.tv_sec = 0;
- myTimeout.it_value.tv_usec = 0;
- if (setitimer(ITIMER_REAL, &myTimeout, &oldTimeout) < 0)
- ERR_OUT(SETITIMER_ERROR);
-
- if (oldTimeout.it_value.tv_sec || oldTimeout.it_value.tv_usec) {
- oldTimerOn = 1;
-
- /* is old timer going to expire before ours */
- if (CmpTimes(oldTimeout.it_value, timeout, <)) {
- timeout.tv_sec = oldTimeout.it_value.tv_sec;
- timeout.tv_usec = oldTimeout.it_value.tv_usec;
- }
- }
- myTimeout.it_value.tv_sec = timeout.tv_sec;
- myTimeout.it_value.tv_usec = timeout.tv_usec;
-
- /* set up signal handler, don't restart socket send/recv */
- act.sa_handler = myHandler;
- sigemptyset(&act.sa_mask);
- act.sa_flags = 0;
-#ifdef SA_INTERRUPT
- act.sa_flags |= SA_INTERRUPT;
-#endif
- if (sigaction(SIGALRM, &act, &oact) < 0)
- ERR_OUT(SIGACT_ERROR);
-
- if (setitimer(ITIMER_REAL, &myTimeout, 0) < 0)
- ERR_OUT(SETITIMER_ERROR);
- }
-
- /* do main work */
-#ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- ret = wolfSSL_connect(ssl);
-#endif
-#ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END)
- ret = wolfSSL_accept(ssl);
-#endif
-
- /* do callbacks */
- if (toCb) {
- if (oldTimerOn) {
- gettimeofday(&endTime, 0);
- SubtractTimes(endTime, startTime, totalTime);
- /* adjust old timer for elapsed time */
- if (CmpTimes(totalTime, oldTimeout.it_value, <))
- SubtractTimes(oldTimeout.it_value, totalTime,
- oldTimeout.it_value);
- else {
- /* reset value to interval, may be off */
- oldTimeout.it_value.tv_sec = oldTimeout.it_interval.tv_sec;
- oldTimeout.it_value.tv_usec =oldTimeout.it_interval.tv_usec;
- }
- /* keep iter the same whether there or not */
- }
- /* restore old handler */
- if (sigaction(SIGALRM, &oact, 0) < 0)
- ret = SIGACT_ERROR; /* more pressing error, stomp */
- else
- /* use old settings which may turn off (expired or not there) */
- if (setitimer(ITIMER_REAL, &oldTimeout, 0) < 0)
- ret = SETITIMER_ERROR;
-
- /* if we had a timeout call callback */
- if (ssl->timeoutInfo.timeoutName[0]) {
- ssl->timeoutInfo.timeoutValue.tv_sec = timeout.tv_sec;
- ssl->timeoutInfo.timeoutValue.tv_usec = timeout.tv_usec;
- (toCb)(&ssl->timeoutInfo);
- }
- /* clean up */
- FreeTimeoutInfo(&ssl->timeoutInfo, ssl->heap);
- ssl->toInfoOn = 0;
- }
- if (hsCb) {
- FinishHandShakeInfo(&ssl->handShakeInfo);
- (hsCb)(&ssl->handShakeInfo);
- ssl->hsInfoOn = 0;
- }
- return ret;
- }
-
-
-#ifndef NO_WOLFSSL_CLIENT
-
- int wolfSSL_connect_ex(WOLFSSL* ssl, HandShakeCallBack hsCb,
- TimeoutCallBack toCb, WOLFSSL_TIMEVAL timeout)
- {
- WOLFSSL_ENTER("wolfSSL_connect_ex");
- return wolfSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
- }
-
-#endif
-
-
-#ifndef NO_WOLFSSL_SERVER
-
- int wolfSSL_accept_ex(WOLFSSL* ssl, HandShakeCallBack hsCb,
- TimeoutCallBack toCb, WOLFSSL_TIMEVAL timeout)
- {
- WOLFSSL_ENTER("wolfSSL_accept_ex");
- return wolfSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
- }
-
-#endif
-
-#endif /* WOLFSSL_CALLBACKS */
-
-
-#ifndef NO_PSK
-
- void wolfSSL_CTX_set_psk_client_callback(WOLFSSL_CTX* ctx,
- wc_psk_client_callback cb)
- {
- WOLFSSL_ENTER("SSL_CTX_set_psk_client_callback");
-
- if (ctx == NULL)
- return;
-
- ctx->havePSK = 1;
- ctx->client_psk_cb = cb;
- }
-
-
- void wolfSSL_set_psk_client_callback(WOLFSSL* ssl,wc_psk_client_callback cb)
- {
- byte haveRSA = 1;
- int keySz = 0;
-
- WOLFSSL_ENTER("SSL_set_psk_client_callback");
-
- if (ssl == NULL)
- return;
-
- ssl->options.havePSK = 1;
- ssl->options.client_psk_cb = cb;
-
- #ifdef NO_RSA
- haveRSA = 0;
- #endif
- #ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
- #endif
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
- }
-
-
- void wolfSSL_CTX_set_psk_server_callback(WOLFSSL_CTX* ctx,
- wc_psk_server_callback cb)
- {
- WOLFSSL_ENTER("SSL_CTX_set_psk_server_callback");
- if (ctx == NULL)
- return;
- ctx->havePSK = 1;
- ctx->server_psk_cb = cb;
- }
-
-
- void wolfSSL_set_psk_server_callback(WOLFSSL* ssl,wc_psk_server_callback cb)
- {
- byte haveRSA = 1;
- int keySz = 0;
-
- WOLFSSL_ENTER("SSL_set_psk_server_callback");
- if (ssl == NULL)
- return;
-
- ssl->options.havePSK = 1;
- ssl->options.server_psk_cb = cb;
-
- #ifdef NO_RSA
- haveRSA = 0;
- #endif
- #ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
- #endif
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
- }
-
-
- const char* wolfSSL_get_psk_identity_hint(const WOLFSSL* ssl)
- {
- WOLFSSL_ENTER("SSL_get_psk_identity_hint");
-
- if (ssl == NULL || ssl->arrays == NULL)
- return NULL;
-
- return ssl->arrays->server_hint;
- }
-
-
- const char* wolfSSL_get_psk_identity(const WOLFSSL* ssl)
- {
- WOLFSSL_ENTER("SSL_get_psk_identity");
-
- if (ssl == NULL || ssl->arrays == NULL)
- return NULL;
-
- return ssl->arrays->client_identity;
- }
-
-
- int wolfSSL_CTX_use_psk_identity_hint(WOLFSSL_CTX* ctx, const char* hint)
- {
- WOLFSSL_ENTER("SSL_CTX_use_psk_identity_hint");
- if (hint == 0)
- ctx->server_hint[0] = '\0';
- else {
- /* Qt does not call CTX_set_*_psk_callbacks where havePSK is set */
- #ifdef WOLFSSL_QT
- ctx->havePSK=1;
- #endif
- XSTRNCPY(ctx->server_hint, hint, MAX_PSK_ID_LEN);
- ctx->server_hint[MAX_PSK_ID_LEN] = '\0'; /* null term */
- }
- return WOLFSSL_SUCCESS;
- }
-
-
- int wolfSSL_use_psk_identity_hint(WOLFSSL* ssl, const char* hint)
- {
- WOLFSSL_ENTER("SSL_use_psk_identity_hint");
-
- if (ssl == NULL || ssl->arrays == NULL)
- return WOLFSSL_FAILURE;
-
- if (hint == 0)
- ssl->arrays->server_hint[0] = 0;
- else {
- XSTRNCPY(ssl->arrays->server_hint, hint,
- sizeof(ssl->arrays->server_hint)-1);
- ssl->arrays->server_hint[sizeof(ssl->arrays->server_hint)-1] = '\0';
- }
- return WOLFSSL_SUCCESS;
- }
-
-#endif /* NO_PSK */
-
-
-#ifdef HAVE_ANON
-
- int wolfSSL_CTX_allow_anon_cipher(WOLFSSL_CTX* ctx)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_allow_anon_cipher");
-
- if (ctx == NULL)
- return WOLFSSL_FAILURE;
-
- ctx->haveAnon = 1;
-
- return WOLFSSL_SUCCESS;
- }
-
-#endif /* HAVE_ANON */
-
-
-#ifndef NO_CERTS
-/* used to be defined on NO_FILESYSTEM only, but are generally useful */
-
- int wolfSSL_CTX_load_verify_buffer_ex(WOLFSSL_CTX* ctx,
- const unsigned char* in,
- long sz, int format, int userChain,
- word32 flags)
- {
- int verify;
-
- WOLFSSL_ENTER("wolfSSL_CTX_load_verify_buffer_ex");
-
- verify = GET_VERIFY_SETTING_CTX(ctx);
- if (flags & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY)
- verify = VERIFY_SKIP_DATE;
-
- if (format == WOLFSSL_FILETYPE_PEM)
- return ProcessChainBuffer(ctx, in, sz, format, CA_TYPE, NULL,
- verify);
- else
- return ProcessBuffer(ctx, in, sz, format, CA_TYPE, NULL, NULL,
- userChain, verify);
- }
-
- /* wolfSSL extension allows DER files to be loaded from buffers as well */
- int wolfSSL_CTX_load_verify_buffer(WOLFSSL_CTX* ctx,
- const unsigned char* in,
- long sz, int format)
- {
- return wolfSSL_CTX_load_verify_buffer_ex(ctx, in, sz, format, 0,
- WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS);
- }
-
- int wolfSSL_CTX_load_verify_chain_buffer_format(WOLFSSL_CTX* ctx,
- const unsigned char* in,
- long sz, int format)
- {
- return wolfSSL_CTX_load_verify_buffer_ex(ctx, in, sz, format, 1,
- WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS);
- }
-
-
-#ifdef WOLFSSL_TRUST_PEER_CERT
- int wolfSSL_CTX_trust_peer_buffer(WOLFSSL_CTX* ctx,
- const unsigned char* in,
- long sz, int format)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_trust_peer_buffer");
-
- /* sanity check on arguments */
- if (sz < 0 || in == NULL || ctx == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (format == WOLFSSL_FILETYPE_PEM)
- return ProcessChainBuffer(ctx, in, sz, format, TRUSTED_PEER_TYPE,
- NULL, GET_VERIFY_SETTING_CTX(ctx));
- else
- return ProcessBuffer(ctx, in, sz, format, TRUSTED_PEER_TYPE, NULL,
- NULL, 0, GET_VERIFY_SETTING_CTX(ctx));
- }
-#endif /* WOLFSSL_TRUST_PEER_CERT */
-
-
- int wolfSSL_CTX_use_certificate_buffer(WOLFSSL_CTX* ctx,
- const unsigned char* in, long sz, int format)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_buffer");
- return ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 0,
- GET_VERIFY_SETTING_CTX(ctx));
- }
-
-
- int wolfSSL_CTX_use_PrivateKey_buffer(WOLFSSL_CTX* ctx,
- const unsigned char* in, long sz, int format)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_buffer");
- return ProcessBuffer(ctx, in, sz, format, PRIVATEKEY_TYPE, NULL, NULL,
- 0, GET_VERIFY_SETTING_CTX(ctx));
- }
-
-#ifdef HAVE_PKCS11
- int wolfSSL_CTX_use_PrivateKey_id(WOLFSSL_CTX* ctx, const unsigned char* id,
- long sz, int devId, long keySz)
- {
- int ret = WOLFSSL_FAILURE;
-
- FreeDer(&ctx->privateKey);
- if (AllocDer(&ctx->privateKey, (word32)sz, PRIVATEKEY_TYPE,
- ctx->heap) == 0) {
- XMEMCPY(ctx->privateKey->buffer, id, sz);
- ctx->privateKeyId = 1;
- ctx->privateKeySz = (word32)keySz;
- if (devId != INVALID_DEVID)
- ctx->privateKeyDevId = devId;
- else
- ctx->privateKeyDevId = ctx->devId;
-
- ret = WOLFSSL_SUCCESS;
- }
-
- return ret;
- }
-#endif
-
- int wolfSSL_CTX_use_certificate_chain_buffer_format(WOLFSSL_CTX* ctx,
- const unsigned char* in, long sz, int format)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_buffer_format");
- return ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 1,
- GET_VERIFY_SETTING_CTX(ctx));
- }
-
- int wolfSSL_CTX_use_certificate_chain_buffer(WOLFSSL_CTX* ctx,
- const unsigned char* in, long sz)
- {
- return wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, in, sz,
- WOLFSSL_FILETYPE_PEM);
- }
-
-
-#ifndef NO_DH
-
- /* server wrapper for ctx or ssl Diffie-Hellman parameters */
- static int wolfSSL_SetTmpDH_buffer_wrapper(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
- const unsigned char* buf,
- long sz, int format)
- {
- DerBuffer* der = NULL;
- int ret = 0;
- word32 pSz = MAX_DH_SIZE;
- word32 gSz = MAX_DH_SIZE;
- #ifdef WOLFSSL_SMALL_STACK
- byte* p = NULL;
- byte* g = NULL;
- #else
- byte p[MAX_DH_SIZE];
- byte g[MAX_DH_SIZE];
- #endif
-
- if (ctx == NULL || buf == NULL)
- return BAD_FUNC_ARG;
-
- ret = AllocDer(&der, 0, DH_PARAM_TYPE, ctx->heap);
- if (ret != 0) {
- return ret;
- }
- der->buffer = (byte*)buf;
- der->length = (word32)sz;
-
- #ifdef WOLFSSL_SMALL_STACK
- p = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- g = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
-
- if (p == NULL || g == NULL) {
- XFREE(p, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(g, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- return MEMORY_E;
- }
- #endif
-
- if (format != WOLFSSL_FILETYPE_ASN1 && format != WOLFSSL_FILETYPE_PEM)
- ret = WOLFSSL_BAD_FILETYPE;
- else {
- if (format == WOLFSSL_FILETYPE_PEM) {
-#ifdef WOLFSSL_PEM_TO_DER
- FreeDer(&der);
- ret = PemToDer(buf, sz, DH_PARAM_TYPE, &der, ctx->heap,
- NULL, NULL);
- #ifdef WOLFSSL_WPAS
- #ifndef NO_DSA
- if (ret < 0) {
- ret = PemToDer(buf, sz, DSA_PARAM_TYPE, &der, ctx->heap,
- NULL, NULL);
- }
- #endif
- #endif /* WOLFSSL_WPAS */
-#else
- ret = NOT_COMPILED_IN;
-#endif /* WOLFSSL_PEM_TO_DER */
- }
-
- if (ret == 0) {
- if (wc_DhParamsLoad(der->buffer, der->length, p, &pSz, g, &gSz) < 0)
- ret = WOLFSSL_BAD_FILETYPE;
- else if (ssl)
- ret = wolfSSL_SetTmpDH(ssl, p, pSz, g, gSz);
- else
- ret = wolfSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz);
- }
- }
-
- FreeDer(&der);
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(p, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(g, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- #endif
-
- return ret;
- }
-
-
- /* server Diffie-Hellman parameters, WOLFSSL_SUCCESS on ok */
- int wolfSSL_SetTmpDH_buffer(WOLFSSL* ssl, const unsigned char* buf, long sz,
- int format)
- {
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return wolfSSL_SetTmpDH_buffer_wrapper(ssl->ctx, ssl, buf, sz, format);
- }
-
-
- /* server ctx Diffie-Hellman parameters, WOLFSSL_SUCCESS on ok */
- int wolfSSL_CTX_SetTmpDH_buffer(WOLFSSL_CTX* ctx, const unsigned char* buf,
- long sz, int format)
- {
- return wolfSSL_SetTmpDH_buffer_wrapper(ctx, NULL, buf, sz, format);
- }
-
-#endif /* NO_DH */
-
-
- int wolfSSL_use_certificate_buffer(WOLFSSL* ssl,
- const unsigned char* in, long sz, int format)
- {
- WOLFSSL_ENTER("wolfSSL_use_certificate_buffer");
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return ProcessBuffer(ssl->ctx, in, sz, format, CERT_TYPE, ssl, NULL, 0,
- GET_VERIFY_SETTING_SSL(ssl));
- }
-
-
- int wolfSSL_use_PrivateKey_buffer(WOLFSSL* ssl,
- const unsigned char* in, long sz, int format)
- {
- WOLFSSL_ENTER("wolfSSL_use_PrivateKey_buffer");
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return ProcessBuffer(ssl->ctx, in, sz, format, PRIVATEKEY_TYPE,
- ssl, NULL, 0, GET_VERIFY_SETTING_SSL(ssl));
- }
-
-#ifdef HAVE_PKCS11
- int wolfSSL_use_PrivateKey_id(WOLFSSL* ssl, const unsigned char* id,
- long sz, int devId, long keySz)
- {
- int ret = WOLFSSL_FAILURE;
-
- if (ssl->buffers.weOwnKey)
- FreeDer(&ssl->buffers.key);
- if (AllocDer(&ssl->buffers.key, (word32)sz, PRIVATEKEY_TYPE,
- ssl->heap) == 0) {
- XMEMCPY(ssl->buffers.key->buffer, id, sz);
- ssl->buffers.weOwnKey = 1;
- ssl->buffers.keyId = 1;
- ssl->buffers.keySz = (word32)keySz;
- if (devId != INVALID_DEVID)
- ssl->buffers.keyDevId = devId;
- else
- ssl->buffers.keyDevId = ssl->devId;
-
- ret = WOLFSSL_SUCCESS;
- }
-
- return ret;
- }
-#endif
-
- int wolfSSL_use_certificate_chain_buffer_format(WOLFSSL* ssl,
- const unsigned char* in, long sz, int format)
- {
- WOLFSSL_ENTER("wolfSSL_use_certificate_chain_buffer_format");
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- return ProcessBuffer(ssl->ctx, in, sz, format, CERT_TYPE,
- ssl, NULL, 1, GET_VERIFY_SETTING_SSL(ssl));
- }
-
- int wolfSSL_use_certificate_chain_buffer(WOLFSSL* ssl,
- const unsigned char* in, long sz)
- {
- return wolfSSL_use_certificate_chain_buffer_format(ssl, in, sz,
- WOLFSSL_FILETYPE_PEM);
- }
-
-
- /* unload any certs or keys that SSL owns, leave CTX as is
- WOLFSSL_SUCCESS on ok */
- int wolfSSL_UnloadCertsKeys(WOLFSSL* ssl)
- {
- if (ssl == NULL) {
- WOLFSSL_MSG("Null function arg");
- return BAD_FUNC_ARG;
- }
-
- if (ssl->buffers.weOwnCert && !ssl->keepCert) {
- WOLFSSL_MSG("Unloading cert");
- FreeDer(&ssl->buffers.certificate);
- #ifdef KEEP_OUR_CERT
- FreeX509(ssl->ourCert);
- if (ssl->ourCert) {
- XFREE(ssl->ourCert, ssl->heap, DYNAMIC_TYPE_X509);
- ssl->ourCert = NULL;
- }
- #endif
- ssl->buffers.weOwnCert = 0;
- }
-
- if (ssl->buffers.weOwnCertChain) {
- WOLFSSL_MSG("Unloading cert chain");
- FreeDer(&ssl->buffers.certChain);
- ssl->buffers.weOwnCertChain = 0;
- }
-
- if (ssl->buffers.weOwnKey) {
- WOLFSSL_MSG("Unloading key");
- FreeDer(&ssl->buffers.key);
- ssl->buffers.weOwnKey = 0;
- }
-
- return WOLFSSL_SUCCESS;
- }
-
-
- int wolfSSL_CTX_UnloadCAs(WOLFSSL_CTX* ctx)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_UnloadCAs");
-
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- return wolfSSL_CertManagerUnloadCAs(ctx->cm);
- }
-
-
-#ifdef WOLFSSL_TRUST_PEER_CERT
- int wolfSSL_CTX_Unload_trust_peers(WOLFSSL_CTX* ctx)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_Unload_trust_peers");
-
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- return wolfSSL_CertManagerUnload_trust_peers(ctx->cm);
- }
-#endif /* WOLFSSL_TRUST_PEER_CERT */
-/* old NO_FILESYSTEM end */
-#endif /* !NO_CERTS */
-
-
-#ifdef OPENSSL_EXTRA
-
- int wolfSSL_add_all_algorithms(void)
- {
- WOLFSSL_ENTER("wolfSSL_add_all_algorithms");
- if (wolfSSL_Init() == WOLFSSL_SUCCESS)
- return WOLFSSL_SUCCESS;
- else
- return WOLFSSL_FATAL_ERROR;
- }
-
- int wolfSSL_OpenSSL_add_all_algorithms_noconf(void)
- {
- WOLFSSL_ENTER("wolfSSL_OpenSSL_add_all_algorithms_noconf");
-
- if (wolfSSL_add_all_algorithms() == WOLFSSL_FATAL_ERROR)
- return WOLFSSL_FATAL_ERROR;
-
- return WOLFSSL_SUCCESS;
- }
-
- int wolfSSL_OpenSSL_add_all_algorithms_conf(void)
- {
- WOLFSSL_ENTER("wolfSSL_OpenSSL_add_all_algorithms_conf");
- /* This function is currently the same as
- wolfSSL_OpenSSL_add_all_algorithms_noconf since we do not employ
- the use of a wolfssl.cnf type configuration file and is only used for
- OpenSSL compatability. */
-
- if (wolfSSL_add_all_algorithms() == WOLFSSL_FATAL_ERROR) {
- return WOLFSSL_FATAL_ERROR;
- }
- return WOLFSSL_SUCCESS;
- }
-
- /* returns previous set cache size which stays constant */
- long wolfSSL_CTX_sess_set_cache_size(WOLFSSL_CTX* ctx, long sz)
- {
- /* cache size fixed at compile time in wolfSSL */
- (void)ctx;
- (void)sz;
- WOLFSSL_MSG("session cache is set at compile time");
- #ifndef NO_SESSION_CACHE
- return (long)(SESSIONS_PER_ROW * SESSION_ROWS);
- #else
- return 0;
- #endif
- }
-
-#endif
-
-#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
- void wolfSSL_CTX_set_quiet_shutdown(WOLFSSL_CTX* ctx, int mode)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_set_quiet_shutdown");
- if (mode)
- ctx->quietShutdown = 1;
- }
-
-
- void wolfSSL_set_quiet_shutdown(WOLFSSL* ssl, int mode)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_set_quiet_shutdown");
- if (mode)
- ssl->options.quietShutdown = 1;
- }
-#endif
-
-#ifdef OPENSSL_EXTRA
- void wolfSSL_set_bio(WOLFSSL* ssl, WOLFSSL_BIO* rd, WOLFSSL_BIO* wr)
- {
- WOLFSSL_ENTER("wolfSSL_set_bio");
-
- if (ssl == NULL) {
- WOLFSSL_MSG("Bad argument, ssl was NULL");
- return;
- }
-
- /* if WOLFSSL_BIO is socket type then set WOLFSSL socket to use */
- if (rd != NULL && rd->type == WOLFSSL_BIO_SOCKET) {
- wolfSSL_set_rfd(ssl, rd->num);
- }
- if (wr != NULL && wr->type == WOLFSSL_BIO_SOCKET) {
- wolfSSL_set_wfd(ssl, wr->num);
- }
-
- /* free any existing WOLFSSL_BIOs in use */
- if (ssl->biord != NULL) {
- if (ssl->biord != ssl->biowr) {
- if (ssl->biowr != NULL) {
- wolfSSL_BIO_free(ssl->biowr);
- ssl->biowr = NULL;
- }
- }
- wolfSSL_BIO_free(ssl->biord);
- ssl->biord = NULL;
- }
-
-
- ssl->biord = rd;
- ssl->biowr = wr;
-
- /* set SSL to use BIO callbacks instead */
- if (((ssl->cbioFlag & WOLFSSL_CBIO_RECV) == 0) &&
- (rd != NULL && rd->type != WOLFSSL_BIO_SOCKET)) {
- ssl->CBIORecv = BioReceive;
- }
- if (((ssl->cbioFlag & WOLFSSL_CBIO_SEND) == 0) &&
- (wr != NULL && wr->type != WOLFSSL_BIO_SOCKET)) {
- ssl->CBIOSend = BioSend;
- }
-
- /* User programs should always retry reading from these BIOs */
- if (rd) {
- /* User writes to rd */
- BIO_set_retry_write(rd);
- }
- if (wr) {
- /* User reads from wr */
- BIO_set_retry_read(wr);
- }
- }
-#endif
-
-#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA) || defined(HAVE_WEBSERVER)
- void wolfSSL_CTX_set_client_CA_list(WOLFSSL_CTX* ctx,
- WOLF_STACK_OF(WOLFSSL_X509_NAME)* names)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_set_client_CA_list");
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
- if (ctx != NULL)
- ctx->ca_names = names;
- #else
- (void)ctx;
- (void)names;
- #endif
- }
-
-
- /* returns the CA's set on server side or the CA's sent from server when
- * on client side */
-#if defined(SESSION_CERTS) && defined(OPENSSL_ALL)
- WOLF_STACK_OF(WOLFSSL_X509_NAME)* wolfSSL_get_client_CA_list(
- const WOLFSSL* ssl)
- {
- WOLFSSL_ENTER("wolfSSL_get_client_CA_list");
-
- if (ssl == NULL) {
- WOLFSSL_MSG("Bad argument passed to wolfSSL_get_client_CA_list");
- return NULL;
- }
-
- /* return list of CAs sent from the server */
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- WOLF_STACK_OF(WOLFSSL_X509)* sk;
-
- sk = wolfSSL_get_peer_cert_chain(ssl);
- if (sk != NULL) {
- WOLF_STACK_OF(WOLFSSL_X509_NAME)* ret;
- WOLFSSL_X509* x509;
-
- ret = wolfSSL_sk_X509_NAME_new(NULL);
- do {
- x509 = wolfSSL_sk_X509_pop(sk);
- if (x509 != NULL) {
- if (wolfSSL_X509_get_isCA(x509)) {
- if (wolfSSL_sk_X509_NAME_push(ret,
- wolfSSL_X509_get_subject_name(x509)) != 0) {
- WOLFSSL_MSG("Error pushing X509 name to stack");
- /* continue on to try other certificates and
- * do not fail out here */
- }
- }
- wolfSSL_X509_free(x509);
- }
- } while (x509 != NULL);
- wolfSSL_sk_X509_free(sk);
- return ret;
- }
- return NULL;
- }
- else {
- /* currently only can be set in the CTX */
- return ssl->ctx->ca_names;
- }
- }
-#endif /* SESSION_CERTS */
-
-
- #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
- defined(WOLFSSL_NGINX) || defined (WOLFSSL_HAPROXY)
- /* registers client cert callback, called during handshake if server
- requests client auth but user has not loaded client cert/key */
- void wolfSSL_CTX_set_client_cert_cb(WOLFSSL_CTX *ctx, client_cert_cb cb)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_set_client_cert_cb");
-
- if (ctx != NULL) {
- ctx->CBClientCert = cb;
- }
- }
- #endif /* OPENSSL_ALL || OPENSSL_EXTRA || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
-
-#endif /* OPENSSL_EXTRA || WOLFSSL_EXTRA || HAVE_WEBSERVER */
-
-#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
- WOLF_STACK_OF(WOLFSSL_X509_NAME)* wolfSSL_CTX_get_client_CA_list(
- const WOLFSSL_CTX *s)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_get_client_CA_list");
-
- if (s == NULL)
- return NULL;
-
- return s->ca_names;
- }
-#endif
-
-#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- #if !defined(NO_RSA) && !defined(NO_CERTS)
- WOLF_STACK_OF(WOLFSSL_X509_NAME)* wolfSSL_load_client_CA_file(const char* fname)
- {
- /* The webserver build is using this to load a CA into the server
- * for client authentication as an option. Have this return NULL in
- * that case. If OPENSSL_EXTRA is enabled, go ahead and include
- * the function. */
- #ifdef OPENSSL_EXTRA
- WOLFSSL_STACK *list = NULL;
- WOLFSSL_STACK *node;
- WOLFSSL_BIO* bio;
- WOLFSSL_X509 *cert = NULL;
- WOLFSSL_X509_NAME *subjectName = NULL;
- unsigned long err;
-
- WOLFSSL_ENTER("wolfSSL_load_client_CA_file");
-
- bio = wolfSSL_BIO_new_file(fname, "r");
- if (bio == NULL)
- return NULL;
-
- /* Read each certificate in the chain out of the file. */
- while (wolfSSL_PEM_read_bio_X509(bio, &cert, NULL, NULL) != NULL) {
- subjectName = wolfSSL_X509_get_subject_name(cert);
- if (subjectName == NULL)
- break;
-
- node = wolfSSL_sk_new_node(NULL);
- if (node == NULL)
- break;
- node->type = STACK_TYPE_X509_NAME;
-
- /* Need a persistent copy of the subject name. */
- node->data.name = wolfSSL_X509_NAME_dup(subjectName);
- /*
- * Original cert will be freed so make sure not to try to access
- * it in the future.
- */
- node->data.name->x509 = NULL;
-
- /* Put node on the front of the list. */
- node->num = (list == NULL) ? 1 : list->num + 1;
- node->next = list;
- list = node;
-
- wolfSSL_X509_free(cert);
- cert = NULL;
- }
-
- err = wolfSSL_ERR_peek_last_error();
-
- if (ERR_GET_LIB(err) == ERR_LIB_PEM &&
- ERR_GET_REASON(err) == PEM_R_NO_START_LINE) {
- /*
- * wolfSSL_PEM_read_bio_X509 pushes an ASN_NO_PEM_HEADER error
- * to the error queue on file end. This should not be left
- * for the caller to find so we clear the last error.
- */
- wc_RemoveErrorNode(-1);
- }
-
- wolfSSL_X509_free(cert);
- wolfSSL_BIO_free(bio);
- return list;
- #else
- (void)fname;
- return NULL;
- #endif
- }
- #endif
-#endif
-
-#ifdef OPENSSL_EXTRA
- #if !defined(NO_RSA) && !defined(NO_CERTS)
- int wolfSSL_CTX_add_client_CA(WOLFSSL_CTX* ctx, WOLFSSL_X509* x509)
- {
- WOLFSSL_STACK *node = NULL;
- WOLFSSL_X509_NAME *subjectName = NULL;
-
- WOLFSSL_ENTER("wolfSSL_CTX_add_client_CA");
-
- if (ctx == NULL || x509 == NULL){
- WOLFSSL_MSG("Bad argument");
- return SSL_FAILURE;
- }
-
- subjectName = wolfSSL_X509_get_subject_name(x509);
- if (subjectName == NULL){
- WOLFSSL_MSG("invalid x509 data");
- return SSL_FAILURE;
- }
-
- /* Alloc stack struct */
- node = (WOLF_STACK_OF(WOLFSSL_X509_NAME)*)XMALLOC(
- sizeof(WOLF_STACK_OF(WOLFSSL_X509_NAME)),
- NULL, DYNAMIC_TYPE_OPENSSL);
- if (node == NULL){
- WOLFSSL_MSG("memory allocation error");
- return SSL_FAILURE;
- }
- XMEMSET(node, 0, sizeof(WOLF_STACK_OF(WOLFSSL_X509_NAME)));
-
- /* Alloc and copy WOLFSSL_X509_NAME */
- node->data.name = (WOLFSSL_X509_NAME*)XMALLOC(
- sizeof(WOLFSSL_X509_NAME),
- NULL, DYNAMIC_TYPE_OPENSSL);
- if (node->data.name == NULL) {
- XFREE(node, NULL, DYNAMIC_TYPE_OPENSSL);
- WOLFSSL_MSG("memory allocation error");
- return SSL_FAILURE;
- }
- XMEMCPY(node->data.name, subjectName, sizeof(WOLFSSL_X509_NAME));
- XMEMSET(subjectName, 0, sizeof(WOLFSSL_X509_NAME));
-
- /* push new node onto head of stack */
- node->num = (ctx->ca_names == NULL) ? 1 : ctx->ca_names->num + 1;
- node->next = ctx->ca_names;
- ctx->ca_names = node;
- return SSL_SUCCESS;
- }
- #endif
-
- #ifndef NO_WOLFSSL_STUB
- int wolfSSL_CTX_set_default_verify_paths(WOLFSSL_CTX* ctx)
- {
- /* TODO:, not needed in goahead */
- (void)ctx;
- WOLFSSL_STUB("SSL_CTX_set_default_verify_paths");
- return SSL_NOT_IMPLEMENTED;
- }
- #endif
-
- #if defined(WOLFCRYPT_HAVE_SRP) && !defined(NO_SHA256) \
- && !defined(WC_NO_RNG)
- static const byte srp_N[] = {
- 0xEE, 0xAF, 0x0A, 0xB9, 0xAD, 0xB3, 0x8D, 0xD6, 0x9C, 0x33, 0xF8,
- 0x0A, 0xFA, 0x8F, 0xC5, 0xE8, 0x60, 0x72, 0x61, 0x87, 0x75, 0xFF,
- 0x3C, 0x0B, 0x9E, 0xA2, 0x31, 0x4C, 0x9C, 0x25, 0x65, 0x76, 0xD6,
- 0x74, 0xDF, 0x74, 0x96, 0xEA, 0x81, 0xD3, 0x38, 0x3B, 0x48, 0x13,
- 0xD6, 0x92, 0xC6, 0xE0, 0xE0, 0xD5, 0xD8, 0xE2, 0x50, 0xB9, 0x8B,
- 0xE4, 0x8E, 0x49, 0x5C, 0x1D, 0x60, 0x89, 0xDA, 0xD1, 0x5D, 0xC7,
- 0xD7, 0xB4, 0x61, 0x54, 0xD6, 0xB6, 0xCE, 0x8E, 0xF4, 0xAD, 0x69,
- 0xB1, 0x5D, 0x49, 0x82, 0x55, 0x9B, 0x29, 0x7B, 0xCF, 0x18, 0x85,
- 0xC5, 0x29, 0xF5, 0x66, 0x66, 0x0E, 0x57, 0xEC, 0x68, 0xED, 0xBC,
- 0x3C, 0x05, 0x72, 0x6C, 0xC0, 0x2F, 0xD4, 0xCB, 0xF4, 0x97, 0x6E,
- 0xAA, 0x9A, 0xFD, 0x51, 0x38, 0xFE, 0x83, 0x76, 0x43, 0x5B, 0x9F,
- 0xC6, 0x1D, 0x2F, 0xC0, 0xEB, 0x06, 0xE3
- };
- static const byte srp_g[] = {
- 0x02
- };
-
- int wolfSSL_CTX_set_srp_username(WOLFSSL_CTX* ctx, char* username)
- {
- int r = 0;
- SrpSide srp_side = SRP_CLIENT_SIDE;
- WC_RNG rng;
- byte salt[SRP_SALT_SIZE];
-
- WOLFSSL_ENTER("wolfSSL_CTX_set_srp_username");
- if (ctx == NULL || ctx->srp == NULL || username==NULL)
- return SSL_FAILURE;
-
- if (ctx->method->side == WOLFSSL_SERVER_END){
- srp_side = SRP_SERVER_SIDE;
- } else if (ctx->method->side == WOLFSSL_CLIENT_END){
- srp_side = SRP_CLIENT_SIDE;
- } else {
- WOLFSSL_MSG("Init CTX failed");
- return SSL_FAILURE;
- }
-
- if (wc_SrpInit(ctx->srp, SRP_TYPE_SHA256, srp_side) < 0){
- WOLFSSL_MSG("Init CTX failed");
- XFREE(ctx->srp, ctx->heap, DYNAMIC_TYPE_SRP);
- wolfSSL_CTX_free(ctx);
- return SSL_FAILURE;
- }
- r = wc_SrpSetUsername(ctx->srp, (const byte*)username,
- (word32)XSTRLEN(username));
- if (r < 0) {
- WOLFSSL_MSG("fail to set srp username.");
- return SSL_FAILURE;
- }
-
- /* if wolfSSL_CTX_set_srp_password has already been called, */
- /* execute wc_SrpSetPassword here */
- if (ctx->srp_password != NULL){
- if (wc_InitRng(&rng) < 0){
- WOLFSSL_MSG("wc_InitRng failed");
- return SSL_FAILURE;
- }
- XMEMSET(salt, 0, sizeof(salt)/sizeof(salt[0]));
- if (wc_RNG_GenerateBlock(&rng, salt,
- sizeof(salt)/sizeof(salt[0])) < 0){
- WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
- wc_FreeRng(&rng);
- return SSL_FAILURE;
- }
- if (wc_SrpSetParams(ctx->srp, srp_N, sizeof(srp_N)/sizeof(srp_N[0]),
- srp_g, sizeof(srp_g)/sizeof(srp_g[0]),
- salt, sizeof(salt)/sizeof(salt[0])) < 0){
- WOLFSSL_MSG("wc_SrpSetParam failed");
- wc_FreeRng(&rng);
- return SSL_FAILURE;
- }
- r = wc_SrpSetPassword(ctx->srp,
- (const byte*)ctx->srp_password,
- (word32)XSTRLEN((char *)ctx->srp_password));
- if (r < 0) {
- WOLFSSL_MSG("fail to set srp password.");
- return SSL_FAILURE;
- }
- wc_FreeRng(&rng);
- XFREE(ctx->srp_password, ctx->heap, DYNAMIC_TYPE_SRP);
- ctx->srp_password = NULL;
- }
-
- return SSL_SUCCESS;
- }
-
- int wolfSSL_CTX_set_srp_password(WOLFSSL_CTX* ctx, char* password)
- {
- int r;
- WC_RNG rng;
- byte salt[SRP_SALT_SIZE];
-
- WOLFSSL_ENTER("wolfSSL_CTX_set_srp_password");
- if (ctx == NULL || ctx->srp == NULL || password == NULL)
- return SSL_FAILURE;
-
- if (ctx->srp->user != NULL){
- if (wc_InitRng(&rng) < 0){
- WOLFSSL_MSG("wc_InitRng failed");
- return SSL_FAILURE;
- }
- XMEMSET(salt, 0, sizeof(salt)/sizeof(salt[0]));
- if (wc_RNG_GenerateBlock(&rng, salt,
- sizeof(salt)/sizeof(salt[0])) < 0){
- WOLFSSL_MSG("wc_RNG_GenerateBlock failed");
- wc_FreeRng(&rng);
- return SSL_FAILURE;
- }
- if (wc_SrpSetParams(ctx->srp, srp_N, sizeof(srp_N)/sizeof(srp_N[0]),
- srp_g, sizeof(srp_g)/sizeof(srp_g[0]),
- salt, sizeof(salt)/sizeof(salt[0])) < 0){
- WOLFSSL_MSG("wc_SrpSetParam failed");
- wc_FreeRng(&rng);
- return SSL_FAILURE;
- }
- r = wc_SrpSetPassword(ctx->srp, (const byte*)password,
- (word32)XSTRLEN(password));
- if (r < 0) {
- WOLFSSL_MSG("wc_SrpSetPassword failed.");
- wc_FreeRng(&rng);
- return SSL_FAILURE;
- }
- if (ctx->srp_password != NULL){
- XFREE(ctx->srp_password,NULL,
- DYNAMIC_TYPE_SRP);
- ctx->srp_password = NULL;
- }
- wc_FreeRng(&rng);
- } else {
- /* save password for wolfSSL_set_srp_username */
- if (ctx->srp_password != NULL)
- XFREE(ctx->srp_password,ctx->heap, DYNAMIC_TYPE_SRP);
-
- ctx->srp_password = (byte*)XMALLOC(XSTRLEN(password) + 1, ctx->heap,
- DYNAMIC_TYPE_SRP);
- if (ctx->srp_password == NULL){
- WOLFSSL_MSG("memory allocation error");
- return SSL_FAILURE;
- }
- XMEMCPY(ctx->srp_password, password, XSTRLEN(password) + 1);
- }
- return SSL_SUCCESS;
- }
- #endif /* WOLFCRYPT_HAVE_SRP && !NO_SHA256 && !WC_NO_RNG */
-
- /* keyblock size in bytes or -1 */
- int wolfSSL_get_keyblock_size(WOLFSSL* ssl)
- {
- if (ssl == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- return 2 * (ssl->specs.key_size + ssl->specs.iv_size +
- ssl->specs.hash_size);
- }
-
-
- /* store keys returns WOLFSSL_SUCCESS or -1 on error */
- int wolfSSL_get_keys(WOLFSSL* ssl, unsigned char** ms, unsigned int* msLen,
- unsigned char** sr, unsigned int* srLen,
- unsigned char** cr, unsigned int* crLen)
- {
- if (ssl == NULL || ssl->arrays == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- *ms = ssl->arrays->masterSecret;
- *sr = ssl->arrays->serverRandom;
- *cr = ssl->arrays->clientRandom;
-
- *msLen = SECRET_LEN;
- *srLen = RAN_LEN;
- *crLen = RAN_LEN;
-
- return WOLFSSL_SUCCESS;
- }
-
-#endif /* OPENSSL_EXTRA */
-
-#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
- void wolfSSL_set_accept_state(WOLFSSL* ssl)
- {
- WOLFSSL_ENTER("wolfSSL_set_accept_state");
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- #ifdef HAVE_ECC
- ecc_key key;
- word32 idx = 0;
-
- if (ssl->options.haveStaticECC && ssl->buffers.key != NULL) {
- if (wc_ecc_init(&key) >= 0) {
- if (wc_EccPrivateKeyDecode(ssl->buffers.key->buffer, &idx, &key,
- ssl->buffers.key->length) != 0) {
- ssl->options.haveECDSAsig = 0;
- ssl->options.haveECC = 0;
- ssl->options.haveStaticECC = 0;
- }
- wc_ecc_free(&key);
- }
- }
- #endif
-
- #ifndef NO_DH
- if (!ssl->options.haveDH && ssl->ctx->haveDH) {
- ssl->buffers.serverDH_P = ssl->ctx->serverDH_P;
- ssl->buffers.serverDH_G = ssl->ctx->serverDH_G;
- ssl->options.haveDH = 1;
- }
- #endif
- }
-
- if (InitSSL_Side(ssl, WOLFSSL_SERVER_END) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error initializing server side");
- }
- }
-
-#endif /* OPENSSL_EXTRA || WOLFSSL_EXTRA */
-
- /* return true if connection established */
- int wolfSSL_is_init_finished(WOLFSSL* ssl)
- {
- if (ssl == NULL)
- return 0;
-
- if (ssl->options.handShakeState == HANDSHAKE_DONE)
- return 1;
-
- return 0;
- }
-
-#ifdef OPENSSL_EXTRA
-
- void wolfSSL_CTX_set_tmp_rsa_callback(WOLFSSL_CTX* ctx,
- WOLFSSL_RSA*(*f)(WOLFSSL*, int, int))
- {
- /* wolfSSL verifies all these internally */
- (void)ctx;
- (void)f;
- }
-
-
- void wolfSSL_set_shutdown(WOLFSSL* ssl, int opt)
- {
- WOLFSSL_ENTER("wolfSSL_set_shutdown");
- if(ssl==NULL) {
- WOLFSSL_MSG("Shutdown not set. ssl is null");
- return;
- }
-
- ssl->options.sentNotify = (opt&WOLFSSL_SENT_SHUTDOWN) > 0;
- ssl->options.closeNotify = (opt&WOLFSSL_RECEIVED_SHUTDOWN) > 0;
- }
-
-
- long wolfSSL_CTX_get_options(WOLFSSL_CTX* ctx)
- {
- WOLFSSL_ENTER("wolfSSL_CTX_get_options");
- WOLFSSL_MSG("wolfSSL options are set through API calls and macros");
- if(ctx == NULL)
- return BAD_FUNC_ARG;
- return ctx->mask;
- }
-
-#endif
-
-#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
-
- static long wolf_set_options(long old_op, long op);
- long wolfSSL_CTX_set_options(WOLFSSL_CTX* ctx, long opt)
- {
- WOLFSSL_ENTER("SSL_CTX_set_options");
-
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- ctx->mask = wolf_set_options(ctx->mask, opt);
-
- return ctx->mask;
- }
-
-#endif
-
-#ifdef OPENSSL_EXTRA
-
- long wolfSSL_CTX_clear_options(WOLFSSL_CTX* ctx, long opt)
- {
- WOLFSSL_ENTER("SSL_CTX_clear_options");
- if(ctx == NULL)
- return BAD_FUNC_ARG;
- ctx->mask &= ~opt;
- return ctx->mask;
- }
-
- int wolfSSL_set_rfd(WOLFSSL* ssl, int rfd)
- {
- WOLFSSL_ENTER("SSL_set_rfd");
- ssl->rfd = rfd; /* not used directly to allow IO callbacks */
-
- ssl->IOCB_ReadCtx = &ssl->rfd;
-
- return WOLFSSL_SUCCESS;
- }
-
-
- int wolfSSL_set_wfd(WOLFSSL* ssl, int wfd)
- {
- WOLFSSL_ENTER("SSL_set_wfd");
- ssl->wfd = wfd; /* not used directly to allow IO callbacks */
-
- ssl->IOCB_WriteCtx = &ssl->wfd;
-
- return WOLFSSL_SUCCESS;
- }
-
-#ifndef NO_CERTS
-
- WOLFSSL_X509_STORE* wolfSSL_CTX_get_cert_store(WOLFSSL_CTX* ctx)
- {
- if (ctx == NULL) {
- return NULL;
- }
-
- return &ctx->x509_store;
- }
-
- void wolfSSL_CTX_set_cert_store(WOLFSSL_CTX* ctx, WOLFSSL_X509_STORE* str)
- {
- if (ctx == NULL || str == NULL) {
- return;
- }
-
- /* free cert manager if have one */
- if (ctx->cm != NULL) {
- wolfSSL_CertManagerFree(ctx->cm);
- }
- ctx->cm = str->cm;
-
- /* free existing store if it exists */
- if (ctx->x509_store_pt != NULL) {
- /* cert manager was free'd a little earlier in this function */
- ctx->x509_store_pt->cm = NULL;
- }
- wolfSSL_X509_STORE_free(ctx->x509_store_pt);
- ctx->x509_store.cache = str->cache;
- ctx->x509_store_pt = str; /* take ownership of store and free it
- with CTX free */
- }
-
-
- WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get_current_cert(
- WOLFSSL_X509_STORE_CTX* ctx)
- {
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_current_cert");
- if (ctx)
- return ctx->current_cert;
- return NULL;
- }
-
-
- int wolfSSL_X509_STORE_CTX_get_error(WOLFSSL_X509_STORE_CTX* ctx)
- {
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error");
- if (ctx != NULL)
- return ctx->error;
- return 0;
- }
-
-
- int wolfSSL_X509_STORE_CTX_get_error_depth(WOLFSSL_X509_STORE_CTX* ctx)
- {
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error_depth");
- if(ctx)
- return ctx->error_depth;
- return WOLFSSL_FATAL_ERROR;
- }
-
- void wolfSSL_X509_STORE_CTX_set_verify_cb(WOLFSSL_X509_STORE_CTX *ctx,
- WOLFSSL_X509_STORE_CTX_verify_cb verify_cb)
- {
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_verify_cb");
- if(ctx == NULL)
- return;
- ctx->verify_cb = verify_cb;
- }
-
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- void wolfSSL_X509_STORE_set_verify_cb(WOLFSSL_X509_STORE *st,
- WOLFSSL_X509_STORE_CTX_verify_cb verify_cb)
- {
- WOLFSSL_ENTER("WOLFSSL_X509_STORE_set_verify_cb");
- if (st != NULL) {
- st->verify_cb = verify_cb;
- }
- }
-#endif
-
-
-#endif /* !NO_CERTS */
-
- WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_md(void)
- {
- static WOLFSSL_BIO_METHOD meth;
-
- WOLFSSL_ENTER("wolfSSL_BIO_f_md");
- meth.type = WOLFSSL_BIO_MD;
-
- return &meth;
- }
-
- /* return the context and initialize the BIO state */
- int wolfSSL_BIO_get_md_ctx(WOLFSSL_BIO *bio, WOLFSSL_EVP_MD_CTX **mdcp)
- {
- int ret = WOLFSSL_FAILURE;
-
- if ((bio != NULL) && (mdcp != NULL)) {
- *mdcp = (WOLFSSL_EVP_MD_CTX*)bio->ptr;
- ret = WOLFSSL_SUCCESS;
- }
-
- return ret;
- }
-
- WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_buffer(void)
- {
- static WOLFSSL_BIO_METHOD meth;
-
- WOLFSSL_ENTER("BIO_f_buffer");
- meth.type = WOLFSSL_BIO_BUFFER;
-
- return &meth;
- }
-
- #ifndef NO_WOLFSSL_STUB
- long wolfSSL_BIO_set_write_buffer_size(WOLFSSL_BIO* bio, long size)
- {
- /* wolfSSL has internal buffer, compatibility only */
- WOLFSSL_ENTER("BIO_set_write_buffer_size");
- WOLFSSL_STUB("BIO_set_write_buffer_size");
- (void)bio;
- return size;
- }
- #endif
-
- WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_bio(void)
- {
- static WOLFSSL_BIO_METHOD bio_meth;
-
- WOLFSSL_ENTER("wolfSSL_BIO_s_bio");
- bio_meth.type = WOLFSSL_BIO_BIO;
-
- return &bio_meth;
- }
-
-
-#ifndef NO_FILESYSTEM
- WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_file(void)
- {
- static WOLFSSL_BIO_METHOD file_meth;
-
- WOLFSSL_ENTER("wolfSSL_BIO_s_file");
- file_meth.type = WOLFSSL_BIO_FILE;
-
- return &file_meth;
- }
-#endif
-
-
- WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_ssl(void)
- {
- static WOLFSSL_BIO_METHOD meth;
-
- WOLFSSL_ENTER("wolfSSL_BIO_f_ssl");
- meth.type = WOLFSSL_BIO_SSL;
-
- return &meth;
- }
-
-
- WOLFSSL_BIO_METHOD *wolfSSL_BIO_s_socket(void)
- {
- static WOLFSSL_BIO_METHOD meth;
-
- WOLFSSL_ENTER("wolfSSL_BIO_s_socket");
- meth.type = WOLFSSL_BIO_SOCKET;
-
- return &meth;
- }
-
-
- WOLFSSL_BIO* wolfSSL_BIO_new_socket(int sfd, int closeF)
- {
- WOLFSSL_BIO* bio = wolfSSL_BIO_new(wolfSSL_BIO_s_socket());
-
- WOLFSSL_ENTER("BIO_new_socket");
- if (bio) {
- bio->type = WOLFSSL_BIO_SOCKET;
- bio->shutdown = (byte)closeF;
- bio->num = sfd;
- }
- return bio;
- }
-
-
- int wolfSSL_BIO_eof(WOLFSSL_BIO* b)
- {
- WOLFSSL_ENTER("BIO_eof");
- if ((b != NULL) && (b->eof))
- return 1;
-
- return 0;
- }
-
-
- long wolfSSL_BIO_set_ssl(WOLFSSL_BIO* b, WOLFSSL* ssl, int closeF)
- {
- WOLFSSL_ENTER("wolfSSL_BIO_set_ssl");
-
- if (b != NULL) {
- b->ptr = ssl;
- b->shutdown = (byte)closeF;
- /* add to ssl for bio free if SSL_free called before/instead of free_all? */
- }
-
- return 0;
- }
-
-#ifndef NO_FILESYSTEM
- long wolfSSL_BIO_set_fd(WOLFSSL_BIO* b, int fd, int closeF)
- {
- WOLFSSL_ENTER("wolfSSL_BIO_set_fd");
-
- if (b != NULL) {
- b->num = fd;
- b->shutdown = (byte)closeF;
- }
-
- return WOLFSSL_SUCCESS;
- }
-#endif
-
- /* Sets the close flag */
- int wolfSSL_BIO_set_close(WOLFSSL_BIO *b, long flag)
- {
- WOLFSSL_ENTER("wolfSSL_BIO_set_close");
- if (b != NULL) {
- b->shutdown = (byte)flag;
- }
-
- return WOLFSSL_SUCCESS;
- }
-
-
- WOLFSSL_BIO* wolfSSL_BIO_new(WOLFSSL_BIO_METHOD* method)
- {
- WOLFSSL_BIO* bio;
-
- WOLFSSL_ENTER("wolfSSL_BIO_new");
- if (method == NULL) {
- WOLFSSL_MSG("Bad method pointer passed in");
- return NULL;
- }
-
- bio = (WOLFSSL_BIO*) XMALLOC(sizeof(WOLFSSL_BIO), 0,
- DYNAMIC_TYPE_OPENSSL);
- if (bio) {
- XMEMSET(bio, 0, sizeof(WOLFSSL_BIO));
- bio->type = (byte)method->type;
- bio->method = method;
- bio->shutdown = BIO_CLOSE; /* default to close things */
- bio->init = 1;
- if (method->type != WOLFSSL_BIO_FILE &&
- method->type != WOLFSSL_BIO_SOCKET &&
- method->type != WOLFSSL_BIO_MD) {
- bio->mem_buf =(WOLFSSL_BUF_MEM*)XMALLOC(sizeof(WOLFSSL_BUF_MEM),
- 0, DYNAMIC_TYPE_OPENSSL);
- if (bio->mem_buf == NULL) {
- WOLFSSL_MSG("Memory error");
- wolfSSL_BIO_free(bio);
- return NULL;
- }
- bio->mem_buf->data = (char*)bio->ptr;
- }
-
- if (method->type == WOLFSSL_BIO_MD) {
- bio->ptr = wolfSSL_EVP_MD_CTX_new();
- if (bio->ptr == NULL) {
- WOLFSSL_MSG("Memory error");
- wolfSSL_BIO_free(bio);
- return NULL;
- }
- }
-
- /* check if is custom method */
- if (method->createCb) {
- method->createCb(bio);
- }
- }
- return bio;
- }
-
- WOLFSSL_BIO* wolfSSL_BIO_new_mem_buf(const void* buf, int len)
- {
- WOLFSSL_BIO* bio = NULL;
-
- if (buf == NULL || len < 0) {
- return bio;
- }
-
- bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
- if (bio == NULL) {
- return bio;
- }
-
- bio->num = bio->wrSz = len;
- bio->ptr = (byte*)XMALLOC(len, 0, DYNAMIC_TYPE_OPENSSL);
- if (bio->ptr == NULL) {
- wolfSSL_BIO_free(bio);
- return NULL;
- }
- if (bio->mem_buf != NULL) {
- bio->mem_buf->data = (char*)bio->ptr;
- bio->mem_buf->length = bio->num;
- }
-
- XMEMCPY(bio->ptr, buf, len);
-
- return bio;
- }
-
- /*
- * Note : If the flag BIO_NOCLOSE is set then freeing memory buffers is up
- * to the application.
- * Returns 1 on success, 0 on failure
- */
- int wolfSSL_BIO_free(WOLFSSL_BIO* bio)
- {
- int ret;
-
- /* unchain?, doesn't matter in goahead since from free all */
- WOLFSSL_ENTER("wolfSSL_BIO_free");
- if (bio) {
-
- if (bio->infoCb) {
- /* info callback is called before free */
- ret = (int)bio->infoCb(bio, WOLFSSL_BIO_CB_FREE, NULL, 0, 0, 1);
- if (ret <= 0) {
- return ret;
- }
- }
-
- /* call custom set free callback */
- if (bio->method && bio->method->freeCb) {
- bio->method->freeCb(bio);
- }
-
- /* remove from pair by setting the paired bios pair to NULL */
- if (bio->pair != NULL) {
- bio->pair->pair = NULL;
- }
-
- if (bio->shutdown) {
- if (bio->type == WOLFSSL_BIO_SSL && bio->ptr)
- wolfSSL_free((WOLFSSL*)bio->ptr);
- #ifdef CloseSocket
- if (bio->type == WOLFSSL_BIO_SOCKET && bio->num)
- CloseSocket(bio->num);
- #endif
- }
-
- #ifndef NO_FILESYSTEM
- if (bio->type == WOLFSSL_BIO_FILE && bio->shutdown == BIO_CLOSE) {
- if (bio->ptr) {
- XFCLOSE((XFILE)bio->ptr);
- }
- }
- #endif
-
- if (bio->shutdown != BIO_NOCLOSE) {
- if (bio->type == WOLFSSL_BIO_MEMORY && bio->ptr != NULL) {
- if (bio->mem_buf != NULL) {
- if (bio->mem_buf->data != (char*)bio->ptr) {
- XFREE(bio->ptr, bio->heap, DYNAMIC_TYPE_OPENSSL);
- bio->ptr = NULL;
- }
- }
- else {
- XFREE(bio->ptr, bio->heap, DYNAMIC_TYPE_OPENSSL);
- bio->ptr = NULL;
- }
- }
- if (bio->mem_buf != NULL) {
- wolfSSL_BUF_MEM_free(bio->mem_buf);
- bio->mem_buf = NULL;
- }
- }
-
- if (bio->type == WOLFSSL_BIO_MD) {
- wolfSSL_EVP_MD_CTX_free((WOLFSSL_EVP_MD_CTX*)bio->ptr);
- }
-
- XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL);
- }
- return 1;
- }
-
- /* like BIO_free, but no return value */
- void wolfSSL_BIO_vfree(WOLFSSL_BIO* bio)
- {
- wolfSSL_BIO_free(bio);
- }
-
-
- int wolfSSL_BIO_free_all(WOLFSSL_BIO* bio)
- {
- WOLFSSL_ENTER("BIO_free_all");
- while (bio) {
- WOLFSSL_BIO* next = bio->next;
- wolfSSL_BIO_free(bio);
- bio = next;
- }
- return 0;
- }
-
-
- WOLFSSL_BIO* wolfSSL_BIO_push(WOLFSSL_BIO* top, WOLFSSL_BIO* append)
- {
- WOLFSSL_ENTER("BIO_push");
- top->next = append;
- append->prev = top;
-
- return top;
- }
-#endif /* OPENSSL_EXTRA */
-
-#ifdef WOLFSSL_ENCRYPTED_KEYS
-
- void wolfSSL_CTX_set_default_passwd_cb_userdata(WOLFSSL_CTX* ctx,
- void* userdata)
- {
- WOLFSSL_ENTER("SSL_CTX_set_default_passwd_cb_userdata");
- if (ctx)
- ctx->passwd_userdata = userdata;
- }
-
-
- void wolfSSL_CTX_set_default_passwd_cb(WOLFSSL_CTX* ctx,pem_password_cb* cb)
- {
- WOLFSSL_ENTER("SSL_CTX_set_default_passwd_cb");
- if (ctx)
- ctx->passwd_cb = cb;
- }
-
- pem_password_cb* wolfSSL_CTX_get_default_passwd_cb(WOLFSSL_CTX *ctx)
- {
- if (ctx == NULL || ctx->passwd_cb == NULL) {
- return NULL;
- }
-
- return ctx->passwd_cb;
- }
-
-
- void* wolfSSL_CTX_get_default_passwd_cb_userdata(WOLFSSL_CTX *ctx)
- {
- if (ctx == NULL) {
- return NULL;
- }
-
- return ctx->passwd_userdata;
- }
-
-#endif /* WOLFSSL_ENCRYPTED_KEYS */
-
-
-#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- int wolfSSL_num_locks(void)
- {
- return 0;
- }
-
- void wolfSSL_set_locking_callback(void (*f)(int, int, const char*, int))
- {
- WOLFSSL_ENTER("wolfSSL_set_locking_callback");
-
- if (wc_SetMutexCb(f) != 0) {
- WOLFSSL_MSG("Error when setting mutex call back");
- }
- }
-
-
- typedef unsigned long (idCb)(void);
- static idCb* inner_idCb = NULL;
-
- unsigned long wolfSSL_thread_id(void)
- {
- if (inner_idCb != NULL) {
- return inner_idCb();
- }
- else {
- return 0;
- }
- }
-
-
- void wolfSSL_set_id_callback(unsigned long (*f)(void))
- {
- inner_idCb = f;
- }
-
- unsigned long wolfSSL_ERR_get_error(void)
- {
- WOLFSSL_ENTER("wolfSSL_ERR_get_error");
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
- {
- unsigned long ret = wolfSSL_ERR_peek_error_line_data(NULL, NULL,
- NULL, NULL);
- wc_RemoveErrorNode(-1);
- return ret;
- }
-#elif (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE))
- {
- int ret = wc_PullErrorNode(NULL, NULL, NULL);
-
- if (ret < 0) {
- if (ret == BAD_STATE_E) return 0; /* no errors in queue */
- WOLFSSL_MSG("Error with pulling error node!");
- WOLFSSL_LEAVE("wolfSSL_ERR_get_error", ret);
- ret = 0 - ret; /* return absolute value of error */
-
- /* panic and try to clear out nodes */
- wc_ClearErrorNodes();
- }
-
- return (unsigned long)ret;
- }
-#else
- return (unsigned long)(0 - NOT_COMPILED_IN);
-#endif
- }
-
-#if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE))
- /* print out and clear all errors */
- void wolfSSL_ERR_print_errors(WOLFSSL_BIO* bio)
- {
- const char* file = NULL;
- const char* reason = NULL;
- int ret;
- int line = 0;
- char buf[WOLFSSL_MAX_ERROR_SZ * 2];
-
- WOLFSSL_ENTER("wolfSSL_ERR_print_errors");
-
- if (bio == NULL) {
- WOLFSSL_MSG("BIO passed in was null");
- return;
- }
-
- do {
- ret = wc_PeekErrorNode(0, &file, &reason, &line);
- if (ret >= 0) {
- const char* r = wolfSSL_ERR_reason_error_string(0 - ret);
- XSNPRINTF(buf, sizeof(buf), "error:%d:wolfSSL library:%s:%s:%d\n",
- ret, r, file, line);
- wolfSSL_BIO_write(bio, buf, (int)XSTRLEN(buf));
- wc_RemoveErrorNode(0);
- }
- } while (ret >= 0);
- }
-#endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
-
-#endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
-
-#ifdef OPENSSL_EXTRA
-
-#if !defined(NO_WOLFSSL_SERVER)
-size_t wolfSSL_get_server_random(const WOLFSSL *ssl, unsigned char *out,
- size_t outSz)
-{
- size_t size;
-
- /* return max size of buffer */
- if (outSz == 0) {
- return RAN_LEN;
- }
-
- if (ssl == NULL || out == NULL) {
- return 0;
- }
-
- if (ssl->options.saveArrays == 0 || ssl->arrays == NULL) {
- WOLFSSL_MSG("Arrays struct not saved after handshake");
- return 0;
- }
-
- if (outSz > RAN_LEN) {
- size = RAN_LEN;
- }
- else {
- size = outSz;
- }
-
- XMEMCPY(out, ssl->arrays->serverRandom, size);
- return size;
-}
-
-
-/* Used to get the peer ephemeral public key sent during the connection
- * NOTE: currently wolfSSL_KeepHandshakeResources(WOLFSSL* ssl) must be called
- * before the ephemeral key is stored.
- * return WOLFSSL_SUCCESS on success */
-int wolfSSL_get_server_tmp_key(const WOLFSSL* ssl, WOLFSSL_EVP_PKEY** pkey)
-{
- WOLFSSL_EVP_PKEY* ret = NULL;
-
- WOLFSSL_ENTER("wolfSSL_get_server_tmp_key");
-
- if (ssl == NULL || pkey == NULL) {
- WOLFSSL_MSG("Bad argument passed in");
- return WOLFSSL_FAILURE;
- }
-
-#ifdef HAVE_ECC
- if (ssl->peerEccKey != NULL) {
- unsigned char* der;
- const unsigned char* pt;
- unsigned int derSz = 0;
- int sz;
-
- if (wc_ecc_export_x963(ssl->peerEccKey, NULL, &derSz) !=
- LENGTH_ONLY_E) {
- WOLFSSL_MSG("get ecc der size failed");
- return WOLFSSL_FAILURE;
- }
-
- derSz += MAX_SEQ_SZ + (2 * MAX_ALGO_SZ) + MAX_SEQ_SZ + TRAILING_ZERO;
- der = (unsigned char*)XMALLOC(derSz, ssl->heap, DYNAMIC_TYPE_KEY);
- if (der == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
-
- if ((sz = wc_EccPublicKeyToDer(ssl->peerEccKey, der, derSz, 1)) <= 0) {
- WOLFSSL_MSG("get ecc der failed");
- XFREE(der, ssl->heap, DYNAMIC_TYPE_KEY);
- return WOLFSSL_FAILURE;
- }
- pt = der; /* in case pointer gets advanced */
- ret = wolfSSL_d2i_PUBKEY(NULL, &pt, sz);
- XFREE(der, ssl->heap, DYNAMIC_TYPE_KEY);
- }
-#endif
-
- *pkey = ret;
- if (ret == NULL)
- return WOLFSSL_FAILURE;
- else
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* !NO_WOLFSSL_SERVER */
-
-int wolfSSL_CTX_set_min_proto_version(WOLFSSL_CTX* ctx, int version)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_set_min_proto_version");
-
- if (ctx == NULL) {
- return BAD_FUNC_ARG;
- }
-
- switch (version) {
-#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
- case SSL3_VERSION:
- ctx->minDowngrade = SSLv3_MINOR;
- break;
-#endif
-#ifndef NO_TLS
- #ifndef NO_OLD_TLS
- #ifdef WOLFSSL_ALLOW_TLSV10
- case TLS1_VERSION:
- ctx->minDowngrade = TLSv1_MINOR;
- break;
- #endif
- case TLS1_1_VERSION:
- ctx->minDowngrade = TLSv1_1_MINOR;
- break;
- #endif
- #ifndef WOLFSSL_NO_TLS12
- case TLS1_2_VERSION:
- ctx->minDowngrade = TLSv1_2_MINOR;
- break;
- #endif
- #ifdef WOLFSSL_TLS13
- case TLS1_3_VERSION:
- ctx->minDowngrade = TLSv1_3_MINOR;
- break;
- #endif
-#endif
-#ifdef WOLFSSL_DTLS
- #ifndef NO_OLD_TLS
- case DTLS1_VERSION:
- ctx->minDowngrade = DTLS_MINOR;
- break;
- #endif
- case DTLS1_2_VERSION:
- ctx->minDowngrade = DTLSv1_2_MINOR;
- break;
-#endif
- default:
- return BAD_FUNC_ARG;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_CTX_set_max_proto_version(WOLFSSL_CTX* ctx, int ver)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_set_max_proto_version");
-
- /* supported only at compile-time only */
- (void)ctx;
- (void)ver;
- return WOLFSSL_SUCCESS;
-}
-
-
-#if !defined(NO_WOLFSSL_CLIENT)
-/* Return the amount of random bytes copied over or error case.
- * ssl : ssl struct after handshake
- * out : buffer to hold random bytes
- * outSz : either 0 (return max buffer sz) or size of out buffer
- *
- * NOTE: wolfSSL_KeepArrays(ssl) must be called to retain handshake information.
- */
-size_t wolfSSL_get_client_random(const WOLFSSL* ssl, unsigned char* out,
- size_t outSz)
-{
- size_t size;
-
- /* return max size of buffer */
- if (outSz == 0) {
- return RAN_LEN;
- }
-
- if (ssl == NULL || out == NULL) {
- return 0;
- }
-
- if (ssl->options.saveArrays == 0 || ssl->arrays == NULL) {
- WOLFSSL_MSG("Arrays struct not saved after handshake");
- return 0;
- }
-
- if (outSz > RAN_LEN) {
- size = RAN_LEN;
- }
- else {
- size = outSz;
- }
-
- XMEMCPY(out, ssl->arrays->clientRandom, size);
- return size;
-}
-#endif /* !NO_WOLFSSL_CLIENT */
-
-
- unsigned long wolfSSLeay(void)
- {
- return SSLEAY_VERSION_NUMBER;
- }
-
- unsigned long wolfSSL_OpenSSL_version_num(void)
- {
- return OPENSSL_VERSION_NUMBER;
- }
-
- const char* wolfSSLeay_version(int type)
- {
- static const char* version = "SSLeay wolfSSL compatibility";
- (void)type;
- return version;
- }
-
-
-#ifndef NO_MD5
- int wolfSSL_MD5_Init(WOLFSSL_MD5_CTX* md5)
- {
- int ret;
- typedef char md5_test[sizeof(MD5_CTX) >= sizeof(wc_Md5) ? 1 : -1];
- (void)sizeof(md5_test);
-
- WOLFSSL_ENTER("MD5_Init");
- ret = wc_InitMd5((wc_Md5*)md5);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_MD5_Update(WOLFSSL_MD5_CTX* md5, const void* input,
- unsigned long sz)
- {
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_MD5_Update");
- ret = wc_Md5Update((wc_Md5*)md5, (const byte*)input, (word32)sz);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_MD5_Final(byte* input, WOLFSSL_MD5_CTX* md5)
- {
- int ret;
-
- WOLFSSL_ENTER("MD5_Final");
- ret = wc_Md5Final((wc_Md5*)md5, input);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-#endif /* !NO_MD5 */
-
-
-#ifndef NO_SHA
- int wolfSSL_SHA_Init(WOLFSSL_SHA_CTX* sha)
- {
- int ret;
-
- typedef char sha_test[sizeof(SHA_CTX) >= sizeof(wc_Sha) ? 1 : -1];
- (void)sizeof(sha_test);
-
- WOLFSSL_ENTER("SHA_Init");
- ret = wc_InitSha((wc_Sha*)sha);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA_Update(WOLFSSL_SHA_CTX* sha, const void* input,
- unsigned long sz)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA_Update");
- ret = wc_ShaUpdate((wc_Sha*)sha, (const byte*)input, (word32)sz);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA_Final(byte* input, WOLFSSL_SHA_CTX* sha)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA_Final");
- ret = wc_ShaFinal((wc_Sha*)sha, input);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA1_Init(WOLFSSL_SHA_CTX* sha)
- {
- WOLFSSL_ENTER("SHA1_Init");
- return SHA_Init(sha);
- }
-
-
- int wolfSSL_SHA1_Update(WOLFSSL_SHA_CTX* sha, const void* input,
- unsigned long sz)
- {
- WOLFSSL_ENTER("SHA1_Update");
- return SHA_Update(sha, input, sz);
- }
-
-
- int wolfSSL_SHA1_Final(byte* input, WOLFSSL_SHA_CTX* sha)
- {
- WOLFSSL_ENTER("SHA1_Final");
- return SHA_Final(input, sha);
- }
-#endif /* !NO_SHA */
-
-#ifdef WOLFSSL_SHA224
-
- int wolfSSL_SHA224_Init(WOLFSSL_SHA224_CTX* sha)
- {
- int ret;
-
- typedef char sha_test[sizeof(SHA224_CTX) >= sizeof(wc_Sha224) ? 1 : -1];
- (void)sizeof(sha_test);
-
- WOLFSSL_ENTER("SHA224_Init");
- ret = wc_InitSha224((wc_Sha224*)sha);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA224_Update(WOLFSSL_SHA224_CTX* sha, const void* input,
- unsigned long sz)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA224_Update");
- ret = wc_Sha224Update((wc_Sha224*)sha, (const byte*)input, (word32)sz);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA224_Final(byte* input, WOLFSSL_SHA224_CTX* sha)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA224_Final");
- ret = wc_Sha224Final((wc_Sha224*)sha, input);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-#endif /* WOLFSSL_SHA224 */
-
-
- int wolfSSL_SHA256_Init(WOLFSSL_SHA256_CTX* sha256)
- {
- int ret;
-
- typedef char sha_test[sizeof(SHA256_CTX) >= sizeof(wc_Sha256) ? 1 : -1];
- (void)sizeof(sha_test);
-
- WOLFSSL_ENTER("SHA256_Init");
- ret = wc_InitSha256((wc_Sha256*)sha256);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA256_Update(WOLFSSL_SHA256_CTX* sha, const void* input,
- unsigned long sz)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA256_Update");
- ret = wc_Sha256Update((wc_Sha256*)sha, (const byte*)input, (word32)sz);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA256_Final(byte* input, WOLFSSL_SHA256_CTX* sha)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA256_Final");
- ret = wc_Sha256Final((wc_Sha256*)sha, input);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
-#ifdef WOLFSSL_SHA384
-
- int wolfSSL_SHA384_Init(WOLFSSL_SHA384_CTX* sha)
- {
- int ret;
-
- typedef char sha_test[sizeof(SHA384_CTX) >= sizeof(wc_Sha384) ? 1 : -1];
- (void)sizeof(sha_test);
-
- WOLFSSL_ENTER("SHA384_Init");
- ret = wc_InitSha384((wc_Sha384*)sha);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA384_Update(WOLFSSL_SHA384_CTX* sha, const void* input,
- unsigned long sz)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA384_Update");
- ret = wc_Sha384Update((wc_Sha384*)sha, (const byte*)input, (word32)sz);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA384_Final(byte* input, WOLFSSL_SHA384_CTX* sha)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA384_Final");
- ret = wc_Sha384Final((wc_Sha384*)sha, input);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-#endif /* WOLFSSL_SHA384 */
-
-
-#ifdef WOLFSSL_SHA512
-
- int wolfSSL_SHA512_Init(WOLFSSL_SHA512_CTX* sha)
- {
- int ret;
-
- typedef char sha_test[sizeof(SHA512_CTX) >= sizeof(wc_Sha512) ? 1 : -1];
- (void)sizeof(sha_test);
-
- WOLFSSL_ENTER("SHA512_Init");
- ret = wc_InitSha512((wc_Sha512*)sha);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA512_Update(WOLFSSL_SHA512_CTX* sha, const void* input,
- unsigned long sz)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA512_Update");
- ret = wc_Sha512Update((wc_Sha512*)sha, (const byte*)input, (word32)sz);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA512_Final(byte* input, WOLFSSL_SHA512_CTX* sha)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA512_Final");
- ret = wc_Sha512Final((wc_Sha512*)sha, input);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-#endif /* WOLFSSL_SHA512 */
-
-#ifdef WOLFSSL_SHA3
-#ifndef WOLFSSL_NOSHA3_224
-
- int wolfSSL_SHA3_224_Init(WOLFSSL_SHA3_224_CTX* sha)
- {
- int ret;
-
- typedef char sha_test[sizeof(SHA3_224_CTX) >= sizeof(wc_Sha3) ? 1 : -1];
- (void)sizeof(sha_test);
-
- WOLFSSL_ENTER("SHA3_224_Init");
- ret = wc_InitSha3_224((wc_Sha3*)sha, NULL, 0);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA3_224_Update(WOLFSSL_SHA3_224_CTX* sha, const void* input,
- unsigned long sz)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA3_224_Update");
- ret = wc_Sha3_224_Update((wc_Sha3*)sha, (const byte*)input, (word32)sz);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA3_224_Final(byte* input, WOLFSSL_SHA3_224_CTX* sha)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA3_224_Final");
- ret = wc_Sha3_224_Final((wc_Sha3*)sha, input);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-#endif /* WOLFSSL_NOSHA3_224 */
-
-
-#ifndef WOLFSSL_NOSHA3_256
- int wolfSSL_SHA3_256_Init(WOLFSSL_SHA3_256_CTX* sha3_256)
- {
- int ret;
-
- typedef char sha_test[sizeof(SHA3_256_CTX) >= sizeof(wc_Sha3) ? 1 : -1];
- (void)sizeof(sha_test);
-
- WOLFSSL_ENTER("SHA3_256_Init");
- ret = wc_InitSha3_256((wc_Sha3*)sha3_256, NULL, 0);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA3_256_Update(WOLFSSL_SHA3_256_CTX* sha, const void* input,
- unsigned long sz)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA3_256_Update");
- ret = wc_Sha3_256_Update((wc_Sha3*)sha, (const byte*)input, (word32)sz);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA3_256_Final(byte* input, WOLFSSL_SHA3_256_CTX* sha)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA3_256_Final");
- ret = wc_Sha3_256_Final((wc_Sha3*)sha, input);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-#endif /* WOLFSSL_NOSHA3_256 */
-
-
- int wolfSSL_SHA3_384_Init(WOLFSSL_SHA3_384_CTX* sha)
- {
- int ret;
-
- typedef char sha_test[sizeof(SHA3_384_CTX) >= sizeof(wc_Sha3) ? 1 : -1];
- (void)sizeof(sha_test);
-
- WOLFSSL_ENTER("SHA3_384_Init");
- ret = wc_InitSha3_384((wc_Sha3*)sha, NULL, 0);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA3_384_Update(WOLFSSL_SHA3_384_CTX* sha, const void* input,
- unsigned long sz)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA3_384_Update");
- ret = wc_Sha3_384_Update((wc_Sha3*)sha, (const byte*)input, (word32)sz);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA3_384_Final(byte* input, WOLFSSL_SHA3_384_CTX* sha)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA3_384_Final");
- ret = wc_Sha3_384_Final((wc_Sha3*)sha, input);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
-
-#ifndef WOLFSSL_NOSHA3_512
-
- int wolfSSL_SHA3_512_Init(WOLFSSL_SHA3_512_CTX* sha)
- {
- int ret;
-
- typedef char sha_test[sizeof(SHA3_512_CTX) >= sizeof(wc_Sha3) ? 1 : -1];
- (void)sizeof(sha_test);
-
- WOLFSSL_ENTER("SHA3_512_Init");
- ret = wc_InitSha3_512((wc_Sha3*)sha, NULL, 0);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA3_512_Update(WOLFSSL_SHA3_512_CTX* sha, const void* input,
- unsigned long sz)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA3_512_Update");
- ret = wc_Sha3_512_Update((wc_Sha3*)sha, (const byte*)input, (word32)sz);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-
- int wolfSSL_SHA3_512_Final(byte* input, WOLFSSL_SHA3_512_CTX* sha)
- {
- int ret;
-
- WOLFSSL_ENTER("SHA3_512_Final");
- ret = wc_Sha3_512_Final((wc_Sha3*)sha, input);
-
- /* return 1 on success, 0 otherwise */
- if (ret == 0)
- return 1;
-
- return 0;
- }
-
-#endif /* WOLFSSL_NOSHA3_512 */
-#endif /* WOLFSSL_SHA3 */
-
- /* store for external read of iv, WOLFSSL_SUCCESS on success */
- int wolfSSL_StoreExternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx)
- {
- WOLFSSL_ENTER("wolfSSL_StoreExternalIV");
-
- if (ctx == NULL) {
- WOLFSSL_MSG("Bad function argument");
- return WOLFSSL_FATAL_ERROR;
- }
-
- switch (ctx->cipherType) {
-
-#ifndef NO_AES
-#ifdef HAVE_AES_CBC
- case AES_128_CBC_TYPE :
- case AES_192_CBC_TYPE :
- case AES_256_CBC_TYPE :
- WOLFSSL_MSG("AES CBC");
- XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
- break;
-#endif
-#ifdef HAVE_AESGCM
- case AES_128_GCM_TYPE :
- case AES_192_GCM_TYPE :
- case AES_256_GCM_TYPE :
- WOLFSSL_MSG("AES GCM");
- XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
- break;
-#endif /* HAVE_AESGCM */
-#ifdef HAVE_AES_ECB
- case AES_128_ECB_TYPE :
- case AES_192_ECB_TYPE :
- case AES_256_ECB_TYPE :
- WOLFSSL_MSG("AES ECB");
- break;
-#endif
-#ifdef WOLFSSL_AES_COUNTER
- case AES_128_CTR_TYPE :
- case AES_192_CTR_TYPE :
- case AES_256_CTR_TYPE :
- WOLFSSL_MSG("AES CTR");
- XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
- break;
-#endif /* WOLFSSL_AES_COUNTER */
-#ifdef WOLFSSL_AES_CFB
-#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
- case AES_128_CFB1_TYPE:
- case AES_192_CFB1_TYPE:
- case AES_256_CFB1_TYPE:
- WOLFSSL_MSG("AES CFB1");
- break;
- case AES_128_CFB8_TYPE:
- case AES_192_CFB8_TYPE:
- case AES_256_CFB8_TYPE:
- WOLFSSL_MSG("AES CFB8");
- break;
-#endif /* !HAVE_SELFTEST && !HAVE_FIPS */
- case AES_128_CFB128_TYPE:
- case AES_192_CFB128_TYPE:
- case AES_256_CFB128_TYPE:
- WOLFSSL_MSG("AES CFB128");
- break;
-#endif /* WOLFSSL_AES_CFB */
-#if defined(WOLFSSL_AES_OFB)
- case AES_128_OFB_TYPE:
- case AES_192_OFB_TYPE:
- case AES_256_OFB_TYPE:
- WOLFSSL_MSG("AES OFB");
- break;
-#endif /* WOLFSSL_AES_OFB */
-#ifdef WOLFSSL_AES_XTS
- case AES_128_XTS_TYPE:
- case AES_256_XTS_TYPE:
- WOLFSSL_MSG("AES XTS");
- break;
-#endif /* WOLFSSL_AES_XTS */
-#endif /* NO_AES */
-
-#ifndef NO_DES3
- case DES_CBC_TYPE :
- WOLFSSL_MSG("DES CBC");
- XMEMCPY(ctx->iv, &ctx->cipher.des.reg, DES_BLOCK_SIZE);
- break;
-
- case DES_EDE3_CBC_TYPE :
- WOLFSSL_MSG("DES EDE3 CBC");
- XMEMCPY(ctx->iv, &ctx->cipher.des3.reg, DES_BLOCK_SIZE);
- break;
-#endif
-#ifdef WOLFSSL_DES_ECB
- case DES_ECB_TYPE :
- WOLFSSL_MSG("DES ECB");
- break;
- case DES_EDE3_ECB_TYPE :
- WOLFSSL_MSG("DES3 ECB");
- break;
-#endif
-
-#ifdef HAVE_IDEA
- case IDEA_CBC_TYPE :
- WOLFSSL_MSG("IDEA CBC");
- XMEMCPY(ctx->iv, &ctx->cipher.idea.reg, IDEA_BLOCK_SIZE);
- break;
-#endif
- case ARC4_TYPE :
- WOLFSSL_MSG("ARC4");
- break;
-
- case NULL_CIPHER_TYPE :
- WOLFSSL_MSG("NULL");
- break;
-
- default: {
- WOLFSSL_MSG("bad type");
- return WOLFSSL_FATAL_ERROR;
- }
- }
- return WOLFSSL_SUCCESS;
- }
-
- /* set internal IV from external, WOLFSSL_SUCCESS on success */
- int wolfSSL_SetInternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx)
- {
-
- WOLFSSL_ENTER("wolfSSL_SetInternalIV");
-
- if (ctx == NULL) {
- WOLFSSL_MSG("Bad function argument");
- return WOLFSSL_FATAL_ERROR;
- }
-
- switch (ctx->cipherType) {
-
-#ifndef NO_AES
-#ifdef HAVE_AES_CBC
- case AES_128_CBC_TYPE :
- case AES_192_CBC_TYPE :
- case AES_256_CBC_TYPE :
- WOLFSSL_MSG("AES CBC");
- XMEMCPY(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
- break;
-#endif
-#ifdef HAVE_AESGCM
- case AES_128_GCM_TYPE :
- case AES_192_GCM_TYPE :
- case AES_256_GCM_TYPE :
- WOLFSSL_MSG("AES GCM");
- XMEMCPY(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
- break;
-#endif
-#ifdef HAVE_AES_ECB
- case AES_128_ECB_TYPE :
- case AES_192_ECB_TYPE :
- case AES_256_ECB_TYPE :
- WOLFSSL_MSG("AES ECB");
- break;
-#endif
-#ifdef WOLFSSL_AES_COUNTER
- case AES_128_CTR_TYPE :
- case AES_192_CTR_TYPE :
- case AES_256_CTR_TYPE :
- WOLFSSL_MSG("AES CTR");
- XMEMCPY(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
- break;
-#endif
-
-#endif /* NO_AES */
-
-#ifndef NO_DES3
- case DES_CBC_TYPE :
- WOLFSSL_MSG("DES CBC");
- XMEMCPY(&ctx->cipher.des.reg, ctx->iv, DES_BLOCK_SIZE);
- break;
-
- case DES_EDE3_CBC_TYPE :
- WOLFSSL_MSG("DES EDE3 CBC");
- XMEMCPY(&ctx->cipher.des3.reg, ctx->iv, DES_BLOCK_SIZE);
- break;
-#endif
-#ifdef WOLFSSL_DES_ECB
- case DES_ECB_TYPE :
- WOLFSSL_MSG("DES ECB");
- break;
- case DES_EDE3_ECB_TYPE :
- WOLFSSL_MSG("DES3 ECB");
- break;
-#endif
-
-#ifdef HAVE_IDEA
- case IDEA_CBC_TYPE :
- WOLFSSL_MSG("IDEA CBC");
- XMEMCPY(&ctx->cipher.idea.reg, ctx->iv, IDEA_BLOCK_SIZE);
- break;
-#endif
- case ARC4_TYPE :
- WOLFSSL_MSG("ARC4");
- break;
-
- case NULL_CIPHER_TYPE :
- WOLFSSL_MSG("NULL");
- break;
-
- default: {
- WOLFSSL_MSG("bad type");
- return WOLFSSL_FATAL_ERROR;
- }
- }
- return WOLFSSL_SUCCESS;
- }
-
- unsigned char* wolfSSL_HMAC(const WOLFSSL_EVP_MD* evp_md, const void* key,
- int key_len, const unsigned char* d, int n,
- unsigned char* md, unsigned int* md_len)
- {
- int type;
- int mdlen;
- unsigned char* ret = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- Hmac* hmac = NULL;
-#else
- Hmac hmac[1];
-#endif
- void* heap = NULL;
-
- WOLFSSL_ENTER("wolfSSL_HMAC");
- if (!md) {
- WOLFSSL_MSG("Static buffer not supported, pass in md buffer");
- return NULL; /* no static buffer support */
- }
-
-#ifndef NO_MD5
- if (XSTRNCMP(evp_md, "MD5", 3) == 0) {
- type = WC_MD5;
- mdlen = WC_MD5_DIGEST_SIZE;
- } else
-#endif
-#ifdef WOLFSSL_SHA224
- if (XSTRNCMP(evp_md, "SHA224", 6) == 0) {
- type = WC_SHA224;
- mdlen = WC_SHA224_DIGEST_SIZE;
- } else
-#endif
-#ifndef NO_SHA256
- if (XSTRNCMP(evp_md, "SHA256", 6) == 0) {
- type = WC_SHA256;
- mdlen = WC_SHA256_DIGEST_SIZE;
- } else
-#endif
-#ifdef WOLFSSL_SHA384
- if (XSTRNCMP(evp_md, "SHA384", 6) == 0) {
- type = WC_SHA384;
- mdlen = WC_SHA384_DIGEST_SIZE;
- } else
-#endif
-#ifdef WOLFSSL_SHA512
- if (XSTRNCMP(evp_md, "SHA512", 6) == 0) {
- type = WC_SHA512;
- mdlen = WC_SHA512_DIGEST_SIZE;
- } else
-#endif
-#ifdef WOLFSSL_SHA3
- #ifndef WOLFSSL_NOSHA3_224
- if (XSTRNCMP(evp_md, "SHA3_224", 8) == 0) {
- type = WC_SHA3_224;
- mdlen = WC_SHA3_224_DIGEST_SIZE;
- } else
- #endif
- #ifndef WOLFSSL_NOSHA3_256
- if (XSTRNCMP(evp_md, "SHA3_256", 8) == 0) {
- type = WC_SHA3_256;
- mdlen = WC_SHA3_256_DIGEST_SIZE;
- } else
- #endif
- if (XSTRNCMP(evp_md, "SHA3_384", 8) == 0) {
- type = WC_SHA3_384;
- mdlen = WC_SHA3_384_DIGEST_SIZE;
- } else
- #ifndef WOLFSSL_NOSHA3_512
- if (XSTRNCMP(evp_md, "SHA3_512", 8) == 0) {
- type = WC_SHA3_512;
- mdlen = WC_SHA3_512_DIGEST_SIZE;
- } else
- #endif
-#endif
-#ifndef NO_SHA
- if (XSTRNCMP(evp_md, "SHA", 3) == 0) {
- type = WC_SHA;
- mdlen = WC_SHA_DIGEST_SIZE;
- } else
-#endif
- {
- return NULL;
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap, DYNAMIC_TYPE_HMAC);
- if (hmac == NULL)
- return NULL;
- #endif
-
- if (wc_HmacInit(hmac, heap, INVALID_DEVID) == 0) {
- if (wc_HmacSetKey(hmac, type, (const byte*)key, key_len) == 0) {
- if (wc_HmacUpdate(hmac, d, n) == 0) {
- if (wc_HmacFinal(hmac, md) == 0) {
- if (md_len)
- *md_len = mdlen;
- ret = md;
- }
- }
- }
- wc_HmacFree(hmac);
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(hmac, heap, DYNAMIC_TYPE_HMAC);
- #endif
-
- (void)evp_md;
- return ret;
- }
-
- void wolfSSL_ERR_clear_error(void)
- {
- WOLFSSL_ENTER("wolfSSL_ERR_clear_error");
-
-#if defined(DEBUG_WOLFSSL) || defined(WOLFSSL_NGINX) || \
- defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
- wc_ClearErrorNodes();
-#endif
- }
-
-
- int wolfSSL_RAND_status(void)
- {
- return WOLFSSL_SUCCESS; /* wolfCrypt provides enough seed internally */
- }
-
-
- #ifndef NO_WOLFSSL_STUB
- void wolfSSL_RAND_add(const void* add, int len, double entropy)
- {
- (void)add;
- (void)len;
- (void)entropy;
- WOLFSSL_STUB("RAND_add");
- /* wolfSSL seeds/adds internally, use explicit RNG if you want
- to take control */
- }
- #endif
-
-#ifndef NO_DES3
- /* 0 on ok */
- int wolfSSL_DES_key_sched(WOLFSSL_const_DES_cblock* key,
- WOLFSSL_DES_key_schedule* schedule)
- {
- WOLFSSL_ENTER("wolfSSL_DES_key_sched");
-
- if (key == NULL || schedule == NULL) {
- WOLFSSL_MSG("Null argument passed in");
- }
- else {
- XMEMCPY(schedule, key, sizeof(WOLFSSL_const_DES_cblock));
- }
-
- return 0;
- }
-
-
- /* intended to behave similar to Kerberos mit_des_cbc_cksum
- * return the last 4 bytes of cipher text */
- WOLFSSL_DES_LONG wolfSSL_DES_cbc_cksum(const unsigned char* in,
- WOLFSSL_DES_cblock* out, long length, WOLFSSL_DES_key_schedule* sc,
- WOLFSSL_const_DES_cblock* iv)
- {
- WOLFSSL_DES_LONG ret;
- unsigned char* tmp;
- unsigned char* data = (unsigned char*)in;
- long dataSz = length;
- byte dynamicFlag = 0; /* when padding the buffer created needs free'd */
-
- WOLFSSL_ENTER("wolfSSL_DES_cbc_cksum");
-
- if (in == NULL || out == NULL || sc == NULL || iv == NULL) {
- WOLFSSL_MSG("Bad argument passed in");
- return 0;
- }
-
- /* if input length is not a multiple of DES_BLOCK_SIZE pad with 0s */
- if (dataSz % DES_BLOCK_SIZE) {
- dataSz += DES_BLOCK_SIZE - (dataSz % DES_BLOCK_SIZE);
- data = (unsigned char*)XMALLOC(dataSz, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (data == NULL) {
- WOLFSSL_MSG("Issue creating temporary buffer");
- return 0;
- }
- dynamicFlag = 1; /* set to free buffer at end */
- XMEMCPY(data, in, length);
- XMEMSET(data + length, 0, dataSz - length); /* padding */
- }
-
- tmp = (unsigned char*)XMALLOC(dataSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmp == NULL) {
- WOLFSSL_MSG("Issue creating temporary buffer");
- if (dynamicFlag == 1) {
- XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- return 0;
- }
-
- wolfSSL_DES_cbc_encrypt(data, tmp, dataSz, sc,
- (WOLFSSL_DES_cblock*)iv, 1);
- XMEMCPY((unsigned char*)out, tmp + (dataSz - DES_BLOCK_SIZE),
- DES_BLOCK_SIZE);
-
- ret = (((*((unsigned char*)out + 4) & 0xFF) << 24)|
- ((*((unsigned char*)out + 5) & 0xFF) << 16)|
- ((*((unsigned char*)out + 6) & 0xFF) << 8) |
- (*((unsigned char*)out + 7) & 0xFF));
-
- XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (dynamicFlag == 1) {
- XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- return ret;
- }
-
-
- void wolfSSL_DES_cbc_encrypt(const unsigned char* input,
- unsigned char* output, long length,
- WOLFSSL_DES_key_schedule* schedule,
- WOLFSSL_DES_cblock* ivec, int enc)
- {
- Des myDes;
- byte lastblock[DES_BLOCK_SIZE];
- int lb_sz;
- long blk;
-
- WOLFSSL_ENTER("DES_cbc_encrypt");
-
- /* OpenSSL compat, no ret */
- wc_Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
- lb_sz = length%DES_BLOCK_SIZE;
- blk = length/DES_BLOCK_SIZE;
-
- if (enc){
- wc_Des_CbcEncrypt(&myDes, output, input, (word32)blk*DES_BLOCK_SIZE);
- if(lb_sz){
- XMEMSET(lastblock, 0, DES_BLOCK_SIZE);
- XMEMCPY(lastblock, input+length-lb_sz, lb_sz);
- wc_Des_CbcEncrypt(&myDes, output+blk*DES_BLOCK_SIZE,
- lastblock, (word32)DES_BLOCK_SIZE);
- }
- }
- else {
- wc_Des_CbcDecrypt(&myDes, output, input, (word32)blk*DES_BLOCK_SIZE);
- if(lb_sz){
- wc_Des_CbcDecrypt(&myDes, lastblock, input+length-lb_sz, (word32)DES_BLOCK_SIZE);
- XMEMCPY(output+length-lb_sz, lastblock, lb_sz);
- }
- }
- }
-
-
- /* WOLFSSL_DES_key_schedule is a unsigned char array of size 8 */
- void wolfSSL_DES_ede3_cbc_encrypt(const unsigned char* input,
- unsigned char* output, long sz,
- WOLFSSL_DES_key_schedule* ks1,
- WOLFSSL_DES_key_schedule* ks2,
- WOLFSSL_DES_key_schedule* ks3,
- WOLFSSL_DES_cblock* ivec, int enc)
- {
- int ret;
- Des3 des;
- byte key[24];/* EDE uses 24 size key */
- byte lastblock[DES_BLOCK_SIZE];
- int lb_sz;
- long blk;
-
- WOLFSSL_ENTER("wolfSSL_DES_ede3_cbc_encrypt");
-
- XMEMSET(key, 0, sizeof(key));
- XMEMCPY(key, *ks1, DES_BLOCK_SIZE);
- XMEMCPY(&key[DES_BLOCK_SIZE], *ks2, DES_BLOCK_SIZE);
- XMEMCPY(&key[DES_BLOCK_SIZE * 2], *ks3, DES_BLOCK_SIZE);
- lb_sz = sz%DES_BLOCK_SIZE;
- blk = sz/DES_BLOCK_SIZE;
-
- /* OpenSSL compat, no ret */
- (void)wc_Des3Init(&des, NULL, INVALID_DEVID);
-
- if (enc) {
- wc_Des3_SetKey(&des, key, (const byte*)ivec, DES_ENCRYPTION);
- ret = wc_Des3_CbcEncrypt(&des, output, input, (word32)blk*DES_BLOCK_SIZE);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &des.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- (void)ret; /* ignore return codes for processing */
- if(lb_sz){
- XMEMSET(lastblock, 0, DES_BLOCK_SIZE);
- XMEMCPY(lastblock, input+sz-lb_sz, lb_sz);
- ret = wc_Des3_CbcEncrypt(&des, output+blk*DES_BLOCK_SIZE,
- lastblock, (word32)DES_BLOCK_SIZE);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &des.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- (void)ret; /* ignore return codes for processing */
- }
- }
- else {
- wc_Des3_SetKey(&des, key, (const byte*)ivec, DES_DECRYPTION);
- ret = wc_Des3_CbcDecrypt(&des, output, input, (word32)blk*DES_BLOCK_SIZE);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &des.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- (void)ret; /* ignore return codes for processing */
- if(lb_sz){
- ret = wc_Des3_CbcDecrypt(&des, lastblock, input+sz-lb_sz, (word32)DES_BLOCK_SIZE);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &des.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- (void)ret; /* ignore return codes for processing */
- XMEMCPY(output+sz-lb_sz, lastblock, lb_sz);
- }
- }
- wc_Des3Free(&des);
- }
-
-
- /* correctly sets ivec for next call */
- void wolfSSL_DES_ncbc_encrypt(const unsigned char* input,
- unsigned char* output, long length,
- WOLFSSL_DES_key_schedule* schedule, WOLFSSL_DES_cblock* ivec,
- int enc)
- {
- Des myDes;
- byte lastblock[DES_BLOCK_SIZE];
- int lb_sz;
- long idx = length;
- long blk;
-
- WOLFSSL_ENTER("DES_ncbc_encrypt");
-
- /* OpenSSL compat, no ret */
- wc_Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
- lb_sz = length%DES_BLOCK_SIZE;
- blk = length/DES_BLOCK_SIZE;
- idx -= sizeof(DES_cblock);
- if (lb_sz) {
- idx += DES_BLOCK_SIZE - lb_sz;
- }
- if (enc){
- wc_Des_CbcEncrypt(&myDes, output, input,
- (word32)blk * DES_BLOCK_SIZE);
- if (lb_sz){
- XMEMSET(lastblock, 0, DES_BLOCK_SIZE);
- XMEMCPY(lastblock, input+length-lb_sz, lb_sz);
- wc_Des_CbcEncrypt(&myDes, output + blk * DES_BLOCK_SIZE,
- lastblock, (word32)DES_BLOCK_SIZE);
- }
- XMEMCPY(ivec, output + idx, sizeof(DES_cblock));
- } else {
- WOLFSSL_DES_cblock tmp;
- XMEMCPY(tmp, input + idx, sizeof(DES_cblock));
- wc_Des_CbcDecrypt(&myDes, output, input,
- (word32)blk * DES_BLOCK_SIZE);
- if (lb_sz){
- wc_Des_CbcDecrypt(&myDes, lastblock, input + length - lb_sz,
- (word32)DES_BLOCK_SIZE);
- XMEMCPY(output+length-lb_sz, lastblock, lb_sz);
- }
- XMEMCPY(ivec, tmp, sizeof(WOLFSSL_DES_cblock));
- }
-
- }
-
-#endif /* NO_DES3 */
-
- void wolfSSL_ERR_free_strings(void)
- {
- /* handled internally */
- }
-
- void wolfSSL_cleanup_all_ex_data(void)
- {
- /* nothing to do here */
- }
-
- int wolfSSL_clear(WOLFSSL* ssl)
- {
- if (ssl == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- ssl->options.isClosed = 0;
- ssl->options.connReset = 0;
- ssl->options.sentNotify = 0;
- ssl->options.sendVerify = 0;
- ssl->options.serverState = NULL_STATE;
- ssl->options.clientState = NULL_STATE;
- ssl->options.connectState = CONNECT_BEGIN;
- ssl->options.acceptState = ACCEPT_BEGIN;
- ssl->options.handShakeState = NULL_STATE;
- ssl->options.handShakeDone = 0;
- /* ssl->options.processReply = doProcessInit; */
-
- ssl->keys.encryptionOn = 0;
- XMEMSET(&ssl->msgsReceived, 0, sizeof(ssl->msgsReceived));
-
- if (ssl->hsHashes != NULL) {
-#ifndef NO_OLD_TLS
-#ifndef NO_MD5
- if (wc_InitMd5_ex(&ssl->hsHashes->hashMd5, ssl->heap,
- ssl->devId) != 0) {
- return WOLFSSL_FAILURE;
- }
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_Md5SetFlags(&ssl->hsHashes->hashMd5, WC_HASH_FLAG_WILLCOPY);
- #endif
-#endif
-#ifndef NO_SHA
- if (wc_InitSha_ex(&ssl->hsHashes->hashSha, ssl->heap,
- ssl->devId) != 0) {
- return WOLFSSL_FAILURE;
- }
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_ShaSetFlags(&ssl->hsHashes->hashSha, WC_HASH_FLAG_WILLCOPY);
- #endif
-#endif
-#endif
-#ifndef NO_SHA256
- if (wc_InitSha256_ex(&ssl->hsHashes->hashSha256, ssl->heap,
- ssl->devId) != 0) {
- return WOLFSSL_FAILURE;
- }
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_Sha256SetFlags(&ssl->hsHashes->hashSha256, WC_HASH_FLAG_WILLCOPY);
- #endif
-#endif
-#ifdef WOLFSSL_SHA384
- if (wc_InitSha384_ex(&ssl->hsHashes->hashSha384, ssl->heap,
- ssl->devId) != 0) {
- return WOLFSSL_FAILURE;
- }
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_Sha384SetFlags(&ssl->hsHashes->hashSha384, WC_HASH_FLAG_WILLCOPY);
- #endif
-#endif
-#ifdef WOLFSSL_SHA512
- if (wc_InitSha512_ex(&ssl->hsHashes->hashSha512, ssl->heap,
- ssl->devId) != 0) {
- return WOLFSSL_FAILURE;
- }
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_Sha512SetFlags(&ssl->hsHashes->hashSha512, WC_HASH_FLAG_WILLCOPY);
- #endif
-#endif
- }
-#ifdef SESSION_CERTS
- ssl->session.chain.count = 0;
-#endif
-#ifdef KEEP_PEER_CERT
- FreeX509(&ssl->peerCert);
- InitX509(&ssl->peerCert, 0, ssl->heap);
-#endif
-
- return WOLFSSL_SUCCESS;
- }
-
- long wolfSSL_SSL_SESSION_set_timeout(WOLFSSL_SESSION* ses, long t)
- {
- word32 tmptime;
- if (!ses || t < 0)
- return BAD_FUNC_ARG;
-
- tmptime = t & 0xFFFFFFFF;
-
- ses->timeout = tmptime;
-
- return WOLFSSL_SUCCESS;
- }
-
-#endif
-
-#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- long wolfSSL_CTX_set_mode(WOLFSSL_CTX* ctx, long mode)
- {
- /* WOLFSSL_MODE_ACCEPT_MOVING_WRITE_BUFFER is wolfSSL default mode */
-
- WOLFSSL_ENTER("SSL_CTX_set_mode");
- switch(mode) {
- case SSL_MODE_ENABLE_PARTIAL_WRITE:
- ctx->partialWrite = 1;
- break;
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- case SSL_MODE_RELEASE_BUFFERS:
- WOLFSSL_MSG("SSL_MODE_RELEASE_BUFFERS not implemented.");
- break;
- #endif
- default:
- WOLFSSL_MSG("Mode Not Implemented");
- }
-
- /* SSL_MODE_AUTO_RETRY
- * Should not return -1 with renegotiation on read/write */
-
- return mode;
- }
-#endif
-
-#ifdef OPENSSL_EXTRA
-
- #ifndef NO_WOLFSSL_STUB
- long wolfSSL_SSL_get_mode(WOLFSSL* ssl)
- {
- /* TODO: */
- (void)ssl;
- WOLFSSL_STUB("SSL_get_mode");
- return 0;
- }
- #endif
-
- #ifndef NO_WOLFSSL_STUB
- long wolfSSL_CTX_get_mode(WOLFSSL_CTX* ctx)
- {
- /* TODO: */
- (void)ctx;
- WOLFSSL_STUB("SSL_CTX_get_mode");
- return 0;
- }
- #endif
-
- #ifndef NO_WOLFSSL_STUB
- void wolfSSL_CTX_set_default_read_ahead(WOLFSSL_CTX* ctx, int m)
- {
- /* TODO: maybe? */
- (void)ctx;
- (void)m;
- WOLFSSL_STUB("SSL_CTX_set_default_read_ahead");
- }
- #endif
-
-
- /* Storing app session context id, this value is inherited by WOLFSSL
- * objects created from WOLFSSL_CTX. Any session that is imported with a
- * different session context id will be rejected.
- *
- * ctx structure to set context in
- * sid_ctx value of context to set
- * sid_ctx_len length of sid_ctx buffer
- *
- * Returns SSL_SUCCESS in success case and SSL_FAILURE when failing
- */
- int wolfSSL_CTX_set_session_id_context(WOLFSSL_CTX* ctx,
- const unsigned char* sid_ctx,
- unsigned int sid_ctx_len)
- {
- WOLFSSL_ENTER("SSL_CTX_set_session_id_context");
-
- /* No application specific context needed for wolfSSL */
- if (sid_ctx_len > ID_LEN || ctx == NULL || sid_ctx == NULL) {
- return SSL_FAILURE;
- }
- XMEMCPY(ctx->sessionCtx, sid_ctx, sid_ctx_len);
- ctx->sessionCtxSz = (byte)sid_ctx_len;
-
- return SSL_SUCCESS;
- }
-
-
-
- /* Storing app session context id. Any session that is imported with a
- * different session context id will be rejected.
- *
- * ssl structure to set context in
- * id value of context to set
- * len length of sid_ctx buffer
- *
- * Returns SSL_SUCCESS in success case and SSL_FAILURE when failing
- */
- int wolfSSL_set_session_id_context(WOLFSSL* ssl, const unsigned char* id,
- unsigned int len)
- {
- WOLFSSL_ENTER("wolfSSL_set_session_id_context");
-
- if (len > ID_LEN || ssl == NULL || id == NULL) {
- return SSL_FAILURE;
- }
- XMEMCPY(ssl->sessionCtx, id, len);
- ssl->sessionCtxSz = (byte)len;
-
- return SSL_SUCCESS;
- }
-
-
- long wolfSSL_CTX_sess_get_cache_size(WOLFSSL_CTX* ctx)
- {
- (void)ctx;
- #ifndef NO_SESSION_CACHE
- return (long)(SESSIONS_PER_ROW * SESSION_ROWS);
- #else
- return 0;
- #endif
- }
-
-
- /* returns the unsigned error value and increments the pointer into the
- * error queue.
- *
- * file pointer to file name
- * line gets set to line number of error when not NULL
- */
- unsigned long wolfSSL_ERR_get_error_line(const char** file, int* line)
- {
- #ifdef DEBUG_WOLFSSL
- int ret = wc_PullErrorNode(file, NULL, line);
- if (ret < 0) {
- if (ret == BAD_STATE_E) return 0; /* no errors in queue */
- WOLFSSL_MSG("Issue getting error node");
- WOLFSSL_LEAVE("wolfSSL_ERR_get_error_line", ret);
- ret = 0 - ret; /* return absolute value of error */
-
- /* panic and try to clear out nodes */
- wc_ClearErrorNodes();
- }
- return (unsigned long)ret;
- #else
- (void)file;
- (void)line;
-
- return 0;
- #endif
- }
-
-
-#if (defined(DEBUG_WOLFSSL) || defined(OPENSSL_EXTRA)) && \
- (!defined(_WIN32) && !defined(NO_ERROR_QUEUE))
- static const char WOLFSSL_SYS_ACCEPT_T[] = "accept";
- static const char WOLFSSL_SYS_BIND_T[] = "bind";
- static const char WOLFSSL_SYS_CONNECT_T[] = "connect";
- static const char WOLFSSL_SYS_FOPEN_T[] = "fopen";
- static const char WOLFSSL_SYS_FREAD_T[] = "fread";
- static const char WOLFSSL_SYS_GETADDRINFO_T[] = "getaddrinfo";
- static const char WOLFSSL_SYS_GETSOCKOPT_T[] = "getsockopt";
- static const char WOLFSSL_SYS_GETSOCKNAME_T[] = "getsockname";
- static const char WOLFSSL_SYS_GETHOSTBYNAME_T[] = "gethostbyname";
- static const char WOLFSSL_SYS_GETNAMEINFO_T[] = "getnameinfo";
- static const char WOLFSSL_SYS_GETSERVBYNAME_T[] = "getservbyname";
- static const char WOLFSSL_SYS_IOCTLSOCKET_T[] = "ioctlsocket";
- static const char WOLFSSL_SYS_LISTEN_T[] = "listen";
- static const char WOLFSSL_SYS_OPENDIR_T[] = "opendir";
- static const char WOLFSSL_SYS_SETSOCKOPT_T[] = "setsockopt";
- static const char WOLFSSL_SYS_SOCKET_T[] = "socket";
-
- /* switch with int mapped to function name for compatibility */
- static const char* wolfSSL_ERR_sys_func(int fun)
- {
- switch (fun) {
- case WOLFSSL_SYS_ACCEPT: return WOLFSSL_SYS_ACCEPT_T;
- case WOLFSSL_SYS_BIND: return WOLFSSL_SYS_BIND_T;
- case WOLFSSL_SYS_CONNECT: return WOLFSSL_SYS_CONNECT_T;
- case WOLFSSL_SYS_FOPEN: return WOLFSSL_SYS_FOPEN_T;
- case WOLFSSL_SYS_FREAD: return WOLFSSL_SYS_FREAD_T;
- case WOLFSSL_SYS_GETADDRINFO: return WOLFSSL_SYS_GETADDRINFO_T;
- case WOLFSSL_SYS_GETSOCKOPT: return WOLFSSL_SYS_GETSOCKOPT_T;
- case WOLFSSL_SYS_GETSOCKNAME: return WOLFSSL_SYS_GETSOCKNAME_T;
- case WOLFSSL_SYS_GETHOSTBYNAME: return WOLFSSL_SYS_GETHOSTBYNAME_T;
- case WOLFSSL_SYS_GETNAMEINFO: return WOLFSSL_SYS_GETNAMEINFO_T;
- case WOLFSSL_SYS_GETSERVBYNAME: return WOLFSSL_SYS_GETSERVBYNAME_T;
- case WOLFSSL_SYS_IOCTLSOCKET: return WOLFSSL_SYS_IOCTLSOCKET_T;
- case WOLFSSL_SYS_LISTEN: return WOLFSSL_SYS_LISTEN_T;
- case WOLFSSL_SYS_OPENDIR: return WOLFSSL_SYS_OPENDIR_T;
- case WOLFSSL_SYS_SETSOCKOPT: return WOLFSSL_SYS_SETSOCKOPT_T;
- case WOLFSSL_SYS_SOCKET: return WOLFSSL_SYS_SOCKET_T;
- default:
- return "NULL";
- }
- }
-#endif /* DEBUG_WOLFSSL */
-
-
- /* @TODO when having an error queue this needs to push to the queue */
- void wolfSSL_ERR_put_error(int lib, int fun, int err, const char* file,
- int line)
- {
- WOLFSSL_ENTER("wolfSSL_ERR_put_error");
-
- #if !defined(DEBUG_WOLFSSL) && !defined(OPENSSL_EXTRA)
- (void)fun;
- (void)err;
- (void)file;
- (void)line;
- WOLFSSL_MSG("Not compiled in debug mode");
- #elif defined(OPENSSL_EXTRA) && \
- (defined(_WIN32) || defined(NO_ERROR_QUEUE))
- (void)fun;
- (void)file;
- (void)line;
- WOLFSSL_ERROR(err);
- #else
- WOLFSSL_ERROR_LINE(err, wolfSSL_ERR_sys_func(fun), (unsigned int)line,
- file, NULL);
- #endif
- (void)lib;
- }
-
-
- /* Similar to wolfSSL_ERR_get_error_line but takes in a flags argument for
- * more flexibility.
- *
- * file output pointer to file where error happened
- * line output to line number of error
- * data output data. Is a string if ERR_TXT_STRING flag is used
- * flags bit flag to adjust data output
- *
- * Returns the error value or 0 if no errors are in the queue
- */
- unsigned long wolfSSL_ERR_get_error_line_data(const char** file, int* line,
- const char** data, int *flags)
- {
- int ret;
-
- WOLFSSL_STUB("wolfSSL_ERR_get_error_line_data");
-
- if (flags != NULL) {
- if ((*flags & ERR_TXT_STRING) == ERR_TXT_STRING) {
- ret = wc_PullErrorNode(file, data, line);
- if (ret < 0) {
- if (ret == BAD_STATE_E) return 0; /* no errors in queue */
- WOLFSSL_MSG("Error with pulling error node!");
- WOLFSSL_LEAVE("wolfSSL_ERR_get_error_line_data", ret);
- ret = 0 - ret; /* return absolute value of error */
-
- /* panic and try to clear out nodes */
- wc_ClearErrorNodes();
- }
-
- return (unsigned long)ret;
- }
- }
-
- ret = wc_PullErrorNode(file, NULL, line);
- if (ret < 0) {
- if (ret == BAD_STATE_E) return 0; /* no errors in queue */
- WOLFSSL_MSG("Error with pulling error node!");
- WOLFSSL_LEAVE("wolfSSL_ERR_get_error_line_data", ret);
- ret = 0 - ret; /* return absolute value of error */
-
- /* panic and try to clear out nodes */
- wc_ClearErrorNodes();
- }
-
- return (unsigned long)ret;
- }
-
-#endif /* OPENSSL_EXTRA */
-
-
-#if (defined(KEEP_PEER_CERT) && defined(SESSION_CERTS)) || \
- (defined(OPENSSL_ALL) && defined(HAVE_PKCS7))
- /* Decode the X509 DER encoded certificate into a WOLFSSL_X509 object.
- *
- * x509 WOLFSSL_X509 object to decode into.
- * in X509 DER data.
- * len Length of the X509 DER data.
- * returns the new certificate on success, otherwise NULL.
- */
- static int DecodeToX509(WOLFSSL_X509* x509, const byte* in, int len)
- {
- int ret;
- #ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert;
- #else
- DecodedCert cert[1];
- #endif
- if (x509 == NULL || in == NULL || len <= 0)
- return BAD_FUNC_ARG;
-
- #ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
- DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- return MEMORY_E;
- #endif
-
- /* Create a DecodedCert object and copy fields into WOLFSSL_X509 object.
- */
- InitDecodedCert(cert, (byte*)in, len, NULL);
- if ((ret = ParseCertRelative(cert, CERT_TYPE, 0, NULL)) == 0) {
- /* Check if x509 was not previously initialized by wolfSSL_X509_new() */
- if (x509->dynamicMemory != TRUE)
- InitX509(x509, 0, NULL);
- ret = CopyDecodedToX509(x509, cert);
- FreeDecodedCert(cert);
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
- #endif
-
- return ret;
- }
-#endif /* (KEEP_PEER_CERT && SESSION_CERTS) || (OPENSSL_ALL && HAVE_PKCS7) */
-
-
-#ifdef KEEP_PEER_CERT
- WOLFSSL_ABI
- WOLFSSL_X509* wolfSSL_get_peer_certificate(WOLFSSL* ssl)
- {
- WOLFSSL_ENTER("SSL_get_peer_certificate");
- if (ssl == NULL)
- return NULL;
-
- if (ssl->peerCert.issuer.sz)
- return &ssl->peerCert;
-#ifdef SESSION_CERTS
- else if (ssl->session.chain.count > 0) {
- if (DecodeToX509(&ssl->peerCert, ssl->session.chain.certs[0].buffer,
- ssl->session.chain.certs[0].length) == 0) {
- return &ssl->peerCert;
- }
- }
-#endif
- return 0;
- }
-
-#endif /* KEEP_PEER_CERT */
-
-#if defined(SESSION_CERTS)
-/* Return stack of peer certs.
- * If Qt or OPENSSL_ALL is defined then return ssl->peerCertChain.
- * All other cases return &ssl->session.chain
- * ssl->peerCertChain is type WOLFSSL_STACK*
- * ssl->session.chain is type WOLFSSL_X509_CHAIN
- * Caller does not need to free return. The stack is Free'd when WOLFSSL* ssl is.
- */
-WOLF_STACK_OF(WOLFSSL_X509)* wolfSSL_get_peer_cert_chain(const WOLFSSL* ssl)
-{
- WOLFSSL_STACK* sk;
- WOLFSSL_ENTER("wolfSSL_get_peer_cert_chain");
-
- if (ssl == NULL)
- return NULL;
-
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- if (ssl->peerCertChain == NULL)
- wolfSSL_set_peer_cert_chain((WOLFSSL*) ssl);
- sk = ssl->peerCertChain;
- #else
- sk = (WOLF_STACK_OF(WOLFSSL_X509)* )&ssl->session.chain;
- #endif
-
- if (sk == NULL) {
- WOLFSSL_MSG("Error: Null Peer Cert Chain");
- }
- return sk;
-}
-
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
-/* Builds up and creates a stack of peer certificates for ssl->peerCertChain
- based off of the ssl session chain. Returns stack of WOLFSSL_X509 certs or
- NULL on failure */
-WOLF_STACK_OF(WOLFSSL_X509)* wolfSSL_set_peer_cert_chain(WOLFSSL* ssl)
-{
- WOLFSSL_STACK* sk;
- WOLFSSL_X509* x509;
- int i = 0;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_set_peer_cert_chain");
- if ((ssl == NULL) || (ssl->session.chain.count == 0))
- return NULL;
-
- sk = wolfSSL_sk_X509_new();
- i = ssl->session.chain.count-1;
- for (; i >= 0; i--) {
- /* For servers, the peer certificate chain does not include the peer
- certificate, so do not add it to the stack */
- if (ssl->options.side == WOLFSSL_SERVER_END && i == 0)
- continue;
- x509 = wolfSSL_X509_new();
- if (x509 == NULL) {
- WOLFSSL_MSG("Error Creating X509");
- return NULL;
- }
- ret = DecodeToX509(x509, ssl->session.chain.certs[i].buffer,
- ssl->session.chain.certs[i].length);
-
- if (ret != 0 || wolfSSL_sk_X509_push(sk, x509) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error decoding cert");
- wolfSSL_X509_free(x509);
- wolfSSL_sk_X509_free(sk);
- return NULL;
- }
- }
-
- if (sk == NULL) {
- WOLFSSL_MSG("Null session chain");
- }
- /* This is Free'd when ssl is Free'd */
- ssl->peerCertChain = sk;
- return sk;
-}
-#endif /* OPENSSL_ALL || WOLFSSL_QT */
-#endif /* SESSION_CERTS */
-
-#ifndef NO_CERTS
-#if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
- defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
-
-/* user externally called free X509, if dynamic go ahead with free, otherwise
- * don't */
-static void ExternalFreeX509(WOLFSSL_X509* x509)
-{
- int doFree = 0;
-
- WOLFSSL_ENTER("ExternalFreeX509");
- if (x509) {
- if (x509->dynamicMemory) {
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
- if (wc_LockMutex(&x509->refMutex) != 0) {
- WOLFSSL_MSG("Couldn't lock x509 mutex");
- }
- /* only free if all references to it are done */
- x509->refCount--;
- if (x509->refCount == 0)
- doFree = 1;
- wc_UnLockMutex(&x509->refMutex);
- #else
- doFree = 1;
- #endif /* OPENSSL_EXTRA */
-
- if (doFree) {
- FreeX509(x509);
- XFREE(x509, x509->heap, DYNAMIC_TYPE_X509);
- }
- } else {
- WOLFSSL_MSG("free called on non dynamic object, not freeing");
- }
- }
-}
-
-/* Frees an external WOLFSSL_X509 structure */
-WOLFSSL_ABI
-void wolfSSL_X509_free(WOLFSSL_X509* x509)
-{
- WOLFSSL_ENTER("wolfSSL_FreeX509");
- ExternalFreeX509(x509);
-}
-
-
-/* copy name into in buffer, at most sz bytes, if buffer is null will
- malloc buffer, call responsible for freeing */
-WOLFSSL_ABI
-char* wolfSSL_X509_NAME_oneline(WOLFSSL_X509_NAME* name, char* in, int sz)
-{
- int copySz;
-
- if (name == NULL) {
- WOLFSSL_MSG("WOLFSSL_X509_NAME pointer was NULL");
- return NULL;
- }
-
- copySz = min(sz, name->sz);
-
- WOLFSSL_ENTER("wolfSSL_X509_NAME_oneline");
- if (!name->sz) return in;
-
- if (!in) {
- #ifdef WOLFSSL_STATIC_MEMORY
- WOLFSSL_MSG("Using static memory -- please pass in a buffer");
- return NULL;
- #else
- in = (char*)XMALLOC(name->sz, NULL, DYNAMIC_TYPE_OPENSSL);
- if (!in ) return in;
- copySz = name->sz;
- #endif
- }
-
- if (copySz <= 0)
- return in;
-
- XMEMCPY(in, name->name, copySz - 1);
- in[copySz - 1] = 0;
-
- return in;
-}
-
-#if defined(OPENSSL_EXTRA) && defined(XSNPRINTF)
-/* Copies X509 subject name into a buffer, with comma-separated name entries
- * (matching OpenSSL v1.0.0 format)
- * Example Output for Issuer:
- *
- * C=US, ST=Montana, L=Bozeman, O=Sawtooth, OU=Consulting,
- * CN=www.wolfssl.com, emailAddress=info@wolfssl.com
- */
-char* wolfSSL_X509_get_name_oneline(WOLFSSL_X509_NAME* name, char* in, int sz)
-{
- WOLFSSL_X509_NAME_ENTRY* entry;
- int nameSz, strSz, count, i, idx = 0;
- int totalSz = 0;
- char *str;
- char tmpBuf[256];
- char buf[80];
- const char* sn;
- WOLFSSL_ENTER("wolfSSL_X509_get_name_oneline");
-
- if (name == NULL) {
- WOLFSSL_MSG("wolfSSL_X509_get_subject_name failed");
- return NULL;
- }
- #ifdef WOLFSSL_STATIC_MEMORY
- if (!in) {
- WOLFSSL_MSG("Using static memory -- please pass in a buffer");
- return NULL;
- }
- #endif
-
- tmpBuf[0] = '\0'; /* Make sure tmpBuf is NULL terminated */
- /* Loop through X509 name entries and copy new format to buffer */
- count = wolfSSL_X509_NAME_entry_count(name);
- for (i = 0; i < count; i++) {
-
- /* Get name entry and size */
- entry = wolfSSL_X509_NAME_get_entry(name, i);
- if (entry == NULL) {
- WOLFSSL_MSG("X509_NAME_get_entry failed");
- return NULL;
- }
- nameSz = wolfSSL_X509_NAME_get_text_by_NID(name, entry->nid, buf,
- sizeof(buf));
- if (nameSz < 0) {
- WOLFSSL_MSG("X509_NAME_get_text_by_NID failed");
- return NULL;
- }
-
- /* Get short name */
- sn = wolfSSL_OBJ_nid2sn(entry->nid);
- if (sn == NULL) {
- WOLFSSL_MSG("OBJ_nid2sn failed");
- return NULL;
- }
-
- /* Copy sn and name text to buffer
- * Add extra strSz for '=', ',', ' ' and '\0' characters in XSNPRINTF.
- */
- if (i != count - 1) {
- strSz = (int)XSTRLEN(sn) + nameSz + 4;
- totalSz+= strSz;
- str = (char*)XMALLOC(strSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (str == NULL) {
- WOLFSSL_MSG("Memory error");
- return NULL;
- }
- XSNPRINTF(str, strSz, "%s=%s, ", sn, buf);
- }
- else {
- /* Copy last name entry
- * Add extra strSz for '=' and '\0' characters in XSNPRINTF.
- */
- strSz = (int)XSTRLEN(sn) + nameSz + 2;
- totalSz+= strSz;
- str = (char*)XMALLOC(strSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (str == NULL) {
- WOLFSSL_MSG("Memory error");
- return NULL;
- }
- XSNPRINTF(str, strSz, "%s=%s", sn, buf);
- }
- /* Copy string to tmpBuf */
- XSTRNCAT(tmpBuf, str, strSz);
- idx += strSz;
- XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- /* Allocate space based on total string size if no buffer was provided */
- if (!in) {
- in = (char*)XMALLOC(totalSz+1, NULL, DYNAMIC_TYPE_OPENSSL);
- if (in == NULL) {
- WOLFSSL_MSG("Memory error");
- return in;
- }
- }
- else {
- if (totalSz > sz) {
- WOLFSSL_MSG("Memory error");
- return NULL;
- }
- }
-
- XMEMCPY(in, tmpBuf, totalSz);
- in[totalSz] = '\0';
-
- return in;
-}
-#endif
-
-
-/* Wraps wolfSSL_X509_d2i
- *
- * returns a WOLFSSL_X509 structure pointer on success and NULL on fail
- */
-WOLFSSL_X509* wolfSSL_d2i_X509(WOLFSSL_X509** x509, const unsigned char** in,
- int len)
-{
- WOLFSSL_X509* newX509 = NULL;
-
- newX509 = wolfSSL_X509_d2i(x509, *in, len);
- if (newX509 != NULL) {
- *in += newX509->derCert->length;
- }
- return newX509;
-}
-
-
-WOLFSSL_X509* wolfSSL_X509_d2i(WOLFSSL_X509** x509, const byte* in, int len)
-{
- WOLFSSL_X509 *newX509 = NULL;
-
- WOLFSSL_ENTER("wolfSSL_X509_d2i");
-
- if (in != NULL && len != 0) {
- #ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert;
- #else
- DecodedCert cert[1];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
- DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- return NULL;
- #endif
-
- InitDecodedCert(cert, (byte*)in, len, NULL);
- if (ParseCertRelative(cert, CERT_TYPE, 0, NULL) == 0) {
- newX509 = wolfSSL_X509_new();
- if (newX509 != NULL) {
- if (CopyDecodedToX509(newX509, cert) != 0) {
- wolfSSL_X509_free(newX509);
- newX509 = NULL;
- }
- }
- }
- FreeDecodedCert(cert);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
- #endif
- }
-
- if (x509 != NULL)
- *x509 = newX509;
-
- return newX509;
-}
-#endif /* KEEP_PEER_CERT || SESSION_CERTS || OPENSSL_EXTRA ||
- OPENSSL_EXTRA_X509_SMALL */
-
-
-
-#if defined(OPENSSL_ALL) || defined(KEEP_OUR_CERT) || defined(KEEP_PEER_CERT) || \
- defined(SESSION_CERTS)
- /* return the next, if any, altname from the peer cert */
- WOLFSSL_ABI
- char* wolfSSL_X509_get_next_altname(WOLFSSL_X509* cert)
- {
- char* ret = NULL;
- WOLFSSL_ENTER("wolfSSL_X509_get_next_altname");
-
- /* don't have any to work with */
- if (cert == NULL || cert->altNames == NULL)
- return NULL;
-
- /* already went through them */
- if (cert->altNamesNext == NULL)
- return NULL;
-
- ret = cert->altNamesNext->name;
- cert->altNamesNext = cert->altNamesNext->next;
-
- return ret;
- }
-
- int wolfSSL_X509_get_isCA(WOLFSSL_X509* x509)
- {
- int isCA = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_isCA");
-
- if (x509 != NULL)
- isCA = x509->isCa;
-
- WOLFSSL_LEAVE("wolfSSL_X509_get_isCA", isCA);
-
- return isCA;
- }
-
- int wolfSSL_X509_get_signature(WOLFSSL_X509* x509,
- unsigned char* buf, int* bufSz)
- {
- WOLFSSL_ENTER("wolfSSL_X509_get_signature");
- if (x509 == NULL || bufSz == NULL || (*bufSz < (int)x509->sig.length &&
- buf != NULL))
- return WOLFSSL_FATAL_ERROR;
-
- if (buf != NULL)
- XMEMCPY(buf, x509->sig.buffer, x509->sig.length);
- *bufSz = x509->sig.length;
-
- return WOLFSSL_SUCCESS;
- }
-
-
- /* Getter function that copies over the DER public key buffer to "buf" and
- * sets the size in bufSz. If "buf" is NULL then just bufSz is set to needed
- * buffer size. "bufSz" passed in should initially be set by the user to be
- * the size of "buf". This gets checked to make sure the buffer is large
- * enough to hold the public key.
- *
- * Note: this is the X.509 form of key with "header" info.
- * return WOLFSSL_SUCCESS on success
- */
- int wolfSSL_X509_get_pubkey_buffer(WOLFSSL_X509* x509,
- unsigned char* buf, int* bufSz)
- {
- #ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert;
- #else
- DecodedCert cert[1];
- #endif
- word32 idx;
- const byte* der;
- int length = 0;
- int ret, derSz = 0;
- int badDate = 0;
- const byte* pubKeyX509 = NULL;
- int pubKeyX509Sz = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_pubkey_buffer");
- if (x509 == NULL || bufSz == NULL) {
- WOLFSSL_LEAVE("wolfSSL_X509_get_pubkey_buffer", BAD_FUNC_ARG);
- return WOLFSSL_FATAL_ERROR;
- }
-
-
- #ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert),
- x509->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (cert == NULL) {
- WOLFSSL_LEAVE("wolfSSL_X509_get_pubkey_buffer", MEMORY_E);
- return WOLFSSL_FATAL_ERROR;
- }
- #endif
-
- der = wolfSSL_X509_get_der(x509, &derSz);
- InitDecodedCert(cert, der, derSz, NULL);
- ret = wc_GetPubX509(cert, 0, &badDate);
- if (ret >= 0) {
- idx = cert->srcIdx;
- pubKeyX509 = cert->source + cert->srcIdx;
- ret = GetSequence(cert->source, &cert->srcIdx, &length,
- cert->maxIdx);
- pubKeyX509Sz = length + (cert->srcIdx - idx);
- }
-
- FreeDecodedCert(cert);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, x509->heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
-
- if (ret < 0) {
- WOLFSSL_LEAVE("wolfSSL_X509_get_pubkey_buffer", ret);
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (buf != NULL) {
- if (pubKeyX509Sz > *bufSz) {
- WOLFSSL_LEAVE("wolfSSL_X509_get_pubkey_buffer", BUFFER_E);
- return WOLFSSL_FATAL_ERROR;
- }
- XMEMCPY(buf, pubKeyX509, pubKeyX509Sz);
- }
- *bufSz = pubKeyX509Sz;
-
- return WOLFSSL_SUCCESS;
- }
-
-
- /* Getter function for the public key OID value
- * return public key OID stored in WOLFSSL_X509 structure */
- int wolfSSL_X509_get_pubkey_type(WOLFSSL_X509* x509)
- {
- if (x509 == NULL)
- return WOLFSSL_FAILURE;
- return x509->pubKeyOID;
- }
-
- /* write X509 serial number in unsigned binary to buffer
- buffer needs to be at least EXTERNAL_SERIAL_SIZE (32) for all cases
- return WOLFSSL_SUCCESS on success */
- int wolfSSL_X509_get_serial_number(WOLFSSL_X509* x509,
- byte* in, int* inOutSz)
- {
- WOLFSSL_ENTER("wolfSSL_X509_get_serial_number");
- if (x509 == NULL || in == NULL ||
- inOutSz == NULL || *inOutSz < x509->serialSz)
- return BAD_FUNC_ARG;
-
- XMEMCPY(in, x509->serial, x509->serialSz);
- *inOutSz = x509->serialSz;
-
- return WOLFSSL_SUCCESS;
- }
-
- /* not an openssl compatibility function - getting for derCert */
- const byte* wolfSSL_X509_get_der(WOLFSSL_X509* x509, int* outSz)
- {
- WOLFSSL_ENTER("wolfSSL_X509_get_der");
-
- if (x509 == NULL || x509->derCert == NULL || outSz == NULL)
- return NULL;
-
- *outSz = (int)x509->derCert->length;
- return x509->derCert->buffer;
- }
-
- /* used by JSSE (not a standard compatibility function) */
- /* this is not thread safe */
- WOLFSSL_ABI
- const byte* wolfSSL_X509_notBefore(WOLFSSL_X509* x509)
- {
- static byte notBeforeData[CTC_DATE_SIZE]; /* temp buffer for date */
- WOLFSSL_ENTER("wolfSSL_X509_notBefore");
-
- if (x509 == NULL)
- return NULL;
-
- XMEMSET(notBeforeData, 0, sizeof(notBeforeData));
- notBeforeData[0] = (byte)x509->notBefore.type;
- notBeforeData[1] = (byte)x509->notBefore.length;
- XMEMCPY(¬BeforeData[2], x509->notBefore.data, x509->notBefore.length);
-
- return notBeforeData;
- }
- /* used by JSSE (not a standard compatibility function) */
- /* this is not thread safe */
- WOLFSSL_ABI
- const byte* wolfSSL_X509_notAfter(WOLFSSL_X509* x509)
- {
- static byte notAfterData[CTC_DATE_SIZE]; /* temp buffer for date */
- WOLFSSL_ENTER("wolfSSL_X509_notAfter");
-
- if (x509 == NULL)
- return NULL;
-
- XMEMSET(notAfterData, 0, sizeof(notAfterData));
- notAfterData[0] = (byte)x509->notAfter.type;
- notAfterData[1] = (byte)x509->notAfter.length;
- XMEMCPY(¬AfterData[2], x509->notAfter.data, x509->notAfter.length);
-
- return notAfterData;
- }
-
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
- WOLFSSL_ASN1_TIME* wolfSSL_X509_gmtime_adj(WOLFSSL_ASN1_TIME *s, long adj)
- {
- (void) s;
- (void) adj;
- WOLFSSL_STUB("wolfSSL_X509_gmtime_adj");
- return NULL;
- }
- #endif
-
- /* get the buffer to be signed (tbs) from the WOLFSSL_X509 certificate
- *
- * outSz : gets set to the size of the buffer
- * returns a pointer to the internal buffer at the location of TBS on
- * on success and NULL on failure.
- */
- const unsigned char* wolfSSL_X509_get_tbs(WOLFSSL_X509* x509, int* outSz)
- {
- int sz = 0, len;
- unsigned int idx = 0, tmpIdx;
- const unsigned char* der = NULL;
- const unsigned char* tbs = NULL;
-
- if (x509 == NULL || outSz == NULL) {
- return NULL;
- }
-
- der = wolfSSL_X509_get_der(x509, &sz);
- if (der == NULL) {
- return NULL;
- }
-
- if (GetSequence(der, &idx, &len, sz) < 0) {
- return NULL;
- }
- tbs = der + idx;
- tmpIdx = idx;
- if (GetSequence(der, &idx, &len, sz) < 0) {
- return NULL;
- }
- *outSz = len + (idx - tmpIdx);
- return tbs;
- }
-
- int wolfSSL_X509_version(WOLFSSL_X509* x509)
- {
- WOLFSSL_ENTER("wolfSSL_X509_version");
-
- if (x509 == NULL)
- return 0;
-
- return x509->version;
- }
-
-#ifdef WOLFSSL_SEP
-
-/* copy oid into in buffer, at most *inOutSz bytes, if buffer is null will
- malloc buffer, call responsible for freeing. Actual size returned in
- *inOutSz. Requires inOutSz be non-null */
-byte* wolfSSL_X509_get_device_type(WOLFSSL_X509* x509, byte* in, int *inOutSz)
-{
- int copySz;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_dev_type");
- if (inOutSz == NULL) return NULL;
- if (!x509->deviceTypeSz) return in;
-
- copySz = min(*inOutSz, x509->deviceTypeSz);
-
- if (!in) {
- #ifdef WOLFSSL_STATIC_MEMORY
- WOLFSSL_MSG("Using static memory -- please pass in a buffer");
- return NULL;
- #else
- in = (byte*)XMALLOC(x509->deviceTypeSz, 0, DYNAMIC_TYPE_OPENSSL);
- if (!in) return in;
- copySz = x509->deviceTypeSz;
- #endif
- }
-
- XMEMCPY(in, x509->deviceType, copySz);
- *inOutSz = copySz;
-
- return in;
-}
-
-
-byte* wolfSSL_X509_get_hw_type(WOLFSSL_X509* x509, byte* in, int* inOutSz)
-{
- int copySz;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_hw_type");
- if (inOutSz == NULL) return NULL;
- if (!x509->hwTypeSz) return in;
-
- copySz = min(*inOutSz, x509->hwTypeSz);
-
- if (!in) {
- #ifdef WOLFSSL_STATIC_MEMORY
- WOLFSSL_MSG("Using static memory -- please pass in a buffer");
- return NULL;
- #else
- in = (byte*)XMALLOC(x509->hwTypeSz, 0, DYNAMIC_TYPE_OPENSSL);
- if (!in) return in;
- copySz = x509->hwTypeSz;
- #endif
- }
-
- XMEMCPY(in, x509->hwType, copySz);
- *inOutSz = copySz;
-
- return in;
-}
-
-
-byte* wolfSSL_X509_get_hw_serial_number(WOLFSSL_X509* x509,byte* in,
- int* inOutSz)
-{
- int copySz;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_hw_serial_number");
- if (inOutSz == NULL) return NULL;
- if (!x509->hwTypeSz) return in;
-
- copySz = min(*inOutSz, x509->hwSerialNumSz);
-
- if (!in) {
- #ifdef WOLFSSL_STATIC_MEMORY
- WOLFSSL_MSG("Using static memory -- please pass in a buffer");
- return NULL;
- #else
- in = (byte*)XMALLOC(x509->hwSerialNumSz, 0, DYNAMIC_TYPE_OPENSSL);
- if (!in) return in;
- copySz = x509->hwSerialNumSz;
- #endif
- }
-
- XMEMCPY(in, x509->hwSerialNum, copySz);
- *inOutSz = copySz;
-
- return in;
-}
-
-#endif /* WOLFSSL_SEP */
-
-/* require OPENSSL_EXTRA since wolfSSL_X509_free is wrapped by OPENSSL_EXTRA */
-#if !defined(NO_CERTS) && defined(OPENSSL_EXTRA)
-
-WOLFSSL_ASN1_TIME* wolfSSL_X509_get_notBefore(const WOLFSSL_X509* x509)
-{
- WOLFSSL_ENTER("wolfSSL_X509_get_notBefore");
-
- if (x509 == NULL)
- return NULL;
-
- return (WOLFSSL_ASN1_TIME*)&x509->notBefore;
-}
-
-
-WOLFSSL_ASN1_TIME* wolfSSL_X509_get_notAfter(const WOLFSSL_X509* x509)
-{
- WOLFSSL_ENTER("wolfSSL_X509_get_notAfter");
-
- if (x509 == NULL)
- return NULL;
-
- return (WOLFSSL_ASN1_TIME*)&x509->notAfter;
-}
-
-
-/* return 1 on success 0 on fail */
-int wolfSSL_sk_X509_push(WOLF_STACK_OF(WOLFSSL_X509_NAME)* sk, WOLFSSL_X509* x509)
-{
- WOLFSSL_STACK* node;
- WOLFSSL_ENTER("wolfSSL_sk_X509_push");
-
- if (sk == NULL || x509 == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* no previous values in stack */
- if (sk->data.x509 == NULL) {
- sk->data.x509 = x509;
- sk->num += 1;
- return WOLFSSL_SUCCESS;
- }
-
- /* stack already has value(s) create a new node and add more */
- node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_X509);
- if (node == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
- XMEMSET(node, 0, sizeof(WOLFSSL_STACK));
-
- /* push new x509 onto head of stack */
- node->data.x509 = sk->data.x509;
- node->next = sk->next;
- node->type = sk->type;
- sk->next = node;
- sk->data.x509 = x509;
- sk->num += 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-WOLFSSL_X509* wolfSSL_sk_X509_pop(WOLF_STACK_OF(WOLFSSL_X509_NAME)* sk) {
- WOLFSSL_STACK* node;
- WOLFSSL_X509* x509;
-
- if (sk == NULL) {
- return NULL;
- }
-
- node = sk->next;
- x509 = sk->data.x509;
-
- if (node != NULL) { /* update sk and remove node from stack */
- sk->data.x509 = node->data.x509;
- sk->next = node->next;
- XFREE(node, NULL, DYNAMIC_TYPE_X509);
- }
- else { /* last x509 in stack */
- sk->data.x509 = NULL;
- }
-
- if (sk->num > 0) {
- sk->num -= 1;
- }
-
- return x509;
-}
-
-/* Getter function for WOLFSSL_X509 pointer
- *
- * sk is the stack to retrieve pointer from
- * i is the index value in stack
- *
- * returns a pointer to a WOLFSSL_X509 structure on success and NULL on
- * fail
- */
-WOLFSSL_X509* wolfSSL_sk_X509_value(STACK_OF(WOLFSSL_X509)* sk, int i)
-{
- WOLFSSL_ENTER("wolfSSL_sk_X509_value");
-
- for (; sk != NULL && i > 0; i--)
- sk = sk->next;
-
- if (i != 0 || sk == NULL)
- return NULL;
- return sk->data.x509;
-}
-
-WOLFSSL_X509* wolfSSL_sk_X509_shift(WOLF_STACK_OF(WOLFSSL_X509)* sk)
-{
- return wolfSSL_sk_X509_pop(sk);
-}
-
-#ifndef NO_WOLFSSL_STUB
-void* wolfSSL_sk_X509_OBJECT_value(WOLF_STACK_OF(WOLFSSL_X509_OBJECT)* sk, int x)
-{
- (void) sk;
- (void) x;
- return NULL;
-}
-#endif
-
-
-/* Free's all nodes in X509 stack. This is different then wolfSSL_sk_X509_free
- * in that it allows for choosing the function to use when freeing an X509s.
- *
- * sk stack to free nodes in
- * f X509 free function
- */
-void wolfSSL_sk_X509_pop_free(STACK_OF(WOLFSSL_X509)* sk,
- void (*f) (WOLFSSL_X509*))
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_X509_pop_free");
-
- if (sk == NULL) {
- return;
- }
-
- /* parse through stack freeing each node */
- node = sk->next;
- while (node && sk->num > 1) {
- WOLFSSL_STACK* tmp = node;
- node = node->next;
-
- if (f)
- f(tmp->data.x509);
- else
- wolfSSL_X509_free(tmp->data.x509);
- tmp->data.x509 = NULL;
- XFREE(tmp, NULL, DYNAMIC_TYPE_X509);
- sk->num -= 1;
- }
-
- /* free head of stack */
- if (sk->num == 1) {
- if (f)
- f(sk->data.x509);
- else
- wolfSSL_X509_free(sk->data.x509);
- sk->data.x509 = NULL;
- }
- XFREE(sk, NULL, DYNAMIC_TYPE_X509);
-}
-
-
-/* free structure for x509 stack */
-void wolfSSL_sk_X509_free(WOLF_STACK_OF(WOLFSSL_X509)* sk)
-{
- wolfSSL_sk_X509_pop_free(sk, NULL);
-}
-
-#endif /* NO_CERTS && OPENSSL_EXTRA */
-
-#if defined(OPENSSL_ALL) || defined (WOLFSSL_QT)
-/* return 1 on success 0 on fail */
-int wolfSSL_sk_ACCESS_DESCRIPTION_push(WOLF_STACK_OF(ACCESS_DESCRIPTION)* sk,
- WOLFSSL_ACCESS_DESCRIPTION* access)
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_ACCESS_DESCRIPTION_push");
-
- if (sk == NULL || access == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* no previous values in stack */
- if (sk->data.access == NULL) {
- sk->data.access = access;
- sk->num += 1;
- return WOLFSSL_SUCCESS;
- }
-
- /* stack already has value(s) create a new node and add more */
- node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_ASN1);
- if (node == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
- XMEMSET(node, 0, sizeof(WOLFSSL_STACK));
-
- /* push new obj onto head of stack */
- node->data.access = sk->data.access;
- node->next = sk->next;
- node->type = sk->type;
- sk->next = node;
- sk->data.access = access;
- sk->num += 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Frees all nodes in ACCESS_DESCRIPTION stack
-*
-* sk stack of nodes to free
-* f free function to use, not called with wolfSSL
-*/
-void wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(WOLFSSL_STACK* sk,
- void (*f) (WOLFSSL_ACCESS_DESCRIPTION*))
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_ACCESS_DESCRIPTION_pop_free");
-
- if (sk == NULL) {
- return;
- }
-
- /* parse through stack freeing each node */
- node = sk->next;
- while (node && sk->num > 1) {
- WOLFSSL_STACK* tmp = node;
- node = node->next;
-
- if (f)
- f(tmp->data.access);
- else
- wolfSSL_ACCESS_DESCRIPTION_free(tmp->data.access);
- tmp->data.access = NULL;
- XFREE(tmp, NULL, DYNAMIC_TYPE_ASN1);
- sk->num -= 1;
- }
-
- /* free head of stack */
- if (sk->num == 1) {
- if (f)
- f(sk->data.access);
- else {
- if(sk->data.access->method) {
-
- wolfSSL_ASN1_OBJECT_free(sk->data.access->method);
- }
- if(sk->data.access->location) {
- wolfSSL_GENERAL_NAME_free(sk->data.access->location);
- }
- }
- sk->data.access = NULL;
- }
- XFREE(sk, NULL, DYNAMIC_TYPE_ASN1);
-}
-
-void wolfSSL_sk_ACCESS_DESCRIPTION_free(WOLFSSL_STACK* sk)
-{
- wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(sk, NULL);
-}
-
-void wolfSSL_ACCESS_DESCRIPTION_free(WOLFSSL_ACCESS_DESCRIPTION* access)
-{
- WOLFSSL_ENTER("wolfSSL_ACCESS_DESCRIPTION_free");
- if (access == NULL)
- return;
-
- if (access->method)
- wolfSSL_ASN1_OBJECT_free(access->method);
- if (access->location)
- wolfSSL_GENERAL_NAME_free(access->location);
-
- /* access = NULL, don't try to access or double free it */
-}
-#endif
-
-#ifdef OPENSSL_EXTRA
-
-/* create a generic wolfSSL stack node
- * returns a new WOLFSSL_STACK structure on success */
-WOLFSSL_STACK* wolfSSL_sk_new_node(void* heap)
-{
- WOLFSSL_STACK* sk;
- WOLFSSL_ENTER("wolfSSL_sk_new_node");
-
- sk = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), heap,
- DYNAMIC_TYPE_OPENSSL);
- if (sk != NULL) {
- XMEMSET(sk, 0, sizeof(*sk));
- sk->heap = heap;
- }
-
- return sk;
-}
-
-/* free's node but does not free internal data such as in->data.x509 */
-void wolfSSL_sk_free_node(WOLFSSL_STACK* in)
-{
- if (in != NULL) {
- XFREE(in, in->heap, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-/* pushes node "in" onto "stack" and returns pointer to the new stack on success
- * also handles internal "num" for number of nodes on stack
- * return WOLFSSL_SUCCESS on success
- */
-int wolfSSL_sk_push_node(WOLFSSL_STACK** stack, WOLFSSL_STACK* in)
-{
- if (stack == NULL || in == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- if (*stack == NULL) {
- in->num = 1;
- *stack = in;
- return WOLFSSL_SUCCESS;
- }
-
- in->num = (*stack)->num + 1;
- in->next = *stack;
- *stack = in;
- return WOLFSSL_SUCCESS;
-}
-
-/* return 1 on success 0 on fail */
-int wolfSSL_sk_push(WOLFSSL_STACK* sk, const void *data)
-{
- int ret = WOLFSSL_FAILURE;
- WOLFSSL_ENTER("wolfSSL_sk_push");
-
- switch (sk->type) {
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- case STACK_TYPE_X509:
- ret = wolfSSL_sk_X509_push(sk, (WOLFSSL_X509*) data);
- break;
- #ifndef NO_WOLFSSL_STUB
- case STACK_TYPE_CIPHER:
- ret = wolfSSL_sk_CIPHER_push(sk, (WOLFSSL_CIPHER*) data);
- break;
- #endif
- case STACK_TYPE_GEN_NAME:
- ret = wolfSSL_sk_ASN1_OBJECT_push(sk, (WOLFSSL_ASN1_OBJECT*) data);
- break;
- case STACK_TYPE_ACCESS_DESCRIPTION:
- ret = wolfSSL_sk_ACCESS_DESCRIPTION_push(sk,
- (WOLFSSL_ACCESS_DESCRIPTION*) data);
- break;
- case STACK_TYPE_NULL:
- ret = wolfSSL_sk_GENERIC_push(sk, (void*) data);
- break;
- case STACK_TYPE_OBJ:
- ret = wolfSSL_sk_ASN1_OBJECT_push(sk, (WOLFSSL_ASN1_OBJECT*) data);
- break;
- #endif
- default:
- ret = wolfSSL_sk_ASN1_OBJECT_push(sk, (WOLFSSL_ASN1_OBJECT*) data);
- break;
- }
-
- return ret;
-}
-
-/* Creates and returns new GENERAL_NAME structure */
-WOLFSSL_GENERAL_NAME* wolfSSL_GENERAL_NAME_new(void)
-{
- WOLFSSL_GENERAL_NAME* gn;
- WOLFSSL_ENTER("GENERAL_NAME_new");
-
- gn = (WOLFSSL_GENERAL_NAME*)XMALLOC(sizeof(WOLFSSL_GENERAL_NAME), NULL,
- DYNAMIC_TYPE_ASN1);
- if (gn == NULL) {
- return NULL;
- }
- XMEMSET(gn, 0, sizeof(WOLFSSL_GENERAL_NAME));
-
- gn->d.ia5 = wolfSSL_ASN1_STRING_new();
- if (gn->d.ia5 == NULL) {
- WOLFSSL_MSG("Issue creating ASN1_STRING struct");
- wolfSSL_GENERAL_NAME_free(gn);
- return NULL;
- }
- return gn;
-}
-
-/* return 1 on success 0 on fail */
-int wolfSSL_sk_GENERAL_NAME_push(WOLF_STACK_OF(WOLFSSL_GENERAL_NAME)* sk,
- WOLFSSL_GENERAL_NAME* gn)
-{
- WOLFSSL_STACK* node;
- WOLFSSL_ENTER("wolfSSL_sk_GENERAL_NAME_push");
-
- if (sk == NULL || gn == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* no previous values in stack */
- if (sk->data.gn == NULL) {
- sk->data.gn = gn;
- sk->num += 1;
-
- return WOLFSSL_SUCCESS;
- }
-
- /* stack already has value(s) create a new node and add more */
- node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_ASN1);
- if (node == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
- XMEMSET(node, 0, sizeof(WOLFSSL_STACK));
-
- /* push new obj onto head of stack */
- node->data.gn = sk->data.gn;
- node->next = sk->next;
- sk->next = node;
- sk->data.gn = gn;
- sk->num += 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Returns the general name at index i from the stack
- *
- * sk stack to get general name from
- * idx index to get
- *
- * return a pointer to the internal node of the stack
- */
-WOLFSSL_GENERAL_NAME* wolfSSL_sk_GENERAL_NAME_value(WOLFSSL_STACK* sk, int idx)
-{
- WOLFSSL_STACK* ret;
-
- if (sk == NULL) {
- return NULL;
- }
-
- ret = wolfSSL_sk_get_node(sk, idx);
- if (ret != NULL) {
- return ret->data.gn;
- }
- return NULL;
-}
-
-/* Gets the number of nodes in the stack
- *
- * sk stack to get the number of nodes from
- *
- * returns the number of nodes, -1 if no nodes
- */
-int wolfSSL_sk_GENERAL_NAME_num(WOLFSSL_STACK* sk)
-{
- WOLFSSL_ENTER("wolfSSL_sk_GENERAL_NAME_num");
-
- if (sk == NULL) {
- return -1;
- }
-
- return (int)sk->num;
-}
-
-/* Frees all nodes in a GENERAL NAME stack
- *
- * sk stack of nodes to free
- * f free function to use, not called with wolfSSL
- */
-void wolfSSL_sk_GENERAL_NAME_pop_free(WOLFSSL_STACK* sk,
- void (*f) (WOLFSSL_GENERAL_NAME*))
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_GENERAL_NAME_pop_free");
-
- if (sk == NULL) {
- return;
- }
-
- /* parse through stack freeing each node */
- node = sk->next;
- while (node && sk->num > 1) {
- WOLFSSL_STACK* tmp = node;
- node = node->next;
-
- if (f)
- f(tmp->data.gn);
- else
- wolfSSL_GENERAL_NAME_free(tmp->data.gn);
- XFREE(tmp, NULL, DYNAMIC_TYPE_ASN1);
- sk->num -= 1;
- }
-
- /* free head of stack */
- if (sk->num == 1) {
- if (f)
- f(sk->data.gn);
- else
- wolfSSL_GENERAL_NAME_free(sk->data.gn);
- }
- XFREE(sk, NULL, DYNAMIC_TYPE_ASN1);
-}
-
-void wolfSSL_sk_GENERAL_NAME_free(WOLFSSL_STACK* sk)
-{
- WOLFSSL_ENTER("sk_GENERAL_NAME_free");
- wolfSSL_sk_GENERAL_NAME_pop_free(sk, NULL);
-}
-
-
-/* returns the number of nodes in stack on success and WOLFSSL_FATAL_ERROR
- * on fail */
-int wolfSSL_sk_ACCESS_DESCRIPTION_num(WOLFSSL_STACK* sk)
-{
- if (sk == NULL) {
- return WOLFSSL_FATAL_ERROR;
- }
-
- return (int)sk->num;
-}
-
-#ifndef NO_WOLFSSL_STUB
-/* similar to call to sk_ACCESS_DESCRIPTION_pop_free */
-void wolfSSL_AUTHORITY_INFO_ACCESS_free(
- WOLF_STACK_OF(WOLFSSL_ACCESS_DESCRIPTION)* sk)
-{
- WOLFSSL_STUB("wolfSSL_AUTHORITY_INFO_ACCESS_free");
- (void)sk;
-}
-#endif
-
-/* returns the node at index "idx", NULL if not found */
-WOLFSSL_STACK* wolfSSL_sk_get_node(WOLFSSL_STACK* sk, int idx)
-{
- int i;
- WOLFSSL_STACK* ret = NULL;
- WOLFSSL_STACK* current;
-
- current = sk;
- for (i = 0; i <= idx && current != NULL; i++) {
- if (i == idx) {
- ret = current;
- break;
- }
- current = current->next;
- }
- return ret;
-}
-
-/* returns NULL on fail and pointer to internal data on success */
-WOLFSSL_ACCESS_DESCRIPTION* wolfSSL_sk_ACCESS_DESCRIPTION_value(
- WOLFSSL_STACK* sk, int idx)
-{
- WOLFSSL_STACK* ret;
-
- if (sk == NULL) {
- return NULL;
- }
-
- ret = wolfSSL_sk_get_node(sk, idx);
- if (ret != NULL) {
- return ret->data.access;
- }
- return NULL;
-}
-
-/* Frees GENERAL_NAME objects.
-*/
-void wolfSSL_GENERAL_NAME_free(WOLFSSL_GENERAL_NAME* name)
-{
- WOLFSSL_ENTER("wolfSSL_GENERAL_NAME_Free");
- if(name != NULL) {
- if (name->d.dNSName != NULL) {
- wolfSSL_ASN1_STRING_free(name->d.dNSName);
- name->d.dNSName = NULL;
- }
- if (name->d.uniformResourceIdentifier != NULL) {
- wolfSSL_ASN1_STRING_free(name->d.uniformResourceIdentifier);
- name->d.uniformResourceIdentifier = NULL;
- }
- if (name->d.iPAddress != NULL) {
- wolfSSL_ASN1_STRING_free(name->d.iPAddress);
- name->d.iPAddress = NULL;
- }
- if (name->d.registeredID != NULL) {
- wolfSSL_ASN1_OBJECT_free(name->d.registeredID);
- name->d.registeredID = NULL;
- }
- if (name->d.ia5 != NULL) {
- wolfSSL_ASN1_STRING_free(name->d.ia5);
- name->d.ia5 = NULL;
- }
- XFREE(name, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-void wolfSSL_GENERAL_NAMES_free(WOLFSSL_GENERAL_NAMES *gens)
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_GENERAL_NAMES_free");
-
- if (gens == NULL) {
- return;
- }
-
- /* parse through stack freeing each node */
- node = gens->next;
- while (gens->num > 1) {
- WOLFSSL_STACK* tmp = node;
- node = node->next;
-
- wolfSSL_ASN1_OBJECT_free(tmp->data.obj);
- XFREE(tmp, NULL, DYNAMIC_TYPE_ASN1);
- gens->num -= 1;
- }
-
- /* free head of stack */
- if (gens->num == 1) {
- wolfSSL_ASN1_OBJECT_free(gens->data.obj);
- }
- XFREE(gens, NULL, DYNAMIC_TYPE_ASN1);
-}
-
-#if defined(OPENSSL_ALL)
-WOLF_STACK_OF(WOLFSSL_X509_EXTENSION)* wolfSSL_sk_X509_EXTENSION_new_null(void)
-{
- WOLFSSL_STACK* sk = wolfSSL_sk_new_node(NULL);
- if (sk) {
- sk->type = STACK_TYPE_X509_EXT;
- }
-
- return (WOLF_STACK_OF(WOLFSSL_X509_EXTENSION)*)sk;;
-}
-
-/* returns the number of nodes on the stack */
-int wolfSSL_sk_X509_EXTENSION_num(WOLF_STACK_OF(WOLFSSL_X509_EXTENSION)* sk)
-{
- if (sk != NULL) {
- return (int)sk->num;
- }
- return WOLFSSL_FATAL_ERROR;
-}
-
-
-/* returns null on failure and pointer to internal value on success */
-WOLFSSL_X509_EXTENSION* wolfSSL_sk_X509_EXTENSION_value(
- WOLF_STACK_OF(WOLFSSL_X509_EXTENSION)* sk, int idx)
-{
- WOLFSSL_STACK* ret;
-
- if (sk == NULL) {
- return NULL;
- }
-
- ret = wolfSSL_sk_get_node(sk, idx);
- if (ret != NULL) {
- return ret->data.ext;
- }
- return NULL;
-}
-
-/* frees all of the nodes and the values in stack */
-void wolfSSL_sk_X509_EXTENSION_pop_free(
- WOLF_STACK_OF(WOLFSSL_X509_EXTENSION)* sk,
- void (*f) (WOLFSSL_X509_EXTENSION*))
-{
- WOLFSSL_STACK* current;
-
- if (sk == NULL) {
- return;
- }
-
- current = sk;
- while (current != NULL) {
- WOLFSSL_STACK* toFree = current;
- current = current->next;
-
- if (f)
- f(toFree->data.ext);
- wolfSSL_sk_free_node(toFree);
- }
-}
-
-#if defined(HAVE_ECC)
-/* Copies ecc_key into new WOLFSSL_EC_KEY object
- *
- * src : EC_KEY to duplicate. If EC_KEY is not null, create new EC_KEY and copy
- * internal ecc_key from src to dup.
- *
- * Returns pointer to duplicate EC_KEY.
- */
-WOLFSSL_EC_KEY *wolfSSL_EC_KEY_dup(const WOLFSSL_EC_KEY *src)
-{
- WOLFSSL_EC_KEY *dup;
- ecc_key *key, *srcKey;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_EC_KEY_dup");
-
- if (src == NULL || src->internal == NULL || src->group == NULL || \
- src->pub_key == NULL || src->priv_key == NULL) {
-
- WOLFSSL_MSG("src NULL error");
- return NULL;
- }
-
- dup = wolfSSL_EC_KEY_new();
- if (dup == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_new error");
- return NULL;
- }
-
- key = (ecc_key*)dup->internal;
- if (key == NULL) {
- WOLFSSL_MSG("ecc_key NULL error");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
- srcKey = (ecc_key*)src->internal;
-
- /* ecc_key */
- /* copy pubkey */
- ret = wc_ecc_copy_point(&srcKey->pubkey, &key->pubkey);
- if (ret != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_copy_point error");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
-
- /* copy private key k */
- ret = mp_copy(&srcKey->k, &key->k);
- if (ret != MP_OKAY) {
- WOLFSSL_MSG("mp_copy error");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
-
- /* copy domain parameters */
- if (srcKey->dp) {
- ret = wc_ecc_set_curve(key, 0, srcKey->dp->id);
- if (ret != 0) {
- WOLFSSL_MSG("wc_ecc_set_curve error");
- return NULL;
- }
- }
-
- key->type = srcKey->type;
- key->idx = srcKey->idx;
- key->state = srcKey->state;
- key->flags = srcKey->flags;
-
- /* Copy group */
- if (dup->group == NULL) {
- WOLFSSL_MSG("EC_GROUP_new_by_curve_name error");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
-
- dup->group->curve_idx = src->group->curve_idx;
- dup->group->curve_nid = src->group->curve_nid;
- dup->group->curve_oid = src->group->curve_oid;
-
- /* Copy public key */
- if (src->pub_key->internal == NULL || dup->pub_key->internal == NULL) {
- WOLFSSL_MSG("NULL pub_key error");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
-
- /* Copy public key internal */
- ret = wc_ecc_copy_point((ecc_point*)src->pub_key->internal, \
- (ecc_point*)dup->pub_key->internal);
- if (ret != MP_OKAY) {
- WOLFSSL_MSG("ecc_copy_point error");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
-
- /* Copy X, Y, Z */
- dup->pub_key->X = wolfSSL_BN_dup(src->pub_key->X);
- if (!dup->pub_key->X && src->pub_key->X) {
- WOLFSSL_MSG("Error copying EC_POINT");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
- dup->pub_key->Y = wolfSSL_BN_dup(src->pub_key->Y);
- if (!dup->pub_key->Y && src->pub_key->Y) {
- WOLFSSL_MSG("Error copying EC_POINT");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
- dup->pub_key->Z = wolfSSL_BN_dup(src->pub_key->Z);
- if (!dup->pub_key->Z && src->pub_key->Z) {
- WOLFSSL_MSG("Error copying EC_POINT");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
-
- dup->pub_key->inSet = src->pub_key->inSet;
- dup->pub_key->exSet = src->pub_key->exSet;
-
- /* Copy private key */
- if (src->priv_key->internal == NULL || dup->priv_key->internal == NULL) {
- WOLFSSL_MSG("NULL priv_key error");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
-
- /* Free priv_key before call to dup function */
- wolfSSL_BN_free(dup->priv_key);
- dup->priv_key = wolfSSL_BN_dup(src->priv_key);
- if (dup->priv_key == NULL) {
- WOLFSSL_MSG("BN_dup error");
- wolfSSL_EC_KEY_free(dup);
- return NULL;
- }
-
- return dup;
-
-}
-#endif /* HAVE_ECC */
-
-#if !defined(NO_DH)
-int wolfSSL_DH_check(const WOLFSSL_DH *dh, int *codes)
-{
- int isPrime = MP_NO, codeTmp = 0;
- WC_RNG rng;
-
- WOLFSSL_ENTER("wolfSSL_DH_check");
- if (dh == NULL){
- return WOLFSSL_FAILURE;
- }
-
- if (dh->g == NULL || dh->g->internal == NULL){
- codeTmp = DH_NOT_SUITABLE_GENERATOR;
- }
-
- if (dh->p == NULL || dh->p->internal == NULL){
- codeTmp = DH_CHECK_P_NOT_PRIME;
- }
- else
- {
- /* test if dh->p has prime */
- if (wc_InitRng(&rng) == 0){
- mp_prime_is_prime_ex((mp_int*)dh->p->internal,8,&isPrime,&rng);
- }
- else {
- WOLFSSL_MSG("Error initializing rng\n");
- return WOLFSSL_FAILURE;
- }
- wc_FreeRng(&rng);
- if (isPrime != MP_YES){
- codeTmp = DH_CHECK_P_NOT_PRIME;
- }
- }
- /* User may choose to enter NULL for codes if they don't want to check it*/
- if (codes != NULL){
- *codes = codeTmp;
- }
-
- /* if codeTmp was set,some check was flagged invalid */
- if (codeTmp){
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Converts DER encoded DH parameters to a WOLFSSL_DH structure.
- *
- * dh : structure to copy DH parameters into.
- * pp : DER encoded DH parameters
- * length : length to copy
- *
- * Returns pointer to WOLFSSL_DH structure on success, or NULL on failure
- */
-WOLFSSL_DH *wolfSSL_d2i_DHparams(WOLFSSL_DH **dh, const unsigned char **pp,
- long length)
-{
- WOLFSSL_DH *newDH = NULL;
- int ret;
- word32 idx = 0;
-
- WOLFSSL_ENTER("wolfSSL_d2i_DHparams");
-
- if (pp == NULL || length <= 0) {
- WOLFSSL_MSG("bad argument");
- return NULL;
- }
-
- if ((newDH = wolfSSL_DH_new()) == NULL) {
- WOLFSSL_MSG("wolfSSL_DH_new() failed");
- return NULL;
- }
-
- ret = wc_DhKeyDecode(*pp, &idx, (DhKey*)newDH->internal, (word32)length);
- if (ret != 0) {
- WOLFSSL_MSG("DhKeyDecode() failed");
- wolfSSL_DH_free(newDH);
- return NULL;
- }
- newDH->inSet = 1;
-
- if (SetDhExternal(newDH) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetDhExternal failed");
- wolfSSL_DH_free(newDH);
- return NULL;
- }
-
- *pp += length;
- if (dh != NULL){
- *dh = newDH;
- }
-
- return newDH;
-}
-
-/* Converts internal WOLFSSL_DH structure to DER encoded DH.
- *
- * dh : structure to copy DH parameters from.
- * out : DER buffer for DH parameters
- *
- * Returns size of DER on success and WOLFSSL_FAILURE if error
- */
-int wolfSSL_i2d_DHparams(const WOLFSSL_DH *dh, unsigned char **out)
-{
- word32 len;
- int ret = 0;
-
- WOLFSSL_ENTER("wolfSSL_i2d_DHparams");
-
- if (dh == NULL) {
- WOLFSSL_MSG("Bad parameters");
- return WOLFSSL_FAILURE;
- }
-
- /* Get total length */
- len = 2 + mp_leading_bit((mp_int*)dh->p->internal) +
- mp_unsigned_bin_size((mp_int*)dh->p->internal) +
- 2 + mp_leading_bit((mp_int*)dh->g->internal) +
- mp_unsigned_bin_size((mp_int*)dh->g->internal);
-
- /* Two bytes required for length if ASN.1 SEQ data greater than 127 bytes
- * and less than 256 bytes.
- */
- len = ((len > 127) ? 2 : 1) + len;
-
- if (out != NULL && *out != NULL) {
- ret = StoreDHparams(*out, &len, (mp_int*)dh->p->internal,
- (mp_int*)dh->g->internal);
- if (ret != MP_OKAY) {
- WOLFSSL_MSG("StoreDHparams error");
- len = 0;
- }
- else{
- *out += len;
- }
- }
- return (int)len;
-}
-#endif /* !NO_DH */
-
-#endif /* OPENSSL_ALL */
-
-#endif /* OPENSSL_EXTRA */
-
-#ifndef NO_FILESYSTEM
-
-#ifndef NO_STDIO_FILESYSTEM
-
-WOLFSSL_X509* wolfSSL_X509_d2i_fp(WOLFSSL_X509** x509, XFILE file)
-{
- WOLFSSL_X509* newX509 = NULL;
-
- WOLFSSL_ENTER("wolfSSL_X509_d2i_fp");
-
- if (file != XBADFILE) {
- byte* fileBuffer = NULL;
- long sz = 0;
-
- if (XFSEEK(file, 0, XSEEK_END) != 0)
- return NULL;
- sz = XFTELL(file);
- XREWIND(file);
-
- if (sz > MAX_WOLFSSL_FILE_SIZE || sz < 0) {
- WOLFSSL_MSG("X509_d2i file size error");
- return NULL;
- }
-
- fileBuffer = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
- if (fileBuffer != NULL) {
- int ret = (int)XFREAD(fileBuffer, 1, sz, file);
- if (ret == sz) {
- newX509 = wolfSSL_X509_d2i(NULL, fileBuffer, (int)sz);
- }
- XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
- }
- }
-
- if (x509 != NULL)
- *x509 = newX509;
-
- return newX509;
-}
-
-#endif /* NO_STDIO_FILESYSTEM */
-
-WOLFSSL_ABI
-WOLFSSL_X509* wolfSSL_X509_load_certificate_file(const char* fname, int format)
-{
-#ifdef WOLFSSL_SMALL_STACK
- byte staticBuffer[1]; /* force heap usage */
-#else
- byte staticBuffer[FILE_BUFFER_SIZE];
-#endif
- byte* fileBuffer = staticBuffer;
- int dynamic = 0;
- int ret;
- long sz = 0;
- XFILE file;
-
- WOLFSSL_X509* x509 = NULL;
-
- /* Check the inputs */
- if ((fname == NULL) ||
- (format != WOLFSSL_FILETYPE_ASN1 && format != WOLFSSL_FILETYPE_PEM))
- return NULL;
-
- file = XFOPEN(fname, "rb");
- if (file == XBADFILE)
- return NULL;
-
- if (XFSEEK(file, 0, XSEEK_END) != 0){
- XFCLOSE(file);
- return NULL;
- }
- sz = XFTELL(file);
- XREWIND(file);
-
- if (sz > MAX_WOLFSSL_FILE_SIZE || sz < 0) {
- WOLFSSL_MSG("X509_load_certificate_file size error");
- XFCLOSE(file);
- return NULL;
- }
-
- if (sz > (long)sizeof(staticBuffer)) {
- fileBuffer = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
- if (fileBuffer == NULL) {
- XFCLOSE(file);
- return NULL;
- }
- dynamic = 1;
- }
-
- ret = (int)XFREAD(fileBuffer, 1, sz, file);
- if (ret != sz) {
- XFCLOSE(file);
- if (dynamic)
- XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
- return NULL;
- }
-
- XFCLOSE(file);
-
- x509 = wolfSSL_X509_load_certificate_buffer(fileBuffer, (int)sz, format);
-
- if (dynamic)
- XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
-
- return x509;
-}
-
-#endif /* NO_FILESYSTEM */
-
-
-WOLFSSL_X509* wolfSSL_X509_load_certificate_buffer(
- const unsigned char* buf, int sz, int format)
-{
- int ret;
- WOLFSSL_X509* x509 = NULL;
- DerBuffer* der = NULL;
-
- WOLFSSL_ENTER("wolfSSL_X509_load_certificate_ex");
-
- if (format == WOLFSSL_FILETYPE_PEM) {
- #ifdef WOLFSSL_PEM_TO_DER
- if (PemToDer(buf, sz, CERT_TYPE, &der, NULL, NULL, NULL) != 0) {
- FreeDer(&der);
- }
- #else
- ret = NOT_COMPILED_IN;
- #endif
- }
- else {
- ret = AllocDer(&der, (word32)sz, CERT_TYPE, NULL);
- if (ret == 0) {
- XMEMCPY(der->buffer, buf, sz);
- }
- }
-
- /* At this point we want `der` to have the certificate in DER format */
- /* ready to be decoded. */
- if (der != NULL && der->buffer != NULL) {
- #ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert;
- #else
- DecodedCert cert[1];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
- DYNAMIC_TYPE_DCERT);
- if (cert != NULL)
- #endif
- {
- InitDecodedCert(cert, der->buffer, der->length, NULL);
- if (ParseCertRelative(cert, CERT_TYPE, 0, NULL) == 0) {
- x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
- DYNAMIC_TYPE_X509);
- if (x509 != NULL) {
- InitX509(x509, 1, NULL);
- if (CopyDecodedToX509(x509, cert) != 0) {
- wolfSSL_X509_free(x509);
- x509 = NULL;
- }
- }
- }
-
- FreeDecodedCert(cert);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
- #endif
- }
-
- FreeDer(&der);
- }
-
- return x509;
-}
-
-#endif /* KEEP_PEER_CERT || SESSION_CERTS */
-
-/* OPENSSL_EXTRA is needed for wolfSSL_X509_d21 function
- KEEP_OUR_CERT is to insure ability for returning ssl certificate */
-#if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
- defined(KEEP_OUR_CERT)
-WOLFSSL_X509* wolfSSL_get_certificate(WOLFSSL* ssl)
-{
- if (ssl == NULL) {
- return NULL;
- }
-
- if (ssl->buffers.weOwnCert) {
- if (ssl->ourCert == NULL) {
- if (ssl->buffers.certificate == NULL) {
- WOLFSSL_MSG("Certificate buffer not set!");
- return NULL;
- }
- #ifndef WOLFSSL_X509_STORE_CERTS
- ssl->ourCert = wolfSSL_X509_d2i(NULL,
- ssl->buffers.certificate->buffer,
- ssl->buffers.certificate->length);
- #endif
- }
- return ssl->ourCert;
- }
- else { /* if cert not owned get parent ctx cert or return null */
- if (ssl->ctx) {
- if (ssl->ctx->ourCert == NULL) {
- if (ssl->ctx->certificate == NULL) {
- WOLFSSL_MSG("Ctx Certificate buffer not set!");
- return NULL;
- }
- #ifndef WOLFSSL_X509_STORE_CERTS
- ssl->ctx->ourCert = wolfSSL_X509_d2i(NULL,
- ssl->ctx->certificate->buffer,
- ssl->ctx->certificate->length);
- #endif
- ssl->ctx->ownOurCert = 1;
- }
- return ssl->ctx->ourCert;
- }
- }
-
- return NULL;
-}
-#endif /* OPENSSL_EXTRA && KEEP_OUR_CERT */
-#endif /* NO_CERTS */
-
-
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
-#ifndef NO_ASN
-void wolfSSL_ASN1_OBJECT_free(WOLFSSL_ASN1_OBJECT* obj)
-{
- if (obj == NULL) {
- return;
- }
- if ((obj->obj != NULL) && ((obj->dynamic & WOLFSSL_ASN1_DYNAMIC_DATA) != 0)) {
- WOLFSSL_MSG("Freeing ASN1 data");
- XFREE((void*)obj->obj, obj->heap, DYNAMIC_TYPE_ASN1);
- obj->obj = NULL;
- }
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- if (obj->pathlen != NULL) {
- wolfSSL_ASN1_INTEGER_free(obj->pathlen);
- obj->pathlen = NULL;
- }
- #endif
- if ((obj->dynamic & WOLFSSL_ASN1_DYNAMIC) != 0) {
- WOLFSSL_MSG("Freeing ASN1 OBJECT");
- XFREE(obj, NULL, DYNAMIC_TYPE_ASN1);
- }
-}
-#endif /* NO_ASN */
-#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
-
-#ifdef OPENSSL_EXTRA
-#ifndef NO_ASN
-WOLFSSL_ASN1_OBJECT* wolfSSL_ASN1_OBJECT_new(void)
-{
- WOLFSSL_ASN1_OBJECT* obj;
-
- obj = (WOLFSSL_ASN1_OBJECT*)XMALLOC(sizeof(WOLFSSL_ASN1_OBJECT), NULL,
- DYNAMIC_TYPE_ASN1);
- if (obj == NULL) {
- return NULL;
- }
-
- XMEMSET(obj, 0, sizeof(WOLFSSL_ASN1_OBJECT));
- obj->d.ia5 = &(obj->d.ia5_internal);
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- obj->d.iPAddress = &(obj->d.iPAddress_internal);
-#endif
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- return obj;
-}
-
-/* Creates and returns a new WOLFSSL_CIPHER stack. */
-WOLFSSL_STACK* wolfSSL_sk_new_asn1_obj(void)
-{
- WOLFSSL_STACK* sk;
- WOLFSSL_ENTER("wolfSSL_sk_new_asn1_obj");
-
- sk = wolfSSL_sk_new_null();
- if (sk == NULL)
- return NULL;
- sk->type = STACK_TYPE_OBJ;
-
- return sk;
-}
-
-/* return 1 on success 0 on fail */
-int wolfSSL_sk_ASN1_OBJECT_push(WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)* sk,
- WOLFSSL_ASN1_OBJECT* obj)
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_ASN1_OBJECT_push");
-
- if (sk == NULL || obj == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* no previous values in stack */
- if (sk->data.obj == NULL) {
- sk->data.obj = obj;
- sk->num += 1;
- return WOLFSSL_SUCCESS;
- }
-
- /* stack already has value(s) create a new node and add more */
- node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_ASN1);
- if (node == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
- XMEMSET(node, 0, sizeof(WOLFSSL_STACK));
-
- /* push new obj onto head of stack */
- node->data.obj = sk->data.obj;
- node->next = sk->next;
- node->type = sk->type;
- sk->next = node;
- sk->data.obj = obj;
- sk->num += 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-WOLFSSL_ASN1_OBJECT* wolfSSL_sk_ASN1_OBJECT_pop(
- WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)* sk)
-{
- WOLFSSL_STACK* node;
- WOLFSSL_ASN1_OBJECT* obj;
-
- if (sk == NULL) {
- return NULL;
- }
-
- node = sk->next;
- obj = sk->data.obj;
-
- if (node != NULL) { /* update sk and remove node from stack */
- sk->data.obj = node->data.obj;
- sk->next = node->next;
- XFREE(node, NULL, DYNAMIC_TYPE_ASN1);
- }
- else { /* last obj in stack */
- sk->data.obj = NULL;
- }
-
- if (sk->num > 0) {
- sk->num -= 1;
- }
-
- return obj;
-}
-
-
-/* Free the structure for ASN1_OBJECT stack
- *
- * sk stack to free nodes in
- */
-void wolfSSL_sk_ASN1_OBJECT_free(WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)* sk)
-{
- wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
-}
-
-/* Free's all nodes in ASN1_OBJECT stack.
- * This is different then wolfSSL_ASN1_OBJECT_free in that it allows for
- * choosing the function to use when freeing an ASN1_OBJECT stack.
- *
- * sk stack to free nodes in
- * f X509 free function
- */
-void wolfSSL_sk_ASN1_OBJECT_pop_free(WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)* sk,
- void (*f) (WOLFSSL_ASN1_OBJECT*))
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_ASN1_OBJECT_pop_free");
-
- if (sk == NULL) {
- WOLFSSL_MSG("Parameter error");
- return;
- }
-
- /* parse through stack freeing each node */
- node = sk->next;
- while (node && sk->num > 1) {
- WOLFSSL_STACK* tmp = node;
- node = node->next;
-
- if (f)
- f(tmp->data.obj);
- else
- wolfSSL_ASN1_OBJECT_free(tmp->data.obj);
- tmp->data.obj = NULL;
- XFREE(tmp, NULL, DYNAMIC_TYPE_ASN1);
- sk->num -= 1;
- }
-
- /* free head of stack */
- if (sk->num == 1) {
- if (f)
- f(sk->data.obj);
- else
- wolfSSL_ASN1_OBJECT_free(sk->data.obj);
- sk->data.obj = NULL;
- }
- XFREE(sk, NULL, DYNAMIC_TYPE_ASN1);
-}
-
-int wolfSSL_ASN1_STRING_to_UTF8(unsigned char **out, WOLFSSL_ASN1_STRING *in)
-{
- /*
- ASN1_STRING_to_UTF8() converts the string in to UTF8 format,
- the converted data is allocated in a buffer in *out.
- The length of out is returned or a negative error code.
- The buffer *out should be free using OPENSSL_free().
- */
- unsigned char* buf;
- unsigned char* inPtr;
- int inLen;
-
- if (!out || !in) {
- return -1;
- }
-
- inPtr = wolfSSL_ASN1_STRING_data(in);
- inLen = wolfSSL_ASN1_STRING_length(in);
- if (!inPtr || inLen < 0) {
- return -1;
- }
- buf = (unsigned char*)XMALLOC(inLen + 1, NULL, DYNAMIC_TYPE_OPENSSL);
- if (!buf) {
- return -1;
- }
- XMEMCPY(buf, inPtr, inLen + 1);
- *out = buf;
- return inLen;
-}
-
-/* Returns string representation of ASN1_STRING */
-char* wolfSSL_i2s_ASN1_STRING(WOLFSSL_v3_ext_method *method,
- const WOLFSSL_ASN1_STRING *s)
-{
- int i;
- int tmpSz = 100;
- int valSz = 5;
- char* tmp;
- char val[5];
- unsigned char* str;
-
- WOLFSSL_ENTER("wolfSSL_i2s_ASN1_STRING");
- (void)method;
-
- if(s == NULL || s->data == NULL) {
- WOLFSSL_MSG("Bad Function Argument");
- return NULL;
- }
- str = (unsigned char*)XMALLOC(s->length, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (str == NULL) {
- WOLFSSL_MSG("Memory Error");
- return NULL;
- }
- XMEMCPY(str, (unsigned char*)s->data, s->length);
-
- tmp = (char*)XMALLOC(tmpSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmp == NULL) {
- WOLFSSL_MSG("Memory Error");
- XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return NULL;
- }
- XMEMSET(tmp, 0, tmpSz);
-
- for (i = 0; i < tmpSz && i < (s->length - 1); i++) {
- XSNPRINTF(val, valSz - 1, "%02X:", str[i]);
- XSTRNCAT(tmp, val, valSz);
- }
- XSNPRINTF(val, valSz - 1, "%02X", str[i]);
- XSTRNCAT(tmp, val, valSz);
- XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- return tmp;
-}
-#endif /* NO_ASN */
-
-void wolfSSL_set_connect_state(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_set_connect_state");
- if (ssl == NULL) {
- WOLFSSL_MSG("WOLFSSL struct pointer passed in was null");
- return;
- }
-
- #ifndef NO_DH
- /* client creates its own DH parameters on handshake */
- if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
- XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- }
- ssl->buffers.serverDH_P.buffer = NULL;
- if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
- XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- }
- ssl->buffers.serverDH_G.buffer = NULL;
- #endif
-
- if (InitSSL_Side(ssl, WOLFSSL_CLIENT_END) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error initializing client side");
- }
-}
-#endif /* OPENSSL_EXTRA */
-
-
-int wolfSSL_get_shutdown(const WOLFSSL* ssl)
-{
- int isShutdown = 0;
-
- WOLFSSL_ENTER("wolfSSL_get_shutdown");
-
- if (ssl) {
- /* in OpenSSL, WOLFSSL_SENT_SHUTDOWN = 1, when closeNotifySent *
- * WOLFSSL_RECEIVED_SHUTDOWN = 2, from close notify or fatal err */
- isShutdown = ((ssl->options.closeNotify||ssl->options.connReset) << 1)
- | (ssl->options.sentNotify);
- }
- return isShutdown;
-}
-
-
-int wolfSSL_session_reused(WOLFSSL* ssl)
-{
- int resuming = 0;
- if (ssl)
- resuming = ssl->options.resuming;
- return resuming;
-}
-
-#if defined(OPENSSL_EXTRA) || defined(HAVE_EXT_CACHE)
-WOLFSSL_SESSION* wolfSSL_SESSION_dup(WOLFSSL_SESSION* session)
-{
-#ifdef HAVE_EXT_CACHE
- WOLFSSL_SESSION* copy;
-
- WOLFSSL_ENTER("wolfSSL_SESSION_dup");
-
- if (session == NULL)
- return NULL;
-#ifdef HAVE_SESSION_TICKET
- if (session->isDynamic && !session->ticket) {
- WOLFSSL_MSG("Session dynamic flag is set but ticket pointer is null");
- return NULL;
- }
-#endif
-
- copy = (WOLFSSL_SESSION*)XMALLOC(sizeof(WOLFSSL_SESSION), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (copy != NULL) {
- XMEMCPY(copy, session, sizeof(WOLFSSL_SESSION));
- copy->isAlloced = 1;
-#ifdef HAVE_SESSION_TICKET
- if (session->isDynamic) {
- copy->ticket = (byte*)XMALLOC(session->ticketLen, NULL,
- DYNAMIC_TYPE_SESSION_TICK);
- XMEMCPY(copy->ticket, session->ticket, session->ticketLen);
- } else {
- copy->ticket = copy->staticTicket;
- }
-#endif
-#if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
- copy->peer = wolfSSL_X509_dup(session->peer);
-#endif
- }
- return copy;
-#else
- WOLFSSL_MSG("wolfSSL_SESSION_dup was called "
- "but HAVE_EXT_CACHE is not defined");
- (void)session;
- return NULL;
-#endif /* HAVE_EXT_CACHE */
-}
-
-void wolfSSL_SESSION_free(WOLFSSL_SESSION* session)
-{
- if (session == NULL)
- return;
-
-#if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
- if (session->peer) {
- wolfSSL_X509_free(session->peer);
- }
-#endif
-
-#ifdef HAVE_EXT_CACHE
- if (session->isAlloced) {
- #ifdef HAVE_SESSION_TICKET
- if (session->isDynamic)
- XFREE(session->ticket, NULL, DYNAMIC_TYPE_SESSION_TICK);
- #endif
- XFREE(session, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-#else
- /* No need to free since cache is static */
- (void)session;
-#endif
-}
-#endif
-
-
-/* helper function that takes in a protocol version struct and returns string */
-static const char* wolfSSL_internal_get_version(ProtocolVersion* version)
-{
- WOLFSSL_ENTER("wolfSSL_get_version");
-
- if (version == NULL) {
- return "Bad arg";
- }
-
- if (version->major == SSLv3_MAJOR) {
- switch (version->minor) {
- #ifndef NO_OLD_TLS
- #ifdef WOLFSSL_ALLOW_SSLV3
- case SSLv3_MINOR :
- return "SSLv3";
- #endif
- #ifdef WOLFSSL_ALLOW_TLSV10
- case TLSv1_MINOR :
- return "TLSv1";
- #endif
- case TLSv1_1_MINOR :
- return "TLSv1.1";
- #endif
- case TLSv1_2_MINOR :
- return "TLSv1.2";
- #ifdef WOLFSSL_TLS13
- case TLSv1_3_MINOR :
- #ifdef WOLFSSL_TLS13_DRAFT
- #ifdef WOLFSSL_TLS13_DRAFT_18
- return "TLSv1.3 (Draft 18)";
- #elif defined(WOLFSSL_TLS13_DRAFT_22)
- return "TLSv1.3 (Draft 22)";
- #elif defined(WOLFSSL_TLS13_DRAFT_23)
- return "TLSv1.3 (Draft 23)";
- #elif defined(WOLFSSL_TLS13_DRAFT_26)
- return "TLSv1.3 (Draft 26)";
- #else
- return "TLSv1.3 (Draft 28)";
- #endif
- #else
- return "TLSv1.3";
- #endif
- #endif
- default:
- return "unknown";
- }
- }
-#ifdef WOLFSSL_DTLS
- else if (version->major == DTLS_MAJOR) {
- switch (version->minor) {
- case DTLS_MINOR :
- return "DTLS";
- case DTLSv1_2_MINOR :
- return "DTLSv1.2";
- default:
- return "unknown";
- }
- }
-#endif /* WOLFSSL_DTLS */
- return "unknown";
-}
-
-
-const char* wolfSSL_get_version(WOLFSSL* ssl)
-{
- if (ssl == NULL) {
- WOLFSSL_MSG("Bad argument");
- return "unknown";
- }
-
- return wolfSSL_internal_get_version(&ssl->version);
-}
-
-
-/* current library version */
-const char* wolfSSL_lib_version(void)
-{
- return LIBWOLFSSL_VERSION_STRING;
-}
-
-
-/* current library version in hex */
-word32 wolfSSL_lib_version_hex(void)
-{
- return LIBWOLFSSL_VERSION_HEX;
-}
-
-
-int wolfSSL_get_current_cipher_suite(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("SSL_get_current_cipher_suite");
- if (ssl)
- return (ssl->options.cipherSuite0 << 8) | ssl->options.cipherSuite;
- return 0;
-}
-
-WOLFSSL_CIPHER* wolfSSL_get_current_cipher(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("SSL_get_current_cipher");
- if (ssl) {
- ssl->cipher.cipherSuite0 = ssl->options.cipherSuite0;
- ssl->cipher.cipherSuite = ssl->options.cipherSuite;
- return &ssl->cipher;
- }
- else
- return NULL;
-}
-
-
-const char* wolfSSL_CIPHER_get_name(const WOLFSSL_CIPHER* cipher)
-{
- WOLFSSL_ENTER("wolfSSL_CIPHER_get_name");
-
- if (cipher == NULL) {
- return NULL;
- }
-
- #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
- !defined(WOLFSSL_QT)
- return GetCipherNameIana(cipher->cipherSuite0, cipher->cipherSuite);
- #else
- return wolfSSL_get_cipher_name_from_suite(cipher->cipherSuite0,
- cipher->cipherSuite);
- #endif
-}
-
-const char* wolfSSL_CIPHER_get_version(const WOLFSSL_CIPHER* cipher)
-{
- WOLFSSL_ENTER("SSL_CIPHER_get_version");
-
- if (cipher == NULL || cipher->ssl == NULL) {
- return NULL;
- }
-
- return wolfSSL_get_version(cipher->ssl);
-}
-
-const char* wolfSSL_SESSION_CIPHER_get_name(WOLFSSL_SESSION* session)
-{
- if (session == NULL) {
- return NULL;
- }
-
-#if defined(SESSION_CERTS) || !defined(NO_RESUME_SUITE_CHECK) || \
- (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET))
- #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS)
- return GetCipherNameIana(session->cipherSuite0, session->cipherSuite);
- #else
- return GetCipherNameInternal(session->cipherSuite0, session->cipherSuite);
- #endif
-#else
- return NULL;
-#endif
-}
-
-const char* wolfSSL_get_cipher(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_get_cipher");
- return wolfSSL_CIPHER_get_name(wolfSSL_get_current_cipher(ssl));
-}
-
-/* gets cipher name in the format DHE-RSA-... rather then TLS_DHE... */
-const char* wolfSSL_get_cipher_name(WOLFSSL* ssl)
-{
- /* get access to cipher_name_idx in internal.c */
- return wolfSSL_get_cipher_name_internal(ssl);
-}
-
-const char* wolfSSL_get_cipher_name_from_suite(const byte cipherSuite0,
- const byte cipherSuite)
-{
- return GetCipherNameInternal(cipherSuite0, cipherSuite);
-}
-
-const char* wolfSSL_get_cipher_name_iana_from_suite(const byte cipherSuite0,
- const byte cipherSuite)
-{
- return GetCipherNameIana(cipherSuite0, cipherSuite);
-}
-
-
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
-/* Creates and returns a new WOLFSSL_CIPHER stack. */
-WOLFSSL_STACK* wolfSSL_sk_new_cipher(void)
-{
- WOLFSSL_STACK* sk;
- WOLFSSL_ENTER("wolfSSL_sk_new_cipher");
-
- sk = wolfSSL_sk_new_null();
- if (sk == NULL)
- return NULL;
- sk->type = STACK_TYPE_CIPHER;
-
- return sk;
-}
-
-#ifndef NO_WOLFSSL_STUB
-/* Keep as stubs for now */
-/* return 1 on success 0 on fail */
-int wolfSSL_sk_CIPHER_push(WOLF_STACK_OF(WOLFSSL_CIPHER)* sk,
- WOLFSSL_CIPHER* cipher)
-{
- WOLFSSL_STUB("wolfSSL_sk_CIPHER_push");
- (void)sk;
- (void)cipher;
- return 0;
-}
-
-
-WOLFSSL_CIPHER* wolfSSL_sk_CIPHER_pop(WOLF_STACK_OF(WOLFSSL_CIPHER)* sk)
-{
- WOLFSSL_STUB("wolfSSL_sk_CIPHER_pop");
- (void)sk;
- return NULL;
-}
-#endif /* NO_WOLFSSL_STUB */
-#endif /* WOLFSSL_QT || OPENSSL_ALL */
-
-word32 wolfSSL_CIPHER_get_id(const WOLFSSL_CIPHER* cipher)
-{
- word16 cipher_id = 0;
-
- WOLFSSL_ENTER("SSL_CIPHER_get_id");
-
- if (cipher && cipher->ssl) {
- cipher_id = (cipher->ssl->options.cipherSuite0 << 8) |
- cipher->ssl->options.cipherSuite;
- }
-
- return cipher_id;
-}
-
-const WOLFSSL_CIPHER* wolfSSL_get_cipher_by_value(word16 value)
-{
- const WOLFSSL_CIPHER* cipher = NULL;
- byte cipherSuite0, cipherSuite;
- WOLFSSL_ENTER("SSL_get_cipher_by_value");
-
- /* extract cipher id information */
- cipherSuite = (value & 0xFF);
- cipherSuite0 = ((value >> 8) & 0xFF);
-
- /* TODO: lookup by cipherSuite0 / cipherSuite */
- (void)cipherSuite0;
- (void)cipherSuite;
-
- return cipher;
-}
-
-
-#if defined(OPENSSL_ALL)
-/* Free the structure for WOLFSSL_CIPHER stack
- *
- * sk stack to free nodes in
- */
-void wolfSSL_sk_CIPHER_free(WOLF_STACK_OF(WOLFSSL_CIPHER)* sk)
-{
- WOLFSSL_STACK* node;
- WOLFSSL_STACK* tmp;
- WOLFSSL_ENTER("wolfSSL_sk_CIPHER_free");
-
- if (sk == NULL)
- return;
-
- /* parse through stack freeing each node */
- node = sk->next;
- while (node) {
- tmp = node;
- node = node->next;
- XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-
- /* free head of stack */
- XFREE(sk, NULL, DYNAMIC_TYPE_ASN1);
-}
-#endif
-
-#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448) || \
- !defined(NO_DH)
-#ifdef HAVE_FFDHE
-static const char* wolfssl_ffdhe_name(word16 group)
-{
- const char* str = NULL;
- switch (group) {
- case WOLFSSL_FFDHE_2048:
- str = "FFDHE_2048";
- break;
- case WOLFSSL_FFDHE_3072:
- str = "FFDHE_3072";
- break;
- case WOLFSSL_FFDHE_4096:
- str = "FFDHE_4096";
- break;
- case WOLFSSL_FFDHE_6144:
- str = "FFDHE_6144";
- break;
- case WOLFSSL_FFDHE_8192:
- str = "FFDHE_8192";
- break;
- }
- return str;
-}
-#endif
-/* Return the name of the curve used for key exchange as a printable string.
- *
- * ssl The SSL/TLS object.
- * returns NULL if ECDH was not used, otherwise the name as a string.
- */
-const char* wolfSSL_get_curve_name(WOLFSSL* ssl)
-{
- const char* cName = NULL;
-
- if (ssl == NULL)
- return NULL;
-
-#ifdef HAVE_FFDHE
- if (ssl->namedGroup != 0) {
- cName = wolfssl_ffdhe_name(ssl->namedGroup);
- }
-#endif
-
-#ifdef HAVE_CURVE25519
- if (ssl->ecdhCurveOID == ECC_X25519_OID && cName == NULL) {
- cName = "X25519";
- }
-#endif
-
-#ifdef HAVE_CURVE448
- if (ssl->ecdhCurveOID == ECC_X448_OID && cName == NULL) {
- cName = "X448";
- }
-#endif
-
-#ifdef HAVE_ECC
- if (ssl->ecdhCurveOID != 0 && cName == NULL) {
- cName = wc_ecc_get_name(wc_ecc_get_oid(ssl->ecdhCurveOID, NULL,
- NULL));
- }
-#endif
-
- return cName;
-}
-#endif
-
-
-#if defined(OPENSSL_EXTRA_X509_SMALL) || defined(KEEP_PEER_CERT) || \
- defined(SESSION_CERTS)
-/* Smaller subset of X509 compatibility functions. Avoid increasing the size of
- * this subset and its memory usage */
-
-#if !defined(NO_CERTS)
-/* returns a pointer to a new WOLFSSL_X509 structure on success and NULL on
- * fail
- */
-WOLFSSL_X509* wolfSSL_X509_new(void)
-{
- WOLFSSL_X509* x509;
-
- x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
- DYNAMIC_TYPE_X509);
- if (x509 != NULL) {
- InitX509(x509, 1, NULL);
- }
-
- return x509;
-}
-
-WOLFSSL_ABI
-WOLFSSL_X509_NAME* wolfSSL_X509_get_subject_name(WOLFSSL_X509* cert)
-{
- WOLFSSL_ENTER("wolfSSL_X509_get_subject_name");
- if (cert && cert->subject.sz != 0)
- return &cert->subject;
- return NULL;
-}
-
-#if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
-/******************************************************************************
-* wolfSSL_X509_subject_name_hash - compute the hash digest of the raw subject name
-*
-* RETURNS:
-* The beginning of the hash digest. Otherwise, returns zero.
-* Note:
-* Returns a different hash value from OpenSSL's X509_subject_name_hash() API
-* depending on the subject name.
-*/
-unsigned long wolfSSL_X509_subject_name_hash(const WOLFSSL_X509* x509)
-{
- word32 ret = 0;
- int retHash;
- WOLFSSL_X509_NAME *subjectName = NULL;
-
-#ifdef WOLFSSL_PIC32MZ_HASH
- byte digest[PIC32_DIGEST_SIZE];
-#else
- byte digest[WC_SHA_DIGEST_SIZE];
-#endif
-
- if (x509 == NULL){
- return WOLFSSL_FAILURE;
- }
-
- subjectName = wolfSSL_X509_get_subject_name((WOLFSSL_X509*)x509);
-
- if (subjectName != NULL){
- retHash = wc_ShaHash((const byte*)subjectName->name,
- (word32)subjectName->sz, digest);
-
- if(retHash != 0){
- WOLFSSL_MSG("Hash of X509 subjectName has failed");
- return WOLFSSL_FAILURE;
- }
- ret = MakeWordFromHash(digest);
- }
-
- return (unsigned long)ret;
-}
-#endif
-
-WOLFSSL_ABI
-WOLFSSL_X509_NAME* wolfSSL_X509_get_issuer_name(WOLFSSL_X509* cert)
-{
- WOLFSSL_ENTER("X509_get_issuer_name");
- if (cert && cert->issuer.sz != 0)
- return &cert->issuer;
- return NULL;
-}
-
-
-int wolfSSL_X509_get_signature_type(WOLFSSL_X509* x509)
-{
- int type = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_signature_type");
-
- if (x509 != NULL)
- type = x509->sigOID;
-
- return type;
-}
-
-#if defined(OPENSSL_EXTRA_X509_SMALL)
-
-/* Used to get a string from the WOLFSSL_X509_NAME structure that
- * corresponds with the NID value passed in.
- *
- * name structure to get string from
- * nid NID value to search for
- * buf [out] buffer to hold results. If NULL then the buffer size minus the
- * null char is returned.
- * len size of "buf" passed in
- *
- * returns the length of string found, not including the NULL terminator.
- * It's possible the function could return a negative value in the
- * case that len is less than or equal to 0. A negative value is
- * considered an error case.
- */
-int wolfSSL_X509_NAME_get_text_by_NID(WOLFSSL_X509_NAME* name,
- int nid, char* buf, int len)
-{
- char *text = NULL;
- int textSz = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_NAME_get_text_by_NID");
-
- switch (nid) {
- case ASN_COMMON_NAME:
- text = name->fullName.fullName + name->fullName.cnIdx;
- textSz = name->fullName.cnLen;
- break;
- case ASN_SUR_NAME:
- text = name->fullName.fullName + name->fullName.snIdx;
- textSz = name->fullName.snLen;
- break;
- case ASN_SERIAL_NUMBER:
- text = name->fullName.fullName + name->fullName.serialIdx;
- textSz = name->fullName.serialLen;
- break;
- case ASN_COUNTRY_NAME:
- text = name->fullName.fullName + name->fullName.cIdx;
- textSz = name->fullName.cLen;
- break;
- case ASN_LOCALITY_NAME:
- text = name->fullName.fullName + name->fullName.lIdx;
- textSz = name->fullName.lLen;
- break;
- case ASN_STATE_NAME:
- text = name->fullName.fullName + name->fullName.stIdx;
- textSz = name->fullName.stLen;
- break;
- case ASN_ORG_NAME:
- text = name->fullName.fullName + name->fullName.oIdx;
- textSz = name->fullName.oLen;
- break;
- case ASN_ORGUNIT_NAME:
- text = name->fullName.fullName + name->fullName.ouIdx;
- textSz = name->fullName.ouLen;
- break;
- case ASN_DOMAIN_COMPONENT:
- text = name->fullName.fullName + name->fullName.dcIdx[0];
- textSz = name->fullName.dcLen[0];
- break;
- case NID_emailAddress:
- text = name->fullName.fullName + name->fullName.emailIdx;
- textSz = name->fullName.emailLen;
- break;
- #ifdef WOLFSSL_CERT_EXT
- case ASN_BUS_CAT:
- text = name->fullName.fullName + name->fullName.bcIdx;
- textSz = name->fullName.bcLen;
- break;
- #endif
- default:
- WOLFSSL_MSG("Entry type not found");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* if buf is NULL return size of buffer needed (minus null char) */
- if (buf == NULL) {
- return textSz;
- }
-
- if (buf != NULL && text != NULL) {
- textSz = min(textSz + 1, len); /* + 1 to account for null char */
- if (textSz > 0) {
- XMEMCPY(buf, text, textSz - 1);
- buf[textSz - 1] = '\0';
- }
- }
-
- WOLFSSL_LEAVE("wolfSSL_X509_NAME_get_text_by_NID", textSz);
- return (textSz - 1); /* do not include null character in size */
-}
-
-/* Creates a new WOLFSSL_EVP_PKEY structure that has the public key from x509
- *
- * returns a pointer to the created WOLFSSL_EVP_PKEY on success and NULL on fail
- */
-WOLFSSL_EVP_PKEY* wolfSSL_X509_get_pubkey(WOLFSSL_X509* x509)
-{
- WOLFSSL_EVP_PKEY* key = NULL;
- WOLFSSL_ENTER("X509_get_pubkey");
- if (x509 != NULL) {
- key = wolfSSL_EVP_PKEY_new_ex(x509->heap);
- if (key != NULL) {
- if (x509->pubKeyOID == RSAk) {
- key->type = EVP_PKEY_RSA;
- }
- else if (x509->pubKeyOID == DSAk) {
- key->type = EVP_PKEY_DSA;
- }
- else {
- key->type = EVP_PKEY_EC;
- }
- key->save_type = 0;
- key->pkey.ptr = (char*)XMALLOC(
- x509->pubKey.length, x509->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (key->pkey.ptr == NULL) {
- wolfSSL_EVP_PKEY_free(key);
- return NULL;
- }
- XMEMCPY(key->pkey.ptr, x509->pubKey.buffer, x509->pubKey.length);
- key->pkey_sz = x509->pubKey.length;
-
- #ifdef HAVE_ECC
- key->pkey_curve = (int)x509->pkCurveOID;
- #endif /* HAVE_ECC */
-
- /* decode RSA key */
- #ifndef NO_RSA
- if (key->type == EVP_PKEY_RSA) {
- key->ownRsa = 1;
- key->rsa = wolfSSL_RSA_new();
- if (key->rsa == NULL) {
- wolfSSL_EVP_PKEY_free(key);
- return NULL;
- }
-
- if (wolfSSL_RSA_LoadDer_ex(key->rsa,
- (const unsigned char*)key->pkey.ptr, key->pkey_sz,
- WOLFSSL_RSA_LOAD_PUBLIC) != SSL_SUCCESS) {
- wolfSSL_EVP_PKEY_free(key);
- return NULL;
- }
- }
- #endif /* NO_RSA */
-
- /* decode ECC key */
- #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA)
- if (key->type == EVP_PKEY_EC) {
- word32 idx = 0;
-
- key->ownEcc = 1;
- key->ecc = wolfSSL_EC_KEY_new();
- if (key->ecc == NULL || key->ecc->internal == NULL) {
- wolfSSL_EVP_PKEY_free(key);
- return NULL;
- }
-
- /* not using wolfSSL_EC_KEY_LoadDer because public key in x509
- * is in the format of x963 (no sequence at start of buffer) */
- if (wc_EccPublicKeyDecode((const unsigned char*)key->pkey.ptr,
- &idx, (ecc_key*)key->ecc->internal, key->pkey_sz) < 0) {
- WOLFSSL_MSG("wc_EccPublicKeyDecode failed");
- wolfSSL_EVP_PKEY_free(key);
- return NULL;
- }
-
- if (SetECKeyExternal(key->ecc) != SSL_SUCCESS) {
- WOLFSSL_MSG("SetECKeyExternal failed");
- wolfSSL_EVP_PKEY_free(key);
- return NULL;
- }
-
- key->ecc->inSet = 1;
- }
- #endif /* HAVE_ECC */
-
- #ifndef NO_DSA
- if (key->type == EVP_PKEY_DSA) {
- key->ownDsa = 1;
- key->dsa = wolfSSL_DSA_new();
- if (key->dsa == NULL) {
- wolfSSL_EVP_PKEY_free(key);
- return NULL;
- }
-
- if (wolfSSL_DSA_LoadDer_ex(key->dsa,
- (const unsigned char*)key->pkey.ptr, key->pkey_sz, \
- WOLFSSL_DSA_LOAD_PUBLIC) != SSL_SUCCESS) {
- wolfSSL_DSA_free(key->dsa);
- key->dsa = NULL;
- wolfSSL_EVP_PKEY_free(key);
- return NULL;
- }
- }
- #endif /* NO_DSA */
- }
- }
- return key;
-}
-#endif /* OPENSSL_EXTRA_X509_SMALL */
-#endif /* !NO_CERTS */
-
-/* End of smaller subset of X509 compatibility functions. Avoid increasing the
- * size of this subset and its memory usage */
-#endif /* OPENSSL_EXTRA_X509_SMALL */
-
-#if defined(OPENSSL_ALL)
-/* Takes two WOLFSSL_X509* certificates and performs a Sha hash of each, if the
- * has values are the same, then it will do an XMEMCMP to confirm they are
- * identical. Returns a 0 when certificates match, returns a negative number
- * when certificates are not a match.
-*/
-int wolfSSL_X509_cmp(const WOLFSSL_X509 *a, const WOLFSSL_X509 *b)
-{
- const byte* derA;
- const byte* derB;
- int retHashA;
- int retHashB;
- int outSzA = 0;
- int outSzB = 0;
-
- #ifdef WOLFSSL_PIC32MZ_HASH
- byte digestA[PIC32_DIGEST_SIZE];
- byte digestB[PIC32_DIGEST_SIZE];
- #else
- byte digestA[WC_SHA_DIGEST_SIZE];
- byte digestB[WC_SHA_DIGEST_SIZE];
- #endif
-
- if (a == NULL || b == NULL){
- return BAD_FUNC_ARG;
- }
-
- derA = wolfSSL_X509_get_der((WOLFSSL_X509*)a, &outSzA);
- if(derA == NULL){
- WOLFSSL_MSG("wolfSSL_X509_get_der - certificate A has failed");
- return WOLFSSL_FATAL_ERROR;
- }
- derB = wolfSSL_X509_get_der((WOLFSSL_X509*)b, &outSzB);
- if(derB == NULL){
- WOLFSSL_MSG("wolfSSL_X509_get_der - certificate B has failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- retHashA = wc_ShaHash(derA, (word32)outSzA, digestA);
- if(retHashA != 0){
- WOLFSSL_MSG("Hash of certificate A has failed");
- return WOLFSSL_FATAL_ERROR;
- }
- retHashB = wc_ShaHash(derB, (word32)outSzB, digestB);
- if(retHashB != 0){
- WOLFSSL_MSG("Hash of certificate B has failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (outSzA == outSzB){
- #ifdef WOLFSSL_PIC32MZ_HASH
- if(XMEMCMP(digestA, digestB, PIC32_DIGEST_SIZE) != 0){
- return WOLFSSL_FATAL_ERROR;
- }
- #else
- if(XMEMCMP(digestA, digestB, WC_SHA_DIGEST_SIZE) != 0){
- return WOLFSSL_FATAL_ERROR;
- }
- #endif
- else{
- WOLFSSL_LEAVE("wolfSSL_X509_cmp", 0);
- return 0;
- }
- }
- else{
- WOLFSSL_LEAVE("wolfSSL_X509_cmp", WOLFSSL_FATAL_ERROR);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif
-
-#if defined(OPENSSL_EXTRA)
-#if !defined(NO_CERTS)
- int wolfSSL_X509_ext_isSet_by_NID(WOLFSSL_X509* x509, int nid)
- {
- int isSet = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_ext_isSet_by_NID");
-
- if (x509 != NULL) {
- switch (nid) {
- case BASIC_CA_OID: isSet = x509->basicConstSet; break;
- case ALT_NAMES_OID: isSet = x509->subjAltNameSet; break;
- case AUTH_KEY_OID: isSet = x509->authKeyIdSet; break;
- case SUBJ_KEY_OID: isSet = x509->subjKeyIdSet; break;
- case KEY_USAGE_OID: isSet = x509->keyUsageSet; break;
- case CRL_DIST_OID: isSet = x509->CRLdistSet; break;
- case EXT_KEY_USAGE_OID: isSet = ((x509->extKeyUsageSrc) ? 1 : 0);
- break;
- case AUTH_INFO_OID: isSet = x509->authInfoSet; break;
- #if defined(WOLFSSL_SEP) || defined(WOLFSSL_QT)
- case CERT_POLICY_OID: isSet = x509->certPolicySet; break;
- #endif /* WOLFSSL_SEP || WOLFSSL_QT */
- default:
- WOLFSSL_MSG("NID not in table");
- }
- }
-
- WOLFSSL_LEAVE("wolfSSL_X509_ext_isSet_by_NID", isSet);
-
- return isSet;
- }
-
-
- int wolfSSL_X509_ext_get_critical_by_NID(WOLFSSL_X509* x509, int nid)
- {
- int crit = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_ext_get_critical_by_NID");
-
- if (x509 != NULL) {
- switch (nid) {
- case BASIC_CA_OID: crit = x509->basicConstCrit; break;
- case ALT_NAMES_OID: crit = x509->subjAltNameCrit; break;
- case AUTH_KEY_OID: crit = x509->authKeyIdCrit; break;
- case SUBJ_KEY_OID: crit = x509->subjKeyIdCrit; break;
- case KEY_USAGE_OID: crit = x509->keyUsageCrit; break;
- case CRL_DIST_OID: crit= x509->CRLdistCrit; break;
- #if defined(WOLFSSL_SEP) || defined(WOLFSSL_QT)
- case CERT_POLICY_OID: crit = x509->certPolicyCrit; break;
- #endif /* WOLFSSL_SEP || WOLFSSL_QT */
- }
- }
-
- WOLFSSL_LEAVE("wolfSSL_X509_ext_get_critical_by_NID", crit);
-
- return crit;
- }
-
-
- int wolfSSL_X509_get_isSet_pathLength(WOLFSSL_X509* x509)
- {
- int isSet = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_isSet_pathLength");
-
- if (x509 != NULL)
- isSet = x509->basicConstPlSet;
-
- WOLFSSL_LEAVE("wolfSSL_X509_get_isSet_pathLength", isSet);
-
- return isSet;
- }
-
-
- word32 wolfSSL_X509_get_pathLength(WOLFSSL_X509* x509)
- {
- word32 pathLength = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_pathLength");
-
- if (x509 != NULL)
- pathLength = x509->pathLength;
-
- WOLFSSL_LEAVE("wolfSSL_X509_get_pathLength", pathLength);
-
- return pathLength;
- }
-
-
- unsigned int wolfSSL_X509_get_keyUsage(WOLFSSL_X509* x509)
- {
- word16 usage = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_keyUsage");
-
- if (x509 != NULL)
- usage = x509->keyUsage;
-
- WOLFSSL_LEAVE("wolfSSL_X509_get_keyUsage", usage);
-
- return usage;
- }
-
-
- byte* wolfSSL_X509_get_authorityKeyID(WOLFSSL_X509* x509,
- byte* dst, int* dstLen)
- {
- byte *id = NULL;
- int copySz = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_authorityKeyID");
-
- if (x509 != NULL) {
- if (x509->authKeyIdSet) {
- copySz = min(dstLen != NULL ? *dstLen : 0,
- (int)x509->authKeyIdSz);
- id = x509->authKeyId;
- }
-
- if (dst != NULL && dstLen != NULL && id != NULL && copySz > 0) {
- XMEMCPY(dst, id, copySz);
- id = dst;
- *dstLen = copySz;
- }
- }
-
- WOLFSSL_LEAVE("wolfSSL_X509_get_authorityKeyID", copySz);
-
- return id;
- }
-
-
- byte* wolfSSL_X509_get_subjectKeyID(WOLFSSL_X509* x509,
- byte* dst, int* dstLen)
- {
- byte *id = NULL;
- int copySz = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_subjectKeyID");
-
- if (x509 != NULL) {
- if (x509->subjKeyIdSet) {
- copySz = min(dstLen != NULL ? *dstLen : 0,
- (int)x509->subjKeyIdSz);
- id = x509->subjKeyId;
- }
-
- if (dst != NULL && dstLen != NULL && id != NULL && copySz > 0) {
- XMEMCPY(dst, id, copySz);
- id = dst;
- *dstLen = copySz;
- }
- }
-
- WOLFSSL_LEAVE("wolfSSL_X509_get_subjectKeyID", copySz);
-
- return id;
- }
-
-
- int wolfSSL_X509_NAME_entry_count(WOLFSSL_X509_NAME* name)
- {
- int count = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_NAME_entry_count");
-
- if (name != NULL)
- count = name->fullName.locSz;
-
- WOLFSSL_LEAVE("wolfSSL_X509_NAME_entry_count", count);
- return count;
- }
-
-
-
- int wolfSSL_X509_NAME_get_index_by_NID(WOLFSSL_X509_NAME* name,
- int nid, int pos)
- {
- int value = nid, i;
-
- WOLFSSL_ENTER("wolfSSL_X509_NAME_get_index_by_NID");
-
- if (name == NULL || pos >= DN_NAMES_MAX + DOMAIN_COMPONENT_MAX) {
- return BAD_FUNC_ARG;
- }
-
- if (value == NID_emailAddress) {
- value = ASN_EMAIL_NAME;
- }
-
- i = pos + 1; /* start search after index passed in */
- if (i < 0) {
- i = 0;
- }
-
- for (;i < name->fullName.locSz &&
- i < DN_NAMES_MAX + DOMAIN_COMPONENT_MAX; i++) {
- if (name->fullName.loc[i] == value) {
- return i;
- }
- }
- return WOLFSSL_FATAL_ERROR;
- }
-
-
- WOLFSSL_ASN1_STRING* wolfSSL_X509_NAME_ENTRY_get_data(
- WOLFSSL_X509_NAME_ENTRY* in)
- {
- WOLFSSL_ENTER("wolfSSL_X509_NAME_ENTRY_get_data");
- if (in == NULL)
- return NULL;
-
- return in->value;
- }
-
-
- /* Creates a new WOLFSSL_ASN1_STRING structure.
- *
- * returns a pointer to the new structure created on success or NULL if fail
- */
- WOLFSSL_ASN1_STRING* wolfSSL_ASN1_STRING_new(void)
- {
- WOLFSSL_ASN1_STRING* asn1;
-
- WOLFSSL_ENTER("wolfSSL_ASN1_STRING_new");
-
- asn1 = (WOLFSSL_ASN1_STRING*)XMALLOC(sizeof(WOLFSSL_ASN1_STRING), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (asn1 != NULL) {
- XMEMSET(asn1, 0, sizeof(WOLFSSL_ASN1_STRING));
- }
-
- return asn1; /* no check for null because error case is returning null*/
- }
-
-
- /* used to free a WOLFSSL_ASN1_STRING structure */
- void wolfSSL_ASN1_STRING_free(WOLFSSL_ASN1_STRING* asn1)
- {
- WOLFSSL_ENTER("wolfSSL_ASN1_STRING_free");
-
- if (asn1 != NULL) {
- if (asn1->length > 0 && asn1->data != NULL && asn1->isDynamic) {
- XFREE(asn1->data, NULL, DYNAMIC_TYPE_OPENSSL);
- }
- XFREE(asn1, NULL, DYNAMIC_TYPE_OPENSSL);
- }
- }
-
-
- /* Creates a new WOLFSSL_ASN1_STRING structure given the input type.
- *
- * type is the type of set when WOLFSSL_ASN1_STRING is created
- *
- * returns a pointer to the new structure created on success or NULL if fail
- */
- WOLFSSL_ASN1_STRING* wolfSSL_ASN1_STRING_type_new(int type)
- {
- WOLFSSL_ASN1_STRING* asn1;
-
- WOLFSSL_ENTER("wolfSSL_ASN1_STRING_type_new");
-
- asn1 = wolfSSL_ASN1_STRING_new();
- if (asn1 == NULL) {
- return NULL;
- }
- asn1->type = type;
-
- return asn1;
- }
-
-
-/******************************************************************************
-* wolfSSL_ASN1_STRING_type - returns the type of
-*
-* RETURNS:
-* returns the type set for . Otherwise, returns WOLFSSL_FAILURE.
-*/
- int wolfSSL_ASN1_STRING_type(const WOLFSSL_ASN1_STRING* asn1)
- {
-
- WOLFSSL_ENTER("wolfSSL_ASN1_STRING_type");
-
- if (asn1 == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- return asn1->type;
- }
-
- /* if dataSz is negative then use XSTRLEN to find length of data
- * return WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure */
- /* `data` can be NULL and only buffer will be allocated */
- int wolfSSL_ASN1_STRING_set(WOLFSSL_ASN1_STRING* asn1, const void* data,
- int dataSz)
- {
- int sz;
-
- WOLFSSL_ENTER("wolfSSL_ASN1_STRING_set");
-
- if (asn1 == NULL || (data == NULL && dataSz < 0)) {
- return WOLFSSL_FAILURE;
- }
-
- if (dataSz < 0) {
- sz = (int)XSTRLEN((const char*)data) + 1; /* +1 for null */
- }
- else {
- sz = dataSz;
- }
-
- if (sz < 0) {
- return WOLFSSL_FAILURE;
- }
-
- /* free any existing data before copying */
- if (asn1->data != NULL && asn1->isDynamic) {
- XFREE(asn1->data, NULL, DYNAMIC_TYPE_OPENSSL);
- asn1->data = NULL;
- }
-
- if (sz > CTC_NAME_SIZE) {
- /* create new data buffer and copy over */
- asn1->data = (char*)XMALLOC(sz, NULL, DYNAMIC_TYPE_OPENSSL);
- if (asn1->data == NULL) {
- return WOLFSSL_FAILURE;
- }
- asn1->isDynamic = 1;
- }
- else {
- XMEMSET(asn1->strData, 0, CTC_NAME_SIZE);
- asn1->data = asn1->strData;
- asn1->isDynamic = 0;
- }
- if (data != NULL) {
- XMEMCPY(asn1->data, data, sz);
- asn1->data[sz] = '\0';
- }
- asn1->length = sz;
-
- return WOLFSSL_SUCCESS;
- }
-
-
- unsigned char* wolfSSL_ASN1_STRING_data(WOLFSSL_ASN1_STRING* asn)
- {
- WOLFSSL_ENTER("wolfSSL_ASN1_STRING_data");
-
- if (asn) {
- return (unsigned char*)asn->data;
- }
- else {
- return NULL;
- }
- }
-
-
- int wolfSSL_ASN1_STRING_length(WOLFSSL_ASN1_STRING* asn)
- {
- WOLFSSL_ENTER("wolfSSL_ASN1_STRING_length");
-
- if (asn) {
- return asn->length;
- }
- else {
- return 0;
- }
- }
-
-#ifndef NO_WOLFSSL_STUB
- WOLFSSL_ASN1_STRING* wolfSSL_d2i_DISPLAYTEXT(WOLFSSL_ASN1_STRING **asn,
- const unsigned char **in, long len)
- {
- WOLFSSL_STUB("d2i_DISPLAYTEXT");
- (void)asn;
- (void)in;
- (void)len;
- return NULL;
- }
-#endif
-
-#ifdef XSNPRINTF /* a snprintf function needs to be available */
- /* Writes the human readable form of x509 to bio.
- *
- * bio WOLFSSL_BIO to write to.
- * x509 Certificate to write.
- *
- * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on failure
- */
- int wolfSSL_X509_print_ex(WOLFSSL_BIO* bio, WOLFSSL_X509* x509,
- unsigned long nmflags, unsigned long cflag)
- {
- WOLFSSL_ENTER("wolfSSL_X509_print_ex");
-
- #ifndef NO_WOLFSSL_STUB
- /* flags currently not supported */
- (void)nmflags;
- (void)cflag;
- #endif
-
- if (bio == NULL || x509 == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_BIO_write(bio, "Certificate:\n",
- (int)XSTRLEN("Certificate:\n")) <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_BIO_write(bio, " Data:\n",
- (int)XSTRLEN(" Data:\n")) <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- /* print version of cert */
- {
- int version;
- char tmp[20];
-
- if ((version = wolfSSL_X509_version(x509)) < 0) {
- WOLFSSL_MSG("Error getting X509 version");
- return WOLFSSL_FAILURE;
- }
- if (wolfSSL_BIO_write(bio, " Version:",
- (int)XSTRLEN(" Version:")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp), " %d (0x%x)\n", version, (byte)version-1);
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
-
- /* print serial number out */
- {
- unsigned char serial[32];
- int sz = sizeof(serial);
-
- XMEMSET(serial, 0, sz);
- if (wolfSSL_X509_get_serial_number(x509, serial, &sz)
- != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error getting x509 serial number");
- return WOLFSSL_FAILURE;
- }
- if (wolfSSL_BIO_write(bio, " Serial Number:",
- (int)XSTRLEN(" Serial Number:")) <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- /* if serial can fit into byte than print on the same line */
- if (sz <= (int)sizeof(byte)) {
- char tmp[17];
- XSNPRINTF(tmp, sizeof(tmp), " %d (0x%x)\n", serial[0],serial[0]);
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
- else {
- int i;
- char tmp[100];
- int tmpSz = 100;
- char val[5];
- int valSz = 5;
-
- /* serial is larger than int size so print off hex values */
- if (wolfSSL_BIO_write(bio, "\n ",
- (int)XSTRLEN("\n ")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- tmp[0] = '\0';
- for (i = 0; i < sz - 1 && (3 * i) < tmpSz - valSz; i++) {
- XSNPRINTF(val, sizeof(val) - 1, "%02x:", serial[i]);
- val[3] = '\0'; /* make sure is null terminated */
- XSTRNCAT(tmp, val, valSz);
- }
- XSNPRINTF(val, sizeof(val) - 1, "%02x\n", serial[i]);
- val[3] = '\0'; /* make sure is null terminated */
- XSTRNCAT(tmp, val, valSz);
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
- }
-
- /* print signature algo */
- {
- int oid;
- const char* sig;
-
- if ((oid = wolfSSL_X509_get_signature_type(x509)) <= 0) {
- WOLFSSL_MSG("Error getting x509 signature type");
- return WOLFSSL_FAILURE;
- }
- if (wolfSSL_BIO_write(bio, " Signature Algorithm: ",
- (int)XSTRLEN(" Signature Algorithm: ")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- sig = GetSigName(oid);
- if (wolfSSL_BIO_write(bio, sig, (int)XSTRLEN(sig)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- if (wolfSSL_BIO_write(bio, "\n", (int)XSTRLEN("\n")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
-
- /* print issuer */
- {
- char* issuer;
- #ifdef WOLFSSL_SMALL_STACK
- char* buff = NULL;
- int issSz = 0;
- #else
- char buff[256];
- int issSz = 256;
- #endif
-
- #if defined(WOLFSSL_QT)
- issuer = wolfSSL_X509_get_name_oneline(
- wolfSSL_X509_get_issuer_name(x509), buff, issSz);
- #else
- issuer = wolfSSL_X509_NAME_oneline(
- wolfSSL_X509_get_issuer_name(x509), buff, issSz);
- #endif
-
- if (wolfSSL_BIO_write(bio, " Issuer: ",
- (int)XSTRLEN(" Issuer: ")) <= 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(issuer, NULL, DYNAMIC_TYPE_OPENSSL);
- #endif
- return WOLFSSL_FAILURE;
- }
- if (issuer != NULL) {
- if (wolfSSL_BIO_write(bio, issuer, (int)XSTRLEN(issuer)) <= 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(issuer, NULL, DYNAMIC_TYPE_OPENSSL);
- #endif
- return WOLFSSL_FAILURE;
- }
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(issuer, NULL, DYNAMIC_TYPE_OPENSSL);
- #endif
- if (wolfSSL_BIO_write(bio, "\n", (int)XSTRLEN("\n")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
-
- #ifndef NO_ASN_TIME
- /* print validity */
- {
- char tmp[80];
-
- if (wolfSSL_BIO_write(bio, " Validity\n",
- (int)XSTRLEN(" Validity\n")) <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_BIO_write(bio, " Not Before: ",
- (int)XSTRLEN(" Not Before: ")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- if (x509->notBefore.length > 0) {
- if (GetTimeString(x509->notBefore.data, ASN_UTC_TIME,
- tmp, sizeof(tmp)) != WOLFSSL_SUCCESS) {
- if (GetTimeString(x509->notBefore.data, ASN_GENERALIZED_TIME,
- tmp, sizeof(tmp)) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error getting not before date");
- return WOLFSSL_FAILURE;
- }
- }
- }
- else {
- XSTRNCPY(tmp, "Not Set", sizeof(tmp)-1);
- }
- tmp[sizeof(tmp) - 1] = '\0'; /* make sure null terminated */
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_BIO_write(bio, "\n Not After : ",
- (int)XSTRLEN("\n Not After : ")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- if (x509->notAfter.length > 0) {
- if (GetTimeString(x509->notAfter.data, ASN_UTC_TIME,
- tmp, sizeof(tmp)) != WOLFSSL_SUCCESS) {
- if (GetTimeString(x509->notAfter.data, ASN_GENERALIZED_TIME,
- tmp, sizeof(tmp)) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error getting not after date");
- return WOLFSSL_FAILURE;
- }
- }
- }
- else {
- XSTRNCPY(tmp, "Not Set", sizeof(tmp)-1);
- }
- tmp[sizeof(tmp) - 1] = '\0'; /* make sure null terminated */
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
- #endif
-
- /* print subject */
- {
- char* subject;
- #ifdef WOLFSSL_SMALL_STACK
- char* buff = NULL;
- int subSz = 0;
- #else
- char buff[256];
- int subSz = 256;
- #endif
-
- #if defined(WOLFSSL_QT)
- subject = wolfSSL_X509_get_name_oneline(
- wolfSSL_X509_get_subject_name(x509), buff, subSz);
- #else
- subject = wolfSSL_X509_NAME_oneline(
- wolfSSL_X509_get_subject_name(x509), buff, subSz);
- #endif
-
- if (wolfSSL_BIO_write(bio, "\n Subject: ",
- (int)XSTRLEN("\n Subject: ")) <= 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(subject, NULL, DYNAMIC_TYPE_OPENSSL);
- #endif
- return WOLFSSL_FAILURE;
- }
- if (subject != NULL) {
- if (wolfSSL_BIO_write(bio, subject, (int)XSTRLEN(subject)) <= 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(subject, NULL, DYNAMIC_TYPE_OPENSSL);
- #endif
- return WOLFSSL_FAILURE;
- }
- }
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(subject, NULL, DYNAMIC_TYPE_OPENSSL);
- #endif
- }
-
- /* get and print public key */
- if (wolfSSL_BIO_write(bio, "\n Subject Public Key Info:\n",
- (int)XSTRLEN("\n Subject Public Key Info:\n")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- {
- #if (!defined(NO_RSA) && !defined(HAVE_USER_RSA)) || defined(HAVE_ECC)
- char tmp[100];
- #endif
-
- switch (x509->pubKeyOID) {
- #ifndef NO_RSA
- case RSAk:
- if (wolfSSL_BIO_write(bio,
- " Public Key Algorithm: rsaEncryption\n",
- (int)XSTRLEN(" Public Key Algorithm: rsaEncryption\n")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- #ifdef HAVE_USER_RSA
- if (wolfSSL_BIO_write(bio,
- " Build without user RSA to print key\n",
- (int)XSTRLEN(" Build without user RSA to print key\n"))
- <= 0) {
- return WOLFSSL_FAILURE;
- }
- #else
- {
- RsaKey rsa;
- word32 idx = 0;
- int sz;
- byte lbit = 0;
- int rawLen;
- unsigned char* rawKey;
-
- if (wc_InitRsaKey(&rsa, NULL) != 0) {
- WOLFSSL_MSG("wc_InitRsaKey failure");
- return WOLFSSL_FAILURE;
- }
- if (wc_RsaPublicKeyDecode(x509->pubKey.buffer,
- &idx, &rsa, x509->pubKey.length) != 0) {
- WOLFSSL_MSG("Error decoding RSA key");
- wc_FreeRsaKey(&rsa);
- return WOLFSSL_FAILURE;
- }
- if ((sz = wc_RsaEncryptSize(&rsa)) < 0) {
- WOLFSSL_MSG("Error getting RSA key size");
- wc_FreeRsaKey(&rsa);
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1, "%s%s: (%d bit)\n%s\n",
- " ", "Public-Key", 8 * sz,
- " Modulus:");
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp,
- (int)XSTRLEN(tmp)) <= 0) {
- wc_FreeRsaKey(&rsa);
- return WOLFSSL_FAILURE;
- }
-
- /* print out modulus */
- XSNPRINTF(tmp, sizeof(tmp) - 1," ");
- tmp[sizeof(tmp) - 1] = '\0';
- if (mp_leading_bit(&rsa.n)) {
- lbit = 1;
- XSTRNCAT(tmp, "00", 3);
- }
-
- rawLen = mp_unsigned_bin_size(&rsa.n);
- rawKey = (unsigned char*)XMALLOC(rawLen, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (rawKey == NULL) {
- WOLFSSL_MSG("Memory error");
- wc_FreeRsaKey(&rsa);
- return WOLFSSL_FAILURE;
- }
- mp_to_unsigned_bin(&rsa.n, rawKey);
- for (idx = 0; idx < (word32)rawLen; idx++) {
- char val[5];
- int valSz = 5;
-
- if ((idx == 0) && !lbit) {
- XSNPRINTF(val, valSz - 1, "%02x", rawKey[idx]);
- }
- else if ((idx != 0) && (((idx + lbit) % 15) == 0)) {
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp,
- (int)XSTRLEN(tmp)) <= 0) {
- XFREE(rawKey, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- wc_FreeRsaKey(&rsa);
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1,
- ":\n ");
- XSNPRINTF(val, valSz - 1, "%02x", rawKey[idx]);
- }
- else {
- XSNPRINTF(val, valSz - 1, ":%02x", rawKey[idx]);
- }
- XSTRNCAT(tmp, val, valSz);
- }
- XFREE(rawKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- /* print out remaining modulus values */
- if ((idx > 0) && (((idx - 1 + lbit) % 15) != 0)) {
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp,
- (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
-
- /* print out exponent values */
- rawLen = mp_unsigned_bin_size(&rsa.e);
- if (rawLen < 0) {
- WOLFSSL_MSG("Error getting exponent size");
- wc_FreeRsaKey(&rsa);
- return WOLFSSL_FAILURE;
- }
-
- if ((word32)rawLen < sizeof(word32)) {
- rawLen = sizeof(word32);
- }
- rawKey = (unsigned char*)XMALLOC(rawLen, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (rawKey == NULL) {
- WOLFSSL_MSG("Memory error");
- wc_FreeRsaKey(&rsa);
- return WOLFSSL_FAILURE;
- }
- XMEMSET(rawKey, 0, rawLen);
- mp_to_unsigned_bin(&rsa.e, rawKey);
- if ((word32)rawLen <= sizeof(word32)) {
- idx = *(word32*)rawKey;
- #ifdef BIG_ENDIAN_ORDER
- idx = ByteReverseWord32(idx);
- #endif
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1,
- "\n Exponent: %d (0x%x)\n",idx, idx);
- if (wolfSSL_BIO_write(bio, tmp,
- (int)XSTRLEN(tmp)) <= 0) {
- XFREE(rawKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- wc_FreeRsaKey(&rsa);
- return WOLFSSL_FAILURE;
- }
- XFREE(rawKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- wc_FreeRsaKey(&rsa);
- }
- #endif /* HAVE_USER_RSA */
- break;
- #endif /* NO_RSA */
-
- #ifdef HAVE_ECC
- case ECDSAk:
- {
- word32 i;
- ecc_key ecc;
-
- if (wolfSSL_BIO_write(bio,
- " Public Key Algorithm: EC\n",
- (int)XSTRLEN(" Public Key Algorithm: EC\n")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- if (wc_ecc_init_ex(&ecc, x509->heap, INVALID_DEVID)
- != 0) {
- return WOLFSSL_FAILURE;
- }
-
- i = 0;
- if (wc_EccPublicKeyDecode(x509->pubKey.buffer, &i,
- &ecc, x509->pubKey.length) != 0) {
- wc_ecc_free(&ecc);
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1, "%s%s: (%d bit)\n%s\n",
- " ", "Public-Key",
- 8 * wc_ecc_size(&ecc),
- " pub:");
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp,
- (int)XSTRLEN(tmp)) <= 0) {
- wc_ecc_free(&ecc);
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1," ");
- {
- word32 derSz;
- byte* der;
-
- derSz = wc_ecc_size(&ecc) * WOLFSSL_BIT_SIZE;
- der = (byte*)XMALLOC(derSz, x509->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (der == NULL) {
- wc_ecc_free(&ecc);
- return WOLFSSL_FAILURE;
- }
-
- if (wc_ecc_export_x963(&ecc, der, &derSz) != 0) {
- wc_ecc_free(&ecc);
- XFREE(der, x509->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- for (i = 0; i < derSz; i++) {
- char val[5];
- int valSz = 5;
-
- if (i == 0) {
- XSNPRINTF(val, valSz - 1, "%02x", der[i]);
- }
- else if ((i % 15) == 0) {
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp,
- (int)XSTRLEN(tmp)) <= 0) {
- wc_ecc_free(&ecc);
- XFREE(der, x509->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1,
- ":\n ");
- XSNPRINTF(val, valSz - 1, "%02x", der[i]);
- }
- else {
- XSNPRINTF(val, valSz - 1, ":%02x", der[i]);
- }
- XSTRNCAT(tmp, val, valSz);
- }
-
- /* print out remaining modulus values */
- if ((i > 0) && (((i - 1) % 15) != 0)) {
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp,
- (int)XSTRLEN(tmp)) <= 0) {
- wc_ecc_free(&ecc);
- XFREE(der, x509->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- }
-
- XFREE(der, x509->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1, "\n%s%s: %s\n",
- " ", "ASN1 OID",
- ecc.dp->name);
- if (wolfSSL_BIO_write(bio, tmp,
- (int)XSTRLEN(tmp)) <= 0) {
- wc_ecc_free(&ecc);
- return WOLFSSL_FAILURE;
- }
- wc_ecc_free(&ecc);
- }
- break;
- #endif /* HAVE_ECC */
- default:
- WOLFSSL_MSG("Unknown key type");
- return WOLFSSL_FAILURE;
- }
- }
-
- /* print out extensions */
- if (wolfSSL_BIO_write(bio, " X509v3 extensions:\n",
- (int)XSTRLEN(" X509v3 extensions:\n")) <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- /* print subject key id */
- if (x509->subjKeyIdSet && x509->subjKeyId != NULL &&
- x509->subjKeyIdSz > 0) {
- char tmp[100];
- word32 i;
- char val[5];
- int valSz = 5;
-
-
- if (wolfSSL_BIO_write(bio,
- " X509v3 Subject Key Identifier: \n",
- (int)XSTRLEN(" X509v3 Subject Key Identifier: \n"))
- <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- XSNPRINTF(tmp, sizeof(tmp) - 1, " ");
- for (i = 0; i < sizeof(tmp) && i < (x509->subjKeyIdSz - 1); i++) {
- XSNPRINTF(val, valSz - 1, "%02X:", x509->subjKeyId[i]);
- XSTRNCAT(tmp, val, valSz);
- }
- XSNPRINTF(val, valSz - 1, "%02X\n", x509->subjKeyId[i]);
- XSTRNCAT(tmp, val, valSz);
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
-
- /* printf out authority key id */
- if (x509->authKeyIdSet && x509->authKeyId != NULL &&
- x509->authKeyIdSz > 0) {
- char tmp[100];
- word32 i;
- char val[5];
- int valSz = 5;
- int len = 0;
-
- if (wolfSSL_BIO_write(bio,
- " X509v3 Authority Key Identifier: \n",
- (int)XSTRLEN(" X509v3 Authority Key Identifier: \n"))
- <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- XSNPRINTF(tmp, sizeof(tmp) - 1, " keyid");
- for (i = 0; i < x509->authKeyIdSz; i++) {
- /* check if buffer is almost full */
- if (XSTRLEN(tmp) >= sizeof(tmp) - valSz) {
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- tmp[0] = '\0';
- }
- XSNPRINTF(val, valSz - 1, ":%02X", x509->authKeyId[i]);
- XSTRNCAT(tmp, val, valSz);
- }
- len = (int)XSTRLEN("\n");
- XSTRNCAT(tmp, "\n", len + 1);
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
-
- /* print basic constraint */
- if (x509->basicConstSet) {
- char tmp[100];
-
- if (wolfSSL_BIO_write(bio,
- "\n X509v3 Basic Constraints: \n",
- (int)XSTRLEN("\n X509v3 Basic Constraints: \n"))
- <= 0) {
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp),
- " CA:%s\n",
- (x509->isCa)? "TRUE": "FALSE");
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
-
- /* print out signature */
- if (x509->sig.length > 0) {
- unsigned char* sig;
- int sigSz;
- int i;
- char tmp[100];
- int sigOid = wolfSSL_X509_get_signature_type(x509);
-
- if (wolfSSL_BIO_write(bio,
- " Signature Algorithm: ",
- (int)XSTRLEN(" Signature Algorithm: ")) <= 0) {
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1,"%s\n", GetSigName(sigOid));
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- sigSz = (int)x509->sig.length;
- sig = (unsigned char*)XMALLOC(sigSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (sig == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_X509_get_signature(x509, sig, &sigSz) <= 0) {
- XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1," ");
- tmp[sizeof(tmp) - 1] = '\0';
- for (i = 0; i < sigSz; i++) {
- char val[5];
- int valSz = 5;
-
- if (i == 0) {
- XSNPRINTF(val, valSz - 1, "%02x", sig[i]);
- }
- else if (((i % 18) == 0)) {
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp))
- <= 0) {
- XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1,
- ":\n ");
- XSNPRINTF(val, valSz - 1, "%02x", sig[i]);
- }
- else {
- XSNPRINTF(val, valSz - 1, ":%02x", sig[i]);
- }
- XSTRNCAT(tmp, val, valSz);
- }
- XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- /* print out remaining sig values */
- if ((i > 0) && (((i - 1) % 18) != 0)) {
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp))
- <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
- }
-
- /* done with print out */
- if (wolfSSL_BIO_write(bio, "\n\0", (int)XSTRLEN("\n\0")) <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
- }
- int wolfSSL_X509_print(WOLFSSL_BIO* bio, WOLFSSL_X509* x509)
- {
- return wolfSSL_X509_print_ex(bio, x509, 0, 0);
- }
-
-#endif /* XSNPRINTF */
-
-#endif /* NO_CERTS */
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
-/* Creates cipher->description based on cipher->offset
- * cipher->offset is set in wolfSSL_get_ciphers_compat when it is added
- * to a stack of ciphers.
- * @param [in] cipher: A cipher from a stack of ciphers.
- * return WOLFSSL_SUCCESS if cipher->description is set, else WOLFSSL_FAILURE
- */
-int wolfSSL_sk_CIPHER_description(WOLFSSL_CIPHER* cipher)
-{
- int ret = WOLFSSL_FAILURE;
- int i,j,k;
- int strLen;
- unsigned long offset;
- char* dp;
- const char* name;
- const char *keaStr, *authStr, *encStr, *macStr, *protocol;
- char n[MAX_SEGMENTS][MAX_SEGMENT_SZ] = {{0}};
- unsigned char len = MAX_DESCRIPTION_SZ-1;
- const CipherSuiteInfo* cipher_names;
- ProtocolVersion pv;
- WOLFSSL_ENTER("wolfSSL_sk_CIPHER_description");
-
- if (cipher == NULL)
- return WOLFSSL_FAILURE;
-
- dp = cipher->description;
- if (dp == NULL)
- return WOLFSSL_FAILURE;
-
- cipher_names = GetCipherNames();
-
- offset = cipher->offset;
- pv.major = cipher_names[offset].major;
- pv.minor = cipher_names[offset].minor;
- protocol = wolfSSL_internal_get_version(&pv);
-
- name = cipher_names[offset].name;
-
- if (name == NULL)
- return ret;
-
- /* Segment cipher name into n[n0,n1,n2,n4]
- * These are used later for comparisons to create:
- * keaStr, authStr, encStr, macStr
- *
- * If cipher_name = ECDHE-ECDSA-AES256-SHA
- * then n0 = "ECDHE", n1 = "ECDSA", n2 = "AES256", n3 = "SHA"
- * and n = [n0,n1,n2,n3,0]
- */
- strLen = (int)XSTRLEN(name);
-
- for (i = 0, j = 0, k = 0; i <= strLen; i++) {
- if (k > MAX_SEGMENTS || j > MAX_SEGMENT_SZ)
- break;
-
- if (name[i] != '-' && name[i] != '\0') {
- n[k][j] = name[i]; /* Fill kth segment string until '-' */
- j++;
- }
- else {
- n[k][j] = '\0';
- j = 0;
- k++;
- }
- }
- /* keaStr */
- keaStr = GetCipherKeaStr(n);
- /* authStr */
- authStr = GetCipherAuthStr(n);
- /* encStr */
- encStr = GetCipherEncStr(n);
- if ((cipher->bits = SetCipherBits(encStr)) == WOLFSSL_FAILURE) {
- WOLFSSL_MSG("Cipher Bits Not Set.");
- }
- /* macStr */
- macStr = GetCipherMacStr(n);
-
-
- /* Build up the string by copying onto the end. */
- XSTRNCPY(dp, name, len);
- dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
- len -= (int)strLen; dp += strLen;
-
- XSTRNCPY(dp, " ", len);
- dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
- len -= (int)strLen; dp += strLen;
- XSTRNCPY(dp, protocol, len);
- dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
- len -= (int)strLen; dp += strLen;
-
- XSTRNCPY(dp, " Kx=", len);
- dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
- len -= (int)strLen; dp += strLen;
- XSTRNCPY(dp, keaStr, len);
- dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
- len -= (int)strLen; dp += strLen;
-
- XSTRNCPY(dp, " Au=", len);
- dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
- len -= (int)strLen; dp += strLen;
- XSTRNCPY(dp, authStr, len);
- dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
- len -= (int)strLen; dp += strLen;
-
- XSTRNCPY(dp, " Enc=", len);
- dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
- len -= (int)strLen; dp += strLen;
- XSTRNCPY(dp, encStr, len);
- dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
- len -= (int)strLen; dp += strLen;
-
- XSTRNCPY(dp, " Mac=", len);
- dp[len-1] = '\0'; strLen = (int)XSTRLEN(dp);
- len -= (int)strLen; dp += strLen;
- XSTRNCPY(dp, macStr, len);
- dp[len-1] = '\0';
-
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-char* wolfSSL_CIPHER_description(const WOLFSSL_CIPHER* cipher, char* in,
- int len)
-{
- char *ret = in;
- const char *keaStr, *authStr, *encStr, *macStr;
- size_t strLen;
- WOLFSSL_ENTER("wolfSSL_CIPHER_description");
-
- if (cipher == NULL || in == NULL)
- return NULL;
-
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- /* if cipher is in the stack from wolfSSL_get_ciphers_compat then
- * Return the description based on cipher_names[cipher->offset]
- */
- if (cipher->in_stack == TRUE) {
- wolfSSL_sk_CIPHER_description((WOLFSSL_CIPHER*)cipher);
- XSTRNCPY(in,cipher->description,len);
- return ret;
- }
-#endif
-
- /* Get the cipher description based on the SSL session cipher */
- switch (cipher->ssl->specs.kea) {
- case no_kea:
- keaStr = "None";
- break;
-#ifndef NO_RSA
- case rsa_kea:
- keaStr = "RSA";
- break;
-#endif
-#ifndef NO_DH
- case diffie_hellman_kea:
- keaStr = "DHE";
- break;
-#endif
- case fortezza_kea:
- keaStr = "FZ";
- break;
-#ifndef NO_PSK
- case psk_kea:
- keaStr = "PSK";
- break;
- #ifndef NO_DH
- case dhe_psk_kea:
- keaStr = "DHEPSK";
- break;
- #endif
- #ifdef HAVE_ECC
- case ecdhe_psk_kea:
- keaStr = "ECDHEPSK";
- break;
- #endif
-#endif
-#ifdef HAVE_NTRU
- case ntru_kea:
- keaStr = "NTRU";
- break;
-#endif
-#ifdef HAVE_ECC
- case ecc_diffie_hellman_kea:
- keaStr = "ECDHE";
- break;
- case ecc_static_diffie_hellman_kea:
- keaStr = "ECDH";
- break;
-#endif
- default:
- keaStr = "unknown";
- break;
- }
-
- switch (cipher->ssl->specs.sig_algo) {
- case anonymous_sa_algo:
- authStr = "None";
- break;
-#ifndef NO_RSA
- case rsa_sa_algo:
- authStr = "RSA";
- break;
-#endif
-#ifndef NO_DSA
- case dsa_sa_algo:
- authStr = "DSA";
- break;
-#endif
-#ifdef HAVE_ECC
- case ecc_dsa_sa_algo:
- authStr = "ECDSA";
- break;
-#endif
- default:
- authStr = "unknown";
- break;
- }
-
- switch (cipher->ssl->specs.bulk_cipher_algorithm) {
- case wolfssl_cipher_null:
- encStr = "None";
- break;
-#ifndef NO_RC4
- case wolfssl_rc4:
- encStr = "RC4(128)";
- break;
-#endif
-#ifndef NO_DES3
- case wolfssl_triple_des:
- encStr = "3DES(168)";
- break;
-#endif
-#ifdef HAVE_IDEA
- case wolfssl_idea:
- encStr = "IDEA(128)";
- break;
-#endif
-#ifndef NO_AES
- case wolfssl_aes:
- if (cipher->ssl->specs.key_size == 128)
- encStr = "AES(128)";
- else if (cipher->ssl->specs.key_size == 256)
- encStr = "AES(256)";
- else
- encStr = "AES(?)";
- break;
- #ifdef HAVE_AESGCM
- case wolfssl_aes_gcm:
- if (cipher->ssl->specs.key_size == 128)
- encStr = "AESGCM(128)";
- else if (cipher->ssl->specs.key_size == 256)
- encStr = "AESGCM(256)";
- else
- encStr = "AESGCM(?)";
- break;
- #endif
- #ifdef HAVE_AESCCM
- case wolfssl_aes_ccm:
- if (cipher->ssl->specs.key_size == 128)
- encStr = "AESCCM(128)";
- else if (cipher->ssl->specs.key_size == 256)
- encStr = "AESCCM(256)";
- else
- encStr = "AESCCM(?)";
- break;
- #endif
-#endif
-#ifdef HAVE_CHACHA
- case wolfssl_chacha:
- encStr = "CHACHA20/POLY1305(256)";
- break;
-#endif
-#ifdef HAVE_CAMELLIA
- case wolfssl_camellia:
- if (cipher->ssl->specs.key_size == 128)
- encStr = "Camellia(128)";
- else if (cipher->ssl->specs.key_size == 256)
- encStr = "Camellia(256)";
- else
- encStr = "Camellia(?)";
- break;
-#endif
-#if defined(HAVE_HC128) && !defined(NO_HC128)
- case wolfssl_hc128:
- encStr = "HC128(128)";
- break;
-#endif
-#if defined(HAVE_RABBIT) && !defined(NO_RABBIT)
- case wolfssl_rabbit:
- encStr = "RABBIT(128)";
- break;
-#endif
- default:
- encStr = "unknown";
- break;
- }
-
- switch (cipher->ssl->specs.mac_algorithm) {
- case no_mac:
- macStr = "None";
- break;
-#ifndef NO_MD5
- case md5_mac:
- macStr = "MD5";
- break;
-#endif
-#ifndef NO_SHA
- case sha_mac:
- macStr = "SHA1";
- break;
-#endif
-#ifdef HAVE_SHA224
- case sha224_mac:
- macStr = "SHA224";
- break;
-#endif
-#ifndef NO_SHA256
- case sha256_mac:
- macStr = "SHA256";
- break;
-#endif
-#ifdef HAVE_SHA384
- case sha384_mac:
- macStr = "SHA384";
- break;
-#endif
-#ifdef HAVE_SHA512
- case sha512_mac:
- macStr = "SHA512";
- break;
-#endif
- default:
- macStr = "unknown";
- break;
- }
-
- /* Build up the string by copying onto the end. */
- XSTRNCPY(in, wolfSSL_CIPHER_get_name(cipher), len);
- in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
-
- XSTRNCPY(in, " ", len);
- in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
- XSTRNCPY(in, wolfSSL_get_version(cipher->ssl), len);
- in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
-
- XSTRNCPY(in, " Kx=", len);
- in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
- XSTRNCPY(in, keaStr, len);
- in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
-
- XSTRNCPY(in, " Au=", len);
- in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
- XSTRNCPY(in, authStr, len);
- in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
-
- XSTRNCPY(in, " Enc=", len);
- in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
- XSTRNCPY(in, encStr, len);
- in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
-
- XSTRNCPY(in, " Mac=", len);
- in[len-1] = '\0'; strLen = XSTRLEN(in); len -= (int)strLen; in += strLen;
- XSTRNCPY(in, macStr, len);
- in[len-1] = '\0';
-
- return ret;
-}
-
-
-#ifndef NO_SESSION_CACHE
-
-WOLFSSL_SESSION* wolfSSL_get1_session(WOLFSSL* ssl)
-{
- if (ssl == NULL) {
- return NULL;
- }
-
- /* sessions are stored statically, no need for reference count */
- return wolfSSL_get_session(ssl);
-}
-
-#endif /* NO_SESSION_CACHE */
-
-
-
-/* was do nothing */
-/*
-void OPENSSL_free(void* buf)
-{
- (void)buf;
-}
-*/
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_OCSP_parse_url(char* url, char** host, char** port, char** path,
- int* ssl)
-{
- (void)url;
- (void)host;
- (void)port;
- (void)path;
- (void)ssl;
- WOLFSSL_STUB("OCSP_parse_url");
- return 0;
-}
-#endif
-
-#ifndef NO_MD4
-
-void wolfSSL_MD4_Init(WOLFSSL_MD4_CTX* md4)
-{
- /* make sure we have a big enough buffer */
- typedef char ok[sizeof(md4->buffer) >= sizeof(Md4) ? 1 : -1];
- (void) sizeof(ok);
-
- WOLFSSL_ENTER("MD4_Init");
- wc_InitMd4((Md4*)md4);
-}
-
-
-void wolfSSL_MD4_Update(WOLFSSL_MD4_CTX* md4, const void* data,
- unsigned long len)
-{
- WOLFSSL_ENTER("MD4_Update");
- wc_Md4Update((Md4*)md4, (const byte*)data, (word32)len);
-}
-
-
-void wolfSSL_MD4_Final(unsigned char* digest, WOLFSSL_MD4_CTX* md4)
-{
- WOLFSSL_ENTER("MD4_Final");
- wc_Md4Final((Md4*)md4, digest);
-}
-
-#endif /* NO_MD4 */
-
-
-/* Removes a WOLFSSL_BIO struct from the WOLFSSL_BIO linked list.
- *
- * bio is the WOLFSSL_BIO struct in the list and removed.
- *
- * The return WOLFSSL_BIO struct is the next WOLFSSL_BIO in the list or NULL if
- * there is none.
- */
-WOLFSSL_BIO* wolfSSL_BIO_pop(WOLFSSL_BIO* bio)
-{
- if (bio == NULL) {
- WOLFSSL_MSG("Bad argument passed in");
- return NULL;
- }
-
- if (bio->prev != NULL) {
- bio->prev->next = bio->next;
- }
-
- if (bio->next != NULL) {
- bio->next->prev = bio->prev;
- }
-
- return bio->next;
-}
-
-
-
-WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_mem(void)
-{
- static WOLFSSL_BIO_METHOD meth;
-
- WOLFSSL_ENTER("wolfSSL_BIO_s_mem");
- meth.type = WOLFSSL_BIO_MEMORY;
-
- return &meth;
-}
-
-
-WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_base64(void)
-{
- static WOLFSSL_BIO_METHOD meth;
-
- WOLFSSL_ENTER("wolfSSL_BIO_f_base64");
- meth.type = WOLFSSL_BIO_BASE64;
-
- return &meth;
-}
-
-
-/* Set the flag for the bio.
- *
- * bio the structure to set the flag in
- * flags the flag to use
- */
-void wolfSSL_BIO_set_flags(WOLFSSL_BIO* bio, int flags)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_set_flags");
-
- if (bio != NULL) {
- bio->flags |= flags;
- }
-}
-
-void wolfSSL_BIO_clear_flags(WOLFSSL_BIO *bio, int flags)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_clear_flags");
- if (bio != NULL) {
- bio->flags &= ~flags;
- }
-}
-
-/* Set ex_data for WOLFSSL_BIO
- *
- * bio : BIO structure to set ex_data in
- * idx : Index of ex_data to set
- * data : Data to set in ex_data
- *
- * Returns WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE on failure
- */
-int wolfSSL_BIO_set_ex_data(WOLFSSL_BIO *bio, int idx, void *data)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_set_ex_data");
- #ifdef HAVE_EX_DATA
- if (bio != NULL && idx < MAX_EX_DATA) {
- return wolfSSL_CRYPTO_set_ex_data(&bio->ex_data, idx, data);
- }
- #else
- (void)bio;
- (void)idx;
- (void)data;
- #endif
- return WOLFSSL_FAILURE;
-}
-
-/* Get ex_data in WOLFSSL_BIO at given index
- *
- * bio : BIO structure to get ex_data from
- * idx : Index of ex_data to get data from
- *
- * Returns void pointer to ex_data on success or NULL on failure
- */
-void *wolfSSL_BIO_get_ex_data(WOLFSSL_BIO *bio, int idx)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_get_ex_data");
- #ifdef HAVE_EX_DATA
- if (bio != NULL && idx < MAX_EX_DATA && idx >= 0) {
- return wolfSSL_CRYPTO_get_ex_data(&bio->ex_data, idx);
- }
- #else
- (void)bio;
- (void)idx;
- #endif
- return NULL;
-}
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_RAND_screen(void)
-{
- WOLFSSL_STUB("RAND_screen");
-}
-#endif
-
-
-
-int wolfSSL_RAND_load_file(const char* fname, long len)
-{
- (void)fname;
- /* wolfCrypt provides enough entropy internally or will report error */
- if (len == -1)
- return 1024;
- else
- return (int)len;
-}
-
-
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_COMP_METHOD* wolfSSL_COMP_zlib(void)
-{
- WOLFSSL_STUB("COMP_zlib");
- return 0;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_COMP_METHOD* wolfSSL_COMP_rle(void)
-{
- WOLFSSL_STUB("COMP_rle");
- return 0;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_COMP_add_compression_method(int method, void* data)
-{
- (void)method;
- (void)data;
- WOLFSSL_STUB("COMP_add_compression_method");
- return 0;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_set_dynlock_create_callback(WOLFSSL_dynlock_value* (*f)(
- const char*, int))
-{
- WOLFSSL_STUB("CRYPTO_set_dynlock_create_callback");
- (void)f;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_set_dynlock_lock_callback(
- void (*f)(int, WOLFSSL_dynlock_value*, const char*, int))
-{
- WOLFSSL_STUB("CRYPTO_set_set_dynlock_lock_callback");
- (void)f;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_set_dynlock_destroy_callback(
- void (*f)(WOLFSSL_dynlock_value*, const char*, int))
-{
- WOLFSSL_STUB("CRYPTO_set_set_dynlock_destroy_callback");
- (void)f;
-}
-#endif
-
-
-const char* wolfSSL_X509_verify_cert_error_string(long err)
-{
- return wolfSSL_ERR_reason_error_string(err);
-}
-
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_X509_LOOKUP_add_dir(WOLFSSL_X509_LOOKUP* lookup, const char* dir,
- long len)
-{
- (void)lookup;
- (void)dir;
- (void)len;
- WOLFSSL_STUB("X509_LOOKUP_add_dir");
- return 0;
-}
-#endif
-
-int wolfSSL_X509_LOOKUP_load_file(WOLFSSL_X509_LOOKUP* lookup,
- const char* file, long type)
-{
-#if !defined(NO_FILESYSTEM) && \
- (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
- int ret = WOLFSSL_FAILURE;
- XFILE fp;
- long sz;
- byte* pem = NULL;
- byte* curr = NULL;
- byte* prev = NULL;
- WOLFSSL_X509* x509;
- const char* header = NULL;
- const char* footer = NULL;
-
- if (type != X509_FILETYPE_PEM)
- return BAD_FUNC_ARG;
-
- fp = XFOPEN(file, "r");
- if (fp == XBADFILE)
- return BAD_FUNC_ARG;
-
- if(XFSEEK(fp, 0, XSEEK_END) != 0) {
- XFCLOSE(fp);
- return WOLFSSL_BAD_FILE;
- }
- sz = XFTELL(fp);
- XREWIND(fp);
-
- if (sz > MAX_WOLFSSL_FILE_SIZE || sz <= 0) {
- WOLFSSL_MSG("X509_LOOKUP_load_file size error");
- goto end;
- }
-
- pem = (byte*)XMALLOC(sz, 0, DYNAMIC_TYPE_PEM);
- if (pem == NULL) {
- ret = MEMORY_ERROR;
- goto end;
- }
-
- /* Read in file which may be CRLs or certificates. */
- if (XFREAD(pem, (size_t)sz, 1, fp) != 1)
- goto end;
-
- prev = curr = pem;
- do {
- /* get PEM header and footer based on type */
- if (wc_PemGetHeaderFooter(CRL_TYPE, &header, &footer) == 0 &&
- XSTRNSTR((char*)curr, header, (unsigned int)sz) != NULL) {
-#ifdef HAVE_CRL
- WOLFSSL_CERT_MANAGER* cm = lookup->store->cm;
-
- if (cm->crl == NULL) {
- if (wolfSSL_CertManagerEnableCRL(cm, 0) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Enable CRL failed");
- goto end;
- }
- }
-
- ret = BufferLoadCRL(cm->crl, curr, sz, WOLFSSL_FILETYPE_PEM,
- NO_VERIFY);
- if (ret != WOLFSSL_SUCCESS)
- goto end;
-#endif
- curr = (byte*)XSTRNSTR((char*)curr, footer, (unsigned int)sz);
- }
- else if (wc_PemGetHeaderFooter(CERT_TYPE, &header, &footer) == 0 &&
- XSTRNSTR((char*)curr, header, (unsigned int)sz) != NULL) {
- x509 = wolfSSL_X509_load_certificate_buffer(curr, (int)sz,
- WOLFSSL_FILETYPE_PEM);
- if (x509 == NULL)
- goto end;
- ret = wolfSSL_X509_STORE_add_cert(lookup->store, x509);
- wolfSSL_X509_free(x509);
- if (ret != WOLFSSL_SUCCESS)
- goto end;
- curr = (byte*)XSTRNSTR((char*)curr, footer, (unsigned int)sz);
- }
- else
- goto end;
-
- if (curr == NULL)
- goto end;
-
- curr++;
- sz -= (long)(curr - prev);
- prev = curr;
- }
- while (ret == WOLFSSL_SUCCESS);
-
-end:
- if (pem != NULL)
- XFREE(pem, 0, DYNAMIC_TYPE_PEM);
- XFCLOSE(fp);
- return ret;
-#else
- (void)lookup;
- (void)file;
- (void)type;
- return WOLFSSL_FAILURE;
-#endif
-}
-
-WOLFSSL_X509_LOOKUP_METHOD* wolfSSL_X509_LOOKUP_hash_dir(void)
-{
- /* Method implementation in functions. */
- static WOLFSSL_X509_LOOKUP_METHOD meth = { 1 };
- return &meth;
-}
-
-WOLFSSL_X509_LOOKUP_METHOD* wolfSSL_X509_LOOKUP_file(void)
-{
- /* Method implementation in functions. */
- static WOLFSSL_X509_LOOKUP_METHOD meth = { 0 };
- return &meth;
-}
-
-
-WOLFSSL_X509_LOOKUP* wolfSSL_X509_STORE_add_lookup(WOLFSSL_X509_STORE* store,
- WOLFSSL_X509_LOOKUP_METHOD* m)
-{
- WOLFSSL_ENTER("SSL_X509_STORE_add_lookup");
- if (store == NULL)
- return NULL;
-
- /* Method is a dummy value and is not needed. */
- (void)m;
- /* Make sure the lookup has a back reference to the store. */
- store->lookup.store = store;
- return &store->lookup;
-}
-
-
-#ifndef NO_CERTS
-/* Converts the X509 to DER format and outputs it into bio.
- *
- * bio is the structure to hold output DER
- * x509 certificate to create DER from
- *
- * returns WOLFSSL_SUCCESS on success
- */
-int wolfSSL_i2d_X509_bio(WOLFSSL_BIO* bio, WOLFSSL_X509* x509)
-{
- WOLFSSL_ENTER("wolfSSL_i2d_X509_bio");
-
- if (bio == NULL || x509 == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- if (x509->derCert != NULL) {
- word32 len = x509->derCert->length;
- byte* der = x509->derCert->buffer;
-
- if (wolfSSL_BIO_write(bio, der, len) == (int)len) {
- return SSL_SUCCESS;
- }
- }
-
- return WOLFSSL_FAILURE;
-}
-
-
-/* Converts an internal structure to a DER buffer
- *
- * x509 structure to get DER buffer from
- * out buffer to hold result. If NULL then *out is NULL then a new buffer is
- * created.
- *
- * returns the size of the DER result on success
- */
-int wolfSSL_i2d_X509(WOLFSSL_X509* x509, unsigned char** out)
-{
- const unsigned char* der;
- int derSz = 0;
-
- WOLFSSL_ENTER("wolfSSL_i2d_X509");
-
- if (x509 == NULL) {
- WOLFSSL_LEAVE("wolfSSL_i2d_X509", BAD_FUNC_ARG);
- return BAD_FUNC_ARG;
- }
-
- der = wolfSSL_X509_get_der(x509, &derSz);
- if (der == NULL) {
- WOLFSSL_LEAVE("wolfSSL_i2d_X509", MEMORY_E);
- return MEMORY_E;
- }
-
- if (out != NULL && *out == NULL) {
- *out = (unsigned char*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_OPENSSL);
- if (*out == NULL) {
- WOLFSSL_LEAVE("wolfSSL_i2d_X509", MEMORY_E);
- return MEMORY_E;
- }
- }
-
- if (out != NULL)
- XMEMCPY(*out, der, derSz);
-
- WOLFSSL_LEAVE("wolfSSL_i2d_X509", derSz);
- return derSz;
-}
-
-
-/* Converts the DER from bio and creates a WOLFSSL_X509 structure from it.
- *
- * bio is the structure holding DER
- * x509 certificate to create from DER. Can be NULL
- *
- * returns pointer to WOLFSSL_X509 structure on success and NULL on fail
- */
-WOLFSSL_X509* wolfSSL_d2i_X509_bio(WOLFSSL_BIO* bio, WOLFSSL_X509** x509)
-{
- WOLFSSL_X509* localX509 = NULL;
- unsigned char* mem = NULL;
- int ret;
- word32 size;
-
- WOLFSSL_ENTER("wolfSSL_d2i_X509_bio");
-
- if (bio == NULL) {
- WOLFSSL_MSG("Bad Function Argument bio is NULL");
- return NULL;
- }
-
- ret = wolfSSL_BIO_get_mem_data(bio, &mem);
- if (mem == NULL || ret <= 0) {
- WOLFSSL_MSG("Failed to get data from bio struct");
- return NULL;
- }
- size = ret;
-
- localX509 = wolfSSL_X509_d2i(NULL, mem, size);
- if (localX509 == NULL) {
- return NULL;
- }
-
- if (x509 != NULL) {
- *x509 = localX509;
- }
-
- return localX509;
-}
-
-
-#if !defined(NO_ASN) && !defined(NO_PWDBASED)
-WC_PKCS12* wolfSSL_d2i_PKCS12_bio(WOLFSSL_BIO* bio, WC_PKCS12** pkcs12)
-{
- WC_PKCS12* localPkcs12 = NULL;
- unsigned char* mem = NULL;
- int ret;
- word32 size;
-
- WOLFSSL_ENTER("wolfSSL_d2i_PKCS12_bio");
-
- if (bio == NULL) {
- WOLFSSL_MSG("Bad Function Argument bio is NULL");
- return NULL;
- }
-
- localPkcs12 = wc_PKCS12_new();
- if (localPkcs12 == NULL) {
- WOLFSSL_MSG("Memory error");
- return NULL;
- }
-
- if (pkcs12 != NULL) {
- *pkcs12 = localPkcs12;
- }
-
- ret = wolfSSL_BIO_get_mem_data(bio, &mem);
- if (mem == NULL || ret <= 0) {
- WOLFSSL_MSG("Failed to get data from bio struct");
- wc_PKCS12_free(localPkcs12);
- if (pkcs12 != NULL) {
- *pkcs12 = NULL;
- }
- return NULL;
- }
- size = ret;
-
- ret = wc_d2i_PKCS12(mem, size, localPkcs12);
- if (ret < 0) {
- WOLFSSL_MSG("Failed to get PKCS12 sequence");
- wc_PKCS12_free(localPkcs12);
- if (pkcs12 != NULL) {
- *pkcs12 = NULL;
- }
- return NULL;
- }
-
- return localPkcs12;
-}
-
-/* Converts the PKCS12 to DER format and outputs it into bio.
- *
- * bio is the structure to hold output DER
- * pkcs12 structure to create DER from
- *
- * return 1 for success or 0 if an error occurs
- */
-int wolfSSL_i2d_PKCS12_bio(WOLFSSL_BIO *bio, WC_PKCS12 *pkcs12)
-{
- int ret = WOLFSSL_FAILURE;
-
- WOLFSSL_ENTER("wolfSSL_i2d_PKCS12_bio");
-
- if ((bio != NULL) && (pkcs12 != NULL)) {
- word32 certSz = 0;
- byte *certDer = NULL;
-
- certSz = wc_i2d_PKCS12(pkcs12, &certDer, NULL);
- if ((certSz > 0) && (certDer != NULL)) {
- if (wolfSSL_BIO_write(bio, certDer, certSz) == (int)certSz) {
- ret = SSL_SUCCESS;
- }
- }
-
- if (certDer != NULL) {
- XFREE(certDer, NULL, DYNAMIC_TYPE_PKCS);
- }
- }
-
- return ret;
-}
-
-/* helper function to get raw pointer to DER buffer from WOLFSSL_EVP_PKEY */
-static int wolfSSL_EVP_PKEY_get_der(WOLFSSL_EVP_PKEY* key, unsigned char** der)
-{
- if (!key)
- return WOLFSSL_FAILURE;
- if (der)
- *der = (unsigned char*)key->pkey.ptr;
- return key->pkey_sz;
-}
-
-/* Copies unencrypted DER key buffer into "der". If "der" is null then the size
- * of buffer needed is returned
- * NOTE: This also advances the "der" pointer to be at the end of buffer.
- *
- * Returns size of key buffer on success
- */
-int wolfSSL_i2d_PrivateKey(WOLFSSL_EVP_PKEY* key, unsigned char** der)
-{
- if (key == NULL) {
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (key->pkey_sz <= 0 || !key->pkey.ptr) {
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (der != NULL) {
- /* since this function signature has no size value passed in it is
- * assumed that the user has allocated a large enough buffer */
- XMEMCPY(*der, key->pkey.ptr, key->pkey_sz);
- *der += key->pkey_sz;
- }
- return key->pkey_sz;
-}
-
-/* Creates a new WC_PKCS12 structure
- *
- * pass password to use
- * name friendlyName to use
- * pkey private key to go into PKCS12 bundle
- * cert certificate to go into PKCS12 bundle
- * ca extra certificates that can be added to bundle. Can be NULL
- * keyNID type of encryption to use on the key (-1 means no encryption)
- * certNID type of encryption to use on the certificate
- * itt number of iterations with encryption
- * macItt number of iterations with mac creation
- * keyType flag for signature and/or encryption key
- *
- * returns a pointer to a new WC_PKCS12 structure on success and NULL on fail
- */
-WC_PKCS12* wolfSSL_PKCS12_create(char* pass, char* name,
- WOLFSSL_EVP_PKEY* pkey, WOLFSSL_X509* cert,
- WOLF_STACK_OF(WOLFSSL_X509)* ca,
- int keyNID, int certNID, int itt, int macItt, int keyType)
-{
- WC_PKCS12* pkcs12;
- WC_DerCertList* list = NULL;
- word32 passSz;
- byte* keyDer;
- word32 keyDerSz;
- byte* certDer;
- int certDerSz;
-
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_PKCS12_create()");
-
- if (pass == NULL || pkey == NULL || cert == NULL) {
- WOLFSSL_LEAVE("wolfSSL_PKCS12_create()", BAD_FUNC_ARG);
- return NULL;
- }
- passSz = (word32)XSTRLEN(pass);
-
- if ((ret = wolfSSL_EVP_PKEY_get_der(pkey, &keyDer)) < 0) {
- WOLFSSL_LEAVE("wolfSSL_PKCS12_create", ret);
- return NULL;
- }
- keyDerSz = ret;
-
- certDer = (byte*)wolfSSL_X509_get_der(cert, &certDerSz);
- if (certDer == NULL) {
- return NULL;
- }
-
- if (ca != NULL) {
- WC_DerCertList* cur;
- unsigned long numCerts = ca->num;
- byte* curDer;
- int curDerSz = 0;
- WOLFSSL_STACK* sk = ca;
-
- while (numCerts > 0 && sk != NULL) {
- cur = (WC_DerCertList*)XMALLOC(sizeof(WC_DerCertList), NULL,
- DYNAMIC_TYPE_PKCS);
- if (cur == NULL) {
- wc_FreeCertList(list, NULL);
- return NULL;
- }
-
- curDer = (byte*)wolfSSL_X509_get_der(sk->data.x509, &curDerSz);
- if (curDer == NULL || curDerSz < 0) {
- XFREE(cur, NULL, DYNAMIC_TYPE_PKCS);
- wc_FreeCertList(list, NULL);
- return NULL;
- }
-
- cur->buffer = (byte*)XMALLOC(curDerSz, NULL, DYNAMIC_TYPE_PKCS);
- if (cur->buffer == NULL) {
- XFREE(cur, NULL, DYNAMIC_TYPE_PKCS);
- wc_FreeCertList(list, NULL);
- return NULL;
- }
- XMEMCPY(cur->buffer, curDer, curDerSz);
- cur->bufferSz = curDerSz;
- cur->next = list;
- list = cur;
-
- sk = sk->next;
- numCerts--;
- }
- }
-
- pkcs12 = wc_PKCS12_create(pass, passSz, name, keyDer, keyDerSz,
- certDer, certDerSz, list, keyNID, certNID, itt, macItt,
- keyType, NULL);
-
- if (ca != NULL) {
- wc_FreeCertList(list, NULL);
- }
-
- return pkcs12;
-}
-
-
-/* return WOLFSSL_SUCCESS on success, WOLFSSL_FAILURE on failure */
-int wolfSSL_PKCS12_parse(WC_PKCS12* pkcs12, const char* psw,
- WOLFSSL_EVP_PKEY** pkey, WOLFSSL_X509** cert, WOLF_STACK_OF(WOLFSSL_X509)** ca)
-{
- DecodedCert DeCert;
- void* heap = NULL;
- int ret;
- byte* certData = NULL;
- word32 certDataSz;
- byte* pk = NULL;
- word32 pkSz;
- WC_DerCertList* certList = NULL;
-
- WOLFSSL_ENTER("wolfSSL_PKCS12_parse");
-
- /* make sure we init return args */
- if (pkey) *pkey = NULL;
- if (cert) *cert = NULL;
- if (ca) *ca = NULL;
-
- if (pkcs12 == NULL || psw == NULL || pkey == NULL || cert == NULL) {
- WOLFSSL_MSG("Bad argument value");
- return WOLFSSL_FAILURE;
- }
-
- heap = wc_PKCS12_GetHeap(pkcs12);
-
- if (ca == NULL) {
- ret = wc_PKCS12_parse(pkcs12, psw, &pk, &pkSz, &certData, &certDataSz,
- NULL);
- }
- else {
- ret = wc_PKCS12_parse(pkcs12, psw, &pk, &pkSz, &certData, &certDataSz,
- &certList);
- }
- if (ret < 0) {
- WOLFSSL_LEAVE("wolfSSL_PKCS12_parse", ret);
- return WOLFSSL_FAILURE;
- }
-
- /* Decode cert and place in X509 stack struct */
- if (certList != NULL) {
- WC_DerCertList* current = certList;
-
- *ca = (WOLF_STACK_OF(WOLFSSL_X509)*)XMALLOC(sizeof(WOLF_STACK_OF(WOLFSSL_X509)),
- heap, DYNAMIC_TYPE_X509);
- if (*ca == NULL) {
- if (pk != NULL) {
- XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
- }
- if (certData != NULL) {
- XFREE(*cert, heap, DYNAMIC_TYPE_PKCS); *cert = NULL;
- }
- /* Free up WC_DerCertList and move on */
- while (current != NULL) {
- WC_DerCertList* next = current->next;
-
- XFREE(current->buffer, heap, DYNAMIC_TYPE_PKCS);
- XFREE(current, heap, DYNAMIC_TYPE_PKCS);
- current = next;
- }
- return WOLFSSL_FAILURE;
- }
- XMEMSET(*ca, 0, sizeof(WOLF_STACK_OF(WOLFSSL_X509)));
-
- /* add list of DER certs as X509's to stack */
- while (current != NULL) {
- WC_DerCertList* toFree = current;
- WOLFSSL_X509* x509;
-
- x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), heap,
- DYNAMIC_TYPE_X509);
- InitX509(x509, 1, heap);
- InitDecodedCert(&DeCert, current->buffer, current->bufferSz, heap);
- if (ParseCertRelative(&DeCert, CERT_TYPE, NO_VERIFY, NULL) != 0) {
- WOLFSSL_MSG("Issue with parsing certificate");
- FreeDecodedCert(&DeCert);
- wolfSSL_X509_free(x509);
- }
- else {
- if ((ret = CopyDecodedToX509(x509, &DeCert)) != 0) {
- WOLFSSL_MSG("Failed to copy decoded cert");
- FreeDecodedCert(&DeCert);
- wolfSSL_X509_free(x509);
- wolfSSL_sk_X509_free(*ca); *ca = NULL;
- if (pk != NULL) {
- XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
- }
- if (certData != NULL) {
- XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
- }
- /* Free up WC_DerCertList */
- while (current != NULL) {
- WC_DerCertList* next = current->next;
-
- XFREE(current->buffer, heap, DYNAMIC_TYPE_PKCS);
- XFREE(current, heap, DYNAMIC_TYPE_PKCS);
- current = next;
- }
- return WOLFSSL_FAILURE;
- }
- FreeDecodedCert(&DeCert);
-
- if (wolfSSL_sk_X509_push(*ca, x509) != 1) {
- WOLFSSL_MSG("Failed to push x509 onto stack");
- wolfSSL_X509_free(x509);
- wolfSSL_sk_X509_free(*ca); *ca = NULL;
- if (pk != NULL) {
- XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
- }
- if (certData != NULL) {
- XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
- }
-
- /* Free up WC_DerCertList */
- while (current != NULL) {
- WC_DerCertList* next = current->next;
-
- XFREE(current->buffer, heap, DYNAMIC_TYPE_PKCS);
- XFREE(current, heap, DYNAMIC_TYPE_PKCS);
- current = next;
- }
- return WOLFSSL_FAILURE;
- }
- }
- current = current->next;
- XFREE(toFree->buffer, heap, DYNAMIC_TYPE_PKCS);
- XFREE(toFree, heap, DYNAMIC_TYPE_PKCS);
- }
- }
-
-
- /* Decode cert and place in X509 struct */
- if (certData != NULL) {
- *cert = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), heap,
- DYNAMIC_TYPE_X509);
- if (*cert == NULL) {
- if (pk != NULL) {
- XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
- }
- if (ca != NULL) {
- wolfSSL_sk_X509_free(*ca); *ca = NULL;
- }
- XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
- return WOLFSSL_FAILURE;
- }
- InitX509(*cert, 1, heap);
- InitDecodedCert(&DeCert, certData, certDataSz, heap);
- if (ParseCertRelative(&DeCert, CERT_TYPE, NO_VERIFY, NULL) != 0) {
- WOLFSSL_MSG("Issue with parsing certificate");
- }
- if ((ret = CopyDecodedToX509(*cert, &DeCert)) != 0) {
- WOLFSSL_MSG("Failed to copy decoded cert");
- FreeDecodedCert(&DeCert);
- if (pk != NULL) {
- XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
- }
- if (ca != NULL) {
- wolfSSL_sk_X509_free(*ca); *ca = NULL;
- }
- wolfSSL_X509_free(*cert); *cert = NULL;
- return WOLFSSL_FAILURE;
- }
- FreeDecodedCert(&DeCert);
- XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
- }
-
-
- /* get key type */
- ret = BAD_STATE_E;
- if (pk != NULL) { /* decode key if present */
- *pkey = wolfSSL_EVP_PKEY_new_ex(heap);
- if (*pkey == NULL) {
- wolfSSL_X509_free(*cert); *cert = NULL;
- if (ca != NULL) {
- wolfSSL_sk_X509_free(*ca); *ca = NULL;
- }
- XFREE(pk, heap, DYNAMIC_TYPE_PUBLIC_KEY);
- return WOLFSSL_FAILURE;
- }
- #ifndef NO_RSA
- {
- word32 keyIdx = 0;
- RsaKey key;
-
- if (wc_InitRsaKey(&key, heap) != 0) {
- ret = BAD_STATE_E;
- }
- else {
- if ((ret = wc_RsaPrivateKeyDecode(pk, &keyIdx, &key, pkSz))
- == 0) {
- (*pkey)->type = EVP_PKEY_RSA;
- (*pkey)->rsa = wolfSSL_RSA_new();
- (*pkey)->ownRsa = 1; /* we own RSA */
- if ((*pkey)->rsa == NULL) {
- WOLFSSL_MSG("issue creating EVP RSA key");
- wolfSSL_X509_free(*cert); *cert = NULL;
- if (ca != NULL) {
- wolfSSL_sk_X509_free(*ca); *ca = NULL;
- }
- wolfSSL_EVP_PKEY_free(*pkey); *pkey = NULL;
- XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
- return WOLFSSL_FAILURE;
- }
- if ((ret = wolfSSL_RSA_LoadDer_ex((*pkey)->rsa, pk, pkSz,
- WOLFSSL_RSA_LOAD_PRIVATE)) != SSL_SUCCESS) {
- WOLFSSL_MSG("issue loading RSA key");
- wolfSSL_X509_free(*cert); *cert = NULL;
- if (ca != NULL) {
- wolfSSL_sk_X509_free(*ca); *ca = NULL;
- }
- wolfSSL_EVP_PKEY_free(*pkey); *pkey = NULL;
- XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
- return WOLFSSL_FAILURE;
- }
-
- WOLFSSL_MSG("Found PKCS12 RSA key");
- ret = 0; /* set in success state for upcoming ECC check */
- }
- wc_FreeRsaKey(&key);
- }
- }
- #endif /* NO_RSA */
-
- #ifdef HAVE_ECC
- {
- word32 keyIdx = 0;
- ecc_key key;
-
- if (ret != 0) { /* if is in fail state check if ECC key */
- if (wc_ecc_init(&key) != 0) {
- wolfSSL_X509_free(*cert); *cert = NULL;
- if (ca != NULL) {
- wolfSSL_sk_X509_free(*ca); *ca = NULL;
- }
- wolfSSL_EVP_PKEY_free(*pkey); *pkey = NULL;
- XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
- return WOLFSSL_FAILURE;
- }
-
- if ((ret = wc_EccPrivateKeyDecode(pk, &keyIdx, &key, pkSz))
- != 0) {
- wolfSSL_X509_free(*cert); *cert = NULL;
- if (ca != NULL) {
- wolfSSL_sk_X509_free(*ca); *ca = NULL;
- }
- wolfSSL_EVP_PKEY_free(*pkey); *pkey = NULL;
- XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
- WOLFSSL_MSG("Bad PKCS12 key format");
- return WOLFSSL_FAILURE;
- }
- (*pkey)->type = EVP_PKEY_EC;
- (*pkey)->pkey_curve = key.dp->oidSum;
- wc_ecc_free(&key);
- WOLFSSL_MSG("Found PKCS12 ECC key");
- }
- }
- #else
- if (ret != 0) { /* if is in fail state and no ECC then fail */
- wolfSSL_X509_free(*cert); *cert = NULL;
- if (ca != NULL) {
- wolfSSL_sk_X509_free(*ca); *ca = NULL;
- }
- wolfSSL_EVP_PKEY_free(*pkey); *pkey = NULL;
- XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
- WOLFSSL_MSG("Bad PKCS12 key format");
- return WOLFSSL_FAILURE;
- }
- #endif /* HAVE_ECC */
-
- (*pkey)->save_type = 0;
- (*pkey)->pkey_sz = pkSz;
- (*pkey)->pkey.ptr = (char*)pk;
- }
-
- (void)ret;
- (void)ca;
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* !defined(NO_ASN) && !defined(NO_PWDBASED) */
-
-
-/* no-op function. Was initially used for adding encryption algorithms available
- * for PKCS12 */
-void wolfSSL_PKCS12_PBE_add(void)
-{
- WOLFSSL_ENTER("wolfSSL_PKCS12_PBE_add");
-}
-
-
-
-WOLFSSL_STACK* wolfSSL_X509_STORE_CTX_get_chain(WOLFSSL_X509_STORE_CTX* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_chain");
-
- if (ctx == NULL) {
- return NULL;
- }
-
-#ifdef SESSION_CERTS
- /* if chain is null but sesChain is available then populate stack */
- if (ctx->chain == NULL && ctx->sesChain != NULL) {
- int i;
- WOLFSSL_X509_CHAIN* c = ctx->sesChain;
- WOLFSSL_STACK* sk = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK),
- NULL, DYNAMIC_TYPE_X509);
-
- if (sk == NULL) {
- return NULL;
- }
-
- XMEMSET(sk, 0, sizeof(WOLFSSL_STACK));
- ctx->chain = sk;
-
- for (i = 0; i < c->count && i < MAX_CHAIN_DEPTH; i++) {
- WOLFSSL_X509* x509 = wolfSSL_get_chain_X509(c, i);
-
- if (x509 == NULL) {
- WOLFSSL_MSG("Unable to get x509 from chain");
- wolfSSL_sk_X509_free(sk);
- return NULL;
- }
-
- if (wolfSSL_sk_X509_push(sk, x509) != SSL_SUCCESS) {
- WOLFSSL_MSG("Unable to load x509 into stack");
- wolfSSL_sk_X509_free(sk);
- wolfSSL_X509_free(x509);
- return NULL;
- }
- }
-
-#if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
- /* add CA used to verify top of chain to the list */
- if (c->count > 0) {
- WOLFSSL_X509* x509 = wolfSSL_get_chain_X509(c, c->count - 1);
- if (x509 != NULL) {
- WOLFSSL_X509* issuer = NULL;
- if (wolfSSL_X509_STORE_CTX_get1_issuer(&issuer, ctx, x509)
- == WOLFSSL_SUCCESS) {
- /* check that the certificate being looked up is not self
- * signed and that a issuer was found */
- if (issuer != NULL && wolfSSL_X509_NAME_cmp(&x509->issuer,
- &x509->subject) != 0) {
- if (wolfSSL_sk_X509_push(sk, issuer) != SSL_SUCCESS) {
- WOLFSSL_MSG("Unable to load CA x509 into stack");
- wolfSSL_sk_X509_free(sk);
- wolfSSL_X509_free(issuer);
- return NULL;
- }
- }
- else {
- WOLFSSL_MSG("Certificate is self signed");
- if (issuer != NULL)
- wolfSSL_X509_free(issuer);
- }
- }
- else {
- WOLFSSL_MSG("Could not find CA for certificate");
- }
- }
- }
-#endif
-
- }
-#endif /* SESSION_CERTS */
-
- return ctx->chain;
-}
-
-/* make shallow copy of the stack, data pointers are copied by reference */
-WOLFSSL_STACK* wolfSSL_sk_X509_dup(WOLFSSL_STACK* sk)
-{
- unsigned long i;
- WOLFSSL_STACK* dup = NULL;
- WOLFSSL_STACK* node = NULL;
- WOLFSSL_STACK *dIdx = NULL, *sIdx = sk;
-
- if (sk == NULL) {
- return NULL;
- }
-
- for (i = 0; i < sk->num; i++) {
-
- node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_X509);
- if (node == NULL) {
- if (i != 0) {
- wolfSSL_sk_free(dup);
- }
- WOLFSSL_MSG("Memory error");
- return NULL;
- }
- XMEMSET(node, 0, sizeof(WOLFSSL_STACK));
-
- /* copy sk node to new node, data by reference */
- node->data.x509 = sIdx->data.x509;
- node->num = sIdx->num;
-
- /* insert node into list, progress idx */
- if (i == 0) {
- dup = node;
- } else {
- dIdx->next = node;
- }
-
- dIdx = node;
- sIdx = sIdx->next;
- }
-
- return dup;
-}
-
-
-/* like X509_STORE_CTX_get_chain(), but return a copy with data reference
- counts increased */
-WOLFSSL_STACK* wolfSSL_X509_STORE_CTX_get1_chain(WOLFSSL_X509_STORE_CTX* ctx)
-{
- unsigned long i;
- WOLFSSL_STACK* ref;
- WOLFSSL_STACK* dup;
-
- if (ctx == NULL) {
- return NULL;
- }
-
- /* get chain in ctx */
- ref = wolfSSL_X509_STORE_CTX_get_chain(ctx);
- if (ref == NULL) {
- return ref;
- }
-
- /* create duplicate of ctx chain */
- dup = wolfSSL_sk_X509_dup(ref);
- if (dup == NULL) {
- return NULL;
- }
-
- /* increase ref counts of inner data X509 */
- ref = dup;
- for (i = 0; i < dup->num && ref != NULL; i++) {
- if (wc_LockMutex(&ref->data.x509->refMutex) != 0) {
- WOLFSSL_MSG("Failed to lock x509 mutex");
- }
- ref->data.x509->refCount++;
- wc_UnLockMutex(&ref->data.x509->refMutex);
- ref = ref->next;
- }
-
- return dup;
-}
-
-
-int wolfSSL_X509_STORE_add_cert(WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509)
-{
- int result = WOLFSSL_FATAL_ERROR;
-
- WOLFSSL_ENTER("wolfSSL_X509_STORE_add_cert");
- if (store != NULL && store->cm != NULL && x509 != NULL
- && x509->derCert != NULL) {
- DerBuffer* derCert = NULL;
-
- result = AllocDer(&derCert, x509->derCert->length,
- x509->derCert->type, NULL);
- if (result == 0) {
- /* AddCA() frees the buffer. */
- XMEMCPY(derCert->buffer,
- x509->derCert->buffer, x509->derCert->length);
- result = AddCA(store->cm, &derCert, WOLFSSL_USER_CA, VERIFY);
- }
- }
-
- WOLFSSL_LEAVE("wolfSSL_X509_STORE_add_cert", result);
-
- if (result != WOLFSSL_SUCCESS) {
- result = WOLFSSL_FATAL_ERROR;
- }
-
- return result;
-}
-
-WOLFSSL_X509_STORE* wolfSSL_X509_STORE_new(void)
-{
- WOLFSSL_X509_STORE* store = NULL;
- WOLFSSL_ENTER("SSL_X509_STORE_new");
-
- if ((store = (WOLFSSL_X509_STORE*)XMALLOC(sizeof(WOLFSSL_X509_STORE), NULL,
- DYNAMIC_TYPE_X509_STORE)) == NULL)
- goto err_exit;
-
- XMEMSET(store, 0, sizeof(WOLFSSL_X509_STORE));
- store->isDynamic = 1;
-
- if ((store->cm = wolfSSL_CertManagerNew()) == NULL)
- goto err_exit;
-
-#ifdef HAVE_CRL
- store->crl = NULL;
- if ((store->crl = (WOLFSSL_X509_CRL *)XMALLOC(sizeof(WOLFSSL_X509_CRL),
- NULL, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
- goto err_exit;
- if (InitCRL(store->crl, NULL) < 0)
- goto err_exit;
-#endif
-
-#ifdef OPENSSL_EXTRA
- if ((store->param = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
- sizeof(WOLFSSL_X509_VERIFY_PARAM),
- NULL,DYNAMIC_TYPE_OPENSSL)) == NULL)
- goto err_exit;
-
-#endif
-
- return store;
-
-err_exit:
- if (store == NULL)
- return NULL;
-
- wolfSSL_X509_STORE_free(store);
-
- return NULL;
-}
-
-
-void wolfSSL_X509_STORE_free(WOLFSSL_X509_STORE* store)
-{
- if (store != NULL && store->isDynamic) {
- if (store->cm != NULL)
- wolfSSL_CertManagerFree(store->cm);
-#ifdef HAVE_CRL
- if (store->crl != NULL)
- wolfSSL_X509_CRL_free(store->crl);
-#endif
-#ifdef OPENSSL_EXTRA
- if (store->param != NULL)
- XFREE(store->param, NULL, DYNAMIC_TYPE_OPENSSL);
-#endif
- XFREE(store, NULL, DYNAMIC_TYPE_X509_STORE);
- }
-}
-
-
-int wolfSSL_X509_STORE_set_flags(WOLFSSL_X509_STORE* store, unsigned long flag)
-{
- int ret = WOLFSSL_SUCCESS;
-
- WOLFSSL_ENTER("wolfSSL_X509_STORE_set_flags");
-
- if (store == NULL)
- return WOLFSSL_FAILURE;
-
- if ((flag & WOLFSSL_CRL_CHECKALL) || (flag & WOLFSSL_CRL_CHECK)) {
- ret = wolfSSL_CertManagerEnableCRL(store->cm, (int)flag);
- }
-
- (void)store;
- (void)flag;
-
- return ret;
-}
-
-
-int wolfSSL_X509_STORE_set_default_paths(WOLFSSL_X509_STORE* store)
-{
- (void)store;
- return WOLFSSL_SUCCESS;
-}
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_X509_STORE_get_by_subject(WOLFSSL_X509_STORE_CTX* ctx, int idx,
- WOLFSSL_X509_NAME* name, WOLFSSL_X509_OBJECT* obj)
-{
- (void)ctx;
- (void)idx;
- (void)name;
- (void)obj;
- WOLFSSL_STUB("X509_STORE_get_by_subject");
- return 0;
-}
-#endif
-
-WOLFSSL_X509_STORE_CTX* wolfSSL_X509_STORE_CTX_new(void)
-{
- WOLFSSL_X509_STORE_CTX* ctx;
- WOLFSSL_ENTER("X509_STORE_CTX_new");
-
- ctx = (WOLFSSL_X509_STORE_CTX*)XMALLOC(sizeof(WOLFSSL_X509_STORE_CTX), NULL,
- DYNAMIC_TYPE_X509_CTX);
- if (ctx != NULL) {
- ctx->param = NULL;
- wolfSSL_X509_STORE_CTX_init(ctx, NULL, NULL, NULL);
- }
-
- return ctx;
-}
-
-
-int wolfSSL_X509_STORE_CTX_init(WOLFSSL_X509_STORE_CTX* ctx,
- WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509, WOLF_STACK_OF(WOLFSSL_X509)* sk)
-{
- WOLFSSL_X509* x509_cert;
- int ret = 0;
- (void)sk;
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_init");
-
- if (ctx != NULL) {
- ctx->store = store;
- #ifndef WOLFSSL_X509_STORE_CERTS
- ctx->current_cert = x509;
- #else
- if(x509 != NULL){
- ctx->current_cert = wolfSSL_X509_d2i(NULL, x509->derCert->buffer,x509->derCert->length);
- if(ctx->current_cert == NULL)
- return WOLFSSL_FATAL_ERROR;
- } else
- ctx->current_cert = NULL;
- #endif
-
- ctx->chain = sk;
- /* Add intermediate certificates from stack to store */
- while (sk != NULL) {
- x509_cert = sk->data.x509;
- if (x509_cert != NULL && x509_cert->isCa) {
- ret = wolfSSL_X509_STORE_add_cert(store, x509_cert);
- if (ret < 0) {
- return WOLFSSL_FATAL_ERROR;
- }
- }
- sk = sk->next;
- }
-
- ctx->sesChain = NULL;
- ctx->domain = NULL;
-#if defined(HAVE_EX_DATA) || defined(FORTRESS)
- XMEMSET(&ctx->ex_data, 0, sizeof(ctx->ex_data));
-#endif
- ctx->userCtx = NULL;
- ctx->error = 0;
- ctx->error_depth = 0;
- ctx->discardSessionCerts = 0;
-#ifdef OPENSSL_EXTRA
- if (ctx->param == NULL) {
- ctx->param = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
- sizeof(WOLFSSL_X509_VERIFY_PARAM),
- NULL,DYNAMIC_TYPE_OPENSSL);
- if (ctx->param == NULL){
- WOLFSSL_MSG("wolfSSL_X509_STORE_CTX_init failed");
- return SSL_FATAL_ERROR;
- }
- }
-#endif
- return SSL_SUCCESS;
- }
- return WOLFSSL_FATAL_ERROR;
-}
-
-
-void wolfSSL_X509_STORE_CTX_free(WOLFSSL_X509_STORE_CTX* ctx)
-{
- WOLFSSL_ENTER("X509_STORE_CTX_free");
- if (ctx != NULL) {
- #if !defined(OPENSSL_ALL) && !defined(WOLFSSL_QT)
- if (ctx->store != NULL)
- wolfSSL_X509_STORE_free(ctx->store);
- #ifndef WOLFSSL_KEEP_STORE_CERTS
- if (ctx->current_cert != NULL)
- wolfSSL_FreeX509(ctx->current_cert);
- #endif
- #endif /* !OPENSSL_ALL && !WOLFSSL_QT */
-#ifdef OPENSSL_EXTRA
- if (ctx->param != NULL){
- XFREE(ctx->param,NULL,DYNAMIC_TYPE_OPENSSL);
- }
-#endif
- XFREE(ctx, NULL, DYNAMIC_TYPE_X509_CTX);
- }
-}
-
-
-void wolfSSL_X509_STORE_CTX_cleanup(WOLFSSL_X509_STORE_CTX* ctx)
-{
- (void)ctx;
- /* Do nothing */
-}
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
-/* Returns corresponding X509 error from internal ASN error */
-static int GetX509Error(int e)
-{
- switch (e) {
- case ASN_BEFORE_DATE_E:
- return X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
- case ASN_AFTER_DATE_E:
- return X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
- case ASN_NO_SIGNER_E:
- return X509_V_ERR_INVALID_CA;
- case ASN_SELF_SIGNED_E:
- return X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
- case ASN_PATHLEN_INV_E:
- case ASN_PATHLEN_SIZE_E:
- return X509_V_ERR_PATH_LENGTH_EXCEEDED;
- case ASN_SIG_OID_E:
- case ASN_SIG_CONFIRM_E:
- case ASN_SIG_HASH_E:
- case ASN_SIG_KEY_E:
- return X509_V_ERR_CERT_SIGNATURE_FAILURE;
- default:
- WOLFSSL_MSG("Error not configured or implemented yet");
- return e;
- }
-}
-#endif
-
-/* Verifies certificate chain using WOLFSSL_X509_STORE_CTX
- * returns 0 on success or < 0 on failure.
- */
-int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
-{
- int ret = 0;
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- int depth = 0;
- int error;
- byte *afterDate, *beforeDate;
-#endif
- WOLFSSL_ENTER("wolfSSL_X509_verify_cert");
-
- if (ctx != NULL && ctx->store != NULL && ctx->store->cm != NULL
- && ctx->current_cert != NULL && ctx->current_cert->derCert != NULL) {
- ret = wolfSSL_CertManagerVerifyBuffer(ctx->store->cm,
- ctx->current_cert->derCert->buffer,
- ctx->current_cert->derCert->length,
- WOLFSSL_FILETYPE_ASN1);
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- /* If there was an error, process it and add it to CTX */
- if (ret < 0) {
- /* Get corresponding X509 error */
- error = GetX509Error(ret);
- /* Set error depth */
- if (ctx->chain)
- depth = (int)ctx->chain->num;
-
- wolfSSL_X509_STORE_CTX_set_error(ctx, error);
- wolfSSL_X509_STORE_CTX_set_error_depth(ctx, depth);
- ctx->store->verify_cb(0, ctx);
- }
-
- error = 0;
- /* wolfSSL_CertManagerVerifyBuffer only returns ASN_AFTER_DATE_E or
- ASN_BEFORE_DATE_E if there are no additional errors found in the
- cert. Therefore, check if the cert is expired or not yet valid
- in order to return the correct expected error. */
- afterDate = ctx->current_cert->notAfter.data;
- beforeDate = ctx->current_cert->notBefore.data;
-
- if (ValidateDate(afterDate, ctx->current_cert->notAfter.type,
- AFTER) < 1) {
- error = X509_V_ERR_CERT_HAS_EXPIRED;
- }
- else if (ValidateDate(beforeDate, ctx->current_cert->notBefore.type,
- BEFORE) < 1) {
- error = X509_V_ERR_CERT_NOT_YET_VALID;
- }
-
- if (error != 0 ) {
- wolfSSL_X509_STORE_CTX_set_error(ctx, error);
- wolfSSL_X509_STORE_CTX_set_error_depth(ctx, depth);
- if (ctx->store && ctx->store->verify_cb)
- ctx->store->verify_cb(0, ctx);
- }
-#endif /* OPENSSL_ALL || WOLFSSL_QT */
- return ret;
- }
- return WOLFSSL_FATAL_ERROR;
-}
-
-
-/* Use the public key to verify the signature. Note: this only verifies
- * the certificate signature.
- * returns WOLFSSL_SUCCESS on successful signature verification */
-int wolfSSL_X509_verify(WOLFSSL_X509* x509, WOLFSSL_EVP_PKEY* pkey)
-{
- int ret;
- const byte* der;
- int derSz = 0;
- int type;
-
- if (x509 == NULL || pkey == NULL) {
- return WOLFSSL_FATAL_ERROR;
- }
-
- der = wolfSSL_X509_get_der(x509, &derSz);
- if (der == NULL) {
- WOLFSSL_MSG("Error getting WOLFSSL_X509 DER");
- return WOLFSSL_FATAL_ERROR;
- }
-
- switch (pkey->type) {
- case EVP_PKEY_RSA:
- type = RSAk;
- break;
-
- case EVP_PKEY_EC:
- type = ECDSAk;
- break;
-
- case EVP_PKEY_DSA:
- type = DSAk;
- break;
-
- default:
- WOLFSSL_MSG("Unknown pkey key type");
- return WOLFSSL_FATAL_ERROR;
- }
-
- ret = CheckCertSignaturePubKey(der, derSz, x509->heap,
- (unsigned char*)pkey->pkey.ptr, pkey->pkey_sz, type);
- if (ret == 0) {
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-#endif /* NO_CERTS */
-
-#if !defined(NO_FILESYSTEM)
-static void *wolfSSL_d2i_X509_fp_ex(XFILE file, void **x509, int type)
-{
- void *newx509 = NULL;
- byte *fileBuffer = NULL;
- long sz = 0;
-
- /* init variable */
- if (x509)
- *x509 = NULL;
-
- /* argument check */
- if (file == XBADFILE) {
- return NULL;
- }
-
- /* determine file size */
- if (XFSEEK(file, 0, XSEEK_END) != 0) {
- return NULL;
- }
- sz = XFTELL(file);
- XREWIND(file);
-
- if (sz > MAX_WOLFSSL_FILE_SIZE || sz <= 0) {
- WOLFSSL_MSG("d2i_X509_fp_ex file size error");
- return NULL;
- }
-
- fileBuffer = (byte *)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
- if (fileBuffer != NULL) {
- if ((long)XFREAD(fileBuffer, 1, sz, file) != sz) {
- WOLFSSL_MSG("File read failed");
- goto err_exit;
- }
- if (type == CERT_TYPE) {
- newx509 = (void *)wolfSSL_X509_d2i(NULL, fileBuffer, (int)sz);
- }
- #ifdef HAVE_CRL
- else if (type == CRL_TYPE) {
- newx509 = (void *)wolfSSL_d2i_X509_CRL(NULL, fileBuffer, (int)sz);
- }
- #endif
- #if !defined(NO_ASN) && !defined(NO_PWDBASED)
- else if (type == PKCS12_TYPE) {
- if ((newx509 = wc_PKCS12_new()) == NULL) {
- goto err_exit;
- }
- if (wc_d2i_PKCS12(fileBuffer, (int)sz, (WC_PKCS12*)newx509) < 0) {
- goto err_exit;
- }
- }
- #endif
- else {
- goto err_exit;
- }
- if (newx509 == NULL) {
- WOLFSSL_MSG("X509 failed");
- goto err_exit;
- }
- }
-
- if (x509)
- *x509 = newx509;
-
- goto _exit;
-
-err_exit:
-#if !defined(NO_ASN) && !defined(NO_PWDBASED)
- if ((newx509 != NULL) && (type == PKCS12_TYPE)) {
- wc_PKCS12_free((WC_PKCS12*)newx509);
- newx509 = NULL;
- }
-#endif
-_exit:
- if (fileBuffer != NULL)
- XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
-
- return newx509;
-}
-
-WOLFSSL_X509_PKCS12 *wolfSSL_d2i_PKCS12_fp(XFILE fp, WOLFSSL_X509_PKCS12 **pkcs12)
-{
- WOLFSSL_ENTER("wolfSSL_d2i_PKCS12_fp");
- return (WOLFSSL_X509_PKCS12 *)wolfSSL_d2i_X509_fp_ex(fp, (void **)pkcs12, PKCS12_TYPE);
-}
-
-WOLFSSL_X509 *wolfSSL_d2i_X509_fp(XFILE fp, WOLFSSL_X509 **x509)
-{
- WOLFSSL_ENTER("wolfSSL_d2i_X509_fp");
- return (WOLFSSL_X509 *)wolfSSL_d2i_X509_fp_ex(fp, (void **)x509, CERT_TYPE);
-}
-#endif /* !NO_FILESYSTEM */
-
-
-#ifdef HAVE_CRL
-#ifndef NO_FILESYSTEM
-WOLFSSL_X509_CRL *wolfSSL_d2i_X509_CRL_fp(XFILE fp, WOLFSSL_X509_CRL **crl)
-{
- WOLFSSL_ENTER("wolfSSL_d2i_X509_CRL_fp");
- return (WOLFSSL_X509_CRL *)wolfSSL_d2i_X509_fp_ex(fp, (void **)crl, CRL_TYPE);
-}
-#endif /* !NO_FILESYSTEM */
-
-
-WOLFSSL_X509_CRL* wolfSSL_d2i_X509_CRL(WOLFSSL_X509_CRL** crl,
- const unsigned char* in, int len)
-{
- WOLFSSL_X509_CRL *newcrl = NULL;
- int ret = WOLFSSL_SUCCESS;
-
- WOLFSSL_ENTER("wolfSSL_d2i_X509_CRL");
-
- if (in == NULL) {
- WOLFSSL_MSG("Bad argument value");
- } else {
- newcrl = (WOLFSSL_X509_CRL*)XMALLOC(sizeof(WOLFSSL_X509_CRL), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (newcrl == NULL){
- WOLFSSL_MSG("New CRL allocation failed");
- } else {
- ret = InitCRL(newcrl, NULL);
- if (ret < 0) {
- WOLFSSL_MSG("Init tmp CRL failed");
- } else {
- ret = BufferLoadCRL(newcrl, in, len, WOLFSSL_FILETYPE_ASN1,
- NO_VERIFY);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Buffer Load CRL failed");
- } else {
- if (crl) {
- *crl = newcrl;
- }
- }
- }
- }
- }
-
- if((ret != WOLFSSL_SUCCESS) && (newcrl != NULL)) {
- wolfSSL_X509_CRL_free(newcrl);
- newcrl = NULL;
- }
-
- return newcrl;
-}
-
-void wolfSSL_X509_CRL_free(WOLFSSL_X509_CRL *crl)
-{
- WOLFSSL_ENTER("wolfSSL_X509_CRL_free");
-
- FreeCRL(crl, 1);
- return;
-}
-#endif /* HAVE_CRL */
-
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_ASN1_TIME* wolfSSL_X509_CRL_get_lastUpdate(WOLFSSL_X509_CRL* crl)
-{
- (void)crl;
- WOLFSSL_STUB("X509_CRL_get_lastUpdate");
- return 0;
-}
-#endif
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_ASN1_TIME* wolfSSL_X509_CRL_get_nextUpdate(WOLFSSL_X509_CRL* crl)
-{
- (void)crl;
- WOLFSSL_STUB("X509_CRL_get_nextUpdate");
- return 0;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_X509_CRL_verify(WOLFSSL_X509_CRL* crl, WOLFSSL_EVP_PKEY* key)
-{
- (void)crl;
- (void)key;
- WOLFSSL_STUB("X509_CRL_verify");
- return 0;
-}
-#endif
-#endif /* OPENSSL_EXTRA */
-
-#ifdef OPENSSL_EXTRA
-
-/* Gets pointer to X509_STORE that was used to create context.
- *
- * Return valid pointer on success, NULL if ctx was NULL or not initialized
- */
-WOLFSSL_X509_STORE* wolfSSL_X509_STORE_CTX_get0_store(
- WOLFSSL_X509_STORE_CTX* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get0_store");
-
- if (ctx == NULL)
- return NULL;
-
- return ctx->store;
-}
-
-WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get0_cert(WOLFSSL_X509_STORE_CTX* ctx)
-{
- if (ctx == NULL)
- return NULL;
-
- return ctx->current_cert;
-}
-
-void wolfSSL_X509_STORE_CTX_set_time(WOLFSSL_X509_STORE_CTX* ctx,
- unsigned long flags,
- time_t t)
-{
- (void)flags;
-
- if (ctx == NULL || ctx->param == NULL)
- return;
-
- ctx->param->check_time = t;
- ctx->param->flags |= WOLFSSL_USE_CHECK_TIME;
-}
-
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_X509_STORE_CTX_set_purpose(WOLFSSL_X509_STORE_CTX *ctx,
- int purpose)
-{
- (void)ctx;
- (void)purpose;
- WOLFSSL_STUB("wolfSSL_X509_STORE_CTX_set_purpose");
- return 0;
-}
-#endif
-#endif /* WOLFSSL_QT || OPENSSL_ALL */
-
-#ifndef NO_WOLFSSL_STUB
-/* Returns default file name and path of config file. However
- a wolfssl.cnf file is not currently supported */
-char* wolfSSL_CONF_get1_default_config_file(void)
-{
- WOLFSSL_ENTER("wolfSSL_CONF_get1_default_config_file");
- WOLFSSL_STUB("CONF_get1_default_config_file");
- return NULL;
-}
-#endif
-/******************************************************************************
-* wolfSSL_X509_VERIFY_PARAM_set1_host - sets the DNS hostname to name
-* hostnames is cleared if name is NULL or empty.
-*
-* RETURNS:
-*
-*/
-int wolfSSL_X509_VERIFY_PARAM_set1_host(WOLFSSL_X509_VERIFY_PARAM* pParam,
- const char* name,
- unsigned int nameSz)
-{
- unsigned int sz = 0;
-
- if (pParam == NULL)
- return WOLFSSL_FAILURE;
-
- XMEMSET(pParam->hostName, 0, WOLFSSL_HOST_NAME_MAX);
-
- if (name == NULL)
- return WOLFSSL_SUCCESS;
-
- sz = (unsigned int)XSTRLEN(name);
-
- /* If name is NUL-terminated, namelen can be set to zero. */
- if(nameSz == 0 || nameSz > sz)
- nameSz = sz;
-
- if (nameSz > 0 && name[nameSz - 1] == '\0')
- nameSz--;
-
- if (nameSz > WOLFSSL_HOST_NAME_MAX-1)
- nameSz = WOLFSSL_HOST_NAME_MAX-1;
-
- if (nameSz > 0)
- XMEMCPY(pParam->hostName, name, nameSz);
-
- pParam->hostName[nameSz] = '\0';
-
- return WOLFSSL_SUCCESS;
-}
-/******************************************************************************
-* wolfSSL_get0_param - return a pointer to the SSL verification parameters
-*
-* RETURNS:
-* returns pointer to the SSL verification parameters on success,
-* otherwise returns NULL
-*/
-WOLFSSL_X509_VERIFY_PARAM* wolfSSL_get0_param(WOLFSSL* ssl)
-{
- if (ssl == NULL) {
- return NULL;
- }
- return ssl->param;
-}
-
-/* Set the host flag in the X509_VERIFY_PARAM structure */
-void wolfSSL_X509_VERIFY_PARAM_set_hostflags(WOLFSSL_X509_VERIFY_PARAM* param,
- unsigned int flags)
-{
- if (param != NULL) {
- param->hostFlags = flags;
- }
-}
-
-/* Sets the expected IP address to ipasc.
- *
- * param is a pointer to the X509_VERIFY_PARAM structure
- * ipasc is a NULL-terminated string with N.N.N.N for IPv4 and
- * HH:HH ... HH:HH for IPv6. There is no validation performed on the
- * parameter, and it must be an exact match with the IP in the cert.
- *
- * return 1 for success and 0 for failure*/
-int wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(WOLFSSL_X509_VERIFY_PARAM *param,
- const char *ipasc)
-{
- int ret = WOLFSSL_FAILURE;
-
- if (param != NULL) {
- if (ipasc == NULL) {
- param->ipasc[0] = '\0';
- }
- else {
- XSTRNCPY(param->ipasc, ipasc, WOLFSSL_MAX_IPSTR-1);
- param->ipasc[WOLFSSL_MAX_IPSTR-1] = '\0';
- }
- ret = WOLFSSL_SUCCESS;
- }
-
- return ret;
-}
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_X509_OBJECT_free_contents(WOLFSSL_X509_OBJECT* obj)
-{
- (void)obj;
- WOLFSSL_STUB("X509_OBJECT_free_contents");
-}
-#endif
-
-#ifndef NO_ASN_TIME
-int wolfSSL_X509_cmp_current_time(const WOLFSSL_ASN1_TIME* asnTime)
-{
- return wolfSSL_X509_cmp_time(asnTime, NULL);
-}
-
-/* return -1 if asnTime is earlier than or equal to cmpTime, and 1 otherwise
- * return 0 on error
- */
-int wolfSSL_X509_cmp_time(const WOLFSSL_ASN1_TIME* asnTime, time_t* cmpTime)
-{
- int ret = WOLFSSL_FAILURE, i = 0;
- time_t tmpTime, *pTime = &tmpTime;
- byte data_ptr[MAX_TIME_STRING_SZ], inv = 0;
- struct tm ts, *tmpTs, *ct;
-#if defined(NEED_TMP_TIME)
- /* for use with gmtime_r */
- struct tm tmpTimeStorage;
-
- tmpTs = &tmpTimeStorage;
-#else
- tmpTs = NULL;
-#endif
- (void)tmpTs;
-
- if (asnTime == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- if (cmpTime == NULL) {
- /* Use current time */
- *pTime = XTIME(0);
- }
- else {
- pTime = cmpTime;
- }
-
- /* Convert ASN1_time to time_t */
- XMEMSET(&ts, 0, sizeof(struct tm));
-
- /* Check type */
- if (asnTime->type == ASN_UTC_TIME) {
- /* 2-digit year */
- XMEMCPY(data_ptr, &asnTime->data[i], ASN_UTC_TIME_SIZE);
- ts.tm_year = (data_ptr[i] - '0') * 10; i++;
- ts.tm_year += data_ptr[i] - '0'; i++;
- if (ts.tm_year < 70) {
- ts.tm_year += 100;
- }
- }
- else if (asnTime->type == ASN_GENERALIZED_TIME) {
- /* 4-digit year */
- XMEMCPY(data_ptr, &asnTime->data[i], ASN_GENERALIZED_TIME_SIZE);
- ts.tm_year = (data_ptr[i] - '0') * 1000; i++;
- ts.tm_year += (data_ptr[i] - '0') * 100; i++;
- ts.tm_year += (data_ptr[i] - '0') * 10; i++;
- ts.tm_year += data_ptr[i] - '0'; i++;
- ts.tm_year -= 1900;
- }
- else {
- /* Invalid type */
- inv = 1;
- }
-
- if (inv != 1) {
- ts.tm_mon = (data_ptr[i] - '0') * 10; i++;
- ts.tm_mon += (data_ptr[i] - '0') - 1; i++; /* January is 0 not 1 */
- ts.tm_mday = (data_ptr[i] - '0') * 10; i++;
- ts.tm_mday += (data_ptr[i] - '0'); i++;
- ts.tm_hour = (data_ptr[i] - '0') * 10; i++;
- ts.tm_hour += (data_ptr[i] - '0'); i++;
- ts.tm_min = (data_ptr[i] - '0') * 10; i++;
- ts.tm_min += (data_ptr[i] - '0'); i++;
- ts.tm_sec = (data_ptr[i] - '0') * 10; i++;
- ts.tm_sec += (data_ptr[i] - '0');
-
- /* Convert to time struct*/
- ct = XGMTIME(pTime, tmpTs);
-
- if (ct == NULL)
- return GETTIME_ERROR;
-
- /* DateGreaterThan returns 1 for >; 0 for <= */
- ret = DateGreaterThan(&ts, ct) ? 1 : -1;
- }
-
- return ret;
-}
-#endif /* !NO_ASN_TIME */
-
-#if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
- !defined(TIME_OVERRIDES)
-WOLFSSL_ASN1_TIME *wolfSSL_X509_time_adj_ex(WOLFSSL_ASN1_TIME *asnTime,
- int offset_day, long offset_sec, time_t *in_tm)
-{
- /* get current time if in_tm is null */
- time_t t = in_tm ? *in_tm : XTIME(0);
- return wolfSSL_ASN1_TIME_adj(asnTime, t, offset_day, offset_sec);
-}
-
-WOLFSSL_ASN1_TIME *wolfSSL_X509_time_adj(WOLFSSL_ASN1_TIME *asnTime,
- long offset_sec, time_t *in_tm)
-{
- return wolfSSL_X509_time_adj_ex(asnTime, 0, offset_sec, in_tm);
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_sk_X509_REVOKED_num(WOLFSSL_X509_REVOKED* revoked)
-{
- (void)revoked;
- WOLFSSL_STUB("sk_X509_REVOKED_num");
- return 0;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_X509_REVOKED* wolfSSL_X509_CRL_get_REVOKED(WOLFSSL_X509_CRL* crl)
-{
- (void)crl;
- WOLFSSL_STUB("X509_CRL_get_REVOKED");
- return 0;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_X509_REVOKED* wolfSSL_sk_X509_REVOKED_value(
- WOLFSSL_X509_REVOKED* revoked, int value)
-{
- (void)revoked;
- (void)value;
- WOLFSSL_STUB("sk_X509_REVOKED_value");
- return 0;
-}
-#endif
-
-/* Used to create a new WOLFSSL_ASN1_INTEGER structure.
- * returns a pointer to new structure on success and NULL on failure
- */
-WOLFSSL_ASN1_INTEGER* wolfSSL_ASN1_INTEGER_new(void)
-{
- WOLFSSL_ASN1_INTEGER* a;
-
- a = (WOLFSSL_ASN1_INTEGER*)XMALLOC(sizeof(WOLFSSL_ASN1_INTEGER), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (a == NULL) {
- return NULL;
- }
-
- XMEMSET(a, 0, sizeof(WOLFSSL_ASN1_INTEGER));
- a->data = a->intData;
- a->dataMax = WOLFSSL_ASN1_INTEGER_MAX;
- a->length = 0;
- return a;
-}
-
-
-/* free's internal elements of WOLFSSL_ASN1_INTEGER and free's "in" itself */
-void wolfSSL_ASN1_INTEGER_free(WOLFSSL_ASN1_INTEGER* in)
-{
- if (in != NULL) {
- if (in->isDynamic) {
- XFREE(in->data, NULL, DYNAMIC_TYPE_OPENSSL);
- }
- XFREE(in, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-
-/* Duplicate all WOLFSSL_ASN1_INTEGER members from src to dup
- * src : WOLFSSL_ASN1_INTEGER to duplicate
- * Returns pointer to duplicate WOLFSSL_ASN1_INTEGER
- */
-WOLFSSL_ASN1_INTEGER* wolfSSL_ASN1_INTEGER_dup(const WOLFSSL_ASN1_INTEGER* src)
-{
- WOLFSSL_ASN1_INTEGER* dup;
- WOLFSSL_ENTER("wolfSSL_ASN1_INTEGER_dup");
- if (!src)
- return NULL;
-
- dup = wolfSSL_ASN1_INTEGER_new();
-
- if (dup == NULL)
- return NULL;
-
- dup->negative = src->negative;
- dup->dataMax = src->dataMax;
- dup->isDynamic = src->isDynamic;
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- dup->length = src->length;
-#endif
- XSTRNCPY((char*)dup->intData,(const char*)src->intData,WOLFSSL_ASN1_INTEGER_MAX);
-
- if (dup->isDynamic && src->data && dup->dataMax) {
- dup->data = (unsigned char*)
- XMALLOC(src->dataMax,NULL,DYNAMIC_TYPE_OPENSSL);
- if (dup->data == NULL) {
- wolfSSL_ASN1_INTEGER_free(dup);
- return NULL;
- }
- XMEMCPY(dup->data,src->data,dup->dataMax);
- }
- return dup;
-}
-
-
-/* sets the value of WOLFSSL_ASN1_INTEGER a to the long value v. */
-int wolfSSL_ASN1_INTEGER_set(WOLFSSL_ASN1_INTEGER *a, long v)
-{
- int ret = WOLFSSL_SUCCESS; /* return 1 for success and 0 for failure */
- int j;
- unsigned int i = 0;
- unsigned char tmp[sizeof(long)+1] = {0};
-
- if (a != NULL) {
- /* dynamically create data buffer, +2 for type and length */
- a->data = (unsigned char*)XMALLOC((sizeof(long)+1) + 2, NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (a->data == NULL) {
- wolfSSL_ASN1_INTEGER_free(a);
- ret = WOLFSSL_FAILURE;
- }
- else {
- a->dataMax = (int)(sizeof(long)+1) + 2;
- a->isDynamic = 1;
- }
- }
- else {
- /* Invalid parameter */
- ret = WOLFSSL_FAILURE;
- }
-
-
- if (ret != WOLFSSL_FAILURE) {
- /* Set type */
- a->data[i++] = ASN_INTEGER;
-
- /* Check for negative */
- if (v < 0) {
- a->negative = 1;
- v *= -1;
- }
-
- /* Create char buffer */
- for (j = 0; j < (int)sizeof(long); j++) {
- if (v == 0) {
- break;
- }
- tmp[j] = (unsigned char)(v & 0xff);
- v >>= 8;
- }
-
- /* Set length */
- a->data[i++] = (unsigned char)((j == 0) ? ++j : j);
- /* +2 for type and length */
- a->length = j + 2;
-
- /* Copy to data */
- for (; j > 0; j--) {
- a->data[i++] = tmp[j-1];
- }
- }
-
- return ret;
-}
-
-
-WOLFSSL_ASN1_INTEGER* wolfSSL_X509_get_serialNumber(WOLFSSL_X509* x509)
-{
- WOLFSSL_ASN1_INTEGER* a;
- int i = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_serialNumber");
-
- a = wolfSSL_ASN1_INTEGER_new();
- if (a == NULL)
- return NULL;
-
- /* Make sure there is space for the data, ASN.1 type and length. */
- if (x509->serialSz > (WOLFSSL_ASN1_INTEGER_MAX - 2)) {
- /* dynamically create data buffer, +2 for type and length */
- a->data = (unsigned char*)XMALLOC(x509->serialSz + 2, NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (a->data == NULL) {
- wolfSSL_ASN1_INTEGER_free(a);
- return NULL;
- }
- a->dataMax = x509->serialSz + 2;
- a->isDynamic = 1;
- } else {
- /* Use array instead of dynamic memory */
- a->data = a->intData;
- a->dataMax = WOLFSSL_ASN1_INTEGER_MAX;
- }
-
- #ifdef WOLFSSL_QT
- XMEMCPY(&a->data[i], x509->serial, x509->serialSz);
- a->length = x509->serialSz;
- #else
- a->data[i++] = ASN_INTEGER;
- i += SetLength(x509->serialSz, a->data + i);
- XMEMCPY(&a->data[i], x509->serial, x509->serialSz);
- a->length = x509->serialSz + 2;
- #endif
-
- x509->serialNumber = a;
-
- return a;
-}
-
-#endif /* OPENSSL_EXTRA */
-
-#if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
- defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
-#ifndef NO_ASN_TIME
-int wolfSSL_ASN1_TIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_TIME* asnTime)
-{
- char buf[MAX_TIME_STRING_SZ];
- int ret = WOLFSSL_SUCCESS;
-
- WOLFSSL_ENTER("wolfSSL_ASN1_TIME_print");
-
- if (bio == NULL || asnTime == NULL) {
- WOLFSSL_MSG("NULL function argument");
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_ASN1_TIME_to_string((WOLFSSL_ASN1_TIME*)asnTime, buf,
- sizeof(buf)) == NULL) {
- XMEMSET(buf, 0, MAX_TIME_STRING_SZ);
- XMEMCPY(buf, "Bad time value", 14);
- ret = WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_BIO_write(bio, buf, (int)XSTRLEN(buf)) <= 0) {
- WOLFSSL_MSG("Unable to write to bio");
- return WOLFSSL_FAILURE;
- }
-
- return ret;
-}
-
-char* wolfSSL_ASN1_TIME_to_string(WOLFSSL_ASN1_TIME* t, char* buf, int len)
-{
- WOLFSSL_ENTER("wolfSSL_ASN1_TIME_to_string");
-
- if (t == NULL || buf == NULL || len < 5) {
- WOLFSSL_MSG("Bad argument");
- return NULL;
- }
-
- if (t->length > len) {
- WOLFSSL_MSG("Length of date is longer then buffer");
- return NULL;
- }
-
- if (!GetTimeString(t->data, t->type, buf, len)) {
- return NULL;
- }
-
- return buf;
-}
-#endif /* !NO_ASN_TIME */
-#endif /* WOLFSSL_MYSQL_COMPATIBLE || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
- OPENSSL_EXTRA*/
-
-
-#ifdef OPENSSL_EXTRA
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_ASN1_INTEGER_cmp(const WOLFSSL_ASN1_INTEGER* a,
- const WOLFSSL_ASN1_INTEGER* b)
-{
- (void)a;
- (void)b;
- WOLFSSL_STUB("ASN1_INTEGER_cmp");
- return 0;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-long wolfSSL_ASN1_INTEGER_get(const WOLFSSL_ASN1_INTEGER* i)
-{
- (void)i;
- WOLFSSL_STUB("ASN1_INTEGER_get");
- return 0;
-}
-#endif
-
-/* get X509_STORE_CTX ex_data, max idx is MAX_EX_DATA */
-void* wolfSSL_X509_STORE_CTX_get_ex_data(WOLFSSL_X509_STORE_CTX* ctx, int idx)
-{
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_ex_data");
- #if defined(HAVE_EX_DATA) || defined(FORTRESS)
- if (ctx != NULL) {
- return wolfSSL_CRYPTO_get_ex_data(&ctx->ex_data, idx);
- }
- #else
- (void)ctx;
- (void)idx;
- #endif
- return NULL;
-}
-
-
-/* set X509_STORE_CTX ex_data, max idx is MAX_EX_DATA. Return WOLFSSL_SUCCESS
- * on success, WOLFSSL_FAILURE on error. */
-int wolfSSL_X509_STORE_CTX_set_ex_data(WOLFSSL_X509_STORE_CTX* ctx, int idx,
- void *data)
-{
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_ex_data");
- #if defined(HAVE_EX_DATA) || defined(FORTRESS)
- if (ctx != NULL)
- {
- return wolfSSL_CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
- }
- #else
- (void)ctx;
- (void)idx;
- (void)data;
- #endif
- return WOLFSSL_FAILURE;
-}
-
-#if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL)
-void wolfSSL_X509_STORE_CTX_set_depth(WOLFSSL_X509_STORE_CTX* ctx, int depth)
-{
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_depth");
- if (ctx)
- ctx->depth = depth;
-}
-#endif
-
-
-WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get0_current_issuer(
- WOLFSSL_X509_STORE_CTX* ctx)
-{
- int ret;
- WOLFSSL_X509* issuer;
-
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get0_current_issuer");
-
- if (ctx == NULL) {
- return NULL;
- }
-
- ret = wolfSSL_X509_STORE_CTX_get1_issuer(&issuer, ctx, ctx->current_cert);
- if (ret == WOLFSSL_SUCCESS) {
- return issuer;
- }
-
- return NULL;
-}
-
-
-/* Gets an index to store SSL structure at.
- *
- * Returns positive index on success and negative values on failure
- */
-int wolfSSL_get_ex_data_X509_STORE_CTX_idx(void)
-{
- WOLFSSL_ENTER("wolfSSL_get_ex_data_X509_STORE_CTX_idx");
-
- /* store SSL at index 0 */
- return 0;
-}
-
-
-/* Set an error stat in the X509 STORE CTX
- *
- */
-void wolfSSL_X509_STORE_CTX_set_error(WOLFSSL_X509_STORE_CTX* ctx, int er)
-{
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_error");
-
- if (ctx != NULL) {
- ctx->error = er;
- }
-}
-
-/* Set the error depth in the X509 STORE CTX */
-void wolfSSL_X509_STORE_CTX_set_error_depth(WOLFSSL_X509_STORE_CTX* ctx,
- int depth)
-{
- WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_set_error_depth");
-
- if (ctx != NULL) {
- ctx->error_depth = depth;
- }
-}
-
-/* Sets a function callback that will send information about the state of all
- * WOLFSSL objects that have been created by the WOLFSSL_CTX structure passed
- * in.
- *
- * ctx WOLFSSL_CTX structure to set callback function in
- * f callback function to use
- */
-void wolfSSL_CTX_set_info_callback(WOLFSSL_CTX* ctx,
- void (*f)(const WOLFSSL* ssl, int type, int val))
-{
- WOLFSSL_ENTER("wolfSSL_CTX_set_info_callback");
- if (ctx == NULL) {
- WOLFSSL_MSG("Bad function argument");
- }
- else {
- ctx->CBIS = f;
- }
-}
-
-
-unsigned long wolfSSL_ERR_peek_error(void)
-{
- WOLFSSL_ENTER("wolfSSL_ERR_peek_error");
-
- return wolfSSL_ERR_peek_error_line_data(NULL, NULL, NULL, NULL);
-}
-
-int wolfSSL_ERR_GET_LIB(unsigned long err)
-{
- switch (err) {
- case PEM_R_NO_START_LINE:
- case PEM_R_PROBLEMS_GETTING_PASSWORD:
- case PEM_R_BAD_PASSWORD_READ:
- case PEM_R_BAD_DECRYPT:
- return ERR_LIB_PEM;
- case EVP_R_BAD_DECRYPT:
- case EVP_R_BN_DECODE_ERROR:
- case EVP_R_DECODE_ERROR:
- case EVP_R_PRIVATE_KEY_DECODE_ERROR:
- return ERR_LIB_EVP;
- default:
- return 0;
- }
-}
-
-/* This function is to find global error values that are the same through out
- * all library version. With wolfSSL having only one set of error codes the
- * return value is pretty straight forward. The only thing needed is all wolfSSL
- * error values are typically negative.
- *
- * Returns the error reason
- */
-int wolfSSL_ERR_GET_REASON(unsigned long err)
-{
- int ret = (int)err;
-
- WOLFSSL_ENTER("wolfSSL_ERR_GET_REASON");
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
- /* Nginx looks for this error to know to stop parsing certificates. */
- if (err == ((ERR_LIB_PEM << 24) | PEM_R_NO_START_LINE))
- return PEM_R_NO_START_LINE;
-#endif
-
- /* check if error value is in range of wolfSSL errors */
- ret = 0 - ret; /* setting as negative value */
- /* wolfCrypt range is less than MAX (-100)
- wolfSSL range is MIN (-300) and lower */
- if (ret < MAX_CODE_E && ret > MIN_CODE_E) {
- return ret;
- }
- else {
- WOLFSSL_MSG("Not in range of typical error values");
- ret = (int)err;
- }
-
- return ret;
-}
-
-
-/* returns a string that describes the alert
- *
- * alertID the alert value to look up
- */
-const char* wolfSSL_alert_type_string_long(int alertID)
-{
- WOLFSSL_ENTER("wolfSSL_alert_type_string_long");
-
- switch (alertID) {
- case close_notify:
- {
- static const char close_notify_str[] =
- "close_notify";
- return close_notify_str;
- }
-
- case unexpected_message:
- {
- static const char unexpected_message_str[] =
- "unexpected_message";
- return unexpected_message_str;
- }
-
- case bad_record_mac:
- {
- static const char bad_record_mac_str[] =
- "bad_record_mac";
- return bad_record_mac_str;
- }
-
- case record_overflow:
- {
- static const char record_overflow_str[] =
- "record_overflow";
- return record_overflow_str;
- }
-
- case decompression_failure:
- {
- static const char decompression_failure_str[] =
- "decompression_failure";
- return decompression_failure_str;
- }
-
- case handshake_failure:
- {
- static const char handshake_failure_str[] =
- "handshake_failure";
- return handshake_failure_str;
- }
-
- case no_certificate:
- {
- static const char no_certificate_str[] =
- "no_certificate";
- return no_certificate_str;
- }
-
- case bad_certificate:
- {
- static const char bad_certificate_str[] =
- "bad_certificate";
- return bad_certificate_str;
- }
-
- case unsupported_certificate:
- {
- static const char unsupported_certificate_str[] =
- "unsupported_certificate";
- return unsupported_certificate_str;
- }
-
- case certificate_revoked:
- {
- static const char certificate_revoked_str[] =
- "certificate_revoked";
- return certificate_revoked_str;
- }
-
- case certificate_expired:
- {
- static const char certificate_expired_str[] =
- "certificate_expired";
- return certificate_expired_str;
- }
-
- case certificate_unknown:
- {
- static const char certificate_unknown_str[] =
- "certificate_unknown";
- return certificate_unknown_str;
- }
-
- case illegal_parameter:
- {
- static const char illegal_parameter_str[] =
- "illegal_parameter";
- return illegal_parameter_str;
- }
-
- case unknown_ca:
- {
- static const char unknown_ca_str[] =
- "unknown_ca";
- return unknown_ca_str;
- }
-
- case decode_error:
- {
- static const char decode_error_str[] =
- "decode_error";
- return decode_error_str;
- }
-
- case decrypt_error:
- {
- static const char decrypt_error_str[] =
- "decrypt_error";
- return decrypt_error_str;
- }
-
- #ifdef WOLFSSL_MYSQL_COMPATIBLE
- /* catch name conflict for enum protocol with MYSQL build */
- case wc_protocol_version:
- {
- static const char wc_protocol_version_str[] =
- "wc_protocol_version";
- return wc_protocol_version_str;
- }
-
- #else
- case protocol_version:
- {
- static const char protocol_version_str[] =
- "protocol_version";
- return protocol_version_str;
- }
-
- #endif
- case no_renegotiation:
- {
- static const char no_renegotiation_str[] =
- "no_renegotiation";
- return no_renegotiation_str;
- }
-
- case unrecognized_name:
- {
- static const char unrecognized_name_str[] =
- "unrecognized_name";
- return unrecognized_name_str;
- }
-
- case bad_certificate_status_response:
- {
- static const char bad_certificate_status_response_str[] =
- "bad_certificate_status_response";
- return bad_certificate_status_response_str;
- }
-
- case no_application_protocol:
- {
- static const char no_application_protocol_str[] =
- "no_application_protocol";
- return no_application_protocol_str;
- }
-
- default:
- WOLFSSL_MSG("Unknown Alert");
- return NULL;
- }
-}
-
-
-const char* wolfSSL_alert_desc_string_long(int alertID)
-{
- WOLFSSL_ENTER("wolfSSL_alert_desc_string_long");
- return wolfSSL_alert_type_string_long(alertID);
-}
-
-
-/* Gets the current state of the WOLFSSL structure
- *
- * ssl WOLFSSL structure to get state of
- *
- * Returns a human readable string of the WOLFSSL structure state
- */
-const char* wolfSSL_state_string_long(const WOLFSSL* ssl)
-{
-
- static const char* OUTPUT_STR[14][6][3] = {
- {
- {"SSLv3 Initialization","SSLv3 Initialization","SSLv3 Initialization"},
- {"TLSv1 Initialization","TLSv2 Initialization","TLSv2 Initialization"},
- {"TLSv1_1 Initialization","TLSv1_1 Initialization","TLSv1_1 Initialization"},
- {"TLSv1_2 Initialization","TLSv1_2 Initialization","TLSv1_2 Initialization"},
- {"DTLSv1 Initialization","DTLSv1 Initialization","DTLSv1 Initialization"},
- {"DTLSv1_2 Initialization","DTLSv1_2 Initialization","DTLSv1_2 Initialization"},
- },
- {
- {"SSLv3 read Server Hello Verify Request",
- "SSLv3 write Server Hello Verify Request",
- "SSLv3 Server Hello Verify Request"},
- {"TLSv1 read Server Hello Verify Request",
- "TLSv1 write Server Hello Verify Request",
- "TLSv1 Server Hello Verify Request"},
- {"TLSv1_1 read Server Hello Verify Request",
- "TLSv1_1 write Server Hello Verify Request",
- "TLSv1_1 Server Hello Verify Request"},
- {"TLSv1_2 read Server Hello Verify Request",
- "TLSv1_2 write Server Hello Verify Request",
- "TLSv1_2 Server Hello Verify Request"},
- {"DTLSv1 read Server Hello Verify Request",
- "DTLSv1 write Server Hello Verify Request",
- "DTLSv1 Server Hello Verify Request"},
- {"DTLSv1_2 read Server Hello Verify Request",
- "DTLSv1_2 write Server Hello Verify Request",
- "DTLSv1_2 Server Hello Verify Request"},
- },
- {
- {"SSLv3 read Server Hello",
- "SSLv3 write Server Hello",
- "SSLv3 Server Hello"},
- {"TLSv1 read Server Hello",
- "TLSv1 write Server Hello",
- "TLSv1 Server Hello"},
- {"TLSv1_1 read Server Hello",
- "TLSv1_1 write Server Hello",
- "TLSv1_1 Server Hello"},
- {"TLSv1_2 read Server Hello",
- "TLSv1_2 write Server Hello",
- "TLSv1_2 Server Hello"},
- {"DTLSv1 read Server Hello",
- "DTLSv1 write Server Hello",
- "DTLSv1 Server Hello"},
- {"DTLSv1_2 read Server Hello"
- "DTLSv1_2 write Server Hello",
- "DTLSv1_2 Server Hello",
- },
- },
- {
- {"SSLv3 read Server Session Ticket",
- "SSLv3 write Server Session Ticket",
- "SSLv3 Server Session Ticket"},
- {"TLSv1 read Server Session Ticket",
- "TLSv1 write Server Session Ticket",
- "TLSv1 Server Session Ticket"},
- {"TLSv1_1 read Server Session Ticket",
- "TLSv1_1 write Server Session Ticket",
- "TLSv1_1 Server Session Ticket"},
- {"TLSv1_2 read Server Session Ticket",
- "TLSv1_2 write Server Session Ticket",
- "TLSv1_2 Server Session Ticket"},
- {"DTLSv1 read Server Session Ticket",
- "DTLSv1 write Server Session Ticket",
- "DTLSv1 Server Session Ticket"},
- {"DTLSv1_2 read Server Session Ticket",
- "DTLSv1_2 write Server Session Ticket",
- "DTLSv1_2 Server Session Ticket"},
- },
- {
- {"SSLv3 read Server Cert",
- "SSLv3 write Server Cert",
- "SSLv3 Server Cert"},
- {"TLSv1 read Server Cert",
- "TLSv1 write Server Cert",
- "TLSv1 Server Cert"},
- {"TLSv1_1 read Server Cert",
- "TLSv1_1 write Server Cert",
- "TLSv1_1 Server Cert"},
- {"TLSv1_2 read Server Cert",
- "TLSv1_2 write Server Cert",
- "TLSv1_2 Server Cert"},
- {"DTLSv1 read Server Cert",
- "DTLSv1 write Server Cert",
- "DTLSv1 Server Cert"},
- {"DTLSv1_2 read Server Cert",
- "DTLSv1_2 write Server Cert",
- "DTLSv1_2 Server Cert"},
- },
- {
- {"SSLv3 read Server Key Exchange",
- "SSLv3 write Server Key Exchange",
- "SSLv3 Server Key Exchange"},
- {"TLSv1 read Server Key Exchange",
- "TLSv1 write Server Key Exchange",
- "TLSv1 Server Key Exchange"},
- {"TLSv1_1 read Server Key Exchange",
- "TLSv1_1 write Server Key Exchange",
- "TLSv1_1 Server Key Exchange"},
- {"TLSv1_2 read Server Key Exchange",
- "TLSv1_2 write Server Key Exchange",
- "TLSv1_2 Server Key Exchange"},
- {"DTLSv1 read Server Key Exchange",
- "DTLSv1 write Server Key Exchange",
- "DTLSv1 Server Key Exchange"},
- {"DTLSv1_2 read Server Key Exchange",
- "DTLSv1_2 write Server Key Exchange",
- "DTLSv1_2 Server Key Exchange"},
- },
- {
- {"SSLv3 read Server Hello Done",
- "SSLv3 write Server Hello Done",
- "SSLv3 Server Hello Done"},
- {"TLSv1 read Server Hello Done",
- "TLSv1 write Server Hello Done",
- "TLSv1 Server Hello Done"},
- {"TLSv1_1 read Server Hello Done",
- "TLSv1_1 write Server Hello Done",
- "TLSv1_1 Server Hello Done"},
- {"TLSv1_2 read Server Hello Done",
- "TLSv1_2 write Server Hello Done",
- "TLSv1_2 Server Hello Done"},
- {"DTLSv1 read Server Hello Done",
- "DTLSv1 write Server Hello Done",
- "DTLSv1 Server Hello Done"},
- {"DTLSv1_2 read Server Hello Done",
- "DTLSv1_2 write Server Hello Done",
- "DTLSv1_2 Server Hello Done"},
- },
- {
- {"SSLv3 read Server Change CipherSpec",
- "SSLv3 write Server Change CipherSpec",
- "SSLv3 Server Change CipherSpec"},
- {"TLSv1 read Server Change CipherSpec",
- "TLSv1 write Server Change CipherSpec",
- "TLSv1 Server Change CipherSpec"},
- {"TLSv1_1 read Server Change CipherSpec",
- "TLSv1_1 write Server Change CipherSpec",
- "TLSv1_1 Server Change CipherSpec"},
- {"TLSv1_2 read Server Change CipherSpec",
- "TLSv1_2 write Server Change CipherSpec",
- "TLSv1_2 Server Change CipherSpec"},
- {"DTLSv1 read Server Change CipherSpec",
- "DTLSv1 write Server Change CipherSpec",
- "DTLSv1 Server Change CipherSpec"},
- {"DTLSv1_2 read Server Change CipherSpec",
- "DTLSv1_2 write Server Change CipherSpec",
- "DTLSv1_2 Server Change CipherSpec"},
- },
- {
- {"SSLv3 read Server Finished",
- "SSLv3 write Server Finished",
- "SSLv3 Server Finished"},
- {"TLSv1 read Server Finished",
- "TLSv1 write Server Finished",
- "TLSv1 Server Finished"},
- {"TLSv1_1 read Server Finished",
- "TLSv1_1 write Server Finished",
- "TLSv1_1 Server Finished"},
- {"TLSv1_2 read Server Finished",
- "TLSv1_2 write Server Finished",
- "TLSv1_2 Server Finished"},
- {"DTLSv1 read Server Finished",
- "DTLSv1 write Server Finished",
- "DTLSv1 Server Finished"},
- {"DTLSv1_2 read Server Finished",
- "DTLSv1_2 write Server Finished",
- "DTLSv1_2 Server Finished"},
- },
- {
- {"SSLv3 read Client Hello",
- "SSLv3 write Client Hello",
- "SSLv3 Client Hello"},
- {"TLSv1 read Client Hello",
- "TLSv1 write Client Hello",
- "TLSv1 Client Hello"},
- {"TLSv1_1 read Client Hello",
- "TLSv1_1 write Client Hello",
- "TLSv1_1 Client Hello"},
- {"TLSv1_2 read Client Hello",
- "TLSv1_2 write Client Hello",
- "TLSv1_2 Client Hello"},
- {"DTLSv1 read Client Hello",
- "DTLSv1 write Client Hello",
- "DTLSv1 Client Hello"},
- {"DTLSv1_2 read Client Hello",
- "DTLSv1_2 write Client Hello",
- "DTLSv1_2 Client Hello"},
- },
- {
- {"SSLv3 read Client Key Exchange",
- "SSLv3 write Client Key Exchange",
- "SSLv3 Client Key Exchange"},
- {"TLSv1 read Client Key Exchange",
- "TLSv1 write Client Key Exchange",
- "TLSv1 Client Key Exchange"},
- {"TLSv1_1 read Client Key Exchange",
- "TLSv1_1 write Client Key Exchange",
- "TLSv1_1 Client Key Exchange"},
- {"TLSv1_2 read Client Key Exchange",
- "TLSv1_2 write Client Key Exchange",
- "TLSv1_2 Client Key Exchange"},
- {"DTLSv1 read Client Key Exchange",
- "DTLSv1 write Client Key Exchange",
- "DTLSv1 Client Key Exchange"},
- {"DTLSv1_2 read Client Key Exchange",
- "DTLSv1_2 write Client Key Exchange",
- "DTLSv1_2 Client Key Exchange"},
- },
- {
- {"SSLv3 read Client Change CipherSpec",
- "SSLv3 write Client Change CipherSpec",
- "SSLv3 Client Change CipherSpec"},
- {"TLSv1 read Client Change CipherSpec",
- "TLSv1 write Client Change CipherSpec",
- "TLSv1 Client Change CipherSpec"},
- {"TLSv1_1 read Client Change CipherSpec",
- "TLSv1_1 write Client Change CipherSpec",
- "TLSv1_1 Client Change CipherSpec"},
- {"TLSv1_2 read Client Change CipherSpec",
- "TLSv1_2 write Client Change CipherSpec",
- "TLSv1_2 Client Change CipherSpec"},
- {"DTLSv1 read Client Change CipherSpec",
- "DTLSv1 write Client Change CipherSpec",
- "DTLSv1 Client Change CipherSpec"},
- {"DTLSv1_2 read Client Change CipherSpec",
- "DTLSv1_2 write Client Change CipherSpec",
- "DTLSv1_2 Client Change CipherSpec"},
- },
- {
- {"SSLv3 read Client Finished",
- "SSLv3 write Client Finished",
- "SSLv3 Client Finished"},
- {"TLSv1 read Client Finished",
- "TLSv1 write Client Finished",
- "TLSv1 Client Finished"},
- {"TLSv1_1 read Client Finished",
- "TLSv1_1 write Client Finished",
- "TLSv1_1 Client Finished"},
- {"TLSv1_2 read Client Finished",
- "TLSv1_2 write Client Finished",
- "TLSv1_2 Client Finished"},
- {"DTLSv1 read Client Finished",
- "DTLSv1 write Client Finished",
- "DTLSv1 Client Finished"},
- {"DTLSv1_2 read Client Finished",
- "DTLSv1_2 write Client Finished",
- "DTLSv1_2 Client Finished"},
- },
- {
- {"SSLv3 Handshake Done",
- "SSLv3 Handshake Done",
- "SSLv3 Handshake Done"},
- {"TLSv1 Handshake Done",
- "TLSv1 Handshake Done",
- "TLSv1 Handshake Done"},
- {"TLSv1_1 Handshake Done",
- "TLSv1_1 Handshake Done",
- "TLSv1_1 Handshake Done"},
- {"TLSv1_2 Handshake Done",
- "TLSv1_2 Handshake Done",
- "TLSv1_2 Handshake Done"},
- {"DTLSv1 Handshake Done",
- "DTLSv1 Handshake Done",
- "DTLSv1 Handshake Done"},
- {"DTLSv1_2 Handshake Done"
- "DTLSv1_2 Handshake Done"
- "DTLSv1_2 Handshake Done"}
- }
- };
- enum ProtocolVer {
- SSL_V3 = 0,
- TLS_V1,
- TLS_V1_1,
- TLS_V1_2,
- DTLS_V1,
- DTLS_V1_2,
- UNKNOWN = 100
- };
-
- enum IOMode {
- SS_READ = 0,
- SS_WRITE,
- SS_NEITHER
- };
-
- enum SslState {
- ss_null_state = 0,
- ss_server_helloverify,
- ss_server_hello,
- ss_sessionticket,
- ss_server_cert,
- ss_server_keyexchange,
- ss_server_hellodone,
- ss_server_changecipherspec,
- ss_server_finished,
- ss_client_hello,
- ss_client_keyexchange,
- ss_client_changecipherspec,
- ss_client_finished,
- ss_handshake_done
- };
-
- int protocol = 0;
- int cbmode = 0;
- int state = 0;
-
- WOLFSSL_ENTER("wolfSSL_state_string_long");
- if (ssl == NULL) {
- WOLFSSL_MSG("Null argument passed in");
- return NULL;
- }
-
- /* Get state of callback */
- if (ssl->cbmode == SSL_CB_MODE_WRITE){
- cbmode = SS_WRITE;
- } else if (ssl->cbmode == SSL_CB_MODE_READ){
- cbmode = SS_READ;
- } else {
- cbmode = SS_NEITHER;
- }
-
- /* Get protocol version */
- switch (ssl->version.major){
- case SSLv3_MAJOR:
- switch (ssl->version.minor){
- case TLSv1_MINOR:
- protocol = TLS_V1;
- break;
- case TLSv1_1_MINOR:
- protocol = TLS_V1_1;
- break;
- case TLSv1_2_MINOR:
- protocol = TLS_V1_2;
- break;
- case SSLv3_MINOR:
- protocol = SSL_V3;
- break;
- default:
- protocol = UNKNOWN;
- }
- break;
- case DTLS_MAJOR:
- switch (ssl->version.minor){
- case DTLS_MINOR:
- protocol = DTLS_V1;
- break;
- case DTLSv1_2_MINOR:
- protocol = DTLS_V1_2;
- break;
- default:
- protocol = UNKNOWN;
- }
- break;
- default:
- protocol = UNKNOWN;
- }
-
- /* accept process */
- if (ssl->cbmode == SSL_CB_MODE_READ){
- state = ssl->cbtype;
- switch (state) {
- case hello_verify_request:
- state = ss_server_helloverify;
- break;
- case session_ticket:
- state = ss_sessionticket;
- break;
- case server_hello:
- state = ss_server_hello;
- break;
- case server_hello_done:
- state = ss_server_hellodone;
- break;
- case certificate:
- state = ss_server_cert;
- break;
- case server_key_exchange:
- state = ss_server_keyexchange;
- break;
- case client_hello:
- state = ss_client_hello;
- break;
- case client_key_exchange:
- state = ss_client_keyexchange;
- break;
- case finished:
- if (ssl->options.side == WOLFSSL_SERVER_END)
- state = ss_client_finished;
- else if (ssl->options.side == WOLFSSL_CLIENT_END)
- state = ss_server_finished;
- else {
- WOLFSSL_MSG("Unknown State");
- state = ss_null_state;
- }
- break;
- default:
- WOLFSSL_MSG("Unknown State");
- state = ss_null_state;
- }
- } else {
- /* Send process */
- if (ssl->options.side == WOLFSSL_SERVER_END)
- state = ssl->options.serverState;
- else
- state = ssl->options.clientState;
-
- switch(state){
- case SERVER_HELLOVERIFYREQUEST_COMPLETE:
- state = ss_server_helloverify;
- break;
- case SERVER_HELLO_COMPLETE:
- state = ss_server_hello;
- break;
- case SERVER_CERT_COMPLETE:
- state = ss_server_cert;
- break;
- case SERVER_KEYEXCHANGE_COMPLETE:
- state = ss_server_keyexchange;
- break;
- case SERVER_HELLODONE_COMPLETE:
- state = ss_server_hellodone;
- break;
- case SERVER_CHANGECIPHERSPEC_COMPLETE:
- state = ss_server_changecipherspec;
- break;
- case SERVER_FINISHED_COMPLETE:
- state = ss_server_finished;
- break;
- case CLIENT_HELLO_COMPLETE:
- state = ss_client_hello;
- break;
- case CLIENT_KEYEXCHANGE_COMPLETE:
- state = ss_client_keyexchange;
- break;
- case CLIENT_CHANGECIPHERSPEC_COMPLETE:
- state = ss_client_changecipherspec;
- break;
- case CLIENT_FINISHED_COMPLETE:
- state = ss_client_finished;
- break;
- case HANDSHAKE_DONE:
- state = ss_handshake_done;
- break;
- default:
- WOLFSSL_MSG("Unknown State");
- state = ss_null_state;
- }
- }
-
- if (protocol == UNKNOWN)
- return NULL;
- else
- return OUTPUT_STR[state][protocol][cbmode];
-}
-
-/*
- * Sets default PEM callback password if null is passed into
- * the callback parameter of a PEM_read_bio_* function.
- *
- * Returns callback phrase size on success or WOLFSSL_FAILURE otherwise.
- */
-int wolfSSL_PEM_def_callback(char* name, int num, int w, void* key)
-{
- int sz;
- (void)w;
- WOLFSSL_ENTER("wolfSSL_PEM_def_callback");
-
- /* We assume that the user passes a default password as userdata */
- if (key) {
- sz = (int)XSTRLEN((const char*)key);
- sz = (sz > num) ? num : sz;
- XMEMCPY(name, key, sz);
- return sz;
- } else {
- WOLFSSL_MSG("Error, default password cannot be created.");
- return WOLFSSL_FAILURE;
- }
-}
-
-#endif /* OPENSSL_EXTRA */
-
-#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
-static long wolf_set_options(long old_op, long op)
-{
- /* if SSL_OP_ALL then turn all bug workarounds on */
- if ((op & SSL_OP_ALL) == SSL_OP_ALL) {
- WOLFSSL_MSG("\tSSL_OP_ALL");
- }
-
- /* by default cookie exchange is on with DTLS */
- if ((op & SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE) {
- WOLFSSL_MSG("\tSSL_OP_COOKIE_EXCHANGE : on by default");
- }
-
- if ((op & WOLFSSL_OP_NO_SSLv2) == WOLFSSL_OP_NO_SSLv2) {
- WOLFSSL_MSG("\tWOLFSSL_OP_NO_SSLv2 : wolfSSL does not support SSLv2");
- }
-
-#ifdef SSL_OP_NO_TLSv1_3
- if ((op & SSL_OP_NO_TLSv1_3) == SSL_OP_NO_TLSv1_3) {
- WOLFSSL_MSG("\tSSL_OP_NO_TLSv1_3");
- }
-#endif
-
- if ((op & SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2) {
- WOLFSSL_MSG("\tSSL_OP_NO_TLSv1_2");
- }
-
- if ((op & SSL_OP_NO_TLSv1_1) == SSL_OP_NO_TLSv1_1) {
- WOLFSSL_MSG("\tSSL_OP_NO_TLSv1_1");
- }
-
- if ((op & SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1) {
- WOLFSSL_MSG("\tSSL_OP_NO_TLSv1");
- }
-
- if ((op & SSL_OP_NO_SSLv3) == SSL_OP_NO_SSLv3) {
- WOLFSSL_MSG("\tSSL_OP_NO_SSLv3");
- }
-
- if ((op & SSL_OP_CIPHER_SERVER_PREFERENCE) == SSL_OP_CIPHER_SERVER_PREFERENCE) {
- WOLFSSL_MSG("\tSSL_OP_CIPHER_SERVER_PREFERENCE");
- }
-
- if ((op & SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION) {
- #ifdef HAVE_LIBZ
- WOLFSSL_MSG("SSL_OP_NO_COMPRESSION");
- #else
- WOLFSSL_MSG("SSL_OP_NO_COMPRESSION: compression not compiled in");
- #endif
- }
-
- return old_op | op;
-}
-#endif
-
-#ifdef OPENSSL_EXTRA
-long wolfSSL_set_options(WOLFSSL* ssl, long op)
-{
- word16 haveRSA = 1;
- word16 havePSK = 0;
- int keySz = 0;
-
- WOLFSSL_ENTER("wolfSSL_set_options");
-
- if (ssl == NULL) {
- return 0;
- }
-
- ssl->options.mask = wolf_set_options(ssl->options.mask, op);
-
-#ifdef SSL_OP_NO_TLSv1_3
- if ((ssl->options.mask & SSL_OP_NO_TLSv1_3) == SSL_OP_NO_TLSv1_3) {
- if (ssl->version.minor == TLSv1_3_MINOR)
- ssl->version.minor = TLSv1_2_MINOR;
- }
-#endif
-
- if ((ssl->options.mask & SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2) {
- if (ssl->version.minor == TLSv1_2_MINOR)
- ssl->version.minor = TLSv1_1_MINOR;
- }
-
- if ((ssl->options.mask & SSL_OP_NO_TLSv1_1) == SSL_OP_NO_TLSv1_1) {
- if (ssl->version.minor == TLSv1_1_MINOR)
- ssl->version.minor = TLSv1_MINOR;
- }
-
- if ((ssl->options.mask & SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1) {
- if (ssl->version.minor == TLSv1_MINOR)
- ssl->version.minor = SSLv3_MINOR;
- }
-
- if ((ssl->options.mask & SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION) {
- #ifdef HAVE_LIBZ
- ssl->options.usingCompression = 0;
- #endif
- }
-
- /* in the case of a version change the cipher suites should be reset */
-#ifndef NO_PSK
- havePSK = ssl->options.havePSK;
-#endif
-#ifdef NO_RSA
- haveRSA = 0;
-#endif
-#ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
-#endif
-
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, havePSK,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
-
- return ssl->options.mask;
-}
-
-
-long wolfSSL_get_options(const WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_get_options");
- if(ssl == NULL)
- return WOLFSSL_FAILURE;
- return ssl->options.mask;
-}
-
-long wolfSSL_clear_options(WOLFSSL* ssl, long opt)
-{
- WOLFSSL_ENTER("SSL_clear_options");
- if(ssl == NULL)
- return WOLFSSL_FAILURE;
- ssl->options.mask &= ~opt;
- return ssl->options.mask;
-}
-
-
-#if defined(HAVE_SECURE_RENEGOTIATION) \
- || defined(HAVE_SERVER_RENEGOTIATION_INFO)
-/* clears the counter for number of renegotiations done
- * returns the current count before it is cleared */
-long wolfSSL_clear_num_renegotiations(WOLFSSL *s)
-{
- long total;
-
- WOLFSSL_ENTER("wolfSSL_clear_num_renegotiations");
- if (s == NULL)
- return 0;
-
- total = s->secure_rene_count;
- s->secure_rene_count = 0;
- return total;
-}
-
-
-/* return the number of renegotiations since wolfSSL_new */
-long wolfSSL_total_renegotiations(WOLFSSL *s)
-{
- WOLFSSL_ENTER("wolfSSL_total_renegotiations");
- return wolfSSL_num_renegotiations(s);
-}
-
-
-/* return the number of renegotiations since wolfSSL_new */
-long wolfSSL_num_renegotiations(WOLFSSL* s)
-{
- if (s == NULL) {
- return 0;
- }
-
- return s->secure_rene_count;
-}
-#endif /* HAVE_SECURE_RENEGOTIATION || HAVE_SERVER_RENEGOTIATION_INFO */
-
-#ifndef NO_DH
-long wolfSSL_set_tmp_dh(WOLFSSL *ssl, WOLFSSL_DH *dh)
-{
- int pSz, gSz;
- byte *p, *g;
- int ret = 0;
-
- WOLFSSL_ENTER("wolfSSL_set_tmp_dh");
-
- if (!ssl || !dh)
- return BAD_FUNC_ARG;
-
- /* Get needed size for p and g */
- pSz = wolfSSL_BN_bn2bin(dh->p, NULL);
- gSz = wolfSSL_BN_bn2bin(dh->g, NULL);
-
- if (pSz <= 0 || gSz <= 0)
- return WOLFSSL_FATAL_ERROR;
-
- p = (byte*)XMALLOC(pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (!p)
- return MEMORY_E;
-
- g = (byte*)XMALLOC(gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (!g) {
- XFREE(p, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- return MEMORY_E;
- }
-
- pSz = wolfSSL_BN_bn2bin(dh->p, p);
- gSz = wolfSSL_BN_bn2bin(dh->g, g);
-
- if (pSz >= 0 && gSz >= 0) /* Conversion successful */
- ret = wolfSSL_SetTmpDH(ssl, p, pSz, g, gSz);
-
- XFREE(p, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(g, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
-
- return pSz > 0 && gSz > 0 ? ret : WOLFSSL_FATAL_ERROR;
-}
-#endif /* !NO_DH */
-
-
-#ifdef HAVE_PK_CALLBACKS
-long wolfSSL_set_tlsext_debug_arg(WOLFSSL* ssl, void *arg)
-{
- if (ssl == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- ssl->loggingCtx = arg;
- return WOLFSSL_SUCCESS;
-}
-#endif /* HAVE_PK_CALLBACKS */
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)
-const unsigned char *SSL_SESSION_get0_id_context(const SSL_SESSION *sess, unsigned int *sid_ctx_length)
-{
- const byte *c = wolfSSL_SESSION_get_id((SSL_SESSION *)sess, sid_ctx_length);
- return c;
-}
-#endif
-
-/*** TBD ***/
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_API int wolfSSL_sk_SSL_COMP_zero(WOLFSSL_STACK* st)
-{
- (void)st;
- WOLFSSL_STUB("wolfSSL_sk_SSL_COMP_zero");
- /* wolfSSL_set_options(ssl, SSL_OP_NO_COMPRESSION); */
- return WOLFSSL_FAILURE;
-}
-#endif
-
-#ifdef HAVE_CERTIFICATE_STATUS_REQUEST
-long wolfSSL_set_tlsext_status_type(WOLFSSL *s, int type)
-{
- WOLFSSL_ENTER("wolfSSL_set_tlsext_status_type");
-
- if (s == NULL){
- return BAD_FUNC_ARG;
- }
-
- if (type == TLSEXT_STATUSTYPE_ocsp){
- int r = 0;
- r = TLSX_UseCertificateStatusRequest(&s->extensions, type, 0, s,
- s->heap, s->devId);
- return (long)r;
- } else {
- WOLFSSL_MSG(
- "SSL_set_tlsext_status_type only supports TLSEXT_STATUSTYPE_ocsp type.");
- return SSL_FAILURE;
- }
-
-}
-#endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
-
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_API long wolfSSL_get_tlsext_status_exts(WOLFSSL *s, void *arg)
-{
- (void)s;
- (void)arg;
- WOLFSSL_STUB("wolfSSL_get_tlsext_status_exts");
- return WOLFSSL_FAILURE;
-}
-#endif
-
-/*** TBD ***/
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_API long wolfSSL_set_tlsext_status_exts(WOLFSSL *s, void *arg)
-{
- (void)s;
- (void)arg;
- WOLFSSL_STUB("wolfSSL_set_tlsext_status_exts");
- return WOLFSSL_FAILURE;
-}
-#endif
-
-/*** TBD ***/
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_API long wolfSSL_get_tlsext_status_ids(WOLFSSL *s, void *arg)
-{
- (void)s;
- (void)arg;
- WOLFSSL_STUB("wolfSSL_get_tlsext_status_ids");
- return WOLFSSL_FAILURE;
-}
-#endif
-
-/*** TBD ***/
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_API long wolfSSL_set_tlsext_status_ids(WOLFSSL *s, void *arg)
-{
- (void)s;
- (void)arg;
- WOLFSSL_STUB("wolfSSL_set_tlsext_status_ids");
- return WOLFSSL_FAILURE;
-}
-#endif
-
-/*** TBD ***/
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_API int SSL_SESSION_set1_id(WOLFSSL_SESSION *s, const unsigned char *sid, unsigned int sid_len)
-{
- (void)s;
- (void)sid;
- (void)sid_len;
- WOLFSSL_STUB("SSL_SESSION_set1_id");
- return WOLFSSL_FAILURE;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-/*** TBD ***/
-WOLFSSL_API int SSL_SESSION_set1_id_context(WOLFSSL_SESSION *s, const unsigned char *sid_ctx, unsigned int sid_ctx_len)
-{
- (void)s;
- (void)sid_ctx;
- (void)sid_ctx_len;
- WOLFSSL_STUB("SSL_SESSION_set1_id_context");
- return WOLFSSL_FAILURE;
-}
-#endif
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) \
- || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
-WOLFSSL_X509_ALGOR* wolfSSL_X509_ALGOR_new(void)
-{
- WOLFSSL_X509_ALGOR* ret;
- ret = (WOLFSSL_X509_ALGOR*)XMALLOC(sizeof(WOLFSSL_X509_ALGOR), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (ret) {
- XMEMSET(ret, 0, sizeof(WOLFSSL_X509_ALGOR));
- }
- return ret;
-}
-
-void wolfSSL_X509_ALGOR_free(WOLFSSL_X509_ALGOR *alg)
-{
- if (alg) {
- wolfSSL_ASN1_OBJECT_free(alg->algorithm);
- wolfSSL_ASN1_TYPE_free(alg->parameter);
- XFREE(alg, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-/* Returns X509_ALGOR struct with signature algorithm */
-const WOLFSSL_X509_ALGOR* wolfSSL_X509_get0_tbs_sigalg(const WOLFSSL_X509 *x509)
-{
- WOLFSSL_ENTER("X509_get0_tbs_sigalg");
-
- if (x509 == NULL) {
- WOLFSSL_MSG("x509 struct NULL error");
- return NULL;
- }
-
- return &x509->algor;
-}
-
-/* Sets paobj pointer to X509_ALGOR signature algorithm */
-void wolfSSL_X509_ALGOR_get0(const WOLFSSL_ASN1_OBJECT **paobj, int *pptype,
- const void **ppval, const WOLFSSL_X509_ALGOR *algor)
-{
- WOLFSSL_ENTER("X509_ALGOR_get0");
-
- if (!algor) {
- WOLFSSL_MSG("algor object is NULL");
- return;
- }
-
- if (paobj)
- *paobj = algor->algorithm;
- if (ppval)
- *ppval = algor->algorithm;
- if (pptype) {
- if (algor->parameter) {
- *pptype = algor->parameter->type;
- }
- else {
- /* Default to V_ASN1_OBJECT */
- *pptype = V_ASN1_OBJECT;
- }
- }
-}
-
-/**
- * Populate algor members.
- *
- * @param algor The object to be set
- * @param aobj The value to be set in algor->algorithm
- * @param ptype The type of algor->parameter
- * @param pval The value of algor->parameter
- * @return WOLFSSL_SUCCESS on success
- * WOLFSSL_FAILURE on missing parameters or bad malloc
- */
-int wolfSSL_X509_ALGOR_set0(WOLFSSL_X509_ALGOR *algor, WOLFSSL_ASN1_OBJECT *aobj,
- int ptype, void *pval)
-{
- if (!algor) {
- return WOLFSSL_FAILURE;
- }
- if (aobj) {
- algor->algorithm = aobj;
- }
- if (pval) {
- if (!algor->parameter) {
- algor->parameter = wolfSSL_ASN1_TYPE_new();
- if (!algor->parameter) {
- return WOLFSSL_FAILURE;
- }
- }
- wolfSSL_ASN1_TYPE_set(algor->parameter, ptype, pval);
- }
- return WOLFSSL_SUCCESS;
-}
-
-/**
- * Set `a` in a smart way.
- *
- * @param a Object to set
- * @param type The type of object in value
- * @param value Object to set
- */
-void wolfSSL_ASN1_TYPE_set(WOLFSSL_ASN1_TYPE *a, int type, void *value)
-{
- if (!a || !value) {
- return;
- }
- switch (type) {
- case V_ASN1_OBJECT:
- a->value.object = value;
- break;
- case V_ASN1_UTCTIME:
- a->value.utctime = value;
- break;
- case V_ASN1_GENERALIZEDTIME:
- a->value.generalizedtime = value;
- break;
- default:
- WOLFSSL_MSG("Unknown or unsupported ASN1_TYPE");
- return;
- }
- a->type = type;
-}
-
-/**
- * Allocate a new WOLFSSL_ASN1_TYPE object.
- *
- * @return New zero'ed WOLFSSL_ASN1_TYPE object
- */
-WOLFSSL_ASN1_TYPE* wolfSSL_ASN1_TYPE_new(void)
-{
- WOLFSSL_ASN1_TYPE* ret = (WOLFSSL_ASN1_TYPE*)XMALLOC(sizeof(WOLFSSL_ASN1_TYPE),
- NULL, DYNAMIC_TYPE_OPENSSL);
- if (!ret)
- return NULL;
- XMEMSET(ret, 0, sizeof(WOLFSSL_ASN1_TYPE));
- return ret;
-}
-
-/**
- * Free WOLFSSL_ASN1_TYPE and all its members.
- *
- * @param at Object to free
- */
-void wolfSSL_ASN1_TYPE_free(WOLFSSL_ASN1_TYPE* at)
-{
- if (at) {
- switch (at->type) {
- case V_ASN1_OBJECT:
- wolfSSL_ASN1_OBJECT_free(at->value.object);
- break;
- case V_ASN1_UTCTIME:
- wolfSSL_ASN1_TIME_free(at->value.utctime);
- break;
- case V_ASN1_GENERALIZEDTIME:
- wolfSSL_ASN1_TIME_free(at->value.generalizedtime);
- break;
- default:
- WOLFSSL_MSG("Unknown or unsupported ASN1_TYPE");
- break;
- }
- XFREE(at, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-/**
- * Allocate a new WOLFSSL_X509_PUBKEY object.
- *
- * @return New zero'ed WOLFSSL_X509_PUBKEY object
- */
-WOLFSSL_X509_PUBKEY *wolfSSL_X509_PUBKEY_new(void)
-{
- WOLFSSL_X509_PUBKEY *ret;
- ret = (WOLFSSL_X509_PUBKEY*)XMALLOC(sizeof(WOLFSSL_X509_PUBKEY), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (!ret) {
- return NULL;
- }
- XMEMSET(ret, 0, sizeof(WOLFSSL_X509_PUBKEY));
- ret->algor = wolfSSL_X509_ALGOR_new();
- if (!ret->algor) {
- wolfSSL_X509_PUBKEY_free(ret);
- return NULL;
- }
- return ret;
-}
-
-/**
- * Free WOLFSSL_X509_PUBKEY and all its members.
- *
- * @param at Object to free
- */
-void wolfSSL_X509_PUBKEY_free(WOLFSSL_X509_PUBKEY *x)
-{
- if (x) {
- if (x->algor) {
- wolfSSL_X509_ALGOR_free(x->algor);
- }
- if (x->pkey) {
- wolfSSL_EVP_PKEY_free(x->pkey);
- }
- XFREE(x, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-/* Returns X509_PUBKEY structure containing X509_ALGOR and EVP_PKEY */
-WOLFSSL_X509_PUBKEY* wolfSSL_X509_get_X509_PUBKEY(const WOLFSSL_X509* x509)
-{
- WOLFSSL_ENTER("X509_get_X509_PUBKEY");
-
- if (x509 == NULL) {
- WOLFSSL_MSG("x509 struct NULL error");
- return NULL;
- }
-
- return (WOLFSSL_X509_PUBKEY*)&x509->key;
-}
-
-/* Sets ppkalg pointer to X509_PUBKEY algorithm. Returns WOLFSSL_SUCCESS on
- success or WOLFSSL_FAILURE on error. */
-int wolfSSL_X509_PUBKEY_get0_param(WOLFSSL_ASN1_OBJECT **ppkalg,
- const unsigned char **pk, int *ppklen, WOLFSSL_X509_ALGOR **pa,
- WOLFSSL_X509_PUBKEY *pub)
-{
- WOLFSSL_ENTER("X509_PUBKEY_get0_param");
-
- if (!pub || !pub->pubKeyOID) {
- WOLFSSL_MSG("X509_PUBKEY struct not populated");
- return WOLFSSL_FAILURE;
- }
-
- if (!pub->algor) {
- if (!(pub->algor = wolfSSL_X509_ALGOR_new())) {
- return WOLFSSL_FAILURE;
- }
- pub->algor->algorithm = wolfSSL_OBJ_nid2obj(pub->pubKeyOID);
- if (pub->algor->algorithm == NULL) {
- WOLFSSL_MSG("Failed to create object from NID");
- return WOLFSSL_FAILURE;
- }
- }
-
- if (pa)
- *pa = pub->algor;
- if (ppkalg)
- *ppkalg = pub->algor->algorithm;
- if (pk)
- wolfSSL_EVP_PKEY_get_der(pub->pkey, (unsigned char **)pk);
- if (ppklen)
- *ppklen = wolfSSL_EVP_PKEY_get_der(pub->pkey, NULL);
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Returns a pointer to the pkey when passed a key */
-WOLFSSL_EVP_PKEY* wolfSSL_X509_PUBKEY_get(WOLFSSL_X509_PUBKEY* key)
-{
- WOLFSSL_ENTER("wolfSSL_X509_PUBKEY_get");
- if(key == NULL || key->pkey == NULL){
- WOLFSSL_LEAVE("wolfSSL_X509_PUBKEY_get", BAD_FUNC_ARG);
- return NULL;
- }
- WOLFSSL_LEAVE("wolfSSL_X509_PUBKEY_get", WOLFSSL_SUCCESS);
- return key->pkey;
-}
-
-int wolfSSL_X509_PUBKEY_set(WOLFSSL_X509_PUBKEY **x, WOLFSSL_EVP_PKEY *key)
-{
- WOLFSSL_X509_PUBKEY *pk = NULL;
-
- WOLFSSL_ENTER("wolfSSL_X509_PUBKEY_set");
-
- if (!x || !key) {
- return WOLFSSL_FAILURE;
- }
-
- if (!(pk = wolfSSL_X509_PUBKEY_new())) {
- return WOLFSSL_FAILURE;
- }
-
- switch (key->type) {
-#ifndef NO_RSA
- case EVP_PKEY_RSA:
- pk->algor->algorithm= wolfSSL_OBJ_nid2obj(RSAk);
- break;
-#endif
-#ifndef NO_DSA
- case EVP_PKEY_DSA:
- pk->algor->algorithm = wolfSSL_OBJ_nid2obj(DSAk);
- break;
-#endif
-#ifdef HAVE_ECC
- case EVP_PKEY_EC:
- pk->algor->algorithm = wolfSSL_OBJ_nid2obj(ECDSAk);
- break;
-#endif
- default:
- WOLFSSL_MSG("Unknown key type");
- goto error;
- }
-
- if (!pk->algor->algorithm) {
- WOLFSSL_MSG("Failed to create algorithm object");
- goto error;
- }
-
- if (!wolfSSL_EVP_PKEY_up_ref(key)) {
- WOLFSSL_MSG("Failed to up key reference");
- goto error;
- }
- pk->pkey = key;
-
- wolfSSL_X509_PUBKEY_free(*x);
- *x = pk;
- return WOLFSSL_SUCCESS;
-error:
- if (pk) {
- wolfSSL_X509_PUBKEY_free(pk);
- }
- return WOLFSSL_FAILURE;
-}
-
-#endif /* OPENSSL_ALL || WOLFSSL_APACHE_HTTPD || WOLFSSL_HAPROXY*/
-
-#ifndef NO_WOLFSSL_STUB
-/*** TBD ***/
-WOLFSSL_API WOLFSSL_EVP_PKEY *wolfSSL_get_privatekey(const WOLFSSL *ssl)
-{
- (void)ssl;
- WOLFSSL_STUB("SSL_get_privatekey");
- return NULL;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-/*** TBD ***/
-WOLFSSL_API int i2t_ASN1_OBJECT(char *buf, int buf_len, WOLFSSL_ASN1_OBJECT *a)
-{
- (void)buf;
- (void)buf_len;
- (void)a;
- WOLFSSL_STUB("i2t_ASN1_OBJECT");
- return -1;
-}
-#endif
-
-/* Return number of bytes written to BIO on success. 0 on failure. */
-WOLFSSL_API int wolfSSL_i2a_ASN1_OBJECT(WOLFSSL_BIO *bp,
- WOLFSSL_ASN1_OBJECT *a)
-{
- int length = 0;
- word32 idx = 0;
- const char null_str[] = "NULL";
-
- WOLFSSL_ENTER("wolfSSL_i2a_ASN1_OBJECT");
-
- if (bp == NULL)
- return WOLFSSL_FAILURE;
-
- if (a == NULL) {
- /* Write "NULL" */
- if (wolfSSL_BIO_write(bp, null_str, (int)XSTRLEN(null_str)) ==
- (int)XSTRLEN(null_str)) {
- return (int)XSTRLEN(null_str);
- }
- else {
- return WOLFSSL_FAILURE;
- }
- }
-
-
- if ((a->obj == NULL) || (a->obj[idx++] != ASN_OBJECT_ID)) {
- WOLFSSL_MSG("Bad ASN1 Object");
- return WOLFSSL_FAILURE;
- }
-
- if (GetLength((const byte*)a->obj, &idx, &length,
- a->objSz) < 0 || length < 0) {
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_BIO_write(bp, a->obj + idx, length) == (int)length) {
- return length;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)
-#ifndef NO_WOLFSSL_STUB
-/*** TBD ***/
-WOLFSSL_API size_t SSL_get_finished(const WOLFSSL *s, void *buf, size_t count)
-{
- (void)s;
- (void)buf;
- (void)count;
- WOLFSSL_STUB("SSL_get_finished");
- return WOLFSSL_FAILURE;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-/*** TBD ***/
-WOLFSSL_API size_t SSL_get_peer_finished(const WOLFSSL *s, void *buf, size_t count)
-{
- (void)s;
- (void)buf;
- (void)count;
- WOLFSSL_STUB("SSL_get_peer_finished");
- return WOLFSSL_FAILURE;
-}
-#endif
-#endif /* WOLFSSL_HAPROXY */
-
-#ifndef NO_WOLFSSL_STUB
-/*** TBD ***/
-WOLFSSL_API void SSL_CTX_set_tmp_dh_callback(WOLFSSL_CTX *ctx, WOLFSSL_DH *(*dh) (WOLFSSL *ssl, int is_export, int keylength))
-{
- (void)ctx;
- (void)dh;
- WOLFSSL_STUB("SSL_CTX_set_tmp_dh_callback");
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-/*** TBD ***/
-WOLFSSL_API WOLF_STACK_OF(SSL_COMP) *SSL_COMP_get_compression_methods(void)
-{
- WOLFSSL_STUB("SSL_COMP_get_compression_methods");
- return NULL;
-}
-#endif
-
-
-int wolfSSL_sk_SSL_CIPHER_num(const WOLF_STACK_OF(WOLFSSL_CIPHER)* p)
-{
- WOLFSSL_ENTER("wolfSSL_sk_SSL_CIPHER_num");
- if (p == NULL) {
- return WOLFSSL_FATAL_ERROR;
- }
- return (int)p->num;
-}
-
-#if !defined(NO_FILESYSTEM)
-#ifndef NO_WOLFSSL_STUB
-/*** TBD ***/
-WOLFSSL_API WOLFSSL_EVP_PKEY *wolfSSL_PEM_read_PrivateKey(XFILE fp, WOLFSSL_EVP_PKEY **x, pem_password_cb *cb, void *u)
-{
- (void)fp;
- (void)x;
- (void)cb;
- (void)u;
- WOLFSSL_STUB("PEM_read_PrivateKey");
- return NULL;
-}
-#endif
-#endif
-
-#if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
-/* Loads certificate(s) files in pem format into X509_STORE struct from either
- * a file or directory.
- * Returns WOLFSSL_SUCCESS on success or WOLFSSL_FAILURE if an error occurs.
- */
-WOLFSSL_API int wolfSSL_X509_STORE_load_locations(WOLFSSL_X509_STORE *str,
- const char *file, const char *dir)
-{
- WOLFSSL_CTX* ctx;
- char *name = NULL;
- int ret = WOLFSSL_SUCCESS;
- int successes = 0;
-#ifdef WOLFSSL_SMALL_STACK
- ReadDirCtx* readCtx = NULL;
-#else
- ReadDirCtx readCtx[1];
-#endif
-
- WOLFSSL_ENTER("X509_STORE_load_locations");
-
- if (str == NULL || str->cm == NULL || (file == NULL && dir == NULL))
- return WOLFSSL_FAILURE;
-
- /* tmp ctx for setting our cert manager */
- ctx = wolfSSL_CTX_new(cm_pick_method());
- if (ctx == NULL)
- return WOLFSSL_FAILURE;
-
- wolfSSL_CertManagerFree(ctx->cm);
- ctx->cm = str->cm;
-
-#ifdef HAVE_CRL
- if (str->cm->crl == NULL) {
- if (wolfSSL_CertManagerEnableCRL(str->cm, 0) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Enable CRL failed");
- wolfSSL_CTX_free(ctx);
- return WOLFSSL_FAILURE;
- }
- }
-#endif
-
- /* Load individual file */
- if (file) {
- /* Try to process file with type DETECT_CERT_TYPE to parse the
- correct certificate header and footer type */
- ret = ProcessFile(ctx, file, WOLFSSL_FILETYPE_PEM, DETECT_CERT_TYPE,
- NULL, 0, str->cm->crl, 0);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Failed to load file");
- ret = WOLFSSL_FAILURE;
- }
- }
-
- /* Load files in dir */
- if (dir && ret == WOLFSSL_SUCCESS) {
- #ifdef WOLFSSL_SMALL_STACK
- readCtx = (ReadDirCtx*)XMALLOC(sizeof(ReadDirCtx), ctx->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (readCtx == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
- #endif
-
- /* try to load each regular file in dir */
- ret = wc_ReadDirFirst(readCtx, dir, &name);
- while (ret == 0 && name) {
- WOLFSSL_MSG(name);
- /* Try to process file with type DETECT_CERT_TYPE to parse the
- correct certificate header and footer type */
- ret = ProcessFile(ctx, name, WOLFSSL_FILETYPE_PEM, DETECT_CERT_TYPE,
- NULL, 0, str->cm->crl, 0);
- /* Not failing on load errors */
- if (ret != WOLFSSL_SUCCESS)
- WOLFSSL_MSG("Failed to load file in path, continuing");
- else
- successes++;
-
- ret = wc_ReadDirNext(readCtx, dir, &name);
- }
- wc_ReadDirClose(readCtx);
-
- /* Success if at least one file in dir was loaded */
- if (successes > 0)
- ret = WOLFSSL_SUCCESS;
- else {
- WOLFSSL_ERROR(ret);
- ret = WOLFSSL_FAILURE;
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(readCtx, ctx->heap, DYNAMIC_TYPE_DIRCTX);
- #endif
- }
-
- ctx->cm = NULL;
- wolfSSL_CTX_free(ctx);
-
- return ret;
-}
-#endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
-
-#ifndef NO_WOLFSSL_STUB
-/*** TBD ***/
-WOLFSSL_API WOLFSSL_CIPHER* wolfSSL_sk_SSL_CIPHER_value(void *ciphers, int idx)
-{
- (void)ciphers;
- (void)idx;
- WOLFSSL_STUB("wolfSSL_sk_SSL_CIPHER_value");
- return NULL;
-}
-#endif
-
-WOLFSSL_API void ERR_load_SSL_strings(void)
-{
-
-}
-
-#ifdef HAVE_OCSP
-WOLFSSL_API long wolfSSL_get_tlsext_status_ocsp_resp(WOLFSSL *s, unsigned char **resp)
-{
- if (s == NULL || resp == NULL)
- return 0;
-
- *resp = s->ocspResp;
- return s->ocspRespSz;
-}
-
-WOLFSSL_API long wolfSSL_set_tlsext_status_ocsp_resp(WOLFSSL *s, unsigned char *resp, int len)
-{
- if (s == NULL)
- return WOLFSSL_FAILURE;
-
- s->ocspResp = resp;
- s->ocspRespSz = len;
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* HAVE_OCSP */
-
-#endif /* OPENSSL_EXTRA */
-
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
-long wolfSSL_get_verify_result(const WOLFSSL *ssl)
-{
- if (ssl == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- return ssl->peerVerifyRet;
-}
-#endif
-
-#ifdef OPENSSL_EXTRA
-
-#ifndef NO_WOLFSSL_STUB
-/* shows the number of accepts attempted by CTX in it's lifetime */
-long wolfSSL_CTX_sess_accept(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_accept");
- (void)ctx;
- return 0;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-/* shows the number of connects attempted CTX in it's lifetime */
-long wolfSSL_CTX_sess_connect(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_connect");
- (void)ctx;
- return 0;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-/* shows the number of accepts completed by CTX in it's lifetime */
-long wolfSSL_CTX_sess_accept_good(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_accept_good");
- (void)ctx;
- return 0;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-/* shows the number of connects completed by CTX in it's lifetime */
-long wolfSSL_CTX_sess_connect_good(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_connect_good");
- (void)ctx;
- return 0;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-/* shows the number of renegotiation accepts attempted by CTX */
-long wolfSSL_CTX_sess_accept_renegotiate(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_accept_renegotiate");
- (void)ctx;
- return 0;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-/* shows the number of renegotiation accepts attempted by CTX */
-long wolfSSL_CTX_sess_connect_renegotiate(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_connect_renegotiate");
- (void)ctx;
- return 0;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-long wolfSSL_CTX_sess_hits(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_hits");
- (void)ctx;
- return 0;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-long wolfSSL_CTX_sess_cb_hits(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_cb_hits");
- (void)ctx;
- return 0;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-long wolfSSL_CTX_sess_cache_full(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_cache_full");
- (void)ctx;
- return 0;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-long wolfSSL_CTX_sess_misses(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_misses");
- (void)ctx;
- return 0;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-long wolfSSL_CTX_sess_timeouts(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_STUB("wolfSSL_CTX_sess_timeouts");
- (void)ctx;
- return 0;
-}
-#endif
-
-
-/* Return the total number of sessions */
-long wolfSSL_CTX_sess_number(WOLFSSL_CTX* ctx)
-{
- word32 total = 0;
-
- WOLFSSL_ENTER("wolfSSL_CTX_sess_number");
- (void)ctx;
-
-#ifdef WOLFSSL_SESSION_STATS
- if (wolfSSL_get_session_stats(NULL, &total, NULL, NULL) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error getting session stats");
- }
-#else
- WOLFSSL_MSG("Please use macro WOLFSSL_SESSION_STATS for session stats");
-#endif
-
- return (long)total;
-}
-
-
-#ifndef NO_CERTS
-long wolfSSL_CTX_add_extra_chain_cert(WOLFSSL_CTX* ctx, WOLFSSL_X509* x509)
-{
- byte* chain = NULL;
- long chainSz = 0;
- int derSz;
- const byte* der;
- int ret;
- int idx = 0;
- DerBuffer *derBuffer = NULL;
-
- WOLFSSL_ENTER("wolfSSL_CTX_add_extra_chain_cert");
-
- if (ctx == NULL || x509 == NULL) {
- WOLFSSL_MSG("Bad Argument");
- return WOLFSSL_FAILURE;
- }
-
- der = wolfSSL_X509_get_der(x509, &derSz);
- if (der == NULL || derSz <= 0) {
- WOLFSSL_MSG("Error getting X509 DER");
- return WOLFSSL_FAILURE;
- }
-
- if (ctx->certificate == NULL) {
- WOLFSSL_ENTER("wolfSSL_use_certificate_chain_buffer_format");
-
- /* Process buffer makes first certificate the leaf. */
- ret = ProcessBuffer(ctx, der, derSz, WOLFSSL_FILETYPE_ASN1, CERT_TYPE,
- NULL, NULL, 1, GET_VERIFY_SETTING_CTX(ctx));
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_LEAVE("wolfSSL_CTX_add_extra_chain_cert", ret);
- return WOLFSSL_FAILURE;
- }
- }
- else {
- /* TODO: Do this elsewhere. */
- ret = AllocDer(&derBuffer, derSz, CERT_TYPE, ctx->heap);
- if (ret != 0) {
- WOLFSSL_MSG("Memory Error");
- return WOLFSSL_FAILURE;
- }
- XMEMCPY(derBuffer->buffer, der, derSz);
- ret = AddCA(ctx->cm, &derBuffer, WOLFSSL_USER_CA,
- GET_VERIFY_SETTING_CTX(ctx));
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_LEAVE("wolfSSL_CTX_add_extra_chain_cert", ret);
- return WOLFSSL_FAILURE;
- }
-
- /* adding cert to existing chain */
- if (ctx->certChain != NULL && ctx->certChain->length > 0) {
- chainSz += ctx->certChain->length;
- }
- chainSz += OPAQUE24_LEN + derSz;
-
- chain = (byte*)XMALLOC(chainSz, ctx->heap, DYNAMIC_TYPE_DER);
- if (chain == NULL) {
- WOLFSSL_MSG("Memory Error");
- return WOLFSSL_FAILURE;
- }
-
- if (ctx->certChain != NULL && ctx->certChain->length > 0) {
- XMEMCPY(chain, ctx->certChain->buffer, ctx->certChain->length);
- idx = ctx->certChain->length;
- }
- c32to24(derSz, chain + idx);
- idx += OPAQUE24_LEN,
- XMEMCPY(chain + idx, der, derSz);
- idx += derSz;
-#ifdef WOLFSSL_TLS13
- ctx->certChainCnt++;
-#endif
-
- FreeDer(&ctx->certChain);
- ret = AllocDer(&ctx->certChain, idx, CERT_TYPE, ctx->heap);
- if (ret == 0) {
- XMEMCPY(ctx->certChain->buffer, chain, idx);
- }
- }
-
- /* on success WOLFSSL_X509 memory is responsibility of ctx */
- wolfSSL_X509_free(x509);
- if (chain != NULL)
- XFREE(chain, ctx->heap, DYNAMIC_TYPE_DER);
-
- return WOLFSSL_SUCCESS;
-}
-
-
-long wolfSSL_CTX_set_tlsext_status_arg(WOLFSSL_CTX* ctx, void* arg)
-{
- if (ctx == NULL || ctx->cm == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- ctx->cm->ocspIOCtx = arg;
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* NO_CERTS */
-
-
-/* Get the session cache mode for CTX
- *
- * ctx WOLFSSL_CTX struct to get cache mode from
- *
- * Returns a bit mask that has the session cache mode */
-WOLFSSL_API long wolfSSL_CTX_get_session_cache_mode(WOLFSSL_CTX* ctx)
-{
- long m = 0;
-
- WOLFSSL_ENTER("SSL_CTX_set_session_cache_mode");
-
- if (ctx == NULL) {
- return m;
- }
-
- if (ctx->sessionCacheOff != 1) {
- m |= SSL_SESS_CACHE_SERVER;
- }
-
- if (ctx->sessionCacheFlushOff == 1) {
- m |= SSL_SESS_CACHE_NO_AUTO_CLEAR;
- }
-
-#ifdef HAVE_EXT_CACHE
- if (ctx->internalCacheOff == 1) {
- m |= SSL_SESS_CACHE_NO_INTERNAL_STORE;
- }
-#endif
-
- return m;
-}
-
-
-int wolfSSL_CTX_get_read_ahead(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- return ctx->readAhead;
-}
-
-
-int wolfSSL_CTX_set_read_ahead(WOLFSSL_CTX* ctx, int v)
-{
- if (ctx == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- ctx->readAhead = (byte)v;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-long wolfSSL_CTX_set_tlsext_opaque_prf_input_callback_arg(WOLFSSL_CTX* ctx,
- void* arg)
-{
- if (ctx == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- ctx->userPRFArg = arg;
- return WOLFSSL_SUCCESS;
-}
-
-
-#ifndef NO_DES3
-/* 0 on success */
-int wolfSSL_DES_set_key(WOLFSSL_const_DES_cblock* myDes,
- WOLFSSL_DES_key_schedule* key)
-{
-#ifdef WOLFSSL_CHECK_DESKEY
- return wolfSSL_DES_set_key_checked(myDes, key);
-#else
- wolfSSL_DES_set_key_unchecked(myDes, key);
- return 0;
-#endif
-}
-
-
-
-/* return true in fail case (1) */
-static int DES_check(word32 mask, word32 mask2, unsigned char* key)
-{
- word32 value[2];
-
- /* sanity check on length made in wolfSSL_DES_set_key_checked */
- value[0] = mask;
- value[1] = mask2;
- return (XMEMCMP(value, key, sizeof(value)) == 0)? 1: 0;
-}
-
-
-/* check that the key is odd parity and is not a weak key
- * returns -1 if parity is wrong, -2 if weak/null key and 0 on success */
-int wolfSSL_DES_set_key_checked(WOLFSSL_const_DES_cblock* myDes,
- WOLFSSL_DES_key_schedule* key)
-{
- if (myDes == NULL || key == NULL) {
- WOLFSSL_MSG("Bad argument passed to wolfSSL_DES_set_key_checked");
- return -2;
- }
- else {
- word32 sz = sizeof(WOLFSSL_DES_key_schedule);
-
- /* sanity check before call to DES_check */
- if (sz != (sizeof(word32) * 2)) {
- WOLFSSL_MSG("Unexpected WOLFSSL_DES_key_schedule size");
- return -2;
- }
-
- /* check odd parity */
- if (wolfSSL_DES_check_key_parity(myDes) != 1) {
- WOLFSSL_MSG("Odd parity test fail");
- return -1;
- }
-
- if (wolfSSL_DES_is_weak_key(myDes) == 1) {
- WOLFSSL_MSG("Weak key found");
- return -2;
- }
-
- /* passed tests, now copy over key */
- XMEMCPY(key, myDes, sizeof(WOLFSSL_const_DES_cblock));
-
- return 0;
- }
-}
-
-
-/* check is not weak. Weak key list from Nist "Recommendation for the Triple
- * Data Encryption Algorithm (TDEA) Block Cipher"
- *
- * returns 1 if is weak 0 if not
- */
-int wolfSSL_DES_is_weak_key(WOLFSSL_const_DES_cblock* key)
-{
- word32 mask, mask2;
-
- WOLFSSL_ENTER("wolfSSL_DES_is_weak_key");
-
- if (key == NULL) {
- WOLFSSL_MSG("NULL key passed in");
- return 1;
- }
-
- mask = 0x01010101; mask2 = 0x01010101;
- if (DES_check(mask, mask2, *key)) {
- WOLFSSL_MSG("Weak key found");
- return 1;
- }
-
- mask = 0xFEFEFEFE; mask2 = 0xFEFEFEFE;
- if (DES_check(mask, mask2, *key)) {
- WOLFSSL_MSG("Weak key found");
- return 1;
- }
-
- mask = 0xE0E0E0E0; mask2 = 0xF1F1F1F1;
- if (DES_check(mask, mask2, *key)) {
- WOLFSSL_MSG("Weak key found");
- return 1;
- }
-
- mask = 0x1F1F1F1F; mask2 = 0x0E0E0E0E;
- if (DES_check(mask, mask2, *key)) {
- WOLFSSL_MSG("Weak key found");
- return 1;
- }
-
- /* semi-weak *key check (list from same Nist paper) */
- mask = 0x011F011F; mask2 = 0x010E010E;
- if (DES_check(mask, mask2, *key) ||
- DES_check(ByteReverseWord32(mask), ByteReverseWord32(mask2), *key)) {
- WOLFSSL_MSG("Weak key found");
- return 1;
- }
-
- mask = 0x01E001E0; mask2 = 0x01F101F1;
- if (DES_check(mask, mask2, *key) ||
- DES_check(ByteReverseWord32(mask), ByteReverseWord32(mask2), *key)) {
- WOLFSSL_MSG("Weak key found");
- return 1;
- }
-
- mask = 0x01FE01FE; mask2 = 0x01FE01FE;
- if (DES_check(mask, mask2, *key) ||
- DES_check(ByteReverseWord32(mask), ByteReverseWord32(mask2), *key)) {
- WOLFSSL_MSG("Weak key found");
- return 1;
- }
-
- mask = 0x1FE01FE0; mask2 = 0x0EF10EF1;
- if (DES_check(mask, mask2, *key) ||
- DES_check(ByteReverseWord32(mask), ByteReverseWord32(mask2), *key)) {
- WOLFSSL_MSG("Weak key found");
- return 1;
- }
-
- mask = 0x1FFE1FFE; mask2 = 0x0EFE0EFE;
- if (DES_check(mask, mask2, *key) ||
- DES_check(ByteReverseWord32(mask), ByteReverseWord32(mask2), *key)) {
- WOLFSSL_MSG("Weak key found");
- return 1;
- }
-
- return 0;
-}
-
-
-void wolfSSL_DES_set_key_unchecked(WOLFSSL_const_DES_cblock* myDes,
- WOLFSSL_DES_key_schedule* key)
-{
- if (myDes != NULL && key != NULL) {
- XMEMCPY(key, myDes, sizeof(WOLFSSL_const_DES_cblock));
- }
-}
-
-
-/* Sets the parity of the DES key for use */
-void wolfSSL_DES_set_odd_parity(WOLFSSL_DES_cblock* myDes)
-{
- word32 i;
- word32 sz = sizeof(WOLFSSL_DES_cblock);
-
- WOLFSSL_ENTER("wolfSSL_DES_set_odd_parity");
-
- for (i = 0; i < sz; i++) {
- unsigned char c = (*myDes)[i];
- if ((
- ((c >> 1) & 0x01) ^
- ((c >> 2) & 0x01) ^
- ((c >> 3) & 0x01) ^
- ((c >> 4) & 0x01) ^
- ((c >> 5) & 0x01) ^
- ((c >> 6) & 0x01) ^
- ((c >> 7) & 0x01)) == (c & 0x01)) {
- WOLFSSL_MSG("Flipping parity bit");
- (*myDes)[i] = c ^ 0x01;
- }
- }
-}
-
-int wolfSSL_DES_check_key_parity(WOLFSSL_DES_cblock *myDes)
-{
- word32 i;
- word32 sz = sizeof(WOLFSSL_DES_cblock);
-
- WOLFSSL_ENTER("wolfSSL_DES_check_key_parity");
-
- for (i = 0; i < sz; i++) {
- unsigned char c = (*myDes)[i];
- if ((
- ((c >> 1) & 0x01) ^
- ((c >> 2) & 0x01) ^
- ((c >> 3) & 0x01) ^
- ((c >> 4) & 0x01) ^
- ((c >> 5) & 0x01) ^
- ((c >> 6) & 0x01) ^
- ((c >> 7) & 0x01)) == (c & 0x01)) {
- return 0;
- }
- }
- return 1;
-}
-
-#ifdef WOLFSSL_DES_ECB
-/* Encrypt or decrypt input message desa with key and get output in desb.
- * if enc is DES_ENCRYPT,input message is encrypted or
- * if enc is DES_DECRYPT,input message is decrypted.
- * */
-void wolfSSL_DES_ecb_encrypt(WOLFSSL_DES_cblock* desa,
- WOLFSSL_DES_cblock* desb, WOLFSSL_DES_key_schedule* key, int enc)
-{
- Des myDes;
-
- WOLFSSL_ENTER("wolfSSL_DES_ecb_encrypt");
-
- if (desa == NULL || key == NULL || desb == NULL ||
- (enc != DES_ENCRYPT && enc != DES_DECRYPT)) {
- WOLFSSL_MSG("Bad argument passed to wolfSSL_DES_ecb_encrypt");
- } else {
- if (wc_Des_SetKey(&myDes, (const byte*) key,
- (const byte*) NULL, !enc) != 0) {
- WOLFSSL_MSG("wc_Des_SetKey return error.");
- return;
- }
- if (enc){
- if (wc_Des_EcbEncrypt(&myDes, (byte*) desb, (const byte*) desa,
- sizeof(WOLFSSL_DES_cblock)) != 0){
- WOLFSSL_MSG("wc_Des_EcbEncrypt return error.");
- }
- } else {
- if (wc_Des_EcbDecrypt(&myDes, (byte*) desb, (const byte*) desa,
- sizeof(WOLFSSL_DES_cblock)) != 0){
- WOLFSSL_MSG("wc_Des_EcbDecrpyt return error.");
- }
- }
- }
-}
-#endif
-#endif /* NO_DES3 */
-
-#ifndef NO_RC4
-/* Set the key state for Arc4 structure.
- *
- * key Arc4 structure to use
- * len length of data buffer
- * data initial state to set Arc4 structure
- */
-void wolfSSL_RC4_set_key(WOLFSSL_RC4_KEY* key, int len,
- const unsigned char* data)
-{
- typedef char rc4_test[sizeof(WOLFSSL_RC4_KEY) >= sizeof(Arc4) ? 1 : -1];
- (void)sizeof(rc4_test);
-
- WOLFSSL_ENTER("wolfSSL_RC4_set_key");
-
- if (key == NULL || len < 0) {
- WOLFSSL_MSG("bad argument passed in");
- return;
- }
-
- XMEMSET(key, 0, sizeof(WOLFSSL_RC4_KEY));
- wc_Arc4SetKey((Arc4*)key, data, (word32)len);
-}
-
-
-/* Encrypt/decrypt with Arc4 structure.
- *
- * len length of buffer to encrypt/decrypt (in/out)
- * in buffer to encrypt/decrypt
- * out results of encryption/decryption
- */
-void wolfSSL_RC4(WOLFSSL_RC4_KEY* key, size_t len,
- const unsigned char* in, unsigned char* out)
-{
- WOLFSSL_ENTER("wolfSSL_RC4");
-
- if (key == NULL || in == NULL || out == NULL) {
- WOLFSSL_MSG("Bad argument passed in");
- return;
- }
-
- wc_Arc4Process((Arc4*)key, out, in, (word32)len);
-}
-#endif /* NO_RC4 */
-
-#ifndef NO_AES
-
-#ifdef WOLFSSL_AES_DIRECT
-/* AES encrypt direct, it is expected to be blocks of AES_BLOCK_SIZE for input.
- *
- * input Data to encrypt
- * output Encrypted data after done
- * key AES key to use for encryption
- */
-void wolfSSL_AES_encrypt(const unsigned char* input, unsigned char* output,
- AES_KEY *key)
-{
- WOLFSSL_ENTER("wolfSSL_AES_encrypt");
-
- if (input == NULL || output == NULL || key == NULL) {
- WOLFSSL_MSG("Null argument passed in");
- return;
- }
-
- wc_AesEncryptDirect((Aes*)key, output, input);
-}
-
-
-/* AES decrypt direct, it is expected to be blocks of AES_BLOCK_SIZE for input.
- *
- * input Data to decrypt
- * output Decrypted data after done
- * key AES key to use for encryption
- */
-void wolfSSL_AES_decrypt(const unsigned char* input, unsigned char* output,
- AES_KEY *key)
-{
- WOLFSSL_ENTER("wolfSSL_AES_decrypt");
-
- if (input == NULL || output == NULL || key == NULL) {
- WOLFSSL_MSG("Null argument passed in");
- return;
- }
-
- wc_AesDecryptDirect((Aes*)key, output, input);
-}
-#endif /* WOLFSSL_AES_DIRECT */
-
-/* Setup of an AES key to use for encryption.
- *
- * key key in bytes to use for encryption
- * bits size of key in bits
- * aes AES structure to initialize
- */
-int wolfSSL_AES_set_encrypt_key(const unsigned char *key, const int bits,
- AES_KEY *aes)
-{
- typedef char aes_test[sizeof(AES_KEY) >= sizeof(Aes) ? 1 : -1];
- (void)sizeof(aes_test);
-
- WOLFSSL_ENTER("wolfSSL_AES_set_encrypt_key");
-
- if (key == NULL || aes == NULL) {
- WOLFSSL_MSG("Null argument passed in");
- return -1;
- }
-
- XMEMSET(aes, 0, sizeof(AES_KEY));
- if (wc_AesSetKey((Aes*)aes, key, ((bits)/8), NULL, AES_ENCRYPTION) != 0) {
- WOLFSSL_MSG("Error in setting AES key");
- return -1;
- }
- return 0;
-}
-
-
-/* Setup of an AES key to use for decryption.
- *
- * key key in bytes to use for decryption
- * bits size of key in bits
- * aes AES structure to initialize
- */
-int wolfSSL_AES_set_decrypt_key(const unsigned char *key, const int bits,
- AES_KEY *aes)
-{
- typedef char aes_test[sizeof(AES_KEY) >= sizeof(Aes) ? 1 : -1];
- (void)sizeof(aes_test);
-
- WOLFSSL_ENTER("wolfSSL_AES_set_decrypt_key");
-
- if (key == NULL || aes == NULL) {
- WOLFSSL_MSG("Null argument passed in");
- return -1;
- }
-
- XMEMSET(aes, 0, sizeof(AES_KEY));
- if (wc_AesSetKey((Aes*)aes, key, ((bits)/8), NULL, AES_DECRYPTION) != 0) {
- WOLFSSL_MSG("Error in setting AES key");
- return -1;
- }
- return 0;
-}
-
-
-#ifdef HAVE_AES_ECB
-/* Encrypt/decrypt a 16 byte block of data using the key passed in.
- *
- * in buffer to encrypt/decrypt
- * out buffer to hold result of encryption/decryption
- * key AES structure to use with encryption/decryption
- * enc AES_ENCRPT for encryption and AES_DECRYPT for decryption
- */
-void wolfSSL_AES_ecb_encrypt(const unsigned char *in, unsigned char* out,
- AES_KEY *key, const int enc)
-{
- Aes* aes;
-
- WOLFSSL_ENTER("wolfSSL_AES_ecb_encrypt");
-
- if (key == NULL || in == NULL || out == NULL) {
- WOLFSSL_MSG("Error, Null argument passed in");
- return;
- }
-
- aes = (Aes*)key;
- if (enc == AES_ENCRYPT) {
- if (wc_AesEcbEncrypt(aes, out, in, AES_BLOCK_SIZE) != 0) {
- WOLFSSL_MSG("Error with AES CBC encrypt");
- }
- }
- else {
- #ifdef HAVE_AES_DECRYPT
- if (wc_AesEcbDecrypt(aes, out, in, AES_BLOCK_SIZE) != 0) {
- WOLFSSL_MSG("Error with AES CBC decrypt");
- }
- #else
- WOLFSSL_MSG("AES decryption not compiled in");
- #endif
- }
-}
-#endif /* HAVE_AES_ECB */
-
-#ifdef HAVE_AES_CBC
-/* Encrypt data using key and iv passed in. iv gets updated to most recent iv
- * state after encryption/decryption.
- *
- * in buffer to encrypt/decrypt
- * out buffer to hold result of encryption/decryption
- * len length of input buffer
- * key AES structure to use with encryption/decryption
- * iv iv to use with operation
- * enc AES_ENCRPT for encryption and AES_DECRYPT for decryption
- */
-void wolfSSL_AES_cbc_encrypt(const unsigned char *in, unsigned char* out,
- size_t len, AES_KEY *key, unsigned char* iv, const int enc)
-{
- Aes* aes;
-
- WOLFSSL_ENTER("wolfSSL_AES_cbc_encrypt");
-
- if (key == NULL || in == NULL || out == NULL || iv == NULL || len == 0) {
- WOLFSSL_MSG("Error, Null argument passed in");
- return;
- }
-
- aes = (Aes*)key;
- if (wc_AesSetIV(aes, (const byte*)iv) != 0) {
- WOLFSSL_MSG("Error with setting iv");
- return;
- }
-
- if (enc == AES_ENCRYPT) {
- if (wc_AesCbcEncrypt(aes, out, in, (word32)len) != 0) {
- WOLFSSL_MSG("Error with AES CBC encrypt");
- }
- }
- else {
- if (wc_AesCbcDecrypt(aes, out, in, (word32)len) != 0) {
- WOLFSSL_MSG("Error with AES CBC decrypt");
- }
- }
-
- /* to be compatible copy iv to iv buffer after completing operation */
- XMEMCPY(iv, (byte*)(aes->reg), AES_BLOCK_SIZE);
-}
-#endif /* HAVE_AES_CBC */
-
-
-/* Encrypt data using CFB mode with key and iv passed in. iv gets updated to
- * most recent iv state after encryption/decryption.
- *
- * in buffer to encrypt/decrypt
- * out buffer to hold result of encryption/decryption
- * len length of input buffer
- * key AES structure to use with encryption/decryption
- * iv iv to use with operation
- * num contains the amount of block used
- * enc AES_ENCRPT for encryption and AES_DECRYPT for decryption
- */
-void wolfSSL_AES_cfb128_encrypt(const unsigned char *in, unsigned char* out,
- size_t len, AES_KEY *key, unsigned char* iv, int* num,
- const int enc)
-{
-#ifndef WOLFSSL_AES_CFB
- WOLFSSL_MSG("CFB mode not enabled please use macro WOLFSSL_AES_CFB");
- (void)in;
- (void)out;
- (void)len;
- (void)key;
- (void)iv;
- (void)num;
- (void)enc;
-
- return;
-#else
- Aes* aes;
-
- WOLFSSL_ENTER("wolfSSL_AES_cbc_encrypt");
- if (key == NULL || in == NULL || out == NULL || iv == NULL) {
- WOLFSSL_MSG("Error, Null argument passed in");
- return;
- }
-
- aes = (Aes*)key;
- if (wc_AesSetIV(aes, (const byte*)iv) != 0) {
- WOLFSSL_MSG("Error with setting iv");
- return;
- }
-
- if (enc == AES_ENCRYPT) {
- if (wc_AesCfbEncrypt(aes, out, in, (word32)len) != 0) {
- WOLFSSL_MSG("Error with AES CBC encrypt");
- }
- }
- else {
- if (wc_AesCfbDecrypt(aes, out, in, (word32)len) != 0) {
- WOLFSSL_MSG("Error with AES CBC decrypt");
- }
- }
-
- /* to be compatible copy iv to iv buffer after completing operation */
- XMEMCPY(iv, (byte*)(aes->reg), AES_BLOCK_SIZE);
-
- /* store number of left over bytes to num */
- *num = (aes->left)? AES_BLOCK_SIZE - aes->left : 0;
-#endif /* WOLFSSL_AES_CFB */
-}
-#endif /* NO_AES */
-
-#ifndef NO_FILESYSTEM
- #ifdef __clang__
- #pragma clang diagnostic push
- #pragma clang diagnostic ignored "-Wformat-nonliteral"
- #endif
-#endif
-
-#if !defined(NO_FILESYSTEM) && defined (OPENSSL_EXTRA)
-/* returns amount printed on success, negative in fail case */
-int wolfSSL_BIO_vprintf(WOLFSSL_BIO* bio, const char* format, va_list args)
-{
- int ret = -1;
-
- if (bio == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- switch (bio->type) {
- case WOLFSSL_BIO_FILE:
- if (bio->ptr == NULL) {
- va_end(args);
- return -1;
- }
- ret = vfprintf((XFILE)bio->ptr, format, args);
- break;
-
- case WOLFSSL_BIO_MEMORY:
- #if defined(OPENSSL_EXTRA) && !defined(_WIN32)
- case WOLFSSL_BIO_SSL:
- {
- int count;
- char* pt = NULL;
- va_list copy;
-
- va_copy(copy, args);
- count = vsnprintf(NULL, 0, format, args);
- if (count >= 0)
- {
- pt = (char*)XMALLOC(count + 1, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (pt != NULL)
- {
- count = vsnprintf(pt, count + 1, format, copy);
- if (count >= 0)
- {
- ret = wolfSSL_BIO_write(bio, pt, count);
- }
- XFREE(pt, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
- }
- va_end(copy);
- }
- break;
- #endif
-
- default:
- WOLFSSL_MSG("Unsupported WOLFSSL_BIO type for wolfSSL_BIO_printf");
- break;
- }
-
- return ret;
-}
-
-/* returns amount printed on success, negative in fail case */
-int wolfSSL_BIO_printf(WOLFSSL_BIO* bio, const char* format, ...)
-{
- int ret;
- va_list args;
- va_start(args, format);
-
- ret = wolfSSL_BIO_vprintf(bio, format, args);
-
- va_end(args);
-
- return ret;
-}
-
-#endif /* !defined(NO_FILESYSTEM) && defined (OPENSSL_EXTRA) */
-
-#if !defined(NO_FILESYSTEM) && defined(__clang__)
-#pragma clang diagnostic pop
-#endif
-
-#undef LINE_LEN
-#define LINE_LEN 16
-int wolfSSL_BIO_dump(WOLFSSL_BIO *bio, const char *buf, int length)
-{
- int ret = 0;
-
- if (bio == NULL)
- return 0;
-
-#ifndef NO_FILESYSTEM
- if (bio->type == WOLFSSL_BIO_FILE) {
- int i;
- char line[80];
-
- if (!buf) {
- return fputs("\tNULL", (XFILE)bio->ptr);
- }
-
- sprintf(line, "\t");
- for (i = 0; i < LINE_LEN; i++) {
- if (i < length)
- sprintf(line + 1 + i * 3,"%02x ", buf[i]);
- else
- sprintf(line + 1 + i * 3, " ");
- }
- sprintf(line + 1 + LINE_LEN * 3, "| ");
- for (i = 0; i < LINE_LEN; i++) {
- if (i < length) {
- sprintf(line + 3 + LINE_LEN * 3 + i,
- "%c", 31 < buf[i] && buf[i] < 127 ? buf[i] : '.');
- }
- }
- ret += fputs(line, (XFILE)bio->ptr);
-
- if (length > LINE_LEN)
- ret += wolfSSL_BIO_dump(bio, buf + LINE_LEN, length - LINE_LEN);
- }
-#else
- (void)buf;
- (void)length;
-#endif
-
- return ret;
-}
-
-#ifndef NO_ASN_TIME
-int wolfSSL_ASN1_UTCTIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_UTCTIME* a)
-{
- WOLFSSL_ENTER("ASN1_UTCTIME_print");
- if (bio == NULL || a == NULL) {
- return WOLFSSL_FAILURE;
- }
- if (a->type != ASN_UTC_TIME) {
- WOLFSSL_MSG("Error, not UTC_TIME");
- return WOLFSSL_FAILURE;
- }
-
- return wolfSSL_ASN1_TIME_print(bio, a);
-}
-
-/* Checks the ASN1 syntax of "a"
- * returns WOLFSSL_SUCCESS (1) if correct otherwise WOLFSSL_FAILURE (0) */
-int wolfSSL_ASN1_TIME_check(const WOLFSSL_ASN1_TIME* a)
-{
- char buf[MAX_TIME_STRING_SZ];
-
- WOLFSSL_ENTER("wolfSSL_ASN1_TIME_check");
-
- /* if can parse the WOLFSSL_ASN1_TIME passed in then consider syntax good */
- if (wolfSSL_ASN1_TIME_to_string((WOLFSSL_ASN1_TIME*)a, buf,
- MAX_TIME_STRING_SZ) == NULL) {
- return WOLFSSL_FAILURE;
- }
- return WOLFSSL_SUCCESS;
-}
-#endif /* !NO_ASN_TIME */
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_ASN1_TIME_diff(int *pday, int *psec,
- const WOLFSSL_ASN1_TIME *from, const WOLFSSL_ASN1_TIME *to)
-{
- WOLFSSL_STUB("wolfSSL_ASN1_TIME_diff");
- (void)pday;
- (void)psec;
- (void)from;
- (void)to;
- return 0;
-}
-
-WOLFSSL_API WOLFSSL_ASN1_TIME *wolfSSL_ASN1_TIME_set(WOLFSSL_ASN1_TIME *s, time_t t)
-{
- WOLFSSL_STUB("wolfSSL_ASN1_TIME_set");
- (void)s;
- (void)t;
- return s;
-}
-#endif /* !NO_WOLFSSL_STUB */
-
-/* Return the month as a string.
- *
- * n The number of the month as a two characters (1 based).
- * returns the month as a string.
- */
-static WC_INLINE const char* MonthStr(const char* n)
-{
- static const char monthStr[12][4] = {
- "Jan", "Feb", "Mar", "Apr", "May", "Jun",
- "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
- return monthStr[(n[0] - '0') * 10 + (n[1] - '0') - 1];
-}
-
-int wolfSSL_ASN1_GENERALIZEDTIME_print(WOLFSSL_BIO* bio,
- const WOLFSSL_ASN1_GENERALIZEDTIME* asnTime)
-{
- const char* p;
- WOLFSSL_ENTER("wolfSSL_ASN1_GENERALIZEDTIME_print");
-
- if (bio == NULL || asnTime == NULL)
- return BAD_FUNC_ARG;
-
- if (asnTime->type != ASN_GENERALIZED_TIME) {
- WOLFSSL_MSG("Error, not GENERALIZED_TIME");
- return WOLFSSL_FAILURE;
- }
- p = (const char *)(asnTime->data);
- /* GetTimeString not always available. */
- wolfSSL_BIO_write(bio, MonthStr(p + 4), 3);
- wolfSSL_BIO_write(bio, " ", 1);
- /* Day */
- wolfSSL_BIO_write(bio, p + 6, 2);
- wolfSSL_BIO_write(bio, " ", 1);
- /* Hour */
- wolfSSL_BIO_write(bio, p + 8, 2);
- wolfSSL_BIO_write(bio, ":", 1);
- /* Min */
- wolfSSL_BIO_write(bio, p + 10, 2);
- wolfSSL_BIO_write(bio, ":", 1);
- /* Secs */
- wolfSSL_BIO_write(bio, p + 12, 2);
- wolfSSL_BIO_write(bio, " ", 1);
- wolfSSL_BIO_write(bio, p, 4);
-
- return 0;
-}
-
-void wolfSSL_ASN1_GENERALIZEDTIME_free(WOLFSSL_ASN1_TIME* asn1Time)
-{
- WOLFSSL_ENTER("wolfSSL_ASN1_GENERALIZEDTIME_free");
- if (asn1Time == NULL)
- return;
- XMEMSET(asn1Time->data, 0, sizeof(asn1Time->data));
-}
-
-int wolfSSL_sk_num(WOLFSSL_STACK* sk)
-{
- WOLFSSL_ENTER("wolfSSL_sk_num");
- if (sk == NULL)
- return 0;
- return (int)sk->num;
-}
-
-void* wolfSSL_sk_value(WOLFSSL_STACK* sk, int i)
-{
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- int offset = i;
-#endif
- WOLFSSL_ENTER("wolfSSL_sk_value");
-
- for (; sk != NULL && i > 0; i--)
- sk = sk->next;
- if (sk == NULL)
- return NULL;
-
- switch (sk->type) {
- case STACK_TYPE_X509:
- return (void*)sk->data.x509;
- case STACK_TYPE_CIPHER:
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- sk->data.cipher.offset = offset;
- #endif
- return (void*)&sk->data.cipher;
- case STACK_TYPE_GEN_NAME:
- return (void*)sk->data.gn;
- case STACK_TYPE_ACCESS_DESCRIPTION:
- return (void*)sk->data.access;
- case STACK_TYPE_OBJ:
- return (void*)sk->data.obj;
- case STACK_TYPE_X509_EXT:
- return (void*)sk->data.ext;
- case STACK_TYPE_CONF_VALUE:
- return (void*)sk->data.conf->value;
- case STACK_TYPE_NULL:
- default:
- return (void*)sk->data.generic;
- }
-}
-
-/* Free the structure for ASN1_OBJECT stack */
-void wolfSSL_sk_free(WOLFSSL_STACK* sk)
-{
- WOLFSSL_ENTER("wolfSSL_sk_free");
-
- if (sk == NULL) {
- WOLFSSL_MSG("Error, BAD_FUNC_ARG");
- return;
- }
-
- switch (sk->type) {
- case STACK_TYPE_X509:
- wolfSSL_sk_X509_free(sk);
- break;
- #if defined(OPENSSL_ALL)
- case STACK_TYPE_CIPHER:
- wolfSSL_sk_CIPHER_free(sk);
- break;
- #endif
- case STACK_TYPE_GEN_NAME:
- wolfSSL_sk_GENERAL_NAME_free(sk);
- break;
- #if defined(OPENSSL_ALL) || defined (WOLFSSL_QT)
- case STACK_TYPE_ACCESS_DESCRIPTION:
- wolfSSL_sk_ACCESS_DESCRIPTION_free(sk);
- break;
- #endif
- case STACK_TYPE_OBJ:
- wolfSSL_sk_ASN1_OBJECT_free(sk);
- break;
- #ifdef OPENSSL_ALL
- case STACK_TYPE_X509_INFO:
- wolfSSL_sk_X509_INFO_free(sk);
- break;
- case STACK_TYPE_X509_NAME:
- wolfSSL_sk_X509_NAME_free(sk);
- break;
- case STACK_TYPE_CONF_VALUE:
- wolfSSL_sk_CONF_VALUE_free(sk);
- break;
- #endif
- case STACK_TYPE_NULL:
- default:
- wolfSSL_sk_GENERIC_free(sk);
- }
-}
-/* Frees each node in the stack and frees the stack.
- * Does not free any internal members of the stack nodes.
- */
-void wolfSSL_sk_GENERIC_pop_free(WOLFSSL_STACK* sk,
- void (*f) (void*))
-{
- WOLFSSL_STACK* node;
- WOLFSSL_STACK* tmp;
- WOLFSSL_ENTER("wolfSSL_sk_GENERIC_pop_free");
-
- if (sk == NULL)
- return;
-
- /* parse through stack freeing each node */
- node = sk->next;
- while (node) {
- tmp = node;
- node = node->next;
- if (f)
- f(tmp->data.generic);
- tmp->data.generic = NULL;
- XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-
- /* free head of stack */
- XFREE(sk, NULL, DYNAMIC_TYPE_ASN1);
-}
-
-/* return 1 on success 0 on fail */
-int wolfSSL_sk_GENERIC_push(WOLFSSL_STACK* sk, void* generic)
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_GENERIC_push");
-
- if (sk == NULL || generic == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* no previous values in stack */
- if (sk->data.generic == NULL) {
- sk->data.generic = generic;
- sk->num += 1;
- return WOLFSSL_SUCCESS;
- }
-
- /* stack already has value(s) create a new node and add more */
- node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK),NULL,DYNAMIC_TYPE_SSL);
- if (node == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
- XMEMSET(node, 0, sizeof(WOLFSSL_STACK));
-
- /* push new node onto head of stack */
- node->type = sk->type;
- node->data.generic = sk->data.generic;
- node->next = sk->next;
- sk->next = node;
- sk->data.generic = generic;
- sk->num += 1;
-
- return WOLFSSL_SUCCESS;
-}
-void wolfSSL_sk_GENERIC_free(WOLFSSL_STACK* sk)
-{
- wolfSSL_sk_GENERIC_pop_free(sk, NULL);
-}
-
-
-/* Free all nodes in a stack */
-void wolfSSL_sk_pop_free(WOLF_STACK_OF(WOLFSSL_ASN1_OBJECT)* sk,
- wolfSSL_sk_freefunc func)
-{
- WOLFSSL_ENTER("wolfSSL_sk_pop_free");
-
- if (sk == NULL) {
- WOLFSSL_MSG("Error, BAD_FUNC_ARG");
- return;
- }
-
- switch(sk->type) {
- #if defined(OPENSSL_ALL) || defined (WOLFSSL_QT)
- case STACK_TYPE_ACCESS_DESCRIPTION:
- wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(sk,
- wolfSSL_ACCESS_DESCRIPTION_free);
- break;
- #endif
- case STACK_TYPE_X509:
- wolfSSL_sk_X509_pop_free(sk,(void (*)(WOLFSSL_X509*))func);
- break;
- case STACK_TYPE_OBJ:
- wolfSSL_sk_ASN1_OBJECT_pop_free(sk,
- (void (*)(WOLFSSL_ASN1_OBJECT*))func);
- break;
- case STACK_TYPE_GEN_NAME:
- wolfSSL_sk_GENERAL_NAME_pop_free(sk,
- (void (*)(WOLFSSL_GENERAL_NAME*))func);
- break;
- #ifdef OPENSSL_ALL
- case STACK_TYPE_X509_NAME:
- wolfSSL_sk_X509_NAME_pop_free(sk,
- (void (*)(WOLFSSL_X509_NAME*))func);
- break;
- case STACK_TYPE_X509_EXT:
- wolfSSL_sk_X509_EXTENSION_pop_free(sk,
- (void (*)(WOLFSSL_X509_EXTENSION*))func);
- break;
- #endif
- #if defined(OPENSSL_ALL)
- case STACK_TYPE_X509_INFO:
- wolfSSL_sk_X509_INFO_pop_free(sk,
- (void (*)(WOLFSSL_X509_INFO*))func);
- break;
- #endif
- default:
- wolfSSL_sk_GENERIC_pop_free(sk,
- (void (*)(void*))func);
- break;
- }
-}
-
-#if defined(OPENSSL_ALL)
-/* Free the structure for WOLFSSL_CONF_VALUE stack
- *
- * sk stack to free nodes in
- */
-void wolfSSL_sk_CONF_VALUE_free(WOLF_STACK_OF(WOLFSSL_CONF_VALUE)* sk)
-{
- WOLFSSL_STACK* node;
- WOLFSSL_STACK* tmp;
- WOLFSSL_ENTER("wolfSSL_sk_CONF_VALUE_free");
-
- if (sk == NULL)
- return;
-
- /* parse through stack freeing each node */
- node = sk->next;
- while (node) {
- tmp = node;
- node = node->next;
- XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-
- /* free head of stack */
- XFREE(sk, NULL, DYNAMIC_TYPE_ASN1);
-}
-#endif
-
-/* Creates and returns a new null stack. */
-WOLFSSL_STACK* wolfSSL_sk_new_null(void)
-{
- WOLFSSL_STACK* sk;
- WOLFSSL_ENTER("wolfSSL_sk_new_null");
-
- sk = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (sk == NULL) {
- WOLFSSL_MSG("WOLFSSL_STACK memory error");
- return NULL;
- }
-
- XMEMSET(sk, 0, sizeof(WOLFSSL_STACK));
- sk->type = STACK_TYPE_NULL;
-
- return sk;
-}
-
-/* frees the wolfSSL_BASIC_CONSTRAINTS object */
-void wolfSSL_BASIC_CONSTRAINTS_free(WOLFSSL_BASIC_CONSTRAINTS *bc)
-{
- WOLFSSL_ENTER("wolfSSL_BASIC_CONSTRAINTS_free");
- if (bc == NULL) {
- WOLFSSL_MSG("Argument is NULL");
- return;
- }
- if (bc->pathlen) {
- wolfSSL_ASN1_INTEGER_free(bc->pathlen);
- }
- XFREE(bc, NULL, DYNAMIC_TYPE_OPENSSL);
-}
-
-/* frees the wolfSSL_AUTHORITY_KEYID object */
-void wolfSSL_AUTHORITY_KEYID_free(WOLFSSL_AUTHORITY_KEYID *id)
-{
- WOLFSSL_ENTER("wolfSSL_AUTHORITY_KEYID_free");
- if(id == NULL) {
- WOLFSSL_MSG("Argument is NULL");
- return;
- }
- if (id->keyid) {
- wolfSSL_ASN1_STRING_free(id->keyid);
- }
- if (id->issuer) {
- wolfSSL_ASN1_OBJECT_free(id->issuer);
- }
- if (id->serial) {
- wolfSSL_ASN1_INTEGER_free(id->serial);
- }
- XFREE(id, NULL, DYNAMIC_TYPE_OPENSSL);
-}
-
-int wolfSSL_sk_SSL_COMP_num(WOLF_STACK_OF(WOLFSSL_COMP)* sk)
-{
- if (sk == NULL)
- return 0;
- return (int)sk->num;
-}
-
-#endif /* OPENSSL_EXTRA */
-
-#if defined(OPENSSL_EXTRA) || defined(HAVE_EXT_CACHE)
-/* stunnel 4.28 needs
- *
- * Callback that is called if a session tries to resume but could not find
- * the session to resume it.
- */
-void wolfSSL_CTX_sess_set_get_cb(WOLFSSL_CTX* ctx,
- WOLFSSL_SESSION*(*f)(WOLFSSL*, unsigned char*, int, int*))
-{
- if (ctx == NULL)
- return;
-
-#ifdef HAVE_EXT_CACHE
- ctx->get_sess_cb = f;
-#else
- (void)f;
-#endif
-}
-
-void wolfSSL_CTX_sess_set_new_cb(WOLFSSL_CTX* ctx,
- int (*f)(WOLFSSL*, WOLFSSL_SESSION*))
-{
- if (ctx == NULL)
- return;
-
-#ifdef HAVE_EXT_CACHE
- ctx->new_sess_cb = f;
-#else
- (void)f;
-#endif
-}
-
-void wolfSSL_CTX_sess_set_remove_cb(WOLFSSL_CTX* ctx, void (*f)(WOLFSSL_CTX*,
- WOLFSSL_SESSION*))
-{
- if (ctx == NULL)
- return;
-
-#ifdef HAVE_EXT_CACHE
- ctx->rem_sess_cb = f;
-#else
- (void)f;
-#endif
-}
-#endif /* OPENSSL_EXTRA || HAVE_EXT_CACHE */
-
-#ifdef OPENSSL_EXTRA
-
-/*
- *
- * Note: It is expected that the importing and exporting function have been
- * built with the same settings. For example if session tickets was
- * enabled with the wolfSSL library exporting a session then it is
- * expected to be turned on with the wolfSSL library importing the session.
- */
-int wolfSSL_i2d_SSL_SESSION(WOLFSSL_SESSION* sess, unsigned char** p)
-{
- int size = 0;
-#ifdef HAVE_EXT_CACHE
- int idx = 0;
-#ifdef SESSION_CERTS
- int i;
-#endif
- unsigned char *data;
-
- if (sess == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* bornOn | timeout | sessionID len | sessionID | masterSecret | haveEMS */
- size += OPAQUE32_LEN + OPAQUE32_LEN + OPAQUE8_LEN + sess->sessionIDSz +
- SECRET_LEN + OPAQUE8_LEN;
-#ifdef SESSION_CERTS
- /* Peer chain */
- size += OPAQUE8_LEN;
- for (i = 0; i < sess->chain.count; i++)
- size += OPAQUE16_LEN + sess->chain.certs[i].length;
-#endif
-#if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
- defined(HAVE_SESSION_TICKET))
- /* Protocol version */
- size += OPAQUE16_LEN;
-#endif
-#if defined(SESSION_CERTS) || !defined(NO_RESUME_SUITE_CHECK) || \
- (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET))
- /* cipher suite */
- size += OPAQUE16_LEN;
-#endif
-#ifndef NO_CLIENT_CACHE
- /* ServerID len | ServerID */
- size += OPAQUE16_LEN + sess->idLen;
-#endif
-#ifdef OPENSSL_EXTRA
- /* session context ID len | session context ID */
- size += OPAQUE8_LEN + sess->sessionCtxSz;
-#endif
-#ifdef WOLFSSL_TLS13
- /* namedGroup */
- size += OPAQUE16_LEN;
-#endif
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
-#ifdef WOLFSSL_TLS13
- /* ticketSeen | ticketAdd */
- size += OPAQUE32_LEN + OPAQUE32_LEN;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- /* ticketNonce */
- size += OPAQUE8_LEN + sess->ticketNonce.len;
-#endif
-#endif
-#ifdef WOLFSSL_EARLY_DATA
- size += OPAQUE32_LEN;
-#endif
-#endif
-#ifdef HAVE_SESSION_TICKET
- /* ticket len | ticket */
- size += OPAQUE16_LEN + sess->ticketLen;
-#endif
-
- if (p != NULL) {
- if (*p == NULL)
- *p = (unsigned char*)XMALLOC(size, NULL, DYNAMIC_TYPE_OPENSSL);
- if (*p == NULL)
- return 0;
- data = *p;
-
- c32toa(sess->bornOn, data + idx); idx += OPAQUE32_LEN;
- c32toa(sess->timeout, data + idx); idx += OPAQUE32_LEN;
- data[idx++] = sess->sessionIDSz;
- XMEMCPY(data + idx, sess->sessionID, sess->sessionIDSz);
- idx += sess->sessionIDSz;
- XMEMCPY(data + idx, sess->masterSecret, SECRET_LEN); idx += SECRET_LEN;
- data[idx++] = (byte)sess->haveEMS;
-#ifdef SESSION_CERTS
- data[idx++] = (byte)sess->chain.count;
- for (i = 0; i < sess->chain.count; i++) {
- c16toa((word16)sess->chain.certs[i].length, data + idx);
- idx += OPAQUE16_LEN;
- XMEMCPY(data + idx, sess->chain.certs[i].buffer,
- sess->chain.certs[i].length);
- idx += sess->chain.certs[i].length;
- }
-#endif
-#if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
- defined(HAVE_SESSION_TICKET))
- data[idx++] = sess->version.major;
- data[idx++] = sess->version.minor;
-#endif
-#if defined(SESSION_CERTS) || !defined(NO_RESUME_SUITE_CHECK) || \
- (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET))
- data[idx++] = sess->cipherSuite0;
- data[idx++] = sess->cipherSuite;
-#endif
-#ifndef NO_CLIENT_CACHE
- c16toa(sess->idLen, data + idx); idx += OPAQUE16_LEN;
- XMEMCPY(data + idx, sess->serverID, sess->idLen);
- idx += sess->idLen;
-#endif
-#ifdef OPENSSL_EXTRA
- data[idx++] = sess->sessionCtxSz;
- XMEMCPY(data + idx, sess->sessionCtx, sess->sessionCtxSz);
- idx += sess->sessionCtxSz;
-#endif
-#ifdef WOLFSSL_TLS13
- c16toa(sess->namedGroup, data + idx);
- idx += OPAQUE16_LEN;
-#endif
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
-#ifdef WOLFSSL_TLS13
- c32toa(sess->ticketSeen, data + idx);
- idx += OPAQUE32_LEN;
- c32toa(sess->ticketAdd, data + idx);
- idx += OPAQUE32_LEN;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- data[idx++] = sess->ticketNonce.len;
- XMEMCPY(data + idx, sess->ticketNonce.data, sess->ticketNonce.len);
- idx += sess->ticketNonce.len;
-#endif
-#endif
-#ifdef WOLFSSL_EARLY_DATA
- c32toa(sess->maxEarlyDataSz, data + idx);
- idx += OPAQUE32_LEN;
-#endif
-#endif
-#ifdef HAVE_SESSION_TICKET
- c16toa(sess->ticketLen, data + idx); idx += OPAQUE16_LEN;
- XMEMCPY(data + idx, sess->ticket, sess->ticketLen);
- idx += sess->ticketLen;
-#endif
- }
-#endif
-
- (void)sess;
- (void)p;
-#ifdef HAVE_EXT_CACHE
- (void)idx;
-#endif
-
- return size;
-}
-
-
-/* TODO: no function to free new session.
- *
- * Note: It is expected that the importing and exporting function have been
- * built with the same settings. For example if session tickets was
- * enabled with the wolfSSL library exporting a session then it is
- * expected to be turned on with the wolfSSL library importing the session.
- */
-WOLFSSL_SESSION* wolfSSL_d2i_SSL_SESSION(WOLFSSL_SESSION** sess,
- const unsigned char** p, long i)
-{
- WOLFSSL_SESSION* s = NULL;
- int ret = 0;
-#if defined(HAVE_EXT_CACHE)
- int idx;
- byte* data;
-#ifdef SESSION_CERTS
- int j;
- word16 length;
-#endif
-#endif
-
- (void)p;
- (void)i;
- (void)ret;
-
- if (sess != NULL)
- s = *sess;
-
-#ifdef HAVE_EXT_CACHE
- if (p == NULL || *p == NULL)
- return NULL;
-
- if (s == NULL) {
- s = (WOLFSSL_SESSION*)XMALLOC(sizeof(WOLFSSL_SESSION), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (s == NULL)
- return NULL;
- XMEMSET(s, 0, sizeof(WOLFSSL_SESSION));
- s->isAlloced = 1;
-#ifdef HAVE_SESSION_TICKET
- s->isDynamic = 0;
-#endif
- }
-
- idx = 0;
- data = (byte*)*p;
-
- /* bornOn | timeout | sessionID len */
- if (i < OPAQUE32_LEN + OPAQUE32_LEN + OPAQUE8_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- ato32(data + idx, &s->bornOn); idx += OPAQUE32_LEN;
- ato32(data + idx, &s->timeout); idx += OPAQUE32_LEN;
- s->sessionIDSz = data[idx++];
-
- /* sessionID | secret | haveEMS */
- if (i - idx < s->sessionIDSz + SECRET_LEN + OPAQUE8_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- XMEMCPY(s->sessionID, data + idx, s->sessionIDSz);
- idx += s->sessionIDSz;
- XMEMCPY(s->masterSecret, data + idx, SECRET_LEN); idx += SECRET_LEN;
- s->haveEMS = data[idx++];
-
-#ifdef SESSION_CERTS
- /* Certificate chain */
- if (i - idx == 0) {
- ret = BUFFER_ERROR;
- goto end;
- }
- s->chain.count = data[idx++];
- for (j = 0; j < s->chain.count; j++) {
- if (i - idx < OPAQUE16_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- ato16(data + idx, &length); idx += OPAQUE16_LEN;
- s->chain.certs[j].length = length;
- if (i - idx < length) {
- ret = BUFFER_ERROR;
- goto end;
- }
- XMEMCPY(s->chain.certs[j].buffer, data + idx, length);
- idx += length;
- }
-#endif
-#if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
- defined(HAVE_SESSION_TICKET))
- /* Protocol Version */
- if (i - idx < OPAQUE16_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- s->version.major = data[idx++];
- s->version.minor = data[idx++];
-#endif
-#if defined(SESSION_CERTS) || !defined(NO_RESUME_SUITE_CHECK) || \
- (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET))
- /* Cipher suite */
- if (i - idx < OPAQUE16_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- s->cipherSuite0 = data[idx++];
- s->cipherSuite = data[idx++];
-#endif
-#ifndef NO_CLIENT_CACHE
- /* ServerID len */
- if (i - idx < OPAQUE16_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- ato16(data + idx, &s->idLen); idx += OPAQUE16_LEN;
-
- /* ServerID */
- if (i - idx < s->idLen) {
- ret = BUFFER_ERROR;
- goto end;
- }
- XMEMCPY(s->serverID, data + idx, s->idLen); idx += s->idLen;
-#endif
-#ifdef OPENSSL_EXTRA
- /* byte for length of session context ID */
- if (i - idx < OPAQUE8_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- s->sessionCtxSz = data[idx++];
-
- /* app session context ID */
- if (i - idx < s->sessionCtxSz) {
- ret = BUFFER_ERROR;
- goto end;
- }
- XMEMCPY(s->sessionCtx, data + idx, s->sessionCtxSz); idx += s->sessionCtxSz;
-#endif
-#ifdef WOLFSSL_TLS13
- if (i - idx < OPAQUE16_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- ato16(data + idx, &s->namedGroup);
- idx += OPAQUE16_LEN;
-#endif
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
-#ifdef WOLFSSL_TLS13
- if (i - idx < (OPAQUE32_LEN * 2)) {
- ret = BUFFER_ERROR;
- goto end;
- }
- ato32(data + idx, &s->ticketSeen);
- idx += OPAQUE32_LEN;
- ato32(data + idx, &s->ticketAdd);
- idx += OPAQUE32_LEN;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- if (i - idx < OPAQUE8_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- s->ticketNonce.len = data[idx++];
-
- if (i - idx < s->ticketNonce.len) {
- ret = BUFFER_ERROR;
- goto end;
- }
- XMEMCPY(s->ticketNonce.data, data + idx, s->ticketNonce.len);
- idx += s->ticketNonce.len;
-#endif
-#endif
-#ifdef WOLFSSL_EARLY_DATA
- if (i - idx < OPAQUE32_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- ato32(data + idx, &s->maxEarlyDataSz);
- idx += OPAQUE32_LEN;
-#endif
-#endif
-#ifdef HAVE_SESSION_TICKET
- /* ticket len */
- if (i - idx < OPAQUE16_LEN) {
- ret = BUFFER_ERROR;
- goto end;
- }
- ato16(data + idx, &s->ticketLen); idx += OPAQUE16_LEN;
-
- /* Dispose of ol dynamic ticket and ensure space for new ticket. */
- if (s->isDynamic)
- XFREE(s->ticket, NULL, DYNAMIC_TYPE_SESSION_TICK);
- if (s->ticketLen <= SESSION_TICKET_LEN)
- s->ticket = s->staticTicket;
- else {
- s->ticket = (byte*)XMALLOC(s->ticketLen, NULL,
- DYNAMIC_TYPE_SESSION_TICK);
- if (s->ticket == NULL) {
- ret = MEMORY_ERROR;
- goto end;
- }
- s->isDynamic = 1;
- }
-
- /* ticket */
- if (i - idx < s->ticketLen) {
- ret = BUFFER_ERROR;
- goto end;
- }
- XMEMCPY(s->ticket, data + idx, s->ticketLen); idx += s->ticketLen;
-#endif
- (void)idx;
-
- if (sess != NULL)
- *sess = s;
-
- *p += idx;
-
-end:
- if (ret != 0 && (sess == NULL || *sess != s))
- wolfSSL_SESSION_free(s);
-#endif
- return s;
-}
-
-
-long wolfSSL_SESSION_get_timeout(const WOLFSSL_SESSION* sess)
-{
- long timeout = 0;
- WOLFSSL_ENTER("wolfSSL_SESSION_get_timeout");
- if (sess)
- timeout = sess->timeout;
- return timeout;
-}
-
-
-long wolfSSL_SESSION_get_time(const WOLFSSL_SESSION* sess)
-{
- long bornOn = 0;
- WOLFSSL_ENTER("wolfSSL_SESSION_get_time");
- if (sess)
- bornOn = sess->bornOn;
- return bornOn;
-}
-
-
-#endif /* OPENSSL_EXTRA */
-
-
-#ifdef KEEP_PEER_CERT
-char* wolfSSL_X509_get_subjectCN(WOLFSSL_X509* x509)
-{
- if (x509 == NULL)
- return NULL;
-
- return x509->subjectCN;
-}
-#endif /* KEEP_PEER_CERT */
-
-#ifdef OPENSSL_EXTRA
-
-#if defined(FORTRESS) && !defined(NO_FILESYSTEM)
-int wolfSSL_cmp_peer_cert_to_file(WOLFSSL* ssl, const char *fname)
-{
- int ret = WOLFSSL_FATAL_ERROR;
-
- WOLFSSL_ENTER("wolfSSL_cmp_peer_cert_to_file");
- if (ssl != NULL && fname != NULL)
- {
- #ifdef WOLFSSL_SMALL_STACK
- byte staticBuffer[1]; /* force heap usage */
- #else
- byte staticBuffer[FILE_BUFFER_SIZE];
- #endif
- byte* myBuffer = staticBuffer;
- int dynamic = 0;
- XFILE file;
- long sz = 0;
- WOLFSSL_CTX* ctx = ssl->ctx;
- WOLFSSL_X509* peer_cert = &ssl->peerCert;
- DerBuffer* fileDer = NULL;
-
- file = XFOPEN(fname, "rb");
- if (file == XBADFILE)
- return WOLFSSL_BAD_FILE;
-
- if (XFSEEK(file, 0, XSEEK_END) != 0) {
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
- sz = XFTELL(file);
- XREWIND(file);
-
- if (sz > MAX_WOLFSSL_FILE_SIZE || sz < 0) {
- WOLFSSL_MSG("cmp_peer_cert_to_file size error");
- XFCLOSE(file);
- return WOLFSSL_BAD_FILE;
- }
-
- if (sz > (long)sizeof(staticBuffer)) {
- WOLFSSL_MSG("Getting dynamic buffer");
- myBuffer = (byte*)XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
- dynamic = 1;
- }
-
- if ((myBuffer != NULL) &&
- (sz > 0) &&
- (XFREAD(myBuffer, 1, sz, file) == (size_t)sz) &&
- (PemToDer(myBuffer, (long)sz, CERT_TYPE,
- &fileDer, ctx->heap, NULL, NULL) == 0) &&
- (fileDer->length != 0) &&
- (fileDer->length == peer_cert->derCert->length) &&
- (XMEMCMP(peer_cert->derCert->buffer, fileDer->buffer,
- fileDer->length) == 0))
- {
- ret = 0;
- }
-
- FreeDer(&fileDer);
-
- if (dynamic)
- XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
-
- XFCLOSE(file);
- }
-
- return ret;
-}
-#endif
-#endif /* OPENSSL_EXTRA */
-#endif /* !WOLFCRYPT_ONLY */
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
-const WOLFSSL_ObjectInfo wolfssl_object_info[] = {
-#ifndef NO_CERTS
- /* oidCertExtType */
- { NID_basic_constraints, BASIC_CA_OID, oidCertExtType, "basicConstraints",
- "X509v3 Basic Constraints"},
- { NID_subject_alt_name, ALT_NAMES_OID, oidCertExtType, "subjectAltName",
- "X509v3 Subject Alternative Name"},
- { CRL_DIST_OID, CRL_DIST_OID, oidCertExtType, "crlDistributionPoints",
- "X509v3 CRL Distribution Points"},
- { NID_info_access, AUTH_INFO_OID, oidCertExtType, "authorityInfoAccess",
- "Authority Information Access"},
- { NID_authority_key_identifier, AUTH_KEY_OID, oidCertExtType,
- "authorityKeyIdentifier", "X509v3 Authority Key Identifier"},
- { NID_subject_key_identifier, SUBJ_KEY_OID, oidCertExtType,
- "subjectKeyIdentifier", "X509v3 Subject Key Identifier"},
- { NID_key_usage, KEY_USAGE_OID, oidCertExtType, "keyUsage",
- "X509v3 Key Usage"},
- { NID_inhibit_any_policy, INHIBIT_ANY_OID, oidCertExtType,
- "inhibitAnyPolicy", "X509v3 Inhibit Any Policy"},
- { NID_ext_key_usage, KEY_USAGE_OID, oidCertExtType,
- "extendedKeyUsage", "X509v3 Extended Key Usage"},
- { NID_name_constraints, NAME_CONS_OID, oidCertExtType,
- "nameConstraints", "X509v3 Name Constraints"},
- { NID_certificate_policies, CERT_POLICY_OID, oidCertExtType,
- "certificatePolicies", "X509v3 Certificate Policies"},
-
- /* oidCertAuthInfoType */
- { AIA_OCSP_OID, AIA_OCSP_OID, oidCertAuthInfoType, "authorityInfoAccess",
- "Authority Information Access"},
- { AIA_CA_ISSUER_OID, AIA_CA_ISSUER_OID, oidCertAuthInfoType,
- "caIssuers", "CA Issuers"},
-
- /* oidCertPolicyType */
- { NID_any_policy, CP_ANY_OID, oidCertPolicyType, "anyPolicy",
- "X509v3 Any Policy"},
-
- /* oidCertAltNameType */
- { NID_hw_name_oid, HW_NAME_OID, oidCertAltNameType, "Hardware name",""},
-
- /* oidCertKeyUseType */
- { NID_anyExtendedKeyUsage, EKU_ANY_OID, oidCertKeyUseType,
- "anyExtendedKeyUsage", "Any Extended Key Usage"},
- { EKU_SERVER_AUTH_OID, EKU_SERVER_AUTH_OID, oidCertKeyUseType,
- "serverAuth", "TLS Web Server Authentication"},
- { EKU_CLIENT_AUTH_OID, EKU_CLIENT_AUTH_OID, oidCertKeyUseType,
- "clientAuth", "TLS Web Client Authentication"},
- { EKU_OCSP_SIGN_OID, EKU_OCSP_SIGN_OID, oidCertKeyUseType,
- "OCSPSigning", "OCSP Signing"},
-
- /* oidCertNameType */
- { NID_commonName, NID_commonName, oidCertNameType, "CN", "commonName"},
- { NID_surname, NID_surname, oidCertNameType, "SN", "surname"},
- { NID_serialNumber, NID_serialNumber, oidCertNameType, "serialNumber",
- "serialNumber"},
- { NID_countryName, NID_countryName, oidCertNameType, "C", "countryName"},
- { NID_localityName, NID_localityName, oidCertNameType, "L", "localityName"},
- { NID_stateOrProvinceName, NID_stateOrProvinceName, oidCertNameType, "ST",
- "stateOrProvinceName"},
- { NID_organizationName, NID_organizationName, oidCertNameType, "O",
- "organizationName"},
- { NID_organizationalUnitName, NID_organizationalUnitName, oidCertNameType,
- "OU", "organizationalUnitName"},
- { NID_emailAddress, NID_emailAddress, oidCertNameType, "emailAddress",
- "emailAddress"},
- { NID_domainComponent, NID_domainComponent, oidCertNameType, "DC",
- "domainComponent"},
- { NID_businessCategory, NID_businessCategory, oidCertNameType, "businessCategory",
- "businessCategory"},
- { NID_jurisdictionCountryName, NID_jurisdictionCountryName, oidCertNameType, "jurisdictionC",
- "jurisdictionCountryName"},
- { NID_jurisdictionStateOrProvinceName, NID_jurisdictionStateOrProvinceName,
- oidCertNameType, "jurisdictionST", "jurisdictionStateOrProvinceName"},
-#endif
-#ifdef OPENSSL_EXTRA /* OPENSSL_EXTRA_X509_SMALL only needs the above */
- /* oidHashType */
- #ifdef WOLFSSL_MD2
- { NID_md2, MD2h, oidHashType, "MD2", "md2"},
- #endif
- #ifdef WOLFSSL_MD5
- { NID_md5, MD5h, oidHashType, "MD5", "md5"},
- #endif
- #ifndef NO_SHA
- { NID_sha1, SHAh, oidHashType, "SHA1", "sha1"},
- #endif
- #ifdef WOLFSSL_SHA224
- { NID_sha224, SHA224h, oidHashType, "SHA224", "sha224"},
- #endif
- #ifndef NO_SHA256
- { NID_sha256, SHA256h, oidHashType, "SHA256", "sha256"},
- #endif
- #ifdef WOLFSSL_SHA384
- { NID_sha384, SHA384h, oidHashType, "SHA384", "sha384"},
- #endif
- #ifdef WOLFSSL_SHA512
- { NID_sha512, SHA512h, oidHashType, "SHA512", "sha512"},
- #endif
-
- /* oidSigType */
- #ifndef NO_DSA
- #ifndef NO_SHA
- { CTC_SHAwDSA, CTC_SHAwDSA, oidSigType, "DSA-SHA1", "dsaWithSHA1"},
- #endif
- #endif /* NO_DSA */
- #ifndef NO_RSA
- #ifdef WOLFSSL_MD2
- { CTC_MD2wRSA, CTC_MD2wRSA, oidSigType, "RSA-MD2",
- "md2WithRSAEncryption"},
- #endif
- #ifndef NO_MD5
- { CTC_MD5wRSA, CTC_MD5wRSA, oidSigType, "RSA-MD5",
- "md5WithRSAEncryption"},
- #endif
- #ifndef NO_SHA
- { CTC_SHAwRSA, CTC_SHAwRSA, oidSigType, "RSA-SHA1",
- "sha1WithRSAEncryption"},
- #endif
- #ifdef WOLFSSL_SHA224
- { CTC_SHA224wRSA, CTC_SHA224wRSA, oidSigType, "RSA-SHA224",
- "sha224WithRSAEncryption"},
- #endif
- #ifndef NO_SHA256
- { CTC_SHA256wRSA, CTC_SHA256wRSA, oidSigType, "RSA-SHA256",
- "sha256WithRSAEncryption"},
- #endif
- #ifdef WOLFSSL_SHA384
- { CTC_SHA384wRSA, CTC_SHA384wRSA, oidSigType, "RSA-SHA384",
- "sha384WithRSAEncryption"},
- #endif
- #ifdef WOLFSSL_SHA512
- { CTC_SHA512wRSA, CTC_SHA512wRSA, oidSigType, "RSA-SHA512",
- "sha512WithRSAEncryption"},
- #endif
- #endif /* NO_RSA */
- #ifdef HAVE_ECC
- #ifndef NO_SHA
- { CTC_SHAwECDSA, CTC_SHAwECDSA, oidSigType, "ecdsa-with-SHA1", "shaWithECDSA"},
- #endif
- #ifdef WOLFSSL_SHA224
- { CTC_SHA224wECDSA, CTC_SHA224wECDSA, oidSigType, "ecdsa-with-SHA224","sha224WithECDSA"},
- #endif
- #ifndef NO_SHA256
- { CTC_SHA256wECDSA, CTC_SHA256wECDSA, oidSigType, "ecdsa-with-SHA256","sha256WithECDSA"},
- #endif
- #ifdef WOLFSSL_SHA384
- { CTC_SHA384wECDSA, CTC_SHA384wECDSA, oidSigType, "ecdsa-with-SHA384","sha384WithECDSA"},
- #endif
- #ifdef WOLFSSL_SHA512
- { CTC_SHA512wECDSA, CTC_SHA512wECDSA, oidSigType, "ecdsa-with-SHA512","sha512WithECDSA"},
- #endif
- #endif /* HAVE_ECC */
-
- /* oidKeyType */
- #ifndef NO_DSA
- { DSAk, DSAk, oidKeyType, "DSA", "dsaEncryption"},
- { NID_dsa, DSAk, oidKeyType, "DSA", "dsaEncryption"},
- #endif /* NO_DSA */
- #ifndef NO_RSA
- { RSAk, RSAk, oidKeyType, "RSA", "rsaEncryption"},
- { NID_rsaEncryption, RSAk, oidKeyType, "RSA", "rsaEncryption"},
- #endif /* NO_RSA */
- #ifdef HAVE_NTRU
- { NTRUk, NTRUk, oidKeyType, "NTRU", "ntruEncryption"},
- #endif /* HAVE_NTRU */
- #ifdef HAVE_ECC
- { ECDSAk, ECDSAk, oidKeyType, "ECDSA", "ecdsaEncryption"},
- { NID_X9_62_id_ecPublicKey, ECDSAk, oidKeyType, "id-ecPublicKey",
- "id-ecPublicKey"},
- #endif /* HAVE_ECC */
- #ifndef NO_DH
- { NID_dhKeyAgreement, DHk, oidKeyType, "dhKeyAgreement", "dhKeyAgreement"},
- #endif
-
- /* oidCurveType */
- #ifdef HAVE_ECC
- { NID_X9_62_prime192v1, ECC_SECP192R1_OID, oidCurveType, "prime192v1", "prime192v1"},
- { NID_X9_62_prime192v2, ECC_PRIME192V2_OID, oidCurveType, "prime192v2", "prime192v2"},
- { NID_X9_62_prime192v3, ECC_PRIME192V3_OID, oidCurveType, "prime192v3", "prime192v3"},
-
- { NID_X9_62_prime239v1, ECC_PRIME239V1_OID, oidCurveType, "prime239v1", "prime239v1"},
- { NID_X9_62_prime239v2, ECC_PRIME239V2_OID, oidCurveType, "prime239v2", "prime239v2"},
- { NID_X9_62_prime239v3, ECC_PRIME239V3_OID, oidCurveType, "prime239v3", "prime239v3"},
-
- { NID_X9_62_prime256v1, ECC_SECP256R1_OID, oidCurveType, "prime256v1", "prime256v1"},
-
- { NID_secp112r1, ECC_SECP112R1_OID, oidCurveType, "secp112r1", "secp112r1"},
- { NID_secp112r2, ECC_SECP112R2_OID, oidCurveType, "secp112r2", "secp112r2"},
-
- { NID_secp128r1, ECC_SECP128R1_OID, oidCurveType, "secp128r1", "secp128r1"},
- { NID_secp128r2, ECC_SECP128R2_OID, oidCurveType, "secp128r2", "secp128r2"},
-
- { NID_secp160r1, ECC_SECP160R1_OID, oidCurveType, "secp160r1", "secp160r1"},
- { NID_secp160r2, ECC_SECP160R2_OID, oidCurveType, "secp160r2", "secp160r2"},
-
- { NID_secp224r1, ECC_SECP224R1_OID, oidCurveType, "secp224r1", "secp224r1"},
- { NID_secp384r1, ECC_SECP384R1_OID, oidCurveType, "secp384r1", "secp384r1"},
- { NID_secp521r1, ECC_SECP521R1_OID, oidCurveType, "secp521r1", "secp521r1"},
-
- { NID_secp160k1, ECC_SECP160K1_OID, oidCurveType, "secp160k1", "secp160k1"},
- { NID_secp192k1, ECC_SECP192K1_OID, oidCurveType, "secp192k1", "secp192k1"},
- { NID_secp224k1, ECC_SECP224K1_OID, oidCurveType, "secp224k1", "secp224k1"},
- { NID_secp256k1, ECC_SECP256K1_OID, oidCurveType, "secp256k1", "secp256k1"},
-
- { NID_brainpoolP160r1, ECC_BRAINPOOLP160R1_OID, oidCurveType, "brainpoolP160r1", "brainpoolP160r1"},
- { NID_brainpoolP192r1, ECC_BRAINPOOLP192R1_OID, oidCurveType, "brainpoolP192r1", "brainpoolP192r1"},
- { NID_brainpoolP224r1, ECC_BRAINPOOLP224R1_OID, oidCurveType, "brainpoolP224r1", "brainpoolP224r1"},
- { NID_brainpoolP256r1, ECC_BRAINPOOLP256R1_OID, oidCurveType, "brainpoolP256r1", "brainpoolP256r1"},
- { NID_brainpoolP320r1, ECC_BRAINPOOLP320R1_OID, oidCurveType, "brainpoolP320r1", "brainpoolP320r1"},
- { NID_brainpoolP384r1, ECC_BRAINPOOLP384R1_OID, oidCurveType, "brainpoolP384r1", "brainpoolP384r1"},
- { NID_brainpoolP512r1, ECC_BRAINPOOLP512R1_OID, oidCurveType, "brainpoolP512r1", "brainpoolP512r1"},
- #endif /* HAVE_ECC */
-
- /* oidBlkType */
- #ifdef WOLFSSL_AES_128
- { AES128CBCb, AES128CBCb, oidBlkType, "AES-128-CBC", "aes-128-cbc"},
- #endif
- #ifdef WOLFSSL_AES_192
- { AES192CBCb, AES192CBCb, oidBlkType, "AES-192-CBC", "aes-192-cbc"},
- #endif
- #ifdef WOLFSSL_AES_256
- { AES256CBCb, AES256CBCb, oidBlkType, "AES-256-CBC", "aes-256-cbc"},
- #endif
- #ifndef NO_DES3
- { NID_des, DESb, oidBlkType, "DES-CBC", "des-cbc"},
- { NID_des3, DES3b, oidBlkType, "DES-EDE3-CBC", "des-ede3-cbc"},
- #endif /* !NO_DES3 */
-
- /* oidOcspType */
- #ifdef HAVE_OCSP
- { NID_id_pkix_OCSP_basic, OCSP_BASIC_OID, oidOcspType, "basicOCSPResponse",
- "Basic OCSP Response"},
- { OCSP_NONCE_OID, OCSP_NONCE_OID, oidOcspType, "Nonce",
- "OCSP Nonce"},
- #endif /* HAVE_OCSP */
-
- #ifndef NO_PWDBASED
- /* oidKdfType */
- { PBKDF2_OID, PBKDF2_OID, oidKdfType, "PBKDFv2", "PBKDF2"},
-
- /* oidPBEType */
- { PBE_SHA1_RC4_128, PBE_SHA1_RC4_128, oidPBEType,
- "PBE-SHA1-RC4-128", "pbeWithSHA1And128BitRC4"},
- { PBE_SHA1_DES, PBE_SHA1_DES, oidPBEType, "PBE-SHA1-DES",
- "pbeWithSHA1AndDES-CBC"},
- { PBE_SHA1_DES3, PBE_SHA1_DES3, oidPBEType, "PBE-SHA1-3DES",
- "pbeWithSHA1And3-KeyTripleDES-CBC"},
- #endif
-
- /* oidKeyWrapType */
- #ifdef WOLFSSL_AES_128
- { AES128_WRAP, AES128_WRAP, oidKeyWrapType, "AES-128 wrap", "aes128-wrap"},
- #endif
- #ifdef WOLFSSL_AES_192
- { AES192_WRAP, AES192_WRAP, oidKeyWrapType, "AES-192 wrap", "aes192-wrap"},
- #endif
- #ifdef WOLFSSL_AES_256
- { AES256_WRAP, AES256_WRAP, oidKeyWrapType, "AES-256 wrap", "aes256-wrap"},
- #endif
-
- #ifndef NO_PKCS7
- #ifndef NO_DH
- /* oidCmsKeyAgreeType */
- #ifndef NO_SHA
- { dhSinglePass_stdDH_sha1kdf_scheme, dhSinglePass_stdDH_sha1kdf_scheme,
- oidCmsKeyAgreeType, "dhSinglePass-stdDH-sha1kdf-scheme", "dhSinglePass-stdDH-sha1kdf-scheme"},
- #endif
- #ifdef WOLFSSL_SHA224
- { dhSinglePass_stdDH_sha224kdf_scheme,
- dhSinglePass_stdDH_sha224kdf_scheme, oidCmsKeyAgreeType,
- "dhSinglePass-stdDH-sha224kdf-scheme", "dhSinglePass-stdDH-sha224kdf-scheme"},
- #endif
- #ifndef NO_SHA256
- { dhSinglePass_stdDH_sha256kdf_scheme,
- dhSinglePass_stdDH_sha256kdf_scheme, oidCmsKeyAgreeType,
- "dhSinglePass-stdDH-sha256kdf-scheme", "dhSinglePass-stdDH-sha256kdf-scheme"},
- #endif
- #ifdef WOLFSSL_SHA384
- { dhSinglePass_stdDH_sha384kdf_scheme,
- dhSinglePass_stdDH_sha384kdf_scheme, oidCmsKeyAgreeType,
- "dhSinglePass-stdDH-sha384kdf-scheme", "dhSinglePass-stdDH-sha384kdf-scheme"},
- #endif
- #ifdef WOLFSSL_SHA512
- { dhSinglePass_stdDH_sha512kdf_scheme,
- dhSinglePass_stdDH_sha512kdf_scheme, oidCmsKeyAgreeType,
- "dhSinglePass-stdDH-sha512kdf-scheme", "dhSinglePass-stdDH-sha512kdf-scheme"},
- #endif
- #endif
- #endif
- #if defined(WOLFSSL_APACHE_HTTPD)
- /* "1.3.6.1.5.5.7.8.7" */
- { NID_id_on_dnsSRV, NID_id_on_dnsSRV, oidCertNameType,
- WOLFSSL_SN_DNS_SRV, WOLFSSL_LN_DNS_SRV },
-
- /* "1.3.6.1.4.1.311.20.2.3" */
- { NID_ms_upn, WOLFSSL_MS_UPN_SUM, oidCertExtType, WOLFSSL_SN_MS_UPN,
- WOLFSSL_LN_MS_UPN },
-
- /* "1.3.6.1.5.5.7.1.24" */
- { NID_tlsfeature, WOLFSSL_TLS_FEATURE_SUM, oidTlsExtType,
- WOLFSSL_SN_TLS_FEATURE, WOLFSSL_LN_TLS_FEATURE },
- #endif
-#endif /* OPENSSL_EXTRA */
-};
-
-#define WOLFSSL_OBJECT_INFO_SZ \
- (sizeof(wolfssl_object_info) / sizeof(*wolfssl_object_info))
-const size_t wolfssl_object_info_sz = WOLFSSL_OBJECT_INFO_SZ;
-#endif
-#if defined(OPENSSL_EXTRA) || \
- (defined(OPENSSL_EXTRA_X509_SMALL) && !defined(NO_RSA))
-static WC_RNG globalRNG;
-static int initGlobalRNG = 0;
-#endif
-#if defined(OPENSSL_EXTRA) && \
- !defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
-WC_RNG* WOLFSSL_RSA_GetRNG(WOLFSSL_RSA *rsa, WC_RNG **tmpRNG, int *initTmpRng)
-{
- WC_RNG* rng = NULL;
-
- if (!rsa || !initTmpRng) {
- return NULL;
- }
- *initTmpRng = 0;
-
-#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && \
- !defined(HAVE_FAST_RSA) && defined(WC_RSA_BLINDING)
- rng = ((RsaKey*)rsa->internal)->rng;
-#endif
- if (rng == NULL && tmpRNG) {
- if (!*tmpRNG) {
-#ifdef WOLFSSL_SMALL_STACK
- *tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (*tmpRNG == NULL)
- return NULL;
-#else
- WOLFSSL_MSG("*tmpRNG is null");
- return NULL;
-#endif
- }
-
- if (wc_InitRng(*tmpRNG) == 0) {
- rng = *tmpRNG;
- *initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
-#ifdef WOLFSSL_SMALL_STACK
- if (*tmpRNG)
- XFREE(*tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- *tmpRNG = NULL;
-#endif
- }
- }
- return rng;
-}
-#endif
-#ifndef WOLFCRYPT_ONLY
-
-#ifdef OPENSSL_EXTRA
-
-/* Not thread safe! Can be called multiple times.
- * Checks if the global RNG has been created. If not then one is created.
- *
- * Returns SSL_SUCCESS when no error is encountered.
- */
-static int wolfSSL_RAND_Init(void)
-{
- if (initGlobalRNG == 0) {
- if (wc_InitRng(&globalRNG) < 0) {
- WOLFSSL_MSG("wolfSSL Init Global RNG failed");
- return 0;
- }
- initGlobalRNG = 1;
- }
-
- return SSL_SUCCESS;
-}
-
-
-/* SSL_SUCCESS on ok */
-int wolfSSL_RAND_seed(const void* seed, int len)
-{
-
- WOLFSSL_MSG("wolfSSL_RAND_seed");
-
- (void)seed;
- (void)len;
-
- return wolfSSL_RAND_Init();
-}
-
-
-/* Returns the path for reading seed data from.
- * Uses the env variable $RANDFILE first if set, if not then used $HOME/.rnd
- *
- * Note uses stdlib by default unless XGETENV macro is overwritten
- *
- * fname buffer to hold path
- * len length of fname buffer
- *
- * Returns a pointer to fname on success and NULL on failure
- */
-const char* wolfSSL_RAND_file_name(char* fname, unsigned long len)
-{
-#ifndef NO_FILESYSTEM
- char* rt;
- char ap[] = "/.rnd";
-
- WOLFSSL_ENTER("wolfSSL_RAND_file_name");
-
- if (fname == NULL) {
- return NULL;
- }
-
- XMEMSET(fname, 0, len);
- /* if access to stdlib.h */
- if ((rt = XGETENV("RANDFILE")) != NULL) {
- if (len > XSTRLEN(rt)) {
- XMEMCPY(fname, rt, XSTRLEN(rt));
- }
- else {
- WOLFSSL_MSG("RANDFILE too large for buffer");
- rt = NULL;
- }
- }
-
- /* $RANDFILE was not set or is too large, check $HOME */
- if (rt == NULL) {
- WOLFSSL_MSG("Environment variable RANDFILE not set");
- if ((rt = XGETENV("HOME")) == NULL) {
- WOLFSSL_MSG("Environment variable HOME not set");
- return NULL;
- }
-
- if (len > XSTRLEN(rt) + XSTRLEN(ap)) {
- fname[0] = '\0';
- XSTRNCAT(fname, rt, len);
- XSTRNCAT(fname, ap, len - XSTRLEN(rt));
- return fname;
- }
- else {
- WOLFSSL_MSG("HOME too large for buffer");
- return NULL;
- }
- }
-
- return fname;
-#else
- /* no filesystem defined */
- WOLFSSL_ENTER("wolfSSL_RAND_file_name");
- WOLFSSL_MSG("No filesystem feature enabled, not compiled in");
- (void)fname;
- (void)len;
- return NULL;
-#endif
-}
-
-
-/* Writes 1024 bytes from the RNG to the given file name.
- *
- * fname name of file to write to
- *
- * Returns the number of bytes written
- */
-int wolfSSL_RAND_write_file(const char* fname)
-{
- int bytes = 0;
-
- WOLFSSL_ENTER("RAND_write_file");
-
- if (fname == NULL) {
- return SSL_FAILURE;
- }
-
-#ifndef NO_FILESYSTEM
- {
- #ifndef WOLFSSL_SMALL_STACK
- unsigned char buf[1024];
- #else
- unsigned char* buf = (unsigned char *)XMALLOC(1024, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (buf == NULL) {
- WOLFSSL_MSG("malloc failed");
- return SSL_FAILURE;
- }
- #endif
- bytes = 1024; /* default size of buf */
-
- if (initGlobalRNG == 0 && wolfSSL_RAND_Init() != SSL_SUCCESS) {
- WOLFSSL_MSG("No RNG to use");
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return 0;
- }
-
- if (wc_RNG_GenerateBlock(&globalRNG, buf, bytes) != 0) {
- WOLFSSL_MSG("Error generating random buffer");
- bytes = 0;
- }
- else {
- XFILE f;
-
- f = XFOPEN(fname, "wb");
- if (f == XBADFILE) {
- WOLFSSL_MSG("Error opening the file");
- bytes = 0;
- }
- else {
- XFWRITE(buf, 1, bytes, f);
- XFCLOSE(f);
- }
- }
- ForceZero(buf, bytes);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- }
-#endif
-
- return bytes;
-}
-
-#ifndef FREERTOS_TCP
-
-/* These constant values are protocol values made by egd */
-#if defined(USE_WOLFSSL_IO) && !defined(USE_WINDOWS_API)
- #define WOLFSSL_EGD_NBLOCK 0x01
- #include
-#endif
-
-/* This collects entropy from the path nm and seeds the global PRNG with it.
- * Makes a call to wolfSSL_RAND_Init which is not thread safe.
- *
- * nm is the file path to the egd server
- *
- * Returns the number of bytes read.
- */
-int wolfSSL_RAND_egd(const char* nm)
-{
-#if defined(USE_WOLFSSL_IO) && !defined(USE_WINDOWS_API) && !defined(HAVE_FIPS) && \
- defined(HAVE_HASHDRBG)
- struct sockaddr_un rem;
- int fd;
- int ret = WOLFSSL_SUCCESS;
- word32 bytes = 0;
- word32 idx = 0;
-#ifndef WOLFSSL_SMALL_STACK
- unsigned char buf[256];
-#else
- unsigned char* buf;
- buf = (unsigned char*)XMALLOC(256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (buf == NULL) {
- WOLFSSL_MSG("Not enough memory");
- return WOLFSSL_FATAL_ERROR;
- }
-#endif
-
- if (nm == NULL) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return WOLFSSL_FATAL_ERROR;
- }
-
- fd = socket(AF_UNIX, SOCK_STREAM, 0);
- if (fd < 0) {
- WOLFSSL_MSG("Error creating socket");
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return WOLFSSL_FATAL_ERROR;
- }
- if (ret == WOLFSSL_SUCCESS) {
- rem.sun_family = AF_UNIX;
- XSTRNCPY(rem.sun_path, nm, sizeof(rem.sun_path) - 1);
- rem.sun_path[sizeof(rem.sun_path)-1] = '\0';
- }
-
- /* connect to egd server */
- if (ret == WOLFSSL_SUCCESS) {
- if (connect(fd, (struct sockaddr*)&rem, sizeof(struct sockaddr_un))
- == -1) {
- WOLFSSL_MSG("error connecting to egd server");
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
-
- while (ret == WOLFSSL_SUCCESS && bytes < 255 && idx + 2 < 256) {
- if (ret == WOLFSSL_SUCCESS) {
- buf[idx] = WOLFSSL_EGD_NBLOCK;
- buf[idx + 1] = 255 - bytes; /* request 255 bytes from server */
- ret = (int)write(fd, buf + idx, 2);
- if (ret <= 0 || ret != 2) {
- if (errno == EAGAIN) {
- ret = WOLFSSL_SUCCESS;
- continue;
- }
- WOLFSSL_MSG("error requesting entropy from egd server");
- ret = WOLFSSL_FATAL_ERROR;
- break;
- }
- }
-
- /* attempting to read */
- buf[idx] = 0;
- ret = (int)read(fd, buf + idx, 256 - bytes);
- if (ret == 0) {
- WOLFSSL_MSG("error reading entropy from egd server");
- ret = WOLFSSL_FATAL_ERROR;
- break;
- }
- if (ret > 0 && buf[idx] > 0) {
- bytes += buf[idx]; /* egd stores amount sent in first byte */
- if (bytes + idx > 255 || buf[idx] > ret) {
- WOLFSSL_MSG("Buffer error");
- ret = WOLFSSL_FATAL_ERROR;
- break;
- }
- XMEMMOVE(buf + idx, buf + idx + 1, buf[idx]);
- idx = bytes;
- ret = WOLFSSL_SUCCESS;
- if (bytes >= 255) {
- break;
- }
- }
- else {
- if (errno == EAGAIN || errno == EINTR) {
- WOLFSSL_MSG("EGD would read");
- ret = WOLFSSL_SUCCESS; /* try again */
- }
- else if (buf[idx] == 0) {
- /* if egd returned 0 then there is no more entropy to be had.
- Do not try more reads. */
- ret = WOLFSSL_SUCCESS;
- break;
- }
- else {
- WOLFSSL_MSG("Error with read");
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
-
- if (bytes > 0 && ret == WOLFSSL_SUCCESS) {
- wolfSSL_RAND_Init(); /* call to check global RNG is created */
- if (wc_RNG_DRBG_Reseed(&globalRNG, (const byte*) buf, bytes)
- != 0) {
- WOLFSSL_MSG("Error with reseeding DRBG structure");
- ret = WOLFSSL_FATAL_ERROR;
- }
- #ifdef SHOW_SECRETS
- { /* print out entropy found */
- word32 i;
- printf("EGD Entropy = ");
- for (i = 0; i < bytes; i++) {
- printf("%02X", buf[i]);
- }
- printf("\n");
- }
- #endif
- }
-
- ForceZero(buf, bytes);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- close(fd);
-
- if (ret == WOLFSSL_SUCCESS) {
- return bytes;
- }
- else {
- return ret;
- }
-#else
- WOLFSSL_MSG("Type of socket needed is not available");
- WOLFSSL_MSG("\tor using mode where DRBG API is not available");
- (void)nm;
-
- return WOLFSSL_FATAL_ERROR;
-#endif /* USE_WOLFSSL_IO && !USE_WINDOWS_API && !HAVE_FIPS && HAVE_HASHDRBG */
-}
-
-#endif /* !FREERTOS_TCP */
-
-void wolfSSL_RAND_Cleanup(void)
-{
- WOLFSSL_ENTER("wolfSSL_RAND_Cleanup()");
-
- if (initGlobalRNG != 0) {
- wc_FreeRng(&globalRNG);
- initGlobalRNG = 0;
- }
-}
-
-
-int wolfSSL_RAND_pseudo_bytes(unsigned char* buf, int num)
-{
- return wolfSSL_RAND_bytes(buf, num);
-}
-
-
-/* SSL_SUCCESS on ok */
-int wolfSSL_RAND_bytes(unsigned char* buf, int num)
-{
- int ret = 0;
- int initTmpRng = 0;
- WC_RNG* rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG;
-#else
- WC_RNG tmpRNG[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_RAND_bytes");
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return ret;
-#endif
-
- if (initGlobalRNG)
- rng = &globalRNG;
- else if(wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- if (rng) {
- if (wc_RNG_GenerateBlock(rng, buf, num) != 0)
- WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
- else
- ret = WOLFSSL_SUCCESS;
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- return ret;
-}
-
-
-int wolfSSL_RAND_poll(void)
-{
- byte entropy[16];
- int ret = 0;
- word32 entropy_sz = 16;
-
- WOLFSSL_ENTER("wolfSSL_RAND_poll");
- if (initGlobalRNG == 0){
- WOLFSSL_MSG("Global RNG no Init");
- return WOLFSSL_FAILURE;
- }
- ret = wc_GenerateSeed(&globalRNG.seed, entropy, entropy_sz);
- if (ret != 0){
- WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
- ret = WOLFSSL_FAILURE;
- }else
- ret = WOLFSSL_SUCCESS;
-
- return ret;
-}
-#endif /* OPENSSL_EXTRA */
-
-
-
-#ifdef OPENSSL_EXTRA
-
-WOLFSSL_ASN1_INTEGER* wolfSSL_BN_to_ASN1_INTEGER(const WOLFSSL_BIGNUM *bn, WOLFSSL_ASN1_INTEGER *ai)
-{
- WOLFSSL_ASN1_INTEGER* a;
- int len;
- WOLFSSL_ENTER("wolfSSL_BN_to_ASN1_INTEGER");
-
- if (ai == NULL) {
- a = wolfSSL_ASN1_INTEGER_new();
-
- if (a == NULL)
- return NULL;
-
- a->type = V_ASN1_INTEGER;
- }
- else {
- a = ai;
- }
- if (a) {
- if (wolfSSL_BN_is_negative(bn) && !wolfSSL_BN_is_zero(bn)) {
- a->type |= V_ASN1_NEG_INTEGER;
- a->negative = 1;
- }
-
- len = wolfSSL_BN_num_bytes(bn);
- if (len == 0)
- len = 1;
-
- /* allocate buffer */
- if (len > (int)sizeof(a->intData)) {
- /* create new data buffer and copy over */
- a->data = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_OPENSSL);
- if (a->data == NULL) {
- if (a != ai)
- wolfSSL_ASN1_INTEGER_free(a);
- return NULL;
- }
- a->isDynamic = 1;
- }
- else {
- XMEMSET(a->intData, 0, sizeof(a->intData));
- a->data = a->intData;
- }
- a->length = len;
-
- /* populate data */
- if (wolfSSL_BN_is_zero(bn)) {
- a->data[0] = 0;
- }
- else {
- len = wolfSSL_BN_bn2bin(bn, a->data);
- }
- a->length = len;
- }
-
- return a;
-}
-
-#ifdef OPENSSL_ALL
-void *wolfSSL_ASN1_item_new(const WOLFSSL_ASN1_ITEM *template)
-{
- void *ret = NULL;
- const WOLFSSL_ASN1_TEMPLATE *member = NULL;
- size_t i;
- WOLFSSL_ENTER("wolfSSL_ASN1_item_new");
- if (!template) {
- return NULL;
- }
- if (!(ret = XMALLOC(template->size, NULL, DYNAMIC_TYPE_OPENSSL))) {
- return NULL;
- }
- XMEMSET(ret, 0, template->size);
- for (member = template->members, i = 0; i < template->mcount;
- member++, i++) {
- switch (member->type) {
- case WOLFSSL_X509_ALGOR_ASN1:
- {
- WOLFSSL_X509_ALGOR* algor = wolfSSL_X509_ALGOR_new();
- if (!algor) {
- goto error;
- }
- *(WOLFSSL_X509_ALGOR**)(((byte*)ret) + member->offset) = algor;
- break;
- }
- case WOLFSSL_ASN1_BIT_STRING_ASN1:
- {
- WOLFSSL_ASN1_BIT_STRING* bit_str = wolfSSL_ASN1_BIT_STRING_new();
- if (!bit_str) {
- goto error;
- }
- *(WOLFSSL_ASN1_BIT_STRING**)(((byte*)ret) + member->offset) = bit_str;
- break;
- }
- default:
- WOLFSSL_MSG("Type not supported in wolfSSL_ASN1_item_new");
- goto error;
- }
- }
- return ret;
-error:
- wolfSSL_ASN1_item_free(ret, template);
- return NULL;
-}
-
-void wolfSSL_ASN1_item_free(void *val, const WOLFSSL_ASN1_ITEM *template)
-{
- const WOLFSSL_ASN1_TEMPLATE *member = NULL;
- size_t i;
- WOLFSSL_ENTER("wolfSSL_ASN1_item_free");
- if (val) {
- for (member = template->members, i = 0; i < template->mcount;
- member++, i++) {
- switch (member->type) {
- case WOLFSSL_X509_ALGOR_ASN1:
- {
- WOLFSSL_X509_ALGOR* algor = *(WOLFSSL_X509_ALGOR**)
- (((byte*)val) + member->offset);
- if (algor) {
- wolfSSL_X509_ALGOR_free(algor);
- }
- break;
- }
- case WOLFSSL_ASN1_BIT_STRING_ASN1:
- {
- WOLFSSL_ASN1_BIT_STRING* bit_str = *(WOLFSSL_ASN1_BIT_STRING**)
- (((byte*)val) + member->offset);
- if (bit_str) {
- wolfSSL_ASN1_BIT_STRING_free(bit_str);
- }
- break;
- }
- default:
- WOLFSSL_MSG("Type not supported in wolfSSL_ASN1_item_free");
- }
- }
- XFREE(val, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-#define bufLenOrNull(buf, len) (buf ? buf + len : NULL)
-
-static int i2dProcessMembers(const void *src, byte *buf,
- const WOLFSSL_ASN1_TEMPLATE *members, size_t mcount)
-{
- const WOLFSSL_ASN1_TEMPLATE *member = NULL;
- int len = 0, ret;
- size_t i;
- WOLFSSL_ENTER("processMembers");
- for (member = members, i = 0; i < mcount; member++, i++) {
- switch (member->type) {
- case WOLFSSL_X509_ALGOR_ASN1:
- {
- word32 oid = 0;
- word32 idx = 0;
- const WOLFSSL_X509_ALGOR* algor = *(const WOLFSSL_X509_ALGOR**)
- (((byte*)src) + member->offset);
- if (!algor->algorithm) {
- WOLFSSL_LEAVE("processMembers", WOLFSSL_FAILURE);
- return WOLFSSL_FAILURE;
- }
-
- if (GetObjectId(algor->algorithm->obj, &idx, &oid,
- algor->algorithm->grp, algor->algorithm->objSz) < 0) {
- WOLFSSL_MSG("Issue getting OID of object");
- return -1;
- }
-
- ret = SetAlgoID(oid, bufLenOrNull(buf, len),
- algor->algorithm->grp, 0);
- if (!ret) {
- return WOLFSSL_FAILURE;
- }
- len += ret;
- break;
- }
- case WOLFSSL_ASN1_BIT_STRING_ASN1:
- {
- const WOLFSSL_ASN1_BIT_STRING* bit_str;
- bit_str = *(const WOLFSSL_ASN1_BIT_STRING**)
- (((byte*)src) + member->offset);
- len += SetBitString(bit_str->length, 0, bufLenOrNull(buf, len));
- if (buf && bit_str->data) {
- XMEMCPY(buf + len, bit_str->data, bit_str->length);
- }
- len += bit_str->length;
- break;
- }
- default:
- WOLFSSL_MSG("Type not support in processMembers");
- WOLFSSL_LEAVE("processMembers", WOLFSSL_FAILURE);
- return WOLFSSL_FAILURE;
- }
- }
- WOLFSSL_LEAVE("processMembers", len);
- return len;
-}
-
-int wolfSSL_ASN1_item_i2d(const void *src, byte **dest,
- const WOLFSSL_ASN1_ITEM *template)
-{
- int len = 0;
- byte *buf = NULL;
-
- WOLFSSL_ENTER("wolfSSL_ASN1_item_i2d");
-
- if (!src || !template) {
- WOLFSSL_LEAVE("wolfSSL_ASN1_item_i2d", WOLFSSL_FAILURE);
- return WOLFSSL_FAILURE;
- }
-
- if (dest && !*dest) {
- len = wolfSSL_ASN1_item_i2d(src, NULL, template);
- if (!len) {
- goto error;
- }
- buf = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1);
- if (!buf) {
- goto error;
- }
- len = 0;
- }
-
- switch (template->type) {
- case ASN_SEQUENCE:
- {
- int seq_len = i2dProcessMembers(src, NULL, template->members,
- template->mcount);
- if (!seq_len) {
- goto error;
- }
- len += SetSequence(seq_len, bufLenOrNull(buf, len));
- if (buf &&
- i2dProcessMembers(src, bufLenOrNull(buf, len), template->members,
- template->mcount) != seq_len) {
- WOLFSSL_MSG("Inconsistent sequence length");
- goto error;
- }
- len += seq_len;
- break;
- }
- default:
- WOLFSSL_MSG("Type not supported in wolfSSL_ASN1_item_i2d");
- goto error;
- }
-
- if (dest && !*dest) {
- *dest = buf;
- }
- else if (dest && *dest && buf) {
- /* *dest length is not checked because the user is responsible
- * for providing a long enough buffer */
- XMEMCPY(*dest, buf, len);
- }
-
- WOLFSSL_LEAVE("wolfSSL_ASN1_item_i2d", len);
- return len;
-error:
- if (buf) {
- XFREE(buf, NULL, DYNAMIC_TYPE_ASN1);
- }
- WOLFSSL_LEAVE("wolfSSL_ASN1_item_i2d", WOLFSSL_FAILURE);
- return WOLFSSL_FAILURE;
-}
-#endif /* OPENSSL_ALL */
-
-#ifndef NO_DH
-
-static void InitwolfSSL_DH(WOLFSSL_DH* dh)
-{
- if (dh) {
- XMEMSET(dh, 0, sizeof(WOLFSSL_DH));
- }
-}
-
-
-WOLFSSL_DH* wolfSSL_DH_new(void)
-{
- WOLFSSL_DH* external;
- DhKey* key;
-
- WOLFSSL_MSG("wolfSSL_DH_new");
-
- key = (DhKey*) XMALLOC(sizeof(DhKey), NULL, DYNAMIC_TYPE_DH);
- if (key == NULL) {
- WOLFSSL_MSG("wolfSSL_DH_new malloc DhKey failure");
- return NULL;
- }
-
- external = (WOLFSSL_DH*) XMALLOC(sizeof(WOLFSSL_DH), NULL,
- DYNAMIC_TYPE_DH);
- if (external == NULL) {
- WOLFSSL_MSG("wolfSSL_DH_new malloc WOLFSSL_DH failure");
- XFREE(key, NULL, DYNAMIC_TYPE_DH);
- return NULL;
- }
-
- InitwolfSSL_DH(external);
- if (wc_InitDhKey(key) != 0) {
- WOLFSSL_MSG("wolfSSL_DH_new InitDhKey failure");
- XFREE(key, NULL, DYNAMIC_TYPE_DH);
- XFREE(external, NULL, DYNAMIC_TYPE_DH);
- return NULL;
- }
- external->internal = key;
-
- return external;
-}
-
-
-void wolfSSL_DH_free(WOLFSSL_DH* dh)
-{
- WOLFSSL_MSG("wolfSSL_DH_free");
-
- if (dh) {
- if (dh->internal) {
- wc_FreeDhKey((DhKey*)dh->internal);
- XFREE(dh->internal, NULL, DYNAMIC_TYPE_DH);
- dh->internal = NULL;
- }
- wolfSSL_BN_free(dh->priv_key);
- wolfSSL_BN_free(dh->pub_key);
- wolfSSL_BN_free(dh->g);
- wolfSSL_BN_free(dh->p);
- wolfSSL_BN_free(dh->q);
- InitwolfSSL_DH(dh); /* set back to NULLs for safety */
-
- XFREE(dh, NULL, DYNAMIC_TYPE_DH);
- }
-}
-
-int SetDhInternal(WOLFSSL_DH* dh)
-{
- int ret = WOLFSSL_FATAL_ERROR;
- int pSz = 1024;
- int gSz = 1024;
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- int privSz = 256; /* Up to 2048-bit */
- int pubSz = 256;
-#endif
-#ifdef WOLFSSL_SMALL_STACK
- unsigned char* p = NULL;
- unsigned char* g = NULL;
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- unsigned char* priv_key = NULL;
- unsigned char* pub_key = NULL;
- #endif
-#else
- unsigned char p[1024];
- unsigned char g[1024];
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- unsigned char priv_key[256];
- unsigned char pub_key[256];
- #endif
-#endif
-
- WOLFSSL_ENTER("SetDhInternal");
-
- if (dh == NULL || dh->p == NULL || dh->g == NULL)
- WOLFSSL_MSG("Bad function arguments");
- else if (wolfSSL_BN_bn2bin(dh->p, NULL) > pSz)
- WOLFSSL_MSG("Bad p internal size");
- else if (wolfSSL_BN_bn2bin(dh->g, NULL) > gSz)
- WOLFSSL_MSG("Bad g internal size");
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- else if (wolfSSL_BN_bn2bin(dh->priv_key, NULL) > privSz)
- WOLFSSL_MSG("Bad private key internal size");
- else if (wolfSSL_BN_bn2bin(dh->pub_key, NULL) > privSz)
- WOLFSSL_MSG("Bad public key internal size");
- #endif
- else {
- #ifdef WOLFSSL_SMALL_STACK
- p = (unsigned char*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- g = (unsigned char*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
-
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- priv_key = (unsigned char*)XMALLOC(privSz,NULL,DYNAMIC_TYPE_PRIVATE_KEY);
- pub_key = (unsigned char*)XMALLOC(pubSz,NULL,DYNAMIC_TYPE_PUBLIC_KEY);
- #endif
-
- if (p == NULL || g == NULL) {
- XFREE(p, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(g, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- return ret;
- }
- #endif /* WOLFSSL_SMALL_STACK */
-
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- privSz = wolfSSL_BN_bn2bin(dh->priv_key, priv_key);
- pubSz = wolfSSL_BN_bn2bin(dh->pub_key, pub_key);
- if (privSz <= 0) {
- WOLFSSL_MSG("No private key size.");
- }
- if (pubSz <= 0) {
- WOLFSSL_MSG("No public key size.");
- }
- if (privSz > 0 || pubSz > 0) {
- ret = wc_DhSetFullKeys((DhKey*)dh->internal,priv_key,privSz,
- pub_key,pubSz);
- if (ret == WOLFSSL_FAILURE) {
- WOLFSSL_MSG("Failed setting private or public key.");
- }
- }
- #endif /* WOLFSSL_QT || OPENSSL_ALL */
-
- pSz = wolfSSL_BN_bn2bin(dh->p, p);
- gSz = wolfSSL_BN_bn2bin(dh->g, g);
-
- if (pSz <= 0 || gSz <= 0)
- WOLFSSL_MSG("Bad BN2bin set");
- else if (wc_DhSetKey((DhKey*)dh->internal, p, pSz, g, gSz) < 0)
- WOLFSSL_MSG("Bad DH SetKey");
- else {
- dh->inSet = 1;
- ret = WOLFSSL_SUCCESS;
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(p, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(g, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- XFREE(priv_key, NULL, DYNAMIC_TYPE_PRIVATE_KEY);
- XFREE(pub_key, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- #endif
- #endif
- }
-
-
- return ret;
-}
-
-#if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
-/* Set the members of DhKey into WOLFSSL_DH
- * DhKey was populated from wc_DhKeyDecode
- */
-int SetDhExternal(WOLFSSL_DH *dh)
-{
- DhKey *key;
- WOLFSSL_MSG("Entering SetDhExternal");
-
- if (dh == NULL || dh->internal == NULL) {
- WOLFSSL_MSG("dh key NULL error");
- }
-
- key = (DhKey*)dh->internal;
-
- if (SetIndividualExternal(&dh->p, &key->p) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dh param p error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&dh->g, &key->g) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dh param g error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&dh->priv_key, &key->priv) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("No DH Private Key");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&dh->pub_key, &key->pub) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("No DH Public Key");
- return WOLFSSL_FATAL_ERROR;
- }
-
- dh->exSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* !NO_DH && (WOLFSSL_QT || OPENSSL_ALL) */
-
-/* return code compliant with OpenSSL :
- * DH prime size in bytes if success, 0 if error
- */
-int wolfSSL_DH_size(WOLFSSL_DH* dh)
-{
- WOLFSSL_MSG("wolfSSL_DH_size");
-
- if (dh == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- return wolfSSL_BN_num_bytes(dh->p);
-}
-
-/* This sets a big number with the 768-bit prime from RFC 2409.
- *
- * bn if not NULL then the big number structure is used. If NULL then a new
- * big number structure is created.
- *
- * Returns a WOLFSSL_BIGNUM structure on success and NULL with failure.
- */
-WOLFSSL_BIGNUM* wolfSSL_DH_768_prime(WOLFSSL_BIGNUM* bn)
-{
- const char prm[] = {
- "FFFFFFFFFFFFFFFFC90FDAA22168C234"
- "C4C6628B80DC1CD129024E088A67CC74"
- "020BBEA63B139B22514A08798E3404DD"
- "EF9519B3CD3A431B302B0A6DF25F1437"
- "4FE1356D6D51C245E485B576625E7EC6"
- "F44C42E9A63A3620FFFFFFFFFFFFFFFF"
- };
-
- WOLFSSL_ENTER("wolfSSL_DH_768_prime");
-
- if (wolfSSL_BN_hex2bn(&bn, prm) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error converting DH 768 prime to big number");
- return NULL;
- }
-
- return bn;
-}
-
-/* This sets a big number with the 1024-bit prime from RFC 2409.
- *
- * bn if not NULL then the big number structure is used. If NULL then a new
- * big number structure is created.
- *
- * Returns a WOLFSSL_BIGNUM structure on success and NULL with failure.
- */
-WOLFSSL_BIGNUM* wolfSSL_DH_1024_prime(WOLFSSL_BIGNUM* bn)
-{
- const char prm[] = {
- "FFFFFFFFFFFFFFFFC90FDAA22168C234"
- "C4C6628B80DC1CD129024E088A67CC74"
- "020BBEA63B139B22514A08798E3404DD"
- "EF9519B3CD3A431B302B0A6DF25F1437"
- "4FE1356D6D51C245E485B576625E7EC6"
- "F44C42E9A637ED6B0BFF5CB6F406B7ED"
- "EE386BFB5A899FA5AE9F24117C4B1FE6"
- "49286651ECE65381FFFFFFFFFFFFFFFF"
- };
-
- WOLFSSL_ENTER("wolfSSL_DH_1024_prime");
-
- if (wolfSSL_BN_hex2bn(&bn, prm) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error converting DH 1024 prime to big number");
- return NULL;
- }
-
- return bn;
-}
-
-/* This sets a big number with the 1536-bit prime from RFC 3526.
- *
- * bn if not NULL then the big number structure is used. If NULL then a new
- * big number structure is created.
- *
- * Returns a WOLFSSL_BIGNUM structure on success and NULL with failure.
- */
-WOLFSSL_BIGNUM* wolfSSL_DH_1536_prime(WOLFSSL_BIGNUM* bn)
-{
- const char prm[] = {
- "FFFFFFFFFFFFFFFFC90FDAA22168C234"
- "C4C6628B80DC1CD129024E088A67CC74"
- "020BBEA63B139B22514A08798E3404DD"
- "EF9519B3CD3A431B302B0A6DF25F1437"
- "4FE1356D6D51C245E485B576625E7EC6"
- "F44C42E9A637ED6B0BFF5CB6F406B7ED"
- "EE386BFB5A899FA5AE9F24117C4B1FE6"
- "49286651ECE45B3DC2007CB8A163BF05"
- "98DA48361C55D39A69163FA8FD24CF5F"
- "83655D23DCA3AD961C62F356208552BB"
- "9ED529077096966D670C354E4ABC9804"
- "F1746C08CA237327FFFFFFFFFFFFFFFF"
- };
-
- WOLFSSL_ENTER("wolfSSL_DH_1536_prime");
-
- if (wolfSSL_BN_hex2bn(&bn, prm) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error converting DH 1536 prime to big number");
- return NULL;
- }
-
- return bn;
-}
-
-/* This sets a big number with the 2048-bit prime from RFC 3526.
- *
- * bn if not NULL then the big number structure is used. If NULL then a new
- * big number structure is created.
- *
- * Returns a WOLFSSL_BIGNUM structure on success and NULL with failure.
- */
-WOLFSSL_BIGNUM* wolfSSL_DH_2048_prime(WOLFSSL_BIGNUM* bn)
-{
- const char prm[] = {
- "FFFFFFFFFFFFFFFFC90FDAA22168C234"
- "C4C6628B80DC1CD129024E088A67CC74"
- "020BBEA63B139B22514A08798E3404DD"
- "EF9519B3CD3A431B302B0A6DF25F1437"
- "4FE1356D6D51C245E485B576625E7EC6"
- "F44C42E9A637ED6B0BFF5CB6F406B7ED"
- "EE386BFB5A899FA5AE9F24117C4B1FE6"
- "49286651ECE45B3DC2007CB8A163BF05"
- "98DA48361C55D39A69163FA8FD24CF5F"
- "83655D23DCA3AD961C62F356208552BB"
- "9ED529077096966D670C354E4ABC9804"
- "F1746C08CA18217C32905E462E36CE3B"
- "E39E772C180E86039B2783A2EC07A28F"
- "B5C55DF06F4C52C9DE2BCBF695581718"
- "3995497CEA956AE515D2261898FA0510"
- "15728E5A8AACAA68FFFFFFFFFFFFFFFF"
- };
-
- WOLFSSL_ENTER("wolfSSL_DH_2048_prime");
-
- if (wolfSSL_BN_hex2bn(&bn, prm) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error converting DH 2048 prime to big number");
- return NULL;
- }
-
- return bn;
-}
-
-/* This sets a big number with the 3072-bit prime from RFC 3526.
- *
- * bn if not NULL then the big number structure is used. If NULL then a new
- * big number structure is created.
- *
- * Returns a WOLFSSL_BIGNUM structure on success and NULL with failure.
- */
-WOLFSSL_BIGNUM* wolfSSL_DH_3072_prime(WOLFSSL_BIGNUM* bn)
-{
- const char prm[] = {
- "FFFFFFFFFFFFFFFFC90FDAA22168C234"
- "C4C6628B80DC1CD129024E088A67CC74"
- "020BBEA63B139B22514A08798E3404DD"
- "EF9519B3CD3A431B302B0A6DF25F1437"
- "4FE1356D6D51C245E485B576625E7EC6"
- "F44C42E9A637ED6B0BFF5CB6F406B7ED"
- "EE386BFB5A899FA5AE9F24117C4B1FE6"
- "49286651ECE45B3DC2007CB8A163BF05"
- "98DA48361C55D39A69163FA8FD24CF5F"
- "83655D23DCA3AD961C62F356208552BB"
- "9ED529077096966D670C354E4ABC9804"
- "F1746C08CA18217C32905E462E36CE3B"
- "E39E772C180E86039B2783A2EC07A28F"
- "B5C55DF06F4C52C9DE2BCBF695581718"
- "3995497CEA956AE515D2261898FA0510"
- "15728E5A8AAAC42DAD33170D04507A33"
- "A85521ABDF1CBA64ECFB850458DBEF0A"
- "8AEA71575D060C7DB3970F85A6E1E4C7"
- "ABF5AE8CDB0933D71E8C94E04A25619D"
- "CEE3D2261AD2EE6BF12FFA06D98A0864"
- "D87602733EC86A64521F2B18177B200C"
- "BBE117577A615D6C770988C0BAD946E2"
- "08E24FA074E5AB3143DB5BFCE0FD108E"
- "4B82D120A93AD2CAFFFFFFFFFFFFFFFF"
- };
-
- WOLFSSL_ENTER("wolfSSL_DH_3072_prime");
-
- if (wolfSSL_BN_hex2bn(&bn, prm) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error converting DH 3072 prime to big number");
- return NULL;
- }
-
- return bn;
-}
-
-/* This sets a big number with the 4096-bit prime from RFC 3526.
- *
- * bn if not NULL then the big number structure is used. If NULL then a new
- * big number structure is created.
- *
- * Returns a WOLFSSL_BIGNUM structure on success and NULL with failure.
- */
-WOLFSSL_BIGNUM* wolfSSL_DH_4096_prime(WOLFSSL_BIGNUM* bn)
-{
- const char prm[] = {
- "FFFFFFFFFFFFFFFFC90FDAA22168C234"
- "C4C6628B80DC1CD129024E088A67CC74"
- "020BBEA63B139B22514A08798E3404DD"
- "EF9519B3CD3A431B302B0A6DF25F1437"
- "4FE1356D6D51C245E485B576625E7EC6"
- "F44C42E9A637ED6B0BFF5CB6F406B7ED"
- "EE386BFB5A899FA5AE9F24117C4B1FE6"
- "49286651ECE45B3DC2007CB8A163BF05"
- "98DA48361C55D39A69163FA8FD24CF5F"
- "83655D23DCA3AD961C62F356208552BB"
- "9ED529077096966D670C354E4ABC9804"
- "F1746C08CA18217C32905E462E36CE3B"
- "E39E772C180E86039B2783A2EC07A28F"
- "B5C55DF06F4C52C9DE2BCBF695581718"
- "3995497CEA956AE515D2261898FA0510"
- "15728E5A8AAAC42DAD33170D04507A33"
- "A85521ABDF1CBA64ECFB850458DBEF0A"
- "8AEA71575D060C7DB3970F85A6E1E4C7"
- "ABF5AE8CDB0933D71E8C94E04A25619D"
- "CEE3D2261AD2EE6BF12FFA06D98A0864"
- "D87602733EC86A64521F2B18177B200C"
- "BBE117577A615D6C770988C0BAD946E2"
- "08E24FA074E5AB3143DB5BFCE0FD108E"
- "4B82D120A92108011A723C12A787E6D7"
- "88719A10BDBA5B2699C327186AF4E23C"
- "1A946834B6150BDA2583E9CA2AD44CE8"
- "DBBBC2DB04DE8EF92E8EFC141FBECAA6"
- "287C59474E6BC05D99B2964FA090C3A2"
- "233BA186515BE7ED1F612970CEE2D7AF"
- "B81BDD762170481CD0069127D5B05AA9"
- "93B4EA988D8FDDC186FFB7DC90A6C08F"
- "4DF435C934063199FFFFFFFFFFFFFFFF"
- };
-
- WOLFSSL_ENTER("wolfSSL_DH_4096_prime");
-
- if (wolfSSL_BN_hex2bn(&bn, prm) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error converting DH 4096 prime to big number");
- return NULL;
- }
-
- return bn;
-}
-
-/* This sets a big number with the 6144-bit prime from RFC 3526.
- *
- * bn if not NULL then the big number structure is used. If NULL then a new
- * big number structure is created.
- *
- * Returns a WOLFSSL_BIGNUM structure on success and NULL with failure.
- */
-WOLFSSL_BIGNUM* wolfSSL_DH_6144_prime(WOLFSSL_BIGNUM* bn)
-{
- const char prm[] = {
- "FFFFFFFFFFFFFFFFC90FDAA22168C234"
- "C4C6628B80DC1CD129024E088A67CC74"
- "020BBEA63B139B22514A08798E3404DD"
- "EF9519B3CD3A431B302B0A6DF25F1437"
- "4FE1356D6D51C245E485B576625E7EC6"
- "F44C42E9A637ED6B0BFF5CB6F406B7ED"
- "EE386BFB5A899FA5AE9F24117C4B1FE6"
- "49286651ECE45B3DC2007CB8A163BF05"
- "98DA48361C55D39A69163FA8FD24CF5F"
- "83655D23DCA3AD961C62F356208552BB"
- "9ED529077096966D670C354E4ABC9804"
- "F1746C08CA18217C32905E462E36CE3B"
- "E39E772C180E86039B2783A2EC07A28F"
- "B5C55DF06F4C52C9DE2BCBF695581718"
- "3995497CEA956AE515D2261898FA0510"
- "15728E5A8AAAC42DAD33170D04507A33"
- "A85521ABDF1CBA64ECFB850458DBEF0A"
- "8AEA71575D060C7DB3970F85A6E1E4C7"
- "ABF5AE8CDB0933D71E8C94E04A25619D"
- "CEE3D2261AD2EE6BF12FFA06D98A0864"
- "D87602733EC86A64521F2B18177B200C"
- "BBE117577A615D6C770988C0BAD946E2"
- "08E24FA074E5AB3143DB5BFCE0FD108E"
- "4B82D120A92108011A723C12A787E6D7"
- "88719A10BDBA5B2699C327186AF4E23C"
- "1A946834B6150BDA2583E9CA2AD44CE8"
- "DBBBC2DB04DE8EF92E8EFC141FBECAA6"
- "287C59474E6BC05D99B2964FA090C3A2"
- "233BA186515BE7ED1F612970CEE2D7AF"
- "B81BDD762170481CD0069127D5B05AA9"
- "93B4EA988D8FDDC186FFB7DC90A6C08F"
- "4DF435C93402849236C3FAB4D27C7026"
- "C1D4DCB2602646DEC9751E763DBA37BD"
- "F8FF9406AD9E530EE5DB382F413001AE"
- "B06A53ED9027D831179727B0865A8918"
- "DA3EDBEBCF9B14ED44CE6CBACED4BB1B"
- "DB7F1447E6CC254B332051512BD7AF42"
- "6FB8F401378CD2BF5983CA01C64B92EC"
- "F032EA15D1721D03F482D7CE6E74FEF6"
- "D55E702F46980C82B5A84031900B1C9E"
- "59E7C97FBEC7E8F323A97A7E36CC88BE"
- "0F1D45B7FF585AC54BD407B22B4154AA"
- "CC8F6D7EBF48E1D814CC5ED20F8037E0"
- "A79715EEF29BE32806A1D58BB7C5DA76"
- "F550AA3D8A1FBFF0EB19CCB1A313D55C"
- "DA56C9EC2EF29632387FE8D76E3C0468"
- "043E8F663F4860EE12BF2D5B0B7474D6"
- "E694F91E6DCC4024FFFFFFFFFFFFFFFF"
- };
-
- WOLFSSL_ENTER("wolfSSL_DH_6144_prime");
-
- if (wolfSSL_BN_hex2bn(&bn, prm) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error converting DH 6144 prime to big number");
- return NULL;
- }
-
- return bn;
-}
-
-
-/* This sets a big number with the 8192-bit prime from RFC 3526.
- *
- * bn if not NULL then the big number structure is used. If NULL then a new
- * big number structure is created.
- *
- * Returns a WOLFSSL_BIGNUM structure on success and NULL with failure.
- */
-WOLFSSL_BIGNUM* wolfSSL_DH_8192_prime(WOLFSSL_BIGNUM* bn)
-{
- const char prm[] = {
- "FFFFFFFFFFFFFFFFC90FDAA22168C234"
- "C4C6628B80DC1CD129024E088A67CC74"
- "020BBEA63B139B22514A08798E3404DD"
- "EF9519B3CD3A431B302B0A6DF25F1437"
- "4FE1356D6D51C245E485B576625E7EC6"
- "F44C42E9A637ED6B0BFF5CB6F406B7ED"
- "EE386BFB5A899FA5AE9F24117C4B1FE6"
- "49286651ECE45B3DC2007CB8A163BF05"
- "98DA48361C55D39A69163FA8FD24CF5F"
- "83655D23DCA3AD961C62F356208552BB"
- "9ED529077096966D670C354E4ABC9804"
- "F1746C08CA18217C32905E462E36CE3B"
- "E39E772C180E86039B2783A2EC07A28F"
- "B5C55DF06F4C52C9DE2BCBF695581718"
- "3995497CEA956AE515D2261898FA0510"
- "15728E5A8AAAC42DAD33170D04507A33"
- "A85521ABDF1CBA64ECFB850458DBEF0A"
- "8AEA71575D060C7DB3970F85A6E1E4C7"
- "ABF5AE8CDB0933D71E8C94E04A25619D"
- "CEE3D2261AD2EE6BF12FFA06D98A0864"
- "D87602733EC86A64521F2B18177B200C"
- "BBE117577A615D6C770988C0BAD946E2"
- "08E24FA074E5AB3143DB5BFCE0FD108E"
- "4B82D120A92108011A723C12A787E6D7"
- "88719A10BDBA5B2699C327186AF4E23C"
- "1A946834B6150BDA2583E9CA2AD44CE8"
- "DBBBC2DB04DE8EF92E8EFC141FBECAA6"
- "287C59474E6BC05D99B2964FA090C3A2"
- "233BA186515BE7ED1F612970CEE2D7AF"
- "B81BDD762170481CD0069127D5B05AA9"
- "93B4EA988D8FDDC186FFB7DC90A6C08F"
- "4DF435C93402849236C3FAB4D27C7026"
- "C1D4DCB2602646DEC9751E763DBA37BD"
- "F8FF9406AD9E530EE5DB382F413001AE"
- "B06A53ED9027D831179727B0865A8918"
- "DA3EDBEBCF9B14ED44CE6CBACED4BB1B"
- "DB7F1447E6CC254B332051512BD7AF42"
- "6FB8F401378CD2BF5983CA01C64B92EC"
- "F032EA15D1721D03F482D7CE6E74FEF6"
- "D55E702F46980C82B5A84031900B1C9E"
- "59E7C97FBEC7E8F323A97A7E36CC88BE"
- "0F1D45B7FF585AC54BD407B22B4154AA"
- "CC8F6D7EBF48E1D814CC5ED20F8037E0"
- "A79715EEF29BE32806A1D58BB7C5DA76"
- "F550AA3D8A1FBFF0EB19CCB1A313D55C"
- "DA56C9EC2EF29632387FE8D76E3C0468"
- "043E8F663F4860EE12BF2D5B0B7474D6"
- "E694F91E6DBE115974A3926F12FEE5E4"
- "38777CB6A932DF8CD8BEC4D073B931BA"
- "3BC832B68D9DD300741FA7BF8AFC47ED"
- "2576F6936BA424663AAB639C5AE4F568"
- "3423B4742BF1C978238F16CBE39D652D"
- "E3FDB8BEFC848AD922222E04A4037C07"
- "13EB57A81A23F0C73473FC646CEA306B"
- "4BCBC8862F8385DDFA9D4B7FA2C087E8"
- "79683303ED5BDD3A062B3CF5B3A278A6"
- "6D2A13F83F44F82DDF310EE074AB6A36"
- "4597E899A0255DC164F31CC50846851D"
- "F9AB48195DED7EA1B1D510BD7EE74D73"
- "FAF36BC31ECFA268359046F4EB879F92"
- "4009438B481C6CD7889A002ED5EE382B"
- "C9190DA6FC026E479558E4475677E9AA"
- "9E3050E2765694DFC81F56E880B96E71"
- "60C980DD98EDD3DFFFFFFFFFFFFFFFFF"
- };
-
- WOLFSSL_ENTER("wolfSSL_DH_8192_prime");
-
- if (wolfSSL_BN_hex2bn(&bn, prm) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error converting DH 8192 prime to big number");
- return NULL;
- }
-
- return bn;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_DH_generate_key(WOLFSSL_DH* dh)
-{
- int ret = WOLFSSL_FAILURE;
- word32 pubSz = 0;
- word32 privSz = 0;
- int initTmpRng = 0;
- WC_RNG* rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG;
-#else
- WC_RNG tmpRNG[1];
-#endif
- unsigned char* pub = NULL;
- unsigned char* priv = NULL;
-
- WOLFSSL_MSG("wolfSSL_DH_generate_key");
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL) {
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
- return ret;
- }
-#endif
-
- if (dh == NULL || dh->p == NULL || dh->g == NULL)
- WOLFSSL_MSG("Bad function arguments");
- else if (dh->inSet == 0 && SetDhInternal(dh) != WOLFSSL_SUCCESS)
- WOLFSSL_MSG("Bad DH set internal");
- else if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng) {
- pubSz = wolfSSL_BN_num_bytes(dh->p);
- if (dh->length) {
- privSz = dh->length/8; /* to bytes */
- } else {
- privSz = pubSz;
- }
- pub = (unsigned char*)XMALLOC(pubSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- priv = (unsigned char*)XMALLOC(privSz, NULL, DYNAMIC_TYPE_PRIVATE_KEY);
- if (pub == NULL || priv == NULL) {
- WOLFSSL_MSG("Unable to malloc memory");
- }
- else if (wc_DhGenerateKeyPair((DhKey*)dh->internal, rng, priv, &privSz,
- pub, &pubSz) < 0)
- WOLFSSL_MSG("Bad wc_DhGenerateKeyPair");
- else {
- if (dh->pub_key)
- wolfSSL_BN_free(dh->pub_key);
-
- dh->pub_key = wolfSSL_BN_new();
- if (dh->pub_key == NULL) {
- WOLFSSL_MSG("Bad DH new pub");
- }
- if (dh->priv_key)
- wolfSSL_BN_free(dh->priv_key);
-
- dh->priv_key = wolfSSL_BN_new();
-
- if (dh->priv_key == NULL) {
- WOLFSSL_MSG("Bad DH new priv");
- }
-
- if (dh->pub_key && dh->priv_key) {
- if (wolfSSL_BN_bin2bn(pub, pubSz, dh->pub_key) == NULL)
- WOLFSSL_MSG("Bad DH bn2bin error pub");
- else if (wolfSSL_BN_bin2bn(priv, privSz, dh->priv_key) == NULL)
- WOLFSSL_MSG("Bad DH bn2bin error priv");
- else
- ret = WOLFSSL_SUCCESS;
- }
- }
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
- XFREE(pub, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(priv, NULL, DYNAMIC_TYPE_PRIVATE_KEY);
-
- return ret;
-}
-
-
-/* return code compliant with OpenSSL :
- * size of shared secret if success, -1 if error
- */
-int wolfSSL_DH_compute_key(unsigned char* key, WOLFSSL_BIGNUM* otherPub,
- WOLFSSL_DH* dh)
-{
- int ret = WOLFSSL_FATAL_ERROR;
- word32 keySz = 0;
- int pubSz = 1024;
- int privSz = 1024;
-#ifdef WOLFSSL_SMALL_STACK
- unsigned char* pub;
- unsigned char* priv = NULL;
-#else
- unsigned char pub [1024];
- unsigned char priv[1024];
-#endif
-
- WOLFSSL_MSG("wolfSSL_DH_compute_key");
-
-#ifdef WOLFSSL_SMALL_STACK
- pub = (unsigned char*)XMALLOC(pubSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- if (pub == NULL)
- return ret;
-
- priv = (unsigned char*)XMALLOC(privSz, NULL, DYNAMIC_TYPE_PRIVATE_KEY);
- if (priv == NULL) {
- XFREE(pub, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- return ret;
- }
-#endif
-
- if (dh == NULL || dh->priv_key == NULL || otherPub == NULL)
- WOLFSSL_MSG("Bad function arguments");
- else if ((keySz = (word32)DH_size(dh)) == 0)
- WOLFSSL_MSG("Bad DH_size");
- else if (wolfSSL_BN_bn2bin(dh->priv_key, NULL) > (int)privSz)
- WOLFSSL_MSG("Bad priv internal size");
- else if (wolfSSL_BN_bn2bin(otherPub, NULL) > (int)pubSz)
- WOLFSSL_MSG("Bad otherPub size");
- else {
- privSz = wolfSSL_BN_bn2bin(dh->priv_key, priv);
- pubSz = wolfSSL_BN_bn2bin(otherPub, pub);
- if (dh->inSet == 0 && SetDhInternal(dh) != SSL_SUCCESS){
- WOLFSSL_MSG("Bad DH set internal");
- }
- if (privSz <= 0 || pubSz <= 0)
- WOLFSSL_MSG("Bad BN2bin set");
- else if (wc_DhAgree((DhKey*)dh->internal, key, &keySz,
- priv, privSz, pub, pubSz) < 0)
- WOLFSSL_MSG("wc_DhAgree failed");
- else
- ret = (int)keySz;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(pub, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(priv, NULL, DYNAMIC_TYPE_PRIVATE_KEY);
-#endif
-
- return ret;
-}
-
-
-#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
-/* ownership of p,q,and g get taken over by "dh" on success and should be free'd
- * with a call to wolfSSL_DH_free -- not individually.
- *
- * returns WOLFSSL_SUCCESS on success
- */
-int wolfSSL_DH_set0_pqg(WOLFSSL_DH *dh, WOLFSSL_BIGNUM *p,
- WOLFSSL_BIGNUM *q, WOLFSSL_BIGNUM *g)
-{
- int ret;
- WOLFSSL_ENTER("wolfSSL_DH_set0_pqg");
-
- /* q can be NULL */
- if (dh == NULL || p == NULL || g == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* free existing internal DH structure and recreate with new p / g */
- if (dh->inSet) {
- ret = wc_FreeDhKey((DhKey*)dh->internal);
- if (ret != 0) {
- WOLFSSL_MSG("Unable to free internal DH key");
- return WOLFSSL_FAILURE;
- }
- }
-
- wolfSSL_BN_free(dh->p);
- wolfSSL_BN_free(dh->q);
- wolfSSL_BN_free(dh->g);
- wolfSSL_BN_free(dh->pub_key);
- wolfSSL_BN_free(dh->priv_key);
-
- dh->p = p;
- dh->q = q;
- dh->g = g;
-
- ret = SetDhInternal(dh);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Unable to set internal DH key");
- dh->p = NULL;
- dh->q = NULL;
- dh->g = NULL;
- dh->inSet = 0;
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* v1.1.0 or later */
-
-#endif /* NO_DH */
-
-
-#ifndef NO_DSA
-static void InitwolfSSL_DSA(WOLFSSL_DSA* dsa)
-{
- if (dsa) {
- dsa->p = NULL;
- dsa->q = NULL;
- dsa->g = NULL;
- dsa->pub_key = NULL;
- dsa->priv_key = NULL;
- dsa->internal = NULL;
- dsa->inSet = 0;
- dsa->exSet = 0;
- }
-}
-
-
-WOLFSSL_DSA* wolfSSL_DSA_new(void)
-{
- WOLFSSL_DSA* external;
- DsaKey* key;
-
- WOLFSSL_MSG("wolfSSL_DSA_new");
-
- key = (DsaKey*) XMALLOC(sizeof(DsaKey), NULL, DYNAMIC_TYPE_DSA);
- if (key == NULL) {
- WOLFSSL_MSG("wolfSSL_DSA_new malloc DsaKey failure");
- return NULL;
- }
-
- external = (WOLFSSL_DSA*) XMALLOC(sizeof(WOLFSSL_DSA), NULL,
- DYNAMIC_TYPE_DSA);
- if (external == NULL) {
- WOLFSSL_MSG("wolfSSL_DSA_new malloc WOLFSSL_DSA failure");
- XFREE(key, NULL, DYNAMIC_TYPE_DSA);
- return NULL;
- }
-
- InitwolfSSL_DSA(external);
- if (wc_InitDsaKey(key) != 0) {
- WOLFSSL_MSG("wolfSSL_DSA_new InitDsaKey failure");
- XFREE(key, NULL, DYNAMIC_TYPE_DSA);
- wolfSSL_DSA_free(external);
- return NULL;
- }
- external->internal = key;
-
- return external;
-}
-
-
-void wolfSSL_DSA_free(WOLFSSL_DSA* dsa)
-{
- WOLFSSL_MSG("wolfSSL_DSA_free");
-
- if (dsa) {
- if (dsa->internal) {
- FreeDsaKey((DsaKey*)dsa->internal);
- XFREE(dsa->internal, NULL, DYNAMIC_TYPE_DSA);
- dsa->internal = NULL;
- }
- wolfSSL_BN_free(dsa->priv_key);
- wolfSSL_BN_free(dsa->pub_key);
- wolfSSL_BN_free(dsa->g);
- wolfSSL_BN_free(dsa->q);
- wolfSSL_BN_free(dsa->p);
- InitwolfSSL_DSA(dsa); /* set back to NULLs for safety */
-
- XFREE(dsa, NULL, DYNAMIC_TYPE_DSA);
-
- /* dsa = NULL, don't try to access or double free it */
- }
-}
-
-#endif /* NO_DSA */
-#endif /* OPENSSL_EXTRA */
-
-#ifdef OPENSSL_EXTRA
-
-#ifndef NO_DSA
-/* wolfSSL -> OpenSSL */
-int SetDsaExternal(WOLFSSL_DSA* dsa)
-{
- DsaKey* key;
- WOLFSSL_MSG("Entering SetDsaExternal");
-
- if (dsa == NULL || dsa->internal == NULL) {
- WOLFSSL_MSG("dsa key NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- key = (DsaKey*)dsa->internal;
-
- if (SetIndividualExternal(&dsa->p, &key->p) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dsa p key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&dsa->q, &key->q) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dsa q key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&dsa->g, &key->g) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dsa g key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&dsa->pub_key, &key->y) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dsa y key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&dsa->priv_key, &key->x) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dsa x key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- dsa->exSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Openssl -> WolfSSL */
-int SetDsaInternal(WOLFSSL_DSA* dsa)
-{
- DsaKey* key;
- WOLFSSL_MSG("Entering SetDsaInternal");
-
- if (dsa == NULL || dsa->internal == NULL) {
- WOLFSSL_MSG("dsa key NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- key = (DsaKey*)dsa->internal;
-
- if (dsa->p != NULL &&
- SetIndividualInternal(dsa->p, &key->p) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa p key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (dsa->q != NULL &&
- SetIndividualInternal(dsa->q, &key->q) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa q key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (dsa->g != NULL &&
- SetIndividualInternal(dsa->g, &key->g) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa g key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (dsa->pub_key != NULL) {
- if (SetIndividualInternal(dsa->pub_key, &key->y) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa pub_key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* public key */
- key->type = DSA_PUBLIC;
- }
-
- if (dsa->priv_key != NULL) {
- if (SetIndividualInternal(dsa->priv_key, &key->x) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa priv_key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* private key */
- key->type = DSA_PRIVATE;
- }
-
- dsa->inSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* NO_DSA */
-#endif /* OPENSSL_EXTRA */
-
-
-#ifdef OPENSSL_EXTRA
-#if !defined(NO_RSA)
-/* Generates a RSA key of length len
- *
- * len length of RSA key i.e. 2048
- * e e to use when generating RSA key
- * f callback function for generation details
- * data user callback argument
- *
- * Note: Because of wc_MakeRsaKey an RSA key size generated can be slightly
- * rounded down. For example generating a key of size 2999 with e =
- * 65537 will make a key of size 374 instead of 375.
- * Returns a new RSA key on success and NULL on failure
- */
-WOLFSSL_RSA* wolfSSL_RSA_generate_key(int len, unsigned long e,
- void(*f)(int, int, void*), void* data)
-{
- WOLFSSL_RSA* rsa = NULL;
- WOLFSSL_BIGNUM* bn = NULL;
-
- WOLFSSL_ENTER("wolfSSL_RSA_generate_key");
-
- (void)f;
- (void)data;
-
- if (len < 0) {
- WOLFSSL_MSG("Bad argument: length was less than 0");
- return NULL;
- }
-
- bn = wolfSSL_BN_new();
- if (bn == NULL) {
- WOLFSSL_MSG("Error creating big number");
- return NULL;
- }
-
- if (wolfSSL_BN_set_word(bn, (WOLFSSL_BN_ULONG)e) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error using e value");
- wolfSSL_BN_free(bn);
- return NULL;
- }
-
- rsa = wolfSSL_RSA_new();
- if (rsa == NULL) {
- WOLFSSL_MSG("memory error");
- }
- else {
- if (wolfSSL_RSA_generate_key_ex(rsa, len, bn, NULL) != SSL_SUCCESS){
- wolfSSL_RSA_free(rsa);
- rsa = NULL;
- }
- }
- wolfSSL_BN_free(bn);
-
- return rsa;
-}
-
-
-/* return compliant with OpenSSL
- * 1 if success, 0 if error
- */
-int wolfSSL_RSA_generate_key_ex(WOLFSSL_RSA* rsa, int bits, WOLFSSL_BIGNUM* bn,
- void* cb)
-{
- int ret = WOLFSSL_FAILURE;
-
- (void)cb;
- (void)bn;
- (void)bits;
-
- WOLFSSL_ENTER("wolfSSL_RSA_generate_key_ex");
-
- if (rsa == NULL || rsa->internal == NULL) {
- /* bit size checked during make key call */
- WOLFSSL_MSG("bad arguments");
- return WOLFSSL_FAILURE;
- }
-
-#ifdef WOLFSSL_KEY_GEN
- {
- #ifdef WOLFSSL_SMALL_STACK
- WC_RNG* rng;
- #else
- WC_RNG rng[1];
- #endif
-
- #ifdef WOLFSSL_SMALL_STACK
- rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (rng == NULL)
- return WOLFSSL_FAILURE;
- #endif
-
- if (wc_InitRng(rng) < 0)
- WOLFSSL_MSG("RNG init failed");
- else if (wc_MakeRsaKey((RsaKey*)rsa->internal, bits,
- wolfSSL_BN_get_word(bn), rng) != MP_OKAY)
- WOLFSSL_MSG("wc_MakeRsaKey failed");
- else if (SetRsaExternal(rsa) != WOLFSSL_SUCCESS)
- WOLFSSL_MSG("SetRsaExternal failed");
- else {
- rsa->inSet = 1;
- ret = WOLFSSL_SUCCESS;
- }
-
- wc_FreeRng(rng);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
- #endif
- }
-#else
- WOLFSSL_MSG("No Key Gen built in");
-#endif
- return ret;
-}
-#endif /* NO_RSA */
-
-#ifndef NO_DSA
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_DSA_generate_key(WOLFSSL_DSA* dsa)
-{
- int ret = WOLFSSL_FAILURE;
-
- WOLFSSL_ENTER("wolfSSL_DSA_generate_key");
-
- if (dsa == NULL || dsa->internal == NULL) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- if (dsa->inSet == 0) {
- WOLFSSL_MSG("No DSA internal set, do it");
-
- if (SetDsaInternal(dsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetDsaInternal failed");
- return ret;
- }
- }
-
-#ifdef WOLFSSL_KEY_GEN
- {
- int initTmpRng = 0;
- WC_RNG *rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG *tmpRNG;
-#else
- WC_RNG tmpRNG[1];
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return WOLFSSL_FATAL_ERROR;
-#endif
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng) {
- if (wc_MakeDsaKey(rng, (DsaKey*)dsa->internal) != MP_OKAY)
- WOLFSSL_MSG("wc_MakeDsaKey failed");
- else if (SetDsaExternal(dsa) != WOLFSSL_SUCCESS)
- WOLFSSL_MSG("SetDsaExternal failed");
- else
- ret = WOLFSSL_SUCCESS;
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
- }
-#else /* WOLFSSL_KEY_GEN */
- WOLFSSL_MSG("No Key Gen built in");
-#endif
- return ret;
-}
-
-
-/* Returns a pointer to a new WOLFSSL_DSA structure on success and NULL on fail
- */
-WOLFSSL_DSA* wolfSSL_DSA_generate_parameters(int bits, unsigned char* seed,
- int seedLen, int* counterRet, unsigned long* hRet,
- WOLFSSL_BN_CB cb, void* CBArg)
-{
- WOLFSSL_DSA* dsa;
-
- WOLFSSL_ENTER("wolfSSL_DSA_generate_parameters()");
-
- (void)cb;
- (void)CBArg;
- dsa = wolfSSL_DSA_new();
- if (dsa == NULL) {
- return NULL;
- }
-
- if (wolfSSL_DSA_generate_parameters_ex(dsa, bits, seed, seedLen,
- counterRet, hRet, NULL) != SSL_SUCCESS) {
- wolfSSL_DSA_free(dsa);
- return NULL;
- }
-
- return dsa;
-}
-
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_DSA_generate_parameters_ex(WOLFSSL_DSA* dsa, int bits,
- unsigned char* seed, int seedLen,
- int* counterRet,
- unsigned long* hRet, void* cb)
-{
- int ret = WOLFSSL_FAILURE;
-
- (void)bits;
- (void)seed;
- (void)seedLen;
- (void)counterRet;
- (void)hRet;
- (void)cb;
-
- WOLFSSL_ENTER("wolfSSL_DSA_generate_parameters_ex");
-
- if (dsa == NULL || dsa->internal == NULL) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
-#ifdef WOLFSSL_KEY_GEN
- {
- int initTmpRng = 0;
- WC_RNG *rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG *tmpRNG;
-#else
- WC_RNG tmpRNG[1];
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return WOLFSSL_FATAL_ERROR;
-#endif
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng) {
- if (wc_MakeDsaParameters(rng, bits,
- (DsaKey*)dsa->internal) != MP_OKAY)
- WOLFSSL_MSG("wc_MakeDsaParameters failed");
- else if (SetDsaExternal(dsa) != WOLFSSL_SUCCESS)
- WOLFSSL_MSG("SetDsaExternal failed");
- else
- ret = WOLFSSL_SUCCESS;
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
- }
-#else /* WOLFSSL_KEY_GEN */
- WOLFSSL_MSG("No Key Gen built in");
-#endif
-
- return ret;
-}
-
-WOLFSSL_DSA_SIG* wolfSSL_DSA_SIG_new(void)
-{
- WOLFSSL_DSA_SIG* sig;
- WOLFSSL_ENTER("wolfSSL_DSA_SIG_new");
- sig = (WOLFSSL_DSA_SIG*)XMALLOC(sizeof(WOLFSSL_DSA_SIG), NULL, DYNAMIC_TYPE_OPENSSL);
- if (sig)
- XMEMSET(sig, 0, sizeof(WOLFSSL_DSA_SIG));
- return sig;
-}
-
-void wolfSSL_DSA_SIG_free(WOLFSSL_DSA_SIG *sig)
-{
- WOLFSSL_ENTER("wolfSSL_DSA_SIG_free");
- if (sig) {
- if (sig->r) {
- wolfSSL_BN_free(sig->r);
- }
- if (sig->s) {
- wolfSSL_BN_free(sig->s);
- }
- XFREE(sig, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-/* return WOLFSSL_SUCCESS on success, < 0 otherwise */
-int wolfSSL_DSA_do_sign(const unsigned char* d, unsigned char* sigRet,
- WOLFSSL_DSA* dsa)
-{
- int ret = WOLFSSL_FATAL_ERROR;
- int initTmpRng = 0;
- WC_RNG* rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG = NULL;
-#else
- WC_RNG tmpRNG[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_DSA_do_sign");
-
- if (d == NULL || sigRet == NULL || dsa == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return ret;
- }
-
- if (dsa->inSet == 0)
- {
- WOLFSSL_MSG("No DSA internal set, do it");
-
- if (SetDsaInternal(dsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetDsaInternal failed");
- return ret;
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return WOLFSSL_FATAL_ERROR;
-#endif
-
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng) {
- if (DsaSign(d, sigRet, (DsaKey*)dsa->internal, rng) < 0)
- WOLFSSL_MSG("DsaSign failed");
- else
- ret = WOLFSSL_SUCCESS;
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- return ret;
-}
-
-#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
-WOLFSSL_DSA_SIG* wolfSSL_DSA_do_sign_ex(const unsigned char* digest,
- int outLen, WOLFSSL_DSA* dsa)
-{
- WOLFSSL_DSA_SIG* sig = NULL;
- byte sigBin[DSA_SIG_SIZE];
-
- WOLFSSL_ENTER("wolfSSL_DSA_do_sign_ex");
-
- if (!digest || !dsa || outLen != WC_SHA_DIGEST_SIZE) {
- WOLFSSL_MSG("Bad function arguments");
- return NULL;
- }
-
- if (wolfSSL_DSA_do_sign(digest, sigBin, dsa) != WOLFSSL_SUCCESS) {
- return NULL;
- }
-
- if (!(sig = wolfSSL_DSA_SIG_new())) {
- goto error;
- }
-
- if (!(sig->r = wolfSSL_BN_bin2bn(sigBin, DSA_HALF_SIZE, NULL))) {
- goto error;
- }
-
- if (!(sig->s = wolfSSL_BN_bin2bn(sigBin + DSA_HALF_SIZE, DSA_HALF_SIZE, NULL))) {
- goto error;
- }
-
- return sig;
-error:
- if (sig) {
- wolfSSL_DSA_SIG_free(sig);
- }
- return NULL;
-}
-#endif /* !HAVE_SELFTEST && !HAVE_FIPS */
-
-int wolfSSL_DSA_do_verify(const unsigned char* d, unsigned char* sig,
- WOLFSSL_DSA* dsa, int *dsacheck)
-{
- int ret = WOLFSSL_FATAL_ERROR;
-
- WOLFSSL_ENTER("wolfSSL_DSA_do_verify");
-
- if (d == NULL || sig == NULL || dsa == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FATAL_ERROR;
- }
- if (dsa->inSet == 0)
- {
- WOLFSSL_MSG("No DSA internal set, do it");
-
- if (SetDsaInternal(dsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetDsaInternal failed");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- ret = DsaVerify(d, sig, (DsaKey*)dsa->internal, dsacheck);
- if (ret != 0 || *dsacheck != 1) {
- WOLFSSL_MSG("DsaVerify failed");
- return ret;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-#if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
-int wolfSSL_DSA_do_verify_ex(const unsigned char* digest, int digest_len,
- WOLFSSL_DSA_SIG* sig, WOLFSSL_DSA* dsa)
-{
- int dsacheck, sz;
- byte sigBin[DSA_SIG_SIZE];
- byte* sigBinPtr = sigBin;
-
- WOLFSSL_ENTER("wolfSSL_DSA_do_verify_ex");
-
- if (!digest || !sig || !dsa || digest_len != WC_SHA_DIGEST_SIZE) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FAILURE;
- }
-
- if (!sig->r || !sig->s) {
- WOLFSSL_MSG("No signature found in DSA_SIG");
- return WOLFSSL_FAILURE;
- }
-
- /* front pad with zeros */
- if (!(sz = wolfSSL_BN_num_bytes(sig->r))) {
- return WOLFSSL_FAILURE;
- }
- while (sz++ < DSA_HALF_SIZE) {
- *sigBinPtr++ = 0;
- }
-
- if (wolfSSL_BN_bn2bin(sig->r, sigBinPtr) == WOLFSSL_FATAL_ERROR) {
- return WOLFSSL_FAILURE;
- }
-
- /* Move to s */
- sigBinPtr = sigBin + DSA_HALF_SIZE;
-
- /* front pad with zeros */
- if (!(sz = wolfSSL_BN_num_bytes(sig->s))) {
- return WOLFSSL_FAILURE;
- }
- while (sz++ < DSA_HALF_SIZE) {
- *sigBinPtr++ = 0;
- }
-
- if (wolfSSL_BN_bn2bin(sig->s, sigBinPtr) == WOLFSSL_FATAL_ERROR) {
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck) != WOLFSSL_SUCCESS ||
- dsacheck != 1) {
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* !HAVE_SELFTEST && !HAVE_FIPS */
-#endif /* NO_DSA */
-
-
-#if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
-
-#ifdef DEBUG_SIGN
-static void show(const char *title, const unsigned char *out, unsigned int outlen)
-{
- const unsigned char *pt;
- printf("%s[%d] = \n", title, (int)outlen);
- outlen = outlen>100?100:outlen;
- for (pt = out; pt < out + outlen;
- printf("%c", ((*pt)&0x6f)>='A'?((*pt)&0x6f):'.'), pt++);
- printf("\n");
-}
-#else
-#define show(a,b,c)
-#endif
-
-/* return SSL_SUCCESS on ok, 0 otherwise */
-int wolfSSL_RSA_sign(int type, const unsigned char* m,
- unsigned int mLen, unsigned char* sigRet,
- unsigned int* sigLen, WOLFSSL_RSA* rsa)
-{
- return wolfSSL_RSA_sign_ex(type, m, mLen, sigRet, sigLen, rsa, 1);
-}
-
-int wolfSSL_RSA_sign_ex(int type, const unsigned char* m,
- unsigned int mLen, unsigned char* sigRet,
- unsigned int* sigLen, WOLFSSL_RSA* rsa, int flag)
-{
- word32 outLen;
- word32 signSz;
- int initTmpRng = 0;
- WC_RNG* rng = NULL;
- int ret = 0;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG = NULL;
- byte* encodedSig = NULL;
-#else
- WC_RNG tmpRNG[1];
- byte encodedSig[MAX_ENCODED_SIG_SZ];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_RSA_sign");
-
- if (m == NULL || sigRet == NULL || sigLen == NULL || rsa == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return 0;
- }
- show("Message to Sign", m, mLen);
-
- switch (type) {
- #ifdef WOLFSSL_MD2
- case NID_md2: type = MD2h; break;
- #endif
- #ifndef NO_MD5
- case NID_md5: type = MD5h; break;
- #endif
- #ifndef NO_SHA
- case NID_sha1: type = SHAh; break;
- #endif
- #ifndef NO_SHA256
- case NID_sha256: type = SHA256h; break;
- #endif
- #ifdef WOLFSSL_SHA384
- case NID_sha384: type = SHA384h; break;
- #endif
- #ifdef WOLFSSL_SHA512
- case NID_sha512: type = SHA512h; break;
- #endif
- #ifndef WOLFSSL_NOSHA3_224
- case NID_sha3_224: type = SHA3_224h; break;
- #endif
- #ifndef WOLFSSL_NOSHA3_256
- case NID_sha3_256: type = SHA3_256h; break;
- #endif
- #ifndef WOLFSSL_NOSHA3_384
- case NID_sha3_384: type = SHA3_384h; break;
- #endif
- #ifndef WOLFSSL_NOSHA3_512
- case NID_sha3_512: type = SHA3_512h; break;
- #endif
- default:
- WOLFSSL_MSG("This NID (md type) not configured or not implemented");
- return 0;
- }
-
- if (rsa->inSet == 0)
- {
- WOLFSSL_MSG("No RSA internal set, do it");
-
- if (SetRsaInternal(rsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetRsaInternal failed");
- return 0;
- }
- }
-
- outLen = (word32)wolfSSL_BN_num_bytes(rsa->n);
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return 0;
-
- encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, NULL,
- DYNAMIC_TYPE_SIGNATURE);
- if (encodedSig == NULL) {
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
- return 0;
- }
-#endif
-
- if (outLen == 0)
- WOLFSSL_MSG("Bad RSA size");
- else if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
-
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng) {
-
- signSz = wc_EncodeSignature(encodedSig, m, mLen, type);
- if (signSz == 0) {
- WOLFSSL_MSG("Bad Encode Signature");
- }
- else {
- show("Encoded Message", encodedSig, signSz);
- if (flag != 0) {
- ret = wc_RsaSSL_Sign(encodedSig, signSz, sigRet, outLen,
- (RsaKey*)rsa->internal, rng);
- if (ret <= 0) {
- WOLFSSL_MSG("Bad Rsa Sign");
- ret = 0;
- }
- else {
- *sigLen = (unsigned int)ret;
- ret = SSL_SUCCESS;
- show("Signature", sigRet, *sigLen);
- }
- } else {
- ret = SSL_SUCCESS;
- XMEMCPY(sigRet, encodedSig, signSz);
- *sigLen = signSz;
- }
- }
-
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
- XFREE(encodedSig, NULL, DYNAMIC_TYPE_SIGNATURE);
-#endif
-
- if (ret == WOLFSSL_SUCCESS)
- WOLFSSL_MSG("wolfSSL_RSA_sign success");
- else {
- WOLFSSL_MSG("wolfSSL_RSA_sign failed");
- }
- return ret;
-}
-
-
-/* returns WOLFSSL_SUCCESS on successful verify and WOLFSSL_FAILURE on fail */
-int wolfSSL_RSA_verify(int type, const unsigned char* m,
- unsigned int mLen, const unsigned char* sig,
- unsigned int sigLen, WOLFSSL_RSA* rsa)
-{
- int ret;
- unsigned char *sigRet ;
- unsigned char *sigDec ;
- unsigned int len;
-
- WOLFSSL_ENTER("wolfSSL_RSA_verify");
- if ((m == NULL) || (sig == NULL)) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FAILURE;
- }
-
- sigRet = (unsigned char *)XMALLOC(sigLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (sigRet == NULL) {
- WOLFSSL_MSG("Memory failure");
- return WOLFSSL_FAILURE;
- }
- sigDec = (unsigned char *)XMALLOC(sigLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (sigDec == NULL) {
- WOLFSSL_MSG("Memory failure");
- XFREE(sigRet, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- /* get non-encrypted signature to be compared with decrypted signature */
- ret = wolfSSL_RSA_sign_ex(type, m, mLen, sigRet, &len, rsa, 0);
- if (ret <= 0) {
- WOLFSSL_MSG("Message Digest Error");
- XFREE(sigRet, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(sigDec, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- show("Encoded Message", sigRet, len);
- /* decrypt signature */
- ret = wc_RsaSSL_Verify(sig, sigLen, (unsigned char *)sigDec, sigLen,
- (RsaKey*)rsa->internal);
- if (ret <= 0) {
- WOLFSSL_MSG("RSA Decrypt error");
- XFREE(sigRet, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(sigDec, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- show("Decrypted Signature", sigDec, ret);
-
- if ((int)len == ret && XMEMCMP(sigRet, sigDec, ret) == 0) {
- WOLFSSL_MSG("wolfSSL_RSA_verify success");
- XFREE(sigRet, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(sigDec, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_SUCCESS;
- }
- else {
- WOLFSSL_MSG("wolfSSL_RSA_verify failed");
- XFREE(sigRet, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(sigDec, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
-}
-
-void wolfSSL_RSA_get0_key(const WOLFSSL_RSA *r, const WOLFSSL_BIGNUM **n,
- const WOLFSSL_BIGNUM **e, const WOLFSSL_BIGNUM **d)
-{
- WOLFSSL_ENTER("wolfSSL_RSA_get0_key");
-
- if (r != NULL) {
- if (n != NULL)
- *n = r->n;
- if (e != NULL)
- *e = r->e;
- if (d != NULL)
- *d = r->d;
- } else {
- if (n != NULL)
- *n = NULL;
- if (e != NULL)
- *e = NULL;
- if (d != NULL)
- *d = NULL;
- }
-}
-
-/* generate p-1 and q-1, WOLFSSL_SUCCESS on ok */
-int wolfSSL_RSA_GenAdd(WOLFSSL_RSA* rsa)
-{
- int err;
- mp_int tmp;
-
- WOLFSSL_MSG("wolfSSL_RsaGenAdd");
-
- if (rsa == NULL || rsa->p == NULL || rsa->q == NULL || rsa->d == NULL ||
- rsa->dmp1 == NULL || rsa->dmq1 == NULL) {
- WOLFSSL_MSG("rsa no init error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (mp_init(&tmp) != MP_OKAY) {
- WOLFSSL_MSG("mp_init error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- err = mp_sub_d((mp_int*)rsa->p->internal, 1, &tmp);
- if (err != MP_OKAY) {
- WOLFSSL_MSG("mp_sub_d error");
- }
- else
- err = mp_mod((mp_int*)rsa->d->internal, &tmp,
- (mp_int*)rsa->dmp1->internal);
-
- if (err != MP_OKAY) {
- WOLFSSL_MSG("mp_mod error");
- }
- else
- err = mp_sub_d((mp_int*)rsa->q->internal, 1, &tmp);
- if (err != MP_OKAY) {
- WOLFSSL_MSG("mp_sub_d error");
- }
- else
- err = mp_mod((mp_int*)rsa->d->internal, &tmp,
- (mp_int*)rsa->dmq1->internal);
-
- mp_clear(&tmp);
-
- if (err == MP_OKAY)
- return WOLFSSL_SUCCESS;
- else
- return WOLFSSL_FATAL_ERROR;
-}
-#endif /* !NO_RSA && !HAVE_USER_RSA */
-
-WOLFSSL_HMAC_CTX* wolfSSL_HMAC_CTX_new(void)
-{
- return (WOLFSSL_HMAC_CTX*)XMALLOC(sizeof(WOLFSSL_HMAC_CTX), NULL,
- DYNAMIC_TYPE_OPENSSL);
-}
-
-int wolfSSL_HMAC_CTX_Init(WOLFSSL_HMAC_CTX* ctx)
-{
- WOLFSSL_MSG("wolfSSL_HMAC_CTX_Init");
-
- if (ctx != NULL) {
- /* wc_HmacSetKey sets up ctx->hmac */
- XMEMSET(ctx, 0, sizeof(WOLFSSL_HMAC_CTX));
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_HMAC_Init_ex(WOLFSSL_HMAC_CTX* ctx, const void* key,
- int keylen, const EVP_MD* type, WOLFSSL_ENGINE* e)
-{
- WOLFSSL_ENTER("wolfSSL_HMAC_Init_ex");
-
- /* WOLFSSL_ENGINE not used, call wolfSSL_HMAC_Init */
- (void)e;
- return wolfSSL_HMAC_Init(ctx, key, keylen, type);
-}
-
-
-/* helper function for Deep copy of internal wolfSSL hmac structure
- * returns WOLFSSL_SUCCESS on success */
-int wolfSSL_HmacCopy(Hmac* des, Hmac* src)
-{
- void* heap;
-
-#ifndef HAVE_FIPS
- heap = src->heap;
-#else
- heap = NULL;
-#endif
- if (wc_HmacInit(des, heap, 0) != 0) {
- return WOLFSSL_FAILURE;
- }
-
- /* requires that hash structures have no dynamic parts to them */
- switch (src->macType) {
- #ifndef NO_MD5
- case WC_MD5:
- wc_Md5Copy(&src->hash.md5, &des->hash.md5);
- break;
- #endif /* !NO_MD5 */
-
- #ifndef NO_SHA
- case WC_SHA:
- wc_ShaCopy(&src->hash.sha, &des->hash.sha);
- break;
- #endif /* !NO_SHA */
-
- #ifdef WOLFSSL_SHA224
- case WC_SHA224:
- wc_Sha224Copy(&src->hash.sha224, &des->hash.sha224);
- break;
- #endif /* WOLFSSL_SHA224 */
-
- #ifndef NO_SHA256
- case WC_SHA256:
- wc_Sha256Copy(&src->hash.sha256, &des->hash.sha256);
- break;
- #endif /* !NO_SHA256 */
-
- #ifdef WOLFSSL_SHA384
- case WC_SHA384:
- wc_Sha384Copy(&src->hash.sha384, &des->hash.sha384);
- break;
- #endif /* WOLFSSL_SHA384 */
- #ifdef WOLFSSL_SHA512
- case WC_SHA512:
- wc_Sha512Copy(&src->hash.sha512, &des->hash.sha512);
- break;
- #endif /* WOLFSSL_SHA512 */
-
- default:
- return WOLFSSL_FAILURE;
- }
-
- XMEMCPY((byte*)des->ipad, (byte*)src->ipad, WC_HMAC_BLOCK_SIZE);
- XMEMCPY((byte*)des->opad, (byte*)src->opad, WC_HMAC_BLOCK_SIZE);
- XMEMCPY((byte*)des->innerHash, (byte*)src->innerHash, WC_MAX_DIGEST_SIZE);
-#ifndef HAVE_FIPS
- des->heap = heap;
-#endif
- des->macType = src->macType;
- des->innerHashKeyed = src->innerHashKeyed;
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- XMEMCPY(&des->asyncDev, &src->asyncDev, sizeof(WC_ASYNC_DEV));
- des->keyLen = src->keyLen;
- #ifdef HAVE_CAVIUM
- des->data = (byte*)XMALLOC(src->dataLen, des->heap,
- DYNAMIC_TYPE_HMAC);
- if (des->data == NULL) {
- return BUFFER_E;
- }
- XMEMCPY(des->data, src->data, src->dataLen);
- des->dataLen = src->dataLen;
- #endif /* HAVE_CAVIUM */
-#endif /* WOLFSSL_ASYNC_CRYPT */
- return WOLFSSL_SUCCESS;
-}
-
-
-/* Deep copy of information from src to des structure
- *
- * des destination to copy information to
- * src structure to get information from
- *
- * Returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on error
- */
-int wolfSSL_HMAC_CTX_copy(WOLFSSL_HMAC_CTX* des, WOLFSSL_HMAC_CTX* src)
-{
- WOLFSSL_ENTER("wolfSSL_HMAC_CTX_copy");
-
- if (des == NULL || src == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- des->type = src->type;
- XMEMCPY((byte *)&des->save_ipad, (byte *)&src->hmac.ipad,
- WC_HMAC_BLOCK_SIZE);
- XMEMCPY((byte *)&des->save_opad, (byte *)&src->hmac.opad,
- WC_HMAC_BLOCK_SIZE);
-
- return wolfSSL_HmacCopy(&des->hmac, &src->hmac);
-}
-
-
-#if defined(HAVE_FIPS) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
-
-static int _HMAC_Init(Hmac* hmac, int type, void* heap)
-{
- int ret = 0;
-
- switch (type) {
- #ifndef NO_MD5
- case WC_MD5:
- ret = wc_InitMd5(&hmac->hash.md5);
- break;
- #endif /* !NO_MD5 */
-
- #ifndef NO_SHA
- case WC_SHA:
- ret = wc_InitSha(&hmac->hash.sha);
- break;
- #endif /* !NO_SHA */
-
- #ifdef WOLFSSL_SHA224
- case WC_SHA224:
- ret = wc_InitSha224(&hmac->hash.sha224);
- break;
- #endif /* WOLFSSL_SHA224 */
-
- #ifndef NO_SHA256
- case WC_SHA256:
- ret = wc_InitSha256(&hmac->hash.sha256);
- break;
- #endif /* !NO_SHA256 */
-
- #ifdef WOLFSSL_SHA384
- case WC_SHA384:
- ret = wc_InitSha384(&hmac->hash.sha384);
- break;
- #endif /* WOLFSSL_SHA384 */
- #ifdef WOLFSSL_SHA512
- case WC_SHA512:
- ret = wc_InitSha512(&hmac->hash.sha512);
- break;
- #endif /* WOLFSSL_SHA512 */
-
- #ifdef WOLFSSL_SHA3
- case WC_SHA3_224:
- ret = wc_InitSha3_224(&hmac->hash.sha3, heap, INVALID_DEVID);
- break;
- case WC_SHA3_256:
- ret = wc_InitSha3_256(&hmac->hash.sha3, heap, INVALID_DEVID);
- break;
- case WC_SHA3_384:
- ret = wc_InitSha3_384(&hmac->hash.sha3, heap, INVALID_DEVID);
- break;
- case WC_SHA3_512:
- ret = wc_InitSha3_512(&hmac->hash.sha3, heap, INVALID_DEVID);
- break;
- #endif
-
- default:
- ret = BAD_FUNC_ARG;
- break;
- }
-
- (void)heap;
-
- return ret;
-}
-
-#else
- #define _HMAC_Init _InitHmac
-#endif
-
-
-int wolfSSL_HMAC_Init(WOLFSSL_HMAC_CTX* ctx, const void* key, int keylen,
- const EVP_MD* type)
-{
- int hmac_error = 0;
- void* heap = NULL;
-
- WOLFSSL_MSG("wolfSSL_HMAC_Init");
-
- if (ctx == NULL) {
- WOLFSSL_MSG("no ctx on init");
- return WOLFSSL_FAILURE;
- }
-
-#ifndef HAVE_FIPS
- heap = ctx->hmac.heap;
-#endif
-
- if (type) {
- WOLFSSL_MSG("init has type");
-
-#ifndef NO_MD5
- if (XSTRNCMP(type, "MD5", 3) == 0) {
- WOLFSSL_MSG("md5 hmac");
- ctx->type = WC_MD5;
- }
- else
-#endif
-#ifdef WOLFSSL_SHA224
- if (XSTRNCMP(type, "SHA224", 6) == 0) {
- WOLFSSL_MSG("sha224 hmac");
- ctx->type = WC_SHA224;
- }
- else
-#endif
-#ifndef NO_SHA256
- if (XSTRNCMP(type, "SHA256", 6) == 0) {
- WOLFSSL_MSG("sha256 hmac");
- ctx->type = WC_SHA256;
- }
- else
-#endif
-#ifdef WOLFSSL_SHA384
- if (XSTRNCMP(type, "SHA384", 6) == 0) {
- WOLFSSL_MSG("sha384 hmac");
- ctx->type = WC_SHA384;
- }
- else
-#endif
-#ifdef WOLFSSL_SHA512
- if (XSTRNCMP(type, "SHA512", 6) == 0) {
- WOLFSSL_MSG("sha512 hmac");
- ctx->type = WC_SHA512;
- }
- else
-#endif
-
-#ifndef NO_SHA
- /* has to be last since would pick or 256, 384, or 512 too */
- if (XSTRNCMP(type, "SHA", 3) == 0) {
- WOLFSSL_MSG("sha hmac");
- ctx->type = WC_SHA;
- }
- else
-#endif
- {
- WOLFSSL_MSG("bad init type");
- return WOLFSSL_FAILURE;
- }
- }
-
- if (key && keylen) {
- WOLFSSL_MSG("keying hmac");
-
- if (wc_HmacInit(&ctx->hmac, NULL, INVALID_DEVID) == 0) {
- hmac_error = wc_HmacSetKey(&ctx->hmac, ctx->type, (const byte*)key,
- (word32)keylen);
- if (hmac_error < 0){
- wc_HmacFree(&ctx->hmac);
- return WOLFSSL_FAILURE;
- }
- XMEMCPY((byte *)&ctx->save_ipad, (byte *)&ctx->hmac.ipad,
- WC_HMAC_BLOCK_SIZE);
- XMEMCPY((byte *)&ctx->save_opad, (byte *)&ctx->hmac.opad,
- WC_HMAC_BLOCK_SIZE);
- }
- /* OpenSSL compat, no error */
- } else if(ctx->type >= 0) { /* MD5 == 0 */
- WOLFSSL_MSG("recover hmac");
- wc_HmacFree(&ctx->hmac);
- if (wc_HmacInit(&ctx->hmac, NULL, INVALID_DEVID) == 0) {
- ctx->hmac.macType = (byte)ctx->type;
- ctx->hmac.innerHashKeyed = 0;
- XMEMCPY((byte *)&ctx->hmac.ipad, (byte *)&ctx->save_ipad,
- WC_HMAC_BLOCK_SIZE);
- XMEMCPY((byte *)&ctx->hmac.opad, (byte *)&ctx->save_opad,
- WC_HMAC_BLOCK_SIZE);
- if ((hmac_error = _HMAC_Init(&ctx->hmac, ctx->hmac.macType, heap))
- !=0) {
- return hmac_error;
- }
- }
- }
-
- (void)hmac_error;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_HMAC_Update(WOLFSSL_HMAC_CTX* ctx, const unsigned char* data,
- int len)
-{
- int hmac_error = 0;
-
- WOLFSSL_MSG("wolfSSL_HMAC_Update");
-
- if (ctx == NULL) {
- WOLFSSL_MSG("no ctx");
- return WOLFSSL_FAILURE;
- }
-
- if (data) {
- WOLFSSL_MSG("updating hmac");
- hmac_error = wc_HmacUpdate(&ctx->hmac, data, (word32)len);
- if (hmac_error < 0){
- WOLFSSL_MSG("hmac update error");
- return WOLFSSL_FAILURE;
- }
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_HMAC_Final(WOLFSSL_HMAC_CTX* ctx, unsigned char* hash,
- unsigned int* len)
-{
- int hmac_error;
-
- WOLFSSL_MSG("wolfSSL_HMAC_Final");
-
- /* "len" parameter is optional. */
- if (ctx == NULL || hash == NULL) {
- WOLFSSL_MSG("invalid parameter");
- return WOLFSSL_FAILURE;
- }
-
- WOLFSSL_MSG("final hmac");
- hmac_error = wc_HmacFinal(&ctx->hmac, hash);
- if (hmac_error < 0){
- WOLFSSL_MSG("final hmac error");
- return WOLFSSL_FAILURE;
- }
-
- if (len) {
- WOLFSSL_MSG("setting output len");
- switch (ctx->type) {
- #ifndef NO_MD5
- case WC_MD5:
- *len = WC_MD5_DIGEST_SIZE;
- break;
- #endif
-
- #ifndef NO_SHA
- case WC_SHA:
- *len = WC_SHA_DIGEST_SIZE;
- break;
- #endif
-
- #ifdef WOLFSSL_SHA224
- case WC_SHA224:
- *len = WC_SHA224_DIGEST_SIZE;
- break;
- #endif
-
- #ifndef NO_SHA256
- case WC_SHA256:
- *len = WC_SHA256_DIGEST_SIZE;
- break;
- #endif
-
- #ifdef WOLFSSL_SHA384
- case WC_SHA384:
- *len = WC_SHA384_DIGEST_SIZE;
- break;
- #endif
-
- #ifdef WOLFSSL_SHA512
- case WC_SHA512:
- *len = WC_SHA512_DIGEST_SIZE;
- break;
- #endif
-
- default:
- WOLFSSL_MSG("bad hmac type");
- return WOLFSSL_FAILURE;
- }
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_HMAC_cleanup(WOLFSSL_HMAC_CTX* ctx)
-{
- WOLFSSL_MSG("wolfSSL_HMAC_cleanup");
-
- if (ctx)
- wc_HmacFree(&ctx->hmac);
-
- return SSL_SUCCESS;
-}
-
-
-void wolfSSL_HMAC_CTX_free(WOLFSSL_HMAC_CTX* ctx)
-{
- if (!ctx) {
- return;
- }
- wolfSSL_HMAC_cleanup(ctx);
- XFREE(ctx, NULL, DYNAMIC_TYPE_OPENSSL);
-}
-
-size_t wolfSSL_HMAC_size(const WOLFSSL_HMAC_CTX *ctx)
-{
- if (!ctx) {
- return 0;
- }
-
- return (size_t)wc_HashGetDigestSize((enum wc_HashType)ctx->hmac.macType);
-}
-
-#ifndef NO_DES3
-
-void wolfSSL_3des_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset,
- unsigned char* iv, int len)
-{
- (void)len;
-
- WOLFSSL_MSG("wolfSSL_3des_iv");
-
- if (ctx == NULL || iv == NULL) {
- WOLFSSL_MSG("Bad function argument");
- return;
- }
-
- if (doset)
- wc_Des3_SetIV(&ctx->cipher.des3, iv); /* OpenSSL compat, no ret */
- else
- XMEMCPY(iv, &ctx->cipher.des3.reg, DES_BLOCK_SIZE);
-}
-
-#endif /* NO_DES3 */
-
-
-#ifndef NO_AES
-
-void wolfSSL_aes_ctr_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset,
- unsigned char* iv, int len)
-{
- (void)len;
-
- WOLFSSL_MSG("wolfSSL_aes_ctr_iv");
-
- if (ctx == NULL || iv == NULL) {
- WOLFSSL_MSG("Bad function argument");
- return;
- }
-
- if (doset)
- (void)wc_AesSetIV(&ctx->cipher.aes, iv); /* OpenSSL compat, no ret */
- else
- XMEMCPY(iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
-}
-
-#endif /* NO_AES */
-
-
-/* Free the dynamically allocated data.
- *
- * p Pointer to dynamically allocated memory.
- */
-void wolfSSL_OPENSSL_free(void* p)
-{
- WOLFSSL_MSG("wolfSSL_OPENSSL_free");
-
- XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
-}
-
-void *wolfSSL_OPENSSL_malloc(size_t a)
-{
- return XMALLOC(a, NULL, DYNAMIC_TYPE_OPENSSL);
-}
-
-#if defined(WOLFSSL_KEY_GEN) && defined(WOLFSSL_PEM_TO_DER)
-
-static int EncryptDerKey(byte *der, int *derSz, const EVP_CIPHER* cipher,
- unsigned char* passwd, int passwdSz, byte **cipherInfo)
-{
- int ret, paddingSz;
- word32 idx, cipherInfoSz;
-#ifdef WOLFSSL_SMALL_STACK
- EncryptedInfo* info = NULL;
-#else
- EncryptedInfo info[1];
-#endif
-
- WOLFSSL_ENTER("EncryptDerKey");
-
- if (der == NULL || derSz == NULL || cipher == NULL ||
- passwd == NULL || cipherInfo == NULL)
- return BAD_FUNC_ARG;
-
-#ifdef WOLFSSL_SMALL_STACK
- info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
- DYNAMIC_TYPE_ENCRYPTEDINFO);
- if (info == NULL) {
- WOLFSSL_MSG("malloc failed");
- return WOLFSSL_FAILURE;
- }
-#endif
-
- XMEMSET(info, 0, sizeof(EncryptedInfo));
-
- /* set the cipher name on info */
- XSTRNCPY(info->name, cipher, NAME_SZ-1);
- info->name[NAME_SZ-1] = '\0'; /* null term */
-
- ret = wc_EncryptedInfoGet(info, info->name);
- if (ret != 0) {
- WOLFSSL_MSG("unsupported cipher");
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(info, NULL, DYNAMIC_TYPE_ENCRYPTEDINFO);
- #endif
- return WOLFSSL_FAILURE;
- }
-
- /* Generate a random salt */
- if (wolfSSL_RAND_bytes(info->iv, info->ivSz) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("generate iv failed");
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(info, NULL, DYNAMIC_TYPE_ENCRYPTEDINFO);
-#endif
- return WOLFSSL_FAILURE;
- }
-
- /* add the padding before encryption */
- paddingSz = ((*derSz)/info->ivSz + 1) * info->ivSz - (*derSz);
- if (paddingSz == 0)
- paddingSz = info->ivSz;
- XMEMSET(der+(*derSz), (byte)paddingSz, paddingSz);
- (*derSz) += paddingSz;
-
- /* encrypt buffer */
- if (wc_BufferKeyEncrypt(info, der, *derSz, passwd, passwdSz, WC_MD5) != 0) {
- WOLFSSL_MSG("encrypt key failed");
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(info, NULL, DYNAMIC_TYPE_ENCRYPTEDINFO);
-#endif
- return WOLFSSL_FAILURE;
- }
-
- /* create cipher info : 'cipher_name,Salt(hex)' */
- cipherInfoSz = (word32)(2*info->ivSz + XSTRLEN(info->name) + 2);
- *cipherInfo = (byte*)XMALLOC(cipherInfoSz, NULL,
- DYNAMIC_TYPE_STRING);
- if (*cipherInfo == NULL) {
- WOLFSSL_MSG("malloc failed");
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(info, NULL, DYNAMIC_TYPE_ENCRYPTEDINFO);
-#endif
- return WOLFSSL_FAILURE;
- }
- XSTRNCPY((char*)*cipherInfo, info->name, cipherInfoSz);
- XSTRNCAT((char*)*cipherInfo, ",", 2);
-
- idx = (word32)XSTRLEN((char*)*cipherInfo);
- cipherInfoSz -= idx;
- ret = Base16_Encode(info->iv, info->ivSz, *cipherInfo+idx, &cipherInfoSz);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(info, NULL, DYNAMIC_TYPE_ENCRYPTEDINFO);
-#endif
- if (ret != 0) {
- WOLFSSL_MSG("Base16_Encode failed");
- XFREE(*cipherInfo, NULL, DYNAMIC_TYPE_STRING);
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* WOLFSSL_KEY_GEN || WOLFSSL_PEM_TO_DER */
-
-#if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
-static int wolfSSL_RSA_To_Der(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey)
-{
- int derSz = 0;
- int ret;
- byte* derBuf;
-
- WOLFSSL_ENTER("wolfSSL_RSA_To_Der");
-
- if (!rsa || (publicKey != 0 && publicKey != 1)) {
- WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", BAD_FUNC_ARG);
- return BAD_FUNC_ARG;
- }
-
- if (rsa->inSet == 0) {
- if ((ret = SetRsaInternal(rsa)) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetRsaInternal() Failed");
- WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", ret);
- return ret;
- }
- }
-
- if (publicKey) {
- if ((derSz = wc_RsaPublicKeyDerSize((RsaKey *)rsa->internal, 1)) < 0) {
- WOLFSSL_MSG("wc_RsaPublicKeyDerSize failed");
- WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", derSz);
- return derSz;
- }
- }
- else {
- if ((derSz = wc_RsaKeyToDer((RsaKey*)rsa->internal, NULL, 0)) < 0) {
- WOLFSSL_MSG("wc_RsaKeyToDer failed");
- WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", derSz);
- return derSz;
- }
- }
-
- if (outBuf) {
- if (!(derBuf = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER))) {
- WOLFSSL_MSG("malloc failed");
- WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", MEMORY_ERROR);
- return MEMORY_ERROR;
- }
-
- /* Key to DER */
- if (publicKey) {
- derSz = wc_RsaKeyToPublicDer((RsaKey*)rsa->internal, derBuf, derSz);
- }
- else {
- derSz = wc_RsaKeyToDer((RsaKey*)rsa->internal, derBuf, derSz);
- }
-
- if (derSz < 0) {
- WOLFSSL_MSG("wc_RsaKeyToPublicDer failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- else {
- if (*outBuf) {
- XMEMCPY(*outBuf, derBuf, derSz);
- XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- else {
- *outBuf = derBuf;
- }
- }
- }
-
- WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", derSz);
- return derSz;
-}
-#endif
-
-#if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
-#if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && !defined(NO_RSA)
-
-/* Takes a WOLFSSL_RSA key and writes it out to a WOLFSSL_BIO
- *
- * bio the WOLFSSL_BIO to write to
- * key the WOLFSSL_RSA key to write out
- * cipher cipher used
- * passwd password string if used
- * len length of password string
- * cb password callback to use
- * arg null terminated string for passphrase
- */
-int wolfSSL_PEM_write_bio_RSAPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_RSA* key,
- const WOLFSSL_EVP_CIPHER* cipher,
- unsigned char* passwd, int len,
- pem_password_cb* cb, void* arg)
-{
- int ret;
- WOLFSSL_EVP_PKEY* pkey;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_RSAPrivateKey");
-
- if (bio == NULL || key == NULL) {
- WOLFSSL_MSG("Bad Function Arguments");
- return WOLFSSL_FAILURE;
- }
-
- pkey = wolfSSL_EVP_PKEY_new_ex(bio->heap);
- if (pkey == NULL) {
- WOLFSSL_MSG("wolfSSL_EVP_PKEY_new_ex failed");
- return WOLFSSL_FAILURE;
- }
-
- pkey->type = EVP_PKEY_RSA;
- pkey->rsa = key;
- pkey->ownRsa = 0;
-#if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
- /* similar to how wolfSSL_PEM_write_mem_RSAPrivateKey finds DER of key */
- {
- int derSz;
- byte* derBuf = NULL;
-
- if ((derSz = wolfSSL_RSA_To_Der(key, &derBuf, 0)) < 0) {
- WOLFSSL_MSG("wolfSSL_RSA_To_Der failed");
- return WOLFSSL_FAILURE;
- }
-
- pkey->pkey.ptr = (char*)XMALLOC(derSz, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (pkey->pkey.ptr == NULL) {
- WOLFSSL_MSG("key malloc failed");
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
- pkey->pkey_sz = derSz;
- XMEMCPY(pkey->pkey.ptr, derBuf, derSz);
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-#endif
-
- ret = wolfSSL_PEM_write_bio_PrivateKey(bio, pkey, cipher, passwd, len,
- cb, arg);
-
- wolfSSL_EVP_PKEY_free(pkey);
-
- return ret;
-}
-
-#if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
-/* Takes an RSA public key and writes it out to a WOLFSSL_BIO
- * Returns WOLFSSL_SUCCESS or WOLFSSL_FAILURE
- */
-int wolfSSL_PEM_write_bio_RSA_PUBKEY(WOLFSSL_BIO* bio, WOLFSSL_RSA* rsa)
-{
- int ret = 0, derSz = 0;
- byte *derBuf = NULL;
- WOLFSSL_EVP_PKEY* pkey = NULL;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_RSA_PUBKEY");
-
- if (bio == NULL || rsa == NULL) {
- WOLFSSL_MSG("Bad Function Arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* Initialize pkey structure */
- pkey = wolfSSL_EVP_PKEY_new_ex(bio->heap);
- if (pkey == NULL) {
- WOLFSSL_MSG("wolfSSL_EVP_PKEY_new_ex failed");
- return WOLFSSL_FAILURE;
- }
-
- pkey->type = EVP_PKEY_RSA;
- pkey->rsa = rsa;
- pkey->ownRsa = 0;
-
- if ((derSz = wolfSSL_RSA_To_Der(rsa, &derBuf, 1)) < 0) {
- WOLFSSL_MSG("wolfSSL_RSA_To_Der failed");
- return WOLFSSL_FAILURE;
- }
-
- pkey->pkey.ptr = (char*)XMALLOC(derSz, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (pkey->pkey.ptr == NULL) {
- WOLFSSL_MSG("key malloc failed");
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
- pkey->pkey_sz = derSz;
- XMEMCPY(pkey->pkey.ptr, derBuf, derSz);
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- ret = wolfSSL_PEM_write_bio_PUBKEY(bio, pkey);
- wolfSSL_EVP_PKEY_free(pkey);
-
- return ret;
-}
-#endif
-
-
-/* Reads an RSA public key from a WOLFSSL_BIO into a WOLFSSL_RSA
- * Returns WOLFSSL_SUCCESS or WOLFSSL_FAILURE
- */
-WOLFSSL_RSA *wolfSSL_PEM_read_bio_RSA_PUBKEY(WOLFSSL_BIO* bio,WOLFSSL_RSA** rsa,
- pem_password_cb* cb, void *pass)
-{
- WOLFSSL_EVP_PKEY* pkey;
- WOLFSSL_RSA* local;
-
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_RSA_PUBKEY");
-
- pkey = wolfSSL_PEM_read_bio_PUBKEY(bio, NULL, cb, pass);
- if (pkey == NULL) {
- return NULL;
- }
-
- /* Since the WOLFSSL_RSA structure is being taken from WOLFSSL_EVP_PKEY the
- * flag indicating that the WOLFSSL_RSA structure is owned should be FALSE
- * to avoid having it free'd */
- pkey->ownRsa = 0;
- local = pkey->rsa;
- if (rsa != NULL){
- *rsa = local;
- }
-
- wolfSSL_EVP_PKEY_free(pkey);
- return local;
-}
-
-#endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) && !defined(NO_RSA) */
-
-/* Takes a public key and writes it out to a WOLFSSL_BIO
- * Returns WOLFSSL_SUCCESS or WOLFSSL_FAILURE
- */
-int wolfSSL_PEM_write_bio_PUBKEY(WOLFSSL_BIO* bio, WOLFSSL_EVP_PKEY* key)
-{
- byte* keyDer;
- int pemSz;
- int ret;
- byte* tmp;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_PUBKEY");
-
- if (bio == NULL || key == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- keyDer = (byte*)key->pkey.ptr;
-
- pemSz = wc_DerToPem(keyDer, key->pkey_sz, NULL, 0, PUBLICKEY_TYPE);
- if (pemSz < 0) {
- WOLFSSL_LEAVE("wolfSSL_PEM_write_bio_PUBKEY", pemSz);
- return WOLFSSL_FAILURE;
- }
- tmp = (byte*)XMALLOC(pemSz, bio->heap, DYNAMIC_TYPE_OPENSSL);
- if (tmp == NULL) {
- return MEMORY_E;
- }
-
- ret = wc_DerToPemEx(keyDer, key->pkey_sz, tmp, pemSz,
- NULL, PUBLICKEY_TYPE);
- if (ret < 0) {
- WOLFSSL_LEAVE("wolfSSL_PEM_write_bio_PUBKEY", ret);
- XFREE(tmp, bio->heap, DYNAMIC_TYPE_OPENSSL);
- return WOLFSSL_FAILURE;
- }
-
- ret = wolfSSL_BIO_write(bio, tmp, pemSz);
- XFREE(tmp, bio->heap, DYNAMIC_TYPE_OPENSSL);
- if (ret != pemSz) {
- WOLFSSL_MSG("Unable to write full PEM to BIO");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Takes a private key and writes it out to a WOLFSSL_BIO
- * Returns WOLFSSL_SUCCESS or WOLFSSL_FAILURE
- */
-int wolfSSL_PEM_write_bio_PrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EVP_PKEY* key,
- const WOLFSSL_EVP_CIPHER* cipher,
- unsigned char* passwd, int len,
- pem_password_cb* cb, void* arg)
-{
- byte* keyDer;
- int pemSz;
- int type;
- int ret;
- byte* tmp;
-
- (void)cipher;
- (void)passwd;
- (void)len;
- (void)cb;
- (void)arg;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_PrivateKey");
-
- if (bio == NULL || key == NULL) {
- WOLFSSL_MSG("Bad Function Arguments");
- return WOLFSSL_FAILURE;
- }
-
- keyDer = (byte*)key->pkey.ptr;
-
- switch (key->type) {
-#ifndef NO_RSA
- case EVP_PKEY_RSA:
- type = PRIVATEKEY_TYPE;
- break;
-#endif
-
-#ifndef NO_DSA
- case EVP_PKEY_DSA:
- type = DSA_PRIVATEKEY_TYPE;
- break;
-#endif
-
-#ifdef HAVE_ECC
- case EVP_PKEY_EC:
- type = ECC_PRIVATEKEY_TYPE;
- break;
-#endif
-
-#if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL))
- case EVP_PKEY_DH:
- type = DH_PRIVATEKEY_TYPE;
- break;
-#endif
-
- default:
- WOLFSSL_MSG("Unknown Key type!");
- type = PRIVATEKEY_TYPE;
- }
-
- pemSz = wc_DerToPem(keyDer, key->pkey_sz, NULL, 0, type);
- if (pemSz < 0) {
- WOLFSSL_LEAVE("wolfSSL_PEM_write_bio_PrivateKey", pemSz);
- return WOLFSSL_FAILURE;
- }
- tmp = (byte*)XMALLOC(pemSz, bio->heap, DYNAMIC_TYPE_OPENSSL);
- if (tmp == NULL) {
- return MEMORY_E;
- }
-
- ret = wc_DerToPemEx(keyDer, key->pkey_sz, tmp, pemSz,
- NULL, type);
- if (ret < 0) {
- WOLFSSL_LEAVE("wolfSSL_PEM_write_bio_PrivateKey", ret);
- XFREE(tmp, bio->heap, DYNAMIC_TYPE_OPENSSL);
- return WOLFSSL_FAILURE;
- }
-
- ret = wolfSSL_BIO_write(bio, tmp, pemSz);
- XFREE(tmp, bio->heap, DYNAMIC_TYPE_OPENSSL);
- if (ret != pemSz) {
- WOLFSSL_MSG("Unable to write full PEM to BIO");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN) */
-
-#if (defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)) && \
- (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_PEM_write_mem_RSAPrivateKey(RSA* rsa, const EVP_CIPHER* cipher,
- unsigned char* passwd, int passwdSz,
- unsigned char **pem, int *plen)
-{
- byte *derBuf = NULL, *tmp, *cipherInfo = NULL;
- int derSz = 0;
- const int type = PRIVATEKEY_TYPE;
- const char* header = NULL;
- const char* footer = NULL;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_mem_RSAPrivateKey");
-
- if (pem == NULL || plen == NULL || rsa == NULL || rsa->internal == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FAILURE;
- }
-
- if (wc_PemGetHeaderFooter(type, &header, &footer) != 0)
- return WOLFSSL_FAILURE;
-
- if (rsa->inSet == 0) {
- WOLFSSL_MSG("No RSA internal set, do it");
-
- if (SetRsaInternal(rsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetRsaInternal failed");
- return WOLFSSL_FAILURE;
- }
- }
-
- if ((derSz = wolfSSL_RSA_To_Der(rsa, &derBuf, 0)) < 0) {
- WOLFSSL_MSG("wolfSSL_RSA_To_Der failed");
- return WOLFSSL_FAILURE;
- }
-
- /* encrypt DER buffer if required */
- if (passwd != NULL && passwdSz > 0 && cipher != NULL) {
- int ret;
-
- ret = EncryptDerKey(derBuf, &derSz, cipher,
- passwd, passwdSz, &cipherInfo);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("EncryptDerKey failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- return ret;
- }
-
- /* tmp buffer with a max size */
- *plen = (derSz * 2) + (int)XSTRLEN(header) + 1 +
- (int)XSTRLEN(footer) + 1 + HEADER_ENCRYPTED_KEY_SIZE;
- }
- else {
- /* tmp buffer with a max size */
- *plen = (derSz * 2) + (int)XSTRLEN(header) + 1 +
- (int)XSTRLEN(footer) + 1;
- }
-
- tmp = (byte*)XMALLOC(*plen, NULL, DYNAMIC_TYPE_PEM);
- if (tmp == NULL) {
- WOLFSSL_MSG("malloc failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- if (cipherInfo != NULL)
- XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
- return WOLFSSL_FAILURE;
- }
-
- /* DER to PEM */
- *plen = wc_DerToPemEx(derBuf, derSz, tmp, *plen, cipherInfo, type);
- if (*plen <= 0) {
- WOLFSSL_MSG("wc_DerToPemEx failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
- if (cipherInfo != NULL)
- XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
- return WOLFSSL_FAILURE;
- }
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- if (cipherInfo != NULL)
- XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
-
- *pem = (byte*)XMALLOC((*plen)+1, NULL, DYNAMIC_TYPE_KEY);
- if (*pem == NULL) {
- WOLFSSL_MSG("malloc failed");
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
- return WOLFSSL_FAILURE;
- }
- XMEMSET(*pem, 0, (*plen)+1);
-
- if (XMEMCPY(*pem, tmp, *plen) == NULL) {
- WOLFSSL_MSG("XMEMCPY failed");
- XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
- return WOLFSSL_FAILURE;
- }
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
-
- return WOLFSSL_SUCCESS;
-}
-
-
-#ifndef NO_FILESYSTEM
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_PEM_write_RSAPrivateKey(XFILE fp, WOLFSSL_RSA *rsa,
- const EVP_CIPHER *enc,
- unsigned char *kstr, int klen,
- pem_password_cb *cb, void *u)
-{
- byte *pem;
- int plen, ret;
-
- (void)cb;
- (void)u;
-
- WOLFSSL_MSG("wolfSSL_PEM_write_RSAPrivateKey");
-
- if (fp == XBADFILE || rsa == NULL || rsa->internal == NULL)
- {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FAILURE;
- }
-
- ret = wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, enc, kstr, klen, &pem, &plen);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("wolfSSL_PEM_write_mem_RSAPrivateKey failed");
- return WOLFSSL_FAILURE;
- }
-
- ret = (int)XFWRITE(pem, plen, 1, fp);
- if (ret != 1) {
- WOLFSSL_MSG("RSA private key file write failed");
- return WOLFSSL_FAILURE;
- }
-
- XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
- return WOLFSSL_SUCCESS;
-}
-#endif /* NO_FILESYSTEM */
-#endif /* WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA && WOLFSSL_PEM_TO_DER */
-
-
-#ifdef HAVE_ECC
-
-#ifdef ALT_ECC_SIZE
-static int SetIndividualInternalEcc(WOLFSSL_BIGNUM* bn, mp_int* mpi)
-{
- WOLFSSL_MSG("Entering SetIndividualInternal");
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (mpi == NULL) {
- WOLFSSL_MSG("mpi NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (mp_copy((mp_int*)bn->internal, mpi) != MP_OKAY) {
- WOLFSSL_MSG("mp_copy error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* ALT_ECC_SIZE */
-
-/* EC_POINT Openssl -> WolfSSL */
-static int SetECPointInternal(WOLFSSL_EC_POINT *p)
-{
- ecc_point* point;
- WOLFSSL_ENTER("SetECPointInternal");
-
- if (p == NULL || p->internal == NULL) {
- WOLFSSL_MSG("ECPoint NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- point = (ecc_point*)p->internal;
-
-#ifndef ALT_ECC_SIZE
- if (p->X != NULL && SetIndividualInternal(p->X, point->x) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ecc point X error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (p->Y != NULL && SetIndividualInternal(p->Y, point->y) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ecc point Y error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (p->Z != NULL && SetIndividualInternal(p->Z, point->z) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ecc point Z error");
- return WOLFSSL_FATAL_ERROR;
- }
-#else
- if (p->X != NULL && SetIndividualInternalEcc(p->X, point->x) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ecc point X error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (p->Y != NULL && SetIndividualInternalEcc(p->Y, point->y) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ecc point Y error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (p->Z != NULL && SetIndividualInternalEcc(p->Z, point->z) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ecc point Z error");
- return WOLFSSL_FATAL_ERROR;
- }
-#endif
-
- p->inSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* EC_POINT WolfSSL -> OpenSSL */
-static int SetECPointExternal(WOLFSSL_EC_POINT *p)
-{
- ecc_point* point;
-
- WOLFSSL_ENTER("SetECPointExternal");
-
- if (p == NULL || p->internal == NULL) {
- WOLFSSL_MSG("ECPoint NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- point = (ecc_point*)p->internal;
-
- if (SetIndividualExternal(&p->X, point->x) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ecc point X error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&p->Y, point->y) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ecc point Y error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&p->Z, point->z) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ecc point Z error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- p->exSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* EC_KEY wolfSSL -> OpenSSL */
-int SetECKeyExternal(WOLFSSL_EC_KEY* eckey)
-{
- ecc_key* key;
-
- WOLFSSL_ENTER("SetECKeyExternal");
-
- if (eckey == NULL || eckey->internal == NULL) {
- WOLFSSL_MSG("ec key NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- key = (ecc_key*)eckey->internal;
-
- /* set group (OID, nid and idx) */
- eckey->group->curve_oid = ecc_sets[key->idx].oidSum;
- eckey->group->curve_nid = ecc_sets[key->idx].id;
- eckey->group->curve_idx = key->idx;
-
- if (eckey->pub_key->internal != NULL) {
- /* set the internal public key */
- if (wc_ecc_copy_point(&key->pubkey,
- (ecc_point*)eckey->pub_key->internal) != MP_OKAY) {
- WOLFSSL_MSG("SetECKeyExternal ecc_copy_point failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* set the external pubkey (point) */
- if (SetECPointExternal(eckey->pub_key) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECKeyExternal SetECPointExternal failed");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- /* set the external privkey */
- if (key->type == ECC_PRIVATEKEY) {
- if (SetIndividualExternal(&eckey->priv_key, &key->k) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ec priv key error");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- eckey->exSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* EC_KEY Openssl -> WolfSSL */
-int SetECKeyInternal(WOLFSSL_EC_KEY* eckey)
-{
- ecc_key* key;
-
- WOLFSSL_ENTER("SetECKeyInternal");
-
- if (eckey == NULL || eckey->internal == NULL || eckey->group == NULL) {
- WOLFSSL_MSG("ec key NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- key = (ecc_key*)eckey->internal;
-
- /* validate group */
- if ((eckey->group->curve_idx < 0) ||
- (wc_ecc_is_valid_idx(eckey->group->curve_idx) == 0)) {
- WOLFSSL_MSG("invalid curve idx");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* set group (idx of curve and corresponding domain parameters) */
- key->idx = eckey->group->curve_idx;
- key->dp = &ecc_sets[key->idx];
-
- /* set pubkey (point) */
- if (eckey->pub_key != NULL) {
- if (SetECPointInternal(eckey->pub_key) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ec key pub error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* copy over the public point to key */
- if (wc_ecc_copy_point((ecc_point*)eckey->pub_key->internal, &key->pubkey) != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_copy_point error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* public key */
- key->type = ECC_PUBLICKEY;
- }
-
- /* set privkey */
- if (eckey->priv_key != NULL) {
- if (SetIndividualInternal(eckey->priv_key, &key->k) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("ec key priv error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* private key */
- key->type = ECC_PRIVATEKEY;
- }
-
- eckey->inSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-WOLFSSL_EC_POINT *wolfSSL_EC_KEY_get0_public_key(const WOLFSSL_EC_KEY *key)
-{
- WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_public_key");
-
- if (key == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_get0_public_key Bad arguments");
- return NULL;
- }
-
- return key->pub_key;
-}
-
-const WOLFSSL_EC_GROUP *wolfSSL_EC_KEY_get0_group(const WOLFSSL_EC_KEY *key)
-{
- WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_group");
-
- if (key == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_get0_group Bad arguments");
- return NULL;
- }
-
- return key->group;
-}
-
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_EC_KEY_set_private_key(WOLFSSL_EC_KEY *key,
- const WOLFSSL_BIGNUM *priv_key)
-{
- WOLFSSL_ENTER("wolfSSL_EC_KEY_set_private_key");
-
- if (key == NULL || priv_key == NULL) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* free key if previously set */
- if (key->priv_key != NULL)
- wolfSSL_BN_free(key->priv_key);
-
- key->priv_key = wolfSSL_BN_dup(priv_key);
- if (key->priv_key == NULL) {
- WOLFSSL_MSG("key ecc priv key NULL");
- return WOLFSSL_FAILURE;
- }
-
- if (SetECKeyInternal(key) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECKeyInternal failed");
- wolfSSL_BN_free(key->priv_key);
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-WOLFSSL_BIGNUM *wolfSSL_EC_KEY_get0_private_key(const WOLFSSL_EC_KEY *key)
-{
- WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_private_key");
-
- if (key == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_get0_private_key Bad arguments");
- return NULL;
- }
-
- if (wolfSSL_BN_is_zero(key->priv_key)) {
- /* return NULL if not set */
- return NULL;
- }
-
- return key->priv_key;
-}
-
-WOLFSSL_EC_KEY *wolfSSL_EC_KEY_new_by_curve_name(int nid)
-{
- WOLFSSL_EC_KEY *key;
- int x;
- int eccEnum;
-
- WOLFSSL_ENTER("wolfSSL_EC_KEY_new_by_curve_name");
-
- /* If NID passed in is OpenSSL type, convert it to ecc_curve_id enum */
- eccEnum = NIDToEccEnum(nid);
- if (eccEnum == -1)
- eccEnum = nid;
-
- key = wolfSSL_EC_KEY_new();
- if (key == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_new failure");
- return NULL;
- }
-
- /* set the nid of the curve */
- key->group->curve_nid = eccEnum;
-
- /* search and set the corresponding internal curve idx */
- for (x = 0; ecc_sets[x].size != 0; x++)
- if (ecc_sets[x].id == key->group->curve_nid) {
- key->group->curve_idx = x;
- key->group->curve_oid = ecc_sets[x].oidSum;
- break;
- }
-
- return key;
-}
-
-const char* wolfSSL_EC_curve_nid2nist(int nid)
-{
- const WOLF_EC_NIST_NAME* nist_name;
- for (nist_name = kNistCurves; nist_name->name != NULL; nist_name++) {
- if (nist_name->nid == nid) {
- return kNistCurves->name;
- }
- }
- return NULL;
-}
-
-#ifdef WOLFSSL_TLS13
-static int populate_groups(int* groups, int max_count, char *list)
-{
- char *end;
- int len;
- int count = 0;
- const WOLF_EC_NIST_NAME* nist_name;
-
- if (!groups || !list) {
- return -1;
- }
-
- for (end = list; ; list = ++end) {
- if (count > max_count) {
- WOLFSSL_MSG("Too many curves in list");
- return -1;
- }
- while (*end != ':' && *end != '\0') end++;
- len = (int)(end - list); /* end points to char after end
- * of curve name so no need for -1 */
- if ((len < kNistCurves_MIN_NAME_LEN) ||
- (len > kNistCurves_MAX_NAME_LEN)) {
- WOLFSSL_MSG("Unrecognized curve name in list");
- return -1;
- }
- for (nist_name = kNistCurves; nist_name->name != NULL; nist_name++) {
- if (len == nist_name->name_len &&
- XSTRNCMP(list, nist_name->name, nist_name->name_len) == 0) {
- break;
- }
- }
- if (!nist_name->name) {
- WOLFSSL_MSG("Unrecognized curve name in list");
- return -1;
- }
- groups[count++] = nist_name->nid;
- if (*end == '\0') break;
- }
-
- return count;
-}
-
-int wolfSSL_CTX_set1_groups_list(WOLFSSL_CTX *ctx, char *list)
-{
- int groups[WOLFSSL_MAX_GROUP_COUNT];
- int count;
-
- if (!ctx || !list) {
- return WOLFSSL_FAILURE;
- }
-
- if ((count = populate_groups(groups,
- WOLFSSL_MAX_GROUP_COUNT, list)) == -1) {
- return WOLFSSL_FAILURE;
- }
-
- return wolfSSL_CTX_set_groups(ctx, groups, count) == WOLFSSL_SUCCESS ?
- WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
-}
-
-int wolfSSL_set1_groups_list(WOLFSSL *ssl, char *list)
-{
- int groups[WOLFSSL_MAX_GROUP_COUNT];
- int count;
-
- if (!ssl || !list) {
- return WOLFSSL_FAILURE;
- }
-
- if ((count = populate_groups(groups,
- WOLFSSL_MAX_GROUP_COUNT, list)) == -1) {
- return WOLFSSL_FAILURE;
- }
-
- return wolfSSL_set_groups(ssl, groups, count) == WOLFSSL_SUCCESS ?
- WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
-}
-#endif /* WOLFSSL_TLS13 */
-
-static void InitwolfSSL_ECKey(WOLFSSL_EC_KEY* key)
-{
- if (key) {
- key->group = NULL;
- key->pub_key = NULL;
- key->priv_key = NULL;
- key->internal = NULL;
- key->inSet = 0;
- key->exSet = 0;
- }
-}
-
-WOLFSSL_EC_KEY *wolfSSL_EC_KEY_new(void)
-{
- WOLFSSL_EC_KEY *external;
- WOLFSSL_ENTER("wolfSSL_EC_KEY_new");
-
- external = (WOLFSSL_EC_KEY*)XMALLOC(sizeof(WOLFSSL_EC_KEY), NULL,
- DYNAMIC_TYPE_ECC);
- if (external == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc WOLFSSL_EC_KEY failure");
- return NULL;
- }
- XMEMSET(external, 0, sizeof(WOLFSSL_EC_KEY));
-
- InitwolfSSL_ECKey(external);
-
- external->internal = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL,
- DYNAMIC_TYPE_ECC);
- if (external->internal == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc ecc key failure");
- goto error;
- }
- XMEMSET(external->internal, 0, sizeof(ecc_key));
-
- if (wc_ecc_init((ecc_key*)external->internal) != 0) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_new init ecc key failure");
- goto error;
- }
-
- /* curve group */
- external->group = wolfSSL_EC_GROUP_new_by_curve_name(ECC_CURVE_DEF);
- if (external->group == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc WOLFSSL_EC_GROUP failure");
- goto error;
- }
-
- /* public key */
- external->pub_key = wolfSSL_EC_POINT_new(external->group);
- if (external->pub_key == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_POINT_new failure");
- goto error;
- }
-
- /* private key */
- external->priv_key = wolfSSL_BN_new();
- if (external->priv_key == NULL) {
- WOLFSSL_MSG("wolfSSL_BN_new failure");
- goto error;
- }
-
- return external;
-error:
- wolfSSL_EC_KEY_free(external);
- return NULL;
-}
-
-void wolfSSL_EC_KEY_free(WOLFSSL_EC_KEY *key)
-{
- WOLFSSL_ENTER("wolfSSL_EC_KEY_free");
-
- if (key != NULL) {
- if (key->internal != NULL) {
- wc_ecc_free((ecc_key*)key->internal);
- XFREE(key->internal, NULL, DYNAMIC_TYPE_ECC);
- }
- wolfSSL_BN_free(key->priv_key);
- wolfSSL_EC_POINT_free(key->pub_key);
- wolfSSL_EC_GROUP_free(key->group);
- InitwolfSSL_ECKey(key); /* set back to NULLs for safety */
-
- XFREE(key, NULL, DYNAMIC_TYPE_ECC);
- /* key = NULL, don't try to access or double free it */
- }
-}
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_EC_KEY_set_group(WOLFSSL_EC_KEY *key, WOLFSSL_EC_GROUP *group)
-{
- (void)key;
- (void)group;
-
- WOLFSSL_ENTER("wolfSSL_EC_KEY_set_group");
- WOLFSSL_STUB("EC_KEY_set_group");
-
- return -1;
-}
-#endif
-
-int wolfSSL_EC_KEY_generate_key(WOLFSSL_EC_KEY *key)
-{
- int initTmpRng = 0;
- WC_RNG* rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG = NULL;
-#else
- WC_RNG tmpRNG[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_EC_KEY_generate_key");
-
- if (key == NULL || key->internal == NULL ||
- key->group == NULL || key->group->curve_idx < 0) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key Bad arguments");
- return 0;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return 0;
-#endif
-
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key failed to set RNG");
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
- return 0;
- }
-
- if (wc_ecc_make_key_ex(rng, 0, (ecc_key*)key->internal,
- key->group->curve_nid) != MP_OKAY) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key wc_ecc_make_key failed");
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
- return 0;
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- if (SetECKeyExternal(key) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key SetECKeyExternal failed");
- return 0;
- }
-
- return 1;
-}
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_EC_KEY_set_asn1_flag(WOLFSSL_EC_KEY *key, int asn1_flag)
-{
- (void)key;
- (void)asn1_flag;
-
- WOLFSSL_ENTER("wolfSSL_EC_KEY_set_asn1_flag");
- WOLFSSL_STUB("EC_KEY_set_asn1_flag");
-}
-#endif
-
-static int setupPoint(const WOLFSSL_EC_POINT *p) {
- if (!p) {
- return WOLFSSL_FAILURE;
- }
- if (p->inSet == 0) {
- WOLFSSL_MSG("No ECPoint internal set, do it");
-
- if (SetECPointInternal((WOLFSSL_EC_POINT *)p) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECPointInternal SetECPointInternal failed");
- return WOLFSSL_FAILURE;
- }
- }
- return WOLFSSL_SUCCESS;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_EC_KEY_set_public_key(WOLFSSL_EC_KEY *key,
- const WOLFSSL_EC_POINT *pub)
-{
- ecc_point *pub_p, *key_p;
-
- WOLFSSL_ENTER("wolfSSL_EC_KEY_set_public_key");
-
- if (key == NULL || key->internal == NULL ||
- pub == NULL || pub->internal == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- if (key->inSet == 0) {
- if (SetECKeyInternal(key) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECKeyInternal failed");
- return WOLFSSL_FAILURE;
- }
- }
-
- if (setupPoint(pub) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- pub_p = (ecc_point*)pub->internal;
- key_p = (ecc_point*)key->pub_key->internal;
-
- /* create new point if required */
- if (key_p == NULL)
- key_p = wc_ecc_new_point();
-
- if (key_p == NULL) {
- WOLFSSL_MSG("key ecc point NULL");
- return WOLFSSL_FAILURE;
- }
-
- if (wc_ecc_copy_point(pub_p, key_p) != MP_OKAY) {
- WOLFSSL_MSG("ecc_copy_point failure");
- return WOLFSSL_FAILURE;
- }
-
- if (SetECPointExternal(key->pub_key) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECKeyInternal failed");
- return WOLFSSL_FAILURE;
- }
-
- if (SetECKeyInternal(key) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECKeyInternal failed");
- return WOLFSSL_FAILURE;
- }
-
- wolfSSL_EC_POINT_dump("pub", pub);
- wolfSSL_EC_POINT_dump("key->pub_key", key->pub_key);
-
- return WOLFSSL_SUCCESS;
-}
-/* End EC_KEY */
-
-int wolfSSL_ECDSA_size(const WOLFSSL_EC_KEY *key)
-{
- const EC_GROUP *group;
- int bits, bytes;
- word32 headerSz = 4; /* 2*ASN_TAG + 2*LEN(ENUM) */
-
- if (!key) {
- return WOLFSSL_FAILURE;
- }
-
- if (!(group = wolfSSL_EC_KEY_get0_group(key))) {
- return WOLFSSL_FAILURE;
- }
- if ((bits = wolfSSL_EC_GROUP_order_bits(group)) == 0) {
- return WOLFSSL_FAILURE;
- }
- bytes = (bits + 7) / 8; /* bytes needed to hold bits */
- return headerSz +
- 2 + /* possible leading zeroes in r and s */
- bytes + bytes + /* r and s */
- 2;
-}
-
-int wolfSSL_ECDSA_sign(int type, const unsigned char *digest,
- int digestSz, unsigned char *sig,
- unsigned int *sigSz, WOLFSSL_EC_KEY *key)
-{
- int ret = WOLFSSL_SUCCESS;
- WC_RNG* rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG = NULL;
-#else
- WC_RNG tmpRNG[1];
-#endif
- int initTmpRng = 0;
-
- WOLFSSL_ENTER("wolfSSL_ECDSA_sign");
-
- if (!key) {
- return WOLFSSL_FAILURE;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return WOLFSSL_FAILURE;
-#endif
-
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0) {
- WOLFSSL_MSG("Global RNG no Init");
- }
- else {
- rng = &globalRNG;
- }
- }
- if (rng) {
- if (wc_ecc_sign_hash(digest, digestSz, sig, sigSz, rng, (ecc_key*)key->internal) != MP_OKAY) {
- ret = WOLFSSL_FAILURE;
- }
- if (initTmpRng) {
- wc_FreeRng(tmpRNG);
- }
- } else {
- ret = WOLFSSL_FAILURE;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- if (tmpRNG)
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- (void)type;
- return ret;
-}
-
-#ifndef HAVE_SELFTEST
-/* ECC point compression types were not included in selftest ecc.h */
-
-char* wolfSSL_EC_POINT_point2hex(const WOLFSSL_EC_GROUP* group,
- const WOLFSSL_EC_POINT* point, int form,
- WOLFSSL_BN_CTX* ctx)
-{
- static const char* hexDigit = "0123456789ABCDEF";
- char* hex = NULL;
- int id;
- int i, sz, len;
-
- (void)ctx;
-
- if (group == NULL || point == NULL)
- return NULL;
-
- id = wc_ecc_get_curve_id(group->curve_idx);
-
- if ((sz = wc_ecc_get_curve_size_from_id(id)) < 0)
- return NULL;
-
- len = sz + 1;
- if (form == POINT_CONVERSION_UNCOMPRESSED)
- len += sz;
-
- hex = (char*)XMALLOC(2 * len + 1, NULL, DYNAMIC_TYPE_ECC);
- if (hex == NULL)
- return NULL;
- XMEMSET(hex, 0, 2 * len + 1);
-
- /* Put in x-ordinate after format byte. */
- i = sz - mp_unsigned_bin_size((mp_int*)point->X->internal) + 1;
- if (mp_to_unsigned_bin((mp_int*)point->X->internal, (byte*)(hex + i)) < 0) {
- XFREE(hex, NULL, DYNAMIC_TYPE_ECC);
- return NULL;
- }
-
- if (form == POINT_CONVERSION_COMPRESSED) {
- hex[0] = mp_isodd((mp_int*)point->Y->internal) ? ECC_POINT_COMP_ODD :
- ECC_POINT_COMP_EVEN;
- }
- else {
- hex[0] = ECC_POINT_UNCOMP;
- /* Put in y-ordinate after x-ordinate */
- i = 1 + 2 * sz - mp_unsigned_bin_size((mp_int*)point->Y->internal);
- if (mp_to_unsigned_bin((mp_int*)point->Y->internal,
- (byte*)(hex + i)) < 0) {
- XFREE(hex, NULL, DYNAMIC_TYPE_ECC);
- return NULL;
- }
- }
-
- for (i = len-1; i >= 0; i--) {
- byte b = hex[i];
- hex[i * 2 + 1] = hexDigit[b & 0xf];
- hex[i * 2 ] = hexDigit[b >> 4];
- }
-
- return hex;
-}
-
-#endif /* HAVE_SELFTEST */
-
-void wolfSSL_EC_POINT_dump(const char *msg, const WOLFSSL_EC_POINT *p)
-{
-#if defined(DEBUG_WOLFSSL)
- char *num;
-
- WOLFSSL_ENTER("wolfSSL_EC_POINT_dump");
-
- if (!WOLFSSL_IS_DEBUG_ON() || wolfSSL_GetLoggingCb()) {
- return;
- }
-
- if (p == NULL) {
- printf("%s = NULL", msg);
- return;
- }
-
- printf("%s:\n\tinSet=%d, exSet=%d\n", msg, p->inSet, p->exSet);
- num = wolfSSL_BN_bn2hex(p->X);
- printf("\tX = %s\n", num);
- XFREE(num, NULL, DYNAMIC_TYPE_ECC);
- num = wolfSSL_BN_bn2hex(p->Y);
- printf("\tY = %s\n", num);
- XFREE(num, NULL, DYNAMIC_TYPE_ECC);
- num = wolfSSL_BN_bn2hex(p->Z);
- printf("\tZ = %s\n", num);
- XFREE(num, NULL, DYNAMIC_TYPE_ECC);
-#else
- (void)msg;
- (void)p;
-#endif
-}
-
-/* Start EC_GROUP */
-
-/* return code compliant with OpenSSL :
- * 0 if equal, 1 if not and -1 in case of error
- */
-int wolfSSL_EC_GROUP_cmp(const WOLFSSL_EC_GROUP *a, const WOLFSSL_EC_GROUP *b,
- WOLFSSL_BN_CTX *ctx)
-{
- (void)ctx;
-
- WOLFSSL_ENTER("wolfSSL_EC_GROUP_cmp");
-
- if (a == NULL || b == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_GROUP_cmp Bad arguments");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* ok */
- if ((a->curve_idx == b->curve_idx) && (a->curve_nid == b->curve_nid))
- return 0;
-
- /* ko */
- return 1;
-}
-
-#endif /* HAVE_ECC */
-#endif /* OPENSSL_EXTRA */
-
-#if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
-const WOLFSSL_EC_METHOD* wolfSSL_EC_GROUP_method_of(
- const WOLFSSL_EC_GROUP *group)
-{
- return group;
-}
-
-int wolfSSL_EC_METHOD_get_field_type(const WOLFSSL_EC_METHOD *meth)
-{
- if (meth) {
- return NID_X9_62_prime_field;
- }
- return WOLFSSL_FAILURE;
-}
-
-void wolfSSL_EC_GROUP_free(WOLFSSL_EC_GROUP *group)
-{
- WOLFSSL_ENTER("wolfSSL_EC_GROUP_free");
-
- XFREE(group, NULL, DYNAMIC_TYPE_ECC);
- /* group = NULL, don't try to access or double free it */
-}
-#endif
-
-#ifdef OPENSSL_EXTRA
-#ifdef HAVE_ECC
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_EC_GROUP_set_asn1_flag(WOLFSSL_EC_GROUP *group, int flag)
-{
- (void)group;
- (void)flag;
-
- WOLFSSL_ENTER("wolfSSL_EC_GROUP_set_asn1_flag");
- WOLFSSL_STUB("EC_GROUP_set_asn1_flag");
-}
-#endif
-
-WOLFSSL_EC_GROUP *wolfSSL_EC_GROUP_new_by_curve_name(int nid)
-{
- WOLFSSL_EC_GROUP *g;
- int x;
- int eccEnum;
-
- WOLFSSL_ENTER("wolfSSL_EC_GROUP_new_by_curve_name");
-
- /* If NID passed in is OpenSSL type, convert it to ecc_curve_id enum */
- eccEnum = NIDToEccEnum(nid);
- if (eccEnum == -1)
- eccEnum = nid;
-
-
- /* curve group */
- g = (WOLFSSL_EC_GROUP*) XMALLOC(sizeof(WOLFSSL_EC_GROUP), NULL,
- DYNAMIC_TYPE_ECC);
- if (g == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_GROUP_new_by_curve_name malloc failure");
- return NULL;
- }
- XMEMSET(g, 0, sizeof(WOLFSSL_EC_GROUP));
-
- /* set the nid of the curve */
- g->curve_nid = eccEnum;
-
- if (eccEnum > ECC_CURVE_DEF) {
- /* search and set the corresponding internal curve idx */
- for (x = 0; ecc_sets[x].size != 0; x++)
- if (ecc_sets[x].id == g->curve_nid) {
- g->curve_idx = x;
- g->curve_oid = ecc_sets[x].oidSum;
- break;
- }
- }
-
- return g;
-}
-
-/* return code compliant with OpenSSL :
- * the curve nid if success, 0 if error
- */
-int wolfSSL_EC_GROUP_get_curve_name(const WOLFSSL_EC_GROUP *group)
-{
- int nid;
- WOLFSSL_ENTER("wolfSSL_EC_GROUP_get_curve_name");
-
- if (group == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_GROUP_get_curve_name Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* If curve_nid is ECC Enum type, return corresponding OpenSSL nid */
- if ((nid = EccEnumToNID(group->curve_nid)) != -1)
- return nid;
-
- return group->curve_nid;
-}
-
-/* return code compliant with OpenSSL :
- * the degree of the curve if success, 0 if error
- */
-int wolfSSL_EC_GROUP_get_degree(const WOLFSSL_EC_GROUP *group)
-{
- int nid;
- int tmp;
-
- WOLFSSL_ENTER("wolfSSL_EC_GROUP_get_degree");
-
- if (group == NULL || group->curve_idx < 0) {
- WOLFSSL_MSG("wolfSSL_EC_GROUP_get_degree Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* If curve_nid passed in is an ecc_curve_id enum, convert it to the
- corresponding OpenSSL NID */
- tmp = EccEnumToNID(group->curve_nid);
- if (tmp != -1){
- nid = tmp;
- }
- else{
- nid = group->curve_nid;
- }
-
- switch(nid) {
- case NID_secp112r1:
- case NID_secp112r2:
- return 112;
- case NID_secp128r1:
- case NID_secp128r2:
- return 128;
- case NID_secp160k1:
- case NID_secp160r1:
- case NID_secp160r2:
- case NID_brainpoolP160r1:
- return 160;
- case NID_secp192k1:
- case NID_brainpoolP192r1:
- case NID_X9_62_prime192v1:
- return 192;
- case NID_secp224k1:
- case NID_secp224r1:
- case NID_brainpoolP224r1:
- return 224;
- case NID_secp256k1:
- case NID_brainpoolP256r1:
- case NID_X9_62_prime256v1:
- return 256;
- case NID_brainpoolP320r1:
- return 320;
- case NID_secp384r1:
- case NID_brainpoolP384r1:
- return 384;
- case NID_secp521r1:
- return 521;
- case NID_brainpoolP512r1:
- return 512;
- default:
- return WOLFSSL_FAILURE;
- }
-}
-
-/* Converts OpenSSL NID value of ECC curves to the associated enum values in
- ecc_curve_id, used by ecc_sets[].*/
-int NIDToEccEnum(int n)
-{
- WOLFSSL_ENTER("NIDToEccEnum()");
-
- switch(n) {
- case NID_X9_62_prime192v1:
- return ECC_SECP192R1;
- case NID_X9_62_prime192v2:
- return ECC_PRIME192V2;
- case NID_X9_62_prime192v3:
- return ECC_PRIME192V3;
- case NID_X9_62_prime239v1:
- return ECC_PRIME239V1;
- case NID_X9_62_prime239v2:
- return ECC_PRIME239V2;
- case NID_X9_62_prime239v3:
- return ECC_PRIME239V3;
- case NID_X9_62_prime256v1:
- return ECC_SECP256R1;
- case NID_secp112r1:
- return ECC_SECP112R1;
- case NID_secp112r2:
- return ECC_SECP112R2;
- case NID_secp128r1:
- return ECC_SECP128R1;
- case NID_secp128r2:
- return ECC_SECP128R2;
- case NID_secp160r1:
- return ECC_SECP160R1;
- case NID_secp160r2:
- return ECC_SECP160R2;
- case NID_secp224r1:
- return ECC_SECP224R1;
- case NID_secp384r1:
- return ECC_SECP384R1;
- case NID_secp521r1:
- return ECC_SECP521R1;
- case NID_secp160k1:
- return ECC_SECP160K1;
- case NID_secp192k1:
- return ECC_SECP192K1;
- case NID_secp224k1:
- return ECC_SECP224K1;
- case NID_secp256k1:
- return ECC_SECP256K1;
- case NID_brainpoolP160r1:
- return ECC_BRAINPOOLP160R1;
- case NID_brainpoolP192r1:
- return ECC_BRAINPOOLP192R1;
- case NID_brainpoolP224r1:
- return ECC_BRAINPOOLP224R1;
- case NID_brainpoolP256r1:
- return ECC_BRAINPOOLP256R1;
- case NID_brainpoolP320r1:
- return ECC_BRAINPOOLP320R1;
- case NID_brainpoolP384r1:
- return ECC_BRAINPOOLP384R1;
- case NID_brainpoolP512r1:
- return ECC_BRAINPOOLP512R1;
- default:
- WOLFSSL_MSG("NID not found");
- return -1;
- }
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_EC_GROUP_get_order(const WOLFSSL_EC_GROUP *group,
- WOLFSSL_BIGNUM *order, WOLFSSL_BN_CTX *ctx)
-{
- (void)ctx;
-
- if (group == NULL || order == NULL || order->internal == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (mp_init((mp_int*)order->internal) != MP_OKAY) {
- WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order mp_init failure");
- return WOLFSSL_FAILURE;
- }
-
- if (mp_read_radix((mp_int*)order->internal,
- ecc_sets[group->curve_idx].order, MP_RADIX_HEX) != MP_OKAY) {
- WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order mp_read order failure");
- mp_clear((mp_int*)order->internal);
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_EC_GROUP_order_bits(const WOLFSSL_EC_GROUP *group)
-{
- int ret;
- mp_int order;
-
- if (group == NULL || group->curve_idx < 0) {
- WOLFSSL_MSG("wolfSSL_EC_GROUP_order_bits NULL error");
- return 0;
- }
-
- ret = mp_init(&order);
- if (ret == 0) {
- ret = mp_read_radix(&order, ecc_sets[group->curve_idx].order,
- MP_RADIX_HEX);
- if (ret == 0)
- ret = mp_count_bits(&order);
- mp_clear(&order);
- }
-
- return ret;
-}
-
-/* End EC_GROUP */
-
-/* Start EC_POINT */
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_ECPoint_i2d(const WOLFSSL_EC_GROUP *group,
- const WOLFSSL_EC_POINT *p,
- unsigned char *out, unsigned int *len)
-{
- int err;
-
- WOLFSSL_ENTER("wolfSSL_ECPoint_i2d");
-
- if (group == NULL || p == NULL || len == NULL) {
- WOLFSSL_MSG("wolfSSL_ECPoint_i2d NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (setupPoint(p) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- if (out != NULL) {
- wolfSSL_EC_POINT_dump("i2d p", p);
- }
-
- err = wc_ecc_export_point_der(group->curve_idx, (ecc_point*)p->internal,
- out, len);
- if (err != MP_OKAY && !(out == NULL && err == LENGTH_ONLY_E)) {
- WOLFSSL_MSG("wolfSSL_ECPoint_i2d wc_ecc_export_point_der failed");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_ECPoint_d2i(unsigned char *in, unsigned int len,
- const WOLFSSL_EC_GROUP *group, WOLFSSL_EC_POINT *p)
-{
- WOLFSSL_ENTER("wolfSSL_ECPoint_d2i");
-
- if (group == NULL || p == NULL || p->internal == NULL || in == NULL) {
- WOLFSSL_MSG("wolfSSL_ECPoint_d2i NULL error");
- return WOLFSSL_FAILURE;
- }
-
-#ifndef HAVE_SELFTEST
- if (wc_ecc_import_point_der_ex(in, len, group->curve_idx,
- (ecc_point*)p->internal, 0) != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_import_point_der_ex failed");
- return WOLFSSL_FAILURE;
- }
-#else
- /* ECC_POINT_UNCOMP is not defined CAVP self test so use magic number */
- if (in[0] == 0x04) {
- if (wc_ecc_import_point_der(in, len, group->curve_idx,
- (ecc_point*)p->internal) != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_import_point_der failed");
- return WOLFSSL_FAILURE;
- }
- }
- else {
- WOLFSSL_MSG("Only uncompressed points supported with HAVE_SELFTEST");
- return WOLFSSL_FAILURE;
- }
-#endif
-
- /* Set new external point */
- if (SetECPointExternal(p) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECPointExternal failed");
- return WOLFSSL_FAILURE;
- }
-
- wolfSSL_EC_POINT_dump("d2i p", p);
-
- return WOLFSSL_SUCCESS;
-}
-
-size_t wolfSSL_EC_POINT_point2oct(const WOLFSSL_EC_GROUP *group,
- const WOLFSSL_EC_POINT *p,
- char form,
- byte *buf, size_t len, WOLFSSL_BN_CTX *ctx)
-{
- word32 min_len = (word32)len;
-#ifndef HAVE_SELFTEST
- int compressed = form == POINT_CONVERSION_COMPRESSED ? 1 : 0;
-#endif /* !HAVE_SELFTEST */
-
- WOLFSSL_ENTER("EC_POINT_point2oct");
-
- if (!group || !p) {
- return WOLFSSL_FAILURE;
- }
-
- if (setupPoint(p) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_EC_POINT_is_at_infinity(group, p)) {
- /* encodes to a single 0 octet */
- if (buf != NULL) {
- if (len < 1) {
- ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
- return WOLFSSL_FAILURE;
- }
- buf[0] = 0;
- }
- return 1;
- }
-
- if (form != POINT_CONVERSION_UNCOMPRESSED
-#ifndef HAVE_SELFTEST
- && form != POINT_CONVERSION_COMPRESSED
-#endif /* !HAVE_SELFTEST */
- ) {
- WOLFSSL_MSG("Unsupported curve form");
- return WOLFSSL_FAILURE;
- }
-
-#ifndef HAVE_SELFTEST
- if (wc_ecc_export_point_der_ex(group->curve_idx, (ecc_point*)p->internal,
- buf, &min_len, compressed) != (buf ? MP_OKAY : LENGTH_ONLY_E)) {
- return WOLFSSL_FAILURE;
- }
-#else
- if (wc_ecc_export_point_der(group->curve_idx, (ecc_point*)p->internal,
- buf, &min_len) != (buf ? MP_OKAY : LENGTH_ONLY_E)) {
- return WOLFSSL_FAILURE;
- }
-#endif /* !HAVE_SELFTEST */
-
- (void)ctx;
-
- return (size_t)min_len;
-}
-
-int wolfSSL_EC_POINT_oct2point(const WOLFSSL_EC_GROUP *group,
- WOLFSSL_EC_POINT *p, const unsigned char *buf,
- size_t len, WOLFSSL_BN_CTX *ctx)
-{
- WOLFSSL_ENTER("wolfSSL_EC_POINT_oct2point");
-
- if (!group || !p) {
- return WOLFSSL_FAILURE;
- }
-
- (void)ctx;
-
- return wolfSSL_ECPoint_d2i((unsigned char*)buf, (unsigned int)len, group, p);
-}
-
-int wolfSSL_i2o_ECPublicKey(const WOLFSSL_EC_KEY *in, unsigned char **out)
-{
- size_t len;
- unsigned char *tmp = NULL;
- char form;
- WOLFSSL_ENTER("wolfSSL_i2o_ECPublicKey");
-
- if (!in) {
- WOLFSSL_MSG("wolfSSL_i2o_ECPublicKey Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* Default to compressed form if not set */
- form = in->form == POINT_CONVERSION_UNCOMPRESSED ?
- POINT_CONVERSION_UNCOMPRESSED:
- POINT_CONVERSION_COMPRESSED;
-
- len = wolfSSL_EC_POINT_point2oct(in->group, in->pub_key, form,
- NULL, 0, NULL);
-
- if (len != WOLFSSL_FAILURE && out) {
- if (!*out) {
- if (!(tmp = (unsigned char*)XMALLOC(len, NULL,
- DYNAMIC_TYPE_OPENSSL))) {
- WOLFSSL_MSG("malloc failed");
- return WOLFSSL_FAILURE;
- }
- *out = tmp;
- }
-
- if (wolfSSL_EC_POINT_point2oct(in->group, in->pub_key, form, *out,
- len, NULL) == WOLFSSL_FAILURE) {
- if (tmp) {
- XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
- *out = NULL;
- }
- return WOLFSSL_FAILURE;
- }
-
- if (!tmp) {
- /* Move buffer forward if it was not alloced in this function */
- *out += len;
- }
- }
-
- return (int)len;
-}
-
-void wolfSSL_EC_KEY_set_conv_form(WOLFSSL_EC_KEY *eckey, char form)
-{
- if (eckey && (form == POINT_CONVERSION_COMPRESSED ||
- form == POINT_CONVERSION_UNCOMPRESSED)) {
- eckey->form = form;
- }
-}
-
-
-/* wolfSSL_EC_POINT_point2bn should return "in" if not null */
-WOLFSSL_BIGNUM *wolfSSL_EC_POINT_point2bn(const WOLFSSL_EC_GROUP *group,
- const WOLFSSL_EC_POINT *p,
- char form,
- WOLFSSL_BIGNUM *in, WOLFSSL_BN_CTX *ctx)
-{
- size_t len;
- byte *buf;
- WOLFSSL_BIGNUM *ret = NULL;
-
- WOLFSSL_ENTER("wolfSSL_EC_POINT_oct2point");
-
- if (!group || !p) {
- return NULL;
- }
-
- if ((len = wolfSSL_EC_POINT_point2oct(group, p, form,
- NULL, 0, ctx)) == WOLFSSL_FAILURE) {
- return NULL;
- }
-
- if (!(buf = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_TMP_BUFFER))) {
- WOLFSSL_MSG("malloc failed");
- return NULL;
- }
-
- if (wolfSSL_EC_POINT_point2oct(group, p, form,
- buf, len, ctx) == len) {
- ret = wolfSSL_BN_bin2bn(buf, (int)len, in);
- }
-
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- return ret;
-}
-
-WOLFSSL_EC_POINT *wolfSSL_EC_POINT_new(const WOLFSSL_EC_GROUP *group)
-{
- WOLFSSL_EC_POINT *p;
-
- WOLFSSL_ENTER("wolfSSL_EC_POINT_new");
-
- if (group == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_POINT_new NULL error");
- return NULL;
- }
-
- p = (WOLFSSL_EC_POINT *)XMALLOC(sizeof(WOLFSSL_EC_POINT), NULL,
- DYNAMIC_TYPE_ECC);
- if (p == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_POINT_new malloc ecc point failure");
- return NULL;
- }
- XMEMSET(p, 0, sizeof(WOLFSSL_EC_POINT));
-
- p->internal = wc_ecc_new_point();
- if (p->internal == NULL) {
- WOLFSSL_MSG("ecc_new_point failure");
- XFREE(p, NULL, DYNAMIC_TYPE_ECC);
- return NULL;
- }
-
- return p;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_EC_POINT_get_affine_coordinates_GFp(const WOLFSSL_EC_GROUP *group,
- const WOLFSSL_EC_POINT *point,
- WOLFSSL_BIGNUM *x,
- WOLFSSL_BIGNUM *y,
- WOLFSSL_BN_CTX *ctx)
-{
- mp_digit mp;
- mp_int modulus;
- (void)ctx;
-
- WOLFSSL_ENTER("wolfSSL_EC_POINT_get_affine_coordinates_GFp");
-
- if (group == NULL || point == NULL || point->internal == NULL ||
- x == NULL || y == NULL || wolfSSL_EC_POINT_is_at_infinity(group, point)) {
- WOLFSSL_MSG("wolfSSL_EC_POINT_get_affine_coordinates_GFp NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (setupPoint(point) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- if (!wolfSSL_BN_is_one(point->Z)) {
- if (mp_init(&modulus) != MP_OKAY) {
- WOLFSSL_MSG("mp_init failed");
- return WOLFSSL_FAILURE;
- }
- /* Map the Jacobian point back to affine space */
- if (mp_read_radix(&modulus, ecc_sets[group->curve_idx].prime, MP_RADIX_HEX) != MP_OKAY) {
- WOLFSSL_MSG("mp_read_radix failed");
- mp_clear(&modulus);
- return WOLFSSL_FAILURE;
- }
- if (mp_montgomery_setup(&modulus, &mp) != MP_OKAY) {
- WOLFSSL_MSG("mp_montgomery_setup failed");
- mp_clear(&modulus);
- return WOLFSSL_FAILURE;
- }
- if (ecc_map((ecc_point*)point->internal, &modulus, mp) != MP_OKAY) {
- WOLFSSL_MSG("ecc_map failed");
- mp_clear(&modulus);
- return WOLFSSL_FAILURE;
- }
- if (SetECPointExternal((WOLFSSL_EC_POINT *)point) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECPointExternal failed");
- mp_clear(&modulus);
- return WOLFSSL_FAILURE;
- }
- }
-
- BN_copy(x, point->X);
- BN_copy(y, point->Y);
- mp_clear(&modulus);
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_EC_POINT_set_affine_coordinates_GFp(const WOLFSSL_EC_GROUP *group,
- WOLFSSL_EC_POINT *point,
- const WOLFSSL_BIGNUM *x,
- const WOLFSSL_BIGNUM *y,
- WOLFSSL_BN_CTX *ctx)
-{
- (void)ctx;
- WOLFSSL_ENTER("wolfSSL_EC_POINT_set_affine_coordinates_GFp");
-
- if (group == NULL || point == NULL || point->internal == NULL ||
- x == NULL || y == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_POINT_set_affine_coordinates_GFp NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (!point->X) {
- point->X = wolfSSL_BN_new();
- }
- if (!point->Y) {
- point->Y = wolfSSL_BN_new();
- }
- if (!point->Z) {
- point->Z = wolfSSL_BN_new();
- }
- if (!point->X || !point->Y || !point->Z) {
- WOLFSSL_MSG("wolfSSL_BN_new failed");
- return WOLFSSL_FAILURE;
- }
-
- BN_copy(point->X, x);
- BN_copy(point->Y, y);
- BN_copy(point->Z, wolfSSL_BN_value_one());
-
- if (SetECPointInternal((WOLFSSL_EC_POINT *)point) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECPointInternal failed");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-#if !defined(WOLFSSL_ATECC508A) && !defined(HAVE_SELFTEST)
-/* Calculate the value: generator * n + q * m
- * return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_EC_POINT_mul(const WOLFSSL_EC_GROUP *group, WOLFSSL_EC_POINT *r,
- const WOLFSSL_BIGNUM *n, const WOLFSSL_EC_POINT *q,
- const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
-{
- mp_int a, prime;
- int ret = WOLFSSL_FAILURE;
- ecc_point* result = NULL;
- ecc_point* tmp = NULL;
-
- (void)ctx;
-
- WOLFSSL_ENTER("wolfSSL_EC_POINT_mul");
-
- if (!group || !r) {
- WOLFSSL_MSG("wolfSSL_EC_POINT_mul NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (!(result = wc_ecc_new_point())) {
- WOLFSSL_MSG("wolfSSL_EC_POINT_new error");
- return WOLFSSL_FAILURE;
- }
-
- /* read the curve prime and a */
- if (mp_init_multi(&prime, &a, NULL, NULL, NULL, NULL) != MP_OKAY) {
- WOLFSSL_MSG("mp_init_multi error");
- goto cleanup;
- }
-
- if (q && setupPoint(q) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("setupPoint error");
- goto cleanup;
- }
-
- if (mp_read_radix(&prime, ecc_sets[group->curve_idx].prime, MP_RADIX_HEX)
- != MP_OKAY) {
- WOLFSSL_MSG("mp_read_radix prime error");
- goto cleanup;
- }
-
- if (mp_read_radix(&a, ecc_sets[group->curve_idx].Af, MP_RADIX_HEX)
- != MP_OKAY) {
- WOLFSSL_MSG("mp_read_radix a error");
- goto cleanup;
- }
-
- if (n) {
- /* load generator */
- if (wc_ecc_get_generator(result, group->curve_idx)
- != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_get_generator error");
- goto cleanup;
- }
- }
-
- if (n && q && m) {
- /* r = generator * n + q * m */
-#ifdef ECC_SHAMIR
- if (ecc_mul2add(result, (mp_int*)n->internal,
- (ecc_point*)q->internal, (mp_int*)m->internal,
- result, &a, &prime, NULL)
- != MP_OKAY) {
- WOLFSSL_MSG("ecc_mul2add error");
- goto cleanup;
- }
-#else
- mp_digit mp = 0;
- if (mp_montgomery_setup(&prime, &mp) != MP_OKAY) {
- WOLFSSL_MSG("mp_montgomery_setup nqm error");
- goto cleanup;
- }
- if (!(tmp = wc_ecc_new_point())) {
- WOLFSSL_MSG("wolfSSL_EC_POINT_new nqm error");
- goto cleanup;
- }
- /* r = generator * n */
- if (wc_ecc_mulmod((mp_int*)n->internal, result, result, &a, &prime, 1)
- != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_mulmod nqm error");
- goto cleanup;
- }
- /* tmp = q * m */
- if (wc_ecc_mulmod((mp_int*)m->internal, (ecc_point*)q->internal,
- tmp, &a, &prime, 1) != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_mulmod nqm error");
- goto cleanup;
- }
- /* result = result + tmp */
- if (ecc_projective_add_point(tmp, result, result, &a, &prime, mp)
- != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_mulmod nqm error");
- goto cleanup;
- }
- if (ecc_map(result, &prime, mp) != MP_OKAY) {
- WOLFSSL_MSG("ecc_map nqm error");
- goto cleanup;
- }
-#endif
- }
- else if (n) {
- /* r = generator * n */
- if (wc_ecc_mulmod((mp_int*)n->internal, result, result, &a, &prime, 1)
- != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_mulmod gn error");
- goto cleanup;
- }
- }
- else if (q && m) {
- /* r = q * m */
- if (wc_ecc_mulmod((mp_int*)m->internal, (ecc_point*)q->internal,
- result, &a, &prime, 1) != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_mulmod qm error");
- goto cleanup;
- }
- }
-
- /* copy to destination */
- if (wc_ecc_copy_point(result, (ecc_point*)r->internal)) {
- WOLFSSL_MSG("wc_ecc_copy_point error");
- goto cleanup;
- }
- r->inSet = 1;
- if (SetECPointExternal(r) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECPointExternal error");
- goto cleanup;
- }
-
- ret = WOLFSSL_SUCCESS;
-cleanup:
- mp_clear(&a);
- mp_clear(&prime);
- wc_ecc_del_point(result);
- wc_ecc_del_point(tmp);
- return ret;
-}
-#endif /* !defined(WOLFSSL_ATECC508A) && defined(ECC_SHAMIR) &&
- * !defined(HAVE_SELFTEST) */
-
-void wolfSSL_EC_POINT_clear_free(WOLFSSL_EC_POINT *p)
-{
- WOLFSSL_ENTER("wolfSSL_EC_POINT_clear_free");
-
- wolfSSL_EC_POINT_free(p);
-}
-
-/* return code compliant with OpenSSL :
- * 0 if equal, 1 if not and -1 in case of error
- */
-int wolfSSL_EC_POINT_cmp(const WOLFSSL_EC_GROUP *group,
- const WOLFSSL_EC_POINT *a, const WOLFSSL_EC_POINT *b,
- WOLFSSL_BN_CTX *ctx)
-{
- int ret;
-
- (void)ctx;
-
- WOLFSSL_ENTER("wolfSSL_EC_POINT_cmp");
-
- if (group == NULL || a == NULL || a->internal == NULL || b == NULL ||
- b->internal == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_POINT_cmp Bad arguments");
- return WOLFSSL_FATAL_ERROR;
- }
-
- ret = wc_ecc_cmp_point((ecc_point*)a->internal, (ecc_point*)b->internal);
- if (ret == MP_EQ)
- return 0;
- else if (ret == MP_LT || ret == MP_GT)
- return 1;
-
- return WOLFSSL_FATAL_ERROR;
-}
-
-int wolfSSL_EC_POINT_copy(WOLFSSL_EC_POINT *dest, const WOLFSSL_EC_POINT *src)
-{
- WOLFSSL_ENTER("wolfSSL_EC_POINT_copy");
-
- if (!dest || !src) {
- return WOLFSSL_FAILURE;
- }
-
- if (setupPoint(src) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- if (wc_ecc_copy_point((ecc_point*) dest->internal,
- (ecc_point*) src->internal) != MP_OKAY) {
- return WOLFSSL_FAILURE;
- }
-
- dest->inSet = 1;
-
- if (SetECPointExternal(dest) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* HAVE_ECC */
-#endif /* OPENSSL_EXTRA */
-
-#if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
-void wolfSSL_EC_POINT_free(WOLFSSL_EC_POINT *p)
-{
- WOLFSSL_ENTER("wolfSSL_EC_POINT_free");
-
- if (p != NULL) {
- if (p->internal != NULL) {
- wc_ecc_del_point((ecc_point*)p->internal);
- p->internal = NULL;
- }
-
- wolfSSL_BN_free(p->X);
- wolfSSL_BN_free(p->Y);
- wolfSSL_BN_free(p->Z);
- p->X = NULL;
- p->Y = NULL;
- p->Z = NULL;
- p->inSet = p->exSet = 0;
-
- XFREE(p, NULL, DYNAMIC_TYPE_ECC);
- /* p = NULL, don't try to access or double free it */
- }
-}
-#endif
-
-#ifdef OPENSSL_EXTRA
-#ifdef HAVE_ECC
-/* return code compliant with OpenSSL :
- * 1 if point at infinity, 0 else
- */
-int wolfSSL_EC_POINT_is_at_infinity(const WOLFSSL_EC_GROUP *group,
- const WOLFSSL_EC_POINT *point)
-{
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_EC_POINT_is_at_infinity");
-
- if (group == NULL || point == NULL || point->internal == NULL) {
- WOLFSSL_MSG("wolfSSL_EC_POINT_is_at_infinity NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (setupPoint(point) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- ret = wc_ecc_point_is_at_infinity((ecc_point*)point->internal);
- if (ret < 0) {
- WOLFSSL_MSG("ecc_point_is_at_infinity failure");
- return WOLFSSL_FAILURE;
- }
-
- return ret;
-}
-
-/* End EC_POINT */
-
-size_t wolfSSL_EC_get_builtin_curves(WOLFSSL_EC_BUILTIN_CURVE *r, size_t nitems)
-{
- size_t i, min_nitems;
-#ifdef HAVE_SELFTEST
- size_t ecc_sets_count;
- for (i = 0; ecc_sets[i].size != 0 && ecc_sets[i].name != NULL; i++);
- ecc_sets_count = i;
-#endif
-
- if (r == NULL || nitems == 0)
- return ecc_sets_count;
-
- min_nitems = nitems < ecc_sets_count ? nitems : ecc_sets_count;
-
- for (i = 0; i < min_nitems; i++) {
- r[i].nid = EccEnumToNID(ecc_sets[i].id);
- r[i].comment = wolfSSL_OBJ_nid2sn(r[i].nid);
- }
-
- return ecc_sets_count;
-}
-
-/* Start ECDSA_SIG */
-void wolfSSL_ECDSA_SIG_free(WOLFSSL_ECDSA_SIG *sig)
-{
- WOLFSSL_ENTER("wolfSSL_ECDSA_SIG_free");
-
- if (sig) {
- wolfSSL_BN_free(sig->r);
- wolfSSL_BN_free(sig->s);
-
- XFREE(sig, NULL, DYNAMIC_TYPE_ECC);
- }
-}
-
-WOLFSSL_ECDSA_SIG *wolfSSL_ECDSA_SIG_new(void)
-{
- WOLFSSL_ECDSA_SIG *sig;
-
- WOLFSSL_ENTER("wolfSSL_ECDSA_SIG_new");
-
- sig = (WOLFSSL_ECDSA_SIG*) XMALLOC(sizeof(WOLFSSL_ECDSA_SIG), NULL,
- DYNAMIC_TYPE_ECC);
- if (sig == NULL) {
- WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA signature failure");
- return NULL;
- }
-
- sig->s = NULL;
- sig->r = wolfSSL_BN_new();
- if (sig->r == NULL) {
- WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA r failure");
- wolfSSL_ECDSA_SIG_free(sig);
- return NULL;
- }
-
- sig->s = wolfSSL_BN_new();
- if (sig->s == NULL) {
- WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA s failure");
- wolfSSL_ECDSA_SIG_free(sig);
- return NULL;
- }
-
- return sig;
-}
-
-/* return signature structure on success, NULL otherwise */
-WOLFSSL_ECDSA_SIG *wolfSSL_ECDSA_do_sign(const unsigned char *d, int dlen,
- WOLFSSL_EC_KEY *key)
-{
- WOLFSSL_ECDSA_SIG *sig = NULL;
- int initTmpRng = 0;
- WC_RNG* rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG = NULL;
-#else
- WC_RNG tmpRNG[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_ECDSA_do_sign");
-
- if (d == NULL || key == NULL || key->internal == NULL) {
- WOLFSSL_MSG("wolfSSL_ECDSA_do_sign Bad arguments");
- return NULL;
- }
-
- /* set internal key if not done */
- if (key->inSet == 0)
- {
- WOLFSSL_MSG("wolfSSL_ECDSA_do_sign No EC key internal set, do it");
-
- if (SetECKeyInternal(key) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("wolfSSL_ECDSA_do_sign SetECKeyInternal failed");
- return NULL;
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return NULL;
-#endif
-
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("wolfSSL_ECDSA_do_sign Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("wolfSSL_ECDSA_do_sign Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng) {
- mp_int sig_r, sig_s;
-
- if (mp_init_multi(&sig_r, &sig_s, NULL, NULL, NULL, NULL) == MP_OKAY) {
- if (wc_ecc_sign_hash_ex(d, dlen, rng, (ecc_key*)key->internal,
- &sig_r, &sig_s) != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_sign_hash_ex failed");
- }
- else {
- /* put signature blob in ECDSA structure */
- sig = wolfSSL_ECDSA_SIG_new();
- if (sig == NULL)
- WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new failed");
- else if (SetIndividualExternal(&(sig->r), &sig_r)!=WOLFSSL_SUCCESS){
- WOLFSSL_MSG("ecdsa r key error");
- wolfSSL_ECDSA_SIG_free(sig);
- sig = NULL;
- }
- else if (SetIndividualExternal(&(sig->s), &sig_s)!=WOLFSSL_SUCCESS){
- WOLFSSL_MSG("ecdsa s key error");
- wolfSSL_ECDSA_SIG_free(sig);
- sig = NULL;
- }
-
- }
- mp_free(&sig_r);
- mp_free(&sig_s);
- }
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- return sig;
-}
-
-/* return code compliant with OpenSSL :
- * 1 for a valid signature, 0 for an invalid signature and -1 on error
- */
-int wolfSSL_ECDSA_do_verify(const unsigned char *d, int dlen,
- const WOLFSSL_ECDSA_SIG *sig, WOLFSSL_EC_KEY *key)
-{
- int check_sign = 0;
-
- WOLFSSL_ENTER("wolfSSL_ECDSA_do_verify");
-
- if (d == NULL || sig == NULL || key == NULL || key->internal == NULL) {
- WOLFSSL_MSG("wolfSSL_ECDSA_do_verify Bad arguments");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* set internal key if not done */
- if (key->inSet == 0)
- {
- WOLFSSL_MSG("No EC key internal set, do it");
-
- if (SetECKeyInternal(key) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECKeyInternal failed");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- if (wc_ecc_verify_hash_ex((mp_int*)sig->r->internal,
- (mp_int*)sig->s->internal, d, dlen, &check_sign,
- (ecc_key *)key->internal) != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_verify_hash failed");
- return WOLFSSL_FATAL_ERROR;
- }
- else if (check_sign == 0) {
- WOLFSSL_MSG("wc_ecc_verify_hash incorrect signature detected");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-WOLFSSL_ECDSA_SIG *wolfSSL_d2i_ECDSA_SIG(WOLFSSL_ECDSA_SIG **sig,
- const unsigned char **pp, long len)
-{
- WOLFSSL_ECDSA_SIG *s = NULL;
-
- if (pp == NULL)
- return NULL;
-
- if (sig != NULL)
- s = *sig;
- if (s == NULL) {
- s = wolfSSL_ECDSA_SIG_new();
- if (s == NULL)
- return NULL;
- }
-
- /* DecodeECC_DSA_Sig calls mp_init, so free these */
- mp_free((mp_int*)s->r->internal);
- mp_free((mp_int*)s->s->internal);
-
- if (DecodeECC_DSA_Sig(*pp, (word32)len, (mp_int*)s->r->internal,
- (mp_int*)s->s->internal) != MP_OKAY) {
- if (sig == NULL || *sig == NULL)
- wolfSSL_ECDSA_SIG_free(s);
- return NULL;
- }
-
- *pp += len;
- if (sig != NULL)
- *sig = s;
- return s;
-}
-
-int wolfSSL_i2d_ECDSA_SIG(const WOLFSSL_ECDSA_SIG *sig, unsigned char **pp)
-{
- word32 len;
-
- if (sig == NULL)
- return 0;
-
- /* ASN.1: SEQ + INT + INT
- * ASN.1 Integer must be a positive value - prepend zero if number has
- * top bit set.
- */
- len = 2 + mp_leading_bit((mp_int*)sig->r->internal) +
- mp_unsigned_bin_size((mp_int*)sig->r->internal) +
- 2 + mp_leading_bit((mp_int*)sig->s->internal) +
- mp_unsigned_bin_size((mp_int*)sig->s->internal);
- /* Two bytes required for length if ASN.1 SEQ data greater than 127 bytes
- * and less than 256 bytes.
- */
- len = 1 + ((len > 127) ? 2 : 1) + len;
- if (pp != NULL && *pp != NULL) {
- if (StoreECC_DSA_Sig(*pp, &len, (mp_int*)sig->r->internal,
- (mp_int*)sig->s->internal) != MP_OKAY) {
- len = 0;
- }
- else
- *pp += len;
- }
-
- return (int)len;
-}
-/* End ECDSA_SIG */
-
-/* Start ECDH */
-/* return code compliant with OpenSSL :
- * length of computed key if success, -1 if error
- */
-int wolfSSL_ECDH_compute_key(void *out, size_t outlen,
- const WOLFSSL_EC_POINT *pub_key,
- WOLFSSL_EC_KEY *ecdh,
- void *(*KDF) (const void *in, size_t inlen,
- void *out, size_t *outlen))
-{
- word32 len;
- (void)KDF;
-
- (void)KDF;
-
- WOLFSSL_ENTER("wolfSSL_ECDH_compute_key");
-
- if (out == NULL || pub_key == NULL || pub_key->internal == NULL ||
- ecdh == NULL || ecdh->internal == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* set internal key if not done */
- if (ecdh->inSet == 0)
- {
- WOLFSSL_MSG("No EC key internal set, do it");
-
- if (SetECKeyInternal(ecdh) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECKeyInternal failed");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- len = (word32)outlen;
-
- if (wc_ecc_shared_secret_ssh((ecc_key*)ecdh->internal,
- (ecc_point*)pub_key->internal,
- (byte *)out, &len) != MP_OKAY) {
- WOLFSSL_MSG("wc_ecc_shared_secret failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- return len;
-}
-/* End ECDH */
-
-#if !defined(NO_FILESYSTEM)
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_PEM_write_EC_PUBKEY(XFILE fp, WOLFSSL_EC_KEY *x)
-{
- (void)fp;
- (void)x;
- WOLFSSL_STUB("PEM_write_EC_PUBKEY");
- WOLFSSL_MSG("wolfSSL_PEM_write_EC_PUBKEY not implemented");
-
- return WOLFSSL_FAILURE;
-}
-#endif
-
-/* Uses the same format of input as wolfSSL_PEM_read_bio_PrivateKey but expects
- * the results to be an EC key.
- *
- * bio structure to read EC private key from
- * ec if not null is then set to the result
- * cb password callback for reading PEM
- * pass password string
- *
- * returns a pointer to a new WOLFSSL_EC_KEY struct on success and NULL on fail
- */
-
-WOLFSSL_EC_KEY* wolfSSL_PEM_read_bio_EC_PUBKEY(WOLFSSL_BIO* bio,
- WOLFSSL_EC_KEY** ec,
- pem_password_cb* cb, void *pass)
-{
- WOLFSSL_EVP_PKEY* pkey;
- WOLFSSL_EC_KEY* local;
-
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_EC_PUBKEY");
-
- pkey = wolfSSL_PEM_read_bio_PUBKEY(bio, NULL, cb, pass);
- if (pkey == NULL) {
- return NULL;
- }
-
- /* Since the WOLFSSL_EC_KEY structure is being taken from WOLFSSL_EVP_PKEY the
- * flag indicating that the WOLFSSL_EC_KEY structure is owned should be FALSE
- * to avoid having it free'd */
- pkey->ownEcc = 0;
- local = pkey->ecc;
- if (ec != NULL) {
- *ec = local;
- }
-
- wolfSSL_EVP_PKEY_free(pkey);
- return local;
-}
-
-/* Reads a private EC key from a WOLFSSL_BIO into a WOLFSSL_EC_KEY.
- * Returns WOLFSSL_SUCCESS or WOLFSSL_FAILURE
- */
-WOLFSSL_EC_KEY* wolfSSL_PEM_read_bio_ECPrivateKey(WOLFSSL_BIO* bio,
- WOLFSSL_EC_KEY** ec,
- pem_password_cb* cb,
- void *pass)
-{
- WOLFSSL_EVP_PKEY* pkey;
- WOLFSSL_EC_KEY* local;
-
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_ECPrivateKey");
-
- pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, cb, pass);
- if (pkey == NULL) {
- return NULL;
- }
-
- /* Since the WOLFSSL_EC_KEY structure is being taken from WOLFSSL_EVP_PKEY the
- * flag indicating that the WOLFSSL_EC_KEY structure is owned should be FALSE
- * to avoid having it free'd */
- pkey->ownEcc = 0;
- local = pkey->ecc;
- if (ec != NULL) {
- *ec = local;
- }
-
- wolfSSL_EVP_PKEY_free(pkey);
- return local;
-}
-#endif /* NO_FILESYSTEM */
-
-#if defined(WOLFSSL_KEY_GEN)
-/* Takes a public WOLFSSL_EC_KEY and writes it out to WOLFSSL_BIO
- * Returns WOLFSSL_SUCCESS or WOLFSSL_FAILURE
- */
-int wolfSSL_PEM_write_bio_EC_PUBKEY(WOLFSSL_BIO* bio, WOLFSSL_EC_KEY* ec)
-{
- int ret = 0, der_max_len = 0, derSz = 0;
- byte *derBuf;
- WOLFSSL_EVP_PKEY* pkey;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_EC_PUBKEY");
-
- if (bio == NULL || ec == NULL) {
- WOLFSSL_MSG("Bad Function Arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* Initialize pkey structure */
- pkey = wolfSSL_EVP_PKEY_new_ex(bio->heap);
- if (pkey == NULL) {
- WOLFSSL_MSG("wolfSSL_EVP_PKEY_new_ex failed");
- return WOLFSSL_FAILURE;
- }
-
- /* Set pkey info */
- pkey->ecc = ec;
- pkey->ownEcc = 0; /* pkey does not own ECC */
- pkey->type = EVP_PKEY_EC;
-
- /* 4 > size of pub, priv + ASN.1 additional information */
- der_max_len = 4 * wc_ecc_size((ecc_key*)ec->internal) + AES_BLOCK_SIZE;
-
- derBuf = (byte*)XMALLOC(der_max_len, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (derBuf == NULL) {
- WOLFSSL_MSG("Malloc failed");
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- /* convert key to der format */
- derSz = wc_EccPublicKeyToDer((ecc_key*)ec->internal, derBuf, der_max_len, 1);
- if (derSz < 0) {
- WOLFSSL_MSG("wc_EccPublicKeyToDer failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- pkey->pkey.ptr = (char*)XMALLOC(derSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (pkey->pkey.ptr == NULL) {
- WOLFSSL_MSG("key malloc failed");
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- /* add der info to the evp key */
- pkey->pkey_sz = derSz;
- XMEMCPY(pkey->pkey.ptr, derBuf, derSz);
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
-
- if((ret = wolfSSL_PEM_write_bio_PUBKEY(bio, pkey)) != WOLFSSL_SUCCESS){
- WOLFSSL_MSG("wolfSSL_PEM_write_bio_PUBKEY failed");
- }
- wolfSSL_EVP_PKEY_free(pkey);
-
- return ret;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_PEM_write_bio_ECPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EC_KEY* ec,
- const EVP_CIPHER* cipher,
- unsigned char* passwd, int len,
- pem_password_cb* cb, void* arg)
-{
- int ret = 0, der_max_len = 0, derSz = 0;
- byte *derBuf;
- WOLFSSL_EVP_PKEY* pkey;
- WOLFSSL_ENTER("WOLFSSL_PEM_write_bio_ECPrivateKey");
-
- if (bio == NULL || ec == NULL) {
- WOLFSSL_MSG("Bad Function Arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* Initialize pkey structure */
- pkey = wolfSSL_EVP_PKEY_new_ex(bio->heap);
- if (pkey == NULL) {
- WOLFSSL_MSG("wolfSSL_EVP_PKEY_new_ex failed");
- return WOLFSSL_FAILURE;
- }
-
- /* Set pkey info */
- pkey->ecc = ec;
- pkey->ownEcc = 0; /* pkey does not own ECC */
- pkey->type = EVP_PKEY_EC;
-
- /* 4 > size of pub, priv + ASN.1 additional informations
- */
- der_max_len = 4 * wc_ecc_size((ecc_key*)ec->internal) + AES_BLOCK_SIZE;
-
- derBuf = (byte*)XMALLOC(der_max_len, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (derBuf == NULL) {
- WOLFSSL_MSG("Malloc failed");
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- /* convert key to der format */
- derSz = wc_EccKeyToDer((ecc_key*)ec->internal, derBuf, der_max_len);
- if (derSz < 0) {
- WOLFSSL_MSG("wc_EccKeyToDer failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- pkey->pkey.ptr = (char*)XMALLOC(derSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (pkey->pkey.ptr == NULL) {
- WOLFSSL_MSG("key malloc failed");
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- /* add der info to the evp key */
- pkey->pkey_sz = derSz;
- XMEMCPY(pkey->pkey.ptr, derBuf, derSz);
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
-
- ret = wolfSSL_PEM_write_bio_PrivateKey(bio, pkey, cipher, passwd, len,
- cb, arg);
- wolfSSL_EVP_PKEY_free(pkey);
-
- return ret;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_PEM_write_mem_ECPrivateKey(WOLFSSL_EC_KEY* ecc,
- const EVP_CIPHER* cipher,
- unsigned char* passwd, int passwdSz,
- unsigned char **pem, int *plen)
-{
-#if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
- byte *derBuf, *tmp, *cipherInfo = NULL;
- int der_max_len = 0, derSz = 0;
- const int type = ECC_PRIVATEKEY_TYPE;
- const char* header = NULL;
- const char* footer = NULL;
-
- WOLFSSL_MSG("wolfSSL_PEM_write_mem_ECPrivateKey");
-
- if (pem == NULL || plen == NULL || ecc == NULL || ecc->internal == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FAILURE;
- }
-
- if (wc_PemGetHeaderFooter(type, &header, &footer) != 0)
- return WOLFSSL_FAILURE;
-
- if (ecc->inSet == 0) {
- WOLFSSL_MSG("No ECC internal set, do it");
-
- if (SetECKeyInternal(ecc) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECKeyInternal failed");
- return WOLFSSL_FAILURE;
- }
- }
-
- /* 4 > size of pub, priv + ASN.1 additional information */
- der_max_len = 4 * wc_ecc_size((ecc_key*)ecc->internal) + AES_BLOCK_SIZE;
-
- derBuf = (byte*)XMALLOC(der_max_len, NULL, DYNAMIC_TYPE_DER);
- if (derBuf == NULL) {
- WOLFSSL_MSG("malloc failed");
- return WOLFSSL_FAILURE;
- }
-
- /* Key to DER */
- derSz = wc_EccKeyToDer((ecc_key*)ecc->internal, derBuf, der_max_len);
- if (derSz < 0) {
- WOLFSSL_MSG("wc_EccKeyToDer failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- return WOLFSSL_FAILURE;
- }
-
- /* encrypt DER buffer if required */
- if (passwd != NULL && passwdSz > 0 && cipher != NULL) {
- int ret;
-
- ret = EncryptDerKey(derBuf, &derSz, cipher,
- passwd, passwdSz, &cipherInfo);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("EncryptDerKey failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- return ret;
- }
-
- /* tmp buffer with a max size */
- *plen = (derSz * 2) + (int)XSTRLEN(header) + 1 +
- (int)XSTRLEN(footer) + 1 + HEADER_ENCRYPTED_KEY_SIZE;
- }
- else { /* tmp buffer with a max size */
- *plen = (derSz * 2) + (int)XSTRLEN(header) + 1 +
- (int)XSTRLEN(footer) + 1;
- }
-
- tmp = (byte*)XMALLOC(*plen, NULL, DYNAMIC_TYPE_PEM);
- if (tmp == NULL) {
- WOLFSSL_MSG("malloc failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- if (cipherInfo != NULL)
- XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
- return WOLFSSL_FAILURE;
- }
-
- /* DER to PEM */
- *plen = wc_DerToPemEx(derBuf, derSz, tmp, *plen, cipherInfo, type);
- if (*plen <= 0) {
- WOLFSSL_MSG("wc_DerToPemEx failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
- if (cipherInfo != NULL)
- XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
- return WOLFSSL_FAILURE;
- }
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- if (cipherInfo != NULL)
- XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
-
- *pem = (byte*)XMALLOC((*plen)+1, NULL, DYNAMIC_TYPE_KEY);
- if (*pem == NULL) {
- WOLFSSL_MSG("malloc failed");
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
- return WOLFSSL_FAILURE;
- }
- XMEMSET(*pem, 0, (*plen)+1);
-
- if (XMEMCPY(*pem, tmp, *plen) == NULL) {
- WOLFSSL_MSG("XMEMCPY failed");
- XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
- return WOLFSSL_FAILURE;
- }
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
-
- return WOLFSSL_SUCCESS;
-#else
- (void)ecc;
- (void)cipher;
- (void)passwd;
- (void)passwdSz;
- (void)pem;
- (void)plen;
- return WOLFSSL_FAILURE;
-#endif /* WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM */
-}
-
-#ifndef NO_FILESYSTEM
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_PEM_write_ECPrivateKey(XFILE fp, WOLFSSL_EC_KEY *ecc,
- const EVP_CIPHER *enc,
- unsigned char *kstr, int klen,
- pem_password_cb *cb, void *u)
-{
- byte *pem;
- int plen, ret;
-
- (void)cb;
- (void)u;
-
- WOLFSSL_MSG("wolfSSL_PEM_write_ECPrivateKey");
-
- if (fp == XBADFILE || ecc == NULL || ecc->internal == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FAILURE;
- }
-
- ret = wolfSSL_PEM_write_mem_ECPrivateKey(ecc, enc, kstr, klen, &pem, &plen);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("wolfSSL_PEM_write_mem_ECPrivateKey failed");
- return WOLFSSL_FAILURE;
- }
-
- ret = (int)XFWRITE(pem, plen, 1, fp);
- if (ret != 1) {
- WOLFSSL_MSG("ECC private key file write failed");
- return WOLFSSL_FAILURE;
- }
-
- XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* NO_FILESYSTEM */
-#endif /* defined(WOLFSSL_KEY_GEN) */
-
-#endif /* HAVE_ECC */
-
-
-#ifndef NO_DSA
-
-#if defined(WOLFSSL_KEY_GEN)
-
-/* Takes a DSA Privatekey and writes it out to a WOLFSSL_BIO
- * Returns WOLFSSL_SUCCESS or WOLFSSL_FAILURE
- */
-int wolfSSL_PEM_write_bio_DSAPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_DSA* dsa,
- const EVP_CIPHER* cipher,
- unsigned char* passwd, int len,
- pem_password_cb* cb, void* arg)
-{
- int ret = 0, der_max_len = 0, derSz = 0;
- byte *derBuf;
- WOLFSSL_EVP_PKEY* pkey;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_DSAPrivateKey");
-
- if (bio == NULL || dsa == NULL) {
- WOLFSSL_MSG("Bad Function Arguments");
- return WOLFSSL_FAILURE;
- }
-
- pkey = wolfSSL_EVP_PKEY_new_ex(bio->heap);
- if (pkey == NULL) {
- WOLFSSL_MSG("wolfSSL_EVP_PKEY_new_ex failed");
- return WOLFSSL_FAILURE;
- }
-
- pkey->type = EVP_PKEY_DSA;
- pkey->dsa = dsa;
- pkey->ownDsa = 0;
-
- /* 4 > size of pub, priv, p, q, g + ASN.1 additional information */
- der_max_len = 4 * wolfSSL_BN_num_bytes(dsa->g) + AES_BLOCK_SIZE;
-
- derBuf = (byte*)XMALLOC(der_max_len, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (derBuf == NULL) {
- WOLFSSL_MSG("Malloc failed");
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- /* convert key to der format */
- derSz = wc_DsaKeyToDer((DsaKey*)dsa->internal, derBuf, der_max_len);
- if (derSz < 0) {
- WOLFSSL_MSG("wc_DsaKeyToDer failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- pkey->pkey.ptr = (char*)XMALLOC(derSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (pkey->pkey.ptr == NULL) {
- WOLFSSL_MSG("key malloc failed");
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- /* add der info to the evp key */
- pkey->pkey_sz = derSz;
- XMEMCPY(pkey->pkey.ptr, derBuf, derSz);
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
-
- ret = wolfSSL_PEM_write_bio_PrivateKey(bio, pkey, cipher, passwd, len,
- cb, arg);
- wolfSSL_EVP_PKEY_free(pkey);
-
- return ret;
-}
-
-#ifndef HAVE_SELFTEST
-/* Takes a DSA public key and writes it out to a WOLFSSL_BIO
- * Returns WOLFSSL_SUCCESS or WOLFSSL_FAILURE
- */
-int wolfSSL_PEM_write_bio_DSA_PUBKEY(WOLFSSL_BIO* bio, WOLFSSL_DSA* dsa)
-{
- int ret = 0, derMax = 0, derSz = 0;
- byte *derBuf;
- WOLFSSL_EVP_PKEY* pkey;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_DSA_PUBKEY");
-
- if (bio == NULL || dsa == NULL) {
- WOLFSSL_MSG("Bad function arguements");
- return WOLFSSL_FAILURE;
- }
-
- pkey = wolfSSL_EVP_PKEY_new_ex(bio->heap);
- if (pkey == NULL) {
- WOLFSSL_MSG("wolfSSL_EVP_PKEY_new_ex failed");
- return WOLFSSL_FAILURE;
- }
-
- pkey->type = EVP_PKEY_DSA;
- pkey->dsa = dsa;
- pkey->ownDsa = 0;
-
- /* 4 > size of pub, priv, p, q, g + ASN.1 additional information */
- derMax = 4 * wolfSSL_BN_num_bytes(dsa->g) + AES_BLOCK_SIZE;
-
- derBuf = (byte*)XMALLOC(derMax, bio->heap, DYNAMIC_TYPE_DER);
- if (derBuf == NULL) {
- WOLFSSL_MSG("malloc failed");
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- /* Key to DER */
- derSz = wc_DsaKeyToPublicDer((DsaKey*)dsa->internal, derBuf, derMax);
- if (derSz < 0) {
- WOLFSSL_MSG("wc_DsaKeyToDer failed");
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_DER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
-
- pkey->pkey.ptr = (char*)XMALLOC(derSz, bio->heap, DYNAMIC_TYPE_DER);
-
- if (pkey->pkey.ptr == NULL) {
- WOLFSSL_MSG("key malloc failed");
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_DER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
- pkey->pkey_sz = derSz;
- XMEMSET(pkey->pkey.ptr, 0, derSz);
-
- if (XMEMCPY(pkey->pkey.ptr, derBuf, derSz) == NULL) {
- WOLFSSL_MSG("XMEMCPY failed");
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_DER);
- XFREE(pkey->pkey.ptr, bio->heap, DYNAMIC_TYPE_DER);
- wolfSSL_EVP_PKEY_free(pkey);
- return WOLFSSL_FAILURE;
- }
- XFREE(derBuf, bio->heap, DYNAMIC_TYPE_DER);
- ret = wolfSSL_PEM_write_bio_PUBKEY(bio, pkey);
- wolfSSL_EVP_PKEY_free(pkey);
- return ret;
-}
-#endif /* HAVE_SELFTEST */
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
- const EVP_CIPHER* cipher,
- unsigned char* passwd, int passwdSz,
- unsigned char **pem, int *plen)
-{
-#if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
- byte *derBuf, *tmp, *cipherInfo = NULL;
- int der_max_len = 0, derSz = 0;
- const int type = DSA_PRIVATEKEY_TYPE;
- const char* header = NULL;
- const char* footer = NULL;
-
- WOLFSSL_MSG("wolfSSL_PEM_write_mem_DSAPrivateKey");
-
- if (pem == NULL || plen == NULL || dsa == NULL || dsa->internal == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FAILURE;
- }
-
- if (wc_PemGetHeaderFooter(type, &header, &footer) != 0)
- return WOLFSSL_FAILURE;
-
- if (dsa->inSet == 0) {
- WOLFSSL_MSG("No DSA internal set, do it");
-
- if (SetDsaInternal(dsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetDsaInternal failed");
- return WOLFSSL_FAILURE;
- }
- }
-
- /* 4 > size of pub, priv, p, q, g + ASN.1 additional information */
- der_max_len = 4 * wolfSSL_BN_num_bytes(dsa->g) + AES_BLOCK_SIZE;
-
- derBuf = (byte*)XMALLOC(der_max_len, NULL, DYNAMIC_TYPE_DER);
- if (derBuf == NULL) {
- WOLFSSL_MSG("malloc failed");
- return WOLFSSL_FAILURE;
- }
-
- /* Key to DER */
- derSz = wc_DsaKeyToDer((DsaKey*)dsa->internal, derBuf, der_max_len);
- if (derSz < 0) {
- WOLFSSL_MSG("wc_DsaKeyToDer failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- return WOLFSSL_FAILURE;
- }
-
- /* encrypt DER buffer if required */
- if (passwd != NULL && passwdSz > 0 && cipher != NULL) {
- int ret;
-
- ret = EncryptDerKey(derBuf, &derSz, cipher,
- passwd, passwdSz, &cipherInfo);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("EncryptDerKey failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- return ret;
- }
-
- /* tmp buffer with a max size */
- *plen = (derSz * 2) + (int)XSTRLEN(header) + 1 +
- (int)XSTRLEN(footer) + 1 + HEADER_ENCRYPTED_KEY_SIZE;
- }
- else { /* tmp buffer with a max size */
- *plen = (derSz * 2) + (int)XSTRLEN(header) + 1 +
- (int)XSTRLEN(footer) + 1;
- }
-
- tmp = (byte*)XMALLOC(*plen, NULL, DYNAMIC_TYPE_PEM);
- if (tmp == NULL) {
- WOLFSSL_MSG("malloc failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- if (cipherInfo != NULL)
- XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
- return WOLFSSL_FAILURE;
- }
-
- /* DER to PEM */
- *plen = wc_DerToPemEx(derBuf, derSz, tmp, *plen, cipherInfo, type);
- if (*plen <= 0) {
- WOLFSSL_MSG("wc_DerToPemEx failed");
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
- if (cipherInfo != NULL)
- XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
- return WOLFSSL_FAILURE;
- }
- XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
- if (cipherInfo != NULL)
- XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
-
- *pem = (byte*)XMALLOC((*plen)+1, NULL, DYNAMIC_TYPE_KEY);
- if (*pem == NULL) {
- WOLFSSL_MSG("malloc failed");
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
- return WOLFSSL_FAILURE;
- }
- XMEMSET(*pem, 0, (*plen)+1);
-
- if (XMEMCPY(*pem, tmp, *plen) == NULL) {
- WOLFSSL_MSG("XMEMCPY failed");
- XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
- return WOLFSSL_FAILURE;
- }
- XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
-
- return WOLFSSL_SUCCESS;
-#else
- (void)dsa;
- (void)cipher;
- (void)passwd;
- (void)passwdSz;
- (void)pem;
- (void)plen;
- return WOLFSSL_FAILURE;
-#endif /* WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM */
-}
-
-#ifndef NO_FILESYSTEM
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_PEM_write_DSAPrivateKey(XFILE fp, WOLFSSL_DSA *dsa,
- const EVP_CIPHER *enc,
- unsigned char *kstr, int klen,
- pem_password_cb *cb, void *u)
-{
- byte *pem;
- int plen, ret;
-
- (void)cb;
- (void)u;
-
- WOLFSSL_MSG("wolfSSL_PEM_write_DSAPrivateKey");
-
- if (fp == XBADFILE || dsa == NULL || dsa->internal == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FAILURE;
- }
-
- ret = wolfSSL_PEM_write_mem_DSAPrivateKey(dsa, enc, kstr, klen, &pem, &plen);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("wolfSSL_PEM_write_mem_DSAPrivateKey failed");
- return WOLFSSL_FAILURE;
- }
-
- ret = (int)XFWRITE(pem, plen, 1, fp);
- if (ret != 1) {
- WOLFSSL_MSG("DSA private key file write failed");
- return WOLFSSL_FAILURE;
- }
-
- XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* NO_FILESYSTEM */
-#endif /* defined(WOLFSSL_KEY_GEN) */
-
-#ifndef NO_FILESYSTEM
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_PEM_write_DSA_PUBKEY(XFILE fp, WOLFSSL_DSA *x)
-{
- (void)fp;
- (void)x;
- WOLFSSL_STUB("PEM_write_DSA_PUBKEY");
- WOLFSSL_MSG("wolfSSL_PEM_write_DSA_PUBKEY not implemented");
-
- return WOLFSSL_FAILURE;
-}
-#endif
-#endif /* NO_FILESYSTEM */
-
-#endif /* #ifndef NO_DSA */
-
-static int pem_read_bio_key(WOLFSSL_BIO* bio, pem_password_cb* cb, void* pass,
- int keyType, int* eccFlag, DerBuffer** der)
-{
-#ifdef WOLFSSL_SMALL_STACK
- EncryptedInfo* info = NULL;
-#else
- EncryptedInfo info[1];
-#endif /* WOLFSSL_SMALL_STACK */
- pem_password_cb* localCb = NULL;
- char* mem = NULL;
- int memSz = 0;
- int ret;
-
- if(cb) {
- localCb = cb;
- } else {
- if(pass) {
- localCb = wolfSSL_PEM_def_callback;
- }
- }
-
- if ((ret = wolfSSL_BIO_pending(bio)) > 0) {
- memSz = ret;
- mem = (char*)XMALLOC(memSz, bio->heap, DYNAMIC_TYPE_OPENSSL);
- if (mem == NULL) {
- WOLFSSL_MSG("Memory error");
- ret = MEMORY_E;
- }
- if (ret >= 0) {
- if ((ret = wolfSSL_BIO_read(bio, mem, memSz)) <= 0) {
- XFREE(mem, bio->heap, DYNAMIC_TYPE_OPENSSL);
- mem = NULL;
- ret = MEMORY_E;
- }
- }
- }
- else if (bio->type == WOLFSSL_BIO_FILE) {
- int sz = 100; /* read from file by 100 byte chunks */
- int idx = 0;
- char* tmp = (char*)XMALLOC(sz, bio->heap, DYNAMIC_TYPE_OPENSSL);
- memSz = 0;
- if (tmp == NULL) {
- WOLFSSL_MSG("Memory error");
- ret = MEMORY_E;
- }
-
- while (ret >= 0 && (sz = wolfSSL_BIO_read(bio, tmp, sz)) > 0) {
- char* newMem;
- if (memSz + sz < 0) {
- /* sanity check */
- break;
- }
- newMem = (char*)XREALLOC(mem, memSz + sz, bio->heap,
- DYNAMIC_TYPE_OPENSSL);
- if (newMem == NULL) {
- WOLFSSL_MSG("Memory error");
- XFREE(mem, bio->heap, DYNAMIC_TYPE_OPENSSL);
- mem = NULL;
- XFREE(tmp, bio->heap, DYNAMIC_TYPE_OPENSSL);
- tmp = NULL;
- ret = MEMORY_E;
- break;
- }
- mem = newMem;
- XMEMCPY(mem + idx, tmp, sz);
- memSz += sz;
- idx += sz;
- sz = 100; /* read another 100 byte chunk from file */
- }
- XFREE(tmp, bio->heap, DYNAMIC_TYPE_OPENSSL);
- tmp = NULL;
- if (memSz <= 0) {
- WOLFSSL_MSG("No data to read from bio");
- if (mem != NULL) {
- XFREE(mem, bio->heap, DYNAMIC_TYPE_OPENSSL);
- mem = NULL;
- }
- ret = BUFFER_E;
- }
- }
- else {
- WOLFSSL_MSG("No data to read from bio");
- ret = NOT_COMPILED_IN;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- if (ret >= 0) {
- info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (info == NULL) {
- WOLFSSL_MSG("Error getting memory for EncryptedInfo structure");
- XFREE(mem, bio->heap, DYNAMIC_TYPE_OPENSSL);
- mem = NULL;
- ret = MEMORY_E;
- }
- }
-#endif
-
- if (ret >= 0) {
- XMEMSET(info, 0, sizeof(EncryptedInfo));
- info->passwd_cb = localCb;
- info->passwd_userdata = pass;
- ret = PemToDer((const unsigned char*)mem, memSz, keyType, der,
- NULL, info, eccFlag);
-
- if (ret < 0) {
- WOLFSSL_MSG("Bad Pem To Der");
- }
- else {
- /* write left over data back to bio */
- if ((memSz - (int)info->consumed) > 0 &&
- bio->type != WOLFSSL_BIO_FILE) {
- if (wolfSSL_BIO_write(bio, mem + (int)info->consumed,
- memSz - (int)info->consumed) <= 0) {
- WOLFSSL_MSG("Unable to advance bio read pointer");
- }
- }
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- XFREE(mem, bio->heap, DYNAMIC_TYPE_OPENSSL);
-
- return ret;
-}
-
-WOLFSSL_EVP_PKEY* wolfSSL_PEM_read_bio_PrivateKey(WOLFSSL_BIO* bio,
- WOLFSSL_EVP_PKEY** key,
- pem_password_cb* cb,
- void* pass)
-{
- WOLFSSL_EVP_PKEY* pkey = NULL;
- DerBuffer* der = NULL;
- int keyFormat = 0;
- int type = -1;
-
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_PrivateKey");
-
- if (bio == NULL)
- return pkey;
-
- if (pem_read_bio_key(bio, cb, pass, PRIVATEKEY_TYPE, &keyFormat,
- &der) >= 0) {
- const unsigned char* ptr = der->buffer;
-
- if (keyFormat) {
- /* keyFormat is Key_Sum enum */
- if (keyFormat == RSAk)
- type = EVP_PKEY_RSA;
- else if (keyFormat == ECDSAk)
- type = EVP_PKEY_EC;
- else if (keyFormat == DSAk)
- type = EVP_PKEY_DSA;
- else if (keyFormat == DHk)
- type = EVP_PKEY_DH;
- }
- else {
- /* Default to RSA if format is not set */
- type = EVP_PKEY_RSA;
- }
-
- /* handle case where reuse is attempted */
- if (key != NULL && *key != NULL)
- pkey = *key;
-
- wolfSSL_d2i_PrivateKey(type, &pkey, &ptr, der->length);
- if (pkey == NULL) {
- WOLFSSL_MSG("Error loading DER buffer into WOLFSSL_EVP_PKEY");
- }
- }
-
- FreeDer(&der);
-
- if (key != NULL && pkey != NULL)
- *key = pkey;
-
- WOLFSSL_LEAVE("wolfSSL_PEM_read_bio_PrivateKey", 0);
-
- return pkey;
-}
-
-WOLFSSL_EVP_PKEY *wolfSSL_PEM_read_bio_PUBKEY(WOLFSSL_BIO* bio,
- WOLFSSL_EVP_PKEY **key,
- pem_password_cb *cb, void *pass)
-{
- WOLFSSL_EVP_PKEY* pkey = NULL;
- DerBuffer* der = NULL;
- int keyFormat = 0;
-
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_PUBKEY");
-
- if (bio == NULL)
- return pkey;
-
- if (pem_read_bio_key(bio, cb, pass, PUBLICKEY_TYPE, &keyFormat, &der) >= 0) {
- const unsigned char* ptr = der->buffer;
-
- /* handle case where reuse is attempted */
- if (key != NULL && *key != NULL)
- pkey = *key;
-
- wolfSSL_d2i_PUBKEY(&pkey, &ptr, der->length);
- if (pkey == NULL) {
- WOLFSSL_MSG("Error loading DER buffer into WOLFSSL_EVP_PKEY");
- }
- }
-
- FreeDer(&der);
-
- if (key != NULL && pkey != NULL)
- *key = pkey;
-
- WOLFSSL_LEAVE("wolfSSL_PEM_read_bio_PUBKEY", 0);
-
- return pkey;
-}
-
-
-#if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && !defined(NO_RSA)
-/* Uses the same format of input as wolfSSL_PEM_read_bio_PrivateKey but expects
- * the results to be an RSA key.
- *
- * bio structure to read RSA private key from
- * rsa if not null is then set to the result
- * cb password callback for reading PEM
- * pass password string
- *
- * returns a pointer to a new WOLFSSL_RSA structure on success and NULL on fail
- */
-WOLFSSL_RSA* wolfSSL_PEM_read_bio_RSAPrivateKey(WOLFSSL_BIO* bio,
- WOLFSSL_RSA** rsa, pem_password_cb* cb, void* pass)
-{
- WOLFSSL_EVP_PKEY* pkey;
- WOLFSSL_RSA* local;
-
- WOLFSSL_ENTER("PEM_read_bio_RSAPrivateKey");
-
- pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, cb, pass);
- if (pkey == NULL) {
- return NULL;
- }
-
- /* Since the WOLFSSL_RSA structure is being taken from WOLFSSL_EVP_PEKY the
- * flag indicating that the WOLFSSL_RSA structure is owned should be FALSE
- * to avoid having it free'd */
- pkey->ownRsa = 0;
- local = pkey->rsa;
- if (rsa != NULL) {
- *rsa = local;
- }
-
- wolfSSL_EVP_PKEY_free(pkey);
- return local;
-}
-#endif /* OPENSSL_EXTRA || OPENSSL_ALL || !NO_RSA */
-
-#if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && (!defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN))
-/* Uses the same format of input as wolfSSL_PEM_read_bio_PrivateKey but expects
- * the results to be an DSA key.
- *
- * bio structure to read DSA private key from
- * dsa if not null is then set to the result
- * cb password callback for reading PEM
- * pass password string
- *
- * returns a pointer to a new WOLFSSL_DSA structure on success and NULL on fail
- */
-WOLFSSL_DSA* wolfSSL_PEM_read_bio_DSAPrivateKey(WOLFSSL_BIO* bio,
- WOLFSSL_DSA** dsa,
- pem_password_cb* cb,void *pass)
-{
- WOLFSSL_EVP_PKEY* pkey = NULL;
- WOLFSSL_DSA* local;
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_DSAPrivateKey");
-
-
- pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, cb, pass);
- if (pkey == NULL) {
- WOLFSSL_MSG("Error in PEM_read_bio_PrivateKey");
- return NULL;
- }
- /* Since the WOLFSSL_DSA structure is being taken from WOLFSSL_EVP_PKEY the
- * flag indicating that the WOLFSSL_DSA structure is owned should be FALSE
- * to avoid having it free'd */
- pkey->ownDsa = 0;
- local = pkey->dsa;
- if (dsa != NULL) {
- *dsa = local;
- }
- wolfSSL_EVP_PKEY_free(pkey);
- return local;
-}
-
-/* Reads an DSA public key from a WOLFSSL_BIO into a WOLFSSL_DSA.
- * Returns WOLFSSL_SUCCESS or WOLFSSL_FAILURE
- */
-WOLFSSL_DSA *wolfSSL_PEM_read_bio_DSA_PUBKEY(WOLFSSL_BIO* bio,WOLFSSL_DSA** dsa,
- pem_password_cb* cb, void *pass)
-{
- WOLFSSL_EVP_PKEY* pkey;
- WOLFSSL_DSA* local;
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_DSA_PUBKEY");
-
- pkey = wolfSSL_PEM_read_bio_PUBKEY(bio, NULL, cb, pass);
- if (pkey == NULL) {
- WOLFSSL_MSG("wolfSSL_PEM_read_bio_PUBKEY failed");
- return NULL;
- }
-
- /* Since the WOLFSSL_DSA structure is being taken from WOLFSSL_EVP_PKEY the
- * flag indicating that the WOLFSSL_DSA structure is owned should be FALSE
- * to avoid having it free'd */
- pkey->ownDsa = 0;
- local = pkey->dsa;
- if (dsa != NULL) {
- *dsa = local;
- }
-
- wolfSSL_EVP_PKEY_free(pkey);
- return local;
-}
-#endif
-
-#ifdef HAVE_ECC
-/* returns a new WOLFSSL_EC_GROUP structure on success and NULL on fail */
-WOLFSSL_EC_GROUP* wolfSSL_PEM_read_bio_ECPKParameters(WOLFSSL_BIO* bio,
- WOLFSSL_EC_GROUP** group, pem_password_cb* cb, void* pass)
-{
- WOLFSSL_EVP_PKEY* pkey;
- WOLFSSL_EC_GROUP* ret = NULL;
-
- /* check on if bio is null is done in wolfSSL_PEM_read_bio_PrivateKey */
- pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, cb, pass);
- if (pkey != NULL) {
- if (pkey->type != EVP_PKEY_EC) {
- WOLFSSL_MSG("Unexpected key type");
- }
- else {
- ret = (WOLFSSL_EC_GROUP*)wolfSSL_EC_KEY_get0_group(pkey->ecc);
-
- /* set ecc group to null so it is not free'd when pkey is free'd */
- pkey->ecc->group = NULL;
- }
- }
-
- (void)group;
- wolfSSL_EVP_PKEY_free(pkey);
- return ret;
-}
-#endif /* HAVE_ECC */
-
-#if !defined(NO_FILESYSTEM)
-WOLFSSL_EVP_PKEY *wolfSSL_PEM_read_PUBKEY(XFILE fp, EVP_PKEY **x,
- pem_password_cb *cb, void *u)
-{
- (void)fp;
- (void)x;
- (void)cb;
- (void)u;
-
- WOLFSSL_MSG("wolfSSL_PEM_read_PUBKEY not implemented");
-
- return NULL;
-}
-#endif /* NO_FILESYSTEM */
-
-#ifndef NO_RSA
-
-#if defined(XSNPRINTF) && !defined(HAVE_FAST_RSA)
-/* snprintf() must be available */
-
-/******************************************************************************
-* wolfSSL_RSA_print - writes the human readable form of RSA to bio
-*
-* RETURNS:
-* returns WOLFSSL_SUCCESS on success, otherwise returns WOLFSSL_FAILURE
-*/
-int wolfSSL_RSA_print(WOLFSSL_BIO* bio, WOLFSSL_RSA* rsa, int offset)
-{
- char tmp[100] = {0};
- word32 idx = 0;
- int sz = 0;
- byte lbit = 0;
- int rawLen = 0;
- byte* rawKey = NULL;
- RsaKey* iRsa = NULL;
- int i = 0;
- mp_int *rsaElem = NULL;
- char rsaStr[][20] = { "Modulus:",
- "PublicExponent:",
- "PrivateExponent:",
- "Prime1:",
- "Prime2:",
- "Exponent1:",
- "Exponent2:",
- "Coefficient:"
- };
-
- WOLFSSL_ENTER("wolfSSL_RSA_print");
- (void)offset;
-
- if (bio == NULL || rsa == NULL) {
- return WOLFSSL_FATAL_ERROR;
- }
-
- if ((sz = wolfSSL_RSA_size(rsa)) < 0) {
- WOLFSSL_MSG("Error getting RSA key size");
- return WOLFSSL_FAILURE;
- }
- iRsa = (RsaKey*)rsa->internal;
-
- XSNPRINTF(tmp, sizeof(tmp) - 1, "\n%s: (%d bit)",
- "RSA Private-Key", 8 * sz);
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- for (i=0; in;
- break;
- case 1:
- rsaElem = &iRsa->e;
- break;
- case 2:
- rsaElem = &iRsa->d;
- break;
- case 3:
- rsaElem = &iRsa->p;
- break;
- case 4:
- rsaElem = &iRsa->q;
- break;
- case 5:
- rsaElem = &iRsa->dP;
- break;
- case 6:
- rsaElem = &iRsa->dQ;
- break;
- case 7:
- rsaElem = &iRsa->u;
- break;
- default:
- WOLFSSL_MSG("Bad index value");
- }
-
- if (i == 1) {
- /* Print out exponent values */
- rawLen = mp_unsigned_bin_size(rsaElem);
- if (rawLen < 0) {
- WOLFSSL_MSG("Error getting exponent size");
- return WOLFSSL_FAILURE;
- }
-
- if ((word32)rawLen < sizeof(word32)) {
- rawLen = sizeof(word32);
- }
- rawKey = (byte*)XMALLOC(rawLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (rawKey == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
- XMEMSET(rawKey, 0, rawLen);
- mp_to_unsigned_bin(rsaElem, rawKey);
- if ((word32)rawLen <= sizeof(word32)) {
- idx = *(word32*)rawKey;
- #ifdef BIG_ENDIAN_ORDER
- idx = ByteReverseWord32(idx);
- #endif
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1, "\nExponent: %d (0x%x)", idx, idx);
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- XFREE(rawKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- XFREE(rawKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- else {
- XSNPRINTF(tmp, sizeof(tmp) - 1, "\n%s\n ", rsaStr[i]);
- tmp[sizeof(tmp) - 1] = '\0';
- if (mp_leading_bit(rsaElem)) {
- lbit = 1;
- XSTRNCAT(tmp, "00", 3);
- }
-
- rawLen = mp_unsigned_bin_size(rsaElem);
- rawKey = (byte*)XMALLOC(rawLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (rawKey == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
- mp_to_unsigned_bin(rsaElem, rawKey);
- for (idx = 0; idx < (word32)rawLen; idx++) {
- char val[5];
- int valSz = 5;
-
- if ((idx == 0) && !lbit) {
- XSNPRINTF(val, valSz - 1, "%02x", rawKey[idx]);
- }
- else if ((idx != 0) && (((idx + lbit) % 15) == 0)) {
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- XFREE(rawKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- XSNPRINTF(tmp, sizeof(tmp) - 1,
- ":\n ");
- XSNPRINTF(val, valSz - 1, "%02x", rawKey[idx]);
- }
- else {
- XSNPRINTF(val, valSz - 1, ":%02x", rawKey[idx]);
- }
- XSTRNCAT(tmp, val, valSz);
- }
- XFREE(rawKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- /* print out remaining values */
- if ((idx > 0) && (((idx - 1 + lbit) % 15) != 0)) {
- tmp[sizeof(tmp) - 1] = '\0';
- if (wolfSSL_BIO_write(bio, tmp, (int)XSTRLEN(tmp)) <= 0) {
- return WOLFSSL_FAILURE;
- }
- }
- lbit = 0;
- }
-
- }
- /* done with print out */
- if (wolfSSL_BIO_write(bio, "\n\0", (int)XSTRLEN("\n\0")) <= 0) {
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* XSNPRINTF */
-
-#if !defined(NO_FILESYSTEM)
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_RSA *wolfSSL_PEM_read_RSAPublicKey(XFILE fp, WOLFSSL_RSA **x,
- pem_password_cb *cb, void *u)
-{
- (void)fp;
- (void)x;
- (void)cb;
- (void)u;
- WOLFSSL_STUB("PEM_read_RSAPublicKey");
- WOLFSSL_MSG("wolfSSL_PEM_read_RSAPublicKey not implemented");
-
- return NULL;
-}
-#endif
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_PEM_write_RSAPublicKey(XFILE fp, WOLFSSL_RSA *x)
-{
- (void)fp;
- (void)x;
- WOLFSSL_STUB("PEM_write_RSAPublicKey");
- WOLFSSL_MSG("wolfSSL_PEM_write_RSAPublicKey not implemented");
-
- return WOLFSSL_FAILURE;
-}
-#endif
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_PEM_write_RSA_PUBKEY(XFILE fp, WOLFSSL_RSA *x)
-{
- (void)fp;
- (void)x;
- WOLFSSL_STUB("PEM_write_RSA_PUBKEY");
- WOLFSSL_MSG("wolfSSL_PEM_write_RSA_PUBKEY not implemented");
-
- return WOLFSSL_FAILURE;
-}
-#endif
-
-#endif /* NO_FILESYSTEM */
-
-WOLFSSL_RSA *wolfSSL_d2i_RSAPublicKey(WOLFSSL_RSA **r, const unsigned char **pp,
- long len)
-{
- WOLFSSL_RSA *rsa = NULL;
-
- WOLFSSL_ENTER("d2i_RSAPublicKey");
-
- if (pp == NULL) {
- WOLFSSL_MSG("Bad argument");
- return NULL;
- }
- if ((rsa = wolfSSL_RSA_new()) == NULL) {
- WOLFSSL_MSG("RSA_new failed");
- return NULL;
- }
-
- if (wolfSSL_RSA_LoadDer_ex(rsa, *pp, (int)len, WOLFSSL_RSA_LOAD_PUBLIC)
- != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("RSA_LoadDer failed");
- wolfSSL_RSA_free(rsa);
- rsa = NULL;
- }
- if (r != NULL)
- *r = rsa;
-
- return rsa;
-}
-
-/* Converts an RSA private key from DER format to an RSA structure.
-Returns pointer to the RSA structure on success and NULL if error. */
-WOLFSSL_RSA *wolfSSL_d2i_RSAPrivateKey(WOLFSSL_RSA **r,
- const unsigned char **derBuf, long derSz)
-{
- WOLFSSL_RSA *rsa = NULL;
-
- WOLFSSL_ENTER("wolfSSL_d2i_RSAPrivateKey");
-
- /* check for bad functions arguments */
- if (derBuf == NULL) {
- WOLFSSL_MSG("Bad argument");
- return NULL;
- }
- if ((rsa = wolfSSL_RSA_new()) == NULL) {
- WOLFSSL_MSG("RSA_new failed");
- return NULL;
- }
-
- if (wolfSSL_RSA_LoadDer_ex(rsa, *derBuf, (int)derSz,
- WOLFSSL_RSA_LOAD_PRIVATE) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("RSA_LoadDer failed");
- wolfSSL_RSA_free(rsa);
- rsa = NULL;
- }
- if (r != NULL)
- *r = rsa;
-
- return rsa;
-}
-
-#if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(NO_RSA) && !defined(HAVE_USER_RSA)
-/* Converts an internal RSA structure to DER format.
- * If "pp" is null then buffer size only is returned.
- * If "*pp" is null then a created buffer is set in *pp and the caller is
- * responsible for free'ing it.
- * Returns size of DER on success and WOLFSSL_FAILURE if error
- */
-int wolfSSL_i2d_RSAPrivateKey(WOLFSSL_RSA *rsa, unsigned char **pp)
-{
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_i2d_RSAPrivateKey");
-
- /* check for bad functions arguments */
- if (rsa == NULL) {
- WOLFSSL_MSG("Bad Function Arguments");
- return BAD_FUNC_ARG;
- }
-
- if ((ret = wolfSSL_RSA_To_Der(rsa, pp, 0)) < 0) {
- WOLFSSL_MSG("wolfSSL_RSA_To_Der failed");
- return WOLFSSL_FAILURE;
- }
-
- return ret; /* returns size of DER if successful */
-}
-
-
-int wolfSSL_i2d_RSAPublicKey(WOLFSSL_RSA *rsa, const unsigned char **pp)
-{
- int ret;
-
- /* check for bad functions arguments */
- if (rsa == NULL) {
- WOLFSSL_MSG("Bad Function Arguments");
- return BAD_FUNC_ARG;
- }
-
- if ((ret = wolfSSL_RSA_To_Der(rsa, (byte**)pp, 1)) < 0) {
- WOLFSSL_MSG("wolfSSL_RSA_To_Der failed");
- return WOLFSSL_FAILURE;
- }
-
- return ret;
-}
-#endif /* !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
- * !defined(NO_RSA) && !defined(HAVE_USER_RSA) */
-
-#endif /* !NO_RSA */
-#endif /* OPENSSL_EXTRA */
-
-#if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
-/* return WOLFSSL_SUCCESS if success, WOLFSSL_FATAL_ERROR if error */
-int wolfSSL_RSA_LoadDer(WOLFSSL_RSA* rsa, const unsigned char* derBuf, int derSz)
-{
- return wolfSSL_RSA_LoadDer_ex(rsa, derBuf, derSz, WOLFSSL_RSA_LOAD_PRIVATE);
-}
-
-
-int wolfSSL_RSA_LoadDer_ex(WOLFSSL_RSA* rsa, const unsigned char* derBuf,
- int derSz, int opt)
-{
-
- word32 idx = 0;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_RSA_LoadDer");
-
- if (rsa == NULL || rsa->internal == NULL || derBuf == NULL || derSz <= 0) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (opt == WOLFSSL_RSA_LOAD_PRIVATE) {
- ret = wc_RsaPrivateKeyDecode(derBuf, &idx, (RsaKey*)rsa->internal, derSz);
- }
- else {
- ret = wc_RsaPublicKeyDecode(derBuf, &idx, (RsaKey*)rsa->internal, derSz);
- }
-
- if (ret < 0) {
- if (opt == WOLFSSL_RSA_LOAD_PRIVATE) {
- WOLFSSL_MSG("RsaPrivateKeyDecode failed");
- }
- else {
- WOLFSSL_MSG("RsaPublicKeyDecode failed");
- }
- return SSL_FATAL_ERROR;
- }
-
- if (SetRsaExternal(rsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetRsaExternal failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- rsa->inSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-#if defined(WC_RSA_PSS) && (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || \
- defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX))
-static int hash2mgf(enum wc_HashType hType)
-{
- switch (hType) {
-#ifndef NO_SHA
- case WC_HASH_TYPE_SHA:
- return WC_MGF1SHA1;
-#endif
-#ifndef NO_SHA256
-#ifdef WOLFSSL_SHA224
- case WC_HASH_TYPE_SHA224:
- return WC_MGF1SHA224;
-#endif
- case WC_HASH_TYPE_SHA256:
- return WC_MGF1SHA256;
-#endif
-#ifdef WOLFSSL_SHA384
- case WC_HASH_TYPE_SHA384:
- return WC_MGF1SHA384;
-#endif
-#ifdef WOLFSSL_SHA512
- case WC_HASH_TYPE_SHA512:
- return WC_MGF1SHA512;
-#endif
- case WC_HASH_TYPE_NONE:
- case WC_HASH_TYPE_MD2:
- case WC_HASH_TYPE_MD4:
- case WC_HASH_TYPE_MD5:
- case WC_HASH_TYPE_MD5_SHA:
- case WC_HASH_TYPE_SHA3_224:
- case WC_HASH_TYPE_SHA3_256:
- case WC_HASH_TYPE_SHA3_384:
- case WC_HASH_TYPE_SHA3_512:
- case WC_HASH_TYPE_BLAKE2B:
- case WC_HASH_TYPE_BLAKE2S:
- default:
- WOLFSSL_MSG("Unrecognized or unsupported hash function");
- return WC_MGF1NONE;
- }
-}
-
-/*
- * +-----------+
- * | M |
- * +-----------+
- * |
- * V
- * Hash
- * |
- * V
- * +--------+----------+----------+
- * M' = |Padding1| mHash | salt |
- * +--------+----------+----------+
- * |
- * +--------+----------+ V
- * DB = |Padding2|maskedseed| Hash
- * +--------+----------+ |
- * | |
- * V | +--+
- * xor <--- MGF <---| |bc|
- * | | +--+
- * | | |
- * V V V
- * +-------------------+----------+--+
- * EM = | maskedDB |maskedseed|bc|
- * +-------------------+----------+--+
- * Diagram taken from https://tools.ietf.org/html/rfc3447#section-9.1
- */
-int wolfSSL_RSA_padding_add_PKCS1_PSS(WOLFSSL_RSA *rsa, unsigned char *EM,
- const unsigned char *mHash,
- const WOLFSSL_EVP_MD *hashAlg, int saltLen)
-{
- int hashLen, emLen, mgf;
- int ret = WOLFSSL_FAILURE;
- int initTmpRng = 0;
- WC_RNG *rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG = NULL;
-#else
- WC_RNG _tmpRNG[1];
- WC_RNG* tmpRNG = _tmpRNG;
-#endif
- enum wc_HashType hashType;
-
- WOLFSSL_ENTER("wolfSSL_RSA_padding_add_PKCS1_PSS");
-
- if (!rsa || !EM || !mHash || !hashAlg) {
- return WOLFSSL_FAILURE;
- }
-
- if (!(rng = WOLFSSL_RSA_GetRNG(rsa, (WC_RNG**)&tmpRNG, &initTmpRng))) {
- WOLFSSL_MSG("WOLFSSL_RSA_GetRNG error");
- goto cleanup;
- }
-
- if (!rsa->exSet && SetRsaExternal(rsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetRsaExternal error");
- goto cleanup;
- }
-
- hashType = wolfSSL_EVP_md2macType(hashAlg);
- if (hashType < WC_HASH_TYPE_NONE || hashType > WC_HASH_TYPE_MAX) {
- WOLFSSL_MSG("wolfSSL_EVP_md2macType error");
- goto cleanup;
- }
-
- if ((mgf = hash2mgf(hashType)) == WC_MGF1NONE) {
- WOLFSSL_MSG("hash2mgf error");
- goto cleanup;
- }
-
- if ((hashLen = wolfSSL_EVP_MD_size(hashAlg)) < 0) {
- WOLFSSL_MSG("wolfSSL_EVP_MD_size error");
- goto cleanup;
- }
-
- if ((emLen = wolfSSL_RSA_size(rsa)) <= 0) {
- WOLFSSL_MSG("wolfSSL_RSA_size error");
- goto cleanup;
- }
-
- switch (saltLen) {
- /* Negative saltLen values are treated differently */
- case RSA_PSS_SALTLEN_DIGEST:
- saltLen = hashLen;
- break;
- case RSA_PSS_SALTLEN_MAX_SIGN:
- case RSA_PSS_SALTLEN_MAX:
- saltLen = emLen - hashLen - 2;
- break;
- default:
- if (saltLen < 0) {
- /* Not any currently implemented negative value */
- WOLFSSL_MSG("invalid saltLen");
- goto cleanup;
- }
- }
-
- if (wc_RsaPad_ex(mHash, wolfSSL_EVP_MD_size(hashAlg), EM, emLen,
- RSA_BLOCK_TYPE_1, rng, WC_RSA_PSS_PAD,
- wolfSSL_EVP_md2macType(hashAlg), mgf, NULL, 0, saltLen,
- wolfSSL_BN_num_bits(rsa->n), NULL) != MP_OKAY) {
- WOLFSSL_MSG("wc_RsaPad_ex error");
- goto cleanup;
- }
-
- ret = WOLFSSL_SUCCESS;
-cleanup:
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-#ifdef WOLFSSL_SMALL_STACK
- if (tmpRNG)
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return ret;
-}
-
-/*
- * Refer to wolfSSL_RSA_padding_add_PKCS1_PSS
- * for an explanation of the parameters.
- */
-int wolfSSL_RSA_verify_PKCS1_PSS(WOLFSSL_RSA *rsa, const unsigned char *mHash,
- const WOLFSSL_EVP_MD *hashAlg,
- const unsigned char *EM, int saltLen)
-{
- int hashLen, mgf, emLen, mPrimeLen;
- enum wc_HashType hashType;
- byte *mPrime = NULL;
- byte *buf = NULL;
-
- WOLFSSL_ENTER("wolfSSL_RSA_verify_PKCS1_PSS");
-
- if (!rsa || !mHash || !hashAlg || !EM) {
- return WOLFSSL_FAILURE;
- }
-
- if ((hashLen = wolfSSL_EVP_MD_size(hashAlg)) < 0) {
- return WOLFSSL_FAILURE;
- }
-
- if ((emLen = wolfSSL_RSA_size(rsa)) <= 0) {
- WOLFSSL_MSG("wolfSSL_RSA_size error");
- return WOLFSSL_FAILURE;
- }
-
- switch (saltLen) {
- /* Negative saltLen values are treated differently */
- case RSA_PSS_SALTLEN_DIGEST:
- saltLen = hashLen;
- break;
- case RSA_PSS_SALTLEN_MAX_SIGN:
- case RSA_PSS_SALTLEN_MAX:
- saltLen = emLen - hashLen - 2;
- break;
- default:
- if (saltLen < 0) {
- /* Not any currently implemented negative value */
- WOLFSSL_MSG("invalid saltLen");
- return WOLFSSL_FAILURE;
- }
- }
-
- if (!rsa->exSet && SetRsaExternal(rsa) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- hashType = wolfSSL_EVP_md2macType(hashAlg);
- if (hashType < WC_HASH_TYPE_NONE || hashType > WC_HASH_TYPE_MAX) {
- WOLFSSL_MSG("wolfSSL_EVP_md2macType error");
- return WOLFSSL_FAILURE;
- }
-
- if ((mgf = hash2mgf(hashType)) == WC_MGF1NONE) {
- WOLFSSL_MSG("hash2mgf error");
- return WOLFSSL_FAILURE;
- }
-
- if ((hashLen = wolfSSL_EVP_MD_size(hashAlg)) < 0) {
- WOLFSSL_MSG("wolfSSL_EVP_MD_size error");
- return WOLFSSL_FAILURE;
- }
-
- if (!(buf = (byte*)XMALLOC(emLen, NULL, DYNAMIC_TYPE_TMP_BUFFER))) {
- WOLFSSL_MSG("malloc error");
- return WOLFSSL_FAILURE;
- }
- XMEMCPY(buf, EM, emLen);
-
- /* Remove and verify the PSS padding */
- if ((mPrimeLen = wc_RsaUnPad_ex(buf, emLen, &mPrime,
- RSA_BLOCK_TYPE_1, WC_RSA_PSS_PAD, hashType,
- mgf, NULL, 0, saltLen,
- wolfSSL_BN_num_bits(rsa->n), NULL)) < 0) {
- WOLFSSL_MSG("wc_RsaPad_ex error");
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
-
- /* Verify the hash is correct */
- if (wc_RsaPSS_CheckPadding_ex(mHash, hashLen, mPrime, mPrimeLen, hashType,
- saltLen, wolfSSL_BN_num_bits(rsa->n))
- != MP_OKAY) {
- WOLFSSL_MSG("wc_RsaPSS_CheckPadding_ex error");
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-#if defined(OPENSSL_EXTRA)
-WOLFSSL_RSA_METHOD *wolfSSL_RSA_meth_new(const char *name, int flags)
-{
- int name_len;
- WOLFSSL_RSA_METHOD* meth;
-
- if (name == NULL) {
- return NULL;
- }
-
- meth = (WOLFSSL_RSA_METHOD*)XMALLOC(sizeof(WOLFSSL_RSA_METHOD), NULL,
- DYNAMIC_TYPE_OPENSSL);
- name_len = (int)XSTRLEN(name);
- if (!meth) {
- return NULL;
- }
- meth->flags = flags;
- meth->name = (char*)XMALLOC(name_len+1, NULL, DYNAMIC_TYPE_OPENSSL);
- if (!meth->name) {
- XFREE(meth, NULL, DYNAMIC_TYPE_OPENSSL);
- return NULL;
- }
- XMEMCPY(meth->name, name, name_len+1);
-
- return meth;
-}
-
-void wolfSSL_RSA_meth_free(WOLFSSL_RSA_METHOD *meth)
-{
- if (meth) {
- XFREE(meth->name, NULL, DYNAMIC_TYPE_OPENSSL);
- XFREE(meth, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_RSA_meth_set(WOLFSSL_RSA_METHOD *rsa, void* p)
-{
- (void)rsa;
- (void)p;
- WOLFSSL_STUB("RSA_METHOD is not implemented.");
- return 1;
-}
-#endif
-
-int wolfSSL_RSA_set_method(WOLFSSL_RSA *rsa, WOLFSSL_RSA_METHOD *meth)
-{
- if (rsa)
- rsa->meth = meth;
- return 1;
-}
-
-const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_method(const WOLFSSL_RSA *rsa)
-{
- if (!rsa) {
- return NULL;
- }
- return rsa->meth;
-}
-
-const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_default_method(void)
-{
- return wolfSSL_RSA_meth_new("wolfSSL RSA", 0);
-}
-
-int wolfSSL_RSA_flags(const WOLFSSL_RSA *r)
-{
- if (r && r->meth) {
- return r->meth->flags;
- } else {
- return 0;
- }
-}
-
-void wolfSSL_RSA_set_flags(WOLFSSL_RSA *r, int flags)
-{
- if (r && r->meth) {
- r->meth->flags = flags;
- }
-}
-
-#if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
-WOLFSSL_RSA* wolfSSL_RSAPublicKey_dup(WOLFSSL_RSA *rsa)
-{
- int derSz = 0;
- byte *derBuf = NULL;
- WOLFSSL_RSA* local;
-
- WOLFSSL_ENTER("wolfSSL_RSAPublicKey_dup");
-
- if (!rsa) {
- return NULL;
- }
-
- local = wolfSSL_RSA_new();
- if (local == NULL) {
- WOLFSSL_MSG("Error creating a new WOLFSSL_RSA structure");
- return NULL;
- }
-
- if ((derSz = wolfSSL_RSA_To_Der(rsa, &derBuf, 1)) < 0) {
- WOLFSSL_MSG("wolfSSL_RSA_To_Der failed");
- return NULL;
- }
-
- if (wolfSSL_RSA_LoadDer_ex(local,
- derBuf, derSz,
- WOLFSSL_RSA_LOAD_PUBLIC) != SSL_SUCCESS) {
- wolfSSL_RSA_free(local);
- local = NULL;
- }
- XFREE(derBuf, NULL, DYNAMIC_TYPE_ASN1);
- return local;
-}
-#endif
-
-void* wolfSSL_RSA_get_ex_data(const WOLFSSL_RSA *rsa, int idx)
-{
- WOLFSSL_ENTER("wolfSSL_RSA_get_ex_data");
-#ifdef HAVE_EX_DATA
- if (rsa) {
- return wolfSSL_CRYPTO_get_ex_data(&rsa->ex_data, idx);
- }
-#else
- (void)rsa;
- (void)idx;
-#endif
- return NULL;
-}
-
-int wolfSSL_RSA_set_ex_data(WOLFSSL_RSA *rsa, int idx, void *data)
-{
- WOLFSSL_ENTER("wolfSSL_RSA_set_ex_data");
- #ifdef HAVE_EX_DATA
- if (rsa) {
- return wolfSSL_CRYPTO_set_ex_data(&rsa->ex_data, idx, data);
- }
- #else
- (void)rsa;
- (void)idx;
- (void)data;
- #endif
- return WOLFSSL_FAILURE;
-}
-
-int wolfSSL_RSA_set0_key(WOLFSSL_RSA *r, WOLFSSL_BIGNUM *n, WOLFSSL_BIGNUM *e,
- WOLFSSL_BIGNUM *d)
-{
- /* If the fields n and e in r are NULL, the corresponding input
- * parameters MUST be non-NULL for n and e. d may be
- * left NULL (in case only the public key is used).
- */
- if ((!r->n && !n) || (!r->e && !e))
- return 0;
-
- if (n) {
- wolfSSL_BN_free(r->n);
- r->n = n;
- }
- if (e) {
- wolfSSL_BN_free(r->e);
- r->e = e;
- }
- if (d) {
- wolfSSL_BN_clear_free(r->d);
- r->d = d;
- }
-
- return 1;
-}
-#endif /* OPENSSL_EXTRA */
-#endif /* NO_RSA */
-
-#ifdef OPENSSL_EXTRA
-#ifndef NO_DSA
-/* return WOLFSSL_SUCCESS if success, WOLFSSL_FATAL_ERROR if error */
-int wolfSSL_DSA_LoadDer(WOLFSSL_DSA* dsa, const unsigned char* derBuf, int derSz)
-{
- word32 idx = 0;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_DSA_LoadDer");
-
- if (dsa == NULL || dsa->internal == NULL || derBuf == NULL || derSz <= 0) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FATAL_ERROR;
- }
-
- ret = DsaPrivateKeyDecode(derBuf, &idx, (DsaKey*)dsa->internal, derSz);
- if (ret < 0) {
- WOLFSSL_MSG("DsaPrivateKeyDecode failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetDsaExternal(dsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetDsaExternal failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- dsa->inSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Loads DSA key from DER buffer. opt = DSA_LOAD_PRIVATE or DSA_LOAD_PUBLIC.
- returns 1 on success, or 0 on failure. */
-int wolfSSL_DSA_LoadDer_ex(WOLFSSL_DSA* dsa, const unsigned char* derBuf,
- int derSz, int opt)
-{
- word32 idx = 0;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_DSA_LoadDer");
-
- if (dsa == NULL || dsa->internal == NULL || derBuf == NULL || derSz <= 0) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (opt == WOLFSSL_DSA_LOAD_PRIVATE) {
- ret = DsaPrivateKeyDecode(derBuf, &idx, (DsaKey*)dsa->internal, derSz);
- }
- else {
- ret = DsaPublicKeyDecode(derBuf, &idx, (DsaKey*)dsa->internal, derSz);
- }
-
- if (ret < 0 && opt == WOLFSSL_DSA_LOAD_PRIVATE) {
- WOLFSSL_MSG("DsaPrivateKeyDecode failed");
- return WOLFSSL_FATAL_ERROR;
- }
- else if (ret < 0 && opt == WOLFSSL_DSA_LOAD_PUBLIC) {
- WOLFSSL_MSG("DsaPublicKeyDecode failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetDsaExternal(dsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetDsaExternal failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- dsa->inSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* !NO_DSA */
-
-#ifdef HAVE_ECC
-/* return WOLFSSL_SUCCESS if success, WOLFSSL_FATAL_ERROR if error */
-int wolfSSL_EC_KEY_LoadDer(WOLFSSL_EC_KEY* key, const unsigned char* derBuf,
- int derSz)
-{
- return wolfSSL_EC_KEY_LoadDer_ex(key, derBuf, derSz,
- WOLFSSL_EC_KEY_LOAD_PRIVATE);
-}
-
-int wolfSSL_EC_KEY_LoadDer_ex(WOLFSSL_EC_KEY* key, const unsigned char* derBuf,
- int derSz, int opt)
-{
- word32 idx = 0;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_EC_KEY_LoadDer");
-
- if (key == NULL || key->internal == NULL || derBuf == NULL || derSz <= 0) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (opt == WOLFSSL_EC_KEY_LOAD_PRIVATE) {
- ret = wc_EccPrivateKeyDecode(derBuf, &idx, (ecc_key*)key->internal,
- derSz);
- }
- else {
- ret = wc_EccPublicKeyDecode(derBuf, &idx, (ecc_key*)key->internal,
- derSz);
- }
- if (ret < 0) {
- if (opt == WOLFSSL_EC_KEY_LOAD_PRIVATE) {
- WOLFSSL_MSG("wc_EccPrivateKeyDecode failed");
- }
- else {
- WOLFSSL_MSG("wc_EccPublicKeyDecode failed");
- }
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetECKeyExternal(key) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetECKeyExternal failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- key->inSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* HAVE_ECC */
-
-#if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
-/* return WOLFSSL_SUCCESS if success, WOLFSSL_FATAL_ERROR if error */
-int wolfSSL_DH_LoadDer(WOLFSSL_DH* dh, const unsigned char* derBuf, int derSz)
-{
- word32 idx = 0;
- int ret;
-
- if (dh == NULL || dh->internal == NULL || derBuf == NULL || derSz <= 0) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FATAL_ERROR;
- }
-
- ret = wc_DhKeyDecode(derBuf, &idx, (DhKey*)dh->internal, (word32)derSz);
- if (ret < 0) {
- WOLFSSL_MSG("wc_DhKeyDecode failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetDhExternal(dh) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetDhExternal failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* ! NO_DH && WOLFSSL_QT || OPENSSL_ALL */
-
-#endif /* OPENSSL_EXTRA */
-
-
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
-
-/* increments ref count of WOLFSSL_RSA. Return 1 on success, 0 on error */
-int wolfSSL_RSA_up_ref(WOLFSSL_RSA* rsa)
-{
- if (rsa) {
- if (wc_LockMutex(&rsa->refMutex) != 0) {
- WOLFSSL_MSG("Failed to lock x509 mutex");
- }
- rsa->refCount++;
- wc_UnLockMutex(&rsa->refMutex);
-
- return 1;
- }
-
- return 0;
-}
-
-/* increments ref count of WOLFSSL_X509. Return 1 on success, 0 on error */
-int wolfSSL_X509_up_ref(WOLFSSL_X509* x509)
-{
- if (x509) {
- if (wc_LockMutex(&x509->refMutex) != 0) {
- WOLFSSL_MSG("Failed to lock x509 mutex");
- }
- x509->refCount++;
- wc_UnLockMutex(&x509->refMutex);
-
- return 1;
- }
-
- return 0;
-}
-
-#endif /* OPENSSL_EXTRA || OPENSSL_ALL */
-
-
-#ifdef WOLFSSL_ALT_CERT_CHAINS
-int wolfSSL_is_peer_alt_cert_chain(const WOLFSSL* ssl)
-{
- int isUsing = 0;
- if (ssl)
- isUsing = ssl->options.usingAltCertChain;
- return isUsing;
-}
-#endif /* WOLFSSL_ALT_CERT_CHAINS */
-
-
-#ifdef SESSION_CERTS
-
-#ifdef WOLFSSL_ALT_CERT_CHAINS
-/* Get peer's alternate certificate chain */
-WOLFSSL_X509_CHAIN* wolfSSL_get_peer_alt_chain(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_get_peer_alt_chain");
- if (ssl)
- return &ssl->session.altChain;
-
- return 0;
-}
-#endif /* WOLFSSL_ALT_CERT_CHAINS */
-
-
-/* Get peer's certificate chain */
-WOLFSSL_X509_CHAIN* wolfSSL_get_peer_chain(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_get_peer_chain");
- if (ssl)
- return &ssl->session.chain;
-
- return 0;
-}
-
-
-/* Get peer's certificate chain total count */
-int wolfSSL_get_chain_count(WOLFSSL_X509_CHAIN* chain)
-{
- WOLFSSL_ENTER("wolfSSL_get_chain_count");
- if (chain)
- return chain->count;
-
- return 0;
-}
-
-
-/* Get peer's ASN.1 DER certificate at index (idx) length in bytes */
-int wolfSSL_get_chain_length(WOLFSSL_X509_CHAIN* chain, int idx)
-{
- WOLFSSL_ENTER("wolfSSL_get_chain_length");
- if (chain)
- return chain->certs[idx].length;
-
- return 0;
-}
-
-
-/* Get peer's ASN.1 DER certificate at index (idx) */
-byte* wolfSSL_get_chain_cert(WOLFSSL_X509_CHAIN* chain, int idx)
-{
- WOLFSSL_ENTER("wolfSSL_get_chain_cert");
- if (chain)
- return chain->certs[idx].buffer;
-
- return 0;
-}
-
-
-/* Get peer's wolfSSL X509 certificate at index (idx) */
-WOLFSSL_X509* wolfSSL_get_chain_X509(WOLFSSL_X509_CHAIN* chain, int idx)
-{
- int ret;
- WOLFSSL_X509* x509 = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert = NULL;
-#else
- DecodedCert cert[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_get_chain_X509");
- if (chain != NULL) {
- #ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
- DYNAMIC_TYPE_DCERT);
- if (cert != NULL)
- #endif
- {
- InitDecodedCert(cert, chain->certs[idx].buffer,
- chain->certs[idx].length, NULL);
-
- if ((ret = ParseCertRelative(cert, CERT_TYPE, 0, NULL)) != 0) {
- WOLFSSL_MSG("Failed to parse cert");
- }
- else {
- x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
- DYNAMIC_TYPE_X509);
- if (x509 == NULL) {
- WOLFSSL_MSG("Failed alloc X509");
- }
- else {
- InitX509(x509, 1, NULL);
-
- if ((ret = CopyDecodedToX509(x509, cert)) != 0) {
- WOLFSSL_MSG("Failed to copy decoded");
- wolfSSL_X509_free(x509);
- x509 = NULL;
- }
- }
- }
-
- FreeDecodedCert(cert);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
- #endif
- }
- }
- (void)ret;
-
- return x509;
-}
-
-
-/* Get peer's PEM certificate at index (idx), output to buffer if inLen big
- enough else return error (-1). If buffer is NULL only calculate
- outLen. Output length is in *outLen WOLFSSL_SUCCESS on ok */
-int wolfSSL_get_chain_cert_pem(WOLFSSL_X509_CHAIN* chain, int idx,
- unsigned char* buf, int inLen, int* outLen)
-{
-#if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
- const char* header = NULL;
- const char* footer = NULL;
- int headerLen;
- int footerLen;
- int i;
- int err;
- word32 szNeeded = 0;
-
- WOLFSSL_ENTER("wolfSSL_get_chain_cert_pem");
- if (!chain || !outLen || idx < 0 || idx >= wolfSSL_get_chain_count(chain))
- return BAD_FUNC_ARG;
-
- err = wc_PemGetHeaderFooter(CERT_TYPE, &header, &footer);
- if (err != 0)
- return err;
-
- headerLen = (int)XSTRLEN(header);
- footerLen = (int)XSTRLEN(footer);
-
- /* Null output buffer return size needed in outLen */
- if(!buf) {
- if(Base64_Encode(chain->certs[idx].buffer, chain->certs[idx].length,
- NULL, &szNeeded) != LENGTH_ONLY_E)
- return WOLFSSL_FAILURE;
- *outLen = szNeeded + headerLen + footerLen;
- return LENGTH_ONLY_E;
- }
-
- /* don't even try if inLen too short */
- if (inLen < headerLen + footerLen + chain->certs[idx].length)
- return BAD_FUNC_ARG;
-
- /* header */
- if (XMEMCPY(buf, header, headerLen) == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- i = headerLen;
-
- /* body */
- *outLen = inLen; /* input to Base64_Encode */
- if ( (err = Base64_Encode(chain->certs[idx].buffer,
- chain->certs[idx].length, buf + i, (word32*)outLen)) < 0)
- return err;
- i += *outLen;
-
- /* footer */
- if ( (i + footerLen) > inLen)
- return BAD_FUNC_ARG;
- if (XMEMCPY(buf + i, footer, footerLen) == NULL)
- return WOLFSSL_FATAL_ERROR;
- *outLen += headerLen + footerLen;
-
- return WOLFSSL_SUCCESS;
-#else
- (void)chain;
- (void)idx;
- (void)buf;
- (void)inLen;
- (void)outLen;
- return WOLFSSL_FAILURE;
-#endif /* WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM */
-}
-
-
-/* get session ID */
-WOLFSSL_ABI
-const byte* wolfSSL_get_sessionID(const WOLFSSL_SESSION* session)
-{
- WOLFSSL_ENTER("wolfSSL_get_sessionID");
- if (session)
- return session->sessionID;
-
- return NULL;
-}
-
-
-#endif /* SESSION_CERTS */
-
-#ifdef HAVE_FUZZER
-void wolfSSL_SetFuzzerCb(WOLFSSL* ssl, CallbackFuzzer cbf, void* fCtx)
-{
- if (ssl) {
- ssl->fuzzerCb = cbf;
- ssl->fuzzerCtx = fCtx;
- }
-}
-#endif
-
-#ifndef NO_CERTS
-#ifdef HAVE_PK_CALLBACKS
-
-#ifdef HAVE_ECC
-void wolfSSL_CTX_SetEccKeyGenCb(WOLFSSL_CTX* ctx, CallbackEccKeyGen cb)
-{
- if (ctx)
- ctx->EccKeyGenCb = cb;
-}
-void wolfSSL_SetEccKeyGenCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->EccKeyGenCtx = ctx;
-}
-void* wolfSSL_GetEccKeyGenCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->EccKeyGenCtx;
-
- return NULL;
-}
-
-WOLFSSL_ABI
-void wolfSSL_CTX_SetEccSignCb(WOLFSSL_CTX* ctx, CallbackEccSign cb)
-{
- if (ctx)
- ctx->EccSignCb = cb;
-}
-void wolfSSL_SetEccSignCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->EccSignCtx = ctx;
-}
-void* wolfSSL_GetEccSignCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->EccSignCtx;
-
- return NULL;
-}
-
-void wolfSSL_CTX_SetEccVerifyCb(WOLFSSL_CTX* ctx, CallbackEccVerify cb)
-{
- if (ctx)
- ctx->EccVerifyCb = cb;
-}
-void wolfSSL_SetEccVerifyCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->EccVerifyCtx = ctx;
-}
-void* wolfSSL_GetEccVerifyCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->EccVerifyCtx;
-
- return NULL;
-}
-
-void wolfSSL_CTX_SetEccSharedSecretCb(WOLFSSL_CTX* ctx, CallbackEccSharedSecret cb)
-{
- if (ctx)
- ctx->EccSharedSecretCb = cb;
-}
-void wolfSSL_SetEccSharedSecretCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->EccSharedSecretCtx = ctx;
-}
-void* wolfSSL_GetEccSharedSecretCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->EccSharedSecretCtx;
-
- return NULL;
-}
-#endif /* HAVE_ECC */
-
-#ifdef HAVE_ED25519
-void wolfSSL_CTX_SetEd25519SignCb(WOLFSSL_CTX* ctx, CallbackEd25519Sign cb)
-{
- if (ctx)
- ctx->Ed25519SignCb = cb;
-}
-void wolfSSL_SetEd25519SignCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->Ed25519SignCtx = ctx;
-}
-void* wolfSSL_GetEd25519SignCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->Ed25519SignCtx;
-
- return NULL;
-}
-
-void wolfSSL_CTX_SetEd25519VerifyCb(WOLFSSL_CTX* ctx, CallbackEd25519Verify cb)
-{
- if (ctx)
- ctx->Ed25519VerifyCb = cb;
-}
-void wolfSSL_SetEd25519VerifyCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->Ed25519VerifyCtx = ctx;
-}
-void* wolfSSL_GetEd25519VerifyCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->Ed25519VerifyCtx;
-
- return NULL;
-}
-#endif /* HAVE_ED25519 */
-
-#ifdef HAVE_CURVE25519
-void wolfSSL_CTX_SetX25519KeyGenCb(WOLFSSL_CTX* ctx,
- CallbackX25519KeyGen cb)
-{
- if (ctx)
- ctx->X25519KeyGenCb = cb;
-}
-void wolfSSL_SetX25519KeyGenCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->X25519KeyGenCtx = ctx;
-}
-void* wolfSSL_GetX25519KeyGenCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->X25519KeyGenCtx;
-
- return NULL;
-}
-
-void wolfSSL_CTX_SetX25519SharedSecretCb(WOLFSSL_CTX* ctx,
- CallbackX25519SharedSecret cb)
-{
- if (ctx)
- ctx->X25519SharedSecretCb = cb;
-}
-void wolfSSL_SetX25519SharedSecretCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->X25519SharedSecretCtx = ctx;
-}
-void* wolfSSL_GetX25519SharedSecretCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->X25519SharedSecretCtx;
-
- return NULL;
-}
-#endif /* HAVE_CURVE25519 */
-
-#ifdef HAVE_ED448
-void wolfSSL_CTX_SetEd448SignCb(WOLFSSL_CTX* ctx, CallbackEd448Sign cb)
-{
- if (ctx)
- ctx->Ed448SignCb = cb;
-}
-void wolfSSL_SetEd448SignCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->Ed448SignCtx = ctx;
-}
-void* wolfSSL_GetEd448SignCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->Ed448SignCtx;
-
- return NULL;
-}
-
-void wolfSSL_CTX_SetEd448VerifyCb(WOLFSSL_CTX* ctx, CallbackEd448Verify cb)
-{
- if (ctx)
- ctx->Ed448VerifyCb = cb;
-}
-void wolfSSL_SetEd448VerifyCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->Ed448VerifyCtx = ctx;
-}
-void* wolfSSL_GetEd448VerifyCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->Ed448VerifyCtx;
-
- return NULL;
-}
-#endif /* HAVE_ED448 */
-
-#ifdef HAVE_CURVE448
-void wolfSSL_CTX_SetX448KeyGenCb(WOLFSSL_CTX* ctx,
- CallbackX448KeyGen cb)
-{
- if (ctx)
- ctx->X448KeyGenCb = cb;
-}
-void wolfSSL_SetX448KeyGenCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->X448KeyGenCtx = ctx;
-}
-void* wolfSSL_GetX448KeyGenCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->X448KeyGenCtx;
-
- return NULL;
-}
-
-void wolfSSL_CTX_SetX448SharedSecretCb(WOLFSSL_CTX* ctx,
- CallbackX448SharedSecret cb)
-{
- if (ctx)
- ctx->X448SharedSecretCb = cb;
-}
-void wolfSSL_SetX448SharedSecretCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->X448SharedSecretCtx = ctx;
-}
-void* wolfSSL_GetX448SharedSecretCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->X448SharedSecretCtx;
-
- return NULL;
-}
-#endif /* HAVE_CURVE448 */
-
-#ifndef NO_RSA
-void wolfSSL_CTX_SetRsaSignCb(WOLFSSL_CTX* ctx, CallbackRsaSign cb)
-{
- if (ctx)
- ctx->RsaSignCb = cb;
-}
-void wolfSSL_CTX_SetRsaSignCheckCb(WOLFSSL_CTX* ctx, CallbackRsaVerify cb)
-{
- if (ctx)
- ctx->RsaSignCheckCb = cb;
-}
-void wolfSSL_SetRsaSignCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->RsaSignCtx = ctx;
-}
-void* wolfSSL_GetRsaSignCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->RsaSignCtx;
-
- return NULL;
-}
-
-
-void wolfSSL_CTX_SetRsaVerifyCb(WOLFSSL_CTX* ctx, CallbackRsaVerify cb)
-{
- if (ctx)
- ctx->RsaVerifyCb = cb;
-}
-void wolfSSL_SetRsaVerifyCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->RsaVerifyCtx = ctx;
-}
-void* wolfSSL_GetRsaVerifyCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->RsaVerifyCtx;
-
- return NULL;
-}
-
-#ifdef WC_RSA_PSS
-void wolfSSL_CTX_SetRsaPssSignCb(WOLFSSL_CTX* ctx, CallbackRsaPssSign cb)
-{
- if (ctx)
- ctx->RsaPssSignCb = cb;
-}
-void wolfSSL_CTX_SetRsaPssSignCheckCb(WOLFSSL_CTX* ctx, CallbackRsaPssVerify cb)
-{
- if (ctx)
- ctx->RsaPssSignCheckCb = cb;
-}
-void wolfSSL_SetRsaPssSignCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->RsaPssSignCtx = ctx;
-}
-void* wolfSSL_GetRsaPssSignCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->RsaPssSignCtx;
-
- return NULL;
-}
-
-void wolfSSL_CTX_SetRsaPssVerifyCb(WOLFSSL_CTX* ctx, CallbackRsaPssVerify cb)
-{
- if (ctx)
- ctx->RsaPssVerifyCb = cb;
-}
-void wolfSSL_SetRsaPssVerifyCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->RsaPssVerifyCtx = ctx;
-}
-void* wolfSSL_GetRsaPssVerifyCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->RsaPssVerifyCtx;
-
- return NULL;
-}
-#endif /* WC_RSA_PSS */
-
-void wolfSSL_CTX_SetRsaEncCb(WOLFSSL_CTX* ctx, CallbackRsaEnc cb)
-{
- if (ctx)
- ctx->RsaEncCb = cb;
-}
-void wolfSSL_SetRsaEncCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->RsaEncCtx = ctx;
-}
-void* wolfSSL_GetRsaEncCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->RsaEncCtx;
-
- return NULL;
-}
-
-void wolfSSL_CTX_SetRsaDecCb(WOLFSSL_CTX* ctx, CallbackRsaDec cb)
-{
- if (ctx)
- ctx->RsaDecCb = cb;
-}
-void wolfSSL_SetRsaDecCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->RsaDecCtx = ctx;
-}
-void* wolfSSL_GetRsaDecCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->RsaDecCtx;
-
- return NULL;
-}
-#endif /* NO_RSA */
-
-#endif /* HAVE_PK_CALLBACKS */
-#endif /* NO_CERTS */
-
-#if defined(HAVE_PK_CALLBACKS) && !defined(NO_DH)
-void wolfSSL_CTX_SetDhAgreeCb(WOLFSSL_CTX* ctx, CallbackDhAgree cb)
-{
- if (ctx)
- ctx->DhAgreeCb = cb;
-}
-void wolfSSL_SetDhAgreeCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->DhAgreeCtx = ctx;
-}
-void* wolfSSL_GetDhAgreeCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->DhAgreeCtx;
-
- return NULL;
-}
-#endif /* HAVE_PK_CALLBACKS && !NO_DH */
-
-
-#ifdef WOLFSSL_HAVE_WOLFSCEP
- /* Used by autoconf to see if wolfSCEP is available */
- void wolfSSL_wolfSCEP(void) {}
-#endif
-
-
-#ifdef WOLFSSL_HAVE_CERT_SERVICE
- /* Used by autoconf to see if cert service is available */
- void wolfSSL_cert_service(void) {}
-#endif
-
-#ifdef OPENSSL_EXTRA
- #ifndef NO_CERTS
- void wolfSSL_X509_NAME_free(WOLFSSL_X509_NAME *name)
- {
- WOLFSSL_ENTER("wolfSSL_X509_NAME_free");
- FreeX509Name(name, NULL);
- XFREE(name, NULL, DYNAMIC_TYPE_X509);
- }
-
-
- /* Malloc's a new WOLFSSL_X509_NAME structure
- *
- * returns NULL on failure, otherwise returns a new structure.
- */
- WOLFSSL_X509_NAME* wolfSSL_X509_NAME_new(void)
- {
- WOLFSSL_X509_NAME* name;
-
- WOLFSSL_ENTER("wolfSSL_X509_NAME_new");
-
- name = (WOLFSSL_X509_NAME*)XMALLOC(sizeof(WOLFSSL_X509_NAME), NULL,
- DYNAMIC_TYPE_X509);
- if (name != NULL) {
- InitX509Name(name, 1);
- }
- return name;
- }
-
- /* Creates a duplicate of a WOLFSSL_X509_NAME structure.
- Returns a new WOLFSSL_X509_NAME structure or NULL on failure */
- WOLFSSL_X509_NAME* wolfSSL_X509_NAME_dup(WOLFSSL_X509_NAME *name)
- {
- WOLFSSL_X509_NAME* dup = NULL;
-
- WOLFSSL_ENTER("wolfSSL_X509_NAME_dup");
-
- if (name == NULL) {
- WOLFSSL_MSG("NULL parameter");
- return NULL;
- }
-
- if (!(dup = wolfSSL_X509_NAME_new())) {
- return NULL;
- }
-
- /* copy contents */
- XMEMCPY(dup, name, sizeof(WOLFSSL_X509_NAME));
- InitX509Name(dup, 1);
- dup->sz = name->sz;
-
- /* handle dynamic portions */
- if (name->dynamicName) {
- if (!(dup->name = (char*)XMALLOC(name->sz, 0,
- DYNAMIC_TYPE_OPENSSL))) {
- goto err;
- }
- }
- XMEMCPY(dup->name, name->name, name->sz);
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
- !defined(NO_ASN)
- if (!(dup->fullName.fullName = (char*)XMALLOC(name->fullName.fullNameLen,
- 0, DYNAMIC_TYPE_OPENSSL))) {
- goto err;
- }
- XMEMCPY(dup->fullName.fullName, name->fullName.fullName,
- name->fullName.fullNameLen);
- #endif
-
- return dup;
-
- err:
- if (dup) {
- if (dup->dynamicName && dup->name) {
- XFREE(dup->name, 0, DYNAMIC_TYPE_OPENSSL);
- dup->name = NULL;
- }
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
- !defined(NO_ASN)
- if (dup->fullName.fullName &&
- dup->fullName.fullName != name->fullName.fullName) {
- XFREE(dup->fullName.fullName, 0, DYNAMIC_TYPE_OPENSSL);
- dup->fullName.fullName = NULL;
- }
- #endif
- wolfSSL_X509_NAME_free(dup);
- }
- return NULL;
- }
-
-#if defined(WOLFSSL_CERT_GEN)
- /* helper function for CopyX509NameToCertName()
- *
- * returns WOLFSSL_SUCCESS on success
- */
- static int CopyX509NameEntry(char* out, int mx, char* in, int inLen)
- {
- if (inLen > mx) {
- WOLFSSL_MSG("Name too long");
- XMEMCPY(out, in, mx);
- }
- else {
- XMEMCPY(out, in, inLen);
- out[inLen] = '\0';
- }
-
- /* make sure is null terminated */
- out[mx-1] = '\0';
-
- return WOLFSSL_SUCCESS;
- }
-
-
- /* Helper function to copy cert name from a WOLFSSL_X509_NAME structure to
- * a CertName structure.
- *
- * returns WOLFSSL_SUCCESS on success and a negative error value on failure
- */
- static int CopyX509NameToCertName(WOLFSSL_X509_NAME* n, CertName* cName)
- {
- DecodedName* dn = NULL;
-
- if (n == NULL || cName == NULL) {
- return BAD_FUNC_ARG;
- }
-
- dn = &(n->fullName);
-
- /* initialize cert name */
- cName->country[0] = '\0';
- cName->countryEnc = CTC_PRINTABLE;
- cName->state[0] = '\0';
- cName->stateEnc = CTC_UTF8;
- cName->locality[0] = '\0';
- cName->localityEnc = CTC_UTF8;
- cName->sur[0] = '\0';
- cName->surEnc = CTC_UTF8;
- cName->org[0] = '\0';
- cName->orgEnc = CTC_UTF8;
- cName->unit[0] = '\0';
- cName->unitEnc = CTC_UTF8;
- cName->commonName[0] = '\0';
- cName->commonNameEnc = CTC_UTF8;
- cName->serialDev[0] = '\0';
- cName->serialDevEnc = CTC_PRINTABLE;
- #ifdef WOLFSSL_CERT_EXT
- cName->busCat[0] = '\0';
- cName->busCatEnc = CTC_UTF8;
- cName->joiC[0] = '\0';
- cName->joiCEnc = CTC_PRINTABLE;
- cName->joiSt[0] = '\0';
- cName->joiStEnc = CTC_PRINTABLE;
- #endif
- cName->email[0] = '\0';
-
-
- /* ASN_COUNTRY_NAME */
- WOLFSSL_MSG("Copy Country Name");
- if (CopyX509NameEntry(cName->country, CTC_NAME_SIZE, dn->fullName + dn->cIdx,
- dn->cLen) != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- /* ASN_ORGUNIT_NAME */
- WOLFSSL_MSG("Copy Org Unit Name");
- if (CopyX509NameEntry(cName->unit, CTC_NAME_SIZE, dn->fullName + dn->ouIdx,
- dn->ouLen) != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- /* ASN_ORG_NAME */
- WOLFSSL_MSG("Copy Org Name");
- if (CopyX509NameEntry(cName->org, CTC_NAME_SIZE, dn->fullName + dn->oIdx,
- dn->oLen) != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- /* ASN_STATE_NAME */
- WOLFSSL_MSG("Copy State Name");
- if (CopyX509NameEntry(cName->state, CTC_NAME_SIZE, dn->fullName + dn->stIdx,
- dn->stLen) != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- /* ASN_LOCALITY_NAME */
- WOLFSSL_MSG("Copy Locality Name");
- if (CopyX509NameEntry(cName->locality, CTC_NAME_SIZE,
- dn->fullName + dn->lIdx, dn->lLen)
- != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- /* ASN_SUR_NAME */
- WOLFSSL_MSG("Copy Sur Name");
- if (CopyX509NameEntry(cName->sur, CTC_NAME_SIZE, dn->fullName + dn->snIdx,
- dn->snLen) != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- /* ASN_COMMON_NAME */
- WOLFSSL_MSG("Copy Common Name");
- if (CopyX509NameEntry(cName->commonName, CTC_NAME_SIZE,
- dn->fullName + dn->cnIdx, dn->cnLen)
- != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- /* ASN_SERIAL_NUMBER */
- WOLFSSL_MSG("Copy Serial Number of Device");
- if (CopyX509NameEntry(cName->serialDev, CTC_NAME_SIZE,
- dn->fullName + dn->serialIdx, dn->serialLen)
- != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- #ifdef WOLFSSL_CERT_EXT
- /* ASN_BUS_CAT */
- WOLFSSL_MSG("Copy Business Category");
- if (CopyX509NameEntry(cName->busCat, CTC_NAME_SIZE,
- dn->fullName + dn->bcIdx, dn->bcLen)
- != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- /* JoI Country */
- WOLFSSL_MSG("Copy Jurisdiction of Incorporation Country");
- if (CopyX509NameEntry(cName->joiC, CTC_NAME_SIZE,
- dn->fullName + dn->jcIdx, dn->jcLen)
- != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- /* JoI State */
- WOLFSSL_MSG("Copy Jurisdiction of Incorporation State");
- if (CopyX509NameEntry(cName->joiSt, CTC_NAME_SIZE,
- dn->fullName + dn->jsIdx, dn->jsLen)
- != SSL_SUCCESS) {
- return BUFFER_E;
- }
- #endif
-
- WOLFSSL_MSG("Copy Email");
- if (CopyX509NameEntry(cName->email, CTC_NAME_SIZE,
- dn->fullName + dn->emailIdx, dn->emailLen)
- != SSL_SUCCESS) {
- return BUFFER_E;
- }
-
- return WOLFSSL_SUCCESS;
- }
-
-#ifdef WOLFSSL_CERT_REQ
- static int ReqCertFromX509(Cert* cert, WOLFSSL_X509* req)
- {
- int ret;
-
- if (wc_InitCert(cert) != 0)
- return WOLFSSL_FAILURE;
-
- ret = CopyX509NameToCertName(&req->subject, &cert->subject);
- if (ret == WOLFSSL_SUCCESS) {
- cert->version = req->version;
- cert->isCA = req->isCa;
-#ifdef WOLFSSL_CERT_EXT
- if (req->subjKeyIdSz != 0) {
- XMEMCPY(cert->skid, req->subjKeyId, req->subjKeyIdSz);
- cert->skidSz = req->subjKeyIdSz;
- }
- if (req->keyUsageSet)
- cert->keyUsage = req->keyUsage;
- /* Extended Key Usage not supported. */
-#endif
- }
-
- return ret;
- }
-#endif
-
- /* convert a WOLFSSL_X509 to a Cert structure for writing out */
- static int CertFromX509(Cert* cert, WOLFSSL_X509* x509)
- {
- int ret;
- #ifdef WOLFSSL_CERT_EXT
- int i;
- #endif
-
- WOLFSSL_ENTER("wolfSSL_X509_to_Cert()");
-
- if (x509 == NULL || cert == NULL) {
- return BAD_FUNC_ARG;
- }
-
- wc_InitCert(cert);
-
- cert->version = (int)wolfSSL_X509_get_version(x509);
-
- #ifdef WOLFSSL_ALT_NAMES
- if (x509->notBefore.length > 0) {
- if ((x509->notBefore.length + 2) < CTC_DATE_SIZE) {
- cert->beforeDate[0] = x509->notBefore.type;
- cert->beforeDate[1] = x509->notBefore.length;
- XMEMCPY(&cert->beforeDate[2], x509->notBefore.data,
- x509->notBefore.length);
- cert->beforeDateSz = x509->notBefore.length + 2;
- }
- else {
- WOLFSSL_MSG("Not before date too large");
- return WOLFSSL_FAILURE;
- }
- }
- else {
- cert->beforeDateSz = 0;
- }
- if (x509->notAfter.length > 0) {
- if ((x509->notAfter.length + 2) < CTC_DATE_SIZE) {
- cert->afterDate[0] = x509->notAfter.type;
- cert->afterDate[1] = x509->notAfter.length;
- XMEMCPY(&cert->afterDate[2], x509->notAfter.data,
- x509->notAfter.length);
- cert->afterDateSz = x509->notAfter.length + 2;
- }
- else {
- WOLFSSL_MSG("Not after date too large");
- return WOLFSSL_FAILURE;
- }
- }
- else {
- cert->afterDateSz = 0;
- }
-
- cert->altNamesSz = FlattenAltNames(cert->altNames,
- sizeof(cert->altNames), x509->altNames);
-
- #endif /* WOLFSSL_ALT_NAMES */
-
- cert->sigType = wolfSSL_X509_get_signature_type(x509);
- cert->keyType = x509->pubKeyOID;
- cert->isCA = wolfSSL_X509_get_isCA(x509);
-
- #ifdef WOLFSSL_CERT_EXT
- if (x509->subjKeyIdSz < CTC_MAX_SKID_SIZE) {
- XMEMCPY(cert->skid, x509->subjKeyId, x509->subjKeyIdSz);
- cert->skidSz = (int)x509->subjKeyIdSz;
- }
- else {
- WOLFSSL_MSG("Subject Key ID too large");
- return WOLFSSL_FAILURE;
- }
-
- if (x509->authKeyIdSz < CTC_MAX_AKID_SIZE) {
- XMEMCPY(cert->akid, x509->authKeyId, x509->authKeyIdSz);
- cert->akidSz = (int)x509->authKeyIdSz;
- }
- else {
- WOLFSSL_MSG("Auth Key ID too large");
- return WOLFSSL_FAILURE;
- }
-
- for (i = 0; i < x509->certPoliciesNb; i++) {
- /* copy the smaller of MAX macros, by default they are currently equal*/
- if ((int)CTC_MAX_CERTPOL_SZ <= (int)MAX_CERTPOL_SZ) {
- XMEMCPY(cert->certPolicies[i], x509->certPolicies[i],
- CTC_MAX_CERTPOL_SZ);
- }
- else {
- XMEMCPY(cert->certPolicies[i], x509->certPolicies[i],
- MAX_CERTPOL_SZ);
- }
- }
- cert->certPoliciesNb = (word16)x509->certPoliciesNb;
-
- cert->keyUsage = x509->keyUsage;
- #endif /* WOLFSSL_CERT_EXT */
-
- #ifdef WOLFSSL_CERT_REQ
- /* copy over challenge password for REQ certs */
- XMEMCPY(cert->challengePw, x509->challengePw, CTC_NAME_SIZE);
- #endif
-
- if (x509->serialSz <= CTC_SERIAL_SIZE) {
- XMEMCPY(cert->serial, x509->serial, x509->serialSz);
- }
- else {
- WOLFSSL_MSG("Serial size error");
- return WOLFSSL_FAILURE;
- }
-
- /* copy over Name structures */
- if (x509->issuerSet)
- cert->selfSigned = 0;
- if ((ret = CopyX509NameToCertName(&(x509->issuer), &(cert->issuer)))
- != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error copying over issuer names");
- WOLFSSL_LEAVE("wolfSSL_X509_to_Cert()", ret);
- return WOLFSSL_FAILURE;
- }
- if ((ret = CopyX509NameToCertName(&(x509->subject), &(cert->subject)))
- != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error copying over subject names");
- WOLFSSL_LEAVE("wolfSSL_X509_to_Cert()", ret);
- return WOLFSSL_FAILURE;
- }
-
- cert->heap = x509->heap;
-
- return WOLFSSL_SUCCESS;
- }
-
-
- /* returns the sig type to use on success i.e CTC_SHAwRSA and WOLFSSL_FALURE
- * on fail case */
- static int wolfSSL_sigTypeFromPKEY(WOLFSSL_EVP_MD* md,
- WOLFSSL_EVP_PKEY* pkey)
- {
- int hashType;
- int sigType = WOLFSSL_FAILURE;
-
- /* Convert key type and hash algorithm to a signature algorithm */
- if (wolfSSL_EVP_get_hashinfo(md, &hashType, NULL) == WOLFSSL_FAILURE)
- return WOLFSSL_FAILURE;
-
-
- if (pkey->type == EVP_PKEY_RSA) {
- switch (hashType) {
- case WC_HASH_TYPE_SHA:
- sigType = CTC_SHAwRSA;
- break;
- case WC_HASH_TYPE_SHA224:
- sigType = CTC_SHA224wRSA;
- break;
- case WC_HASH_TYPE_SHA256:
- sigType = CTC_SHA256wRSA;
- break;
- case WC_HASH_TYPE_SHA384:
- sigType = CTC_SHA384wRSA;
- break;
- case WC_HASH_TYPE_SHA512:
- sigType = CTC_SHA512wRSA;
- break;
- default:
- return WOLFSSL_FAILURE;
- }
- }
- else if (pkey->type == EVP_PKEY_EC) {
- switch (hashType) {
- case WC_HASH_TYPE_SHA:
- sigType = CTC_SHAwECDSA;
- break;
- case WC_HASH_TYPE_SHA224:
- sigType = CTC_SHA224wECDSA;
- break;
- case WC_HASH_TYPE_SHA256:
- sigType = CTC_SHA256wECDSA;
- break;
- case WC_HASH_TYPE_SHA384:
- sigType = CTC_SHA384wECDSA;
- break;
- case WC_HASH_TYPE_SHA512:
- sigType = CTC_SHA512wECDSA;
- break;
- default:
- return WOLFSSL_FAILURE;
- }
- }
- else
- return WOLFSSL_FAILURE;
- return sigType;
- }
-
-
- /* generates DER buffer from WOLFSSL_X509
- * If req == 1 then creates a request DER buffer
- *
- * updates derSz with certificate body size on success
- * return WOLFSSL_SUCCESS on success
- */
- static int wolfSSL_X509_make_der(WOLFSSL_X509* x509, int req,
- unsigned char* der, int* derSz)
- {
- int ret;
- Cert cert;
- void* key = NULL;
- int type = -1;
- #ifndef NO_RSA
- RsaKey rsa;
- #endif
- #ifdef HAVE_ECC
- ecc_key ecc;
- #endif
- WC_RNG rng;
- word32 idx = 0;
-
- if (x509 == NULL || der == NULL || derSz == NULL)
- return BAD_FUNC_ARG;
-
- #ifndef WOLFSSL_CERT_REQ
- if (req) {
- WOLFSSL_MSG("WOLFSSL_CERT_REQ needed for certificate request");
- return WOLFSSL_FAILURE;
- }
- #endif
-
- #ifdef WOLFSSL_CERT_REQ
- if (req) {
- if (ReqCertFromX509(&cert, x509) != WOLFSSL_SUCCESS)
- return WOLFSSL_FAILURE;
- }
- else
- #endif
- {
- /* Create a Cert that has the certificate fields. */
- if (CertFromX509(&cert, x509) != WOLFSSL_SUCCESS)
- return WOLFSSL_FAILURE;
- }
-
- /* Create a public key object from requests public key. */
- #ifndef NO_RSA
- if (x509->pubKeyOID == RSAk) {
- type = RSA_TYPE;
- ret = wc_InitRsaKey(&rsa, x509->heap);
- if (ret != 0)
- return ret;
- ret = wc_RsaPublicKeyDecode(x509->pubKey.buffer, &idx, &rsa,
- x509->pubKey.length);
- if (ret != 0) {
- wc_FreeRsaKey(&rsa);
- return ret;
- }
- key = (void*)&rsa;
- }
- #endif
- #ifdef HAVE_ECC
- if (x509->pubKeyOID == ECDSAk) {
- type = ECC_TYPE;
- ret = wc_ecc_init(&ecc);
- if (ret != 0)
- return ret;
- ret = wc_EccPublicKeyDecode(x509->pubKey.buffer, &idx, &ecc,
- x509->pubKey.length);
- if (ret != 0) {
- wc_ecc_free(&ecc);
- return ret;
- }
- key = (void*)&ecc;
- }
- #endif
- if (key == NULL)
- return WOLFSSL_FAILURE;
-
- /* Make the body of the certificate request. */
- #ifdef WOLFSSL_CERT_REQ
- if (req) {
- ret = wc_MakeCertReq_ex(&cert, der, *derSz, type, key);
- }
- else
- #endif
- {
- ret = wc_InitRng(&rng);
- if (ret != 0)
- return WOLFSSL_FAILURE;
-
- ret = wc_MakeCert_ex(&cert, der, *derSz, type, key, &rng);
- wc_FreeRng(&rng);
- }
- if (ret < 0) {
- return ret;
- }
-
- /* Dispose of the public key object. */
- #ifndef NO_RSA
- if (x509->pubKeyOID == RSAk)
- wc_FreeRsaKey(&rsa);
- #endif
- #ifdef HAVE_ECC
- if (x509->pubKeyOID == ECDSAk)
- wc_ecc_free(&ecc);
- #endif
- *derSz = ret;
-
- return WOLFSSL_SUCCESS;
- }
-
-
- /* signs a der buffer for the WOLFSSL_X509 structure using the PKEY and MD
- * hash passed in
- *
- * WARNING: this free's and replaces the existing DER buffer in the
- * WOLFSSL_X509 with the newly signed buffer.
- * returns size of signed buffer on success and negative values on fail
- */
- static int wolfSSL_X509_resign_cert(WOLFSSL_X509* x509, int req,
- unsigned char* der, int derSz, int certBodySz, WOLFSSL_EVP_MD* md,
- WOLFSSL_EVP_PKEY* pkey)
- {
- int ret;
- void* key = NULL;
- int type = -1;
- int sigType;
- WC_RNG rng;
-
- sigType = wolfSSL_sigTypeFromPKEY(md, pkey);
- if (sigType == WOLFSSL_FAILURE)
- return WOLFSSL_FATAL_ERROR;
-
-
- /* Get the private key object and type from pkey. */
- #ifndef NO_RSA
- if (pkey->type == EVP_PKEY_RSA) {
- type = RSA_TYPE;
- key = pkey->rsa->internal;
- }
- #endif
- #ifdef HAVE_ECC
- if (pkey->type == EVP_PKEY_EC) {
- type = ECC_TYPE;
- key = pkey->ecc->internal;
- }
- #endif
-
- /* Sign the certificate request body. */
- ret = wc_InitRng(&rng);
- if (ret != 0)
- return ret;
- ret = wc_SignCert_ex(certBodySz, sigType, der, derSz, type, key, &rng);
- wc_FreeRng(&rng);
- if (ret < 0)
- return ret;
-
- /* Put in the new certificate encoding into the x509 object. */
- FreeDer(&x509->derCert);
- type = CERT_TYPE;
- #ifdef WOLFSSL_REQ_CERT
- if (req) {
- type = CERTREQ_TYPE;
- }
- #endif
-
- if (AllocDer(&x509->derCert, ret, type, NULL) != 0)
- return WOLFSSL_FATAL_ERROR;
- XMEMCPY(x509->derCert->buffer, der, ret);
- x509->derCert->length = ret;
-
- (void)req;
- return ret;
- }
-
-
- /* returns the size of signature on success */
- int wolfSSL_X509_sign(WOLFSSL_X509* x509, WOLFSSL_EVP_PKEY* pkey,
- const WOLFSSL_EVP_MD* md)
- {
- int ret;
- byte der[4096]; /* @TODO dynamic set based on expected cert size */
- int derSz = sizeof(der);
-
- WOLFSSL_ENTER("wolfSSL_X509_sign");
-
- if (x509 == NULL || pkey == NULL || md == NULL)
- return WOLFSSL_FAILURE;
-
- x509->sigOID = wolfSSL_sigTypeFromPKEY((WOLFSSL_EVP_MD*)md, pkey);
- if ((ret = wolfSSL_X509_make_der(x509, 0, der, &derSz)) !=
- WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Unable to make DER for X509");
- WOLFSSL_LEAVE("wolfSSL_X509_sign", ret);
- return WOLFSSL_FAILURE;
- }
-
- ret = wolfSSL_X509_resign_cert(x509, 0, der, sizeof(der), derSz,
- (WOLFSSL_EVP_MD*)md, pkey);
- if (ret <= 0) {
- WOLFSSL_LEAVE("wolfSSL_X509_sign", ret);
- return WOLFSSL_FAILURE;
- }
-
- return ret;
- }
-
-
- /* Converts the x509 name structure into DER format.
- *
- * out pointer to either a pre setup buffer or a pointer to null for
- * creating a dynamic buffer. In the case that a pre-existing buffer is
- * used out will be incremented the size of the DER buffer on success.
- *
- * returns the size of the buffer on success, or negative value with failure
- */
- int wolfSSL_i2d_X509_NAME(WOLFSSL_X509_NAME* name, unsigned char** out)
- {
- CertName cName;
- unsigned char buf[256]; /* ASN_MAX_NAME */
- int sz;
- WOLFSSL_ENTER("wolfSSL_i2d_X509_NAME");
-
- if (out == NULL || name == NULL) {
- return BAD_FUNC_ARG;
- }
- XMEMSET(&cName, 0, sizeof(CertName));
-
- if (CopyX509NameToCertName(name, &cName) != SSL_SUCCESS) {
- WOLFSSL_MSG("Error converting x509 name to internal CertName");
- return SSL_FATAL_ERROR;
- }
-
- sz = SetName(buf, sizeof(buf), &cName);
- if (sz < 0) {
- return sz;
- }
-
- /* using buffer passed in */
- if (*out != NULL) {
- XMEMCPY(*out, buf, sz);
- *out += sz;
- }
- else {
- *out = (unsigned char*)XMALLOC(sz, NULL, DYNAMIC_TYPE_OPENSSL);
- if (*out == NULL) {
- return MEMORY_E;
- }
- XMEMCPY(*out, buf, sz);
- }
-
- return sz;
- }
-#endif /* WOLFSSL_CERT_GEN */
-
-
- /* Compares the two X509 names. If the size of x is larger then y then a
- * positive value is returned if x is smaller a negative value is returned.
- * In the case that the sizes are equal a the value of strcmp between the
- * two names is returned.
- *
- * x First name for comparison
- * y Second name to compare with x
- */
- int wolfSSL_X509_NAME_cmp(const WOLFSSL_X509_NAME* x,
- const WOLFSSL_X509_NAME* y)
- {
- const char* _x;
- const char* _y;
- WOLFSSL_ENTER("wolfSSL_X509_NAME_cmp");
-
- if (x == NULL || y == NULL) {
- WOLFSSL_MSG("Bad argument passed in");
- return -2;
- }
-
- if (x == y) {
- return 0; /* match */
- }
-
- if (x->sz != y->sz) {
- return x->sz - y->sz;
- }
-
- /*
- * If the name member is not set or is immediately null terminated then
- * compare the staticName member
- */
- _x = (x->name && *x->name) ? x->name : x->staticName;
- _y = (y->name && *y->name) ? y->name : y->staticName;
-
- return XSTRNCMP(_x, _y, x->sz); /* y sz is the same */
- }
-
-
- WOLFSSL_X509 *wolfSSL_PEM_read_bio_X509(WOLFSSL_BIO *bp, WOLFSSL_X509 **x,
- pem_password_cb *cb, void *u)
- {
- WOLFSSL_X509* x509 = NULL;
-#if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
- unsigned char* pem = NULL;
- int pemSz;
- long i = 0, l;
- const char* footer = NULL;
-
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_X509");
-
- if (bp == NULL) {
- WOLFSSL_LEAVE("wolfSSL_PEM_read_bio_X509", BAD_FUNC_ARG);
- return NULL;
- }
-
- if ((l = wolfSSL_BIO_get_len(bp)) <= 0) {
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
- /* No certificate in buffer */
- WOLFSSL_ERROR(ASN_NO_PEM_HEADER);
- #endif
- return NULL;
- }
-
- pem = (unsigned char*)XMALLOC(l, 0, DYNAMIC_TYPE_PEM);
- if (pem == NULL)
- return NULL;
-
- i = 0;
- if (wc_PemGetHeaderFooter(CERT_TYPE, NULL, &footer) != 0) {
- XFREE(pem, 0, DYNAMIC_TYPE_PEM);
- return NULL;
- }
-
- /* TODO: Inefficient
- * reading in one byte at a time until see "END CERTIFICATE"
- */
- while ((l = wolfSSL_BIO_read(bp, (char *)&pem[i], 1)) == 1) {
- i++;
- if (i > 26 && XMEMCMP((char *)&pem[i-26], footer, 25) == 0) {
- if (pem[i-1] == '\r') {
- /* found \r , Windows line ending is \r\n so try to read one
- * more byte for \n, ignoring return value */
- (void)wolfSSL_BIO_read(bp, (char *)&pem[i++], 1);
- }
- break;
- }
- }
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
- if (l == 0)
- WOLFSSL_ERROR(ASN_NO_PEM_HEADER);
- #endif
- pemSz = (int)i;
- x509 = wolfSSL_X509_load_certificate_buffer(pem, pemSz,
- WOLFSSL_FILETYPE_PEM);
-
- if (x != NULL) {
- *x = x509;
- }
-
- XFREE(pem, NULL, DYNAMIC_TYPE_PEM);
-
-#endif /* WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM */
- (void)bp;
- (void)x;
- (void)cb;
- (void)u;
-
- return x509;
- }
-
- WOLFSSL_X509_CRL *wolfSSL_PEM_read_bio_X509_CRL(WOLFSSL_BIO *bp,
- WOLFSSL_X509_CRL **x, pem_password_cb *cb, void *u)
- {
-#if defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_CRL)
- unsigned char* pem = NULL;
- int pemSz;
- int derSz;
- DerBuffer* der = NULL;
- WOLFSSL_X509_CRL* crl = NULL;
-
- if ((pemSz = wolfSSL_BIO_get_len(bp)) <= 0) {
- goto err;
- }
-
- pem = (unsigned char*)XMALLOC(pemSz, 0, DYNAMIC_TYPE_PEM);
- if (pem == NULL) {
- goto err;
- }
-
- if (wolfSSL_BIO_read(bp, pem, pemSz) != pemSz) {
- goto err;
- }
-
- if((PemToDer(pem, pemSz, CRL_TYPE, &der, NULL, NULL, NULL)) < 0) {
- goto err;
- }
- derSz = der->length;
- if((crl = wolfSSL_d2i_X509_CRL(x, der->buffer, derSz)) == NULL) {
- goto err;
- }
-
-err:
- if(pem != NULL) {
- XFREE(pem, 0, DYNAMIC_TYPE_PEM);
- }
- if(der != NULL) {
- FreeDer(&der);
- }
-
- (void)cb;
- (void)u;
-
- return crl;
-#else
- (void)bp;
- (void)x;
- (void)cb;
- (void)u;
-
- return NULL;
-#endif
- }
-
-#if !defined(NO_FILESYSTEM)
- static void* wolfSSL_PEM_read_X509_ex(XFILE fp, void **x,
- pem_password_cb *cb, void *u, int type)
- {
- unsigned char* pem = NULL;
- int pemSz;
- long i = 0, l;
- void *newx509;
- int derSz;
- DerBuffer* der = NULL;
-
- WOLFSSL_ENTER("wolfSSL_PEM_read_X509");
-
- if (fp == XBADFILE) {
- WOLFSSL_LEAVE("wolfSSL_PEM_read_X509", BAD_FUNC_ARG);
- return NULL;
- }
- /* Read cert from file */
- i = XFTELL(fp);
- if (i < 0) {
- WOLFSSL_LEAVE("wolfSSL_PEM_read_X509", BAD_FUNC_ARG);
- return NULL;
- }
-
- if (XFSEEK(fp, 0, XSEEK_END) != 0)
- return NULL;
- l = XFTELL(fp);
- if (l < 0)
- return NULL;
- if (XFSEEK(fp, i, SEEK_SET) != 0)
- return NULL;
- pemSz = (int)(l - i);
-
- /* check calculated length */
- if (pemSz > MAX_WOLFSSL_FILE_SIZE || pemSz < 0) {
- WOLFSSL_MSG("PEM_read_X509_ex file size error");
- return NULL;
- }
-
- /* allocate pem buffer */
- pem = (unsigned char*)XMALLOC(pemSz, NULL, DYNAMIC_TYPE_PEM);
- if (pem == NULL)
- return NULL;
-
- if ((int)XFREAD((char *)pem, 1, pemSz, fp) != pemSz)
- goto err_exit;
-
- switch (type) {
- case CERT_TYPE:
- newx509 = (void *)wolfSSL_X509_load_certificate_buffer(pem,
- pemSz, WOLFSSL_FILETYPE_PEM);
- break;
-
- #ifdef HAVE_CRL
- case CRL_TYPE:
- if ((PemToDer(pem, pemSz, CRL_TYPE, &der, NULL, NULL, NULL)) < 0)
- goto err_exit;
- derSz = der->length;
- newx509 = (void*)wolfSSL_d2i_X509_CRL((WOLFSSL_X509_CRL **)x,
- (const unsigned char *)der->buffer, derSz);
- if (newx509 == NULL)
- goto err_exit;
- FreeDer(&der);
- break;
- #endif
-
- default:
- goto err_exit;
- }
- if (x != NULL) {
- *x = newx509;
- }
- XFREE(pem, NULL, DYNAMIC_TYPE_PEM);
- return newx509;
-
- err_exit:
- if (pem != NULL)
- XFREE(pem, NULL, DYNAMIC_TYPE_PEM);
- if (der != NULL)
- FreeDer(&der);
-
- /* unused */
- (void)cb;
- (void)u;
- (void)derSz;
-
- return NULL;
- }
-
- WOLFSSL_API WOLFSSL_X509* wolfSSL_PEM_read_X509(XFILE fp, WOLFSSL_X509 **x,
- pem_password_cb *cb, void *u)
- {
- return (WOLFSSL_X509* )wolfSSL_PEM_read_X509_ex(fp, (void **)x, cb, u, CERT_TYPE);
- }
-
-#if defined(HAVE_CRL)
- WOLFSSL_API WOLFSSL_X509_CRL* wolfSSL_PEM_read_X509_CRL(XFILE fp, WOLFSSL_X509_CRL **crl,
- pem_password_cb *cb, void *u)
- {
- return (WOLFSSL_X509_CRL* )wolfSSL_PEM_read_X509_ex(fp, (void **)crl, cb, u, CRL_TYPE);
- }
-#endif
-
- int wolfSSL_PEM_write_X509(XFILE fp, WOLFSSL_X509* x)
- {
- int ret;
- WOLFSSL_BIO* bio;
-
- if (x == NULL)
- return 0;
-
- bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
- if (bio == NULL)
- return 0;
-
- if (wolfSSL_BIO_set_fp(bio, fp, BIO_NOCLOSE) != WOLFSSL_SUCCESS) {
- wolfSSL_BIO_free(bio);
- bio = NULL;
- }
-
- ret = wolfSSL_PEM_write_bio_X509(bio, x);
-
- if (bio != NULL)
- wolfSSL_BIO_free(bio);
-
- return ret;
- }
-#endif /* !NO_FILESYSTEM */
-
- #define PEM_BEGIN "-----BEGIN "
- #define PEM_BEGIN_SZ 11
- #define PEM_END "-----END "
- #define PEM_END_SZ 9
- #define PEM_HDR_FIN "-----"
- #define PEM_HDR_FIN_SZ 5
- #define PEM_HDR_FIN_EOL_NEWLINE "-----\n"
- #define PEM_HDR_FIN_EOL_NULL_TERM "-----\0"
- #define PEM_HDR_FIN_EOL_SZ 6
-
- int wolfSSL_PEM_read_bio(WOLFSSL_BIO* bio, char **name, char **header,
- unsigned char **data, long *len)
- {
- int ret = WOLFSSL_SUCCESS;
- char pem[256];
- int pemLen;
- char* p;
- char* nameStr = NULL;
- int nameLen = 0;
- char* headerStr = NULL;
- int headerLen;
- int headerFound = 0;
- unsigned char* der = NULL;
- word32 derLen = 0;
-
- if (bio == NULL || name == NULL || header == NULL || data == NULL ||
- len == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* Find header line. */
- pem[sizeof(pem) - 1] = '\0';
- while ((pemLen = wolfSSL_BIO_gets(bio, pem, sizeof(pem) - 1)) > 0) {
- if (XSTRNCMP(pem, PEM_BEGIN, PEM_BEGIN_SZ) == 0)
- break;
- }
- if (pemLen <= 0)
- ret = WOLFSSL_FAILURE;
- /* Have a header line. */
- if (ret == WOLFSSL_SUCCESS) {
- while (pem[pemLen - 1] == '\r' || pem[pemLen - 1] == '\n')
- pemLen--;
- pem[pemLen] = '\0';
- if (XSTRNCMP(pem + pemLen - PEM_HDR_FIN_SZ, PEM_HDR_FIN,
- PEM_HDR_FIN_SZ) != 0) {
- ret = WOLFSSL_FAILURE;
- }
- }
-
- /* Get out name. */
- if (ret == WOLFSSL_SUCCESS) {
- nameLen = pemLen - PEM_BEGIN_SZ - PEM_HDR_FIN_SZ;
- nameStr = (char*)XMALLOC(nameLen + 1, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (nameStr == NULL)
- ret = WOLFSSL_FAILURE;
- }
- if (ret == WOLFSSL_SUCCESS) {
- XSTRNCPY(nameStr, pem + PEM_BEGIN_SZ, nameLen);
- nameStr[nameLen] = '\0';
-
- /* Get header of PEM - encryption header. */
- headerLen = 0;
- while ((pemLen = wolfSSL_BIO_gets(bio, pem, sizeof(pem) - 1)) > 0) {
- while (pemLen > 0 && (pem[pemLen - 1] == '\r' ||
- pem[pemLen - 1] == '\n')) {
- pemLen--;
- }
- pem[pemLen++] = '\n';
- pem[pemLen] = '\0';
-
- /* Header separator is a blank line. */
- if (pem[0] == '\n') {
- headerFound = 1;
- break;
- }
-
- /* Didn't find a blank line - no header. */
- if (XSTRNCMP(pem, PEM_END, PEM_END_SZ) == 0) {
- der = (unsigned char*)headerStr;
- derLen = headerLen;
- /* Empty header - empty string. */
- headerStr = (char*)XMALLOC(1, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (headerStr == NULL)
- ret = WOLFSSL_FAILURE;
- else
- headerStr[0] = '\0';
- break;
- }
-
- p = (char*)XREALLOC(headerStr, headerLen + pemLen + 1, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (p == NULL) {
- ret = WOLFSSL_FAILURE;
- break;
- }
-
- headerStr = p;
- XMEMCPY(headerStr + headerLen, pem, pemLen + 1);
- headerLen += pemLen;
- }
- if (pemLen <= 0)
- ret = WOLFSSL_FAILURE;
- }
-
- /* Get body of PEM - if there was a header */
- if (ret == WOLFSSL_SUCCESS && headerFound) {
- derLen = 0;
- while ((pemLen = wolfSSL_BIO_gets(bio, pem, sizeof(pem) - 1)) > 0) {
- while (pemLen > 0 && (pem[pemLen - 1] == '\r' ||
- pem[pemLen - 1] == '\n')) {
- pemLen--;
- }
- pem[pemLen++] = '\n';
- pem[pemLen] = '\0';
-
- if (XSTRNCMP(pem, PEM_END, PEM_END_SZ) == 0)
- break;
-
- p = (char*)XREALLOC(der, derLen + pemLen + 1, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (p == NULL) {
- ret = WOLFSSL_FAILURE;
- break;
- }
-
- der = (unsigned char*)p;
- XMEMCPY(der + derLen, pem, pemLen + 1);
- derLen += pemLen;
- }
- if (pemLen <= 0)
- ret = WOLFSSL_FAILURE;
- }
-
- /* Check trailer. */
- if (ret == WOLFSSL_SUCCESS) {
- if (XSTRNCMP(pem + PEM_END_SZ, nameStr, nameLen) != 0)
- ret = WOLFSSL_FAILURE;
- }
- if (ret == WOLFSSL_SUCCESS) {
- if (XSTRNCMP(pem + PEM_END_SZ + nameLen,
- PEM_HDR_FIN_EOL_NEWLINE,
- PEM_HDR_FIN_EOL_SZ) != 0 &&
- XSTRNCMP(pem + PEM_END_SZ + nameLen,
- PEM_HDR_FIN_EOL_NULL_TERM,
- PEM_HDR_FIN_EOL_SZ) != 0) {
- ret = WOLFSSL_FAILURE;
- }
- }
-
- /* Base64 decode body. */
- if (ret == WOLFSSL_SUCCESS) {
- if (Base64_Decode(der, derLen, der, &derLen) != 0)
- ret = WOLFSSL_FAILURE;
- }
-
- if (ret == WOLFSSL_SUCCESS) {
- *name = nameStr;
- *header = headerStr;
- *data = der;
- *len = derLen;
- nameStr = NULL;
- headerStr = NULL;
- der = NULL;
- }
-
- if (nameStr != NULL)
- XFREE(nameStr, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (headerStr != NULL)
- XFREE(headerStr, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (der != NULL)
- XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- return ret;
- }
-
- int wolfSSL_PEM_write_bio(WOLFSSL_BIO* bio, const char *name,
- const char *header, const unsigned char *data,
- long len)
- {
- int err = 0;
- int outSz = 0;
- int nameLen;
- int headerLen;
- byte* pem = NULL;
- word32 pemLen;
- word32 derLen = (word32)len;
-
- if (bio == NULL || name == NULL || header == NULL || data == NULL)
- return 0;
-
- nameLen = (int)XSTRLEN(name);
- headerLen = (int)XSTRLEN(header);
-
- pemLen = (derLen + 2) / 3 * 4;
- pemLen += (pemLen + 63) / 64;
-
- pem = (byte*)XMALLOC(pemLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- err = pem == NULL;
- if (!err)
- err = Base64_Encode(data, derLen, pem, &pemLen) != 0;
-
- if (!err) {
- err = wolfSSL_BIO_write(bio, PEM_BEGIN, PEM_BEGIN_SZ) !=
- (int)PEM_BEGIN_SZ;
- }
- if (!err)
- err = wolfSSL_BIO_write(bio, name, nameLen) != nameLen;
- if (!err) {
- err = wolfSSL_BIO_write(bio, PEM_HDR_FIN_EOL_NEWLINE,
- PEM_HDR_FIN_EOL_SZ) != (int)PEM_HDR_FIN_EOL_SZ;
- }
- if (!err && headerLen > 0) {
- err = wolfSSL_BIO_write(bio, header, headerLen) != headerLen;
- /* Blank line after a header and before body. */
- if (!err)
- err = wolfSSL_BIO_write(bio, "\n", 1) != 1;
- headerLen++;
- }
- if (!err)
- err = wolfSSL_BIO_write(bio, pem, pemLen) != (int)pemLen;
- if (!err)
- err = wolfSSL_BIO_write(bio, PEM_END, PEM_END_SZ) !=
- (int)PEM_END_SZ;
- if (!err)
- err = wolfSSL_BIO_write(bio, name, nameLen) != nameLen;
- if (!err) {
- err = wolfSSL_BIO_write(bio, PEM_HDR_FIN_EOL_NEWLINE,
- PEM_HDR_FIN_EOL_SZ) != (int)PEM_HDR_FIN_EOL_SZ;
- }
-
- if (!err) {
- outSz = PEM_BEGIN_SZ + nameLen + PEM_HDR_FIN_EOL_SZ + headerLen +
- pemLen + PEM_END_SZ + nameLen + PEM_HDR_FIN_EOL_SZ;
- }
-
- if (pem != NULL)
- XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- return outSz;
- }
-
-#if !defined(NO_FILESYSTEM)
- int wolfSSL_PEM_read(XFILE fp, char **name, char **header,
- unsigned char **data, long *len)
- {
- int ret;
- WOLFSSL_BIO* bio;
-
- if (name == NULL || header == NULL || data == NULL || len == NULL)
- return WOLFSSL_FAILURE;
-
- bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
- if (bio == NULL)
- return 0;
-
- if (wolfSSL_BIO_set_fp(bio, fp, BIO_NOCLOSE) != WOLFSSL_SUCCESS) {
- wolfSSL_BIO_free(bio);
- bio = NULL;
- }
-
- ret = wolfSSL_PEM_read_bio(bio, name, header, data, len);
-
- if (bio != NULL)
- wolfSSL_BIO_free(bio);
-
- return ret;
- }
-
- int wolfSSL_PEM_write(XFILE fp, const char *name, const char *header,
- const unsigned char *data, long len)
- {
- int ret;
- WOLFSSL_BIO* bio;
-
- if (name == NULL || header == NULL || data == NULL)
- return 0;
-
- bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
- if (bio == NULL)
- return 0;
-
- if (wolfSSL_BIO_set_fp(bio, fp, BIO_NOCLOSE) != WOLFSSL_SUCCESS) {
- wolfSSL_BIO_free(bio);
- bio = NULL;
- }
-
- ret = wolfSSL_PEM_write_bio(bio, name, header, data, len);
-
- if (bio != NULL)
- wolfSSL_BIO_free(bio);
-
- return ret;
- }
-#endif
-
- int wolfSSL_PEM_get_EVP_CIPHER_INFO(char* header, EncryptedInfo* cipher)
- {
- if (header == NULL || cipher == NULL)
- return WOLFSSL_FAILURE;
-
- XMEMSET(cipher, 0, sizeof(*cipher));
-
- if (wc_EncryptedInfoParse(cipher, &header, XSTRLEN(header)) != 0)
- return WOLFSSL_FAILURE;
-
- return WOLFSSL_SUCCESS;
- }
-
- int wolfSSL_PEM_do_header(EncryptedInfo* cipher, unsigned char* data,
- long* len, pem_password_cb* callback, void* ctx)
- {
- int ret = WOLFSSL_SUCCESS;
- char password[NAME_SZ];
- int passwordSz;
-
- if (cipher == NULL || data == NULL || len == NULL || callback == NULL)
- return WOLFSSL_FAILURE;
-
- passwordSz = callback(password, sizeof(password), PEM_PASS_READ, ctx);
- if (passwordSz < 0)
- ret = WOLFSSL_FAILURE;
-
- if (ret == WOLFSSL_SUCCESS) {
- if (wc_BufferKeyDecrypt(cipher, data, (word32)*len, (byte*)password,
- passwordSz, WC_MD5) != 0) {
- ret = WOLFSSL_FAILURE;
- }
- }
-
- if (passwordSz > 0)
- XMEMSET(password, 0, passwordSz);
-
- return ret;
- }
-
- /*
- * bp : bio to read X509 from
- * x : x509 to write to
- * cb : password call back for reading PEM
- * u : password
- * _AUX is for working with a trusted X509 certificate
- */
- WOLFSSL_X509 *wolfSSL_PEM_read_bio_X509_AUX(WOLFSSL_BIO *bp,
- WOLFSSL_X509 **x, pem_password_cb *cb, void *u) {
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_X509");
-
- /* AUX info is; trusted/rejected uses, friendly name, private key id,
- * and potentially a stack of "other" info. wolfSSL does not store
- * friendly name or private key id yet in WOLFSSL_X509 for human
- * readability and does not support extra trusted/rejected uses for
- * root CA. */
- return wolfSSL_PEM_read_bio_X509(bp, x, cb, u);
- }
-
-
-#ifdef OPENSSL_ALL
- /* create and return a new WOLFSSL_X509_PKEY structure or NULL on failure */
- static WOLFSSL_X509_PKEY* wolfSSL_X509_PKEY_new(void* heap)
- {
- WOLFSSL_X509_PKEY* ret;
-
- ret = (WOLFSSL_X509_PKEY*)XMALLOC(sizeof(WOLFSSL_X509_PKEY), heap,
- DYNAMIC_TYPE_KEY);
- if (ret != NULL) {
- XMEMSET(ret, 0, sizeof(WOLFSSL_X509_PKEY));
- ret->heap = heap;
- }
- return ret;
- }
-
-
- /* sets the values of X509_PKEY based on certificate passed in
- * return WOLFSSL_SUCCESS on success */
- static int wolfSSL_X509_PKEY_set(WOLFSSL_X509_PKEY* xPkey,
- WOLFSSL_X509* x509)
- {
- if (xPkey == NULL || x509 == NULL) {
- return BAD_FUNC_ARG;
- }
- wolfSSL_EVP_PKEY_free(xPkey->dec_pkey);
- xPkey->dec_pkey = wolfSSL_X509_get_pubkey(x509);
- if (xPkey->dec_pkey == NULL) {
- return WOLFSSL_FAILURE;
- }
- return WOLFSSL_SUCCESS;
- }
-
-
- /* free up all memory used by "xPkey" passed in */
- static void wolfSSL_X509_PKEY_free(WOLFSSL_X509_PKEY* xPkey)
- {
- if (xPkey != NULL) {
- wolfSSL_EVP_PKEY_free(xPkey->dec_pkey);
- }
- XFREE(xPkey, xPkey->heap, DYNAMIC_TYPE_KEY);
- }
-
-
- /* Takes control of x509 on success
- * helper function to break out code needed to set WOLFSSL_X509_INFO up
- * free's "info" passed in if is not defaults
- *
- * returns WOLFSSL_SUCCESS on success
- */
- static int wolfSSL_X509_INFO_set(WOLFSSL_X509_INFO* info,
- WOLFSSL_X509* x509)
- {
- if (info == NULL || x509 == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* check is fresh "info" passed in, if not free it */
- if (info->x509 != NULL || info->x_pkey != NULL) {
- WOLFSSL_X509_INFO* tmp;
-
- tmp = wolfSSL_X509_INFO_new();
- if (tmp == NULL) {
- WOLFSSL_MSG("Unable to create new structure");
- return MEMORY_E;
- }
- wolfSSL_X509_INFO_free(info);
- info = tmp;
- }
-
- info->x509 = x509;
-
- //@TODO info->num
- //@TODO info->enc_cipher
- //@TODO info->enc_len
- //@TODO info->enc_data
- //@TODO info->crl
-
- info->x_pkey = wolfSSL_X509_PKEY_new(x509->heap);
- return wolfSSL_X509_PKEY_set(info->x_pkey, x509);
- }
-
-
- /*
- * bio WOLFSSL_BIO to read certificates from
- * sk possible stack to push more X509_INFO structs to. Can be NULL
- * cb callback password for encrypted PEM certificates
- * u user input such as password
- *
- * returns stack on success and NULL or default stack passed in on fail
- */
- WOLF_STACK_OF(WOLFSSL_X509_INFO)* wolfSSL_PEM_X509_INFO_read_bio(
- WOLFSSL_BIO* bio, WOLF_STACK_OF(WOLFSSL_X509_INFO)* sk,
- pem_password_cb* cb, void* u)
- {
- WOLF_STACK_OF(WOLFSSL_X509_INFO)* localSk;
- WOLFSSL_X509* x509 = NULL;
- int ret = WOLFSSL_SUCCESS;
-
- WOLFSSL_ENTER("wolfSSL_PEM_X509_INFO_read_bio");
-
- /* attempt to used passed in stack or create a new one */
- if (sk != NULL) {
- localSk = sk;
- }
- else {
- localSk = wolfSSL_sk_X509_INFO_new_null();
- }
- if (localSk == NULL) {
- WOLFSSL_LEAVE("wolfSSL_PEM_X509_INFO_read_bio", MEMORY_E);
- return NULL;
- }
-
- /* parse through BIO and push new info's found onto stack */
- do {
- x509 = wolfSSL_PEM_read_bio_X509(bio, NULL, cb, u);
- if (x509 != NULL) {
- WOLFSSL_X509_INFO* current;
-
- current = wolfSSL_X509_INFO_new();
- if (current == NULL) {
- WOLFSSL_LEAVE("wolfSSL_PEM_X509_INFO_read_bio", MEMORY_E);
- return NULL;
- }
- ret = wolfSSL_X509_INFO_set(current, x509);
- if (ret != WOLFSSL_SUCCESS) {
- wolfSSL_X509_free(x509);
- }
- else {
- wolfSSL_sk_X509_INFO_push(localSk, current);
- }
- }
- } while (x509 != NULL && ret == WOLFSSL_SUCCESS);
- WOLFSSL_LEAVE("wolfSSL_PEM_X509_INFO_read_bio", ret);
- return localSk;
- }
-#endif /* OPENSSL_ALL */
-
- void wolfSSL_X509_NAME_ENTRY_free(WOLFSSL_X509_NAME_ENTRY* ne)
- {
- WOLFSSL_ENTER("wolfSSL_X509_NAME_ENTRY_free");
- if (ne != NULL) {
- if (ne->value != NULL && ne->value != &(ne->data)) {
- wolfSSL_ASN1_STRING_free(ne->value);
- }
- XFREE(ne, NULL, DYNAMIC_TYPE_NAME_ENTRY);
- }
- }
-
-
- WOLFSSL_X509_NAME_ENTRY* wolfSSL_X509_NAME_ENTRY_new(void)
- {
- WOLFSSL_X509_NAME_ENTRY* ne;
-
- ne = (WOLFSSL_X509_NAME_ENTRY*)XMALLOC(sizeof(WOLFSSL_X509_NAME_ENTRY),
- NULL, DYNAMIC_TYPE_NAME_ENTRY);
- if (ne != NULL) {
- XMEMSET(ne, 0, sizeof(WOLFSSL_X509_NAME_ENTRY));
- ne->value = &(ne->data);
- }
-
- return ne;
- }
-
-
- /* Create a new WOLFSSL_X509_NAME_ENTRY structure based on the text passed
- * in. Returns NULL on failure */
- WOLFSSL_X509_NAME_ENTRY* wolfSSL_X509_NAME_ENTRY_create_by_txt(
- WOLFSSL_X509_NAME_ENTRY **neIn, const char *txt, int type,
- const unsigned char *data, int dataSz)
- {
- int nid = -1;
- WOLFSSL_X509_NAME_ENTRY* ne = NULL;
-
- WOLFSSL_ENTER("wolfSSL_X509_NAME_ENTRY_create_by_txt()");
-
- if (txt == NULL) {
- return NULL;
- }
-
- if (neIn != NULL) {
- ne = *neIn;
- }
-
- nid = wolfSSL_OBJ_txt2nid(txt);
- if (nid == NID_undef) {
- WOLFSSL_MSG("Unable to find text");
- ne = NULL;
- }
- else {
- if (ne == NULL) {
- ne = wolfSSL_X509_NAME_ENTRY_new();
- if (ne == NULL) {
- return NULL;
- }
- }
- ne->nid = nid;
- ne->value = wolfSSL_ASN1_STRING_type_new(type);
- if (ne->value != NULL) {
- wolfSSL_ASN1_STRING_set(ne->value, (const void*)data, dataSz);
- ne->set = 1;
- }
- }
-
- return ne;
- }
-
-
- WOLFSSL_X509_NAME_ENTRY* wolfSSL_X509_NAME_ENTRY_create_by_NID(
- WOLFSSL_X509_NAME_ENTRY** out, int nid, int type,
- const unsigned char* data, int dataSz)
- {
- WOLFSSL_X509_NAME_ENTRY* ne;
-
- WOLFSSL_ENTER("wolfSSL_X509_NAME_ENTRY_create_by_NID()");
-
- ne = wolfSSL_X509_NAME_ENTRY_new();
- if (ne == NULL) {
- return NULL;
- }
-
- ne->nid = nid;
- ne->value = wolfSSL_ASN1_STRING_type_new(type);
- wolfSSL_ASN1_STRING_set(ne->value, (const void*)data, dataSz);
- ne->set = 1;
-
- if (out != NULL) {
- *out = ne;
- }
-
- return ne;
- }
-
- static int RebuildFullNameAdd(DecodedName* dName, char* data)
- {
- int totalLen = 0;
- int i;
- char* fullName;
- int idx;
-
- if (dName->cnLen != 0)
- totalLen += dName->cnLen + 4;
- if (dName->snLen != 0)
- totalLen += dName->snLen + 4;
- if (dName->cLen != 0)
- totalLen += dName->cLen + 3;
- if (dName->lLen != 0)
- totalLen += dName->lLen + 3;
- if (dName->stLen != 0)
- totalLen += dName->stLen + 4;
- if (dName->oLen != 0)
- totalLen += dName->oLen + 3;
- if (dName->ouLen != 0)
- totalLen += dName->ouLen + 4;
- if (dName->emailLen != 0)
- totalLen += dName->emailLen + 14;
- if (dName->uidLen != 0)
- totalLen += dName->uidLen + 5;
- if (dName->serialLen != 0)
- totalLen += dName->serialLen + 14;
- if (dName->dcNum != 0) {
- for (i = 0; i < dName->dcNum; i++)
- totalLen += dName->dcLen[i] + 4;
- }
-
- fullName = (char*)XMALLOC(totalLen + 1, NULL, DYNAMIC_TYPE_X509);
- if (fullName == NULL)
- return MEMORY_E;
-
- idx = 0;
- dName->entryCount = 0;
- if (dName->cnLen != 0) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, WOLFSSL_COMMON_NAME, 4);
- idx += 4;
- if (dName->cnIdx == -1)
- XMEMCPY(fullName + idx, data, dName->cnLen);
- else {
- XMEMCPY(fullName + idx, dName->fullName + dName->cnIdx,
- dName->cnLen);
- }
- dName->cnIdx = idx;
- idx += dName->cnLen;
- }
- if (dName->snLen != 0) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, WOLFSSL_SUR_NAME, 4);
- idx += 4;
- if (dName->snIdx == -1)
- XMEMCPY(fullName + idx, data, dName->snLen);
- else {
- XMEMCPY(fullName + idx, dName->fullName + dName->snIdx,
- dName->snLen);
- }
- dName->snIdx = idx;
- idx += dName->snLen;
- }
- if (dName->cLen != 0) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, WOLFSSL_COUNTRY_NAME, 3);
- idx += 3;
- if (dName->cIdx == -1)
- XMEMCPY(fullName + idx, data, dName->cLen);
- else {
- XMEMCPY(fullName + idx, dName->fullName + dName->cIdx,
- dName->cLen);
- }
- dName->cIdx = idx;
- idx += dName->cLen;
- }
- if (dName->lLen != 0) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, WOLFSSL_LOCALITY_NAME, 3);
- idx += 3;
- if (dName->lIdx == -1)
- XMEMCPY(fullName + idx, data, dName->lLen);
- else {
- XMEMCPY(fullName + idx, dName->fullName + dName->lIdx,
- dName->lLen);
- }
- dName->lIdx = idx;
- idx += dName->lLen;
- }
- if (dName->stLen != 0) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, WOLFSSL_STATE_NAME, 4);
- idx += 4;
- if (dName->stIdx == -1)
- XMEMCPY(fullName + idx, data, dName->stLen);
- else {
- XMEMCPY(fullName + idx, dName->fullName + dName->stIdx,
- dName->stLen);
- }
- dName->stIdx = idx;
- idx += dName->stLen;
- }
- if (dName->oLen != 0) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, WOLFSSL_ORG_NAME, 3);
- idx += 3;
- if (dName->oIdx == -1)
- XMEMCPY(fullName + idx, data, dName->oLen);
- else {
- XMEMCPY(fullName + idx, dName->fullName + dName->oIdx,
- dName->oLen);
- }
- dName->oIdx = idx;
- idx += dName->oLen;
- }
- if (dName->ouLen != 0) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, WOLFSSL_ORGUNIT_NAME, 4);
- idx += 4;
- if (dName->ouIdx == -1)
- XMEMCPY(fullName + idx, data, dName->ouLen);
- else {
- XMEMCPY(fullName + idx, dName->fullName + dName->ouIdx,
- dName->ouLen);
- }
- dName->ouIdx = idx;
- idx += dName->ouLen;
- }
- if (dName->emailLen != 0) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, "/emailAddress=", 14);
- idx += 14;
- if (dName->emailIdx == -1)
- XMEMCPY(fullName + idx, data, dName->emailLen);
- else {
- XMEMCPY(fullName + idx, dName->fullName + dName->emailIdx,
- dName->emailLen);
- }
- dName->emailIdx = idx;
- idx += dName->emailLen;
- }
- if (dName->dcNum != 0) {
- for (i = 0; i < dName->dcNum; i++) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, WOLFSSL_DOMAIN_COMPONENT, 4);
- idx += 4;
- XMEMCPY(fullName + idx, dName->fullName + dName->dcIdx[i],
- dName->dcLen[i]);
- dName->dcIdx[i] = idx;
- idx += dName->dcLen[i];
- }
- }
- if (dName->uidLen != 0) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, "/UID=", 5);
- idx += 5;
- if (dName->uidIdx == -1)
- XMEMCPY(fullName + idx, data, dName->uidLen);
- else {
- XMEMCPY(fullName + idx, dName->fullName + dName->uidIdx,
- dName->uidLen);
- }
- dName->uidIdx = idx;
- idx += dName->uidLen;
- }
- if (dName->serialLen != 0) {
- dName->entryCount++;
- XMEMCPY(fullName + idx, WOLFSSL_SERIAL_NUMBER, 14);
- idx += 14;
- if (dName->serialIdx == -1)
- XMEMCPY(fullName + idx, data, dName->serialLen);
- else {
- XMEMCPY(fullName + idx, dName->fullName + dName->serialIdx,
- dName->serialLen);
- }
- dName->serialIdx = idx;
- idx += dName->serialLen;
- }
-
- if (dName->fullName != NULL)
- XFREE(dName->fullName, NULL, DYNAMIC_TYPE_X509);
- dName->fullName = fullName;
- dName->fullNameLen = idx + 1;
-
- return 0;
- }
-
- /* Copies entry into name. With it being copied freeing entry becomes the
- * callers responsibility.
- * returns 1 for success and 0 for error */
- int wolfSSL_X509_NAME_add_entry(WOLFSSL_X509_NAME* name,
- WOLFSSL_X509_NAME_ENTRY* entry, int idx, int set)
- {
- int i;
- int fullName = 1;
-
- WOLFSSL_ENTER("wolfSSL_X509_NAME_add_entry()");
-
- switch (entry->nid) {
- case ASN_COMMON_NAME:
- name->fullName.cnIdx = -1;
- name->fullName.cnLen = entry->value->length;
- name->fullName.cnNid = entry->nid;
- break;
- case ASN_SUR_NAME:
- name->fullName.snIdx = -1;
- name->fullName.snLen = entry->value->length;
- name->fullName.snNid = entry->nid;
- break;
- case ASN_SERIAL_NUMBER:
- name->fullName.serialIdx = -1;
- name->fullName.serialLen = entry->value->length;
- name->fullName.serialNid = entry->nid;
- break;
- case ASN_COUNTRY_NAME:
- name->fullName.cIdx = -1;
- name->fullName.cLen = entry->value->length;
- name->fullName.cNid = entry->nid;
- break;
- case ASN_LOCALITY_NAME:
- name->fullName.lIdx = -1;
- name->fullName.lLen = entry->value->length;
- name->fullName.lNid = entry->nid;
- break;
- case ASN_STATE_NAME:
- name->fullName.stIdx = -1;
- name->fullName.stLen = entry->value->length;
- name->fullName.stNid = entry->nid;
- break;
- case ASN_ORG_NAME:
- name->fullName.oIdx = -1;
- name->fullName.oLen = entry->value->length;
- name->fullName.oNid = entry->nid;
- break;
- case ASN_ORGUNIT_NAME:
- name->fullName.ouIdx = -1;
- name->fullName.ouLen = entry->value->length;
- name->fullName.ouNid = entry->nid;
- break;
- case NID_emailAddress:
- name->fullName.emailIdx = -1;
- name->fullName.emailLen = entry->value->length;
- name->fullName.emailNid = entry->nid;
- break;
- case ASN_USER_ID:
- name->fullName.uidIdx = -1;
- name->fullName.uidLen = entry->value->length;
- name->fullName.uidNid = entry->nid;
- break;
- case ASN_DOMAIN_COMPONENT:
- name->fullName.dcIdx[0] = -1;
- name->fullName.dcLen[0] = entry->value->length;
- break;
- default:
- fullName = 0;
- break;
- }
-
- if (fullName) {
- int nid = entry->nid;
-
- if (nid == NID_emailAddress) {
- nid = (int)ASN_EMAIL_NAME;
- }
-
- if (idx >= DN_NAMES_MAX + DOMAIN_COMPONENT_MAX) {
- return WOLFSSL_FAILURE;
- }
-
- if (idx >= 0) {
- name->fullName.loc[idx] = nid;
- if (idx == name->fullName.locSz) {
- name->fullName.locSz += 1;
- }
- }
-
- /* place at end */
- if (idx < 0 && name->fullName.locSz + 1
- < DN_NAMES_MAX + DOMAIN_COMPONENT_MAX) {
- name->fullName.loc[name->fullName.locSz] = nid;
- name->fullName.locSz += 1;
- }
-
- if (RebuildFullNameAdd(&name->fullName, entry->value->data) != 0)
- return WOLFSSL_FAILURE;
- }
- else {
- for (i = 0; i < MAX_NAME_ENTRIES; i++) {
- if (name->extra[i].set != 1) { /* not set so overwritten */
- WOLFSSL_X509_NAME_ENTRY* current = &(name->extra[i]);
- WOLFSSL_ASN1_STRING* str;
-
- WOLFSSL_MSG("Found place for name entry");
-
- XMEMCPY(current, entry, sizeof(WOLFSSL_X509_NAME_ENTRY));
- str = entry->value;
- XMEMCPY(&(current->data), str, sizeof(WOLFSSL_ASN1_STRING));
- current->value = &(current->data);
- current->data.data = (char*)XMALLOC(str->length,
- name->x509->heap, DYNAMIC_TYPE_OPENSSL);
-
- if (current->data.data == NULL) {
- return SSL_FAILURE;
- }
- XMEMCPY(current->data.data, str->data, str->length);
-
- /* make sure is null terminated */
- current->data.data[str->length - 1] = '\0';
-
- current->set = 1; /* make sure now listed as set */
- break;
- }
- }
-
- if (i == MAX_NAME_ENTRIES) {
- WOLFSSL_MSG("No spot found for name entry");
- return SSL_FAILURE;
- }
- }
-
- (void)idx;
- (void)set;
- return SSL_SUCCESS;
- }
-
- int wolfSSL_X509_NAME_add_entry_by_txt(WOLFSSL_X509_NAME *name,
- const char *field, int type,
- const unsigned char *bytes, int len,
- int loc, int set)
- {
- int ret = WOLFSSL_FAILURE;
- int nid;
- WOLFSSL_X509_NAME_ENTRY* entry;
-
- (void)type;
- WOLFSSL_ENTER("wolfSSL_X509_NAME_add_entry_by_txt");
-
- if (name == NULL || field == NULL)
- return WOLFSSL_FAILURE;
-
- if ((nid = wolfSSL_OBJ_txt2nid(field)) == NID_undef)
- return WOLFSSL_FAILURE;
-
- entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
- nid, type, (unsigned char*)bytes, len);
- if (entry == NULL)
- return WOLFSSL_FAILURE;
-
- ret = wolfSSL_X509_NAME_add_entry(name, entry, loc, set);
- wolfSSL_X509_NAME_ENTRY_free(entry);
-
- return ret;
- }
-
- int wolfSSL_X509_NAME_add_entry_by_NID(WOLFSSL_X509_NAME *name, int nid,
- int type, const unsigned char *bytes,
- int len, int loc, int set)
- {
- int ret;
- WOLFSSL_X509_NAME_ENTRY* entry;
- entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL, nid, type, bytes, len);
- if (entry == NULL)
- return WOLFSSL_FAILURE;
- ret = wolfSSL_X509_NAME_add_entry(name, entry, loc, set);
- wolfSSL_X509_NAME_ENTRY_free(entry);
- return ret;
- }
- #endif /* !NO_CERTS */
-
-
- /* NID variables are dependent on compatibility header files currently
- *
- * returns a pointer to a new WOLFSSL_ASN1_OBJECT struct on success and NULL
- * on fail
- */
-
- WOLFSSL_ASN1_OBJECT* wolfSSL_OBJ_nid2obj(int id)
- {
- return wolfSSL_OBJ_nid2obj_ex(id, NULL);
- }
-
-
- WOLFSSL_LOCAL WOLFSSL_ASN1_OBJECT* wolfSSL_OBJ_nid2obj_ex(int id,
- WOLFSSL_ASN1_OBJECT* arg_obj)
- {
- word32 oidSz = 0;
- const byte* oid;
- word32 type = 0;
- WOLFSSL_ASN1_OBJECT* obj = arg_obj;
- byte objBuf[MAX_OID_SZ + MAX_LENGTH_SZ + 1]; /* +1 for object tag */
- word32 objSz = 0;
- const char* sName = NULL;
- int i;
-
- WOLFSSL_ENTER("wolfSSL_OBJ_nid2obj()");
-
- for (i = 0; i < (int)WOLFSSL_OBJECT_INFO_SZ; i++) {
- if (wolfssl_object_info[i].nid == id) {
- id = wolfssl_object_info[i].id;
- sName = wolfssl_object_info[i].sName;
- type = wolfssl_object_info[i].type;
- break;
- }
- }
- if (i == (int)WOLFSSL_OBJECT_INFO_SZ) {
- WOLFSSL_MSG("NID not in table");
- #ifdef WOLFSSL_QT
- sName = NULL;
- type = id;
- #else
- return NULL;
- #endif
- }
-
- #ifdef HAVE_ECC
- if (type == 0 && wc_ecc_get_oid(id, &oid, &oidSz) > 0) {
- type = oidCurveType;
- }
- #endif /* HAVE_ECC */
-
- if (sName != NULL) {
- if (XSTRLEN(sName) > WOLFSSL_MAX_SNAME - 1) {
- WOLFSSL_MSG("Attempted short name is too large");
- return NULL;
- }
- }
-
- oid = OidFromId(id, type, &oidSz);
-
- /* set object ID to buffer */
- if (obj == NULL){
- obj = wolfSSL_ASN1_OBJECT_new();
- if (obj == NULL) {
- WOLFSSL_MSG("Issue creating WOLFSSL_ASN1_OBJECT struct");
- return NULL;
- }
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC;
- } else {
- obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC;
- }
- obj->type = id;
- obj->grp = type;
-
- obj->sName[0] = '\0';
- if (sName != NULL) {
- XMEMCPY(obj->sName, (char*)sName, XSTRLEN((char*)sName));
- }
-
- objBuf[0] = ASN_OBJECT_ID; objSz++;
- objSz += SetLength(oidSz, objBuf + 1);
- XMEMCPY(objBuf + objSz, oid, oidSz);
- objSz += oidSz;
- obj->objSz = objSz;
- if(((obj->dynamic & WOLFSSL_ASN1_DYNAMIC_DATA) != 0) ||
- (obj->obj == NULL)) {
- obj->obj = (byte*)XREALLOC((byte*)obj->obj, obj->objSz, NULL,
- DYNAMIC_TYPE_ASN1);
- if (obj->obj == NULL) {
- wolfSSL_ASN1_OBJECT_free(obj);
- return NULL;
- }
- obj->dynamic |= WOLFSSL_ASN1_DYNAMIC_DATA ;
- } else {
- obj->dynamic &= ~WOLFSSL_ASN1_DYNAMIC_DATA ;
- }
- XMEMCPY((byte*)obj->obj, objBuf, obj->objSz);
-
- (void)type;
-
- return obj;
- }
-
- static const char* oid_translate_num_to_str(const char* oid)
- {
- const struct oid_dict {
- const char* num;
- const char* desc;
- } oid_dict[] = {
- { "2.5.29.37.0", "Any Extended Key Usage" },
- { "1.3.6.1.5.5.7.3.1", "TLS Web Server Authentication" },
- { "1.3.6.1.5.5.7.3.2", "TLS Web Client Authentication" },
- { "1.3.6.1.5.5.7.3.3", "Code Signing" },
- { "1.3.6.1.5.5.7.3.4", "E-mail Protection" },
- { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
- { "1.3.6.1.5.5.7.3.9", "OCSP Signing" },
- { NULL, NULL }
- };
- const struct oid_dict* idx;
-
- for (idx = oid_dict; idx->num != NULL; idx++) {
- if (!XSTRNCMP(oid, idx->num, XSTRLEN(idx->num))) {
- return idx->desc;
- }
- }
- return NULL;
- }
-
- /* if no_name is one than use numerical form otherwise can be short name.
- *
- * returns the buffer size on success
- */
- int wolfSSL_OBJ_obj2txt(char *buf, int bufLen, WOLFSSL_ASN1_OBJECT *a, int no_name)
- {
- int bufSz;
- const char* desc;
-
- WOLFSSL_ENTER("wolfSSL_OBJ_obj2txt()");
-
- if (buf == NULL || bufLen <= 1 || a == NULL) {
- WOLFSSL_MSG("Bad input argument");
- return WOLFSSL_FAILURE;
- }
-
- if (no_name == 1) {
- int length;
- word32 idx = 0;
- byte tag;
-
- if (GetASNTag(a->obj, &idx, &tag, a->objSz) != 0) {
- return WOLFSSL_FAILURE;
- }
-
- if (tag != ASN_OBJECT_ID) {
- WOLFSSL_MSG("Bad ASN1 Object");
- return WOLFSSL_FAILURE;
- }
-
- if (GetLength((const byte*)a->obj, &idx, &length,
- a->objSz) < 0 || length < 0) {
- return ASN_PARSE_E;
- }
-
- if (bufLen < MAX_OID_STRING_SZ) {
- bufSz = bufLen - 1;
- }
- else {
- bufSz = MAX_OID_STRING_SZ;
- }
-
- if ((bufSz = DecodePolicyOID(buf, (word32)bufSz, a->obj + idx,
- (word32)length)) <= 0) {
- WOLFSSL_MSG("Error decoding OID");
- return WOLFSSL_FAILURE;
- }
-
- }
- else { /* return short name */
- if (XSTRLEN(a->sName) + 1 < (word32)bufLen - 1) {
- bufSz = (int)XSTRLEN(a->sName);
- }
- else {
- bufSz = bufLen - 1;
- }
- if (bufSz) {
- XMEMCPY(buf, a->sName, bufSz);
- }
- else if (wolfSSL_OBJ_obj2txt(buf, bufLen, a, 1)) {
- if ((desc = oid_translate_num_to_str(buf))) {
- bufSz = (int)XSTRLEN(desc);
- XMEMCPY(buf, desc, min(bufSz, bufLen));
- }
- }
- else if (a->type == GEN_DNS || a->type == GEN_EMAIL || a->type == GEN_URI) {
- bufSz = (int)XSTRLEN((const char*)a->obj);
- XMEMCPY(buf, a->obj, min(bufSz, bufLen));
- }
- }
-
- buf[bufSz] = '\0';
- #ifdef WOLFSSL_QT
- /* For unknown extension types, QT expects the short name to be the
- text representation of the oid */
- if (XSTRLEN(a->sName) == 0) {
- XMEMCPY(a->sName, buf, bufSz);
- }
- #endif
- return bufSz;
- }
-#endif /* OPENSSL_EXTRA */
-
-#if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
- /* DN_Tags to strings */
- static const struct DN_Tag_Strings {
- enum DN_Tags tag;
- const char* str;
- } dn_tag_strings[] = {
- { ASN_COMMON_NAME, "CN" },
- { ASN_SUR_NAME, "SN" },
- { ASN_SERIAL_NUMBER, "serialNumber" },
- { ASN_COUNTRY_NAME, "C" },
- { ASN_LOCALITY_NAME, "L" },
- { ASN_STATE_NAME, "ST" },
- { ASN_ORG_NAME, "O"},
- { ASN_ORGUNIT_NAME, "OU"},
- { ASN_BUS_CAT, "businessCategory"},
- { ASN_EMAIL_NAME, "emailAddress"},
- { ASN_USER_ID, "UID"},
- { ASN_DOMAIN_COMPONENT, "DC"},
- { ASN_DN_NULL, NULL }
- };
-
- int wolfSSL_X509_NAME_get_index_by_OBJ(WOLFSSL_X509_NAME *name,
- const WOLFSSL_ASN1_OBJECT *obj,
- int idx) {
- const struct DN_Tag_Strings* dn;
- enum DN_Tags tag = ASN_DN_NULL;
-
- if (!name || idx >= name->fullName.locSz ||
- !obj || !obj->obj) {
- return -1;
- }
-
- if (idx < 0) {
- idx = 0;
- }
- for (dn = dn_tag_strings; dn->str != NULL; dn++) {
- /* Find the DN_Tags number for the name */
- if (XSTRNCMP((const char*) obj->sName, dn->str, obj->objSz - 1) == 0) {
- tag = dn->tag;
- break;
- }
- }
- if (!tag) {
- /* Unable to identify desired name */
- return -1;
- }
- for (idx++; idx < name->fullName.locSz; idx++) {
- /* Find index of desired name */
- if ((enum DN_Tags)name->fullName.loc[idx] == tag) {
- return idx;
- }
- }
- return -1;
- }
-#endif
-
-#if defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || \
- defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL) || \
- defined(WOLFSSL_NGINX) || defined(HAVE_POCO_LIB) || \
- defined(WOLFSSL_HAPROXY)
-
-#ifndef NO_SHA
- /* One shot SHA1 hash of message.
- *
- * d message to hash
- * n size of d buffer
- * md buffer to hold digest. Should be SHA_DIGEST_SIZE.
- *
- * Note: if md is null then a static buffer of SHA_DIGEST_SIZE is used.
- * When the static buffer is used this function is not thread safe.
- *
- * Returns a pointer to the message digest on success and NULL on failure.
- */
- unsigned char *wolfSSL_SHA1(const unsigned char *d, size_t n,
- unsigned char *md)
- {
- static byte dig[WC_SHA_DIGEST_SIZE];
- wc_Sha sha;
-
- WOLFSSL_ENTER("wolfSSL_SHA1");
-
- if (wc_InitSha_ex(&sha, NULL, 0) != 0) {
- WOLFSSL_MSG("SHA1 Init failed");
- return NULL;
- }
-
- if (wc_ShaUpdate(&sha, (const byte*)d, (word32)n) != 0) {
- WOLFSSL_MSG("SHA1 Update failed");
- return NULL;
- }
-
- if (wc_ShaFinal(&sha, dig) != 0) {
- WOLFSSL_MSG("SHA1 Final failed");
- return NULL;
- }
-
- wc_ShaFree(&sha);
-
- if (md != NULL) {
- XMEMCPY(md, dig, WC_SHA_DIGEST_SIZE);
- return md;
- }
- else {
- return (unsigned char*)dig;
- }
- }
-#endif /* ! NO_SHA */
-
-#ifndef NO_SHA256
- /* One shot SHA256 hash of message.
- *
- * d message to hash
- * n size of d buffer
- * md buffer to hold digest. Should be WC_SHA256_DIGEST_SIZE.
- *
- * Note: if md is null then a static buffer of WC_SHA256_DIGEST_SIZE is used.
- * When the static buffer is used this function is not thread safe.
- *
- * Returns a pointer to the message digest on success and NULL on failure.
- */
- unsigned char *wolfSSL_SHA256(const unsigned char *d, size_t n,
- unsigned char *md)
- {
- static byte dig[WC_SHA256_DIGEST_SIZE];
- wc_Sha256 sha;
-
- WOLFSSL_ENTER("wolfSSL_SHA256");
-
- if (wc_InitSha256_ex(&sha, NULL, 0) != 0) {
- WOLFSSL_MSG("SHA256 Init failed");
- return NULL;
- }
-
- if (wc_Sha256Update(&sha, (const byte*)d, (word32)n) != 0) {
- WOLFSSL_MSG("SHA256 Update failed");
- return NULL;
- }
-
- if (wc_Sha256Final(&sha, dig) != 0) {
- WOLFSSL_MSG("SHA256 Final failed");
- return NULL;
- }
-
- wc_Sha256Free(&sha);
-
- if (md != NULL) {
- XMEMCPY(md, dig, WC_SHA256_DIGEST_SIZE);
- return md;
- }
- else {
- return (unsigned char*)dig;
- }
- }
-#endif /* ! NO_SHA256 */
-
-#ifdef WOLFSSL_SHA384
- /* One shot SHA384 hash of message.
- *
- * d message to hash
- * n size of d buffer
- * md buffer to hold digest. Should be WC_SHA256_DIGEST_SIZE.
- *
- * Note: if md is null then a static buffer of WC_SHA256_DIGEST_SIZE is used.
- * When the static buffer is used this function is not thread safe.
- *
- * Returns a pointer to the message digest on success and NULL on failure.
- */
- unsigned char *wolfSSL_SHA384(const unsigned char *d, size_t n,
- unsigned char *md)
- {
- static byte dig[WC_SHA384_DIGEST_SIZE];
- wc_Sha384 sha;
-
- WOLFSSL_ENTER("wolfSSL_SHA384");
-
- if (wc_InitSha384_ex(&sha, NULL, 0) != 0) {
- WOLFSSL_MSG("SHA384 Init failed");
- return NULL;
- }
-
- if (wc_Sha384Update(&sha, (const byte*)d, (word32)n) != 0) {
- WOLFSSL_MSG("SHA384 Update failed");
- return NULL;
- }
-
- if (wc_Sha384Final(&sha, dig) != 0) {
- WOLFSSL_MSG("SHA384 Final failed");
- return NULL;
- }
-
- wc_Sha384Free(&sha);
-
- if (md != NULL) {
- XMEMCPY(md, dig, WC_SHA384_DIGEST_SIZE);
- return md;
- }
- else {
- return (unsigned char*)dig;
- }
- }
-#endif /* WOLFSSL_SHA384 */
-
-
-#if defined(WOLFSSL_SHA512)
- /* One shot SHA512 hash of message.
- *
- * d message to hash
- * n size of d buffer
- * md buffer to hold digest. Should be WC_SHA256_DIGEST_SIZE.
- *
- * Note: if md is null then a static buffer of WC_SHA256_DIGEST_SIZE is used.
- * When the static buffer is used this function is not thread safe.
- *
- * Returns a pointer to the message digest on success and NULL on failure.
- */
- unsigned char *wolfSSL_SHA512(const unsigned char *d, size_t n,
- unsigned char *md)
- {
- static byte dig[WC_SHA512_DIGEST_SIZE];
- wc_Sha512 sha;
-
- WOLFSSL_ENTER("wolfSSL_SHA512");
-
- if (wc_InitSha512_ex(&sha, NULL, 0) != 0) {
- WOLFSSL_MSG("SHA512 Init failed");
- return NULL;
- }
-
- if (wc_Sha512Update(&sha, (const byte*)d, (word32)n) != 0) {
- WOLFSSL_MSG("SHA512 Update failed");
- return NULL;
- }
-
- if (wc_Sha512Final(&sha, dig) != 0) {
- WOLFSSL_MSG("SHA512 Final failed");
- return NULL;
- }
-
- wc_Sha512Free(&sha);
-
- if (md != NULL) {
- XMEMCPY(md, dig, WC_SHA512_DIGEST_SIZE);
- return md;
- }
- else {
- return (unsigned char*)dig;
- }
- }
-#endif /* defined(WOLFSSL_SHA512) */
-#endif /* OPENSSL_EXTRA */
-
-#ifndef WOLFCRYPT_ONLY
-#if defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY) || \
- defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL) || \
- defined(WOLFSSL_NGINX) || defined(HAVE_POCO_LIB) || \
- defined(WOLFSSL_HAPROXY)
-
- char wolfSSL_CTX_use_certificate(WOLFSSL_CTX *ctx, WOLFSSL_X509 *x)
- {
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_CTX_use_certificate");
-
- FreeDer(&ctx->certificate); /* Make sure previous is free'd */
- ret = AllocDer(&ctx->certificate, x->derCert->length, CERT_TYPE,
- ctx->heap);
- if (ret != 0)
- return 0;
-
- XMEMCPY(ctx->certificate->buffer, x->derCert->buffer,
- x->derCert->length);
-#ifdef KEEP_OUR_CERT
- if (ctx->ourCert != NULL && ctx->ownOurCert) {
- FreeX509(ctx->ourCert);
- XFREE(ctx->ourCert, ctx->heap, DYNAMIC_TYPE_X509);
- }
- #ifndef WOLFSSL_X509_STORE_CERTS
- ctx->ourCert = x;
- #else
- ctx->ourCert = wolfSSL_X509_d2i(NULL, x->derCert->buffer,x->derCert->length);
- if(ctx->ourCert == NULL){
- return 0;
- }
- #endif
-
- ctx->ownOurCert = 0;
-#endif
-
- /* Update the available options with public keys. */
- switch (x->pubKeyOID) {
- case RSAk:
- ctx->haveRSA = 1;
- break;
- #ifdef HAVE_ED25519
- case ED25519k:
- #endif
- #ifdef HAVE_ED448
- case ED448k:
- #endif
- case ECDSAk:
- ctx->haveECC = 1;
- #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
- ctx->pkCurveOID = x->pkCurveOID;
- #endif
- break;
- }
-
- return WOLFSSL_SUCCESS;
- }
-
- int wolfSSL_CTX_add1_chain_cert(WOLFSSL_CTX* ctx, WOLFSSL_X509* x509)
- {
- int ret;
- if (ctx == NULL || x509 == NULL || x509->derCert == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- ret = wolfSSL_CTX_load_verify_buffer(ctx, x509->derCert->buffer,
- x509->derCert->length, WOLFSSL_FILETYPE_ASN1);
-
- return (ret == 0) ? WOLFSSL_SUCCESS : WOLFSSL_FAILURE;
- }
-
- int wolfSSL_BIO_read_filename(WOLFSSL_BIO *b, const char *name) {
- #ifndef NO_FILESYSTEM
- XFILE fp;
-
- WOLFSSL_ENTER("wolfSSL_BIO_new_file");
-
- if ((wolfSSL_BIO_get_fp(b, &fp) == WOLFSSL_SUCCESS) && (fp != XBADFILE))
- {
- XFCLOSE(fp);
- }
-
- fp = XFOPEN(name, "r");
- if (fp == XBADFILE)
- return WOLFSSL_BAD_FILE;
-
- if (wolfSSL_BIO_set_fp(b, fp, BIO_CLOSE) != WOLFSSL_SUCCESS) {
- XFCLOSE(fp);
- return WOLFSSL_BAD_FILE;
- }
-
- /* file is closed when bio is free'd */
- return WOLFSSL_SUCCESS;
- #else
- (void)name;
- (void)b;
- return WOLFSSL_NOT_IMPLEMENTED;
- #endif
- }
-
- /* Return the corresponding short name for the nid .
- * or NULL if short name can't be found.
- */
- const char * wolfSSL_OBJ_nid2sn(int n) {
- const WOLFSSL_ObjectInfo *obj_info = wolfssl_object_info;
- size_t i;
- WOLFSSL_ENTER("wolfSSL_OBJ_nid2sn");
- for (i = 0; i < WOLFSSL_OBJECT_INFO_SZ; i++, obj_info++) {
- if (obj_info->nid == n) {
- return obj_info->sName;
- }
- }
- WOLFSSL_MSG("SN not found");
- return NULL;
- }
-
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- int wolfSSL_OBJ_sn2nid(const char *sn) {
- WOLFSSL_ENTER("wolfSSL_OBJ_sn2nid");
- if (sn == NULL)
- return NID_undef;
- return wc_OBJ_sn2nid(sn);
- }
-#endif
-
-
- /* Gets the NID value that corresponds with the ASN1 object.
- *
- * o ASN1 object to get NID of
- *
- * Return NID on success and a negative value on failure
- */
- int wolfSSL_OBJ_obj2nid(const WOLFSSL_ASN1_OBJECT *o)
- {
- word32 oid = 0;
- word32 idx = 0;
-
- WOLFSSL_ENTER("wolfSSL_OBJ_obj2nid");
-
- if (o == NULL) {
- return -1;
- }
-
- #ifdef WOLFSSL_QT
- if (o->grp == oidCertExtType) {
- /* If nid is an unknown extension, return NID_undef */
- if (wolfSSL_OBJ_nid2sn(o->nid) == NULL)
- return NID_undef;
- }
- #endif
-
- if (o->nid > 0)
- return o->nid;
- if (GetObjectId(o->obj, &idx, &oid, o->grp, o->objSz) < 0) {
- WOLFSSL_MSG("Issue getting OID of object");
- return -1;
- }
-
- return oid2nid(oid, o->grp);
- }
-
- /* Returns the long name that corresponds with an ASN1_OBJECT nid value.
- * n : NID value of ASN1_OBJECT to search */
- const char* wolfSSL_OBJ_nid2ln(int n)
- {
- const WOLFSSL_ObjectInfo *obj_info = wolfssl_object_info;
- size_t i;
- WOLFSSL_ENTER("wolfSSL_OBJ_nid2ln");
- for (i = 0; i < WOLFSSL_OBJECT_INFO_SZ; i++, obj_info++) {
- if (obj_info->nid == n) {
- return obj_info->lName;
- }
- }
- WOLFSSL_MSG("NID not found in table");
- return NULL;
- }
-
- /* Return the corresponding NID for the long name
- * or NID_undef if NID can't be found.
- */
- int wolfSSL_OBJ_ln2nid(const char *ln)
- {
- const WOLFSSL_ObjectInfo *obj_info = wolfssl_object_info;
- size_t i, lnlen;
- WOLFSSL_ENTER("wolfSSL_OBJ_ln2nid");
- if (ln && (lnlen = XSTRLEN(ln)) > 0) {
- /* Accept input like "/commonName=" */
- if (ln[0] == '/') {
- ln++;
- lnlen--;
- }
- if (lnlen) {
- if (ln[lnlen-1] == '=') {
- lnlen--;
- }
- for (i = 0; i < WOLFSSL_OBJECT_INFO_SZ; i++, obj_info++) {
- if (lnlen == XSTRLEN(obj_info->lName) &&
- XSTRNCMP(ln, obj_info->lName, lnlen) == 0) {
- return obj_info->nid;
- }
- }
- }
- }
- return NID_undef;
- }
-
- /* compares two objects, return 0 if equal */
- int wolfSSL_OBJ_cmp(const WOLFSSL_ASN1_OBJECT* a,
- const WOLFSSL_ASN1_OBJECT* b)
- {
- WOLFSSL_ENTER("wolfSSL_OBJ_cmp");
-
- if (a != NULL && b != NULL &&
- a->obj != NULL && b->obj != NULL &&
- a->objSz == b->objSz) {
- return XMEMCMP(a->obj, b->obj, a->objSz);
- }
-
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* Gets the NID value that is related to the OID string passed in. Example
- * string would be "2.5.29.14" for subject key ID.
- *
- * returns NID value on success and NID_undef on error
- */
- int wolfSSL_OBJ_txt2nid(const char* s)
- {
- unsigned int i;
- #ifdef WOLFSSL_CERT_EXT
- int ret;
- unsigned int sum = 0;
- unsigned int outSz = MAX_OID_SZ;
- unsigned char out[MAX_OID_SZ];
- #endif
-
- WOLFSSL_ENTER("OBJ_txt2nid");
-
- if (s == NULL) {
- return NID_undef;
- }
-
- #ifdef WOLFSSL_CERT_EXT
- ret = EncodePolicyOID(out, &outSz, s, NULL);
- if (ret == 0) {
- /* sum OID */
- for (i = 0; i < outSz; i++) {
- sum += out[i];
- }
- }
- #endif /* WOLFSSL_CERT_EXT */
-
- /* get the group that the OID's sum is in
- * @TODO possible conflict with multiples */
- for (i = 0; i < WOLFSSL_OBJECT_INFO_SZ; i++) {
- int len;
- #ifdef WOLFSSL_CERT_EXT
- if (ret == 0) {
- if (wolfssl_object_info[i].id == (int)sum) {
- return wolfssl_object_info[i].nid;
- }
- }
- #endif
-
- /* try as a short name */
- len = (int)XSTRLEN(s);
- if (XSTRNCMP(wolfssl_object_info[i].sName, s, len) == 0) {
- return wolfssl_object_info[i].nid;
- }
-
- /* try as a long name */
- if (XSTRNCMP(wolfssl_object_info[i].lName, s, len) == 0) {
- return wolfssl_object_info[i].nid;
- }
- }
-
- return NID_undef;
- }
-
- /* Creates new ASN1_OBJECT from short name, long name, or text
- * representation of oid. If no_name is 0, then short name, long name, and
- * numerical value of oid are interpreted. If no_name is 1, then only the
- * numerical value of the oid is interpreted.
- *
- * Returns pointer to ASN1_OBJECT on success, or NULL on error.
- */
-#if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
- WOLFSSL_ASN1_OBJECT* wolfSSL_OBJ_txt2obj(const char* s, int no_name)
- {
- int len, i, ret;
- int nid = NID_undef;
- unsigned int outSz = MAX_OID_SZ;
- unsigned char out[MAX_OID_SZ];
- unsigned int sum = 0;
-
- WOLFSSL_ENTER("wolfSSL_OBJ_txt2obj");
-
- if (s == NULL)
- return NULL;
-
- /* If s is numerical value, try to sum oid */
- ret = EncodePolicyOID(out, &outSz, s, NULL);
- if (ret == 0) {
- for (i = 0; i < (int)outSz; i++) {
- sum += out[i];
- }
- }
-
- len = (int)XSTRLEN(s);
-
- /* TODO: update short names in wolfssl_object_info and check OID sums
- are correct */
- for (i = 0; i < (int)WOLFSSL_OBJECT_INFO_SZ; i++) {
- /* Short name, long name, and numerical value are interpreted */
- if (no_name == 0 && ((XSTRNCMP(s, wolfssl_object_info[i].sName, len) == 0) ||
- (XSTRNCMP(s, wolfssl_object_info[i].lName, len) == 0) ||
- (wolfssl_object_info[i].id == (int)sum)))
- nid = wolfssl_object_info[i].nid;
- /* Only numerical value is interpreted */
- else if (no_name == 1 && wolfssl_object_info[i].id == (int)sum)
- nid = wolfssl_object_info[i].nid;
- }
-
- if (nid != NID_undef)
- return wolfSSL_OBJ_nid2obj(nid);
-
- return NULL;
- }
-#endif
-
- /* compatibility function. Its intended use is to remove OID's from an
- * internal table that have been added with OBJ_create. wolfSSL manages its
- * own internal OID values and does not currently support OBJ_create. */
- void wolfSSL_OBJ_cleanup(void)
- {
- WOLFSSL_ENTER("wolfSSL_OBJ_cleanup()");
- }
-
- #ifndef NO_WOLFSSL_STUB
- int wolfSSL_OBJ_create(const char *oid, const char *sn, const char *ln)
- {
- (void)oid;
- (void)sn;
- (void)ln;
- WOLFSSL_STUB("wolfSSL_OBJ_create");
- return WOLFSSL_FAILURE;
- }
- #endif
-
- void wolfSSL_set_verify_depth(WOLFSSL *ssl, int depth)
- {
- #if !defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
- (void)ssl;
- (void)depth;
- WOLFSSL_STUB("wolfSSL_set_verify_depth");
- #else
- WOLFSSL_ENTER("wolfSSL_set_verify_depth");
- ssl->options.verifyDepth = (byte)depth;
- #endif
- }
-
-
- WOLFSSL_ASN1_OBJECT * wolfSSL_X509_NAME_ENTRY_get_object(WOLFSSL_X509_NAME_ENTRY *ne) {
- WOLFSSL_ENTER("wolfSSL_X509_NAME_ENTRY_get_object");
- if (ne == NULL) return NULL;
- if (wolfSSL_OBJ_nid2obj_ex(ne->nid, &ne->object) != NULL) {
- ne->object.nid = ne->nid;
- return &ne->object;
- }
- return NULL;
- }
-
-
- /* looks up the DN given the location "loc". "loc" is the number indicating
- * the order that the DN was parsed as, 0 is first DN parsed.
- *
- * returns the setup WOLFSSL_X509_NAME pointer on success and NULL on fail
- */
- static WOLFSSL_X509_NAME* wolfSSL_nameByLoc(WOLFSSL_X509_NAME *name, int loc)
- {
- char* pt = NULL;
- int sz = 0;
-
- switch (name->fullName.loc[loc])
- {
- case ASN_COMMON_NAME:
- sz = name->fullName.cnLen;
- pt = &name->fullName.fullName[name->fullName.cnIdx];
- name->cnEntry.nid = name->fullName.cnNid;
- break;
- case ASN_COUNTRY_NAME:
- sz = name->fullName.cLen;
- pt = &name->fullName.fullName[name->fullName.cIdx];
- name->cnEntry.nid = name->fullName.cNid;
- break;
- case ASN_LOCALITY_NAME:
- sz = name->fullName.lLen;
- pt = &name->fullName.fullName[name->fullName.lIdx];
- name->cnEntry.nid = name->fullName.lNid;
- break;
- case ASN_STATE_NAME:
- sz = name->fullName.stLen;
- pt = &name->fullName.fullName[name->fullName.stIdx];
- name->cnEntry.nid = name->fullName.stNid;
- break;
- case ASN_ORG_NAME:
- sz = name->fullName.oLen;
- pt = &name->fullName.fullName[name->fullName.oIdx];
- name->cnEntry.nid = name->fullName.oNid;
- break;
- case ASN_ORGUNIT_NAME:
- sz = name->fullName.ouLen;
- pt = &name->fullName.fullName[name->fullName.ouIdx];
- name->cnEntry.nid = name->fullName.ouNid;
- break;
- case ASN_EMAIL_NAME:
- sz = name->fullName.emailLen;
- pt = &name->fullName.fullName[name->fullName.emailIdx];
- name->cnEntry.nid = name->fullName.emailNid;
- break;
- case ASN_SUR_NAME:
- sz = name->fullName.snLen;
- pt = &name->fullName.fullName[name->fullName.snIdx];
- name->cnEntry.nid = name->fullName.snNid;
- break;
- case ASN_USER_ID:
- sz = name->fullName.uidLen;
- pt = &name->fullName.fullName[name->fullName.uidIdx];
- name->cnEntry.nid = name->fullName.uidNid;
- break;
- case ASN_SERIAL_NUMBER:
- sz = name->fullName.serialLen;
- pt = &name->fullName.fullName[name->fullName.serialIdx];
- name->cnEntry.nid = name->fullName.serialNid;
- break;
-#ifdef WOLFSSL_CERT_EXT
- case ASN_BUS_CAT:
- sz = name->fullName.bcLen;
- pt = &name->fullName.fullName[name->fullName.bcIdx];
- break;
-#endif
-
- case ASN_DOMAIN_COMPONENT:
- /* get index of DC i.e. first or second or ... case */
- {
- int idx = 0, i;
- for (i = 0; i < loc; i++) {
- if (name->fullName.loc[i] == ASN_DOMAIN_COMPONENT) {
- idx++;
- }
- }
-
- /* check that index is not larger than max buffer size or larger
- * than the number of domain components parsed */
- if (idx >= DOMAIN_COMPONENT_MAX || idx > name->fullName.dcNum) {
- WOLFSSL_MSG("Index was larger then domain buffer");
- return NULL;
- }
- pt = &name->fullName.fullName[name->fullName.dcIdx[idx]],
- sz = name->fullName.dcLen[idx];
- name->cnEntry.nid = ASN_DOMAIN_COMPONENT;
- name->cnEntry.data.type = CTC_UTF8;
- }
- break;
-
- default:
- return NULL;
- }
-
- /* -1 to leave room for trailing terminator 0 */
- if (sz == 0 || sz >= CTC_NAME_SIZE - 1)
- return NULL;
- if (wolfSSL_ASN1_STRING_set(name->cnEntry.value, pt, sz) !=
- WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error setting local ASN1 string data");
- return NULL;
- }
- name->cnEntry.value->type = CTC_UTF8;
- name->cnEntry.set = 1;
- return name;
- }
-
-
- WOLFSSL_X509_NAME_ENTRY *wolfSSL_X509_NAME_get_entry(
- WOLFSSL_X509_NAME *name, int loc)
- {
- WOLFSSL_ENTER("wolfSSL_X509_NAME_get_entry");
-
- if (name == NULL) {
- return NULL;
- }
-
- if (loc < 0) {
- WOLFSSL_MSG("Bad argument");
- return NULL;
- }
-
- if (loc <= DN_NAMES_MAX + name->fullName.dcNum) {
- if (wolfSSL_nameByLoc(name, loc) != NULL)
- return &name->cnEntry;
- }
- /* DC component */
- if (name->fullName.dcMode) {
- if (name->fullName.fullName != NULL){
- if (loc == name->fullName.dcNum){
- name->cnEntry.data.data
- = &name->fullName.fullName[name->fullName.cIdx];
- name->cnEntry.data.length = name->fullName.cLen;
- name->cnEntry.nid = ASN_COUNTRY_NAME;
- } else {
- name->cnEntry.data.data
- = &name->fullName.fullName[name->fullName.dcIdx[loc]];
- name->cnEntry.data.length = name->fullName.dcLen[loc];
- name->cnEntry.nid = ASN_DOMAIN_COMPONENT;
- }
- }
- name->cnEntry.data.type = CTC_UTF8;
- /* common name index case */
- } else if (loc == name->fullName.cnIdx && name->x509 != NULL) {
- /* get CN shortcut from x509 since it has null terminator */
- name->cnEntry.data.data = name->x509->subjectCN;
- name->cnEntry.data.length = name->fullName.cnLen;
- name->cnEntry.data.type = CTC_UTF8;
- name->cnEntry.nid = ASN_COMMON_NAME;
- name->cnEntry.set = 1;
- } else {
- WOLFSSL_MSG("loc passed in is not in range of parsed DN's");
- return NULL;
- }
- return &name->cnEntry;
- }
-
- #ifndef NO_WOLFSSL_STUB
- int wolfSSL_X509_check_private_key(WOLFSSL_X509 *x509, WOLFSSL_EVP_PKEY *key)
- {
- (void) x509;
- (void) key;
- WOLFSSL_ENTER("wolfSSL_X509_check_private_key");
- WOLFSSL_STUB("X509_check_private_key");
-
- return WOLFSSL_SUCCESS;
- }
-
- WOLF_STACK_OF(WOLFSSL_X509_NAME) *wolfSSL_dup_CA_list(
- WOLF_STACK_OF(WOLFSSL_X509_NAME) *sk)
- {
- (void) sk;
- WOLFSSL_ENTER("wolfSSL_dup_CA_list");
- WOLFSSL_STUB("SSL_dup_CA_list");
-
- return NULL;
- }
-
- #endif
-
-#endif /* OPENSSL_ALL || HAVE_LIGHTY || WOLFSSL_MYSQL_COMPATIBLE ||
- HAVE_STUNNEL || WOLFSSL_NGINX || HAVE_POCO_LIB || WOLFSSL_HAPROXY */
-#endif /* OPENSSL_EXTRA */
-
-#ifndef WOLFCRYPT_ONLY
-
-#ifdef OPENSSL_EXTRA
-
-/* wolfSSL uses negative values for error states. This function returns an
- * unsigned type so the value returned is the absolute value of the error.
- */
-unsigned long wolfSSL_ERR_peek_last_error_line(const char **file, int *line)
-{
- WOLFSSL_ENTER("wolfSSL_ERR_peek_last_error");
-
- (void)line;
- (void)file;
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(DEBUG_WOLFSSL) || \
- defined(WOLFSSL_HAPROXY)
- {
- int ret;
-
- if ((ret = wc_PeekErrorNode(-1, file, NULL, line)) < 0) {
- WOLFSSL_MSG("Issue peeking at error node in queue");
- return 0;
- }
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
- if (ret == -ASN_NO_PEM_HEADER)
- return (ERR_LIB_PEM << 24) | PEM_R_NO_START_LINE;
- #endif
- return (unsigned long)ret;
- }
-#else
- return (unsigned long)(0 - NOT_COMPILED_IN);
-#endif
-}
-
-
-#ifndef NO_CERTS
-int wolfSSL_CTX_use_PrivateKey(WOLFSSL_CTX *ctx, WOLFSSL_EVP_PKEY *pkey)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey");
-
- if (ctx == NULL || pkey == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- if (pkey->pkey.ptr != NULL) {
- /* ptr for WOLFSSL_EVP_PKEY struct is expected to be DER format */
- return wolfSSL_CTX_use_PrivateKey_buffer(ctx,
- (const unsigned char*)pkey->pkey.ptr,
- pkey->pkey_sz, SSL_FILETYPE_ASN1);
- }
-
- WOLFSSL_MSG("wolfSSL private key not set");
- return BAD_FUNC_ARG;
-}
-#endif /* !NO_CERTS */
-
-
-#if defined(HAVE_EX_DATA) || defined(FORTRESS)
-void* wolfSSL_CTX_get_ex_data(const WOLFSSL_CTX* ctx, int idx)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_get_ex_data");
- #ifdef HAVE_EX_DATA
- if(ctx != NULL) {
- return wolfSSL_CRYPTO_get_ex_data(&ctx->ex_data, idx);
- }
- #else
- (void)ctx;
- (void)idx;
- #endif
- return NULL;
-}
-
-int wolfSSL_CTX_get_ex_new_index(long idx, void* arg, void* a, void* b,
- void* c)
-{
- static int ctx_idx = 0;
-
- WOLFSSL_ENTER("wolfSSL_CTX_get_ex_new_index");
- (void)idx;
- (void)arg;
- (void)a;
- (void)b;
- (void)c;
-
- return ctx_idx++;
-}
-
-/* Return the index that can be used for the WOLFSSL structure to store
- * application data.
- *
- */
-int wolfSSL_get_ex_new_index(long argValue, void* arg,
- WOLFSSL_CRYPTO_EX_new* cb1, WOLFSSL_CRYPTO_EX_dup* cb2,
- WOLFSSL_CRYPTO_EX_free* cb3)
-{
- static int ssl_idx = 0;
-
- WOLFSSL_ENTER("wolfSSL_get_ex_new_index");
-
- (void)argValue;
- (void)arg;
- (void)cb1;
- (void)cb2;
- (void)cb3;
-
- return ssl_idx++;
-}
-
-
-int wolfSSL_CTX_set_ex_data(WOLFSSL_CTX* ctx, int idx, void* data)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_set_ex_data");
- #ifdef HAVE_EX_DATA
- if (ctx != NULL)
- {
- return wolfSSL_CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
- }
- #else
- (void)ctx;
- (void)idx;
- (void)data;
- #endif
- return WOLFSSL_FAILURE;
-}
-#endif
-
-
-/* Returns char* to app data stored in ex[0].
- *
- * ssl WOLFSSL structure to get app data from
- */
-void* wolfSSL_get_app_data(const WOLFSSL *ssl)
-{
- /* checkout exdata stuff... */
- WOLFSSL_ENTER("wolfSSL_get_app_data");
-
- return wolfSSL_get_ex_data(ssl, 0);
-}
-
-
-/* Set ex array 0 to have app data
- *
- * ssl WOLFSSL struct to set app data in
- * arg data to be stored
- *
- * Returns SSL_SUCCESS on success and SSL_FAILURE on failure
- */
-int wolfSSL_set_app_data(WOLFSSL *ssl, void* arg) {
- WOLFSSL_ENTER("wolfSSL_set_app_data");
-
- return wolfSSL_set_ex_data(ssl, 0, arg);
-}
-
-
-int wolfSSL_set_ex_data(WOLFSSL* ssl, int idx, void* data)
-{
- WOLFSSL_ENTER("wolfSSL_set_ex_data");
-#if defined(HAVE_EX_DATA) || defined(FORTRESS)
- if (ssl != NULL)
- {
- return wolfSSL_CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
- }
-#else
- WOLFSSL_MSG("HAVE_EX_DATA macro is not defined");
- (void)ssl;
- (void)idx;
- (void)data;
-#endif
- return WOLFSSL_FAILURE;
-}
-
-
-
-void* wolfSSL_get_ex_data(const WOLFSSL* ssl, int idx)
-{
- WOLFSSL_ENTER("wolfSSL_get_ex_data");
-#if defined(HAVE_EX_DATA) || defined(FORTRESS)
- if (ssl != NULL) {
- return wolfSSL_CRYPTO_get_ex_data(&ssl->ex_data, idx);
- }
-#else
- WOLFSSL_MSG("HAVE_EX_DATA macro is not defined");
- (void)ssl;
- (void)idx;
-#endif
- return 0;
-}
-
-#ifndef NO_DSA
-WOLFSSL_DSA *wolfSSL_PEM_read_bio_DSAparams(WOLFSSL_BIO *bp, WOLFSSL_DSA **x,
- pem_password_cb *cb, void *u)
-{
- WOLFSSL_DSA* dsa;
- DsaKey* key;
- int length;
- unsigned char* buf;
- word32 bufSz;
- int ret;
- word32 idx = 0;
- DerBuffer* pDer;
-
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_DSAparams");
-
- ret = wolfSSL_BIO_get_mem_data(bp, &buf);
- if (ret <= 0) {
- WOLFSSL_LEAVE("wolfSSL_PEM_read_bio_DSAparams", ret);
- return NULL;
- }
-
- bufSz = (word32)ret;
-
- if (cb != NULL || u != NULL) {
- /*
- * cb is for a call back when encountering encrypted PEM files
- * if cb == NULL and u != NULL then u = null terminated password string
- */
- WOLFSSL_MSG("Not yet supporting call back or password for encrypted PEM");
- }
-
- if ((ret = PemToDer(buf, (long)bufSz, DSA_PARAM_TYPE, &pDer, NULL, NULL,
- NULL)) < 0 ) {
- WOLFSSL_MSG("Issue converting from PEM to DER");
- return NULL;
- }
-
- if ((ret = GetSequence(pDer->buffer, &idx, &length, pDer->length)) < 0) {
- WOLFSSL_LEAVE("wolfSSL_PEM_read_bio_DSAparams", ret);
- FreeDer(&pDer);
- return NULL;
- }
-
- dsa = wolfSSL_DSA_new();
- if (dsa == NULL) {
- FreeDer(&pDer);
- WOLFSSL_MSG("Error creating DSA struct");
- return NULL;
- }
-
- key = (DsaKey*)dsa->internal;
- if (key == NULL) {
- FreeDer(&pDer);
- wolfSSL_DSA_free(dsa);
- WOLFSSL_MSG("Error finding DSA key struct");
- return NULL;
- }
-
- if (GetInt(&key->p, pDer->buffer, &idx, pDer->length) < 0 ||
- GetInt(&key->q, pDer->buffer, &idx, pDer->length) < 0 ||
- GetInt(&key->g, pDer->buffer, &idx, pDer->length) < 0 ) {
- WOLFSSL_MSG("dsa key error");
- FreeDer(&pDer);
- wolfSSL_DSA_free(dsa);
- return NULL;
- }
-
- if (SetIndividualExternal(&dsa->p, &key->p) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dsa p key error");
- FreeDer(&pDer);
- wolfSSL_DSA_free(dsa);
- return NULL;
- }
-
- if (SetIndividualExternal(&dsa->q, &key->q) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dsa q key error");
- FreeDer(&pDer);
- wolfSSL_DSA_free(dsa);
- return NULL;
- }
-
- if (SetIndividualExternal(&dsa->g, &key->g) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dsa g key error");
- FreeDer(&pDer);
- wolfSSL_DSA_free(dsa);
- return NULL;
- }
-
- if (x != NULL) {
- *x = dsa;
- }
-
- FreeDer(&pDer);
- return dsa;
-}
-#endif /* NO_DSA */
-#endif /* OPENSSL_EXTRA */
-#endif /* WOLFCRYPT_ONLY */
-
-#if defined(OPENSSL_EXTRA)
-
-/* Begin functions for openssl/buffer.h */
-WOLFSSL_BUF_MEM* wolfSSL_BUF_MEM_new(void)
-{
- WOLFSSL_BUF_MEM* buf;
- buf = (WOLFSSL_BUF_MEM*)XMALLOC(sizeof(WOLFSSL_BUF_MEM), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (buf) {
- XMEMSET(buf, 0, sizeof(WOLFSSL_BUF_MEM));
- }
- return buf;
-}
-
-
-/* returns length of buffer on success */
-int wolfSSL_BUF_MEM_grow(WOLFSSL_BUF_MEM* buf, size_t len)
-{
- int len_int = (int)len;
- int mx;
-
- /* verify provided arguments */
- if (buf == NULL || len_int < 0) {
- return 0; /* BAD_FUNC_ARG; */
- }
-
- /* check to see if fits in existing length */
- if (buf->length > len) {
- buf->length = len;
- return len_int;
- }
-
- /* check to see if fits in max buffer */
- if (buf->max >= len) {
- if (buf->data != NULL) {
- XMEMSET(&buf->data[buf->length], 0, len - buf->length);
- }
- buf->length = len;
- return len_int;
- }
-
- /* expand size, to handle growth */
- mx = (len_int + 3) / 3 * 4;
-
- /* use realloc */
- buf->data = (char*)XREALLOC(buf->data, mx, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (buf->data == NULL) {
- return 0; /* ERR_R_MALLOC_FAILURE; */
- }
-
- buf->max = mx;
- XMEMSET(&buf->data[buf->length], 0, len - buf->length);
- buf->length = len;
-
- return len_int;
-}
-
-void wolfSSL_BUF_MEM_free(WOLFSSL_BUF_MEM* buf)
-{
- if (buf) {
- if (buf->data) {
- XFREE(buf->data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- buf->data = NULL;
- }
- buf->max = 0;
- buf->length = 0;
- XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-/* End Functions for openssl/buffer.h */
-
-#endif /* OPENSSL_EXTRA */
-
-
-#if defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL) \
- || defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_EXTRA)
-
-WOLFSSL_BIO *wolfSSL_BIO_new_file(const char *filename, const char *mode)
-{
-#ifndef NO_FILESYSTEM
- WOLFSSL_BIO* bio;
- XFILE fp;
-
- WOLFSSL_ENTER("wolfSSL_BIO_new_file");
-
- fp = XFOPEN(filename, mode);
- if (fp == XBADFILE)
- return NULL;
-
- bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
- if (bio == NULL) {
- XFCLOSE(fp);
- return bio;
- }
-
- if (wolfSSL_BIO_set_fp(bio, fp, BIO_CLOSE) != WOLFSSL_SUCCESS) {
- XFCLOSE(fp);
- wolfSSL_BIO_free(bio);
- bio = NULL;
- }
-
- /* file is closed when BIO is free'd */
- return bio;
-#else
- (void)filename;
- (void)mode;
- return NULL;
-#endif /* NO_FILESYSTEM */
-}
-
-#ifndef NO_FILESYSTEM
-WOLFSSL_BIO* wolfSSL_BIO_new_fp(XFILE fp, int close_flag)
-{
- WOLFSSL_BIO* bio;
-
- WOLFSSL_ENTER("wolfSSL_BIO_new_fp");
-
- bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
- if (bio == NULL) {
- return bio;
- }
-
- if (wolfSSL_BIO_set_fp(bio, fp, close_flag) != WOLFSSL_SUCCESS) {
- wolfSSL_BIO_free(bio);
- bio = NULL;
- }
-
- /* file is closed when BIO is free'd or by user depending on flag */
- return bio;
-}
-#endif
-
-
-#ifndef NO_DH
-WOLFSSL_DH *wolfSSL_PEM_read_bio_DHparams(WOLFSSL_BIO *bio, WOLFSSL_DH **x,
- pem_password_cb *cb, void *u)
-{
-#ifndef NO_FILESYSTEM
- WOLFSSL_DH* localDh = NULL;
- unsigned char* mem = NULL;
- word32 size;
- long sz;
- int ret;
- DerBuffer *der = NULL;
- byte* p = NULL;
- byte* g = NULL;
- word32 pSz = MAX_DH_SIZE;
- word32 gSz = MAX_DH_SIZE;
- int memAlloced = 0;
-
- WOLFSSL_ENTER("wolfSSL_PEM_read_bio_DHparams");
- (void)cb;
- (void)u;
-
- if (bio == NULL) {
- WOLFSSL_MSG("Bad Function Argument bio is NULL");
- return NULL;
- }
-
- if (bio->type == WOLFSSL_BIO_MEMORY) {
- /* Use the buffer directly. */
- ret = wolfSSL_BIO_get_mem_data(bio, &mem);
- if (mem == NULL || ret <= 0) {
- WOLFSSL_MSG("Failed to get data from bio struct");
- goto end;
- }
- size = ret;
- }
- else if (bio->type == WOLFSSL_BIO_FILE) {
- /* Read whole file into a new buffer. */
- if (XFSEEK((XFILE)bio->ptr, 0, SEEK_END) != 0)
- goto end;
- sz = XFTELL((XFILE)bio->ptr);
- if (XFSEEK((XFILE)bio->ptr, 0, SEEK_SET) != 0)
- goto end;
- if (sz > MAX_WOLFSSL_FILE_SIZE || sz <= 0L) {
- WOLFSSL_MSG("PEM_read_bio_DHparams file size error");
- goto end;
- }
- mem = (unsigned char*)XMALLOC(sz, NULL, DYNAMIC_TYPE_PEM);
- if (mem == NULL)
- goto end;
- memAlloced = 1;
-
- if (wolfSSL_BIO_read(bio, (char *)mem, (int)sz) <= 0)
- goto end;
- size = (word32)sz;
- }
- else {
- WOLFSSL_MSG("BIO type not supported for reading DH parameters");
- goto end;
- }
-
- ret = PemToDer(mem, size, DH_PARAM_TYPE, &der, NULL, NULL, NULL);
- if (ret != 0)
- goto end;
-
- /* Use the object passed in, otherwise allocate a new object */
- if (x != NULL)
- localDh = *x;
- if (localDh == NULL) {
- localDh = wolfSSL_DH_new();
- if (localDh == NULL)
- goto end;
- }
-
- /* Load data in manually */
- p = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- g = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- if (p == NULL || g == NULL)
- goto end;
-
- /* Extract the p and g as data from the DER encoded DH parameters. */
- ret = wc_DhParamsLoad(der->buffer, der->length, p, &pSz, g, &gSz);
- if (ret != 0) {
- if (x != NULL && localDh != *x)
- XFREE(localDh, NULL, DYNAMIC_TYPE_OPENSSL);
- localDh = NULL;
- goto end;
- }
-
- if (x != NULL)
- *x = localDh;
-
- /* Put p and g in as big numbers. */
- if (localDh->p != NULL) {
- wolfSSL_BN_free(localDh->p);
- localDh->p = NULL;
- }
- if (localDh->g != NULL) {
- wolfSSL_BN_free(localDh->g);
- localDh->g = NULL;
- }
- localDh->p = wolfSSL_BN_bin2bn(p, pSz, NULL);
- localDh->g = wolfSSL_BN_bin2bn(g, gSz, NULL);
- if (localDh->p == NULL || localDh->g == NULL) {
- if (x != NULL && localDh != *x)
- wolfSSL_DH_free(localDh);
- localDh = NULL;
- }
-
- if (localDh != NULL && localDh->inSet == 0) {
- if (SetDhInternal(localDh) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Unable to set internal DH structure");
- wolfSSL_DH_free(localDh);
- localDh = NULL;
- }
- }
-
-end:
- if (memAlloced) XFREE(mem, NULL, DYNAMIC_TYPE_PEM);
- if (der != NULL) FreeDer(&der);
- XFREE(p, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(g, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
- return localDh;
-#else
- (void)bio;
- (void)x;
- (void)cb;
- (void)u;
- return NULL;
-#endif
-}
-
-#ifndef NO_FILESYSTEM
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
-/* Convert DH key parameters to DER format, write to output (outSz)
- * If output is NULL then max expected size is set to outSz and LENGTH_ONLY_E is
- * returned.
- *
- * Note : static function due to redefinition complications with DhKey and FIPS
- * version 2 build.
- *
- * return bytes written on success */
-int wc_DhParamsToDer(DhKey* key, byte* out, word32* outSz)
-{
- word32 sz = 0, idx = 0;
- int pSz = 0, gSz = 0, ret;
- byte scratch[MAX_LENGTH_SZ];
-
- if (key == NULL || outSz == NULL) {
- return BAD_FUNC_ARG;
- }
-
- pSz = mp_unsigned_bin_size(&key->p);
- if (pSz < 0) {
- return pSz;
- }
- if (mp_leading_bit(&key->p)) {
- pSz++;
- }
-
- gSz = mp_unsigned_bin_size(&key->g);
- if (gSz < 0) {
- return gSz;
- }
- if (mp_leading_bit(&key->g)) {
- gSz++;
- }
-
- sz = ASN_TAG_SZ; /* Integer */
- sz += SetLength(pSz, scratch);
- sz += ASN_TAG_SZ; /* Integer */
- sz += SetLength(gSz, scratch);
- sz += gSz + pSz;
-
- if (out == NULL) {
- byte seqScratch[MAX_SEQ_SZ];
-
- *outSz = sz + SetSequence(sz, seqScratch);
- return LENGTH_ONLY_E;
- }
-
- if (*outSz < MAX_SEQ_SZ || *outSz < sz) {
- return BUFFER_E;
- }
-
- idx += SetSequence(sz, out);
- if (*outSz < idx + sz) {
- return BUFFER_E;
- }
-
- out[idx++] = ASN_INTEGER;
- idx += SetLength(pSz, out + idx);
- if (mp_leading_bit(&key->p)) {
- out[idx++] = 0x00;
- pSz -= 1; /* subtract 1 from size to account for leading 0 */
- }
- ret = mp_to_unsigned_bin(&key->p, out + idx);
- if (ret != MP_OKAY) {
- return BUFFER_E;
- }
- idx += pSz;
-
- out[idx++] = ASN_INTEGER;
- idx += SetLength(gSz, out + idx);
- if (mp_leading_bit(&key->g)) {
- out[idx++] = 0x00;
- gSz -= 1; /* subtract 1 from size to account for leading 0 */
- }
- ret = mp_to_unsigned_bin(&key->g, out + idx);
- if (ret != MP_OKAY) {
- return BUFFER_E;
- }
- idx += gSz;
- return idx;
-}
-
-int wc_DhPubKeyToDer(DhKey* key, byte* out, word32* outSz)
-{
- word32 sz = 0;
- word32 paramSz = 0;
- int ret;
- int pubSz = 0;
- int idx = 0;
- byte scratch[MAX_ALGO_SZ];
-
- /* Get size of entire key */
-
- /* SEQUENCE <--| SetAlgoId
- * OBJECT IDENTIFIER <--|
- * SEQUENCE <--
- * INTEGER | wc_DhParamsToDer
- * INTEGER <--
- */
- ret = wc_DhParamsToDer(key, NULL, ¶mSz);
- if (ret != LENGTH_ONLY_E)
- return ASN_PARSE_E;
- sz += paramSz;
- sz += SetAlgoID(DHk, scratch, oidKeyType, paramSz);
-
- /* BIT STRING
- * INTEGER
- */
- pubSz = mp_unsigned_bin_size(&key->pub);
- if (pubSz < 0)
- return pubSz;
-
- if (mp_leading_bit(&key->pub))
- pubSz++;
-
- sz += ASN_TAG_SZ; /* Integer */
- sz += SetLength(pubSz, scratch);
- sz += pubSz;
-
- sz += SetBitString(pubSz, 0, scratch);
-
- if (out == NULL) {
- /* Uppermost SEQUENCE */
- *outSz = sz + SetSequence(sz, scratch);
- return LENGTH_ONLY_E;
- }
- /* end get size of entire key */
-
- /* Check for indexing errors */
- if (*outSz < MAX_SEQ_SZ || *outSz < sz) {
- return BUFFER_E;
- }
-
- /* Build Up Entire Key */
-
- idx += SetSequence(sz, out);
-
- idx += SetAlgoID(DHk, out+idx, oidKeyType, paramSz);
- ret = wc_DhParamsToDer(key, out+idx, ¶mSz);
- if (ret < 0)
- return ret;
- idx += ret;
-
- /* BIT STRING
- * INTEGER
- */
- idx += SetBitString(pubSz, 0, out+idx);
-
- out[idx++] = ASN_INTEGER;
- idx += SetLength(pubSz, out + idx);
- if (mp_leading_bit(&key->pub)) {
- out[idx++] = 0x00;
- pubSz -= 1; /* subtract 1 from size to account for leading 0 */
- }
- ret = mp_to_unsigned_bin(&key->pub, out + idx);
- if (ret != MP_OKAY) {
- return BUFFER_E;
- }
- idx += pubSz;
-
- return idx;
-}
-
-int wc_DhPrivKeyToDer(DhKey* key, byte* out, word32* outSz)
-{
- word32 sz = 0;
- word32 paramSz = 0;
- int ret;
- int privSz = 0;
- int idx = 0;
- byte scratch[MAX_ALGO_SZ];
-
- /* Get size of entire key */
-
- /* INTEGER 0 */
- sz += ASN_TAG_SZ; /* Integer */
- sz += SetLength(1, scratch);
- sz += 1;
-
- /* SEQUENCE <--| SetAlgoId
- * OBJECT IDENTIFIER <--|
- * SEQUENCE <--
- * INTEGER | wc_DhParamsToDer
- * INTEGER <--
- */
- ret = wc_DhParamsToDer(key, NULL, ¶mSz);
- if (ret != LENGTH_ONLY_E)
- return ASN_PARSE_E;
- sz += paramSz;
- sz += SetAlgoID(DHk, scratch, oidKeyType, paramSz);
-
- /* OCTET STRING
- * INTEGER
- */
- privSz = mp_unsigned_bin_size(&key->priv);
- if (privSz < 0)
- return privSz;
- else if (privSz > 256) /* Key is larger than 2048 */
- return ASN_VERSION_E;
-
- if (mp_leading_bit(&key->priv))
- privSz++;
-
- sz += ASN_TAG_SZ; /* Integer */
- sz += SetLength(privSz, scratch);
- sz += privSz;
-
- sz += SetOctetString(privSz + ASN_OCTET_STRING, scratch);
-
- if (out == NULL) {
- /* Uppermost SEQUENCE */
- *outSz = sz + SetSequence(sz, scratch);
- return LENGTH_ONLY_E;
- }
- /* end get size of entire key */
-
- /* Check for indexing errors */
- if (*outSz < MAX_SEQ_SZ || *outSz < sz) {
- return BUFFER_E;
- }
-
- /* Build Up Entire Key */
-
- idx += SetSequence(sz, out);
-
- /* INTEGER 0 */
- out[idx++] = ASN_INTEGER;
- idx += SetLength(1, out+idx);
- out[idx++] = 0;
-
- idx += SetAlgoID(DHk, out+idx, oidKeyType, paramSz);
- ret = wc_DhParamsToDer(key, out+idx, ¶mSz);
- if (ret < 0)
- return ret;
- idx += ret;
-
- /* OCTET STRING
- * INTEGER
- */
- if (privSz == 256) {
- idx += SetOctetString(privSz + ASN_OCTET_STRING, out+idx);
- } else if (privSz == 128) {
- idx += SetOctetString(privSz + ASN_OCTET_STRING-1, out+idx);
- } else if (privSz == 64) {
- idx += SetOctetString(privSz + ASN_OCTET_STRING-2, out+idx);
- } else {
- WOLFSSL_MSG("Unsupported key size");
- return ASN_VERSION_E;
- }
-
- out[idx++] = ASN_INTEGER;
- idx += SetLength(privSz, out + idx);
- if (mp_leading_bit(&key->priv)) {
- out[idx++] = 0x00;
- privSz -= 1; /* subtract 1 from size to account for leading 0 */
- }
- ret = mp_to_unsigned_bin(&key->priv, out + idx);
- if (ret != MP_OKAY) {
- return BUFFER_E;
- }
- idx += privSz;
-
- return idx;
-}
-
-/* Writes the DH parameters in PEM format from "dh" out to the file pointer
- * passed in.
- *
- * returns WOLFSSL_SUCCESS on success
- */
-int wolfSSL_PEM_write_DHparams(XFILE fp, WOLFSSL_DH* dh)
-{
- int ret;
- word32 derSz = 0, pemSz = 0;
- byte *der, *pem;
- DhKey* key;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_DHparams");
-
- if (dh == NULL) {
- WOLFSSL_LEAVE("wolfSSL_PEM_write_DHparams", BAD_FUNC_ARG);
- return WOLFSSL_FAILURE;
- }
-
- if (dh->inSet == 0) {
- if (SetDhInternal(dh) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Unable to set internal DH structure");
- return WOLFSSL_FAILURE;
- }
- }
- key = (DhKey*)dh->internal;
- ret = wc_DhParamsToDer(key, NULL, &derSz);
- if (ret != LENGTH_ONLY_E) {
- WOLFSSL_MSG("Failed to get size of DH params");
- WOLFSSL_LEAVE("wolfSSL_PEM_write_DHparams", ret);
- return WOLFSSL_FAILURE;
- }
-
- der = (byte*)XMALLOC(derSz, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (der == NULL) {
- WOLFSSL_LEAVE("wolfSSL_PEM_write_DHparams", MEMORY_E);
- return WOLFSSL_FAILURE;
- }
- ret = wc_DhParamsToDer(key, der, &derSz);
- if (ret <= 0) {
- WOLFSSL_MSG("Failed to export DH params");
- WOLFSSL_LEAVE("wolfSSL_PEM_write_DHparams", ret);
- XFREE(der, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
-
- /* convert to PEM */
- ret = wc_DerToPem(der, derSz, NULL, 0, DH_PARAM_TYPE);
- if (ret < 0) {
- WOLFSSL_MSG("Failed to convert DH params to PEM");
- WOLFSSL_LEAVE("wolfSSL_PEM_write_DHparams", ret);
- XFREE(der, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
- pemSz = (word32)ret;
-
- pem = (byte*)XMALLOC(pemSz, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (pem == NULL) {
- WOLFSSL_LEAVE("wolfSSL_PEM_write_DHparams", MEMORY_E);
- XFREE(der, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
- ret = wc_DerToPem(der, derSz, pem, pemSz, DH_PARAM_TYPE);
- XFREE(der, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (ret < 0) {
- WOLFSSL_MSG("Failed to convert DH params to PEM");
- WOLFSSL_LEAVE("wolfSSL_PEM_write_DHparams", ret);
- XFREE(pem, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
-
- ret = (int)XFWRITE(pem, 1, pemSz, fp);
- XFREE(pem, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (ret <= 0) {
- WOLFSSL_MSG("Failed to write to file");
- WOLFSSL_LEAVE("wolfSSL_PEM_write_DHparams", ret);
- return WOLFSSL_FAILURE;
- }
-
- WOLFSSL_LEAVE("wolfSSL_PEM_write_DHparams", WOLFSSL_SUCCESS);
- return WOLFSSL_SUCCESS;
-}
-#endif /* WOLFSSL_QT || OPENSSL_ALL */
-#endif /* !NO_FILESYSTEM */
-#endif /* !NO_DH */
-
-#ifdef WOLFSSL_CERT_GEN
-
-#ifdef WOLFSSL_CERT_REQ
-/* writes the x509 from x to the WOLFSSL_BIO bp
- *
- * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on fail
- */
-int wolfSSL_PEM_write_bio_X509_REQ(WOLFSSL_BIO *bp, WOLFSSL_X509 *x)
-{
- byte* pem;
- int pemSz = 0;
- const unsigned char* der;
- int derSz;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_X509_REQ()");
-
- if (x == NULL || bp == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- der = wolfSSL_X509_get_der(x, &derSz);
- if (der == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* get PEM size */
- pemSz = wc_DerToPemEx(der, derSz, NULL, 0, NULL, CERTREQ_TYPE);
- if (pemSz < 0) {
- return WOLFSSL_FAILURE;
- }
-
- /* create PEM buffer and convert from DER */
- pem = (byte*)XMALLOC(pemSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (pem == NULL) {
- return WOLFSSL_FAILURE;
- }
- if (wc_DerToPemEx(der, derSz, pem, pemSz, NULL, CERTREQ_TYPE) < 0) {
- XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
-
- /* write the PEM to BIO */
- ret = wolfSSL_BIO_write(bp, pem, pemSz);
- XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if (ret <= 0) return WOLFSSL_FAILURE;
- return WOLFSSL_SUCCESS;
-}
-#endif /* WOLFSSL_CERT_REQ */
-
-
-/* writes the x509 from x to the WOLFSSL_BIO bp
- *
- * returns WOLFSSL_SUCCESS on success and WOLFSSL_FAILURE on fail
- */
-int wolfSSL_PEM_write_bio_X509_AUX(WOLFSSL_BIO *bp, WOLFSSL_X509 *x)
-{
- byte* pem;
- int pemSz = 0;
- const unsigned char* der;
- int derSz;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_X509_AUX()");
-
- if (bp == NULL || x == NULL) {
- WOLFSSL_MSG("NULL argument passed in");
- return WOLFSSL_FAILURE;
- }
-
- der = wolfSSL_X509_get_der(x, &derSz);
- if (der == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* get PEM size */
- pemSz = wc_DerToPemEx(der, derSz, NULL, 0, NULL, CERT_TYPE);
- if (pemSz < 0) {
- return WOLFSSL_FAILURE;
- }
-
- /* create PEM buffer and convert from DER */
- pem = (byte*)XMALLOC(pemSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (pem == NULL) {
- return WOLFSSL_FAILURE;
- }
- if (wc_DerToPemEx(der, derSz, pem, pemSz, NULL, CERT_TYPE) < 0) {
- XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
-
- /* write the PEM to BIO */
- ret = wolfSSL_BIO_write(bp, pem, pemSz);
- XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if (ret <= 0) return WOLFSSL_FAILURE;
- return WOLFSSL_SUCCESS;
-}
-#endif /* WOLFSSL_CERT_GEN */
-
-int wolfSSL_PEM_write_bio_X509(WOLFSSL_BIO *bio, WOLFSSL_X509 *cert)
-{
- byte* pem;
- int pemSz = 0;
- const unsigned char* der;
- int derSz;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_X509_AUX()");
-
- if (bio == NULL || cert == NULL) {
- WOLFSSL_MSG("NULL argument passed in");
- return WOLFSSL_FAILURE;
- }
-
- der = wolfSSL_X509_get_der(cert, &derSz);
- if (der == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* get PEM size */
- pemSz = wc_DerToPemEx(der, derSz, NULL, 0, NULL, CERT_TYPE);
- if (pemSz < 0) {
- return WOLFSSL_FAILURE;
- }
-
- /* create PEM buffer and convert from DER */
- pem = (byte*)XMALLOC(pemSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (pem == NULL) {
- return WOLFSSL_FAILURE;
- }
- if (wc_DerToPemEx(der, derSz, pem, pemSz, NULL, CERT_TYPE) < 0) {
- XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
-
- /* write the PEM to BIO */
- ret = wolfSSL_BIO_write(bio, pem, pemSz);
- XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if (ret <= 0) return WOLFSSL_FAILURE;
- return WOLFSSL_SUCCESS;
-}
-
-
-#if defined(OPENSSL_EXTRA) && !defined(NO_DH)
-/* Initialize ctx->dh with dh's params. Return WOLFSSL_SUCCESS on ok */
-long wolfSSL_CTX_set_tmp_dh(WOLFSSL_CTX* ctx, WOLFSSL_DH* dh)
-{
- int pSz, gSz;
- byte *p, *g;
- int ret=0;
-
- WOLFSSL_ENTER("wolfSSL_CTX_set_tmp_dh");
-
- if(!ctx || !dh)
- return BAD_FUNC_ARG;
-
- /* Get needed size for p and g */
- pSz = wolfSSL_BN_bn2bin(dh->p, NULL);
- gSz = wolfSSL_BN_bn2bin(dh->g, NULL);
-
- if(pSz <= 0 || gSz <= 0)
- return WOLFSSL_FATAL_ERROR;
-
- p = (byte*)XMALLOC(pSz, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if(!p)
- return MEMORY_E;
-
- g = (byte*)XMALLOC(gSz, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if(!g) {
- XFREE(p, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- return MEMORY_E;
- }
-
- pSz = wolfSSL_BN_bn2bin(dh->p, p);
- gSz = wolfSSL_BN_bn2bin(dh->g, g);
-
- if(pSz >= 0 && gSz >= 0) /* Conversion successful */
- ret = wolfSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz);
-
- XFREE(p, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(g, ctx->heap, DYNAMIC_TYPE_PUBLIC_KEY);
-
- return pSz > 0 && gSz > 0 ? ret : WOLFSSL_FATAL_ERROR;
-}
-#endif /* OPENSSL_EXTRA && !NO_DH */
-
-
-/* returns the enum value associated with handshake state
- *
- * ssl the WOLFSSL structure to get state of
- */
-int wolfSSL_get_state(const WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_get_state");
-
- if (ssl == NULL) {
- WOLFSSL_MSG("Null argument passed in");
- return SSL_FAILURE;
- }
-
- return ssl->options.handShakeState;
-}
-#endif /* HAVE_LIGHTY || HAVE_STUNNEL || WOLFSSL_MYSQL_COMPATIBLE */
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
- || defined(WOLFSSL_NGINX) || defined(WOLFSSL_QT)
-
-long wolfSSL_ctrl(WOLFSSL* ssl, int cmd, long opt, void* pt)
-{
- WOLFSSL_ENTER("wolfSSL_ctrl");
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- switch (cmd) {
- #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- case SSL_CTRL_SET_TLSEXT_HOSTNAME:
- WOLFSSL_MSG("Entering Case: SSL_CTRL_SET_TLSEXT_HOSTNAME.");
- #ifdef HAVE_SNI
- if (pt == NULL) {
- WOLFSSL_MSG("Passed in NULL Host Name.");
- break;
- }
- return wolfSSL_set_tlsext_host_name(ssl, (const char*) pt);
- #else
- WOLFSSL_MSG("SNI not enabled.");
- break;
- #endif /* HAVE_SNI */
- #endif /* WOLFSSL_NGINX || WOLFSSL_QT || OPENSSL_ALL */
- default:
- WOLFSSL_MSG("Case not implemented.");
- }
- (void)opt;
- (void)pt;
- return WOLFSSL_FAILURE;
-}
-
-long wolfSSL_CTX_ctrl(WOLFSSL_CTX* ctx, int cmd, long opt, void* pt)
-{
-#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- long ctrl_opt;
-#endif
- long ret = WOLFSSL_SUCCESS;
-
- WOLFSSL_ENTER("wolfSSL_CTX_ctrl");
- if (ctx == NULL)
- return WOLFSSL_FAILURE;
-
- switch (cmd) {
- case SSL_CTRL_CHAIN:
-#ifdef SESSION_CERTS
- {
- /*
- * We don't care about opt here because a copy of the certificate is
- * stored anyway so increasing the reference counter is not necessary.
- * Just check to make sure that it is set to one of the correct values.
- */
- WOLF_STACK_OF(WOLFSSL_X509)* sk = (WOLF_STACK_OF(WOLFSSL_X509)*) pt;
- WOLFSSL_X509* x509;
- int i;
- if (opt != 0 && opt != 1) {
- ret = WOLFSSL_FAILURE;
- break;
- }
- /* Clear certificate chain */
- FreeDer(&ctx->certChain);
- if (sk) {
- for (i = 0; i < wolfSSL_sk_X509_num(sk); i++) {
- x509 = wolfSSL_sk_X509_value(sk, i);
- /* Prevent wolfSSL_CTX_add_extra_chain_cert from freeing cert */
- if (wolfSSL_X509_up_ref(x509) != 1) {
- WOLFSSL_MSG("Error increasing reference count");
- continue;
- }
- if (wolfSSL_CTX_add_extra_chain_cert(ctx, x509) !=
- WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Error adding certificate to context");
- /* Decrease reference count on failure */
- wolfSSL_X509_free(x509);
- }
- }
- }
- /* Free previous chain */
- wolfSSL_sk_X509_free(ctx->x509Chain);
- ctx->x509Chain = sk;
- if (sk) {
- for (i = 0; i < wolfSSL_sk_X509_num(sk); i++) {
- x509 = wolfSSL_sk_X509_value(sk, i);
- /* On successful setting of new chain up all refs */
- if (wolfSSL_X509_up_ref(x509) != 1) {
- WOLFSSL_MSG("Error increasing reference count");
- continue;
- }
- }
- }
- }
-#else
- WOLFSSL_MSG("Session certificates not compiled in");
- ret = WOLFSSL_FAILURE;
-#endif
- break;
-
-#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- case SSL_CTRL_OPTIONS:
- WOLFSSL_MSG("Entering Case: SSL_CTRL_OPTIONS.");
- ctrl_opt = wolfSSL_CTX_set_options(ctx, opt);
-
- #ifdef WOLFSSL_QT
- /* Set whether to use client or server cipher preference */
- if ((ctrl_opt & SSL_OP_CIPHER_SERVER_PREFERENCE)
- == SSL_OP_CIPHER_SERVER_PREFERENCE) {
- WOLFSSL_MSG("Using Server's Cipher Preference.");
- ctx->useClientOrder = FALSE;
- } else {
- WOLFSSL_MSG("Using Client's Cipher Preference.");
- ctx->useClientOrder = TRUE;
- }
- #endif /* WOLFSSL_QT */
-
- return ctrl_opt;
-#endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
- case SSL_CTRL_EXTRA_CHAIN_CERT:
- WOLFSSL_MSG("Entering Case: SSL_CTRL_EXTRA_CHAIN_CERT.");
- if (pt == NULL) {
- WOLFSSL_MSG("Passed in x509 pointer NULL.");
- ret = WOLFSSL_FAILURE;
- break;
- }
- return wolfSSL_CTX_add_extra_chain_cert(ctx, (WOLFSSL_X509*)pt);
-
-#ifndef NO_DH
- case SSL_CTRL_SET_TMP_DH:
- WOLFSSL_MSG("Entering Case: SSL_CTRL_SET_TMP_DH.");
- if (pt == NULL) {
- WOLFSSL_MSG("Passed in DH pointer NULL.");
- ret = WOLFSSL_FAILURE;
- break;
- }
- return wolfSSL_CTX_set_tmp_dh(ctx, (WOLFSSL_DH*)pt);
-#endif
-
-#ifdef HAVE_ECC
- case SSL_CTRL_SET_TMP_ECDH:
- WOLFSSL_MSG("Entering Case: SSL_CTRL_SET_TMP_ECDH.");
- if (pt == NULL) {
- WOLFSSL_MSG("Passed in ECDH pointer NULL.");
- ret = WOLFSSL_FAILURE;
- break;
- }
- return wolfSSL_SSL_CTX_set_tmp_ecdh(ctx, (WOLFSSL_EC_KEY*)pt);
-#endif
- case SSL_CTRL_MODE:
- wolfSSL_CTX_set_mode(ctx,opt);
- break;
-
- default:
- WOLFSSL_MSG("CTX_ctrl cmd not implemented");
- ret = WOLFSSL_FAILURE;
- break;
- }
-
- (void)ctx;
- (void)cmd;
- (void)opt;
- (void)pt;
- WOLFSSL_LEAVE("wolfSSL_CTX_ctrl", (int)ret);
- return ret;
-}
-
-#ifndef WOLFSSL_NO_STUB
-long wolfSSL_CTX_callback_ctrl(WOLFSSL_CTX* ctx, int cmd, void (*fp)(void))
-{
- (void) ctx;
- (void) cmd;
- (void) fp;
- WOLFSSL_STUB("wolfSSL_CTX_callback_ctrl");
- return WOLFSSL_FAILURE;
-
-}
-#endif /* WOLFSSL_NO_STUB */
-
-#ifndef NO_WOLFSSL_STUB
-long wolfSSL_CTX_clear_extra_chain_certs(WOLFSSL_CTX* ctx)
-{
- return wolfSSL_CTX_ctrl(ctx, SSL_CTRL_CLEAR_EXTRA_CHAIN_CERTS, 0l, NULL);
-}
-#endif
-
-/* Returns the verifyCallback from the ssl structure if successful.
-Returns NULL otherwise. */
-VerifyCallback wolfSSL_get_verify_callback(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_get_verify_callback()");
- if (ssl) {
- return ssl->verifyCallback;
- }
- return NULL;
-}
-
-/* Creates a new bio pair.
-Returns WOLFSSL_SUCCESS if no error, WOLFSSL_FAILURE otherwise.*/
-int wolfSSL_BIO_new_bio_pair(WOLFSSL_BIO **bio1_p, size_t writebuf1,
- WOLFSSL_BIO **bio2_p, size_t writebuf2)
-{
- WOLFSSL_BIO *bio1 = NULL, *bio2 = NULL;
- int ret = 1;
-
- WOLFSSL_ENTER("wolfSSL_BIO_new_bio_pair()");
-
- if (bio1_p == NULL || bio2_p == NULL) {
- WOLFSSL_MSG("Bad Function Argument");
- return BAD_FUNC_ARG;
- }
-
- /* set up the new bio structures and write buf sizes */
- if ((bio1 = wolfSSL_BIO_new(wolfSSL_BIO_s_bio())) == NULL) {
- WOLFSSL_MSG("Bio allocation failed");
- ret = WOLFSSL_FAILURE;
- }
- if (ret) {
- if ((bio2 = wolfSSL_BIO_new(wolfSSL_BIO_s_bio())) == NULL) {
- WOLFSSL_MSG("Bio allocation failed");
- ret = WOLFSSL_FAILURE;
- }
- }
- if (ret && writebuf1) {
- if (!(ret = wolfSSL_BIO_set_write_buf_size(bio1, writebuf1))) {
- WOLFSSL_MSG("wolfSSL_BIO_set_write_buf() failure");
- }
- }
- if (ret && writebuf2) {
- if (!(ret = wolfSSL_BIO_set_write_buf_size(bio2, writebuf2))) {
- WOLFSSL_MSG("wolfSSL_BIO_set_write_buf() failure");
- }
- }
-
- if (ret) {
- if ((ret = wolfSSL_BIO_make_bio_pair(bio1, bio2))) {
- *bio1_p = bio1;
- *bio2_p = bio2;
- }
- }
- if (!ret) {
- wolfSSL_BIO_free(bio1);
- bio1 = NULL;
- wolfSSL_BIO_free(bio2);
- bio2 = NULL;
- }
- return ret;
-}
-
-
-#if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(NO_RSA) && !defined(HAVE_USER_RSA)
-/* Converts an rsa key from a bio buffer into an internal rsa structure.
-Returns a pointer to the new WOLFSSL_RSA structure. */
-WOLFSSL_RSA* wolfSSL_d2i_RSAPrivateKey_bio(WOLFSSL_BIO *bio, WOLFSSL_RSA **out)
-{
- const unsigned char* bioMem = NULL;
- int bioMemSz = 0;
- WOLFSSL_RSA* key = NULL;
- unsigned char maxKeyBuf[4096];
- unsigned char* bufPtr = NULL;
- unsigned char* extraBioMem = NULL;
- int extraBioMemSz = 0;
- int derLength = 0;
- int j = 0, i = 0;
-
- WOLFSSL_ENTER("wolfSSL_d2i_RSAPrivateKey_bio()");
-
- if (bio == NULL) {
- WOLFSSL_MSG("Bad Function Argument");
- return NULL;
- }
- (void)out;
-
- bioMemSz = wolfSSL_BIO_pending(bio);
- if (bioMemSz <= 0) {
- WOLFSSL_MSG("wolfSSL_BIO_pending() failure");
- return NULL;
- }
-
- bioMem = (unsigned char*)XMALLOC(bioMemSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (bioMem == NULL) {
- WOLFSSL_MSG("Malloc failure");
- return NULL;
- }
-
- bufPtr = maxKeyBuf;
- if (wolfSSL_BIO_read(bio, (unsigned char*)bioMem, (int)bioMemSz) == bioMemSz) {
- const byte* bioMemPt = bioMem; /* leave bioMem pointer unaltered */
- if ((key = wolfSSL_d2i_RSAPrivateKey(NULL, &bioMemPt, bioMemSz)) == NULL) {
- XFREE((unsigned char*)bioMem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return NULL;
- }
-
- /* This function is used to get the total length of the rsa key. */
- derLength = wolfSSL_i2d_RSAPrivateKey(key, &bufPtr);
-
- /* Write extra data back into bio object if necessary. */
- extraBioMemSz = (bioMemSz - derLength);
- if (extraBioMemSz > 0) {
- extraBioMem = (unsigned char *)XMALLOC(extraBioMemSz, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (extraBioMem == NULL) {
- WOLFSSL_MSG("Malloc failure");
- XFREE((unsigned char*)extraBioMem, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- XFREE((unsigned char*)bioMem, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- return NULL;
- }
-
- for (i = derLength; i < bioMemSz; i++) {
- *(extraBioMem + j) = *(bioMem + i);
- j++;
- }
-
- wolfSSL_BIO_write(bio, extraBioMem, extraBioMemSz);
- if (wolfSSL_BIO_pending(bio) <= 0) {
- WOLFSSL_MSG("Failed to write memory to bio");
- XFREE((unsigned char*)extraBioMem, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- XFREE((unsigned char*)bioMem, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- return NULL;
- }
- XFREE((unsigned char*)extraBioMem, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- if (out != NULL && key != NULL) {
- *out = key;
- }
- }
- XFREE((unsigned char*)bioMem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return key;
-}
-#endif
-
-
-/* Adds the ASN1 certificate to the user ctx.
-Returns WOLFSSL_SUCCESS if no error, returns WOLFSSL_FAILURE otherwise.*/
-int wolfSSL_CTX_use_certificate_ASN1(WOLFSSL_CTX *ctx, int derSz,
- const unsigned char *der)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_ASN1()");
- if (der != NULL && ctx != NULL) {
- if (wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
- WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS) {
- return WOLFSSL_SUCCESS;
- }
-
- }
- return WOLFSSL_FAILURE;
-}
-
-
-#if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(NO_RSA) && !defined(HAVE_USER_RSA)
-/* Adds the rsa private key to the user ctx.
-Returns WOLFSSL_SUCCESS if no error, returns WOLFSSL_FAILURE otherwise.*/
-int wolfSSL_CTX_use_RSAPrivateKey(WOLFSSL_CTX* ctx, WOLFSSL_RSA* rsa)
-{
- int ret;
- int derSize;
- unsigned char maxDerBuf[4096];
- unsigned char* key = NULL;
-
- WOLFSSL_ENTER("wolfSSL_CTX_use_RSAPrivateKey()");
-
- if (ctx == NULL || rsa == NULL) {
- WOLFSSL_MSG("one or more inputs were NULL");
- return BAD_FUNC_ARG;
- }
- key = maxDerBuf;
- /* convert RSA struct to der encoded buffer and get the size */
- if ((derSize = wolfSSL_i2d_RSAPrivateKey(rsa, &key)) <= 0) {
- WOLFSSL_MSG("wolfSSL_i2d_RSAPrivateKey() failure");
- return WOLFSSL_FAILURE;
- }
- ret = wolfSSL_CTX_use_PrivateKey_buffer(ctx, (const unsigned char*)maxDerBuf,
- derSize, SSL_FILETYPE_ASN1);
- if (ret != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("wolfSSL_CTX_USE_PrivateKey_buffer() failure");
- return WOLFSSL_FAILURE;
- }
- return ret;
-}
-#endif /* NO_RSA && !HAVE_FAST_RSA */
-
-
-/* Converts EVP_PKEY data from a bio buffer to a WOLFSSL_EVP_PKEY structure.
-Returns pointer to private EVP_PKEY struct upon success, NULL if there
-is a failure.*/
-WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey_bio(WOLFSSL_BIO* bio,
- WOLFSSL_EVP_PKEY** out)
-{
- unsigned char* mem = NULL;
- int memSz = 0;
- WOLFSSL_EVP_PKEY* key = NULL;
- int i = 0, j = 0;
- unsigned char* extraBioMem = NULL;
- int extraBioMemSz = 0;
- int derLength = 0;
-
- WOLFSSL_ENTER("wolfSSL_d2i_PrivateKey_bio()");
-
- if (bio == NULL) {
- return NULL;
- }
- (void)out;
-
- memSz = wolfSSL_BIO_pending(bio);
- if (memSz <= 0) {
- WOLFSSL_MSG("wolfSSL_BIO_pending() failure");
- return NULL;
- }
-
- mem = (unsigned char*)XMALLOC(memSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (mem == NULL) {
- WOLFSSL_MSG("Malloc failure");
- return NULL;
- }
-
- if (wolfSSL_BIO_read(bio, (unsigned char*)mem, memSz) == memSz) {
- /* Determines key type and returns the new private EVP_PKEY object */
- if ((key = wolfSSL_d2i_PrivateKey_EVP(NULL, &mem, (long)memSz)) == NULL) {
- WOLFSSL_MSG("wolfSSL_d2i_PrivateKey_EVP() failure");
- XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return NULL;
- }
-
- /* Write extra data back into bio object if necessary. */
- derLength = key->pkey_sz;
- extraBioMemSz = (memSz - derLength);
- if (extraBioMemSz > 0) {
- extraBioMem = (unsigned char *)XMALLOC(extraBioMemSz, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (extraBioMem == NULL) {
- WOLFSSL_MSG("Malloc failure");
- XFREE((unsigned char*)extraBioMem, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return NULL;
- }
-
- for (i = derLength; i < memSz; i++) {
- *(extraBioMem + j) = *(mem + i);
- j++;
- }
-
- wolfSSL_BIO_write(bio, extraBioMem, extraBioMemSz);
- if (wolfSSL_BIO_pending(bio) <= 0) {
- WOLFSSL_MSG("Failed to write memory to bio");
- XFREE((unsigned char*)extraBioMem, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return NULL;
- }
- XFREE((unsigned char*)extraBioMem, bio->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- if (out != NULL) {
- *out = key;
- }
- }
- XFREE(mem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return key;
-}
-
-
-/* Converts a DER encoded private key to a WOLFSSL_EVP_PKEY structure.
- * returns a pointer to a new WOLFSSL_EVP_PKEY structure on success and NULL
- * on fail */
-WOLFSSL_EVP_PKEY* wolfSSL_d2i_PrivateKey_EVP(WOLFSSL_EVP_PKEY** out,
- unsigned char** in, long inSz)
-{
- WOLFSSL_EVP_PKEY* pkey = NULL;
- const unsigned char* mem;
- long memSz = inSz;
-
- WOLFSSL_ENTER("wolfSSL_d2i_PrivateKey_EVP()");
-
- if (in == NULL || *in == NULL || inSz < 0) {
- WOLFSSL_MSG("Bad argument");
- return NULL;
- }
- mem = *in;
-
- #if !defined(NO_RSA)
- {
- RsaKey rsa;
- word32 keyIdx = 0;
-
- /* test if RSA key */
- if (wc_InitRsaKey(&rsa, NULL) == 0 &&
- wc_RsaPrivateKeyDecode(mem, &keyIdx, &rsa, (word32)memSz) == 0) {
- wc_FreeRsaKey(&rsa);
- pkey = wolfSSL_EVP_PKEY_new();
- if (pkey != NULL) {
- pkey->pkey_sz = keyIdx;
- pkey->pkey.ptr = (char*)XMALLOC(memSz, NULL,
- DYNAMIC_TYPE_PRIVATE_KEY);
- if (pkey->pkey.ptr == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
- XMEMCPY(pkey->pkey.ptr, mem, keyIdx);
- pkey->type = EVP_PKEY_RSA;
- if (out != NULL) {
- *out = pkey;
- }
-
- pkey->ownRsa = 1;
- pkey->rsa = wolfSSL_RSA_new();
- if (pkey->rsa == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
-
- if (wolfSSL_RSA_LoadDer_ex(pkey->rsa,
- (const unsigned char*)pkey->pkey.ptr,
- pkey->pkey_sz, WOLFSSL_RSA_LOAD_PRIVATE) != 1) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
-
- return pkey;
- }
- }
- wc_FreeRsaKey(&rsa);
- }
- #endif /* NO_RSA */
-
- #ifdef HAVE_ECC
- {
- word32 keyIdx = 0;
- ecc_key ecc;
-
- /* test if ecc key */
- if (wc_ecc_init(&ecc) == 0 &&
- wc_EccPrivateKeyDecode(mem, &keyIdx, &ecc, (word32)memSz) == 0) {
- wc_ecc_free(&ecc);
- pkey = wolfSSL_EVP_PKEY_new();
- if (pkey != NULL) {
- pkey->pkey_sz = keyIdx;
- pkey->pkey.ptr = (char*)XMALLOC(keyIdx, NULL,
- DYNAMIC_TYPE_PRIVATE_KEY);
- if (pkey->pkey.ptr == NULL) {
- wolfSSL_EVP_PKEY_free(pkey);
- return NULL;
- }
- XMEMCPY(pkey->pkey.ptr, mem, keyIdx);
- pkey->type = EVP_PKEY_EC;
- if (out != NULL) {
- *out = pkey;
- }
- return pkey;
- }
- }
- wc_ecc_free(&ecc);
- }
- #endif /* HAVE_ECC */
- return pkey;
-}
-#endif /* OPENSSL_ALL || WOLFSSL_ASIO || WOLFSSL_HAPROXY || WOLFSSL_QT */
-
-
-/* stunnel compatibility functions*/
-#if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && (defined(HAVE_STUNNEL) || \
- defined(WOLFSSL_NGINX) || defined(HAVE_LIGHTY) || \
- defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_OPENSSH)))
-void wolfSSL_ERR_remove_thread_state(void* pid)
-{
- (void) pid;
- return;
-}
-
-#ifndef NO_FILESYSTEM
-/***TBD ***/
-void wolfSSL_print_all_errors_fp(XFILE fp)
-{
- (void)fp;
-}
-#endif
-
-int wolfSSL_SESSION_set_ex_data(WOLFSSL_SESSION* session, int idx, void* data)
-{
- WOLFSSL_ENTER("wolfSSL_SESSION_set_ex_data");
-#ifdef HAVE_EX_DATA
- if(session != NULL) {
- return wolfSSL_CRYPTO_set_ex_data(&session->ex_data, idx, data);
- }
-#else
- (void)session;
- (void)idx;
- (void)data;
-#endif
- return WOLFSSL_FAILURE;
-}
-
-
-int wolfSSL_SESSION_get_ex_new_index(long idx, void* data, void* cb1,
- void* cb2, CRYPTO_free_func* cb3)
-{
- WOLFSSL_ENTER("wolfSSL_SESSION_get_ex_new_index");
- (void)idx;
- (void)cb1;
- (void)cb2;
- (void)cb3;
- if (XSTRNCMP((const char*)data, "redirect index", 14) == 0) {
- return 0;
- }
- else if (XSTRNCMP((const char*)data, "addr index", 10) == 0) {
- return 1;
- }
- return WOLFSSL_FAILURE;
-}
-
-
-void* wolfSSL_SESSION_get_ex_data(const WOLFSSL_SESSION* session, int idx)
-{
- WOLFSSL_ENTER("wolfSSL_SESSION_get_ex_data");
-#ifdef HAVE_EX_DATA
- if (session != NULL) {
- return wolfSSL_CRYPTO_get_ex_data(&session->ex_data, idx);
- }
-#else
- (void)session;
- (void)idx;
-#endif
- return NULL;
-}
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_CRYPTO_set_mem_ex_functions(void *(*m) (size_t, const char *, int),
- void *(*r) (void *, size_t, const char *,
- int), void (*f) (void *))
-{
- (void) m;
- (void) r;
- (void) f;
- WOLFSSL_ENTER("wolfSSL_CRYPTO_set_mem_ex_functions");
- WOLFSSL_STUB("CRYPTO_set_mem_ex_functions");
-
- return WOLFSSL_FAILURE;
-}
-#endif
-
-
-void wolfSSL_CRYPTO_cleanup_all_ex_data(void){
- WOLFSSL_ENTER("CRYPTO_cleanup_all_ex_data");
-}
-
-
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_DH *wolfSSL_DH_generate_parameters(int prime_len, int generator,
- void (*callback) (int, int, void *), void *cb_arg)
-{
- (void)prime_len;
- (void)generator;
- (void)callback;
- (void)cb_arg;
- WOLFSSL_ENTER("wolfSSL_DH_generate_parameters");
- WOLFSSL_STUB("DH_generate_parameters");
-
- return NULL;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_DH_generate_parameters_ex(WOLFSSL_DH* dh, int prime_len, int generator,
- void (*callback) (int, int, void *))
-{
- (void)prime_len;
- (void)generator;
- (void)callback;
- (void)dh;
- WOLFSSL_ENTER("wolfSSL_DH_generate_parameters_ex");
- WOLFSSL_STUB("DH_generate_parameters_ex");
-
- return -1;
-}
-#endif
-
-void wolfSSL_ERR_load_crypto_strings(void)
-{
- WOLFSSL_ENTER("wolfSSL_ERR_load_crypto_strings");
- /* Do nothing */
- return;
-}
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_FIPS_mode(void)
-{
- WOLFSSL_ENTER("wolfSSL_FIPS_mode");
- WOLFSSL_STUB("FIPS_mode");
-
- return WOLFSSL_FAILURE;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_FIPS_mode_set(int r)
-{
- (void)r;
- WOLFSSL_ENTER("wolfSSL_FIPS_mode_set");
- WOLFSSL_STUB("FIPS_mode_set");
-
- return WOLFSSL_FAILURE;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_RAND_set_rand_method(const void *meth)
-{
- (void) meth;
- WOLFSSL_ENTER("wolfSSL_RAND_set_rand_method");
- WOLFSSL_STUB("RAND_set_rand_method");
-
- /* if implemented RAND_bytes and RAND_pseudo_bytes need updated
- * those two functions will call the respective functions from meth */
- return SSL_FAILURE;
-}
-#endif
-
-int wolfSSL_CIPHER_get_bits(const WOLFSSL_CIPHER *c, int *alg_bits)
-{
- int ret = WOLFSSL_FAILURE;
- WOLFSSL_ENTER("wolfSSL_CIPHER_get_bits");
-
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- (void)alg_bits;
- if (c!= NULL)
- ret = c->bits;
- #else
- if (c != NULL && c->ssl != NULL) {
- ret = 8 * c->ssl->specs.key_size;
- if (alg_bits != NULL) {
- *alg_bits = ret;
- }
- }
- #endif
- return ret;
-}
-
-#if defined(OPENSSL_ALL)
-WOLFSSL_X509_INFO* wolfSSL_X509_INFO_new(void)
-{
- WOLFSSL_X509_INFO* info;
- info = (WOLFSSL_X509_INFO*)XMALLOC(sizeof(WOLFSSL_X509_INFO), NULL,
- DYNAMIC_TYPE_X509);
- if (info) {
- XMEMSET(info, 0, sizeof(*info));
- }
- return info;
-}
-
-void wolfSSL_X509_INFO_free(WOLFSSL_X509_INFO* info)
-{
- if (info == NULL)
- return;
-
- if (info->x509) {
- wolfSSL_X509_free(info->x509);
- info->x509 = NULL;
- }
-#ifdef HAVE_CRL
- if (info->crl) {
- wolfSSL_X509_CRL_free(info->crl);
- info->crl = NULL;
- }
-#endif
- wolfSSL_X509_PKEY_free(info->x_pkey);
- info->x_pkey = NULL;
-
- XFREE(info, NULL, DYNAMIC_TYPE_X509);
-}
-#endif
-
-WOLFSSL_STACK* wolfSSL_sk_X509_INFO_new_null(void)
-{
- WOLFSSL_STACK* sk = wolfSSL_sk_new_node(NULL);
- if (sk) {
- sk->type = STACK_TYPE_X509_INFO;
- }
- return sk;
-}
-
-
-/* returns value less than 0 on fail to match
- * On a successful match the priority level found is returned
- */
-int wolfSSL_sk_SSL_CIPHER_find(
- WOLF_STACK_OF(WOLFSSL_CIPHER)* sk, const WOLFSSL_CIPHER* toFind)
-{
- WOLFSSL_STACK* next;
- int i, sz;
-
- if (sk == NULL || toFind == NULL) {
- return WOLFSSL_FATAL_ERROR;
- }
-
- sz = wolfSSL_sk_SSL_CIPHER_num(sk);
- next = sk;
- for (i = 0; i < sz && next != NULL; i++) {
- if (next->data.cipher.cipherSuite0 == toFind->cipherSuite0 &&
- next->data.cipher.cipherSuite == toFind->cipherSuite) {
- return sz - i; /* reverse because stack pushed highest on first */
- }
- next = next->next;
- }
- return WOLFSSL_FATAL_ERROR;
-}
-
-
-/* copies over data of "in" to "out" */
-static void wolfSSL_CIPHER_copy(WOLFSSL_CIPHER* in, WOLFSSL_CIPHER* out)
-{
- if (in == NULL || out == NULL)
- return;
-
- out->cipherSuite = in->cipherSuite;
- out->cipherSuite0 = in->cipherSuite0;
-}
-
-
-/* create duplicate of stack and return the new stack
- * returns null on failure */
-WOLF_STACK_OF(WOLFSSL_CIPHER)* wolfSSL_sk_SSL_CIPHER_dup(
- WOLF_STACK_OF(WOLFSSL_CIPHER)* in)
-{
- WOLFSSL_STACK* current;
- WOLF_STACK_OF(WOLFSSL_CIPHER)* ret = NULL;
- int i, sz;
-
- sz = wolfSSL_sk_SSL_CIPHER_num(in);
- current = in;
- for (i = 0; i < sz && current != NULL; i++) {
- WOLFSSL_STACK* add = wolfSSL_sk_new_node(in->heap);
- if (add != NULL) {
- add->type = STACK_TYPE_CIPHER;
- wolfSSL_CIPHER_copy(&(current->data.cipher), &(add->data.cipher));
- add->num = i+1;
- add->next = ret;
- ret = add;
- current = current->next;
- }
- }
- return ret;
-}
-
-/* nothing to do yet */
-static void wolfSSL_CIPHER_free(WOLFSSL_CIPHER* in)
-{
- (void)in;
-}
-
-
-/* free's all nodes in the stack and there data */
-void wolfSSL_sk_SSL_CIPHER_free(WOLF_STACK_OF(WOLFSSL_CIPHER)* sk)
-{
- WOLFSSL_STACK* current = sk;
-
- while (current != NULL) {
- WOLFSSL_STACK* toFree = current;
- current = current->next;
-
- wolfSSL_CIPHER_free(&(toFree->data.cipher));
- wolfSSL_sk_free_node(toFree);
- }
-}
-
-
-int wolfSSL_sk_X509_INFO_num(const WOLF_STACK_OF(WOLFSSL_X509_INFO) *sk)
-{
- WOLFSSL_ENTER("wolfSSL_sk_X509_INFO_num");
-
- if (sk == NULL)
- return -1;
- return (int)sk->num;
-}
-
-WOLFSSL_X509_INFO* wolfSSL_sk_X509_INFO_value(const WOLF_STACK_OF(WOLFSSL_X509_INFO) *sk, int i)
-{
- WOLFSSL_ENTER("wolfSSL_sk_X509_INFO_value");
-
- for (; sk != NULL && i > 0; i--)
- sk = sk->next;
-
- if (i != 0 || sk == NULL)
- return NULL;
- return sk->data.info;
-}
-
-WOLFSSL_X509_INFO* wolfSSL_sk_X509_INFO_pop(WOLF_STACK_OF(WOLFSSL_X509_INFO)* sk)
-{
- WOLFSSL_STACK* node;
- WOLFSSL_X509_INFO* info;
-
- if (sk == NULL) {
- return NULL;
- }
-
- node = sk->next;
- info = sk->data.info;
-
- if (node != NULL) { /* update sk and remove node from stack */
- sk->data.info = node->data.info;
- sk->next = node->next;
- XFREE(node, NULL, DYNAMIC_TYPE_OPENSSL);
- }
- else { /* last x509 in stack */
- sk->data.info = NULL;
- }
-
- if (sk->num > 0) {
- sk->num -= 1;
- }
-
- return info;
-}
-
-#if defined(OPENSSL_ALL)
-void wolfSSL_sk_X509_INFO_pop_free(WOLF_STACK_OF(WOLFSSL_X509_INFO)* sk,
- void (*f) (WOLFSSL_X509_INFO*))
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_X509_INFO_pop_free");
-
- if (sk == NULL) {
- return;
- }
-
- /* parse through stack freeing each node */
- node = sk->next;
- while (node && sk->num > 1) {
- WOLFSSL_STACK* tmp = node;
- node = node->next;
-
- if (f)
- f(tmp->data.info);
- else
- wolfSSL_X509_INFO_free(tmp->data.info);
- tmp->data.info = NULL;
- XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
- sk->num -= 1;
- }
-
- /* free head of stack */
- if (sk->num == 1) {
- if (f)
- f(sk->data.info);
- else
- wolfSSL_X509_INFO_free(sk->data.info);
- sk->data.info = NULL;
- }
- XFREE(sk, NULL, DYNAMIC_TYPE_OPENSSL);
-}
-
-void wolfSSL_sk_X509_INFO_free(WOLF_STACK_OF(WOLFSSL_X509_INFO) *sk)
-{
- wolfSSL_sk_X509_INFO_pop_free(sk, NULL);
-}
-
-
-/* Adds the WOLFSSL_X509_INFO to the stack "sk". "sk" takes control of "in" and
- * tries to free it when the stack is free'd.
- *
- * return 1 on success 0 on fail
- */
-int wolfSSL_sk_X509_INFO_push(WOLF_STACK_OF(WOLFSSL_X509_INFO)* sk,
- WOLFSSL_X509_INFO* in)
-{
- WOLFSSL_STACK* node;
-
- if (sk == NULL || in == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* no previous values in stack */
- if (sk->data.info == NULL) {
- sk->data.info = in;
- sk->num += 1;
- return WOLFSSL_SUCCESS;
- }
-
- /* stack already has value(s) create a new node and add more */
- node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_X509);
- if (node == NULL) {
- WOLFSSL_MSG("Memory error");
- return WOLFSSL_FAILURE;
- }
- XMEMSET(node, 0, sizeof(WOLFSSL_STACK));
-
- /* push new obj onto head of stack */
- node->data.info = sk->data.info;
- node->next = sk->next;
- node->type = sk->type;
- sk->next = node;
- sk->data.info = in;
- sk->num += 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-WOLF_STACK_OF(WOLFSSL_X509_NAME)* wolfSSL_sk_X509_NAME_new(wolf_sk_compare_cb cb)
-{
- WOLFSSL_STACK* sk;
-
- WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_new");
-
- sk = wolfSSL_sk_new_node(NULL);
- if (sk != NULL) {
- sk->type = STACK_TYPE_X509_NAME;
- sk->comp = cb;
- }
-
- return sk;
-}
-
-int wolfSSL_sk_X509_NAME_push(WOLF_STACK_OF(WOLFSSL_X509_NAME)* sk,
- WOLFSSL_X509_NAME* name)
-{
- WOLFSSL_STACK* node;
-
- WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_push");
-
- if (sk == NULL || name == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* no previous values in stack */
- if (sk->data.name == NULL) {
- sk->data.name = name;
- sk->num += 1;
- return 0;
- }
-
- /* stack already has value(s) create a new node and add more */
- node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (node == NULL) {
- WOLFSSL_MSG("Memory error");
- return MEMORY_E;
- }
- XMEMSET(node, 0, sizeof(WOLFSSL_STACK));
-
- /* push new obj onto head of stack */
- node->data.name = sk->data.name;
- node->next = sk->next;
- sk->type = STACK_TYPE_X509_NAME;
- sk->next = node;
- sk->data.name = name;
- sk->num += 1;
-
- return 0;
-}
-
-/* return index of found, or negative to indicate not found */
-int wolfSSL_sk_X509_NAME_find(const WOLF_STACK_OF(WOLFSSL_X509_NAME) *sk,
- WOLFSSL_X509_NAME *name)
-{
- int i;
-
- WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_find");
-
- if (sk == NULL)
- return BAD_FUNC_ARG;
-
- for (i = 0; sk; i++, sk = sk->next) {
- if (wolfSSL_X509_NAME_cmp(sk->data.name, name) == 0) {
- return i;
- }
- }
- return -1;
-}
-
-int wolfSSL_sk_X509_OBJECT_num(const WOLF_STACK_OF(WOLFSSL_X509_OBJECT) *s)
-{
- WOLFSSL_ENTER("wolfSSL_sk_X509_OBJECT_num");
- if (s) {
- return (int)s->num;
- } else {
- return 0;
- }
-}
-
-
-int wolfSSL_sk_X509_NAME_set_cmp_func(WOLF_STACK_OF(WOLFSSL_X509_NAME)* sk,
- wolf_sk_compare_cb cb)
-{
- WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_set_cmp_func");
-
- if (sk == NULL)
- return BAD_FUNC_ARG;
-
- sk->comp = cb;
- return 0;
-}
-#endif /* OPENSSL_ALL */
-
-int wolfSSL_sk_X509_NAME_num(const WOLF_STACK_OF(WOLFSSL_X509_NAME) *sk)
-{
- WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_num");
-
- if (sk == NULL)
- return BAD_FUNC_ARG;
-
- return (int)sk->num;
-}
-
-/* Getter function for WOLFSSL_X509_NAME pointer
- *
- * sk is the stack to retrieve pointer from
- * i is the index value in stack
- *
- * returns a pointer to a WOLFSSL_X509_NAME structure on success and NULL on
- * fail
- */
-WOLFSSL_X509_NAME* wolfSSL_sk_X509_NAME_value(const STACK_OF(WOLFSSL_X509_NAME)* sk,
- int i)
-{
- WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_value");
-
- for (; sk != NULL && i > 0; i--) {
- sk = sk->next;
- }
-
- if (i != 0 || sk == NULL)
- return NULL;
-
- return sk->data.name;
-}
-
-WOLFSSL_X509_NAME* wolfSSL_sk_X509_NAME_pop(WOLF_STACK_OF(WOLFSSL_X509_NAME)* sk)
-{
- WOLFSSL_STACK* node;
- WOLFSSL_X509_NAME* name;
-
- if (sk == NULL) {
- return NULL;
- }
-
- node = sk->next;
- name = sk->data.name;
-
- if (node != NULL) { /* update sk and remove node from stack */
- sk->data.name = node->data.name;
- sk->next = node->next;
- XFREE(node, NULL, DYNAMIC_TYPE_OPENSSL);
- }
- else { /* last x509 in stack */
- sk->data.name = NULL;
- }
-
- if (sk->num > 0) {
- sk->num -= 1;
- }
-
- return name;
-}
-
-void wolfSSL_sk_X509_NAME_pop_free(WOLF_STACK_OF(WOLFSSL_X509_NAME)* sk,
- void (*f) (WOLFSSL_X509_NAME*))
-{
- WOLFSSL_STACK* node;
- WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_pop_free");
-
- if (sk == NULL)
- return;
-
- node = sk->next;
- while (node && sk->num > 1) {
- WOLFSSL_STACK* tmp = node;
- node = node->next;
- if (f)
- f(tmp->data.name);
- else
- wolfSSL_X509_NAME_free(tmp->data.name);
- tmp->data.name = NULL;
- XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
- sk->num -= 1;
- }
-
- /* free head of stack */
- if (sk->num == 1) {
- if (f)
- f(sk->data.name);
- else
- wolfSSL_X509_NAME_free(sk->data.name);
- sk->data.name = NULL;
- }
-
- XFREE(sk, sk->heap, DYNAMIC_TYPE_OPENSSL);
-}
-
-/* Free only the sk structure, NOT X509_NAME members */
-void wolfSSL_sk_X509_NAME_free(WOLF_STACK_OF(WOLFSSL_X509_NAME)* sk)
-{
- WOLFSSL_STACK* node;
- WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_free");
-
- if (sk == NULL)
- return;
-
- node = sk->next;
- while (sk->num > 1) {
- WOLFSSL_STACK* tmp = node;
- node = node->next;
- XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
- sk->num -= 1;
- }
-
- XFREE(sk, sk->heap, DYNAMIC_TYPE_OPENSSL);
-}
-
-#if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
-/* Helper function for X509_NAME_print_ex. Sets *buf to string for domain
- name attribute based on NID. Returns size of buf */
-static int get_dn_attr_by_nid(int n, const char** buf)
-{
- int len = 0;
- const char *str;
-
- switch(n)
- {
- case NID_commonName :
- str = "CN";
- len = 2;
- break;
- case NID_countryName:
- str = "C";
- len = 1;
- break;
- case NID_localityName:
- str = "L";
- len = 1;
- break;
- case NID_stateOrProvinceName:
- str = "ST";
- len = 2;
- break;
- case NID_organizationName:
- str = "O";
- len = 1;
- break;
- case NID_organizationalUnitName:
- str = "OU";
- len = 2;
- break;
- case NID_emailAddress:
- str = "emailAddress";
- len = 12;
- break;
- default:
- WOLFSSL_MSG("Attribute type not found");
- str = NULL;
-
- }
- if (buf != NULL)
- *buf = str;
- return len;
-}
-#endif
-
-/*
- * The BIO output of wolfSSL_X509_NAME_print_ex does NOT include the null terminator
- */
-int wolfSSL_X509_NAME_print_ex(WOLFSSL_BIO* bio, WOLFSSL_X509_NAME* name,
- int indent, unsigned long flags)
-{
-#if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
- int count = 0, len = 0, totalSz = 0, tmpSz = 0;
- char tmp[ASN_NAME_MAX];
- char fullName[ASN_NAME_MAX];
- const char *buf = NULL;
- WOLFSSL_X509_NAME_ENTRY* ne;
- WOLFSSL_ASN1_STRING* str;
-#endif
- int i;
- (void)flags;
- WOLFSSL_ENTER("wolfSSL_X509_NAME_print_ex");
-
- for (i = 0; i < indent; i++) {
- if (wolfSSL_BIO_write(bio, " ", 1) != 1)
- return WOLFSSL_FAILURE;
- }
-
-#if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
- /* If XN_FLAG_DN_REV is present, print X509_NAME in reverse order */
- if (flags == (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)) {
- fullName[0] = '\0';
- count = wolfSSL_X509_NAME_entry_count(name);
- for (i = 0; i < count; i++) {
- ne = wolfSSL_X509_NAME_get_entry(name, count - i - 1);
- if (ne == NULL)
- return WOLFSSL_FAILURE;
-
- str = wolfSSL_X509_NAME_ENTRY_get_data(ne);
- if (str == NULL)
- return WOLFSSL_FAILURE;
-
- len = get_dn_attr_by_nid(ne->nid, &buf);
- if (len == 0 || buf == NULL)
- return WOLFSSL_FAILURE;
-
- tmpSz = str->length + len + 2; /* + 2 for '=' and comma */
- if (tmpSz > ASN_NAME_MAX) {
- WOLFSSL_MSG("Size greater than ASN_NAME_MAX");
- return WOLFSSL_FAILURE;
- }
-
- if (i < count - 1) {
- /* tmpSz+1 for last null char */
- XSNPRINTF(tmp, tmpSz+1, "%s=%s,", buf, str->data);
- XSTRNCAT(fullName, tmp, tmpSz);
- }
- else {
- XSNPRINTF(tmp, tmpSz, "%s=%s", buf, str->data);
- XSTRNCAT(fullName, tmp, tmpSz-1);
- tmpSz--; /* Don't include null char in tmpSz */
- }
- totalSz += tmpSz;
- }
- if (wolfSSL_BIO_write(bio, fullName, totalSz) != totalSz)
- return WOLFSSL_FAILURE;
- return WOLFSSL_SUCCESS;
- }
-#else
- if (flags == XN_FLAG_RFC2253) {
- if (wolfSSL_BIO_write(bio, name->name + 1, name->sz - 2)
- != name->sz - 2)
- return WOLFSSL_FAILURE;
- }
-#endif /* WOLFSSL_APACHE_HTTPD || OPENSSL_ALL || WOLFSSL_NGINX */
- else if (wolfSSL_BIO_write(bio, name->name, name->sz - 1) != name->sz - 1)
- return WOLFSSL_FAILURE;
-
- return WOLFSSL_SUCCESS;
-}
-
-#ifndef NO_WOLFSSL_STUB
-WOLFSSL_ASN1_BIT_STRING* wolfSSL_X509_get0_pubkey_bitstr(const WOLFSSL_X509* x)
-{
- (void)x;
- WOLFSSL_ENTER("wolfSSL_X509_get0_pubkey_bitstr");
- WOLFSSL_STUB("X509_get0_pubkey_bitstr");
-
- return NULL;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_CTX_add_session(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* session)
-{
- (void)ctx;
- (void)session;
- WOLFSSL_ENTER("wolfSSL_CTX_add_session");
- WOLFSSL_STUB("SSL_CTX_add_session");
-
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-
-int wolfSSL_version(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_version");
- if (ssl->version.major == SSLv3_MAJOR) {
- switch (ssl->version.minor) {
- case SSLv3_MINOR :
- return SSL3_VERSION;
- case TLSv1_MINOR :
- return TLS1_VERSION;
- case TLSv1_1_MINOR :
- return TLS1_1_VERSION;
- case TLSv1_2_MINOR :
- return TLS1_2_VERSION;
- case TLSv1_3_MINOR :
- return TLS1_3_VERSION;
- default:
- return WOLFSSL_FAILURE;
- }
- }
- else if (ssl->version.major == DTLS_MAJOR) {
- switch (ssl->version.minor) {
- case DTLS_MINOR :
- return DTLS1_VERSION;
- case DTLSv1_2_MINOR :
- return DTLS1_2_VERSION;
- default:
- return WOLFSSL_FAILURE;
- }
- }
- return WOLFSSL_FAILURE;
-}
-
-
-int wolfSSL_X509_NAME_get_sz(WOLFSSL_X509_NAME* name)
-{
- WOLFSSL_ENTER("wolfSSL_X509_NAME_get_sz");
- if(!name)
- return -1;
- return name->sz;
-}
-
-#ifdef HAVE_SNI
-int wolfSSL_set_tlsext_host_name(WOLFSSL* ssl, const char* host_name)
-{
- int ret;
- WOLFSSL_ENTER("wolfSSL_set_tlsext_host_name");
- ret = wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
- host_name, (word16)XSTRLEN(host_name));
- WOLFSSL_LEAVE("wolfSSL_set_tlsext_host_name", ret);
- return ret;
-}
-
-
-#ifndef NO_WOLFSSL_SERVER
-const char * wolfSSL_get_servername(WOLFSSL* ssl, byte type)
-{
- void * serverName = NULL;
- if (ssl == NULL)
- return NULL;
- TLSX_SNI_GetRequest(ssl->extensions, type, &serverName);
- return (const char *)serverName;
-}
-#endif /* NO_WOLFSSL_SERVER */
-#endif /* HAVE_SNI */
-
-WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
-{
- if (ssl && ctx && SetSSL_CTX(ssl, ctx, 0) == WOLFSSL_SUCCESS)
- return ssl->ctx;
- return NULL;
-}
-
-
-VerifyCallback wolfSSL_CTX_get_verify_callback(WOLFSSL_CTX* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_get_verify_callback");
- if(ctx)
- return ctx->verifyCallback;
- return NULL;
-}
-
-
-void wolfSSL_CTX_set_servername_callback(WOLFSSL_CTX* ctx, CallbackSniRecv cb)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_set_servername_callback");
- if (ctx)
- ctx->sniRecvCb = cb;
-}
-
-int wolfSSL_CTX_set_tlsext_servername_callback(WOLFSSL_CTX* ctx,
- CallbackSniRecv cb)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_set_tlsext_servername_callback");
- if (ctx) {
- ctx->sniRecvCb = cb;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-int wolfSSL_CTX_set_servername_arg(WOLFSSL_CTX* ctx, void* arg)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_set_servername_arg");
- if (ctx) {
- ctx->sniRecvCbArg = arg;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-void wolfSSL_ERR_load_BIO_strings(void) {
- WOLFSSL_ENTER("ERR_load_BIO_strings");
- /* do nothing */
-}
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_THREADID_set_callback(void(*threadid_func)(void*))
-{
- WOLFSSL_ENTER("wolfSSL_THREADID_set_callback");
- WOLFSSL_STUB("CRYPTO_THREADID_set_callback");
- (void)threadid_func;
- return;
-}
-#endif
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_THREADID_set_numeric(void* id, unsigned long val)
-{
- WOLFSSL_ENTER("wolfSSL_THREADID_set_numeric");
- WOLFSSL_STUB("CRYPTO_THREADID_set_numeric");
- (void)id;
- (void)val;
- return;
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_STUB
-WOLF_STACK_OF(WOLFSSL_X509)* wolfSSL_X509_STORE_get1_certs(
- WOLFSSL_X509_STORE_CTX* ctx, WOLFSSL_X509_NAME* name)
-{
- WOLFSSL_ENTER("wolfSSL_X509_STORE_get1_certs");
- WOLFSSL_STUB("X509_STORE_get1_certs");
- (void)ctx;
- (void)name;
- return NULL;
-}
-
-WOLF_STACK_OF(WOLFSSL_X509_OBJECT)* wolfSSL_X509_STORE_get0_objects(
- WOLFSSL_X509_STORE* store)
-{
- WOLFSSL_ENTER("wolfSSL_X509_STORE_get0_objects");
- WOLFSSL_STUB("wolfSSL_X509_STORE_get0_objects");
- (void)store;
- return NULL;
-}
-
-WOLFSSL_X509_OBJECT* wolfSSL_sk_X509_OBJECT_delete(
- WOLF_STACK_OF(WOLFSSL_X509_OBJECT)* sk, int i)
-{
- WOLFSSL_ENTER("wolfSSL_sk_X509_OBJECT_delete");
- WOLFSSL_STUB("wolfSSL_sk_X509_OBJECT_delete");
- (void)sk;
- (void)i;
- return NULL;
-}
-
-void wolfSSL_X509_OBJECT_free(WOLFSSL_X509_OBJECT *a)
-{
- WOLFSSL_ENTER("wolfSSL_X509_OBJECT_free");
- WOLFSSL_STUB("wolfSSL_X509_OBJECT_free");
- (void)a;
-}
-
-#endif
-
-
-#endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (HAVE_STUNNEL || WOLFSSL_NGINX || HAVE_LIGHTY)) */
-
-
-#if defined(OPENSSL_EXTRA)
-
-int wolfSSL_sk_X509_num(const WOLF_STACK_OF(WOLFSSL_X509) *s)
-{
- WOLFSSL_ENTER("wolfSSL_sk_X509_num");
-
- if (s == NULL)
- return -1;
- return (int)s->num;
-}
-
-unsigned long wolfSSL_ERR_peek_last_error(void)
-{
- WOLFSSL_ENTER("wolfSSL_ERR_peek_last_error");
-
-#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_NGINX)
- {
- int ret;
-
- if ((ret = wc_PeekErrorNode(-1, NULL, NULL, NULL)) < 0) {
- WOLFSSL_MSG("Issue peeking at error node in queue");
- return 0;
- }
- if (ret == -ASN_NO_PEM_HEADER)
- return (ERR_LIB_PEM << 24) | PEM_R_NO_START_LINE;
- return (unsigned long)ret;
- }
-#else
- return (unsigned long)(0 - NOT_COMPILED_IN);
-#endif
-}
-
-#endif /* OPENSSL_EXTRA */
-
-WOLFSSL_CTX* wolfSSL_get_SSL_CTX(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_get_SSL_CTX");
- return ssl->ctx;
-}
-
-#if defined(OPENSSL_ALL) || \
- defined(OPENSSL_EXTRA) || defined(HAVE_STUNNEL) || \
- defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
-
-const byte* wolfSSL_SESSION_get_id(WOLFSSL_SESSION* sess, unsigned int* idLen)
-{
- WOLFSSL_ENTER("wolfSSL_SESSION_get_id");
- if(!sess || !idLen) {
- WOLFSSL_MSG("Bad func args. Please provide idLen");
- return NULL;
- }
- *idLen = sess->sessionIDSz;
- return sess->sessionID;
-}
-
-#if (defined(HAVE_SESSION_TICKET) || defined(SESSION_CERTS)) && \
- !defined(NO_FILESYSTEM)
-
-#if defined(SESSION_CERTS) || \
- (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET))
-/* returns a pointer to the protocol used by the session */
-static const char* wolfSSL_SESSION_get_protocol(const WOLFSSL_SESSION* in)
-{
- return wolfSSL_internal_get_version((ProtocolVersion*)&in->version);
-}
-#endif
-
-/* returns true (non 0) if the session has EMS (extended master secret) */
-static int wolfSSL_SESSION_haveEMS(const WOLFSSL_SESSION* in)
-{
- if (in == NULL)
- return 0;
- return in->haveEMS;
-}
-
-#if defined(HAVE_SESSION_TICKET)
-/* prints out the ticket to bio passed in
- * return WOLFSSL_SUCCESS on success
- */
-static int wolfSSL_SESSION_print_ticket(WOLFSSL_BIO* bio,
- const WOLFSSL_SESSION* in, const char* tab)
-{
- unsigned short i, j, z, sz;
- short tag = 0;
- byte* pt;
-
-
- if (in == NULL || bio == NULL) {
- return BAD_FUNC_ARG;
- }
-
- sz = in->ticketLen;
- pt = in->ticket;
-
- if (wolfSSL_BIO_printf(bio, "%s\n", (sz == 0)? " NONE": "") <= 0)
- return WOLFSSL_FAILURE;
-
- for (i = 0; i < sz;) {
- char asc[16];
-
- if (sz - i < 16) {
- if (wolfSSL_BIO_printf(bio, "%s%04X -", tab, tag + (sz - i)) <= 0)
- return WOLFSSL_FAILURE;
- }
- else {
- if (wolfSSL_BIO_printf(bio, "%s%04X -", tab, tag) <= 0)
- return WOLFSSL_FAILURE;
- }
- for (j = 0; i < sz && j < 8; j++,i++) {
- asc[j] = ((pt[i])&0x6f)>='A'?((pt[i])&0x6f):'.';
- if (wolfSSL_BIO_printf(bio, " %02X", pt[i]) <= 0)
- return WOLFSSL_FAILURE;
- }
-
- if (i < sz) {
- asc[j] = ((pt[i])&0x6f)>='A'?((pt[i])&0x6f):'.';
- if (wolfSSL_BIO_printf(bio, "-%02X", pt[i]) <= 0)
- return WOLFSSL_FAILURE;
- j++;
- i++;
- }
-
- for (; i < sz && j < 16; j++,i++) {
- asc[j] = ((pt[i])&0x6f)>='A'?((pt[i])&0x6f):'.';
- if (wolfSSL_BIO_printf(bio, " %02X", pt[i]) <= 0)
- return WOLFSSL_FAILURE;
- }
-
- /* pad out spacing */
- for (z = j; z < 17; z++) {
- if (wolfSSL_BIO_printf(bio, " ") <= 0)
- return WOLFSSL_FAILURE;
- }
-
- for (z = 0; z < j; z++) {
- if (wolfSSL_BIO_printf(bio, "%c", asc[z]) <= 0)
- return WOLFSSL_FAILURE;
- }
- if (wolfSSL_BIO_printf(bio, "\n") <= 0)
- return WOLFSSL_FAILURE;
-
- tag += 16;
- }
- return WOLFSSL_SUCCESS;
-}
-#endif /* HAVE_SESSION_TICKET */
-
-
-/* prints out the session information in human readable form
- * return WOLFSSL_SUCCESS on success
- */
-int wolfSSL_SESSION_print(WOLFSSL_BIO *bp, const WOLFSSL_SESSION *x)
-{
- const unsigned char* pt;
- unsigned char buf[SECRET_LEN];
- unsigned int sz = 0, i;
- int ret;
- WOLFSSL_SESSION* session = (WOLFSSL_SESSION*)x;
-
- if (session == NULL) {
- WOLFSSL_MSG("Bad NULL argument");
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_BIO_printf(bp, "%s\n", "SSL-Session:") <= 0)
- return WOLFSSL_FAILURE;
-
-#if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
- defined(HAVE_SESSION_TICKET))
- if (wolfSSL_BIO_printf(bp, " Protocol : %s\n",
- wolfSSL_SESSION_get_protocol(session)) <= 0)
- return WOLFSSL_FAILURE;
-#endif
-
- if (wolfSSL_BIO_printf(bp, " Cipher : %s\n",
- wolfSSL_SESSION_CIPHER_get_name(session)) <= 0)
- return WOLFSSL_FAILURE;
-
- pt = wolfSSL_SESSION_get_id(session, &sz);
- if (wolfSSL_BIO_printf(bp, " Session-ID: ") <= 0)
- return WOLFSSL_FAILURE;
-
- for (i = 0; i < sz; i++) {
- if (wolfSSL_BIO_printf(bp, "%02X", pt[i]) <= 0)
- return WOLFSSL_FAILURE;
- }
- if (wolfSSL_BIO_printf(bp, "\n") <= 0)
- return WOLFSSL_FAILURE;
-
- if (wolfSSL_BIO_printf(bp, " Session-ID-ctx: \n") <= 0)
- return WOLFSSL_FAILURE;
-
- ret = wolfSSL_SESSION_get_master_key(x, buf, sizeof(buf));
- if (wolfSSL_BIO_printf(bp, " Master-Key: ") <= 0)
- return WOLFSSL_FAILURE;
-
- if (ret > 0) {
- sz = (unsigned int)ret;
- for (i = 0; i < sz; i++) {
- if (wolfSSL_BIO_printf(bp, "%02X", buf[i]) <= 0)
- return WOLFSSL_FAILURE;
- }
- }
- if (wolfSSL_BIO_printf(bp, "\n") <= 0)
- return WOLFSSL_FAILURE;
-
- /* @TODO PSK identity hint and SRP */
-
- if (wolfSSL_BIO_printf(bp, " TLS session ticket:") <= 0)
- return WOLFSSL_FAILURE;
-
-#ifdef HAVE_SESSION_TICKET
- if (wolfSSL_SESSION_print_ticket(bp, x, " ") != WOLFSSL_SUCCESS)
- return WOLFSSL_FAILURE;
-#endif
-
- if (wolfSSL_BIO_printf(bp, " Start Time: %ld\n",
- wolfSSL_SESSION_get_time(x)) <= 0)
- return WOLFSSL_FAILURE;
-
- if (wolfSSL_BIO_printf(bp, " Timeout : %ld (sec)\n",
- wolfSSL_SESSION_get_timeout(x)) <= 0)
- return WOLFSSL_FAILURE;
-
- /* @TODO verify return code print */
-
- if (wolfSSL_BIO_printf(bp, " Extended master secret: %s\n",
- (wolfSSL_SESSION_haveEMS(session) == 0)? "no" : "yes") <= 0)
- return WOLFSSL_FAILURE;
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* (HAVE_SESSION_TICKET || SESSION_CERTS) && !NO_FILESYSTEM */
-
-#endif /* OPENSSL_ALL || OPENSSL_EXTRA || HAVE_STUNNEL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
-
-#if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && defined(HAVE_STUNNEL)) \
- || defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX)
-
-int wolfSSL_CTX_get_verify_mode(WOLFSSL_CTX* ctx)
-{
- int mode = 0;
- WOLFSSL_ENTER("wolfSSL_CTX_get_verify_mode");
-
- if(!ctx)
- return WOLFSSL_FATAL_ERROR;
-
- if (ctx->verifyPeer)
- mode |= WOLFSSL_VERIFY_PEER;
- else if (ctx->verifyNone)
- mode |= WOLFSSL_VERIFY_NONE;
-
- if (ctx->failNoCert)
- mode |= WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT;
-
- if (ctx->failNoCertxPSK)
- mode |= WOLFSSL_VERIFY_FAIL_EXCEPT_PSK;
-
- WOLFSSL_LEAVE("wolfSSL_CTX_get_verify_mode", mode);
- return mode;
-}
-
-#endif
-#if defined(OPENSSL_EXTRA) && defined(HAVE_CURVE25519)
-/* return 1 if success, 0 if error
- * output keys are little endian format
- */
-int wolfSSL_EC25519_generate_key(unsigned char *priv, unsigned int *privSz,
- unsigned char *pub, unsigned int *pubSz)
-{
-#ifndef WOLFSSL_KEY_GEN
- WOLFSSL_MSG("No Key Gen built in");
- (void) priv;
- (void) privSz;
- (void) pub;
- (void) pubSz;
- return WOLFSSL_FAILURE;
-#else /* WOLFSSL_KEY_GEN */
- int ret = WOLFSSL_FAILURE;
- int initTmpRng = 0;
- WC_RNG *rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG *tmpRNG = NULL;
-#else
- WC_RNG tmpRNG[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_EC25519_generate_key");
-
- if (priv == NULL || privSz == NULL || *privSz < CURVE25519_KEYSIZE ||
- pub == NULL || pubSz == NULL || *pubSz < CURVE25519_KEYSIZE) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return WOLFSSL_FAILURE;
-#endif
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng) {
- curve25519_key key;
-
- if (wc_curve25519_init(&key) != MP_OKAY)
- WOLFSSL_MSG("wc_curve25519_init failed");
- else if (wc_curve25519_make_key(rng, CURVE25519_KEYSIZE, &key)!=MP_OKAY)
- WOLFSSL_MSG("wc_curve25519_make_key failed");
- /* export key pair */
- else if (wc_curve25519_export_key_raw_ex(&key, priv, privSz, pub,
- pubSz, EC25519_LITTLE_ENDIAN)
- != MP_OKAY)
- WOLFSSL_MSG("wc_curve25519_export_key_raw_ex failed");
- else
- ret = WOLFSSL_SUCCESS;
-
- wc_curve25519_free(&key);
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- return ret;
-#endif /* WOLFSSL_KEY_GEN */
-}
-
-/* return 1 if success, 0 if error
- * input and output keys are little endian format
- */
-int wolfSSL_EC25519_shared_key(unsigned char *shared, unsigned int *sharedSz,
- const unsigned char *priv, unsigned int privSz,
- const unsigned char *pub, unsigned int pubSz)
-{
-#ifndef WOLFSSL_KEY_GEN
- WOLFSSL_MSG("No Key Gen built in");
- (void) shared;
- (void) sharedSz;
- (void) priv;
- (void) privSz;
- (void) pub;
- (void) pubSz;
- return WOLFSSL_FAILURE;
-#else /* WOLFSSL_KEY_GEN */
- int ret = WOLFSSL_FAILURE;
- curve25519_key privkey, pubkey;
-
- WOLFSSL_ENTER("wolfSSL_EC25519_shared_key");
-
- if (shared == NULL || sharedSz == NULL || *sharedSz < CURVE25519_KEYSIZE ||
- priv == NULL || privSz < CURVE25519_KEYSIZE ||
- pub == NULL || pubSz < CURVE25519_KEYSIZE) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* import private key */
- if (wc_curve25519_init(&privkey) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve25519_init privkey failed");
- return ret;
- }
- if (wc_curve25519_import_private_ex(priv, privSz, &privkey,
- EC25519_LITTLE_ENDIAN) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve25519_import_private_ex failed");
- wc_curve25519_free(&privkey);
- return ret;
- }
-
- /* import public key */
- if (wc_curve25519_init(&pubkey) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve25519_init pubkey failed");
- wc_curve25519_free(&privkey);
- return ret;
- }
- if (wc_curve25519_import_public_ex(pub, pubSz, &pubkey,
- EC25519_LITTLE_ENDIAN) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve25519_import_public_ex failed");
- wc_curve25519_free(&privkey);
- wc_curve25519_free(&pubkey);
- return ret;
- }
-
- if (wc_curve25519_shared_secret_ex(&privkey, &pubkey,
- shared, sharedSz,
- EC25519_LITTLE_ENDIAN) != MP_OKAY)
- WOLFSSL_MSG("wc_curve25519_shared_secret_ex failed");
- else
- ret = WOLFSSL_SUCCESS;
-
- wc_curve25519_free(&privkey);
- wc_curve25519_free(&pubkey);
-
- return ret;
-#endif /* WOLFSSL_KEY_GEN */
-}
-#endif /* OPENSSL_EXTRA && HAVE_CURVE25519 */
-
-#if defined(OPENSSL_EXTRA) && defined(HAVE_ED25519)
-/* return 1 if success, 0 if error
- * output keys are little endian format
- */
-int wolfSSL_ED25519_generate_key(unsigned char *priv, unsigned int *privSz,
- unsigned char *pub, unsigned int *pubSz)
-{
-#ifndef WOLFSSL_KEY_GEN
- WOLFSSL_MSG("No Key Gen built in");
- (void) priv;
- (void) privSz;
- (void) pub;
- (void) pubSz;
- return WOLFSSL_FAILURE;
-#else /* WOLFSSL_KEY_GEN */
- int ret = WOLFSSL_FAILURE;
- int initTmpRng = 0;
- WC_RNG *rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG *tmpRNG = NULL;
-#else
- WC_RNG tmpRNG[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_ED25519_generate_key");
-
- if (priv == NULL || privSz == NULL || *privSz < ED25519_PRV_KEY_SIZE ||
- pub == NULL || pubSz == NULL || *pubSz < ED25519_PUB_KEY_SIZE) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return WOLFSSL_FATAL_ERROR;
-#endif
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng) {
- ed25519_key key;
-
- if (wc_ed25519_init(&key) != MP_OKAY)
- WOLFSSL_MSG("wc_ed25519_init failed");
- else if (wc_ed25519_make_key(rng, ED25519_KEY_SIZE, &key)!=MP_OKAY)
- WOLFSSL_MSG("wc_ed25519_make_key failed");
- /* export private key */
- else if (wc_ed25519_export_key(&key, priv, privSz, pub, pubSz)!=MP_OKAY)
- WOLFSSL_MSG("wc_ed25519_export_key failed");
- else
- ret = WOLFSSL_SUCCESS;
-
- wc_ed25519_free(&key);
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- return ret;
-#endif /* WOLFSSL_KEY_GEN */
-}
-
-/* return 1 if success, 0 if error
- * input and output keys are little endian format
- * priv is a buffer containing private and public part of key
- */
-int wolfSSL_ED25519_sign(const unsigned char *msg, unsigned int msgSz,
- const unsigned char *priv, unsigned int privSz,
- unsigned char *sig, unsigned int *sigSz)
-{
-#ifndef WOLFSSL_KEY_GEN
- WOLFSSL_MSG("No Key Gen built in");
- (void) msg;
- (void) msgSz;
- (void) priv;
- (void) privSz;
- (void) sig;
- (void) sigSz;
- return WOLFSSL_FAILURE;
-#else /* WOLFSSL_KEY_GEN */
- ed25519_key key;
- int ret = WOLFSSL_FAILURE;
-
- WOLFSSL_ENTER("wolfSSL_ED25519_sign");
-
- if (priv == NULL || privSz != ED25519_PRV_KEY_SIZE ||
- msg == NULL || sig == NULL || *sigSz < ED25519_SIG_SIZE) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* import key */
- if (wc_ed25519_init(&key) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve25519_init failed");
- return ret;
- }
- if (wc_ed25519_import_private_key(priv, privSz/2,
- priv+(privSz/2), ED25519_PUB_KEY_SIZE,
- &key) != MP_OKAY){
- WOLFSSL_MSG("wc_ed25519_import_private failed");
- wc_ed25519_free(&key);
- return ret;
- }
-
- if (wc_ed25519_sign_msg(msg, msgSz, sig, sigSz, &key) != MP_OKAY)
- WOLFSSL_MSG("wc_curve25519_shared_secret_ex failed");
- else
- ret = WOLFSSL_SUCCESS;
-
- wc_ed25519_free(&key);
-
- return ret;
-#endif /* WOLFSSL_KEY_GEN */
-}
-
-/* return 1 if success, 0 if error
- * input and output keys are little endian format
- * pub is a buffer containing public part of key
- */
-int wolfSSL_ED25519_verify(const unsigned char *msg, unsigned int msgSz,
- const unsigned char *pub, unsigned int pubSz,
- const unsigned char *sig, unsigned int sigSz)
-{
-#ifndef WOLFSSL_KEY_GEN
- WOLFSSL_MSG("No Key Gen built in");
- (void) msg;
- (void) msgSz;
- (void) pub;
- (void) pubSz;
- (void) sig;
- (void) sigSz;
- return WOLFSSL_FAILURE;
-#else /* WOLFSSL_KEY_GEN */
- ed25519_key key;
- int ret = WOLFSSL_FAILURE, check = 0;
-
- WOLFSSL_ENTER("wolfSSL_ED25519_verify");
-
- if (pub == NULL || pubSz != ED25519_PUB_KEY_SIZE ||
- msg == NULL || sig == NULL || sigSz != ED25519_SIG_SIZE) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* import key */
- if (wc_ed25519_init(&key) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve25519_init failed");
- return ret;
- }
- if (wc_ed25519_import_public(pub, pubSz, &key) != MP_OKAY){
- WOLFSSL_MSG("wc_ed25519_import_public failed");
- wc_ed25519_free(&key);
- return ret;
- }
-
- if ((ret = wc_ed25519_verify_msg((byte*)sig, sigSz, msg, msgSz,
- &check, &key)) != MP_OKAY) {
- WOLFSSL_MSG("wc_ed25519_verify_msg failed");
- }
- else if (!check)
- WOLFSSL_MSG("wc_ed25519_verify_msg failed (signature invalid)");
- else
- ret = WOLFSSL_SUCCESS;
-
- wc_ed25519_free(&key);
-
- return ret;
-#endif /* WOLFSSL_KEY_GEN */
-}
-
-#endif /* OPENSSL_EXTRA && HAVE_ED25519 */
-
-#if defined(OPENSSL_EXTRA) && defined(HAVE_CURVE448)
-/* return 1 if success, 0 if error
- * output keys are little endian format
- */
-int wolfSSL_EC448_generate_key(unsigned char *priv, unsigned int *privSz,
- unsigned char *pub, unsigned int *pubSz)
-{
-#ifndef WOLFSSL_KEY_GEN
- WOLFSSL_MSG("No Key Gen built in");
- (void) priv;
- (void) privSz;
- (void) pub;
- (void) pubSz;
- return WOLFSSL_FAILURE;
-#else /* WOLFSSL_KEY_GEN */
- int ret = WOLFSSL_FAILURE;
- int initTmpRng = 0;
- WC_RNG *rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG *tmpRNG = NULL;
-#else
- WC_RNG tmpRNG[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_EC448_generate_key");
-
- if (priv == NULL || privSz == NULL || *privSz < CURVE448_KEY_SIZE ||
- pub == NULL || pubSz == NULL || *pubSz < CURVE448_KEY_SIZE) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return WOLFSSL_FAILURE;
-#endif
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng) {
- curve448_key key;
-
- if (wc_curve448_init(&key) != MP_OKAY)
- WOLFSSL_MSG("wc_curve448_init failed");
- else if (wc_curve448_make_key(rng, CURVE448_KEY_SIZE, &key)!=MP_OKAY)
- WOLFSSL_MSG("wc_curve448_make_key failed");
- /* export key pair */
- else if (wc_curve448_export_key_raw_ex(&key, priv, privSz, pub, pubSz,
- EC448_LITTLE_ENDIAN)
- != MP_OKAY)
- WOLFSSL_MSG("wc_curve448_export_key_raw_ex failed");
- else
- ret = WOLFSSL_SUCCESS;
-
- wc_curve448_free(&key);
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- return ret;
-#endif /* WOLFSSL_KEY_GEN */
-}
-
-/* return 1 if success, 0 if error
- * input and output keys are little endian format
- */
-int wolfSSL_EC448_shared_key(unsigned char *shared, unsigned int *sharedSz,
- const unsigned char *priv, unsigned int privSz,
- const unsigned char *pub, unsigned int pubSz)
-{
-#ifndef WOLFSSL_KEY_GEN
- WOLFSSL_MSG("No Key Gen built in");
- (void) shared;
- (void) sharedSz;
- (void) priv;
- (void) privSz;
- (void) pub;
- (void) pubSz;
- return WOLFSSL_FAILURE;
-#else /* WOLFSSL_KEY_GEN */
- int ret = WOLFSSL_FAILURE;
- curve448_key privkey, pubkey;
-
- WOLFSSL_ENTER("wolfSSL_EC448_shared_key");
-
- if (shared == NULL || sharedSz == NULL || *sharedSz < CURVE448_KEY_SIZE ||
- priv == NULL || privSz < CURVE448_KEY_SIZE ||
- pub == NULL || pubSz < CURVE448_KEY_SIZE) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* import private key */
- if (wc_curve448_init(&privkey) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve448_init privkey failed");
- return ret;
- }
- if (wc_curve448_import_private_ex(priv, privSz, &privkey,
- EC448_LITTLE_ENDIAN) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve448_import_private_ex failed");
- wc_curve448_free(&privkey);
- return ret;
- }
-
- /* import public key */
- if (wc_curve448_init(&pubkey) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve448_init pubkey failed");
- wc_curve448_free(&privkey);
- return ret;
- }
- if (wc_curve448_import_public_ex(pub, pubSz, &pubkey,
- EC448_LITTLE_ENDIAN) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve448_import_public_ex failed");
- wc_curve448_free(&privkey);
- wc_curve448_free(&pubkey);
- return ret;
- }
-
- if (wc_curve448_shared_secret_ex(&privkey, &pubkey, shared, sharedSz,
- EC448_LITTLE_ENDIAN) != MP_OKAY)
- WOLFSSL_MSG("wc_curve448_shared_secret_ex failed");
- else
- ret = WOLFSSL_SUCCESS;
-
- wc_curve448_free(&privkey);
- wc_curve448_free(&pubkey);
-
- return ret;
-#endif /* WOLFSSL_KEY_GEN */
-}
-#endif /* OPENSSL_EXTRA && HAVE_CURVE448 */
-
-#if defined(OPENSSL_EXTRA) && defined(HAVE_ED448)
-/* return 1 if success, 0 if error
- * output keys are little endian format
- */
-int wolfSSL_ED448_generate_key(unsigned char *priv, unsigned int *privSz,
- unsigned char *pub, unsigned int *pubSz)
-{
-#ifndef WOLFSSL_KEY_GEN
- WOLFSSL_MSG("No Key Gen built in");
- (void) priv;
- (void) privSz;
- (void) pub;
- (void) pubSz;
- return WOLFSSL_FAILURE;
-#else /* WOLFSSL_KEY_GEN */
- int ret = WOLFSSL_FAILURE;
- int initTmpRng = 0;
- WC_RNG *rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG *tmpRNG = NULL;
-#else
- WC_RNG tmpRNG[1];
-#endif
-
- WOLFSSL_ENTER("wolfSSL_ED448_generate_key");
-
- if (priv == NULL || privSz == NULL || *privSz < ED448_PRV_KEY_SIZE ||
- pub == NULL || pubSz == NULL || *pubSz < ED448_PUB_KEY_SIZE) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return WOLFSSL_FATAL_ERROR;
-#endif
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0)
- WOLFSSL_MSG("Global RNG no Init");
- else
- rng = &globalRNG;
- }
-
- if (rng) {
- ed448_key key;
-
- if (wc_ed448_init(&key) != MP_OKAY)
- WOLFSSL_MSG("wc_ed448_init failed");
- else if (wc_ed448_make_key(rng, ED448_KEY_SIZE, &key) != MP_OKAY)
- WOLFSSL_MSG("wc_ed448_make_key failed");
- /* export private key */
- else if (wc_ed448_export_key(&key, priv, privSz, pub, pubSz) != MP_OKAY)
- WOLFSSL_MSG("wc_ed448_export_key failed");
- else
- ret = WOLFSSL_SUCCESS;
-
- wc_ed448_free(&key);
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- return ret;
-#endif /* WOLFSSL_KEY_GEN */
-}
-
-/* return 1 if success, 0 if error
- * input and output keys are little endian format
- * priv is a buffer containing private and public part of key
- */
-int wolfSSL_ED448_sign(const unsigned char *msg, unsigned int msgSz,
- const unsigned char *priv, unsigned int privSz,
- unsigned char *sig, unsigned int *sigSz)
-{
-#ifndef WOLFSSL_KEY_GEN
- WOLFSSL_MSG("No Key Gen built in");
- (void) msg;
- (void) msgSz;
- (void) priv;
- (void) privSz;
- (void) sig;
- (void) sigSz;
- return WOLFSSL_FAILURE;
-#else /* WOLFSSL_KEY_GEN */
- ed448_key key;
- int ret = WOLFSSL_FAILURE;
-
- WOLFSSL_ENTER("wolfSSL_ED448_sign");
-
- if (priv == NULL || privSz != ED448_PRV_KEY_SIZE || msg == NULL ||
- sig == NULL || *sigSz < ED448_SIG_SIZE) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* import key */
- if (wc_ed448_init(&key) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve448_init failed");
- return ret;
- }
- if (wc_ed448_import_private_key(priv, privSz/2, priv+(privSz/2),
- ED448_PUB_KEY_SIZE, &key) != MP_OKAY){
- WOLFSSL_MSG("wc_ed448_import_private failed");
- wc_ed448_free(&key);
- return ret;
- }
-
- if (wc_ed448_sign_msg(msg, msgSz, sig, sigSz, &key) != MP_OKAY)
- WOLFSSL_MSG("wc_curve448_shared_secret_ex failed");
- else
- ret = WOLFSSL_SUCCESS;
-
- wc_ed448_free(&key);
-
- return ret;
-#endif /* WOLFSSL_KEY_GEN */
-}
-
-/* return 1 if success, 0 if error
- * input and output keys are little endian format
- * pub is a buffer containing public part of key
- */
-int wolfSSL_ED448_verify(const unsigned char *msg, unsigned int msgSz,
- const unsigned char *pub, unsigned int pubSz,
- const unsigned char *sig, unsigned int sigSz)
-{
-#ifndef WOLFSSL_KEY_GEN
- WOLFSSL_MSG("No Key Gen built in");
- (void) msg;
- (void) msgSz;
- (void) pub;
- (void) pubSz;
- (void) sig;
- (void) sigSz;
- return WOLFSSL_FAILURE;
-#else /* WOLFSSL_KEY_GEN */
- ed448_key key;
- int ret = WOLFSSL_FAILURE, check = 0;
-
- WOLFSSL_ENTER("wolfSSL_ED448_verify");
-
- if (pub == NULL || pubSz != ED448_PUB_KEY_SIZE || msg == NULL ||
- sig == NULL || sigSz != ED448_SIG_SIZE) {
- WOLFSSL_MSG("Bad arguments");
- return WOLFSSL_FAILURE;
- }
-
- /* import key */
- if (wc_ed448_init(&key) != MP_OKAY) {
- WOLFSSL_MSG("wc_curve448_init failed");
- return ret;
- }
- if (wc_ed448_import_public(pub, pubSz, &key) != MP_OKAY){
- WOLFSSL_MSG("wc_ed448_import_public failed");
- wc_ed448_free(&key);
- return ret;
- }
-
- if ((ret = wc_ed448_verify_msg((byte*)sig, sigSz, msg, msgSz, &check,
- &key)) != MP_OKAY) {
- WOLFSSL_MSG("wc_ed448_verify_msg failed");
- }
- else if (!check)
- WOLFSSL_MSG("wc_ed448_verify_msg failed (signature invalid)");
- else
- ret = WOLFSSL_SUCCESS;
-
- wc_ed448_free(&key);
-
- return ret;
-#endif /* WOLFSSL_KEY_GEN */
-}
-
-#endif /* OPENSSL_EXTRA && HAVE_ED448 */
-
-#ifdef WOLFSSL_JNI
-
-int wolfSSL_set_jobject(WOLFSSL* ssl, void* objPtr)
-{
- WOLFSSL_ENTER("wolfSSL_set_jobject");
- if (ssl != NULL)
- {
- ssl->jObjectRef = objPtr;
- return WOLFSSL_SUCCESS;
- }
- return WOLFSSL_FAILURE;
-}
-
-void* wolfSSL_get_jobject(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_get_jobject");
- if (ssl != NULL)
- return ssl->jObjectRef;
- return NULL;
-}
-
-#endif /* WOLFSSL_JNI */
-
-
-#ifdef WOLFSSL_ASYNC_CRYPT
-int wolfSSL_CTX_AsyncPoll(WOLFSSL_CTX* ctx, WOLF_EVENT** events, int maxEvents,
- WOLF_EVENT_FLAG flags, int* eventCount)
-{
- if (ctx == NULL) {
- return BAD_FUNC_ARG;
- }
-
- return wolfAsync_EventQueuePoll(&ctx->event_queue, NULL,
- events, maxEvents, flags, eventCount);
-}
-
-int wolfSSL_AsyncPoll(WOLFSSL* ssl, WOLF_EVENT_FLAG flags)
-{
- int ret, eventCount = 0;
- WOLF_EVENT* events[1];
-
- if (ssl == NULL) {
- return BAD_FUNC_ARG;
- }
-
- ret = wolfAsync_EventQueuePoll(&ssl->ctx->event_queue, ssl,
- events, sizeof(events)/sizeof(*events), flags, &eventCount);
- if (ret == 0) {
- ret = eventCount;
- }
-
- return ret;
-}
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
-#ifdef OPENSSL_EXTRA
-unsigned long wolfSSL_ERR_peek_error_line_data(const char **file, int *line,
- const char **data, int *flags)
-{
- WOLFSSL_ENTER("wolfSSL_ERR_peek_error_line_data");
-
- (void)line;
- (void)file;
-
- /* No data or flags stored - error display only in Nginx. */
- if (data != NULL) {
- *data = "";
- }
- if (flags != NULL) {
- *flags = 0;
- }
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
- defined(WOLFSSL_OPENSSH) || defined(WOLFSSL_HAPROXY) || \
- defined(WOLFSSL_MYSQL_COMPATIBLE)
- {
- int ret = 0;
-
- while (1) {
- if ((ret = wc_PeekErrorNode(-1, file, NULL, line)) < 0) {
- WOLFSSL_MSG("Issue peeking at error node in queue");
- return 0;
- }
- /* OpenSSL uses positive error codes */
- if (ret < 0) {
- ret = -ret;
- }
-
- if (ret == -ASN_NO_PEM_HEADER)
- return (ERR_LIB_PEM << 24) | PEM_R_NO_START_LINE;
- if (ret != WANT_READ && ret != WANT_WRITE &&
- ret != ZERO_RETURN && ret != WOLFSSL_ERROR_ZERO_RETURN &&
- ret != SOCKET_PEER_CLOSED_E && ret != SOCKET_ERROR_E)
- break;
-
- wc_RemoveErrorNode(-1);
- }
-
- return (unsigned long)ret;
- }
-#else
- return (unsigned long)(0 - NOT_COMPILED_IN);
-#endif
-}
-#endif
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
-
-/* returns a pointer to internal cipher suite list. Should not be free'd by
- * caller.
- */
-WOLF_STACK_OF(WOLFSSL_CIPHER) *wolfSSL_get_ciphers_compat(const WOLFSSL *ssl)
-{
- WOLF_STACK_OF(WOLFSSL_CIPHER)* ret = NULL;
- Suites* suites;
-
- WOLFSSL_ENTER("wolfSSL_get_ciphers_compat");
- if (ssl == NULL || (ssl->suites == NULL && ssl->ctx->suites == NULL)) {
- return NULL;
- }
-
- if (ssl->suites != NULL) {
- suites = ssl->suites;
- }
- else {
- suites = ssl->ctx->suites;
- }
-
- /* check if stack needs populated */
- if (suites->stack == NULL) {
- int i;
- for (i = 0; i < suites->suiteSz; i+=2) {
- WOLFSSL_STACK* add = wolfSSL_sk_new_node(ssl->heap);
- if (add != NULL) {
- add->type = STACK_TYPE_CIPHER;
- add->data.cipher.cipherSuite0 = suites->suites[i];
- add->data.cipher.cipherSuite = suites->suites[i+1];
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- /* in_stack is checked in wolfSSL_CIPHER_description */
- add->data.cipher.in_stack = 1;
- #endif
-
- add->next = ret;
- if (ret != NULL) {
- add->num = ret->num + 1;
- }
- else {
- add->num = 1;
- }
- ret = add;
- }
- }
- suites->stack = ret;
- }
- return suites->stack;
-}
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_OPENSSL_config(char *config_name)
-{
- (void)config_name;
- WOLFSSL_STUB("OPENSSL_config");
-}
-#endif /* !NO_WOLFSSL_STUB */
-#endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) \
- || defined(OPENSSL_EXTRA) || defined(HAVE_LIGHTY)
-int wolfSSL_X509_get_ex_new_index(int idx, void *arg, void *a, void *b, void *c)
-{
- static int x509_idx = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_ex_new_index");
- (void)idx;
- (void)arg;
- (void)a;
- (void)b;
- (void)c;
-
- return x509_idx++;
-}
-
-#if defined(HAVE_EX_DATA) || defined(FORTRESS)
-void* wolfSSL_CRYPTO_get_ex_data(const WOLFSSL_CRYPTO_EX_DATA* ex_data, int idx)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_get_ex_data");
-#ifdef MAX_EX_DATA
- if(ex_data && idx < MAX_EX_DATA && idx >= 0) {
- return ex_data->ex_data[idx];
- }
-#else
- (void)ex_data;
- (void)idx;
-#endif
- return NULL;
-}
-
-int wolfSSL_CRYPTO_set_ex_data(WOLFSSL_CRYPTO_EX_DATA* ex_data, int idx, void *data)
-{
- WOLFSSL_ENTER("wolfSSL_CRYPTO_set_ex_data");
-#ifdef MAX_EX_DATA
- if (ex_data && idx < MAX_EX_DATA && idx >= 0) {
- ex_data->ex_data[idx] = data;
- return WOLFSSL_SUCCESS;
- }
-#else
- (void)ex_data;
- (void)idx;
- (void)data;
-#endif
- return WOLFSSL_FAILURE;
-}
-#endif /* defined(HAVE_EX_DATA) || defined(FORTRESS) */
-
-void *wolfSSL_X509_get_ex_data(X509 *x509, int idx)
-{
- WOLFSSL_ENTER("wolfSSL_X509_get_ex_data");
- #ifdef HAVE_EX_DATA
- if (x509 != NULL) {
- return wolfSSL_CRYPTO_get_ex_data(&x509->ex_data, idx);
- }
- #else
- (void)x509;
- (void)idx;
- #endif
- return NULL;
-}
-
-int wolfSSL_X509_set_ex_data(X509 *x509, int idx, void *data)
-{
- WOLFSSL_ENTER("wolfSSL_X509_set_ex_data");
- #ifdef HAVE_EX_DATA
- if (x509 != NULL)
- {
- return wolfSSL_CRYPTO_set_ex_data(&x509->ex_data, idx, data);
- }
- #else
- (void)x509;
- (void)idx;
- (void)data;
- #endif
- return WOLFSSL_FAILURE;
-}
-
-int wolfSSL_X509_NAME_digest(const WOLFSSL_X509_NAME *name,
- const WOLFSSL_EVP_MD *type, unsigned char *md, unsigned int *len)
-{
- WOLFSSL_ENTER("wolfSSL_X509_NAME_digest");
-
- if (name == NULL || type == NULL)
- return WOLFSSL_FAILURE;
-
-#if !defined(NO_FILESYSTEM) && !defined(NO_PWDBASED)
- return wolfSSL_EVP_Digest((unsigned char*)name->fullName.fullName,
- name->fullName.fullNameLen, md, len, type, NULL);
-#else
- (void)md;
- (void)len;
- return NOT_COMPILED_IN;
-#endif
-}
-
-long wolfSSL_SSL_CTX_get_timeout(const WOLFSSL_CTX *ctx)
-{
- WOLFSSL_ENTER("wolfSSL_SSL_CTX_get_timeout");
-
- if (ctx == NULL)
- return 0;
-
- return ctx->timeout;
-}
-
-
-/* returns the time in seconds of the current timeout */
-long wolfSSL_get_timeout(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_get_timeout");
-
- if (ssl == NULL)
- return 0;
- return ssl->timeout;
-}
-
-#ifdef HAVE_ECC
-int wolfSSL_SSL_CTX_set_tmp_ecdh(WOLFSSL_CTX *ctx, WOLFSSL_EC_KEY *ecdh)
-{
- WOLFSSL_ENTER("wolfSSL_SSL_CTX_set_tmp_ecdh");
-
- if (ctx == NULL || ecdh == NULL)
- return BAD_FUNC_ARG;
-
- ctx->ecdhCurveOID = ecdh->group->curve_oid;
-
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-/* Assumes that the session passed in is from the cache. */
-int wolfSSL_SSL_CTX_remove_session(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *s)
-{
- WOLFSSL_ENTER("wolfSSL_SSL_CTX_remove_session");
-
- if (ctx == NULL || s == NULL)
- return BAD_FUNC_ARG;
-
-#ifdef HAVE_EXT_CACHE
- if (!ctx->internalCacheOff)
-#endif
- {
- /* Don't remove session just timeout session. */
- s->timeout = 0;
- }
-
-#ifdef HAVE_EXT_CACHE
- if (ctx->rem_sess_cb != NULL)
- ctx->rem_sess_cb(ctx, s);
-#endif
-
- return 0;
-}
-
-BIO *wolfSSL_SSL_get_rbio(const WOLFSSL *s)
-{
- WOLFSSL_ENTER("wolfSSL_SSL_get_rbio");
- /* Nginx sets the buffer size if the read BIO is different to write BIO.
- * The setting buffer size doesn't do anything so return NULL for both.
- */
- if (s == NULL)
- return NULL;
-
- return s->biord;
-}
-BIO *wolfSSL_SSL_get_wbio(const WOLFSSL *s)
-{
- WOLFSSL_ENTER("wolfSSL_SSL_get_wbio");
- (void)s;
- /* Nginx sets the buffer size if the read BIO is different to write BIO.
- * The setting buffer size doesn't do anything so return NULL for both.
- */
- if (s == NULL)
- return NULL;
-
- return s->biowr;
-}
-
-int wolfSSL_SSL_do_handshake(WOLFSSL *s)
-{
- WOLFSSL_ENTER("wolfSSL_SSL_do_handshake");
-
- if (s == NULL)
- return WOLFSSL_FAILURE;
-
- if (s->options.side == WOLFSSL_CLIENT_END) {
- #ifndef NO_WOLFSSL_CLIENT
- return wolfSSL_connect(s);
- #else
- WOLFSSL_MSG("Client not compiled in");
- return WOLFSSL_FAILURE;
- #endif
- }
-
-#ifndef NO_WOLFSSL_SERVER
- return wolfSSL_accept(s);
-#else
- WOLFSSL_MSG("Server not compiled in");
- return WOLFSSL_FAILURE;
-#endif
-}
-
-int wolfSSL_SSL_in_init(WOLFSSL *ssl)
-{
- WOLFSSL_ENTER("SSL_in_init");
-
- if (ssl == NULL)
- return WOLFSSL_FAILURE;
-
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- return ssl->options.connectState < SECOND_REPLY_DONE;
- }
- return ssl->options.acceptState < ACCEPT_THIRD_REPLY_DONE;
-}
-
-int wolfSSL_SSL_in_connect_init(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("SSL_connect_init");
-
- if (ssl == NULL)
- return WOLFSSL_FAILURE;
-
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- return ssl->options.connectState > CONNECT_BEGIN &&
- ssl->options.connectState < SECOND_REPLY_DONE;
- }
-
- return ssl->options.acceptState > ACCEPT_BEGIN &&
- ssl->options.acceptState < ACCEPT_THIRD_REPLY_DONE;
-}
-
-#ifndef NO_SESSION_CACHE
-
-WOLFSSL_SESSION *wolfSSL_SSL_get0_session(const WOLFSSL *ssl)
-{
- WOLFSSL_SESSION *session;
-
- WOLFSSL_ENTER("wolfSSL_SSL_get0_session");
-
- if (ssl == NULL) {
- return NULL;
- }
-
- session = wolfSSL_get_session((WOLFSSL*)ssl);
-
-#ifdef HAVE_EXT_CACHE
- ((WOLFSSL*)ssl)->extSession = session;
-#endif
-
- return session;
-}
-
-#endif /* NO_SESSION_CACHE */
-
-int wolfSSL_X509_check_host(X509 *x, const char *chk, size_t chklen,
- unsigned int flags, char **peername)
-{
- int ret;
- DecodedCert dCert;
-
- WOLFSSL_ENTER("wolfSSL_X509_check_host");
-
- /* flags and peername not needed for Nginx. */
- (void)flags;
- (void)peername;
-
- if (flags == WOLFSSL_NO_WILDCARDS) {
- WOLFSSL_MSG("X509_CHECK_FLAG_NO_WILDCARDS not yet implemented");
- return WOLFSSL_FAILURE;
- }
-
- InitDecodedCert(&dCert, x->derCert->buffer, x->derCert->length, NULL);
- ret = ParseCertRelative(&dCert, CERT_TYPE, 0, NULL);
- if (ret != 0) {
- FreeDecodedCert(&dCert);
- return WOLFSSL_FAILURE;
- }
-
- ret = CheckHostName(&dCert, (char *)chk, chklen);
- FreeDecodedCert(&dCert);
- if (ret != 0)
- return WOLFSSL_FAILURE;
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_i2a_ASN1_INTEGER(BIO *bp, const WOLFSSL_ASN1_INTEGER *a)
-{
- static char num[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
- int i;
- word32 j;
- word32 len = 0;
-
- WOLFSSL_ENTER("wolfSSL_i2a_ASN1_INTEGER");
-
- if (bp == NULL || a == NULL)
- return WOLFSSL_FAILURE;
-
- /* Skip ASN.1 INTEGER (type) byte. */
- i = 1;
- /* When indefinite length, can't determine length with data available. */
- if (a->data[i] == 0x80)
- return 0;
- /* One length byte if less than 0x80. */
- if (a->data[i] < 0x80)
- len = a->data[i++];
- /* Multiple length byte if greater than 0x80. */
- else if (a->data[i] > 0x80) {
- switch (a->data[i++] - 0x80) {
- case 4:
- len |= a->data[i++] << 24;
- FALL_THROUGH;
- case 3:
- len |= a->data[i++] << 16;
- FALL_THROUGH;
- case 2:
- len |= a->data[i++] << 8;
- FALL_THROUGH;
- case 1:
- len |= a->data[i++];
- break;
- default:
- /* Not supporting greater than 4 bytes of length. */
- return 0;
- }
- }
-
- /* Zero length integer is the value zero. */
- if (len == 0) {
- wolfSSL_BIO_write(bp, "00", 2);
- return 2;
- }
-
- /* Don't do negative - just write out every byte. */
- for (j = 0; j < len; i++,j++) {
- wolfSSL_BIO_write(bp, &num[a->data[i] >> 4], 1);
- wolfSSL_BIO_write(bp, &num[a->data[i] & 0xf], 1);
- }
-
- /* Two nibbles written for each byte. */
- return len * 2;
-}
-
-
-#if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
-/* Expected return values from implementations of OpenSSL ticket key callback.
- */
-#define TICKET_KEY_CB_RET_FAILURE -1
-#define TICKET_KEY_CB_RET_NOT_FOUND 0
-#define TICKET_KEY_CB_RET_OK 1
-#define TICKET_KEY_CB_RET_RENEW 2
-
-/* The ticket key callback as used in OpenSSL is stored here. */
-static int (*ticketKeyCb)(WOLFSSL *ssl, unsigned char *name, unsigned char *iv,
- WOLFSSL_EVP_CIPHER_CTX *ectx, WOLFSSL_HMAC_CTX *hctx, int enc) = NULL;
-
-/* Implementation of session ticket encryption/decryption using OpenSSL
- * callback to initialize the cipher and HMAC.
- *
- * ssl The SSL/TLS object.
- * keyName The key name - used to identify the key to be used.
- * iv The IV to use.
- * mac The MAC of the encrypted data.
- * enc Encrypt ticket.
- * encTicket The ticket data.
- * encTicketLen The length of the ticket data.
- * encLen The encrypted/decrypted ticket length - output length.
- * ctx Ignored. Application specific data.
- * returns WOLFSSL_TICKET_RET_OK to indicate success,
- * WOLFSSL_TICKET_RET_CREATE if a new ticket is required and
- * WOLFSSL_TICKET_RET_FATAL on error.
- */
-static int wolfSSL_TicketKeyCb(WOLFSSL* ssl,
- unsigned char keyName[WOLFSSL_TICKET_NAME_SZ],
- unsigned char iv[WOLFSSL_TICKET_IV_SZ],
- unsigned char mac[WOLFSSL_TICKET_MAC_SZ],
- int enc, unsigned char* encTicket,
- int encTicketLen, int* encLen, void* ctx)
-{
- byte digest[WC_MAX_DIGEST_SIZE];
- WOLFSSL_EVP_CIPHER_CTX evpCtx;
- WOLFSSL_HMAC_CTX hmacCtx;
- unsigned int mdSz = 0;
- int len = 0;
- int ret = WOLFSSL_TICKET_RET_FATAL;
- int res;
-
- (void)ctx;
-
- if (ticketKeyCb == NULL)
- return WOLFSSL_TICKET_RET_FATAL;
-
- wolfSSL_EVP_CIPHER_CTX_init(&evpCtx);
- /* Initialize the cipher and HMAC. */
- res = ticketKeyCb(ssl, keyName, iv, &evpCtx, &hmacCtx, enc);
- if (res != TICKET_KEY_CB_RET_OK && res != TICKET_KEY_CB_RET_RENEW)
- return WOLFSSL_TICKET_RET_FATAL;
-
- if (enc)
- {
- /* Encrypt in place. */
- if (!wolfSSL_EVP_CipherUpdate(&evpCtx, encTicket, &len,
- encTicket, encTicketLen))
- goto end;
- encTicketLen = len;
- if (!wolfSSL_EVP_EncryptFinal(&evpCtx, &encTicket[encTicketLen], &len))
- goto end;
- /* Total length of encrypted data. */
- encTicketLen += len;
- *encLen = encTicketLen;
-
- /* HMAC the encrypted data into the parameter 'mac'. */
- if (!wolfSSL_HMAC_Update(&hmacCtx, encTicket, encTicketLen))
- goto end;
-#ifdef WOLFSSL_SHA512
- /* Check for SHA512, which would overrun the mac buffer */
- if (hmacCtx.hmac.macType == WC_SHA512)
- goto end;
-#endif
- if (!wolfSSL_HMAC_Final(&hmacCtx, mac, &mdSz))
- goto end;
- }
- else
- {
- /* HMAC the encrypted data and compare it to the passed in data. */
- if (!wolfSSL_HMAC_Update(&hmacCtx, encTicket, encTicketLen))
- goto end;
- if (!wolfSSL_HMAC_Final(&hmacCtx, digest, &mdSz))
- goto end;
- if (XMEMCMP(mac, digest, mdSz) != 0)
- goto end;
-
- /* Decrypt the ticket data in place. */
- if (!wolfSSL_EVP_CipherUpdate(&evpCtx, encTicket, &len,
- encTicket, encTicketLen))
- goto end;
- encTicketLen = len;
- if (!wolfSSL_EVP_DecryptFinal(&evpCtx, &encTicket[encTicketLen], &len))
- goto end;
- /* Total length of decrypted data. */
- *encLen = encTicketLen + len;
- }
-
- ret = (res == TICKET_KEY_CB_RET_RENEW) ? WOLFSSL_TICKET_RET_CREATE :
- WOLFSSL_TICKET_RET_OK;
-end:
- return ret;
-}
-
-/* Set the callback to use when encrypting/decrypting tickets.
- *
- * ctx The SSL/TLS context object.
- * cb The OpenSSL session ticket callback.
- * returns WOLFSSL_SUCCESS to indicate success.
- */
-int wolfSSL_CTX_set_tlsext_ticket_key_cb(WOLFSSL_CTX *ctx, int (*cb)(
- WOLFSSL *ssl, unsigned char *name, unsigned char *iv,
- WOLFSSL_EVP_CIPHER_CTX *ectx, WOLFSSL_HMAC_CTX *hctx, int enc))
-{
- /* Store callback in a global. */
- ticketKeyCb = cb;
- /* Set the ticket encryption callback to be a wrapper around OpenSSL
- * callback.
- */
- ctx->ticketEncCb = wolfSSL_TicketKeyCb;
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* HAVE_SESSION_TICKET */
-
-#endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
- OPENSSL_EXTRA || HAVE_LIGHTY */
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
-#ifdef HAVE_OCSP
-/* Not an OpenSSL API. */
-int wolfSSL_get_ocsp_response(WOLFSSL* ssl, byte** response)
-{
- *response = ssl->ocspResp;
- return ssl->ocspRespSz;
-}
-
-/* Not an OpenSSL API. */
-char* wolfSSL_get_ocsp_url(WOLFSSL* ssl)
-{
- return ssl->url;
-}
-
-/* Not an OpenSSL API. */
-int wolfSSL_set_ocsp_url(WOLFSSL* ssl, char* url)
-{
- if (ssl == NULL)
- return WOLFSSL_FAILURE;
-
- ssl->url = url;
- return WOLFSSL_SUCCESS;
-}
-#endif /* OCSP */
-#endif /* OPENSSL_ALL / WOLFSSL_NGINX / WOLFSSL_HAPROXY */
-
-#if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
- defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
-int wolfSSL_CTX_get_extra_chain_certs(WOLFSSL_CTX* ctx, WOLF_STACK_OF(X509)** chain)
-{
- word32 idx;
- word32 length;
- WOLFSSL_STACK* node;
- WOLFSSL_STACK* last = NULL;
-
- if (ctx == NULL || chain == NULL) {
- chain = NULL;
- return WOLFSSL_FAILURE;
- }
- if (ctx->x509Chain != NULL) {
- *chain = ctx->x509Chain;
- return WOLFSSL_SUCCESS;
- }
-
- /* If there are no chains then success! */
- *chain = NULL;
- if (ctx->certChain == NULL || ctx->certChain->length == 0) {
- return WOLFSSL_SUCCESS;
- }
-
- /* Create a new stack of WOLFSSL_X509 object from chain buffer. */
- for (idx = 0; idx < ctx->certChain->length; ) {
- node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_OPENSSL);
- if (node == NULL)
- return WOLFSSL_FAILURE;
- node->next = NULL;
-
- /* 3 byte length | X509 DER data */
- ato24(ctx->certChain->buffer + idx, &length);
- idx += 3;
-
- /* Create a new X509 from DER encoded data. */
- node->data.x509 = wolfSSL_X509_d2i(NULL, ctx->certChain->buffer + idx,
- length);
- if (node->data.x509 == NULL) {
- XFREE(node, NULL, DYNAMIC_TYPE_OPENSSL);
- /* Return as much of the chain as we created. */
- ctx->x509Chain = *chain;
- return WOLFSSL_FAILURE;
- }
- idx += length;
-
- /* Add object to the end of the stack. */
- if (last == NULL) {
- node->num = 1;
- *chain = node;
- }
- else {
- (*chain)->num++;
- last->next = node;
- }
-
- last = node;
- }
-
- ctx->x509Chain = *chain;
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_CTX_set_tlsext_status_cb(WOLFSSL_CTX* ctx,
- int(*cb)(WOLFSSL*, void*))
-{
- if (ctx == NULL || ctx->cm == NULL)
- return WOLFSSL_FAILURE;
-
-#if !defined(NO_WOLFSSL_SERVER) && (defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
- || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2))
- /* Ensure stapling is on for callback to be used. */
- wolfSSL_CTX_EnableOCSPStapling(ctx);
-
- if (ctx->cm->ocsp_stapling == NULL)
- return WOLFSSL_FAILURE;
-
- ctx->cm->ocsp_stapling->statusCb = cb;
-#else
- (void)cb;
-#endif
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_X509_STORE_CTX_get1_issuer(WOLFSSL_X509 **issuer,
- WOLFSSL_X509_STORE_CTX *ctx, WOLFSSL_X509 *x)
-{
- WOLFSSL_STACK* node;
- Signer* ca = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- DecodedCert* cert = NULL;
-#else
- DecodedCert cert[1];
-#endif
-
- if (issuer == NULL || ctx == NULL || x == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- if (ctx->chain != NULL) {
- for (node = ctx->chain; node != NULL; node = node->next) {
- if (wolfSSL_X509_check_issued(node->data.x509, x) == X509_V_OK) {
- *issuer = x;
- return WOLFSSL_SUCCESS;
- }
- }
- }
-
-
-#ifdef WOLFSSL_SMALL_STACK
- cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL, DYNAMIC_TYPE_DCERT);
- if (cert == NULL)
- return WOLFSSL_FAILURE;
-#endif
-
- /* Use existing CA retrieval APIs that use DecodedCert. */
- InitDecodedCert(cert, x->derCert->buffer, x->derCert->length, NULL);
- if (ParseCertRelative(cert, CERT_TYPE, 0, NULL) == 0) {
- #ifndef NO_SKID
- if (cert->extAuthKeyIdSet)
- ca = GetCA(ctx->store->cm, cert->extAuthKeyId);
- if (ca == NULL)
- ca = GetCAByName(ctx->store->cm, cert->issuerHash);
- #else /* NO_SKID */
- ca = GetCA(ctx->store->cm, cert->issuerHash);
- #endif /* NO SKID */
- }
- FreeDecodedCert(cert);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(cert, NULL, DYNAMIC_TYPE_DCERT);
-#endif
-
- if (ca == NULL)
- return WOLFSSL_FAILURE;
-
-#ifdef WOLFSSL_SIGNER_DER_CERT
- /* populate issuer with Signer DER */
- *issuer = wolfSSL_X509_d2i(issuer, ca->derCert->buffer,
- ca->derCert->length);
- if (*issuer == NULL)
- return WOLFSSL_FAILURE;
-#else
- /* Create an empty certificate as CA doesn't have a certificate. */
- *issuer = (WOLFSSL_X509 *)XMALLOC(sizeof(WOLFSSL_X509), 0,
- DYNAMIC_TYPE_OPENSSL);
- if (*issuer == NULL)
- return WOLFSSL_FAILURE;
-
- InitX509((*issuer), 1, NULL);
-#endif
-
- /* Result is ignored when passed to wolfSSL_OCSP_cert_to_id(). */
-
- return WOLFSSL_SUCCESS;
-}
-
-void wolfSSL_X509_email_free(WOLF_STACK_OF(WOLFSSL_STRING) *sk)
-{
- WOLFSSL_STACK *curr;
-
- while (sk != NULL) {
- curr = sk;
- sk = sk->next;
-
- XFREE(curr, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-WOLF_STACK_OF(WOLFSSL_STRING) *wolfSSL_X509_get1_ocsp(WOLFSSL_X509 *x)
-{
- WOLFSSL_STACK* list = NULL;
- char* url;
-
- if (x->authInfoSz == 0)
- return NULL;
-
- list = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK) + x->authInfoSz + 1,
- NULL, DYNAMIC_TYPE_OPENSSL);
- if (list == NULL)
- return NULL;
-
- url = (char*)list;
- url += sizeof(WOLFSSL_STACK);
- XMEMCPY(url, x->authInfo, x->authInfoSz);
- url[x->authInfoSz] = '\0';
-
- list->data.string = url;
- list->next = NULL;
-
- return list;
-}
-
-int wolfSSL_X509_check_issued(WOLFSSL_X509 *issuer, WOLFSSL_X509 *subject)
-{
- WOLFSSL_X509_NAME *issuerName = wolfSSL_X509_get_issuer_name(subject);
- WOLFSSL_X509_NAME *subjectName = wolfSSL_X509_get_subject_name(issuer);
-
- if (issuerName == NULL || subjectName == NULL)
- return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
-
- /* Literal matching of encoded names and key ids. */
- if (issuerName->sz != subjectName->sz ||
- XMEMCMP(issuerName->name, subjectName->name, subjectName->sz) != 0) {
- return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
- }
-
- if (subject->authKeyId != NULL && issuer->subjKeyId != NULL) {
- if (subject->authKeyIdSz != issuer->subjKeyIdSz ||
- XMEMCMP(subject->authKeyId, issuer->subjKeyId,
- issuer->subjKeyIdSz) != 0) {
- return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
- }
- }
-
- return X509_V_OK;
-}
-
-WOLFSSL_X509* wolfSSL_X509_dup(WOLFSSL_X509 *x)
-{
- return wolfSSL_X509_d2i(NULL, x->derCert->buffer, x->derCert->length);
-}
-
-char* wolfSSL_sk_WOLFSSL_STRING_value(WOLF_STACK_OF(WOLFSSL_STRING)* strings,
- int idx)
-{
- for (; idx > 0 && strings != NULL; idx--)
- strings = strings->next;
- if (strings == NULL)
- return NULL;
- return strings->data.string;
-}
-#endif /* WOLFSSL_NGINX || WOLFSSL_HAPROXY || OPENSSL_EXTRA || OPENSSL_ALL */
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
-#ifdef HAVE_ALPN
-void wolfSSL_get0_alpn_selected(const WOLFSSL *ssl, const unsigned char **data,
- unsigned int *len)
-{
- word16 nameLen;
-
- if (ssl != NULL && data != NULL && len != NULL) {
- TLSX_ALPN_GetRequest(ssl->extensions, (void **)data, &nameLen);
- *len = nameLen;
- }
-}
-
-int wolfSSL_select_next_proto(unsigned char **out, unsigned char *outLen,
- const unsigned char *in, unsigned int inLen,
- const unsigned char *clientNames,
- unsigned int clientLen)
-{
- unsigned int i, j;
- byte lenIn, lenClient;
-
- if (out == NULL || outLen == NULL || in == NULL || clientNames == NULL)
- return OPENSSL_NPN_UNSUPPORTED;
-
- for (i = 0; i < inLen; i += lenIn) {
- lenIn = in[i++];
- for (j = 0; j < clientLen; j += lenClient) {
- lenClient = clientNames[j++];
-
- if (lenIn != lenClient)
- continue;
-
- if (XMEMCMP(in + i, clientNames + j, lenIn) == 0) {
- *out = (unsigned char *)(in + i);
- *outLen = lenIn;
- return OPENSSL_NPN_NEGOTIATED;
- }
- }
- }
-
- *out = (unsigned char *)clientNames + 1;
- *outLen = clientNames[0];
- return OPENSSL_NPN_NO_OVERLAP;
-}
-
-void wolfSSL_CTX_set_alpn_select_cb(WOLFSSL_CTX *ctx,
- int (*cb) (WOLFSSL *ssl,
- const unsigned char **out,
- unsigned char *outlen,
- const unsigned char *in,
- unsigned int inlen,
- void *arg), void *arg)
-{
- if (ctx != NULL) {
- ctx->alpnSelect = cb;
- ctx->alpnSelectArg = arg;
- }
-}
-
-void wolfSSL_CTX_set_next_protos_advertised_cb(WOLFSSL_CTX *s,
- int (*cb) (WOLFSSL *ssl,
- const unsigned char
- **out,
- unsigned int *outlen,
- void *arg), void *arg)
-{
- (void)s;
- (void)cb;
- (void)arg;
- WOLFSSL_STUB("wolfSSL_CTX_set_next_protos_advertised_cb");
-}
-
-void wolfSSL_CTX_set_next_proto_select_cb(WOLFSSL_CTX *s,
- int (*cb) (WOLFSSL *ssl,
- unsigned char **out,
- unsigned char *outlen,
- const unsigned char *in,
- unsigned int inlen,
- void *arg), void *arg)
-{
- (void)s;
- (void)cb;
- (void)arg;
- WOLFSSL_STUB("wolfSSL_CTX_set_next_proto_select_cb");
-}
-
-void wolfSSL_get0_next_proto_negotiated(const WOLFSSL *s, const unsigned char **data,
- unsigned *len)
-{
- (void)s;
- (void)data;
- (void)len;
- WOLFSSL_STUB("wolfSSL_get0_next_proto_negotiated");
-}
-#endif /* HAVE_ALPN */
-
-#endif /* WOLFSSL_NGINX / WOLFSSL_HAPROXY */
-
-#if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
-int wolfSSL_CTX_set1_curves_list(WOLFSSL_CTX* ctx, const char* names)
-{
- int idx, start = 0, len;
- word16 curve;
- char name[MAX_CURVE_NAME_SZ];
-
- /* Disable all curves so that only the ones the user wants are enabled. */
- ctx->disabledCurves = 0xFFFFFFFFUL;
- for (idx = 1; names[idx-1] != '\0'; idx++) {
- if (names[idx] != ':' && names[idx] != '\0')
- continue;
-
- len = idx - 1 - start;
- if (len > MAX_CURVE_NAME_SZ - 1)
- return WOLFSSL_FAILURE;
-
- XMEMCPY(name, names + start, len);
- name[len] = 0;
-
- if ((XSTRNCMP(name, "prime256v1", len) == 0) ||
- (XSTRNCMP(name, "secp256r1", len) == 0) ||
- (XSTRNCMP(name, "P-256", len) == 0)) {
- curve = WOLFSSL_ECC_SECP256R1;
- }
- else if ((XSTRNCMP(name, "secp384r1", len) == 0) ||
- (XSTRNCMP(name, "P-384", len) == 0)) {
- curve = WOLFSSL_ECC_SECP384R1;
- }
- else if ((XSTRNCMP(name, "secp521r1", len) == 0) ||
- (XSTRNCMP(name, "P-521", len) == 0)) {
- curve = WOLFSSL_ECC_SECP521R1;
- }
- else if (XSTRNCMP(name, "X25519", len) == 0) {
- curve = WOLFSSL_ECC_X25519;
- }
- else if (XSTRNCMP(name, "X448", len) == 0) {
- curve = WOLFSSL_ECC_X448;
- }
- else {
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- int ret;
- const ecc_set_type *eccSet;
-
- ret = wc_ecc_get_curve_idx_from_name(name);
- if (ret < 0) {
- WOLFSSL_MSG("Could not find name in set");
- return WOLFSSL_FAILURE;
- }
-
- eccSet = wc_ecc_get_curve_params(ret);
- if (eccSet == NULL) {
- WOLFSSL_MSG("NULL set returned");
- return WOLFSSL_FAILURE;
- }
-
- curve = GetCurveByOID(eccSet->oidSum);
- #else
- WOLFSSL_MSG("API not present to search farther using name");
- return WOLFSSL_FAILURE;
- #endif
- }
-
- if (curve > (sizeof(word32) * WOLFSSL_BIT_SIZE)) {
- /* shift left more than size of ctx->disabledCurves causes static
- * analysis report */
- WOLFSSL_MSG("curve value is too large for upcoming shift");
- return WOLFSSL_FAILURE;
- }
-
- #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT)
- /* set the supported curve so client TLS extension contains only the
- * desired curves */
- if (wolfSSL_CTX_UseSupportedCurve(ctx, curve) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Unable to set supported curve");
- return WOLFSSL_FAILURE;
- }
- #endif
-
- /* Switch the bit to off and therefore is enabled. */
- ctx->disabledCurves &= ~(1U << curve);
- start = idx + 1;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_set1_curves_list(WOLFSSL* ssl, const char* names)
-{
- if (ssl == NULL) {
- return WOLFSSL_FAILURE;
- }
- return wolfSSL_CTX_set1_curves_list(ssl->ctx, names);
-}
-#endif /* OPENSSL_EXTRA && HAVE_ECC */
-
-#ifdef OPENSSL_EXTRA
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_CTX_set_msg_callback(WOLFSSL_CTX *ctx, SSL_Msg_Cb cb)
-{
- WOLFSSL_STUB("SSL_CTX_set_msg_callback");
- (void)ctx;
- (void)cb;
- return WOLFSSL_FAILURE;
-}
-#endif
-
-
-/* Sets a callback for when sending and receiving protocol messages.
- *
- * ssl WOLFSSL structure to set callback in
- * cb callback to use
- *
- * return SSL_SUCCESS on success and SSL_FAILURE with error case
- */
-int wolfSSL_set_msg_callback(WOLFSSL *ssl, SSL_Msg_Cb cb)
-{
- WOLFSSL_ENTER("wolfSSL_set_msg_callback");
-
- if (ssl == NULL) {
- return SSL_FAILURE;
- }
-
- if (cb != NULL) {
- ssl->toInfoOn = 1;
- }
-
- ssl->protoMsgCb = cb;
- return SSL_SUCCESS;
-}
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_CTX_set_msg_callback_arg(WOLFSSL_CTX *ctx, void* arg)
-{
- WOLFSSL_STUB("SSL_CTX_set_msg_callback_arg");
- (void)ctx;
- (void)arg;
- return WOLFSSL_FAILURE;
-}
-#endif
-
-int wolfSSL_set_msg_callback_arg(WOLFSSL *ssl, void* arg)
-{
- WOLFSSL_ENTER("wolfSSL_set_msg_callback_arg");
- if (ssl == NULL)
- return WOLFSSL_FAILURE;
-
- ssl->protoMsgCtx = arg;
- return WOLFSSL_SUCCESS;
-}
-
-void *wolfSSL_OPENSSL_memdup(const void *data, size_t siz, const char* file, int line)
-{
- void *ret;
- (void)file;
- (void)line;
-
- if (data == NULL || siz >= INT_MAX)
- return NULL;
-
- ret = OPENSSL_malloc(siz);
- if (ret == NULL) {
- return NULL;
- }
- return XMEMCPY(ret, data, siz);
-}
-
-int wolfSSL_CTX_set_alpn_protos(WOLFSSL_CTX *ctx, const unsigned char *p,
- unsigned int p_len)
-{
- WOLFSSL_ENTER("wolfSSL_CTX_set_alpn_protos");
- if(ctx == NULL)
- return BAD_FUNC_ARG;
- if((void *)ctx->alpn_cli_protos != NULL)
- wolfSSL_OPENSSL_free((void *)ctx->alpn_cli_protos);
- ctx->alpn_cli_protos =
- (const unsigned char *)wolfSSL_OPENSSL_memdup(p, p_len, NULL, 0);
- if (ctx->alpn_cli_protos == NULL) {
- return SSL_FAILURE;
- }
- ctx->alpn_cli_protos_len = p_len;
-
- return SSL_SUCCESS;
-}
-
-
-#ifdef HAVE_ALPN
-/* Sets the ALPN extension protos
- *
- * example format is
- * unsigned char p[] = {
- * 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
- * };
- *
- * returns WOLFSSL_SUCCESS on success */
-int wolfSSL_set_alpn_protos(WOLFSSL* ssl,
- const unsigned char* p, unsigned int p_len)
-{
- WOLFSSL_BIO* bio;
- char* pt;
-
- unsigned int sz;
- unsigned int idx = 0;
- int alpn_opt = WOLFSSL_ALPN_CONTINUE_ON_MISMATCH;
- WOLFSSL_ENTER("wolfSSL_set_alpn_protos");
-
- if (ssl == NULL || p_len <= 1) {
- return WOLFSSL_FAILURE;
- }
-
- bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
- if (bio == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- /* convert into comma separated list */
- while (idx < p_len - 1) {
- unsigned int i;
-
- sz = p[idx++];
- if (idx + sz > p_len) {
- WOLFSSL_MSG("Bad list format");
- wolfSSL_BIO_free(bio);
- return WOLFSSL_FAILURE;
- }
- if (sz > 0) {
- for (i = 0; i < sz; i++) {
- wolfSSL_BIO_write(bio, &p[idx++], 1);
- }
- if (idx < p_len - 1)
- wolfSSL_BIO_write(bio, ",", 1);
- }
- }
- wolfSSL_BIO_write(bio, "\0", 1);
-
- /* clears out all current ALPN extensions set */
- TLSX_Remove(&ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL, ssl->heap);
-
- if ((sz = wolfSSL_BIO_get_mem_data(bio, &pt)) > 0) {
- wolfSSL_UseALPN(ssl, pt, sz, alpn_opt);
- }
- wolfSSL_BIO_free(bio);
- return WOLFSSL_SUCCESS;
-}
-#endif /* HAVE_ALPN */
-#endif
-
-#endif /* WOLFCRYPT_ONLY */
-
-#if defined(OPENSSL_EXTRA)
-
-#define WOLFSSL_BIO_INCLUDED
-#include "src/bio.c"
-
-int oid2nid(word32 oid, int grp)
-{
- /* get OID type */
- switch (grp) {
- /* oidHashType */
- case oidHashType:
- switch (oid) {
- #ifdef WOLFSSL_MD2
- case MD2h:
- return NID_md2;
- #endif
- #ifndef NO_MD5
- case MD5h:
- return NID_md5;
- #endif
- #ifndef NO_SHA
- case SHAh:
- return NID_sha1;
- #endif
- case SHA224h:
- return NID_sha224;
- #ifndef NO_SHA256
- case SHA256h:
- return NID_sha256;
- #endif
- #ifdef WOLFSSL_SHA384
- case SHA384h:
- return NID_sha384;
- #endif
- #ifdef WOLFSSL_SHA512
- case SHA512h:
- return NID_sha512;
- #endif
- }
- break;
-
- /* oidSigType */
- case oidSigType:
- switch (oid) {
- #ifndef NO_DSA
- case CTC_SHAwDSA:
- return CTC_SHAwDSA;
- #endif /* NO_DSA */
- #ifndef NO_RSA
- case CTC_MD2wRSA:
- return CTC_MD2wRSA;
- case CTC_MD5wRSA:
- return CTC_MD5wRSA;
- case CTC_SHAwRSA:
- return CTC_SHAwRSA;
- case CTC_SHA224wRSA:
- return CTC_SHA224wRSA;
- case CTC_SHA256wRSA:
- return CTC_SHA256wRSA;
- case CTC_SHA384wRSA:
- return CTC_SHA384wRSA;
- case CTC_SHA512wRSA:
- return CTC_SHA512wRSA;
- #endif /* NO_RSA */
- #ifdef HAVE_ECC
- case CTC_SHAwECDSA:
- return CTC_SHAwECDSA;
- case CTC_SHA224wECDSA:
- return CTC_SHA224wECDSA;
- case CTC_SHA256wECDSA:
- return CTC_SHA256wECDSA;
- case CTC_SHA384wECDSA:
- return CTC_SHA384wECDSA;
- case CTC_SHA512wECDSA:
- return CTC_SHA512wECDSA;
- #endif /* HAVE_ECC */
- }
- break;
-
- /* oidKeyType */
- case oidKeyType:
- switch (oid) {
- #ifndef NO_DSA
- case DSAk:
- return DSAk;
- #endif /* NO_DSA */
- #ifndef NO_RSA
- case RSAk:
- return RSAk;
- #endif /* NO_RSA */
- #ifdef HAVE_NTRU
- case NTRUk:
- return NTRUk;
- #endif /* HAVE_NTRU */
- #ifdef HAVE_ECC
- case ECDSAk:
- return ECDSAk;
- #endif /* HAVE_ECC */
- }
- break;
-
-
- #ifdef HAVE_ECC
- case oidCurveType:
- switch (oid) {
- case ECC_SECP192R1_OID:
- return NID_X9_62_prime192v1;
- case ECC_PRIME192V2_OID:
- return NID_X9_62_prime192v2;
- case ECC_PRIME192V3_OID:
- return NID_X9_62_prime192v3;
- case ECC_PRIME239V1_OID:
- return NID_X9_62_prime239v1;
- case ECC_PRIME239V2_OID:
- return NID_X9_62_prime239v2;
- case ECC_PRIME239V3_OID:
- return NID_X9_62_prime239v3;
- case ECC_SECP256R1_OID:
- return NID_X9_62_prime256v1;
- case ECC_SECP112R1_OID:
- return NID_secp112r1;
- case ECC_SECP112R2_OID:
- return NID_secp112r2;
- case ECC_SECP128R1_OID:
- return NID_secp128r1;
- case ECC_SECP128R2_OID:
- return NID_secp128r2;
- case ECC_SECP160R1_OID:
- return NID_secp160r1;
- case ECC_SECP160R2_OID:
- return NID_secp160r2;
- case ECC_SECP224R1_OID:
- return NID_secp224r1;
- case ECC_SECP384R1_OID:
- return NID_secp384r1;
- case ECC_SECP521R1_OID:
- return NID_secp521r1;
- case ECC_SECP160K1_OID:
- return NID_secp160k1;
- case ECC_SECP192K1_OID:
- return NID_secp192k1;
- case ECC_SECP224K1_OID:
- return NID_secp224k1;
- case ECC_SECP256K1_OID:
- return NID_secp256k1;
- case ECC_BRAINPOOLP160R1_OID:
- return NID_brainpoolP160r1;
- case ECC_BRAINPOOLP192R1_OID:
- return NID_brainpoolP192r1;
- case ECC_BRAINPOOLP224R1_OID:
- return NID_brainpoolP224r1;
- case ECC_BRAINPOOLP256R1_OID:
- return NID_brainpoolP256r1;
- case ECC_BRAINPOOLP320R1_OID:
- return NID_brainpoolP320r1;
- case ECC_BRAINPOOLP384R1_OID:
- return NID_brainpoolP384r1;
- case ECC_BRAINPOOLP512R1_OID:
- return NID_brainpoolP512r1;
- }
- break;
- #endif /* HAVE_ECC */
-
- /* oidBlkType */
- case oidBlkType:
- switch (oid) {
- #ifdef WOLFSSL_AES_128
- case AES128CBCb:
- return AES128CBCb;
- #endif
- #ifdef WOLFSSL_AES_192
- case AES192CBCb:
- return AES192CBCb;
- #endif
- #ifdef WOLFSSL_AES_256
- case AES256CBCb:
- return AES256CBCb;
- #endif
- #ifndef NO_DES3
- case DESb:
- return NID_des;
- case DES3b:
- return NID_des3;
- #endif
- }
- break;
-
- #ifdef HAVE_OCSP
- case oidOcspType:
- switch (oid) {
- case OCSP_BASIC_OID:
- return NID_id_pkix_OCSP_basic;
- case OCSP_NONCE_OID:
- return OCSP_NONCE_OID;
- }
- break;
- #endif /* HAVE_OCSP */
-
- /* oidCertExtType */
- case oidCertExtType:
- switch (oid) {
- case BASIC_CA_OID:
- return BASIC_CA_OID;
- case ALT_NAMES_OID:
- return ALT_NAMES_OID;
- case CRL_DIST_OID:
- return CRL_DIST_OID;
- case AUTH_INFO_OID:
- return AUTH_INFO_OID;
- case AUTH_KEY_OID:
- return AUTH_KEY_OID;
- case SUBJ_KEY_OID:
- return SUBJ_KEY_OID;
- case INHIBIT_ANY_OID:
- return INHIBIT_ANY_OID;
- case KEY_USAGE_OID:
- return NID_key_usage;
- case NAME_CONS_OID:
- return NID_name_constraints;
- case CERT_POLICY_OID:
- return NID_certificate_policies;
- }
- break;
-
- /* oidCertAuthInfoType */
- case oidCertAuthInfoType:
- switch (oid) {
- case AIA_OCSP_OID:
- return AIA_OCSP_OID;
- case AIA_CA_ISSUER_OID:
- return AIA_CA_ISSUER_OID;
- }
- break;
-
- /* oidCertPolicyType */
- case oidCertPolicyType:
- switch (oid) {
- case CP_ANY_OID:
- return NID_any_policy;
- }
- break;
-
- /* oidCertAltNameType */
- case oidCertAltNameType:
- switch (oid) {
- case HW_NAME_OID:
- return NID_hw_name_oid;
- }
- break;
-
- /* oidCertKeyUseType */
- case oidCertKeyUseType:
- switch (oid) {
- case EKU_ANY_OID:
- return NID_anyExtendedKeyUsage;
- case EKU_SERVER_AUTH_OID:
- return EKU_SERVER_AUTH_OID;
- case EKU_CLIENT_AUTH_OID:
- return EKU_CLIENT_AUTH_OID;
- case EKU_OCSP_SIGN_OID:
- return EKU_OCSP_SIGN_OID;
- }
- break;
-
- /* oidKdfType */
- case oidKdfType:
- switch (oid) {
- case PBKDF2_OID:
- return PBKDF2_OID;
- }
- break;
-
- /* oidPBEType */
- case oidPBEType:
- switch (oid) {
- case PBE_SHA1_RC4_128:
- return PBE_SHA1_RC4_128;
- case PBE_SHA1_DES:
- return PBE_SHA1_DES;
- case PBE_SHA1_DES3:
- return PBE_SHA1_DES3;
- }
- break;
-
- /* oidKeyWrapType */
- case oidKeyWrapType:
- switch (oid) {
- #ifdef WOLFSSL_AES_128
- case AES128_WRAP:
- return AES128_WRAP;
- #endif
- #ifdef WOLFSSL_AES_192
- case AES192_WRAP:
- return AES192_WRAP;
- #endif
- #ifdef WOLFSSL_AES_256
- case AES256_WRAP:
- return AES256_WRAP;
- #endif
- }
- break;
-
- /* oidCmsKeyAgreeType */
- case oidCmsKeyAgreeType:
- switch (oid) {
- #ifndef NO_SHA
- case dhSinglePass_stdDH_sha1kdf_scheme:
- return dhSinglePass_stdDH_sha1kdf_scheme;
- #endif
- #ifdef WOLFSSL_SHA224
- case dhSinglePass_stdDH_sha224kdf_scheme:
- return dhSinglePass_stdDH_sha224kdf_scheme;
- #endif
- #ifndef NO_SHA256
- case dhSinglePass_stdDH_sha256kdf_scheme:
- return dhSinglePass_stdDH_sha256kdf_scheme;
- #endif
- #ifdef WOLFSSL_SHA384
- case dhSinglePass_stdDH_sha384kdf_scheme:
- return dhSinglePass_stdDH_sha384kdf_scheme;
- #endif
- #ifdef WOLFSSL_SHA512
- case dhSinglePass_stdDH_sha512kdf_scheme:
- return dhSinglePass_stdDH_sha512kdf_scheme;
- #endif
- }
- break;
-
- default:
- WOLFSSL_MSG("NID not in table");
- return -1;
- }
-
- return -1;
-}
-
-
-/* when calling SetIndividualInternal, mpi should be cleared by caller if no
- * longer used. ie mp_free(mpi). This is to free data when fastmath is
- * disabled since a copy of mpi is made by this function and placed into bn.
- */
-int SetIndividualInternal(WOLFSSL_BIGNUM* bn, mp_int* mpi)
-{
- WOLFSSL_MSG("Entering SetIndividualInternal");
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (mpi == NULL || (mp_init(mpi) != MP_OKAY)) {
- WOLFSSL_MSG("mpi NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (mp_copy((mp_int*)bn->internal, mpi) != MP_OKAY) {
- WOLFSSL_MSG("mp_copy error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-#ifndef NO_ASN
-WOLFSSL_BIGNUM *wolfSSL_ASN1_INTEGER_to_BN(const WOLFSSL_ASN1_INTEGER *ai,
- WOLFSSL_BIGNUM *bn)
-{
- mp_int mpi;
- word32 idx = 0;
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_ASN1_INTEGER_to_BN");
-
- if (ai == NULL) {
- return NULL;
- }
-
- ret = GetInt(&mpi, ai->data, &idx, ai->dataMax);
- if (ret != 0) {
- #ifdef WOLFSSL_QT
- mp_init(&mpi); /* must init mpi */
- /* Serial number in QT starts at index 0 of data */
- if (mp_read_unsigned_bin(&mpi, (byte*)ai->data, ai->length) != 0) {
- mp_clear(&mpi);
- return NULL;
- }
- #else
- /* expecting ASN1 format for INTEGER */
- WOLFSSL_LEAVE("wolfSSL_ASN1_INTEGER_to_BN", ret);
- return NULL;
- #endif
- }
-
- /* mp_clear needs called because mpi is copied and causes memory leak with
- * --disable-fastmath */
- ret = SetIndividualExternal(&bn, &mpi);
- mp_clear(&mpi);
-
- if (ret != WOLFSSL_SUCCESS) {
- return NULL;
- }
- return bn;
-}
-#endif /* !NO_ASN */
-
-#if !defined(NO_DSA) && !defined(NO_DH)
-WOLFSSL_DH *wolfSSL_DSA_dup_DH(const WOLFSSL_DSA *dsa)
-{
- WOLFSSL_DH* dh;
- DhKey* key;
-
- WOLFSSL_ENTER("wolfSSL_DSA_dup_DH");
-
- if (dsa == NULL) {
- return NULL;
- }
-
- dh = wolfSSL_DH_new();
- if (dh == NULL) {
- return NULL;
- }
- key = (DhKey*)dh->internal;
-
- if (dsa->p != NULL &&
- SetIndividualInternal(((WOLFSSL_DSA*)dsa)->p, &key->p) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa p key error");
- wolfSSL_DH_free(dh);
- return NULL;
- }
- if (dsa->g != NULL &&
- SetIndividualInternal(((WOLFSSL_DSA*)dsa)->g, &key->g) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa g key error");
- wolfSSL_DH_free(dh);
- return NULL;
- }
-
- if (SetIndividualExternal(&dh->p, &key->p) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dsa p key error");
- wolfSSL_DH_free(dh);
- return NULL;
- }
- if (SetIndividualExternal(&dh->g, &key->g) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("dsa g key error");
- wolfSSL_DH_free(dh);
- return NULL;
- }
-
- return dh;
-}
-#endif /* !NO_DSA && !NO_DH */
-
-
-#ifndef NO_RSA
-#if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
-/* Openssl -> WolfSSL */
-int SetRsaInternal(WOLFSSL_RSA* rsa)
-{
- RsaKey* key;
- WOLFSSL_MSG("Entering SetRsaInternal");
-
- if (rsa == NULL || rsa->internal == NULL) {
- WOLFSSL_MSG("rsa key NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- key = (RsaKey*)rsa->internal;
-
- if (SetIndividualInternal(rsa->n, &key->n) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa n key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualInternal(rsa->e, &key->e) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa e key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* public key */
- key->type = RSA_PUBLIC;
-
- if (rsa->d != NULL) {
- if (SetIndividualInternal(rsa->d, &key->d) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa d key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* private key */
- key->type = RSA_PRIVATE;
- }
-
- if (rsa->p != NULL &&
- SetIndividualInternal(rsa->p, &key->p) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa p key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (rsa->q != NULL &&
- SetIndividualInternal(rsa->q, &key->q) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa q key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
-#ifndef RSA_LOW_MEM
- if (rsa->dmp1 != NULL &&
- SetIndividualInternal(rsa->dmp1, &key->dP) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa dP key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (rsa->dmq1 != NULL &&
- SetIndividualInternal(rsa->dmq1, &key->dQ) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa dQ key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (rsa->iqmp != NULL &&
- SetIndividualInternal(rsa->iqmp, &key->u) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa u key error");
- return WOLFSSL_FATAL_ERROR;
- }
-#endif /* !RSA_LOW_MEM */
-
- rsa->inSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* SSL_SUCCESS on ok */
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_RSA_blinding_on(WOLFSSL_RSA* rsa, WOLFSSL_BN_CTX* bn)
-{
- (void)rsa;
- (void)bn;
- WOLFSSL_STUB("RSA_blinding_on");
- WOLFSSL_MSG("wolfSSL_RSA_blinding_on");
-
- return WOLFSSL_SUCCESS; /* on by default */
-}
-#endif
-
-/* return compliant with OpenSSL
- * size of encrypted data if success , -1 if error
- */
-int wolfSSL_RSA_public_encrypt(int len, const unsigned char* fr,
- unsigned char* to, WOLFSSL_RSA* rsa, int padding)
-{
- int initTmpRng = 0;
- WC_RNG *rng = NULL;
- int outLen;
- int ret = 0;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG = NULL;
-#else
- WC_RNG _tmpRNG[1];
- WC_RNG* tmpRNG = _tmpRNG;
-#endif
-#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
- int mgf = WC_MGF1NONE;
- enum wc_HashType hash = WC_HASH_TYPE_NONE;
-#endif
-
- WOLFSSL_MSG("wolfSSL_RSA_public_encrypt");
-
- /* Check and remap the padding to internal values, if needed. */
-#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
- if (padding == RSA_PKCS1_PADDING)
- padding = WC_RSA_PKCSV15_PAD;
- else if (padding == RSA_PKCS1_OAEP_PADDING) {
- padding = WC_RSA_OAEP_PAD;
- hash = WC_HASH_TYPE_SHA;
- mgf = WC_MGF1SHA1;
- }
- else if (padding == RSA_PKCS1_PSS_PADDING) {
- padding = WC_RSA_PSS_PAD;
- hash = WC_HASH_TYPE_SHA256;
- mgf = WC_MGF1SHA256;
- }
- else if (padding == RSA_NO_PADDING) {
- padding = WC_RSA_NO_PAD;
- }
-#else
- if (padding == RSA_PKCS1_PADDING)
- ;
-#endif
- else {
- WOLFSSL_MSG("wolfSSL_RSA_public_encrypt unsupported padding");
- return 0;
- }
-
- if (rsa->inSet == 0)
- {
- if (SetRsaInternal(rsa) != SSL_SUCCESS) {
- WOLFSSL_MSG("SetRsaInternal failed");
- return 0;
- }
- }
-
- outLen = wolfSSL_RSA_size(rsa);
-
- rng = WOLFSSL_RSA_GetRNG(rsa, (WC_RNG**)&tmpRNG, &initTmpRng);
-
- if (outLen == 0) {
- WOLFSSL_MSG("Bad RSA size");
- }
-
- if (rng) {
-#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
- ret = wc_RsaPublicEncrypt_ex(fr, len, to, outLen,
- (RsaKey*)rsa->internal, rng, padding,
- hash, mgf, NULL, 0);
-#else
- ret = wc_RsaPublicEncrypt(fr, len, to, outLen,
- (RsaKey*)rsa->internal, rng);
-#endif
- if (ret <= 0) {
- WOLFSSL_MSG("Bad Rsa Encrypt");
- }
- if (len <= 0) {
- WOLFSSL_MSG("Bad Rsa Encrypt");
- }
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-#ifdef WOLFSSL_SMALL_STACK
- if (tmpRNG)
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- if (ret >= 0)
- WOLFSSL_MSG("wolfSSL_RSA_public_encrypt success");
- else {
- WOLFSSL_MSG("wolfSSL_RSA_public_encrypt failed");
- ret = WOLFSSL_FATAL_ERROR; /* return -1 on error case */
- }
- return ret;
-}
-
-
-
-
-/* return compliant with OpenSSL
- * size of plain recovered data if success , -1 if error
- */
-int wolfSSL_RSA_private_decrypt(int len, const unsigned char* fr,
- unsigned char* to, WOLFSSL_RSA* rsa, int padding)
-{
- int outLen;
- int ret = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
- int mgf = WC_MGF1NONE;
- enum wc_HashType hash = WC_HASH_TYPE_NONE;
- #endif
-
- WOLFSSL_MSG("wolfSSL_RSA_private_decrypt");
-
-#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
- if (padding == RSA_PKCS1_PADDING)
- padding = WC_RSA_PKCSV15_PAD;
- else if (padding == RSA_PKCS1_OAEP_PADDING) {
- padding = WC_RSA_OAEP_PAD;
- hash = WC_HASH_TYPE_SHA;
- mgf = WC_MGF1SHA1;
- }
- else if (padding == RSA_PKCS1_PSS_PADDING) {
- padding = WC_RSA_PSS_PAD;
- hash = WC_HASH_TYPE_SHA256;
- mgf = WC_MGF1SHA256;
- }
- else if (padding == RSA_NO_PADDING) {
- padding = WC_RSA_NO_PAD;
- }
-#else
- if (padding == RSA_PKCS1_PADDING)
- ;
-#endif
- else {
- WOLFSSL_MSG("wolfSSL_RSA_private_decrypt unsupported padding");
- return 0;
- }
-
- if (rsa->inSet == 0)
- {
- if (SetRsaInternal(rsa) != SSL_SUCCESS) {
- WOLFSSL_MSG("SetRsaInternal failed");
- return 0;
- }
- }
-
- outLen = wolfSSL_RSA_size(rsa);
- if (outLen == 0) {
- WOLFSSL_MSG("Bad RSA size");
- }
-
- /* size of 'to' buffer must be size of RSA key */
-#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
- ret = wc_RsaPrivateDecrypt_ex(fr, len, to, outLen,
- (RsaKey*)rsa->internal, padding,
- hash, mgf, NULL, 0);
-#else
- ret = wc_RsaPrivateDecrypt(fr, len, to, outLen,
- (RsaKey*)rsa->internal);
-#endif
-
- if (len <= 0) {
- WOLFSSL_MSG("Bad Rsa Decrypt");
- }
-
- if (ret > 0)
- WOLFSSL_MSG("wolfSSL_RSA_private_decrypt success");
- else {
- WOLFSSL_MSG("wolfSSL_RSA_private_decrypt failed");
- ret = WOLFSSL_FATAL_ERROR;
- }
- return ret;
-}
-
-#if !defined(_WIN32) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
-int wolfSSL_RSA_public_decrypt(int flen, const unsigned char* from,
- unsigned char* to, WOLFSSL_RSA* rsa, int padding)
-{
- int tlen = 0;
- int pad_type;
-
- WOLFSSL_ENTER("wolfSSL_RSA_public_decrypt");
-
- if (rsa == NULL || rsa->internal == NULL || from == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return WOLFSSL_FAILURE;
- }
-
- switch (padding) {
- case RSA_PKCS1_PADDING:
- pad_type = WC_RSA_PKCSV15_PAD;
- break;
- case RSA_PKCS1_OAEP_PADDING:
- pad_type = WC_RSA_OAEP_PAD;
- break;
- case RSA_PKCS1_PSS_PADDING:
- pad_type = WC_RSA_PSS_PAD;
- break;
- case RSA_NO_PADDING:
- pad_type = WC_RSA_NO_PAD;
- break;
- default:
- WOLFSSL_MSG("wolfSSL_RSA_public_decrypt unsupported padding");
- return WOLFSSL_FAILURE;
- }
-
- if (rsa->inSet == 0)
- {
- WOLFSSL_MSG("No RSA internal set, do it");
-
- if (SetRsaInternal(rsa) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("SetRsaInternal failed");
- return WOLFSSL_FAILURE;
- }
- }
-
- /* size of 'to' buffer must be size of RSA key */
- tlen = wc_RsaSSL_Verify_ex(from, flen, to, wolfSSL_RSA_size(rsa),
- (RsaKey*)rsa->internal, pad_type);
- if (tlen <= 0)
- WOLFSSL_MSG("wolfSSL_RSA_public_decrypt failed");
- else {
- WOLFSSL_MSG("wolfSSL_RSA_public_decrypt success");
- }
- return tlen;
-}
-#endif /* !defined(_WIN32) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) */
-
-/* RSA private encrypt calls wc_RsaSSL_Sign. Similar function set up as RSA
- * public decrypt.
- *
- * len Length of input buffer
- * in Input buffer to sign
- * out Output buffer (expected to be greater than or equal to RSA key size)
- * rsa Key to use for encryption
- * padding Type of RSA padding to use.
- */
-int wolfSSL_RSA_private_encrypt(int len, unsigned char* in,
- unsigned char* out, WOLFSSL_RSA* rsa, int padding)
-{
- int sz = 0;
- WC_RNG* rng = NULL;
-#if !defined(WC_RSA_BLINDING) || defined(HAVE_USER_RSA)
- WC_RNG rng_lcl;
-#endif
- RsaKey* key;
-
- WOLFSSL_MSG("wolfSSL_RSA_private_encrypt");
-
- if (len < 0 || rsa == NULL || rsa->internal == NULL || in == NULL) {
- WOLFSSL_MSG("Bad function arguments");
- return 0;
- }
-
- if (padding != RSA_PKCS1_PADDING && padding != RSA_PKCS1_PSS_PADDING) {
- WOLFSSL_MSG("wolfSSL_RSA_private_encrypt unsupported padding");
- return 0;
- }
-
- if (rsa->inSet == 0)
- {
- WOLFSSL_MSG("Setting internal RSA structure");
-
- if (SetRsaInternal(rsa) != SSL_SUCCESS) {
- WOLFSSL_MSG("SetRsaInternal failed");
- return 0;
- }
- }
-
- key = (RsaKey*)rsa->internal;
-#if defined(WC_RSA_BLINDING) && !defined(HAVE_USER_RSA)
- rng = key->rng;
-#else
- rng = &rng_lcl;
- #ifndef HAVE_FIPS
- if (wc_InitRng_ex(rng, key->heap, INVALID_DEVID) != 0)
- #else
- if (wc_InitRng(rng) != 0)
- #endif
- {
- WOLFSSL_MSG("Error with random number");
- return SSL_FATAL_ERROR;
- }
-#endif
-
- /* size of output buffer must be size of RSA key */
- sz = wc_RsaSSL_Sign(in, (word32)len, out, wolfSSL_RSA_size(rsa), key, rng);
- #if !defined(WC_RSA_BLINDING) || defined(HAVE_USER_RSA)
- if (wc_FreeRng(rng) != 0) {
- WOLFSSL_MSG("Error freeing random number generator");
- return SSL_FATAL_ERROR;
- }
- #endif
- if (sz <= 0) {
- WOLFSSL_LEAVE("wolfSSL_RSA_private_encrypt", sz);
- return 0;
- }
-
- return sz;
-}
-#endif /* HAVE_USER_RSA */
-#endif
-
-
-/* frees all nodes in the current threads error queue
- *
- * id thread id. ERR_remove_state is depreciated and id is ignored. The
- * current threads queue will be free'd.
- */
-void wolfSSL_ERR_remove_state(unsigned long id)
-{
- WOLFSSL_ENTER("wolfSSL_ERR_remove_state");
- (void)id;
- if (wc_ERR_remove_state() != 0) {
- WOLFSSL_MSG("Error with removing the state");
- }
-}
-
-
-WOLFSSL_BN_CTX* wolfSSL_BN_CTX_new(void)
-{
- static int ctx; /* wolfcrypt doesn't now need ctx */
-
- WOLFSSL_MSG("wolfSSL_BN_CTX_new");
- return (WOLFSSL_BN_CTX*)&ctx;
-
-}
-
-void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx)
-{
- (void)ctx;
- WOLFSSL_MSG("wolfSSL_BN_CTX_init");
-}
-
-
-void wolfSSL_BN_CTX_free(WOLFSSL_BN_CTX* ctx)
-{
- (void)ctx;
- WOLFSSL_MSG("wolfSSL_BN_CTX_free");
- /* do free since static ctx that does nothing */
-}
-
-/* WOLFSSL_SUCCESS on ok */
-int wolfSSL_BN_sub(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
- const WOLFSSL_BIGNUM* b)
-{
- WOLFSSL_MSG("wolfSSL_BN_sub");
-
- if (r == NULL || a == NULL || b == NULL)
- return 0;
-
- if (mp_sub((mp_int*)a->internal,(mp_int*)b->internal,
- (mp_int*)r->internal) == MP_OKAY)
- return WOLFSSL_SUCCESS;
-
- WOLFSSL_MSG("wolfSSL_BN_sub mp_sub failed");
- return 0;
-}
-
-/* WOLFSSL_SUCCESS on ok */
-int wolfSSL_BN_mod(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
- const WOLFSSL_BIGNUM* b, const WOLFSSL_BN_CTX* c)
-{
- (void)c;
- WOLFSSL_MSG("wolfSSL_BN_mod");
-
- if (r == NULL || a == NULL || b == NULL)
- return 0;
-
- if (mp_mod((mp_int*)a->internal,(mp_int*)b->internal,
- (mp_int*)r->internal) == MP_OKAY)
- return WOLFSSL_SUCCESS;
-
- WOLFSSL_MSG("wolfSSL_BN_mod mp_mod failed");
- return 0;
-}
-
-
-/* r = (a^p) % m */
-int wolfSSL_BN_mod_exp(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
- const WOLFSSL_BIGNUM *p, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
-{
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_BN_mod_exp");
-
- (void) ctx;
- if (r == NULL || a == NULL || p == NULL || m == NULL) {
- WOLFSSL_MSG("Bad Argument");
- return WOLFSSL_FAILURE;
- }
-
- if ((ret = mp_exptmod((mp_int*)a->internal,(mp_int*)p->internal,
- (mp_int*)m->internal, (mp_int*)r->internal)) == MP_OKAY) {
- return WOLFSSL_SUCCESS;
- }
-
- WOLFSSL_LEAVE("wolfSSL_BN_mod_exp", ret);
- (void)ret;
-
- return WOLFSSL_FAILURE;
-}
-
-/* r = (a * p) % m */
-int wolfSSL_BN_mod_mul(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
- const WOLFSSL_BIGNUM *p, const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
-{
- int ret;
-
- WOLFSSL_ENTER("wolfSSL_BN_mod_mul");
-
- (void) ctx;
- if (r == NULL || a == NULL || p == NULL || m == NULL) {
- WOLFSSL_MSG("Bad Argument");
- return SSL_FAILURE;
- }
-
- if ((ret = mp_mulmod((mp_int*)a->internal,(mp_int*)p->internal,
- (mp_int*)m->internal, (mp_int*)r->internal)) == MP_OKAY) {
- return SSL_SUCCESS;
- }
-
- WOLFSSL_LEAVE("wolfSSL_BN_mod_mul", ret);
- (void)ret;
-
- return SSL_FAILURE;
-}
-
-#ifdef OPENSSL_EXTRA
-const WOLFSSL_BIGNUM* wolfSSL_BN_value_one(void)
-{
- WOLFSSL_MSG("wolfSSL_BN_value_one");
-
- if (bn_one == NULL) {
- bn_one = wolfSSL_BN_new();
- if (bn_one) {
- if (mp_set_int((mp_int*)bn_one->internal, 1) != MP_OKAY) {
- /* handle error by freeing BN and returning NULL */
- wolfSSL_BN_free(bn_one);
- bn_one = NULL;
- }
- }
- }
-
- return bn_one;
-}
-#endif
-
-/* return compliant with OpenSSL
- * size of BIGNUM in bytes, 0 if error */
-int wolfSSL_BN_num_bytes(const WOLFSSL_BIGNUM* bn)
-{
- WOLFSSL_ENTER("wolfSSL_BN_num_bytes");
-
- if (bn == NULL || bn->internal == NULL)
- return WOLFSSL_FAILURE;
-
- return mp_unsigned_bin_size((mp_int*)bn->internal);
-}
-
-/* return compliant with OpenSSL
- * size of BIGNUM in bits, 0 if error */
-int wolfSSL_BN_num_bits(const WOLFSSL_BIGNUM* bn)
-{
- WOLFSSL_ENTER("wolfSSL_BN_num_bits");
-
- if (bn == NULL || bn->internal == NULL)
- return WOLFSSL_FAILURE;
-
- return mp_count_bits((mp_int*)bn->internal);
-}
-
-int wolfSSL_BN_is_negative(const WOLFSSL_BIGNUM* bn)
-{
- if (bn == NULL)
- return WOLFSSL_FAILURE;
-
- return mp_isneg((mp_int*)bn->internal);
-}
-
-/* return compliant with OpenSSL
- * 1 if BIGNUM is zero, 0 else */
-int wolfSSL_BN_is_zero(const WOLFSSL_BIGNUM* bn)
-{
- WOLFSSL_MSG("wolfSSL_BN_is_zero");
-
- if (bn == NULL || bn->internal == NULL)
- return WOLFSSL_FAILURE;
-
- if (mp_iszero((mp_int*)bn->internal) == MP_YES)
- return WOLFSSL_SUCCESS;
-
- return WOLFSSL_FAILURE;
-}
-
-/* return compliant with OpenSSL
- * 1 if BIGNUM is one, 0 else */
-int wolfSSL_BN_is_one(const WOLFSSL_BIGNUM* bn)
-{
- WOLFSSL_MSG("wolfSSL_BN_is_one");
-
- if (bn == NULL || bn->internal == NULL)
- return WOLFSSL_FAILURE;
-
- if (mp_cmp_d((mp_int*)bn->internal, 1) == MP_EQ)
- return WOLFSSL_SUCCESS;
-
- return WOLFSSL_FAILURE;
-}
-
-/* return compliant with OpenSSL
- * 1 if BIGNUM is odd, 0 else */
-int wolfSSL_BN_is_odd(const WOLFSSL_BIGNUM* bn)
-{
- WOLFSSL_MSG("wolfSSL_BN_is_odd");
-
- if (bn == NULL || bn->internal == NULL)
- return WOLFSSL_FAILURE;
-
- if (mp_isodd((mp_int*)bn->internal) == MP_YES)
- return WOLFSSL_SUCCESS;
-
- return WOLFSSL_FAILURE;
-}
-
-/* return compliant with OpenSSL
- * 1 if BIGNUM is word, 0 else */
-int wolfSSL_BN_is_word(const WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w)
-{
- WOLFSSL_ENTER("wolfSSL_BN_is_word");
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (mp_isword((mp_int*)bn->internal, w) == MP_YES) {
- return WOLFSSL_SUCCESS;
- }
-
- return WOLFSSL_FAILURE;
-}
-
-/* return compliant with OpenSSL
- * -1 if a < b, 0 if a == b and 1 if a > b
- */
-int wolfSSL_BN_cmp(const WOLFSSL_BIGNUM* a, const WOLFSSL_BIGNUM* b)
-{
- int ret;
-
- WOLFSSL_MSG("wolfSSL_BN_cmp");
-
- if (a == NULL || a->internal == NULL || b == NULL || b->internal == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- ret = mp_cmp((mp_int*)a->internal, (mp_int*)b->internal);
-
- return (ret == MP_EQ ? 0 : (ret == MP_GT ? 1 : -1));
-}
-
-/* return compliant with OpenSSL
- * length of BIGNUM in bytes, -1 if error */
-int wolfSSL_BN_bn2bin(const WOLFSSL_BIGNUM* bn, unsigned char* r)
-{
- WOLFSSL_MSG("wolfSSL_BN_bn2bin");
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("NULL bn error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (r == NULL)
- return mp_unsigned_bin_size((mp_int*)bn->internal);
-
- if (mp_to_unsigned_bin((mp_int*)bn->internal, r) != MP_OKAY) {
- WOLFSSL_MSG("mp_to_unsigned_bin error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- return mp_unsigned_bin_size((mp_int*)bn->internal);
-}
-
-
-WOLFSSL_BIGNUM* wolfSSL_BN_bin2bn(const unsigned char* str, int len,
- WOLFSSL_BIGNUM* ret)
-{
- int weOwn = 0;
-
- WOLFSSL_MSG("wolfSSL_BN_bin2bn");
-
- /* if ret is null create a BN */
- if (ret == NULL) {
- ret = wolfSSL_BN_new();
- weOwn = 1;
- if (ret == NULL)
- return NULL;
- }
-
- /* check ret and ret->internal then read in value */
- if (ret && ret->internal) {
- if (mp_read_unsigned_bin((mp_int*)ret->internal, str, len) != 0) {
- WOLFSSL_MSG("mp_read_unsigned_bin failure");
- if (weOwn)
- wolfSSL_BN_free(ret);
- return NULL;
- }
- } else {
- return NULL;
- }
-
- return ret;
-}
-
-/* return compliant with OpenSSL
- * 1 if success, 0 if error */
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n)
-{
- (void)bn;
- (void)n;
- WOLFSSL_ENTER("wolfSSL_BN_mask_bits");
- WOLFSSL_STUB("BN_mask_bits");
- return SSL_FAILURE;
-}
-#endif
-
-
-/* WOLFSSL_SUCCESS on ok */
-int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
-{
- int ret = 0;
- int len = bits / 8;
- int initTmpRng = 0;
- WC_RNG* rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG = NULL;
- byte* buff = NULL;
-#else
- WC_RNG tmpRNG[1];
- byte buff[1024];
-#endif
-
- (void)top;
- (void)bottom;
- WOLFSSL_MSG("wolfSSL_BN_rand");
-
- if (bits % 8)
- len++;
-
-#ifdef WOLFSSL_SMALL_STACK
- buff = (byte*)XMALLOC(1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- tmpRNG = (WC_RNG*) XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (buff == NULL || tmpRNG == NULL) {
- XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
- return ret;
- }
-#endif
-
- if (bn == NULL || bn->internal == NULL)
- WOLFSSL_MSG("Bad function arguments");
- else if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else if (initGlobalRNG)
- rng = &globalRNG;
-
- if (rng) {
- if (wc_RNG_GenerateBlock(rng, buff, len) != 0)
- WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
- else {
- buff[0] |= 0x80 | 0x40;
- buff[len-1] |= 0x01;
-
- if (mp_read_unsigned_bin((mp_int*)bn->internal,buff,len) != MP_OKAY)
- WOLFSSL_MSG("mp read bin failed");
- else
- ret = WOLFSSL_SUCCESS;
- }
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- return ret;
-}
-
-
-/* WOLFSSL_SUCCESS on ok
- * code is same as wolfSSL_BN_rand except for how top and bottom is handled.
- * top -1 then leave most sig bit alone
- * top 0 then most sig is set to 1
- * top is 1 then first two most sig bits are 1
- *
- * bottom is hot then odd number */
-int wolfSSL_BN_pseudo_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
-{
- int ret = 0;
- int len = bits / 8;
- int initTmpRng = 0;
- WC_RNG* rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG = NULL;
- byte* buff = NULL;
-#else
- WC_RNG tmpRNG[1];
- byte buff[1024];
-#endif
-
- WOLFSSL_MSG("wolfSSL_BN_rand");
-
- if (bits % 8)
- len++;
-
-#ifdef WOLFSSL_SMALL_STACK
- buff = (byte*)XMALLOC(1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- tmpRNG = (WC_RNG*) XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (buff == NULL || tmpRNG == NULL) {
- XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
-#endif
-
- if (bn == NULL || bn->internal == NULL)
- WOLFSSL_MSG("Bad function arguments");
- else if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else if (initGlobalRNG)
- rng = &globalRNG;
-
- if (rng) {
- if (wc_RNG_GenerateBlock(rng, buff, len) != 0)
- WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
- else {
- switch (top) {
- case -1:
- break;
-
- case 0:
- buff[0] |= 0x80;
- break;
-
- case 1:
- buff[0] |= 0x80 | 0x40;
- break;
- }
-
- if (bottom == 1) {
- buff[len-1] |= 0x01;
- }
-
- if (mp_read_unsigned_bin((mp_int*)bn->internal,buff,len) != MP_OKAY)
- WOLFSSL_MSG("mp read bin failed");
- else
- ret = WOLFSSL_SUCCESS;
- }
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return ret;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if bit set, 0 else
- */
-int wolfSSL_BN_is_bit_set(const WOLFSSL_BIGNUM* bn, int n)
-{
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FAILURE;
- }
-
- return mp_is_bit_set((mp_int*)bn->internal, (mp_digit)n);
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 else
- */
-int wolfSSL_BN_set_bit(WOLFSSL_BIGNUM* bn, int n)
-{
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (mp_set_bit((mp_int*)bn->internal, n) != MP_OKAY) {
- WOLFSSL_MSG("mp_set_bit error");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_BN_clear_bit(WOLFSSL_BIGNUM* bn, int n)
-{
- int ret = WOLFSSL_FAILURE;
-#ifndef WOLFSSL_SMALL_STACK
- mp_int tmp[1];
-#else
- mp_int* tmp = NULL;
-#endif
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- goto cleanup;
- }
- if (mp_is_bit_set((mp_int*)bn->internal, n)) {
-#ifdef WOLFSSL_SMALL_STACK
- tmp = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
- if (tmp == NULL) {
- goto cleanup;
- }
-#endif
- if (mp_init(tmp) != MP_OKAY) {
- goto cleanup;
- }
- if (mp_set_bit(tmp, n) != MP_OKAY) {
- goto cleanup;
- }
- if (mp_sub((mp_int*)bn->internal, tmp, (mp_int*)bn->internal) != MP_OKAY) {
- goto cleanup;
- }
- }
-
- ret = WOLFSSL_SUCCESS;
-cleanup:
- mp_clear(tmp);
-#ifdef WOLFSSL_SMALL_STACK
- if (tmp)
- XFREE(tmp, NULL, DYNAMIC_TYPE_BIGINT);
-#endif
- return ret;
-}
-
-
-/* WOLFSSL_SUCCESS on ok */
-/* Note on use: this function expects str to be an even length. It is
- * converting pairs of bytes into 8-bit values. As an example, the RSA
- * public exponent is commonly 0x010001. To get it to convert, you need
- * to pass in the string "010001", it will fail if you use "10001". This
- * is an affect of how Base16_Decode() works.
- */
-int wolfSSL_BN_hex2bn(WOLFSSL_BIGNUM** bn, const char* str)
-{
- int ret = 0;
- word32 decSz = 1024;
-#ifdef WOLFSSL_SMALL_STACK
- byte* decoded;
-#else
- byte decoded[1024];
-#endif
- int weOwn = 0;
- int strLen;
-
- WOLFSSL_MSG("wolfSSL_BN_hex2bn");
-
-#ifdef WOLFSSL_SMALL_STACK
- decoded = (byte*)XMALLOC(decSz, NULL, DYNAMIC_TYPE_DER);
- if (decoded == NULL)
- return ret;
-#endif
-
- if (str == NULL || str[0] == '\0') {
- WOLFSSL_MSG("Bad function argument");
- ret = WOLFSSL_FAILURE;
- } else {
- strLen = (int)XSTRLEN(str);
- /* ignore trailing new lines */
- while (str[strLen-1] == '\n' && strLen > 0) strLen--;
-
- if (Base16_Decode((byte*)str, strLen, decoded, &decSz) < 0)
- WOLFSSL_MSG("Bad Base16_Decode error");
- else if (bn == NULL)
- ret = decSz;
- else {
- if (*bn == NULL) {
- *bn = wolfSSL_BN_new();
- if (*bn != NULL) {
- weOwn = 1;
- }
- }
-
- if (*bn == NULL)
- WOLFSSL_MSG("BN new failed");
- else if (wolfSSL_BN_bin2bn(decoded, decSz, *bn) == NULL) {
- WOLFSSL_MSG("Bad bin2bn error");
- if (weOwn == 1) {
- wolfSSL_BN_free(*bn); /* Free new BN */
- }
- }
- else
- ret = WOLFSSL_SUCCESS;
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(decoded, NULL, DYNAMIC_TYPE_DER);
-#endif
-
- return ret;
-}
-
-
-WOLFSSL_BIGNUM* wolfSSL_BN_dup(const WOLFSSL_BIGNUM* bn)
-{
- WOLFSSL_BIGNUM* ret;
-
- WOLFSSL_MSG("wolfSSL_BN_dup");
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return NULL;
- }
-
- ret = wolfSSL_BN_new();
- if (ret == NULL) {
- WOLFSSL_MSG("bn new error");
- return NULL;
- }
-
- if (mp_copy((mp_int*)bn->internal, (mp_int*)ret->internal) != MP_OKAY) {
- WOLFSSL_MSG("mp_copy error");
- wolfSSL_BN_free(ret);
- return NULL;
- }
-
- ret->neg = bn->neg;
-
- return ret;
-}
-
-
-WOLFSSL_BIGNUM* wolfSSL_BN_copy(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* bn)
-{
- WOLFSSL_MSG("wolfSSL_BN_copy");
-
- if (r == NULL || bn == NULL) {
- WOLFSSL_MSG("r or bn NULL error");
- return NULL;
- }
-
- if (mp_copy((mp_int*)bn->internal, (mp_int*)r->internal) != MP_OKAY) {
- WOLFSSL_MSG("mp_copy error");
- return NULL;
- }
-
- r->neg = bn->neg;
-
- return r;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 else
- */
-int wolfSSL_BN_set_word(WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w)
-{
- WOLFSSL_MSG("wolfSSL_BN_set_word");
-
- if (bn == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (mp_set_int((mp_int*)bn->internal, w) != MP_OKAY) {
- WOLFSSL_MSG("mp_init_set_int error");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-/* Returns the big number as an unsigned long if possible.
- *
- * bn big number structure to get value from
- *
- * Returns value or 0xFFFFFFFFL if bigger than unsigned long.
- */
-unsigned long wolfSSL_BN_get_word(const WOLFSSL_BIGNUM* bn)
-{
- mp_int* mp;
-
- WOLFSSL_MSG("wolfSSL_BN_get_word");
-
- if (bn == NULL) {
- WOLFSSL_MSG("Invalid argument");
- return 0;
- }
-
- if (wolfSSL_BN_num_bytes(bn) > (int)sizeof(unsigned long)) {
- WOLFSSL_MSG("bignum is larger than unsigned long");
- return 0xFFFFFFFFL;
- }
- mp = (mp_int*)bn->internal;
-
- return (unsigned long)(mp->dp[0]);
-}
-
-/* return code compliant with OpenSSL :
- * number length in decimal if success, 0 if error
- */
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_BN_dec2bn(WOLFSSL_BIGNUM** bn, const char* str)
-{
- (void)bn;
- (void)str;
-
- WOLFSSL_MSG("wolfSSL_BN_dec2bn");
- WOLFSSL_STUB("BN_dec2bn");
- return SSL_FAILURE;
-}
-#endif
-
-#if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
-char *wolfSSL_BN_bn2dec(const WOLFSSL_BIGNUM *bn)
-{
- int len = 0;
- char *buf;
-
- WOLFSSL_MSG("wolfSSL_BN_bn2dec");
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return NULL;
- }
-
- if (mp_radix_size((mp_int*)bn->internal, MP_RADIX_DEC, &len) != MP_OKAY) {
- WOLFSSL_MSG("mp_radix_size failure");
- return NULL;
- }
-
- buf = (char*) XMALLOC(len, NULL, DYNAMIC_TYPE_OPENSSL);
- if (buf == NULL) {
- WOLFSSL_MSG("BN_bn2dec malloc buffer failure");
- return NULL;
- }
-
- if (mp_todecimal((mp_int*)bn->internal, buf) != MP_OKAY) {
- XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
- return NULL;
- }
-
- return buf;
-}
-#else
-char* wolfSSL_BN_bn2dec(const WOLFSSL_BIGNUM* bn)
-{
- (void)bn;
-
- WOLFSSL_MSG("wolfSSL_BN_bn2dec");
-
- return NULL;
-}
-#endif /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) */
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 else
- */
-int wolfSSL_BN_lshift(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *bn, int n)
-{
- WOLFSSL_MSG("wolfSSL_BN_lshift");
-
- if (r == NULL || r->internal == NULL || bn == NULL || bn->internal == NULL){
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (mp_mul_2d((mp_int*)bn->internal, n, (mp_int*)r->internal) != MP_OKAY) {
- WOLFSSL_MSG("mp_mul_2d error");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 else
- */
-int wolfSSL_BN_rshift(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *bn, int n)
-{
- WOLFSSL_MSG("wolfSSL_BN_rshift");
-
- if (r == NULL || r->internal == NULL || bn == NULL || bn->internal == NULL){
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (mp_div_2d((mp_int*)bn->internal, n,
- (mp_int*)r->internal, NULL) != MP_OKAY) {
- WOLFSSL_MSG("mp_mul_2d error");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 else
- */
-int wolfSSL_BN_add_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w)
-{
- WOLFSSL_MSG("wolfSSL_BN_add_word");
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (mp_add_d((mp_int*)bn->internal, w, (mp_int*)bn->internal) != MP_OKAY) {
- WOLFSSL_MSG("mp_add_d error");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-/* return code compliant with OpenSSL :
- * 1 if success, 0 else
- */
-int wolfSSL_BN_add(WOLFSSL_BIGNUM *r, WOLFSSL_BIGNUM *a, WOLFSSL_BIGNUM *b)
-{
- WOLFSSL_MSG("wolfSSL_BN_add");
-
- if (r == NULL || r->internal == NULL || a == NULL || a->internal == NULL ||
- b == NULL || b->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (mp_add((mp_int*)a->internal, (mp_int*)b->internal,
- (mp_int*)r->internal) != MP_OKAY) {
- WOLFSSL_MSG("mp_add_d error");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-/* r = a + b (mod m) */
-int wolfSSL_BN_mod_add(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *a,
- const WOLFSSL_BIGNUM *b, const WOLFSSL_BIGNUM *m,
- WOLFSSL_BN_CTX *ctx)
-{
- (void)ctx;
- WOLFSSL_MSG("wolfSSL_BN_add");
-
- if (r == NULL || r->internal == NULL ||
- a == NULL || a->internal == NULL ||
- b == NULL || b->internal == NULL ||
- m == NULL || m->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FAILURE;
- }
-
- if (mp_addmod((mp_int*)a->internal, (mp_int*)b->internal,
- (mp_int*)m->internal, (mp_int*)r->internal) != MP_OKAY) {
- WOLFSSL_MSG("mp_add_d error");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-#ifdef WOLFSSL_KEY_GEN
-
-/* return code compliant with OpenSSL :
- * 1 if prime, 0 if not, -1 if error
- */
-int wolfSSL_BN_is_prime_ex(const WOLFSSL_BIGNUM *bn, int nbchecks,
- WOLFSSL_BN_CTX *ctx, WOLFSSL_BN_GENCB *cb)
-{
- WC_RNG* rng = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- WC_RNG* tmpRNG = NULL;
-#else
- WC_RNG tmpRNG[1];
-#endif
- int initTmpRng = 0;
- int res = MP_NO;
-
- (void)ctx;
- (void)cb;
-
- WOLFSSL_MSG("wolfSSL_BN_is_prime_ex");
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (tmpRNG == NULL)
- return WOLFSSL_FAILURE;
-#endif
- if (wc_InitRng(tmpRNG) == 0) {
- rng = tmpRNG;
- initTmpRng = 1;
- }
- else {
- WOLFSSL_MSG("Bad RNG Init, trying global");
- if (initGlobalRNG == 0) {
- WOLFSSL_MSG("Global RNG no Init");
- }
- else
- rng = &globalRNG;
- }
-
- if (rng) {
- if (mp_prime_is_prime_ex((mp_int*)bn->internal,
- nbchecks, &res, rng) != MP_OKAY) {
- WOLFSSL_MSG("mp_prime_is_prime_ex error");
- res = MP_NO;
- }
- }
-
- if (initTmpRng)
- wc_FreeRng(tmpRNG);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(tmpRNG, NULL, DYNAMIC_TYPE_RNG);
-#endif
-
- if (res != MP_YES) {
- WOLFSSL_MSG("mp_prime_is_prime_ex not prime");
- return WOLFSSL_FAILURE;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-/* return code compliant with OpenSSL :
- * (bn mod w) if success, -1 if error
- */
-WOLFSSL_BN_ULONG wolfSSL_BN_mod_word(const WOLFSSL_BIGNUM *bn,
- WOLFSSL_BN_ULONG w)
-{
- WOLFSSL_BN_ULONG ret = 0;
-
- WOLFSSL_MSG("wolfSSL_BN_mod_word");
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return (WOLFSSL_BN_ULONG)WOLFSSL_FATAL_ERROR;
- }
-
- if (mp_mod_d((mp_int*)bn->internal, w, &ret) != MP_OKAY) {
- WOLFSSL_MSG("mp_add_d error");
- return (WOLFSSL_BN_ULONG)WOLFSSL_FATAL_ERROR;
- }
-
- return ret;
-}
-#endif /* #ifdef WOLFSSL_KEY_GEN */
-
-char *wolfSSL_BN_bn2hex(const WOLFSSL_BIGNUM *bn)
-{
- int len = 0;
- char *buf;
-
- WOLFSSL_ENTER("wolfSSL_BN_bn2hex");
-
- if (bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return NULL;
- }
-
- if (mp_radix_size((mp_int*)bn->internal, MP_RADIX_HEX, &len) != MP_OKAY) {
- WOLFSSL_MSG("mp_radix_size failure");
- return NULL;
- }
- len += 1; /* add one for null terminator */
-
- buf = (char*)XMALLOC(len, NULL, DYNAMIC_TYPE_OPENSSL);
- if (buf == NULL) {
- WOLFSSL_MSG("BN_bn2hex malloc buffer failure");
- return NULL;
- }
-
- if (mp_tohex((mp_int*)bn->internal, buf) != MP_OKAY) {
- XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
- return NULL;
- }
-
- return buf;
-}
-
-#ifndef NO_FILESYSTEM
-/* return code compliant with OpenSSL :
- * 1 if success, 0 if error
- */
-int wolfSSL_BN_print_fp(XFILE fp, const WOLFSSL_BIGNUM *bn)
-{
- char *buf;
-
- WOLFSSL_ENTER("wolfSSL_BN_print_fp");
-
- if (fp == XBADFILE || bn == NULL || bn->internal == NULL) {
- WOLFSSL_MSG("bn NULL error");
- return WOLFSSL_FAILURE;
- }
-
- buf = wolfSSL_BN_bn2hex(bn);
- if (buf == NULL) {
- WOLFSSL_MSG("wolfSSL_BN_bn2hex failure");
- return WOLFSSL_FAILURE;
- }
-
- fprintf(fp, "%s", buf);
- XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
-
- return WOLFSSL_SUCCESS;
-}
-#endif /* !NO_FILESYSTEM */
-
-
-WOLFSSL_BIGNUM *wolfSSL_BN_CTX_get(WOLFSSL_BN_CTX *ctx)
-{
- /* ctx is not used, return new Bignum */
- (void)ctx;
-
- WOLFSSL_ENTER("wolfSSL_BN_CTX_get");
-
- return wolfSSL_BN_new();
-}
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_BN_CTX_start(WOLFSSL_BN_CTX *ctx)
-{
- (void)ctx;
-
- WOLFSSL_ENTER("wolfSSL_BN_CTX_start");
- WOLFSSL_STUB("BN_CTX_start");
- WOLFSSL_MSG("wolfSSL_BN_CTX_start TBD");
-}
-#endif
-
-
-WOLFSSL_BIGNUM *wolfSSL_BN_mod_inverse(WOLFSSL_BIGNUM *r,
- WOLFSSL_BIGNUM *a,
- const WOLFSSL_BIGNUM *n,
- WOLFSSL_BN_CTX *ctx)
-{
- int dynamic = 0;
-
- /* ctx is not used */
- (void)ctx;
-
- WOLFSSL_ENTER("wolfSSL_BN_mod_inverse");
-
- /* check parameter */
- if (r == NULL) {
- r = wolfSSL_BN_new();
- if (r == NULL){
- WOLFSSL_MSG("WolfSSL_BN_new() failed");
- return NULL;
- }
- dynamic = 1;
- }
-
- if (a == NULL) {
- WOLFSSL_MSG("a NULL error");
- if (dynamic == 1) {
- wolfSSL_BN_free(r);
- }
- return NULL;
- }
-
- if (n == NULL) {
- WOLFSSL_MSG("n NULL error");
- if (dynamic == 1) {
- wolfSSL_BN_free(r);
- }
- return NULL;
- }
-
- /* Compute inverse of a modulo n and return r */
- if (mp_invmod((mp_int *)a->internal,(mp_int *)n->internal,
- (mp_int*)r->internal) == MP_VAL){
- WOLFSSL_MSG("mp_invmod() error");
- if (dynamic == 1) {
- wolfSSL_BN_free(r);
- }
- return NULL;
- }
-
- return r;
-}
-#endif /* OPENSSL_EXTRA */
-#if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_ASN)
-static int unprintable_char(char c)
-{
- const unsigned char last_unprintable = 31;
- const unsigned char LF = 10;
- const unsigned char CR = 13;
-
- if (c <= last_unprintable && c != LF && c != CR) {
- return 1;
- }
- return 0;
-}
-
-int wolfSSL_ASN1_STRING_print(WOLFSSL_BIO *out, WOLFSSL_ASN1_STRING *str)
-{
- int i;
-
- WOLFSSL_ENTER("wolfSSL_ASN1_STRING_print");
- if (out == NULL || str == NULL)
- return WOLFSSL_FAILURE;
-
- for (i=0; i < str->length; i++) {
- if (unprintable_char(str->data[i])) {
- str->data[i] = '.';
- }
- }
-
- if (wolfSSL_BIO_write(out, str->data, str->length) != str->length){
- return WOLFSSL_FAILURE;
- }
-
- return str->length;
-}
-#endif /* (WOLFSSL_QT || OPENSSL_ALL) && !NO_ASN */
-
-#if defined(OPENSSL_EXTRA)
-int wolfSSL_X509_check_ca(WOLFSSL_X509 *x509)
-{
- WOLFSSL_ENTER("X509_check_ca");
-
- if (x509 == NULL)
- return WOLFSSL_FAILURE;
- if (x509->isCa)
- return 1;
- if (x509->extKeyUsageCrit)
- return 4;
-
- return 0;
-}
-
-
-const char *wolfSSL_ASN1_tag2str(int tag)
-{
- static const char *const tag_label[31] = {
- "EOC", "BOOLEAN", "INTEGER", "BIT STRING", "OCTET STRING", "NULL",
- "OBJECT", "OBJECT DESCRIPTOR", "EXTERNAL", "REAL", "ENUMERATED",
- "", "UTF8STRING", "", "", "",
- "SEQUENCE", "SET", "NUMERICSTRING", "PRINTABLESTRING", "T61STRING",
- "VIDEOTEXTSTRING", "IA5STRING", "UTCTIME", "GENERALIZEDTIME",
- "GRAPHICSTRING", "VISIBLESTRING", "GENERALSTRING", "UNIVERSALSTRING",
- "", "BMPSTRING"
- };
-
- if ((tag == V_ASN1_NEG_INTEGER) || (tag == V_ASN1_NEG_ENUMERATED))
- tag &= ~0x100;
- if (tag < 0 || tag > 30)
- return "(unknown)";
- return tag_label[tag];
-}
-
-static int check_esc_char(char c, char *esc)
-{
- char *ptr;
-
- ptr = esc;
- while(*ptr != 0){
- if (c == *ptr)
- return 1;
- ptr++;
- }
- return 0;
-}
-
-int wolfSSL_ASN1_STRING_print_ex(WOLFSSL_BIO *out, WOLFSSL_ASN1_STRING *str,
- unsigned long flags)
-{
- size_t str_len = 0, type_len = 0;
- unsigned char *typebuf = NULL;
- const char *hash="#";
-
- WOLFSSL_ENTER("wolfSSL_ASN1_STRING_PRINT_ex");
- if (out == NULL || str == NULL)
- return WOLFSSL_FAILURE;
-
- /* add ASN1 type tag */
- if (flags & ASN1_STRFLGS_SHOW_TYPE){
- const char *tag = wolfSSL_ASN1_tag2str(str->type);
- /* colon len + tag len + null*/
- type_len = XSTRLEN(tag) + 2;
- typebuf = (unsigned char *)XMALLOC(type_len , NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (typebuf == NULL){
- WOLFSSL_MSG("memory alloc failed.");
- return WOLFSSL_FAILURE;
- }
- XMEMSET(typebuf, 0, type_len);
- XSNPRINTF((char*)typebuf, (size_t)type_len , "%s:", tag);
- type_len--;
- }
-
- /* dump hex */
- if (flags & ASN1_STRFLGS_DUMP_ALL){
- static const char hex_char[] = { '0', '1', '2', '3', '4', '5', '6',
- '7','8', '9', 'A', 'B', 'C', 'D',
- 'E', 'F' };
- char hex_tmp[4];
- char *str_ptr, *str_end;
-
- if (type_len > 0){
- if (wolfSSL_BIO_write(out, typebuf, (int)type_len) != (int)type_len){
- XFREE(typebuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- str_len += type_len;
- }
- if (wolfSSL_BIO_write(out, hash, 1) != 1){
- goto err_exit;
- }
- str_len++;
- if (flags & ASN1_STRFLGS_DUMP_DER){
- hex_tmp[0] = hex_char[str->type >> 4];
- hex_tmp[1] = hex_char[str->type & 0xf];
- hex_tmp[2] = hex_char[str->length >> 4];
- hex_tmp[3] = hex_char[str->length & 0xf];
- if (wolfSSL_BIO_write(out, hex_tmp, 4) != 4){
- goto err_exit;
- }
- str_len += 4;
- XMEMSET(hex_tmp, 0, 4);
- }
-
- str_ptr = str->data;
- str_end = str->data + str->length;
- while (str_ptr < str_end){
- hex_tmp[0] = hex_char[*str_ptr >> 4];
- hex_tmp[1] = hex_char[*str_ptr & 0xf];
- if (wolfSSL_BIO_write(out, hex_tmp, 2) != 2){
- goto err_exit;
- }
- str_ptr++;
- str_len += 2;
- }
- if (type_len > 0)
- XFREE(typebuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- return (int)str_len;
- }
-
- if (type_len > 0){
- if (wolfSSL_BIO_write(out, typebuf, (int)type_len) != (int)type_len){
- XFREE(typebuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
- }
- str_len += type_len;
- }
-
- if (flags & ASN1_STRFLGS_ESC_2253){
- char esc_ch[] = "+;<>\\";
- char* esc_ptr;
-
- esc_ptr = str->data;
- while (*esc_ptr != 0){
- if (check_esc_char(*esc_ptr, esc_ch)){
- if (wolfSSL_BIO_write(out,"\\", 1) != 1)
- goto err_exit;
- str_len++;
- }
- if (wolfSSL_BIO_write(out, esc_ptr, 1) != 1)
- goto err_exit;
- str_len++;
- esc_ptr++;
- }
- if (type_len > 0)
- XFREE(typebuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return (int)str_len;
- }
-
- if (wolfSSL_BIO_write(out, str->data, str->length) != str->length){
- goto err_exit;
- }
- str_len += str->length;
- if (type_len > 0)
- XFREE(typebuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- return (int)str_len;
-
-err_exit:
- if (type_len > 0)
- XFREE(typebuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return WOLFSSL_FAILURE;
-}
-
-#if !defined(NO_ASN_TIME) && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
-WOLFSSL_ASN1_TIME* wolfSSL_ASN1_TIME_new(void)
-{
- WOLFSSL_ASN1_TIME* ret = (WOLFSSL_ASN1_TIME*)
- XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL, DYNAMIC_TYPE_OPENSSL);
- if (!ret)
- return NULL;
- XMEMSET(ret, 0, sizeof(WOLFSSL_ASN1_TIME));
- return ret;
-}
-
-void wolfSSL_ASN1_TIME_free(WOLFSSL_ASN1_TIME* t)
-{
- if (t) {
- XFREE(t, NULL, DYNAMIC_TYPE_OPENSSL);
- }
-}
-
-WOLFSSL_ASN1_TIME* wolfSSL_ASN1_TIME_adj(WOLFSSL_ASN1_TIME *s, time_t t,
- int offset_day, long offset_sec)
-{
- const time_t sec_per_day = 24*60*60;
- struct tm* ts = NULL;
- struct tm* tmpTime;
- time_t t_adj = 0;
- time_t offset_day_sec = 0;
-#if defined(NEED_TMP_TIME)
- struct tm tmpTimeStorage;
-
- tmpTime = &tmpTimeStorage;
-#else
- tmpTime = NULL;
-#endif
- (void)tmpTime;
-
- WOLFSSL_ENTER("wolfSSL_ASN1_TIME_adj");
-
- if (s == NULL){
- s = wolfSSL_ASN1_TIME_new();
- if (s == NULL){
- return NULL;
- }
- }
-
- /* compute GMT time with offset */
- offset_day_sec = offset_day * sec_per_day;
- t_adj = t + offset_day_sec + offset_sec;
- ts = (struct tm *)XGMTIME(&t_adj, tmpTime);
- if (ts == NULL){
- WOLFSSL_MSG("failed to get time data.");
- XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
- return NULL;
- }
-
- /* create ASN1 time notation */
- /* UTC Time */
- if (ts->tm_year >= 50 && ts->tm_year < 150){
- char utc_str[ASN_UTC_TIME_SIZE];
- int utc_year = 0,utc_mon,utc_day,utc_hour,utc_min,utc_sec;
-
- s->type = V_ASN1_UTCTIME;
- s->length = ASN_UTC_TIME_SIZE;
-
- if (ts->tm_year >= 50 && ts->tm_year < 100){
- utc_year = ts->tm_year;
- } else if (ts->tm_year >= 100 && ts->tm_year < 150){
- utc_year = ts->tm_year - 100;
- }
- utc_mon = ts->tm_mon + 1;
- utc_day = ts->tm_mday;
- utc_hour = ts->tm_hour;
- utc_min = ts->tm_min;
- utc_sec = ts->tm_sec;
- XSNPRINTF((char *)utc_str, sizeof(utc_str),
- "%02d%02d%02d%02d%02d%02dZ",
- utc_year, utc_mon, utc_day, utc_hour, utc_min, utc_sec);
- XMEMCPY(s->data, (byte *)utc_str, s->length);
- /* GeneralizedTime */
- } else {
- char gt_str[ASN_GENERALIZED_TIME_MAX];
- int gt_year,gt_mon,gt_day,gt_hour,gt_min,gt_sec;
-
- s->type = V_ASN1_GENERALIZEDTIME;
- s->length = ASN_GENERALIZED_TIME_SIZE;
-
- gt_year = ts->tm_year + 1900;
- gt_mon = ts->tm_mon + 1;
- gt_day = ts->tm_mday;
- gt_hour = ts->tm_hour;
- gt_min = ts->tm_min;
- gt_sec = ts->tm_sec;
- XSNPRINTF((char *)gt_str, sizeof(gt_str),
- "%4d%02d%02d%02d%02d%02dZ",
- gt_year, gt_mon, gt_day, gt_hour, gt_min,gt_sec);
- XMEMCPY(s->data, (byte *)gt_str, s->length);
- }
-
- return s;
-}
-#endif /* !NO_ASN_TIME && !USER_TIME && !TIME_OVERRIDES */
-
-#ifndef NO_ASN_TIME
-/* not a compatibility function - length getter for opaque type */
-int wolfSSL_ASN1_TIME_get_length(WOLFSSL_ASN1_TIME *t)
-{
- WOLFSSL_ENTER("wolfSSL_ASN1_TIME_get_length");
- if (t == NULL)
- return WOLFSSL_FAILURE;
- return t->length;
-}
-/* not a compatibility function - data getter for opaque type */
-unsigned char* wolfSSL_ASN1_TIME_get_data(WOLFSSL_ASN1_TIME *t)
-{
- WOLFSSL_ENTER("wolfSSL_ASN1_TIME_get_data");
- if (t == NULL)
- return NULL;
- return t->data;
-}
-
-WOLFSSL_ASN1_TIME* wolfSSL_ASN1_TIME_to_generalizedtime(WOLFSSL_ASN1_TIME *t,
- WOLFSSL_ASN1_TIME **out)
-{
- int time_type = 0;
- WOLFSSL_ASN1_TIME *ret = NULL;
-
- WOLFSSL_ENTER("wolfSSL_ASN1_TIME_to_generalizedtime");
- if (t == NULL) {
- WOLFSSL_MSG("Invalid ASN_TIME value");
- } else {
- time_type = t->type;
- if (time_type != ASN_UTC_TIME && time_type != ASN_GENERALIZED_TIME){
- WOLFSSL_MSG("Invalid ASN_TIME type.");
- } else {
- if (out == NULL || *out == NULL) {
- ret = wolfSSL_ASN1_TIME_new();
- if (ret == NULL){
- WOLFSSL_MSG("memory alloc failed.");
- }
- } else {
- ret = *out;
- }
- }
- }
-
- if (ret != NULL) {
- if (time_type == ASN_GENERALIZED_TIME){
- XMEMCPY(ret->data, t->data, ASN_GENERALIZED_TIME_SIZE);
- } else { /* ASN_UTC_TIME */
- /* convert UTC to generalized time */
- ret->type = ASN_GENERALIZED_TIME;
- ret->length = ASN_GENERALIZED_TIME_SIZE;
- if (t->data[0] >= '5') {
- ret->data[0] = '1'; ret->data[1] = '9';
- } else {
- ret->data[0] = '2'; ret->data[1] = '0';
- }
- XMEMCPY(&ret->data[2], t->data, ASN_UTC_TIME_SIZE);
- }
- }
-
- return ret;
-}
-#endif /* !NO_ASN_TIME */
-
-#ifndef NO_ASN
-int wolfSSL_i2c_ASN1_INTEGER(WOLFSSL_ASN1_INTEGER *a, unsigned char **pp)
-{
- unsigned char *pptr = NULL;
- char pad = 0 ;
- unsigned char pad_val = 0;
- int ret_size = 0;
- unsigned char data1 = 0;
- unsigned char neg = 0;
- int i = 0;
-
- WOLFSSL_ENTER("wolfSSL_i2c_ASN1_INTEGER");
- if (a == NULL)
- return WOLFSSL_FAILURE;
-
- ret_size = a->intData[1];
- if (ret_size == 0)
- ret_size = 1;
- else{
- ret_size = (int)a->intData[1];
- neg = a->negative;
- data1 = a->intData[2];
- if (ret_size == 1 && data1 == 0)
- neg = 0;
- /* 0x80 or greater positive number in first byte */
- if (!neg && (data1 > 127)){
- pad = 1;
- pad_val = 0;
- } else if (neg){
- /* negative number */
- if (data1 > 128){
- pad = 1;
- pad_val = 0xff;
- } else if (data1 == 128){
- for (i = 3; i < a->intData[1] + 2; i++){
- if (a->intData[i]){
- pad = 1;
- pad_val = 0xff;
- break;
- }
- }
- }
- }
- ret_size += (int)pad;
- }
- if (pp == NULL)
- return ret_size;
-
- pptr = *pp;
- if (pad)
- *(pptr++) = pad_val;
- if (a->intData[1] == 0)
- *(pptr++) = 0;
- else if (!neg){
- /* positive number */
- for (i=0; i < a->intData[1]; i++){
- *pptr = a->intData[i+2];
- pptr++;
- }
- } else {
- /* negative number */
- int str_len = 0;
-
- /* 0 padding from end of buffer */
- str_len = (int)a->intData[1];
- pptr += a->intData[1] - 1;
- while (!a->intData[str_len + 2] && str_len > 1){
- *(pptr--) = 0;
- str_len--;
- }
- /* 2's complement next octet */
- *(pptr--) = ((a->intData[str_len + 1]) ^ 0xff) + 1;
- str_len--;
- /* Complement any octets left */
- while (str_len > 0){
- *(pptr--) = a->intData[str_len + 1] ^ 0xff;
- str_len--;
- }
- }
- *pp += ret_size;
- return ret_size;
-}
-#endif /* !NO_ASN */
-
-
-#ifndef NO_CERTS
-int wolfSSL_X509_CA_num(WOLFSSL_X509_STORE* store)
-{
- int i = 0;
- int cnt_ret = 0;
- Signer **table;
-
- WOLFSSL_ENTER("wolfSSL_X509_CA_num");
- if (store == NULL || store->cm == NULL){
- WOLFSSL_MSG("invalid parameter");
- return WOLFSSL_FAILURE;
- }
-
- table = store->cm->caTable;
- if (table){
- if (wc_LockMutex(&store->cm->caLock) == 0){
- for (i = 0; i < CA_TABLE_SIZE; i++) {
- Signer* signer = table[i];
- while (signer) {
- Signer* next = signer->next;
- cnt_ret++;
- signer = next;
- }
- }
- wc_UnLockMutex(&store->cm->caLock);
- }
- }
-
- return cnt_ret;
-}
-#endif /* !NO_CERTS */
-
-long wolfSSL_X509_get_version(const WOLFSSL_X509 *x509)
-{
- int version = 0;
-
- WOLFSSL_ENTER("wolfSSL_X509_get_version");
-
- if (x509 == NULL){
- WOLFSSL_MSG("invalid parameter");
- return 0L;
- }
- version = x509->version;
- if (version != 0)
- return (long)version - 1L;
-
- return 0L;
-}
-
-
-int wolfSSL_X509_get_signature_nid(const WOLFSSL_X509 *x)
-{
- if (x == NULL)
- return 0;
-
- return oid2nid(x->sigOID, oidSigType);
-}
-#endif /* OPENSSL_EXTRA */
-
-#if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
-/* return compliant with OpenSSL
- * RSA modulus size in bytes, -1 if error
- */
-int wolfSSL_RSA_size(const WOLFSSL_RSA* rsa)
-{
- WOLFSSL_ENTER("wolfSSL_RSA_size");
-
- if (rsa == NULL)
- return WOLFSSL_FATAL_ERROR;
- if (rsa->inSet == 0)
- {
- if (SetRsaInternal((WOLFSSL_RSA*)rsa) != SSL_SUCCESS) {
- WOLFSSL_MSG("SetRsaInternal failed");
- return 0;
- }
- }
- return wc_RsaEncryptSize((RsaKey*)rsa->internal);
-}
-#endif
-
-#if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) && \
- !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
-/* WolfSSL -> OpenSSL */
-int SetRsaExternal(WOLFSSL_RSA* rsa)
-{
- RsaKey* key;
- WOLFSSL_MSG("Entering SetRsaExternal");
-
- if (rsa == NULL || rsa->internal == NULL) {
- WOLFSSL_MSG("rsa key NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- key = (RsaKey*)rsa->internal;
-
- if (SetIndividualExternal(&rsa->n, &key->n) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa n key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&rsa->e, &key->e) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa e key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (key->type == RSA_PRIVATE) {
- if (SetIndividualExternal(&rsa->d, &key->d) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa d key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&rsa->p, &key->p) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa p key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&rsa->q, &key->q) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa q key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- #ifndef RSA_LOW_MEM
- if (SetIndividualExternal(&rsa->dmp1, &key->dP) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa dP key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&rsa->dmq1, &key->dQ) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa dQ key error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (SetIndividualExternal(&rsa->iqmp, &key->u) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("rsa u key error");
- return WOLFSSL_FATAL_ERROR;
- }
- #endif /* !RSA_LOW_MEM */
- }
- rsa->exSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-#endif
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
-/* when calling SetIndividualExternal, mpi should be cleared by caller if no
- * longer used. ie mp_free(mpi). This is to free data when fastmath is
- * disabled since a copy of mpi is made by this function and placed into bn.
- */
-int SetIndividualExternal(WOLFSSL_BIGNUM** bn, mp_int* mpi)
-{
- byte dynamic = 0;
-
- WOLFSSL_MSG("Entering SetIndividualExternal");
-
- if (mpi == NULL || bn == NULL) {
- WOLFSSL_MSG("mpi NULL error");
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (*bn == NULL) {
- *bn = wolfSSL_BN_new();
- if (*bn == NULL) {
- WOLFSSL_MSG("SetIndividualExternal alloc failed");
- return WOLFSSL_FATAL_ERROR;
- }
- dynamic = 1;
- }
-
- if (mp_copy(mpi, (mp_int*)((*bn)->internal)) != MP_OKAY) {
- WOLFSSL_MSG("mp_copy error");
- if (dynamic == 1) {
- wolfSSL_BN_free(*bn);
- }
- return WOLFSSL_FATAL_ERROR;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-static void InitwolfSSL_BigNum(WOLFSSL_BIGNUM* bn)
-{
- if (bn) {
- XMEMSET(bn, 0, sizeof(WOLFSSL_BIGNUM));
- bn->neg = 0;
- bn->internal = NULL;
- }
-}
-
-
-WOLFSSL_BIGNUM* wolfSSL_BN_new(void)
-{
- WOLFSSL_BIGNUM* external;
- mp_int* mpi;
-
- WOLFSSL_MSG("wolfSSL_BN_new");
-
-#if !defined(USE_FAST_MATH) || defined(HAVE_WOLF_BIGINT)
- mpi = (mp_int*) XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
- if (mpi == NULL) {
- WOLFSSL_MSG("wolfSSL_BN_new malloc mpi failure");
- return NULL;
- }
-#endif
-
- external = (WOLFSSL_BIGNUM*) XMALLOC(sizeof(WOLFSSL_BIGNUM), NULL,
- DYNAMIC_TYPE_BIGINT);
- if (external == NULL) {
- WOLFSSL_MSG("wolfSSL_BN_new malloc WOLFSSL_BIGNUM failure");
-#if !defined(USE_FAST_MATH) || defined(HAVE_WOLF_BIGINT)
- XFREE(mpi, NULL, DYNAMIC_TYPE_BIGINT);
-#endif
- return NULL;
- }
-
-#if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
- mpi = &external->fp;
-#endif
-
- InitwolfSSL_BigNum(external);
- if (mp_init(mpi) != MP_OKAY) {
- wolfSSL_BN_free(external);
- return NULL;
- }
- external->internal = mpi;
-
- return external;
-}
-
-
-#if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
-/* This function works without BN_free only with TFM */
-void wolfSSL_BN_init(WOLFSSL_BIGNUM* bn)
-{
- if(bn == NULL)return;
- WOLFSSL_MSG("wolfSSL_BN_init");
- InitwolfSSL_BigNum(bn);
- if (mp_init(&bn->fp) != MP_OKAY)
- return;
- bn->internal = (void *)&bn->fp;
-}
-#endif
-
-void wolfSSL_BN_free(WOLFSSL_BIGNUM* bn)
-{
- WOLFSSL_MSG("wolfSSL_BN_free");
- if (bn) {
- if (bn->internal) {
- mp_int* bni = (mp_int*)bn->internal;
- mp_free(bni);
-#if !defined(USE_FAST_MATH) || defined(HAVE_WOLF_BIGINT)
- XFREE(bn->internal, NULL, DYNAMIC_TYPE_BIGINT);
-#endif
- bn->internal = NULL;
- }
- XFREE(bn, NULL, DYNAMIC_TYPE_BIGINT);
- /* bn = NULL, don't try to access or double free it */
- }
-}
-
-void wolfSSL_BN_clear_free(WOLFSSL_BIGNUM* bn)
-{
- WOLFSSL_MSG("wolfSSL_BN_clear_free");
- if (bn) {
- if (bn->internal) {
- mp_int* bni = (mp_int*)bn->internal;
- mp_forcezero(bni);
- }
- wolfSSL_BN_free(bn);
- }
-}
-
-void wolfSSL_BN_clear(WOLFSSL_BIGNUM* bn)
-{
- WOLFSSL_MSG("wolfSSL_BN_clear");
- if (bn && bn->internal) {
- mp_forcezero((mp_int*)bn->internal);
- }
-}
-#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
-
-#if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
-static void InitwolfSSL_Rsa(WOLFSSL_RSA* rsa)
-{
- if (rsa) {
- XMEMSET(rsa, 0, sizeof(WOLFSSL_RSA));
- }
-}
-
-
-void wolfSSL_RSA_free(WOLFSSL_RSA* rsa)
-{
- WOLFSSL_ENTER("wolfSSL_RSA_free");
-
- if (rsa) {
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
- int doFree = 0;
- if (wc_LockMutex(&rsa->refMutex) != 0) {
- WOLFSSL_MSG("Couldn't lock rsa mutex");
- }
-
- /* only free if all references to it are done */
- rsa->refCount--;
- if (rsa->refCount == 0) {
- doFree = 1;
- }
- wc_UnLockMutex(&rsa->refMutex);
-
- if (!doFree) {
- return;
- }
-
- wc_FreeMutex(&rsa->refMutex);
-#endif
-
- if (rsa->internal) {
-#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && \
- !defined(HAVE_FAST_RSA) && defined(WC_RSA_BLINDING)
- WC_RNG* rng;
-
- /* check if RNG is owned before freeing it */
- if (rsa->ownRng) {
- rng = ((RsaKey*)rsa->internal)->rng;
- if (rng != NULL && rng != &globalRNG) {
- wc_FreeRng(rng);
- XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
- }
- }
-#endif /* WC_RSA_BLINDING */
- wc_FreeRsaKey((RsaKey*)rsa->internal);
- XFREE(rsa->internal, NULL, DYNAMIC_TYPE_RSA);
- rsa->internal = NULL;
- }
- wolfSSL_BN_free(rsa->iqmp);
- wolfSSL_BN_free(rsa->dmq1);
- wolfSSL_BN_free(rsa->dmp1);
- wolfSSL_BN_free(rsa->q);
- wolfSSL_BN_free(rsa->p);
- wolfSSL_BN_free(rsa->d);
- wolfSSL_BN_free(rsa->e);
- wolfSSL_BN_free(rsa->n);
-
- #ifdef WC_RSA_BLINDING
- if (rsa->rng && wc_FreeRng(rsa->rng) != 0) {
- WOLFSSL_MSG("Issue freeing rng");
- }
- XFREE(rsa->rng, NULL, DYNAMIC_TYPE_RNG);
- #endif
-
-#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
- if (rsa->meth) {
- wolfSSL_RSA_meth_free(rsa->meth);
- }
-#endif
-
- InitwolfSSL_Rsa(rsa); /* set back to NULLs for safety */
-
- XFREE(rsa, NULL, DYNAMIC_TYPE_RSA);
-
- /* rsa = NULL, don't try to access or double free it */
- }
-}
-
-WOLFSSL_RSA* wolfSSL_RSA_new(void)
-{
- WOLFSSL_RSA* external;
- RsaKey* key;
-
- WOLFSSL_ENTER("wolfSSL_RSA_new");
-
- key = (RsaKey*) XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
- if (key == NULL) {
- WOLFSSL_MSG("wolfSSL_RSA_new malloc RsaKey failure");
- return NULL;
- }
-
- external = (WOLFSSL_RSA*) XMALLOC(sizeof(WOLFSSL_RSA), NULL,
- DYNAMIC_TYPE_RSA);
- if (external == NULL) {
- WOLFSSL_MSG("wolfSSL_RSA_new malloc WOLFSSL_RSA failure");
- XFREE(key, NULL, DYNAMIC_TYPE_RSA);
- return NULL;
- }
-
- InitwolfSSL_Rsa(external);
- if (wc_InitRsaKey(key, NULL) != 0) {
- WOLFSSL_MSG("InitRsaKey WOLFSSL_RSA failure");
- XFREE(external, NULL, DYNAMIC_TYPE_RSA);
- XFREE(key, NULL, DYNAMIC_TYPE_RSA);
- return NULL;
- }
-
-#if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && \
- !defined(HAVE_FAST_RSA) && defined(WC_RSA_BLINDING)
- {
- WC_RNG* rng;
-
- rng = (WC_RNG*) XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
- if (rng != NULL && wc_InitRng(rng) != 0) {
- WOLFSSL_MSG("InitRng failure, attempting to use global RNG");
- XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
- rng = NULL;
- }
-
- external->ownRng = 1;
- if (rng == NULL && initGlobalRNG) {
- external->ownRng = 0;
- rng = &globalRNG;
- }
-
- if (rng == NULL) {
- WOLFSSL_MSG("wolfSSL_RSA_new no WC_RNG for blinding");
- XFREE(external, NULL, DYNAMIC_TYPE_RSA);
- XFREE(key, NULL, DYNAMIC_TYPE_RSA);
- return NULL;
- }
-
- wc_RsaSetRNG(key, rng);
- }
-#else
- XMEMSET(key, 0, sizeof(RsaKey));
-#endif /* WC_RSA_BLINDING */
-
- external->internal = key;
- external->inSet = 0;
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
- external->refCount = 1;
- wc_InitMutex(&external->refMutex);
-#endif
- return external;
-}
-#endif /* !NO_RSA && (OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL) */
-
-#if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
-PKCS7* wolfSSL_PKCS7_new(void)
-{
- WOLFSSL_PKCS7* pkcs7;
- int ret = 0;
-
- pkcs7 = (WOLFSSL_PKCS7*)XMALLOC(sizeof(*pkcs7), NULL, DYNAMIC_TYPE_PKCS7);
- if (pkcs7 != NULL) {
- XMEMSET(pkcs7, 0, sizeof(*pkcs7));
- ret = wc_PKCS7_Init(&pkcs7->pkcs7, NULL, INVALID_DEVID);
- }
-
- if (ret != 0 && pkcs7 != NULL)
- XFREE(pkcs7, NULL, DYNAMIC_TYPE_PKCS7);
-
- return (PKCS7*)pkcs7;
-}
-/******************************************************************************
-* wolfSSL_PKCS7_SIGNED_new - allocates PKCS7 and initialize it for a signed data
-*
-* RETURNS:
-* returns pointer to the PKCS7 structure on success, otherwise returns NULL
-*/
-PKCS7_SIGNED* wolfSSL_PKCS7_SIGNED_new(void)
-{
- byte signedData[]= { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x02};
- PKCS7* pkcs7 = NULL;
-
- if ((pkcs7 = wolfSSL_PKCS7_new()) == NULL)
- return NULL;
- pkcs7->contentOID = SIGNED_DATA;
- if ((wc_PKCS7_SetContentType(pkcs7, signedData, sizeof(signedData))) < 0) {
- if (pkcs7) {
- wolfSSL_PKCS7_free(pkcs7);
- return NULL;
- }
- }
- return pkcs7;
-}
-
-void wolfSSL_PKCS7_free(PKCS7* pkcs7)
-{
- WOLFSSL_PKCS7* p7 = (WOLFSSL_PKCS7*)pkcs7;
-
- if (p7 != NULL) {
- if (p7->data != NULL)
- XFREE(p7->data, NULL, DYNAMIC_TYPE_PKCS7);
- wc_PKCS7_Free(&p7->pkcs7);
- XFREE(p7, NULL, DYNAMIC_TYPE_PKCS7);
- }
-}
-void wolfSSL_PKCS7_SIGNED_free(PKCS7_SIGNED* p7)
-{
- wolfSSL_PKCS7_free(p7);
- return;
-}
-PKCS7* wolfSSL_d2i_PKCS7(PKCS7** p7, const unsigned char** in, int len)
-{
- WOLFSSL_PKCS7* pkcs7 = NULL;
- word32 idx = 0;
-
- if (in == NULL)
- return NULL;
-
- if ((pkcs7 = (WOLFSSL_PKCS7*)wolfSSL_PKCS7_new()) == NULL)
- return NULL;
-
- if (GetSequence(*in, &idx, &pkcs7->len, len) < 0) {
- wolfSSL_PKCS7_free((PKCS7*)pkcs7);
- return NULL;
- }
- pkcs7->len += idx;
-
- pkcs7->data = (byte*)XMALLOC(pkcs7->len, NULL, DYNAMIC_TYPE_PKCS7);
- if (pkcs7->data == NULL) {
- wolfSSL_PKCS7_free((PKCS7*)pkcs7);
- return NULL;
- }
- XMEMCPY(pkcs7->data, *in, pkcs7->len);
- *in += pkcs7->len;
-
- if (p7 != NULL)
- *p7 = (PKCS7*)pkcs7;
- return (PKCS7*)pkcs7;
-}
-
-PKCS7* wolfSSL_d2i_PKCS7_bio(WOLFSSL_BIO* bio, PKCS7** p7)
-{
- WOLFSSL_PKCS7* pkcs7;
-
- if (bio == NULL)
- return NULL;
-
- if ((pkcs7 = (WOLFSSL_PKCS7*)wolfSSL_PKCS7_new()) == NULL)
- return NULL;
-
- pkcs7->len = wolfSSL_BIO_pending(bio);
- pkcs7->data = (byte*)XMALLOC(pkcs7->len, NULL, DYNAMIC_TYPE_PKCS7);
- if (pkcs7->data == NULL) {
- wolfSSL_PKCS7_free((PKCS7*)pkcs7);
- return NULL;
- }
-
- if (wolfSSL_BIO_read(bio, pkcs7->data, pkcs7->len) != pkcs7->len) {
- wolfSSL_PKCS7_free((PKCS7*)pkcs7);
- return NULL;
- }
-
- if (p7 != NULL)
- *p7 = (PKCS7*)pkcs7;
- return (PKCS7*)pkcs7;
-}
-
-int wolfSSL_PKCS7_verify(PKCS7* pkcs7, WOLFSSL_STACK* certs,
- WOLFSSL_X509_STORE* store, WOLFSSL_BIO* in,
- WOLFSSL_BIO* out, int flags)
-{
- int ret = 0;
- unsigned char* mem = NULL;
- int memSz = 0;
- WOLFSSL_PKCS7* p7 = (WOLFSSL_PKCS7*)pkcs7;
-
- if (pkcs7 == NULL)
- return WOLFSSL_FAILURE;
-
- if (in != NULL) {
- if ((memSz = wolfSSL_BIO_get_mem_data(in, &mem)) < 0)
- return WOLFSSL_FAILURE;
-
- p7->pkcs7.content = mem;
- p7->pkcs7.contentSz = memSz;
- }
-
- /* certs is the list of certificates to find the cert with issuer/serial. */
- (void)certs;
- /* store is the certificate store to use to verify signer certificate
- * associated with the signers.
- */
- (void)store;
-
- ret = wc_PKCS7_VerifySignedData_ex(&p7->pkcs7, NULL, 0, p7->data, p7->len,
- NULL, 0);
- if (ret != 0)
- return WOLFSSL_FAILURE;
-
- if ((flags & PKCS7_NOVERIFY) != PKCS7_NOVERIFY) {
- /* All signer certificates are verified. */
- return WOLFSSL_FAILURE;
- }
-
- if (out != NULL)
- wolfSSL_BIO_write(out, p7->pkcs7.content, p7->pkcs7.contentSz);
-
- return WOLFSSL_SUCCESS;
-}
-
-WOLFSSL_STACK* wolfSSL_PKCS7_get0_signers(PKCS7* pkcs7, WOLFSSL_STACK* certs,
- int flags)
-{
- WOLFSSL_STACK* signers = NULL;
- WOLFSSL_PKCS7* p7 = (WOLFSSL_PKCS7*)pkcs7;
-
- if (p7 == NULL)
- return NULL;
- /* Only PKCS#7 messages with a single cert that is the verifying certificate
- * is supported.
- */
- if ((flags | PKCS7_NOINTERN) == PKCS7_NOINTERN)
- return NULL;
-
- signers = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_X509);
- if (signers == NULL)
- return NULL;
-
- signers->num = 1;
- signers->data.x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
- DYNAMIC_TYPE_X509);
- if (signers->data.x509 == NULL) {
- XFREE(signers, NULL, DYNAMIC_TYPE_X509);
- return NULL;
- }
-
- if (DecodeToX509(signers->data.x509, p7->pkcs7.singleCert,
- p7->pkcs7.singleCertSz) != 0) {
- XFREE(signers->data.x509, NULL, DYNAMIC_TYPE_X509);
- XFREE(signers, NULL, DYNAMIC_TYPE_X509);
- return NULL;
- }
-
- (void)certs;
-
- return signers;
-}
-
-/******************************************************************************
-* wolfSSL_PEM_write_bio_PKCS7 - writes the PKCS7 data to BIO
-*
-* RETURNS:
-* returns WOLFSSL_SUCCESS on success, otherwise returns WOLFSSL_FAILURE
-*/
-
-int wolfSSL_PEM_write_bio_PKCS7(WOLFSSL_BIO* bio, PKCS7* p7)
-{
-#ifdef WOLFSSL_SMALL_STACK
- byte* outputHead;
- byte* outputFoot;
-#else
- byte outputHead[2048];
- byte outputFoot[2048];
-#endif
- word32 outputHeadSz = 2048;
- word32 outputFootSz = 2048;
- word32 outputSz = 0;
- byte* output = NULL;
- byte* pem = NULL;
- int pemSz = -1;
- enum wc_HashType hashType;
- byte hashBuf[WC_MAX_DIGEST_SIZE];
- word32 hashSz = -1;
-
- WOLFSSL_ENTER("wolfSSL_PEM_write_bio_PKCS7()");
-
- if (bio == NULL || p7 == NULL)
- return WOLFSSL_FAILURE;
-
-#ifdef WOLFSSL_SMALL_STACK
- outputHead = (byte*)XMALLOC(outputHeadSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (outputHead == NULL)
- return MEMORY_E;
-
- outputFoot = (byte*)XMALLOC(outputFootSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (outputFoot == NULL)
- goto error;
-
-#endif
-
- XMEMSET(hashBuf, 0, WC_MAX_DIGEST_SIZE);
- XMEMSET(outputHead, 0, outputHeadSz);
- XMEMSET(outputFoot, 0, outputFootSz);
-
- hashType = wc_OidGetHash(p7->hashOID);
- hashSz = wc_HashGetDigestSize(hashType);
- if (hashSz > WC_MAX_DIGEST_SIZE)
- return WOLFSSL_FAILURE;
-
- /* only SIGNED_DATA is supported */
- switch (p7->contentOID) {
- case SIGNED_DATA:
- break;
- default:
- WOLFSSL_MSG("Unknown PKCS#7 Type");
- return WOLFSSL_FAILURE;
- };
-
- if ((wc_PKCS7_EncodeSignedData_ex(p7, hashBuf, hashSz,
- outputHead, &outputHeadSz, outputFoot, &outputFootSz)) != 0)
- return WOLFSSL_FAILURE;
-
- outputSz = outputHeadSz + p7->contentSz + outputFootSz;
- output = (byte*)XMALLOC(outputSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
-
- if (!output)
- return WOLFSSL_FAILURE;
-
- XMEMSET(output, 0, outputSz);
- outputSz = 0;
- XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
- outputSz += outputHeadSz;
- XMEMCPY(&output[outputSz], p7->content, p7->contentSz);
- outputSz += p7->contentSz;
- XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
- outputSz += outputFootSz;
-
- /* get PEM size */
- pemSz = wc_DerToPemEx(output, outputSz, NULL, 0, NULL, CERT_TYPE);
- if (pemSz < 0)
- goto error;
-
- pemSz++; /* for '\0'*/
-
- /* create PEM buffer and convert from DER to PEM*/
- if ((pem = (byte*)XMALLOC(pemSz, bio->heap, DYNAMIC_TYPE_TMP_BUFFER)) == NULL)
- goto error;
-
- XMEMSET(pem, 0, pemSz);
-
- if (wc_DerToPemEx(output, outputSz, pem, pemSz, NULL, CERT_TYPE) < 0) {
- goto error;
- }
- if ((wolfSSL_BIO_write(bio, pem, pemSz) == pemSz)) {
- XFREE(output, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(pem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(outputHead, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(outputFoot, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return WOLFSSL_SUCCESS;
- }
-
-error:
-#ifdef WOLFSSL_SMALL_STACK
- if (outputHead) {
- XFREE(outputHead, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
- if (outputFoot) {
- XFREE(outputFoot, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-#endif
- if (output) {
- XFREE(output, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
- if (pem) {
- XFREE(pem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
- return WOLFSSL_FAILURE;
-}
-#endif /* OPENSSL_ALL && HAVE_PKCS7 */
-
-#if defined(OPENSSL_EXTRA)
-WOLFSSL_STACK* wolfSSL_sk_X509_new(void)
-{
- WOLFSSL_STACK* s = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
- DYNAMIC_TYPE_X509);
- if (s != NULL) {
- XMEMSET(s, 0, sizeof(*s));
- s->type = STACK_TYPE_X509;
- }
-
- return s;
-}
-#endif
-
-#ifdef OPENSSL_ALL
-int wolfSSL_PEM_write_bio_PKCS8PrivateKey(WOLFSSL_BIO* bio,
- WOLFSSL_EVP_PKEY* pkey,
- const WOLFSSL_EVP_CIPHER* enc,
- char* passwd, int passwdSz,
- pem_password_cb* cb, void* ctx)
-{
- int ret = 0;
- char password[NAME_SZ];
- byte* key = NULL;
- word32 keySz;
- byte* pem = NULL;
- int pemSz;
- int type = PKCS8_PRIVATEKEY_TYPE;
- int algId;
- const byte* curveOid;
- word32 oidSz;
- int encAlgId;
-
- if (bio == NULL || pkey == NULL)
- return -1;
-
- keySz = pkey->pkey_sz + 128;
- key = (byte*)XMALLOC(keySz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (key == NULL)
- ret = MEMORY_E;
-
- if (ret == 0 && enc != NULL && passwd == NULL) {
- passwdSz = cb(password, sizeof(password), 1, ctx);
- if (passwdSz < 0)
- ret = WOLFSSL_FAILURE;
- passwd = password;
- }
-
- if (ret == 0 && enc != NULL) {
- WC_RNG rng;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- #ifndef NO_DES3
- if (enc == EVP_DES_CBC)
- encAlgId = DESb;
- else if (enc == EVP_DES_EDE3_CBC)
- encAlgId = DES3b;
- else
- #endif
- #if !defined(NO_AES) && defined(HAVE_AES_CBC)
- #ifdef WOLFSSL_AES_256
- if (enc == EVP_AES_256_CBC)
- encAlgId = AES256CBCb;
- else
- #endif
- #endif
- ret = -1;
- if (ret == 0) {
- ret = TraditionalEnc((byte*)pkey->pkey.ptr, pkey->pkey_sz, key,
- &keySz, passwd, passwdSz, PKCS5, PBES2,
- encAlgId, NULL, 0, WC_PKCS12_ITT_DEFAULT,
- &rng, NULL);
- if (ret > 0) {
- keySz = ret;
- ret = 0;
- }
- }
- wc_FreeRng(&rng);
- }
- type = PKCS8_ENC_PRIVATEKEY_TYPE;
- }
- if (ret == 0 && enc == NULL) {
- type = PKCS8_PRIVATEKEY_TYPE;
- if (pkey->type == EVP_PKEY_EC) {
- algId = ECDSAk;
- ret = wc_ecc_get_oid(pkey->ecc->group->curve_oid, &curveOid,
- &oidSz);
- }
- else {
- algId = RSAk;
- curveOid = NULL;
- oidSz = 0;
- }
-
- if (ret >= 0) {
- ret = wc_CreatePKCS8Key(key, &keySz, (byte*)pkey->pkey.ptr,
- pkey->pkey_sz, algId, curveOid, oidSz);
- keySz = ret;
- }
- }
-
- if (password == passwd)
- XMEMSET(password, 0, passwdSz);
-
- if (ret >= 0) {
- pemSz = 2 * keySz + 2 * 64;
- pem = (byte*)XMALLOC(pemSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (pem == NULL)
- ret = MEMORY_E;
- }
-
- if (ret >= 0)
- ret = wc_DerToPemEx(key, keySz, pem, pemSz, NULL, type);
-
- if (key != NULL)
- XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if (ret >= 0) {
- if (wolfSSL_BIO_write(bio, pem, ret) != ret)
- ret = -1;
- }
-
- if (pem != NULL)
- XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- return ret < 0 ? 0 : ret;
-
-}
-
-static int bio_get_data(WOLFSSL_BIO* bio, byte** data)
-{
- int ret = 0;
- byte* mem = NULL;
-#ifndef NO_FILESYSTEM
- long memSz;
- XFILE file;
- long curr;
-#endif
-
- if ((ret = wolfSSL_BIO_pending(bio)) > 0) {
- }
-#ifndef NO_FILESYSTEM
- else if (bio->type == WOLFSSL_BIO_FILE) {
- if (wolfSSL_BIO_get_fp(bio, &file) != WOLFSSL_SUCCESS)
- ret = BAD_FUNC_ARG;
- if (ret == 0) {
- curr = XFTELL(file);
- if (curr < 0) {
- ret = WOLFSSL_BAD_FILE;
- }
- if (XFSEEK(file, 0, XSEEK_END) != 0)
- ret = WOLFSSL_BAD_FILE;
- }
- if (ret == 0) {
- memSz = XFTELL(file);
- if (memSz > MAX_WOLFSSL_FILE_SIZE || memSz < 0) {
- ret = WOLFSSL_BAD_FILE;
- }
- }
- if (ret == 0) {
- memSz -= curr;
- ret = (int)memSz;
- if (XFSEEK(file, curr, SEEK_SET) != 0)
- ret = WOLFSSL_BAD_FILE;
- }
- }
-#endif
-
- if (ret > 0) {
- mem = (byte*)XMALLOC(ret, bio->heap, DYNAMIC_TYPE_OPENSSL);
- if (mem == NULL) {
- WOLFSSL_MSG("Memory error");
- ret = MEMORY_E;
- }
- if (ret >= 0) {
- if ((ret = wolfSSL_BIO_read(bio, mem, ret)) <= 0) {
- XFREE(mem, bio->heap, DYNAMIC_TYPE_OPENSSL);
- ret = MEMORY_E;
- mem = NULL;
- }
- }
- }
-
- *data = mem;
-
- return ret;
-}
-
-#ifndef NO_WOLFSSL_STUB
-void wolfSSL_BIO_set_init(WOLFSSL_BIO* bio, int init)
-{
- WOLFSSL_STUB("wolfSSL_BIO_set_init");
- (void)bio;
- (void)init;
-}
-
-void wolfSSL_BIO_set_shutdown(WOLFSSL_BIO* bio, int shut)
-{
- WOLFSSL_STUB("wolfSSL_BIO_set_shutdown");
- (void)bio;
- (void)shut;
-
-}
-int wolfSSL_BIO_get_shutdown(WOLFSSL_BIO* bio)
-{
- WOLFSSL_STUB("wolfSSL_BIO_get_shutdown");
- (void)bio;
- return 0;
-}
-#endif /* NO_WOLFSSL_STUB */
-
-void wolfSSL_BIO_clear_retry_flags(WOLFSSL_BIO* bio)
-{
- WOLFSSL_ENTER("wolfSSL_BIO_clear_retry_flags");
-
- if (bio)
- bio->flags &= ~(WOLFSSL_BIO_FLAG_READ|WOLFSSL_BIO_FLAG_RETRY);
-}
-
-int wolfSSL_BIO_should_retry(WOLFSSL_BIO *bio)
-{
- int ret = 0;
- if (bio != NULL) {
- ret = (int)(bio->flags & WOLFSSL_BIO_FLAG_RETRY);
- }
-
- return ret;
-}
-
-/* DER data is PKCS#8 encrypted. */
-WOLFSSL_EVP_PKEY* wolfSSL_d2i_PKCS8PrivateKey_bio(WOLFSSL_BIO* bio,
- WOLFSSL_EVP_PKEY** pkey,
- pem_password_cb* cb,
- void* ctx)
-{
- int ret;
- byte* der;
- int len;
- byte* p;
- char password[NAME_SZ];
- int passwordSz;
- word32 algId;
- WOLFSSL_EVP_PKEY* key;
-
- if ((len = bio_get_data(bio, &der)) < 0)
- return NULL;
-
- if (cb != NULL) {
- passwordSz = cb(password, sizeof(password), PEM_PASS_READ, ctx);
- if (passwordSz < 0) {
- XFREE(der, bio->heap, DYNAMIC_TYPE_OPENSSL);
- return NULL;
- }
-
- ret = ToTraditionalEnc(der, len, password, passwordSz, &algId);
- if (ret < 0) {
- XFREE(der, bio->heap, DYNAMIC_TYPE_OPENSSL);
- return NULL;
- }
-
- XMEMSET(password, 0, passwordSz);
- }
-
- p = der;
- key = wolfSSL_d2i_PrivateKey_EVP(pkey, &p, len);
- XFREE(der, bio->heap, DYNAMIC_TYPE_OPENSSL);
- return key;
-}
-
-/* Detect which type of key it is before decoding. */
-WOLFSSL_EVP_PKEY* wolfSSL_d2i_AutoPrivateKey(WOLFSSL_EVP_PKEY** pkey,
- const unsigned char** pp,
- long length)
-{
- int ret;
- WOLFSSL_EVP_PKEY* key = NULL;
- const byte* der = *pp;
- word32 idx = 0;
- int len = 0;
- word32 end = 0;
- int cnt = 0;
- int type;
- word32 algId;
- word32 keyLen = (word32)length;
-
- /* Take off PKCS#8 wrapper if found. */
- if ((len = ToTraditionalInline_ex(der, &idx, keyLen, &algId)) >= 0) {
- der += idx;
- keyLen = len;
- }
- idx = 0;
- len = 0;
-
- /* Use the number of elements in the outer sequence to determine key type.
- */
- ret = GetSequence(der, &idx, &len, keyLen);
- if (ret >= 0) {
- end = idx + len;
- while (ret >= 0 && idx < end) {
- /* Skip type */
- idx++;
- /* Get length and skip over - keeping count */
- len = 0;
- ret = GetLength(der, &idx, &len, keyLen);
- if (ret >= 0) {
- if (idx + len > end)
- ret = ASN_PARSE_E;
- else {
- idx += len;
- cnt++;
- }
- }
- }
- }
-
- if (ret >= 0) {
- /* ECC includes version, private[, curve][, public key] */
- if (cnt >= 2 && cnt <= 4)
- type = EVP_PKEY_EC;
- else
- type = EVP_PKEY_RSA;
-
- key = wolfSSL_d2i_PrivateKey(type, pkey, &der, keyLen);
- *pp = der;
- }
-
- return key;
-}
-#endif
-
-#if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
-int wolfSSL_X509_set_subject_name(WOLFSSL_X509 *cert, WOLFSSL_X509_NAME *name)
-{
- int i;
- WOLFSSL_X509_NAME_ENTRY* ne;
-
- WOLFSSL_ENTER("X509_set_subject_name");
- if (cert == NULL || name == NULL)
- return WOLFSSL_FAILURE;
-
- FreeX509Name(&cert->subject, cert->heap);
- InitX509Name(&cert->subject, 0);
- if (name->dynamicName) {
- cert->subject.name = (char*)XMALLOC(name->sz, cert->heap,
- DYNAMIC_TYPE_SUBJECT_CN);
- if (cert->subject.name == NULL)
- return WOLFSSL_FAILURE;
- }
- XMEMCPY(cert->subject.name, name->name, name->sz);
- cert->subject.sz = name->sz;
-
- for (i = 0; i < 10; i++) {
- ne = wolfSSL_X509_NAME_get_entry(name, i);
- if (ne != NULL)
- wolfSSL_X509_NAME_add_entry(&cert->subject, ne, i, 1);
- }
- cert->subject.x509 = cert;
- cert->subject.name = cert->subject.fullName.fullName;
- cert->subject.sz = cert->subject.fullName.fullNameLen;
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_X509_set_issuer_name(WOLFSSL_X509 *cert, WOLFSSL_X509_NAME *name)
-{
- int i;
- WOLFSSL_X509_NAME_ENTRY* ne;
-
- WOLFSSL_ENTER("X509_set_issuer_name");
- if (cert == NULL || name == NULL)
- return WOLFSSL_FAILURE;
-
- FreeX509Name(&cert->issuer, cert->heap);
- InitX509Name(&cert->issuer, 0);
- if (name->dynamicName) {
- cert->issuer.name = (char*)XMALLOC(name->sz, cert->heap,
- DYNAMIC_TYPE_SUBJECT_CN);
- if (cert->issuer.name == NULL)
- return WOLFSSL_FAILURE;
- }
- XMEMCPY(cert->issuer.name, name->name, name->sz);
- cert->issuer.sz = name->sz;
-
- for (i = 0; i < 10; i++) {
- ne = wolfSSL_X509_NAME_get_entry(name, i);
- if (ne != NULL)
- wolfSSL_X509_NAME_add_entry(&cert->issuer, ne, i, 1);
- }
- cert->issuer.x509 = cert;
- cert->issuer.name = cert->issuer.fullName.fullName;
- cert->issuer.sz = cert->issuer.fullName.fullNameLen;
- cert->issuerSet = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_X509_set_notAfter(WOLFSSL_X509* x509, const WOLFSSL_ASN1_TIME* t)
-{
- if (x509 == NULL || t == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- XMEMCPY(&x509->notAfter, t, sizeof(WOLFSSL_ASN1_TIME));
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_X509_set_notBefore(WOLFSSL_X509* x509, const WOLFSSL_ASN1_TIME* t)
-{
- if (x509 == NULL || t == NULL) {
- return WOLFSSL_FAILURE;
- }
-
- XMEMCPY(&x509->notBefore, t, sizeof(WOLFSSL_ASN1_TIME));
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_X509_set_serialNumber(WOLFSSL_X509* x509, WOLFSSL_ASN1_INTEGER* s)
-{
- WOLFSSL_ENTER("wolfSSL_X509_set_serialNumber");
- if (!x509 || !s || s->dataMax >= EXTERNAL_SERIAL_SIZE)
- return WOLFSSL_FAILURE;
-
- if (s->isDynamic)
- XSTRNCPY((char*)x509->serial,(char*)s->data,s->dataMax);
- else
- XSTRNCPY((char*)x509->serial,(char*)s->intData,s->dataMax);
-
- x509->serial[s->dataMax] = 0;
- x509->serialSz = s->dataMax;
-
- return WOLFSSL_SUCCESS;
-}
-
-
-int wolfSSL_X509_set_pubkey(WOLFSSL_X509 *cert, WOLFSSL_EVP_PKEY *pkey)
-{
- byte* p;
- WOLFSSL_ENTER("wolfSSL_X509_set_pubkey");
-
- if (cert == NULL || pkey == NULL)
- return WOLFSSL_FAILURE;
-
- if (pkey->type == EVP_PKEY_RSA)
- cert->pubKeyOID = RSAk;
- else if (pkey->type == EVP_PKEY_EC)
- cert->pubKeyOID = ECDSAk;
- else
- return WOLFSSL_FAILURE;
-
- p = (byte*)XMALLOC(pkey->pkey_sz, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (p == NULL)
- return WOLFSSL_FAILURE;
-
- if (cert->pubKey.buffer != NULL)
- XFREE(cert->pubKey.buffer, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- cert->pubKey.buffer = p;
- XMEMCPY(cert->pubKey.buffer, pkey->pkey.ptr, pkey->pkey_sz);
- cert->pubKey.length = pkey->pkey_sz;
-
- return WOLFSSL_SUCCESS;
-}
-
-int wolfSSL_X509_set_version(WOLFSSL_X509* x509, long v)
-{
- WOLFSSL_ENTER("wolfSSL_X509_set_version");
- if ((x509 == NULL) || (v < 0) || (v > INT_MAX)) {
- return WOLFSSL_FAILURE;
- }
- x509->version = (int) v + 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* OPENSSL_EXTRA && !NO_CERTS && WOLFSSL_CERT_GEN && WOLFSSL_CERT_REQ */
-
-#if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
-
-void wolfSSL_X509V3_set_ctx(WOLFSSL_X509V3_CTX* ctx, WOLFSSL_X509* issuer,
- WOLFSSL_X509* subject, WOLFSSL_X509* req, WOLFSSL_X509_CRL* crl,
- int flag)
-{
- int ret = WOLFSSL_SUCCESS;
- WOLFSSL_ENTER("wolfSSL_X509V3_set_ctx");
- if (!ctx || !ctx->x509)
- return;
-
- /* Set parameters in ctx as long as ret == WOLFSSL_SUCCESS */
- if (issuer)
- ret = wolfSSL_X509_set_issuer_name(ctx->x509,&issuer->issuer);
-
- if (subject && ret == WOLFSSL_SUCCESS)
- ret = wolfSSL_X509_set_subject_name(ctx->x509,&subject->subject);
-
- if (req && ret == WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("req not implemented.");
- }
-
- if (crl && ret == WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("crl not implemented.");
- }
-
- if (flag && ret == WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("flag not implemented.");
- }
-
- if (!ret) {
- WOLFSSL_MSG("Error setting WOLFSSL_X509V3_CTX parameters.");
- }
-}
-
-int wolfSSL_i2d_X509_REQ(WOLFSSL_X509* req, unsigned char** out)
-{
- const unsigned char* der;
- int derSz = 0;
- WOLFSSL_ENTER("wolfSSL_i2d_X509_REQ");
-
- if (req == NULL || out == NULL) {
- return BAD_FUNC_ARG;
- }
-
- der = wolfSSL_X509_get_der(req, &derSz);
- if (der == NULL) {
- return MEMORY_E;
- }
-
- if (*out == NULL) {
- *out = (unsigned char*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_OPENSSL);
- if (*out == NULL) {
- return MEMORY_E;
- }
- }
-
- XMEMCPY(*out, der, derSz);
-
- return derSz;
-}
-
-WOLFSSL_X509* wolfSSL_X509_REQ_new(void)
-{
- return wolfSSL_X509_new();
-}
-
-void wolfSSL_X509_REQ_free(WOLFSSL_X509* req)
-{
- wolfSSL_X509_free(req);
-}
-
-int wolfSSL_X509_REQ_sign(WOLFSSL_X509 *req, WOLFSSL_EVP_PKEY *pkey,
- const WOLFSSL_EVP_MD *md)
-{
- byte der[2048];
- int derSz = sizeof(der);
-
- if (req == NULL || pkey == NULL || md == NULL)
- return WOLFSSL_FAILURE;
-
- /* Create a Cert that has the certificate request fields. */
- req->sigOID = wolfSSL_sigTypeFromPKEY((WOLFSSL_EVP_MD*)md, pkey);
- if (wolfSSL_X509_make_der(req, 1, der, &derSz) != WOLFSSL_SUCCESS) {
- return WOLFSSL_FAILURE;
- }
-
- if (wolfSSL_X509_resign_cert(req, 1, der, sizeof(der), derSz,
- (WOLFSSL_EVP_MD*)md, pkey) <= 0) {
- return WOLFSSL_FAILURE;
- }
- return WOLFSSL_SUCCESS;
-}
-
-
-#ifndef NO_WOLFSSL_STUB
-int wolfSSL_X509_REQ_add_extensions(WOLFSSL_X509* req,
- WOLF_STACK_OF(WOLFSSL_X509_EXTENSION)* ext)
-{
- (void)req;
- (void)ext;
- return WOLFSSL_FATAL_ERROR;
-}
-#endif
-
-int wolfSSL_X509_REQ_set_subject_name(WOLFSSL_X509 *req,
- WOLFSSL_X509_NAME *name)
-{
- return wolfSSL_X509_set_subject_name(req, name);
-}
-
-int wolfSSL_X509_REQ_set_pubkey(WOLFSSL_X509 *req, WOLFSSL_EVP_PKEY *pkey)
-{
- return wolfSSL_X509_set_pubkey(req, pkey);
-}
-#endif /* OPENSSL_EXTRA && !NO_CERTS && WOLFSSL_CERT_GEN && WOLFSSL_CERT_REQ */
-
-
diff --git a/client/wolfssl/src/tls.c b/client/wolfssl/src/tls.c
deleted file mode 100644
index 1b9858a..0000000
--- a/client/wolfssl/src/tls.c
+++ /dev/null
@@ -1,11755 +0,0 @@
-/* tls.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifndef WOLFCRYPT_ONLY
-
-#include
-#include
-#include
-#include
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include
-#endif
-
-#ifdef HAVE_CURVE25519
- #include
-#endif
-#ifdef HAVE_CURVE448
- #include
-#endif
-#ifdef HAVE_NTRU
- #include "libntruencrypt/ntru_crypto.h"
- #include
-#endif
-
-#ifdef HAVE_QSH
- static int TLSX_AddQSHKey(QSHKey** list, QSHKey* key);
- static byte* TLSX_QSHKeyFind_Pub(QSHKey* qsh, word16* pubLen, word16 name);
-#if defined(HAVE_NTRU)
- static int TLSX_CreateNtruKey(WOLFSSL* ssl, int type);
-#endif
-#endif /* HAVE_QSH */
-
-#if (!defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_TLS13) && \
- !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)) || \
- (defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES))
-static int TLSX_KeyShare_IsSupported(int namedGroup);
-#endif
-
-#if ((!defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_TLS13) && \
- !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)) || \
- (defined(WOLFSSL_TLS13) && !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) \
- && !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)) || \
- ((defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES))) && \
- defined(HAVE_TLS_EXTENSIONS)
-static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions);
-#endif
-
-
-#ifndef NO_TLS
-
-/* Digest enable checks */
-#ifdef NO_OLD_TLS /* TLS 1.2 only */
- #if defined(NO_SHA256) && !defined(WOLFSSL_SHA384) && \
- !defined(WOLFSSL_SHA512)
- #error Must have SHA256, SHA384 or SHA512 enabled for TLS 1.2
- #endif
-#else /* TLS 1.1 or older */
- #if defined(NO_MD5) && defined(NO_SHA)
- #error Must have SHA1 and MD5 enabled for old TLS
- #endif
-#endif
-
-#ifdef WOLFSSL_TLS13
- #if !defined(NO_DH) && \
- !defined(HAVE_FFDHE_2048) && !defined(HAVE_FFDHE_3072) && \
- !defined(HAVE_FFDHE_4096) && !defined(HAVE_FFDHE_6144) && \
- !defined(HAVE_FFDHE_8192)
- #error Please configure your TLS 1.3 DH key size using either: HAVE_FFDHE_2048, HAVE_FFDHE_3072, HAVE_FFDHE_4096, HAVE_FFDHE_6144 or HAVE_FFDHE_8192
- #endif
- #if !defined(NO_RSA) && !defined(WC_RSA_PSS)
- #error The build option WC_RSA_PSS is required for TLS 1.3 with RSA
- #endif
- #ifndef HAVE_TLS_EXTENSIONS
- #ifndef _MSC_VER
- #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3"
- #else
- #pragma message("Error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3")
- #endif
- #endif
-#endif
-
-/* Warn if secrets logging is enabled */
-#if defined(SHOW_SECRETS) || defined(WOLFSSL_SSLKEYLOGFILE)
- #ifndef _MSC_VER
- #warning The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment
- #else
- #pragma message("Warning: The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment")
- #endif
-#endif
-
-/* Optional Pre-Master-Secret logging for Wireshark */
-#if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
-#ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
- #define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
-#endif
-#endif
-
-#ifndef WOLFSSL_NO_TLS12
-
-#ifdef WOLFSSL_SHA384
- #define HSHASH_SZ WC_SHA384_DIGEST_SIZE
-#else
- #define HSHASH_SZ FINISHED_SZ
-#endif
-
-#ifdef WOLFSSL_RENESAS_TSIP_TLS
- int tsip_useable(const WOLFSSL *ssl);
- int tsip_generateMasterSecret(const byte *pre,
- const byte *cr,const byte *sr,
- byte *ms/* out */);
- int tsip_generateSeesionKey(WOLFSSL *ssl);
- int tsip_generateVerifyData(const byte *ms, const byte *side,
- const byte *handshake_hash,
- byte *hashes /* out */);
-#endif
-
-int BuildTlsHandshakeHash(WOLFSSL* ssl, byte* hash, word32* hashLen)
-{
- int ret = 0;
- word32 hashSz = FINISHED_SZ;
-
- if (ssl == NULL || hash == NULL || hashLen == NULL || *hashLen < HSHASH_SZ)
- return BAD_FUNC_ARG;
-
- /* for constant timing perform these even if error */
-#ifndef NO_OLD_TLS
- ret |= wc_Md5GetHash(&ssl->hsHashes->hashMd5, hash);
- ret |= wc_ShaGetHash(&ssl->hsHashes->hashSha, &hash[WC_MD5_DIGEST_SIZE]);
-#endif
-
- if (IsAtLeastTLSv1_2(ssl)) {
-#ifndef NO_SHA256
- if (ssl->specs.mac_algorithm <= sha256_mac ||
- ssl->specs.mac_algorithm == blake2b_mac) {
- ret |= wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
- hashSz = WC_SHA256_DIGEST_SIZE;
- }
-#endif
-#ifdef WOLFSSL_SHA384
- if (ssl->specs.mac_algorithm == sha384_mac) {
- ret |= wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
- hashSz = WC_SHA384_DIGEST_SIZE;
- }
-#endif
- }
-
- *hashLen = hashSz;
-
- if (ret != 0)
- ret = BUILD_MSG_ERROR;
-
- return ret;
-}
-
-
-int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
-{
- int ret;
- const byte* side;
- word32 hashSz = HSHASH_SZ;
-#if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
- DECLARE_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
- if (handshake_hash == NULL)
- return MEMORY_E;
-#else
- byte handshake_hash[HSHASH_SZ];
-#endif
-
- ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
- if (ret == 0) {
- if (XSTRNCMP((const char*)sender, (const char*)client, SIZEOF_SENDER) == 0)
- side = tls_client;
- else
- side = tls_server;
-
-#ifdef WOLFSSL_HAVE_PRF
-#if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- if (tsip_useable(ssl)) {
- ret = tsip_generateVerifyData(ssl->arrays->tsip_masterSecret,
- side, handshake_hash, (byte*)hashes /* out */);
- } else
-#endif
- ret = wc_PRF_TLS((byte*)hashes, TLS_FINISHED_SZ, ssl->arrays->masterSecret,
- SECRET_LEN, side, FINISHED_LABEL_SZ, handshake_hash, hashSz,
- IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
- ssl->heap, ssl->devId);
-#else
- /* Pseudo random function must be enabled in the configuration. */
- ret = PRF_MISSING;
- WOLFSSL_MSG("Pseudo-random function is not enabled");
-
- (void)side;
- (void)hashes;
-#endif
- }
-
-#if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
- FREE_VAR(handshake_hash, ssl->heap);
-#endif
-
- return ret;
-}
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#ifndef NO_OLD_TLS
-
-#ifdef WOLFSSL_ALLOW_TLSV10
-ProtocolVersion MakeTLSv1(void)
-{
- ProtocolVersion pv;
- pv.major = SSLv3_MAJOR;
- pv.minor = TLSv1_MINOR;
-
- return pv;
-}
-#endif /* WOLFSSL_ALLOW_TLSV10 */
-
-
-ProtocolVersion MakeTLSv1_1(void)
-{
- ProtocolVersion pv;
- pv.major = SSLv3_MAJOR;
- pv.minor = TLSv1_1_MINOR;
-
- return pv;
-}
-
-#endif /* !NO_OLD_TLS */
-
-
-#ifndef WOLFSSL_NO_TLS12
-
-ProtocolVersion MakeTLSv1_2(void)
-{
- ProtocolVersion pv;
- pv.major = SSLv3_MAJOR;
- pv.minor = TLSv1_2_MINOR;
-
- return pv;
-}
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#ifdef WOLFSSL_TLS13
-/* The TLS v1.3 protocol version.
- *
- * returns the protocol version data for TLS v1.3.
- */
-ProtocolVersion MakeTLSv1_3(void)
-{
- ProtocolVersion pv;
- pv.major = SSLv3_MAJOR;
- pv.minor = TLSv1_3_MINOR;
-
- return pv;
-}
-#endif
-
-#ifndef WOLFSSL_NO_TLS12
-
-#ifdef HAVE_EXTENDED_MASTER
-static const byte ext_master_label[EXT_MASTER_LABEL_SZ + 1] =
- "extended master secret";
-#endif
-static const byte master_label[MASTER_LABEL_SZ + 1] = "master secret";
-static const byte key_label [KEY_LABEL_SZ + 1] = "key expansion";
-
-static int _DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
- const byte* ms, word32 msLen,
- const byte* sr, const byte* cr,
- int tls1_2, int hash_type,
- void* heap, int devId)
-{
- int ret;
-#if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
- DECLARE_VAR(seed, byte, SEED_LEN, heap);
- if (seed == NULL)
- return MEMORY_E;
-#else
- byte seed[SEED_LEN];
-#endif
-
- XMEMCPY(seed, sr, RAN_LEN);
- XMEMCPY(seed + RAN_LEN, cr, RAN_LEN);
-
-#ifdef WOLFSSL_HAVE_PRF
- ret = wc_PRF_TLS(key_dig, key_dig_len, ms, msLen, key_label, KEY_LABEL_SZ,
- seed, SEED_LEN, tls1_2, hash_type, heap, devId);
-#else
- /* Pseudo random function must be enabled in the configuration. */
- ret = PRF_MISSING;
- WOLFSSL_MSG("Pseudo-random function is not enabled");
-
- (void)key_dig;
- (void)key_dig_len;
- (void)ms;
- (void)msLen;
- (void)tls1_2;
- (void)hash_type;
- (void)heap;
- (void)devId;
- (void)key_label;
- (void)master_label;
-#ifdef HAVE_EXTENDED_MASTER
- (void)ext_master_label;
-#endif
-#endif
-
-#if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
- FREE_VAR(seed, heap);
-#endif
-
- return ret;
-}
-
-/* External facing wrapper so user can call as well, 0 on success */
-int wolfSSL_DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
- const byte* ms, word32 msLen,
- const byte* sr, const byte* cr,
- int tls1_2, int hash_type)
-{
- return _DeriveTlsKeys(key_dig, key_dig_len, ms, msLen, sr, cr, tls1_2,
- hash_type, NULL, INVALID_DEVID);
-}
-
-
-int DeriveTlsKeys(WOLFSSL* ssl)
-{
- int ret;
- int key_dig_len = 2 * ssl->specs.hash_size +
- 2 * ssl->specs.key_size +
- 2 * ssl->specs.iv_size;
-#ifdef WOLFSSL_SMALL_STACK
- byte* key_dig;
-#else
- byte key_dig[MAX_PRF_DIG];
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST);
- if (key_dig == NULL) {
- return MEMORY_E;
- }
-#endif
-#if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- if (tsip_useable(ssl))
- ret = tsip_generateSeesionKey(ssl);
- else {
-#endif
- ret = _DeriveTlsKeys(key_dig, key_dig_len,
- ssl->arrays->masterSecret, SECRET_LEN,
- ssl->arrays->serverRandom, ssl->arrays->clientRandom,
- IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
- ssl->heap, ssl->devId);
- if (ret == 0)
- ret = StoreKeys(ssl, key_dig, PROVISION_CLIENT_SERVER);
-#if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- }
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
-#endif
-
- return ret;
-}
-
-static int _MakeTlsMasterSecret(byte* ms, word32 msLen,
- const byte* pms, word32 pmsLen,
- const byte* cr, const byte* sr,
- int tls1_2, int hash_type,
- void* heap, int devId)
-{
- int ret;
-#if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
- DECLARE_VAR(seed, byte, SEED_LEN, heap);
- if (seed == NULL)
- return MEMORY_E;
-#else
- byte seed[SEED_LEN];
-#endif
-
- XMEMCPY(seed, cr, RAN_LEN);
- XMEMCPY(seed + RAN_LEN, sr, RAN_LEN);
-
-#ifdef WOLFSSL_HAVE_PRF
- ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, master_label, MASTER_LABEL_SZ,
- seed, SEED_LEN, tls1_2, hash_type, heap, devId);
-#else
- /* Pseudo random function must be enabled in the configuration. */
- ret = PRF_MISSING;
- WOLFSSL_MSG("Pseudo-random function is not enabled");
-
- (void)ms;
- (void)msLen;
- (void)pms;
- (void)pmsLen;
- (void)tls1_2;
- (void)hash_type;
- (void)heap;
- (void)devId;
-#endif
-
-#if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
- FREE_VAR(seed, heap);
-#endif
-
- return ret;
-}
-
-/* External facing wrapper so user can call as well, 0 on success */
-int wolfSSL_MakeTlsMasterSecret(byte* ms, word32 msLen,
- const byte* pms, word32 pmsLen,
- const byte* cr, const byte* sr,
- int tls1_2, int hash_type)
-{
- return _MakeTlsMasterSecret(ms, msLen, pms, pmsLen, cr, sr, tls1_2,
- hash_type, NULL, INVALID_DEVID);
-}
-
-
-#ifdef HAVE_EXTENDED_MASTER
-
-static int _MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
- const byte* pms, word32 pmsLen,
- const byte* sHash, word32 sHashLen,
- int tls1_2, int hash_type,
- void* heap, int devId)
-{
- int ret;
-
-#ifdef WOLFSSL_HAVE_PRF
- ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, ext_master_label, EXT_MASTER_LABEL_SZ,
- sHash, sHashLen, tls1_2, hash_type, heap, devId);
-#else
- /* Pseudo random function must be enabled in the configuration. */
- ret = PRF_MISSING;
- WOLFSSL_MSG("Pseudo-random function is not enabled");
-
- (void)ms;
- (void)msLen;
- (void)pms;
- (void)pmsLen;
- (void)sHash;
- (void)sHashLen;
- (void)tls1_2;
- (void)hash_type;
- (void)heap;
- (void)devId;
-#endif
- return ret;
-}
-
-/* External facing wrapper so user can call as well, 0 on success */
-int wolfSSL_MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
- const byte* pms, word32 pmsLen,
- const byte* sHash, word32 sHashLen,
- int tls1_2, int hash_type)
-{
- return _MakeTlsExtendedMasterSecret(ms, msLen, pms, pmsLen, sHash, sHashLen,
- tls1_2, hash_type, NULL, INVALID_DEVID);
-}
-
-#endif /* HAVE_EXTENDED_MASTER */
-
-
-int MakeTlsMasterSecret(WOLFSSL* ssl)
-{
- int ret;
-
-#ifdef HAVE_EXTENDED_MASTER
- if (ssl->options.haveEMS) {
- word32 hashSz = HSHASH_SZ;
- #ifdef WOLFSSL_SMALL_STACK
- byte* handshake_hash = (byte*)XMALLOC(HSHASH_SZ, ssl->heap,
- DYNAMIC_TYPE_DIGEST);
- if (handshake_hash == NULL)
- return MEMORY_E;
- #else
- byte handshake_hash[HSHASH_SZ];
- #endif
-
- ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
- if (ret == 0) {
- ret = _MakeTlsExtendedMasterSecret(
- ssl->arrays->masterSecret, SECRET_LEN,
- ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
- handshake_hash, hashSz,
- IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
- ssl->heap, ssl->devId);
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(handshake_hash, ssl->heap, DYNAMIC_TYPE_DIGEST);
- #endif
- }
- else
-#endif /* HAVE_EXTENDED_MASTER */
- {
-#if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- if (tsip_useable(ssl)) {
- ret = tsip_generateMasterSecret(
- &ssl->arrays->preMasterSecret[VERSION_SZ],
- ssl->arrays->clientRandom,
- ssl->arrays->serverRandom,
- ssl->arrays->tsip_masterSecret);
- } else
-#endif
- ret = _MakeTlsMasterSecret(ssl->arrays->masterSecret, SECRET_LEN,
- ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
- ssl->arrays->clientRandom, ssl->arrays->serverRandom,
- IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
- ssl->heap, ssl->devId);
- }
- if (ret == 0) {
- #ifdef SHOW_SECRETS
- /* Wireshark Pre-Master-Secret Format:
- * CLIENT_RANDOM
- */
- const char* CLIENT_RANDOM_LABEL = "CLIENT_RANDOM";
- int i, pmsPos = 0;
- char pmsBuf[13 + 1 + 64 + 1 + 96 + 1 + 1];
-
- XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%s ",
- CLIENT_RANDOM_LABEL);
- pmsPos += XSTRLEN(CLIENT_RANDOM_LABEL) + 1;
- for (i = 0; i < RAN_LEN; i++) {
- XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
- ssl->arrays->clientRandom[i]);
- pmsPos += 2;
- }
- XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, " ");
- pmsPos += 1;
- for (i = 0; i < SECRET_LEN; i++) {
- XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
- ssl->arrays->masterSecret[i]);
- pmsPos += 2;
- }
- XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "\n");
- pmsPos += 1;
-
- /* print master secret */
- puts(pmsBuf);
-
- #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
- {
- FILE* f = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "a");
- if (f != XBADFILE) {
- XFWRITE(pmsBuf, 1, pmsPos, f);
- XFCLOSE(f);
- }
- }
- #endif
- #endif /* SHOW_SECRETS */
-
- ret = DeriveTlsKeys(ssl);
- }
-
- return ret;
-}
-
-
-/* Used by EAP-TLS and EAP-TTLS to derive keying material from
- * the master_secret. */
-int wolfSSL_make_eap_keys(WOLFSSL* ssl, void* msk, unsigned int len,
- const char* label)
-{
- int ret;
-#ifdef WOLFSSL_SMALL_STACK
- byte* seed;
-#else
- byte seed[SEED_LEN];
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- seed = (byte*)XMALLOC(SEED_LEN, ssl->heap, DYNAMIC_TYPE_SEED);
- if (seed == NULL)
- return MEMORY_E;
-#endif
-
- /*
- * As per RFC-5281, the order of the client and server randoms is reversed
- * from that used by the TLS protocol to derive keys.
- */
- XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
- XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
-
-#ifdef WOLFSSL_HAVE_PRF
- ret = wc_PRF_TLS((byte*)msk, len, ssl->arrays->masterSecret, SECRET_LEN,
- (const byte *)label, (word32)XSTRLEN(label), seed, SEED_LEN,
- IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
- ssl->heap, ssl->devId);
-#else
- /* Pseudo random function must be enabled in the configuration. */
- ret = PRF_MISSING;
- WOLFSSL_MSG("Pseudo-random function is not enabled");
-
- (void)msk;
- (void)len;
- (void)label;
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(seed, ssl->heap, DYNAMIC_TYPE_SEED);
-#endif
-
- return ret;
-}
-
-
-static WC_INLINE void GetSEQIncrement(WOLFSSL* ssl, int verify, word32 seq[2])
-{
- if (verify) {
- seq[0] = ssl->keys.peer_sequence_number_hi;
- seq[1] = ssl->keys.peer_sequence_number_lo++;
- if (seq[1] > ssl->keys.peer_sequence_number_lo) {
- /* handle rollover */
- ssl->keys.peer_sequence_number_hi++;
- }
- }
- else {
- seq[0] = ssl->keys.sequence_number_hi;
- seq[1] = ssl->keys.sequence_number_lo++;
- if (seq[1] > ssl->keys.sequence_number_lo) {
- /* handle rollover */
- ssl->keys.sequence_number_hi++;
- }
- }
-}
-
-
-#ifdef WOLFSSL_DTLS
-static WC_INLINE void DtlsGetSEQ(WOLFSSL* ssl, int order, word32 seq[2])
-{
- if (order == PREV_ORDER) {
- /* Previous epoch case */
- seq[0] = (((word32)ssl->keys.dtls_epoch - 1) << 16) |
- (ssl->keys.dtls_prev_sequence_number_hi & 0xFFFF);
- seq[1] = ssl->keys.dtls_prev_sequence_number_lo;
- }
- else if (order == PEER_ORDER) {
- seq[0] = ((word32)ssl->keys.curEpoch << 16) |
- (ssl->keys.curSeq_hi & 0xFFFF);
- seq[1] = ssl->keys.curSeq_lo; /* explicit from peer */
- }
- else {
- seq[0] = ((word32)ssl->keys.dtls_epoch << 16) |
- (ssl->keys.dtls_sequence_number_hi & 0xFFFF);
- seq[1] = ssl->keys.dtls_sequence_number_lo;
- }
-}
-#endif /* WOLFSSL_DTLS */
-
-
-static WC_INLINE void WriteSEQ(WOLFSSL* ssl, int verifyOrder, byte* out)
-{
- word32 seq[2] = {0, 0};
-
- if (!ssl->options.dtls) {
- GetSEQIncrement(ssl, verifyOrder, seq);
- }
- else {
-#ifdef WOLFSSL_DTLS
- DtlsGetSEQ(ssl, verifyOrder, seq);
-#endif
- }
-
- c32toa(seq[0], out);
- c32toa(seq[1], out + OPAQUE32_LEN);
-}
-
-
-/*** end copy ***/
-
-
-/* return HMAC digest type in wolfSSL format */
-int wolfSSL_GetHmacType(WOLFSSL* ssl)
-{
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- switch (ssl->specs.mac_algorithm) {
- #ifndef NO_MD5
- case md5_mac:
- {
- return WC_MD5;
- }
- #endif
- #ifndef NO_SHA256
- case sha256_mac:
- {
- return WC_SHA256;
- }
- #endif
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- {
- return WC_SHA384;
- }
-
- #endif
- #ifndef NO_SHA
- case sha_mac:
- {
- return WC_SHA;
- }
- #endif
- #ifdef HAVE_BLAKE2
- case blake2b_mac:
- {
- return BLAKE2B_ID;
- }
- #endif
- default:
- {
- return WOLFSSL_FATAL_ERROR;
- }
- }
-}
-
-
-int wolfSSL_SetTlsHmacInner(WOLFSSL* ssl, byte* inner, word32 sz, int content,
- int verify)
-{
- if (ssl == NULL || inner == NULL)
- return BAD_FUNC_ARG;
-
- XMEMSET(inner, 0, WOLFSSL_TLS_HMAC_INNER_SZ);
-
- WriteSEQ(ssl, verify, inner);
- inner[SEQ_SZ] = (byte)content;
- inner[SEQ_SZ + ENUM_LEN] = ssl->version.major;
- inner[SEQ_SZ + ENUM_LEN + ENUM_LEN] = ssl->version.minor;
- c16toa((word16)sz, inner + SEQ_SZ + ENUM_LEN + VERSION_SZ);
-
- return 0;
-}
-
-
-#ifndef WOLFSSL_AEAD_ONLY
-#if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
- !defined(HAVE_SELFTEST)
-
-/* Update the hash in the HMAC.
- *
- * hmac HMAC object.
- * data Data to be hashed.
- * sz Size of data to hash.
- * returns 0 on success, otherwise failure.
- */
-static int Hmac_HashUpdate(Hmac* hmac, const byte* data, word32 sz)
-{
- int ret = BAD_FUNC_ARG;
-
- switch (hmac->macType) {
- #ifndef NO_SHA
- case WC_SHA:
- ret = wc_ShaUpdate(&hmac->hash.sha, data, sz);
- break;
- #endif /* !NO_SHA */
-
- #ifndef NO_SHA256
- case WC_SHA256:
- ret = wc_Sha256Update(&hmac->hash.sha256, data, sz);
- break;
- #endif /* !NO_SHA256 */
-
- #ifdef WOLFSSL_SHA384
- case WC_SHA384:
- ret = wc_Sha384Update(&hmac->hash.sha384, data, sz);
- break;
- #endif /* WOLFSSL_SHA384 */
-
- #ifdef WOLFSSL_SHA512
- case WC_SHA512:
- ret = wc_Sha512Update(&hmac->hash.sha512, data, sz);
- break;
- #endif /* WOLFSSL_SHA512 */
- }
-
- return ret;
-}
-
-/* Finalize the hash but don't put the EOC, padding or length in.
- *
- * hmac HMAC object.
- * hash Hash result.
- * returns 0 on success, otherwise failure.
- */
-static int Hmac_HashFinalRaw(Hmac* hmac, unsigned char* hash)
-{
- int ret = BAD_FUNC_ARG;
-
- switch (hmac->macType) {
- #ifndef NO_SHA
- case WC_SHA:
- ret = wc_ShaFinalRaw(&hmac->hash.sha, hash);
- break;
- #endif /* !NO_SHA */
-
- #ifndef NO_SHA256
- case WC_SHA256:
- ret = wc_Sha256FinalRaw(&hmac->hash.sha256, hash);
- break;
- #endif /* !NO_SHA256 */
-
- #ifdef WOLFSSL_SHA384
- case WC_SHA384:
- ret = wc_Sha384FinalRaw(&hmac->hash.sha384, hash);
- break;
- #endif /* WOLFSSL_SHA384 */
-
- #ifdef WOLFSSL_SHA512
- case WC_SHA512:
- ret = wc_Sha512FinalRaw(&hmac->hash.sha512, hash);
- break;
- #endif /* WOLFSSL_SHA512 */
- }
-
- return ret;
-}
-
-/* Finalize the HMAC by performing outer hash.
- *
- * hmac HMAC object.
- * mac MAC result.
- * returns 0 on success, otherwise failure.
- */
-static int Hmac_OuterHash(Hmac* hmac, unsigned char* mac)
-{
- int ret = BAD_FUNC_ARG;
- wc_HashAlg hash;
- enum wc_HashType hashType = (enum wc_HashType)hmac->macType;
- int digestSz = wc_HashGetDigestSize(hashType);
- int blockSz = wc_HashGetBlockSize(hashType);
-
- if ((digestSz >= 0) && (blockSz >= 0)) {
- ret = wc_HashInit(&hash, hashType);
- }
- if (ret == 0) {
- ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->opad,
- blockSz);
- if (ret == 0)
- ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->innerHash,
- digestSz);
- if (ret == 0)
- ret = wc_HashFinal(&hash, hashType, mac);
- wc_HashFree(&hash, hashType);
- }
-
- return ret;
-}
-
-/* Calculate the HMAC of the header + message data.
- * Constant time implementation using wc_Sha*FinalRaw().
- *
- * hmac HMAC object.
- * digest MAC result.
- * in Message data.
- * sz Size of the message data.
- * header Constructed record header with length of handshake data.
- * returns 0 on success, otherwise failure.
- */
-static int Hmac_UpdateFinal_CT(Hmac* hmac, byte* digest, const byte* in,
- word32 sz, byte* header)
-{
- byte lenBytes[8];
- int i, j, k;
- int blockBits, blockMask;
- int lastBlockLen, macLen, extraLen, eocIndex;
- int blocks, safeBlocks, lenBlock, eocBlock;
- int maxLen;
- int blockSz, padSz;
- int ret;
- word32 realLen;
- byte extraBlock;
-
- switch (hmac->macType) {
- #ifndef NO_SHA
- case WC_SHA:
- blockSz = WC_SHA_BLOCK_SIZE;
- blockBits = 6;
- macLen = WC_SHA_DIGEST_SIZE;
- padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
- break;
- #endif /* !NO_SHA */
-
- #ifndef NO_SHA256
- case WC_SHA256:
- blockSz = WC_SHA256_BLOCK_SIZE;
- blockBits = 6;
- macLen = WC_SHA256_DIGEST_SIZE;
- padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
- break;
- #endif /* !NO_SHA256 */
-
- #ifdef WOLFSSL_SHA384
- case WC_SHA384:
- blockSz = WC_SHA384_BLOCK_SIZE;
- blockBits = 7;
- macLen = WC_SHA384_DIGEST_SIZE;
- padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
- break;
- #endif /* WOLFSSL_SHA384 */
-
- #ifdef WOLFSSL_SHA512
- case WC_SHA512:
- blockSz = WC_SHA512_BLOCK_SIZE;
- blockBits = 7;
- macLen = WC_SHA512_DIGEST_SIZE;
- padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
- break;
- #endif /* WOLFSSL_SHA512 */
-
- default:
- return BAD_FUNC_ARG;
- }
- blockMask = blockSz - 1;
-
- /* Size of data to HMAC if padding length byte is zero. */
- maxLen = WOLFSSL_TLS_HMAC_INNER_SZ + sz - 1 - macLen;
- /* Complete data (including padding) has block for EOC and/or length. */
- extraBlock = ctSetLTE((maxLen + padSz) & blockMask, padSz);
- /* Total number of blocks for data including padding. */
- blocks = ((maxLen + blockSz - 1) >> blockBits) + extraBlock;
- /* Up to last 6 blocks can be hashed safely. */
- safeBlocks = blocks - 6;
-
- /* Length of message data. */
- realLen = maxLen - in[sz - 1];
- /* Number of message bytes in last block. */
- lastBlockLen = realLen & blockMask;
- /* Number of padding bytes in last block. */
- extraLen = ((blockSz * 2 - padSz - lastBlockLen) & blockMask) + 1;
- /* Number of blocks to create for hash. */
- lenBlock = (realLen + extraLen) >> blockBits;
- /* Block containing EOC byte. */
- eocBlock = realLen >> blockBits;
- /* Index of EOC byte in block. */
- eocIndex = realLen & blockMask;
-
- /* Add length of hmac's ipad to total length. */
- realLen += blockSz;
- /* Length as bits - 8 bytes bigendian. */
- c32toa(realLen >> ((sizeof(word32) * 8) - 3), lenBytes);
- c32toa(realLen << 3, lenBytes + sizeof(word32));
-
- ret = Hmac_HashUpdate(hmac, (unsigned char*)hmac->ipad, blockSz);
- if (ret != 0)
- return ret;
-
- XMEMSET(hmac->innerHash, 0, macLen);
-
- if (safeBlocks > 0) {
- ret = Hmac_HashUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
- if (ret != 0)
- return ret;
- ret = Hmac_HashUpdate(hmac, in, safeBlocks * blockSz -
- WOLFSSL_TLS_HMAC_INNER_SZ);
- if (ret != 0)
- return ret;
- }
- else
- safeBlocks = 0;
-
- XMEMSET(digest, 0, macLen);
- k = safeBlocks * blockSz;
- for (i = safeBlocks; i < blocks; i++) {
- unsigned char hashBlock[WC_MAX_BLOCK_SIZE];
- unsigned char isEocBlock = ctMaskEq(i, eocBlock);
- unsigned char isOutBlock = ctMaskEq(i, lenBlock);
-
- for (j = 0; j < blockSz; j++, k++) {
- unsigned char atEoc = ctMaskEq(j, eocIndex) & isEocBlock;
- unsigned char pastEoc = ctMaskGT(j, eocIndex) & isEocBlock;
- unsigned char b = 0;
-
- if (k < WOLFSSL_TLS_HMAC_INNER_SZ)
- b = header[k];
- else if (k < maxLen)
- b = in[k - WOLFSSL_TLS_HMAC_INNER_SZ];
-
- b = ctMaskSel(atEoc, 0x80, b);
- b &= (unsigned char)~(word32)pastEoc;
- b &= ((unsigned char)~(word32)isOutBlock) | isEocBlock;
-
- if (j >= blockSz - 8) {
- b = ctMaskSel(isOutBlock, lenBytes[j - (blockSz - 8)], b);
- }
-
- hashBlock[j] = b;
- }
-
- ret = Hmac_HashUpdate(hmac, hashBlock, blockSz);
- if (ret != 0)
- return ret;
- ret = Hmac_HashFinalRaw(hmac, hashBlock);
- if (ret != 0)
- return ret;
- for (j = 0; j < macLen; j++)
- ((unsigned char*)hmac->innerHash)[j] |= hashBlock[j] & isOutBlock;
- }
-
- ret = Hmac_OuterHash(hmac, digest);
-
- return ret;
-}
-
-#endif
-
-#if defined(WOLFSSL_NO_HASH_RAW) || defined(HAVE_FIPS) || \
- defined(HAVE_SELFTEST) || defined(HAVE_BLAKE2)
-
-/* Calculate the HMAC of the header + message data.
- * Constant time implementation using normal hashing operations.
- * Update-Final need to be constant time.
- *
- * hmac HMAC object.
- * digest MAC result.
- * in Message data.
- * sz Size of the message data.
- * header Constructed record header with length of handshake data.
- * returns 0 on success, otherwise failure.
- */
-static int Hmac_UpdateFinal(Hmac* hmac, byte* digest, const byte* in,
- word32 sz, byte* header)
-{
- byte dummy[WC_MAX_BLOCK_SIZE] = {0};
- int ret;
- word32 msgSz, blockSz, macSz, padSz, maxSz, realSz;
- word32 currSz, offset = 0;
- int msgBlocks, blocks, blockBits;
- int i;
-
- switch (hmac->macType) {
- #ifndef NO_SHA
- case WC_SHA:
- blockSz = WC_SHA_BLOCK_SIZE;
- blockBits = 6;
- macSz = WC_SHA_DIGEST_SIZE;
- padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
- break;
- #endif /* !NO_SHA */
-
- #ifndef NO_SHA256
- case WC_SHA256:
- blockSz = WC_SHA256_BLOCK_SIZE;
- blockBits = 6;
- macSz = WC_SHA256_DIGEST_SIZE;
- padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
- break;
- #endif /* !NO_SHA256 */
-
- #ifdef WOLFSSL_SHA384
- case WC_SHA384:
- blockSz = WC_SHA384_BLOCK_SIZE;
- blockBits = 7;
- macSz = WC_SHA384_DIGEST_SIZE;
- padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
- break;
- #endif /* WOLFSSL_SHA384 */
-
- #ifdef WOLFSSL_SHA512
- case WC_SHA512:
- blockSz = WC_SHA512_BLOCK_SIZE;
- blockBits = 7;
- macSz = WC_SHA512_DIGEST_SIZE;
- padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
- break;
- #endif /* WOLFSSL_SHA512 */
-
- #ifdef HAVE_BLAKE2
- case WC_HASH_TYPE_BLAKE2B:
- blockSz = BLAKE2B_BLOCKBYTES;
- blockBits = 7;
- macSz = BLAKE2B_256;
- padSz = 0;
- break;
- #endif /* HAVE_BLAK2 */
-
- default:
- return BAD_FUNC_ARG;
- }
-
- msgSz = sz - (1 + in[sz - 1] + macSz);
- /* Make negative result 0 */
- msgSz &= ~(0 - (msgSz >> 31));
- realSz = WOLFSSL_TLS_HMAC_INNER_SZ + msgSz;
- maxSz = WOLFSSL_TLS_HMAC_INNER_SZ + (sz - 1) - macSz;
-
- /* Calculate #blocks processed in HMAC for max and real data. */
- blocks = maxSz >> blockBits;
- blocks += ((maxSz + padSz) % blockSz) < padSz;
- msgBlocks = realSz >> blockBits;
- /* #Extra blocks to process. */
- blocks -= msgBlocks + (((realSz + padSz) % blockSz) < padSz);
- /* Calculate whole blocks. */
- msgBlocks--;
-
- ret = wc_HmacUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
- if (ret == 0) {
- /* Fill the rest of the block with any available data. */
- currSz = ctMaskLT(msgSz, blockSz) & msgSz;
- currSz |= ctMaskGTE(msgSz, blockSz) & blockSz;
- currSz -= WOLFSSL_TLS_HMAC_INNER_SZ;
- currSz &= ~(0 - (currSz >> 31));
- ret = wc_HmacUpdate(hmac, in, currSz);
- offset = currSz;
- }
- if (ret == 0) {
- /* Do the hash operations on a block basis. */
- for (i = 0; i < msgBlocks; i++, offset += blockSz) {
- ret = wc_HmacUpdate(hmac, in + offset, blockSz);
- if (ret != 0)
- break;
- }
- }
- if (ret == 0)
- ret = wc_HmacUpdate(hmac, in + offset, msgSz - offset);
- if (ret == 0)
- ret = wc_HmacFinal(hmac, digest);
- if (ret == 0) {
- /* Do the dummy hash operations. Do at least one. */
- for (i = 0; i < blocks + 1; i++) {
- ret = wc_HmacUpdate(hmac, dummy, blockSz);
- if (ret != 0)
- break;
- }
- }
-
- return ret;
-}
-
-#endif
-
-int TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz, int padSz,
- int content, int verify)
-{
- Hmac hmac;
- byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
- int ret = 0;
-#ifdef HAVE_TRUNCATED_HMAC
- word32 hashSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
- : ssl->specs.hash_size;
-#else
- word32 hashSz = ssl->specs.hash_size;
-#endif
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
-#ifdef HAVE_FUZZER
- /* Fuzz "in" buffer with sz to be used in HMAC algorithm */
- if (ssl->fuzzerCb) {
- if (verify && padSz >= 0) {
- ssl->fuzzerCb(ssl, in, sz + hashSz + padSz + 1, FUZZ_HMAC,
- ssl->fuzzerCtx);
- }
- else {
- ssl->fuzzerCb(ssl, in, sz, FUZZ_HMAC, ssl->fuzzerCtx);
- }
- }
-#endif
-
- wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
-#if defined(WOLFSSL_RENESAS_TSIP_TLS) && \
- !defined(NO_WOLFSSL_RENESAS_TSIP_TLS_SESSION)
- if (tsip_useable(ssl)) {
- if (ssl->specs.hash_size == WC_SHA_DIGEST_SIZE)
- ret = tsip_Sha1Hmac(ssl, myInner, WOLFSSL_TLS_HMAC_INNER_SZ,
- in, sz, digest, verify);
- else if (ssl->specs.hash_size == WC_SHA256_DIGEST_SIZE)
- ret = tsip_Sha256Hmac(ssl, myInner, WOLFSSL_TLS_HMAC_INNER_SZ,
- in, sz, digest, verify);
- else
- ret = TSIP_MAC_DIGSZ_E;
-
- return ret;
- }
-#endif
- ret = wc_HmacInit(&hmac, ssl->heap, ssl->devId);
- if (ret != 0)
- return ret;
-
- ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
- wolfSSL_GetMacSecret(ssl, verify),
- ssl->specs.hash_size);
- if (ret == 0) {
- /* Constant time verification required. */
- if (verify && padSz >= 0) {
-#if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
- !defined(HAVE_SELFTEST)
- #ifdef HAVE_BLAKE2
- if (wolfSSL_GetHmacType(ssl) == WC_HASH_TYPE_BLAKE2B) {
- ret = Hmac_UpdateFinal(&hmac, digest, in,
- sz + hashSz + padSz + 1, myInner);
- }
- else
- #endif
- {
- ret = Hmac_UpdateFinal_CT(&hmac, digest, in,
- sz + hashSz + padSz + 1, myInner);
- }
-#else
- ret = Hmac_UpdateFinal(&hmac, digest, in, sz + hashSz + padSz + 1,
- myInner);
-#endif
- }
- else {
- ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
- if (ret == 0)
- ret = wc_HmacUpdate(&hmac, in, sz); /* content */
- if (ret == 0)
- ret = wc_HmacFinal(&hmac, digest);
- }
- }
-
- wc_HmacFree(&hmac);
-
- return ret;
-}
-#endif /* WOLFSSL_AEAD_ONLY */
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#ifdef HAVE_TLS_EXTENSIONS
-
-/**
- * The TLSX semaphore is used to calculate the size of the extensions to be sent
- * from one peer to another.
- */
-
-/** Supports up to 64 flags. Increase as needed. */
-#define SEMAPHORE_SIZE 8
-
-/**
- * Converts the extension type (id) to an index in the semaphore.
- *
- * Official reference for TLS extension types:
- * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
- *
- * Motivation:
- * Previously, we used the extension type itself as the index of that
- * extension in the semaphore as the extension types were declared
- * sequentially, but maintain a semaphore as big as the number of available
- * extensions is no longer an option since the release of renegotiation_info.
- *
- * How to update:
- * Assign extension types that extrapolate the number of available semaphores
- * to the first available index going backwards in the semaphore array.
- * When adding a new extension type that don't extrapolate the number of
- * available semaphores, check for a possible collision with with a
- * 'remapped' extension type.
- */
-static WC_INLINE word16 TLSX_ToSemaphore(word16 type)
-{
- switch (type) {
-
- case TLSX_RENEGOTIATION_INFO: /* 0xFF01 */
- return 63;
-
- default:
- if (type > 62) {
- /* This message SHOULD only happens during the adding of
- new TLS extensions in which its IANA number overflows
- the current semaphore's range, or if its number already
- is assigned to be used by another extension.
- Use this check value for the new extension and decrement
- the check value by one. */
- WOLFSSL_MSG("### TLSX semaphore collision or overflow detected!");
- }
- }
-
- return type;
-}
-
-/** Checks if a specific light (tls extension) is not set in the semaphore. */
-#define IS_OFF(semaphore, light) \
- (!(((semaphore)[(light) / 8] & (byte) (0x01 << ((light) % 8)))))
-
-/** Turn on a specific light (tls extension) in the semaphore. */
-/* the semaphore marks the extensions already written to the message */
-#define TURN_ON(semaphore, light) \
- ((semaphore)[(light) / 8] |= (byte) (0x01 << ((light) % 8)))
-
-/** Turn off a specific light (tls extension) in the semaphore. */
-#define TURN_OFF(semaphore, light) \
- ((semaphore)[(light) / 8] &= (byte) ~(0x01 << ((light) % 8)))
-
-/** Creates a new extension. */
-static TLSX* TLSX_New(TLSX_Type type, void* data, void* heap)
-{
- TLSX* extension = (TLSX*)XMALLOC(sizeof(TLSX), heap, DYNAMIC_TYPE_TLSX);
-
- (void)heap;
-
- if (extension) {
- extension->type = type;
- extension->data = data;
- extension->resp = 0;
- extension->next = NULL;
- }
-
- return extension;
-}
-
-/**
- * Creates a new extension and pushes it to the provided list.
- * Checks for duplicate extensions, keeps the newest.
- */
-static int TLSX_Push(TLSX** list, TLSX_Type type, void* data, void* heap)
-{
- TLSX* extension = TLSX_New(type, data, heap);
-
- if (extension == NULL)
- return MEMORY_E;
-
- /* pushes the new extension on the list. */
- extension->next = *list;
- *list = extension;
-
- /* remove duplicate extensions, there should be only one of each type. */
- do {
- if (extension->next && extension->next->type == type) {
- TLSX *next = extension->next;
-
- extension->next = next->next;
- next->next = NULL;
-
- TLSX_FreeAll(next, heap);
-
- /* there is no way to occur more than
- * two extensions of the same type.
- */
- break;
- }
- } while ((extension = extension->next));
-
- return 0;
-}
-
-#ifndef NO_WOLFSSL_CLIENT
-
-int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type);
-
-int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type)
-{
- TLSX *extension = TLSX_Find(ssl->extensions, type);
-
- if (!extension)
- extension = TLSX_Find(ssl->ctx->extensions, type);
-
- return extension == NULL;
-}
-
-int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl);
-
-int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl)
-{
- SendAlert(ssl, alert_fatal, unsupported_extension);
- return UNSUPPORTED_EXTENSION;
-}
-
-#else
-
-#define TLSX_CheckUnsupportedExtension(ssl, type) 0
-#define TLSX_HandleUnsupportedExtension(ssl) 0
-
-#endif
-
-/** Mark an extension to be sent back to the client. */
-void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type);
-
-void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type)
-{
- TLSX *extension = TLSX_Find(ssl->extensions, type);
-
- if (extension)
- extension->resp = 1;
-}
-
-/******************************************************************************/
-/* Application-Layer Protocol Negotiation */
-/******************************************************************************/
-
-#ifdef HAVE_ALPN
-/** Creates a new ALPN object, providing protocol name to use. */
-static ALPN* TLSX_ALPN_New(char *protocol_name, word16 protocol_nameSz,
- void* heap)
-{
- ALPN *alpn;
-
- WOLFSSL_ENTER("TLSX_ALPN_New");
-
- if (protocol_name == NULL ||
- protocol_nameSz > WOLFSSL_MAX_ALPN_PROTO_NAME_LEN) {
- WOLFSSL_MSG("Invalid arguments");
- return NULL;
- }
-
- alpn = (ALPN*)XMALLOC(sizeof(ALPN), heap, DYNAMIC_TYPE_TLSX);
- if (alpn == NULL) {
- WOLFSSL_MSG("Memory failure");
- return NULL;
- }
-
- alpn->next = NULL;
- alpn->negotiated = 0;
- alpn->options = 0;
-
- alpn->protocol_name = (char*)XMALLOC(protocol_nameSz + 1,
- heap, DYNAMIC_TYPE_TLSX);
- if (alpn->protocol_name == NULL) {
- WOLFSSL_MSG("Memory failure");
- XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
- return NULL;
- }
-
- XMEMCPY(alpn->protocol_name, protocol_name, protocol_nameSz);
- alpn->protocol_name[protocol_nameSz] = 0;
-
- (void)heap;
-
- return alpn;
-}
-
-/** Releases an ALPN object. */
-static void TLSX_ALPN_Free(ALPN *alpn, void* heap)
-{
- (void)heap;
-
- if (alpn == NULL)
- return;
-
- XFREE(alpn->protocol_name, heap, DYNAMIC_TYPE_TLSX);
- XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
-}
-
-/** Releases all ALPN objects in the provided list. */
-static void TLSX_ALPN_FreeAll(ALPN *list, void* heap)
-{
- ALPN* alpn;
-
- while ((alpn = list)) {
- list = alpn->next;
- TLSX_ALPN_Free(alpn, heap);
- }
-}
-
-/** Tells the buffered size of the ALPN objects in a list. */
-static word16 TLSX_ALPN_GetSize(ALPN *list)
-{
- ALPN* alpn;
- word16 length = OPAQUE16_LEN; /* list length */
-
- while ((alpn = list)) {
- list = alpn->next;
-
- length++; /* protocol name length is on one byte */
- length += (word16)XSTRLEN(alpn->protocol_name);
- }
-
- return length;
-}
-
-/** Writes the ALPN objects of a list in a buffer. */
-static word16 TLSX_ALPN_Write(ALPN *list, byte *output)
-{
- ALPN* alpn;
- word16 length = 0;
- word16 offset = OPAQUE16_LEN; /* list length offset */
-
- while ((alpn = list)) {
- list = alpn->next;
-
- length = (word16)XSTRLEN(alpn->protocol_name);
-
- /* protocol name length */
- output[offset++] = (byte)length;
-
- /* protocol name value */
- XMEMCPY(output + offset, alpn->protocol_name, length);
-
- offset += length;
- }
-
- /* writing list length */
- c16toa(offset - OPAQUE16_LEN, output);
-
- return offset;
-}
-
-/** Finds a protocol name in the provided ALPN list */
-static ALPN* TLSX_ALPN_Find(ALPN *list, char *protocol_name, word16 size)
-{
- ALPN *alpn;
-
- if (list == NULL || protocol_name == NULL)
- return NULL;
-
- alpn = list;
- while (alpn != NULL && (
- (word16)XSTRLEN(alpn->protocol_name) != size ||
- XSTRNCMP(alpn->protocol_name, protocol_name, size)))
- alpn = alpn->next;
-
- return alpn;
-}
-
-/** Set the ALPN matching client and server requirements */
-static int TLSX_SetALPN(TLSX** extensions, const void* data, word16 size,
- void* heap)
-{
- ALPN *alpn;
- int ret;
-
- if (extensions == NULL || data == NULL)
- return BAD_FUNC_ARG;
-
- alpn = TLSX_ALPN_New((char *)data, size, heap);
- if (alpn == NULL) {
- WOLFSSL_MSG("Memory failure");
- return MEMORY_E;
- }
-
- alpn->negotiated = 1;
-
- ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL, (void*)alpn,
- heap);
- if (ret != 0) {
- TLSX_ALPN_Free(alpn, heap);
- return ret;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-/** Parses a buffer of ALPN extensions and set the first one matching
- * client and server requirements */
-static int TLSX_ALPN_ParseAndSet(WOLFSSL *ssl, byte *input, word16 length,
- byte isRequest)
-{
- word16 size = 0, offset = 0, idx = 0;
- int r = BUFFER_ERROR;
- byte match = 0;
- TLSX *extension;
- ALPN *alpn = NULL, *list;
-
- if (OPAQUE16_LEN > length)
- return BUFFER_ERROR;
-
- ato16(input, &size);
- offset += OPAQUE16_LEN;
-
- if (size == 0)
- return BUFFER_ERROR;
-
- extension = TLSX_Find(ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
- if (extension == NULL)
- extension = TLSX_Find(ssl->ctx->extensions,
- TLSX_APPLICATION_LAYER_PROTOCOL);
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
- if (ssl->alpnSelect != NULL) {
- const byte* out;
- unsigned char outLen;
-
- if (ssl->alpnSelect(ssl, &out, &outLen, input + offset, size,
- ssl->alpnSelectArg) == 0) {
- WOLFSSL_MSG("ALPN protocol match");
- if (TLSX_UseALPN(&ssl->extensions, (char*)out, outLen, 0, ssl->heap)
- == WOLFSSL_SUCCESS) {
- if (extension == NULL) {
- extension = TLSX_Find(ssl->extensions,
- TLSX_APPLICATION_LAYER_PROTOCOL);
- }
- }
- }
- }
-#endif
-
- if (extension == NULL || extension->data == NULL) {
- return isRequest ? 0
- : TLSX_HandleUnsupportedExtension(ssl);
- }
-
- /* validating alpn list length */
- if (length != OPAQUE16_LEN + size)
- return BUFFER_ERROR;
-
- list = (ALPN*)extension->data;
-
- /* keep the list sent by client */
- if (isRequest) {
- if (ssl->alpn_client_list != NULL)
- XFREE(ssl->alpn_client_list, ssl->heap, DYNAMIC_TYPE_ALPN);
-
- ssl->alpn_client_list = (char *)XMALLOC(size, ssl->heap,
- DYNAMIC_TYPE_ALPN);
- if (ssl->alpn_client_list == NULL)
- return MEMORY_ERROR;
- }
-
- for (size = 0; offset < length; offset += size) {
-
- size = input[offset++];
- if (offset + size > length || size == 0)
- return BUFFER_ERROR;
-
- if (isRequest) {
- XMEMCPY(ssl->alpn_client_list+idx, (char*)input + offset, size);
- idx += size;
- ssl->alpn_client_list[idx++] = ',';
- }
-
- if (!match) {
- alpn = TLSX_ALPN_Find(list, (char*)input + offset, size);
- if (alpn != NULL) {
- WOLFSSL_MSG("ALPN protocol match");
- match = 1;
-
- /* skip reading other values if not required */
- if (!isRequest)
- break;
- }
- }
- }
-
- if (isRequest)
- ssl->alpn_client_list[idx-1] = 0;
-
- if (!match) {
- WOLFSSL_MSG("No ALPN protocol match");
-
- /* do nothing if no protocol match between client and server and option
- is set to continue (like OpenSSL) */
- if (list->options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) {
- WOLFSSL_MSG("Continue on mismatch");
- return 0;
- }
-
- SendAlert(ssl, alert_fatal, no_application_protocol);
- return UNKNOWN_ALPN_PROTOCOL_NAME_E;
- }
-
- /* set the matching negotiated protocol */
- r = TLSX_SetALPN(&ssl->extensions,
- alpn->protocol_name,
- (word16)XSTRLEN(alpn->protocol_name),
- ssl->heap);
- if (r != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("TLSX_UseALPN failed");
- return BUFFER_ERROR;
- }
-
- /* reply to ALPN extension sent from client */
- if (isRequest) {
-#ifndef NO_WOLFSSL_SERVER
- TLSX_SetResponse(ssl, TLSX_APPLICATION_LAYER_PROTOCOL);
-#endif
- }
-
- return 0;
-}
-
-/** Add a protocol name to the list of accepted usable ones */
-int TLSX_UseALPN(TLSX** extensions, const void* data, word16 size, byte options,
- void* heap)
-{
- ALPN *alpn;
- TLSX *extension;
- int ret;
-
- if (extensions == NULL || data == NULL)
- return BAD_FUNC_ARG;
-
- alpn = TLSX_ALPN_New((char *)data, size, heap);
- if (alpn == NULL) {
- WOLFSSL_MSG("Memory failure");
- return MEMORY_E;
- }
-
- /* Set Options of ALPN */
- alpn->options = options;
-
- extension = TLSX_Find(*extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
- if (extension == NULL) {
- ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL,
- (void*)alpn, heap);
- if (ret != 0) {
- TLSX_ALPN_Free(alpn, heap);
- return ret;
- }
- }
- else {
- /* push new ALPN object to extension data. */
- alpn->next = (ALPN*)extension->data;
- extension->data = (void*)alpn;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-/** Get the protocol name set by the server */
-int TLSX_ALPN_GetRequest(TLSX* extensions, void** data, word16 *dataSz)
-{
- TLSX *extension;
- ALPN *alpn;
-
- if (extensions == NULL || data == NULL || dataSz == NULL)
- return BAD_FUNC_ARG;
-
- extension = TLSX_Find(extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
- if (extension == NULL) {
- WOLFSSL_MSG("TLS extension not found");
- return WOLFSSL_ALPN_NOT_FOUND;
- }
-
- alpn = (ALPN *)extension->data;
- if (alpn == NULL) {
- WOLFSSL_MSG("ALPN extension not found");
- *data = NULL;
- *dataSz = 0;
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (alpn->negotiated != 1) {
-
- /* consider as an error */
- if (alpn->options & WOLFSSL_ALPN_FAILED_ON_MISMATCH) {
- WOLFSSL_MSG("No protocol match with peer -> Failed");
- return WOLFSSL_FATAL_ERROR;
- }
-
- /* continue without negotiated protocol */
- WOLFSSL_MSG("No protocol match with peer -> Continue");
- return WOLFSSL_ALPN_NOT_FOUND;
- }
-
- if (alpn->next != NULL) {
- WOLFSSL_MSG("Only one protocol name must be accepted");
- return WOLFSSL_FATAL_ERROR;
- }
-
- *data = alpn->protocol_name;
- *dataSz = (word16)XSTRLEN((char*)*data);
-
- return WOLFSSL_SUCCESS;
-}
-
-#define ALPN_FREE_ALL TLSX_ALPN_FreeAll
-#define ALPN_GET_SIZE TLSX_ALPN_GetSize
-#define ALPN_WRITE TLSX_ALPN_Write
-#define ALPN_PARSE TLSX_ALPN_ParseAndSet
-
-#else /* HAVE_ALPN */
-
-#define ALPN_FREE_ALL(list, heap)
-#define ALPN_GET_SIZE(list) 0
-#define ALPN_WRITE(a, b) 0
-#define ALPN_PARSE(a, b, c, d) 0
-
-#endif /* HAVE_ALPN */
-
-/******************************************************************************/
-/* Server Name Indication */
-/******************************************************************************/
-
-#ifdef HAVE_SNI
-
-/** Creates a new SNI object. */
-static SNI* TLSX_SNI_New(byte type, const void* data, word16 size, void* heap)
-{
- SNI* sni = (SNI*)XMALLOC(sizeof(SNI), heap, DYNAMIC_TYPE_TLSX);
-
- (void)heap;
-
- if (sni) {
- sni->type = type;
- sni->next = NULL;
-
- #ifndef NO_WOLFSSL_SERVER
- sni->options = 0;
- sni->status = WOLFSSL_SNI_NO_MATCH;
- #endif
-
- switch (sni->type) {
- case WOLFSSL_SNI_HOST_NAME:
- sni->data.host_name = (char*)XMALLOC(size + 1, heap,
- DYNAMIC_TYPE_TLSX);
- if (sni->data.host_name) {
- XSTRNCPY(sni->data.host_name, (const char*)data, size);
- sni->data.host_name[size] = '\0';
- } else {
- XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
- sni = NULL;
- }
- break;
-
- default: /* invalid type */
- XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
- sni = NULL;
- }
- }
-
- return sni;
-}
-
-/** Releases a SNI object. */
-static void TLSX_SNI_Free(SNI* sni, void* heap)
-{
- if (sni) {
- switch (sni->type) {
- case WOLFSSL_SNI_HOST_NAME:
- XFREE(sni->data.host_name, heap, DYNAMIC_TYPE_TLSX);
- break;
- }
-
- XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
- }
- (void)heap;
-}
-
-/** Releases all SNI objects in the provided list. */
-static void TLSX_SNI_FreeAll(SNI* list, void* heap)
-{
- SNI* sni;
-
- while ((sni = list)) {
- list = sni->next;
- TLSX_SNI_Free(sni, heap);
- }
-}
-
-/** Tells the buffered size of the SNI objects in a list. */
-static word16 TLSX_SNI_GetSize(SNI* list)
-{
- SNI* sni;
- word16 length = OPAQUE16_LEN; /* list length */
-
- while ((sni = list)) {
- list = sni->next;
-
- length += ENUM_LEN + OPAQUE16_LEN; /* sni type + sni length */
-
- switch (sni->type) {
- case WOLFSSL_SNI_HOST_NAME:
- length += (word16)XSTRLEN((char*)sni->data.host_name);
- break;
- }
- }
-
- return length;
-}
-
-/** Writes the SNI objects of a list in a buffer. */
-static word16 TLSX_SNI_Write(SNI* list, byte* output)
-{
- SNI* sni;
- word16 length = 0;
- word16 offset = OPAQUE16_LEN; /* list length offset */
-
- while ((sni = list)) {
- list = sni->next;
-
- output[offset++] = sni->type; /* sni type */
-
- switch (sni->type) {
- case WOLFSSL_SNI_HOST_NAME:
- length = (word16)XSTRLEN((char*)sni->data.host_name);
-
- c16toa(length, output + offset); /* sni length */
- offset += OPAQUE16_LEN;
-
- XMEMCPY(output + offset, sni->data.host_name, length);
-
- offset += length;
- break;
- }
- }
-
- c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
-
- return offset;
-}
-
-/** Finds a SNI object in the provided list. */
-static SNI* TLSX_SNI_Find(SNI *list, byte type)
-{
- SNI* sni = list;
-
- while (sni && sni->type != type)
- sni = sni->next;
-
- return sni;
-}
-
-/** Sets the status of a SNI object. */
-static void TLSX_SNI_SetStatus(TLSX* extensions, byte type, byte status)
-{
- TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
- SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
-
- if (sni)
- sni->status = status;
-}
-
-/** Gets the status of a SNI object. */
-byte TLSX_SNI_Status(TLSX* extensions, byte type)
-{
- TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
- SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
-
- if (sni)
- return sni->status;
-
- return 0;
-}
-
-/** Parses a buffer of SNI extensions. */
-static int TLSX_SNI_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte isRequest)
-{
-#ifndef NO_WOLFSSL_SERVER
- word16 size = 0;
- word16 offset = 0;
- int cacheOnly = 0;
- SNI *sni = NULL;
- byte type;
- int matchStat;
- byte matched;
-#endif
-
- TLSX *extension = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
-
- if (!extension)
- extension = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
-
- if (!isRequest) {
- #ifndef NO_WOLFSSL_CLIENT
- if (!extension || !extension->data)
- return TLSX_HandleUnsupportedExtension(ssl);
-
- if (length > 0)
- return BUFFER_ERROR; /* SNI response MUST be empty. */
-
- /* This call enables wolfSSL_SNI_GetRequest() to be called in the
- * client side to fetch the used SNI. It will only work if the SNI
- * was set at the SSL object level. Right now we only support one
- * name type, WOLFSSL_SNI_HOST_NAME, but in the future, the
- * inclusion of other name types will turn this method inaccurate,
- * as the extension response doesn't contains information of which
- * name was accepted.
- */
- TLSX_SNI_SetStatus(ssl->extensions, WOLFSSL_SNI_HOST_NAME,
- WOLFSSL_SNI_REAL_MATCH);
-
- return 0;
- #endif
- }
-
-#ifndef NO_WOLFSSL_SERVER
- if (!extension || !extension->data) {
- #if defined(WOLFSSL_ALWAYS_KEEP_SNI) && !defined(NO_WOLFSSL_SERVER)
- /* This will keep SNI even though TLSX_UseSNI has not been called.
- * Enable it so that the received sni is available to functions
- * that use a custom callback when SNI is received.
- */
-
- cacheOnly = 1;
- WOLFSSL_MSG("Forcing SSL object to store SNI parameter");
- #else
- /* Skipping, SNI not enabled at server side. */
- return 0;
- #endif
- }
-
- if (OPAQUE16_LEN > length)
- return BUFFER_ERROR;
-
- ato16(input, &size);
- offset += OPAQUE16_LEN;
-
- /* validating sni list length */
- if (length != OPAQUE16_LEN + size || size == 0)
- return BUFFER_ERROR;
-
- /* SNI was badly specified and only one type is now recognized and allowed.
- * Only one SNI value per type (RFC6066), so, no loop. */
- type = input[offset++];
- if (type != WOLFSSL_SNI_HOST_NAME)
- return BUFFER_ERROR;
-
- if (offset + OPAQUE16_LEN > length)
- return BUFFER_ERROR;
- ato16(input + offset, &size);
- offset += OPAQUE16_LEN;
-
- if (offset + size != length || size == 0)
- return BUFFER_ERROR;
-
- if (!cacheOnly && !(sni = TLSX_SNI_Find((SNI*)extension->data, type)))
- return 0; /* not using this type of SNI. */
-
-#ifdef WOLFSSL_TLS13
- /* Don't process the second ClientHello SNI extension if there
- * was problems with the first.
- */
- if (!cacheOnly && sni->status != 0)
- return 0;
-#endif
- matched = cacheOnly || (XSTRLEN(sni->data.host_name) == size &&
- XSTRNCMP(sni->data.host_name, (const char*)input + offset, size) == 0);
-
- if (matched || sni->options & WOLFSSL_SNI_ANSWER_ON_MISMATCH) {
- int r = TLSX_UseSNI(&ssl->extensions, type, input + offset, size,
- ssl->heap);
- if (r != WOLFSSL_SUCCESS)
- return r; /* throws error. */
-
- if (cacheOnly) {
- WOLFSSL_MSG("Forcing storage of SNI, Fake match");
- matchStat = WOLFSSL_SNI_FORCE_KEEP;
- }
- else if (matched) {
- WOLFSSL_MSG("SNI did match!");
- matchStat = WOLFSSL_SNI_REAL_MATCH;
- }
- else {
- WOLFSSL_MSG("fake SNI match from ANSWER_ON_MISMATCH");
- matchStat = WOLFSSL_SNI_FAKE_MATCH;
- }
-
- TLSX_SNI_SetStatus(ssl->extensions, type, (byte)matchStat);
-
- if(!cacheOnly)
- TLSX_SetResponse(ssl, TLSX_SERVER_NAME);
- }
- else if (!(sni->options & WOLFSSL_SNI_CONTINUE_ON_MISMATCH)) {
- SendAlert(ssl, alert_fatal, unrecognized_name);
-
- return UNKNOWN_SNI_HOST_NAME_E;
- }
-#else
- (void)input;
-#endif
-
- return 0;
-}
-
-static int TLSX_SNI_VerifyParse(WOLFSSL* ssl, byte isRequest)
-{
- (void)ssl;
-
- if (isRequest) {
- #ifndef NO_WOLFSSL_SERVER
- TLSX* ctx_ext = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
- TLSX* ssl_ext = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
- SNI* ctx_sni = ctx_ext ? (SNI*)ctx_ext->data : NULL;
- SNI* ssl_sni = ssl_ext ? (SNI*)ssl_ext->data : NULL;
- SNI* sni = NULL;
-
- for (; ctx_sni; ctx_sni = ctx_sni->next) {
- if (ctx_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
- sni = TLSX_SNI_Find(ssl_sni, ctx_sni->type);
-
- if (sni) {
- if (sni->status != WOLFSSL_SNI_NO_MATCH)
- continue;
-
- /* if ssl level overrides ctx level, it is ok. */
- if ((sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) == 0)
- continue;
- }
-
- SendAlert(ssl, alert_fatal, handshake_failure);
- return SNI_ABSENT_ERROR;
- }
- }
-
- for (; ssl_sni; ssl_sni = ssl_sni->next) {
- if (ssl_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
- if (ssl_sni->status != WOLFSSL_SNI_NO_MATCH)
- continue;
-
- SendAlert(ssl, alert_fatal, handshake_failure);
- return SNI_ABSENT_ERROR;
- }
- }
- #endif /* NO_WOLFSSL_SERVER */
- }
-
- return 0;
-}
-
-int TLSX_UseSNI(TLSX** extensions, byte type, const void* data, word16 size,
- void* heap)
-{
- TLSX* extension;
- SNI* sni = NULL;
-
- if (extensions == NULL || data == NULL)
- return BAD_FUNC_ARG;
-
- if ((sni = TLSX_SNI_New(type, data, size, heap)) == NULL)
- return MEMORY_E;
-
- extension = TLSX_Find(*extensions, TLSX_SERVER_NAME);
- if (!extension) {
- int ret = TLSX_Push(extensions, TLSX_SERVER_NAME, (void*)sni, heap);
-
- if (ret != 0) {
- TLSX_SNI_Free(sni, heap);
- return ret;
- }
- }
- else {
- /* push new SNI object to extension data. */
- sni->next = (SNI*)extension->data;
- extension->data = (void*)sni;
-
- /* remove duplicate SNI, there should be only one of each type. */
- do {
- if (sni->next && sni->next->type == type) {
- SNI* next = sni->next;
-
- sni->next = next->next;
- TLSX_SNI_Free(next, heap);
-
- /* there is no way to occur more than
- * two SNIs of the same type.
- */
- break;
- }
- } while ((sni = sni->next));
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-#ifndef NO_WOLFSSL_SERVER
-
-/** Tells the SNI requested by the client. */
-word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type, void** data)
-{
- TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
- SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
-
- if (sni && sni->status != WOLFSSL_SNI_NO_MATCH) {
- switch (sni->type) {
- case WOLFSSL_SNI_HOST_NAME:
- if (data) {
- *data = sni->data.host_name;
- return (word16)XSTRLEN((char*)*data);
- }
- }
- }
-
- return 0;
-}
-
-/** Sets the options for a SNI object. */
-void TLSX_SNI_SetOptions(TLSX* extensions, byte type, byte options)
-{
- TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
- SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
-
- if (sni)
- sni->options = options;
-}
-
-/** Retrieves a SNI request from a client hello buffer. */
-int TLSX_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
- byte type, byte* sni, word32* inOutSz)
-{
- word32 offset = 0;
- word32 len32 = 0;
- word16 len16 = 0;
-
- if (helloSz < RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + CLIENT_HELLO_FIRST)
- return INCOMPLETE_DATA;
-
- /* TLS record header */
- if ((enum ContentType) clientHello[offset++] != handshake) {
-
- /* checking for SSLv2.0 client hello according to: */
- /* http://tools.ietf.org/html/rfc4346#appendix-E.1 */
- if ((enum HandShakeType) clientHello[++offset] == client_hello) {
- offset += ENUM_LEN + VERSION_SZ; /* skip version */
-
- ato16(clientHello + offset, &len16);
- offset += OPAQUE16_LEN;
-
- if (len16 % 3) /* cipher_spec_length must be multiple of 3 */
- return BUFFER_ERROR;
-
- ato16(clientHello + offset, &len16);
- /* Returning SNI_UNSUPPORTED do not increment offset here */
-
- if (len16 != 0) /* session_id_length must be 0 */
- return BUFFER_ERROR;
-
- return SNI_UNSUPPORTED;
- }
-
- return BUFFER_ERROR;
- }
-
- if (clientHello[offset++] != SSLv3_MAJOR)
- return BUFFER_ERROR;
-
- if (clientHello[offset++] < TLSv1_MINOR)
- return SNI_UNSUPPORTED;
-
- ato16(clientHello + offset, &len16);
- offset += OPAQUE16_LEN;
-
- if (offset + len16 > helloSz)
- return INCOMPLETE_DATA;
-
- /* Handshake header */
- if ((enum HandShakeType) clientHello[offset] != client_hello)
- return BUFFER_ERROR;
-
- c24to32(clientHello + offset + 1, &len32);
- offset += HANDSHAKE_HEADER_SZ;
-
- if (offset + len32 > helloSz)
- return BUFFER_ERROR;
-
- /* client hello */
- offset += VERSION_SZ + RAN_LEN; /* version, random */
-
- if (helloSz < offset + clientHello[offset])
- return BUFFER_ERROR;
-
- offset += ENUM_LEN + clientHello[offset]; /* skip session id */
-
- /* cypher suites */
- if (helloSz < offset + OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- ato16(clientHello + offset, &len16);
- offset += OPAQUE16_LEN;
-
- if (helloSz < offset + len16)
- return BUFFER_ERROR;
-
- offset += len16; /* skip cypher suites */
-
- /* compression methods */
- if (helloSz < offset + 1)
- return BUFFER_ERROR;
-
- if (helloSz < offset + clientHello[offset])
- return BUFFER_ERROR;
-
- offset += ENUM_LEN + clientHello[offset]; /* skip compression methods */
-
- /* extensions */
- if (helloSz < offset + OPAQUE16_LEN)
- return 0; /* no extensions in client hello. */
-
- ato16(clientHello + offset, &len16);
- offset += OPAQUE16_LEN;
-
- if (helloSz < offset + len16)
- return BUFFER_ERROR;
-
- while (len16 >= OPAQUE16_LEN + OPAQUE16_LEN) {
- word16 extType;
- word16 extLen;
-
- ato16(clientHello + offset, &extType);
- offset += OPAQUE16_LEN;
-
- ato16(clientHello + offset, &extLen);
- offset += OPAQUE16_LEN;
-
- if (helloSz < offset + extLen)
- return BUFFER_ERROR;
-
- if (extType != TLSX_SERVER_NAME) {
- offset += extLen; /* skip extension */
- } else {
- word16 listLen;
-
- ato16(clientHello + offset, &listLen);
- offset += OPAQUE16_LEN;
-
- if (helloSz < offset + listLen)
- return BUFFER_ERROR;
-
- while (listLen > ENUM_LEN + OPAQUE16_LEN) {
- byte sniType = clientHello[offset++];
- word16 sniLen;
-
- ato16(clientHello + offset, &sniLen);
- offset += OPAQUE16_LEN;
-
- if (helloSz < offset + sniLen)
- return BUFFER_ERROR;
-
- if (sniType != type) {
- offset += sniLen;
- listLen -= min(ENUM_LEN + OPAQUE16_LEN + sniLen, listLen);
- continue;
- }
-
- *inOutSz = min(sniLen, *inOutSz);
- XMEMCPY(sni, clientHello + offset, *inOutSz);
-
- return WOLFSSL_SUCCESS;
- }
- }
-
- len16 -= min(2 * OPAQUE16_LEN + extLen, len16);
- }
-
- return len16 ? BUFFER_ERROR : 0;
-}
-
-#endif
-
-#define SNI_FREE_ALL TLSX_SNI_FreeAll
-#define SNI_GET_SIZE TLSX_SNI_GetSize
-#define SNI_WRITE TLSX_SNI_Write
-#define SNI_PARSE TLSX_SNI_Parse
-#define SNI_VERIFY_PARSE TLSX_SNI_VerifyParse
-
-#else
-
-#define SNI_FREE_ALL(list, heap)
-#define SNI_GET_SIZE(list) 0
-#define SNI_WRITE(a, b) 0
-#define SNI_PARSE(a, b, c, d) 0
-#define SNI_VERIFY_PARSE(a, b) 0
-
-#endif /* HAVE_SNI */
-
-/******************************************************************************/
-/* Trusted CA Key Indication */
-/******************************************************************************/
-
-#ifdef HAVE_TRUSTED_CA
-
-/** Creates a new TCA object. */
-static TCA* TLSX_TCA_New(byte type, const byte* id, word16 idSz, void* heap)
-{
- TCA* tca = (TCA*)XMALLOC(sizeof(TCA), heap, DYNAMIC_TYPE_TLSX);
-
- if (tca) {
- XMEMSET(tca, 0, sizeof(TCA));
- tca->type = type;
-
- switch (type) {
- case WOLFSSL_TRUSTED_CA_PRE_AGREED:
- break;
-
- #ifndef NO_SHA
- case WOLFSSL_TRUSTED_CA_KEY_SHA1:
- case WOLFSSL_TRUSTED_CA_CERT_SHA1:
- if (idSz == WC_SHA_DIGEST_SIZE &&
- (tca->id =
- (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
- XMEMCPY(tca->id, id, idSz);
- tca->idSz = idSz;
- }
- else {
- XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
- tca = NULL;
- }
- break;
- #endif
-
- case WOLFSSL_TRUSTED_CA_X509_NAME:
- if (idSz > 0 &&
- (tca->id =
- (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
- XMEMCPY(tca->id, id, idSz);
- tca->idSz = idSz;
- }
- else {
- XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
- tca = NULL;
- }
- break;
-
- default: /* invalid type */
- XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
- tca = NULL;
- }
- }
-
- (void)heap;
-
- return tca;
-}
-
-/** Releases a TCA object. */
-static void TLSX_TCA_Free(TCA* tca, void* heap)
-{
- (void)heap;
-
- if (tca) {
- if (tca->id)
- XFREE(tca->id, heap, DYNAMIC_TYPE_TLSX);
- XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
- }
-}
-
-/** Releases all TCA objects in the provided list. */
-static void TLSX_TCA_FreeAll(TCA* list, void* heap)
-{
- TCA* tca;
-
- while ((tca = list)) {
- list = tca->next;
- TLSX_TCA_Free(tca, heap);
- }
-}
-
-/** Tells the buffered size of the TCA objects in a list. */
-static word16 TLSX_TCA_GetSize(TCA* list)
-{
- TCA* tca;
- word16 length = OPAQUE16_LEN; /* list length */
-
- while ((tca = list)) {
- list = tca->next;
-
- length += ENUM_LEN; /* tca type */
-
- switch (tca->type) {
- case WOLFSSL_TRUSTED_CA_PRE_AGREED:
- break;
- case WOLFSSL_TRUSTED_CA_KEY_SHA1:
- case WOLFSSL_TRUSTED_CA_CERT_SHA1:
- length += tca->idSz;
- break;
- case WOLFSSL_TRUSTED_CA_X509_NAME:
- length += OPAQUE16_LEN + tca->idSz;
- break;
- }
- }
-
- return length;
-}
-
-/** Writes the TCA objects of a list in a buffer. */
-static word16 TLSX_TCA_Write(TCA* list, byte* output)
-{
- TCA* tca;
- word16 offset = OPAQUE16_LEN; /* list length offset */
-
- while ((tca = list)) {
- list = tca->next;
-
- output[offset++] = tca->type; /* tca type */
-
- switch (tca->type) {
- case WOLFSSL_TRUSTED_CA_PRE_AGREED:
- break;
- #ifndef NO_SHA
- case WOLFSSL_TRUSTED_CA_KEY_SHA1:
- case WOLFSSL_TRUSTED_CA_CERT_SHA1:
- if (tca->id != NULL) {
- XMEMCPY(output + offset, tca->id, tca->idSz);
- offset += tca->idSz;
- }
- else {
- /* ID missing. Set to an empty string. */
- c16toa(0, output + offset);
- offset += OPAQUE16_LEN;
- }
- break;
- #endif
- case WOLFSSL_TRUSTED_CA_X509_NAME:
- if (tca->id != NULL) {
- c16toa(tca->idSz, output + offset); /* tca length */
- offset += OPAQUE16_LEN;
- XMEMCPY(output + offset, tca->id, tca->idSz);
- offset += tca->idSz;
- }
- else {
- /* ID missing. Set to an empty string. */
- c16toa(0, output + offset);
- offset += OPAQUE16_LEN;
- }
- break;
- default:
- /* ID unknown. Set to an empty string. */
- c16toa(0, output + offset);
- offset += OPAQUE16_LEN;
- }
- }
-
- c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
-
- return offset;
-}
-
-#ifndef NO_WOLFSSL_SERVER
-static TCA* TLSX_TCA_Find(TCA *list, byte type, const byte* id, word16 idSz)
-{
- TCA* tca = list;
-
- while (tca && tca->type != type && type != WOLFSSL_TRUSTED_CA_PRE_AGREED &&
- idSz != tca->idSz && !XMEMCMP(id, tca->id, idSz))
- tca = tca->next;
-
- return tca;
-}
-#endif /* NO_WOLFSSL_SERVER */
-
-/** Parses a buffer of TCA extensions. */
-static int TLSX_TCA_Parse(WOLFSSL* ssl, const byte* input, word16 length,
- byte isRequest)
-{
-#ifndef NO_WOLFSSL_SERVER
- word16 size = 0;
- word16 offset = 0;
-#endif
-
- TLSX *extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
-
- if (!extension)
- extension = TLSX_Find(ssl->ctx->extensions, TLSX_TRUSTED_CA_KEYS);
-
- if (!isRequest) {
- #ifndef NO_WOLFSSL_CLIENT
- if (!extension || !extension->data)
- return TLSX_HandleUnsupportedExtension(ssl);
-
- if (length > 0)
- return BUFFER_ERROR; /* TCA response MUST be empty. */
-
- /* Set the flag that we're good for keys */
- TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
-
- return 0;
- #endif
- }
-
-#ifndef NO_WOLFSSL_SERVER
- if (!extension || !extension->data) {
- /* Skipping, TCA not enabled at server side. */
- return 0;
- }
-
- if (OPAQUE16_LEN > length)
- return BUFFER_ERROR;
-
- ato16(input, &size);
- offset += OPAQUE16_LEN;
-
- /* validating tca list length */
- if (length != OPAQUE16_LEN + size)
- return BUFFER_ERROR;
-
- for (size = 0; offset < length; offset += size) {
- TCA *tca = NULL;
- byte type;
- const byte* id = NULL;
- word16 idSz = 0;
-
- if (offset + ENUM_LEN > length)
- return BUFFER_ERROR;
-
- type = input[offset++];
-
- switch (type) {
- case WOLFSSL_TRUSTED_CA_PRE_AGREED:
- break;
- #ifndef NO_SHA
- case WOLFSSL_TRUSTED_CA_KEY_SHA1:
- case WOLFSSL_TRUSTED_CA_CERT_SHA1:
- if (offset + WC_SHA_DIGEST_SIZE > length)
- return BUFFER_ERROR;
- idSz = WC_SHA_DIGEST_SIZE;
- id = input + offset;
- offset += idSz;
- break;
- #endif
- case WOLFSSL_TRUSTED_CA_X509_NAME:
- if (offset + OPAQUE16_LEN > length)
- return BUFFER_ERROR;
- ato16(input + offset, &idSz);
- offset += OPAQUE16_LEN;
- if ((offset > length) || (idSz > length - offset))
- return BUFFER_ERROR;
- id = input + offset;
- offset += idSz;
- break;
- default:
- return TCA_INVALID_ID_TYPE;
- }
-
- /* Find the type/ID in the TCA list. */
- tca = TLSX_TCA_Find((TCA*)extension->data, type, id, idSz);
- if (tca != NULL) {
- /* Found it. Set the response flag and break out of the loop. */
- TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
- break;
- }
- }
-#else
- (void)input;
-#endif
-
- return 0;
-}
-
-/* Checks to see if the server sent a response for the TCA. */
-static int TLSX_TCA_VerifyParse(WOLFSSL* ssl, byte isRequest)
-{
- (void)ssl;
-
- if (!isRequest) {
- #ifndef NO_WOLFSSL_CLIENT
- TLSX* extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
-
- if (extension && !extension->resp) {
- SendAlert(ssl, alert_fatal, handshake_failure);
- return TCA_ABSENT_ERROR;
- }
- #endif /* NO_WOLFSSL_CLIENT */
- }
-
- return 0;
-}
-
-int TLSX_UseTrustedCA(TLSX** extensions, byte type,
- const byte* id, word16 idSz, void* heap)
-{
- TLSX* extension;
- TCA* tca = NULL;
-
- if (extensions == NULL)
- return BAD_FUNC_ARG;
-
- if ((tca = TLSX_TCA_New(type, id, idSz, heap)) == NULL)
- return MEMORY_E;
-
- extension = TLSX_Find(*extensions, TLSX_TRUSTED_CA_KEYS);
- if (!extension) {
- int ret = TLSX_Push(extensions, TLSX_TRUSTED_CA_KEYS, (void*)tca, heap);
-
- if (ret != 0) {
- TLSX_TCA_Free(tca, heap);
- return ret;
- }
- }
- else {
- /* push new TCA object to extension data. */
- tca->next = (TCA*)extension->data;
- extension->data = (void*)tca;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-#define TCA_FREE_ALL TLSX_TCA_FreeAll
-#define TCA_GET_SIZE TLSX_TCA_GetSize
-#define TCA_WRITE TLSX_TCA_Write
-#define TCA_PARSE TLSX_TCA_Parse
-#define TCA_VERIFY_PARSE TLSX_TCA_VerifyParse
-
-#else /* HAVE_TRUSTED_CA */
-
-#define TCA_FREE_ALL(list, heap)
-#define TCA_GET_SIZE(list) 0
-#define TCA_WRITE(a, b) 0
-#define TCA_PARSE(a, b, c, d) 0
-#define TCA_VERIFY_PARSE(a, b) 0
-
-#endif /* HAVE_TRUSTED_CA */
-
-/******************************************************************************/
-/* Max Fragment Length Negotiation */
-/******************************************************************************/
-
-#ifdef HAVE_MAX_FRAGMENT
-
-static word16 TLSX_MFL_Write(byte* data, byte* output)
-{
- output[0] = data[0];
-
- return ENUM_LEN;
-}
-
-static int TLSX_MFL_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte isRequest)
-{
- if (length != ENUM_LEN)
- return BUFFER_ERROR;
-
-#ifdef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
- (void) isRequest;
-#else
- if (!isRequest)
- if (TLSX_CheckUnsupportedExtension(ssl, TLSX_MAX_FRAGMENT_LENGTH))
- return TLSX_HandleUnsupportedExtension(ssl);
-#endif
-
- switch (*input) {
- case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
- case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
- case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
- case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
- case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
- case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
-
- default:
- SendAlert(ssl, alert_fatal, illegal_parameter);
-
- return UNKNOWN_MAX_FRAG_LEN_E;
- }
-
-#ifndef NO_WOLFSSL_SERVER
- if (isRequest) {
- int ret = TLSX_UseMaxFragment(&ssl->extensions, *input, ssl->heap);
-
- if (ret != WOLFSSL_SUCCESS)
- return ret; /* throw error */
-
- TLSX_SetResponse(ssl, TLSX_MAX_FRAGMENT_LENGTH);
- }
-#endif
-
- return 0;
-}
-
-int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap)
-{
- byte* data = NULL;
- int ret = 0;
-
- if (extensions == NULL || mfl < WOLFSSL_MFL_MIN || mfl > WOLFSSL_MFL_MAX)
- return BAD_FUNC_ARG;
-
- data = (byte*)XMALLOC(ENUM_LEN, heap, DYNAMIC_TYPE_TLSX);
- if (data == NULL)
- return MEMORY_E;
-
- data[0] = mfl;
-
- ret = TLSX_Push(extensions, TLSX_MAX_FRAGMENT_LENGTH, data, heap);
- if (ret != 0) {
- XFREE(data, heap, DYNAMIC_TYPE_TLSX);
- return ret;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-
-#define MFL_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
-#define MFL_GET_SIZE(data) ENUM_LEN
-#define MFL_WRITE TLSX_MFL_Write
-#define MFL_PARSE TLSX_MFL_Parse
-
-#else
-
-#define MFL_FREE_ALL(a, b)
-#define MFL_GET_SIZE(a) 0
-#define MFL_WRITE(a, b) 0
-#define MFL_PARSE(a, b, c, d) 0
-
-#endif /* HAVE_MAX_FRAGMENT */
-
-/******************************************************************************/
-/* Truncated HMAC */
-/******************************************************************************/
-
-#ifdef HAVE_TRUNCATED_HMAC
-
-static int TLSX_THM_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte isRequest)
-{
- if (length != 0 || input == NULL)
- return BUFFER_ERROR;
-
- if (!isRequest) {
- #ifndef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
- if (TLSX_CheckUnsupportedExtension(ssl, TLSX_TRUNCATED_HMAC))
- return TLSX_HandleUnsupportedExtension(ssl);
- #endif
- }
- else {
- #ifndef NO_WOLFSSL_SERVER
- int ret = TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
-
- if (ret != WOLFSSL_SUCCESS)
- return ret; /* throw error */
-
- TLSX_SetResponse(ssl, TLSX_TRUNCATED_HMAC);
- #endif
- }
-
- ssl->truncated_hmac = 1;
-
- return 0;
-}
-
-int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap)
-{
- int ret = 0;
-
- if (extensions == NULL)
- return BAD_FUNC_ARG;
-
- ret = TLSX_Push(extensions, TLSX_TRUNCATED_HMAC, NULL, heap);
- if (ret != 0)
- return ret;
-
- return WOLFSSL_SUCCESS;
-}
-
-#define THM_PARSE TLSX_THM_Parse
-
-#else
-
-#define THM_PARSE(a, b, c, d) 0
-
-#endif /* HAVE_TRUNCATED_HMAC */
-
-/******************************************************************************/
-/* Certificate Status Request */
-/******************************************************************************/
-
-#ifdef HAVE_CERTIFICATE_STATUS_REQUEST
-
-static void TLSX_CSR_Free(CertificateStatusRequest* csr, void* heap)
-{
- switch (csr->status_type) {
- case WOLFSSL_CSR_OCSP:
- FreeOcspRequest(&csr->request.ocsp);
- break;
- }
-
- XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
- (void)heap;
-}
-
-static word16 TLSX_CSR_GetSize(CertificateStatusRequest* csr, byte isRequest)
-{
- word16 size = 0;
-
- /* shut up compiler warnings */
- (void) csr; (void) isRequest;
-
-#ifndef NO_WOLFSSL_CLIENT
- if (isRequest) {
- switch (csr->status_type) {
- case WOLFSSL_CSR_OCSP:
- size += ENUM_LEN + 2 * OPAQUE16_LEN;
-
- if (csr->request.ocsp.nonceSz)
- size += OCSP_NONCE_EXT_SZ;
- break;
- }
- }
-#endif
-#if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
- if (!isRequest && csr->ssl->options.tls1_3)
- return OPAQUE8_LEN + OPAQUE24_LEN + csr->response.length;
-#endif
-
- return size;
-}
-
-static word16 TLSX_CSR_Write(CertificateStatusRequest* csr, byte* output,
- byte isRequest)
-{
- /* shut up compiler warnings */
- (void) csr; (void) output; (void) isRequest;
-
-#ifndef NO_WOLFSSL_CLIENT
- if (isRequest) {
- word16 offset = 0;
- word16 length = 0;
-
- /* type */
- output[offset++] = csr->status_type;
-
- switch (csr->status_type) {
- case WOLFSSL_CSR_OCSP:
- /* responder id list */
- c16toa(0, output + offset);
- offset += OPAQUE16_LEN;
-
- /* request extensions */
- if (csr->request.ocsp.nonceSz)
- length = (word16)EncodeOcspRequestExtensions(
- &csr->request.ocsp,
- output + offset + OPAQUE16_LEN,
- OCSP_NONCE_EXT_SZ);
-
- c16toa(length, output + offset);
- offset += OPAQUE16_LEN + length;
-
- break;
- }
-
- return offset;
- }
-#endif
-#if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
- if (!isRequest && csr->ssl->options.tls1_3) {
- word16 offset = 0;
- output[offset++] = csr->status_type;
- c32to24(csr->response.length, output + offset);
- offset += OPAQUE24_LEN;
- XMEMCPY(output + offset, csr->response.buffer, csr->response.length);
- offset += csr->response.length;
- return offset;
- }
-#endif
-
- return 0;
-}
-
-static int TLSX_CSR_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte isRequest)
-{
- int ret;
-
- /* shut up compiler warnings */
- (void) ssl; (void) input;
-
- if (!isRequest) {
-#ifndef NO_WOLFSSL_CLIENT
- TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
- CertificateStatusRequest* csr = extension ?
- (CertificateStatusRequest*)extension->data : NULL;
-
- if (!csr) {
- /* look at context level */
- extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST);
- csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
-
- if (!csr) /* unexpected extension */
- return TLSX_HandleUnsupportedExtension(ssl);
-
- /* enable extension at ssl level */
- ret = TLSX_UseCertificateStatusRequest(&ssl->extensions,
- csr->status_type, csr->options, ssl,
- ssl->heap, ssl->devId);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
-
- switch (csr->status_type) {
- case WOLFSSL_CSR_OCSP:
- /* propagate nonce */
- if (csr->request.ocsp.nonceSz) {
- OcspRequest* request =
- (OcspRequest*)TLSX_CSR_GetRequest(ssl->extensions);
-
- if (request) {
- XMEMCPY(request->nonce, csr->request.ocsp.nonce,
- csr->request.ocsp.nonceSz);
- request->nonceSz = csr->request.ocsp.nonceSz;
- }
- }
- break;
- }
- }
-
- ssl->status_request = 1;
-
- #ifdef WOLFSSL_TLS13
- if (ssl->options.tls1_3) {
- word32 resp_length;
- word32 offset = 0;
-
- /* Get the new extension potentially created above. */
- extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
- csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
- if (csr == NULL)
- return MEMORY_ERROR;
-
- ret = 0;
- if (OPAQUE8_LEN + OPAQUE24_LEN > length)
- ret = BUFFER_ERROR;
- if (ret == 0 && input[offset++] != WOLFSSL_CSR_OCSP)
- ret = BAD_CERTIFICATE_STATUS_ERROR;
- if (ret == 0) {
- c24to32(input + offset, &resp_length);
- offset += OPAQUE24_LEN;
- if (offset + resp_length != length)
- ret = BUFFER_ERROR;
- }
- #if !defined(NO_WOLFSSL_SERVER)
- if (ret == 0) {
- csr->response.buffer = input + offset;
- csr->response.length = resp_length;
- }
- #endif
-
- return ret;
- }
- else
- #endif
- {
- /* extension_data MUST be empty. */
- return length ? BUFFER_ERROR : 0;
- }
-#endif
- }
- else {
-#ifndef NO_WOLFSSL_SERVER
- byte status_type;
- word16 offset = 0;
- word16 size = 0;
-
- if (length == 0)
- return 0;
- if (length < ENUM_LEN)
- return BUFFER_ERROR;
-
- status_type = input[offset++];
-
- switch (status_type) {
- case WOLFSSL_CSR_OCSP: {
-
- /* skip responder_id_list */
- if (length - offset < OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- ato16(input + offset, &size);
- offset += OPAQUE16_LEN + size;
-
- /* skip request_extensions */
- if (length - offset < OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- ato16(input + offset, &size);
- offset += OPAQUE16_LEN + size;
-
- if (offset > length)
- return BUFFER_ERROR;
-
- /* is able to send OCSP response? */
- if (ssl->ctx->cm == NULL || !ssl->ctx->cm->ocspStaplingEnabled)
- return 0;
- }
- break;
-
- /* unknown status type */
- default:
- return 0;
- }
-
- /* if using status_request and already sending it, skip this one */
- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
- if (ssl->status_request_v2)
- return 0;
- #endif
-
- /* accept the first good status_type and return */
- ret = TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
- 0, ssl, ssl->heap, ssl->devId);
- if (ret != WOLFSSL_SUCCESS)
- return ret; /* throw error */
-
- #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
- if (ssl->options.tls1_3) {
- OcspRequest* request;
- TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
- CertificateStatusRequest* csr = extension ?
- (CertificateStatusRequest*)extension->data : NULL;
- if (csr == NULL)
- return MEMORY_ERROR;
-
- request = &csr->request.ocsp;
- ret = CreateOcspResponse(ssl, &request, &csr->response);
- if (ret != 0)
- return ret;
- if (csr->response.buffer)
- TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
- }
- else
- #endif
- TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
- ssl->status_request = status_type;
-#endif
- }
-
- return 0;
-}
-
-int TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert, void* heap)
-{
- TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
- CertificateStatusRequest* csr = extension ?
- (CertificateStatusRequest*)extension->data : NULL;
- int ret = 0;
-
- if (csr) {
- switch (csr->status_type) {
- case WOLFSSL_CSR_OCSP: {
- byte nonce[MAX_OCSP_NONCE_SZ];
- int nonceSz = csr->request.ocsp.nonceSz;
-
- /* preserve nonce */
- XMEMCPY(nonce, csr->request.ocsp.nonce, nonceSz);
-
- if ((ret = InitOcspRequest(&csr->request.ocsp, cert, 0, heap))
- != 0)
- return ret;
-
- /* restore nonce */
- XMEMCPY(csr->request.ocsp.nonce, nonce, nonceSz);
- csr->request.ocsp.nonceSz = nonceSz;
- }
- break;
- }
- }
-
- return ret;
-}
-
-void* TLSX_CSR_GetRequest(TLSX* extensions)
-{
- TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
- CertificateStatusRequest* csr = extension ?
- (CertificateStatusRequest*)extension->data : NULL;
-
- if (csr) {
- switch (csr->status_type) {
- case WOLFSSL_CSR_OCSP:
- return &csr->request.ocsp;
- break;
- }
- }
-
- return NULL;
-}
-
-int TLSX_CSR_ForceRequest(WOLFSSL* ssl)
-{
- TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
- CertificateStatusRequest* csr = extension ?
- (CertificateStatusRequest*)extension->data : NULL;
-
- if (csr) {
- switch (csr->status_type) {
- case WOLFSSL_CSR_OCSP:
- if (ssl->ctx->cm->ocspEnabled) {
- csr->request.ocsp.ssl = ssl;
- return CheckOcspRequest(ssl->ctx->cm->ocsp,
- &csr->request.ocsp, NULL);
- }
- else
- return OCSP_LOOKUP_FAIL;
- }
- }
-
- return 0;
-}
-
-int TLSX_UseCertificateStatusRequest(TLSX** extensions, byte status_type,
- byte options, WOLFSSL* ssl, void* heap,
- int devId)
-{
- CertificateStatusRequest* csr = NULL;
- int ret = 0;
-
- if (!extensions || status_type != WOLFSSL_CSR_OCSP)
- return BAD_FUNC_ARG;
-
- csr = (CertificateStatusRequest*)
- XMALLOC(sizeof(CertificateStatusRequest), heap, DYNAMIC_TYPE_TLSX);
- if (!csr)
- return MEMORY_E;
-
- ForceZero(csr, sizeof(CertificateStatusRequest));
-
- csr->status_type = status_type;
- csr->options = options;
- csr->ssl = ssl;
-
- switch (csr->status_type) {
- case WOLFSSL_CSR_OCSP:
- if (options & WOLFSSL_CSR_OCSP_USE_NONCE) {
- WC_RNG rng;
-
- #ifndef HAVE_FIPS
- ret = wc_InitRng_ex(&rng, heap, devId);
- #else
- ret = wc_InitRng(&rng);
- (void)devId;
- #endif
- if (ret == 0) {
- if (wc_RNG_GenerateBlock(&rng, csr->request.ocsp.nonce,
- MAX_OCSP_NONCE_SZ) == 0)
- csr->request.ocsp.nonceSz = MAX_OCSP_NONCE_SZ;
-
- wc_FreeRng(&rng);
- }
- }
- break;
- }
-
- if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST, csr, heap)) != 0) {
- XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
- return ret;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-#define CSR_FREE_ALL TLSX_CSR_Free
-#define CSR_GET_SIZE TLSX_CSR_GetSize
-#define CSR_WRITE TLSX_CSR_Write
-#define CSR_PARSE TLSX_CSR_Parse
-
-#else
-
-#define CSR_FREE_ALL(data, heap)
-#define CSR_GET_SIZE(a, b) 0
-#define CSR_WRITE(a, b, c) 0
-#define CSR_PARSE(a, b, c, d) 0
-
-#endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
-
-/******************************************************************************/
-/* Certificate Status Request v2 */
-/******************************************************************************/
-
-#ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
-
-static void TLSX_CSR2_FreeAll(CertificateStatusRequestItemV2* csr2, void* heap)
-{
- CertificateStatusRequestItemV2* next;
-
- for (; csr2; csr2 = next) {
- next = csr2->next;
-
- switch (csr2->status_type) {
- case WOLFSSL_CSR2_OCSP:
- case WOLFSSL_CSR2_OCSP_MULTI:
- while(csr2->requests--)
- FreeOcspRequest(&csr2->request.ocsp[csr2->requests]);
- break;
- }
-
- XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
- }
- (void)heap;
-}
-
-static word16 TLSX_CSR2_GetSize(CertificateStatusRequestItemV2* csr2,
- byte isRequest)
-{
- word16 size = 0;
-
- /* shut up compiler warnings */
- (void) csr2; (void) isRequest;
-
-#ifndef NO_WOLFSSL_CLIENT
- if (isRequest) {
- CertificateStatusRequestItemV2* next;
-
- for (size = OPAQUE16_LEN; csr2; csr2 = next) {
- next = csr2->next;
-
- switch (csr2->status_type) {
- case WOLFSSL_CSR2_OCSP:
- case WOLFSSL_CSR2_OCSP_MULTI:
- size += ENUM_LEN + 3 * OPAQUE16_LEN;
-
- if (csr2->request.ocsp[0].nonceSz)
- size += OCSP_NONCE_EXT_SZ;
- break;
- }
- }
- }
-#endif
-
- return size;
-}
-
-static word16 TLSX_CSR2_Write(CertificateStatusRequestItemV2* csr2,
- byte* output, byte isRequest)
-{
- /* shut up compiler warnings */
- (void) csr2; (void) output; (void) isRequest;
-
-#ifndef NO_WOLFSSL_CLIENT
- if (isRequest) {
- word16 offset;
- word16 length;
-
- for (offset = OPAQUE16_LEN; csr2 != NULL; csr2 = csr2->next) {
- /* status_type */
- output[offset++] = csr2->status_type;
-
- /* request */
- switch (csr2->status_type) {
- case WOLFSSL_CSR2_OCSP:
- case WOLFSSL_CSR2_OCSP_MULTI:
- /* request_length */
- length = 2 * OPAQUE16_LEN;
-
- if (csr2->request.ocsp[0].nonceSz)
- length += OCSP_NONCE_EXT_SZ;
-
- c16toa(length, output + offset);
- offset += OPAQUE16_LEN;
-
- /* responder id list */
- c16toa(0, output + offset);
- offset += OPAQUE16_LEN;
-
- /* request extensions */
- length = 0;
-
- if (csr2->request.ocsp[0].nonceSz)
- length = (word16)EncodeOcspRequestExtensions(
- &csr2->request.ocsp[0],
- output + offset + OPAQUE16_LEN,
- OCSP_NONCE_EXT_SZ);
-
- c16toa(length, output + offset);
- offset += OPAQUE16_LEN + length;
- break;
- }
- }
-
- /* list size */
- c16toa(offset - OPAQUE16_LEN, output);
-
- return offset;
- }
-#endif
-
- return 0;
-}
-
-static int TLSX_CSR2_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte isRequest)
-{
- int ret;
-
- /* shut up compiler warnings */
- (void) ssl; (void) input;
-
- if (!isRequest) {
-#ifndef NO_WOLFSSL_CLIENT
- TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
- CertificateStatusRequestItemV2* csr2 = extension ?
- (CertificateStatusRequestItemV2*)extension->data : NULL;
-
- if (!csr2) {
- /* look at context level */
- extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST_V2);
- csr2 = extension ?
- (CertificateStatusRequestItemV2*)extension->data : NULL;
-
- if (!csr2) /* unexpected extension */
- return TLSX_HandleUnsupportedExtension(ssl);
-
- /* enable extension at ssl level */
- for (; csr2; csr2 = csr2->next) {
- ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
- csr2->status_type, csr2->options, ssl->heap,
- ssl->devId);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
-
- switch (csr2->status_type) {
- case WOLFSSL_CSR2_OCSP:
- /* followed by */
- case WOLFSSL_CSR2_OCSP_MULTI:
- /* propagate nonce */
- if (csr2->request.ocsp[0].nonceSz) {
- OcspRequest* request =
- (OcspRequest*)TLSX_CSR2_GetRequest(ssl->extensions,
- csr2->status_type, 0);
-
- if (request) {
- XMEMCPY(request->nonce,
- csr2->request.ocsp[0].nonce,
- csr2->request.ocsp[0].nonceSz);
-
- request->nonceSz =
- csr2->request.ocsp[0].nonceSz;
- }
- }
- break;
- }
- }
- }
-
- ssl->status_request_v2 = 1;
-
- return length ? BUFFER_ERROR : 0; /* extension_data MUST be empty. */
-#endif
- }
- else {
-#ifndef NO_WOLFSSL_SERVER
- byte status_type;
- word16 request_length;
- word16 offset = 0;
- word16 size = 0;
-
- /* list size */
- if (offset + OPAQUE16_LEN >= length) {
- return BUFFER_E;
- }
-
- ato16(input + offset, &request_length);
- offset += OPAQUE16_LEN;
-
- if (length - OPAQUE16_LEN != request_length)
- return BUFFER_ERROR;
-
- while (length > offset) {
- if (length - offset < ENUM_LEN + OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- status_type = input[offset++];
-
- ato16(input + offset, &request_length);
- offset += OPAQUE16_LEN;
-
- if (length - offset < request_length)
- return BUFFER_ERROR;
-
- switch (status_type) {
- case WOLFSSL_CSR2_OCSP:
- case WOLFSSL_CSR2_OCSP_MULTI:
- /* skip responder_id_list */
- if (length - offset < OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- ato16(input + offset, &size);
- if (length - offset < size)
- return BUFFER_ERROR;
-
- offset += OPAQUE16_LEN + size;
- /* skip request_extensions */
- if (length - offset < OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- ato16(input + offset, &size);
- if (length - offset < size)
- return BUFFER_ERROR;
-
- offset += OPAQUE16_LEN + size;
- if (offset > length)
- return BUFFER_ERROR;
-
- /* is able to send OCSP response? */
- if (ssl->ctx->cm == NULL
- || !ssl->ctx->cm->ocspStaplingEnabled)
- continue;
- break;
-
- default:
- /* unknown status type, skipping! */
- offset += request_length;
- continue;
- }
-
- /* if using status_request and already sending it, skip this one */
- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
- if (ssl->status_request)
- return 0;
- #endif
-
- /* accept the first good status_type and return */
- ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
- status_type, 0, ssl->heap, ssl->devId);
- if (ret != WOLFSSL_SUCCESS)
- return ret; /* throw error */
-
- TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST_V2);
- ssl->status_request_v2 = status_type;
-
- return 0;
- }
-#endif
- }
-
- return 0;
-}
-
-int TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert, byte isPeer,
- void* heap)
-{
- TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
- CertificateStatusRequestItemV2* csr2 = extension ?
- (CertificateStatusRequestItemV2*)extension->data : NULL;
- int ret = 0;
-
- for (; csr2; csr2 = csr2->next) {
- switch (csr2->status_type) {
- case WOLFSSL_CSR2_OCSP:
- if (!isPeer || csr2->requests != 0)
- break;
-
- FALL_THROUGH; /* followed by */
-
- case WOLFSSL_CSR2_OCSP_MULTI: {
- if (csr2->requests < 1 + MAX_CHAIN_DEPTH) {
- byte nonce[MAX_OCSP_NONCE_SZ];
- int nonceSz = csr2->request.ocsp[0].nonceSz;
-
- /* preserve nonce, replicating nonce of ocsp[0] */
- XMEMCPY(nonce, csr2->request.ocsp[0].nonce, nonceSz);
-
- if ((ret = InitOcspRequest(
- &csr2->request.ocsp[csr2->requests], cert,
- 0, heap)) != 0)
- return ret;
-
- /* restore nonce */
- XMEMCPY(csr2->request.ocsp[csr2->requests].nonce,
- nonce, nonceSz);
- csr2->request.ocsp[csr2->requests].nonceSz = nonceSz;
- csr2->requests++;
- }
- }
- break;
- }
- }
-
- (void)cert;
- return ret;
-}
-
-void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type, byte idx)
-{
- TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
- CertificateStatusRequestItemV2* csr2 = extension ?
- (CertificateStatusRequestItemV2*)extension->data : NULL;
-
- for (; csr2; csr2 = csr2->next) {
- if (csr2->status_type == status_type) {
- switch (csr2->status_type) {
- case WOLFSSL_CSR2_OCSP:
- /* followed by */
-
- case WOLFSSL_CSR2_OCSP_MULTI:
- /* requests are initialized in the reverse order */
- return idx < csr2->requests
- ? &csr2->request.ocsp[csr2->requests - idx - 1]
- : NULL;
- break;
- }
- }
- }
-
- return NULL;
-}
-
-int TLSX_CSR2_ForceRequest(WOLFSSL* ssl)
-{
- TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
- CertificateStatusRequestItemV2* csr2 = extension ?
- (CertificateStatusRequestItemV2*)extension->data : NULL;
-
- /* forces only the first one */
- if (csr2) {
- switch (csr2->status_type) {
- case WOLFSSL_CSR2_OCSP:
- /* followed by */
-
- case WOLFSSL_CSR2_OCSP_MULTI:
- if (ssl->ctx->cm->ocspEnabled) {
- csr2->request.ocsp[0].ssl = ssl;
- return CheckOcspRequest(ssl->ctx->cm->ocsp,
- &csr2->request.ocsp[0], NULL);
- }
- else
- return OCSP_LOOKUP_FAIL;
- }
- }
-
- return 0;
-}
-
-int TLSX_UseCertificateStatusRequestV2(TLSX** extensions, byte status_type,
- byte options, void* heap, int devId)
-{
- TLSX* extension = NULL;
- CertificateStatusRequestItemV2* csr2 = NULL;
- int ret = 0;
-
- if (!extensions)
- return BAD_FUNC_ARG;
-
- if (status_type != WOLFSSL_CSR2_OCSP
- && status_type != WOLFSSL_CSR2_OCSP_MULTI)
- return BAD_FUNC_ARG;
-
- csr2 = (CertificateStatusRequestItemV2*)
- XMALLOC(sizeof(CertificateStatusRequestItemV2), heap, DYNAMIC_TYPE_TLSX);
- if (!csr2)
- return MEMORY_E;
-
- ForceZero(csr2, sizeof(CertificateStatusRequestItemV2));
-
- csr2->status_type = status_type;
- csr2->options = options;
- csr2->next = NULL;
-
- switch (csr2->status_type) {
- case WOLFSSL_CSR2_OCSP:
- case WOLFSSL_CSR2_OCSP_MULTI:
- if (options & WOLFSSL_CSR2_OCSP_USE_NONCE) {
- WC_RNG rng;
-
- #ifndef HAVE_FIPS
- ret = wc_InitRng_ex(&rng, heap, devId);
- #else
- ret = wc_InitRng(&rng);
- (void)devId;
- #endif
- if (ret == 0) {
- if (wc_RNG_GenerateBlock(&rng, csr2->request.ocsp[0].nonce,
- MAX_OCSP_NONCE_SZ) == 0)
- csr2->request.ocsp[0].nonceSz = MAX_OCSP_NONCE_SZ;
-
- wc_FreeRng(&rng);
- }
- }
- break;
- }
-
- /* append new item */
- if ((extension = TLSX_Find(*extensions, TLSX_STATUS_REQUEST_V2))) {
- CertificateStatusRequestItemV2* last =
- (CertificateStatusRequestItemV2*)extension->data;
-
- for (; last->next; last = last->next);
-
- last->next = csr2;
- }
- else if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST_V2, csr2,heap))) {
- XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
- return ret;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-#define CSR2_FREE_ALL TLSX_CSR2_FreeAll
-#define CSR2_GET_SIZE TLSX_CSR2_GetSize
-#define CSR2_WRITE TLSX_CSR2_Write
-#define CSR2_PARSE TLSX_CSR2_Parse
-
-#else
-
-#define CSR2_FREE_ALL(data, heap)
-#define CSR2_GET_SIZE(a, b) 0
-#define CSR2_WRITE(a, b, c) 0
-#define CSR2_PARSE(a, b, c, d) 0
-
-#endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
-
-/******************************************************************************/
-/* Supported Elliptic Curves */
-/******************************************************************************/
-
-#ifdef HAVE_SUPPORTED_CURVES
-
-#if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && !defined(HAVE_CURVE448) \
- && !defined(HAVE_FFDHE)
-#error Elliptic Curves Extension requires Elliptic Curve Cryptography. \
- Use --enable-ecc in the configure script or define HAVE_ECC. \
- Alternatively use FFDHE for DH ciperhsuites.
-#endif
-
-static int TLSX_SupportedCurve_New(SupportedCurve** curve, word16 name,
- void* heap)
-{
- if (curve == NULL)
- return BAD_FUNC_ARG;
-
- (void)heap;
-
- *curve = (SupportedCurve*)XMALLOC(sizeof(SupportedCurve), heap,
- DYNAMIC_TYPE_TLSX);
- if (*curve == NULL)
- return MEMORY_E;
-
- (*curve)->name = name;
- (*curve)->next = NULL;
-
- return 0;
-}
-
-static int TLSX_PointFormat_New(PointFormat** point, byte format, void* heap)
-{
- if (point == NULL)
- return BAD_FUNC_ARG;
-
- (void)heap;
-
- *point = (PointFormat*)XMALLOC(sizeof(PointFormat), heap,
- DYNAMIC_TYPE_TLSX);
- if (*point == NULL)
- return MEMORY_E;
-
- (*point)->format = format;
- (*point)->next = NULL;
-
- return 0;
-}
-
-static void TLSX_SupportedCurve_FreeAll(SupportedCurve* list, void* heap)
-{
- SupportedCurve* curve;
-
- while ((curve = list)) {
- list = curve->next;
- XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
- }
- (void)heap;
-}
-
-static void TLSX_PointFormat_FreeAll(PointFormat* list, void* heap)
-{
- PointFormat* point;
-
- while ((point = list)) {
- list = point->next;
- XFREE(point, heap, DYNAMIC_TYPE_TLSX);
- }
- (void)heap;
-}
-
-static int TLSX_SupportedCurve_Append(SupportedCurve* list, word16 name,
- void* heap)
-{
- int ret = BAD_FUNC_ARG;
-
- while (list) {
- if (list->name == name) {
- ret = 0; /* curve already in use */
- break;
- }
-
- if (list->next == NULL) {
- ret = TLSX_SupportedCurve_New(&list->next, name, heap);
- break;
- }
-
- list = list->next;
- }
-
- return ret;
-}
-
-static int TLSX_PointFormat_Append(PointFormat* list, byte format, void* heap)
-{
- int ret = BAD_FUNC_ARG;
-
- while (list) {
- if (list->format == format) {
- ret = 0; /* format already in use */
- break;
- }
-
- if (list->next == NULL) {
- ret = TLSX_PointFormat_New(&list->next, format, heap);
- break;
- }
-
- list = list->next;
- }
-
- return ret;
-}
-
-#if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
-
-static void TLSX_SupportedCurve_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
-{
- word16 i;
-
- for (i = 0; i < ssl->suites->suiteSz; i+= 2) {
- if (ssl->suites->suites[i] == TLS13_BYTE)
- return;
- if (ssl->suites->suites[i] == ECC_BYTE ||
- ssl->suites->suites[i] == CHACHA_BYTE) {
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- return;
- #endif
- }
- else {
- #ifdef HAVE_FFDHE
- return;
- #endif
- }
- }
-
- /* turns semaphore on to avoid sending this extension. */
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
-}
-
-static void TLSX_PointFormat_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
-{
- word16 i;
-
- for (i = 0; i < ssl->suites->suiteSz; i+= 2) {
- if (ssl->suites->suites[i] == TLS13_BYTE)
- return;
- if (ssl->suites->suites[i] == ECC_BYTE ||
- ssl->suites->suites[i] == CHACHA_BYTE) {
- #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- return;
- #endif
- }
- else {
- #ifdef HAVE_FFDHE
- return;
- #endif
- }
- }
-
- /* turns semaphore on to avoid sending this extension. */
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
-}
-
-#endif
-
-#ifndef NO_WOLFSSL_SERVER
-
-static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
-{
-#if defined(HAVE_FFDHE) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)
- (void)semaphore;
-#endif
-
- if (ssl->options.cipherSuite0 == TLS13_BYTE)
- return;
- if (ssl->options.cipherSuite0 == ECC_BYTE ||
- ssl->options.cipherSuite0 == CHACHA_BYTE) {
-#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
- return;
-#endif
- }
- else {
-#ifdef HAVE_FFDHE
- return;
-#endif
- }
-
-#if !defined(HAVE_FFDHE) || (!defined(HAVE_ECC) && !defined(HAVE_CURVE25519) \
- && !defined(HAVE_CURVE448))
- /* turns semaphore on to avoid sending this extension. */
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
-#endif
-}
-
-#endif
-#ifndef NO_WOLFSSL_CLIENT
-
-static word16 TLSX_SupportedCurve_GetSize(SupportedCurve* list)
-{
- SupportedCurve* curve;
- word16 length = OPAQUE16_LEN; /* list length */
-
- while ((curve = list)) {
- list = curve->next;
- length += OPAQUE16_LEN; /* curve length */
- }
-
- return length;
-}
-
-#endif
-
-static word16 TLSX_PointFormat_GetSize(PointFormat* list)
-{
- PointFormat* point;
- word16 length = ENUM_LEN; /* list length */
-
- while ((point = list)) {
- list = point->next;
- length += ENUM_LEN; /* format length */
- }
-
- return length;
-}
-
-#ifndef NO_WOLFSSL_CLIENT
-
-static word16 TLSX_SupportedCurve_Write(SupportedCurve* list, byte* output)
-{
- word16 offset = OPAQUE16_LEN;
-
- while (list) {
- c16toa(list->name, output + offset);
- offset += OPAQUE16_LEN;
- list = list->next;
- }
-
- c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
-
- return offset;
-}
-
-#endif
-
-static word16 TLSX_PointFormat_Write(PointFormat* list, byte* output)
-{
- word16 offset = ENUM_LEN;
-
- while (list) {
- output[offset++] = list->format;
- list = list->next;
- }
-
- output[0] = (byte)(offset - ENUM_LEN);
-
- return offset;
-}
-
-#if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
- !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
-
-static int TLSX_SupportedCurve_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte isRequest)
-{
- word16 offset;
- word16 name;
- int ret;
-
- if(!isRequest && !IsAtLeastTLSv1_3(ssl->version)) {
-#ifdef WOLFSSL_ALLOW_SERVER_SC_EXT
- return 0;
-#else
- return BUFFER_ERROR; /* servers doesn't send this extension. */
-#endif
- }
-
- if (OPAQUE16_LEN > length || length % OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- ato16(input, &offset);
-
- /* validating curve list length */
- if (length != OPAQUE16_LEN + offset)
- return BUFFER_ERROR;
-
- offset = OPAQUE16_LEN;
- if (offset == length)
- return 0;
-
-#if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
- if (!isRequest) {
- TLSX* extension;
- SupportedCurve* curve;
-
- extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
- if (extension != NULL) {
- /* Replace client list with server list of supported groups. */
- curve = (SupportedCurve*)extension->data;
- extension->data = NULL;
- TLSX_SupportedCurve_FreeAll(curve, ssl->heap);
-
- ato16(input + offset, &name);
- offset += OPAQUE16_LEN;
-
- ret = TLSX_SupportedCurve_New(&curve, name, ssl->heap);
- if (ret != 0)
- return ret; /* throw error */
- extension->data = (void*)curve;
- }
- }
-#endif
-
- for (; offset < length; offset += OPAQUE16_LEN) {
- ato16(input + offset, &name);
-
- ret = TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret; /* throw error */
- }
-
- return 0;
-}
-
-#endif
-
-#if !defined(NO_WOLFSSL_SERVER)
-
-#if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
-
-/* Checks the priority of the groups on the server and set the supported groups
- * response if there is a group not advertised by the client that is preferred.
- *
- * ssl SSL/TLS object.
- * returns 0 on success, otherwise an error.
- */
-int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl)
-{
- int ret;
- TLSX* extension;
- TLSX* priority = NULL;
- TLSX* ext = NULL;
- word16 name;
- SupportedCurve* curve;
-
- extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
- /* May be doing PSK with no key exchange. */
- if (extension == NULL)
- return 0;
-
- if ((ret = TLSX_PopulateSupportedGroups(ssl, &priority)) != WOLFSSL_SUCCESS)
- return ret;
-
- ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
- curve = (SupportedCurve*)ext->data;
- name = curve->name;
-
- curve = (SupportedCurve*)extension->data;
- while (curve != NULL) {
- if (curve->name == name)
- break;
- curve = curve->next;
- }
-
- if (curve == NULL) {
- /* Couldn't find the preferred group in client list. */
- extension->resp = 1;
-
- /* Send server list back and free client list. */
- curve = (SupportedCurve*)extension->data;
- extension->data = ext->data;
- ext->data = curve;
- }
-
- TLSX_FreeAll(priority, ssl->heap);
-
- return 0;
-}
-
-#endif
-
-#if defined(HAVE_FFDHE) && !defined(WOLFSSL_NO_TLS12)
-/* Set the highest priority common FFDHE group on the server as compared to
- * client extensions.
- *
- * ssl SSL/TLS object.
- * returns 0 on success, otherwise an error.
- */
-int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl)
-{
- int ret = 0;
- TLSX* extension;
- TLSX* priority = NULL;
- TLSX* ext = NULL;
- SupportedCurve* serverGroup;
- SupportedCurve* clientGroup;
- SupportedCurve* group;
- const DhParams* params = NULL;
- int found = 0;
-
- extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
- /* May be doing PSK with no key exchange. */
- if (extension == NULL)
- return 0;
- clientGroup = (SupportedCurve*)extension->data;
- for (group = clientGroup; group != NULL; group = group->next) {
- if (group->name >= MIN_FFHDE_GROUP && group->name <= MAX_FFHDE_GROUP) {
- found = 1;
- break;
- }
- }
- if (!found)
- return 0;
-
- if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
- XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- }
- if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
- XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- }
- ssl->buffers.serverDH_P.buffer = NULL;
- ssl->buffers.serverDH_G.buffer = NULL;
- ssl->buffers.weOwnDH = 0;
- ssl->options.haveDH = 0;
-
-
- if ((ret = TLSX_PopulateSupportedGroups(ssl, &priority)) != WOLFSSL_SUCCESS)
- return ret;
- ret = 0;
-
- ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
- serverGroup = (SupportedCurve*)ext->data;
-
- for (; serverGroup != NULL; serverGroup = serverGroup->next) {
- if ((serverGroup->name & NAMED_DH_MASK) != NAMED_DH_MASK)
- continue;
-
- for (group = clientGroup; group != NULL; group = group->next) {
- if (serverGroup->name != group->name)
- continue;
-
- switch (serverGroup->name) {
- #ifdef HAVE_FFDHE_2048
- case WOLFSSL_FFDHE_2048:
- params = wc_Dh_ffdhe2048_Get();
- break;
- #endif
- #ifdef HAVE_FFDHE_3072
- case WOLFSSL_FFDHE_3072:
- params = wc_Dh_ffdhe3072_Get();
- break;
- #endif
- #ifdef HAVE_FFDHE_4096
- case WOLFSSL_FFDHE_4096:
- params = wc_Dh_ffdhe4096_Get();
- break;
- #endif
- #ifdef HAVE_FFDHE_6144
- case WOLFSSL_FFDHE_6144:
- params = wc_Dh_ffdhe6144_Get();
- break;
- #endif
- #ifdef HAVE_FFDHE_8192
- case WOLFSSL_FFDHE_8192:
- params = wc_Dh_ffdhe8192_Get();
- break;
- #endif
- }
- if (params == NULL)
- return BAD_FUNC_ARG;
- if (params->p_len >= ssl->options.minDhKeySz &&
- params->p_len <= ssl->options.maxDhKeySz) {
- break;
- }
- }
-
- if (group != NULL && serverGroup->name == group->name)
- break;
- }
-
- if (serverGroup) {
- ssl->buffers.serverDH_P.buffer = (unsigned char *)params->p;
- ssl->buffers.serverDH_P.length = params->p_len;
- ssl->buffers.serverDH_G.buffer = (unsigned char *)params->g;
- ssl->buffers.serverDH_G.length = params->g_len;
- ssl->namedGroup = serverGroup->name;
- #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
- !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- ssl->options.dhDoKeyTest = 0;
- #endif
- ssl->options.haveDH = 1;
- }
-
- TLSX_FreeAll(priority, ssl->heap);
-
- return ret;
-}
-#endif /* HAVE_FFDHE && !WOLFSSL_NO_TLS12 */
-
-#endif /* !NO_WOLFSSL_SERVER */
-
-#if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
-/* Return the preferred group.
- *
- * ssl SSL/TLS object.
- * checkSupported Whether to check for the first supported group.
- * returns BAD_FUNC_ARG if no group found, otherwise the group.
- */
-int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl, int checkSupported)
-{
- TLSX* extension;
- SupportedCurve* curve;
-
- extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
- if (extension == NULL)
- return BAD_FUNC_ARG;
-
- curve = (SupportedCurve*)extension->data;
- while (curve != NULL) {
- if (!checkSupported || TLSX_KeyShare_IsSupported(curve->name))
- return curve->name;
- curve = curve->next;
- }
-
- return BAD_FUNC_ARG;
-}
-
-#endif
-
-#ifndef NO_WOLFSSL_SERVER
-
-static int TLSX_PointFormat_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte isRequest)
-{
- int ret;
-
- /* validating formats list length */
- if (ENUM_LEN > length || length != (word16)ENUM_LEN + input[0])
- return BUFFER_ERROR;
-
- if (isRequest) {
- /* adding uncompressed point format to response */
- ret = TLSX_UsePointFormat(&ssl->extensions, WOLFSSL_EC_PF_UNCOMPRESSED,
- ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret; /* throw error */
-
- TLSX_SetResponse(ssl, TLSX_EC_POINT_FORMATS);
- }
-
- return 0;
-}
-
-#if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
-int TLSX_ValidateSupportedCurves(WOLFSSL* ssl, byte first, byte second) {
- TLSX* extension = NULL;
- SupportedCurve* curve = NULL;
- word32 oid = 0;
- word32 pkOid = 0;
- word32 defOid = 0;
- word32 defSz = 80; /* Maximum known curve size is 66. */
- word32 nextOid = 0;
- word32 nextSz = 80; /* Maximum known curve size is 66. */
- word32 currOid = ssl->ecdhCurveOID;
- int ephmSuite = 0;
- word16 octets = 0; /* according to 'ecc_set_type ecc_sets[];' */
- int sig = 0; /* validate signature */
- int key = 0; /* validate key */
-
- (void)oid;
-
- if (first == ECC_BYTE || first == CHACHA_BYTE)
- extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
- if (!extension)
- return 1; /* no suite restriction */
-
- for (curve = (SupportedCurve*)extension->data;
- curve && !(sig && key);
- curve = curve->next) {
-
- #ifdef OPENSSL_EXTRA
- /* skip if name is not in supported ECC range */
- if (curve->name > WOLFSSL_ECC_X448)
- continue;
- /* skip if curve is disabled by user */
- if (ssl->ctx->disabledCurves & (1 << curve->name))
- continue;
- #endif
-
- /* find supported curve */
- switch (curve->name) {
-#ifdef HAVE_ECC
- #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP160R1:
- pkOid = oid = ECC_SECP160R1_OID;
- octets = 20;
- break;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_SECPR2
- case WOLFSSL_ECC_SECP160R2:
- pkOid = oid = ECC_SECP160R2_OID;
- octets = 20;
- break;
- #endif /* HAVE_ECC_SECPR2 */
- #ifdef HAVE_ECC_KOBLITZ
- case WOLFSSL_ECC_SECP160K1:
- pkOid = oid = ECC_SECP160K1_OID;
- octets = 20;
- break;
- #endif /* HAVE_ECC_KOBLITZ */
- #endif
- #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP192R1:
- pkOid = oid = ECC_SECP192R1_OID;
- octets = 24;
- break;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_KOBLITZ
- case WOLFSSL_ECC_SECP192K1:
- pkOid = oid = ECC_SECP192K1_OID;
- octets = 24;
- break;
- #endif /* HAVE_ECC_KOBLITZ */
- #endif
- #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP224R1:
- pkOid = oid = ECC_SECP224R1_OID;
- octets = 28;
- break;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_KOBLITZ
- case WOLFSSL_ECC_SECP224K1:
- pkOid = oid = ECC_SECP224K1_OID;
- octets = 28;
- break;
- #endif /* HAVE_ECC_KOBLITZ */
- #endif
- #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP256R1:
- pkOid = oid = ECC_SECP256R1_OID;
- octets = 32;
- break;
- #endif /* !NO_ECC_SECP */
- #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
-#endif
- #ifdef HAVE_CURVE25519
- case WOLFSSL_ECC_X25519:
- oid = ECC_X25519_OID;
- #ifdef HAVE_ED25519
- pkOid = ECC_ED25519_OID;
- #else
- pkOid = ECC_X25519_OID;
- #endif
- octets = 32;
- break;
- #endif /* HAVE_CURVE25519 */
-#ifdef HAVE_ECC
- #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
- #ifdef HAVE_ECC_KOBLITZ
- case WOLFSSL_ECC_SECP256K1:
- pkOid = oid = ECC_SECP256K1_OID;
- octets = 32;
- break;
- #endif /* HAVE_ECC_KOBLITZ */
- #ifdef HAVE_ECC_BRAINPOOL
- case WOLFSSL_ECC_BRAINPOOLP256R1:
- pkOid = oid = ECC_BRAINPOOLP256R1_OID;
- octets = 32;
- break;
- #endif /* HAVE_ECC_BRAINPOOL */
- #endif
-#endif
- #ifdef HAVE_CURVE448
- case WOLFSSL_ECC_X448:
- oid = ECC_X448_OID;
- #ifdef HAVE_ED448
- pkOid = ECC_ED448_OID;
- #else
- pkOid = ECC_X448_OID;
- #endif
- octets = 57;
- break;
- #endif /* HAVE_CURVE448 */
-#ifdef HAVE_ECC
- #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP384R1:
- pkOid = oid = ECC_SECP384R1_OID;
- octets = 48;
- break;
- #endif /* !NO_ECC_SECP */
- #ifdef HAVE_ECC_BRAINPOOL
- case WOLFSSL_ECC_BRAINPOOLP384R1:
- pkOid = oid = ECC_BRAINPOOLP384R1_OID;
- octets = 48;
- break;
- #endif /* HAVE_ECC_BRAINPOOL */
- #endif
- #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
- #ifdef HAVE_ECC_BRAINPOOL
- case WOLFSSL_ECC_BRAINPOOLP512R1:
- pkOid = oid = ECC_BRAINPOOLP512R1_OID;
- octets = 64;
- break;
- #endif /* HAVE_ECC_BRAINPOOL */
- #endif
- #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP521R1:
- pkOid = oid = ECC_SECP521R1_OID;
- octets = 66;
- break;
- #endif /* !NO_ECC_SECP */
- #endif
-#endif
- default: continue; /* unsupported curve */
- }
-
- #ifdef HAVE_ECC
- /* Set default Oid */
- if (defOid == 0 && ssl->eccTempKeySz <= octets && defSz > octets) {
- defOid = oid;
- defSz = octets;
- }
-
- /* The eccTempKeySz is the preferred ephemeral key size */
- if (currOid == 0 && ssl->eccTempKeySz == octets)
- currOid = oid;
- if ((nextOid == 0 || nextSz > octets) && ssl->eccTempKeySz <= octets) {
- nextOid = oid;
- nextSz = octets;
- }
- #else
- if (defOid == 0 && defSz > octets) {
- defOid = oid;
- defSz = octets;
- }
-
- if (currOid == 0)
- currOid = oid;
- if (nextOid == 0 || nextSz > octets) {
- nextOid = oid;
- nextSz = octets;
- }
- #endif
-
- if (first == ECC_BYTE) {
- switch (second) {
- /* ECDHE_ECDSA */
- case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
- case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
- case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
- case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
- case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
- case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
- case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
- case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
- case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
- case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
- sig |= ssl->pkCurveOID == pkOid;
- key |= ssl->ecdhCurveOID == oid;
- ephmSuite = 1;
- break;
-
-#ifdef WOLFSSL_STATIC_DH
- /* ECDH_ECDSA */
- case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
- case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
- case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
- case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
- case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
- case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
- case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
- case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
- if (oid == ECC_X25519_OID && defOid == oid) {
- defOid = 0;
- defSz = 80;
- }
- if (oid == ECC_X448_OID && defOid == oid) {
- defOid = 0;
- defSz = 80;
- }
- sig |= ssl->pkCurveOID == pkOid;
- key |= ssl->pkCurveOID == oid;
- break;
-#endif /* WOLFSSL_STATIC_DH */
-#ifndef NO_RSA
- /* ECDHE_RSA */
- case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
- case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
- case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
- case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
- case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
- case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
- case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
- case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
- sig = 1;
- key |= ssl->ecdhCurveOID == oid;
- ephmSuite = 1;
- break;
-
-#ifdef WOLFSSL_STATIC_DH
- /* ECDH_RSA */
- case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
- case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
- case TLS_ECDH_RSA_WITH_RC4_128_SHA:
- case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
- case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
- case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
- case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
- case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
- if (oid == ECC_X25519_OID && defOid == oid) {
- defOid = 0;
- defSz = 80;
- }
- if (oid == ECC_X448_OID && defOid == oid) {
- defOid = 0;
- defSz = 80;
- }
- sig = 1;
- key |= ssl->pkCurveOID == pkOid;
- break;
-#endif /* WOLFSSL_STATIC_DH */
-#endif
- default:
- if (oid == ECC_X25519_OID && defOid == oid) {
- defOid = 0;
- defSz = 80;
- }
- if (oid == ECC_X448_OID && defOid == oid) {
- defOid = 0;
- defSz = 80;
- }
- if (oid != ECC_X25519_OID && oid != ECC_X448_OID) {
- sig = 1;
- }
- key = 1;
- break;
- }
- }
-
- /* ChaCha20-Poly1305 ECC cipher suites */
- if (first == CHACHA_BYTE) {
- switch (second) {
- /* ECDHE_ECDSA */
- case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
- case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
- sig |= ssl->pkCurveOID == pkOid;
- key |= ssl->ecdhCurveOID == oid;
- ephmSuite = 1;
- break;
-#ifndef NO_RSA
- /* ECDHE_RSA */
- case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
- case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
- sig = 1;
- key |= ssl->ecdhCurveOID == oid;
- ephmSuite = 1;
- break;
-#endif
- default:
- sig = 1;
- key = 1;
- break;
- }
- }
- }
-
- /* Choose the default if it is at the required strength. */
-#ifdef HAVE_ECC
- if (ssl->ecdhCurveOID == 0 && defSz == ssl->eccTempKeySz)
-#else
- if (ssl->ecdhCurveOID == 0)
-#endif
- {
- key = 1;
- ssl->ecdhCurveOID = defOid;
- }
- /* Choose any curve at the required strength. */
- if (ssl->ecdhCurveOID == 0) {
- key = 1;
- ssl->ecdhCurveOID = currOid;
- }
- /* Choose the default if it is at the next highest strength. */
- if (ssl->ecdhCurveOID == 0 && defSz == nextSz)
- ssl->ecdhCurveOID = defOid;
- /* Choose any curve at the next highest strength. */
- if (ssl->ecdhCurveOID == 0)
- ssl->ecdhCurveOID = nextOid;
- /* No curve and ephemeral ECC suite requires a matching curve. */
- if (ssl->ecdhCurveOID == 0 && ephmSuite)
- key = 0;
-
- return sig && key;
-}
-#endif
-
-#endif /* NO_WOLFSSL_SERVER */
-
-int TLSX_UseSupportedCurve(TLSX** extensions, word16 name, void* heap)
-{
- TLSX* extension = NULL;
- SupportedCurve* curve = NULL;
- int ret;
-
- if (extensions == NULL)
- return BAD_FUNC_ARG;
-
- extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
-
- if (!extension) {
- ret = TLSX_SupportedCurve_New(&curve, name, heap);
- if (ret != 0)
- return ret;
-
- ret = TLSX_Push(extensions, TLSX_SUPPORTED_GROUPS, curve, heap);
- if (ret != 0) {
- XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
- return ret;
- }
- }
- else {
- ret = TLSX_SupportedCurve_Append((SupportedCurve*)extension->data, name,
- heap);
- if (ret != 0)
- return ret;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-int TLSX_UsePointFormat(TLSX** extensions, byte format, void* heap)
-{
- TLSX* extension = NULL;
- PointFormat* point = NULL;
- int ret = 0;
-
- if (extensions == NULL)
- return BAD_FUNC_ARG;
-
- extension = TLSX_Find(*extensions, TLSX_EC_POINT_FORMATS);
-
- if (!extension) {
- ret = TLSX_PointFormat_New(&point, format, heap);
- if (ret != 0)
- return ret;
-
- ret = TLSX_Push(extensions, TLSX_EC_POINT_FORMATS, point, heap);
- if (ret != 0) {
- XFREE(point, heap, DYNAMIC_TYPE_TLSX);
- return ret;
- }
- }
- else {
- ret = TLSX_PointFormat_Append((PointFormat*)extension->data, format,
- heap);
- if (ret != 0)
- return ret;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-#define EC_FREE_ALL TLSX_SupportedCurve_FreeAll
-#define EC_VALIDATE_REQUEST TLSX_SupportedCurve_ValidateRequest
-
-#ifndef NO_WOLFSSL_CLIENT
-#define EC_GET_SIZE TLSX_SupportedCurve_GetSize
-#define EC_WRITE TLSX_SupportedCurve_Write
-#else
-#define EC_GET_SIZE(list) 0
-#define EC_WRITE(a, b) 0
-#endif
-
-#if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
- !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
-#define EC_PARSE TLSX_SupportedCurve_Parse
-#else
-#define EC_PARSE(a, b, c, d) 0
-#endif
-
-#define PF_FREE_ALL TLSX_PointFormat_FreeAll
-#define PF_VALIDATE_REQUEST TLSX_PointFormat_ValidateRequest
-#define PF_VALIDATE_RESPONSE TLSX_PointFormat_ValidateResponse
-
-#define PF_GET_SIZE TLSX_PointFormat_GetSize
-#define PF_WRITE TLSX_PointFormat_Write
-
-#ifndef NO_WOLFSSL_SERVER
-#define PF_PARSE TLSX_PointFormat_Parse
-#else
-#define PF_PARSE(a, b, c, d) 0
-#endif
-
-#else
-
-#define EC_FREE_ALL(list, heap)
-#define EC_GET_SIZE(list) 0
-#define EC_WRITE(a, b) 0
-#define EC_PARSE(a, b, c, d) 0
-#define EC_VALIDATE_REQUEST(a, b)
-
-#define PF_FREE_ALL(list, heap)
-#define PF_GET_SIZE(list) 0
-#define PF_WRITE(a, b) 0
-#define PF_PARSE(a, b, c, d) 0
-#define PF_VALIDATE_REQUEST(a, b)
-#define PF_VALIDATE_RESPONSE(a, b)
-
-#endif /* HAVE_SUPPORTED_CURVES */
-
-/******************************************************************************/
-/* Renegotiation Indication */
-/******************************************************************************/
-
-#if defined(HAVE_SECURE_RENEGOTIATION) \
- || defined(HAVE_SERVER_RENEGOTIATION_INFO)
-
-static byte TLSX_SecureRenegotiation_GetSize(SecureRenegotiation* data,
- int isRequest)
-{
- byte length = OPAQUE8_LEN; /* empty info length */
-
- /* data will be NULL for HAVE_SERVER_RENEGOTIATION_INFO only */
- if (data && data->enabled && data->verifySet) {
- /* client sends client_verify_data only */
- length += TLS_FINISHED_SZ;
-
- /* server also sends server_verify_data */
- if (!isRequest)
- length += TLS_FINISHED_SZ;
- }
-
- return length;
-}
-
-static word16 TLSX_SecureRenegotiation_Write(SecureRenegotiation* data,
- byte* output, int isRequest)
-{
- word16 offset = OPAQUE8_LEN; /* RenegotiationInfo length */
- if (data && data->enabled && data->verifySet) {
- /* client sends client_verify_data only */
- XMEMCPY(output + offset, data->client_verify_data, TLS_FINISHED_SZ);
- offset += TLS_FINISHED_SZ;
-
- /* server also sends server_verify_data */
- if (!isRequest) {
- XMEMCPY(output + offset, data->server_verify_data, TLS_FINISHED_SZ);
- offset += TLS_FINISHED_SZ;
- }
- }
-
- output[0] = (byte)(offset - 1); /* info length - self */
-
- return offset;
-}
-
-static int TLSX_SecureRenegotiation_Parse(WOLFSSL* ssl, byte* input,
- word16 length, byte isRequest)
-{
- int ret = SECURE_RENEGOTIATION_E;
-
- if (length >= OPAQUE8_LEN) {
- if (isRequest) {
- #ifndef NO_WOLFSSL_SERVER
- if (ssl->secure_renegotiation == NULL) {
- ret = wolfSSL_UseSecureRenegotiation(ssl);
- if (ret == WOLFSSL_SUCCESS)
- ret = 0;
- }
- if (ret != 0 && ret != SECURE_RENEGOTIATION_E) {
- }
- else if (!ssl->secure_renegotiation->enabled) {
- if (*input == 0) {
- input++; /* get past size */
-
- ssl->secure_renegotiation->enabled = 1;
- TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
- ret = 0;
- }
- else {
- /* already in error state */
- WOLFSSL_MSG("SCR client verify data present");
- }
- }
- else if (*input == TLS_FINISHED_SZ) {
- if (length < TLS_FINISHED_SZ + 1) {
- WOLFSSL_MSG("SCR malformed buffer");
- ret = BUFFER_E;
- }
- else {
- input++; /* get past size */
-
- /* validate client verify data */
- if (XMEMCMP(input,
- ssl->secure_renegotiation->client_verify_data,
- TLS_FINISHED_SZ) == 0) {
- WOLFSSL_MSG("SCR client verify data match");
- TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
- ret = 0; /* verified */
- } else {
- /* already in error state */
- WOLFSSL_MSG("SCR client verify data Failure");
- }
- }
- }
- #endif
- }
- else {
- #ifndef NO_WOLFSSL_CLIENT
- if (!ssl->secure_renegotiation->enabled) {
- if (*input == 0) {
- ssl->secure_renegotiation->enabled = 1;
- ret = 0;
- }
- }
- else if (*input == 2 * TLS_FINISHED_SZ &&
- length == 2 * TLS_FINISHED_SZ + OPAQUE8_LEN) {
- input++; /* get past size */
-
- /* validate client and server verify data */
- if (XMEMCMP(input,
- ssl->secure_renegotiation->client_verify_data,
- TLS_FINISHED_SZ) == 0 &&
- XMEMCMP(input + TLS_FINISHED_SZ,
- ssl->secure_renegotiation->server_verify_data,
- TLS_FINISHED_SZ) == 0) {
- WOLFSSL_MSG("SCR client and server verify data match");
- ret = 0; /* verified */
- } else {
- /* already in error state */
- WOLFSSL_MSG("SCR client and server verify data Failure");
- }
- }
- #endif
- }
- }
-
- if (ret != 0) {
- SendAlert(ssl, alert_fatal, handshake_failure);
- }
-
- return ret;
-}
-
-int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap)
-{
- int ret = 0;
- SecureRenegotiation* data;
-
- data = (SecureRenegotiation*)XMALLOC(sizeof(SecureRenegotiation), heap,
- DYNAMIC_TYPE_TLSX);
- if (data == NULL)
- return MEMORY_E;
-
- XMEMSET(data, 0, sizeof(SecureRenegotiation));
-
- ret = TLSX_Push(extensions, TLSX_RENEGOTIATION_INFO, data, heap);
- if (ret != 0) {
- XFREE(data, heap, DYNAMIC_TYPE_TLSX);
- return ret;
- }
-
- return WOLFSSL_SUCCESS;
-}
-
-#ifdef HAVE_SERVER_RENEGOTIATION_INFO
-
-int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap)
-{
- int ret;
-
- /* send empty renegotiation_info extension */
- TLSX* ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
- if (ext == NULL) {
- ret = TLSX_UseSecureRenegotiation(extensions, heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
-
- ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
- }
- if (ext)
- ext->resp = 1;
-
- return WOLFSSL_SUCCESS;
-}
-
-#endif /* HAVE_SERVER_RENEGOTIATION_INFO */
-
-
-#define SCR_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
-#define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize
-#define SCR_WRITE TLSX_SecureRenegotiation_Write
-#define SCR_PARSE TLSX_SecureRenegotiation_Parse
-
-#else
-
-#define SCR_FREE_ALL(a, heap)
-#define SCR_GET_SIZE(a, b) 0
-#define SCR_WRITE(a, b, c) 0
-#define SCR_PARSE(a, b, c, d) 0
-
-#endif /* HAVE_SECURE_RENEGOTIATION */
-
-/******************************************************************************/
-/* Session Tickets */
-/******************************************************************************/
-
-#ifdef HAVE_SESSION_TICKET
-
-#if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
-static void TLSX_SessionTicket_ValidateRequest(WOLFSSL* ssl)
-{
- TLSX* extension = TLSX_Find(ssl->extensions, TLSX_SESSION_TICKET);
- SessionTicket* ticket = extension ?
- (SessionTicket*)extension->data : NULL;
-
- if (ticket) {
- /* TODO validate ticket timeout here! */
- if (ticket->lifetime == 0xfffffff) {
- /* send empty ticket on timeout */
- TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
- }
- }
-}
-#endif /* WLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
-
-
-static word16 TLSX_SessionTicket_GetSize(SessionTicket* ticket, int isRequest)
-{
- (void)isRequest;
- return ticket ? ticket->size : 0;
-}
-
-static word16 TLSX_SessionTicket_Write(SessionTicket* ticket, byte* output,
- int isRequest)
-{
- word16 offset = 0; /* empty ticket */
-
- if (isRequest && ticket) {
- XMEMCPY(output + offset, ticket->data, ticket->size);
- offset += ticket->size;
- }
-
- return offset;
-}
-
-
-static int TLSX_SessionTicket_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte isRequest)
-{
- int ret = 0;
-
- (void) input; /* avoid unused parameter if NO_WOLFSSL_SERVER defined */
-
- if (!isRequest) {
- if (TLSX_CheckUnsupportedExtension(ssl, TLSX_SESSION_TICKET))
- return TLSX_HandleUnsupportedExtension(ssl);
-
- if (length != 0)
- return BUFFER_ERROR;
-
-#ifndef NO_WOLFSSL_CLIENT
- ssl->expect_session_ticket = 1;
-#endif
- }
-#ifndef NO_WOLFSSL_SERVER
- else {
- /* server side */
- if (ssl->ctx->ticketEncCb == NULL) {
- WOLFSSL_MSG("Client sent session ticket, server has no callback");
- return 0;
- }
-
- if (length == 0) {
- /* blank ticket */
- ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
- if (ret == WOLFSSL_SUCCESS) {
- ret = 0;
- TLSX_SetResponse(ssl, TLSX_SESSION_TICKET); /* send blank ticket */
- ssl->options.createTicket = 1; /* will send ticket msg */
- ssl->options.useTicket = 1;
- ssl->options.resuming = 0; /* no standard resumption */
- ssl->arrays->sessionIDSz = 0; /* no echo on blank ticket */
- }
- } else {
- /* got actual ticket from client */
- ret = DoClientTicket(ssl, input, length);
- if (ret == WOLFSSL_TICKET_RET_OK) { /* use ticket to resume */
- WOLFSSL_MSG("Using existing client ticket");
- ssl->options.useTicket = 1;
- ssl->options.resuming = 1;
- } else if (ret == WOLFSSL_TICKET_RET_CREATE) {
- WOLFSSL_MSG("Using existing client ticket, creating new one");
- ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
- if (ret == WOLFSSL_SUCCESS) {
- ret = 0;
- TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
- /* send blank ticket */
- ssl->options.createTicket = 1; /* will send ticket msg */
- ssl->options.useTicket = 1;
- ssl->options.resuming = 1;
- }
- } else if (ret == WOLFSSL_TICKET_RET_REJECT) {
- WOLFSSL_MSG("Process client ticket rejected, not using");
- ssl->options.rejectTicket = 1;
- ret = 0; /* not fatal */
- } else if (ret == WOLFSSL_TICKET_RET_FATAL || ret < 0) {
- WOLFSSL_MSG("Process client ticket fatal error, not using");
- }
- }
- }
-#endif /* NO_WOLFSSL_SERVER */
-
- return ret;
-}
-
-WOLFSSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
- byte* data, word16 size, void* heap)
-{
- SessionTicket* ticket = (SessionTicket*)XMALLOC(sizeof(SessionTicket),
- heap, DYNAMIC_TYPE_TLSX);
- if (ticket) {
- ticket->data = (byte*)XMALLOC(size, heap, DYNAMIC_TYPE_TLSX);
- if (ticket->data == NULL) {
- XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
- return NULL;
- }
-
- XMEMCPY(ticket->data, data, size);
- ticket->size = size;
- ticket->lifetime = lifetime;
- }
-
- (void)heap;
-
- return ticket;
-}
-WOLFSSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap)
-{
- if (ticket) {
- XFREE(ticket->data, heap, DYNAMIC_TYPE_TLSX);
- XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
- }
-
- (void)heap;
-}
-
-int TLSX_UseSessionTicket(TLSX** extensions, SessionTicket* ticket, void* heap)
-{
- int ret = 0;
-
- if (extensions == NULL)
- return BAD_FUNC_ARG;
-
- /* If the ticket is NULL, the client will request a new ticket from the
- server. Otherwise, the client will use it in the next client hello. */
- if ((ret = TLSX_Push(extensions, TLSX_SESSION_TICKET, (void*)ticket, heap))
- != 0)
- return ret;
-
- return WOLFSSL_SUCCESS;
-}
-
-#define WOLF_STK_VALIDATE_REQUEST TLSX_SessionTicket_ValidateRequest
-#define WOLF_STK_GET_SIZE TLSX_SessionTicket_GetSize
-#define WOLF_STK_WRITE TLSX_SessionTicket_Write
-#define WOLF_STK_PARSE TLSX_SessionTicket_Parse
-#define WOLF_STK_FREE(stk, heap) TLSX_SessionTicket_Free((SessionTicket*)stk,(heap))
-
-#else
-
-#define WOLF_STK_FREE(a, b)
-#define WOLF_STK_VALIDATE_REQUEST(a)
-#define WOLF_STK_GET_SIZE(a, b) 0
-#define WOLF_STK_WRITE(a, b, c) 0
-#define WOLF_STK_PARSE(a, b, c, d) 0
-
-#endif /* HAVE_SESSION_TICKET */
-
-/******************************************************************************/
-/* Quantum-Safe-Hybrid */
-/******************************************************************************/
-
-#ifdef HAVE_QSH
-#if defined(HAVE_NTRU)
-static WC_RNG* gRng;
-static wolfSSL_Mutex* gRngMutex;
-#endif
-
-static void TLSX_QSH_FreeAll(QSHScheme* list, void* heap)
-{
- QSHScheme* current;
-
- while ((current = list)) {
- list = current->next;
- XFREE(current, heap, DYNAMIC_TYPE_TLSX);
- }
-
- (void)heap;
-}
-
-static int TLSX_QSH_Append(QSHScheme** list, word16 name, byte* pub,
- word16 pubLen)
-{
- QSHScheme* temp;
-
- if (list == NULL)
- return BAD_FUNC_ARG;
-
- if ((temp = (QSHScheme*)XMALLOC(sizeof(QSHScheme), NULL,
- DYNAMIC_TYPE_TLSX)) == NULL)
- return MEMORY_E;
-
- temp->name = name;
- temp->PK = pub;
- temp->PKLen = pubLen;
- temp->next = *list;
-
- *list = temp;
-
- return 0;
-}
-
-
-/* request for server's public key : 02 indicates 0-2 requested */
-static byte TLSX_QSH_SerPKReq(byte* output, byte isRequest)
-{
- if (isRequest) {
- /* only request one public key from the server */
- output[0] = 0x01;
-
- return OPAQUE8_LEN;
- }
- else {
- return 0;
- }
-}
-
-#ifndef NO_WOLFSSL_CLIENT
-
-/* check for TLS_QSH suite */
-static void TLSX_QSH_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
-{
- int i;
-
- for (i = 0; i < ssl->suites->suiteSz; i+= 2)
- if (ssl->suites->suites[i] == QSH_BYTE)
- return;
-
- /* No QSH suite found */
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_QUANTUM_SAFE_HYBRID));
-}
-
-
-/* return the size of the QSH hello extension
- list the list of QSHScheme structs containing id and key
- isRequest if 1 then is being sent to the server
- */
-word16 TLSX_QSH_GetSize(QSHScheme* list, byte isRequest)
-{
- QSHScheme* temp = list;
- word16 length = 0;
-
- /* account for size of scheme list and public key list */
- if (isRequest)
- length = OPAQUE16_LEN;
- length += OPAQUE24_LEN;
-
- /* for each non null element in list add size */
- while ((temp)) {
- /* add public key info Scheme | Key Length | Key */
- length += OPAQUE16_LEN;
- length += OPAQUE16_LEN;
- length += temp->PKLen;
-
- /* if client add name size for scheme list
- advance to next QSHScheme struct in list */
- if (isRequest)
- length += OPAQUE16_LEN;
- temp = temp->next;
- }
-
- /* add length for request server public keys */
- if (isRequest)
- length += OPAQUE8_LEN;
-
- return length;
-}
-
-
-/* write out a list of QSHScheme IDs */
-static word16 TLSX_QSH_Write(QSHScheme* list, byte* output)
-{
- QSHScheme* current = list;
- word16 length = 0;
-
- length += OPAQUE16_LEN;
-
- while (current) {
- c16toa(current->name, output + length);
- length += OPAQUE16_LEN;
- current = (QSHScheme*)current->next;
- }
-
- c16toa(length - OPAQUE16_LEN, output); /* writing list length */
-
- return length;
-}
-
-
-/* write public key list in extension */
-static word16 TLSX_QSHPK_WriteR(QSHScheme* format, byte* output)
-{
- word32 offset = 0;
- word16 public_len = 0;
-
- if (!format)
- return offset;
-
- /* write scheme ID */
- c16toa(format->name, output + offset);
- offset += OPAQUE16_LEN;
-
- /* write public key matching scheme */
- public_len = format->PKLen;
- c16toa(public_len, output + offset);
- offset += OPAQUE16_LEN;
- if (format->PK) {
- XMEMCPY(output+offset, format->PK, public_len);
- }
-
- return public_len + offset;
-}
-
-word16 TLSX_QSHPK_Write(QSHScheme* list, byte* output)
-{
- QSHScheme* current = list;
- word32 length = 0;
- word24 toWire;
-
- length += OPAQUE24_LEN;
-
- while (current) {
- length += TLSX_QSHPK_WriteR(current, output + length);
- current = (QSHScheme*)current->next;
- }
- /* length of public keys sent */
- c32to24(length - OPAQUE24_LEN, toWire);
- output[0] = toWire[0];
- output[1] = toWire[1];
- output[2] = toWire[2];
-
- return length;
-}
-
-#endif /* NO_WOLFSSL_CLIENT */
-#ifndef NO_WOLFSSL_SERVER
-
-static void TLSX_QSHAgreement(TLSX** extensions, void* heap)
-{
- TLSX* extension = TLSX_Find(*extensions, TLSX_QUANTUM_SAFE_HYBRID);
- QSHScheme* format = NULL;
- QSHScheme* del = NULL;
- QSHScheme* prev = NULL;
-
- if (extension == NULL)
- return;
-
- format = (QSHScheme*)extension->data;
- while (format) {
- if (format->PKLen == 0) {
- /* case of head */
- if (format == extension->data) {
- extension->data = format->next;
- }
- if (prev)
- prev->next = format->next;
- del = format;
- format = format->next;
- XFREE(del, heap, DYNAMIC_TYPE_TMP_BUFFER);
- del = NULL;
- } else {
- prev = format;
- format = format->next;
- }
- }
-
- (void)heap;
-}
-
-
-/* Parse in hello extension
- input the byte stream to process
- length length of total extension found
- isRequest set to 1 if being sent to the server
- */
-static int TLSX_QSH_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte isRequest)
-{
- byte numKeys = 0;
- word16 offset = 0;
- word16 schemSz = 0;
- word16 offset_len = 0;
- word32 offset_pk = 0;
- word16 name = 0;
- word16 PKLen = 0;
- byte* PK = NULL;
- int r;
-
-
- if (OPAQUE16_LEN > length)
- return BUFFER_ERROR;
-
- if (isRequest) {
- ato16(input, &schemSz);
-
- /* list of public keys available for QSH schemes */
- offset_len = schemSz + OPAQUE16_LEN;
- }
-
- offset_pk = ((input[offset_len] << 16) & 0xFF00000) |
- (((input[offset_len + 1]) << 8) & 0xFF00) |
- (input[offset_len + 2] & 0xFF);
- offset_len += OPAQUE24_LEN;
-
- /* check buffer size */
- if (offset_pk > length)
- return BUFFER_ERROR;
-
- /* set maximum number of keys the client will accept */
- if (!isRequest)
- numKeys = (ssl->maxRequest < 1)? 1 : ssl->maxRequest;
-
- /* hello extension read list of scheme ids */
- if (isRequest) {
-
- /* read in request for public keys */
- ssl->minRequest = (input[length -1] >> 4) & 0xFF;
- ssl->maxRequest = input[length -1] & 0x0F;
-
- /* choose the min between min requested by client and 1 */
- numKeys = (ssl->minRequest > 1) ? ssl->minRequest : 1;
-
- if (ssl->minRequest > ssl->maxRequest)
- return BAD_FUNC_ARG;
-
- offset += OPAQUE16_LEN;
- schemSz += offset;
-
- /* check buffer size */
- if (schemSz > length)
- return BUFFER_ERROR;
-
- while ((offset < schemSz) && numKeys) {
- /* Scheme ID list */
- ato16(input + offset, &name);
- offset += OPAQUE16_LEN;
-
- /* validate we have scheme id */
- if (ssl->user_set_QSHSchemes &&
- !TLSX_ValidateQSHScheme(&ssl->extensions, name)) {
- continue;
- }
-
- /* server create keys on demand */
- if ((r = TLSX_CreateNtruKey(ssl, name)) != 0) {
- WOLFSSL_MSG("Error creating ntru keys");
- return r;
- }
-
- /* peer sent an agreed upon scheme */
- r = TLSX_UseQSHScheme(&ssl->extensions, name, NULL, 0, ssl->heap);
-
- if (r != WOLFSSL_SUCCESS) return r; /* throw error */
-
- numKeys--;
- }
-
- /* choose the min between min requested by client and 1 */
- numKeys = (ssl->minRequest > 1) ? ssl->minRequest : 1;
- }
-
- /* QSHPK struct */
- offset_pk += offset_len;
- while ((offset_len < offset_pk) && numKeys) {
- QSHKey * temp;
-
- if ((temp = (QSHKey*)XMALLOC(sizeof(QSHKey), ssl->heap,
- DYNAMIC_TYPE_TLSX)) == NULL)
- return MEMORY_E;
-
- /* initialize */
- temp->next = NULL;
- temp->pub.buffer = NULL;
- temp->pub.length = 0;
- temp->pri.buffer = NULL;
- temp->pri.length = 0;
-
- /* scheme id */
- ato16(input + offset_len, &(temp->name));
- offset_len += OPAQUE16_LEN;
-
- /* public key length */
- ato16(input + offset_len, &PKLen);
- temp->pub.length = PKLen;
- offset_len += OPAQUE16_LEN;
-
-
- if (isRequest) {
- /* validate we have scheme id */
- if (ssl->user_set_QSHSchemes &&
- (!TLSX_ValidateQSHScheme(&ssl->extensions, temp->name))) {
- offset_len += PKLen;
- XFREE(temp, ssl->heap, DYNAMIC_TYPE_TLSX);
- continue;
- }
- }
-
- /* read in public key */
- if (PKLen > 0) {
- temp->pub.buffer = (byte*)XMALLOC(temp->pub.length,
- ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- XMEMCPY(temp->pub.buffer, input + offset_len, temp->pub.length);
- offset_len += PKLen;
- }
- else {
- PK = NULL;
- }
-
- /* use own key when adding to extensions list for sending reply */
- PKLen = 0;
- PK = TLSX_QSHKeyFind_Pub(ssl->QSH_Key, &PKLen, temp->name);
- r = TLSX_UseQSHScheme(&ssl->extensions, temp->name, PK, PKLen,
- ssl->heap);
-
- /* store peers key */
- ssl->peerQSHKeyPresent = 1;
- if (TLSX_AddQSHKey(&ssl->peerQSHKey, temp) != 0)
- return MEMORY_E;
-
- if (temp->pub.length == 0) {
- XFREE(temp, ssl->heap, DYNAMIC_TYPE_TLSX);
- }
-
- if (r != WOLFSSL_SUCCESS) {return r;} /* throw error */
-
- numKeys--;
- }
-
- /* reply to a QSH extension sent from client */
- if (isRequest) {
- TLSX_SetResponse(ssl, TLSX_QUANTUM_SAFE_HYBRID);
- /* only use schemes we have key generated for -- free the rest */
- TLSX_QSHAgreement(&ssl->extensions, ssl->heap);
- }
-
- return 0;
-}
-
-
-/* Used for parsing in QSHCipher structs on Key Exchange */
-int TLSX_QSHCipher_Parse(WOLFSSL* ssl, const byte* input, word16 length,
- byte isServer)
-{
- QSHKey* key;
- word16 Max_Secret_Len = 48;
- word16 offset = 0;
- word16 offset_len = 0;
- word32 offset_pk = 0;
- word16 name = 0;
- word16 secretLen = 0;
- byte* secret = NULL;
- word16 buffLen = 0;
- byte buff[145]; /* size enough for 3 secrets */
- buffer* buf;
-
- /* pointer to location where secret should be stored */
- if (isServer) {
- buf = ssl->QSH_secret->CliSi;
- }
- else {
- buf = ssl->QSH_secret->SerSi;
- }
-
- offset_pk = ((input[offset_len] << 16) & 0xFF0000) |
- (((input[offset_len + 1]) << 8) & 0xFF00) |
- (input[offset_len + 2] & 0xFF);
- offset_len += OPAQUE24_LEN;
-
- /* validating extension list length -- check if trying to read over edge
- of buffer */
- if (length < (offset_pk + OPAQUE24_LEN)) {
- return BUFFER_ERROR;
- }
-
- /* QSHCipherList struct */
- offset_pk += offset_len;
- while (offset_len < offset_pk) {
-
- /* scheme id */
- ato16(input + offset_len, &name);
- offset_len += OPAQUE16_LEN;
-
- /* public key length */
- ato16(input + offset_len, &secretLen);
- offset_len += OPAQUE16_LEN;
-
- /* read in public key */
- if (secretLen > 0) {
- secret = (byte*)(input + offset_len);
- offset_len += secretLen;
- }
- else {
- secret = NULL;
- }
-
- /* no secret sent */
- if (secret == NULL)
- continue;
-
- /* find corresponding key */
- key = ssl->QSH_Key;
- while (key) {
- if (key->name == name)
- break;
- else
- key = (QSHKey*)key->next;
- }
-
- /* if we do not have the key than there was a big issue negotiation */
- if (key == NULL) {
- WOLFSSL_MSG("key was null for decryption!!!\n");
- return MEMORY_E;
- }
-
- /* Decrypt sent secret */
- buffLen = Max_Secret_Len;
- QSH_Decrypt(key, secret, secretLen, buff + offset, &buffLen);
- offset += buffLen;
- }
-
- /* allocate memory for buffer */
- buf->length = offset;
- buf->buffer = (byte*)XMALLOC(offset, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (buf->buffer == NULL)
- return MEMORY_E;
-
- /* store secrets */
- XMEMCPY(buf->buffer, buff, offset);
- ForceZero(buff, offset);
-
- return offset_len;
-}
-
-
-/* return 1 on success */
-int TLSX_ValidateQSHScheme(TLSX** extensions, word16 theirs) {
- TLSX* extension = TLSX_Find(*extensions, TLSX_QUANTUM_SAFE_HYBRID);
- QSHScheme* format = NULL;
-
- /* if no extension is sent then do not use QSH */
- if (!extension) {
- WOLFSSL_MSG("No QSH Extension");
- return 0;
- }
-
- for (format = (QSHScheme*)extension->data; format; format = format->next) {
- if (format->name == theirs) {
- WOLFSSL_MSG("Found Matching QSH Scheme");
- return 1; /* have QSH */
- }
- }
-
- return 0;
-}
-#endif /* NO_WOLFSSL_SERVER */
-
-/* test if the QSH Scheme is implemented
- return 1 if yes 0 if no */
-static int TLSX_HaveQSHScheme(word16 name)
-{
- switch(name) {
- #ifdef HAVE_NTRU
- case WOLFSSL_NTRU_EESS439:
- case WOLFSSL_NTRU_EESS593:
- case WOLFSSL_NTRU_EESS743:
- return 1;
- #endif
- case WOLFSSL_LWE_XXX:
- case WOLFSSL_HFE_XXX:
- return 0; /* not supported yet */
-
- default:
- return 0;
- }
-}
-
-
-/* Add a QSHScheme struct to list of usable ones */
-int TLSX_UseQSHScheme(TLSX** extensions, word16 name, byte* pKey, word16 pkeySz,
- void* heap)
-{
- TLSX* extension = NULL;
- QSHScheme* format = NULL;
- int ret = 0;
-
- /* sanity check */
- if (extensions == NULL || (pKey == NULL && pkeySz != 0))
- return BAD_FUNC_ARG;
-
- extension = TLSX_Find(*extensions, TLSX_QUANTUM_SAFE_HYBRID);
-
- /* if scheme is implemented than add */
- if (TLSX_HaveQSHScheme(name)) {
- if ((ret = TLSX_QSH_Append(&format, name, pKey, pkeySz)) != 0)
- return ret;
-
- extension = TLSX_Find(*extensions, TLSX_QUANTUM_SAFE_HYBRID);
- if (!extension) {
- if ((ret = TLSX_Push(extensions, TLSX_QUANTUM_SAFE_HYBRID, format,
- heap)) != 0) {
- XFREE(format, 0, DYNAMIC_TYPE_TLSX);
- return ret;
- }
- }
- else {
- /* push new QSH object to extension data. */
- format->next = (QSHScheme*)extension->data;
- extension->data = (void*)format;
-
- /* look for another format of the same name to remove (replacement) */
- do {
- if (format->next && (format->next->name == name)) {
- QSHScheme* next = format->next;
-
- format->next = next->next;
- XFREE(next, 0, DYNAMIC_TYPE_TLSX);
-
- break;
- }
- } while ((format = format->next));
- }
- }
- return WOLFSSL_SUCCESS;
-}
-
-#define QSH_FREE_ALL TLSX_QSH_FreeAll
-#define QSH_VALIDATE_REQUEST TLSX_QSH_ValidateRequest
-
-#ifndef NO_WOLFSSL_CLIENT
-#define QSH_GET_SIZE TLSX_QSH_GetSize
-#define QSH_WRITE TLSX_QSH_Write
-#else
-#define QSH_GET_SIZE(list, a) 0
-#define QSH_WRITE(a, b) 0
-#endif
-
-#ifndef NO_WOLFSSL_SERVER
-#define QSH_PARSE TLSX_QSH_Parse
-#else
-#define QSH_PARSE(a, b, c, d) 0
-#endif
-
-#define QSHPK_WRITE TLSX_QSHPK_Write
-#define QSH_SERREQ TLSX_QSH_SerPKReq
-#else
-
-#define QSH_FREE_ALL(list, heap)
-#define QSH_GET_SIZE(list, a) 0
-#define QSH_WRITE(a, b) 0
-#define QSH_PARSE(a, b, c, d) 0
-#define QSHPK_WRITE(a, b) 0
-#define QSH_SERREQ(a, b) 0
-#define QSH_VALIDATE_REQUEST(a, b)
-
-#endif /* HAVE_QSH */
-
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
-/******************************************************************************/
-/* Encrypt-then-MAC */
-/******************************************************************************/
-
-#ifndef WOLFSSL_NO_TLS12
-static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl);
-
-/**
- * Get the size of the Encrypt-Then-MAC extension.
- *
- * msgType Type of message to put extension into.
- * pSz Size of extension data.
- * return SANITY_MSG_E when the message is not allowed to have extension and
- * 0 otherwise.
- */
-static int TLSX_EncryptThenMac_GetSize(byte msgType, word16* pSz)
-{
- (void)pSz;
-
- if (msgType != client_hello && msgType != server_hello) {
- return SANITY_MSG_E;
- }
-
- /* Empty extension */
-
- return 0;
-}
-
-/**
- * Write the Encrypt-Then-MAC extension.
- *
- * data Unused
- * output Extension data buffer. Unused.
- * msgType Type of message to put extension into.
- * pSz Size of extension data.
- * return SANITY_MSG_E when the message is not allowed to have extension and
- * 0 otherwise.
- */
-static int TLSX_EncryptThenMac_Write(void* data, byte* output, byte msgType,
- word16* pSz)
-{
- (void)data;
- (void)output;
- (void)pSz;
-
- if (msgType != client_hello && msgType != server_hello) {
- return SANITY_MSG_E;
- }
-
- /* Empty extension */
-
- return 0;
-}
-
-/**
- * Parse the Encrypt-Then-MAC extension.
- *
- * ssl SSL object
- * input Extension data buffer.
- * length Length of this extension's data.
- * msgType Type of message to extension appeared in.
- * return SANITY_MSG_E when the message is not allowed to have extension,
- * BUFFER_ERROR when the extension's data is invalid,
- * MEMORY_E when unable to allocate memory and
- * 0 otherwise.
- */
-static int TLSX_EncryptThenMac_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte msgType)
-{
- int ret;
-
- (void)input;
-
- if (msgType != client_hello && msgType != server_hello) {
- return SANITY_MSG_E;
- }
-
- /* Empty extension */
- if (length != 0)
- return BUFFER_ERROR;
-
- if (msgType == client_hello) {
- /* Check the user hasn't disallowed use of Encrypt-Then-Mac. */
- if (!ssl->options.disallowEncThenMac) {
- ssl->options.encThenMac = 1;
- /* Set the extension reply. */
- ret = TLSX_EncryptThenMac_Use(ssl);
- if (ret != 0)
- return ret;
- TLSX_SetResponse(ssl, TLSX_ENCRYPT_THEN_MAC);
- }
- return 0;
- }
-
- /* Server Hello */
- if (ssl->options.disallowEncThenMac)
- return SANITY_MSG_E;
-
- ssl->options.encThenMac = 1;
- return 0;
-
-}
-
-/**
- * Add the Encrypt-Then-MAC extension to list.
- *
- * ssl SSL object
- * return MEMORY_E when unable to allocate memory and 0 otherwise.
- */
-static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl)
-{
- int ret = 0;
- TLSX* extension;
-
- /* Find the Encrypt-Then-Mac extension if it exists. */
- extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
- if (extension == NULL) {
- /* Push new Encrypt-Then-Mac extension. */
- ret = TLSX_Push(&ssl->extensions, TLSX_ENCRYPT_THEN_MAC, NULL,
- ssl->heap);
- if (ret != 0)
- return ret;
- }
-
- return 0;
-}
-
-#define ETM_GET_SIZE TLSX_EncryptThenMac_GetSize
-#define ETM_WRITE TLSX_EncryptThenMac_Write
-#define ETM_PARSE TLSX_EncryptThenMac_Parse
-
-#else
-
-#define ETM_GET_SIZE(a, b) 0
-#define ETM_WRITE(a, b, c, d) 0
-#define ETM_PARSE(a, b, c, d) 0
-
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
-
-/******************************************************************************/
-/* Supported Versions */
-/******************************************************************************/
-
-#ifdef WOLFSSL_TLS13
-/* Return the size of the SupportedVersions extension's data.
- *
- * data The SSL/TLS object.
- * msgType The type of the message this extension is being written into.
- * returns the length of data that will be in the extension.
- */
-static int TLSX_SupportedVersions_GetSize(void* data, byte msgType, word16* pSz)
-{
- WOLFSSL* ssl = (WOLFSSL*)data;
-
- if (msgType == client_hello) {
- /* TLS v1.2 and TLS v1.3 */
- int cnt = 0;
-
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- if ((ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0)
- #endif
- cnt++;
-
- if (ssl->options.downgrade) {
-#ifndef WOLFSSL_NO_TLS12
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- if ((ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0)
- #endif
- cnt++;
-#endif
-
-#ifndef NO_OLD_TLS
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- if ((ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0)
- #endif
- cnt++;
- #ifdef WOLFSSL_ALLOW_TLSV10
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- if ((ssl->options.mask & SSL_OP_NO_TLSv1) == 0)
- #endif
- cnt++;
- #endif
-#endif
- }
-
- *pSz += (word16)(OPAQUE8_LEN + cnt * OPAQUE16_LEN);
- }
-#ifndef WOLFSSL_TLS13_DRAFT_18
- else if (msgType == server_hello || msgType == hello_retry_request)
- *pSz += OPAQUE16_LEN;
-#endif
- else
- return SANITY_MSG_E;
-
- return 0;
-}
-
-/* Writes the SupportedVersions extension into the buffer.
- *
- * data The SSL/TLS object.
- * output The buffer to write the extension into.
- * msgType The type of the message this extension is being written into.
- * returns the length of data that was written.
- */
-static int TLSX_SupportedVersions_Write(void* data, byte* output,
- byte msgType, word16* pSz)
-{
- WOLFSSL* ssl = (WOLFSSL*)data;
- byte major;
- byte* cnt;
-
- if (msgType == client_hello) {
- major = ssl->ctx->method->version.major;
-
-
- cnt = output++;
- *cnt = 0;
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- if ((ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0)
- #endif
- {
- *cnt += OPAQUE16_LEN;
-#ifdef WOLFSSL_TLS13_DRAFT
- /* The TLS draft major number. */
- *(output++) = TLS_DRAFT_MAJOR;
- /* Version of draft supported. */
- *(output++) = TLS_DRAFT_MINOR;
-#else
- *(output++) = major;
- *(output++) = (byte)TLSv1_3_MINOR;
-#endif
- }
- if (ssl->options.downgrade) {
-#ifndef WOLFSSL_NO_TLS12
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- if ((ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0)
- #endif
- {
- *cnt += OPAQUE16_LEN;
- *(output++) = major;
- *(output++) = (byte)TLSv1_2_MINOR;
- }
-#endif
-
-#ifndef NO_OLD_TLS
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- if ((ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0)
- #endif
- {
- *cnt += OPAQUE16_LEN;
- *(output++) = major;
- *(output++) = (byte)TLSv1_1_MINOR;
- }
- #ifdef WOLFSSL_ALLOW_TLSV10
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- if ((ssl->options.mask & SSL_OP_NO_TLSv1) == 0)
- #endif
- {
- *cnt += OPAQUE16_LEN;
- *(output++) = major;
- *(output++) = (byte)TLSv1_MINOR;
- }
- #endif
-#endif
- }
-
- *pSz += (word16)(OPAQUE8_LEN + *cnt);
- }
-#ifndef WOLFSSL_TLS13_DRAFT_18
- else if (msgType == server_hello || msgType == hello_retry_request) {
- #ifdef WOLFSSL_TLS13_DRAFT
- if (ssl->version.major == SSLv3_MAJOR &&
- ssl->version.minor == TLSv1_3_MINOR) {
- output[0] = TLS_DRAFT_MAJOR;
- output[1] = TLS_DRAFT_MINOR;
- }
- else
- #endif
- {
- output[0] = ssl->version.major;
- output[1] = ssl->version.minor;
- }
-
- *pSz += OPAQUE16_LEN;
- }
-#endif
- else
- return SANITY_MSG_E;
-
- return 0;
-}
-
-/* Parse the SupportedVersions extension.
- *
- * ssl The SSL/TLS object.
- * input The buffer with the extension data.
- * length The length of the extension data.
- * msgType The type of the message this extension is being parsed from.
- * returns 0 on success, otherwise failure.
- */
-static int TLSX_SupportedVersions_Parse(WOLFSSL* ssl, byte* input,
- word16 length, byte msgType)
-{
- ProtocolVersion pv = ssl->ctx->method->version;
- int i;
- int len;
- byte major, minor;
- int newMinor = 0;
- int set = 0;
-
- if (msgType == client_hello) {
- /* Must contain a length and at least one version. */
- if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1)
- return BUFFER_ERROR;
-
- len = *input;
-
- /* Protocol version array must fill rest of data. */
- if (length != (word16)OPAQUE8_LEN + len)
- return BUFFER_ERROR;
-
- input++;
-
- /* Find first match. */
- for (i = 0; i < len; i += OPAQUE16_LEN) {
- major = input[i];
- minor = input[i + OPAQUE8_LEN];
-
-#ifdef WOLFSSL_TLS13_DRAFT
- if (major == TLS_DRAFT_MAJOR && minor == TLS_DRAFT_MINOR) {
- major = SSLv3_MAJOR;
- minor = TLSv1_3_MINOR;
- }
-#else
- if (major == TLS_DRAFT_MAJOR)
- continue;
-#endif
-
- if (major != pv.major)
- continue;
-
- /* No upgrade allowed. */
- if (minor > ssl->version.minor)
- continue;
- /* Check downgrade. */
- if (minor < ssl->version.minor) {
- if (!ssl->options.downgrade)
- continue;
-
- if (minor < ssl->options.minDowngrade)
- continue;
-
- if (newMinor == 0 && minor > ssl->options.oldMinor) {
- /* Downgrade the version. */
- ssl->version.minor = minor;
- }
- }
-
- if (minor >= TLSv1_3_MINOR) {
- if (!ssl->options.tls1_3) {
- ssl->options.tls1_3 = 1;
- TLSX_Push(&ssl->extensions, TLSX_SUPPORTED_VERSIONS, ssl,
- ssl->heap);
-#ifndef WOLFSSL_TLS13_DRAFT_18
- TLSX_SetResponse(ssl, TLSX_SUPPORTED_VERSIONS);
-#endif
- }
- if (minor > newMinor) {
- ssl->version.minor = minor;
- newMinor = minor;
- }
- }
- else if (minor > ssl->options.oldMinor)
- ssl->options.oldMinor = minor;
-
- set = 1;
- }
- if (!set) {
- #ifdef WOLFSSL_MYSQL_COMPATIBLE
- SendAlert(ssl, alert_fatal, wc_protocol_version);
- #else
- SendAlert(ssl, alert_fatal, protocol_version);
- #endif
- return VERSION_ERROR;
- }
- }
-#ifndef WOLFSSL_TLS13_DRAFT_18
- else if (msgType == server_hello || msgType == hello_retry_request) {
- /* Must contain one version. */
- if (length != OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- major = input[0];
- minor = input[OPAQUE8_LEN];
-
- #ifdef WOLFSSL_TLS13_DRAFT
- if (major == TLS_DRAFT_MAJOR && minor == TLS_DRAFT_MINOR) {
- major = SSLv3_MAJOR;
- minor = TLSv1_3_MINOR;
- }
- #endif
-
- if (major != pv.major)
- return VERSION_ERROR;
-
- /* Can't downgrade with this extension below TLS v1.3. */
- if (minor < TLSv1_3_MINOR)
- return VERSION_ERROR;
-
- /* Version is TLS v1.2 to handle downgrading from TLS v1.3+. */
- if (ssl->options.downgrade && ssl->version.minor == TLSv1_2_MINOR) {
- /* Set minor version back to TLS v1.3+ */
- ssl->version.minor = ssl->ctx->method->version.minor;
- }
-
- /* No upgrade allowed. */
- if (ssl->version.minor < minor)
- return VERSION_ERROR;
-
- /* Check downgrade. */
- if (ssl->version.minor > minor) {
- if (!ssl->options.downgrade)
- return VERSION_ERROR;
-
- if (minor < ssl->options.minDowngrade)
- return VERSION_ERROR;
-
- /* Downgrade the version. */
- ssl->version.minor = minor;
- }
- }
-#endif
- else
- return SANITY_MSG_E;
-
- return 0;
-}
-
-/* Sets a new SupportedVersions extension into the extension list.
- *
- * extensions The list of extensions.
- * data The extensions specific data.
- * heap The heap used for allocation.
- * returns 0 on success, otherwise failure.
- */
-static int TLSX_SetSupportedVersions(TLSX** extensions, const void* data,
- void* heap)
-{
- if (extensions == NULL || data == NULL)
- return BAD_FUNC_ARG;
-
- return TLSX_Push(extensions, TLSX_SUPPORTED_VERSIONS, (void *)data, heap);
-}
-
-#define SV_GET_SIZE TLSX_SupportedVersions_GetSize
-#define SV_WRITE TLSX_SupportedVersions_Write
-#define SV_PARSE TLSX_SupportedVersions_Parse
-
-#else
-
-#define SV_GET_SIZE(a, b, c) 0
-#define SV_WRITE(a, b, c, d) 0
-#define SV_PARSE(a, b, c, d) 0
-
-#endif /* WOLFSSL_TLS13 */
-
-#if defined(WOLFSSL_TLS13)
-
-/******************************************************************************/
-/* Cookie */
-/******************************************************************************/
-
-/* Free the cookie data.
- *
- * cookie Cookie data.
- * heap The heap used for allocation.
- */
-static void TLSX_Cookie_FreeAll(Cookie* cookie, void* heap)
-{
- (void)heap;
-
- if (cookie != NULL)
- XFREE(cookie, heap, DYNAMIC_TYPE_TLSX);
-}
-
-/* Get the size of the encoded Cookie extension.
- * In messages: ClientHello and HelloRetryRequest.
- *
- * cookie The cookie to write.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes of the encoded Cookie extension.
- */
-static int TLSX_Cookie_GetSize(Cookie* cookie, byte msgType, word16* pSz)
-{
- if (msgType == client_hello || msgType == hello_retry_request)
- *pSz += OPAQUE16_LEN + cookie->len;
- else
- return SANITY_MSG_E;
- return 0;
-}
-
-/* Writes the Cookie extension into the output buffer.
- * Assumes that the the output buffer is big enough to hold data.
- * In messages: ClientHello and HelloRetryRequest.
- *
- * cookie The cookie to write.
- * output The buffer to write into.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes written into the buffer.
- */
-static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
- word16* pSz)
-{
- if (msgType == client_hello || msgType == hello_retry_request) {
- c16toa(cookie->len, output);
- output += OPAQUE16_LEN;
- XMEMCPY(output, &cookie->data, cookie->len);
- *pSz += OPAQUE16_LEN + cookie->len;
- }
- else
- return SANITY_MSG_E;
- return 0;
-}
-
-/* Parse the Cookie extension.
- * In messages: ClientHello and HelloRetryRequest.
- *
- * ssl The SSL/TLS object.
- * input The extension data.
- * length The length of the extension data.
- * msgType The type of the message this extension is being parsed from.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_Cookie_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte msgType)
-{
- word16 len;
- word16 idx = 0;
- TLSX* extension;
- Cookie* cookie;
-
- if (msgType != client_hello && msgType != hello_retry_request)
- return SANITY_MSG_E;
-
- /* Message contains length and Cookie which must be at least one byte
- * in length.
- */
- if (length < OPAQUE16_LEN + 1)
- return BUFFER_E;
- ato16(input + idx, &len);
- idx += OPAQUE16_LEN;
- if (length - idx != len)
- return BUFFER_E;
-
- if (msgType == hello_retry_request)
- return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0);
-
- /* client_hello */
- extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
- if (extension == NULL)
- return HRR_COOKIE_ERROR;
-
- cookie = (Cookie*)extension->data;
- if (cookie->len != len || XMEMCMP(&cookie->data, input + idx, len) != 0)
- return HRR_COOKIE_ERROR;
-
- /* Request seen. */
- extension->resp = 0;
-
- return 0;
-}
-
-/* Use the data to create a new Cookie object in the extensions.
- *
- * ssl SSL/TLS object.
- * data Cookie data.
- * len Length of cookie data in bytes.
- * mac MAC data.
- * macSz Length of MAC data in bytes.
- * resp Indicates the extension will go into a response (HelloRetryRequest).
- * returns 0 on success and other values indicate failure.
- */
-int TLSX_Cookie_Use(WOLFSSL* ssl, byte* data, word16 len, byte* mac,
- byte macSz, int resp)
-{
- int ret = 0;
- TLSX* extension;
- Cookie* cookie;
-
- /* Find the cookie extension if it exists. */
- extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
- if (extension == NULL) {
- /* Push new cookie extension. */
- ret = TLSX_Push(&ssl->extensions, TLSX_COOKIE, NULL, ssl->heap);
- if (ret != 0)
- return ret;
-
- extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
- if (extension == NULL)
- return MEMORY_E;
- }
-
- /* The Cookie structure has one byte for cookie data already. */
- cookie = (Cookie*)XMALLOC(sizeof(Cookie) + len + macSz - 1, ssl->heap,
- DYNAMIC_TYPE_TLSX);
- if (cookie == NULL)
- return MEMORY_E;
-
- cookie->len = len + macSz;
- XMEMCPY(&cookie->data, data, len);
- if (mac != NULL)
- XMEMCPY(&cookie->data + len, mac, macSz);
-
- extension->data = (void*)cookie;
- extension->resp = (byte)resp;
-
- return 0;
-}
-
-#define CKE_FREE_ALL TLSX_Cookie_FreeAll
-#define CKE_GET_SIZE TLSX_Cookie_GetSize
-#define CKE_WRITE TLSX_Cookie_Write
-#define CKE_PARSE TLSX_Cookie_Parse
-
-#else
-
-#define CKE_FREE_ALL(a, b) 0
-#define CKE_GET_SIZE(a, b, c) 0
-#define CKE_WRITE(a, b, c, d) 0
-#define CKE_PARSE(a, b, c, d) 0
-
-#endif
-#if !defined(WOLFSSL_NO_SIGALG)
-/******************************************************************************/
-/* Signature Algorithms */
-/******************************************************************************/
-
-/* Return the size of the SignatureAlgorithms extension's data.
- *
- * data Unused
- * returns the length of data that will be in the extension.
- */
-
-static word16 TLSX_SignatureAlgorithms_GetSize(void* data)
-{
- WOLFSSL* ssl = (WOLFSSL*)data;
-
- return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
-}
-
-/* Creates a bit string of supported hash algorithms with RSA PSS.
- * The bit string is used when determining which signature algorithm to use
- * when creating the CertificateVerify message.
- * Note: Valid data has an even length as each signature algorithm is two bytes.
- *
- * ssl The SSL/TLS object.
- * input The buffer with the list of supported signature algorithms.
- * length The length of the list in bytes.
- * returns 0 on success, BUFFER_ERROR when the length is not even.
- */
-static int TLSX_SignatureAlgorithms_MapPss(WOLFSSL *ssl, byte* input,
- word16 length)
-{
- word16 i;
-
- if ((length & 1) == 1)
- return BUFFER_ERROR;
-
- ssl->pssAlgo = 0;
- for (i = 0; i < length; i += 2) {
- if (input[i] == rsa_pss_sa_algo && input[i + 1] <= sha512_mac)
- ssl->pssAlgo |= 1 << input[i + 1];
- #ifdef WOLFSSL_TLS13
- if (input[i] == rsa_pss_sa_algo && input[i + 1] >= pss_sha256 &&
- input[i + 1] <= pss_sha512) {
- ssl->pssAlgo |= 1 << input[i + 1];
- }
- #endif
- }
-
- return 0;
-}
-
-/* Writes the SignatureAlgorithms extension into the buffer.
- *
- * data Unused
- * output The buffer to write the extension into.
- * returns the length of data that was written.
- */
-static word16 TLSX_SignatureAlgorithms_Write(void* data, byte* output)
-{
- WOLFSSL* ssl = (WOLFSSL*)data;
-
- c16toa(ssl->suites->hashSigAlgoSz, output);
- XMEMCPY(output + OPAQUE16_LEN, ssl->suites->hashSigAlgo,
- ssl->suites->hashSigAlgoSz);
-
- TLSX_SignatureAlgorithms_MapPss(ssl, output + OPAQUE16_LEN,
- ssl->suites->hashSigAlgoSz);
-
- return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
-}
-
-/* Parse the SignatureAlgorithms extension.
- *
- * ssl The SSL/TLS object.
- * input The buffer with the extension data.
- * length The length of the extension data.
- * returns 0 on success, otherwise failure.
- */
-static int TLSX_SignatureAlgorithms_Parse(WOLFSSL *ssl, byte* input,
- word16 length, byte isRequest, Suites* suites)
-{
- word16 len;
-
- if (!isRequest)
- return BUFFER_ERROR;
-
- /* Must contain a length and at least algorithm. */
- if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
- return BUFFER_ERROR;
-
- ato16(input, &len);
- input += OPAQUE16_LEN;
-
- /* Algorithm array must fill rest of data. */
- if (length != OPAQUE16_LEN + len)
- return BUFFER_ERROR;
-
- /* truncate hashSigAlgo list if too long */
- suites->hashSigAlgoSz = len;
- if (suites->hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
- WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
- suites->hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
- }
- XMEMCPY(suites->hashSigAlgo, input, suites->hashSigAlgoSz);
-
- return TLSX_SignatureAlgorithms_MapPss(ssl, input, len);
-}
-
-/* Sets a new SignatureAlgorithms extension into the extension list.
- *
- * extensions The list of extensions.
- * data The extensions specific data.
- * heap The heap used for allocation.
- * returns 0 on success, otherwise failure.
- */
-static int TLSX_SetSignatureAlgorithms(TLSX** extensions, const void* data,
- void* heap)
-{
- if (extensions == NULL)
- return BAD_FUNC_ARG;
-
- return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS, (void *)data, heap);
-}
-
-#define SA_GET_SIZE TLSX_SignatureAlgorithms_GetSize
-#define SA_WRITE TLSX_SignatureAlgorithms_Write
-#define SA_PARSE TLSX_SignatureAlgorithms_Parse
-#endif
-/******************************************************************************/
-/* Signature Algorithms Certificate */
-/******************************************************************************/
-
-#ifdef WOLFSSL_TLS13
-#if !defined(WOLFSSL_TLS13_DRAFT_18) && !defined(WOLFSSL_TLS13_DRAFT_22)
-/* Return the size of the SignatureAlgorithms extension's data.
- *
- * data Unused
- * returns the length of data that will be in the extension.
- */
-static word16 TLSX_SignatureAlgorithmsCert_GetSize(void* data)
-{
- WOLFSSL* ssl = (WOLFSSL*)data;
-
- return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
-}
-
-/* Writes the SignatureAlgorithmsCert extension into the buffer.
- *
- * data Unused
- * output The buffer to write the extension into.
- * returns the length of data that was written.
- */
-static word16 TLSX_SignatureAlgorithmsCert_Write(void* data, byte* output)
-{
- WOLFSSL* ssl = (WOLFSSL*)data;
-
- c16toa(ssl->certHashSigAlgoSz, output);
- XMEMCPY(output + OPAQUE16_LEN, ssl->certHashSigAlgo,
- ssl->certHashSigAlgoSz);
-
- return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
-}
-
-/* Parse the SignatureAlgorithmsCert extension.
- *
- * ssl The SSL/TLS object.
- * input The buffer with the extension data.
- * length The length of the extension data.
- * returns 0 on success, otherwise failure.
- */
-static int TLSX_SignatureAlgorithmsCert_Parse(WOLFSSL *ssl, byte* input,
- word16 length, byte isRequest)
-{
- word16 len;
-
- if (!isRequest)
- return BUFFER_ERROR;
-
- /* Must contain a length and at least algorithm. */
- if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
- return BUFFER_ERROR;
-
- ato16(input, &len);
- input += OPAQUE16_LEN;
-
- /* Algorithm array must fill rest of data. */
- if (length != OPAQUE16_LEN + len)
- return BUFFER_ERROR;
-
- /* truncate hashSigAlgo list if too long */
- ssl->certHashSigAlgoSz = len;
- if (ssl->certHashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
- WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
- ssl->certHashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
- }
- XMEMCPY(ssl->certHashSigAlgo, input, ssl->certHashSigAlgoSz);
-
- return 0;
-}
-
-/* Sets a new SignatureAlgorithmsCert extension into the extension list.
- *
- * extensions The list of extensions.
- * data The extensions specific data.
- * heap The heap used for allocation.
- * returns 0 on success, otherwise failure.
- */
-static int TLSX_SetSignatureAlgorithmsCert(TLSX** extensions, const void* data,
- void* heap)
-{
- if (extensions == NULL)
- return BAD_FUNC_ARG;
-
- return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS_CERT, (void *)data,
- heap);
-}
-
-#define SAC_GET_SIZE TLSX_SignatureAlgorithmsCert_GetSize
-#define SAC_WRITE TLSX_SignatureAlgorithmsCert_Write
-#define SAC_PARSE TLSX_SignatureAlgorithmsCert_Parse
-#endif /* !WOLFSSL_TLS13_DRAFT_18 && !WOLFSSL_TLS13_DRAFT_22 */
-#endif /* WOLFSSL_TLS13 */
-
-
-/******************************************************************************/
-/* Key Share */
-/******************************************************************************/
-
-#ifdef WOLFSSL_TLS13
-/* Create a key share entry using named Diffie-Hellman parameters group.
- * Generates a key pair.
- *
- * ssl The SSL/TLS object.
- * kse The key share entry object.
- * returns 0 on success, otherwise failure.
- */
-static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
-{
- int ret;
-#ifndef NO_DH
- byte* keyData;
- void* key = NULL;
- word32 keySz;
- word32 dataSz;
- const DhParams* params;
-#ifdef WOLFSSL_SMALL_STACK
- DhKey* dhKey = NULL;
-#else
- DhKey dhKey[1];
-#endif
-
- /* TODO: [TLS13] The key size should come from wolfcrypt. */
- /* Pick the parameters from the named group. */
- switch (kse->group) {
- #ifdef HAVE_FFDHE_2048
- case WOLFSSL_FFDHE_2048:
- params = wc_Dh_ffdhe2048_Get();
- keySz = 29;
- break;
- #endif
- #ifdef HAVE_FFDHE_3072
- case WOLFSSL_FFDHE_3072:
- params = wc_Dh_ffdhe3072_Get();
- keySz = 34;
- break;
- #endif
- #ifdef HAVE_FFDHE_4096
- case WOLFSSL_FFDHE_4096:
- params = wc_Dh_ffdhe4096_Get();
- keySz = 39;
- break;
- #endif
- #ifdef HAVE_FFDHE_6144
- case WOLFSSL_FFDHE_6144:
- params = wc_Dh_ffdhe6144_Get();
- keySz = 46;
- break;
- #endif
- #ifdef HAVE_FFDHE_8192
- case WOLFSSL_FFDHE_8192:
- params = wc_Dh_ffdhe8192_Get();
- keySz = 52;
- break;
- #endif
- default:
- return BAD_FUNC_ARG;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- dhKey = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap, DYNAMIC_TYPE_DH);
- if (dhKey == NULL)
- return MEMORY_E;
-#endif
-
- ret = wc_InitDhKey_ex(dhKey, ssl->heap, ssl->devId);
- if (ret != 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
- #endif
- return ret;
- }
-
- /* Allocate space for the public key. */
- dataSz = params->p_len;
- keyData = (byte*)XMALLOC(dataSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (keyData == NULL) {
- ret = MEMORY_E;
- goto end;
- }
- /* Allocate space for the private key. */
- key = (byte*)XMALLOC(keySz, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- if (key == NULL) {
- ret = MEMORY_E;
- goto end;
- }
-
- /* Set key */
- ret = wc_DhSetKey(dhKey,
- (byte*)params->p, params->p_len,
- (byte*)params->g, params->g_len);
- if (ret != 0)
- goto end;
-
- /* Generate a new key pair. */
- ret = wc_DhGenerateKeyPair(dhKey, ssl->rng, (byte*)key, &keySz, keyData,
- &dataSz);
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* TODO: Make this function non-blocking */
- if (ret == WC_PENDING_E) {
- ret = wc_AsyncWait(ret, &dhKey->asyncDev, WC_ASYNC_FLAG_NONE);
- }
-#endif
- if (ret != 0)
- goto end;
-
- if (params->p_len != dataSz) {
- /* Pad the front of the key data with zeros. */
- XMEMMOVE(keyData + params->p_len - dataSz, keyData, dataSz);
- XMEMSET(keyData, 0, params->p_len - dataSz);
- }
-
- kse->pubKey = keyData;
- kse->pubKeyLen = params->p_len;
- kse->key = key;
- kse->keyLen = keySz;
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Public DH Key");
- WOLFSSL_BUFFER(keyData, params->p_len);
-#endif
-
-end:
-
- wc_FreeDhKey(dhKey);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
-#endif
-
- if (ret != 0) {
- /* Data owned by key share entry otherwise. */
- if (keyData != NULL)
- XFREE(keyData, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (key != NULL)
- XFREE(key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- }
-#else
- (void)ssl;
- (void)kse;
-
- ret = NOT_COMPILED_IN;
-#endif
-
- return ret;
-}
-
-/* Create a key share entry using X25519 parameters group.
- * Generates a key pair.
- *
- * ssl The SSL/TLS object.
- * kse The key share entry object.
- * returns 0 on success, otherwise failure.
- */
-static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
-{
- int ret;
-#ifdef HAVE_CURVE25519
- byte* keyData = NULL;
- word32 dataSize = CURVE25519_KEYSIZE;
- curve25519_key* key;
-
- /* Allocate an ECC key to hold private key. */
- key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
- DYNAMIC_TYPE_PRIVATE_KEY);
- if (key == NULL) {
- WOLFSSL_MSG("EccTempKey Memory error");
- return MEMORY_E;
- }
-
- /* Make an ECC key. */
- ret = wc_curve25519_init(key);
- if (ret != 0)
- goto end;
- ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
- if (ret != 0)
- goto end;
-
- /* Allocate space for the public key. */
- keyData = (byte*)XMALLOC(CURVE25519_KEYSIZE, ssl->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (keyData == NULL) {
- WOLFSSL_MSG("Key data Memory error");
- ret = MEMORY_E;
- goto end;
- }
-
- /* Export public key. */
- if (wc_curve25519_export_public_ex(key, keyData, &dataSize,
- EC25519_LITTLE_ENDIAN) != 0) {
- ret = ECC_EXPORT_ERROR;
- goto end;
- }
-
- kse->pubKey = keyData;
- kse->pubKeyLen = CURVE25519_KEYSIZE;
- kse->key = key;
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Public Curve25519 Key");
- WOLFSSL_BUFFER(keyData, dataSize);
-#endif
-
-end:
- if (ret != 0) {
- /* Data owned by key share entry otherwise. */
- if (keyData != NULL)
- XFREE(keyData, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- wc_curve25519_free(key);
- XFREE(key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- }
-#else
- (void)ssl;
- (void)kse;
-
- ret = NOT_COMPILED_IN;
-#endif /* HAVE_CURVE25519 */
-
- return ret;
-}
-
-/* Create a key share entry using X448 parameters group.
- * Generates a key pair.
- *
- * ssl The SSL/TLS object.
- * kse The key share entry object.
- * returns 0 on success, otherwise failure.
- */
-static int TLSX_KeyShare_GenX448Key(WOLFSSL *ssl, KeyShareEntry* kse)
-{
- int ret;
-#ifdef HAVE_CURVE448
- byte* keyData = NULL;
- word32 dataSize = CURVE448_KEY_SIZE;
- curve448_key* key;
-
- /* Allocate an ECC key to hold private key. */
- key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
- DYNAMIC_TYPE_PRIVATE_KEY);
- if (key == NULL) {
- WOLFSSL_MSG("EccTempKey Memory error");
- return MEMORY_E;
- }
-
- /* Make an ECC key. */
- ret = wc_curve448_init(key);
- if (ret != 0)
- goto end;
- ret = wc_curve448_make_key(ssl->rng, CURVE448_KEY_SIZE, key);
- if (ret != 0)
- goto end;
-
- /* Allocate space for the public key. */
- keyData = (byte*)XMALLOC(CURVE448_KEY_SIZE, ssl->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (keyData == NULL) {
- WOLFSSL_MSG("Key data Memory error");
- ret = MEMORY_E;
- goto end;
- }
-
- /* Export public key. */
- if (wc_curve448_export_public_ex(key, keyData, &dataSize,
- EC448_LITTLE_ENDIAN) != 0) {
- ret = ECC_EXPORT_ERROR;
- goto end;
- }
-
- kse->pubKey = keyData;
- kse->pubKeyLen = CURVE448_KEY_SIZE;
- kse->key = key;
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Public Curve448 Key");
- WOLFSSL_BUFFER(keyData, dataSize);
-#endif
-
-end:
- if (ret != 0) {
- /* Data owned by key share entry otherwise. */
- if (keyData != NULL)
- XFREE(keyData, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- wc_curve448_free(key);
- XFREE(key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- }
-#else
- (void)ssl;
- (void)kse;
-
- ret = NOT_COMPILED_IN;
-#endif /* HAVE_CURVE448 */
-
- return ret;
-}
-
-/* Create a key share entry using named elliptic curve parameters group.
- * Generates a key pair.
- *
- * ssl The SSL/TLS object.
- * kse The key share entry object.
- * returns 0 on success, otherwise failure.
- */
-static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
-{
- int ret;
-#ifdef HAVE_ECC
- byte* keyData = NULL;
- word32 dataSize;
- byte* keyPtr = NULL;
- word32 keySize;
- ecc_key* eccKey;
- word16 curveId;
-
- /* TODO: [TLS13] The key sizes should come from wolfcrypt. */
- /* Translate named group to a curve id. */
- switch (kse->group) {
- #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP256R1:
- curveId = ECC_SECP256R1;
- keySize = 32;
- dataSize = keySize * 2 + 1;
- break;
- #endif /* !NO_ECC_SECP */
- #endif
- #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP384R1:
- curveId = ECC_SECP384R1;
- keySize = 48;
- dataSize = keySize * 2 + 1;
- break;
- #endif /* !NO_ECC_SECP */
- #endif
- #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP521R1:
- curveId = ECC_SECP521R1;
- keySize = 66;
- dataSize = keySize * 2 + 1;
- break;
- #endif /* !NO_ECC_SECP */
- #endif
- #ifdef HAVE_X448
- case WOLFSSL_ECC_X448:
- curveId = ECC_X448;
- dataSize = keySize = 56;
- break;
- #endif
- default:
- return BAD_FUNC_ARG;
- }
-
- /* Allocate an ECC key to hold private key. */
- keyPtr = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap,
- DYNAMIC_TYPE_PRIVATE_KEY);
- if (keyPtr == NULL) {
- WOLFSSL_MSG("EccTempKey Memory error");
- return MEMORY_E;
- }
- eccKey = (ecc_key*)keyPtr;
-
- /* Make an ECC key. */
- ret = wc_ecc_init_ex(eccKey, ssl->heap, ssl->devId);
- if (ret != 0)
- goto end;
- ret = wc_ecc_make_key_ex(ssl->rng, keySize, eccKey, curveId);
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* TODO: Make this function non-blocking */
- if (ret == WC_PENDING_E) {
- ret = wc_AsyncWait(ret, &eccKey->asyncDev, WC_ASYNC_FLAG_NONE);
- }
-#endif
- if (ret != 0)
- goto end;
-
- /* Allocate space for the public key. */
- keyData = (byte*)XMALLOC(dataSize, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (keyData == NULL) {
- WOLFSSL_MSG("Key data Memory error");
- ret = MEMORY_E;
- goto end;
- }
-
- /* Export public key. */
- if (wc_ecc_export_x963(eccKey, keyData, &dataSize) != 0) {
- ret = ECC_EXPORT_ERROR;
- goto end;
- }
-
- kse->pubKey = keyData;
- kse->pubKeyLen = dataSize;
- kse->key = keyPtr;
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Public ECC Key");
- WOLFSSL_BUFFER(keyData, dataSize);
-#endif
-
-end:
- if (ret != 0) {
- /* Data owned by key share entry otherwise. */
- if (keyPtr != NULL)
- XFREE(keyPtr, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- if (keyData != NULL)
- XFREE(keyData, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- }
-#else
- (void)ssl;
- (void)kse;
-
- ret = NOT_COMPILED_IN;
-#endif /* HAVE_ECC */
-
- return ret;
-}
-
-/* Generate a secret/key using the key share entry.
- *
- * ssl The SSL/TLS object.
- * kse The key share entry holding peer data.
- */
-static int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse)
-{
- /* Named FFHE groups have a bit set to identify them. */
- if ((kse->group & NAMED_DH_MASK) == NAMED_DH_MASK)
- return TLSX_KeyShare_GenDhKey(ssl, kse);
- if (kse->group == WOLFSSL_ECC_X25519)
- return TLSX_KeyShare_GenX25519Key(ssl, kse);
- if (kse->group == WOLFSSL_ECC_X448)
- return TLSX_KeyShare_GenX448Key(ssl, kse);
- return TLSX_KeyShare_GenEccKey(ssl, kse);
-}
-
-/* Free the key share dynamic data.
- *
- * list The linked list of key share entry objects.
- * heap The heap used for allocation.
- */
-static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
-{
- KeyShareEntry* current;
-
- while ((current = list) != NULL) {
- list = current->next;
- if ((current->group & NAMED_DH_MASK) == 0) {
- if (current->group == WOLFSSL_ECC_X25519) {
-#ifdef HAVE_CURVE25519
- wc_curve25519_free((curve25519_key*)current->key);
-#endif
- }
- else if (current->group == WOLFSSL_ECC_X448) {
-#ifdef HAVE_CURVE448
- wc_curve448_free((curve448_key*)current->key);
-#endif
- }
- else {
-#ifdef HAVE_ECC
- wc_ecc_free((ecc_key*)(current->key));
-#endif
- }
- }
- if (current->key != NULL)
- XFREE(current->key, heap, DYNAMIC_TYPE_PRIVATE_KEY);
- XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(current->ke, heap, DYNAMIC_TYPE_PUBLIC_KEY);
- XFREE(current, heap, DYNAMIC_TYPE_TLSX);
- }
-
- (void)heap;
-}
-
-/* Get the size of the encoded key share extension.
- *
- * list The linked list of key share extensions.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes of the encoded key share extension.
- */
-static word16 TLSX_KeyShare_GetSize(KeyShareEntry* list, byte msgType)
-{
- word16 len = 0;
- byte isRequest = (msgType == client_hello);
- KeyShareEntry* current;
-
- /* The named group the server wants to use. */
- if (msgType == hello_retry_request)
- return OPAQUE16_LEN;
-
- /* List of key exchange groups. */
- if (isRequest)
- len += OPAQUE16_LEN;
- while ((current = list) != NULL) {
- list = current->next;
-
- if (!isRequest && current->key == NULL)
- continue;
-
- len += KE_GROUP_LEN + OPAQUE16_LEN + current->pubKeyLen;
- }
-
- return len;
-}
-
-/* Writes the key share extension into the output buffer.
- * Assumes that the the output buffer is big enough to hold data.
- *
- * list The linked list of key share entries.
- * output The buffer to write into.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes written into the buffer.
- */
-static word16 TLSX_KeyShare_Write(KeyShareEntry* list, byte* output,
- byte msgType)
-{
- word16 i = 0;
- byte isRequest = (msgType == client_hello);
- KeyShareEntry* current;
-
- if (msgType == hello_retry_request) {
- c16toa(list->group, output);
- return OPAQUE16_LEN;
- }
-
- /* ClientHello has a list but ServerHello is only the chosen. */
- if (isRequest)
- i += OPAQUE16_LEN;
-
- /* Write out all in the list. */
- while ((current = list) != NULL) {
- list = current->next;
-
- if (!isRequest && current->key == NULL)
- continue;
-
- c16toa(current->group, &output[i]);
- i += KE_GROUP_LEN;
- c16toa((word16)(current->pubKeyLen), &output[i]);
- i += OPAQUE16_LEN;
- XMEMCPY(&output[i], current->pubKey, current->pubKeyLen);
- i += (word16)current->pubKeyLen;
- }
- /* Write the length of the list if required. */
- if (isRequest)
- c16toa(i - OPAQUE16_LEN, output);
-
- return i;
-}
-
-/* Process the DH key share extension on the client side.
- *
- * ssl The SSL/TLS object.
- * keyShareEntry The key share entry object to use to calculate shared secret.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_KeyShare_ProcessDh(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
-{
-#ifndef NO_DH
- int ret;
- const DhParams* params;
-#ifdef WOLFSSL_SMALL_STACK
- DhKey* dhKey = NULL;
-#else
- DhKey dhKey[1];
-#endif
-
- switch (keyShareEntry->group) {
- #ifdef HAVE_FFDHE_2048
- case WOLFSSL_FFDHE_2048:
- params = wc_Dh_ffdhe2048_Get();
- break;
- #endif
- #ifdef HAVE_FFDHE_3072
- case WOLFSSL_FFDHE_3072:
- params = wc_Dh_ffdhe3072_Get();
- break;
- #endif
- #ifdef HAVE_FFDHE_4096
- case WOLFSSL_FFDHE_4096:
- params = wc_Dh_ffdhe4096_Get();
- break;
- #endif
- #ifdef HAVE_FFDHE_6144
- case WOLFSSL_FFDHE_6144:
- params = wc_Dh_ffdhe6144_Get();
- break;
- #endif
- #ifdef HAVE_FFDHE_8192
- case WOLFSSL_FFDHE_8192:
- params = wc_Dh_ffdhe8192_Get();
- break;
- #endif
- default:
- return PEER_KEY_ERROR;
- }
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Peer DH Key");
- WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- dhKey = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap, DYNAMIC_TYPE_DH);
- if (dhKey == NULL)
- return MEMORY_E;
-#endif
-
- ret = wc_InitDhKey_ex(dhKey, ssl->heap, ssl->devId);
- if (ret != 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
- #endif
- return ret;
- }
-
- /* Set key */
- ret = wc_DhSetKey(dhKey, (byte*)params->p, params->p_len, (byte*)params->g,
- params->g_len);
- if (ret != 0) {
- wc_FreeDhKey(dhKey);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
- #endif
- return ret;
- }
-
- ret = wc_DhCheckPubKey(dhKey, keyShareEntry->ke, keyShareEntry->keLen);
- if (ret != 0) {
- wc_FreeDhKey(dhKey);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
- #endif
- return PEER_KEY_ERROR;
- }
-
- /* Derive secret from private key and peer's public key. */
- ret = wc_DhAgree(dhKey,
- ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
- (const byte*)keyShareEntry->key, keyShareEntry->keyLen,
- keyShareEntry->ke, keyShareEntry->keLen);
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* TODO: Make this function non-blocking */
- if (ret == WC_PENDING_E) {
- ret = wc_AsyncWait(ret, &dhKey->asyncDev, WC_ASYNC_FLAG_NONE);
- }
-#endif
- /* RFC 8446 Section 7.4.1:
- * ... left-padded with zeros up to the size of the prime. ...
- */
- if (params->p_len > ssl->arrays->preMasterSz) {
- word32 diff = params->p_len - ssl->arrays->preMasterSz;
- XMEMMOVE(ssl->arrays->preMasterSecret + diff,
- ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
- XMEMSET(ssl->arrays->preMasterSecret, 0, diff);
- ssl->arrays->preMasterSz = params->p_len;
- }
-
- ssl->options.dhKeySz = params->p_len;
-
- wc_FreeDhKey(dhKey);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(dhKey, ssl->heap, DYNAMIC_TYPE_DH);
-#endif
- if (keyShareEntry->key != NULL) {
- XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- keyShareEntry->key = NULL;
- }
- XFREE(keyShareEntry->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- keyShareEntry->pubKey = NULL;
- XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- keyShareEntry->ke = NULL;
-
- return ret;
-#else
- (void)ssl;
- (void)keyShareEntry;
- return PEER_KEY_ERROR;
-#endif
-}
-
-/* Process the X25519 key share extension on the client side.
- *
- * ssl The SSL/TLS object.
- * keyShareEntry The key share entry object to use to calculate shared secret.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_KeyShare_ProcessX25519(WOLFSSL* ssl,
- KeyShareEntry* keyShareEntry)
-{
- int ret;
-
-#ifdef HAVE_CURVE25519
- curve25519_key* key = (curve25519_key*)keyShareEntry->key;
- curve25519_key* peerX25519Key;
-
-#ifdef HAVE_ECC
- if (ssl->peerEccKey != NULL) {
- wc_ecc_free(ssl->peerEccKey);
- ssl->peerEccKey = NULL;
- }
-#endif
-
- peerX25519Key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
- DYNAMIC_TYPE_TLSX);
- if (peerX25519Key == NULL) {
- WOLFSSL_MSG("PeerEccKey Memory error");
- return MEMORY_ERROR;
- }
- ret = wc_curve25519_init(peerX25519Key);
- if (ret != 0) {
- XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
- return ret;
- }
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Peer Curve25519 Key");
- WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
-#endif
-
- if (wc_curve25519_check_public(keyShareEntry->ke, keyShareEntry->keLen,
- EC25519_LITTLE_ENDIAN) != 0) {
- ret = ECC_PEERKEY_ERROR;
- }
-
- if (ret == 0) {
- if (wc_curve25519_import_public_ex(keyShareEntry->ke,
- keyShareEntry->keLen, peerX25519Key,
- EC25519_LITTLE_ENDIAN) != 0) {
- ret = ECC_PEERKEY_ERROR;
- }
- }
-
- if (ret == 0) {
- ssl->ecdhCurveOID = ECC_X25519_OID;
-
- ret = wc_curve25519_shared_secret_ex(key, peerX25519Key,
- ssl->arrays->preMasterSecret,
- &ssl->arrays->preMasterSz,
- EC25519_LITTLE_ENDIAN);
- }
-
- wc_curve25519_free(peerX25519Key);
- XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
- wc_curve25519_free((curve25519_key*)keyShareEntry->key);
- if (keyShareEntry->key != NULL) {
- XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- keyShareEntry->key = NULL;
- }
-#else
- (void)ssl;
- (void)keyShareEntry;
-
- ret = PEER_KEY_ERROR;
-#endif /* HAVE_CURVE25519 */
-
- return ret;
-}
-
-/* Process the X448 key share extension on the client side.
- *
- * ssl The SSL/TLS object.
- * keyShareEntry The key share entry object to use to calculate shared secret.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_KeyShare_ProcessX448(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
-{
- int ret;
-
-#ifdef HAVE_CURVE448
- curve448_key* key = (curve448_key*)keyShareEntry->key;
- curve448_key* peerX448Key;
-
-#ifdef HAVE_ECC
- if (ssl->peerEccKey != NULL) {
- wc_ecc_free(ssl->peerEccKey);
- ssl->peerEccKey = NULL;
- }
-#endif
-
- peerX448Key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
- DYNAMIC_TYPE_TLSX);
- if (peerX448Key == NULL) {
- WOLFSSL_MSG("PeerEccKey Memory error");
- return MEMORY_ERROR;
- }
- ret = wc_curve448_init(peerX448Key);
- if (ret != 0) {
- XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
- return ret;
- }
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Peer Curve448 Key");
- WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
-#endif
-
- if (wc_curve448_check_public(keyShareEntry->ke, keyShareEntry->keLen,
- EC448_LITTLE_ENDIAN) != 0) {
- ret = ECC_PEERKEY_ERROR;
- }
-
- if (ret == 0) {
- if (wc_curve448_import_public_ex(keyShareEntry->ke,
- keyShareEntry->keLen, peerX448Key,
- EC448_LITTLE_ENDIAN) != 0) {
- ret = ECC_PEERKEY_ERROR;
- }
- }
-
- if (ret == 0) {
- ssl->ecdhCurveOID = ECC_X448_OID;
-
- ret = wc_curve448_shared_secret_ex(key, peerX448Key,
- ssl->arrays->preMasterSecret,
- &ssl->arrays->preMasterSz,
- EC448_LITTLE_ENDIAN);
- }
-
- wc_curve448_free(peerX448Key);
- XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
- wc_curve448_free((curve448_key*)keyShareEntry->key);
- if (keyShareEntry->key != NULL) {
- XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- keyShareEntry->key = NULL;
- }
-#else
- (void)ssl;
- (void)keyShareEntry;
-
- ret = PEER_KEY_ERROR;
-#endif /* HAVE_CURVE448 */
-
- return ret;
-}
-
-/* Process the ECC key share extension on the client side.
- *
- * ssl The SSL/TLS object.
- * keyShareEntry The key share entry object to use to calculate shared secret.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_KeyShare_ProcessEcc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
-{
- int ret;
-
-#ifdef HAVE_ECC
- int curveId;
- ecc_key* keyShareKey = (ecc_key*)keyShareEntry->key;
-
- if (ssl->peerEccKey != NULL)
- wc_ecc_free(ssl->peerEccKey);
-
- ssl->peerEccKey = (ecc_key*)XMALLOC(sizeof(ecc_key), ssl->heap,
- DYNAMIC_TYPE_ECC);
- if (ssl->peerEccKey == NULL) {
- WOLFSSL_MSG("PeerEccKey Memory error");
- return MEMORY_ERROR;
- }
- ret = wc_ecc_init_ex(ssl->peerEccKey, ssl->heap, ssl->devId);
- if (ret != 0)
- return ret;
-
- /* find supported curve */
- switch (keyShareEntry->group) {
- #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP256R1:
- curveId = ECC_SECP256R1;
- break;
- #endif /* !NO_ECC_SECP */
- #endif
- #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP384R1:
- curveId = ECC_SECP384R1;
- break;
- #endif /* !NO_ECC_SECP */
- #endif
- #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP521R1:
- curveId = ECC_SECP521R1;
- break;
- #endif /* !NO_ECC_SECP */
- #endif
- #ifdef HAVE_X448
- case WOLFSSL_ECC_X448:
- curveId = ECC_X448;
- break;
- #endif
- default:
- /* unsupported curve */
- return ECC_PEERKEY_ERROR;
- }
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Peer ECC Key");
- WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
-#endif
-
- /* Point is validated by import function. */
- if (wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
- ssl->peerEccKey, curveId) != 0) {
- return ECC_PEERKEY_ERROR;
- }
- ssl->ecdhCurveOID = ssl->peerEccKey->dp->oidSum;
-
- do {
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &keyShareKey->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- #endif
- if (ret >= 0)
- ret = wc_ecc_shared_secret(keyShareKey, ssl->peerEccKey,
- ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz);
- } while (ret == WC_PENDING_E);
-
-#if 0
- /* TODO: Switch to support async here and use: */
- ret = EccSharedSecret(ssl, keyShareEntry->key, ssl->peerEccKey,
- keyShareEntry->ke, &keyShareEntry->keLen,
- ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
- ssl->options.side
- );
-#endif
-
- wc_ecc_free(ssl->peerEccKey);
- XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
- ssl->peerEccKey = NULL;
- wc_ecc_free((ecc_key*)(keyShareEntry->key));
- if (keyShareEntry->key != NULL) {
- XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
- keyShareEntry->key = NULL;
- }
-
-#else
- (void)ssl;
- (void)keyShareEntry;
-
- ret = PEER_KEY_ERROR;
-#endif /* HAVE_ECC */
-
- return ret;
-}
-
-/* Process the key share extension on the client side.
- *
- * ssl The SSL/TLS object.
- * keyShareEntry The key share entry object to use to calculate shared secret.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_KeyShare_Process(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
-{
- int ret;
-
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- ssl->session.namedGroup = (byte)keyShareEntry->group;
-#endif
- /* Use Key Share Data from server. */
- if (keyShareEntry->group & NAMED_DH_MASK)
- ret = TLSX_KeyShare_ProcessDh(ssl, keyShareEntry);
- else if (keyShareEntry->group == WOLFSSL_ECC_X25519)
- ret = TLSX_KeyShare_ProcessX25519(ssl, keyShareEntry);
- else if (keyShareEntry->group == WOLFSSL_ECC_X448)
- ret = TLSX_KeyShare_ProcessX448(ssl, keyShareEntry);
- else
- ret = TLSX_KeyShare_ProcessEcc(ssl, keyShareEntry);
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("KE Secret");
- WOLFSSL_BUFFER(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
-#endif
-
- return ret;
-}
-
-/* Parse an entry of the KeyShare extension.
- *
- * ssl The SSL/TLS object.
- * input The extension data.
- * length The length of the extension data.
- * kse The new key share entry object.
- * returns a positive number to indicate amount of data parsed and a negative
- * number on error.
- */
-static int TLSX_KeyShareEntry_Parse(WOLFSSL* ssl, byte* input, word16 length,
- KeyShareEntry **kse)
-{
- int ret;
- word16 group;
- word16 keLen;
- int offset = 0;
- byte* ke;
-
- if (length < OPAQUE16_LEN + OPAQUE16_LEN)
- return BUFFER_ERROR;
- /* Named group */
- ato16(&input[offset], &group);
- offset += OPAQUE16_LEN;
- /* Key exchange data - public key. */
- ato16(&input[offset], &keLen);
- offset += OPAQUE16_LEN;
- if (keLen == 0)
- return INVALID_PARAMETER;
- if (keLen > length - offset)
- return BUFFER_ERROR;
-
- /* Store a copy in the key share object. */
- ke = (byte*)XMALLOC(keLen, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (ke == NULL)
- return MEMORY_E;
- XMEMCPY(ke, &input[offset], keLen);
-
- /* Populate a key share object in the extension. */
- ret = TLSX_KeyShare_Use(ssl, group, keLen, ke, kse);
- if (ret != 0) {
- XFREE(ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- return ret;
- }
-
- /* Total length of the parsed data. */
- return offset + keLen;
-}
-
-/* Searches the groups sent for the specified named group.
- *
- * ssl SSL/TLS object.
- * name Group name to match.
- * returns 1 when the extension has the group name and 0 otherwise.
- */
-static int TLSX_KeyShare_Find(WOLFSSL* ssl, word16 group)
-{
- TLSX* extension;
- KeyShareEntry* list;
-
- extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
- if (extension == NULL) {
- extension = TLSX_Find(ssl->ctx->extensions, TLSX_KEY_SHARE);
- if (extension == NULL)
- return 0;
- }
-
- list = (KeyShareEntry*)extension->data;
- while (list != NULL) {
- if (list->group == group)
- return 1;
- list = list->next;
- }
-
- return 0;
-}
-
-
-/* Searches the supported groups extension for the specified named group.
- *
- * ssl The SSL/TLS object.
- * name The group name to match.
- * returns 1 when the extension has the group name and 0 otherwise.
- */
-static int TLSX_SupportedGroups_Find(WOLFSSL* ssl, word16 name)
-{
-#ifdef HAVE_SUPPORTED_CURVES
- TLSX* extension;
- SupportedCurve* curve = NULL;
-
- if ((extension = TLSX_Find(ssl->extensions,
- TLSX_SUPPORTED_GROUPS)) == NULL) {
- if ((extension = TLSX_Find(ssl->ctx->extensions,
- TLSX_SUPPORTED_GROUPS)) == NULL) {
- return 0;
- }
- }
-
- for (curve = (SupportedCurve*)extension->data; curve; curve = curve->next) {
- if (curve->name == name)
- return 1;
- }
-#endif
-
- (void)ssl;
- (void)name;
-
- return 0;
-}
-
-
-/* Parse the KeyShare extension.
- * Different formats in different messages.
- *
- * ssl The SSL/TLS object.
- * input The extension data.
- * length The length of the extension data.
- * msgType The type of the message this extension is being parsed from.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_KeyShare_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte msgType)
-{
- int ret;
- KeyShareEntry *keyShareEntry = NULL;
- word16 group;
-
- if (msgType == client_hello) {
- int offset = 0;
- word16 len;
- TLSX* extension;
-
- /* Add a KeyShare extension if it doesn't exist. */
- extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
- if (extension == NULL) {
- /* Push new KeyShare extension. */
- ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
- if (ret != 0)
- return ret;
- }
-
- if (length < OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- /* ClientHello contains zero or more key share entries. */
- ato16(input, &len);
- if (len != length - OPAQUE16_LEN)
- return BUFFER_ERROR;
- offset += OPAQUE16_LEN;
-
- while (offset < (int)length) {
- ret = TLSX_KeyShareEntry_Parse(ssl, &input[offset], length - offset,
- &keyShareEntry);
- if (ret < 0)
- return ret;
-
- offset += ret;
- }
-
- ret = 0;
- }
- else if (msgType == server_hello) {
- int len;
-
- if (length < OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- /* The data is the named group the server wants to use. */
- ato16(input, &group);
-
- /* Check the selected group was supported by ClientHello extensions. */
- if (!TLSX_SupportedGroups_Find(ssl, group))
- return BAD_KEY_SHARE_DATA;
-
- /* Check if the group was sent. */
- if (!TLSX_KeyShare_Find(ssl, group))
- return BAD_KEY_SHARE_DATA;
-
- /* ServerHello contains one key share entry. */
- len = TLSX_KeyShareEntry_Parse(ssl, input, length, &keyShareEntry);
- if (len != (int)length)
- return BUFFER_ERROR;
-
- /* Not in list sent if there isn't a private key. */
- if (keyShareEntry == NULL || keyShareEntry->key == NULL)
- return BAD_KEY_SHARE_DATA;
-
- /* Process the entry to calculate the secret. */
- ret = TLSX_KeyShare_Process(ssl, keyShareEntry);
- if (ret == 0)
- ssl->session.namedGroup = ssl->namedGroup = group;
- }
- else if (msgType == hello_retry_request) {
- if (length != OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- /* The data is the named group the server wants to use. */
- ato16(input, &group);
-
- /* Check the selected group was supported by ClientHello extensions. */
- if (!TLSX_SupportedGroups_Find(ssl, group))
- return BAD_KEY_SHARE_DATA;
-
- /* Check if the group was sent. */
- if (TLSX_KeyShare_Find(ssl, group))
- return BAD_KEY_SHARE_DATA;
-
- /* Clear out unusable key shares. */
- ret = TLSX_KeyShare_Empty(ssl);
- if (ret != 0)
- return ret;
-
- /* Try to use the server's group. */
- ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL);
- }
- else {
- /* Not a message type that is allowed to have this extension. */
- return SANITY_MSG_E;
- }
-
- return ret;
-}
-
-/* Create a new key share entry and put it into the list.
- *
- * list The linked list of key share entries.
- * group The named group.
- * heap The memory to allocate with.
- * keyShareEntry The new key share entry object.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_KeyShare_New(KeyShareEntry** list, int group, void *heap,
- KeyShareEntry** keyShareEntry)
-{
- KeyShareEntry* kse;
- KeyShareEntry** next;
-
- kse = (KeyShareEntry*)XMALLOC(sizeof(KeyShareEntry), heap,
- DYNAMIC_TYPE_TLSX);
- if (kse == NULL)
- return MEMORY_E;
-
- XMEMSET(kse, 0, sizeof(*kse));
- kse->group = (word16)group;
-
- /* Add it to the back and maintain the links. */
- while (*list != NULL) {
- /* Assign to temporary to work around compiler bug found by customer. */
- next = &((*list)->next);
- list = next;
- }
- *list = kse;
- *keyShareEntry = kse;
-
- (void)heap;
-
- return 0;
-}
-
-/* Use the data to create a new key share object in the extensions.
- *
- * ssl The SSL/TLS object.
- * group The named group.
- * len The length of the public key data.
- * data The public key data.
- * kse The new key share entry object.
- * returns 0 on success and other values indicate failure.
- */
-int TLSX_KeyShare_Use(WOLFSSL* ssl, word16 group, word16 len, byte* data,
- KeyShareEntry **kse)
-{
- int ret = 0;
- TLSX* extension;
- KeyShareEntry* keyShareEntry = NULL;
-
- /* Find the KeyShare extension if it exists. */
- extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
- if (extension == NULL) {
- /* Push new KeyShare extension. */
- ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
- if (ret != 0)
- return ret;
-
- extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
- if (extension == NULL)
- return MEMORY_E;
- }
- extension->resp = 0;
-
- /* Try to find the key share entry with this group. */
- keyShareEntry = (KeyShareEntry*)extension->data;
- while (keyShareEntry != NULL) {
- if (keyShareEntry->group == group)
- break;
- keyShareEntry = keyShareEntry->next;
- }
-
- /* Create a new key share entry if not found. */
- if (keyShareEntry == NULL) {
- ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, group,
- ssl->heap, &keyShareEntry);
- if (ret != 0)
- return ret;
- }
-
- if (data != NULL) {
- if (keyShareEntry->ke != NULL) {
- XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- }
- keyShareEntry->ke = data;
- keyShareEntry->keLen = len;
- }
- else {
- /* Generate a key pair. */
- ret = TLSX_KeyShare_GenKey(ssl, keyShareEntry);
- if (ret != 0)
- return ret;
- }
-
- if (kse != NULL)
- *kse = keyShareEntry;
-
- return 0;
-}
-
-/* Set an empty Key Share extension.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success and other values indicate failure.
- */
-int TLSX_KeyShare_Empty(WOLFSSL* ssl)
-{
- int ret = 0;
- TLSX* extension;
-
- /* Find the KeyShare extension if it exists. */
- extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
- if (extension == NULL) {
- /* Push new KeyShare extension. */
- ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
- }
- else if (extension->data != NULL) {
- TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
- extension->data = NULL;
- }
-
- return ret;
-}
-
-/* Returns whether this group is supported.
- *
- * namedGroup The named group to check.
- * returns 1 when supported or 0 otherwise.
- */
-static int TLSX_KeyShare_IsSupported(int namedGroup)
-{
- switch (namedGroup) {
- #ifdef HAVE_FFDHE_2048
- case WOLFSSL_FFDHE_2048:
- break;
- #endif
- #ifdef HAVE_FFDHE_3072
- case WOLFSSL_FFDHE_3072:
- break;
- #endif
- #ifdef HAVE_FFDHE_4096
- case WOLFSSL_FFDHE_4096:
- break;
- #endif
- #ifdef HAVE_FFDHE_6144
- case WOLFSSL_FFDHE_6144:
- break;
- #endif
- #ifdef HAVE_FFDHE_8192
- case WOLFSSL_FFDHE_8192:
- break;
- #endif
- #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP256R1:
- break;
- #endif /* !NO_ECC_SECP */
- #endif
- #ifdef HAVE_CURVE25519
- case WOLFSSL_ECC_X25519:
- break;
- #endif
- #ifdef HAVE_CURVE448
- case WOLFSSL_ECC_X448:
- break;
- #endif
- #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP384R1:
- break;
- #endif /* !NO_ECC_SECP */
- #endif
- #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- case WOLFSSL_ECC_SECP521R1:
- break;
- #endif /* !NO_ECC_SECP */
- #endif
- default:
- return 0;
- }
-
- return 1;
-}
-
-/* Examines the application specified group ranking and returns the rank of the
- * group.
- * If no group ranking set then all groups are rank 0 (highest).
- *
- * ssl The SSL/TLS object.
- * group The group to check ranking for.
- * returns ranking from 0 to MAX_GROUP_COUNT-1 or -1 when group not in list.
- */
-static int TLSX_KeyShare_GroupRank(WOLFSSL* ssl, int group)
-{
- byte i;
-
- if (ssl->numGroups == 0) {
-#if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
- #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP256R1;
- #endif
- #endif
-#endif
- #ifndef HAVE_FIPS
- #if defined(HAVE_CURVE25519)
- ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X25519;
- #endif
- #endif
- #ifndef HAVE_FIPS
- #if defined(HAVE_CURVE448)
- ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X448;
- #endif
- #endif
-#if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
- #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP384R1;
- #endif
- #endif
- #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP521R1;
- #endif
- #endif
-#endif
- /* Add FFDHE supported groups. */
- #ifdef HAVE_FFDHE_2048
- ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_2048;
- #endif
- #ifdef HAVE_FFDHE_3072
- ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_3072;
- #endif
- #ifdef HAVE_FFDHE_4096
- ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_4096;
- #endif
- #ifdef HAVE_FFDHE_6144
- ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_6144;
- #endif
- #ifdef HAVE_FFDHE_8192
- ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_8192;
- #endif
- }
-
- for (i = 0; i < ssl->numGroups; i++)
- if (ssl->group[i] == (word16)group)
- return i;
-
- return -1;
-}
-
-/* Set a key share that is supported by the client into extensions.
- *
- * ssl The SSL/TLS object.
- * returns BAD_KEY_SHARE_DATA if no supported group has a key share,
- * 0 if a supported group has a key share and other values indicate an error.
- */
-static int TLSX_KeyShare_SetSupported(WOLFSSL* ssl)
-{
- int ret;
-#ifdef HAVE_SUPPORTED_CURVES
- TLSX* extension;
- SupportedCurve* curve = NULL;
- SupportedCurve* preferredCurve = NULL;
- int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
- int rank;
-
- extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
- if (extension != NULL)
- curve = (SupportedCurve*)extension->data;
- /* Use server's preference order. */
- for (; curve != NULL; curve = curve->next) {
- if (!TLSX_KeyShare_IsSupported(curve->name))
- continue;
-
- rank = TLSX_KeyShare_GroupRank(ssl, curve->name);
- if (rank == -1)
- continue;
- if (rank < preferredRank) {
- preferredCurve = curve;
- preferredRank = rank;
- }
- }
- curve = preferredCurve;
-
- if (curve == NULL)
- return BAD_KEY_SHARE_DATA;
-
- /* Delete the old key share data list. */
- extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
- if (extension != NULL) {
- TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
- extension->data = NULL;
- }
-
- /* Add in the chosen group. */
- ret = TLSX_KeyShare_Use(ssl, curve->name, 0, NULL, NULL);
- if (ret != 0)
- return ret;
-
- /* Set extension to be in response. */
- extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
- extension->resp = 1;
-#else
-
- (void)ssl;
- ret = NOT_COMPILED_IN;
-#endif
-
- return ret;
-}
-
-/* Ensure there is a key pair that can be used for key exchange.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success and other values indicate failure.
- */
-int TLSX_KeyShare_Establish(WOLFSSL *ssl)
-{
- int ret;
- TLSX* extension;
- KeyShareEntry* clientKSE = NULL;
- KeyShareEntry* serverKSE;
- KeyShareEntry* list = NULL;
- KeyShareEntry* preferredKSE = NULL;
- int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
- int rank;
-
- /* Find the KeyShare extension if it exists. */
- extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
- if (extension != NULL)
- list = (KeyShareEntry*)extension->data;
-
- if (extension && extension->resp == 1)
- return 0;
-
- /* Use server's preference order. */
- for (clientKSE = list; clientKSE != NULL; clientKSE = clientKSE->next) {
- if (clientKSE->ke == NULL)
- continue;
-
- /* Check consistency now - extensions in any order. */
- if (!TLSX_SupportedGroups_Find(ssl, clientKSE->group))
- return BAD_KEY_SHARE_DATA;
-
- #ifdef OPENSSL_EXTRA
- if ((clientKSE->group & NAMED_DH_MASK) == 0) {
- /* Check if server supports group. */
- if (ssl->ctx->disabledCurves & (1 << clientKSE->group))
- continue;
- }
- #endif
- if (!TLSX_KeyShare_IsSupported(clientKSE->group))
- continue;
-
- rank = TLSX_KeyShare_GroupRank(ssl, clientKSE->group);
- if (rank == -1)
- continue;
- if (rank < preferredRank) {
- preferredKSE = clientKSE;
- preferredRank = rank;
- }
- }
- clientKSE = preferredKSE;
-
- /* No supported group found - send HelloRetryRequest. */
- if (clientKSE == NULL) {
- ret = TLSX_KeyShare_SetSupported(ssl);
- /* Return KEY_SHARE_ERROR to indicate HelloRetryRequest required. */
- if (ret == 0)
- return KEY_SHARE_ERROR;
- return ret;
- }
-
- list = NULL;
- /* Generate a new key pair. */
- ret = TLSX_KeyShare_New(&list, clientKSE->group, ssl->heap, &serverKSE);
- if (ret != 0)
- return ret;
-
- if (clientKSE->key == NULL) {
- ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
- if (ret != 0)
- return ret;
- }
- else {
- serverKSE->key = clientKSE->key;
- serverKSE->keyLen = clientKSE->keyLen;
- serverKSE->pubKey = clientKSE->pubKey;
- serverKSE->pubKeyLen = clientKSE->pubKeyLen;
- clientKSE->key = NULL;
- clientKSE->pubKey = NULL;
- }
- serverKSE->ke = clientKSE->ke;
- serverKSE->keLen = clientKSE->keLen;
- clientKSE->ke = NULL;
- clientKSE->keLen = 0;
-
- TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
- extension->data = (void *)serverKSE;
-
- extension->resp = 1;
-
- return 0;
-}
-
-/* Derive the shared secret of the key exchange.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success and other values indicate failure.
- */
-int TLSX_KeyShare_DeriveSecret(WOLFSSL *ssl)
-{
- int ret;
- TLSX* extension;
- KeyShareEntry* list = NULL;
-
- /* Find the KeyShare extension if it exists. */
- extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
- if (extension != NULL)
- list = (KeyShareEntry*)extension->data;
-
- if (list == NULL)
- return KEY_SHARE_ERROR;
-
- /* Calculate secret. */
- ret = TLSX_KeyShare_Process(ssl, list);
- if (ret != 0)
- return ret;
-
- return ret;
-}
-
-#define KS_FREE_ALL TLSX_KeyShare_FreeAll
-#define KS_GET_SIZE TLSX_KeyShare_GetSize
-#define KS_WRITE TLSX_KeyShare_Write
-#define KS_PARSE TLSX_KeyShare_Parse
-
-#else
-
-#define KS_FREE_ALL(a, b)
-#define KS_GET_SIZE(a, b) 0
-#define KS_WRITE(a, b, c) 0
-#define KS_PARSE(a, b, c, d) 0
-
-#endif /* WOLFSSL_TLS13 */
-
-/******************************************************************************/
-/* Pre-Shared Key */
-/******************************************************************************/
-
-#if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
-/* Free the pre-shared key dynamic data.
- *
- * list The linked list of key share entry objects.
- * heap The heap used for allocation.
- */
-static void TLSX_PreSharedKey_FreeAll(PreSharedKey* list, void* heap)
-{
- PreSharedKey* current;
-
- while ((current = list) != NULL) {
- list = current->next;
- XFREE(current->identity, heap, DYNAMIC_TYPE_TLSX);
- XFREE(current, heap, DYNAMIC_TYPE_TLSX);
- }
-
- (void)heap;
-}
-
-/* Get the size of the encoded pre shared key extension.
- *
- * list The linked list of pre-shared key extensions.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes of the encoded pre-shared key extension or
- * SANITY_MSG_E to indicate invalid message type.
- */
-static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
- word16* pSz)
-{
- if (msgType == client_hello) {
- /* Length of identities + Length of binders. */
- word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
- while (list != NULL) {
- /* Each entry has: identity, ticket age and binder. */
- len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
- OPAQUE8_LEN + list->binderLen;
- list = list->next;
- }
- *pSz += len;
- return 0;
- }
-
- if (msgType == server_hello) {
- *pSz += OPAQUE16_LEN;
- return 0;
- }
-
- return SANITY_MSG_E;
-}
-
-/* The number of bytes to be written for the binders.
- *
- * list The linked list of pre-shared key extensions.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes of the encoded pre-shared key extension or
- * SANITY_MSG_E to indicate invalid message type.
- */
-int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
- word16* pSz)
-{
- word16 len;
-
- if (msgType != client_hello)
- return SANITY_MSG_E;
-
- /* Length of all binders. */
- len = OPAQUE16_LEN;
- while (list != NULL) {
- len += OPAQUE8_LEN + list->binderLen;
- list = list->next;
- }
-
- *pSz = len;
- return 0;
-}
-
-/* Writes the pre-shared key extension into the output buffer - binders only.
- * Assumes that the the output buffer is big enough to hold data.
- *
- * list The linked list of key share entries.
- * output The buffer to write into.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes written into the buffer.
- */
-int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list, byte* output,
- byte msgType, word16* pSz)
-{
- PreSharedKey* current = list;
- word16 idx = 0;
- word16 lenIdx;
- word16 len;
-
- if (msgType != client_hello)
- return SANITY_MSG_E;
-
- /* Skip length of all binders. */
- lenIdx = idx;
- idx += OPAQUE16_LEN;
- while (current != NULL) {
- /* Binder data length. */
- output[idx++] = current->binderLen;
- /* Binder data. */
- XMEMCPY(output + idx, current->binder, current->binderLen);
- idx += current->binderLen;
-
- current = current->next;
- }
- /* Length of the binders. */
- len = idx - lenIdx - OPAQUE16_LEN;
- c16toa(len, output + lenIdx);
-
- *pSz = idx;
- return 0;
-}
-
-
-/* Writes the pre-shared key extension into the output buffer.
- * Assumes that the the output buffer is big enough to hold data.
- *
- * list The linked list of key share entries.
- * output The buffer to write into.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes written into the buffer.
- */
-static int TLSX_PreSharedKey_Write(PreSharedKey* list, byte* output,
- byte msgType, word16* pSz)
-{
- if (msgType == client_hello) {
- PreSharedKey* current = list;
- word16 idx = 0;
- word16 lenIdx;
- word16 len;
- int ret;
-
- /* Write identites only. Binders after HMACing over this. */
- lenIdx = idx;
- idx += OPAQUE16_LEN;
- while (current != NULL) {
- /* Identity length */
- c16toa(current->identityLen, output + idx);
- idx += OPAQUE16_LEN;
- /* Identity data */
- XMEMCPY(output + idx, current->identity, current->identityLen);
- idx += current->identityLen;
-
- /* Obfuscated ticket age. */
- c32toa(current->ticketAge, output + idx);
- idx += OPAQUE32_LEN;
-
- current = current->next;
- }
- /* Length of the identites. */
- len = idx - lenIdx - OPAQUE16_LEN;
- c16toa(len, output + lenIdx);
-
- /* Don't include binders here.
- * The binders are based on the hash of all the ClientHello data up to
- * and include the identities written above.
- */
- ret = TLSX_PreSharedKey_GetSizeBinders(list, msgType, &len);
- if (ret < 0)
- return ret;
- *pSz += idx + len;
- }
- else if (msgType == server_hello) {
- word16 i;
-
- /* Find the index of the chosen identity. */
- for (i=0; list != NULL && !list->chosen; i++)
- list = list->next;
- if (list == NULL)
- return BUILD_MSG_ERROR;
-
- /* The index of the identity chosen by the server from the list supplied
- * by the client.
- */
- c16toa(i, output);
- *pSz += OPAQUE16_LEN;
- }
- else
- return SANITY_MSG_E;
-
- return 0;
-}
-
-/* Parse the pre-shared key extension.
- * Different formats in different messages.
- *
- * ssl The SSL/TLS object.
- * input The extension data.
- * length The length of the extension data.
- * msgType The type of the message this extension is being parsed from.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_PreSharedKey_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte msgType)
-{
- TLSX* extension;
- PreSharedKey* list;
-
- if (msgType == client_hello) {
- int ret;
- word16 len;
- word16 idx = 0;
-
- TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
-
- /* Length of identities and of binders. */
- if (length - idx < OPAQUE16_LEN + OPAQUE16_LEN)
- return BUFFER_E;
-
- /* Length of identities. */
- ato16(input + idx, &len);
- idx += OPAQUE16_LEN;
- if (len < MIN_PSK_ID_LEN || length - idx < len)
- return BUFFER_E;
-
- /* Create a pre-shared key object for each identity. */
- while (len > 0) {
- byte* identity;
- word16 identityLen;
- word32 age;
-
- if (len < OPAQUE16_LEN)
- return BUFFER_E;
-
- /* Length of identity. */
- ato16(input + idx, &identityLen);
- idx += OPAQUE16_LEN;
- if (len < OPAQUE16_LEN + identityLen + OPAQUE32_LEN ||
- identityLen > MAX_PSK_ID_LEN)
- return BUFFER_E;
- /* Cache identity pointer. */
- identity = input + idx;
- idx += identityLen;
- /* Ticket age. */
- ato32(input + idx, &age);
- idx += OPAQUE32_LEN;
-
- ret = TLSX_PreSharedKey_Use(ssl, identity, identityLen, age, no_mac,
- 0, 0, 1, NULL);
- if (ret != 0)
- return ret;
-
- /* Done with this identity. */
- len -= OPAQUE16_LEN + identityLen + OPAQUE32_LEN;
- }
-
- /* Find the list of identities sent to server. */
- extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
- if (extension == NULL)
- return PSK_KEY_ERROR;
- list = (PreSharedKey*)extension->data;
-
- /* Length of binders. */
- if (idx + OPAQUE16_LEN > length)
- return BUFFER_E;
- ato16(input + idx, &len);
- idx += OPAQUE16_LEN;
- if (len < MIN_PSK_BINDERS_LEN || length - idx < len)
- return BUFFER_E;
-
- /* Set binder for each identity. */
- while (list != NULL && len > 0) {
- /* Length of binder */
- list->binderLen = input[idx++];
- if (list->binderLen < WC_SHA256_DIGEST_SIZE ||
- list->binderLen > WC_MAX_DIGEST_SIZE)
- return BUFFER_E;
- if (len < OPAQUE8_LEN + list->binderLen)
- return BUFFER_E;
-
- /* Copy binder into static buffer. */
- XMEMCPY(list->binder, input + idx, list->binderLen);
- idx += list->binderLen;
-
- /* Done with binder entry. */
- len -= OPAQUE8_LEN + list->binderLen;
-
- /* Next identity. */
- list = list->next;
- }
- if (list != NULL || len != 0)
- return BUFFER_E;
-
- return 0;
- }
-
- if (msgType == server_hello) {
- word16 idx;
-
- /* Index of identity chosen by server. */
- if (length != OPAQUE16_LEN)
- return BUFFER_E;
- ato16(input, &idx);
-
- #ifdef WOLFSSL_EARLY_DATA
- ssl->options.pskIdIndex = idx + 1;
- #endif
-
- /* Find the list of identities sent to server. */
- extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
- if (extension == NULL)
- return PSK_KEY_ERROR;
- list = (PreSharedKey*)extension->data;
-
- /* Mark the identity as chosen. */
- for (; list != NULL && idx > 0; idx--)
- list = list->next;
- if (list == NULL)
- return PSK_KEY_ERROR;
- list->chosen = 1;
-
- #ifdef HAVE_SESSION_TICKET
- if (list->resumption) {
- /* Check that the session's details are the same as the server's. */
- if (ssl->options.cipherSuite0 != ssl->session.cipherSuite0 ||
- ssl->options.cipherSuite != ssl->session.cipherSuite ||
- ssl->session.version.major != ssl->ctx->method->version.major ||
- ssl->session.version.minor != ssl->ctx->method->version.minor) {
- return PSK_KEY_ERROR;
- }
- }
- #endif
-
- return 0;
- }
-
- return SANITY_MSG_E;
-}
-
-/* Create a new pre-shared key and put it into the list.
- *
- * list The linked list of pre-shared key.
- * identity The identity.
- * len The length of the identity data.
- * heap The memory to allocate with.
- * preSharedKey The new pre-shared key object.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_PreSharedKey_New(PreSharedKey** list, byte* identity,
- word16 len, void *heap,
- PreSharedKey** preSharedKey)
-{
- PreSharedKey* psk;
- PreSharedKey** next;
-
- psk = (PreSharedKey*)XMALLOC(sizeof(PreSharedKey), heap, DYNAMIC_TYPE_TLSX);
- if (psk == NULL)
- return MEMORY_E;
- XMEMSET(psk, 0, sizeof(*psk));
-
- /* Make a copy of the identity data. */
- psk->identity = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_TLSX);
- if (psk->identity == NULL) {
- XFREE(psk, heap, DYNAMIC_TYPE_TLSX);
- return MEMORY_E;
- }
- XMEMCPY(psk->identity, identity, len);
- psk->identityLen = len;
-
- /* Add it to the end and maintain the links. */
- while (*list != NULL) {
- /* Assign to temporary to work around compiler bug found by customer. */
- next = &((*list)->next);
- list = next;
- }
- *list = psk;
- *preSharedKey = psk;
-
- (void)heap;
-
- return 0;
-}
-
-static WC_INLINE byte GetHmacLength(int hmac)
-{
- switch (hmac) {
- #ifndef NO_SHA256
- case sha256_mac:
- return WC_SHA256_DIGEST_SIZE;
- #endif
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- return WC_SHA384_DIGEST_SIZE;
- #endif
- #ifdef WOLFSSL_SHA512
- case sha512_mac:
- return WC_SHA512_DIGEST_SIZE;
- #endif
- }
- return 0;
-}
-
-/* Use the data to create a new pre-shared key object in the extensions.
- *
- * ssl The SSL/TLS object.
- * identity The identity.
- * len The length of the identity data.
- * age The age of the identity.
- * hmac The HMAC algorithm.
- * ciphersuite0 The first byte of the ciphersuite to use.
- * ciphersuite The second byte of the ciphersuite to use.
- * resumption The PSK is for resumption of a session.
- * preSharedKey The new pre-shared key object.
- * returns 0 on success and other values indicate failure.
- */
-int TLSX_PreSharedKey_Use(WOLFSSL* ssl, byte* identity, word16 len, word32 age,
- byte hmac, byte cipherSuite0,
- byte cipherSuite, byte resumption,
- PreSharedKey **preSharedKey)
-{
- int ret = 0;
- TLSX* extension;
- PreSharedKey* psk = NULL;
-
- /* Find the pre-shared key extension if it exists. */
- extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
- if (extension == NULL) {
- /* Push new pre-shared key extension. */
- ret = TLSX_Push(&ssl->extensions, TLSX_PRE_SHARED_KEY, NULL, ssl->heap);
- if (ret != 0)
- return ret;
-
- extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
- if (extension == NULL)
- return MEMORY_E;
- }
-
- /* Try to find the pre-shared key with this identity. */
- psk = (PreSharedKey*)extension->data;
- while (psk != NULL) {
- if ((psk->identityLen == len) &&
- (XMEMCMP(psk->identity, identity, len) == 0)) {
- break;
- }
- psk = psk->next;
- }
-
- /* Create a new pre-shared key object if not found. */
- if (psk == NULL) {
- ret = TLSX_PreSharedKey_New((PreSharedKey**)&extension->data, identity,
- len, ssl->heap, &psk);
- if (ret != 0)
- return ret;
- }
-
- /* Update/set age and HMAC algorithm. */
- psk->ticketAge = age;
- psk->hmac = hmac;
- psk->cipherSuite0 = cipherSuite0;
- psk->cipherSuite = cipherSuite;
- psk->resumption = resumption;
- psk->binderLen = GetHmacLength(psk->hmac);
-
- if (preSharedKey != NULL)
- *preSharedKey = psk;
-
- return 0;
-}
-
-#define PSK_FREE_ALL TLSX_PreSharedKey_FreeAll
-#define PSK_GET_SIZE TLSX_PreSharedKey_GetSize
-#define PSK_WRITE TLSX_PreSharedKey_Write
-#define PSK_PARSE TLSX_PreSharedKey_Parse
-
-#else
-
-#define PSK_FREE_ALL(a, b)
-#define PSK_GET_SIZE(a, b, c) 0
-#define PSK_WRITE(a, b, c, d) 0
-#define PSK_PARSE(a, b, c, d) 0
-
-#endif
-
-/******************************************************************************/
-/* PSK Key Exchange Modes */
-/******************************************************************************/
-
-#if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
-/* Get the size of the encoded PSK KE modes extension.
- * Only in ClientHello.
- *
- * modes The PSK KE mode bit string.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes of the encoded PSK KE mode extension.
- */
-static int TLSX_PskKeModes_GetSize(byte modes, byte msgType, word16* pSz)
-{
- if (msgType == client_hello) {
- /* Format: Len | Modes* */
- word16 len = OPAQUE8_LEN;
- /* Check whether each possible mode is to be written. */
- if (modes & (1 << PSK_KE))
- len += OPAQUE8_LEN;
- if (modes & (1 << PSK_DHE_KE))
- len += OPAQUE8_LEN;
- *pSz += len;
- return 0;
- }
-
- return SANITY_MSG_E;
-}
-
-/* Writes the PSK KE modes extension into the output buffer.
- * Assumes that the the output buffer is big enough to hold data.
- * Only in ClientHello.
- *
- * modes The PSK KE mode bit string.
- * output The buffer to write into.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes written into the buffer.
- */
-static int TLSX_PskKeModes_Write(byte modes, byte* output, byte msgType,
- word16* pSz)
-{
- if (msgType == client_hello) {
- /* Format: Len | Modes* */
- int idx = OPAQUE8_LEN;
-
- /* Write out each possible mode. */
- if (modes & (1 << PSK_KE))
- output[idx++] = PSK_KE;
- if (modes & (1 << PSK_DHE_KE))
- output[idx++] = PSK_DHE_KE;
- /* Write out length of mode list. */
- output[0] = idx - OPAQUE8_LEN;
-
- *pSz += idx;
- return 0;
- }
-
- return SANITY_MSG_E;
-}
-
-/* Parse the PSK KE modes extension.
- * Only in ClientHello.
- *
- * ssl The SSL/TLS object.
- * input The extension data.
- * length The length of the extension data.
- * msgType The type of the message this extension is being parsed from.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_PskKeModes_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte msgType)
-{
- int ret;
-
- if (msgType == client_hello) {
- /* Format: Len | Modes* */
- int idx = 0;
- word16 len;
- byte modes = 0;
-
- /* Ensure length byte exists. */
- if (length < OPAQUE8_LEN)
- return BUFFER_E;
-
- /* Get length of mode list and ensure that is the only data. */
- len = input[0];
- if (length - OPAQUE8_LEN != len)
- return BUFFER_E;
-
- idx = OPAQUE8_LEN;
- /* Set a bit for each recognized modes. */
- while (len > 0) {
- /* Ignore unrecognized modes. */
- if (input[idx] <= PSK_DHE_KE)
- modes |= 1 << input[idx];
- idx++;
- len--;
- }
-
- ret = TLSX_PskKeModes_Use(ssl, modes);
- if (ret != 0)
- return ret;
-
- return 0;
- }
-
- return SANITY_MSG_E;
-}
-
-/* Use the data to create a new PSK Key Exchange Modes object in the extensions.
- *
- * ssl The SSL/TLS object.
- * modes The PSK key exchange modes.
- * returns 0 on success and other values indicate failure.
- */
-int TLSX_PskKeModes_Use(WOLFSSL* ssl, byte modes)
-{
- int ret = 0;
- TLSX* extension;
-
- /* Find the PSK key exchange modes extension if it exists. */
- extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
- if (extension == NULL) {
- /* Push new PSK key exchange modes extension. */
- ret = TLSX_Push(&ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES, NULL,
- ssl->heap);
- if (ret != 0)
- return ret;
-
- extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
- if (extension == NULL)
- return MEMORY_E;
- }
-
- extension->val = modes;
-
- return 0;
-}
-
-#define PKM_GET_SIZE TLSX_PskKeModes_GetSize
-#define PKM_WRITE TLSX_PskKeModes_Write
-#define PKM_PARSE TLSX_PskKeModes_Parse
-
-#else
-
-#define PKM_GET_SIZE(a, b, c) 0
-#define PKM_WRITE(a, b, c, d) 0
-#define PKM_PARSE(a, b, c, d) 0
-
-#endif
-
-/******************************************************************************/
-/* Post-Handshake Authentication */
-/******************************************************************************/
-
-#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
-/* Get the size of the encoded Post-Handshake Authentication extension.
- * Only in ClientHello.
- *
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes of the encoded Post-Handshake Authentication
- * extension.
- */
-static int TLSX_PostHandAuth_GetSize(byte msgType, word16* pSz)
-{
- if (msgType == client_hello) {
- *pSz += 0;
- return 0;
- }
-
- return SANITY_MSG_E;
-}
-
-/* Writes the Post-Handshake Authentication extension into the output buffer.
- * Assumes that the the output buffer is big enough to hold data.
- * Only in ClientHello.
- *
- * output The buffer to write into.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes written into the buffer.
- */
-static int TLSX_PostHandAuth_Write(byte* output, byte msgType, word16* pSz)
-{
- (void)output;
-
- if (msgType == client_hello) {
- *pSz += 0;
- return 0;
- }
-
- return SANITY_MSG_E;
-}
-
-/* Parse the Post-Handshake Authentication extension.
- * Only in ClientHello.
- *
- * ssl The SSL/TLS object.
- * input The extension data.
- * length The length of the extension data.
- * msgType The type of the message this extension is being parsed from.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_PostHandAuth_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte msgType)
-{
- (void)input;
-
- if (msgType == client_hello) {
- /* Ensure extension is empty. */
- if (length != 0)
- return BUFFER_E;
-
- ssl->options.postHandshakeAuth = 1;
- return 0;
- }
-
- return SANITY_MSG_E;
-}
-
-/* Create a new Post-handshake authentication object in the extensions.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_PostHandAuth_Use(WOLFSSL* ssl)
-{
- int ret = 0;
- TLSX* extension;
-
- /* Find the PSK key exchange modes extension if it exists. */
- extension = TLSX_Find(ssl->extensions, TLSX_POST_HANDSHAKE_AUTH);
- if (extension == NULL) {
- /* Push new Post-handshake Authentication extension. */
- ret = TLSX_Push(&ssl->extensions, TLSX_POST_HANDSHAKE_AUTH, NULL,
- ssl->heap);
- if (ret != 0)
- return ret;
- }
-
- return 0;
-}
-
-#define PHA_GET_SIZE TLSX_PostHandAuth_GetSize
-#define PHA_WRITE TLSX_PostHandAuth_Write
-#define PHA_PARSE TLSX_PostHandAuth_Parse
-
-#else
-
-#define PHA_GET_SIZE(a, b) 0
-#define PHA_WRITE(a, b, c) 0
-#define PHA_PARSE(a, b, c, d) 0
-
-#endif
-
-/******************************************************************************/
-/* Early Data Indication */
-/******************************************************************************/
-
-#ifdef WOLFSSL_EARLY_DATA
-/* Get the size of the encoded Early Data Indication extension.
- * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
- *
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes of the encoded Early Data Indication extension.
- */
-static int TLSX_EarlyData_GetSize(byte msgType, word16* pSz)
-{
- int ret = 0;
-
- if (msgType == client_hello || msgType == encrypted_extensions)
- *pSz += 0;
- else if (msgType == session_ticket)
- *pSz += OPAQUE32_LEN;
- else
- ret = SANITY_MSG_E;
-
- return ret;
-}
-
-/* Writes the Early Data Indicator extension into the output buffer.
- * Assumes that the the output buffer is big enough to hold data.
- * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
- *
- * max The maximum early data size.
- * output The buffer to write into.
- * msgType The type of the message this extension is being written into.
- * returns the number of bytes written into the buffer.
- */
-static int TLSX_EarlyData_Write(word32 max, byte* output, byte msgType,
- word16* pSz)
-{
- if (msgType == client_hello || msgType == encrypted_extensions)
- return 0;
- else if (msgType == session_ticket) {
- c32toa(max, output);
- *pSz += OPAQUE32_LEN;
- return 0;
- }
-
- return SANITY_MSG_E;
-}
-
-/* Parse the Early Data Indicator extension.
- * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
- *
- * ssl The SSL/TLS object.
- * input The extension data.
- * length The length of the extension data.
- * msgType The type of the message this extension is being parsed from.
- * returns 0 on success and other values indicate failure.
- */
-static int TLSX_EarlyData_Parse(WOLFSSL* ssl, byte* input, word16 length,
- byte msgType)
-{
- if (msgType == client_hello) {
- if (length != 0)
- return BUFFER_E;
-
- if (ssl->earlyData == expecting_early_data)
- return TLSX_EarlyData_Use(ssl, 0);
- ssl->earlyData = early_data_ext;
- return 0;
- }
- if (msgType == encrypted_extensions) {
- if (length != 0)
- return BUFFER_E;
-
- /* Ensure the index of PSK identity chosen by server is 0.
- * Index is plus one to handle 'not set' value of 0.
- */
- if (ssl->options.pskIdIndex != 1)
- return PSK_KEY_ERROR;
-
- return TLSX_EarlyData_Use(ssl, 1);
- }
- if (msgType == session_ticket) {
- word32 maxSz;
-
- if (length != OPAQUE32_LEN)
- return BUFFER_E;
- ato32(input, &maxSz);
-
- ssl->session.maxEarlyDataSz = maxSz;
- return 0;
- }
-
- return SANITY_MSG_E;
-}
-
-/* Use the data to create a new Early Data object in the extensions.
- *
- * ssl The SSL/TLS object.
- * max The maximum early data size.
- * returns 0 on success and other values indicate failure.
- */
-int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 max)
-{
- int ret = 0;
- TLSX* extension;
-
- /* Find the early data extension if it exists. */
- extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
- if (extension == NULL) {
- /* Push new early data extension. */
- ret = TLSX_Push(&ssl->extensions, TLSX_EARLY_DATA, NULL, ssl->heap);
- if (ret != 0)
- return ret;
-
- extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
- if (extension == NULL)
- return MEMORY_E;
- }
-
- extension->resp = 1;
- extension->val = max;
-
- return 0;
-}
-
-#define EDI_GET_SIZE TLSX_EarlyData_GetSize
-#define EDI_WRITE TLSX_EarlyData_Write
-#define EDI_PARSE TLSX_EarlyData_Parse
-
-#else
-
-#define EDI_GET_SIZE(a, b) 0
-#define EDI_WRITE(a, b, c, d) 0
-#define EDI_PARSE(a, b, c, d) 0
-
-#endif
-
-/******************************************************************************/
-/* TLS Extensions Framework */
-/******************************************************************************/
-
-/** Finds an extension in the provided list. */
-TLSX* TLSX_Find(TLSX* list, TLSX_Type type)
-{
- TLSX* extension = list;
-
- while (extension && extension->type != type)
- extension = extension->next;
-
- return extension;
-}
-
-/** Remove an extension. */
-void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap)
-{
- TLSX* extension = *list;
- TLSX** next = list;
-
- while (extension && extension->type != type) {
- next = &extension->next;
- extension = extension->next;
- }
-
- if (extension) {
- *next = extension->next;
- extension->next = NULL;
- TLSX_FreeAll(extension, heap);
- }
-}
-
-/** Releases all extensions in the provided list. */
-void TLSX_FreeAll(TLSX* list, void* heap)
-{
- TLSX* extension;
-
- while ((extension = list)) {
- list = extension->next;
-
- switch (extension->type) {
-
- case TLSX_SERVER_NAME:
- SNI_FREE_ALL((SNI*)extension->data, heap);
- break;
-
- case TLSX_TRUSTED_CA_KEYS:
- TCA_FREE_ALL((TCA*)extension->data, heap);
- break;
-
- case TLSX_MAX_FRAGMENT_LENGTH:
- MFL_FREE_ALL(extension->data, heap);
- break;
-
- case TLSX_TRUNCATED_HMAC:
- /* Nothing to do. */
- break;
-
- case TLSX_SUPPORTED_GROUPS:
- EC_FREE_ALL((SupportedCurve*)extension->data, heap);
- break;
-
- case TLSX_EC_POINT_FORMATS:
- PF_FREE_ALL((PointFormat*)extension->data, heap);
- break;
-
- case TLSX_STATUS_REQUEST:
- CSR_FREE_ALL((CertificateStatusRequest*)extension->data, heap);
- break;
-
- case TLSX_STATUS_REQUEST_V2:
- CSR2_FREE_ALL((CertificateStatusRequestItemV2*)extension->data,
- heap);
- break;
-
- case TLSX_RENEGOTIATION_INFO:
- SCR_FREE_ALL(extension->data, heap);
- break;
-
- case TLSX_SESSION_TICKET:
- WOLF_STK_FREE(extension->data, heap);
- break;
-
- case TLSX_QUANTUM_SAFE_HYBRID:
- QSH_FREE_ALL((QSHScheme*)extension->data, heap);
- break;
-
- case TLSX_APPLICATION_LAYER_PROTOCOL:
- ALPN_FREE_ALL((ALPN*)extension->data, heap);
- break;
-#if !defined(WOLFSSL_NO_SIGALG)
- case TLSX_SIGNATURE_ALGORITHMS:
- break;
-#endif
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- case TLSX_ENCRYPT_THEN_MAC:
- break;
-#endif
-#ifdef WOLFSSL_TLS13
- case TLSX_SUPPORTED_VERSIONS:
- break;
-
- case TLSX_COOKIE:
- CKE_FREE_ALL((Cookie*)extension->data, heap);
- break;
-
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- case TLSX_PRE_SHARED_KEY:
- PSK_FREE_ALL((PreSharedKey*)extension->data, heap);
- break;
-
- case TLSX_PSK_KEY_EXCHANGE_MODES:
- break;
- #endif
-
- #ifdef WOLFSSL_EARLY_DATA
- case TLSX_EARLY_DATA:
- break;
- #endif
-
- #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- case TLSX_POST_HANDSHAKE_AUTH:
- break;
- #endif
-
- #if !defined(WOLFSSL_TLS13_DRAFT_18) && !defined(WOLFSSL_TLS13_DRAFT_22)
- case TLSX_SIGNATURE_ALGORITHMS_CERT:
- break;
- #endif
-
- case TLSX_KEY_SHARE:
- KS_FREE_ALL((KeyShareEntry*)extension->data, heap);
- break;
-#endif
- }
-
- XFREE(extension, heap, DYNAMIC_TYPE_TLSX);
- }
-
- (void)heap;
-}
-
-/** Checks if the tls extensions are supported based on the protocol version. */
-int TLSX_SupportExtensions(WOLFSSL* ssl) {
- return ssl && (IsTLS(ssl) || ssl->version.major == DTLS_MAJOR);
-}
-
-/** Tells the buffered size of the extensions in a list. */
-static int TLSX_GetSize(TLSX* list, byte* semaphore, byte msgType,
- word16* pLength)
-{
- int ret = 0;
- TLSX* extension;
- word16 length = 0;
- byte isRequest = (msgType == client_hello ||
- msgType == certificate_request);
-
- while ((extension = list)) {
- list = extension->next;
-
- /* only extensions marked as response are sent back to the client. */
- if (!isRequest && !extension->resp)
- continue; /* skip! */
-
- /* ssl level extensions are expected to override ctx level ones. */
- if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
- continue; /* skip! */
-
- /* extension type + extension data length. */
- length += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
-
-
- switch (extension->type) {
-
- case TLSX_SERVER_NAME:
- /* SNI only sends the name on the request. */
- if (isRequest)
- length += SNI_GET_SIZE((SNI*)extension->data);
- break;
-
- case TLSX_TRUSTED_CA_KEYS:
- /* TCA only sends the list on the request. */
- if (isRequest)
- length += TCA_GET_SIZE((TCA*)extension->data);
- break;
-
- case TLSX_MAX_FRAGMENT_LENGTH:
- length += MFL_GET_SIZE(extension->data);
- break;
-
- case TLSX_TRUNCATED_HMAC:
- /* always empty. */
- break;
-
- case TLSX_SUPPORTED_GROUPS:
- length += EC_GET_SIZE((SupportedCurve*)extension->data);
- break;
-
- case TLSX_EC_POINT_FORMATS:
- length += PF_GET_SIZE((PointFormat*)extension->data);
- break;
-
- case TLSX_STATUS_REQUEST:
- length += CSR_GET_SIZE(
- (CertificateStatusRequest*)extension->data, isRequest);
- break;
-
- case TLSX_STATUS_REQUEST_V2:
- length += CSR2_GET_SIZE(
- (CertificateStatusRequestItemV2*)extension->data,
- isRequest);
- break;
-
- case TLSX_RENEGOTIATION_INFO:
- length += SCR_GET_SIZE((SecureRenegotiation*)extension->data,
- isRequest);
- break;
-
- case TLSX_SESSION_TICKET:
- length += WOLF_STK_GET_SIZE((SessionTicket*)extension->data,
- isRequest);
- break;
-
- case TLSX_QUANTUM_SAFE_HYBRID:
- length += QSH_GET_SIZE((QSHScheme*)extension->data, isRequest);
- break;
-
- case TLSX_APPLICATION_LAYER_PROTOCOL:
- length += ALPN_GET_SIZE((ALPN*)extension->data);
- break;
-#if !defined(WOLFSSL_NO_SIGALG)
- case TLSX_SIGNATURE_ALGORITHMS:
- length += SA_GET_SIZE(extension->data);
- break;
-#endif
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- case TLSX_ENCRYPT_THEN_MAC:
- ret = ETM_GET_SIZE(msgType, &length);
- break;
-#endif /* HAVE_ENCRYPT_THEN_MAC */
-#ifdef WOLFSSL_TLS13
- case TLSX_SUPPORTED_VERSIONS:
- ret = SV_GET_SIZE(extension->data, msgType, &length);
- break;
-
- case TLSX_COOKIE:
- ret = CKE_GET_SIZE((Cookie*)extension->data, msgType, &length);
- break;
-
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- case TLSX_PRE_SHARED_KEY:
- ret = PSK_GET_SIZE((PreSharedKey*)extension->data, msgType,
- &length);
- break;
-
- case TLSX_PSK_KEY_EXCHANGE_MODES:
- ret = PKM_GET_SIZE(extension->val, msgType, &length);
- break;
- #endif
-
- #ifdef WOLFSSL_EARLY_DATA
- case TLSX_EARLY_DATA:
- ret = EDI_GET_SIZE(msgType, &length);
- break;
- #endif
-
- #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- case TLSX_POST_HANDSHAKE_AUTH:
- ret = PHA_GET_SIZE(msgType, &length);
- break;
- #endif
-
- #if !defined(WOLFSSL_TLS13_DRAFT_18) && !defined(WOLFSSL_TLS13_DRAFT_22)
- case TLSX_SIGNATURE_ALGORITHMS_CERT:
- length += SAC_GET_SIZE(extension->data);
- break;
- #endif
-
- case TLSX_KEY_SHARE:
- length += KS_GET_SIZE((KeyShareEntry*)extension->data, msgType);
- break;
-#endif
- }
-
- /* marks the extension as processed so ctx level */
- /* extensions don't overlap with ssl level ones. */
- TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
- }
-
- *pLength += length;
-
- return ret;
-}
-
-/** Writes the extensions of a list in a buffer. */
-static int TLSX_Write(TLSX* list, byte* output, byte* semaphore,
- byte msgType, word16* pOffset)
-{
- int ret = 0;
- TLSX* extension;
- word16 offset = 0;
- word16 length_offset = 0;
- byte isRequest = (msgType == client_hello ||
- msgType == certificate_request);
-
- while ((extension = list)) {
- list = extension->next;
-
- /* only extensions marked as response are written in a response. */
- if (!isRequest && !extension->resp)
- continue; /* skip! */
-
- /* ssl level extensions are expected to override ctx level ones. */
- if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
- continue; /* skip! */
-
- /* writes extension type. */
- c16toa(extension->type, output + offset);
- offset += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
- length_offset = offset;
-
- /* extension data should be written internally. */
- switch (extension->type) {
- case TLSX_SERVER_NAME:
- if (isRequest) {
- WOLFSSL_MSG("SNI extension to write");
- offset += SNI_WRITE((SNI*)extension->data, output + offset);
- }
- break;
-
- case TLSX_TRUSTED_CA_KEYS:
- WOLFSSL_MSG("Trusted CA Indication extension to write");
- if (isRequest) {
- offset += TCA_WRITE((TCA*)extension->data, output + offset);
- }
- break;
-
- case TLSX_MAX_FRAGMENT_LENGTH:
- WOLFSSL_MSG("Max Fragment Length extension to write");
- offset += MFL_WRITE((byte*)extension->data, output + offset);
- break;
-
- case TLSX_TRUNCATED_HMAC:
- WOLFSSL_MSG("Truncated HMAC extension to write");
- /* always empty. */
- break;
-
- case TLSX_SUPPORTED_GROUPS:
- WOLFSSL_MSG("Supported Groups extension to write");
- offset += EC_WRITE((SupportedCurve*)extension->data,
- output + offset);
- break;
-
- case TLSX_EC_POINT_FORMATS:
- WOLFSSL_MSG("Point Formats extension to write");
- offset += PF_WRITE((PointFormat*)extension->data,
- output + offset);
- break;
-
- case TLSX_STATUS_REQUEST:
- WOLFSSL_MSG("Certificate Status Request extension to write");
- offset += CSR_WRITE((CertificateStatusRequest*)extension->data,
- output + offset, isRequest);
- break;
-
- case TLSX_STATUS_REQUEST_V2:
- WOLFSSL_MSG("Certificate Status Request v2 extension to write");
- offset += CSR2_WRITE(
- (CertificateStatusRequestItemV2*)extension->data,
- output + offset, isRequest);
- break;
-
- case TLSX_RENEGOTIATION_INFO:
- WOLFSSL_MSG("Secure Renegotiation extension to write");
- offset += SCR_WRITE((SecureRenegotiation*)extension->data,
- output + offset, isRequest);
- break;
-
- case TLSX_SESSION_TICKET:
- WOLFSSL_MSG("Session Ticket extension to write");
- offset += WOLF_STK_WRITE((SessionTicket*)extension->data,
- output + offset, isRequest);
- break;
-
- case TLSX_QUANTUM_SAFE_HYBRID:
- WOLFSSL_MSG("Quantum-Safe-Hybrid extension to write");
- if (isRequest) {
- offset += QSH_WRITE((QSHScheme*)extension->data, output + offset);
- }
- offset += QSHPK_WRITE((QSHScheme*)extension->data, output + offset);
- offset += QSH_SERREQ(output + offset, isRequest);
- break;
-
- case TLSX_APPLICATION_LAYER_PROTOCOL:
- WOLFSSL_MSG("ALPN extension to write");
- offset += ALPN_WRITE((ALPN*)extension->data, output + offset);
- break;
-#if !defined(WOLFSSL_NO_SIGALG)
- case TLSX_SIGNATURE_ALGORITHMS:
- WOLFSSL_MSG("Signature Algorithms extension to write");
- offset += SA_WRITE(extension->data, output + offset);
- break;
-#endif
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- case TLSX_ENCRYPT_THEN_MAC:
- WOLFSSL_MSG("Encrypt-Then-Mac extension to write");
- ret = ETM_WRITE(extension->data, output, msgType, &offset);
- break;
-#endif /* HAVE_ENCRYPT_THEN_MAC */
-#ifdef WOLFSSL_TLS13
- case TLSX_SUPPORTED_VERSIONS:
- WOLFSSL_MSG("Supported Versions extension to write");
- ret = SV_WRITE(extension->data, output + offset, msgType, &offset);
- break;
-
- case TLSX_COOKIE:
- WOLFSSL_MSG("Cookie extension to write");
- ret = CKE_WRITE((Cookie*)extension->data, output + offset,
- msgType, &offset);
- break;
-
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- case TLSX_PRE_SHARED_KEY:
- WOLFSSL_MSG("Pre-Shared Key extension to write");
- ret = PSK_WRITE((PreSharedKey*)extension->data, output + offset,
- msgType, &offset);
- break;
-
- case TLSX_PSK_KEY_EXCHANGE_MODES:
- WOLFSSL_MSG("PSK Key Exchange Modes extension to write");
- ret = PKM_WRITE(extension->val, output + offset, msgType,
- &offset);
- break;
- #endif
-
- #ifdef WOLFSSL_EARLY_DATA
- case TLSX_EARLY_DATA:
- WOLFSSL_MSG("Early Data extension to write");
- ret = EDI_WRITE(extension->val, output + offset, msgType,
- &offset);
- break;
- #endif
-
- #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- case TLSX_POST_HANDSHAKE_AUTH:
- WOLFSSL_MSG("Post-Handshake Authentication extension to write");
- ret = PHA_WRITE(output + offset, msgType, &offset);
- break;
- #endif
-
- #if !defined(WOLFSSL_TLS13_DRAFT_18) && !defined(WOLFSSL_TLS13_DRAFT_22)
- case TLSX_SIGNATURE_ALGORITHMS_CERT:
- WOLFSSL_MSG("Signature Algorithms extension to write");
- offset += SAC_WRITE(extension->data, output + offset);
- break;
- #endif
-
- case TLSX_KEY_SHARE:
- WOLFSSL_MSG("Key Share extension to write");
- offset += KS_WRITE((KeyShareEntry*)extension->data,
- output + offset, msgType);
- break;
-#endif
- }
-
- /* writes extension data length. */
- c16toa(offset - length_offset, output + length_offset - OPAQUE16_LEN);
-
- /* marks the extension as processed so ctx level */
- /* extensions don't overlap with ssl level ones. */
- TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
- }
-
- *pOffset += offset;
-
- return ret;
-}
-
-
-#if defined(HAVE_NTRU) && defined(HAVE_QSH)
-
-static word32 GetEntropy(unsigned char* out, word32 num_bytes)
-{
- int ret = 0;
-
- if (gRng == NULL) {
- if ((gRng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL,
- DYNAMIC_TYPE_TLSX)) == NULL)
- return DRBG_OUT_OF_MEMORY;
- wc_InitRng(gRng);
- }
-
- if (gRngMutex == NULL) {
- if ((gRngMutex = (wolfSSL_Mutex*)XMALLOC(sizeof(wolfSSL_Mutex), NULL,
- DYNAMIC_TYPE_TLSX)) == NULL)
- return DRBG_OUT_OF_MEMORY;
- wc_InitMutex(gRngMutex);
- }
-
- ret |= wc_LockMutex(gRngMutex);
- ret |= wc_RNG_GenerateBlock(gRng, out, num_bytes);
- ret |= wc_UnLockMutex(gRngMutex);
-
- if (ret != 0)
- return DRBG_ENTROPY_FAIL;
-
- return DRBG_OK;
-}
-#endif
-
-
-#ifdef HAVE_QSH
-static int TLSX_CreateQSHKey(WOLFSSL* ssl, int type)
-{
- int ret = -1;
-
- (void)ssl;
-
- switch (type) {
-#ifdef HAVE_NTRU
- case WOLFSSL_NTRU_EESS439:
- case WOLFSSL_NTRU_EESS593:
- case WOLFSSL_NTRU_EESS743:
- ret = TLSX_CreateNtruKey(ssl, type);
- break;
-#endif
- default:
- WOLFSSL_MSG("Unknown type for creating NTRU key");
- break;
- }
-
- return ret;
-}
-
-
-static int TLSX_AddQSHKey(QSHKey** list, QSHKey* key)
-{
- QSHKey* current;
-
- if (key == NULL)
- return BAD_FUNC_ARG;
-
- /* if no public key stored in key then do not add */
- if (key->pub.length == 0 || key->pub.buffer == NULL)
- return 0;
-
- /* first element to be added to the list */
- current = *list;
- if (current == NULL) {
- *list = key;
- return 0;
- }
-
- while (current->next) {
- /* can only have one of the key in the list */
- if (current->name == key->name)
- return -1;
- current = (QSHKey*)current->next;
- }
-
- current->next = (struct QSHKey*)key;
-
- return 0;
-}
-
-
-#if defined(HAVE_NTRU)
-int TLSX_CreateNtruKey(WOLFSSL* ssl, int type)
-{
- int ret = -1;
- int ntruType;
-
- /* variable declarations for NTRU*/
- QSHKey* temp = NULL;
- byte public_key[1027];
- word16 public_key_len = sizeof(public_key);
- byte private_key[1120];
- word16 private_key_len = sizeof(private_key);
- DRBG_HANDLE drbg;
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- switch (type) {
- case WOLFSSL_NTRU_EESS439:
- ntruType = NTRU_EES439EP1;
- break;
- case WOLFSSL_NTRU_EESS593:
- ntruType = NTRU_EES593EP1;
- break;
- case WOLFSSL_NTRU_EESS743:
- ntruType = NTRU_EES743EP1;
- break;
- default:
- WOLFSSL_MSG("Unknown type for creating NTRU key");
- return -1;
- }
- ret = ntru_crypto_drbg_external_instantiate(GetEntropy, &drbg);
- if (ret != DRBG_OK) {
- WOLFSSL_MSG("NTRU drbg instantiate failed\n");
- return ret;
- }
-
- if ((ret = ntru_crypto_ntru_encrypt_keygen(drbg, ntruType,
- &public_key_len, NULL, &private_key_len, NULL)) != NTRU_OK)
- return ret;
-
- if ((ret = ntru_crypto_ntru_encrypt_keygen(drbg, ntruType,
- &public_key_len, public_key, &private_key_len, private_key)) != NTRU_OK)
- return ret;
-
- ret = ntru_crypto_drbg_uninstantiate(drbg);
- if (ret != NTRU_OK) {
- WOLFSSL_MSG("NTRU drbg uninstantiate failed\n");
- return ret;
- }
-
- if ((temp = (QSHKey*)XMALLOC(sizeof(QSHKey), ssl->heap,
- DYNAMIC_TYPE_TLSX)) == NULL)
- return MEMORY_E;
- temp->name = type;
- temp->pub.length = public_key_len;
- temp->pub.buffer = (byte*)XMALLOC(public_key_len, ssl->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- XMEMCPY(temp->pub.buffer, public_key, public_key_len);
- temp->pri.length = private_key_len;
- temp->pri.buffer = (byte*)XMALLOC(private_key_len, ssl->heap,
- DYNAMIC_TYPE_ARRAYS);
- XMEMCPY(temp->pri.buffer, private_key, private_key_len);
- temp->next = NULL;
-
- TLSX_AddQSHKey(&ssl->QSH_Key, temp);
-
- (void)ssl;
- (void)type;
-
- return ret;
-}
-#endif
-
-
-/*
- Used to find a public key from the list of keys
- pubLen length of array
- name input the name of the scheme looking for ie WOLFSSL_NTRU_ESSXXX
-
- returns a pointer to public key byte* or NULL if not found
- */
-static byte* TLSX_QSHKeyFind_Pub(QSHKey* qsh, word16* pubLen, word16 name)
-{
- QSHKey* current = qsh;
-
- if (qsh == NULL || pubLen == NULL)
- return NULL;
-
- *pubLen = 0;
-
- while(current) {
- if (current->name == name) {
- *pubLen = current->pub.length;
- return current->pub.buffer;
- }
- current = (QSHKey*)current->next;
- }
-
- return NULL;
-}
-#endif /* HAVE_QSH */
-
-#if (!defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_TLS13) && \
- !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)) || \
- (defined(WOLFSSL_TLS13) && !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) \
- && !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)) || \
- ((defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES))
-
-/* Populates the default supported groups / curves */
-static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions)
-{
- int ret = WOLFSSL_SUCCESS;
-#ifdef WOLFSSL_TLS13
- int i;
-
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- if (ssl->options.resuming && ssl->session.namedGroup != 0) {
- return TLSX_UseSupportedCurve(extensions, ssl->session.namedGroup,
- ssl->heap);
- }
-#endif
-
- if (ssl->numGroups != 0) {
- for (i = 0; i < ssl->numGroups; i++) {
- ret = TLSX_UseSupportedCurve(extensions, ssl->group[i], ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- }
- return WOLFSSL_SUCCESS;
- }
-#endif /* WOLFSSL_TLS13 */
-
-#if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
- /* list in order by strength, since not all servers choose by strength */
- #if defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP521R1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #endif
- #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
- #ifdef HAVE_ECC_BRAINPOOL
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_BRAINPOOLP512R1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #endif
- #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP384R1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #ifdef HAVE_ECC_BRAINPOOL
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_BRAINPOOLP384R1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #endif
-#endif /* HAVE_ECC && HAVE_SUPPORTED_CURVES */
-
- #ifndef HAVE_FIPS
- #if defined(HAVE_CURVE448)
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_X448, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #endif /* HAVE_FIPS */
-
-#if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
- #if !defined(NO_ECC256) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP256R1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #ifdef HAVE_ECC_KOBLITZ
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP256K1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #ifdef HAVE_ECC_BRAINPOOL
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_BRAINPOOLP256R1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #endif
-#endif /* HAVE_ECC && HAVE_SUPPORTED_CURVES */
-
- #ifndef HAVE_FIPS
- #if defined(HAVE_CURVE25519)
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_X25519, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #endif /* HAVE_FIPS */
-
-#if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
- #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP224R1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #ifdef HAVE_ECC_KOBLITZ
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP224K1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #endif
-
- #ifndef HAVE_FIPS
- #if defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP192R1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #ifdef HAVE_ECC_KOBLITZ
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP192K1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #endif
- #if defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)
- #ifndef NO_ECC_SECP
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP160R1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #ifdef HAVE_ECC_SECPR2
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP160R2, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #ifdef HAVE_ECC_KOBLITZ
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_ECC_SECP160K1, ssl->heap);
- if (ret != WOLFSSL_SUCCESS) return ret;
- #endif
- #endif
- #endif /* HAVE_FIPS */
-#endif /* HAVE_ECC && HAVE_SUPPORTED_CURVES */
-
- /* Add FFDHE supported groups. */
- #ifdef HAVE_FFDHE_8192
- if (8192/8 >= ssl->options.minDhKeySz &&
- 8192/8 <= ssl->options.maxDhKeySz) {
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_FFDHE_8192, ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- }
- #endif
- #ifdef HAVE_FFDHE_6144
- if (6144/8 >= ssl->options.minDhKeySz &&
- 6144/8 <= ssl->options.maxDhKeySz) {
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_FFDHE_6144, ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- }
- #endif
- #ifdef HAVE_FFDHE_4096
- if (4096/8 >= ssl->options.minDhKeySz &&
- 4096/8 <= ssl->options.maxDhKeySz) {
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_FFDHE_4096, ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- }
- #endif
- #ifdef HAVE_FFDHE_3072
- if (3072/8 >= ssl->options.minDhKeySz &&
- 3072/8 <= ssl->options.maxDhKeySz) {
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_FFDHE_3072, ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- }
- #endif
- #ifdef HAVE_FFDHE_2048
- if (2048/8 >= ssl->options.minDhKeySz &&
- 2048/8 <= ssl->options.maxDhKeySz) {
- ret = TLSX_UseSupportedCurve(extensions,
- WOLFSSL_FFDHE_2048, ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- }
- #endif
-
- (void)ssl;
- (void)extensions;
-
- return ret;
-}
-
-#endif
-
-int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
-{
- int ret = 0;
- byte* public_key = NULL;
- word16 public_key_len = 0;
-#if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
- int usingPSK = 0;
-#endif
-#ifdef HAVE_QSH
- TLSX* extension;
- QSHScheme* qsh;
- QSHScheme* next;
-
- /* add supported QSHSchemes */
- WOLFSSL_MSG("Adding supported QSH Schemes");
-#endif
-
- /* server will add extension depending on what is parsed from client */
- if (!isServer) {
-#ifdef HAVE_QSH
- /* test if user has set a specific scheme already */
- if (!ssl->user_set_QSHSchemes) {
- if (ssl->sendQSHKeys && ssl->QSH_Key == NULL) {
- if ((ret = TLSX_CreateQSHKey(ssl, WOLFSSL_NTRU_EESS743)) != 0) {
- WOLFSSL_MSG("Error creating ntru keys");
- return ret;
- }
- if ((ret = TLSX_CreateQSHKey(ssl, WOLFSSL_NTRU_EESS593)) != 0) {
- WOLFSSL_MSG("Error creating ntru keys");
- return ret;
- }
- if ((ret = TLSX_CreateQSHKey(ssl, WOLFSSL_NTRU_EESS439)) != 0) {
- WOLFSSL_MSG("Error creating ntru keys");
- return ret;
- }
-
- /* add NTRU 256 */
- public_key = TLSX_QSHKeyFind_Pub(ssl->QSH_Key,
- &public_key_len, WOLFSSL_NTRU_EESS743);
- }
- if (TLSX_UseQSHScheme(&ssl->extensions, WOLFSSL_NTRU_EESS743,
- public_key, public_key_len, ssl->heap)
- != WOLFSSL_SUCCESS)
- ret = -1;
-
- /* add NTRU 196 */
- if (ssl->sendQSHKeys) {
- public_key = TLSX_QSHKeyFind_Pub(ssl->QSH_Key,
- &public_key_len, WOLFSSL_NTRU_EESS593);
- }
- if (TLSX_UseQSHScheme(&ssl->extensions, WOLFSSL_NTRU_EESS593,
- public_key, public_key_len, ssl->heap)
- != WOLFSSL_SUCCESS)
- ret = -1;
-
- /* add NTRU 128 */
- if (ssl->sendQSHKeys) {
- public_key = TLSX_QSHKeyFind_Pub(ssl->QSH_Key,
- &public_key_len, WOLFSSL_NTRU_EESS439);
- }
- if (TLSX_UseQSHScheme(&ssl->extensions, WOLFSSL_NTRU_EESS439,
- public_key, public_key_len, ssl->heap)
- != WOLFSSL_SUCCESS)
- ret = -1;
- }
- else if (ssl->sendQSHKeys && ssl->QSH_Key == NULL) {
- /* for each scheme make a client key */
- extension = TLSX_Find(ssl->extensions, TLSX_QUANTUM_SAFE_HYBRID);
- if (extension) {
- qsh = (QSHScheme*)extension->data;
-
- while (qsh) {
- if ((ret = TLSX_CreateQSHKey(ssl, qsh->name)) != 0)
- return ret;
-
- /* get next now because qsh could be freed */
- next = qsh->next;
-
- /* find the public key created and add to extension*/
- public_key = TLSX_QSHKeyFind_Pub(ssl->QSH_Key,
- &public_key_len, qsh->name);
- if (TLSX_UseQSHScheme(&ssl->extensions, qsh->name,
- public_key, public_key_len,
- ssl->heap) != WOLFSSL_SUCCESS)
- ret = -1;
- qsh = next;
- }
- }
- }
-#endif
-
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- if (!ssl->options.disallowEncThenMac) {
- ret = TLSX_EncryptThenMac_Use(ssl);
- if (ret != 0)
- return ret;
- }
-#endif
-
-#if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
- defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES)
- if (!ssl->options.userCurves && !ssl->ctx->userCurves) {
- if (TLSX_Find(ssl->ctx->extensions,
- TLSX_SUPPORTED_GROUPS) == NULL) {
- ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- }
- }
- if ((!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade) &&
- TLSX_Find(ssl->ctx->extensions, TLSX_EC_POINT_FORMATS) == NULL &&
- TLSX_Find(ssl->extensions, TLSX_EC_POINT_FORMATS) == NULL) {
- ret = TLSX_UsePointFormat(&ssl->extensions,
- WOLFSSL_EC_PF_UNCOMPRESSED, ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- }
-#endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
- } /* is not server */
-
-#if !defined(WOLFSSL_NO_SIGALG)
- WOLFSSL_MSG("Adding signature algorithms extension");
- if ((ret = TLSX_SetSignatureAlgorithms(&ssl->extensions, ssl, ssl->heap))
- != 0) {
- return ret;
- }
-#else
- ret = 0;
-#endif
- #ifdef WOLFSSL_TLS13
- if (!isServer && IsAtLeastTLSv1_3(ssl->version)) {
- /* Add mandatory TLS v1.3 extension: supported version */
- WOLFSSL_MSG("Adding supported versions extension");
- if ((ret = TLSX_SetSupportedVersions(&ssl->extensions, ssl,
- ssl->heap)) != 0) {
- return ret;
- }
-
- #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && \
- !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)
- if (TLSX_Find(ssl->ctx->extensions, TLSX_SUPPORTED_GROUPS) == NULL) {
- /* Put in DH groups for TLS 1.3 only. */
- ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
- ret = 0;
- }
- #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
-
- #if !defined(WOLFSSL_TLS13_DRAFT_18) && !defined(WOLFSSL_TLS13_DRAFT_22)
- if (ssl->certHashSigAlgoSz > 0) {
- WOLFSSL_MSG("Adding signature algorithms cert extension");
- if ((ret = TLSX_SetSignatureAlgorithmsCert(&ssl->extensions,
- ssl, ssl->heap)) != 0) {
- return ret;
- }
- }
- #endif /* !WOLFSSL_TLS13_DRAFT_18 && !WOLFSSL_TLS13_DRAFT_22 */
-
- if (TLSX_Find(ssl->extensions, TLSX_KEY_SHARE) == NULL) {
- word16 namedGroup;
-
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- if (ssl->options.resuming && ssl->session.namedGroup != 0)
- namedGroup = ssl->session.namedGroup;
- else
- #endif
- {
- #if defined(HAVE_ECC) && (!defined(NO_ECC256) || \
- defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP)
- namedGroup = WOLFSSL_ECC_SECP256R1;
- #elif defined(HAVE_CURVE25519)
- namedGroup = WOLFSSL_ECC_X25519;
- #elif defined(HAVE_CURVE448)
- namedGroup = WOLFSSL_ECC_X448;
- #elif defined(HAVE_ECC) && (!defined(NO_ECC384) || \
- defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP)
- namedGroup = WOLFSSL_ECC_SECP384R1;
- #elif defined(HAVE_ECC) && (!defined(NO_ECC521) || \
- defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP)
- namedGroup = WOLFSSL_ECC_SECP521R1;
- #elif defined(HAVE_FFDHE_2048)
- namedGroup = WOLFSSL_FFDHE_2048;
- #elif defined(HAVE_FFDHE_3072)
- namedGroup = WOLFSSL_FFDHE_3072;
- #elif defined(HAVE_FFDHE_4096)
- namedGroup = WOLFSSL_FFDHE_4096;
- #elif defined(HAVE_FFDHE_6144)
- namedGroup = WOLFSSL_FFDHE_6144;
- #elif defined(HAVE_FFDHE_8192)
- namedGroup = WOLFSSL_FFDHE_8192;
- #else
- return KEY_SHARE_ERROR;
- #endif
- }
- ret = TLSX_KeyShare_Use(ssl, namedGroup, 0, NULL, NULL);
- if (ret != 0)
- return ret;
- }
-
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
- #endif
- #if defined(HAVE_SESSION_TICKET)
- if (ssl->options.resuming && ssl->session.ticketLen > 0) {
- WOLFSSL_SESSION* sess = &ssl->session;
- word32 milli;
-
- if (sess->ticketLen > MAX_PSK_ID_LEN) {
- WOLFSSL_MSG("Session ticket length for PSK ext is too large");
- return BUFFER_ERROR;
- }
-
- /* Determine the MAC algorithm for the cipher suite used. */
- ssl->options.cipherSuite0 = sess->cipherSuite0;
- ssl->options.cipherSuite = sess->cipherSuite;
- ret = SetCipherSpecs(ssl);
- if (ret != 0)
- return ret;
- milli = TimeNowInMilliseconds() - sess->ticketSeen +
- sess->ticketAdd;
- /* Pre-shared key is mandatory extension for resumption. */
- ret = TLSX_PreSharedKey_Use(ssl, sess->ticket, sess->ticketLen,
- milli, ssl->specs.mac_algorithm,
- ssl->options.cipherSuite0,
- ssl->options.cipherSuite, 1,
- NULL);
- if (ret != 0)
- return ret;
-
- usingPSK = 1;
- }
- #endif
- #ifndef NO_PSK
- if (ssl->options.client_psk_cb != NULL ||
- ssl->options.client_psk_tls13_cb != NULL) {
- /* Default ciphersuite. */
- byte cipherSuite0 = TLS13_BYTE;
- byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
- const char* cipherName = NULL;
-
- if (ssl->options.client_psk_tls13_cb != NULL) {
- ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(
- ssl, ssl->arrays->server_hint,
- ssl->arrays->client_identity, MAX_PSK_ID_LEN,
- ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
- if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
- &cipherSuite) != 0) {
- return PSK_KEY_ERROR;
- }
- }
- else {
- ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
- ssl->arrays->server_hint, ssl->arrays->client_identity,
- MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
- }
- if (ssl->arrays->psk_keySz == 0 ||
- ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
- return PSK_KEY_ERROR;
- }
- ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
- /* TODO: Callback should be able to change ciphersuite. */
- ssl->options.cipherSuite0 = cipherSuite0;
- ssl->options.cipherSuite = cipherSuite;
- ret = SetCipherSpecs(ssl);
- if (ret != 0)
- return ret;
-
- ret = TLSX_PreSharedKey_Use(ssl,
- (byte*)ssl->arrays->client_identity,
- (word16)XSTRLEN(ssl->arrays->client_identity),
- 0, ssl->specs.mac_algorithm,
- cipherSuite0, cipherSuite, 0,
- NULL);
- if (ret != 0)
- return ret;
-
- usingPSK = 1;
- }
- #endif
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- if (usingPSK) {
- byte modes;
-
- /* Pre-shared key modes: mandatory extension for resumption. */
- modes = 1 << PSK_KE;
- #if !defined(NO_DH) || defined(HAVE_ECC) || \
- defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
- if (!ssl->options.noPskDheKe)
- modes |= 1 << PSK_DHE_KE;
- #endif
- ret = TLSX_PskKeModes_Use(ssl, modes);
- if (ret != 0)
- return ret;
- }
- #endif
- #if defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- if (!isServer && ssl->options.postHandshakeAuth) {
- ret = TLSX_PostHandAuth_Use(ssl);
- if (ret != 0)
- return ret;
- }
- #endif
- }
-
- #endif
-
- (void)isServer;
- (void)public_key;
- (void)public_key_len;
- (void)ssl;
-
- return ret;
-}
-
-
-#if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
-
-/** Tells the buffered size of extensions to be sent into the client hello. */
-int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType, word16* pLength)
-{
- int ret = 0;
- word16 length = 0;
- byte semaphore[SEMAPHORE_SIZE] = {0};
-
- if (!TLSX_SupportExtensions(ssl))
- return 0;
- if (msgType == client_hello) {
- EC_VALIDATE_REQUEST(ssl, semaphore);
- PF_VALIDATE_REQUEST(ssl, semaphore);
- QSH_VALIDATE_REQUEST(ssl, semaphore);
- WOLF_STK_VALIDATE_REQUEST(ssl);
-#if !defined(WOLFSSL_NO_SIGALG)
- if (ssl->suites->hashSigAlgoSz == 0)
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
-#endif
-#if defined(WOLFSSL_TLS13)
- if (!IsAtLeastTLSv1_2(ssl))
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
- if (!IsAtLeastTLSv1_3(ssl->version)) {
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
- #endif
- #ifdef WOLFSSL_EARLY_DATA
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
- #endif
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
- #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
- #endif
- }
-#endif
- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
- || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
- if (!ssl->ctx->cm->ocspStaplingEnabled) {
- /* mark already sent, so it won't send it */
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
- }
- #endif
- }
-
-#ifdef WOLFSSL_TLS13
- #ifndef NO_CERTS
- else if (msgType == certificate_request) {
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
-#if !defined(WOLFSSL_NO_SIGALG)
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
-#endif
- /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
- * TLSX_CERTIFICATE_AUTHORITIES, OID_FILTERS
- * TLSX_STATUS_REQUEST
- */
- }
- #endif
-#endif
- if (ssl->extensions) {
- ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
- if (ret != 0)
- return ret;
- }
- if (ssl->ctx && ssl->ctx->extensions) {
- ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, &length);
- if (ret != 0)
- return ret;
- }
-
-#ifdef HAVE_EXTENDED_MASTER
- if (msgType == client_hello && ssl->options.haveEMS &&
- (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
- length += HELLO_EXT_SZ;
- }
-#endif
-
- if (length)
- length += OPAQUE16_LEN; /* for total length storage. */
-
- *pLength += length;
-
- return ret;
-}
-
-/** Writes the extensions to be sent into the client hello. */
-int TLSX_WriteRequest(WOLFSSL* ssl, byte* output, byte msgType, word16* pOffset)
-{
- int ret = 0;
- word16 offset = 0;
- byte semaphore[SEMAPHORE_SIZE] = {0};
-
- if (!TLSX_SupportExtensions(ssl) || output == NULL)
- return 0;
-
- offset += OPAQUE16_LEN; /* extensions length */
-
- if (msgType == client_hello) {
- EC_VALIDATE_REQUEST(ssl, semaphore);
- PF_VALIDATE_REQUEST(ssl, semaphore);
- WOLF_STK_VALIDATE_REQUEST(ssl);
- QSH_VALIDATE_REQUEST(ssl, semaphore);
-#if !defined(WOLFSSL_NO_SIGALG)
- if (ssl->suites->hashSigAlgoSz == 0)
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
-#endif
-#ifdef WOLFSSL_TLS13
- if (!IsAtLeastTLSv1_2(ssl))
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
- if (!IsAtLeastTLSv1_3(ssl->version)) {
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
- #endif
- #ifdef WOLFSSL_EARLY_DATA
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
- #endif
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
- #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
- #endif
- }
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- /* Must write Pre-shared Key extension at the end in TLS v1.3.
- * Must not write out Pre-shared Key extension in earlier versions of
- * protocol.
- */
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
- #endif
-#endif
- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
- || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
- /* mark already sent, so it won't send it */
- if (!ssl->ctx->cm->ocspStaplingEnabled) {
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
- }
- #endif
- }
-#ifdef WOLFSSL_TLS13
- #ifndef NO_CERTS
- else if (msgType == certificate_request) {
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
-#if !defined(WOLFSSL_NO_SIGALG)
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
-#endif
- /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
- * TLSX_CERTIFICATE_AUTHORITIES, TLSX_OID_FILTERS
- * TLSX_STATUS_REQUEST
- */
- }
- #endif
-#endif
- if (ssl->extensions) {
- ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
- msgType, &offset);
- if (ret != 0)
- return ret;
- }
- if (ssl->ctx && ssl->ctx->extensions) {
- ret = TLSX_Write(ssl->ctx->extensions, output + offset, semaphore,
- msgType, &offset);
- if (ret != 0)
- return ret;
- }
-
-#ifdef HAVE_EXTENDED_MASTER
- if (msgType == client_hello && ssl->options.haveEMS &&
- (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
- WOLFSSL_MSG("EMS extension to write");
- c16toa(HELLO_EXT_EXTMS, output + offset);
- offset += HELLO_EXT_TYPE_SZ;
- c16toa(0, output + offset);
- offset += HELLO_EXT_SZ_SZ;
- }
-#endif
-
-#ifdef WOLFSSL_TLS13
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- if (msgType == client_hello && IsAtLeastTLSv1_3(ssl->version)) {
- /* Write out what we can of Pre-shared key extension. */
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
- ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
- client_hello, &offset);
- if (ret != 0)
- return ret;
- }
- #endif
-#endif
-
- if (offset > OPAQUE16_LEN || msgType != client_hello)
- c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
-
- *pOffset += offset;
-
- return ret;
-}
-
-#endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
-
-#if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
-
-/** Tells the buffered size of extensions to be sent into the server hello. */
-int TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType, word16* pLength)
-{
- int ret = 0;
- word16 length = 0;
- byte semaphore[SEMAPHORE_SIZE] = {0};
-
- switch (msgType) {
-#ifndef NO_WOLFSSL_SERVER
- case server_hello:
- PF_VALIDATE_RESPONSE(ssl, semaphore);
- #ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version)) {
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
- #ifndef WOLFSSL_TLS13_DRAFT_18
- TURN_OFF(semaphore,
- TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
- #endif
- if (!ssl->options.noPskDheKe)
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
- #endif
- }
- else {
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
- #endif
- }
- #endif
- break;
-
- #ifdef WOLFSSL_TLS13
- case hello_retry_request:
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
- #ifndef WOLFSSL_TLS13_DRAFT_18
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
- #endif
- if (!ssl->options.noPskDheKe)
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
- break;
- #endif
-
- #ifdef WOLFSSL_TLS13
- case encrypted_extensions:
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
- #endif
- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
- #endif
- #if defined(HAVE_SECURE_RENEGOTIATION)
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
- #endif
- break;
-
- #ifdef WOLFSSL_EARLY_DATA
- case session_ticket:
- if (ssl->options.tls1_3) {
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
- }
- break;
- #endif
- #endif
-#endif
-
-#ifdef WOLFSSL_TLS13
- #ifndef NO_CERTS
- case certificate:
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
- /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
- * TLSX_SERVER_CERTIFICATE_TYPE
- */
- break;
- #endif
-#endif
- }
-
- #ifdef HAVE_QSH
- /* change response if not using TLS_QSH */
- if (!ssl->options.haveQSH) {
- TLSX* ext = TLSX_Find(ssl->extensions, TLSX_QUANTUM_SAFE_HYBRID);
- if (ext)
- ext->resp = 0;
- }
- #endif
-
-#ifdef HAVE_EXTENDED_MASTER
- if (ssl->options.haveEMS && msgType == server_hello &&
- !IsAtLeastTLSv1_3(ssl->version)) {
- length += HELLO_EXT_SZ;
- }
-#endif
-
- if (TLSX_SupportExtensions(ssl)) {
- ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
- if (ret != 0)
- return ret;
- }
-
- /* All the response data is set at the ssl object only, so no ctx here. */
-
- if (length || msgType != server_hello)
- length += OPAQUE16_LEN; /* for total length storage. */
-
- *pLength += length;
-
- return ret;
-}
-
-/** Writes the server hello extensions into a buffer. */
-int TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType, word16* pOffset)
-{
- int ret = 0;
- word16 offset = 0;
-
- if (TLSX_SupportExtensions(ssl) && output) {
- byte semaphore[SEMAPHORE_SIZE] = {0};
-
- switch (msgType) {
-#ifndef NO_WOLFSSL_SERVER
- case server_hello:
- PF_VALIDATE_RESPONSE(ssl, semaphore);
- #ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version)) {
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
- #ifndef WOLFSSL_TLS13_DRAFT_18
- TURN_OFF(semaphore,
- TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
- #endif
- if (!ssl->options.noPskDheKe)
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
- #endif
- }
- else {
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
- #endif
- }
- #endif
- break;
-
- #ifdef WOLFSSL_TLS13
- case hello_retry_request:
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
- #ifndef WOLFSSL_TLS13_DRAFT_18
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
- #endif
- if (!ssl->options.noPskDheKe)
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
- /* Cookie is written below as last extension. */
- break;
- #endif
-
- #ifdef WOLFSSL_TLS13
- case encrypted_extensions:
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
- #endif
- #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
- #endif
- #if defined(HAVE_SECURE_RENEGOTIATION)
- TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
- #endif
- break;
-
- #ifdef WOLFSSL_EARLY_DATA
- case session_ticket:
- if (ssl->options.tls1_3) {
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
- }
- break;
- #endif
- #endif
-#endif
-
- #ifdef WOLFSSL_TLS13
- #ifndef NO_CERTS
- case certificate:
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
- /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
- * TLSX_SERVER_CERTIFICATE_TYPE
- */
- break;
- #endif
- #endif
- }
-
- offset += OPAQUE16_LEN; /* extensions length */
-
- ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
- msgType, &offset);
- if (ret != 0)
- return ret;
-
-#ifdef WOLFSSL_TLS13
- if (msgType == hello_retry_request) {
- XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
- TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
- ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
- msgType, &offset);
- if (ret != 0)
- return ret;
- }
-#endif
-
-#ifdef HAVE_EXTENDED_MASTER
- if (ssl->options.haveEMS && msgType == server_hello &&
- !IsAtLeastTLSv1_3(ssl->version)) {
- WOLFSSL_MSG("EMS extension to write");
- c16toa(HELLO_EXT_EXTMS, output + offset);
- offset += HELLO_EXT_TYPE_SZ;
- c16toa(0, output + offset);
- offset += HELLO_EXT_SZ_SZ;
- }
-#endif
-
- if (offset > OPAQUE16_LEN || msgType != server_hello)
- c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
- }
-
- if (pOffset)
- *pOffset += offset;
-
- return ret;
-}
-
-#endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_SERVER */
-
-#ifdef WOLFSSL_TLS13
-int TLSX_ParseVersion(WOLFSSL* ssl, byte* input, word16 length, byte msgType,
- int* found)
-{
- int ret = 0;
- int offset = 0;
-
- *found = 0;
- while (offset < (int)length) {
- word16 type;
- word16 size;
-
- if (offset + (2 * OPAQUE16_LEN) > length) {
- ret = BUFFER_ERROR;
- break;
- }
-
- ato16(input + offset, &type);
- offset += HELLO_EXT_TYPE_SZ;
-
- ato16(input + offset, &size);
- offset += OPAQUE16_LEN;
-
- if (offset + size > length) {
- ret = BUFFER_ERROR;
- break;
- }
-
- if (type == TLSX_SUPPORTED_VERSIONS) {
- *found = 1;
-
- WOLFSSL_MSG("Supported Versions extension received");
-
- ret = SV_PARSE(ssl, input + offset, size, msgType);
- break;
- }
-
- offset += size;
- }
-
- return ret;
-}
-#endif
-
-/** Parses a buffer of TLS extensions. */
-int TLSX_Parse(WOLFSSL* ssl, byte* input, word16 length, byte msgType,
- Suites *suites)
-{
- int ret = 0;
- word16 offset = 0;
- byte isRequest = (msgType == client_hello ||
- msgType == certificate_request);
-
-#ifdef HAVE_EXTENDED_MASTER
- byte pendingEMS = 0;
-#endif
-#if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
- int pskDone = 0;
-#endif
-
- if (!ssl || !input || (isRequest && !suites))
- return BAD_FUNC_ARG;
-
- while (ret == 0 && offset < length) {
- word16 type;
- word16 size;
-
-#if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
- if (msgType == client_hello && pskDone)
- return PSK_KEY_ERROR;
-#endif
-
- if (length - offset < HELLO_EXT_TYPE_SZ + OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- ato16(input + offset, &type);
- offset += HELLO_EXT_TYPE_SZ;
-
- ato16(input + offset, &size);
- offset += OPAQUE16_LEN;
-
- if (offset + size > length)
- return BUFFER_ERROR;
-
- switch (type) {
- case TLSX_SERVER_NAME:
- WOLFSSL_MSG("SNI extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) &&
- msgType != client_hello &&
- msgType != server_hello &&
- msgType != encrypted_extensions) {
- return EXT_NOT_ALLOWED;
- }
- else if (!IsAtLeastTLSv1_3(ssl->version) &&
- msgType == encrypted_extensions) {
- return EXT_NOT_ALLOWED;
- }
-#endif
- ret = SNI_PARSE(ssl, input + offset, size, isRequest);
- break;
-
- case TLSX_TRUSTED_CA_KEYS:
- WOLFSSL_MSG("Trusted CA extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) &&
- msgType != client_hello &&
- msgType != encrypted_extensions) {
- return EXT_NOT_ALLOWED;
- }
-#endif
- ret = TCA_PARSE(ssl, input + offset, size, isRequest);
- break;
-
- case TLSX_MAX_FRAGMENT_LENGTH:
- WOLFSSL_MSG("Max Fragment Length extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) &&
- msgType != client_hello &&
- msgType != encrypted_extensions) {
- return EXT_NOT_ALLOWED;
- }
- else if (!IsAtLeastTLSv1_3(ssl->version) &&
- msgType == encrypted_extensions) {
- return EXT_NOT_ALLOWED;
- }
-#endif
- ret = MFL_PARSE(ssl, input + offset, size, isRequest);
- break;
-
- case TLSX_TRUNCATED_HMAC:
- WOLFSSL_MSG("Truncated HMAC extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version))
- break;
-#endif
- ret = THM_PARSE(ssl, input + offset, size, isRequest);
- break;
-
- case TLSX_SUPPORTED_GROUPS:
- WOLFSSL_MSG("Supported Groups extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) &&
- msgType != client_hello &&
- msgType != server_hello &&
- msgType != encrypted_extensions) {
- return EXT_NOT_ALLOWED;
- }
- else if (!IsAtLeastTLSv1_3(ssl->version) &&
- msgType == encrypted_extensions) {
- return EXT_NOT_ALLOWED;
- }
-#endif
- ret = EC_PARSE(ssl, input + offset, size, isRequest);
- break;
-
- case TLSX_EC_POINT_FORMATS:
- WOLFSSL_MSG("Point Formats extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version))
- break;
-#endif
- ret = PF_PARSE(ssl, input + offset, size, isRequest);
- break;
-
- case TLSX_STATUS_REQUEST:
- WOLFSSL_MSG("Certificate Status Request extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
- #ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) &&
- msgType != client_hello &&
- msgType != certificate_request &&
- msgType != certificate) {
- break;
- }
- #endif
- ret = CSR_PARSE(ssl, input + offset, size, isRequest);
- break;
-
- case TLSX_STATUS_REQUEST_V2:
- WOLFSSL_MSG("Certificate Status Request v2 extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) &&
- msgType != client_hello &&
- msgType != certificate_request &&
- msgType != certificate) {
- return EXT_NOT_ALLOWED;
- }
-#endif
- ret = CSR2_PARSE(ssl, input + offset, size, isRequest);
- break;
-
-#ifdef HAVE_EXTENDED_MASTER
- case HELLO_EXT_EXTMS:
- WOLFSSL_MSG("Extended Master Secret extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version))
- break;
-#endif
- if (size != 0)
- return BUFFER_ERROR;
-
-#ifndef NO_WOLFSSL_SERVER
- if (isRequest)
- ssl->options.haveEMS = 1;
-#endif
- pendingEMS = 1;
- break;
-#endif
-
- case TLSX_RENEGOTIATION_INFO:
- WOLFSSL_MSG("Secure Renegotiation extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version))
- break;
-#endif
- ret = SCR_PARSE(ssl, input + offset, size, isRequest);
- break;
-
- case TLSX_SESSION_TICKET:
- WOLFSSL_MSG("Session Ticket extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) &&
- msgType != client_hello) {
- return EXT_NOT_ALLOWED;
- }
-#endif
- ret = WOLF_STK_PARSE(ssl, input + offset, size, isRequest);
- break;
-
- case TLSX_QUANTUM_SAFE_HYBRID:
- WOLFSSL_MSG("Quantum-Safe-Hybrid extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version))
- break;
-#endif
- ret = QSH_PARSE(ssl, input + offset, size, isRequest);
- break;
-
- case TLSX_APPLICATION_LAYER_PROTOCOL:
- WOLFSSL_MSG("ALPN extension received");
-
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) &&
- msgType != client_hello &&
- msgType != server_hello &&
- msgType != encrypted_extensions) {
- return EXT_NOT_ALLOWED;
- }
- else if (!IsAtLeastTLSv1_3(ssl->version) &&
- msgType == encrypted_extensions) {
- return EXT_NOT_ALLOWED;
- }
-#endif
- ret = ALPN_PARSE(ssl, input + offset, size, isRequest);
- break;
-#if !defined(WOLFSSL_NO_SIGALG)
- case TLSX_SIGNATURE_ALGORITHMS:
- WOLFSSL_MSG("Signature Algorithms extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
- if (!IsAtLeastTLSv1_2(ssl))
- break;
-#ifdef WOLFSSL_TLS13
- if (IsAtLeastTLSv1_3(ssl->version) &&
- msgType != client_hello &&
- msgType != certificate_request) {
- return EXT_NOT_ALLOWED;
- }
-#endif
- ret = SA_PARSE(ssl, input + offset, size, isRequest, suites);
- break;
-#endif
-
-#if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
- case TLSX_ENCRYPT_THEN_MAC:
- WOLFSSL_MSG("Encrypt-Then-Mac extension received");
-
- /* Ignore for TLS 1.3+ */
- if (IsAtLeastTLSv1_3(ssl->version))
- break;
-
- ret = ETM_PARSE(ssl, input + offset, size, msgType);
- break;
-#endif /* HAVE_ENCRYPT_THEN_MAC */
-
-#ifdef WOLFSSL_TLS13
- case TLSX_SUPPORTED_VERSIONS:
- WOLFSSL_MSG("Skipping Supported Versions - already processed");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
- break;
-
- case TLSX_COOKIE:
- WOLFSSL_MSG("Cookie extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
- if (!IsAtLeastTLSv1_3(ssl->version))
- break;
-
- if (msgType != client_hello &&
- msgType != hello_retry_request) {
- return EXT_NOT_ALLOWED;
- }
-
- ret = CKE_PARSE(ssl, input + offset, size, msgType);
- break;
-
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- case TLSX_PRE_SHARED_KEY:
- WOLFSSL_MSG("Pre-Shared Key extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
- if (!IsAtLeastTLSv1_3(ssl->version))
- break;
-
- if (msgType != client_hello && msgType != server_hello)
- return EXT_NOT_ALLOWED;
-
- ret = PSK_PARSE(ssl, input + offset, size, msgType);
- pskDone = 1;
- break;
-
- case TLSX_PSK_KEY_EXCHANGE_MODES:
- WOLFSSL_MSG("PSK Key Exchange Modes extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
- if (!IsAtLeastTLSv1_3(ssl->version))
- break;
-
- if (msgType != client_hello)
- return EXT_NOT_ALLOWED;
-
- ret = PKM_PARSE(ssl, input + offset, size, msgType);
- break;
- #endif
-
- #ifdef WOLFSSL_EARLY_DATA
- case TLSX_EARLY_DATA:
- WOLFSSL_MSG("Early Data extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
- if (!IsAtLeastTLSv1_3(ssl->version))
- break;
-
- if (msgType != client_hello && msgType != session_ticket &&
- msgType != encrypted_extensions) {
- return EXT_NOT_ALLOWED;
- }
- if (!IsAtLeastTLSv1_3(ssl->version) &&
- (msgType == session_ticket ||
- msgType == encrypted_extensions)) {
- return EXT_NOT_ALLOWED;
- }
- ret = EDI_PARSE(ssl, input + offset, size, msgType);
- break;
- #endif
-
- #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- case TLSX_POST_HANDSHAKE_AUTH:
- WOLFSSL_MSG("Post Handshake Authentication extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
- if (!IsAtLeastTLSv1_3(ssl->version))
- break;
-
- if (msgType != client_hello)
- return EXT_NOT_ALLOWED;
-
- ret = PHA_PARSE(ssl, input + offset, size, msgType);
- break;
- #endif
-
- #if !defined(WOLFSSL_TLS13_DRAFT_18) && !defined(WOLFSSL_TLS13_DRAFT_22)
- case TLSX_SIGNATURE_ALGORITHMS_CERT:
- WOLFSSL_MSG("Signature Algorithms extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
- if (!IsAtLeastTLSv1_3(ssl->version))
- break;
-
- if (msgType != client_hello &&
- msgType != certificate_request) {
- return EXT_NOT_ALLOWED;
- }
- if (!IsAtLeastTLSv1_3(ssl->version) &&
- msgType == certificate_request) {
- return EXT_NOT_ALLOWED;
- }
-
- ret = SAC_PARSE(ssl, input + offset, size, isRequest);
- break;
- #endif
-
- case TLSX_KEY_SHARE:
- WOLFSSL_MSG("Key Share extension received");
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_BUFFER(input + offset, size);
- #endif
-
- if (!IsAtLeastTLSv1_3(ssl->version))
- break;
-
- if (msgType != client_hello && msgType != server_hello &&
- msgType != hello_retry_request) {
- return EXT_NOT_ALLOWED;
- }
- ret = KS_PARSE(ssl, input + offset, size, msgType);
- break;
-#endif
- default:
- WOLFSSL_MSG("Unknown TLS extension type");
- }
-
- /* offset should be updated here! */
- offset += size;
- }
-
-#ifdef HAVE_EXTENDED_MASTER
- if (!isRequest && ssl->options.haveEMS && !pendingEMS)
- ssl->options.haveEMS = 0;
-#endif
-
- if (ret == 0)
- ret = SNI_VERIFY_PARSE(ssl, isRequest);
- if (ret == 0)
- ret = TCA_VERIFY_PARSE(ssl, isRequest);
-
- return ret;
-}
-
-/* undefining semaphore macros */
-#undef IS_OFF
-#undef TURN_ON
-#undef SEMAPHORE_SIZE
-
-#endif /* HAVE_TLS_EXTENSIONS */
-
-#ifndef NO_WOLFSSL_CLIENT
-
- WOLFSSL_METHOD* wolfTLS_client_method(void)
- {
- return wolfTLS_client_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLS_client_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("TLS_client_method_ex");
- if (method) {
- #if defined(WOLFSSL_TLS13)
- InitSSL_Method(method, MakeTLSv1_3());
- #elif !defined(WOLFSSL_NO_TLS12)
- InitSSL_Method(method, MakeTLSv1_2());
- #elif !defined(NO_OLD_TLS)
- InitSSL_Method(method, MakeTLSv1_1());
- #elif defined(WOLFSSL_ALLOW_TLSV10)
- InitSSL_Method(method, MakeTLSv1());
- #else
- #error No TLS version enabled!
- #endif
-
- method->downgrade = 1;
- method->side = WOLFSSL_CLIENT_END;
- }
- return method;
- }
-
-#ifndef NO_OLD_TLS
- #ifdef WOLFSSL_ALLOW_TLSV10
- WOLFSSL_METHOD* wolfTLSv1_client_method(void)
- {
- return wolfTLSv1_client_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLSv1_client_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("TLSv1_client_method_ex");
- if (method)
- InitSSL_Method(method, MakeTLSv1());
- return method;
- }
- #endif /* WOLFSSL_ALLOW_TLSV10 */
-
- WOLFSSL_METHOD* wolfTLSv1_1_client_method(void)
- {
- return wolfTLSv1_1_client_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLSv1_1_client_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("TLSv1_1_client_method_ex");
- if (method)
- InitSSL_Method(method, MakeTLSv1_1());
- return method;
- }
-#endif /* !NO_OLD_TLS */
-
-#ifndef WOLFSSL_NO_TLS12
- WOLFSSL_ABI
- WOLFSSL_METHOD* wolfTLSv1_2_client_method(void)
- {
- return wolfTLSv1_2_client_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLSv1_2_client_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("TLSv1_2_client_method_ex");
- if (method)
- InitSSL_Method(method, MakeTLSv1_2());
- return method;
- }
-#endif /* WOLFSSL_NO_TLS12 */
-
-#ifdef WOLFSSL_TLS13
- /* The TLS v1.3 client method data.
- *
- * returns the method data for a TLS v1.3 client.
- */
- WOLFSSL_ABI
- WOLFSSL_METHOD* wolfTLSv1_3_client_method(void)
- {
- return wolfTLSv1_3_client_method_ex(NULL);
- }
-
- /* The TLS v1.3 client method data.
- *
- * heap The heap used for allocation.
- * returns the method data for a TLS v1.3 client.
- */
- WOLFSSL_METHOD* wolfTLSv1_3_client_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method = (WOLFSSL_METHOD*)
- XMALLOC(sizeof(WOLFSSL_METHOD), heap,
- DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("TLSv1_3_client_method_ex");
- if (method)
- InitSSL_Method(method, MakeTLSv1_3());
- return method;
- }
-#endif /* WOLFSSL_TLS13 */
-
-#ifdef WOLFSSL_DTLS
-
- WOLFSSL_METHOD* wolfDTLS_client_method(void)
- {
- return wolfDTLS_client_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfDTLS_client_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("DTLS_client_method_ex");
- if (method) {
- #if !defined(WOLFSSL_NO_TLS12)
- InitSSL_Method(method, MakeDTLSv1_2());
- #elif !defined(NO_OLD_TLS)
- InitSSL_Method(method, MakeDTLSv1());
- #else
- #error No DTLS version enabled!
- #endif
-
- method->downgrade = 1;
- method->side = WOLFSSL_CLIENT_END;
- }
- return method;
- }
-
- #ifndef NO_OLD_TLS
- WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
- {
- return wolfDTLSv1_client_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfDTLSv1_client_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("DTLSv1_client_method_ex");
- if (method)
- InitSSL_Method(method, MakeDTLSv1());
- return method;
- }
- #endif /* NO_OLD_TLS */
-
- #ifndef WOLFSSL_NO_TLS12
- WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
- {
- return wolfDTLSv1_2_client_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfDTLSv1_2_client_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("DTLSv1_2_client_method_ex");
- if (method)
- InitSSL_Method(method, MakeDTLSv1_2());
- (void)heap;
- return method;
- }
- #endif /* !WOLFSSL_NO_TLS12 */
-#endif /* WOLFSSL_DTLS */
-
-#endif /* NO_WOLFSSL_CLIENT */
-
-
-/* EITHER SIDE METHODS */
-#if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- #ifndef NO_OLD_TLS
- #ifdef WOLFSSL_ALLOW_TLSV10
- /* Gets a WOLFSL_METHOD type that is not set as client or server
- *
- * Returns a pointer to a WOLFSSL_METHOD struct
- */
- WOLFSSL_METHOD* wolfTLSv1_method(void)
- {
- return wolfTLSv1_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLSv1_method_ex(void* heap)
- {
- WOLFSSL_METHOD* m;
- WOLFSSL_ENTER("TLSv1_method");
- #ifndef NO_WOLFSSL_CLIENT
- m = wolfTLSv1_client_method_ex(heap);
- #else
- m = wolfTLSv1_server_method_ex(heap);
- #endif
- if (m != NULL) {
- m->side = WOLFSSL_NEITHER_END;
- }
-
- return m;
- }
- #endif /* WOLFSSL_ALLOW_TLSV10 */
-
- /* Gets a WOLFSL_METHOD type that is not set as client or server
- *
- * Returns a pointer to a WOLFSSL_METHOD struct
- */
- WOLFSSL_METHOD* wolfTLSv1_1_method(void)
- {
- return wolfTLSv1_1_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLSv1_1_method_ex(void* heap)
- {
- WOLFSSL_METHOD* m;
- WOLFSSL_ENTER("TLSv1_1_method");
- #ifndef NO_WOLFSSL_CLIENT
- m = wolfTLSv1_1_client_method_ex(heap);
- #else
- m = wolfTLSv1_1_server_method_ex(heap);
- #endif
- if (m != NULL) {
- m->side = WOLFSSL_NEITHER_END;
- }
- return m;
- }
- #endif /* !NO_OLD_TLS */
-
- #ifndef WOLFSSL_NO_TLS12
- /* Gets a WOLFSL_METHOD type that is not set as client or server
- *
- * Returns a pointer to a WOLFSSL_METHOD struct
- */
- WOLFSSL_METHOD* wolfTLSv1_2_method(void)
- {
- return wolfTLSv1_2_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLSv1_2_method_ex(void* heap)
- {
- WOLFSSL_METHOD* m;
- WOLFSSL_ENTER("TLSv1_2_method");
- #ifndef NO_WOLFSSL_CLIENT
- m = wolfTLSv1_2_client_method_ex(heap);
- #else
- m = wolfTLSv1_2_server_method_ex(heap);
- #endif
- if (m != NULL) {
- m->side = WOLFSSL_NEITHER_END;
- }
- return m;
- }
- #endif /* !WOLFSSL_NO_TLS12 */
-
- #ifdef WOLFSSL_TLS13
- /* Gets a WOLFSL_METHOD type that is not set as client or server
- *
- * Returns a pointer to a WOLFSSL_METHOD struct
- */
- WOLFSSL_METHOD* wolfTLSv1_3_method(void)
- {
- return wolfTLSv1_3_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLSv1_3_method_ex(void* heap)
- {
- WOLFSSL_METHOD* m;
- WOLFSSL_ENTER("TLSv1_3_method");
- #ifndef NO_WOLFSSL_CLIENT
- m = wolfTLSv1_3_client_method_ex(heap);
- #else
- m = wolfTLSv1_3_server_method_ex(heap);
- #endif
- if (m != NULL) {
- m->side = WOLFSSL_NEITHER_END;
- }
- return m;
- }
- #endif /* WOLFSSL_TLS13 */
-
-#ifdef WOLFSSL_DTLS
- WOLFSSL_METHOD* wolfDTLS_method(void)
- {
- return wolfDTLS_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfDTLS_method_ex(void* heap)
- {
- WOLFSSL_METHOD* m;
- WOLFSSL_ENTER("DTLS_method_ex");
- #ifndef NO_WOLFSSL_CLIENT
- m = wolfDTLS_client_method_ex(heap);
- #else
- m = wolfDTLS_server_method_ex(heap);
- #endif
- if (m != NULL) {
- m->side = WOLFSSL_NEITHER_END;
- }
- return m;
- }
-
- #ifndef NO_OLD_TLS
- WOLFSSL_METHOD* wolfDTLSv1_method(void)
- {
- return wolfDTLSv1_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfDTLSv1_method_ex(void* heap)
- {
- WOLFSSL_METHOD* m;
- WOLFSSL_ENTER("DTLSv1_method_ex");
- #ifndef NO_WOLFSSL_CLIENT
- m = wolfDTLSv1_client_method_ex(heap);
- #else
- m = wolfDTLSv1_server_method_ex(heap);
- #endif
- if (m != NULL) {
- m->side = WOLFSSL_NEITHER_END;
- }
- return m;
- }
- #endif /* !NO_OLD_TLS */
- #ifndef WOLFSSL_NO_TLS12
- WOLFSSL_METHOD* wolfDTLSv1_2_method(void)
- {
- return wolfDTLSv1_2_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfDTLSv1_2_method_ex(void* heap)
- {
- WOLFSSL_METHOD* m;
- WOLFSSL_ENTER("DTLSv1_2_method");
- #ifndef NO_WOLFSSL_CLIENT
- m = wolfDTLSv1_2_client_method_ex(heap);
- #else
- m = wolfDTLSv1_2_server_method_ex(heap);
- #endif
- if (m != NULL) {
- m->side = WOLFSSL_NEITHER_END;
- }
- return m;
- }
- #endif /* !WOLFSSL_NO_TLS12 */
-#endif /* WOLFSSL_DTLS */
-#endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
-
-
-#ifndef NO_WOLFSSL_SERVER
-
- WOLFSSL_METHOD* wolfTLS_server_method(void)
- {
- return wolfTLS_server_method_ex(NULL);
- }
-
- WOLFSSL_METHOD* wolfTLS_server_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("TLS_server_method_ex");
- if (method) {
- #if defined(WOLFSSL_TLS13)
- InitSSL_Method(method, MakeTLSv1_3());
- #elif !defined(WOLFSSL_NO_TLS12)
- InitSSL_Method(method, MakeTLSv1_2());
- #elif !defined(NO_OLD_TLS)
- InitSSL_Method(method, MakeTLSv1_1());
- #elif defined(WOLFSSL_ALLOW_TLSV10)
- InitSSL_Method(method, MakeTLSv1());
- #else
- #error No TLS version enabled!
- #endif
-
- method->downgrade = 1;
- method->side = WOLFSSL_SERVER_END;
- }
- return method;
- }
-
-#ifndef NO_OLD_TLS
- #ifdef WOLFSSL_ALLOW_TLSV10
- WOLFSSL_METHOD* wolfTLSv1_server_method(void)
- {
- return wolfTLSv1_server_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLSv1_server_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("TLSv1_server_method_ex");
- if (method) {
- InitSSL_Method(method, MakeTLSv1());
- method->side = WOLFSSL_SERVER_END;
- }
- return method;
- }
- #endif /* WOLFSSL_ALLOW_TLSV10 */
-
- WOLFSSL_METHOD* wolfTLSv1_1_server_method(void)
- {
- return wolfTLSv1_1_server_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLSv1_1_server_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("TLSv1_1_server_method_ex");
- if (method) {
- InitSSL_Method(method, MakeTLSv1_1());
- method->side = WOLFSSL_SERVER_END;
- }
- return method;
- }
-#endif /* !NO_OLD_TLS */
-
-
-#ifndef WOLFSSL_NO_TLS12
- WOLFSSL_METHOD* wolfTLSv1_2_server_method(void)
- {
- return wolfTLSv1_2_server_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfTLSv1_2_server_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("TLSv1_2_server_method_ex");
- if (method) {
- InitSSL_Method(method, MakeTLSv1_2());
- method->side = WOLFSSL_SERVER_END;
- }
- return method;
- }
-#endif /* !WOLFSSL_NO_TLS12 */
-
-#ifdef WOLFSSL_TLS13
- /* The TLS v1.3 server method data.
- *
- * returns the method data for a TLS v1.3 server.
- */
- WOLFSSL_METHOD* wolfTLSv1_3_server_method(void)
- {
- return wolfTLSv1_3_server_method_ex(NULL);
- }
-
- /* The TLS v1.3 server method data.
- *
- * heap The heap used for allocation.
- * returns the method data for a TLS v1.3 server.
- */
- WOLFSSL_METHOD* wolfTLSv1_3_server_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("TLSv1_3_server_method_ex");
- if (method) {
- InitSSL_Method(method, MakeTLSv1_3());
- method->side = WOLFSSL_SERVER_END;
- }
- return method;
- }
-#endif /* WOLFSSL_TLS13 */
-
-#ifdef WOLFSSL_DTLS
- WOLFSSL_METHOD* wolfDTLS_server_method(void)
- {
- return wolfDTLS_server_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfDTLS_server_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("DTLS_server_method_ex");
- if (method) {
- #if !defined(WOLFSSL_NO_TLS12)
- InitSSL_Method(method, MakeDTLSv1_2());
- #elif !defined(NO_OLD_TLS)
- InitSSL_Method(method, MakeDTLSv1());
- #else
- #error No DTLS version enabled!
- #endif
-
- method->downgrade = 1;
- method->side = WOLFSSL_SERVER_END;
- }
- return method;
- }
-
- #ifndef NO_OLD_TLS
- WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
- {
- return wolfDTLSv1_server_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfDTLSv1_server_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- (void)heap;
- WOLFSSL_ENTER("DTLSv1_server_method_ex");
- if (method) {
- InitSSL_Method(method, MakeDTLSv1());
- method->side = WOLFSSL_SERVER_END;
- }
- return method;
- }
- #endif /* !NO_OLD_TLS */
-
- #ifndef WOLFSSL_NO_TLS12
- WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
- {
- return wolfDTLSv1_2_server_method_ex(NULL);
- }
- WOLFSSL_METHOD* wolfDTLSv1_2_server_method_ex(void* heap)
- {
- WOLFSSL_METHOD* method =
- (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
- heap, DYNAMIC_TYPE_METHOD);
- WOLFSSL_ENTER("DTLSv1_2_server_method_ex");
- (void)heap;
- if (method) {
- InitSSL_Method(method, MakeDTLSv1_2());
- method->side = WOLFSSL_SERVER_END;
- }
- (void)heap;
- return method;
- }
- #endif /* !WOLFSSL_NO_TLS12 */
-#endif /* WOLFSSL_DTLS */
-
-#endif /* NO_WOLFSSL_SERVER */
-
-#endif /* NO_TLS */
-#endif /* WOLFCRYPT_ONLY */
diff --git a/client/wolfssl/src/tls13.c b/client/wolfssl/src/tls13.c
deleted file mode 100644
index 9b9b1d1..0000000
--- a/client/wolfssl/src/tls13.c
+++ /dev/null
@@ -1,9048 +0,0 @@
-/* tls13.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-/*
- * BUILD_GCM
- * Enables AES-GCM ciphersuites.
- * HAVE_AESCCM
- * Enables AES-CCM ciphersuites.
- * HAVE_SESSION_TICKET
- * Enables session tickets - required for TLS 1.3 resumption.
- * NO_PSK
- * Do not enable Pre-Shared Keys.
- * TLS13_SUPPORTS_EXPORTERS
- * Guard to compile out any code for exporter keys.
- * Feature not supported yet.
- * WOLFSSL_ASYNC_CRYPT
- * Enables the use of asynchronous cryptographic operations.
- * This is available for ciphers and certificates.
- * HAVE_CHACHA && HAVE_POLY1305
- * Enables use of CHACHA20-POLY1305 ciphersuites.
- * WOLFSSL_DEBUG_TLS
- * Writes out details of TLS 1.3 protocol including handshake message buffers
- * and key generation input and output.
- * WOLFSSL_EARLY_DATA
- * Allow 0-RTT Handshake using Early Data extensions and handshake message
- * WOLFSSL_EARLY_DATA_GROUP
- * Group EarlyData message with ClientHello when sending
- * WOLFSSL_NO_SERVER_GROUPS_EXT
- * Do not send the server's groups in an extension when the server's top
- * preference is not in client's list.
- * WOLFSSL_POST_HANDSHAKE_AUTH
- * Allow TLS v1.3 code to perform post-handshake authentication of the
- * client.
- * WOLFSSL_SEND_HRR_COOKIE
- * Send a cookie in hello_retry_request message to enable stateless tracking
- * of ClientHello replies.
- * WOLFSSL_TLS13
- * Enable TLS 1.3 protocol implementation.
- * WOLFSSL_TLS13_DRAFT_18
- * Conform with Draft 18 of the TLS v1.3 specification.
- * WOLFSSL_TLS13_DRAFT_22
- * Conform with Draft 22 of the TLS v1.3 specification.
- * WOLFSSL_TLS13_DRAFT_23
- * Conform with Draft 23 of the TLS v1.3 specification.
- * WOLFSSL_TLS13_MIDDLEBOX_COMPAT
- * Enable middlebox compatibility in the TLS 1.3 handshake.
- * This includes sending ChangeCipherSpec before encrypted messages and
- * including a session id.
- * WOLFSSL_TLS13_SHA512
- * Allow generation of SHA-512 digests in handshake - no ciphersuite
- * requires SHA-512 at this time.
- * WOLFSSL_TLS13_TICKET_BEFORE_FINISHED
- * Allow a NewSessionTicket message to be sent by server before Client's
- * Finished message.
- * See TLS v1.3 specification, Section 4.6.1, Paragraph 4 (Note).
- */
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifdef WOLFSSL_TLS13
-#ifdef HAVE_SESSION_TICKET
- #include
-#endif
-
-#ifndef WOLFCRYPT_ONLY
-
-#ifdef HAVE_ERRNO_H
- #include
-#endif
-
-#include
-#include
-#include
-#include
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include
-#endif
-
-#ifdef HAVE_NTRU
- #include "libntruencrypt/ntru_crypto.h"
-#endif
-
-#ifdef __sun
- #include
-#endif
-
-#ifndef TRUE
- #define TRUE 1
-#endif
-#ifndef FALSE
- #define FALSE 0
-#endif
-
-#ifndef HAVE_HKDF
- #error The build option HAVE_HKDF is required for TLS 1.3
-#endif
-
-#ifndef HAVE_TLS_EXTENSIONS
- #ifndef _MSC_VER
- #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3"
- #else
- #pragma message("error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3")
- #endif
-#endif
-
-
-/* Set ret to error value and jump to label.
- *
- * err The error value to set.
- * eLabel The label to jump to.
- */
-#define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; }
-
-
-/* Extract data using HMAC, salt and input.
- * RFC 5869 - HMAC-based Extract-and-Expand Key Derivation Function (HKDF)
- *
- * prk The generated pseudorandom key.
- * salt The salt.
- * saltLen The length of the salt.
- * ikm The input keying material.
- * ikmLen The length of the input keying material.
- * mac The type of digest to use.
- * returns 0 on success, otherwise failure.
- */
-static int Tls13_HKDF_Extract(byte* prk, const byte* salt, int saltLen,
- byte* ikm, int ikmLen, int mac)
-{
- int ret;
- int hash = 0;
- int len = 0;
-
- switch (mac) {
- #ifndef NO_SHA256
- case sha256_mac:
- hash = WC_SHA256;
- len = WC_SHA256_DIGEST_SIZE;
- break;
- #endif
-
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- hash = WC_SHA384;
- len = WC_SHA384_DIGEST_SIZE;
- break;
- #endif
-
- #ifdef WOLFSSL_TLS13_SHA512
- case sha512_mac:
- hash = WC_SHA512;
- len = WC_SHA512_DIGEST_SIZE;
- break;
- #endif
- }
-
- /* When length is 0 then use zeroed data of digest length. */
- if (ikmLen == 0) {
- ikmLen = len;
- XMEMSET(ikm, 0, len);
- }
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG(" Salt");
- WOLFSSL_BUFFER(salt, saltLen);
- WOLFSSL_MSG(" IKM");
- WOLFSSL_BUFFER(ikm, ikmLen);
-#endif
-
- ret = wc_HKDF_Extract(hash, salt, saltLen, ikm, ikmLen, prk);
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG(" PRK");
- WOLFSSL_BUFFER(prk, len);
-#endif
-
- return ret;
-}
-
-/* Expand data using HMAC, salt and label and info.
- * TLS v1.3 defines this function.
- *
- * okm The generated pseudorandom key - output key material.
- * okmLen The length of generated pseudorandom key - output key material.
- * prk The salt - pseudo-random key.
- * prkLen The length of the salt - pseudo-random key.
- * protocol The TLS protocol label.
- * protocolLen The length of the TLS protocol label.
- * info The information to expand.
- * infoLen The length of the information.
- * digest The type of digest to use.
- * returns 0 on success, otherwise failure.
- */
-static int HKDF_Expand_Label(byte* okm, word32 okmLen,
- const byte* prk, word32 prkLen,
- const byte* protocol, word32 protocolLen,
- const byte* label, word32 labelLen,
- const byte* info, word32 infoLen,
- int digest)
-{
- int ret = 0;
- int idx = 0;
- byte data[MAX_HKDF_LABEL_SZ];
-
- /* Output length. */
- data[idx++] = (byte)(okmLen >> 8);
- data[idx++] = (byte)okmLen;
- /* Length of protocol | label. */
- data[idx++] = (byte)(protocolLen + labelLen);
- /* Protocol */
- XMEMCPY(&data[idx], protocol, protocolLen);
- idx += protocolLen;
- /* Label */
- XMEMCPY(&data[idx], label, labelLen);
- idx += labelLen;
- /* Length of hash of messages */
- data[idx++] = (byte)infoLen;
- /* Hash of messages */
- XMEMCPY(&data[idx], info, infoLen);
- idx += infoLen;
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG(" PRK");
- WOLFSSL_BUFFER(prk, prkLen);
- WOLFSSL_MSG(" Info");
- WOLFSSL_BUFFER(data, idx);
-#endif
-
- ret = wc_HKDF_Expand(digest, prk, prkLen, data, idx, okm, okmLen);
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG(" OKM");
- WOLFSSL_BUFFER(okm, okmLen);
-#endif
-
- ForceZero(data, idx);
-
- return ret;
-}
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* Size of the TLS v1.3 label use when deriving keys. */
-#define TLS13_PROTOCOL_LABEL_SZ 9
-/* The protocol label for TLS v1.3. */
-static const byte tls13ProtocolLabel[TLS13_PROTOCOL_LABEL_SZ + 1] = "TLS 1.3, ";
-#else
-/* Size of the TLS v1.3 label use when deriving keys. */
-#define TLS13_PROTOCOL_LABEL_SZ 6
-/* The protocol label for TLS v1.3. */
-static const byte tls13ProtocolLabel[TLS13_PROTOCOL_LABEL_SZ + 1] = "tls13 ";
-#endif
-
-#if !defined(WOLFSSL_TLS13_DRAFT_18) || defined(HAVE_SESSION_TICKET) || \
- !defined(NO_PSK)
-/* Derive a key from a message.
- *
- * ssl The SSL/TLS object.
- * output The buffer to hold the derived key.
- * outputLen The length of the derived key.
- * secret The secret used to derive the key (HMAC secret).
- * label The label used to distinguish the context.
- * labelLen The length of the label.
- * msg The message data to derive key from.
- * msgLen The length of the message data to derive key from.
- * hashAlgo The hash algorithm to use in the HMAC.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveKeyMsg(WOLFSSL* ssl, byte* output, int outputLen,
- const byte* secret, const byte* label, word32 labelLen,
- byte* msg, int msgLen, int hashAlgo)
-{
- byte hash[WC_MAX_DIGEST_SIZE];
- Digest digest;
- word32 hashSz = 0;
- const byte* protocol;
- word32 protocolLen;
- int digestAlg = -1;
- int ret = BAD_FUNC_ARG;
-
- switch (hashAlgo) {
-#ifndef NO_WOLFSSL_SHA256
- case sha256_mac:
- ret = wc_InitSha256_ex(&digest.sha256, ssl->heap, INVALID_DEVID);
- if (ret == 0) {
- ret = wc_Sha256Update(&digest.sha256, msg, msgLen);
- if (ret == 0)
- ret = wc_Sha256Final(&digest.sha256, hash);
- wc_Sha256Free(&digest.sha256);
- }
- hashSz = WC_SHA256_DIGEST_SIZE;
- digestAlg = WC_SHA256;
- break;
-#endif
-#ifdef WOLFSSL_SHA384
- case sha384_mac:
- ret = wc_InitSha384_ex(&digest.sha384, ssl->heap, INVALID_DEVID);
- if (ret == 0) {
- ret = wc_Sha384Update(&digest.sha384, msg, msgLen);
- if (ret == 0)
- ret = wc_Sha384Final(&digest.sha384, hash);
- wc_Sha384Free(&digest.sha384);
- }
- hashSz = WC_SHA384_DIGEST_SIZE;
- digestAlg = WC_SHA384;
- break;
-#endif
-#ifdef WOLFSSL_TLS13_SHA512
- case sha512_mac:
- ret = wc_InitSha512_ex(&digest.sha512, ssl->heap, INVALID_DEVID);
- if (ret == 0) {
- ret = wc_Sha512Update(&digest.sha512, msg, msgLen);
- if (ret == 0)
- ret = wc_Sha512Final(&digest.sha512, hash);
- wc_Sha512Free(&digest.sha512);
- }
- hashSz = WC_SHA512_DIGEST_SIZE;
- digestAlg = WC_SHA512;
- break;
-#endif
- default:
- digestAlg = -1;
- break;
- }
-
- if (digestAlg < 0)
- return HASH_TYPE_E;
-
- if (ret != 0)
- return ret;
-
- switch (ssl->version.minor) {
- case TLSv1_3_MINOR:
- protocol = tls13ProtocolLabel;
- protocolLen = TLS13_PROTOCOL_LABEL_SZ;
- break;
-
- default:
- return VERSION_ERROR;
- }
- if (outputLen == -1)
- outputLen = hashSz;
-
- return HKDF_Expand_Label(output, outputLen, secret, hashSz,
- protocol, protocolLen, label, labelLen,
- hash, hashSz, digestAlg);
-}
-#endif
-
-/* Derive a key.
- *
- * ssl The SSL/TLS object.
- * output The buffer to hold the derived key.
- * outputLen The length of the derived key.
- * secret The secret used to derive the key (HMAC secret).
- * label The label used to distinguish the context.
- * labelLen The length of the label.
- * hashAlgo The hash algorithm to use in the HMAC.
- * includeMsgs Whether to include a hash of the handshake messages so far.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveKey(WOLFSSL* ssl, byte* output, int outputLen,
- const byte* secret, const byte* label, word32 labelLen,
- int hashAlgo, int includeMsgs)
-{
- int ret = 0;
- byte hash[WC_MAX_DIGEST_SIZE];
- word32 hashSz = 0;
- word32 hashOutSz = 0;
- const byte* protocol;
- word32 protocolLen;
- int digestAlg = 0;
-
- switch (hashAlgo) {
- #ifndef NO_SHA256
- case sha256_mac:
- hashSz = WC_SHA256_DIGEST_SIZE;
- digestAlg = WC_SHA256;
- if (includeMsgs)
- ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
- break;
- #endif
-
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- hashSz = WC_SHA384_DIGEST_SIZE;
- digestAlg = WC_SHA384;
- if (includeMsgs)
- ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
- break;
- #endif
-
- #ifdef WOLFSSL_TLS13_SHA512
- case sha512_mac:
- hashSz = WC_SHA512_DIGEST_SIZE;
- digestAlg = WC_SHA512;
- if (includeMsgs)
- ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash);
- break;
- #endif
- }
- if (ret != 0)
- return ret;
-
- /* Only one protocol version defined at this time. */
- protocol = tls13ProtocolLabel;
- protocolLen = TLS13_PROTOCOL_LABEL_SZ;
-
- if (outputLen == -1)
- outputLen = hashSz;
- if (includeMsgs)
- hashOutSz = hashSz;
-
- return HKDF_Expand_Label(output, outputLen, secret, hashSz,
- protocol, protocolLen, label, labelLen,
- hash, hashOutSz, digestAlg);
-}
-
-#ifndef NO_PSK
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the binder key label. */
-#define BINDER_KEY_LABEL_SZ 23
-/* The binder key label. */
-static const byte binderKeyLabel[BINDER_KEY_LABEL_SZ + 1] =
- "external psk binder key";
-#else
-/* The length of the binder key label. */
-#define BINDER_KEY_LABEL_SZ 10
-/* The binder key label. */
-static const byte binderKeyLabel[BINDER_KEY_LABEL_SZ + 1] =
- "ext binder";
-#endif
-/* Derive the binder key.
- *
- * ssl The SSL/TLS object.
- * key The derived key.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveBinderKey(WOLFSSL* ssl, byte* key)
-{
- WOLFSSL_MSG("Derive Binder Key");
- return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret,
- binderKeyLabel, BINDER_KEY_LABEL_SZ,
- NULL, 0, ssl->specs.mac_algorithm);
-}
-#endif /* !NO_PSK */
-
-#ifdef HAVE_SESSION_TICKET
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the binder key resume label. */
-#define BINDER_KEY_RESUME_LABEL_SZ 25
-/* The binder key resume label. */
-static const byte binderKeyResumeLabel[BINDER_KEY_RESUME_LABEL_SZ + 1] =
- "resumption psk binder key";
-#else
-/* The length of the binder key resume label. */
-#define BINDER_KEY_RESUME_LABEL_SZ 10
-/* The binder key resume label. */
-static const byte binderKeyResumeLabel[BINDER_KEY_RESUME_LABEL_SZ + 1] =
- "res binder";
-#endif
-/* Derive the binder resumption key.
- *
- * ssl The SSL/TLS object.
- * key The derived key.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveBinderKeyResume(WOLFSSL* ssl, byte* key)
-{
- WOLFSSL_MSG("Derive Binder Key - Resumption");
- return DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret,
- binderKeyResumeLabel, BINDER_KEY_RESUME_LABEL_SZ,
- NULL, 0, ssl->specs.mac_algorithm);
-}
-#endif /* HAVE_SESSION_TICKET */
-
-#ifdef WOLFSSL_EARLY_DATA
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the early traffic label. */
-#define EARLY_TRAFFIC_LABEL_SZ 27
-/* The early traffic label. */
-static const byte earlyTrafficLabel[EARLY_TRAFFIC_LABEL_SZ + 1] =
- "client early traffic secret";
-#else
-/* The length of the early traffic label. */
-#define EARLY_TRAFFIC_LABEL_SZ 11
-/* The early traffic label. */
-static const byte earlyTrafficLabel[EARLY_TRAFFIC_LABEL_SZ + 1] =
- "c e traffic";
-#endif
-/* Derive the early traffic key.
- *
- * ssl The SSL/TLS object.
- * key The derived key.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveEarlyTrafficSecret(WOLFSSL* ssl, byte* key)
-{
- int ret;
- WOLFSSL_MSG("Derive Early Traffic Secret");
- ret = DeriveKey(ssl, key, -1, ssl->arrays->secret,
- earlyTrafficLabel, EARLY_TRAFFIC_LABEL_SZ,
- ssl->specs.mac_algorithm, 1);
-#ifdef HAVE_SECRET_CALLBACK
- if (ret == 0 && ssl->tls13SecretCb != NULL) {
- ret = ssl->tls13SecretCb(ssl, CLIENT_EARLY_TRAFFIC_SECRET, key,
- ssl->specs.hash_size, ssl->tls13SecretCtx);
- if (ret != 0) {
- return TLS13_SECRET_CB_E;
- }
- }
-#endif /* HAVE_SECRET_CALLBACK */
- return ret;
-}
-
-#ifdef TLS13_SUPPORTS_EXPORTERS
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the early exporter label. */
-#define EARLY_EXPORTER_LABEL_SZ 28
-/* The early exporter label. */
-static const byte earlyExporterLabel[EARLY_EXPORTER_LABEL_SZ + 1] =
- "early exporter master secret";
-#else
-/* The length of the early exporter label. */
-#define EARLY_EXPORTER_LABEL_SZ 12
-/* The early exporter label. */
-static const byte earlyExporterLabel[EARLY_EXPORTER_LABEL_SZ + 1] =
- "e exp master";
-#endif
-/* Derive the early exporter key.
- *
- * ssl The SSL/TLS object.
- * key The derived key.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveEarlyExporterSecret(WOLFSSL* ssl, byte* key)
-{
- int ret;
- WOLFSSL_MSG("Derive Early Exporter Secret");
- ret = DeriveKey(ssl, key, -1, ssl->arrays->secret,
- earlyExporterLabel, EARLY_EXPORTER_LABEL_SZ,
- ssl->specs.mac_algorithm, 1);
-#ifdef HAVE_SECRET_CALLBACK
- if (ret == 0 && ssl->tls13SecretCb != NULL) {
- ret = ssl->tls13SecretCb(ssl, EARLY_EXPORTER_SECRET, key
- ssl->specs.hash_size, ssl->tls13SecretCtx);
- if (ret != 0) {
- return TLS13_SECRET_CB_E;
- }
- }
-#endif /* HAVE_SECRET_CALLBACK */
- return ret;
-}
-#endif
-#endif
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the client handshake label. */
-#define CLIENT_HANDSHAKE_LABEL_SZ 31
-/* The client handshake label. */
-static const byte clientHandshakeLabel[CLIENT_HANDSHAKE_LABEL_SZ + 1] =
- "client handshake traffic secret";
-#else
-/* The length of the client handshake label. */
-#define CLIENT_HANDSHAKE_LABEL_SZ 12
-/* The client handshake label. */
-static const byte clientHandshakeLabel[CLIENT_HANDSHAKE_LABEL_SZ + 1] =
- "c hs traffic";
-#endif
-/* Derive the client handshake key.
- *
- * ssl The SSL/TLS object.
- * key The derived key.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveClientHandshakeSecret(WOLFSSL* ssl, byte* key)
-{
- int ret;
- WOLFSSL_MSG("Derive Client Handshake Secret");
- ret = DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret,
- clientHandshakeLabel, CLIENT_HANDSHAKE_LABEL_SZ,
- ssl->specs.mac_algorithm, 1);
-#ifdef HAVE_SECRET_CALLBACK
- if (ret == 0 && ssl->tls13SecretCb != NULL) {
- ret = ssl->tls13SecretCb(ssl, CLIENT_HANDSHAKE_TRAFFIC_SECRET, key,
- ssl->specs.hash_size, ssl->tls13SecretCtx);
- if (ret != 0) {
- return TLS13_SECRET_CB_E;
- }
- }
-#endif /* HAVE_SECRET_CALLBACK */
- return ret;
-}
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the server handshake label. */
-#define SERVER_HANDSHAKE_LABEL_SZ 31
-/* The server handshake label. */
-static const byte serverHandshakeLabel[SERVER_HANDSHAKE_LABEL_SZ + 1] =
- "server handshake traffic secret";
-#else
-/* The length of the server handshake label. */
-#define SERVER_HANDSHAKE_LABEL_SZ 12
-/* The server handshake label. */
-static const byte serverHandshakeLabel[SERVER_HANDSHAKE_LABEL_SZ + 1] =
- "s hs traffic";
-#endif
-/* Derive the server handshake key.
- *
- * ssl The SSL/TLS object.
- * key The derived key.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveServerHandshakeSecret(WOLFSSL* ssl, byte* key)
-{
- int ret;
- WOLFSSL_MSG("Derive Server Handshake Secret");
- ret = DeriveKey(ssl, key, -1, ssl->arrays->preMasterSecret,
- serverHandshakeLabel, SERVER_HANDSHAKE_LABEL_SZ,
- ssl->specs.mac_algorithm, 1);
-#ifdef HAVE_SECRET_CALLBACK
- if (ret == 0 && ssl->tls13SecretCb != NULL) {
- ret = ssl->tls13SecretCb(ssl, SERVER_HANDSHAKE_TRAFFIC_SECRET, key,
- ssl->specs.hash_size, ssl->tls13SecretCtx);
- if (ret != 0) {
- return TLS13_SECRET_CB_E;
- }
- }
-#endif /* HAVE_SECRET_CALLBACK */
- return ret;
-}
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the client application traffic label. */
-#define CLIENT_APP_LABEL_SZ 33
-/* The client application traffic label. */
-static const byte clientAppLabel[CLIENT_APP_LABEL_SZ + 1] =
- "client application traffic secret";
-#else
-/* The length of the client application traffic label. */
-#define CLIENT_APP_LABEL_SZ 12
-/* The client application traffic label. */
-static const byte clientAppLabel[CLIENT_APP_LABEL_SZ + 1] =
- "c ap traffic";
-#endif
-/* Derive the client application traffic key.
- *
- * ssl The SSL/TLS object.
- * key The derived key.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveClientTrafficSecret(WOLFSSL* ssl, byte* key)
-{
- int ret;
- WOLFSSL_MSG("Derive Client Traffic Secret");
- ret = DeriveKey(ssl, key, -1, ssl->arrays->masterSecret,
- clientAppLabel, CLIENT_APP_LABEL_SZ,
- ssl->specs.mac_algorithm, 1);
-#ifdef HAVE_SECRET_CALLBACK
- if (ret == 0 && ssl->tls13SecretCb != NULL) {
- ret = ssl->tls13SecretCb(ssl, CLIENT_TRAFFIC_SECRET, key,
- ssl->specs.hash_size, ssl->tls13SecretCtx);
- if (ret != 0) {
- return TLS13_SECRET_CB_E;
- }
- }
-#endif /* HAVE_SECRET_CALLBACK */
- return ret;
-}
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the server application traffic label. */
-#define SERVER_APP_LABEL_SZ 33
-/* The server application traffic label. */
-static const byte serverAppLabel[SERVER_APP_LABEL_SZ + 1] =
- "server application traffic secret";
-#else
-/* The length of the server application traffic label. */
-#define SERVER_APP_LABEL_SZ 12
-/* The server application traffic label. */
-static const byte serverAppLabel[SERVER_APP_LABEL_SZ + 1] =
- "s ap traffic";
-#endif
-/* Derive the server application traffic key.
- *
- * ssl The SSL/TLS object.
- * key The derived key.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveServerTrafficSecret(WOLFSSL* ssl, byte* key)
-{
- int ret;
- WOLFSSL_MSG("Derive Server Traffic Secret");
- ret = DeriveKey(ssl, key, -1, ssl->arrays->masterSecret,
- serverAppLabel, SERVER_APP_LABEL_SZ,
- ssl->specs.mac_algorithm, 1);
-#ifdef HAVE_SECRET_CALLBACK
- if (ret == 0 && ssl->tls13SecretCb != NULL) {
- ret = ssl->tls13SecretCb(ssl, SERVER_TRAFFIC_SECRET, key,
- ssl->specs.hash_size, ssl->tls13SecretCtx);
- if (ret != 0) {
- return TLS13_SECRET_CB_E;
- }
- }
-#endif /* HAVE_SECRET_CALLBACK */
- return ret;
-}
-
-#ifdef TLS13_SUPPORTS_EXPORTERS
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the exporter master secret label. */
-#define EXPORTER_MASTER_LABEL_SZ 22
-/* The exporter master secret label. */
-static const byte exporterMasterLabel[EXPORTER_MASTER_LABEL_SZ + 1] =
- "exporter master secret";
-#else
-/* The length of the exporter master secret label. */
-#define EXPORTER_MASTER_LABEL_SZ 10
-/* The exporter master secret label. */
-static const byte exporterMasterLabel[EXPORTER_MASTER_LABEL_SZ + 1] =
- "exp master";
-#endif
-/* Derive the exporter secret.
- *
- * ssl The SSL/TLS object.
- * key The derived key.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveExporterSecret(WOLFSSL* ssl, byte* key)
-{
- int ret;
- WOLFSSL_MSG("Derive Exporter Secret");
- ret = DeriveKey(ssl, key, -1, ssl->arrays->masterSecret,
- exporterMasterLabel, EXPORTER_MASTER_LABEL_SZ,
- ssl->specs.mac_algorithm, 1);
-#ifdef HAVE_SECRET_CALLBACK
- if (ret == 0 && ssl->tls13SecretCb != NULL) {
- ret = ssl->tls13SecretCb(ssl, EXPORTER_SECRET, key,
- ssl->specs.hash_size, ssl->tls13SecretCtx);
- if (ret != 0) {
- return TLS13_SECRET_CB_E;
- }
- }
-#endif /* HAVE_SECRET_CALLBACK */
- return ret;
-}
-#endif
-
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the resumption master secret label. */
-#define RESUME_MASTER_LABEL_SZ 24
-/* The resumption master secret label. */
-static const byte resumeMasterLabel[RESUME_MASTER_LABEL_SZ + 1] =
- "resumption master secret";
-#else
-/* The length of the resumption master secret label. */
-#define RESUME_MASTER_LABEL_SZ 10
-/* The resumption master secret label. */
-static const byte resumeMasterLabel[RESUME_MASTER_LABEL_SZ + 1] =
- "res master";
-#endif
-/* Derive the resumption secret.
- *
- * ssl The SSL/TLS object.
- * key The derived key.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveResumptionSecret(WOLFSSL* ssl, byte* key)
-{
- WOLFSSL_MSG("Derive Resumption Secret");
- return DeriveKey(ssl, key, -1, ssl->arrays->masterSecret,
- resumeMasterLabel, RESUME_MASTER_LABEL_SZ,
- ssl->specs.mac_algorithm, 1);
-}
-#endif
-
-/* Length of the finished label. */
-#define FINISHED_LABEL_SZ 8
-/* Finished label for generating finished key. */
-static const byte finishedLabel[FINISHED_LABEL_SZ+1] = "finished";
-/* Derive the finished secret.
- *
- * ssl The SSL/TLS object.
- * key The key to use with the HMAC.
- * secret The derived secret.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveFinishedSecret(WOLFSSL* ssl, byte* key, byte* secret)
-{
- WOLFSSL_MSG("Derive Finished Secret");
- return DeriveKey(ssl, secret, -1, key, finishedLabel, FINISHED_LABEL_SZ,
- ssl->specs.mac_algorithm, 0);
-}
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* The length of the application traffic label. */
-#define APP_TRAFFIC_LABEL_SZ 26
-/* The application traffic label. */
-static const byte appTrafficLabel[APP_TRAFFIC_LABEL_SZ + 1] =
- "application traffic secret";
-#else
-/* The length of the application traffic label. */
-#define APP_TRAFFIC_LABEL_SZ 11
-/* The application traffic label. */
-static const byte appTrafficLabel[APP_TRAFFIC_LABEL_SZ + 1] =
- "traffic upd";
-#endif
-/* Update the traffic secret.
- *
- * ssl The SSL/TLS object.
- * secret The previous secret and derived secret.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveTrafficSecret(WOLFSSL* ssl, byte* secret)
-{
- WOLFSSL_MSG("Derive New Application Traffic Secret");
- return DeriveKey(ssl, secret, -1, secret,
- appTrafficLabel, APP_TRAFFIC_LABEL_SZ,
- ssl->specs.mac_algorithm, 0);
-}
-
-/* Derive the early secret using HKDF Extract.
- *
- * ssl The SSL/TLS object.
- */
-static int DeriveEarlySecret(WOLFSSL* ssl)
-{
- WOLFSSL_MSG("Derive Early Secret");
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- return Tls13_HKDF_Extract(ssl->arrays->secret, NULL, 0,
- ssl->arrays->psk_key, ssl->arrays->psk_keySz,
- ssl->specs.mac_algorithm);
-#else
- return Tls13_HKDF_Extract(ssl->arrays->secret, NULL, 0,
- ssl->arrays->masterSecret, 0, ssl->specs.mac_algorithm);
-#endif
-}
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
-/* The length of the derived label. */
-#define DERIVED_LABEL_SZ 7
-/* The derived label. */
-static const byte derivedLabel[DERIVED_LABEL_SZ + 1] =
- "derived";
-#endif
-/* Derive the handshake secret using HKDF Extract.
- *
- * ssl The SSL/TLS object.
- */
-static int DeriveHandshakeSecret(WOLFSSL* ssl)
-{
-#ifdef WOLFSSL_TLS13_DRAFT_18
- WOLFSSL_MSG("Derive Handshake Secret");
- return Tls13_HKDF_Extract(ssl->arrays->preMasterSecret,
- ssl->arrays->secret, ssl->specs.hash_size,
- ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
- ssl->specs.mac_algorithm);
-#else
- byte key[WC_MAX_DIGEST_SIZE];
- int ret;
-
- WOLFSSL_MSG("Derive Handshake Secret");
-
- ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->secret,
- derivedLabel, DERIVED_LABEL_SZ,
- NULL, 0, ssl->specs.mac_algorithm);
- if (ret != 0)
- return ret;
-
- return Tls13_HKDF_Extract(ssl->arrays->preMasterSecret,
- key, ssl->specs.hash_size,
- ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
- ssl->specs.mac_algorithm);
-#endif
-}
-
-/* Derive the master secret using HKDF Extract.
- *
- * ssl The SSL/TLS object.
- */
-static int DeriveMasterSecret(WOLFSSL* ssl)
-{
-#ifdef WOLFSSL_TLS13_DRAFT_18
- WOLFSSL_MSG("Derive Master Secret");
- return Tls13_HKDF_Extract(ssl->arrays->masterSecret,
- ssl->arrays->preMasterSecret, ssl->specs.hash_size,
- ssl->arrays->masterSecret, 0, ssl->specs.mac_algorithm);
-#else
- byte key[WC_MAX_DIGEST_SIZE];
- int ret;
-
- WOLFSSL_MSG("Derive Master Secret");
-
- ret = DeriveKeyMsg(ssl, key, -1, ssl->arrays->preMasterSecret,
- derivedLabel, DERIVED_LABEL_SZ,
- NULL, 0, ssl->specs.mac_algorithm);
- if (ret != 0)
- return ret;
-
- return Tls13_HKDF_Extract(ssl->arrays->masterSecret,
- key, ssl->specs.hash_size,
- ssl->arrays->masterSecret, 0, ssl->specs.mac_algorithm);
-#endif
-}
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
-#if defined(HAVE_SESSION_TICKET)
-/* Length of the resumption label. */
-#define RESUMPTION_LABEL_SZ 10
-/* Resumption label for generating PSK associated with the ticket. */
-static const byte resumptionLabel[RESUMPTION_LABEL_SZ+1] = "resumption";
-/* Derive the PSK associated with the ticket.
- *
- * ssl The SSL/TLS object.
- * nonce The nonce to derive with.
- * nonceLen The length of the nonce to derive with.
- * secret The derived secret.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveResumptionPSK(WOLFSSL* ssl, byte* nonce, byte nonceLen,
- byte* secret)
-{
- int digestAlg;
- /* Only one protocol version defined at this time. */
- const byte* protocol = tls13ProtocolLabel;
- word32 protocolLen = TLS13_PROTOCOL_LABEL_SZ;
-
- WOLFSSL_MSG("Derive Resumption PSK");
-
- switch (ssl->specs.mac_algorithm) {
- #ifndef NO_SHA256
- case sha256_mac:
- digestAlg = WC_SHA256;
- break;
- #endif
-
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- digestAlg = WC_SHA384;
- break;
- #endif
-
- #ifdef WOLFSSL_TLS13_SHA512
- case sha512_mac:
- digestAlg = WC_SHA512;
- break;
- #endif
-
- default:
- return BAD_FUNC_ARG;
- }
-
- return HKDF_Expand_Label(secret, ssl->specs.hash_size,
- ssl->session.masterSecret, ssl->specs.hash_size,
- protocol, protocolLen, resumptionLabel,
- RESUMPTION_LABEL_SZ, nonce, nonceLen, digestAlg);
-}
-#endif /* HAVE_SESSION_TICKET */
-#endif /* WOLFSSL_TLS13_DRAFT_18 */
-
-
-/* Calculate the HMAC of message data to this point.
- *
- * ssl The SSL/TLS object.
- * key The HMAC key.
- * hash The hash result - verify data.
- * returns length of verify data generated.
- */
-static int BuildTls13HandshakeHmac(WOLFSSL* ssl, byte* key, byte* hash,
- word32* pHashSz)
-{
- Hmac verifyHmac;
- int hashType = WC_SHA256;
- int hashSz = WC_SHA256_DIGEST_SIZE;
- int ret = BAD_FUNC_ARG;
-
- /* Get the hash of the previous handshake messages. */
- switch (ssl->specs.mac_algorithm) {
- #ifndef NO_SHA256
- case sha256_mac:
- hashType = WC_SHA256;
- hashSz = WC_SHA256_DIGEST_SIZE;
- ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
- break;
- #endif /* !NO_SHA256 */
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- hashType = WC_SHA384;
- hashSz = WC_SHA384_DIGEST_SIZE;
- ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
- break;
- #endif /* WOLFSSL_SHA384 */
- #ifdef WOLFSSL_TLS13_SHA512
- case sha512_mac:
- hashType = WC_SHA512;
- hashSz = WC_SHA512_DIGEST_SIZE;
- ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash);
- break;
- #endif /* WOLFSSL_TLS13_SHA512 */
- }
- if (ret != 0)
- return ret;
-
- /* Calculate the verify data. */
- ret = wc_HmacInit(&verifyHmac, ssl->heap, ssl->devId);
- if (ret == 0) {
- ret = wc_HmacSetKey(&verifyHmac, hashType, key, ssl->specs.hash_size);
- if (ret == 0)
- ret = wc_HmacUpdate(&verifyHmac, hash, hashSz);
- if (ret == 0)
- ret = wc_HmacFinal(&verifyHmac, hash);
- wc_HmacFree(&verifyHmac);
- }
-
- if (pHashSz)
- *pHashSz = hashSz;
-
- return ret;
-}
-
-/* The length of the label to use when deriving keys. */
-#define WRITE_KEY_LABEL_SZ 3
-/* The length of the label to use when deriving IVs. */
-#define WRITE_IV_LABEL_SZ 2
-/* The label to use when deriving keys. */
-static const byte writeKeyLabel[WRITE_KEY_LABEL_SZ+1] = "key";
-/* The label to use when deriving IVs. */
-static const byte writeIVLabel[WRITE_IV_LABEL_SZ+1] = "iv";
-
-/* Derive the keys and IVs for TLS v1.3.
- *
- * ssl The SSL/TLS object.
- * sercret early_data_key when deriving the key and IV for encrypting early
- * data application data and end_of_early_data messages.
- * handshake_key when deriving keys and IVs for encrypting handshake
- * messages.
- * traffic_key when deriving first keys and IVs for encrypting
- * traffic messages.
- * update_traffic_key when deriving next keys and IVs for encrypting
- * traffic messages.
- * side ENCRYPT_SIDE_ONLY when only encryption secret needs to be derived.
- * DECRYPT_SIDE_ONLY when only decryption secret needs to be derived.
- * ENCRYPT_AND_DECRYPT_SIDE when both secret needs to be derived.
- * store 1 indicates to derive the keys and IVs from derived secret and
- * store ready for provisioning.
- * returns 0 on success, otherwise failure.
- */
-static int DeriveTls13Keys(WOLFSSL* ssl, int secret, int side, int store)
-{
- int ret = BAD_FUNC_ARG; /* Assume failure */
- int i = 0;
-#ifdef WOLFSSL_SMALL_STACK
- byte* key_dig;
-#else
- byte key_dig[MAX_PRF_DIG];
-#endif
- int provision;
-
-#ifdef WOLFSSL_SMALL_STACK
- key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST);
- if (key_dig == NULL)
- return MEMORY_E;
-#endif
-
- if (side == ENCRYPT_AND_DECRYPT_SIDE) {
- provision = PROVISION_CLIENT_SERVER;
- }
- else {
- provision = ((ssl->options.side != WOLFSSL_CLIENT_END) ^
- (side == ENCRYPT_SIDE_ONLY)) ? PROVISION_CLIENT :
- PROVISION_SERVER;
- }
-
- /* Derive the appropriate secret to use in the HKDF. */
- switch (secret) {
-#ifdef WOLFSSL_EARLY_DATA
- case early_data_key:
- ret = DeriveEarlyTrafficSecret(ssl, ssl->clientSecret);
- if (ret != 0)
- goto end;
- break;
-#endif
-
- case handshake_key:
- if (provision & PROVISION_CLIENT) {
- ret = DeriveClientHandshakeSecret(ssl,
- ssl->clientSecret);
- if (ret != 0)
- goto end;
- }
- if (provision & PROVISION_SERVER) {
- ret = DeriveServerHandshakeSecret(ssl,
- ssl->serverSecret);
- if (ret != 0)
- goto end;
- }
- break;
-
- case traffic_key:
- if (provision & PROVISION_CLIENT) {
- ret = DeriveClientTrafficSecret(ssl, ssl->clientSecret);
- if (ret != 0)
- goto end;
- }
- if (provision & PROVISION_SERVER) {
- ret = DeriveServerTrafficSecret(ssl, ssl->serverSecret);
- if (ret != 0)
- goto end;
- }
- break;
-
- case update_traffic_key:
- if (provision & PROVISION_CLIENT) {
- ret = DeriveTrafficSecret(ssl, ssl->clientSecret);
- if (ret != 0)
- goto end;
- }
- if (provision & PROVISION_SERVER) {
- ret = DeriveTrafficSecret(ssl, ssl->serverSecret);
- if (ret != 0)
- goto end;
- }
- break;
- }
-
- if (!store)
- goto end;
-
- /* Key data = client key | server key | client IV | server IV */
-
- if (provision & PROVISION_CLIENT) {
- /* Derive the client key. */
- WOLFSSL_MSG("Derive Client Key");
- ret = DeriveKey(ssl, &key_dig[i], ssl->specs.key_size,
- ssl->clientSecret, writeKeyLabel,
- WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0);
- if (ret != 0)
- goto end;
- i += ssl->specs.key_size;
- }
-
- if (provision & PROVISION_SERVER) {
- /* Derive the server key. */
- WOLFSSL_MSG("Derive Server Key");
- ret = DeriveKey(ssl, &key_dig[i], ssl->specs.key_size,
- ssl->serverSecret, writeKeyLabel,
- WRITE_KEY_LABEL_SZ, ssl->specs.mac_algorithm, 0);
- if (ret != 0)
- goto end;
- i += ssl->specs.key_size;
- }
-
- if (provision & PROVISION_CLIENT) {
- /* Derive the client IV. */
- WOLFSSL_MSG("Derive Client IV");
- ret = DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size,
- ssl->clientSecret, writeIVLabel,
- WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0);
- if (ret != 0)
- goto end;
- i += ssl->specs.iv_size;
- }
-
- if (provision & PROVISION_SERVER) {
- /* Derive the server IV. */
- WOLFSSL_MSG("Derive Server IV");
- ret = DeriveKey(ssl, &key_dig[i], ssl->specs.iv_size,
- ssl->serverSecret, writeIVLabel,
- WRITE_IV_LABEL_SZ, ssl->specs.mac_algorithm, 0);
- if (ret != 0)
- goto end;
- }
-
- /* Store keys and IVs but don't activate them. */
- ret = StoreKeys(ssl, key_dig, provision);
-
-end:
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
-#endif
-
- return ret;
-}
-
-#ifdef HAVE_SESSION_TICKET
-#if defined(USER_TICKS)
-#if 0
- word32 TimeNowInMilliseconds(void)
- {
- /*
- write your own clock tick function if don't want gettimeofday()
- needs millisecond accuracy but doesn't have to correlated to EPOCH
- */
- }
-#endif
-
-#elif defined(TIME_OVERRIDES)
- #ifndef HAVE_TIME_T_TYPE
- typedef long time_t;
- #endif
- extern time_t XTIME(time_t * timer);
-
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- return (word32) XTIME(0) * 1000;
- }
-
-#elif defined(XTIME_MS)
- word32 TimeNowInMilliseconds(void)
- {
- return (word32)XTIME_MS(0);
- }
-
-#elif defined(USE_WINDOWS_API)
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- static int init = 0;
- static LARGE_INTEGER freq;
- LARGE_INTEGER count;
-
- if (!init) {
- QueryPerformanceFrequency(&freq);
- init = 1;
- }
-
- QueryPerformanceCounter(&count);
-
- return (word32)(count.QuadPart / (freq.QuadPart / 1000));
- }
-
-#elif defined(HAVE_RTP_SYS)
- #include "rtptime.h"
-
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- return (word32)rtp_get_system_sec() * 1000;
- }
-#elif defined(WOLFSSL_DEOS)
- word32 TimeNowInMilliseconds(void)
- {
- const uint32_t systemTickTimeInHz = 1000000 / systemTickInMicroseconds();
- uint32_t *systemTickPtr = systemTickPointer();
-
- return (word32) (*systemTickPtr/systemTickTimeInHz) * 1000;
- }
-#elif defined(MICRIUM)
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- OS_TICK ticks = 0;
- OS_ERR err;
-
- ticks = OSTimeGet(&err);
-
- return (word32) (ticks / OSCfg_TickRate_Hz) * 1000;
- }
-#elif defined(MICROCHIP_TCPIP_V5)
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- return (word32) (TickGet() / (TICKS_PER_SECOND / 1000));
- }
-#elif defined(MICROCHIP_TCPIP)
- #if defined(MICROCHIP_MPLAB_HARMONY)
- #include
-
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- return (word32)(SYS_TMR_TickCountGet() /
- (SYS_TMR_TickCounterFrequencyGet() / 1000));
- }
- #else
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- return (word32)(SYS_TICK_Get() / (SYS_TICK_TicksPerSecondGet() / 1000));
- }
-
- #endif
-
-#elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- TIME_STRUCT mqxTime;
-
- _time_get_elapsed(&mqxTime);
-
- return (word32) mqxTime.SECONDS * 1000;
- }
-#elif defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS)
- #include "include/task.h"
-
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- return (unsigned int)(((float)xTaskGetTickCount()) /
- (configTICK_RATE_HZ / 1000));
- }
-#elif defined(FREESCALE_KSDK_BM)
- #include "lwip/sys.h" /* lwIP */
-
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- return sys_now();
- }
-#elif defined(WOLFSSL_TIRTOS)
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- return (word32) Seconds_get() * 1000;
- }
-#elif defined(WOLFSSL_UTASKER)
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- return (word32)(uTaskerSystemTick / (TICK_RESOLUTION / 1000));
- }
-#else
- /* The time in milliseconds.
- * Used for tickets to represent difference between when first seen and when
- * sending.
- *
- * returns the time in milliseconds as a 32-bit value.
- */
- word32 TimeNowInMilliseconds(void)
- {
- struct timeval now;
-
- if (gettimeofday(&now, 0) < 0)
- return GETTIME_ERROR;
- /* Convert to milliseconds number. */
- return (word32)(now.tv_sec * 1000 + now.tv_usec / 1000);
- }
-#endif
-#endif /* HAVE_SESSION_TICKET || !NO_PSK */
-
-
-#if !defined(NO_WOLFSSL_SERVER) && (defined(HAVE_SESSION_TICKET) || \
- !defined(NO_PSK))
-/* Add input to all handshake hashes.
- *
- * ssl The SSL/TLS object.
- * input The data to hash.
- * sz The size of the data to hash.
- * returns 0 on success, otherwise failure.
- */
-static int HashInputRaw(WOLFSSL* ssl, const byte* input, int sz)
-{
- int ret = BAD_FUNC_ARG;
-
-#ifndef NO_SHA256
- ret = wc_Sha256Update(&ssl->hsHashes->hashSha256, input, sz);
- if (ret != 0)
- return ret;
-#endif
-#ifdef WOLFSSL_SHA384
- ret = wc_Sha384Update(&ssl->hsHashes->hashSha384, input, sz);
- if (ret != 0)
- return ret;
-#endif
-#ifdef WOLFSSL_TLS13_SHA512
- ret = wc_Sha512Update(&ssl->hsHashes->hashSha512, input, sz);
- if (ret != 0)
- return ret;
-#endif
-
- return ret;
-}
-#endif
-
-/* Extract the handshake header information.
- *
- * ssl The SSL/TLS object.
- * input The buffer holding the message data.
- * inOutIdx On entry, the index into the buffer of the handshake data.
- * On exit, the start of the handshake data.
- * type Type of handshake message.
- * size The length of the handshake message data.
- * totalSz The total size of data in the buffer.
- * returns BUFFER_E if there is not enough input data and 0 on success.
- */
-static int GetHandshakeHeader(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- byte* type, word32* size, word32 totalSz)
-{
- const byte* ptr = input + *inOutIdx;
- (void)ssl;
-
- *inOutIdx += HANDSHAKE_HEADER_SZ;
- if (*inOutIdx > totalSz)
- return BUFFER_E;
-
- *type = ptr[0];
- c24to32(&ptr[1], size);
-
- return 0;
-}
-
-/* Add record layer header to message.
- *
- * output The buffer to write the record layer header into.
- * length The length of the record data.
- * type The type of record message.
- * ssl The SSL/TLS object.
- */
-static void AddTls13RecordHeader(byte* output, word32 length, byte type,
- WOLFSSL* ssl)
-{
- RecordLayerHeader* rl;
-
- rl = (RecordLayerHeader*)output;
- rl->type = type;
- rl->pvMajor = ssl->version.major;
-#ifdef WOLFSSL_TLS13_DRAFT_18
- rl->pvMinor = TLSv1_MINOR;
-#else
- /* NOTE: May be TLSv1_MINOR when sending first ClientHello. */
- rl->pvMinor = TLSv1_2_MINOR;
-#endif
- c16toa((word16)length, rl->length);
-}
-
-/* Add handshake header to message.
- *
- * output The buffer to write the handshake header into.
- * length The length of the handshake data.
- * fragOffset The offset of the fragment data. (DTLS)
- * fragLength The length of the fragment data. (DTLS)
- * type The type of handshake message.
- * ssl The SSL/TLS object. (DTLS)
- */
-static void AddTls13HandShakeHeader(byte* output, word32 length,
- word32 fragOffset, word32 fragLength,
- byte type, WOLFSSL* ssl)
-{
- HandShakeHeader* hs;
- (void)fragOffset;
- (void)fragLength;
- (void)ssl;
-
- /* handshake header */
- hs = (HandShakeHeader*)output;
- hs->type = type;
- c32to24(length, hs->length);
-}
-
-
-/* Add both record layer and handshake header to message.
- *
- * output The buffer to write the headers into.
- * length The length of the handshake data.
- * type The type of record layer message.
- * ssl The SSL/TLS object. (DTLS)
- */
-static void AddTls13Headers(byte* output, word32 length, byte type,
- WOLFSSL* ssl)
-{
- word32 lengthAdj = HANDSHAKE_HEADER_SZ;
- word32 outputAdj = RECORD_HEADER_SZ;
-
- AddTls13RecordHeader(output, length + lengthAdj, handshake, ssl);
- AddTls13HandShakeHeader(output + outputAdj, length, 0, length, type, ssl);
-}
-
-
-#ifndef NO_CERTS
-/* Add both record layer and fragment handshake header to message.
- *
- * output The buffer to write the headers into.
- * fragOffset The offset of the fragment data. (DTLS)
- * fragLength The length of the fragment data. (DTLS)
- * length The length of the handshake data.
- * type The type of record layer message.
- * ssl The SSL/TLS object. (DTLS)
- */
-static void AddTls13FragHeaders(byte* output, word32 fragSz, word32 fragOffset,
- word32 length, byte type, WOLFSSL* ssl)
-{
- word32 lengthAdj = HANDSHAKE_HEADER_SZ;
- word32 outputAdj = RECORD_HEADER_SZ;
- (void)fragSz;
-
- AddTls13RecordHeader(output, fragSz + lengthAdj, handshake, ssl);
- AddTls13HandShakeHeader(output + outputAdj, length, fragOffset, fragSz,
- type, ssl);
-}
-#endif /* NO_CERTS */
-
-/* Write the sequence number into the buffer.
- * No DTLS v1.3 support.
- *
- * ssl The SSL/TLS object.
- * verifyOrder Which set of sequence numbers to use.
- * out The buffer to write into.
- */
-static WC_INLINE void WriteSEQ(WOLFSSL* ssl, int verifyOrder, byte* out)
-{
- word32 seq[2] = {0, 0};
-
- if (verifyOrder) {
- seq[0] = ssl->keys.peer_sequence_number_hi;
- seq[1] = ssl->keys.peer_sequence_number_lo++;
- /* handle rollover */
- if (seq[1] > ssl->keys.peer_sequence_number_lo)
- ssl->keys.peer_sequence_number_hi++;
- }
- else {
- seq[0] = ssl->keys.sequence_number_hi;
- seq[1] = ssl->keys.sequence_number_lo++;
- /* handle rollover */
- if (seq[1] > ssl->keys.sequence_number_lo)
- ssl->keys.sequence_number_hi++;
- }
-
- c32toa(seq[0], out);
- c32toa(seq[1], out + OPAQUE32_LEN);
-}
-
-/* Build the nonce for TLS v1.3 encryption and decryption.
- *
- * ssl The SSL/TLS object.
- * nonce The nonce data to use when encrypting or decrypting.
- * iv The derived IV.
- * order The side on which the message is to be or was sent.
- */
-static WC_INLINE void BuildTls13Nonce(WOLFSSL* ssl, byte* nonce, const byte* iv,
- int order)
-{
- int i;
-
- /* The nonce is the IV with the sequence XORed into the last bytes. */
- WriteSEQ(ssl, order, nonce + AEAD_NONCE_SZ - SEQ_SZ);
- for (i = 0; i < AEAD_NONCE_SZ - SEQ_SZ; i++)
- nonce[i] = iv[i];
- for (; i < AEAD_NONCE_SZ; i++)
- nonce[i] ^= iv[i];
-}
-
-#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
-/* Encrypt with ChaCha20 and create authenication tag with Poly1305.
- *
- * ssl The SSL/TLS object.
- * output The buffer to write encrypted data and authentication tag into.
- * May be the same pointer as input.
- * input The data to encrypt.
- * sz The number of bytes to encrypt.
- * nonce The nonce to use with ChaCha20.
- * aad The additional authentication data.
- * aadSz The size of the addition authentication data.
- * tag The authentication tag buffer.
- * returns 0 on success, otherwise failure.
- */
-static int ChaCha20Poly1305_Encrypt(WOLFSSL* ssl, byte* output,
- const byte* input, word16 sz, byte* nonce,
- const byte* aad, word16 aadSz, byte* tag)
-{
- int ret = 0;
- byte poly[CHACHA20_256_KEY_SIZE];
-
- /* Poly1305 key is 256 bits of zero encrypted with ChaCha20. */
- XMEMSET(poly, 0, sizeof(poly));
-
- /* Set the nonce for ChaCha and get Poly1305 key. */
- ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 0);
- if (ret != 0)
- return ret;
- /* Create Poly1305 key using ChaCha20 keystream. */
- ret = wc_Chacha_Process(ssl->encrypt.chacha, poly, poly, sizeof(poly));
- if (ret != 0)
- return ret;
- ret = wc_Chacha_SetIV(ssl->encrypt.chacha, nonce, 1);
- if (ret != 0)
- return ret;
- /* Encrypt the plain text. */
- ret = wc_Chacha_Process(ssl->encrypt.chacha, output, input, sz);
- if (ret != 0) {
- ForceZero(poly, sizeof(poly));
- return ret;
- }
-
- /* Set key for Poly1305. */
- ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly, sizeof(poly));
- ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */
- if (ret != 0)
- return ret;
- /* Add authentication code of encrypted data to end. */
- ret = wc_Poly1305_MAC(ssl->auth.poly1305, (byte*)aad, aadSz, output, sz,
- tag, POLY1305_AUTH_SZ);
-
- return ret;
-}
-#endif
-
-#ifdef HAVE_NULL_CIPHER
-/* Create authenication tag and copy data over input.
- *
- * ssl The SSL/TLS object.
- * output The buffer to copy data into.
- * May be the same pointer as input.
- * input The data.
- * sz The number of bytes of data.
- * nonce The nonce to use with authentication.
- * aad The additional authentication data.
- * aadSz The size of the addition authentication data.
- * tag The authentication tag buffer.
- * returns 0 on success, otherwise failure.
- */
-static int Tls13IntegrityOnly_Encrypt(WOLFSSL* ssl, byte* output,
- const byte* input, word16 sz,
- const byte* nonce,
- const byte* aad, word16 aadSz, byte* tag)
-{
- int ret;
-
- /* HMAC: nonce | aad | input */
- ret = wc_HmacUpdate(ssl->encrypt.hmac, nonce, HMAC_NONCE_SZ);
- if (ret == 0)
- ret = wc_HmacUpdate(ssl->encrypt.hmac, aad, aadSz);
- if (ret == 0)
- ret = wc_HmacUpdate(ssl->encrypt.hmac, input, sz);
- if (ret == 0)
- ret = wc_HmacFinal(ssl->encrypt.hmac, tag);
- /* Copy the input to output if not the same buffer */
- if (ret == 0 && output != input)
- XMEMCPY(output, input, sz);
-
- return ret;
-}
-#endif
-
-/* Encrypt data for TLS v1.3.
- *
- * ssl The SSL/TLS object.
- * output The buffer to write encrypted data and authentication tag into.
- * May be the same pointer as input.
- * input The record header and data to encrypt.
- * sz The number of bytes to encrypt.
- * aad The additional authentication data.
- * aadSz The size of the addition authentication data.
- * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto
- * returns 0 on success, otherwise failure.
- */
-static int EncryptTls13(WOLFSSL* ssl, byte* output, const byte* input,
- word16 sz, const byte* aad, word16 aadSz, int asyncOkay)
-{
- int ret = 0;
- word16 dataSz = sz - ssl->specs.aead_mac_size;
- word16 macSz = ssl->specs.aead_mac_size;
- word32 nonceSz = 0;
-#ifdef WOLFSSL_ASYNC_CRYPT
- WC_ASYNC_DEV* asyncDev = NULL;
- word32 event_flags = WC_ASYNC_FLAG_CALL_AGAIN;
-#endif
-
- WOLFSSL_ENTER("EncryptTls13");
-
- (void)output;
- (void)input;
- (void)sz;
- (void)dataSz;
- (void)macSz;
- (void)asyncOkay;
- (void)nonceSz;
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ssl->error == WC_PENDING_E) {
- ssl->error = 0; /* clear async */
- }
-#endif
-
- switch (ssl->encrypt.state) {
- case CIPHER_STATE_BEGIN:
- {
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Data to encrypt");
- WOLFSSL_BUFFER(input, dataSz);
-#if !defined(WOLFSSL_TLS13_DRAFT_18) && !defined(WOLFSSL_TLS13_DRAFT_22) && \
- !defined(WOLFSSL_TLS13_DRAFT_23)
- WOLFSSL_MSG("Additional Authentication Data");
- WOLFSSL_BUFFER(aad, aadSz);
-#endif
- #endif
-
- #ifdef CIPHER_NONCE
- if (ssl->encrypt.nonce == NULL)
- ssl->encrypt.nonce = (byte*)XMALLOC(AEAD_NONCE_SZ,
- ssl->heap, DYNAMIC_TYPE_AES_BUFFER);
- if (ssl->encrypt.nonce == NULL)
- return MEMORY_E;
-
- BuildTls13Nonce(ssl, ssl->encrypt.nonce, ssl->keys.aead_enc_imp_IV,
- CUR_ORDER);
- #endif
-
- /* Advance state and proceed */
- ssl->encrypt.state = CIPHER_STATE_DO;
- }
- FALL_THROUGH;
-
- case CIPHER_STATE_DO:
- {
- switch (ssl->specs.bulk_cipher_algorithm) {
- #ifdef BUILD_AESGCM
- case wolfssl_aes_gcm:
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- asyncDev = &ssl->encrypt.aes->asyncDev;
- ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags);
- if (ret != 0)
- break;
- #endif
-
- nonceSz = AESGCM_NONCE_SZ;
- #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)))
- ret = wc_AesGcmEncrypt(ssl->encrypt.aes, output, input,
- dataSz, ssl->encrypt.nonce, nonceSz,
- output + dataSz, macSz, aad, aadSz);
- #else
- ret = wc_AesGcmSetExtIV(ssl->encrypt.aes,
- ssl->encrypt.nonce, nonceSz);
- if (ret == 0) {
- ret = wc_AesGcmEncrypt_ex(ssl->encrypt.aes, output,
- input, dataSz, ssl->encrypt.nonce, nonceSz,
- output + dataSz, macSz, aad, aadSz);
- }
- #endif
- break;
- #endif
-
- #ifdef HAVE_AESCCM
- case wolfssl_aes_ccm:
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- asyncDev = &ssl->encrypt.aes->asyncDev;
- ret = wolfSSL_AsyncInit(ssl, asyncDev, event_flags);
- if (ret != 0)
- break;
- #endif
-
- nonceSz = AESCCM_NONCE_SZ;
- #if ((defined(HAVE_FIPS) || defined(HAVE_SELFTEST)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)))
- ret = wc_AesCcmEncrypt(ssl->encrypt.aes, output, input,
- dataSz, ssl->encrypt.nonce, nonceSz,
- output + dataSz, macSz, aad, aadSz);
- #else
- ret = wc_AesCcmSetNonce(ssl->encrypt.aes,
- ssl->encrypt.nonce, nonceSz);
- if (ret == 0) {
- ret = wc_AesCcmEncrypt_ex(ssl->encrypt.aes, output,
- input, dataSz, ssl->encrypt.nonce, nonceSz,
- output + dataSz, macSz, aad, aadSz);
- }
- #endif
- break;
- #endif
-
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- case wolfssl_chacha:
- ret = ChaCha20Poly1305_Encrypt(ssl, output, input, dataSz,
- ssl->encrypt.nonce, aad, aadSz, output + dataSz);
- break;
- #endif
-
- #ifdef HAVE_NULL_CIPHER
- case wolfssl_cipher_null:
- ret = Tls13IntegrityOnly_Encrypt(ssl, output, input, dataSz,
- ssl->encrypt.nonce, aad, aadSz, output + dataSz);
- break;
- #endif
-
- default:
- WOLFSSL_MSG("wolfSSL Encrypt programming error");
- return ENCRYPT_ERROR;
- }
-
- /* Advance state */
- ssl->encrypt.state = CIPHER_STATE_END;
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- /* if async is not okay, then block */
- if (!asyncOkay) {
- ret = wc_AsyncWait(ret, asyncDev, event_flags);
- }
- else {
- /* If pending, then leave and return will resume below */
- return wolfSSL_AsyncPush(ssl, asyncDev);
- }
- }
- #endif
- }
- FALL_THROUGH;
-
- case CIPHER_STATE_END:
- {
- #ifdef WOLFSSL_DEBUG_TLS
- #ifdef CIPHER_NONCE
- WOLFSSL_MSG("Nonce");
- WOLFSSL_BUFFER(ssl->encrypt.nonce, ssl->specs.iv_size);
- #endif
- WOLFSSL_MSG("Encrypted data");
- WOLFSSL_BUFFER(output, dataSz);
- WOLFSSL_MSG("Authentication Tag");
- WOLFSSL_BUFFER(output + dataSz, macSz);
- #endif
-
- #ifdef CIPHER_NONCE
- ForceZero(ssl->encrypt.nonce, AEAD_NONCE_SZ);
- #endif
-
- break;
- }
- }
-
- /* Reset state */
- ssl->encrypt.state = CIPHER_STATE_BEGIN;
-
- return ret;
-}
-
-#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
-/* Decrypt with ChaCha20 and check authenication tag with Poly1305.
- *
- * ssl The SSL/TLS object.
- * output The buffer to write decrypted data into.
- * May be the same pointer as input.
- * input The data to decrypt.
- * sz The number of bytes to decrypt.
- * nonce The nonce to use with ChaCha20.
- * aad The additional authentication data.
- * aadSz The size of the addition authentication data.
- * tagIn The authentication tag data from packet.
- * returns 0 on success, otherwise failure.
- */
-static int ChaCha20Poly1305_Decrypt(WOLFSSL* ssl, byte* output,
- const byte* input, word16 sz, byte* nonce,
- const byte* aad, word16 aadSz,
- const byte* tagIn)
-{
- int ret;
- byte tag[POLY1305_AUTH_SZ];
- byte poly[CHACHA20_256_KEY_SIZE]; /* generated key for mac */
-
- /* Poly1305 key is 256 bits of zero encrypted with ChaCha20. */
- XMEMSET(poly, 0, sizeof(poly));
-
- /* Set nonce and get Poly1305 key. */
- ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 0);
- if (ret != 0)
- return ret;
- /* Use ChaCha20 keystream to get Poly1305 key for tag. */
- ret = wc_Chacha_Process(ssl->decrypt.chacha, poly, poly, sizeof(poly));
- if (ret != 0)
- return ret;
- ret = wc_Chacha_SetIV(ssl->decrypt.chacha, nonce, 1);
- if (ret != 0)
- return ret;
-
- /* Set key for Poly1305. */
- ret = wc_Poly1305SetKey(ssl->auth.poly1305, poly, sizeof(poly));
- ForceZero(poly, sizeof(poly)); /* done with poly1305 key, clear it */
- if (ret != 0)
- return ret;
- /* Generate authentication tag for encrypted data. */
- if ((ret = wc_Poly1305_MAC(ssl->auth.poly1305, (byte*)aad, aadSz,
- (byte*)input, sz, tag, sizeof(tag))) != 0) {
- return ret;
- }
-
- /* Check tag sent along with packet. */
- if (ConstantCompare(tagIn, tag, POLY1305_AUTH_SZ) != 0) {
- WOLFSSL_MSG("MAC did not match");
- return VERIFY_MAC_ERROR;
- }
-
- /* If the tag was good decrypt message. */
- ret = wc_Chacha_Process(ssl->decrypt.chacha, output, input, sz);
-
- return ret;
-}
-#endif
-
-#ifdef HAVE_NULL_CIPHER
-/* Check HMAC tag and copy over input.
- *
- * ssl The SSL/TLS object.
- * output The buffer to copy data into.
- * May be the same pointer as input.
- * input The data.
- * sz The number of bytes of data.
- * nonce The nonce to use with authentication.
- * aad The additional authentication data.
- * aadSz The size of the addition authentication data.
- * tagIn The authentication tag data from packet.
- * returns 0 on success, otherwise failure.
- */
-static int Tls13IntegrityOnly_Decrypt(WOLFSSL* ssl, byte* output,
- const byte* input, word16 sz,
- const byte* nonce,
- const byte* aad, word16 aadSz,
- const byte* tagIn)
-{
- int ret;
- byte hmac[WC_MAX_DIGEST_SIZE];
-
- /* HMAC: nonce | aad | input */
- ret = wc_HmacUpdate(ssl->decrypt.hmac, nonce, HMAC_NONCE_SZ);
- if (ret == 0)
- ret = wc_HmacUpdate(ssl->decrypt.hmac, aad, aadSz);
- if (ret == 0)
- ret = wc_HmacUpdate(ssl->decrypt.hmac, input, sz);
- if (ret == 0)
- ret = wc_HmacFinal(ssl->decrypt.hmac, hmac);
- /* Check authentication tag matches */
- if (ret == 0 && ConstantCompare(tagIn, hmac, ssl->specs.hash_size) != 0)
- ret = DECRYPT_ERROR;
- /* Copy the input to output if not the same buffer */
- if (ret == 0 && output != input)
- XMEMCPY(output, input, sz);
-
- return ret;
-}
-#endif
-
-/* Decrypt data for TLS v1.3.
- *
- * ssl The SSL/TLS object.
- * output The buffer to write decrypted data into.
- * May be the same pointer as input.
- * input The data to decrypt and authentication tag.
- * sz The length of the encrypted data plus authentication tag.
- * aad The additional authentication data.
- * aadSz The size of the addition authentication data.
- * returns 0 on success, otherwise failure.
- */
-int DecryptTls13(WOLFSSL* ssl, byte* output, const byte* input, word16 sz,
- const byte* aad, word16 aadSz)
-{
- int ret = 0;
- word16 dataSz = sz - ssl->specs.aead_mac_size;
- word16 macSz = ssl->specs.aead_mac_size;
- word32 nonceSz = 0;
-
- WOLFSSL_ENTER("DecryptTls13");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->decrypt.state);
- if (ret != WC_NOT_PENDING_E) {
- /* check for still pending */
- if (ret == WC_PENDING_E)
- return ret;
-
- ssl->error = 0; /* clear async */
-
- /* let failures through so CIPHER_STATE_END logic is run */
- }
- else
-#endif
- {
- /* Reset state */
- ret = 0;
- ssl->decrypt.state = CIPHER_STATE_BEGIN;
- }
-
- (void)output;
- (void)input;
- (void)sz;
- (void)dataSz;
- (void)macSz;
- (void)nonceSz;
-
- switch (ssl->decrypt.state) {
- case CIPHER_STATE_BEGIN:
- {
- #ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Data to decrypt");
- WOLFSSL_BUFFER(input, dataSz);
-#if !defined(WOLFSSL_TLS13_DRAFT_18) && !defined(WOLFSSL_TLS13_DRAFT_22) && \
- !defined(WOLFSSL_TLS13_DRAFT_23)
- WOLFSSL_MSG("Additional Authentication Data");
- WOLFSSL_BUFFER(aad, aadSz);
-#endif
- WOLFSSL_MSG("Authentication tag");
- WOLFSSL_BUFFER(input + dataSz, macSz);
- #endif
-
- #ifdef CIPHER_NONCE
- if (ssl->decrypt.nonce == NULL)
- ssl->decrypt.nonce = (byte*)XMALLOC(AEAD_NONCE_SZ,
- ssl->heap, DYNAMIC_TYPE_AES_BUFFER);
- if (ssl->decrypt.nonce == NULL)
- return MEMORY_E;
-
- BuildTls13Nonce(ssl, ssl->decrypt.nonce, ssl->keys.aead_dec_imp_IV,
- PEER_ORDER);
- #endif
-
- /* Advance state and proceed */
- ssl->decrypt.state = CIPHER_STATE_DO;
- }
- FALL_THROUGH;
-
- case CIPHER_STATE_DO:
- {
- switch (ssl->specs.bulk_cipher_algorithm) {
- #ifdef BUILD_AESGCM
- case wolfssl_aes_gcm:
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev,
- WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- break;
- #endif
-
- nonceSz = AESGCM_NONCE_SZ;
- ret = wc_AesGcmDecrypt(ssl->decrypt.aes, output, input,
- dataSz, ssl->decrypt.nonce, nonceSz,
- input + dataSz, macSz, aad, aadSz);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl,
- &ssl->decrypt.aes->asyncDev);
- }
- #endif
- break;
- #endif
-
- #ifdef HAVE_AESCCM
- case wolfssl_aes_ccm:
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* initialize event */
- ret = wolfSSL_AsyncInit(ssl, &ssl->decrypt.aes->asyncDev,
- WC_ASYNC_FLAG_CALL_AGAIN);
- if (ret != 0)
- break;
- #endif
-
- nonceSz = AESCCM_NONCE_SZ;
- ret = wc_AesCcmDecrypt(ssl->decrypt.aes, output, input,
- dataSz, ssl->decrypt.nonce, nonceSz,
- input + dataSz, macSz, aad, aadSz);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- ret = wolfSSL_AsyncPush(ssl,
- &ssl->decrypt.aes->asyncDev);
- }
- #endif
- break;
- #endif
-
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- case wolfssl_chacha:
- ret = ChaCha20Poly1305_Decrypt(ssl, output, input, dataSz,
- ssl->decrypt.nonce, aad, aadSz, input + dataSz);
- break;
- #endif
-
- #ifdef HAVE_NULL_CIPHER
- case wolfssl_cipher_null:
- ret = Tls13IntegrityOnly_Decrypt(ssl, output, input, dataSz,
- ssl->decrypt.nonce, aad, aadSz, input + dataSz);
- break;
- #endif
- default:
- WOLFSSL_MSG("wolfSSL Decrypt programming error");
- return DECRYPT_ERROR;
- }
-
- /* Advance state */
- ssl->decrypt.state = CIPHER_STATE_END;
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* If pending, leave now */
- if (ret == WC_PENDING_E) {
- return ret;
- }
- #endif
- }
- FALL_THROUGH;
-
- case CIPHER_STATE_END:
- {
- #ifdef WOLFSSL_DEBUG_TLS
- #ifdef CIPHER_NONCE
- WOLFSSL_MSG("Nonce");
- WOLFSSL_BUFFER(ssl->decrypt.nonce, ssl->specs.iv_size);
- #endif
- WOLFSSL_MSG("Decrypted data");
- WOLFSSL_BUFFER(output, dataSz);
- #endif
-
- #ifdef CIPHER_NONCE
- ForceZero(ssl->decrypt.nonce, AEAD_NONCE_SZ);
- #endif
-
- break;
- }
- }
-
-#ifndef WOLFSSL_EARLY_DATA
- if (ret < 0) {
- SendAlert(ssl, alert_fatal, bad_record_mac);
- ret = VERIFY_MAC_ERROR;
- }
-#endif
-
- return ret;
-}
-
-/* Persistable BuildTls13Message arguments */
-typedef struct BuildMsg13Args {
- word32 sz;
- word32 idx;
- word32 headerSz;
- word16 size;
-} BuildMsg13Args;
-
-static void FreeBuildMsg13Args(WOLFSSL* ssl, void* pArgs)
-{
- BuildMsg13Args* args = (BuildMsg13Args*)pArgs;
-
- (void)ssl;
- (void)args;
-
- /* no allocations in BuildTls13Message */
-}
-
-/* Build SSL Message, encrypted.
- * TLS v1.3 encryption is AEAD only.
- *
- * ssl The SSL/TLS object.
- * output The buffer to write record message to.
- * outSz Size of the buffer being written into.
- * input The record data to encrypt (excluding record header).
- * inSz The size of the record data.
- * type The recorder header content type.
- * hashOutput Whether to hash the unencrypted record data.
- * sizeOnly Only want the size of the record message.
- * asyncOkay If non-zero can return WC_PENDING_E, otherwise blocks on crypto
- * returns the size of the encrypted record message or negative value on error.
- */
-int BuildTls13Message(WOLFSSL* ssl, byte* output, int outSz, const byte* input,
- int inSz, int type, int hashOutput, int sizeOnly, int asyncOkay)
-{
- int ret = 0;
- BuildMsg13Args* args;
- BuildMsg13Args lcl_args;
-#ifdef WOLFSSL_ASYNC_CRYPT
- args = (BuildMsg13Args*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
-#endif
-
- WOLFSSL_ENTER("BuildTls13Message");
-
- ret = WC_NOT_PENDING_E;
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (asyncOkay) {
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.buildMsgState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_buildmsg;
- }
- }
- else
-#endif
- {
- args = &lcl_args;
- }
-
- /* Reset state */
- if (ret == WC_NOT_PENDING_E) {
- ret = 0;
- ssl->options.buildMsgState = BUILD_MSG_BEGIN;
- XMEMSET(args, 0, sizeof(BuildMsg13Args));
-
- args->sz = RECORD_HEADER_SZ + inSz;
- args->idx = RECORD_HEADER_SZ;
- args->headerSz = RECORD_HEADER_SZ;
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeBuildMsg13Args;
- #endif
- }
-
- switch (ssl->options.buildMsgState) {
- case BUILD_MSG_BEGIN:
- {
- /* catch mistaken sizeOnly parameter */
- if (sizeOnly) {
- if (output || input) {
- WOLFSSL_MSG("BuildTls13Message with sizeOnly "
- "doesn't need input or output");
- return BAD_FUNC_ARG;
- }
- }
- else if (output == NULL || input == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* Record layer content type at the end of record data. */
- args->sz++;
- /* Authentication data at the end. */
- args->sz += ssl->specs.aead_mac_size;
-
- if (sizeOnly)
- return args->sz;
-
- if (args->sz > (word32)outSz) {
- WOLFSSL_MSG("Oops, want to write past output buffer size");
- return BUFFER_E;
- }
-
- /* Record data length. */
- args->size = (word16)(args->sz - args->headerSz);
- /* Write/update the record header with the new size.
- * Always have the content type as application data for encrypted
- * messages in TLS v1.3.
- */
- AddTls13RecordHeader(output, args->size, application_data, ssl);
-
- /* TLS v1.3 can do in place encryption. */
- if (input != output + args->idx)
- XMEMCPY(output + args->idx, input, inSz);
- args->idx += inSz;
-
- ssl->options.buildMsgState = BUILD_MSG_HASH;
- }
- FALL_THROUGH;
-
- case BUILD_MSG_HASH:
- {
- if (hashOutput) {
- ret = HashOutput(ssl, output, args->headerSz + inSz, 0);
- if (ret != 0)
- goto exit_buildmsg;
- }
-
- /* The real record content type goes at the end of the data. */
- output[args->idx++] = (byte)type;
-
- ssl->options.buildMsgState = BUILD_MSG_ENCRYPT;
- }
- FALL_THROUGH;
-
- case BUILD_MSG_ENCRYPT:
- {
- #ifdef ATOMIC_USER
- if (ssl->ctx->MacEncryptCb) {
- /* User Record Layer Callback handling */
- byte* mac = output + args->idx;
- output += args->headerSz;
-
- ret = ssl->ctx->MacEncryptCb(ssl, mac, output, inSz, type, 0,
- output, output, args->size, ssl->MacEncryptCtx);
- }
- else
- #endif
- {
-#if defined(WOLFSSL_TLS13_DRAFT_18) || defined(WOLFSSL_TLS13_DRAFT_22) || \
- defined(WOLFSSL_TLS13_DRAFT_23)
- output += args->headerSz;
- ret = EncryptTls13(ssl, output, output, args->size, NULL, 0,
- asyncOkay);
-#else
- const byte* aad = output;
- output += args->headerSz;
- ret = EncryptTls13(ssl, output, output, args->size, aad,
- RECORD_HEADER_SZ, asyncOkay);
-#endif
- }
- break;
- }
- }
-
-exit_buildmsg:
-
- WOLFSSL_LEAVE("BuildTls13Message", ret);
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- return ret;
- }
-#endif
-
- /* make sure build message state is reset */
- ssl->options.buildMsgState = BUILD_MSG_BEGIN;
-
- /* return sz on success */
- if (ret == 0)
- ret = args->sz;
-
- /* Final cleanup */
- FreeBuildMsg13Args(ssl, args);
-#ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = NULL;
-#endif
-
- return ret;
-}
-
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
-/* Find the cipher suite in the suites set in the SSL.
- *
- * ssl SSL/TLS object.
- * suite Cipher suite to look for.
- * returns 1 when suite is found in SSL/TLS object's list and 0 otherwise.
- */
-static int FindSuiteSSL(WOLFSSL* ssl, byte* suite)
-{
- word16 i;
-
- for (i = 0; i < ssl->suites->suiteSz; i += 2) {
- if (ssl->suites->suites[i+0] == suite[0] &&
- ssl->suites->suites[i+1] == suite[1]) {
- return 1;
- }
- }
-
- return 0;
-}
-#endif
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
-#if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
-/* Create Cookie extension using the hash of the first ClientHello.
- *
- * ssl SSL/TLS object.
- * hash The hash data.
- * hashSz The size of the hash data in bytes.
- * returns 0 on success, otherwise failure.
- */
-static int CreateCookie(WOLFSSL* ssl, byte* hash, byte hashSz)
-{
- int ret;
- byte mac[WC_MAX_DIGEST_SIZE] = {0};
- Hmac cookieHmac;
- byte cookieType = 0;
- byte macSz = 0;
-
-#if !defined(NO_SHA) && defined(NO_SHA256)
- cookieType = SHA;
- macSz = WC_SHA_DIGEST_SIZE;
-#endif /* NO_SHA */
-#ifndef NO_SHA256
- cookieType = WC_SHA256;
- macSz = WC_SHA256_DIGEST_SIZE;
-#endif /* NO_SHA256 */
- XMEMSET(&cookieHmac, 0, sizeof(Hmac));
-
- ret = wc_HmacSetKey(&cookieHmac, cookieType,
- ssl->buffers.tls13CookieSecret.buffer,
- ssl->buffers.tls13CookieSecret.length);
- if (ret != 0)
- return ret;
- if ((ret = wc_HmacUpdate(&cookieHmac, hash, hashSz)) != 0)
- return ret;
- if ((ret = wc_HmacFinal(&cookieHmac, mac)) != 0)
- return ret;
-
- /* The cookie data is the hash and the integrity check. */
- return TLSX_Cookie_Use(ssl, hash, hashSz, mac, macSz, 1);
-}
-#endif
-
-/* Restart the handshake hash with a hash of the previous messages.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-static int RestartHandshakeHash(WOLFSSL* ssl)
-{
- int ret;
- Hashes hashes;
- byte header[HANDSHAKE_HEADER_SZ] = {0};
- byte* hash = NULL;
- byte hashSz = 0;
-
- ret = BuildCertHashes(ssl, &hashes);
- if (ret != 0)
- return ret;
- switch (ssl->specs.mac_algorithm) {
- #ifndef NO_SHA256
- case sha256_mac:
- hash = hashes.sha256;
- break;
- #endif
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- hash = hashes.sha384;
- break;
- #endif
- #ifdef WOLFSSL_TLS13_SHA512
- case sha512_mac:
- hash = hashes.sha512;
- break;
- #endif
- }
- hashSz = ssl->specs.hash_size;
-
- /* check hash */
- if (hash == NULL && hashSz > 0)
- return BAD_FUNC_ARG;
-
- AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl);
-
- WOLFSSL_MSG("Restart Hash");
- WOLFSSL_BUFFER(hash, hashSz);
-
-#if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
- if (ssl->options.sendCookie) {
- byte cookie[OPAQUE8_LEN + WC_MAX_DIGEST_SIZE + OPAQUE16_LEN * 2];
- TLSX* ext;
- word32 idx = 0;
-
- /* Cookie Data = Hash Len | Hash | CS | KeyShare Group */
- cookie[idx++] = hashSz;
- if (hash)
- XMEMCPY(cookie + idx, hash, hashSz);
- idx += hashSz;
- cookie[idx++] = ssl->options.cipherSuite0;
- cookie[idx++] = ssl->options.cipherSuite;
- if ((ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE)) != NULL) {
- KeyShareEntry* kse = (KeyShareEntry*)ext->data;
- c16toa(kse->group, cookie + idx);
- idx += OPAQUE16_LEN;
- }
- return CreateCookie(ssl, cookie, idx);
- }
-#endif
-
- ret = InitHandshakeHashes(ssl);
- if (ret != 0)
- return ret;
- ret = HashOutputRaw(ssl, header, sizeof(header));
- if (ret != 0)
- return ret;
- return HashOutputRaw(ssl, hash, hashSz);
-}
-
-/* The value in the random field of a ServerHello to indicate
- * HelloRetryRequest.
- */
-static byte helloRetryRequestRandom[] = {
- 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
- 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
- 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
- 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C
-};
-#endif /* WOLFSSL_TLS13_DRAFT_18 */
-
-#ifndef NO_WOLFSSL_CLIENT
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
-/* Setup pre-shared key based on the details in the extension data.
- *
- * ssl SSL/TLS object.
- * psk Pre-shared key extension data.
- * returns 0 on success, PSK_KEY_ERROR when the client PSK callback fails and
- * other negative value on failure.
- */
-static int SetupPskKey(WOLFSSL* ssl, PreSharedKey* psk)
-{
- int ret;
- byte suite[2];
-
- if (psk == NULL)
- return BAD_FUNC_ARG;
-
- suite[0] = psk->cipherSuite0;
- suite[1] = psk->cipherSuite;
- if (!FindSuiteSSL(ssl, suite))
- return PSK_KEY_ERROR;
-
- ssl->options.cipherSuite0 = psk->cipherSuite0;
- ssl->options.cipherSuite = psk->cipherSuite;
- if ((ret = SetCipherSpecs(ssl)) != 0)
- return ret;
-
-#ifdef HAVE_SESSION_TICKET
- if (psk->resumption) {
- #ifdef WOLFSSL_EARLY_DATA
- if (ssl->session.maxEarlyDataSz == 0)
- ssl->earlyData = no_early_data;
- #endif
- /* Resumption PSK is master secret. */
- ssl->arrays->psk_keySz = ssl->specs.hash_size;
-#ifdef WOLFSSL_TLS13_DRAFT_18
- XMEMCPY(ssl->arrays->psk_key, ssl->session.masterSecret,
- ssl->arrays->psk_keySz);
-#else
- if ((ret = DeriveResumptionPSK(ssl, ssl->session.ticketNonce.data,
- ssl->session.ticketNonce.len, ssl->arrays->psk_key)) != 0) {
- return ret;
- }
-#endif
- }
-#endif
-#ifndef NO_PSK
- if (!psk->resumption) {
- #ifndef WOLFSSL_PSK_ONE_ID
- const char* cipherName = NULL;
- byte cipherSuite0 = TLS13_BYTE, cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
-
- /* Get the pre-shared key. */
- if (ssl->options.client_psk_tls13_cb != NULL) {
- ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(ssl,
- (char *)psk->identity, ssl->arrays->client_identity,
- MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN,
- &cipherName);
- if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
- &cipherSuite) != 0) {
- return PSK_KEY_ERROR;
- }
- }
- else {
- ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
- (char *)psk->identity, ssl->arrays->client_identity,
- MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
- }
- if (ssl->arrays->psk_keySz == 0 ||
- ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
- return PSK_KEY_ERROR;
- }
-
- if (psk->cipherSuite0 != cipherSuite0 ||
- psk->cipherSuite != cipherSuite) {
- return PSK_KEY_ERROR;
- }
- #else
- /* PSK information loaded during setting of default TLS extensions. */
- #endif
- }
-#endif
-
- if (ssl->options.noPskDheKe)
- ssl->arrays->preMasterSz = 0;
-
- /* Derive the early secret using the PSK. */
- return DeriveEarlySecret(ssl);
-}
-
-/* Derive and write the binders into the ClientHello in space left when
- * writing the Pre-Shared Key extension.
- *
- * ssl The SSL/TLS object.
- * output The buffer containing the ClientHello.
- * idx The index at the end of the completed ClientHello.
- * returns 0 on success and otherwise failure.
- */
-static int WritePSKBinders(WOLFSSL* ssl, byte* output, word32 idx)
-{
- int ret;
- TLSX* ext;
- PreSharedKey* current;
- byte binderKey[WC_MAX_DIGEST_SIZE];
- word16 len;
-
- WOLFSSL_ENTER("WritePSKBinders");
-
- ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
- if (ext == NULL)
- return SANITY_MSG_E;
-
- /* Get the size of the binders to determine where to write binders. */
- ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data,
- client_hello, &len);
- if (ret < 0)
- return ret;
- idx -= len;
-
- /* Hash truncated ClientHello - up to binders. */
- ret = HashOutput(ssl, output, idx, 0);
- if (ret != 0)
- return ret;
-
- current = (PreSharedKey*)ext->data;
- /* Calculate the binder for each identity based on previous handshake data.
- */
- while (current != NULL) {
- if ((ret = SetupPskKey(ssl, current)) != 0)
- return ret;
-
- #ifdef HAVE_SESSION_TICKET
- if (current->resumption)
- ret = DeriveBinderKeyResume(ssl, binderKey);
- #endif
- #ifndef NO_PSK
- if (!current->resumption)
- ret = DeriveBinderKey(ssl, binderKey);
- #endif
- if (ret != 0)
- return ret;
-
- /* Derive the Finished message secret. */
- ret = DeriveFinishedSecret(ssl, binderKey,
- ssl->keys.client_write_MAC_secret);
- if (ret != 0)
- return ret;
-
- /* Build the HMAC of the handshake message data = binder. */
- ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret,
- current->binder, ¤t->binderLen);
- if (ret != 0)
- return ret;
-
- current = current->next;
- }
-
- /* Data entered into extension, now write to message. */
- ret = TLSX_PreSharedKey_WriteBinders((PreSharedKey*)ext->data, output + idx,
- client_hello, &len);
- if (ret < 0)
- return ret;
-
- /* Hash binders to complete the hash of the ClientHello. */
- ret = HashOutputRaw(ssl, output + idx, len);
- if (ret < 0)
- return ret;
-
- #ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data) {
- if ((ret = SetupPskKey(ssl, (PreSharedKey*)ext->data)) != 0)
- return ret;
-
- /* Derive early data encryption key. */
- ret = DeriveTls13Keys(ssl, early_data_key, ENCRYPT_SIDE_ONLY, 1);
- if (ret != 0)
- return ret;
- if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0)
- return ret;
- }
- #endif
-
- WOLFSSL_LEAVE("WritePSKBinders", ret);
-
- return ret;
-}
-#endif
-
-/* handle generation of TLS 1.3 client_hello (1) */
-/* Send a ClientHello message to the server.
- * Include the information required to start a handshake with servers using
- * protocol versions less than TLS v1.3.
- * Only a client will send this message.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success and otherwise failure.
- */
-int SendTls13ClientHello(WOLFSSL* ssl)
-{
- byte* output;
- word16 length;
- word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- int sendSz;
- int ret;
-
- WOLFSSL_START(WC_FUNC_CLIENT_HELLO_SEND);
- WOLFSSL_ENTER("SendTls13ClientHello");
-
-#ifdef HAVE_SESSION_TICKET
- if (ssl->options.resuming &&
- (ssl->session.version.major != ssl->version.major ||
- ssl->session.version.minor != ssl->version.minor)) {
- #ifndef WOLFSSL_NO_TLS12
- if (ssl->session.version.major == ssl->version.major &&
- ssl->session.version.minor < ssl->version.minor) {
- /* Cannot resume with a different protocol version. */
- ssl->options.resuming = 0;
- ssl->version.major = ssl->session.version.major;
- ssl->version.minor = ssl->session.version.minor;
- return SendClientHello(ssl);
- }
- else
- #endif
- return VERSION_ERROR;
- }
-#endif
-
- if (ssl->suites == NULL) {
- WOLFSSL_MSG("Bad suites pointer in SendTls13ClientHello");
- return SUITES_ERROR;
- }
-
- /* Version | Random | Session Id | Cipher Suites | Compression */
- length = VERSION_SZ + RAN_LEN + ENUM_LEN + ssl->suites->suiteSz +
- SUITE_LEN + COMP_LEN + ENUM_LEN;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- #if defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
- length += ID_LEN;
- #else
- if (ssl->session.sessionIDSz > 0)
- length += ssl->session.sessionIDSz;
- #endif
-#endif
-
- /* Auto populate extensions supported unless user defined. */
- if ((ret = TLSX_PopulateExtensions(ssl, 0)) != 0)
- return ret;
-#ifdef WOLFSSL_EARLY_DATA
- #ifndef NO_PSK
- if (!ssl->options.resuming &&
- ssl->options.client_psk_tls13_cb == NULL &&
- ssl->options.client_psk_cb == NULL)
- #else
- if (!ssl->options.resuming)
- #endif
- ssl->earlyData = no_early_data;
- if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE)
- ssl->earlyData = no_early_data;
- if (ssl->earlyData == no_early_data)
- TLSX_Remove(&ssl->extensions, TLSX_EARLY_DATA, ssl->heap);
- if (ssl->earlyData != no_early_data &&
- (ret = TLSX_EarlyData_Use(ssl, 0)) < 0) {
- return ret;
- }
-#endif
- /* Include length of TLS extensions. */
- ret = TLSX_GetRequestSize(ssl, client_hello, &length);
- if (ret != 0)
- return ret;
-
- /* Total message size. */
- sendSz = length + HANDSHAKE_HEADER_SZ + RECORD_HEADER_SZ;
-
- /* Check buffers are big enough and grow if needed. */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* Get position in output buffer to write new message to. */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* Put the record and handshake headers on. */
- AddTls13Headers(output, length, client_hello, ssl);
-
- /* Protocol version - negotiation now in extension: supported_versions. */
- output[idx++] = SSLv3_MAJOR;
- output[idx++] = TLSv1_2_MINOR;
- /* Keep for downgrade. */
- ssl->chVersion = ssl->version;
-
- /* Client Random */
- if (ssl->options.connectState == CONNECT_BEGIN) {
- ret = wc_RNG_GenerateBlock(ssl->rng, output + idx, RAN_LEN);
- if (ret != 0)
- return ret;
-
- /* Store random for possible second ClientHello. */
- XMEMCPY(ssl->arrays->clientRandom, output + idx, RAN_LEN);
- }
- else
- XMEMCPY(output + idx, ssl->arrays->clientRandom, RAN_LEN);
- idx += RAN_LEN;
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- /* TLS v1.3 does not use session id - 0 length. */
- output[idx++] = 0;
-#else
- if (ssl->session.sessionIDSz > 0) {
- /* Session resumption for old versions of protocol. */
- output[idx++] = ID_LEN;
- XMEMCPY(output + idx, ssl->session.sessionID, ssl->session.sessionIDSz);
- idx += ID_LEN;
- }
- else {
- #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
- output[idx++] = ID_LEN;
- XMEMCPY(output + idx, ssl->arrays->clientRandom, ID_LEN);
- idx += ID_LEN;
- #else
- /* TLS v1.3 does not use session id - 0 length. */
- output[idx++] = 0;
- #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */
- }
-#endif /* WOLFSSL_TLS13_DRAFT_18 */
-
- /* Cipher suites */
- c16toa(ssl->suites->suiteSz, output + idx);
- idx += OPAQUE16_LEN;
- XMEMCPY(output + idx, &ssl->suites->suites, ssl->suites->suiteSz);
- idx += ssl->suites->suiteSz;
-
- /* Compression not supported in TLS v1.3. */
- output[idx++] = COMP_LEN;
- output[idx++] = NO_COMPRESSION;
-
- /* Write out extensions for a request. */
- length = 0;
- ret = TLSX_WriteRequest(ssl, output + idx, client_hello, &length);
- if (ret != 0)
- return ret;
- idx += length;
-
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- /* Resumption has a specific set of extensions and binder is calculated
- * for each identity.
- */
- if (TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY))
- ret = WritePSKBinders(ssl, output, idx);
- else
-#endif
- ret = HashOutput(ssl, output, idx, 0);
- if (ret != 0)
- return ret;
-
- ssl->options.clientState = CLIENT_HELLO_COMPLETE;
-
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello");
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "ClientHello", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- }
-#endif
-
- ssl->buffers.outputBuffer.length += sendSz;
-
-#ifdef WOLFSSL_EARLY_DATA_GROUP
- if (ssl->earlyData == no_early_data)
-#endif
- ret = SendBuffered(ssl);
-
-
- WOLFSSL_LEAVE("SendTls13ClientHello", ret);
- WOLFSSL_END(WC_FUNC_CLIENT_HELLO_SEND);
-
- return ret;
-}
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* handle rocessing of TLS 1.3 hello_retry_request (6) */
-/* Parse and handle a HelloRetryRequest message.
- * Only a client will receive this message.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the message buffer of
- * HelloRetryRequest.
- * On exit, the index of byte after the HelloRetryRequest message.
- * totalSz The length of the current handshake message.
- * returns 0 on success and otherwise failure.
- */
-static int DoTls13HelloRetryRequest(WOLFSSL* ssl, const byte* input,
- word32* inOutIdx, word32 totalSz)
-{
- int ret;
- word32 begin = *inOutIdx;
- word32 i = begin;
- word16 totalExtSz;
- ProtocolVersion pv;
-
- WOLFSSL_ENTER("DoTls13HelloRetryRequest");
-
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "HelloRetryRequest");
- if (ssl->toInfoOn) AddLateName("HelloRetryRequest", &ssl->timeoutInfo);
-#endif
-
- /* Version info and length field of extension data. */
- if (totalSz < i - begin + OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN)
- return BUFFER_ERROR;
-
- /* Protocol version. */
- XMEMCPY(&pv, input + i, OPAQUE16_LEN);
- i += OPAQUE16_LEN;
- ret = CheckVersion(ssl, pv);
- if (ret != 0)
- return ret;
-
- /* Length of extension data. */
- ato16(&input[i], &totalExtSz);
- i += OPAQUE16_LEN;
- if (totalExtSz == 0) {
- WOLFSSL_MSG("HelloRetryRequest must contain extensions");
- return MISSING_HANDSHAKE_DATA;
- }
-
- /* Extension data. */
- if (i - begin + totalExtSz > totalSz)
- return BUFFER_ERROR;
- if ((ret = TLSX_Parse(ssl, (byte *)(input + i), totalExtSz,
- hello_retry_request, NULL)) != 0)
- return ret;
- /* The KeyShare extension parsing fails when not valid. */
-
- /* Move index to byte after message. */
- *inOutIdx = i + totalExtSz;
-
- ssl->options.tls1_3 = 1;
- ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE;
-
- WOLFSSL_LEAVE("DoTls13HelloRetryRequest", ret);
-
- return ret;
-}
-#endif
-
-
-/* handle processing of TLS 1.3 server_hello (2) and hello_retry_request (6) */
-/* Handle the ServerHello message from the server.
- * Only a client will receive this message.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the message buffer of ServerHello.
- * On exit, the index of byte after the ServerHello message.
- * helloSz The length of the current handshake message.
- * returns 0 on success and otherwise failure.
- */
-int DoTls13ServerHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- word32 helloSz, byte* extMsgType)
-{
- ProtocolVersion pv;
- word32 i = *inOutIdx;
- word32 begin = i;
- int ret;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- byte sessIdSz;
- const byte* sessId;
- byte b;
- int foundVersion;
-#endif
- word16 totalExtSz;
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- TLSX* ext;
- PreSharedKey* psk = NULL;
-#endif
-
- WOLFSSL_START(WC_FUNC_SERVER_HELLO_DO);
- WOLFSSL_ENTER("DoTls13ServerHello");
-
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "ServerHello");
- if (ssl->toInfoOn) AddLateName("ServerHello", &ssl->timeoutInfo);
-#endif
-
- /* Protocol version length check. */
- if (OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
-
- /* Protocol version */
- XMEMCPY(&pv, input + i, OPAQUE16_LEN);
- i += OPAQUE16_LEN;
-#ifdef WOLFSSL_TLS13_DRAFT_18
- ret = CheckVersion(ssl, pv);
- if (ret != 0)
- return ret;
- if (!IsAtLeastTLSv1_3(pv) && pv.major != TLS_DRAFT_MAJOR) {
-#ifndef WOLFSSL_NO_TLS12
- if (ssl->options.downgrade) {
- ssl->version = pv;
- return DoServerHello(ssl, input, inOutIdx, helloSz);
- }
-#endif
-
- WOLFSSL_MSG("Client using higher version, fatal error");
- return VERSION_ERROR;
- }
-#else
-#ifndef WOLFSSL_NO_TLS12
- if (pv.major == ssl->version.major && pv.minor < TLSv1_2_MINOR &&
- ssl->options.downgrade) {
- /* Force client hello version 1.2 to work for static RSA. */
- ssl->chVersion.minor = TLSv1_2_MINOR;
- ssl->version.minor = TLSv1_2_MINOR;
- return DoServerHello(ssl, input, inOutIdx, helloSz);
- }
-#endif
- if (pv.major != ssl->version.major || pv.minor != TLSv1_2_MINOR)
- return VERSION_ERROR;
-#endif
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- /* Random length check */
- if ((i - begin) + RAN_LEN > helloSz)
- return BUFFER_ERROR;
-#else
- /* Random and session id length check */
- if ((i - begin) + RAN_LEN + ENUM_LEN > helloSz)
- return BUFFER_ERROR;
-
- if (XMEMCMP(input + i, helloRetryRequestRandom, RAN_LEN) == 0)
- *extMsgType = hello_retry_request;
-#endif
-
- /* Server random - keep for debugging. */
- XMEMCPY(ssl->arrays->serverRandom, input + i, RAN_LEN);
- i += RAN_LEN;
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- /* Session id */
- sessIdSz = input[i++];
- if ((i - begin) + sessIdSz > helloSz)
- return BUFFER_ERROR;
- sessId = input + i;
- i += sessIdSz;
-#endif /* WOLFSSL_TLS13_DRAFT_18 */
- ssl->options.haveSessionId = 1;
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- /* Ciphersuite check */
- if ((i - begin) + OPAQUE16_LEN + OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
-#else
- /* Ciphersuite and compression check */
- if ((i - begin) + OPAQUE16_LEN + OPAQUE8_LEN > helloSz)
- return BUFFER_ERROR;
-#endif
-
- /* Set the cipher suite from the message. */
- ssl->options.cipherSuite0 = input[i++];
- ssl->options.cipherSuite = input[i++];
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- /* Compression */
- b = input[i++];
- if (b != 0) {
- WOLFSSL_MSG("Must be no compression types in list");
- return INVALID_PARAMETER;
- }
-#endif
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- if ((i - begin) + OPAQUE16_LEN > helloSz) {
- if (!ssl->options.downgrade)
- return BUFFER_ERROR;
-#ifndef WOLFSSL_NO_TLS12
- ssl->version.minor = TLSv1_2_MINOR;
-#endif
- ssl->options.haveEMS = 0;
- }
- if ((i - begin) < helloSz)
-#endif
- {
- /* Get extension length and length check. */
- if ((i - begin) + OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
- ato16(&input[i], &totalExtSz);
- i += OPAQUE16_LEN;
- if ((i - begin) + totalExtSz > helloSz)
- return BUFFER_ERROR;
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- /* Need to negotiate version first. */
- if ((ret = TLSX_ParseVersion(ssl, (byte*)input + i, totalExtSz,
- *extMsgType, &foundVersion))) {
- return ret;
- }
- if (!foundVersion) {
- if (!ssl->options.downgrade) {
- WOLFSSL_MSG("Server trying to downgrade to version less than "
- "TLS v1.3");
- return VERSION_ERROR;
- }
-
- if (pv.minor < ssl->options.minDowngrade)
- return VERSION_ERROR;
- ssl->version.minor = pv.minor;
- }
-#endif
-
- /* Parse and handle extensions. */
- ret = TLSX_Parse(ssl, (byte *) input + i, totalExtSz, *extMsgType,
- NULL);
- if (ret != 0)
- return ret;
-
- i += totalExtSz;
- }
- *inOutIdx = i;
-
- ssl->options.serverState = SERVER_HELLO_COMPLETE;
-
-#ifdef HAVE_SECRET_CALLBACK
- if (ssl->sessionSecretCb != NULL) {
- int secretSz = SECRET_LEN;
- ret = ssl->sessionSecretCb(ssl, ssl->session.masterSecret,
- &secretSz, ssl->sessionSecretCtx);
- if (ret != 0 || secretSz != SECRET_LEN) {
- return SESSION_SECRET_CB_E;
- }
- }
-#endif /* HAVE_SECRET_CALLBACK */
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- /* Version only negotiated in extensions for TLS v1.3.
- * Only now do we know how to deal with session id.
- */
- if (!IsAtLeastTLSv1_3(ssl->version)) {
-#ifndef WOLFSSL_NO_TLS12
- ssl->arrays->sessionIDSz = sessIdSz;
-
- if (ssl->arrays->sessionIDSz > ID_LEN) {
- WOLFSSL_MSG("Invalid session ID size");
- ssl->arrays->sessionIDSz = 0;
- return BUFFER_ERROR;
- }
- else if (ssl->arrays->sessionIDSz) {
- XMEMCPY(ssl->arrays->sessionID, sessId, ssl->arrays->sessionIDSz);
- ssl->options.haveSessionId = 1;
- }
-
- /* Force client hello version 1.2 to work for static RSA. */
- ssl->chVersion.minor = TLSv1_2_MINOR;
- /* Complete TLS v1.2 processing of ServerHello. */
- ret = CompleteServerHello(ssl);
-#else
- WOLFSSL_MSG("Client using higher version, fatal error");
- ret = VERSION_ERROR;
-#endif
-
- WOLFSSL_LEAVE("DoTls13ServerHello", ret);
-
- return ret;
- }
-
- #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
- if (sessIdSz == 0)
- return INVALID_PARAMETER;
- if (ssl->session.sessionIDSz != 0) {
- if (ssl->session.sessionIDSz != sessIdSz ||
- XMEMCMP(ssl->session.sessionID, sessId, sessIdSz) != 0) {
- return INVALID_PARAMETER;
- }
- }
- else if (XMEMCMP(ssl->arrays->clientRandom, sessId, sessIdSz) != 0)
- return INVALID_PARAMETER;
- #else
- if (sessIdSz != ssl->session.sessionIDSz || (sessIdSz > 0 &&
- XMEMCMP(ssl->session.sessionID, sessId, sessIdSz) != 0)) {
- WOLFSSL_MSG("Server sent different session id");
- return INVALID_PARAMETER;
- }
- #endif /* WOLFSSL_TLS13_MIDDLEBOX_COMPAT */
-#endif
-
- ret = SetCipherSpecs(ssl);
- if (ret != 0)
- return ret;
-#ifdef HAVE_NULL_CIPHER
- if (ssl->options.cipherSuite0 == ECC_BYTE &&
- (ssl->options.cipherSuite == TLS_SHA256_SHA256 ||
- ssl->options.cipherSuite == TLS_SHA384_SHA384)) {
- ;
- }
- else
-#endif
- /* Check that the negotiated ciphersuite matches protocol version. */
- if (ssl->options.cipherSuite0 != TLS13_BYTE) {
- WOLFSSL_MSG("Server sent non-TLS13 cipher suite in TLS 1.3 packet");
- return INVALID_PARAMETER;
- }
-
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
-#ifndef WOLFSSL_TLS13_DRAFT_18
- if (*extMsgType == server_hello)
-#endif
- {
- ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
- if (ext != NULL)
- psk = (PreSharedKey*)ext->data;
- while (psk != NULL && !psk->chosen)
- psk = psk->next;
- if (psk == NULL) {
- ssl->options.resuming = 0;
- ssl->arrays->psk_keySz = 0;
- XMEMSET(ssl->arrays->psk_key, 0, MAX_PSK_KEY_LEN);
- }
- else if ((ret = SetupPskKey(ssl, psk)) != 0)
- return ret;
- }
-#endif
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- ssl->keys.encryptionOn = 1;
-#else
- if (*extMsgType == server_hello) {
- ssl->keys.encryptionOn = 1;
- ssl->options.serverState = SERVER_HELLO_COMPLETE;
- }
- else {
- ssl->options.tls1_3 = 1;
- ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE;
-
- ret = RestartHandshakeHash(ssl);
- }
-#endif
-
- WOLFSSL_LEAVE("DoTls13ServerHello", ret);
- WOLFSSL_END(WC_FUNC_SERVER_HELLO_DO);
-
- return ret;
-}
-
-/* handle processing TLS 1.3 encrypted_extensions (8) */
-/* Parse and handle an EncryptedExtensions message.
- * Only a client will receive this message.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the message buffer of
- * EncryptedExtensions.
- * On exit, the index of byte after the EncryptedExtensions
- * message.
- * totalSz The length of the current handshake message.
- * returns 0 on success and otherwise failure.
- */
-static int DoTls13EncryptedExtensions(WOLFSSL* ssl, const byte* input,
- word32* inOutIdx, word32 totalSz)
-{
- int ret;
- word32 begin = *inOutIdx;
- word32 i = begin;
- word16 totalExtSz;
-
- WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_DO);
- WOLFSSL_ENTER("DoTls13EncryptedExtensions");
-
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "EncryptedExtensions");
- if (ssl->toInfoOn) AddLateName("EncryptedExtensions", &ssl->timeoutInfo);
-#endif
-
- /* Length field of extension data. */
- if (totalSz < i - begin + OPAQUE16_LEN)
- return BUFFER_ERROR;
- ato16(&input[i], &totalExtSz);
- i += OPAQUE16_LEN;
-
- /* Extension data. */
- if (i - begin + totalExtSz > totalSz)
- return BUFFER_ERROR;
- if ((ret = TLSX_Parse(ssl, (byte *)(input + i), totalExtSz,
- encrypted_extensions, NULL)))
- return ret;
-
- /* Move index to byte after message. */
- *inOutIdx = i + totalExtSz;
-
- /* Always encrypted. */
- *inOutIdx += ssl->keys.padSz;
-
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data) {
- TLSX* ext = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
- if (ext == NULL || !ext->val)
- ssl->earlyData = no_early_data;
- }
-#endif
-
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData == no_early_data) {
- ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY);
- if (ret != 0)
- return ret;
- }
-#endif
-
- ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE;
-
- WOLFSSL_LEAVE("DoTls13EncryptedExtensions", ret);
- WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_DO);
-
- return ret;
-}
-
-/* handle processing TLS v1.3 certificate_request (13) */
-/* Handle a TLS v1.3 CertificateRequest message.
- * This message is always encrypted.
- * Only a client will receive this message.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the message buffer of CertificateRequest.
- * On exit, the index of byte after the CertificateRequest message.
- * size The length of the current handshake message.
- * returns 0 on success and otherwise failure.
- */
-static int DoTls13CertificateRequest(WOLFSSL* ssl, const byte* input,
- word32* inOutIdx, word32 size)
-{
- word16 len;
- word32 begin = *inOutIdx;
- int ret = 0;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- Suites peerSuites;
-#endif
-#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- CertReqCtx* certReqCtx;
-#endif
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_DO);
- WOLFSSL_ENTER("DoTls13CertificateRequest");
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- XMEMSET(&peerSuites, 0, sizeof(Suites));
-#endif
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateRequest");
- if (ssl->toInfoOn) AddLateName("CertificateRequest", &ssl->timeoutInfo);
-#endif
-
- if ((*inOutIdx - begin) + OPAQUE8_LEN > size)
- return BUFFER_ERROR;
-
- /* Length of the request context. */
- len = input[(*inOutIdx)++];
- if ((*inOutIdx - begin) + len > size)
- return BUFFER_ERROR;
- if (ssl->options.connectState < FINISHED_DONE && len > 0)
- return BUFFER_ERROR;
-
-#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- /* CertReqCtx has one byte at end for context value.
- * Increase size to handle other implementations sending more than one byte.
- * That is, allocate extra space, over one byte, to hold the context value.
- */
- certReqCtx = (CertReqCtx*)XMALLOC(sizeof(CertReqCtx) + len - 1, ssl->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (certReqCtx == NULL)
- return MEMORY_E;
- certReqCtx->next = ssl->certReqCtx;
- certReqCtx->len = len;
- XMEMCPY(&certReqCtx->ctx, input + *inOutIdx, len);
- ssl->certReqCtx = certReqCtx;
-#endif
- *inOutIdx += len;
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- /* Signature and hash algorithms. */
- if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
- return BUFFER_ERROR;
- ato16(input + *inOutIdx, &len);
- *inOutIdx += OPAQUE16_LEN;
- if ((*inOutIdx - begin) + len > size)
- return BUFFER_ERROR;
- if (PickHashSigAlgo(ssl, input + *inOutIdx, len) != 0 &&
- ssl->buffers.certificate && ssl->buffers.certificate->buffer &&
- ssl->buffers.key && ssl->buffers.key->buffer) {
- return INVALID_PARAMETER;
- }
- *inOutIdx += len;
-
- /* Length of certificate authority data. */
- if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
- return BUFFER_ERROR;
- ato16(input + *inOutIdx, &len);
- *inOutIdx += OPAQUE16_LEN;
- if ((*inOutIdx - begin) + len > size)
- return BUFFER_ERROR;
-
- /* Certificate authorities. */
- while (len) {
- word16 dnSz;
-
- if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
- return BUFFER_ERROR;
-
- ato16(input + *inOutIdx, &dnSz);
- *inOutIdx += OPAQUE16_LEN;
-
- if ((*inOutIdx - begin) + dnSz > size)
- return BUFFER_ERROR;
-
- *inOutIdx += dnSz;
- len -= OPAQUE16_LEN + dnSz;
- }
-
- /* Certificate extensions */
- if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
- return BUFFER_ERROR;
- ato16(input + *inOutIdx, &len);
- *inOutIdx += OPAQUE16_LEN;
- if ((*inOutIdx - begin) + len > size)
- return BUFFER_ERROR;
- *inOutIdx += len;
-#else
- /* TODO: Add support for more extensions:
- * signed_certificate_timestamp, certificate_authorities, oid_filters.
- */
- /* Certificate extensions */
- if ((*inOutIdx - begin) + OPAQUE16_LEN > size)
- return BUFFER_ERROR;
- ato16(input + *inOutIdx, &len);
- *inOutIdx += OPAQUE16_LEN;
- if ((*inOutIdx - begin) + len > size)
- return BUFFER_ERROR;
- if (len == 0)
- return INVALID_PARAMETER;
- if ((ret = TLSX_Parse(ssl, (byte *)(input + *inOutIdx), len,
- certificate_request, &peerSuites))) {
- return ret;
- }
- *inOutIdx += len;
-#endif
-
- if (ssl->buffers.certificate && ssl->buffers.certificate->buffer &&
- ((ssl->buffers.key && ssl->buffers.key->buffer)
- #ifdef HAVE_PK_CALLBACKS
- || wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)
- #endif
- )) {
-#ifndef WOLFSSL_TLS13_DRAFT_18
- if (PickHashSigAlgo(ssl, peerSuites.hashSigAlgo,
- peerSuites.hashSigAlgoSz) != 0) {
- return INVALID_PARAMETER;
- }
-#endif
- ssl->options.sendVerify = SEND_CERT;
- }
- else {
- ssl->options.sendVerify = SEND_BLANK_CERT;
- }
-
- /* This message is always encrypted so add encryption padding. */
- *inOutIdx += ssl->keys.padSz;
-
- WOLFSSL_LEAVE("DoTls13CertificateRequest", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_DO);
-
- return ret;
-}
-
-#endif /* !NO_WOLFSSL_CLIENT */
-
-#ifndef NO_WOLFSSL_SERVER
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
-/* Refine list of supported cipher suites to those common to server and client.
- *
- * ssl SSL/TLS object.
- * peerSuites The peer's advertised list of supported cipher suites.
- */
-static void RefineSuites(WOLFSSL* ssl, Suites* peerSuites)
-{
- byte suites[WOLFSSL_MAX_SUITE_SZ];
- int suiteSz = 0;
- word16 i, j;
-
- XMEMSET(suites, 0, WOLFSSL_MAX_SUITE_SZ);
-
- for (i = 0; i < ssl->suites->suiteSz; i += 2) {
- for (j = 0; j < peerSuites->suiteSz; j += 2) {
- if (ssl->suites->suites[i+0] == peerSuites->suites[j+0] &&
- ssl->suites->suites[i+1] == peerSuites->suites[j+1]) {
- suites[suiteSz++] = peerSuites->suites[j+0];
- suites[suiteSz++] = peerSuites->suites[j+1];
- }
- }
- }
-
- ssl->suites->suiteSz = suiteSz;
- XMEMCPY(ssl->suites->suites, &suites, sizeof(suites));
-}
-
-/* Handle any Pre-Shared Key (PSK) extension.
- * Must do this in ClientHello as it requires a hash of the truncated message.
- * Don't know size of binders until Pre-Shared Key extension has been parsed.
- *
- * ssl The SSL/TLS object.
- * input The ClientHello message.
- * helloSz The size of the ClientHello message (including binders if present).
- * usingPSK Indicates handshake is using Pre-Shared Keys.
- * returns 0 on success and otherwise failure.
- */
-static int DoPreSharedKeys(WOLFSSL* ssl, const byte* input, word32 helloSz,
- int* usingPSK)
-{
- int ret;
- TLSX* ext;
- word16 bindersLen;
- PreSharedKey* current;
- byte binderKey[WC_MAX_DIGEST_SIZE];
- byte binder[WC_MAX_DIGEST_SIZE];
- word32 binderLen;
- word16 modes;
- byte suite[2];
-#ifdef WOLFSSL_EARLY_DATA
- int pskCnt = 0;
- TLSX* extEarlyData;
-#endif
-#ifndef NO_PSK
- const char* cipherName = NULL;
- byte cipherSuite0 = TLS13_BYTE;
- byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
-#endif
-
- WOLFSSL_ENTER("DoPreSharedKeys");
-
- ext = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
- if (ext == NULL) {
- /* Hash data up to binders for deriving binders in PSK extension. */
- ret = HashInput(ssl, input, helloSz);
- return ret;
- }
-
- /* Extensions pushed on stack/list and PSK must be last. */
- if (ssl->extensions != ext)
- return PSK_KEY_ERROR;
-
- /* Assume we are going to resume with a pre-shared key. */
- ssl->options.resuming = 1;
-
- /* Find the pre-shared key extension and calculate hash of truncated
- * ClientHello for binders.
- */
- ret = TLSX_PreSharedKey_GetSizeBinders((PreSharedKey*)ext->data,
- client_hello, &bindersLen);
- if (ret < 0)
- return ret;
-
- /* Hash data up to binders for deriving binders in PSK extension. */
- ret = HashInput(ssl, input, helloSz - bindersLen);
- if (ret != 0)
- return ret;
-
- /* Look through all client's pre-shared keys for a match. */
- current = (PreSharedKey*)ext->data;
- while (current != NULL) {
- #ifdef WOLFSSL_EARLY_DATA
- pskCnt++;
- #endif
-
- #ifndef NO_PSK
- if (current->identityLen > MAX_PSK_ID_LEN) {
- return BUFFER_ERROR;
- }
- XMEMCPY(ssl->arrays->client_identity, current->identity,
- current->identityLen);
- ssl->arrays->client_identity[current->identityLen] = '\0';
- #endif
-
- #ifdef HAVE_SESSION_TICKET
- /* Decode the identity. */
- if ((ret = DoClientTicket(ssl, current->identity, current->identityLen))
- == WOLFSSL_TICKET_RET_OK) {
- word32 now;
- int diff;
-
- now = TimeNowInMilliseconds();
- if (now == (word32)GETTIME_ERROR)
- return now;
- diff = now - ssl->session.ticketSeen;
- diff -= current->ticketAge - ssl->session.ticketAdd;
- /* Check session and ticket age timeout.
- * Allow +/- 1000 milliseconds on ticket age.
- */
- if (diff > (int)ssl->timeout * 1000 || diff < -1000 ||
- diff - MAX_TICKET_AGE_SECS * 1000 > 1000) {
- /* Invalid difference, fallback to full handshake. */
- ssl->options.resuming = 0;
- break;
- }
-
- /* Check whether resumption is possible based on suites in SSL and
- * ciphersuite in ticket.
- */
- suite[0] = ssl->session.cipherSuite0;
- suite[1] = ssl->session.cipherSuite;
- if (!FindSuiteSSL(ssl, suite)) {
- current = current->next;
- continue;
- }
-
- #ifdef WOLFSSL_EARLY_DATA
- ssl->options.maxEarlyDataSz = ssl->session.maxEarlyDataSz;
- #endif
- /* Use the same cipher suite as before and set up for use. */
- ssl->options.cipherSuite0 = ssl->session.cipherSuite0;
- ssl->options.cipherSuite = ssl->session.cipherSuite;
- ret = SetCipherSpecs(ssl);
- if (ret != 0)
- return ret;
-
- /* Resumption PSK is resumption master secret. */
- ssl->arrays->psk_keySz = ssl->specs.hash_size;
- #ifdef WOLFSSL_TLS13_DRAFT_18
- XMEMCPY(ssl->arrays->psk_key, ssl->session.masterSecret,
- ssl->arrays->psk_keySz);
- #else
- if ((ret = DeriveResumptionPSK(ssl, ssl->session.ticketNonce.data,
- ssl->session.ticketNonce.len, ssl->arrays->psk_key)) != 0) {
- return ret;
- }
- #endif
-
- /* Derive the early secret using the PSK. */
- ret = DeriveEarlySecret(ssl);
- if (ret != 0)
- return ret;
- /* Derive the binder key to use to with HMAC. */
- ret = DeriveBinderKeyResume(ssl, binderKey);
- if (ret != 0)
- return ret;
- }
- else
- #endif
- #ifndef NO_PSK
- if ((ssl->options.server_psk_tls13_cb != NULL &&
- (ssl->arrays->psk_keySz = ssl->options.server_psk_tls13_cb(ssl,
- ssl->arrays->client_identity, ssl->arrays->psk_key,
- MAX_PSK_KEY_LEN, &cipherName)) != 0 &&
- GetCipherSuiteFromName(cipherName, &cipherSuite0,
- &cipherSuite) == 0) ||
- (ssl->options.server_psk_cb != NULL &&
- (ssl->arrays->psk_keySz = ssl->options.server_psk_cb(ssl,
- ssl->arrays->client_identity, ssl->arrays->psk_key,
- MAX_PSK_KEY_LEN)) != 0)) {
- if (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN)
- return PSK_KEY_ERROR;
-
- /* Check whether PSK ciphersuite is in SSL. */
- suite[0] = cipherSuite0;
- suite[1] = cipherSuite;
- if (!FindSuiteSSL(ssl, suite)) {
- current = current->next;
- continue;
- }
-
- /* Default to ciphersuite if cb doesn't specify. */
- ssl->options.resuming = 0;
-
- /* PSK age is always zero. */
- if (current->ticketAge != ssl->session.ticketAdd)
- return PSK_KEY_ERROR;
-
- /* Set PSK ciphersuite into SSL. */
- ssl->options.cipherSuite0 = cipherSuite0;
- ssl->options.cipherSuite = cipherSuite;
- ret = SetCipherSpecs(ssl);
- if (ret != 0)
- return ret;
-
- /* Derive the early secret using the PSK. */
- ret = DeriveEarlySecret(ssl);
- if (ret != 0)
- return ret;
- /* Derive the binder key to use to with HMAC. */
- ret = DeriveBinderKey(ssl, binderKey);
- if (ret != 0)
- return ret;
- }
- else
- #endif
- {
- current = current->next;
- continue;
- }
-
- ssl->options.sendVerify = 0;
-
- /* Derive the Finished message secret. */
- ret = DeriveFinishedSecret(ssl, binderKey,
- ssl->keys.client_write_MAC_secret);
- if (ret != 0)
- return ret;
-
- /* Derive the binder and compare with the one in the extension. */
- ret = BuildTls13HandshakeHmac(ssl,
- ssl->keys.client_write_MAC_secret, binder, &binderLen);
- if (ret != 0)
- return ret;
- if (binderLen != current->binderLen ||
- XMEMCMP(binder, current->binder, binderLen) != 0) {
- return BAD_BINDER;
- }
-
- /* This PSK works, no need to try any more. */
- current->chosen = 1;
- ext->resp = 1;
- break;
- }
-
- /* Hash the rest of the ClientHello. */
- ret = HashInputRaw(ssl, input + helloSz - bindersLen, bindersLen);
- if (ret != 0)
- return ret;
-
- if (current == NULL) {
-#ifdef WOLFSSL_PSK_ID_PROTECTION
- #ifndef NO_CERTS
- if (ssl->buffers.certChainCnt != 0)
- return 0;
- #endif
- return BAD_BINDER;
-#else
- return 0;
-#endif
- }
-
-#ifdef WOLFSSL_EARLY_DATA
- extEarlyData = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
- if (extEarlyData != NULL) {
- if (ssl->earlyData != no_early_data && current == ext->data) {
- extEarlyData->resp = 1;
-
- /* Derive early data decryption key. */
- ret = DeriveTls13Keys(ssl, early_data_key, DECRYPT_SIDE_ONLY, 1);
- if (ret != 0)
- return ret;
- if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0)
- return ret;
-
- ssl->earlyData = process_early_data;
- }
- else
- extEarlyData->resp = 0;
- }
-#endif
-
- /* Get the PSK key exchange modes the client wants to negotiate. */
- ext = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
- if (ext == NULL)
- return MISSING_HANDSHAKE_DATA;
- modes = ext->val;
-
- ext = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
- /* Use (EC)DHE for forward-security if possible. */
- if ((modes & (1 << PSK_DHE_KE)) != 0 && !ssl->options.noPskDheKe &&
- ext != NULL) {
- /* Only use named group used in last session. */
- ssl->namedGroup = ssl->session.namedGroup;
-
- /* Pick key share and Generate a new key if not present. */
- ret = TLSX_KeyShare_Establish(ssl);
- if (ret == KEY_SHARE_ERROR) {
- ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE;
- ret = 0;
- }
- else if (ret < 0)
- return ret;
-
- /* Send new public key to client. */
- ext->resp = 1;
- }
- else {
- if ((modes & (1 << PSK_KE)) == 0)
- return PSK_KEY_ERROR;
- ssl->options.noPskDheKe = 1;
- ssl->arrays->preMasterSz = 0;
- }
-
- *usingPSK = 1;
-
- WOLFSSL_LEAVE("DoPreSharedKeys", ret);
-
- return ret;
-}
-#endif
-
-#if !defined(WOLFSSL_TLS13_DRAFT_18) && defined(WOLFSSL_SEND_HRR_COOKIE)
-/* Check that the Cookie data's integrity.
- *
- * ssl SSL/TLS object.
- * cookie The cookie data - hash and MAC.
- * cookieSz The length of the cookie data in bytes.
- * returns Length of the hash on success, otherwise failure.
- */
-static int CheckCookie(WOLFSSL* ssl, byte* cookie, byte cookieSz)
-{
- int ret;
- byte mac[WC_MAX_DIGEST_SIZE] = {0};
- Hmac cookieHmac;
- byte cookieType = 0;
- byte macSz = 0;
-
-#if !defined(NO_SHA) && defined(NO_SHA256)
- cookieType = SHA;
- macSz = WC_SHA_DIGEST_SIZE;
-#endif /* NO_SHA */
-#ifndef NO_SHA256
- cookieType = WC_SHA256;
- macSz = WC_SHA256_DIGEST_SIZE;
-#endif /* NO_SHA256 */
-
- if (cookieSz < ssl->specs.hash_size + macSz)
- return HRR_COOKIE_ERROR;
- cookieSz -= macSz;
- XMEMSET(&cookieHmac, 0, sizeof(Hmac));
-
- ret = wc_HmacSetKey(&cookieHmac, cookieType,
- ssl->buffers.tls13CookieSecret.buffer,
- ssl->buffers.tls13CookieSecret.length);
- if (ret != 0)
- return ret;
- if ((ret = wc_HmacUpdate(&cookieHmac, cookie, cookieSz)) != 0)
- return ret;
- if ((ret = wc_HmacFinal(&cookieHmac, mac)) != 0)
- return ret;
-
- if (ConstantCompare(cookie + cookieSz, mac, macSz) != 0)
- return HRR_COOKIE_ERROR;
- return cookieSz;
-}
-
-/* Length of the KeyShare Extension */
-#define HRR_KEY_SHARE_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN)
-/* Length of the Supported Vresions Extension */
-#define HRR_VERSIONS_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN)
-/* Length of the Cookie Extension excluding cookie data */
-#define HRR_COOKIE_HDR_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN)
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* PV | CipherSuite | Ext Len */
-#define HRR_BODY_SZ (OPAQUE16_LEN + OPAQUE16_LEN + OPAQUE16_LEN)
-/* HH | PV | CipherSuite | Ext Len | Key Share | Cookie */
-#define MAX_HRR_SZ (HANDSHAKE_HEADER_SZ + \
- HRR_BODY_SZ + \
- HRR_KEY_SHARE_SZ + \
- HRR_COOKIE_HDR_SZ)
-#else
-/* PV | Random | Session Id | CipherSuite | Compression | Ext Len */
-#define HRR_BODY_SZ (VERSION_SZ + RAN_LEN + ENUM_LEN + ID_LEN + \
- SUITE_LEN + COMP_LEN + OPAQUE16_LEN)
-/* HH | PV | CipherSuite | Ext Len | Key Share | Supported Version | Cookie */
-#define MAX_HRR_SZ (HANDSHAKE_HEADER_SZ + \
- HRR_BODY_SZ + \
- HRR_KEY_SHARE_SZ + \
- HRR_VERSIONS_SZ + \
- HRR_COOKIE_HDR_SZ)
-#endif
-
-/* Restart the handshake hash from the cookie value.
- *
- * ssl SSL/TLS object.
- * cookie Cookie data from client.
- * returns 0 on success, otherwise failure.
- */
-static int RestartHandshakeHashWithCookie(WOLFSSL* ssl, Cookie* cookie)
-{
- byte header[HANDSHAKE_HEADER_SZ] = {0};
- byte hrr[MAX_HRR_SZ] = {0};
- int hrrIdx;
- word32 idx;
- byte hashSz;
- byte* cookieData;
- byte cookieDataSz;
- word16 length;
- int keyShareExt = 0;
- int ret;
-
- cookieDataSz = ret = CheckCookie(ssl, &cookie->data, cookie->len);
- if (ret < 0)
- return ret;
- hashSz = cookie->data;
- cookieData = &cookie->data;
- idx = OPAQUE8_LEN;
-
- /* Restart handshake hash with synthetic message hash. */
- AddTls13HandShakeHeader(header, hashSz, 0, 0, message_hash, ssl);
- if ((ret = InitHandshakeHashes(ssl)) != 0)
- return ret;
- if ((ret = HashOutputRaw(ssl, header, sizeof(header))) != 0)
- return ret;
- if ((ret = HashOutputRaw(ssl, cookieData + idx, hashSz)) != 0)
- return ret;
-
- /* Reconstruct the HelloRetryMessage for handshake hash. */
-#ifdef WOLFSSL_TLS13_DRAFT_18
- length = HRR_BODY_SZ + HRR_COOKIE_HDR_SZ + cookie->len;
-#else
- length = HRR_BODY_SZ - ID_LEN + ssl->session.sessionIDSz +
- HRR_COOKIE_HDR_SZ + cookie->len;
- length += HRR_VERSIONS_SZ;
-#endif
- if (cookieDataSz > hashSz + OPAQUE16_LEN) {
- keyShareExt = 1;
- length += HRR_KEY_SHARE_SZ;
- }
-#ifdef WOLFSSL_TLS13_DRAFT_18
- AddTls13HandShakeHeader(hrr, length, 0, 0, hello_retry_request, ssl);
-
- idx += hashSz;
- hrrIdx = HANDSHAKE_HEADER_SZ;
- /* The negotiated protocol version. */
- hrr[hrrIdx++] = TLS_DRAFT_MAJOR;
- hrr[hrrIdx++] = TLS_DRAFT_MINOR;
- /* Cipher Suite */
- hrr[hrrIdx++] = cookieData[idx++];
- hrr[hrrIdx++] = cookieData[idx++];
-
- /* Extensions' length */
- length -= HRR_BODY_SZ;
- c16toa(length, hrr + hrrIdx);
- hrrIdx += 2;
-#else
- AddTls13HandShakeHeader(hrr, length, 0, 0, server_hello, ssl);
-
- idx += hashSz;
- hrrIdx = HANDSHAKE_HEADER_SZ;
-
- /* The negotiated protocol version. */
- hrr[hrrIdx++] = ssl->version.major;
- hrr[hrrIdx++] = TLSv1_2_MINOR;
-
- /* HelloRetryRequest message has fixed value for random. */
- XMEMCPY(hrr + hrrIdx, helloRetryRequestRandom, RAN_LEN);
- hrrIdx += RAN_LEN;
-
- hrr[hrrIdx++] = ssl->session.sessionIDSz;
- if (ssl->session.sessionIDSz > 0) {
- XMEMCPY(hrr + hrrIdx, ssl->session.sessionID, ssl->session.sessionIDSz);
- hrrIdx += ssl->session.sessionIDSz;
- }
-
- /* Cipher Suite */
- hrr[hrrIdx++] = cookieData[idx++];
- hrr[hrrIdx++] = cookieData[idx++];
-
- /* Compression not supported in TLS v1.3. */
- hrr[hrrIdx++] = 0;
-
- /* Extensions' length */
- length -= HRR_BODY_SZ - ID_LEN + ssl->session.sessionIDSz;
- c16toa(length, hrr + hrrIdx);
- hrrIdx += 2;
-
-#endif
- /* Optional KeyShare Extension */
- if (keyShareExt) {
- c16toa(TLSX_KEY_SHARE, hrr + hrrIdx);
- hrrIdx += 2;
- c16toa(OPAQUE16_LEN, hrr + hrrIdx);
- hrrIdx += 2;
- hrr[hrrIdx++] = cookieData[idx++];
- hrr[hrrIdx++] = cookieData[idx++];
- }
-#ifndef WOLFSSL_TLS13_DRAFT_18
- c16toa(TLSX_SUPPORTED_VERSIONS, hrr + hrrIdx);
- hrrIdx += 2;
- c16toa(OPAQUE16_LEN, hrr + hrrIdx);
- hrrIdx += 2;
- #ifdef WOLFSSL_TLS13_DRAFT
- hrr[hrrIdx++] = TLS_DRAFT_MAJOR;
- hrr[hrrIdx++] = TLS_DRAFT_MINOR;
- #else
- hrr[hrrIdx++] = ssl->version.major;
- hrr[hrrIdx++] = ssl->version.minor;
- #endif
-#endif
- /* Mandatory Cookie Extension */
- c16toa(TLSX_COOKIE, hrr + hrrIdx);
- hrrIdx += 2;
- c16toa(cookie->len + OPAQUE16_LEN, hrr + hrrIdx);
- hrrIdx += 2;
- c16toa(cookie->len, hrr + hrrIdx);
- hrrIdx += 2;
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Reconstucted HelloRetryRequest");
- WOLFSSL_BUFFER(hrr, hrrIdx);
- WOLFSSL_MSG("Cookie");
- WOLFSSL_BUFFER(cookieData, cookie->len);
-#endif
-
- if ((ret = HashOutputRaw(ssl, hrr, hrrIdx)) != 0)
- return ret;
- return HashOutputRaw(ssl, cookieData, cookie->len);
-}
-#endif
-
-/* Do SupportedVersion extension for TLS v1.3+ otherwise it is not.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * i The index into the message buffer of ClientHello.
- * helloSz The length of the current handshake message.
- * returns 0 on success and otherwise failure.
- */
-static int DoTls13SupportedVersions(WOLFSSL* ssl, const byte* input, word32 i,
- word32 helloSz, int* wantDowngrade)
-{
- int ret;
- byte b;
- word16 suiteSz;
- word16 totalExtSz;
- int foundVersion = 0;
-
- /* Client random */
- i += RAN_LEN;
- /* Session id - not used in TLS v1.3 */
- b = input[i++];
- if (i + b > helloSz) {
- return BUFFER_ERROR;
- }
- i += b;
- /* Cipher suites */
- if (i + OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
- ato16(input + i, &suiteSz);
- i += OPAQUE16_LEN;
- if (i + suiteSz + 1 > helloSz)
- return BUFFER_ERROR;
- i += suiteSz;
- /* Compression */
- b = input[i++];
- if (i + b > helloSz)
- return BUFFER_ERROR;
- i += b;
-
- /* TLS 1.3 must have extensions */
- if (i < helloSz) {
- if (i + OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
- ato16(&input[i], &totalExtSz);
- i += OPAQUE16_LEN;
- if (totalExtSz != helloSz - i)
- return BUFFER_ERROR;
-
- /* Need to negotiate version first. */
- if ((ret = TLSX_ParseVersion(ssl, (byte*)input + i, totalExtSz,
- client_hello, &foundVersion))) {
- return ret;
- }
- }
- *wantDowngrade = !foundVersion || !IsAtLeastTLSv1_3(ssl->version);
-
- return 0;
-}
-
-/* Handle a ClientHello handshake message.
- * If the protocol version in the message is not TLS v1.3 or higher, use
- * DoClientHello()
- * Only a server will receive this message.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the message buffer of ClientHello.
- * On exit, the index of byte after the ClientHello message and
- * padding.
- * helloSz The length of the current handshake message.
- * returns 0 on success and otherwise failure.
- */
-int DoTls13ClientHello(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- word32 helloSz)
-{
- int ret = VERSION_ERROR;
- byte b = 0;
- ProtocolVersion pv;
- Suites clSuites;
- word32 i = *inOutIdx;
- word32 begin = i;
- word16 totalExtSz = 0;
- int usingPSK = 0;
- byte sessIdSz = 0;
- int wantDowngrade = 0;
-
- WOLFSSL_START(WC_FUNC_CLIENT_HELLO_DO);
- WOLFSSL_ENTER("DoTls13ClientHello");
-
- XMEMSET(&pv, 0, sizeof(ProtocolVersion));
- XMEMSET(&clSuites, 0, sizeof(Suites));
-
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "ClientHello");
- if (ssl->toInfoOn) AddLateName("ClientHello", &ssl->timeoutInfo);
-#endif
-
- /* protocol version, random and session id length check */
- if ((i - begin) + OPAQUE16_LEN + RAN_LEN + OPAQUE8_LEN > helloSz)
- return BUFFER_ERROR;
-
- /* Protocol version */
- XMEMCPY(&pv, input + i, OPAQUE16_LEN);
- ssl->chVersion = pv; /* store */
- i += OPAQUE16_LEN;
- if (pv.major < SSLv3_MAJOR) {
- WOLFSSL_MSG("Legacy version field contains unsupported value");
- #ifdef WOLFSSL_MYSQL_COMPATIBLE
- SendAlert(ssl, alert_fatal, wc_protocol_version);
- #else
- SendAlert(ssl, alert_fatal, protocol_version);
- #endif
- return INVALID_PARAMETER;
- }
- /* Legacy protocol version cannot negotiate TLS 1.3 or higher. */
- if (pv.major > SSLv3_MAJOR || (pv.major == SSLv3_MAJOR &&
- pv.minor >= TLSv1_3_MINOR)) {
- pv.major = SSLv3_MAJOR;
- pv.minor = TLSv1_2_MINOR;
- wantDowngrade = 1;
- ssl->version.minor = pv.minor;
- }
- /* Legacy version must be [ SSLv3_MAJOR, TLSv1_2_MINOR ] for TLS v1.3 */
- else if (pv.major == SSLv3_MAJOR && pv.minor < TLSv1_2_MINOR) {
- wantDowngrade = 1;
- ssl->version.minor = pv.minor;
- }
- else {
- ret = DoTls13SupportedVersions(ssl, input + begin, i - begin, helloSz,
- &wantDowngrade);
- if (ret < 0)
- return ret;
- }
- if (wantDowngrade) {
-#ifndef WOLFSSL_NO_TLS12
- if (!ssl->options.downgrade) {
- WOLFSSL_MSG("Client trying to connect with lesser version than "
- "TLS v1.3");
- return VERSION_ERROR;
- }
-
- if (pv.minor < ssl->options.minDowngrade)
- return VERSION_ERROR;
-
- if ((ret = HashInput(ssl, input + begin, helloSz)) != 0)
- return ret;
- return DoClientHello(ssl, input, inOutIdx, helloSz);
-#else
- WOLFSSL_MSG("Client trying to connect with lesser version than "
- "TLS v1.3");
- return VERSION_ERROR;
-#endif
- }
-
- /* Client random */
- XMEMCPY(ssl->arrays->clientRandom, input + i, RAN_LEN);
- i += RAN_LEN;
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("client random");
- WOLFSSL_BUFFER(ssl->arrays->clientRandom, RAN_LEN);
-#endif
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- /* Session id - empty in TLS v1.3 */
- sessIdSz = input[i++];
- if (sessIdSz > 0 && !ssl->options.downgrade) {
- WOLFSSL_MSG("Client sent session id - not supported");
- return BUFFER_ERROR;
- }
-#else
- sessIdSz = input[i++];
- if (sessIdSz != ID_LEN && sessIdSz != 0)
- return INVALID_PARAMETER;
-#endif
-
- if (sessIdSz + i > helloSz) {
- return BUFFER_ERROR;
- }
-
- ssl->session.sessionIDSz = sessIdSz;
- if (sessIdSz == ID_LEN) {
- XMEMCPY(ssl->session.sessionID, input + i, sessIdSz);
- i += ID_LEN;
- }
-
- /* Cipher suites */
- if ((i - begin) + OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
- ato16(&input[i], &clSuites.suiteSz);
- i += OPAQUE16_LEN;
- /* suites and compression length check */
- if ((i - begin) + clSuites.suiteSz + OPAQUE8_LEN > helloSz)
- return BUFFER_ERROR;
- if (clSuites.suiteSz > WOLFSSL_MAX_SUITE_SZ)
- return BUFFER_ERROR;
- XMEMCPY(clSuites.suites, input + i, clSuites.suiteSz);
- i += clSuites.suiteSz;
- clSuites.hashSigAlgoSz = 0;
-
-#ifdef HAVE_SERVER_RENEGOTIATION_INFO
- ret = FindSuite(&clSuites, 0, TLS_EMPTY_RENEGOTIATION_INFO_SCSV);
- if (ret == SUITES_ERROR)
- return BUFFER_ERROR;
- if (ret >= 0) {
- TLSX* extension;
-
- /* check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV suite */
- ret = TLSX_AddEmptyRenegotiationInfo(&ssl->extensions, ssl->heap);
- if (ret != WOLFSSL_SUCCESS)
- return ret;
-
- extension = TLSX_Find(ssl->extensions, TLSX_RENEGOTIATION_INFO);
- if (extension) {
- ssl->secure_renegotiation = (SecureRenegotiation*)extension->data;
- ssl->secure_renegotiation->enabled = 1;
- }
- }
-#endif /* HAVE_SERVER_RENEGOTIATION_INFO */
-
- /* Compression */
- b = input[i++];
- if ((i - begin) + b > helloSz)
- return BUFFER_ERROR;
- if (b != COMP_LEN) {
- WOLFSSL_MSG("Must be one compression type in list");
- return INVALID_PARAMETER;
- }
- b = input[i++];
- if (b != NO_COMPRESSION) {
- WOLFSSL_MSG("Must be no compression type in list");
- return INVALID_PARAMETER;
- }
-
- /* Extensions */
- if ((i - begin) == helloSz)
- return BUFFER_ERROR;
- if ((i - begin) + OPAQUE16_LEN > helloSz)
- return BUFFER_ERROR;
-
- ato16(&input[i], &totalExtSz);
- i += OPAQUE16_LEN;
- if ((i - begin) + totalExtSz > helloSz)
- return BUFFER_ERROR;
-
- /* Auto populate extensions supported unless user defined. */
- if ((ret = TLSX_PopulateExtensions(ssl, 1)) != 0)
- return ret;
-
- /* Parse extensions */
- if ((ret = TLSX_Parse(ssl, (byte*)input + i, totalExtSz, client_hello,
- &clSuites))) {
- return ret;
- }
-
-#if defined(OPENSSL_ALL) || defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
- defined(WOLFSSL_HAPROXY)
- if ((ret = SNI_Callback(ssl)) != 0)
- return ret;
- ssl->options.side = WOLFSSL_SERVER_END;
-#endif /* OPENSSL_ALL || HAVE_STUNNEL || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
-
- i += totalExtSz;
- *inOutIdx = i;
-
- ssl->options.sendVerify = SEND_CERT;
-
- ssl->options.clientState = CLIENT_HELLO_COMPLETE;
- ssl->options.haveSessionId = 1;
-
-#if !defined(WOLFSSL_TLS13_DRAFT_18) && defined(WOLFSSL_SEND_HRR_COOKIE)
- if (ssl->options.sendCookie &&
- ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
- TLSX* ext;
-
- if ((ext = TLSX_Find(ssl->extensions, TLSX_COOKIE)) == NULL)
- return HRR_COOKIE_ERROR;
- /* Ensure the cookie came from client and isn't the one in the
- * response - HelloRetryRequest.
- */
- if (ext->resp == 1)
- return HRR_COOKIE_ERROR;
- ret = RestartHandshakeHashWithCookie(ssl, (Cookie*)ext->data);
- if (ret != 0)
- return ret;
- }
-#endif
-
-#if (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)) && \
- defined(HAVE_TLS_EXTENSIONS)
- if (TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY) != NULL) {
- /* Refine list for PSK processing. */
- RefineSuites(ssl, &clSuites);
-
- /* Process the Pre-Shared Key extension if present. */
- ret = DoPreSharedKeys(ssl, input + begin, helloSz, &usingPSK);
- if (ret != 0)
- return ret;
- }
- else
-#endif
- {
-#ifdef WOLFSSL_EARLY_DATA
- ssl->earlyData = no_early_data;
-#endif
- if ((ret = HashInput(ssl, input + begin, helloSz)) != 0)
- return ret;
-
- }
-
- if (!usingPSK) {
- if (TLSX_Find(ssl->extensions, TLSX_KEY_SHARE) == NULL) {
- WOLFSSL_MSG("Client did not send a KeyShare extension");
- SendAlert(ssl, alert_fatal, missing_extension);
- return INCOMPLETE_DATA;
- }
- if (TLSX_Find(ssl->extensions, TLSX_SIGNATURE_ALGORITHMS) == NULL) {
- WOLFSSL_MSG("Client did not send a SignatureAlgorithms extension");
- SendAlert(ssl, alert_fatal, missing_extension);
- return INCOMPLETE_DATA;
- }
-
- if ((ret = MatchSuite(ssl, &clSuites)) < 0) {
- WOLFSSL_MSG("Unsupported cipher suite, ClientHello");
- SendAlert(ssl, alert_fatal, handshake_failure);
- return ret;
- }
-
-#ifdef HAVE_NULL_CIPHER
- if (ssl->options.cipherSuite0 == ECC_BYTE &&
- (ssl->options.cipherSuite == TLS_SHA256_SHA256 ||
- ssl->options.cipherSuite == TLS_SHA384_SHA384)) {
- ;
- }
- else
-#endif
- /* Check that the negotiated ciphersuite matches protocol version. */
- if (ssl->options.cipherSuite0 != TLS13_BYTE) {
- WOLFSSL_MSG("Negotiated ciphersuite from lesser version than "
- "TLS v1.3");
- SendAlert(ssl, alert_fatal, handshake_failure);
- return VERSION_ERROR;
- }
-
-#ifdef HAVE_SESSION_TICKET
- if (ssl->options.resuming) {
- ssl->options.resuming = 0;
- XMEMSET(ssl->arrays->psk_key, 0, ssl->specs.hash_size);
- }
-#endif
-
- /* Derive early secret for handshake secret. */
- if ((ret = DeriveEarlySecret(ssl)) != 0)
- return ret;
- }
-
- WOLFSSL_LEAVE("DoTls13ClientHello", ret);
- WOLFSSL_END(WC_FUNC_CLIENT_HELLO_DO);
-
- return ret;
-}
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
-/* handle generation of TLS 1.3 hello_retry_request (6) */
-/* Send the HelloRetryRequest message to indicate the negotiated protocol
- * version and security parameters the server is willing to use.
- * Only a server will send this message.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-int SendTls13HelloRetryRequest(WOLFSSL* ssl)
-{
- int ret;
- byte* output;
- word32 length;
- word16 len;
- word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- int sendSz;
-
- WOLFSSL_ENTER("SendTls13HelloRetryRequest");
-
- /* Get the length of the extensions that will be written. */
- len = 0;
- ret = TLSX_GetResponseSize(ssl, hello_retry_request, &len);
- /* There must be extensions sent to indicate what client needs to do. */
- if (ret != 0)
- return MISSING_HANDSHAKE_DATA;
-
- /* Protocol version + Extensions */
- length = OPAQUE16_LEN + len;
- sendSz = idx + length;
-
- /* Check buffers are big enough and grow if needed. */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* Get position in output buffer to write new message to. */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
- /* Add record and handshake headers. */
- AddTls13Headers(output, length, hello_retry_request, ssl);
-
- /* The negotiated protocol version. */
- output[idx++] = TLS_DRAFT_MAJOR;
- output[idx++] = TLS_DRAFT_MINOR;
-
- /* Add TLS extensions. */
- ret = TLSX_WriteResponse(ssl, output + idx, hello_retry_request, NULL);
- if (ret != 0)
- return ret;
- idx += len;
-
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "HelloRetryRequest");
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "HelloRetryRequest", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- }
-#endif
- if ((ret = HashOutput(ssl, output, idx, 0)) != 0)
- return ret;
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- if (!ssl->options.groupMessages)
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendTls13HelloRetryRequest", ret);
-
- return ret;
-}
-#endif /* WOLFSSL_TLS13_DRAFT_18 */
-
-/* Send TLS v1.3 ServerHello message to client.
- * Only a server will send this message.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-#ifdef WOLFSSL_TLS13_DRAFT_18
-static
-#endif
-/* handle generation of TLS 1.3 server_hello (2) */
-int SendTls13ServerHello(WOLFSSL* ssl, byte extMsgType)
-{
- int ret;
- byte* output;
- word16 length;
- word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- int sendSz;
-
- WOLFSSL_START(WC_FUNC_SERVER_HELLO_SEND);
- WOLFSSL_ENTER("SendTls13ServerHello");
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- if (extMsgType == hello_retry_request) {
- WOLFSSL_MSG("wolfSSL Doing HelloRetryRequest");
- if ((ret = RestartHandshakeHash(ssl)) < 0)
- return ret;
- }
-#endif
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- /* Protocol version, server random, cipher suite and extensions. */
- length = VERSION_SZ + RAN_LEN + SUITE_LEN;
- ret = TLSX_GetResponseSize(ssl, server_hello, &length);
- if (ret != 0)
- return ret;
-#else
- /* Protocol version, server random, session id, cipher suite, compression
- * and extensions.
- */
- length = VERSION_SZ + RAN_LEN + ENUM_LEN + ssl->session.sessionIDSz +
- SUITE_LEN + COMP_LEN;
- ret = TLSX_GetResponseSize(ssl, extMsgType, &length);
- if (ret != 0)
- return ret;
-#endif
- sendSz = idx + length;
-
- /* Check buffers are big enough and grow if needed. */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* Get position in output buffer to write new message to. */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* Put the record and handshake headers on. */
- AddTls13Headers(output, length, server_hello, ssl);
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- /* The negotiated protocol version. */
- output[idx++] = TLS_DRAFT_MAJOR;
- output[idx++] = TLS_DRAFT_MINOR;
-#else
- /* The protocol version must be TLS v1.2 for middleboxes. */
- output[idx++] = ssl->version.major;
- output[idx++] = TLSv1_2_MINOR;
-#endif
-
- if (extMsgType == server_hello) {
- /* Generate server random. */
- if ((ret = wc_RNG_GenerateBlock(ssl->rng, output + idx, RAN_LEN)) != 0)
- return ret;
- }
-#ifndef WOLFSSL_TLS13_DRAFT_18
- else {
- /* HelloRetryRequest message has fixed value for random. */
- XMEMCPY(output + idx, helloRetryRequestRandom, RAN_LEN);
- }
-#endif
- /* Store in SSL for debugging. */
- XMEMCPY(ssl->arrays->serverRandom, output + idx, RAN_LEN);
- idx += RAN_LEN;
-
-#ifdef WOLFSSL_DEBUG_TLS
- WOLFSSL_MSG("Server random");
- WOLFSSL_BUFFER(ssl->arrays->serverRandom, RAN_LEN);
-#endif
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- output[idx++] = ssl->session.sessionIDSz;
- if (ssl->session.sessionIDSz > 0) {
- XMEMCPY(output + idx, ssl->session.sessionID, ssl->session.sessionIDSz);
- idx += ssl->session.sessionIDSz;
- }
-#endif
-
- /* Chosen cipher suite */
- output[idx++] = ssl->options.cipherSuite0;
- output[idx++] = ssl->options.cipherSuite;
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- /* Compression not supported in TLS v1.3. */
- output[idx++] = 0;
-#endif
-
- /* Extensions */
- ret = TLSX_WriteResponse(ssl, output + idx, extMsgType, NULL);
- if (ret != 0)
- return ret;
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- if ((ret = HashOutput(ssl, output, sendSz, 0)) != 0)
- return ret;
-
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "ServerHello");
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "ServerHello", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- }
- #endif
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- ssl->options.serverState = SERVER_HELLO_COMPLETE;
-#else
- if (extMsgType == server_hello)
- ssl->options.serverState = SERVER_HELLO_COMPLETE;
-#endif
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- if (!ssl->options.groupMessages)
-#else
- if (!ssl->options.groupMessages || extMsgType != server_hello)
-#endif
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendTls13ServerHello", ret);
- WOLFSSL_END(WC_FUNC_SERVER_HELLO_SEND);
-
- return ret;
-}
-
-/* handle generation of TLS 1.3 encrypted_extensions (8) */
-/* Send the rest of the extensions encrypted under the handshake key.
- * This message is always encrypted in TLS v1.3.
- * Only a server will send this message.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-static int SendTls13EncryptedExtensions(WOLFSSL* ssl)
-{
- int ret;
- byte* output;
- word16 length = 0;
- word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- int sendSz;
-
- WOLFSSL_START(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND);
- WOLFSSL_ENTER("SendTls13EncryptedExtensions");
-
- ssl->keys.encryptionOn = 1;
-
-#ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
- if ((ret = TLSX_SupportedCurve_CheckPriority(ssl)) != 0)
- return ret;
-#endif
-
- /* Derive the handshake secret now that we are at first message to be
- * encrypted under the keys.
- */
- if ((ret = DeriveHandshakeSecret(ssl)) != 0)
- return ret;
- if ((ret = DeriveTls13Keys(ssl, handshake_key,
- ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0)
- return ret;
-
- /* Setup encrypt/decrypt keys for following messages. */
-#ifdef WOLFSSL_EARLY_DATA
- if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0)
- return ret;
- if (ssl->earlyData != process_early_data) {
- if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0)
- return ret;
- }
-#else
- if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0)
- return ret;
-#endif
-
- ret = TLSX_GetResponseSize(ssl, encrypted_extensions, &length);
- if (ret != 0)
- return ret;
-
- sendSz = idx + length;
- /* Encryption always on. */
- sendSz += MAX_MSG_EXTRA;
-
- /* Check buffers are big enough and grow if needed. */
- ret = CheckAvailableSize(ssl, sendSz);
- if (ret != 0)
- return ret;
-
- /* Get position in output buffer to write new message to. */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* Put the record and handshake headers on. */
- AddTls13Headers(output, length, encrypted_extensions, ssl);
-
- ret = TLSX_WriteResponse(ssl, output + idx, encrypted_extensions, NULL);
- if (ret != 0)
- return ret;
- idx += length;
-
-#ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "EncryptedExtensions");
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "EncryptedExtensions", handshake, output,
- sendSz, WRITE_PROTO, ssl->heap);
- }
-#endif
-
- /* This handshake message is always encrypted. */
- sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ,
- idx - RECORD_HEADER_SZ, handshake, 1, 0, 0);
- if (sendSz < 0)
- return sendSz;
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- ssl->options.serverState = SERVER_ENCRYPTED_EXTENSIONS_COMPLETE;
-
- if (!ssl->options.groupMessages)
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendTls13EncryptedExtensions", ret);
- WOLFSSL_END(WC_FUNC_ENCRYPTED_EXTENSIONS_SEND);
-
- return ret;
-}
-
-#ifndef NO_CERTS
-/* handle generation TLS v1.3 certificate_request (13) */
-/* Send the TLS v1.3 CertificateRequest message.
- * This message is always encrypted in TLS v1.3.
- * Only a server will send this message.
- *
- * ssl SSL/TLS object.
- * reqCtx Request context.
- * reqCtxLen Length of context. 0 when sending as part of handshake.
- * returns 0 on success, otherwise failure.
- */
-static int SendTls13CertificateRequest(WOLFSSL* ssl, byte* reqCtx,
- int reqCtxLen)
-{
- byte* output;
- int ret;
- int sendSz;
- word32 i;
- word16 reqSz;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- TLSX* ext;
-#endif
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_REQUEST_SEND);
- WOLFSSL_ENTER("SendTls13CertificateRequest");
-
- if (ssl->options.side == WOLFSSL_SERVER_END)
- InitSuitesHashSigAlgo(ssl->suites, 1, 1, 0, 1, ssl->buffers.keySz);
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- reqSz = OPAQUE8_LEN + reqCtxLen + REQ_HEADER_SZ + REQ_HEADER_SZ;
- reqSz += LENGTH_SZ + ssl->suites->hashSigAlgoSz;
-
- sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + reqSz;
- /* Always encrypted and make room for padding. */
- sendSz += MAX_MSG_EXTRA;
-
- /* Check buffers are big enough and grow if needed. */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* Get position in output buffer to write new message to. */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* Put the record and handshake headers on. */
- AddTls13Headers(output, reqSz, certificate_request, ssl);
-
- /* Certificate request context. */
- output[i++] = reqCtxLen;
- if (reqCtxLen != 0) {
- XMEMCPY(output + i, reqCtx, reqCtxLen);
- i += reqCtxLen;
- }
-
- /* supported hash/sig */
- c16toa(ssl->suites->hashSigAlgoSz, &output[i]);
- i += LENGTH_SZ;
-
- XMEMCPY(&output[i], ssl->suites->hashSigAlgo, ssl->suites->hashSigAlgoSz);
- i += ssl->suites->hashSigAlgoSz;
-
- /* Certificate authorities not supported yet - empty buffer. */
- c16toa(0, &output[i]);
- i += REQ_HEADER_SZ;
-
- /* Certificate extensions. */
- c16toa(0, &output[i]); /* auth's */
- i += REQ_HEADER_SZ;
-#else
- ext = TLSX_Find(ssl->extensions, TLSX_SIGNATURE_ALGORITHMS);
- if (ext == NULL)
- return EXT_MISSING;
- ext->resp = 0;
-
- i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- reqSz = (word16)(OPAQUE8_LEN + reqCtxLen);
- ret = TLSX_GetRequestSize(ssl, certificate_request, &reqSz);
- if (ret != 0)
- return ret;
-
- sendSz = i + reqSz;
- /* Always encrypted and make room for padding. */
- sendSz += MAX_MSG_EXTRA;
-
- /* Check buffers are big enough and grow if needed. */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* Get position in output buffer to write new message to. */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* Put the record and handshake headers on. */
- AddTls13Headers(output, reqSz, certificate_request, ssl);
-
- /* Certificate request context. */
- output[i++] = (byte)reqCtxLen;
- if (reqCtxLen != 0) {
- XMEMCPY(output + i, reqCtx, reqCtxLen);
- i += reqCtxLen;
- }
-
- /* Certificate extensions. */
- reqSz = 0;
- ret = TLSX_WriteRequest(ssl, output + i, certificate_request, &reqSz);
- if (ret != 0)
- return ret;
- i += reqSz;
-#endif
-
- /* Always encrypted. */
- sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ,
- i - RECORD_HEADER_SZ, handshake, 1, 0, 0);
- if (sendSz < 0)
- return sendSz;
-
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "CertificateRequest");
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "CertificateRequest", handshake, output,
- sendSz, WRITE_PROTO, ssl->heap);
- }
- #endif
-
- ssl->buffers.outputBuffer.length += sendSz;
- if (!ssl->options.groupMessages)
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendTls13CertificateRequest", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_REQUEST_SEND);
-
- return ret;
-}
-#endif /* NO_CERTS */
-#endif /* NO_WOLFSSL_SERVER */
-
-#ifndef NO_CERTS
-#if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)
-/* Encode the signature algorithm into buffer.
- *
- * hashalgo The hash algorithm.
- * hsType The signature type.
- * output The buffer to encode into.
- */
-static WC_INLINE void EncodeSigAlg(byte hashAlgo, byte hsType, byte* output)
-{
- switch (hsType) {
-#ifdef HAVE_ECC
- case ecc_dsa_sa_algo:
- output[0] = hashAlgo;
- output[1] = ecc_dsa_sa_algo;
- break;
-#endif
-#ifdef HAVE_ED25519
- /* ED25519: 0x0807 */
- case ed25519_sa_algo:
- output[0] = ED25519_SA_MAJOR;
- output[1] = ED25519_SA_MINOR;
- (void)hashAlgo;
- break;
-#endif
-#ifdef HAVE_ED448
- /* ED448: 0x0808 */
- case ed448_sa_algo:
- output[0] = ED448_SA_MAJOR;
- output[1] = ED448_SA_MINOR;
- (void)hashAlgo;
- break;
-#endif
-#ifndef NO_RSA
- /* PSS signatures: 0x080[4-6] */
- case rsa_pss_sa_algo:
- output[0] = rsa_pss_sa_algo;
- output[1] = hashAlgo;
- break;
-#endif
- }
-}
-
-/* Decode the signature algorithm.
- *
- * input The encoded signature algorithm.
- * hashalgo The hash algorithm.
- * hsType The signature type.
- * returns INVALID_PARAMETER if not recognized and 0 otherwise.
- */
-static WC_INLINE int DecodeTls13SigAlg(byte* input, byte* hashAlgo,
- byte* hsType)
-{
- int ret = 0;
-
- switch (input[0]) {
- case NEW_SA_MAJOR:
- /* PSS signatures: 0x080[4-6] */
- if (input[1] >= sha256_mac && input[1] <= sha512_mac) {
- *hsType = input[0];
- *hashAlgo = input[1];
- }
- #ifdef HAVE_ED25519
- /* ED25519: 0x0807 */
- else if (input[1] == ED25519_SA_MINOR) {
- *hsType = ed25519_sa_algo;
- /* Hash performed as part of sign/verify operation. */
- *hashAlgo = sha512_mac;
- }
- #endif
- #ifdef HAVE_ED448
- /* ED448: 0x0808 */
- else if (input[1] == ED448_SA_MINOR) {
- *hsType = ed448_sa_algo;
- /* Hash performed as part of sign/verify operation. */
- *hashAlgo = sha512_mac;
- }
- #endif
- else
- ret = INVALID_PARAMETER;
- break;
- default:
- *hashAlgo = input[0];
- *hsType = input[1];
- break;
- }
-
- return ret;
-}
-
-/* Get the hash of the messages so far.
- *
- * ssl The SSL/TLS object.
- * hash The buffer to write the hash to.
- * returns the length of the hash.
- */
-static WC_INLINE int GetMsgHash(WOLFSSL* ssl, byte* hash)
-{
- int ret = 0;
- switch (ssl->specs.mac_algorithm) {
- #ifndef NO_SHA256
- case sha256_mac:
- ret = wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
- if (ret == 0)
- ret = WC_SHA256_DIGEST_SIZE;
- break;
- #endif /* !NO_SHA256 */
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- ret = wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
- if (ret == 0)
- ret = WC_SHA384_DIGEST_SIZE;
- break;
- #endif /* WOLFSSL_SHA384 */
- #ifdef WOLFSSL_TLS13_SHA512
- case sha512_mac:
- ret = wc_Sha512GetHash(&ssl->hsHashes->hashSha512, hash);
- if (ret == 0)
- ret = WC_SHA512_DIGEST_SIZE;
- break;
- #endif /* WOLFSSL_TLS13_SHA512 */
- }
- return ret;
-}
-
-/* The length of the certificate verification label - client and server. */
-#define CERT_VFY_LABEL_SZ 34
-/* The server certificate verification label. */
-static const byte serverCertVfyLabel[CERT_VFY_LABEL_SZ] =
- "TLS 1.3, server CertificateVerify";
-/* The client certificate verification label. */
-static const byte clientCertVfyLabel[CERT_VFY_LABEL_SZ] =
- "TLS 1.3, client CertificateVerify";
-
-/* The number of prefix bytes for signature data. */
-#define SIGNING_DATA_PREFIX_SZ 64
-/* The prefix byte in the signature data. */
-#define SIGNING_DATA_PREFIX_BYTE 0x20
-/* Maximum length of the signature data. */
-#define MAX_SIG_DATA_SZ (SIGNING_DATA_PREFIX_SZ + \
- CERT_VFY_LABEL_SZ + \
- WC_MAX_DIGEST_SIZE)
-
-/* Create the signature data for TLS v1.3 certificate verification.
- *
- * ssl The SSL/TLS object.
- * sigData The signature data.
- * sigDataSz The length of the signature data.
- * check Indicates this is a check not create.
- */
-static int CreateSigData(WOLFSSL* ssl, byte* sigData, word16* sigDataSz,
- int check)
-{
- word16 idx;
- int side = ssl->options.side;
- int ret;
-
- /* Signature Data = Prefix | Label | Handshake Hash */
- XMEMSET(sigData, SIGNING_DATA_PREFIX_BYTE, SIGNING_DATA_PREFIX_SZ);
- idx = SIGNING_DATA_PREFIX_SZ;
-
- if ((side == WOLFSSL_SERVER_END && check) ||
- (side == WOLFSSL_CLIENT_END && !check)) {
- XMEMCPY(&sigData[idx], clientCertVfyLabel, CERT_VFY_LABEL_SZ);
- }
- if ((side == WOLFSSL_CLIENT_END && check) ||
- (side == WOLFSSL_SERVER_END && !check)) {
- XMEMCPY(&sigData[idx], serverCertVfyLabel, CERT_VFY_LABEL_SZ);
- }
- idx += CERT_VFY_LABEL_SZ;
-
- ret = GetMsgHash(ssl, &sigData[idx]);
- if (ret < 0)
- return ret;
-
- *sigDataSz = (word16)(idx + ret);
- ret = 0;
-
- return ret;
-}
-
-#ifndef NO_RSA
-/* Encode the PKCS #1.5 RSA signature.
- *
- * sig The buffer to place the encoded signature into.
- * sigData The data to be signed.
- * sigDataSz The size of the data to be signed.
- * hashAlgo The hash algorithm to use when signing.
- * returns the length of the encoded signature or negative on error.
- */
-static int CreateRSAEncodedSig(byte* sig, byte* sigData, int sigDataSz,
- int sigAlgo, int hashAlgo)
-{
- Digest digest;
- int hashSz = 0;
- int ret = BAD_FUNC_ARG;
- byte* hash;
-
- (void)sigAlgo;
-
- hash = sig;
-
- /* Digest the signature data. */
- switch (hashAlgo) {
-#ifndef NO_WOLFSSL_SHA256
- case sha256_mac:
- ret = wc_InitSha256(&digest.sha256);
- if (ret == 0) {
- ret = wc_Sha256Update(&digest.sha256, sigData, sigDataSz);
- if (ret == 0)
- ret = wc_Sha256Final(&digest.sha256, hash);
- wc_Sha256Free(&digest.sha256);
- }
- hashSz = WC_SHA256_DIGEST_SIZE;
- break;
-#endif
-#ifdef WOLFSSL_SHA384
- case sha384_mac:
- ret = wc_InitSha384(&digest.sha384);
- if (ret == 0) {
- ret = wc_Sha384Update(&digest.sha384, sigData, sigDataSz);
- if (ret == 0)
- ret = wc_Sha384Final(&digest.sha384, hash);
- wc_Sha384Free(&digest.sha384);
- }
- hashSz = WC_SHA384_DIGEST_SIZE;
- break;
-#endif
-#ifdef WOLFSSL_SHA512
- case sha512_mac:
- ret = wc_InitSha512(&digest.sha512);
- if (ret == 0) {
- ret = wc_Sha512Update(&digest.sha512, sigData, sigDataSz);
- if (ret == 0)
- ret = wc_Sha512Final(&digest.sha512, hash);
- wc_Sha512Free(&digest.sha512);
- }
- hashSz = WC_SHA512_DIGEST_SIZE;
- break;
-#endif
- }
-
- if (ret != 0)
- return ret;
-
- return hashSz;
-}
-#endif /* !NO_RSA */
-
-#ifdef HAVE_ECC
-/* Encode the ECC signature.
- *
- * sigData The data to be signed.
- * sigDataSz The size of the data to be signed.
- * hashAlgo The hash algorithm to use when signing.
- * returns the length of the encoded signature or negative on error.
- */
-static int CreateECCEncodedSig(byte* sigData, int sigDataSz, int hashAlgo)
-{
- Digest digest;
- int hashSz = 0;
- int ret = BAD_FUNC_ARG;
-
- /* Digest the signature data. */
- switch (hashAlgo) {
-#ifndef NO_WOLFSSL_SHA256
- case sha256_mac:
- ret = wc_InitSha256(&digest.sha256);
- if (ret == 0) {
- ret = wc_Sha256Update(&digest.sha256, sigData, sigDataSz);
- if (ret == 0)
- ret = wc_Sha256Final(&digest.sha256, sigData);
- wc_Sha256Free(&digest.sha256);
- }
- hashSz = WC_SHA256_DIGEST_SIZE;
- break;
-#endif
-#ifdef WOLFSSL_SHA384
- case sha384_mac:
- ret = wc_InitSha384(&digest.sha384);
- if (ret == 0) {
- ret = wc_Sha384Update(&digest.sha384, sigData, sigDataSz);
- if (ret == 0)
- ret = wc_Sha384Final(&digest.sha384, sigData);
- wc_Sha384Free(&digest.sha384);
- }
- hashSz = WC_SHA384_DIGEST_SIZE;
- break;
-#endif
-#ifdef WOLFSSL_SHA512
- case sha512_mac:
- ret = wc_InitSha512(&digest.sha512);
- if (ret == 0) {
- ret = wc_Sha512Update(&digest.sha512, sigData, sigDataSz);
- if (ret == 0)
- ret = wc_Sha512Final(&digest.sha512, sigData);
- wc_Sha512Free(&digest.sha512);
- }
- hashSz = WC_SHA512_DIGEST_SIZE;
- break;
-#endif
- }
-
- if (ret != 0)
- return ret;
-
- return hashSz;
-}
-#endif /* HAVE_ECC */
-
-#ifndef NO_RSA
-/* Check that the decrypted signature matches the encoded signature
- * based on the digest of the signature data.
- *
- * ssl The SSL/TLS object.
- * sigAlgo The signature algorithm used to generate signature.
- * hashAlgo The hash algorithm used to generate signature.
- * decSig The decrypted signature.
- * decSigSz The size of the decrypted signature.
- * returns 0 on success, otherwise failure.
- */
-static int CheckRSASignature(WOLFSSL* ssl, int sigAlgo, int hashAlgo,
- byte* decSig, word32 decSigSz)
-{
- int ret = 0;
- byte sigData[MAX_SIG_DATA_SZ];
- word16 sigDataSz;
- word32 sigSz;
-
- ret = CreateSigData(ssl, sigData, &sigDataSz, 1);
- if (ret != 0)
- return ret;
-
- if (sigAlgo == rsa_pss_sa_algo) {
- enum wc_HashType hashType = WC_HASH_TYPE_NONE;
-
- ret = ConvertHashPss(hashAlgo, &hashType, NULL);
- if (ret < 0)
- return ret;
-
- /* PSS signature can be done in-place */
- ret = CreateRSAEncodedSig(sigData, sigData, sigDataSz,
- sigAlgo, hashAlgo);
- if (ret < 0)
- return ret;
- sigSz = ret;
-
- ret = wc_RsaPSS_CheckPadding(sigData, sigSz, decSig, decSigSz,
- hashType);
- }
-
- return ret;
-}
-#endif /* !NO_RSA */
-#endif /* !NO_RSA || HAVE_ECC */
-
-/* Get the next certificate from the list for writing into the TLS v1.3
- * Certificate message.
- *
- * data The certificate list.
- * length The length of the certificate data in the list.
- * idx The index of the next certificate.
- * returns the length of the certificate data. 0 indicates no more certificates
- * in the list.
- */
-static word32 NextCert(byte* data, word32 length, word32* idx)
-{
- word32 len;
-
- /* Is index at end of list. */
- if (*idx == length)
- return 0;
-
- /* Length of the current ASN.1 encoded certificate. */
- c24to32(data + *idx, &len);
- /* Include the length field. */
- len += 3;
-
- /* Move index to next certificate and return the current certificate's
- * length.
- */
- *idx += len;
- return len;
-}
-
-/* Add certificate data and empty extension to output up to the fragment size.
- *
- * ssl SSL/TLS object.
- * cert The certificate data to write out.
- * len The length of the certificate data.
- * extSz Length of the extension data with the certificate.
- * idx The start of the certificate data to write out.
- * fragSz The maximum size of this fragment.
- * output The buffer to write to.
- * returns the number of bytes written.
- */
-static word32 AddCertExt(WOLFSSL* ssl, byte* cert, word32 len, word16 extSz,
- word32 idx, word32 fragSz, byte* output)
-{
- word32 i = 0;
- word32 copySz = min(len - idx, fragSz);
-
- if (idx < len) {
- XMEMCPY(output, cert + idx, copySz);
- i = copySz;
- if (copySz == fragSz)
- return i;
- }
- copySz = len + extSz - idx - i;
-
- if (extSz == OPAQUE16_LEN) {
- if (copySz <= fragSz) {
- /* Empty extension */
- output[i++] = 0;
- output[i++] = 0;
- }
- }
- else {
- byte* certExts = ssl->buffers.certExts->buffer + idx + i - len;
- /* Put out as much of the extensions' data as will fit in fragment. */
- if (copySz > fragSz - i)
- copySz = fragSz - i;
- XMEMCPY(output + i, certExts, copySz);
- i += copySz;
- }
-
- return i;
-}
-
-/* handle generation TLS v1.3 certificate (11) */
-/* Send the certificate for this end and any CAs that help with validation.
- * This message is always encrypted in TLS v1.3.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-static int SendTls13Certificate(WOLFSSL* ssl)
-{
- int ret = 0;
- word32 certSz, certChainSz, headerSz, listSz, payloadSz;
- word16 extSz = 0;
- word32 length, maxFragment;
- word32 len = 0;
- word32 idx = 0;
- word32 offset = OPAQUE16_LEN;
- byte* p = NULL;
- byte certReqCtxLen = 0;
- byte* certReqCtx = NULL;
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_SEND);
- WOLFSSL_ENTER("SendTls13Certificate");
-
-#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) {
- certReqCtxLen = ssl->certReqCtx->len;
- certReqCtx = &ssl->certReqCtx->ctx;
- }
-#endif
-
- if (ssl->options.sendVerify == SEND_BLANK_CERT) {
- certSz = 0;
- certChainSz = 0;
- headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ;
- length = headerSz;
- listSz = 0;
- }
- else {
- if (!ssl->buffers.certificate) {
- WOLFSSL_MSG("Send Cert missing certificate buffer");
- return BUFFER_ERROR;
- }
- /* Certificate Data */
- certSz = ssl->buffers.certificate->length;
- /* Cert Req Ctx Len | Cert Req Ctx | Cert List Len | Cert Data Len */
- headerSz = OPAQUE8_LEN + certReqCtxLen + CERT_HEADER_SZ +
- CERT_HEADER_SZ;
-
- ret = TLSX_GetResponseSize(ssl, certificate, &extSz);
- if (ret < 0)
- return ret;
-
- /* Create extensions' data if none already present. */
- if (extSz > OPAQUE16_LEN && ssl->buffers.certExts == NULL) {
- ret = AllocDer(&ssl->buffers.certExts, extSz, CERT_TYPE, ssl->heap);
- if (ret < 0)
- return ret;
-
- extSz = 0;
- ret = TLSX_WriteResponse(ssl, ssl->buffers.certExts->buffer,
- certificate, &extSz);
- if (ret < 0)
- return ret;
- }
-
- /* Length of message data with one certificate and extensions. */
- length = headerSz + certSz + extSz;
- /* Length of list data with one certificate and extensions. */
- listSz = CERT_HEADER_SZ + certSz + extSz;
-
- /* Send rest of chain if sending cert (chain has leading size/s). */
- if (certSz > 0 && ssl->buffers.certChainCnt > 0) {
- p = ssl->buffers.certChain->buffer;
- /* Chain length including extensions. */
- certChainSz = ssl->buffers.certChain->length +
- OPAQUE16_LEN * ssl->buffers.certChainCnt;
- length += certChainSz;
- listSz += certChainSz;
- }
- else
- certChainSz = 0;
- }
-
- payloadSz = length;
-
- if (ssl->fragOffset != 0)
- length -= (ssl->fragOffset + headerSz);
-
- maxFragment = wolfSSL_GetMaxRecordSize(ssl, MAX_RECORD_SIZE);
-
- while (length > 0 && ret == 0) {
- byte* output = NULL;
- word32 fragSz = 0;
- word32 i = RECORD_HEADER_SZ;
- int sendSz = RECORD_HEADER_SZ;
-
- if (ssl->fragOffset == 0) {
- if (headerSz + certSz + extSz + certChainSz <=
- maxFragment - HANDSHAKE_HEADER_SZ) {
- fragSz = headerSz + certSz + extSz + certChainSz;
- }
- else
- fragSz = maxFragment - HANDSHAKE_HEADER_SZ;
-
- sendSz += fragSz + HANDSHAKE_HEADER_SZ;
- i += HANDSHAKE_HEADER_SZ;
- }
- else {
- fragSz = min(length, maxFragment);
- sendSz += fragSz;
- }
-
- sendSz += MAX_MSG_EXTRA;
-
- /* Check buffers are big enough and grow if needed. */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* Get position in output buffer to write new message to. */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- if (ssl->fragOffset == 0) {
- AddTls13FragHeaders(output, fragSz, 0, payloadSz, certificate, ssl);
-
- /* Request context. */
- output[i++] = certReqCtxLen;
- if (certReqCtxLen > 0) {
- XMEMCPY(output + i, certReqCtx, certReqCtxLen);
- i += certReqCtxLen;
- }
- length -= OPAQUE8_LEN + certReqCtxLen;
- fragSz -= OPAQUE8_LEN + certReqCtxLen;
- /* Certificate list length. */
- c32to24(listSz, output + i);
- i += CERT_HEADER_SZ;
- length -= CERT_HEADER_SZ;
- fragSz -= CERT_HEADER_SZ;
- /* Leaf certificate data length. */
- if (certSz > 0) {
- c32to24(certSz, output + i);
- i += CERT_HEADER_SZ;
- length -= CERT_HEADER_SZ;
- fragSz -= CERT_HEADER_SZ;
- }
- }
- else
- AddTls13RecordHeader(output, fragSz, handshake, ssl);
-
- if (certSz > 0 && ssl->fragOffset < certSz + extSz) {
- /* Put in the leaf certificate with extensions. */
- word32 copySz = AddCertExt(ssl, ssl->buffers.certificate->buffer,
- certSz, extSz, ssl->fragOffset, fragSz, output + i);
- i += copySz;
- ssl->fragOffset += copySz;
- length -= copySz;
- fragSz -= copySz;
- if (ssl->fragOffset == certSz + extSz)
- FreeDer(&ssl->buffers.certExts);
- }
- if (certChainSz > 0 && fragSz > 0) {
- /* Put in the CA certificates with empty extensions. */
- while (fragSz > 0) {
- word32 l;
-
- if (offset == len + OPAQUE16_LEN) {
- /* Find next CA certificate to write out. */
- offset = 0;
- /* Point to the start of current cert in chain buffer. */
- p = ssl->buffers.certChain->buffer + idx;
- len = NextCert(ssl->buffers.certChain->buffer,
- ssl->buffers.certChain->length, &idx);
- if (len == 0)
- break;
- }
-
- /* Write out certificate and empty extension. */
- l = AddCertExt(ssl, p, len, OPAQUE16_LEN, offset, fragSz,
- output + i);
- i += l;
- ssl->fragOffset += l;
- length -= l;
- fragSz -= l;
- offset += l;
- }
- }
-
- if ((int)i - RECORD_HEADER_SZ < 0) {
- WOLFSSL_MSG("Send Cert bad inputSz");
- return BUFFER_E;
- }
-
- /* This message is always encrypted. */
- sendSz = BuildTls13Message(ssl, output, sendSz,
- output + RECORD_HEADER_SZ,
- i - RECORD_HEADER_SZ, handshake, 1, 0, 0);
- if (sendSz < 0)
- return sendSz;
-
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "Certificate");
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "Certificate", handshake, output,
- sendSz, WRITE_PROTO, ssl->heap);
- }
- #endif
-
- ssl->buffers.outputBuffer.length += sendSz;
- if (!ssl->options.groupMessages)
- ret = SendBuffered(ssl);
- }
-
- if (ret != WANT_WRITE) {
- /* Clean up the fragment offset. */
- ssl->fragOffset = 0;
- if (ssl->options.side == WOLFSSL_SERVER_END)
- ssl->options.serverState = SERVER_CERT_COMPLETE;
- }
-
-#ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->certReqCtx != NULL) {
- CertReqCtx* ctx = ssl->certReqCtx;
- ssl->certReqCtx = ssl->certReqCtx->next;
- XFREE(ctx, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-#endif
-
- WOLFSSL_LEAVE("SendTls13Certificate", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_SEND);
-
- return ret;
-}
-
-typedef struct Scv13Args {
- byte* output; /* not allocated */
- byte* verify; /* not allocated */
- word32 idx;
- word32 sigLen;
- int sendSz;
- word16 length;
-
- byte sigAlgo;
- byte* sigData;
- word16 sigDataSz;
-} Scv13Args;
-
-static void FreeScv13Args(WOLFSSL* ssl, void* pArgs)
-{
- Scv13Args* args = (Scv13Args*)pArgs;
-
- (void)ssl;
-
- if (args->sigData) {
- XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- args->sigData = NULL;
- }
-}
-
-/* handle generation TLS v1.3 certificate_verify (15) */
-/* Send the TLS v1.3 CertificateVerify message.
- * A hash of all the message so far is used.
- * The signed data is:
- * 0x20 * 64 | context string | 0x00 | hash of messages
- * This message is always encrypted in TLS v1.3.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-static int SendTls13CertificateVerify(WOLFSSL* ssl)
-{
- int ret = 0;
- buffer* sig = &ssl->buffers.sig;
-#ifdef WOLFSSL_ASYNC_CRYPT
- Scv13Args* args = (Scv13Args*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
-#else
- Scv13Args args[1];
-#endif
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_SEND);
- WOLFSSL_ENTER("SendTls13CertificateVerify");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_scv;
- }
- else
-#endif
- {
- /* Reset state */
- ret = 0;
- ssl->options.asyncState = TLS_ASYNC_BEGIN;
- XMEMSET(args, 0, sizeof(Scv13Args));
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeScv13Args;
- #endif
- }
-
- switch(ssl->options.asyncState)
- {
- case TLS_ASYNC_BEGIN:
- {
- if (ssl->options.sendVerify == SEND_BLANK_CERT) {
- return 0; /* sent blank cert, can't verify */
- }
-
- args->sendSz = MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA;
- /* Always encrypted. */
- args->sendSz += MAX_MSG_EXTRA;
-
- /* check for available size */
- if ((ret = CheckAvailableSize(ssl, args->sendSz)) != 0) {
- goto exit_scv;
- }
-
- /* get output buffer */
- args->output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_BUILD;
- } /* case TLS_ASYNC_BEGIN */
- FALL_THROUGH;
-
- case TLS_ASYNC_BUILD:
- {
- /* idx is used to track verify pointer offset to output */
- args->idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- args->verify =
- &args->output[RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ];
-
- if (ssl->buffers.key == NULL) {
- #ifdef HAVE_PK_CALLBACKS
- if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx))
- args->length = GetPrivateKeySigSize(ssl);
- else
- #endif
- ERROR_OUT(NO_PRIVATE_KEY, exit_scv);
- }
- else {
- ret = DecodePrivateKey(ssl, &args->length);
- if (ret != 0)
- goto exit_scv;
- }
-
- if (args->length <= 0) {
- ERROR_OUT(NO_PRIVATE_KEY, exit_scv);
- }
-
- /* Add signature algorithm. */
- if (ssl->hsType == DYNAMIC_TYPE_RSA)
- args->sigAlgo = rsa_pss_sa_algo;
- else if (ssl->hsType == DYNAMIC_TYPE_ECC)
- args->sigAlgo = ecc_dsa_sa_algo;
- #ifdef HAVE_ED25519
- else if (ssl->hsType == DYNAMIC_TYPE_ED25519)
- args->sigAlgo = ed25519_sa_algo;
- #endif
- #ifdef HAVE_ED448
- else if (ssl->hsType == DYNAMIC_TYPE_ED448)
- args->sigAlgo = ed448_sa_algo;
- #endif
- EncodeSigAlg(ssl->suites->hashAlgo, args->sigAlgo, args->verify);
-
- if (ssl->hsType == DYNAMIC_TYPE_RSA) {
- int sigLen = MAX_SIG_DATA_SZ;
- if (args->length > MAX_SIG_DATA_SZ)
- sigLen = args->length;
- args->sigData = (byte*)XMALLOC(sigLen, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- }
- else {
- args->sigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- }
- if (args->sigData == NULL) {
- ERROR_OUT(MEMORY_E, exit_scv);
- }
-
- /* Create the data to be signed. */
- ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 0);
- if (ret != 0)
- goto exit_scv;
-
- #ifndef NO_RSA
- if (ssl->hsType == DYNAMIC_TYPE_RSA) {
- /* build encoded signature buffer */
- sig->length = WC_MAX_DIGEST_SIZE;
- sig->buffer = (byte*)XMALLOC(sig->length, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (sig->buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_scv);
- }
-
- ret = CreateRSAEncodedSig(sig->buffer, args->sigData,
- args->sigDataSz, args->sigAlgo, ssl->suites->hashAlgo);
- if (ret < 0)
- goto exit_scv;
- sig->length = ret;
- ret = 0;
-
- /* Maximum size of RSA Signature. */
- args->sigLen = args->length;
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- if (ssl->hsType == DYNAMIC_TYPE_ECC) {
- sig->length = args->sendSz - args->idx - HASH_SIG_SIZE -
- VERIFY_HEADER;
- ret = CreateECCEncodedSig(args->sigData,
- args->sigDataSz, ssl->suites->hashAlgo);
- if (ret < 0)
- goto exit_scv;
- args->sigDataSz = (word16)ret;
- ret = 0;
- }
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- if (ssl->hsType == DYNAMIC_TYPE_ED25519) {
- ret = Ed25519CheckPubKey(ssl);
- if (ret < 0) {
- ERROR_OUT(ret, exit_scv);
- }
- sig->length = ED25519_SIG_SIZE;
- }
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- if (ssl->hsType == DYNAMIC_TYPE_ED448) {
- ret = Ed448CheckPubKey(ssl);
- if (ret < 0) {
- ERROR_OUT(ret, exit_scv);
- }
- sig->length = ED448_SIG_SIZE;
- }
- #endif /* HAVE_ED448 */
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_DO;
- } /* case TLS_ASYNC_BUILD */
- FALL_THROUGH;
-
- case TLS_ASYNC_DO:
- {
- #ifdef HAVE_ECC
- if (ssl->hsType == DYNAMIC_TYPE_ECC) {
-
- ret = EccSign(ssl, args->sigData, args->sigDataSz,
- args->verify + HASH_SIG_SIZE + VERIFY_HEADER,
- (word32*)&sig->length, (ecc_key*)ssl->hsKey,
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.key
- #else
- NULL
- #endif
- );
- args->length = (word16)sig->length;
- }
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- if (ssl->hsType == DYNAMIC_TYPE_ED25519) {
- ret = Ed25519Sign(ssl, args->sigData, args->sigDataSz,
- args->verify + HASH_SIG_SIZE + VERIFY_HEADER,
- (word32*)&sig->length, (ed25519_key*)ssl->hsKey,
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.key
- #else
- NULL
- #endif
- );
- args->length = (word16)sig->length;
- }
- #endif
- #ifdef HAVE_ED448
- if (ssl->hsType == DYNAMIC_TYPE_ED448) {
- ret = Ed448Sign(ssl, args->sigData, args->sigDataSz,
- args->verify + HASH_SIG_SIZE + VERIFY_HEADER,
- (word32*)&sig->length, (ed448_key*)ssl->hsKey,
- #ifdef HAVE_PK_CALLBACKS
- ssl->buffers.key
- #else
- NULL
- #endif
- );
- args->length = (word16)sig->length;
- }
- #endif
- #ifndef NO_RSA
- if (ssl->hsType == DYNAMIC_TYPE_RSA) {
- ret = RsaSign(ssl, sig->buffer, (word32)sig->length,
- args->verify + HASH_SIG_SIZE + VERIFY_HEADER, &args->sigLen,
- args->sigAlgo, ssl->suites->hashAlgo,
- (RsaKey*)ssl->hsKey,
- ssl->buffers.key
- );
- if (ret == 0) {
- args->length = (word16)args->sigLen;
-
- XMEMCPY(args->sigData,
- args->verify + HASH_SIG_SIZE + VERIFY_HEADER,
- args->sigLen);
- }
- }
- #endif /* !NO_RSA */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_scv;
- }
-
- /* Add signature length. */
- c16toa(args->length, args->verify + HASH_SIG_SIZE);
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_VERIFY;
- } /* case TLS_ASYNC_DO */
- FALL_THROUGH;
-
- case TLS_ASYNC_VERIFY:
- {
- #ifndef NO_RSA
- if (ssl->hsType == DYNAMIC_TYPE_RSA) {
- /* check for signature faults */
- ret = VerifyRsaSign(ssl, args->sigData, args->sigLen,
- sig->buffer, (word32)sig->length, args->sigAlgo,
- ssl->suites->hashAlgo, (RsaKey*)ssl->hsKey,
- ssl->buffers.key
- );
- }
- #endif /* !NO_RSA */
-
- /* Check for error */
- if (ret != 0) {
- goto exit_scv;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_FINALIZE;
- } /* case TLS_ASYNC_VERIFY */
- FALL_THROUGH;
-
- case TLS_ASYNC_FINALIZE:
- {
- /* Put the record and handshake headers on. */
- AddTls13Headers(args->output, args->length + HASH_SIG_SIZE +
- VERIFY_HEADER, certificate_verify, ssl);
-
- args->sendSz = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ +
- args->length + HASH_SIG_SIZE + VERIFY_HEADER;
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_END;
- } /* case TLS_ASYNC_FINALIZE */
- FALL_THROUGH;
-
- case TLS_ASYNC_END:
- {
- /* This message is always encrypted. */
- ret = BuildTls13Message(ssl, args->output,
- MAX_CERT_VERIFY_SZ + MAX_MSG_EXTRA,
- args->output + RECORD_HEADER_SZ,
- args->sendSz - RECORD_HEADER_SZ, handshake,
- 1, 0, 0);
-
- if (ret < 0) {
- goto exit_scv;
- }
- else {
- args->sendSz = ret;
- ret = 0;
- }
-
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn)
- AddPacketName(ssl, "CertificateVerify");
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "CertificateVerify", handshake,
- args->output, args->sendSz, WRITE_PROTO, ssl->heap);
- }
- #endif
-
- ssl->buffers.outputBuffer.length += args->sendSz;
-
- if (!ssl->options.groupMessages)
- ret = SendBuffered(ssl);
- break;
- }
- default:
- ret = INPUT_CASE_ERROR;
- } /* switch(ssl->options.asyncState) */
-
-exit_scv:
-
- WOLFSSL_LEAVE("SendTls13CertificateVerify", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_SEND);
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* Handle async operation */
- if (ret == WC_PENDING_E) {
- return ret;
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- /* Final cleanup */
- FreeScv13Args(ssl, args);
- FreeKeyExchange(ssl);
-
- return ret;
-}
-
-/* handle processing TLS v1.3 certificate (11) */
-/* Parse and handle a TLS v1.3 Certificate message.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the message buffer of Certificate.
- * On exit, the index of byte after the Certificate message.
- * totalSz The length of the current handshake message.
- * returns 0 on success and otherwise failure.
- */
-static int DoTls13Certificate(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- word32 totalSz)
-{
- int ret;
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_DO);
- WOLFSSL_ENTER("DoTls13Certificate");
-
- ret = ProcessPeerCerts(ssl, input, inOutIdx, totalSz);
- if (ret == 0) {
-#if !defined(NO_WOLFSSL_CLIENT)
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- ssl->options.serverState = SERVER_CERT_COMPLETE;
-#endif
-#if !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- ssl->options.handShakeState == HANDSHAKE_DONE) {
- /* reset handshake states */
- ssl->options.serverState = SERVER_FINISHED_COMPLETE;
- ssl->options.acceptState = TICKET_SENT;
- ssl->options.handShakeState = SERVER_FINISHED_COMPLETE;
- }
-#endif
- }
-
- WOLFSSL_LEAVE("DoTls13Certificate", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_DO);
-
- return ret;
-}
-
-#if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)
-
-typedef struct Dcv13Args {
- byte* output; /* not allocated */
- word32 sendSz;
- word16 sz;
- word32 sigSz;
- word32 idx;
- word32 begin;
- byte hashAlgo;
- byte sigAlgo;
-
- byte* sigData;
- word16 sigDataSz;
-} Dcv13Args;
-
-static void FreeDcv13Args(WOLFSSL* ssl, void* pArgs)
-{
- Dcv13Args* args = (Dcv13Args*)pArgs;
-
- if (args->sigData != NULL) {
- XFREE(args->sigData, ssl->heap, DYNAMIC_TYPE_SIGNATURE);
- args->sigData = NULL;
- }
-
- (void)ssl;
-}
-
-/* handle processing TLS v1.3 certificate_verify (15) */
-/* Parse and handle a TLS v1.3 CertificateVerify message.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the message buffer of
- * CertificateVerify.
- * On exit, the index of byte after the CertificateVerify message.
- * totalSz The length of the current handshake message.
- * returns 0 on success and otherwise failure.
- */
-static int DoTls13CertificateVerify(WOLFSSL* ssl, byte* input,
- word32* inOutIdx, word32 totalSz)
-{
- int ret = 0;
- buffer* sig = &ssl->buffers.sig;
-#ifdef WOLFSSL_ASYNC_CRYPT
- Dcv13Args* args = (Dcv13Args*)ssl->async.args;
- typedef char args_test[sizeof(ssl->async.args) >= sizeof(*args) ? 1 : -1];
- (void)sizeof(args_test);
-#else
- Dcv13Args args[1];
-#endif
-
- WOLFSSL_START(WC_FUNC_CERTIFICATE_VERIFY_DO);
- WOLFSSL_ENTER("DoTls13CertificateVerify");
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- ret = wolfSSL_AsyncPop(ssl, &ssl->options.asyncState);
- if (ret != WC_NOT_PENDING_E) {
- /* Check for error */
- if (ret < 0)
- goto exit_dcv;
- }
- else
-#endif
- {
- /* Reset state */
- ret = 0;
- ssl->options.asyncState = TLS_ASYNC_BEGIN;
- XMEMSET(args, 0, sizeof(Dcv13Args));
- args->hashAlgo = sha_mac;
- args->sigAlgo = anonymous_sa_algo;
- args->idx = *inOutIdx;
- args->begin = *inOutIdx;
- #ifdef WOLFSSL_ASYNC_CRYPT
- ssl->async.freeArgs = FreeDcv13Args;
- #endif
- }
-
- switch(ssl->options.asyncState)
- {
- case TLS_ASYNC_BEGIN:
- {
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "CertificateVerify");
- if (ssl->toInfoOn) AddLateName("CertificateVerify",
- &ssl->timeoutInfo);
- #endif
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_BUILD;
- } /* case TLS_ASYNC_BEGIN */
- FALL_THROUGH;
-
- case TLS_ASYNC_BUILD:
- {
- /* Signature algorithm. */
- if ((args->idx - args->begin) + ENUM_LEN + ENUM_LEN > totalSz) {
- ERROR_OUT(BUFFER_ERROR, exit_dcv);
- }
- ret = DecodeTls13SigAlg(input + args->idx, &args->hashAlgo,
- &args->sigAlgo);
- if (ret < 0)
- goto exit_dcv;
- args->idx += OPAQUE16_LEN;
-
- /* Signature length. */
- if ((args->idx - args->begin) + OPAQUE16_LEN > totalSz) {
- ERROR_OUT(BUFFER_ERROR, exit_dcv);
- }
- ato16(input + args->idx, &args->sz);
- args->idx += OPAQUE16_LEN;
-
- /* Signature data. */
- if ((args->idx - args->begin) + args->sz > totalSz ||
- args->sz > ENCRYPT_LEN) {
- ERROR_OUT(BUFFER_ERROR, exit_dcv);
- }
-
- /* Check for public key of required type. */
- #ifdef HAVE_ED25519
- if (args->sigAlgo == ed25519_sa_algo &&
- !ssl->peerEd25519KeyPresent) {
- WOLFSSL_MSG("Oops, peer sent ED25519 key but not in verify");
- }
- #endif
- #ifdef HAVE_ED448
- if (args->sigAlgo == ed448_sa_algo && !ssl->peerEd448KeyPresent) {
- WOLFSSL_MSG("Oops, peer sent ED448 key but not in verify");
- }
- #endif
- #ifdef HAVE_ECC
- if (args->sigAlgo == ecc_dsa_sa_algo &&
- !ssl->peerEccDsaKeyPresent) {
- WOLFSSL_MSG("Oops, peer sent ECC key but not in verify");
- }
- #endif
- #ifndef NO_RSA
- if (args->sigAlgo == rsa_sa_algo) {
- WOLFSSL_MSG("Oops, peer sent PKCS#1.5 signature");
- ERROR_OUT(INVALID_PARAMETER, exit_dcv);
- }
- if (args->sigAlgo == rsa_pss_sa_algo &&
- (ssl->peerRsaKey == NULL || !ssl->peerRsaKeyPresent)) {
- WOLFSSL_MSG("Oops, peer sent RSA key but not in verify");
- }
- #endif
-
- sig->buffer = (byte*)XMALLOC(args->sz, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (sig->buffer == NULL) {
- ERROR_OUT(MEMORY_E, exit_dcv);
- }
- sig->length = args->sz;
- XMEMCPY(sig->buffer, input + args->idx, args->sz);
-
- #ifdef HAVE_ECC
- if (ssl->peerEccDsaKeyPresent) {
- WOLFSSL_MSG("Doing ECC peer cert verify");
-
- args->sigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (args->sigData == NULL) {
- ERROR_OUT(MEMORY_E, exit_dcv);
- }
-
- ret = CreateSigData(ssl, args->sigData, &args->sigDataSz, 1);
- if (ret != 0)
- goto exit_dcv;
- ret = CreateECCEncodedSig(args->sigData,
- args->sigDataSz, args->hashAlgo);
- if (ret < 0)
- goto exit_dcv;
- args->sigDataSz = (word16)ret;
- ret = 0;
- }
- #endif
- #ifdef HAVE_ED25519
- if (ssl->peerEd25519KeyPresent) {
- WOLFSSL_MSG("Doing ED25519 peer cert verify");
-
- args->sigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (args->sigData == NULL) {
- ERROR_OUT(MEMORY_E, exit_dcv);
- }
-
- CreateSigData(ssl, args->sigData, &args->sigDataSz, 1);
- ret = 0;
- }
- #endif
- #ifdef HAVE_ED448
- if (ssl->peerEd448KeyPresent) {
- WOLFSSL_MSG("Doing ED448 peer cert verify");
-
- args->sigData = (byte*)XMALLOC(MAX_SIG_DATA_SZ, ssl->heap,
- DYNAMIC_TYPE_SIGNATURE);
- if (args->sigData == NULL) {
- ERROR_OUT(MEMORY_E, exit_dcv);
- }
-
- CreateSigData(ssl, args->sigData, &args->sigDataSz, 1);
- ret = 0;
- }
- #endif
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_DO;
- } /* case TLS_ASYNC_BUILD */
- FALL_THROUGH;
-
- case TLS_ASYNC_DO:
- {
- #ifndef NO_RSA
- if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) {
- WOLFSSL_MSG("Doing RSA peer cert verify");
-
- ret = RsaVerify(ssl, sig->buffer, (word32)sig->length, &args->output,
- args->sigAlgo, args->hashAlgo, ssl->peerRsaKey,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerRsaKey
- #else
- NULL
- #endif
- );
- if (ret >= 0) {
- args->sendSz = ret;
- ret = 0;
- }
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- if (ssl->peerEccDsaKeyPresent) {
- WOLFSSL_MSG("Doing ECC peer cert verify");
-
- ret = EccVerify(ssl, input + args->idx, args->sz,
- args->sigData, args->sigDataSz,
- ssl->peerEccDsaKey,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerEccDsaKey
- #else
- NULL
- #endif
- );
-
- if (ret >= 0) {
- FreeKey(ssl, DYNAMIC_TYPE_ECC, (void**)&ssl->peerEccDsaKey);
- ssl->peerEccDsaKeyPresent = 0;
- }
- }
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- if (ssl->peerEd25519KeyPresent) {
- WOLFSSL_MSG("Doing ED25519 peer cert verify");
-
- ret = Ed25519Verify(ssl, input + args->idx, args->sz,
- args->sigData, args->sigDataSz,
- ssl->peerEd25519Key,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerEd25519Key
- #else
- NULL
- #endif
- );
-
- if (ret >= 0) {
- FreeKey(ssl, DYNAMIC_TYPE_ED25519,
- (void**)&ssl->peerEd25519Key);
- ssl->peerEd25519KeyPresent = 0;
- }
- }
- #endif
- #ifdef HAVE_ED448
- if (ssl->peerEd448KeyPresent) {
- WOLFSSL_MSG("Doing ED448 peer cert verify");
-
- ret = Ed448Verify(ssl, input + args->idx, args->sz,
- args->sigData, args->sigDataSz,
- ssl->peerEd448Key,
- #ifdef HAVE_PK_CALLBACKS
- &ssl->buffers.peerEd448Key
- #else
- NULL
- #endif
- );
-
- if (ret >= 0) {
- FreeKey(ssl, DYNAMIC_TYPE_ED448,
- (void**)&ssl->peerEd448Key);
- ssl->peerEd448KeyPresent = 0;
- }
- }
- #endif
-
- /* Check for error */
- if (ret != 0) {
- goto exit_dcv;
- }
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_VERIFY;
- } /* case TLS_ASYNC_DO */
- FALL_THROUGH;
-
- case TLS_ASYNC_VERIFY:
- {
- #ifndef NO_RSA
- if (ssl->peerRsaKey != NULL && ssl->peerRsaKeyPresent != 0) {
- ret = CheckRSASignature(ssl, args->sigAlgo, args->hashAlgo,
- args->output, args->sendSz);
- if (ret != 0)
- goto exit_dcv;
-
- FreeKey(ssl, DYNAMIC_TYPE_RSA, (void**)&ssl->peerRsaKey);
- ssl->peerRsaKeyPresent = 0;
- }
- #endif /* !NO_RSA */
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_FINALIZE;
- } /* case TLS_ASYNC_VERIFY */
- FALL_THROUGH;
-
- case TLS_ASYNC_FINALIZE:
- {
- ssl->options.havePeerVerify = 1;
-
- /* Set final index */
- args->idx += args->sz;
- *inOutIdx = args->idx;
-
- /* Encryption is always on: add padding */
- *inOutIdx += ssl->keys.padSz;
-
- /* Advance state and proceed */
- ssl->options.asyncState = TLS_ASYNC_END;
- } /* case TLS_ASYNC_FINALIZE */
-
- case TLS_ASYNC_END:
- {
- break;
- }
- default:
- ret = INPUT_CASE_ERROR;
- } /* switch(ssl->options.asyncState) */
-
-exit_dcv:
-
- WOLFSSL_LEAVE("DoTls13CertificateVerify", ret);
- WOLFSSL_END(WC_FUNC_CERTIFICATE_VERIFY_DO);
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* Handle async operation */
- if (ret == WC_PENDING_E) {
- /* Mark message as not received so it can process again */
- ssl->msgsReceived.got_certificate_verify = 0;
-
- return ret;
- }
- else
-#endif /* WOLFSSL_ASYNC_CRYPT */
- if (ret != 0 && ret != INVALID_PARAMETER)
- SendAlert(ssl, alert_fatal, decrypt_error);
-
- /* Final cleanup */
- FreeDcv13Args(ssl, args);
- FreeKeyExchange(ssl);
-
- return ret;
-}
-#endif /* !NO_RSA || HAVE_ECC */
-
-/* Parse and handle a TLS v1.3 Finished message.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the message buffer of Finished.
- * On exit, the index of byte after the Finished message and padding.
- * size Length of message data.
- * totalSz Length of remaining data in the message buffer.
- * sniff Indicates whether we are sniffing packets.
- * returns 0 on success and otherwise failure.
- */
-static int DoTls13Finished(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- word32 size, word32 totalSz, int sniff)
-{
- int ret;
- word32 finishedSz = 0;
- byte* secret;
- byte mac[WC_MAX_DIGEST_SIZE];
-
- WOLFSSL_START(WC_FUNC_FINISHED_DO);
- WOLFSSL_ENTER("DoTls13Finished");
-
- /* check against totalSz */
- if (*inOutIdx + size + ssl->keys.padSz > totalSz)
- return BUFFER_E;
-
- if (ssl->options.handShakeDone) {
- ret = DeriveFinishedSecret(ssl, ssl->clientSecret,
- ssl->keys.client_write_MAC_secret);
- if (ret != 0)
- return ret;
-
- secret = ssl->keys.client_write_MAC_secret;
- }
- else if (ssl->options.side == WOLFSSL_CLIENT_END) {
- /* All the handshake messages have been received to calculate
- * client and server finished keys.
- */
- ret = DeriveFinishedSecret(ssl, ssl->clientSecret,
- ssl->keys.client_write_MAC_secret);
- if (ret != 0)
- return ret;
-
- ret = DeriveFinishedSecret(ssl, ssl->serverSecret,
- ssl->keys.server_write_MAC_secret);
- if (ret != 0)
- return ret;
-
- secret = ssl->keys.server_write_MAC_secret;
- }
- else
- secret = ssl->keys.client_write_MAC_secret;
-
- ret = BuildTls13HandshakeHmac(ssl, secret, mac, &finishedSz);
- if (ret != 0)
- return ret;
- if (size != finishedSz)
- return BUFFER_ERROR;
-
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "Finished");
- if (ssl->toInfoOn) AddLateName("Finished", &ssl->timeoutInfo);
- #endif
-
- if (sniff == NO_SNIFF) {
- /* Actually check verify data. */
- if (XMEMCMP(input + *inOutIdx, mac, size) != 0){
- WOLFSSL_MSG("Verify finished error on hashes");
- SendAlert(ssl, alert_fatal, decrypt_error);
- return VERIFY_FINISHED_ERROR;
- }
- }
-
- /* Force input exhaustion at ProcessReply by consuming padSz. */
- *inOutIdx += size + ssl->keys.padSz;
-
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- !ssl->options.handShakeDone) {
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data) {
- if ((ret = DeriveTls13Keys(ssl, no_key, DECRYPT_SIDE_ONLY, 1)) != 0)
- return ret;
- }
-#endif
- /* Setup keys for application data messages from client. */
- if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0)
- return ret;
- }
-
-#ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- ssl->options.serverState = SERVER_FINISHED_COMPLETE;
-#endif
-#ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- ssl->options.clientState = CLIENT_FINISHED_COMPLETE;
- ssl->options.handShakeState = HANDSHAKE_DONE;
- ssl->options.handShakeDone = 1;
- }
-#endif
-
- WOLFSSL_LEAVE("DoTls13Finished", 0);
- WOLFSSL_END(WC_FUNC_FINISHED_DO);
-
- return 0;
-}
-#endif /* NO_CERTS */
-
-/* Send the TLS v1.3 Finished message.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-static int SendTls13Finished(WOLFSSL* ssl)
-{
- int sendSz;
- int finishedSz = ssl->specs.hash_size;
- byte* input;
- byte* output;
- int ret;
- int headerSz = HANDSHAKE_HEADER_SZ;
- int outputSz;
- byte* secret;
-
- WOLFSSL_START(WC_FUNC_FINISHED_SEND);
- WOLFSSL_ENTER("SendTls13Finished");
-
- outputSz = WC_MAX_DIGEST_SIZE + DTLS_HANDSHAKE_HEADER_SZ + MAX_MSG_EXTRA;
- /* Check buffers are big enough and grow if needed. */
- if ((ret = CheckAvailableSize(ssl, outputSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
- input = output + RECORD_HEADER_SZ;
-
- AddTls13HandShakeHeader(input, finishedSz, 0, finishedSz, finished, ssl);
-
- /* make finished hashes */
- if (ssl->options.handShakeDone) {
- ret = DeriveFinishedSecret(ssl, ssl->clientSecret,
- ssl->keys.client_write_MAC_secret);
- if (ret != 0)
- return ret;
-
- secret = ssl->keys.client_write_MAC_secret;
- }
- else if (ssl->options.side == WOLFSSL_CLIENT_END)
- secret = ssl->keys.client_write_MAC_secret;
- else {
- /* All the handshake messages have been done to calculate client and
- * server finished keys.
- */
- ret = DeriveFinishedSecret(ssl, ssl->clientSecret,
- ssl->keys.client_write_MAC_secret);
- if (ret != 0)
- return ret;
-
- ret = DeriveFinishedSecret(ssl, ssl->serverSecret,
- ssl->keys.server_write_MAC_secret);
- if (ret != 0)
- return ret;
-
- secret = ssl->keys.server_write_MAC_secret;
- }
- ret = BuildTls13HandshakeHmac(ssl, secret, &input[headerSz], NULL);
- if (ret != 0)
- return ret;
-
- /* This message is always encrypted. */
- sendSz = BuildTls13Message(ssl, output, outputSz, input,
- headerSz + finishedSz, handshake, 1, 0, 0);
- if (sendSz < 0)
- return BUILD_MSG_ERROR;
-
-#ifndef NO_SESSION_CACHE
- if (!ssl->options.resuming && (ssl->options.side == WOLFSSL_SERVER_END ||
- (ssl->options.side == WOLFSSL_SERVER_END && ssl->arrays != NULL))) {
- AddSession(ssl); /* just try */
- }
-#endif
-
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "Finished");
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "Finished", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- }
- #endif
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- /* Can send application data now. */
- if ((ret = DeriveMasterSecret(ssl)) != 0)
- return ret;
-#ifdef WOLFSSL_EARLY_DATA
- if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_SIDE_ONLY, 1))
- != 0) {
- return ret;
- }
- if ((ret = DeriveTls13Keys(ssl, traffic_key, DECRYPT_SIDE_ONLY,
- ssl->earlyData == no_early_data)) != 0) {
- return ret;
- }
-#else
- if ((ret = DeriveTls13Keys(ssl, traffic_key, ENCRYPT_AND_DECRYPT_SIDE,
- 1)) != 0) {
- return ret;
- }
-#endif
- if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0)
- return ret;
- }
-
- if (ssl->options.side == WOLFSSL_CLIENT_END &&
- !ssl->options.handShakeDone) {
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data) {
- if ((ret = DeriveTls13Keys(ssl, no_key, ENCRYPT_AND_DECRYPT_SIDE,
- 1)) != 0) {
- return ret;
- }
- }
-#endif
- /* Setup keys for application data messages. */
- if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0)
- return ret;
-
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- ret = DeriveResumptionSecret(ssl, ssl->session.masterSecret);
- if (ret != 0)
- return ret;
-#endif
- }
-
-#ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- ssl->options.clientState = CLIENT_FINISHED_COMPLETE;
- ssl->options.handShakeState = HANDSHAKE_DONE;
- ssl->options.handShakeDone = 1;
- }
-#endif
-#ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- ssl->options.serverState = SERVER_FINISHED_COMPLETE;
- }
-#endif
-
- if ((ret = SendBuffered(ssl)) != 0)
- return ret;
-
- WOLFSSL_LEAVE("SendTls13Finished", ret);
- WOLFSSL_END(WC_FUNC_FINISHED_SEND);
-
- return ret;
-}
-
-/* handle generation TLS v1.3 key_update (24) */
-/* Send the TLS v1.3 KeyUpdate message.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-static int SendTls13KeyUpdate(WOLFSSL* ssl)
-{
- int sendSz;
- byte* input;
- byte* output;
- int ret;
- int headerSz = HANDSHAKE_HEADER_SZ;
- int outputSz;
- word32 i = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
-
- WOLFSSL_START(WC_FUNC_KEY_UPDATE_SEND);
- WOLFSSL_ENTER("SendTls13KeyUpdate");
-
- outputSz = OPAQUE8_LEN + MAX_MSG_EXTRA;
- /* Check buffers are big enough and grow if needed. */
- if ((ret = CheckAvailableSize(ssl, outputSz)) != 0)
- return ret;
-
- /* get output buffer */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
- input = output + RECORD_HEADER_SZ;
-
- AddTls13Headers(output, OPAQUE8_LEN, key_update, ssl);
-
- /* If:
- * 1. I haven't sent a KeyUpdate requesting a response and
- * 2. This isn't responding to peer KeyUpdate requiring a response then,
- * I want a response.
- */
- ssl->keys.updateResponseReq = output[i++] =
- !ssl->keys.updateResponseReq && !ssl->keys.keyUpdateRespond;
- /* Sent response, no longer need to respond. */
- ssl->keys.keyUpdateRespond = 0;
-
- /* This message is always encrypted. */
- sendSz = BuildTls13Message(ssl, output, outputSz, input,
- headerSz + OPAQUE8_LEN, handshake, 0, 0, 0);
- if (sendSz < 0)
- return BUILD_MSG_ERROR;
-
- #ifdef WOLFSSL_CALLBACKS
- if (ssl->hsInfoOn) AddPacketName(ssl, "KeyUpdate");
- if (ssl->toInfoOn) {
- AddPacketInfo(ssl, "KeyUpdate", handshake, output, sendSz,
- WRITE_PROTO, ssl->heap);
- }
- #endif
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- ret = SendBuffered(ssl);
- if (ret != 0 && ret != WANT_WRITE)
- return ret;
-
- /* Future traffic uses new encryption keys. */
- if ((ret = DeriveTls13Keys(ssl, update_traffic_key, ENCRYPT_SIDE_ONLY, 1))
- != 0)
- return ret;
- if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0)
- return ret;
-
- WOLFSSL_LEAVE("SendTls13KeyUpdate", ret);
- WOLFSSL_END(WC_FUNC_KEY_UPDATE_SEND);
-
- return ret;
-}
-
-/* handle processing TLS v1.3 key_update (24) */
-/* Parse and handle a TLS v1.3 KeyUpdate message.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the message buffer of Finished.
- * On exit, the index of byte after the Finished message and padding.
- * totalSz The length of the current handshake message.
- * returns 0 on success and otherwise failure.
- */
-static int DoTls13KeyUpdate(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
- word32 totalSz)
-{
- int ret;
- word32 i = *inOutIdx;
-
- WOLFSSL_START(WC_FUNC_KEY_UPDATE_DO);
- WOLFSSL_ENTER("DoTls13KeyUpdate");
-
- /* check against totalSz */
- if (OPAQUE8_LEN != totalSz)
- return BUFFER_E;
-
- switch (input[i]) {
- case update_not_requested:
- /* This message in response to any outstanding request. */
- ssl->keys.keyUpdateRespond = 0;
- ssl->keys.updateResponseReq = 0;
- break;
- case update_requested:
- /* New key update requiring a response. */
- ssl->keys.keyUpdateRespond = 1;
- break;
- default:
- return INVALID_PARAMETER;
- }
-
- /* Move index to byte after message. */
- *inOutIdx += totalSz;
- /* Always encrypted. */
- *inOutIdx += ssl->keys.padSz;
-
- /* Future traffic uses new decryption keys. */
- if ((ret = DeriveTls13Keys(ssl, update_traffic_key, DECRYPT_SIDE_ONLY, 1))
- != 0) {
- return ret;
- }
- if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0)
- return ret;
-
- if (ssl->keys.keyUpdateRespond)
- return SendTls13KeyUpdate(ssl);
-
- WOLFSSL_LEAVE("DoTls13KeyUpdate", ret);
- WOLFSSL_END(WC_FUNC_KEY_UPDATE_DO);
-
- return 0;
-}
-
-#ifdef WOLFSSL_EARLY_DATA
-#ifndef NO_WOLFSSL_CLIENT
-/* Send the TLS v1.3 EndOfEarlyData message to indicate that there will be no
- * more early application data.
- * The encryption key now changes to the pre-calculated handshake key.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success and otherwise failure.
- */
-static int SendTls13EndOfEarlyData(WOLFSSL* ssl)
-{
- byte* output;
- int ret;
- int sendSz;
- word32 length;
- word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
-
- WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_SEND);
- WOLFSSL_ENTER("SendTls13EndOfEarlyData");
-
- length = 0;
- sendSz = idx + length + MAX_MSG_EXTRA;
-
- /* Check buffers are big enough and grow if needed. */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* Get position in output buffer to write new message to. */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* Put the record and handshake headers on. */
- AddTls13Headers(output, length, end_of_early_data, ssl);
-
- /* This message is always encrypted. */
- sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ,
- idx - RECORD_HEADER_SZ, handshake, 1, 0, 0);
- if (sendSz < 0)
- return sendSz;
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- if ((ret = SetKeysSide(ssl, ENCRYPT_SIDE_ONLY)) != 0)
- return ret;
-
- if (!ssl->options.groupMessages)
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendTls13EndOfEarlyData", ret);
- WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_SEND);
-
- return ret;
-}
-#endif /* !NO_WOLFSSL_CLIENT */
-
-#ifndef NO_WOLFSSL_SERVER
-/* handle processing of TLS 1.3 end_of_early_data (5) */
-/* Parse the TLS v1.3 EndOfEarlyData message that indicates that there will be
- * no more early application data.
- * The decryption key now changes to the pre-calculated handshake key.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success and otherwise failure.
- */
-static int DoTls13EndOfEarlyData(WOLFSSL* ssl, const byte* input,
- word32* inOutIdx, word32 size)
-{
- int ret;
- word32 begin = *inOutIdx;
-
- (void)input;
-
- WOLFSSL_START(WC_FUNC_END_OF_EARLY_DATA_DO);
- WOLFSSL_ENTER("DoTls13EndOfEarlyData");
-
- if ((*inOutIdx - begin) != size)
- return BUFFER_ERROR;
-
- if (ssl->earlyData == no_early_data) {
- WOLFSSL_MSG("EndOfEarlyData received unexpectedly");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return OUT_OF_ORDER_E;
- }
-
- ssl->earlyData = done_early_data;
-
- /* Always encrypted. */
- *inOutIdx += ssl->keys.padSz;
-
- ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY);
-
- WOLFSSL_LEAVE("DoTls13EndOfEarlyData", ret);
- WOLFSSL_END(WC_FUNC_END_OF_EARLY_DATA_DO);
-
- return ret;
-}
-#endif /* !NO_WOLFSSL_SERVER */
-#endif /* WOLFSSL_EARLY_DATA */
-
-#ifndef NO_WOLFSSL_CLIENT
-/* Handle a New Session Ticket handshake message.
- * Message contains the information required to perform resumption.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the message buffer of Finished.
- * On exit, the index of byte after the Finished message and padding.
- * size The length of the current handshake message.
- * returns 0 on success, otherwise failure.
- */
-static int DoTls13NewSessionTicket(WOLFSSL* ssl, const byte* input,
- word32* inOutIdx, word32 size)
-{
-#ifdef HAVE_SESSION_TICKET
- int ret;
- word32 begin = *inOutIdx;
- word32 lifetime;
- word32 ageAdd;
- word16 length;
- word32 now;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- const byte* nonce;
- byte nonceLength;
-#endif
-
- WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_DO);
- WOLFSSL_ENTER("DoTls13NewSessionTicket");
-
- /* Lifetime hint. */
- if ((*inOutIdx - begin) + SESSION_HINT_SZ > size)
- return BUFFER_ERROR;
- ato32(input + *inOutIdx, &lifetime);
- *inOutIdx += SESSION_HINT_SZ;
- if (lifetime > MAX_LIFETIME)
- return SERVER_HINT_ERROR;
-
- /* Age add. */
- if ((*inOutIdx - begin) + SESSION_ADD_SZ > size)
- return BUFFER_ERROR;
- ato32(input + *inOutIdx, &ageAdd);
- *inOutIdx += SESSION_ADD_SZ;
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- /* Ticket nonce. */
- if ((*inOutIdx - begin) + 1 > size)
- return BUFFER_ERROR;
- nonceLength = input[*inOutIdx];
- if (nonceLength > MAX_TICKET_NONCE_SZ) {
- WOLFSSL_MSG("Nonce length not supported");
- return INVALID_PARAMETER;
- }
- *inOutIdx += 1;
- if ((*inOutIdx - begin) + nonceLength > size)
- return BUFFER_ERROR;
- nonce = input + *inOutIdx;
- *inOutIdx += nonceLength;
-#endif
-
- /* Ticket length. */
- if ((*inOutIdx - begin) + LENGTH_SZ > size)
- return BUFFER_ERROR;
- ato16(input + *inOutIdx, &length);
- *inOutIdx += LENGTH_SZ;
- if ((*inOutIdx - begin) + length > size)
- return BUFFER_ERROR;
-
- if ((ret = SetTicket(ssl, input + *inOutIdx, length)) != 0)
- return ret;
- *inOutIdx += length;
-
- now = TimeNowInMilliseconds();
- if (now == (word32)GETTIME_ERROR)
- return now;
- /* Copy in ticket data (server identity). */
- ssl->timeout = lifetime;
- ssl->session.timeout = lifetime;
- ssl->session.cipherSuite0 = ssl->options.cipherSuite0;
- ssl->session.cipherSuite = ssl->options.cipherSuite;
- ssl->session.ticketSeen = now;
- ssl->session.ticketAdd = ageAdd;
- #ifdef WOLFSSL_EARLY_DATA
- ssl->session.maxEarlyDataSz = ssl->options.maxEarlyDataSz;
- #endif
-#ifndef WOLFSSL_TLS13_DRAFT_18
- ssl->session.ticketNonce.len = nonceLength;
- if (nonceLength > 0)
- XMEMCPY(&ssl->session.ticketNonce.data, nonce, nonceLength);
-#endif
- ssl->session.namedGroup = ssl->namedGroup;
-
- if ((*inOutIdx - begin) + EXTS_SZ > size)
- return BUFFER_ERROR;
- ato16(input + *inOutIdx, &length);
- *inOutIdx += EXTS_SZ;
- if ((*inOutIdx - begin) + length != size)
- return BUFFER_ERROR;
- #ifdef WOLFSSL_EARLY_DATA
- ret = TLSX_Parse(ssl, (byte *)input + (*inOutIdx), length, session_ticket,
- NULL);
- if (ret != 0)
- return ret;
- #endif
- *inOutIdx += length;
-
- #ifndef NO_SESSION_CACHE
- AddSession(ssl);
- #endif
-
- /* Always encrypted. */
- *inOutIdx += ssl->keys.padSz;
-
- ssl->expect_session_ticket = 0;
-#else
- (void)ssl;
- (void)input;
-
- WOLFSSL_ENTER("DoTls13NewSessionTicket");
-
- *inOutIdx += size + ssl->keys.padSz;
-#endif /* HAVE_SESSION_TICKET */
-
- WOLFSSL_LEAVE("DoTls13NewSessionTicket", 0);
- WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_DO);
-
- return 0;
-}
-#endif /* NO_WOLFSSL_CLIENT */
-
-#ifndef NO_WOLFSSL_SERVER
- #ifdef HAVE_SESSION_TICKET
-
-#ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED
-/* Offset of the MAC size in the finished message. */
-#define FINISHED_MSG_SIZE_OFFSET 3
-
-/* Calculate the resumption secret which includes the unseen client finished
- * message.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-static int ExpectedResumptionSecret(WOLFSSL* ssl)
-{
- int ret;
- word32 finishedSz = 0;
- byte mac[WC_MAX_DIGEST_SIZE];
- Digest digest;
- static byte header[] = { 0x14, 0x00, 0x00, 0x00 };
-
- /* Copy the running hash so we can restore it after. */
- switch (ssl->specs.mac_algorithm) {
- #ifndef NO_SHA256
- case sha256_mac:
- ret = wc_Sha256Copy(&ssl->hsHashes->hashSha256, &digest.sha256);
- if (ret != 0)
- return ret;
- break;
- #endif
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- ret = wc_Sha384Copy(&ssl->hsHashes->hashSha384, &digest.sha384);
- if (ret != 0)
- return ret;
- break;
- #endif
- #ifdef WOLFSSL_TLS13_SHA512
- case sha512_mac:
- ret = wc_Sha512Copy(&ssl->hsHashes->hashSha512, &digest.sha512);
- if (ret != 0)
- return ret;
- break;
- #endif
- }
-
- /* Generate the Client's Finished message and hash it. */
- ret = BuildTls13HandshakeHmac(ssl, ssl->keys.client_write_MAC_secret, mac,
- &finishedSz);
- if (ret != 0)
- return ret;
- header[FINISHED_MSG_SIZE_OFFSET] = finishedSz;
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data) {
- static byte endOfEarlyData[] = { 0x05, 0x00, 0x00, 0x00 };
- ret = HashInputRaw(ssl, endOfEarlyData, sizeof(endOfEarlyData));
- if (ret != 0)
- return ret;
- }
-#endif
- if ((ret = HashInputRaw(ssl, header, sizeof(header))) != 0)
- return ret;
- if ((ret = HashInputRaw(ssl, mac, finishedSz)) != 0)
- return ret;
-
- if ((ret = DeriveResumptionSecret(ssl, ssl->session.masterSecret)) != 0)
- return ret;
-
- /* Restore the hash inline with currently seen messages. */
- switch (ssl->specs.mac_algorithm) {
- #ifndef NO_SHA256
- case sha256_mac:
- ret = wc_Sha256Copy(&digest.sha256, &ssl->hsHashes->hashSha256);
- if (ret != 0)
- return ret;
- break;
- #endif
- #ifdef WOLFSSL_SHA384
- case sha384_mac:
- ret = wc_Sha384Copy(&digest.sha384, &ssl->hsHashes->hashSha384);
- if (ret != 0)
- return ret;
- break;
- #endif
- #ifdef WOLFSSL_TLS13_SHA512
- case sha512_mac:
- ret = wc_Sha512Copy(&digest.sha512, &ssl->hsHashes->hashSha384);
- if (ret != 0)
- return ret;
- break;
- #endif
- }
-
- return ret;
-}
-#endif
-
-/* Send New Session Ticket handshake message.
- * Message contains the information required to perform resumption.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-static int SendTls13NewSessionTicket(WOLFSSL* ssl)
-{
- byte* output;
- int ret;
- int sendSz;
- word16 extSz;
- word32 length;
- word32 idx = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
-
- WOLFSSL_START(WC_FUNC_NEW_SESSION_TICKET_SEND);
- WOLFSSL_ENTER("SendTls13NewSessionTicket");
-
-#ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED
- if (!ssl->msgsReceived.got_finished) {
- if ((ret = ExpectedResumptionSecret(ssl)) != 0)
- return ret;
- }
-#endif
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- /* Start ticket nonce at 0 and go up to 255. */
- if (ssl->session.ticketNonce.len == 0) {
- ssl->session.ticketNonce.len = DEF_TICKET_NONCE_SZ;
- ssl->session.ticketNonce.data[0] = 0;
- }
- else
- ssl->session.ticketNonce.data[0]++;
-#endif
-
- if (!ssl->options.noTicketTls13) {
- if ((ret = CreateTicket(ssl)) != 0)
- return ret;
- }
-
-#ifdef WOLFSSL_EARLY_DATA
- ssl->session.maxEarlyDataSz = ssl->options.maxEarlyDataSz;
- if (ssl->session.maxEarlyDataSz > 0)
- TLSX_EarlyData_Use(ssl, ssl->session.maxEarlyDataSz);
- extSz = 0;
- ret = TLSX_GetResponseSize(ssl, session_ticket, &extSz);
- if (ret != 0)
- return ret;
-#else
- extSz = EXTS_SZ;
-#endif
-
- /* Lifetime | Age Add | Ticket | Extensions */
- length = SESSION_HINT_SZ + SESSION_ADD_SZ + LENGTH_SZ +
- ssl->session.ticketLen + extSz;
-#ifndef WOLFSSL_TLS13_DRAFT_18
- /* Nonce */
- length += TICKET_NONCE_LEN_SZ + DEF_TICKET_NONCE_SZ;
-#endif
- sendSz = idx + length + MAX_MSG_EXTRA;
-
- /* Check buffers are big enough and grow if needed. */
- if ((ret = CheckAvailableSize(ssl, sendSz)) != 0)
- return ret;
-
- /* Get position in output buffer to write new message to. */
- output = ssl->buffers.outputBuffer.buffer +
- ssl->buffers.outputBuffer.length;
-
- /* Put the record and handshake headers on. */
- AddTls13Headers(output, length, session_ticket, ssl);
-
- /* Lifetime hint */
- c32toa(ssl->ctx->ticketHint, output + idx);
- idx += SESSION_HINT_SZ;
- /* Age add - obfuscator */
- c32toa(ssl->session.ticketAdd, output + idx);
- idx += SESSION_ADD_SZ;
-
-#ifndef WOLFSSL_TLS13_DRAFT_18
- output[idx++] = ssl->session.ticketNonce.len;
- output[idx++] = ssl->session.ticketNonce.data[0];
-#endif
-
- /* length */
- c16toa(ssl->session.ticketLen, output + idx);
- idx += LENGTH_SZ;
- /* ticket */
- XMEMCPY(output + idx, ssl->session.ticket, ssl->session.ticketLen);
- idx += ssl->session.ticketLen;
-
-#ifdef WOLFSSL_EARLY_DATA
- extSz = 0;
- ret = TLSX_WriteResponse(ssl, output + idx, session_ticket, &extSz);
- if (ret != 0)
- return ret;
- idx += extSz;
-#else
- /* No extension support - empty extensions. */
- c16toa(0, output + idx);
- idx += EXTS_SZ;
-#endif
-
- ssl->options.haveSessionId = 1;
-
-#ifndef NO_SESSION_CACHE
- AddSession(ssl);
-#endif
-
- /* This message is always encrypted. */
- sendSz = BuildTls13Message(ssl, output, sendSz, output + RECORD_HEADER_SZ,
- idx - RECORD_HEADER_SZ, handshake, 0, 0, 0);
- if (sendSz < 0)
- return sendSz;
-
- ssl->buffers.outputBuffer.length += sendSz;
-
- if (!ssl->options.groupMessages)
- ret = SendBuffered(ssl);
-
- WOLFSSL_LEAVE("SendTls13NewSessionTicket", 0);
- WOLFSSL_END(WC_FUNC_NEW_SESSION_TICKET_SEND);
-
- return ret;
-}
- #endif /* HAVE_SESSION_TICKET */
-#endif /* NO_WOLFSSL_SERVER */
-
-/* Make sure no duplicates, no fast forward, or other problems
- *
- * ssl The SSL/TLS object.
- * type Type of handshake message received.
- * returns 0 on success, otherwise failure.
- */
-static int SanityCheckTls13MsgReceived(WOLFSSL* ssl, byte type)
-{
- /* verify not a duplicate, mark received, check state */
- switch (type) {
-
-#ifndef NO_WOLFSSL_SERVER
- case client_hello:
- #ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- WOLFSSL_MSG("ClientHello received by client");
- return OUT_OF_ORDER_E;
- }
- #endif
- if (ssl->options.clientState >= CLIENT_HELLO_COMPLETE) {
- WOLFSSL_MSG("ClientHello received out of order");
- return OUT_OF_ORDER_E;
- }
- if (ssl->msgsReceived.got_client_hello == 2) {
- WOLFSSL_MSG("Too many ClientHello received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_client_hello++;
-
- break;
-#endif
-
-#ifndef NO_WOLFSSL_CLIENT
- case server_hello:
- #ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- WOLFSSL_MSG("ServerHello received by server");
- return OUT_OF_ORDER_E;
- }
- #endif
- #ifdef WOLFSSL_TLS13_DRAFT_18
- if (ssl->msgsReceived.got_server_hello) {
- WOLFSSL_MSG("Duplicate ServerHello received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_server_hello = 1;
- #else
- if (ssl->msgsReceived.got_server_hello == 2) {
- WOLFSSL_MSG("Duplicate ServerHello received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_server_hello++;
- #endif
-
- break;
-#endif
-
-#ifndef NO_WOLFSSL_CLIENT
- case session_ticket:
- #ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- WOLFSSL_MSG("NewSessionTicket received by server");
- return OUT_OF_ORDER_E;
- }
- #endif
- if (ssl->options.clientState < CLIENT_FINISHED_COMPLETE) {
- WOLFSSL_MSG("NewSessionTicket received out of order");
- return OUT_OF_ORDER_E;
- }
- ssl->msgsReceived.got_session_ticket = 1;
-
- break;
-#endif
-
-#ifndef NO_WOLFSSL_SERVER
- #ifdef WOLFSSL_EARLY_DATA
- case end_of_early_data:
- #ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- WOLFSSL_MSG("EndOfEarlyData received by client");
- return OUT_OF_ORDER_E;
- }
- #endif
- if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) {
- WOLFSSL_MSG("EndOfEarlyData received out of order");
- return OUT_OF_ORDER_E;
- }
- if (ssl->options.clientState >= CLIENT_FINISHED_COMPLETE) {
- WOLFSSL_MSG("EndOfEarlyData received out of order");
- return OUT_OF_ORDER_E;
- }
- if (ssl->msgsReceived.got_end_of_early_data == 1) {
- WOLFSSL_MSG("Too many EndOfEarlyData received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_end_of_early_data++;
-
- break;
- #endif
-#endif
-
-#ifdef WOLFSSL_TLS13_DRAFT_18
- #ifndef NO_WOLFSSL_CLIENT
- case hello_retry_request:
- #ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- WOLFSSL_MSG("HelloRetryRequest received by server");
- return OUT_OF_ORDER_E;
- }
- #endif
- if (ssl->options.clientState > CLIENT_FINISHED_COMPLETE) {
- WOLFSSL_MSG("HelloRetryRequest received out of order");
- return OUT_OF_ORDER_E;
- }
- if (ssl->msgsReceived.got_hello_retry_request) {
- WOLFSSL_MSG("Duplicate HelloRetryRequest received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_hello_retry_request = 1;
-
- break;
- #endif
-#endif
-
-#ifndef NO_WOLFSSL_CLIENT
- case encrypted_extensions:
- #ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- WOLFSSL_MSG("EncryptedExtensions received by server");
- return OUT_OF_ORDER_E;
- }
- #endif
- if (ssl->options.serverState != SERVER_HELLO_COMPLETE) {
- WOLFSSL_MSG("EncryptedExtensions received out of order");
- return OUT_OF_ORDER_E;
- }
- if (ssl->msgsReceived.got_encrypted_extensions) {
- WOLFSSL_MSG("Duplicate EncryptedExtensions received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_encrypted_extensions = 1;
-
- break;
-#endif
-
- case certificate:
- #ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END &&
- ssl->options.serverState !=
- SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) {
- WOLFSSL_MSG("Certificate received out of order - Client");
- return OUT_OF_ORDER_E;
- }
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- /* Server's authenticating with PSK must not send this. */
- if (ssl->options.side == WOLFSSL_CLIENT_END &&
- ssl->options.serverState == SERVER_CERT_COMPLETE &&
- ssl->arrays->psk_keySz != 0) {
- WOLFSSL_MSG("Certificate received while using PSK");
- return SANITY_MSG_E;
- }
- #endif
- #endif
- #ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- ssl->options.serverState < SERVER_FINISHED_COMPLETE) {
- WOLFSSL_MSG("Certificate received out of order - Server");
- return OUT_OF_ORDER_E;
- }
- #endif
- if (ssl->msgsReceived.got_certificate) {
- WOLFSSL_MSG("Duplicate Certificate received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_certificate = 1;
-
- break;
-
-#ifndef NO_WOLFSSL_CLIENT
- case certificate_request:
- #ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- WOLFSSL_MSG("CertificateRequest received by server");
- return OUT_OF_ORDER_E;
- }
- #endif
- #ifndef WOLFSSL_POST_HANDSHAKE_AUTH
- if (ssl->options.serverState !=
- SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) {
- WOLFSSL_MSG("CertificateRequest received out of order");
- return OUT_OF_ORDER_E;
- }
- #else
- if (ssl->options.serverState !=
- SERVER_ENCRYPTED_EXTENSIONS_COMPLETE &&
- (ssl->options.serverState != SERVER_FINISHED_COMPLETE ||
- ssl->options.clientState != CLIENT_FINISHED_COMPLETE)) {
- WOLFSSL_MSG("CertificateRequest received out of order");
- return OUT_OF_ORDER_E;
- }
- #endif
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- /* Server's authenticating with PSK must not send this. */
- if (ssl->options.serverState ==
- SERVER_ENCRYPTED_EXTENSIONS_COMPLETE &&
- ssl->arrays->psk_keySz != 0) {
- WOLFSSL_MSG("CertificateRequset received while using PSK");
- return SANITY_MSG_E;
- }
- #endif
- #ifndef WOLFSSL_POST_HANDSHAKE_AUTH
- if (ssl->msgsReceived.got_certificate_request) {
- WOLFSSL_MSG("Duplicate CertificateRequest received");
- return DUPLICATE_MSG_E;
- }
- #endif
- ssl->msgsReceived.got_certificate_request = 1;
-
- break;
-#endif
-
- case certificate_verify:
- #ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if (ssl->options.serverState != SERVER_CERT_COMPLETE) {
- WOLFSSL_MSG("No Cert before CertVerify");
- return OUT_OF_ORDER_E;
- }
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- /* Server's authenticating with PSK must not send this. */
- if (ssl->options.serverState == SERVER_CERT_COMPLETE &&
- ssl->arrays->psk_keySz != 0) {
- WOLFSSL_MSG("CertificateVerify received while using PSK");
- return SANITY_MSG_E;
- }
- #endif
- }
- #endif
- #ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) {
- WOLFSSL_MSG("CertificateVerify received out of order");
- return OUT_OF_ORDER_E;
- }
- if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) {
- WOLFSSL_MSG("CertificateVerify before ClientHello done");
- return OUT_OF_ORDER_E;
- }
- if (!ssl->msgsReceived.got_certificate) {
- WOLFSSL_MSG("No Cert before CertificateVerify");
- return OUT_OF_ORDER_E;
- }
- }
- #endif
- if (ssl->msgsReceived.got_certificate_verify) {
- WOLFSSL_MSG("Duplicate CertificateVerify received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_certificate_verify = 1;
-
- break;
-
- case finished:
- #ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) {
- WOLFSSL_MSG("Finished received out of order");
- return OUT_OF_ORDER_E;
- }
- if (ssl->options.serverState <
- SERVER_ENCRYPTED_EXTENSIONS_COMPLETE) {
- WOLFSSL_MSG("Finished received out of order");
- return OUT_OF_ORDER_E;
- }
- }
- #endif
- #ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_SERVER_END) {
- if (ssl->options.serverState < SERVER_FINISHED_COMPLETE) {
- WOLFSSL_MSG("Finished received out of order");
- return OUT_OF_ORDER_E;
- }
- if (ssl->options.clientState < CLIENT_HELLO_COMPLETE) {
- WOLFSSL_MSG("Finished received out of order");
- return OUT_OF_ORDER_E;
- }
- #ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData == process_early_data) {
- return OUT_OF_ORDER_E;
- }
- #endif
- }
- #endif
- if (ssl->msgsReceived.got_finished) {
- WOLFSSL_MSG("Duplicate Finished received");
- return DUPLICATE_MSG_E;
- }
- ssl->msgsReceived.got_finished = 1;
-
- break;
-
- case key_update:
- if (!ssl->msgsReceived.got_finished) {
- WOLFSSL_MSG("No KeyUpdate before Finished");
- return OUT_OF_ORDER_E;
- }
- break;
-
- default:
- WOLFSSL_MSG("Unknown message type");
- return SANITY_MSG_E;
- }
-
- return 0;
-}
-
-/* Handle a type of handshake message that has been received.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the buffer of the current message.
- * On exit, the index into the buffer of the next message.
- * size The length of the current handshake message.
- * totalSz Length of remaining data in the message buffer.
- * returns 0 on success and otherwise failure.
- */
-int DoTls13HandShakeMsgType(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- byte type, word32 size, word32 totalSz)
-{
- int ret = 0;
- word32 inIdx = *inOutIdx;
-
- (void)totalSz;
-
- WOLFSSL_ENTER("DoTls13HandShakeMsgType");
-
- /* make sure we can read the message */
- if (*inOutIdx + size > totalSz)
- return INCOMPLETE_DATA;
-
- /* sanity check msg received */
- if ((ret = SanityCheckTls13MsgReceived(ssl, type)) != 0) {
- WOLFSSL_MSG("Sanity Check on handshake message type received failed");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return ret;
- }
-
-#ifdef WOLFSSL_CALLBACKS
- /* add name later, add on record and handshake header part back on */
- if (ssl->toInfoOn) {
- int add = RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ;
- AddPacketInfo(ssl, 0, handshake, input + *inOutIdx - add,
- size + add, READ_PROTO, ssl->heap);
- AddLateRecordHeader(&ssl->curRL, &ssl->timeoutInfo);
- }
-#endif
-
- if (ssl->options.handShakeState == HANDSHAKE_DONE &&
- type != session_ticket && type != certificate_request &&
- type != certificate && type != key_update) {
- WOLFSSL_MSG("HandShake message after handshake complete");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return OUT_OF_ORDER_E;
- }
-
- if (ssl->options.side == WOLFSSL_CLIENT_END &&
- ssl->options.serverState == NULL_STATE &&
- type != server_hello && type != hello_retry_request) {
- WOLFSSL_MSG("First server message not server hello");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return OUT_OF_ORDER_E;
- }
-
- if (ssl->options.side == WOLFSSL_SERVER_END &&
- ssl->options.clientState == NULL_STATE && type != client_hello) {
- WOLFSSL_MSG("First client message not client hello");
- SendAlert(ssl, alert_fatal, unexpected_message);
- return OUT_OF_ORDER_E;
- }
-
- /* above checks handshake state */
- switch (type) {
-#ifndef NO_WOLFSSL_CLIENT
- /* Messages only received by client. */
- #ifdef WOLFSSL_TLS13_DRAFT_18
- case hello_retry_request:
- WOLFSSL_MSG("processing hello retry request");
- ret = DoTls13HelloRetryRequest(ssl, input, inOutIdx, size);
- break;
- #endif
-
- case server_hello:
- WOLFSSL_MSG("processing server hello");
- ret = DoTls13ServerHello(ssl, input, inOutIdx, size, &type);
- #if !defined(WOLFSSL_NO_CLIENT_AUTH) && \
- ((defined(HAVE_ED25519) && !defined(NO_ED25519_CLIENT_AUTH)) || \
- (defined(HAVE_ED448) && !defined(NO_ED448_CLIENT_AUTH)))
- if (ssl->options.resuming || !IsAtLeastTLSv1_2(ssl) ||
- IsAtLeastTLSv1_3(ssl->version)) {
- ssl->options.cacheMessages = 0;
- if (ssl->hsHashes->messages != NULL) {
- XFREE(ssl->hsHashes->messages, ssl->heap, DYNAMIC_TYPE_HASHES);
- ssl->hsHashes->messages = NULL;
- }
- }
- #endif
- break;
-
- case encrypted_extensions:
- WOLFSSL_MSG("processing encrypted extensions");
- ret = DoTls13EncryptedExtensions(ssl, input, inOutIdx, size);
- break;
-
- #ifndef NO_CERTS
- case certificate_request:
- WOLFSSL_MSG("processing certificate request");
- ret = DoTls13CertificateRequest(ssl, input, inOutIdx, size);
- break;
- #endif
-
- case session_ticket:
- WOLFSSL_MSG("processing new session ticket");
- ret = DoTls13NewSessionTicket(ssl, input, inOutIdx, size);
- break;
-#endif /* !NO_WOLFSSL_CLIENT */
-
-#ifndef NO_WOLFSSL_SERVER
- /* Messages only received by server. */
- case client_hello:
- WOLFSSL_MSG("processing client hello");
- ret = DoTls13ClientHello(ssl, input, inOutIdx, size);
- break;
-
- #ifdef WOLFSSL_EARLY_DATA
- case end_of_early_data:
- WOLFSSL_MSG("processing end of early data");
- ret = DoTls13EndOfEarlyData(ssl, input, inOutIdx, size);
- break;
- #endif
-#endif /* !NO_WOLFSSL_SERVER */
-
- /* Messages received by both client and server. */
-#ifndef NO_CERTS
- case certificate:
- WOLFSSL_MSG("processing certificate");
- ret = DoTls13Certificate(ssl, input, inOutIdx, size);
- break;
-#endif
-
-#if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519) || \
- defined(HAVE_ED448)
- case certificate_verify:
- WOLFSSL_MSG("processing certificate verify");
- ret = DoTls13CertificateVerify(ssl, input, inOutIdx, size);
- break;
-#endif /* !NO_RSA || HAVE_ECC */
-
- case finished:
- WOLFSSL_MSG("processing finished");
- ret = DoTls13Finished(ssl, input, inOutIdx, size, totalSz, NO_SNIFF);
- break;
-
- case key_update:
- WOLFSSL_MSG("processing finished");
- ret = DoTls13KeyUpdate(ssl, input, inOutIdx, size);
- break;
-
- default:
- WOLFSSL_MSG("Unknown handshake message type");
- ret = UNKNOWN_HANDSHAKE_TYPE;
- break;
- }
-
- /* reset error */
- if (ret == 0 && ssl->error == WC_PENDING_E)
- ssl->error = 0;
-
- if (ret == 0 && type != client_hello && type != session_ticket &&
- type != key_update) {
- ret = HashInput(ssl, input + inIdx, size);
- }
- if (ret == 0 && ssl->buffers.inputBuffer.dynamicFlag) {
- ShrinkInputBuffer(ssl, NO_FORCED_FREE);
- }
-
- if (ret == BUFFER_ERROR || ret == MISSING_HANDSHAKE_DATA)
- SendAlert(ssl, alert_fatal, decode_error);
- else if (ret == EXT_NOT_ALLOWED || ret == PEER_KEY_ERROR ||
- ret == ECC_PEERKEY_ERROR || ret == BAD_KEY_SHARE_DATA ||
- ret == PSK_KEY_ERROR || ret == INVALID_PARAMETER) {
- SendAlert(ssl, alert_fatal, illegal_parameter);
- }
-
- if (ret == 0 && ssl->options.tls1_3) {
- /* Need to hash input message before deriving secrets. */
- #ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_CLIENT_END) {
- if (type == server_hello) {
- if ((ret = DeriveEarlySecret(ssl)) != 0)
- return ret;
- if ((ret = DeriveHandshakeSecret(ssl)) != 0)
- return ret;
-
- if ((ret = DeriveTls13Keys(ssl, handshake_key,
- ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) {
- return ret;
- }
- #ifdef WOLFSSL_EARLY_DATA
- if ((ret = SetKeysSide(ssl, DECRYPT_SIDE_ONLY)) != 0)
- return ret;
- #else
- if ((ret = SetKeysSide(ssl, ENCRYPT_AND_DECRYPT_SIDE)) != 0)
- return ret;
- #endif
- }
-
- if (type == finished) {
- if ((ret = DeriveMasterSecret(ssl)) != 0)
- return ret;
- #ifdef WOLFSSL_EARLY_DATA
- if ((ret = DeriveTls13Keys(ssl, traffic_key,
- ENCRYPT_AND_DECRYPT_SIDE,
- ssl->earlyData == no_early_data)) != 0) {
- return ret;
- }
- #else
- if ((ret = DeriveTls13Keys(ssl, traffic_key,
- ENCRYPT_AND_DECRYPT_SIDE, 1)) != 0) {
- return ret;
- }
- #endif
- }
- #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
- if (type == certificate_request &&
- ssl->options.handShakeState == HANDSHAKE_DONE) {
- /* reset handshake states */
- ssl->options.clientState = CLIENT_HELLO_COMPLETE;
- ssl->options.connectState = FIRST_REPLY_DONE;
- ssl->options.handShakeState = CLIENT_HELLO_COMPLETE;
-
- if (wolfSSL_connect_TLSv13(ssl) != SSL_SUCCESS)
- ret = POST_HAND_AUTH_ERROR;
- }
- #endif
- }
- #endif /* NO_WOLFSSL_CLIENT */
-
-#ifndef NO_WOLFSSL_SERVER
- #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- if (ssl->options.side == WOLFSSL_SERVER_END && type == finished) {
- ret = DeriveResumptionSecret(ssl, ssl->session.masterSecret);
- if (ret != 0)
- return ret;
- }
- #endif
-#endif /* NO_WOLFSSL_SERVER */
- }
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- /* if async, offset index so this msg will be processed again */
- if (ret == WC_PENDING_E && *inOutIdx > 0) {
- *inOutIdx -= HANDSHAKE_HEADER_SZ;
- }
-#endif
-
- WOLFSSL_LEAVE("DoTls13HandShakeMsgType()", ret);
- return ret;
-}
-
-
-/* Handle a handshake message that has been received.
- *
- * ssl The SSL/TLS object.
- * input The message buffer.
- * inOutIdx On entry, the index into the buffer of the current message.
- * On exit, the index into the buffer of the next message.
- * totalSz Length of remaining data in the message buffer.
- * returns 0 on success and otherwise failure.
- */
-int DoTls13HandShakeMsg(WOLFSSL* ssl, byte* input, word32* inOutIdx,
- word32 totalSz)
-{
- int ret = 0;
- word32 inputLength;
-
- WOLFSSL_ENTER("DoTls13HandShakeMsg()");
-
- if (ssl->arrays == NULL) {
- byte type;
- word32 size;
-
- if (GetHandshakeHeader(ssl, input, inOutIdx, &type, &size,
- totalSz) != 0) {
- SendAlert(ssl, alert_fatal, unexpected_message);
- return PARSE_ERROR;
- }
-
- return DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size,
- totalSz);
- }
-
- inputLength = ssl->buffers.inputBuffer.length - *inOutIdx - ssl->keys.padSz;
-
- /* If there is a pending fragmented handshake message,
- * pending message size will be non-zero. */
- if (ssl->arrays->pendingMsgSz == 0) {
- byte type;
- word32 size;
-
- if (GetHandshakeHeader(ssl,input, inOutIdx, &type, &size, totalSz) != 0)
- return PARSE_ERROR;
-
- /* Cap the maximum size of a handshake message to something reasonable.
- * By default is the maximum size of a certificate message assuming
- * nine 2048-bit RSA certificates in the chain. */
- if (size > MAX_HANDSHAKE_SZ) {
- WOLFSSL_MSG("Handshake message too large");
- return HANDSHAKE_SIZE_ERROR;
- }
-
- /* size is the size of the certificate message payload */
- if (inputLength - HANDSHAKE_HEADER_SZ < size) {
- ssl->arrays->pendingMsgType = type;
- ssl->arrays->pendingMsgSz = size + HANDSHAKE_HEADER_SZ;
- ssl->arrays->pendingMsg = (byte*)XMALLOC(size + HANDSHAKE_HEADER_SZ,
- ssl->heap,
- DYNAMIC_TYPE_ARRAYS);
- if (ssl->arrays->pendingMsg == NULL)
- return MEMORY_E;
- XMEMCPY(ssl->arrays->pendingMsg,
- input + *inOutIdx - HANDSHAKE_HEADER_SZ,
- inputLength);
- ssl->arrays->pendingMsgOffset = inputLength;
- *inOutIdx += inputLength + ssl->keys.padSz - HANDSHAKE_HEADER_SZ;
- return 0;
- }
-
- ret = DoTls13HandShakeMsgType(ssl, input, inOutIdx, type, size,
- totalSz);
- }
- else {
- if (inputLength + ssl->arrays->pendingMsgOffset >
- ssl->arrays->pendingMsgSz) {
- inputLength = ssl->arrays->pendingMsgSz -
- ssl->arrays->pendingMsgOffset;
- }
- XMEMCPY(ssl->arrays->pendingMsg + ssl->arrays->pendingMsgOffset,
- input + *inOutIdx, inputLength);
- ssl->arrays->pendingMsgOffset += inputLength;
- *inOutIdx += inputLength + ssl->keys.padSz;
-
- if (ssl->arrays->pendingMsgOffset == ssl->arrays->pendingMsgSz)
- {
- word32 idx = 0;
- ret = DoTls13HandShakeMsgType(ssl,
- ssl->arrays->pendingMsg + HANDSHAKE_HEADER_SZ,
- &idx, ssl->arrays->pendingMsgType,
- ssl->arrays->pendingMsgSz - HANDSHAKE_HEADER_SZ,
- ssl->arrays->pendingMsgSz);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- /* setup to process fragment again */
- ssl->arrays->pendingMsgOffset -= inputLength;
- *inOutIdx -= inputLength + ssl->keys.padSz;
- }
- else
- #endif
- {
- XFREE(ssl->arrays->pendingMsg, ssl->heap, DYNAMIC_TYPE_ARRAYS);
- ssl->arrays->pendingMsg = NULL;
- ssl->arrays->pendingMsgSz = 0;
- }
- }
- }
-
- WOLFSSL_LEAVE("DoTls13HandShakeMsg()", ret);
- return ret;
-}
-
-#ifndef NO_WOLFSSL_CLIENT
-
-/* The client connecting to the server.
- * The protocol version is expecting to be TLS v1.3.
- * If the server downgrades, and older versions of the protocol are compiled
- * in, the client will fallback to wolfSSL_connect().
- * Please see note at top of README if you get an error from connect.
- *
- * ssl The SSL/TLS object.
- * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when
- * unrecoverable error occurs and 0 otherwise.
- * For more error information use wolfSSL_get_error().
- */
-int wolfSSL_connect_TLSv13(WOLFSSL* ssl)
-{
- WOLFSSL_ENTER("wolfSSL_connect_TLSv13()");
-
- #ifdef HAVE_ERRNO_H
- errno = 0;
- #endif
-
- if (ssl->options.side != WOLFSSL_CLIENT_END) {
- WOLFSSL_ERROR(ssl->error = SIDE_ERROR);
- return WOLFSSL_FATAL_ERROR;
- }
-
- if (ssl->buffers.outputBuffer.length > 0
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* do not send buffered or advance state if last error was an
- async pending operation */
- && ssl->error != WC_PENDING_E
- #endif
- ) {
- if ((ssl->error = SendBuffered(ssl)) == 0) {
- /* fragOffset is non-zero when sending fragments. On the last
- * fragment, fragOffset is zero again, and the state can be
- * advanced. */
- if (ssl->fragOffset == 0) {
- ssl->options.connectState++;
- WOLFSSL_MSG("connect state: "
- "Advanced from last buffered fragment send");
- }
- else {
- WOLFSSL_MSG("connect state: "
- "Not advanced, more fragments to send");
- }
- }
- else {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- switch (ssl->options.connectState) {
-
- case CONNECT_BEGIN:
- /* Always send client hello first. */
- if ((ssl->error = SendTls13ClientHello(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
-
- ssl->options.connectState = CLIENT_HELLO_SENT;
- WOLFSSL_MSG("connect state: CLIENT_HELLO_SENT");
- #ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data) {
- #if !defined(WOLFSSL_TLS13_DRAFT_18) && \
- defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
- if ((ssl->error = SendChangeCipher(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.sentChangeCipher = 1;
- #endif
- ssl->options.handShakeState = CLIENT_HELLO_COMPLETE;
- return WOLFSSL_SUCCESS;
- }
- #endif
- FALL_THROUGH;
-
- case CLIENT_HELLO_SENT:
- /* Get the response/s from the server. */
- while (ssl->options.serverState <
- SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
- if ((ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- ssl->options.connectState = HELLO_AGAIN;
- WOLFSSL_MSG("connect state: HELLO_AGAIN");
- FALL_THROUGH;
-
- case HELLO_AGAIN:
- if (ssl->options.certOnly)
- return WOLFSSL_SUCCESS;
-
- if (!ssl->options.tls1_3) {
- #ifndef WOLFSSL_NO_TLS12
- if (ssl->options.downgrade)
- return wolfSSL_connect(ssl);
- #endif
-
- WOLFSSL_MSG("Client using higher version, fatal error");
- return VERSION_ERROR;
- }
-
- if (ssl->options.serverState ==
- SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
- #if !defined(WOLFSSL_TLS13_DRAFT_18) && \
- defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
- if (!ssl->options.sentChangeCipher) {
- if ((ssl->error = SendChangeCipher(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.sentChangeCipher = 1;
- }
- #endif
- /* Try again with different security parameters. */
- if ((ssl->error = SendTls13ClientHello(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- ssl->options.connectState = HELLO_AGAIN_REPLY;
- WOLFSSL_MSG("connect state: HELLO_AGAIN_REPLY");
- FALL_THROUGH;
-
- case HELLO_AGAIN_REPLY:
- /* Get the response/s from the server. */
- while (ssl->options.serverState < SERVER_FINISHED_COMPLETE) {
- if ((ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- ssl->options.connectState = FIRST_REPLY_DONE;
- WOLFSSL_MSG("connect state: FIRST_REPLY_DONE");
- FALL_THROUGH;
-
- case FIRST_REPLY_DONE:
- #ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data) {
- if ((ssl->error = SendTls13EndOfEarlyData(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- WOLFSSL_MSG("sent: end_of_early_data");
- }
- #endif
-
- ssl->options.connectState = FIRST_REPLY_FIRST;
- WOLFSSL_MSG("connect state: FIRST_REPLY_FIRST");
- FALL_THROUGH;
-
- case FIRST_REPLY_FIRST:
- #if !defined(WOLFSSL_TLS13_DRAFT_18) && \
- defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
- if (!ssl->options.sentChangeCipher) {
- if ((ssl->error = SendChangeCipher(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.sentChangeCipher = 1;
- }
- #endif
-
- ssl->options.connectState = FIRST_REPLY_SECOND;
- WOLFSSL_MSG("connect state: FIRST_REPLY_SECOND");
- FALL_THROUGH;
-
- case FIRST_REPLY_SECOND:
- #ifndef NO_CERTS
- if (!ssl->options.resuming && ssl->options.sendVerify) {
- ssl->error = SendTls13Certificate(ssl);
- if (ssl->error != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- WOLFSSL_MSG("sent: certificate");
- }
- #endif
-
- ssl->options.connectState = FIRST_REPLY_THIRD;
- WOLFSSL_MSG("connect state: FIRST_REPLY_THIRD");
- FALL_THROUGH;
-
- case FIRST_REPLY_THIRD:
- #ifndef NO_CERTS
- if (!ssl->options.resuming && ssl->options.sendVerify) {
- ssl->error = SendTls13CertificateVerify(ssl);
- if (ssl->error != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- WOLFSSL_MSG("sent: certificate verify");
- }
- #endif
-
- ssl->options.connectState = FIRST_REPLY_FOURTH;
- WOLFSSL_MSG("connect state: FIRST_REPLY_FOURTH");
- FALL_THROUGH;
-
- case FIRST_REPLY_FOURTH:
- if ((ssl->error = SendTls13Finished(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- WOLFSSL_MSG("sent: finished");
-
- ssl->options.connectState = FINISHED_DONE;
- WOLFSSL_MSG("connect state: FINISHED_DONE");
- FALL_THROUGH;
-
- case FINISHED_DONE:
- #ifndef NO_HANDSHAKE_DONE_CB
- if (ssl->hsDoneCb != NULL) {
- int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx);
- if (cbret < 0) {
- ssl->error = cbret;
- WOLFSSL_MSG("HandShake Done Cb don't continue error");
- return WOLFSSL_FATAL_ERROR;
- }
- }
- #endif /* NO_HANDSHAKE_DONE_CB */
-
- if (!ssl->options.keepResources) {
- FreeHandshakeResources(ssl);
- }
-
- WOLFSSL_LEAVE("wolfSSL_connect_TLSv13()", WOLFSSL_SUCCESS);
- return WOLFSSL_SUCCESS;
-
- default:
- WOLFSSL_MSG("Unknown connect state ERROR");
- return WOLFSSL_FATAL_ERROR; /* unknown connect state */
- }
-}
-#endif
-
-#if defined(WOLFSSL_SEND_HRR_COOKIE)
-/* Send a cookie with the HelloRetryRequest to avoid storing state.
- *
- * ssl SSL/TLS object.
- * secret Secret to use when generating integrity check for cookie.
- * A value of NULL indicates to generate a new random secret.
- * secretSz Size of secret data in bytes.
- * Use a value of 0 to indicate use of default size.
- * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3, SIDE_ERROR when
- * called on a client; WOLFSSL_SUCCESS on success and otherwise failure.
- */
-int wolfSSL_send_hrr_cookie(WOLFSSL* ssl, const unsigned char* secret,
- unsigned int secretSz)
-{
- int ret;
-
- if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
- #ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- return SIDE_ERROR;
-
- if (secretSz == 0) {
- #if !defined(NO_SHA) && defined(NO_SHA256)
- secretSz = WC_SHA_DIGEST_SIZE;
- #endif /* NO_SHA */
- #ifndef NO_SHA256
- secretSz = WC_SHA256_DIGEST_SIZE;
- #endif /* NO_SHA256 */
- }
-
- if (secretSz != ssl->buffers.tls13CookieSecret.length) {
- byte* newSecret;
-
- if (ssl->buffers.tls13CookieSecret.buffer != NULL) {
- ForceZero(ssl->buffers.tls13CookieSecret.buffer,
- ssl->buffers.tls13CookieSecret.length);
- XFREE(ssl->buffers.tls13CookieSecret.buffer,
- ssl->heap, DYNAMIC_TYPE_COOKIE_PWD);
- }
-
- newSecret = (byte*)XMALLOC(secretSz, ssl->heap,
- DYNAMIC_TYPE_COOKIE_PWD);
- if (newSecret == NULL) {
- ssl->buffers.tls13CookieSecret.buffer = NULL;
- ssl->buffers.tls13CookieSecret.length = 0;
- WOLFSSL_MSG("couldn't allocate new cookie secret");
- return MEMORY_ERROR;
- }
- ssl->buffers.tls13CookieSecret.buffer = newSecret;
- ssl->buffers.tls13CookieSecret.length = secretSz;
- }
-
- /* If the supplied secret is NULL, randomly generate a new secret. */
- if (secret == NULL) {
- ret = wc_RNG_GenerateBlock(ssl->rng,
- ssl->buffers.tls13CookieSecret.buffer, secretSz);
- if (ret < 0)
- return ret;
- }
- else
- XMEMCPY(ssl->buffers.tls13CookieSecret.buffer, secret, secretSz);
-
- ssl->options.sendCookie = 1;
-
- ret = WOLFSSL_SUCCESS;
-#else
- (void)secret;
- (void)secretSz;
-
- ret = SIDE_ERROR;
-#endif
-
- return ret;
-}
-#endif
-
-/* Create a key share entry from group.
- * Generates a key pair.
- *
- * ssl The SSL/TLS object.
- * group The named group.
- * returns 0 on success, otherwise failure.
- */
-int wolfSSL_UseKeyShare(WOLFSSL* ssl, word16 group)
-{
- int ret;
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
-
- ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL);
- if (ret != 0)
- return ret;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Send no key share entries - use HelloRetryRequest to negotiate shared group.
- *
- * ssl The SSL/TLS object.
- * returns 0 on success, otherwise failure.
- */
-int wolfSSL_NoKeyShares(WOLFSSL* ssl)
-{
- int ret;
-
- if (ssl == NULL)
- return BAD_FUNC_ARG;
- if (ssl->options.side == WOLFSSL_SERVER_END)
- return SIDE_ERROR;
-
- ret = TLSX_KeyShare_Empty(ssl);
- if (ret != 0)
- return ret;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Do not send a ticket after TLS v1.3 handshake for resumption.
- *
- * ctx The SSL/TLS CTX object.
- * returns BAD_FUNC_ARG when ctx is NULL and 0 on success.
- */
-int wolfSSL_CTX_no_ticket_TLSv13(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version))
- return BAD_FUNC_ARG;
- if (ctx->method->side == WOLFSSL_CLIENT_END)
- return SIDE_ERROR;
-
-#ifdef HAVE_SESSION_TICKET
- ctx->noTicketTls13 = 1;
-#endif
-
- return 0;
-}
-
-/* Do not send a ticket after TLS v1.3 handshake for resumption.
- *
- * ssl The SSL/TLS object.
- * returns BAD_FUNC_ARG when ssl is NULL, not using TLS v1.3, or called on
- * a client and 0 on success.
- */
-int wolfSSL_no_ticket_TLSv13(WOLFSSL* ssl)
-{
- if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- return SIDE_ERROR;
-
-#ifdef HAVE_SESSION_TICKET
- ssl->options.noTicketTls13 = 1;
-#endif
-
- return 0;
-}
-
-/* Disallow (EC)DHE key exchange when using pre-shared keys.
- *
- * ctx The SSL/TLS CTX object.
- * returns BAD_FUNC_ARG when ctx is NULL and 0 on success.
- */
-int wolfSSL_CTX_no_dhe_psk(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version))
- return BAD_FUNC_ARG;
-
- ctx->noPskDheKe = 1;
-
- return 0;
-}
-
-/* Disallow (EC)DHE key exchange when using pre-shared keys.
- *
- * ssl The SSL/TLS object.
- * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3 and 0 on
- * success.
- */
-int wolfSSL_no_dhe_psk(WOLFSSL* ssl)
-{
- if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
-
- ssl->options.noPskDheKe = 1;
-
- return 0;
-}
-
-/* Update the keys for encryption and decryption.
- * If using non-blocking I/O and WOLFSSL_ERROR_WANT_WRITE is returned then
- * calling wolfSSL_write() will have the message sent when ready.
- *
- * ssl The SSL/TLS object.
- * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3,
- * WOLFSSL_ERROR_WANT_WRITE when non-blocking I/O is not ready to write,
- * WOLFSSL_SUCCESS on success and otherwise failure.
- */
-int wolfSSL_update_keys(WOLFSSL* ssl)
-{
- int ret;
-
- if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
-
- ret = SendTls13KeyUpdate(ssl);
- if (ret == WANT_WRITE)
- ret = WOLFSSL_ERROR_WANT_WRITE;
- else if (ret == 0)
- ret = WOLFSSL_SUCCESS;
- return ret;
-}
-
-#if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
-/* Allow post-handshake authentication in TLS v1.3 connections.
- *
- * ctx The SSL/TLS CTX object.
- * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a client and
- * 0 on success.
- */
-int wolfSSL_CTX_allow_post_handshake_auth(WOLFSSL_CTX* ctx)
-{
- if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version))
- return BAD_FUNC_ARG;
- if (ctx->method->side == WOLFSSL_SERVER_END)
- return SIDE_ERROR;
-
- ctx->postHandshakeAuth = 1;
-
- return 0;
-}
-
-/* Allow post-handshake authentication in TLS v1.3 connection.
- *
- * ssl The SSL/TLS object.
- * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3,
- * SIDE_ERROR when not a client and 0 on success.
- */
-int wolfSSL_allow_post_handshake_auth(WOLFSSL* ssl)
-{
- if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
- if (ssl->options.side == WOLFSSL_SERVER_END)
- return SIDE_ERROR;
-
- ssl->options.postHandshakeAuth = 1;
-
- return 0;
-}
-
-/* Request a certificate of the client.
- * Can be called any time after handshake completion.
- * A maximum of 256 requests can be sent on a connection.
- *
- * ssl SSL/TLS object.
- */
-int wolfSSL_request_certificate(WOLFSSL* ssl)
-{
- int ret;
-#ifndef NO_WOLFSSL_SERVER
- CertReqCtx* certReqCtx;
-#endif
-
- if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
-#ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- return SIDE_ERROR;
- if (ssl->options.handShakeState != HANDSHAKE_DONE)
- return NOT_READY_ERROR;
- if (!ssl->options.postHandshakeAuth)
- return POST_HAND_AUTH_ERROR;
-
- certReqCtx = (CertReqCtx*)XMALLOC(sizeof(CertReqCtx), ssl->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (certReqCtx == NULL)
- return MEMORY_E;
- XMEMSET(certReqCtx, 0, sizeof(CertReqCtx));
- certReqCtx->next = ssl->certReqCtx;
- certReqCtx->len = 1;
- if (certReqCtx->next != NULL)
- certReqCtx->ctx = certReqCtx->next->ctx + 1;
- ssl->certReqCtx = certReqCtx;
-
- ssl->msgsReceived.got_certificate = 0;
- ssl->msgsReceived.got_certificate_verify = 0;
- ssl->msgsReceived.got_finished = 0;
-
- ret = SendTls13CertificateRequest(ssl, &certReqCtx->ctx, certReqCtx->len);
- if (ret == WANT_WRITE)
- ret = WOLFSSL_ERROR_WANT_WRITE;
- else if (ret == 0)
- ret = WOLFSSL_SUCCESS;
-#else
- ret = SIDE_ERROR;
-#endif
-
- return ret;
-}
-#endif /* !NO_CERTS && WOLFSSL_POST_HANDSHAKE_AUTH */
-
-#if !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
-/* Get the preferred key exchange group.
- *
- * ssl The SSL/TLS object.
- * returns BAD_FUNC_ARG when ssl is NULL or not using TLS v1.3,
- * SIDE_ERROR when not a client, NOT_READY_ERROR when handshake not complete
- * and group number on success.
- */
-int wolfSSL_preferred_group(WOLFSSL* ssl)
-{
- if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
-#ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_SERVER_END)
- return SIDE_ERROR;
- if (ssl->options.handShakeState != HANDSHAKE_DONE)
- return NOT_READY_ERROR;
-
- /* Return supported groups only. */
- return TLSX_SupportedCurve_Preferred(ssl, 1);
-#else
- return SIDE_ERROR;
-#endif
-}
-#endif
-
-/* Sets the key exchange groups in rank order on a context.
- *
- * ctx SSL/TLS context object.
- * groups Array of groups.
- * count Number of groups in array.
- * returns BAD_FUNC_ARG when ctx or groups is NULL, not using TLS v1.3 or
- * count is greater than WOLFSSL_MAX_GROUP_COUNT and WOLFSSL_SUCCESS on success.
- */
-int wolfSSL_CTX_set_groups(WOLFSSL_CTX* ctx, int* groups, int count)
-{
- int i;
-
- if (ctx == NULL || groups == NULL || count > WOLFSSL_MAX_GROUP_COUNT)
- return BAD_FUNC_ARG;
- if (!IsAtLeastTLSv1_3(ctx->method->version))
- return BAD_FUNC_ARG;
-
- for (i = 0; i < count; i++)
- ctx->group[i] = (word16)groups[i];
- ctx->numGroups = (byte)count;
-
- return WOLFSSL_SUCCESS;
-}
-
-/* Sets the key exchange groups in rank order.
- *
- * ssl SSL/TLS object.
- * groups Array of groups.
- * count Number of groups in array.
- * returns BAD_FUNC_ARG when ssl or groups is NULL, not using TLS v1.3 or
- * count is greater than WOLFSSL_MAX_GROUP_COUNT and WOLFSSL_SUCCESS on success.
- */
-int wolfSSL_set_groups(WOLFSSL* ssl, int* groups, int count)
-{
- int i;
-
- if (ssl == NULL || groups == NULL || count > WOLFSSL_MAX_GROUP_COUNT)
- return BAD_FUNC_ARG;
- if (!IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
-
- for (i = 0; i < count; i++)
- ssl->group[i] = (word16)groups[i];
- ssl->numGroups = (byte)count;
-
- return WOLFSSL_SUCCESS;
-}
-
-#ifndef NO_PSK
-void wolfSSL_CTX_set_psk_client_tls13_callback(WOLFSSL_CTX* ctx,
- wc_psk_client_tls13_callback cb)
-{
- WOLFSSL_ENTER("SSL_CTX_set_psk_client_tls13_callback");
-
- if (ctx == NULL)
- return;
-
- ctx->havePSK = 1;
- ctx->client_psk_tls13_cb = cb;
-}
-
-
-void wolfSSL_set_psk_client_tls13_callback(WOLFSSL* ssl,
- wc_psk_client_tls13_callback cb)
-{
- byte haveRSA = 1;
- int keySz = 0;
-
- WOLFSSL_ENTER("SSL_set_psk_client_tls13_callback");
-
- if (ssl == NULL)
- return;
-
- ssl->options.havePSK = 1;
- ssl->options.client_psk_tls13_cb = cb;
-
- #ifdef NO_RSA
- haveRSA = 0;
- #endif
- #ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
- #endif
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
-}
-
-
-void wolfSSL_CTX_set_psk_server_tls13_callback(WOLFSSL_CTX* ctx,
- wc_psk_server_tls13_callback cb)
-{
- WOLFSSL_ENTER("SSL_CTX_set_psk_server_tls13_callback");
- if (ctx == NULL)
- return;
- ctx->havePSK = 1;
- ctx->server_psk_tls13_cb = cb;
-}
-
-
-void wolfSSL_set_psk_server_tls13_callback(WOLFSSL* ssl,
- wc_psk_server_tls13_callback cb)
-{
- byte haveRSA = 1;
- int keySz = 0;
-
- WOLFSSL_ENTER("SSL_set_psk_server_tls13_callback");
- if (ssl == NULL)
- return;
-
- ssl->options.havePSK = 1;
- ssl->options.server_psk_tls13_cb = cb;
-
- #ifdef NO_RSA
- haveRSA = 0;
- #endif
- #ifndef NO_CERTS
- keySz = ssl->buffers.keySz;
- #endif
- InitSuites(ssl->suites, ssl->version, keySz, haveRSA, TRUE,
- ssl->options.haveDH, ssl->options.haveNTRU,
- ssl->options.haveECDSAsig, ssl->options.haveECC,
- ssl->options.haveStaticECC, ssl->options.side);
-}
-#endif
-
-
-#ifndef NO_WOLFSSL_SERVER
-/* The server accepting a connection from a client.
- * The protocol version is expecting to be TLS v1.3.
- * If the client downgrades, and older versions of the protocol are compiled
- * in, the server will fallback to wolfSSL_accept().
- * Please see note at top of README if you get an error from accept.
- *
- * ssl The SSL/TLS object.
- * returns WOLFSSL_SUCCESS on successful handshake, WOLFSSL_FATAL_ERROR when
- * unrecoverable error occurs and 0 otherwise.
- * For more error information use wolfSSL_get_error().
- */
-int wolfSSL_accept_TLSv13(WOLFSSL* ssl)
-{
- word16 havePSK = 0;
- WOLFSSL_ENTER("SSL_accept_TLSv13()");
-
-#ifdef HAVE_ERRNO_H
- errno = 0;
-#endif
-
-#if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
- havePSK = ssl->options.havePSK;
-#endif
- (void)havePSK;
-
- if (ssl->options.side != WOLFSSL_SERVER_END) {
- WOLFSSL_ERROR(ssl->error = SIDE_ERROR);
- return WOLFSSL_FATAL_ERROR;
- }
-
-#ifndef NO_CERTS
- /* allow no private key if using PK callbacks and CB is set */
- if (!havePSK) {
- if (!ssl->buffers.certificate ||
- !ssl->buffers.certificate->buffer) {
-
- WOLFSSL_MSG("accept error: server cert required");
- WOLFSSL_ERROR(ssl->error = NO_PRIVATE_KEY);
- return WOLFSSL_FATAL_ERROR;
- }
-
- #ifdef HAVE_PK_CALLBACKS
- if (wolfSSL_CTX_IsPrivatePkSet(ssl->ctx)) {
- WOLFSSL_MSG("Using PK for server private key");
- }
- else
- #endif
- if (!ssl->buffers.key || !ssl->buffers.key->buffer) {
- WOLFSSL_MSG("accept error: server key required");
- WOLFSSL_ERROR(ssl->error = NO_PRIVATE_KEY);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif
-
- if (ssl->buffers.outputBuffer.length > 0
- #ifdef WOLFSSL_ASYNC_CRYPT
- /* do not send buffered or advance state if last error was an
- async pending operation */
- && ssl->error != WC_PENDING_E
- #endif
- ) {
- if ((ssl->error = SendBuffered(ssl)) == 0) {
- /* fragOffset is non-zero when sending fragments. On the last
- * fragment, fragOffset is zero again, and the state can be
- * advanced. */
- if (ssl->fragOffset == 0) {
- ssl->options.acceptState++;
- WOLFSSL_MSG("accept state: "
- "Advanced from last buffered fragment send");
- }
- else {
- WOLFSSL_MSG("accept state: "
- "Not advanced, more fragments to send");
- }
- }
- else {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- switch (ssl->options.acceptState) {
-
-#ifdef HAVE_SECURE_RENEGOTIATION
- case TLS13_ACCEPT_BEGIN_RENEG:
-#endif
- case TLS13_ACCEPT_BEGIN :
- /* get client_hello */
- while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) {
- if ((ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- ssl->options.acceptState = TLS13_ACCEPT_CLIENT_HELLO_DONE;
- WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE");
- if (!IsAtLeastTLSv1_3(ssl->version))
- return wolfSSL_accept(ssl);
- FALL_THROUGH;
-
- case TLS13_ACCEPT_CLIENT_HELLO_DONE :
-#ifdef WOLFSSL_TLS13_DRAFT_18
- if (ssl->options.serverState ==
- SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
- if ((ssl->error = SendTls13HelloRetryRequest(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- ssl->options.acceptState = TLS13_ACCEPT_FIRST_REPLY_DONE;
- WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE");
- FALL_THROUGH;
-
- case TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE :
-#else
- if (ssl->options.serverState ==
- SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
- if ((ssl->error = SendTls13ServerHello(ssl,
- hello_retry_request)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-
- ssl->options.acceptState = TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE;
- WOLFSSL_MSG("accept state ACCEPT_HELLO_RETRY_REQUEST_DONE");
- FALL_THROUGH;
-
- case TLS13_ACCEPT_HELLO_RETRY_REQUEST_DONE :
- #ifdef WOLFSSL_TLS13_MIDDLEBOX_COMPAT
- if (ssl->options.serverState ==
- SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
- if ((ssl->error = SendChangeCipher(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.sentChangeCipher = 1;
- ssl->options.serverState = SERVER_HELLO_RETRY_REQUEST_COMPLETE;
- }
- #endif
- ssl->options.acceptState = TLS13_ACCEPT_FIRST_REPLY_DONE;
- WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE");
- FALL_THROUGH;
-#endif
-
- case TLS13_ACCEPT_FIRST_REPLY_DONE :
- if (ssl->options.serverState ==
- SERVER_HELLO_RETRY_REQUEST_COMPLETE) {
- ssl->options.clientState = CLIENT_HELLO_RETRY;
- while (ssl->options.clientState < CLIENT_HELLO_COMPLETE) {
- if ((ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
- }
-
- ssl->options.acceptState = TLS13_ACCEPT_SECOND_REPLY_DONE;
- WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE");
- FALL_THROUGH;
-
- case TLS13_ACCEPT_SECOND_REPLY_DONE :
- if ((ssl->error = SendTls13ServerHello(ssl, server_hello)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.acceptState = TLS13_SERVER_HELLO_SENT;
- WOLFSSL_MSG("accept state SERVER_HELLO_SENT");
- FALL_THROUGH;
-
- case TLS13_SERVER_HELLO_SENT :
- #if !defined(WOLFSSL_TLS13_DRAFT_18) && \
- defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
- if (!ssl->options.sentChangeCipher) {
- if ((ssl->error = SendChangeCipher(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.sentChangeCipher = 1;
- }
- #endif
-
- ssl->options.acceptState = TLS13_ACCEPT_THIRD_REPLY_DONE;
- WOLFSSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE");
- FALL_THROUGH;
-
- case TLS13_ACCEPT_THIRD_REPLY_DONE :
- if (!ssl->options.noPskDheKe) {
- ssl->error = TLSX_KeyShare_DeriveSecret(ssl);
- if (ssl->error != 0)
- return WOLFSSL_FATAL_ERROR;
- }
-
- if ((ssl->error = SendTls13EncryptedExtensions(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- ssl->options.acceptState = TLS13_SERVER_EXTENSIONS_SENT;
- WOLFSSL_MSG("accept state SERVER_EXTENSIONS_SENT");
- FALL_THROUGH;
-
- case TLS13_SERVER_EXTENSIONS_SENT :
-#ifndef NO_CERTS
- if (!ssl->options.resuming) {
- if (ssl->options.verifyPeer) {
- ssl->error = SendTls13CertificateRequest(ssl, NULL, 0);
- if (ssl->error != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
- }
-#endif
- ssl->options.acceptState = TLS13_CERT_REQ_SENT;
- WOLFSSL_MSG("accept state CERT_REQ_SENT");
- FALL_THROUGH;
-
- case TLS13_CERT_REQ_SENT :
-#ifndef NO_CERTS
- if (!ssl->options.resuming && ssl->options.sendVerify) {
- if ((ssl->error = SendTls13Certificate(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif
- ssl->options.acceptState = TLS13_CERT_SENT;
- WOLFSSL_MSG("accept state CERT_SENT");
- FALL_THROUGH;
-
- case TLS13_CERT_SENT :
-#ifndef NO_CERTS
- if (!ssl->options.resuming && ssl->options.sendVerify) {
- if ((ssl->error = SendTls13CertificateVerify(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif
- ssl->options.acceptState = TLS13_CERT_VERIFY_SENT;
- WOLFSSL_MSG("accept state CERT_VERIFY_SENT");
- FALL_THROUGH;
-
- case TLS13_CERT_VERIFY_SENT :
- if ((ssl->error = SendTls13Finished(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
-
- ssl->options.acceptState = TLS13_ACCEPT_FINISHED_SENT;
- WOLFSSL_MSG("accept state ACCEPT_FINISHED_SENT");
-#ifdef WOLFSSL_EARLY_DATA
- if (ssl->earlyData != no_early_data) {
- ssl->options.handShakeState = SERVER_FINISHED_COMPLETE;
- return WOLFSSL_SUCCESS;
- }
-#endif
- FALL_THROUGH;
-
- case TLS13_ACCEPT_FINISHED_SENT :
-#ifdef HAVE_SESSION_TICKET
- #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED
- if (!ssl->options.verifyPeer && !ssl->options.noTicketTls13 &&
- ssl->ctx->ticketEncCb != NULL) {
- if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
-#endif /* HAVE_SESSION_TICKET */
- ssl->options.acceptState = TLS13_PRE_TICKET_SENT;
- WOLFSSL_MSG("accept state TICKET_SENT");
- FALL_THROUGH;
-
- case TLS13_PRE_TICKET_SENT :
- while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
- if ( (ssl->error = ProcessReply(ssl)) < 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
-
- ssl->options.acceptState = TLS13_ACCEPT_FINISHED_DONE;
- WOLFSSL_MSG("accept state ACCEPT_FINISHED_DONE");
- FALL_THROUGH;
-
- case TLS13_ACCEPT_FINISHED_DONE :
-#ifdef HAVE_SESSION_TICKET
- #ifdef WOLFSSL_TLS13_TICKET_BEFORE_FINISHED
- if (!ssl->options.verifyPeer) {
- }
- else
- #endif
- if (!ssl->options.noTicketTls13 && ssl->ctx->ticketEncCb != NULL) {
- if ((ssl->error = SendTls13NewSessionTicket(ssl)) != 0) {
- WOLFSSL_ERROR(ssl->error);
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif /* HAVE_SESSION_TICKET */
- ssl->options.acceptState = TLS13_TICKET_SENT;
- WOLFSSL_MSG("accept state TICKET_SENT");
- FALL_THROUGH;
-
- case TLS13_TICKET_SENT :
-#ifndef NO_HANDSHAKE_DONE_CB
- if (ssl->hsDoneCb) {
- int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx);
- if (cbret < 0) {
- ssl->error = cbret;
- WOLFSSL_MSG("HandShake Done Cb don't continue error");
- return WOLFSSL_FATAL_ERROR;
- }
- }
-#endif /* NO_HANDSHAKE_DONE_CB */
-
- if (!ssl->options.keepResources) {
- FreeHandshakeResources(ssl);
- }
-
- WOLFSSL_LEAVE("SSL_accept()", WOLFSSL_SUCCESS);
- return WOLFSSL_SUCCESS;
-
- default :
- WOLFSSL_MSG("Unknown accept state ERROR");
- return WOLFSSL_FATAL_ERROR;
- }
-}
-#endif
-
-#ifdef WOLFSSL_EARLY_DATA
-/* Sets the maximum amount of early data that can be seen by server when using
- * session tickets for resumption.
- * A value of zero indicates no early data is to be sent by client using session
- * tickets.
- *
- * ctx The SSL/TLS CTX object.
- * sz Maximum size of the early data.
- * returns BAD_FUNC_ARG when ctx is NULL, SIDE_ERROR when not a server and
- * 0 on success.
- */
-int wolfSSL_CTX_set_max_early_data(WOLFSSL_CTX* ctx, unsigned int sz)
-{
- if (ctx == NULL || !IsAtLeastTLSv1_3(ctx->method->version))
- return BAD_FUNC_ARG;
- if (ctx->method->side == WOLFSSL_CLIENT_END)
- return SIDE_ERROR;
-
- ctx->maxEarlyDataSz = sz;
-
- return 0;
-}
-
-/* Sets the maximum amount of early data that can be seen by server when using
- * session tickets for resumption.
- * A value of zero indicates no early data is to be sent by client using session
- * tickets.
- *
- * ssl The SSL/TLS object.
- * sz Maximum size of the early data.
- * returns BAD_FUNC_ARG when ssl is NULL, or not using TLS v1.3,
- * SIDE_ERROR when not a server and 0 on success.
- */
-int wolfSSL_set_max_early_data(WOLFSSL* ssl, unsigned int sz)
-{
- if (ssl == NULL || !IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- return SIDE_ERROR;
-
- ssl->options.maxEarlyDataSz = sz;
-
- return 0;
-}
-
-/* Write early data to the server.
- *
- * ssl The SSL/TLS object.
- * data Early data to write
- * sz The size of the eary data in bytes.
- * outSz The number of early data bytes written.
- * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative;
- * or not using TLS v1.3. SIDE ERROR when not a server. Otherwise the number of
- * early data bytes written.
- */
-int wolfSSL_write_early_data(WOLFSSL* ssl, const void* data, int sz, int* outSz)
-{
- int ret = 0;
-
- WOLFSSL_ENTER("SSL_write_early_data()");
-
- if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL)
- return BAD_FUNC_ARG;
- if (!IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
-
-#ifndef NO_WOLFSSL_CLIENT
- if (ssl->options.side == WOLFSSL_SERVER_END)
- return SIDE_ERROR;
-
- if (ssl->options.handShakeState == NULL_STATE) {
- ssl->earlyData = expecting_early_data;
- ret = wolfSSL_connect_TLSv13(ssl);
- if (ret != WOLFSSL_SUCCESS)
- return WOLFSSL_FATAL_ERROR;
- }
- if (ssl->options.handShakeState == CLIENT_HELLO_COMPLETE) {
- ret = SendData(ssl, data, sz);
- if (ret > 0)
- *outSz = ret;
- }
-#else
- return SIDE_ERROR;
-#endif
-
- WOLFSSL_LEAVE("SSL_write_early_data()", ret);
-
- if (ret < 0)
- ret = WOLFSSL_FATAL_ERROR;
- return ret;
-}
-
-/* Read the any early data from the client.
- *
- * ssl The SSL/TLS object.
- * data Buffer to put the early data into.
- * sz The size of the buffer in bytes.
- * outSz The number of early data bytes read.
- * returns BAD_FUNC_ARG when: ssl, data or outSz is NULL; sz is negative;
- * or not using TLS v1.3. SIDE ERROR when not a server. Otherwise the number of
- * early data bytes read.
- */
-int wolfSSL_read_early_data(WOLFSSL* ssl, void* data, int sz, int* outSz)
-{
- int ret = 0;
-
- WOLFSSL_ENTER("wolfSSL_read_early_data()");
-
-
- if (ssl == NULL || data == NULL || sz < 0 || outSz == NULL)
- return BAD_FUNC_ARG;
- if (!IsAtLeastTLSv1_3(ssl->version))
- return BAD_FUNC_ARG;
-
-#ifndef NO_WOLFSSL_SERVER
- if (ssl->options.side == WOLFSSL_CLIENT_END)
- return SIDE_ERROR;
-
- if (ssl->options.handShakeState == NULL_STATE) {
- ssl->earlyData = expecting_early_data;
- ret = wolfSSL_accept_TLSv13(ssl);
- if (ret <= 0)
- return WOLFSSL_FATAL_ERROR;
- }
- if (ssl->options.handShakeState == SERVER_FINISHED_COMPLETE) {
- ret = ReceiveData(ssl, (byte*)data, sz, FALSE);
- if (ret > 0)
- *outSz = ret;
- if (ssl->error == ZERO_RETURN)
- ssl->error = WOLFSSL_ERROR_NONE;
- }
- else
- ret = 0;
-#else
- return SIDE_ERROR;
-#endif
-
- WOLFSSL_LEAVE("wolfSSL_read_early_data()", ret);
-
- if (ret < 0)
- ret = WOLFSSL_FATAL_ERROR;
- return ret;
-}
-#endif
-
-#ifdef HAVE_SECRET_CALLBACK
-int wolfSSL_set_tls13_secret_cb(WOLFSSL* ssl, Tls13SecretCb cb, void* ctx)
-{
- WOLFSSL_ENTER("wolfSSL_set_tls13_secret_cb");
- if (ssl == NULL)
- return WOLFSSL_FATAL_ERROR;
-
- ssl->tls13SecretCb = cb;
- ssl->tls13SecretCtx = ctx;
-
- return WOLFSSL_SUCCESS;
-}
-#endif
-
-#undef ERROR_OUT
-
-#endif /* !WOLFCRYPT_ONLY */
-
-#endif /* WOLFSSL_TLS13 */
diff --git a/client/wolfssl/src/wolfio.c b/client/wolfssl/src/wolfio.c
deleted file mode 100644
index b7032ca..0000000
--- a/client/wolfssl/src/wolfio.c
+++ /dev/null
@@ -1,2375 +0,0 @@
-/* wolfio.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifndef WOLFCRYPT_ONLY
-
-#ifdef _WIN32_WCE
- /* On WinCE winsock2.h must be included before windows.h for socket stuff */
- #include
-#endif
-
-#include
-#include
-#include
-
-#if defined(HAVE_HTTP_CLIENT)
- #include /* strtol() */
-#endif
-
-/*
-Possible IO enable options:
- * WOLFSSL_USER_IO: Disables default Embed* callbacks and default: off
- allows user to define their own using
- wolfSSL_CTX_SetIORecv and wolfSSL_CTX_SetIOSend
- * USE_WOLFSSL_IO: Enables the wolfSSL IO functions default: off
- * HAVE_HTTP_CLIENT: Enables HTTP client API's default: off
- (unless HAVE_OCSP or HAVE_CRL_IO defined)
- * HAVE_IO_TIMEOUT: Enables support for connect timeout default: off
- */
-
-
-/* if user writes own I/O callbacks they can define WOLFSSL_USER_IO to remove
- automatic setting of default I/O functions EmbedSend() and EmbedReceive()
- but they'll still need SetCallback xxx() at end of file
-*/
-
-#if defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT)
-
-/* Translates return codes returned from
- * send() and recv() if need be.
- */
-static WC_INLINE int TranslateReturnCode(int old, int sd)
-{
- (void)sd;
-
-#if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
- if (old == 0) {
- errno = SOCKET_EWOULDBLOCK;
- return -1; /* convert to BSD style wouldblock as error */
- }
-
- if (old < 0) {
- errno = RTCS_geterror(sd);
- if (errno == RTCSERR_TCP_CONN_CLOSING)
- return 0; /* convert to BSD style closing */
- if (errno == RTCSERR_TCP_CONN_RLSD)
- errno = SOCKET_ECONNRESET;
- if (errno == RTCSERR_TCP_TIMED_OUT)
- errno = SOCKET_EAGAIN;
- }
-#endif
-
- return old;
-}
-
-static WC_INLINE int wolfSSL_LastError(void)
-{
-#ifdef USE_WINDOWS_API
- return WSAGetLastError();
-#elif defined(EBSNET)
- return xn_getlasterror();
-#else
- return errno;
-#endif
-}
-
-#endif /* USE_WOLFSSL_IO || HAVE_HTTP_CLIENT */
-
-
-#ifdef OPENSSL_EXTRA
-/* Use the WOLFSSL read BIO for receiving data. This is set by the function
- * wolfSSL_set_bio and can also be set by wolfSSL_CTX_SetIORecv.
- *
- * ssl WOLFSSL struct passed in that has this function set as the receive
- * callback.
- * buf buffer to fill with data read
- * sz size of buf buffer
- * ctx a user set context
- *
- * returns the amount of data read or want read. See WOLFSSL_CBIO_ERR_* values.
- */
-int BioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx)
-{
- int recvd = WOLFSSL_CBIO_ERR_GENERAL;
-
- WOLFSSL_ENTER("BioReceive");
-
- if (ssl->biord == NULL) {
- WOLFSSL_MSG("WOLFSSL biord not set");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- if (ssl->biord->method && ssl->biord->method->readCb) {
- WOLFSSL_MSG("Calling custom biord");
- recvd = ssl->biord->method->readCb(ssl->biord, buf, sz);
- if (recvd < 0 && recvd != WOLFSSL_CBIO_ERR_WANT_READ)
- return WOLFSSL_CBIO_ERR_GENERAL;
- return recvd;
- }
-
- switch (ssl->biord->type) {
- case WOLFSSL_BIO_MEMORY:
- case WOLFSSL_BIO_BIO:
- if (wolfSSL_BIO_ctrl_pending(ssl->biord) == 0) {
- WOLFSSL_MSG("BIO want read");
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- recvd = wolfSSL_BIO_read(ssl->biord, buf, sz);
- if (recvd <= 0) {
- WOLFSSL_MSG("BIO general error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- break;
-
- default:
- WOLFSSL_MSG("This BIO type is unknown / unsupported");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- (void)ctx;
- return recvd;
-}
-
-
-/* Use the WOLFSSL write BIO for sending data. This is set by the function
- * wolfSSL_set_bio and can also be set by wolfSSL_CTX_SetIOSend.
- *
- * ssl WOLFSSL struct passed in that has this function set as the send callback.
- * buf buffer with data to write out
- * sz size of buf buffer
- * ctx a user set context
- *
- * returns the amount of data sent or want send. See WOLFSSL_CBIO_ERR_* values.
- */
-int BioSend(WOLFSSL* ssl, char *buf, int sz, void *ctx)
-{
- int sent = WOLFSSL_CBIO_ERR_GENERAL;
-
- WOLFSSL_ENTER("BioSend");
-
- if (ssl->biowr == NULL) {
- WOLFSSL_MSG("WOLFSSL biowr not set");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- if (ssl->biowr->method && ssl->biowr->method->writeCb) {
- WOLFSSL_MSG("Calling custom biowr");
- sent = ssl->biowr->method->writeCb(ssl->biowr, buf, sz);
- if (sent < 0) {
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- return sent;
- }
-
- switch (ssl->biowr->type) {
- case WOLFSSL_BIO_MEMORY:
- case WOLFSSL_BIO_BIO:
- sent = wolfSSL_BIO_write(ssl->biowr, buf, sz);
- if (sent < 0) {
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- break;
-
- default:
- WOLFSSL_MSG("This BIO type is unknown / unsupported");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- (void)ctx;
-
- return sent;
-}
-#endif
-
-
-#ifdef USE_WOLFSSL_IO
-
-/* The receive embedded callback
- * return : nb bytes read, or error
- */
-int EmbedReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
-{
- int sd = *(int*)ctx;
- int recvd;
-
- recvd = wolfIO_Recv(sd, buf, sz, ssl->rflags);
- if (recvd < 0) {
- int err = wolfSSL_LastError();
- WOLFSSL_MSG("Embed Receive error");
-
- if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
- WOLFSSL_MSG("\tWould block");
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- else if (err == SOCKET_ECONNRESET) {
- WOLFSSL_MSG("\tConnection reset");
- return WOLFSSL_CBIO_ERR_CONN_RST;
- }
- else if (err == SOCKET_EINTR) {
- WOLFSSL_MSG("\tSocket interrupted");
- return WOLFSSL_CBIO_ERR_ISR;
- }
- else if (err == SOCKET_ECONNABORTED) {
- WOLFSSL_MSG("\tConnection aborted");
- return WOLFSSL_CBIO_ERR_CONN_CLOSE;
- }
- else {
- WOLFSSL_MSG("\tGeneral error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- }
- else if (recvd == 0) {
- WOLFSSL_MSG("Embed receive connection closed");
- return WOLFSSL_CBIO_ERR_CONN_CLOSE;
- }
-
- return recvd;
-}
-
-/* The send embedded callback
- * return : nb bytes sent, or error
- */
-int EmbedSend(WOLFSSL* ssl, char *buf, int sz, void *ctx)
-{
- int sd = *(int*)ctx;
- int sent;
-
-#ifdef WOLFSSL_MAX_SEND_SZ
- if (sz > WOLFSSL_MAX_SEND_SZ)
- sz = WOLFSSL_MAX_SEND_SZ;
-#endif
-
- sent = wolfIO_Send(sd, buf, sz, ssl->wflags);
- if (sent < 0) {
- int err = wolfSSL_LastError();
- WOLFSSL_MSG("Embed Send error");
-
- if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
- WOLFSSL_MSG("\tWould Block");
- return WOLFSSL_CBIO_ERR_WANT_WRITE;
- }
- else if (err == SOCKET_ECONNRESET) {
- WOLFSSL_MSG("\tConnection reset");
- return WOLFSSL_CBIO_ERR_CONN_RST;
- }
- else if (err == SOCKET_EINTR) {
- WOLFSSL_MSG("\tSocket interrupted");
- return WOLFSSL_CBIO_ERR_ISR;
- }
- else if (err == SOCKET_EPIPE) {
- WOLFSSL_MSG("\tSocket EPIPE");
- return WOLFSSL_CBIO_ERR_CONN_CLOSE;
- }
- else {
- WOLFSSL_MSG("\tGeneral error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- }
-
- return sent;
-}
-
-
-#ifdef WOLFSSL_DTLS
-
-#include
-
-#define SENDTO_FUNCTION sendto
-#define RECVFROM_FUNCTION recvfrom
-
-
-/* The receive embedded callback
- * return : nb bytes read, or error
- */
-int EmbedReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
-{
- WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx;
- int recvd;
- int err;
- int sd = dtlsCtx->rfd;
- int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
- SOCKADDR_S peer;
- XSOCKLENT peerSz = sizeof(peer);
-
- WOLFSSL_ENTER("EmbedReceiveFrom()");
-
- if (ssl->options.handShakeDone)
- dtls_timeout = 0;
-
- if (!wolfSSL_get_using_nonblock(ssl)) {
- #ifdef USE_WINDOWS_API
- DWORD timeout = dtls_timeout * 1000;
- #else
- struct timeval timeout;
- XMEMSET(&timeout, 0, sizeof(timeout));
- timeout.tv_sec = dtls_timeout;
- #endif
- if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,
- sizeof(timeout)) != 0) {
- WOLFSSL_MSG("setsockopt rcvtimeo failed");
- }
- }
-
- recvd = (int)RECVFROM_FUNCTION(sd, buf, sz, ssl->rflags,
- (SOCKADDR*)&peer, &peerSz);
-
- recvd = TranslateReturnCode(recvd, sd);
-
- if (recvd < 0) {
- err = wolfSSL_LastError();
- WOLFSSL_MSG("Embed Receive From error");
-
- if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
- if (wolfSSL_dtls_get_using_nonblock(ssl)) {
- WOLFSSL_MSG("\tWould block");
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- else {
- WOLFSSL_MSG("\tSocket timeout");
- return WOLFSSL_CBIO_ERR_TIMEOUT;
- }
- }
- else if (err == SOCKET_ECONNRESET) {
- WOLFSSL_MSG("\tConnection reset");
- return WOLFSSL_CBIO_ERR_CONN_RST;
- }
- else if (err == SOCKET_EINTR) {
- WOLFSSL_MSG("\tSocket interrupted");
- return WOLFSSL_CBIO_ERR_ISR;
- }
- else if (err == SOCKET_ECONNREFUSED) {
- WOLFSSL_MSG("\tConnection refused");
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- else {
- WOLFSSL_MSG("\tGeneral error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- }
- else {
- if (dtlsCtx->peer.sz > 0
- && peerSz != (XSOCKLENT)dtlsCtx->peer.sz
- && XMEMCMP(&peer, dtlsCtx->peer.sa, peerSz) != 0) {
- WOLFSSL_MSG(" Ignored packet from invalid peer");
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- }
-
- return recvd;
-}
-
-
-/* The send embedded callback
- * return : nb bytes sent, or error
- */
-int EmbedSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx)
-{
- WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx;
- int sd = dtlsCtx->wfd;
- int sent;
- int err;
-
- WOLFSSL_ENTER("EmbedSendTo()");
-
- sent = (int)SENDTO_FUNCTION(sd, buf, sz, ssl->wflags,
- (const SOCKADDR*)dtlsCtx->peer.sa,
- dtlsCtx->peer.sz);
-
- sent = TranslateReturnCode(sent, sd);
-
- if (sent < 0) {
- err = wolfSSL_LastError();
- WOLFSSL_MSG("Embed Send To error");
-
- if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
- WOLFSSL_MSG("\tWould Block");
- return WOLFSSL_CBIO_ERR_WANT_WRITE;
- }
- else if (err == SOCKET_ECONNRESET) {
- WOLFSSL_MSG("\tConnection reset");
- return WOLFSSL_CBIO_ERR_CONN_RST;
- }
- else if (err == SOCKET_EINTR) {
- WOLFSSL_MSG("\tSocket interrupted");
- return WOLFSSL_CBIO_ERR_ISR;
- }
- else if (err == SOCKET_EPIPE) {
- WOLFSSL_MSG("\tSocket EPIPE");
- return WOLFSSL_CBIO_ERR_CONN_CLOSE;
- }
- else {
- WOLFSSL_MSG("\tGeneral error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- }
-
- return sent;
-}
-
-
-#ifdef WOLFSSL_MULTICAST
-
-/* The alternate receive embedded callback for Multicast
- * return : nb bytes read, or error
- */
-int EmbedReceiveFromMcast(WOLFSSL *ssl, char *buf, int sz, void *ctx)
-{
- WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx;
- int recvd;
- int err;
- int sd = dtlsCtx->rfd;
-
- WOLFSSL_ENTER("EmbedReceiveFromMcast()");
-
- recvd = (int)RECVFROM_FUNCTION(sd, buf, sz, ssl->rflags, NULL, NULL);
-
- recvd = TranslateReturnCode(recvd, sd);
-
- if (recvd < 0) {
- err = wolfSSL_LastError();
- WOLFSSL_MSG("Embed Receive From error");
-
- if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
- if (wolfSSL_dtls_get_using_nonblock(ssl)) {
- WOLFSSL_MSG("\tWould block");
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- else {
- WOLFSSL_MSG("\tSocket timeout");
- return WOLFSSL_CBIO_ERR_TIMEOUT;
- }
- }
- else if (err == SOCKET_ECONNRESET) {
- WOLFSSL_MSG("\tConnection reset");
- return WOLFSSL_CBIO_ERR_CONN_RST;
- }
- else if (err == SOCKET_EINTR) {
- WOLFSSL_MSG("\tSocket interrupted");
- return WOLFSSL_CBIO_ERR_ISR;
- }
- else if (err == SOCKET_ECONNREFUSED) {
- WOLFSSL_MSG("\tConnection refused");
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- else {
- WOLFSSL_MSG("\tGeneral error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- }
-
- return recvd;
-}
-#endif /* WOLFSSL_MULTICAST */
-
-
-/* The DTLS Generate Cookie callback
- * return : number of bytes copied into buf, or error
- */
-int EmbedGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *ctx)
-{
- int sd = ssl->wfd;
- SOCKADDR_S peer;
- XSOCKLENT peerSz = sizeof(peer);
- byte digest[WC_SHA256_DIGEST_SIZE];
- int ret = 0;
-
- (void)ctx;
-
- XMEMSET(&peer, 0, sizeof(peer));
- if (getpeername(sd, (SOCKADDR*)&peer, &peerSz) != 0) {
- WOLFSSL_MSG("getpeername failed in EmbedGenerateCookie");
- return GEN_COOKIE_E;
- }
-
- ret = wc_Sha256Hash((byte*)&peer, peerSz, digest);
- if (ret != 0)
- return ret;
-
- if (sz > WC_SHA256_DIGEST_SIZE)
- sz = WC_SHA256_DIGEST_SIZE;
- XMEMCPY(buf, digest, sz);
-
- return sz;
-}
-
-#ifdef WOLFSSL_SESSION_EXPORT
-
- /* get the peer information in human readable form (ip, port, family)
- * default function assumes BSD sockets
- * can be overridden with wolfSSL_CTX_SetIOGetPeer
- */
- int EmbedGetPeer(WOLFSSL* ssl, char* ip, int* ipSz,
- unsigned short* port, int* fam)
- {
- SOCKADDR_S peer;
- word32 peerSz;
- int ret;
-
- if (ssl == NULL || ip == NULL || ipSz == NULL ||
- port == NULL || fam == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* get peer information stored in ssl struct */
- peerSz = sizeof(SOCKADDR_S);
- if ((ret = wolfSSL_dtls_get_peer(ssl, (void*)&peer, &peerSz))
- != WOLFSSL_SUCCESS) {
- return ret;
- }
-
- /* extract family, ip, and port */
- *fam = ((SOCKADDR_S*)&peer)->ss_family;
- switch (*fam) {
- case WOLFSSL_IP4:
- if (XINET_NTOP(*fam, &(((SOCKADDR_IN*)&peer)->sin_addr),
- ip, *ipSz) == NULL) {
- WOLFSSL_MSG("XINET_NTOP error");
- return SOCKET_ERROR_E;
- }
- *port = XNTOHS(((SOCKADDR_IN*)&peer)->sin_port);
- break;
-
- case WOLFSSL_IP6:
- #ifdef WOLFSSL_IPV6
- if (XINET_NTOP(*fam, &(((SOCKADDR_IN6*)&peer)->sin6_addr),
- ip, *ipSz) == NULL) {
- WOLFSSL_MSG("XINET_NTOP error");
- return SOCKET_ERROR_E;
- }
- *port = XNTOHS(((SOCKADDR_IN6*)&peer)->sin6_port);
- #endif /* WOLFSSL_IPV6 */
- break;
-
- default:
- WOLFSSL_MSG("Unknown family type");
- return SOCKET_ERROR_E;
- }
- ip[*ipSz - 1] = '\0'; /* make sure has terminator */
- *ipSz = (word16)XSTRLEN(ip);
-
- return WOLFSSL_SUCCESS;
- }
-
- /* set the peer information in human readable form (ip, port, family)
- * default function assumes BSD sockets
- * can be overridden with wolfSSL_CTX_SetIOSetPeer
- */
- int EmbedSetPeer(WOLFSSL* ssl, char* ip, int ipSz,
- unsigned short port, int fam)
- {
- int ret;
- SOCKADDR_S addr;
-
- /* sanity checks on arguments */
- if (ssl == NULL || ip == NULL || ipSz < 0 || ipSz > DTLS_EXPORT_IP) {
- return BAD_FUNC_ARG;
- }
-
- addr.ss_family = fam;
- switch (addr.ss_family) {
- case WOLFSSL_IP4:
- if (XINET_PTON(addr.ss_family, ip,
- &(((SOCKADDR_IN*)&addr)->sin_addr)) <= 0) {
- WOLFSSL_MSG("XINET_PTON error");
- return SOCKET_ERROR_E;
- }
- ((SOCKADDR_IN*)&addr)->sin_port = XHTONS(port);
-
- /* peer sa is free'd in SSL_ResourceFree */
- if ((ret = wolfSSL_dtls_set_peer(ssl, (SOCKADDR_IN*)&addr,
- sizeof(SOCKADDR_IN)))!= WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Import DTLS peer info error");
- return ret;
- }
- break;
-
- case WOLFSSL_IP6:
- #ifdef WOLFSSL_IPV6
- if (XINET_PTON(addr.ss_family, ip,
- &(((SOCKADDR_IN6*)&addr)->sin6_addr)) <= 0) {
- WOLFSSL_MSG("XINET_PTON error");
- return SOCKET_ERROR_E;
- }
- ((SOCKADDR_IN6*)&addr)->sin6_port = XHTONS(port);
-
- /* peer sa is free'd in SSL_ResourceFree */
- if ((ret = wolfSSL_dtls_set_peer(ssl, (SOCKADDR_IN6*)&addr,
- sizeof(SOCKADDR_IN6)))!= WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("Import DTLS peer info error");
- return ret;
- }
- #endif /* WOLFSSL_IPV6 */
- break;
-
- default:
- WOLFSSL_MSG("Unknown address family");
- return BUFFER_E;
- }
-
- return WOLFSSL_SUCCESS;
- }
-#endif /* WOLFSSL_SESSION_EXPORT */
-#endif /* WOLFSSL_DTLS */
-
-
-int wolfIO_Recv(SOCKET_T sd, char *buf, int sz, int rdFlags)
-{
- int recvd;
-
- recvd = (int)RECV_FUNCTION(sd, buf, sz, rdFlags);
- recvd = TranslateReturnCode(recvd, sd);
-
- return recvd;
-}
-
-int wolfIO_Send(SOCKET_T sd, char *buf, int sz, int wrFlags)
-{
- int sent;
-
- sent = (int)SEND_FUNCTION(sd, buf, sz, wrFlags);
- sent = TranslateReturnCode(sent, sd);
-
- return sent;
-}
-
-#endif /* USE_WOLFSSL_IO */
-
-
-#ifdef HAVE_HTTP_CLIENT
-
-#ifndef HAVE_IO_TIMEOUT
- #define io_timeout_sec 0
-#else
-
- #ifndef DEFAULT_TIMEOUT_SEC
- #define DEFAULT_TIMEOUT_SEC 0 /* no timeout */
- #endif
-
- static int io_timeout_sec = DEFAULT_TIMEOUT_SEC;
-
- void wolfIO_SetTimeout(int to_sec)
- {
- io_timeout_sec = to_sec;
- }
-
- int wolfIO_SetBlockingMode(SOCKET_T sockfd, int non_blocking)
- {
- int ret = 0;
-
- #ifdef USE_WINDOWS_API
- unsigned long blocking = non_blocking;
- ret = ioctlsocket(sockfd, FIONBIO, &blocking);
- if (ret == SOCKET_ERROR)
- ret = -1;
- #else
- ret = fcntl(sockfd, F_GETFL, 0);
- if (ret >= 0) {
- if (non_blocking)
- ret |= O_NONBLOCK;
- else
- ret &= ~O_NONBLOCK;
- ret = fcntl(sockfd, F_SETFL, ret);
- }
- #endif
- if (ret < 0) {
- WOLFSSL_MSG("wolfIO_SetBlockingMode failed");
- }
-
- return ret;
- }
-
- int wolfIO_Select(SOCKET_T sockfd, int to_sec)
- {
- fd_set rfds, wfds;
- int nfds = 0;
- struct timeval timeout = { (to_sec > 0) ? to_sec : 0, 0};
- int ret;
-
- #ifndef USE_WINDOWS_API
- nfds = (int)sockfd + 1;
- #endif
-
- FD_ZERO(&rfds);
- FD_SET(sockfd, &rfds);
- wfds = rfds;
-
- ret = select(nfds, &rfds, &wfds, NULL, &timeout);
- if (ret == 0) {
- #ifdef DEBUG_HTTP
- printf("Timeout: %d\n", ret);
- #endif
- return HTTP_TIMEOUT;
- }
- else if (ret > 0) {
- if (FD_ISSET(sockfd, &wfds)) {
- if (!FD_ISSET(sockfd, &rfds)) {
- return 0;
- }
- }
- }
- return SOCKET_ERROR_E;
- }
-#endif /* HAVE_IO_TIMEOUT */
-
-static int wolfIO_Word16ToString(char* d, word16 number)
-{
- int i = 0;
- word16 order = 10000;
- word16 digit;
-
- if (d == NULL)
- return i;
-
- if (number == 0)
- d[i++] = '0';
- else {
- while (order) {
- digit = number / order;
- if (i > 0 || digit != 0)
- d[i++] = (char)digit + '0';
- if (digit != 0)
- number %= digit * order;
-
- order = (order > 1) ? order / 10 : 0;
- }
- }
- d[i] = 0; /* null terminate */
-
- return i;
-}
-
-int wolfIO_TcpConnect(SOCKET_T* sockfd, const char* ip, word16 port, int to_sec)
-{
-#ifdef HAVE_SOCKADDR
- int ret = 0;
- SOCKADDR_S addr;
- int sockaddr_len = sizeof(SOCKADDR_IN);
- /* use gethostbyname for c99 */
-#if defined(HAVE_GETADDRINFO) && !defined(WOLF_C99)
- ADDRINFO hints;
- ADDRINFO* answer = NULL;
- char strPort[6];
-#else
- HOSTENT* entry;
- SOCKADDR_IN *sin;
-#endif
-
- XMEMSET(&addr, 0, sizeof(addr));
-
-#ifdef WOLFIO_DEBUG
- printf("TCP Connect: %s:%d\n", ip, port);
-#endif
-
- /* use gethostbyname for c99 */
-#if defined(HAVE_GETADDRINFO) && !defined(WOLF_C99)
- XMEMSET(&hints, 0, sizeof(hints));
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
-
- if (wolfIO_Word16ToString(strPort, port) == 0) {
- WOLFSSL_MSG("invalid port number for responder");
- return -1;
- }
-
- if (getaddrinfo(ip, strPort, &hints, &answer) < 0 || answer == NULL) {
- WOLFSSL_MSG("no addr info for responder");
- return -1;
- }
-
- sockaddr_len = answer->ai_addrlen;
- XMEMCPY(&addr, answer->ai_addr, sockaddr_len);
- freeaddrinfo(answer);
-#else
- entry = gethostbyname(ip);
- sin = (SOCKADDR_IN *)&addr;
-
- if (entry) {
- sin->sin_family = AF_INET;
- sin->sin_port = XHTONS(port);
- XMEMCPY(&sin->sin_addr.s_addr, entry->h_addr_list[0], entry->h_length);
- }
- else {
- WOLFSSL_MSG("no addr info for responder");
- return -1;
- }
-#endif
-
- *sockfd = (SOCKET_T)socket(addr.ss_family, SOCK_STREAM, 0);
-
-#ifdef USE_WINDOWS_API
- if (*sockfd == INVALID_SOCKET) {
- WOLFSSL_MSG("bad socket fd, out of fds?");
- return -1;
- }
-#else
- if (*sockfd < 0) {
- WOLFSSL_MSG("bad socket fd, out of fds?");
- return -1;
- }
-#endif
-
-#ifdef HAVE_IO_TIMEOUT
- /* if timeout value provided then set socket non-blocking */
- if (to_sec > 0) {
- wolfIO_SetBlockingMode(*sockfd, 1);
- }
-#else
- (void)to_sec;
-#endif
-
- ret = connect(*sockfd, (SOCKADDR *)&addr, sockaddr_len);
-#ifdef HAVE_IO_TIMEOUT
- if (ret != 0) {
- if ((errno == EINPROGRESS) && (to_sec > 0)) {
- /* wait for connect to complete */
- ret = wolfIO_Select(*sockfd, to_sec);
-
- /* restore blocking mode */
- wolfIO_SetBlockingMode(*sockfd, 0);
- }
- }
-#endif
- if (ret != 0) {
- WOLFSSL_MSG("Responder tcp connect failed");
- return -1;
- }
- return ret;
-#else
- (void)sockfd;
- (void)ip;
- (void)port;
- (void)to_sec;
- return -1;
-#endif /* HAVE_SOCKADDR */
-}
-
-#ifndef HTTP_SCRATCH_BUFFER_SIZE
- #define HTTP_SCRATCH_BUFFER_SIZE 512
-#endif
-#ifndef MAX_URL_ITEM_SIZE
- #define MAX_URL_ITEM_SIZE 80
-#endif
-
-int wolfIO_DecodeUrl(const char* url, int urlSz, char* outName, char* outPath,
- word16* outPort)
-{
- int result = -1;
-
- if (url == NULL || urlSz == 0) {
- if (outName)
- *outName = 0;
- if (outPath)
- *outPath = 0;
- if (outPort)
- *outPort = 0;
- }
- else {
- int i, cur;
-
- /* need to break the url down into scheme, address, and port */
- /* "http://example.com:8080/" */
- /* "http://[::1]:443/" */
- if (XSTRNCMP(url, "http://", 7) == 0) {
- cur = 7;
- } else cur = 0;
-
- i = 0;
- if (url[cur] == '[') {
- cur++;
- /* copy until ']' */
- while (i < MAX_URL_ITEM_SIZE-1 && cur < urlSz && url[cur] != 0 &&
- url[cur] != ']') {
- if (outName)
- outName[i] = url[cur];
- i++; cur++;
- }
- cur++; /* skip ']' */
- }
- else {
- while (i < MAX_URL_ITEM_SIZE-1 && cur < urlSz && url[cur] != 0 &&
- url[cur] != ':' && url[cur] != '/') {
- if (outName)
- outName[i] = url[cur];
- i++; cur++;
- }
- }
- if (outName)
- outName[i] = 0;
- /* Need to pick out the path after the domain name */
-
- if (cur < urlSz && url[cur] == ':') {
- char port[6];
- int j;
- word32 bigPort = 0;
- i = 0;
- cur++;
- while (i < 6 && cur < urlSz && url[cur] != 0 && url[cur] != '/') {
- port[i] = url[cur];
- i++; cur++;
- }
-
- for (j = 0; j < i; j++) {
- if (port[j] < '0' || port[j] > '9') return -1;
- bigPort = (bigPort * 10) + (port[j] - '0');
- }
- if (outPort)
- *outPort = (word16)bigPort;
- }
- else if (outPort)
- *outPort = 80;
-
-
- if (cur < urlSz && url[cur] == '/') {
- i = 0;
- while (i < MAX_URL_ITEM_SIZE-1 && cur < urlSz && url[cur] != 0) {
- if (outPath)
- outPath[i] = url[cur];
- i++; cur++;
- }
- if (outPath)
- outPath[i] = 0;
- }
- else if (outPath) {
- outPath[0] = '/';
- outPath[1] = 0;
- }
-
- result = 0;
- }
-
- return result;
-}
-
-static int wolfIO_HttpProcessResponseBuf(int sfd, byte **recvBuf,
- int* recvBufSz, int chunkSz, char* start, int len, int dynType, void* heap)
-{
- byte* newRecvBuf = NULL;
- int newRecvSz = *recvBufSz + chunkSz;
- int pos = 0;
-
- WOLFSSL_MSG("Processing HTTP response");
-#ifdef WOLFIO_DEBUG
- printf("HTTP Chunk %d->%d\n", *recvBufSz, chunkSz);
-#endif
-
- (void)heap;
- (void)dynType;
-
- if (chunkSz < 0 || len < 0) {
- WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf invalid chunk or length size");
- return MEMORY_E;
- }
-
- if (newRecvSz <= 0) {
- WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf new receive size overflow");
- return MEMORY_E;
- }
-
- newRecvBuf = (byte*)XMALLOC(newRecvSz, heap, dynType);
- if (newRecvBuf == NULL) {
- WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf malloc failed");
- return MEMORY_E;
- }
-
- /* if buffer already exists, then we are growing it */
- if (*recvBuf) {
- XMEMCPY(&newRecvBuf[pos], *recvBuf, *recvBufSz);
- XFREE(*recvBuf, heap, dynType);
- pos += *recvBufSz;
- *recvBuf = NULL;
- }
-
- /* copy the remainder of the httpBuf into the respBuf */
- if (len != 0) {
- if (pos + len <= newRecvSz) {
- XMEMCPY(&newRecvBuf[pos], start, len);
- pos += len;
- }
- else {
- WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf bad size");
- XFREE(newRecvBuf, heap, dynType);
- return -1;
- }
- }
-
- /* receive the remainder of chunk */
- while (len < chunkSz) {
- int rxSz = wolfIO_Recv(sfd, (char*)&newRecvBuf[pos], chunkSz-len, 0);
- if (rxSz > 0) {
- len += rxSz;
- pos += rxSz;
- }
- else {
- WOLFSSL_MSG("wolfIO_HttpProcessResponseBuf recv failed");
- XFREE(newRecvBuf, heap, dynType);
- return -1;
- }
- }
-
- *recvBuf = newRecvBuf;
- *recvBufSz = newRecvSz;
-
- return 0;
-}
-
-int wolfIO_HttpProcessResponse(int sfd, const char** appStrList,
- byte** respBuf, byte* httpBuf, int httpBufSz, int dynType, void* heap)
-{
- int result = 0;
- int len = 0;
- char *start, *end;
- int respBufSz = 0;
- int isChunked = 0, chunkSz = 0;
- enum phr_state { phr_init, phr_http_start, phr_have_length, phr_have_type,
- phr_wait_end, phr_get_chunk_len, phr_get_chunk_data,
- phr_http_end
- } state = phr_init;
-
- *respBuf = NULL;
- start = end = NULL;
- do {
- if (state == phr_get_chunk_data) {
- /* get chunk of data */
- result = wolfIO_HttpProcessResponseBuf(sfd, respBuf, &respBufSz,
- chunkSz, start, len, dynType, heap);
-
- state = (result != 0) ? phr_http_end : phr_get_chunk_len;
- end = NULL;
- len = 0;
- }
-
- /* read data if no \r\n or first time */
- if (end == NULL) {
- result = wolfIO_Recv(sfd, (char*)httpBuf+len, httpBufSz-len-1, 0);
- if (result > 0) {
- len += result;
- start = (char*)httpBuf;
- start[len] = 0;
- }
- else {
- WOLFSSL_MSG("wolfIO_HttpProcessResponse recv http from peer failed");
- return -1;
- }
- }
- end = XSTRSTR(start, "\r\n"); /* locate end */
-
- /* handle incomplete rx */
- if (end == NULL) {
- if (len != 0)
- XMEMMOVE(httpBuf, start, len);
- start = end = NULL;
- }
- /* when start is "\r\n" */
- else if (end == start) {
- /* if waiting for end or need chunk len */
- if (state == phr_wait_end || state == phr_get_chunk_len) {
- state = (isChunked) ? phr_get_chunk_len : phr_http_end;
- len -= 2; start += 2; /* skip \r\n */
- }
- else {
- WOLFSSL_MSG("wolfIO_HttpProcessResponse header ended early");
- return -1;
- }
- }
- else {
- *end = 0; /* null terminate */
- len -= (int)(end - start) + 2;
- /* adjust len to remove the first line including the /r/n */
-
- #ifdef WOLFIO_DEBUG
- printf("HTTP Resp: %s\n", start);
- #endif
-
- switch (state) {
- case phr_init:
- if (XSTRLEN(start) < 15) { /* 15 is the length of the two
- constant strings we're about to
- compare against. */
- WOLFSSL_MSG("wolfIO_HttpProcessResponse HTTP header too short.");
- return -1;
- }
- if (XSTRNCASECMP(start, "HTTP/1", 6) == 0) {
- start += 9;
- if (XSTRNCASECMP(start, "200 OK", 6) != 0) {
- WOLFSSL_MSG("wolfIO_HttpProcessResponse not OK");
- return -1;
- }
- state = phr_http_start;
- }
- break;
- case phr_http_start:
- case phr_have_length:
- case phr_have_type:
- if (XSTRLEN(start) < 13) { /* 13 is the shortest of the following
- next lines we're checking for. */
- WOLFSSL_MSG("wolfIO_HttpProcessResponse content type is too short.");
- return -1;
- }
-
- if (XSTRNCASECMP(start, "Content-Type:", 13) == 0) {
- int i;
-
- start += 13;
- while (*start == ' ') start++;
-
- /* try and match against appStrList */
- i = 0;
- while (appStrList[i] != NULL) {
- if (XSTRNCASECMP(start, appStrList[i],
- XSTRLEN(appStrList[i])) == 0) {
- break;
- }
- i++;
- }
- if (appStrList[i] == NULL) {
- WOLFSSL_MSG("wolfIO_HttpProcessResponse appstr mismatch");
- return -1;
- }
- state = (state == phr_http_start) ? phr_have_type : phr_wait_end;
- }
- else if (XSTRNCASECMP(start, "Content-Length:", 15) == 0) {
- start += 15;
- while (*start == ' ') start++;
- chunkSz = XATOI(start);
- state = (state == phr_http_start) ? phr_have_length : phr_wait_end;
- }
- else if (XSTRNCASECMP(start, "Transfer-Encoding:", 18) == 0) {
- start += 18;
- while (*start == ' ') start++;
- if (XSTRNCASECMP(start, "chunked", 7) == 0) {
- isChunked = 1;
- state = (state == phr_http_start) ? phr_have_length : phr_wait_end;
- }
- }
- break;
- case phr_get_chunk_len:
- chunkSz = (int)strtol(start, NULL, 16); /* hex format */
- state = (chunkSz == 0) ? phr_http_end : phr_get_chunk_data;
- break;
- case phr_get_chunk_data:
- /* processing for chunk data done above, since \r\n isn't required */
- case phr_wait_end:
- case phr_http_end:
- /* do nothing */
- break;
- } /* switch (state) */
-
- /* skip to end plus \r\n */
- start = end + 2;
- }
- } while (state != phr_http_end);
-
- if (!isChunked) {
- result = wolfIO_HttpProcessResponseBuf(sfd, respBuf, &respBufSz, chunkSz,
- start, len, dynType, heap);
- }
-
- if (result >= 0) {
- result = respBufSz;
- }
- else {
- WOLFSSL_ERROR(result);
- }
-
- return result;
-}
-int wolfIO_HttpBuildRequest(const char *reqType, const char *domainName,
- const char *path, int pathLen, int reqSz, const char *contentType,
- byte *buf, int bufSize)
-{
- return wolfIO_HttpBuildRequest_ex(reqType, domainName, path, pathLen, reqSz, contentType, "", buf, bufSize);
-}
-
- int wolfIO_HttpBuildRequest_ex(const char *reqType, const char *domainName,
- const char *path, int pathLen, int reqSz, const char *contentType,
- const char *exHdrs, byte *buf, int bufSize)
- {
- word32 reqTypeLen, domainNameLen, reqSzStrLen, contentTypeLen, exHdrsLen, maxLen;
- char reqSzStr[6];
- char* req = (char*)buf;
- const char* blankStr = " ";
- const char* http11Str = " HTTP/1.1";
- const char* hostStr = "\r\nHost: ";
- const char* contentLenStr = "\r\nContent-Length: ";
- const char* contentTypeStr = "\r\nContent-Type: ";
- const char* singleCrLfStr = "\r\n";
- const char* doubleCrLfStr = "\r\n\r\n";
- word32 blankStrLen, http11StrLen, hostStrLen, contentLenStrLen,
- contentTypeStrLen, singleCrLfStrLen, doubleCrLfStrLen;
-
- reqTypeLen = (word32)XSTRLEN(reqType);
- domainNameLen = (word32)XSTRLEN(domainName);
- reqSzStrLen = wolfIO_Word16ToString(reqSzStr, (word16)reqSz);
- contentTypeLen = (word32)XSTRLEN(contentType);
-
- blankStrLen = (word32)XSTRLEN(blankStr);
- http11StrLen = (word32)XSTRLEN(http11Str);
- hostStrLen = (word32)XSTRLEN(hostStr);
- contentLenStrLen = (word32)XSTRLEN(contentLenStr);
- contentTypeStrLen = (word32)XSTRLEN(contentTypeStr);
-
- if(exHdrs){
- singleCrLfStrLen = (word32)XSTRLEN(singleCrLfStr);
- exHdrsLen = (word32)XSTRLEN(exHdrs);
- } else {
- singleCrLfStrLen = 0;
- exHdrsLen = 0;
- }
-
- doubleCrLfStrLen = (word32)XSTRLEN(doubleCrLfStr);
-
- /* determine max length and check it */
- maxLen =
- reqTypeLen +
- blankStrLen +
- pathLen +
- http11StrLen +
- hostStrLen +
- domainNameLen +
- contentLenStrLen +
- reqSzStrLen +
- contentTypeStrLen +
- contentTypeLen +
- singleCrLfStrLen +
- exHdrsLen +
- doubleCrLfStrLen +
- 1 /* null term */;
- if (maxLen > (word32)bufSize)
- return 0;
-
- XSTRNCPY((char*)buf, reqType, bufSize);
- buf += reqTypeLen; bufSize -= reqTypeLen;
- XSTRNCPY((char*)buf, blankStr, bufSize);
- buf += blankStrLen; bufSize -= blankStrLen;
- XSTRNCPY((char*)buf, path, bufSize);
- buf += pathLen; bufSize -= pathLen;
- XSTRNCPY((char*)buf, http11Str, bufSize);
- buf += http11StrLen; bufSize -= http11StrLen;
- if (domainNameLen > 0) {
- XSTRNCPY((char*)buf, hostStr, bufSize);
- buf += hostStrLen; bufSize -= hostStrLen;
- XSTRNCPY((char*)buf, domainName, bufSize);
- buf += domainNameLen; bufSize -= domainNameLen;
- }
- if (reqSz > 0 && reqSzStrLen > 0) {
- XSTRNCPY((char*)buf, contentLenStr, bufSize);
- buf += contentLenStrLen; bufSize -= contentLenStrLen;
- XSTRNCPY((char*)buf, reqSzStr, bufSize);
- buf += reqSzStrLen; bufSize -= reqSzStrLen;
- }
- if (contentTypeLen > 0) {
- XSTRNCPY((char*)buf, contentTypeStr, bufSize);
- buf += contentTypeStrLen; bufSize -= contentTypeStrLen;
- XSTRNCPY((char*)buf, contentType, bufSize);
- buf += contentTypeLen; bufSize -= contentTypeLen;
- }
- if (exHdrsLen > 0)
- {
- XSTRNCPY((char *)buf, singleCrLfStr, bufSize);
- buf += singleCrLfStrLen;
- bufSize -= singleCrLfStrLen;
- XSTRNCPY((char *)buf, exHdrs, bufSize);
- buf += exHdrsLen;
- bufSize -= exHdrsLen;
- }
- XSTRNCPY((char*)buf, doubleCrLfStr, bufSize);
- buf += doubleCrLfStrLen;
-
-#ifdef WOLFIO_DEBUG
- printf("HTTP %s: %s", reqType, req);
-#endif
-
- /* calculate actual length based on original and new pointer */
- return (int)((char*)buf - req);
-}
-
-
-#ifdef HAVE_OCSP
-
-int wolfIO_HttpBuildRequestOcsp(const char* domainName, const char* path,
- int ocspReqSz, byte* buf, int bufSize)
-{
- const char *cacheCtl = "Cache-Control: no-cache";
- return wolfIO_HttpBuildRequest_ex("POST", domainName, path, (int)XSTRLEN(path),
- ocspReqSz, "application/ocsp-request", cacheCtl, buf, bufSize);
-}
-
-/* return: >0 OCSP Response Size
- * -1 error */
-int wolfIO_HttpProcessResponseOcsp(int sfd, byte** respBuf,
- byte* httpBuf, int httpBufSz, void* heap)
-{
- const char* appStrList[] = {
- "application/ocsp-response",
- NULL
- };
-
- return wolfIO_HttpProcessResponse(sfd, appStrList,
- respBuf, httpBuf, httpBufSz, DYNAMIC_TYPE_OCSP, heap);
-}
-
-/* in default wolfSSL callback ctx is the heap pointer */
-int EmbedOcspLookup(void* ctx, const char* url, int urlSz,
- byte* ocspReqBuf, int ocspReqSz, byte** ocspRespBuf)
-{
- SOCKET_T sfd = 0;
- word16 port;
- int ret = -1;
-#ifdef WOLFSSL_SMALL_STACK
- char* path;
- char* domainName;
-#else
- char path[MAX_URL_ITEM_SIZE];
- char domainName[MAX_URL_ITEM_SIZE];
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- path = (char*)XMALLOC(MAX_URL_ITEM_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (path == NULL)
- return MEMORY_E;
-
- domainName = (char*)XMALLOC(MAX_URL_ITEM_SIZE, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (domainName == NULL) {
- XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#endif
-
- if (ocspReqBuf == NULL || ocspReqSz == 0) {
- WOLFSSL_MSG("OCSP request is required for lookup");
- }
- else if (ocspRespBuf == NULL) {
- WOLFSSL_MSG("Cannot save OCSP response");
- }
- else if (wolfIO_DecodeUrl(url, urlSz, domainName, path, &port) < 0) {
- WOLFSSL_MSG("Unable to decode OCSP URL");
- }
- else {
- /* Note, the library uses the EmbedOcspRespFree() callback to
- * free this buffer. */
- int httpBufSz = HTTP_SCRATCH_BUFFER_SIZE;
- byte* httpBuf = (byte*)XMALLOC(httpBufSz, ctx, DYNAMIC_TYPE_OCSP);
-
- if (httpBuf == NULL) {
- WOLFSSL_MSG("Unable to create OCSP response buffer");
- }
- else {
- httpBufSz = wolfIO_HttpBuildRequestOcsp(domainName, path, ocspReqSz,
- httpBuf, httpBufSz);
-
- ret = wolfIO_TcpConnect(&sfd, domainName, port, io_timeout_sec);
- if ((ret != 0) || ((int)sfd < 0)) {
- WOLFSSL_MSG("OCSP Responder connection failed");
- }
- else if (wolfIO_Send(sfd, (char*)httpBuf, httpBufSz, 0) !=
- httpBufSz) {
- WOLFSSL_MSG("OCSP http request failed");
- }
- else if (wolfIO_Send(sfd, (char*)ocspReqBuf, ocspReqSz, 0) !=
- ocspReqSz) {
- WOLFSSL_MSG("OCSP ocsp request failed");
- }
- else {
- ret = wolfIO_HttpProcessResponseOcsp(sfd, ocspRespBuf, httpBuf,
- HTTP_SCRATCH_BUFFER_SIZE, ctx);
- }
-
- CloseSocket(sfd);
- XFREE(httpBuf, ctx, DYNAMIC_TYPE_OCSP);
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(path, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(domainName, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return ret;
-}
-
-/* in default callback ctx is heap hint */
-void EmbedOcspRespFree(void* ctx, byte *resp)
-{
- if (resp)
- XFREE(resp, ctx, DYNAMIC_TYPE_OCSP);
-
- (void)ctx;
-}
-#endif /* HAVE_OCSP */
-
-
-#if defined(HAVE_CRL) && defined(HAVE_CRL_IO)
-
-int wolfIO_HttpBuildRequestCrl(const char* url, int urlSz,
- const char* domainName, byte* buf, int bufSize)
-{
- const char *cacheCtl = "Cache-Control: no-cache";
- return wolfIO_HttpBuildRequest_ex("GET", domainName, url, urlSz, 0, "",
- cacheCtl, buf, bufSize);
-}
-
-int wolfIO_HttpProcessResponseCrl(WOLFSSL_CRL* crl, int sfd, byte* httpBuf,
- int httpBufSz)
-{
- int result;
- byte *respBuf = NULL;
-
- const char* appStrList[] = {
- "application/pkix-crl",
- "application/x-pkcs7-crl",
- NULL
- };
-
- result = wolfIO_HttpProcessResponse(sfd, appStrList,
- &respBuf, httpBuf, httpBufSz, DYNAMIC_TYPE_CRL, crl->heap);
- if (result >= 0) {
- result = BufferLoadCRL(crl, respBuf, result, WOLFSSL_FILETYPE_ASN1, 0);
- }
- XFREE(respBuf, crl->heap, DYNAMIC_TYPE_CRL);
-
- return result;
-}
-
-int EmbedCrlLookup(WOLFSSL_CRL* crl, const char* url, int urlSz)
-{
- SOCKET_T sfd = 0;
- word16 port;
- int ret = -1;
-#ifdef WOLFSSL_SMALL_STACK
- char* domainName;
-#else
- char domainName[MAX_URL_ITEM_SIZE];
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- domainName = (char*)XMALLOC(MAX_URL_ITEM_SIZE, crl->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (domainName == NULL) {
- return MEMORY_E;
- }
-#endif
-
- if (wolfIO_DecodeUrl(url, urlSz, domainName, NULL, &port) < 0) {
- WOLFSSL_MSG("Unable to decode CRL URL");
- }
- else {
- int httpBufSz = HTTP_SCRATCH_BUFFER_SIZE;
- byte* httpBuf = (byte*)XMALLOC(httpBufSz, crl->heap,
- DYNAMIC_TYPE_CRL);
- if (httpBuf == NULL) {
- WOLFSSL_MSG("Unable to create CRL response buffer");
- }
- else {
- httpBufSz = wolfIO_HttpBuildRequestCrl(url, urlSz, domainName,
- httpBuf, httpBufSz);
-
- ret = wolfIO_TcpConnect(&sfd, domainName, port, io_timeout_sec);
- if ((ret != 0) || (sfd < 0)) {
- WOLFSSL_MSG("CRL connection failed");
- }
- else if (wolfIO_Send(sfd, (char*)httpBuf, httpBufSz, 0)
- != httpBufSz) {
- WOLFSSL_MSG("CRL http get failed");
- }
- else {
- ret = wolfIO_HttpProcessResponseCrl(crl, sfd, httpBuf,
- HTTP_SCRATCH_BUFFER_SIZE);
- }
-
- CloseSocket(sfd);
- XFREE(httpBuf, crl->heap, DYNAMIC_TYPE_CRL);
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(domainName, crl->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return ret;
-}
-#endif /* HAVE_CRL && HAVE_CRL_IO */
-
-#endif /* HAVE_HTTP_CLIENT */
-
-
-
-void wolfSSL_CTX_SetIORecv(WOLFSSL_CTX *ctx, CallbackIORecv CBIORecv)
-{
- if (ctx) {
- ctx->CBIORecv = CBIORecv;
- #ifdef OPENSSL_EXTRA
- ctx->cbioFlag |= WOLFSSL_CBIO_RECV;
- #endif
- }
-}
-
-
-void wolfSSL_CTX_SetIOSend(WOLFSSL_CTX *ctx, CallbackIOSend CBIOSend)
-{
- if (ctx) {
- ctx->CBIOSend = CBIOSend;
- #ifdef OPENSSL_EXTRA
- ctx->cbioFlag |= WOLFSSL_CBIO_SEND;
- #endif
- }
-}
-
-
-/* sets the IO callback to use for receives at WOLFSSL level */
-void wolfSSL_SSLSetIORecv(WOLFSSL *ssl, CallbackIORecv CBIORecv)
-{
- if (ssl) {
- ssl->CBIORecv = CBIORecv;
- #ifdef OPENSSL_EXTRA
- ssl->cbioFlag |= WOLFSSL_CBIO_RECV;
- #endif
- }
-}
-
-
-/* sets the IO callback to use for sends at WOLFSSL level */
-void wolfSSL_SSLSetIOSend(WOLFSSL *ssl, CallbackIOSend CBIOSend)
-{
- if (ssl) {
- ssl->CBIOSend = CBIOSend;
- #ifdef OPENSSL_EXTRA
- ssl->cbioFlag |= WOLFSSL_CBIO_SEND;
- #endif
- }
-}
-
-
-void wolfSSL_SetIOReadCtx(WOLFSSL* ssl, void *rctx)
-{
- if (ssl)
- ssl->IOCB_ReadCtx = rctx;
-}
-
-
-void wolfSSL_SetIOWriteCtx(WOLFSSL* ssl, void *wctx)
-{
- if (ssl)
- ssl->IOCB_WriteCtx = wctx;
-}
-
-
-void* wolfSSL_GetIOReadCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->IOCB_ReadCtx;
-
- return NULL;
-}
-
-
-void* wolfSSL_GetIOWriteCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->IOCB_WriteCtx;
-
- return NULL;
-}
-
-
-void wolfSSL_SetIOReadFlags(WOLFSSL* ssl, int flags)
-{
- if (ssl)
- ssl->rflags = flags;
-}
-
-
-void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags)
-{
- if (ssl)
- ssl->wflags = flags;
-}
-
-
-#ifdef WOLFSSL_DTLS
-
-void wolfSSL_CTX_SetGenCookie(WOLFSSL_CTX* ctx, CallbackGenCookie cb)
-{
- if (ctx)
- ctx->CBIOCookie = cb;
-}
-
-
-void wolfSSL_SetCookieCtx(WOLFSSL* ssl, void *ctx)
-{
- if (ssl)
- ssl->IOCB_CookieCtx = ctx;
-}
-
-
-void* wolfSSL_GetCookieCtx(WOLFSSL* ssl)
-{
- if (ssl)
- return ssl->IOCB_CookieCtx;
-
- return NULL;
-}
-
-#ifdef WOLFSSL_SESSION_EXPORT
-
-void wolfSSL_CTX_SetIOGetPeer(WOLFSSL_CTX* ctx, CallbackGetPeer cb)
-{
- if (ctx)
- ctx->CBGetPeer = cb;
-}
-
-
-void wolfSSL_CTX_SetIOSetPeer(WOLFSSL_CTX* ctx, CallbackSetPeer cb)
-{
- if (ctx)
- ctx->CBSetPeer = cb;
-}
-
-#endif /* WOLFSSL_SESSION_EXPORT */
-#endif /* WOLFSSL_DTLS */
-
-
-#ifdef HAVE_NETX
-
-/* The NetX receive callback
- * return : bytes read, or error
- */
-int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
-{
- NetX_Ctx* nxCtx = (NetX_Ctx*)ctx;
- ULONG left;
- ULONG total;
- ULONG copied = 0;
- UINT status;
-
- (void)ssl;
-
- if (nxCtx == NULL || nxCtx->nxSocket == NULL) {
- WOLFSSL_MSG("NetX Recv NULL parameters");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- if (nxCtx->nxPacket == NULL) {
- status = nx_tcp_socket_receive(nxCtx->nxSocket, &nxCtx->nxPacket,
- nxCtx->nxWait);
- if (status != NX_SUCCESS) {
- WOLFSSL_MSG("NetX Recv receive error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- }
-
- if (nxCtx->nxPacket) {
- status = nx_packet_length_get(nxCtx->nxPacket, &total);
- if (status != NX_SUCCESS) {
- WOLFSSL_MSG("NetX Recv length get error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- left = total - nxCtx->nxOffset;
- status = nx_packet_data_extract_offset(nxCtx->nxPacket, nxCtx->nxOffset,
- buf, sz, &copied);
- if (status != NX_SUCCESS) {
- WOLFSSL_MSG("NetX Recv data extract offset error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- nxCtx->nxOffset += copied;
-
- if (copied == left) {
- WOLFSSL_MSG("NetX Recv Drained packet");
- nx_packet_release(nxCtx->nxPacket);
- nxCtx->nxPacket = NULL;
- nxCtx->nxOffset = 0;
- }
- }
-
- return copied;
-}
-
-
-/* The NetX send callback
- * return : bytes sent, or error
- */
-int NetX_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx)
-{
- NetX_Ctx* nxCtx = (NetX_Ctx*)ctx;
- NX_PACKET* packet;
- NX_PACKET_POOL* pool; /* shorthand */
- UINT status;
-
- (void)ssl;
-
- if (nxCtx == NULL || nxCtx->nxSocket == NULL) {
- WOLFSSL_MSG("NetX Send NULL parameters");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- pool = nxCtx->nxSocket->nx_tcp_socket_ip_ptr->nx_ip_default_packet_pool;
- status = nx_packet_allocate(pool, &packet, NX_TCP_PACKET,
- nxCtx->nxWait);
- if (status != NX_SUCCESS) {
- WOLFSSL_MSG("NetX Send packet alloc error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- status = nx_packet_data_append(packet, buf, sz, pool, nxCtx->nxWait);
- if (status != NX_SUCCESS) {
- nx_packet_release(packet);
- WOLFSSL_MSG("NetX Send data append error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- status = nx_tcp_socket_send(nxCtx->nxSocket, packet, nxCtx->nxWait);
- if (status != NX_SUCCESS) {
- nx_packet_release(packet);
- WOLFSSL_MSG("NetX Send socket send error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- return sz;
-}
-
-
-/* like set_fd, but for default NetX context */
-void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxSocket, ULONG waitOption)
-{
- if (ssl) {
- ssl->nxCtx.nxSocket = nxSocket;
- ssl->nxCtx.nxWait = waitOption;
- }
-}
-
-#endif /* HAVE_NETX */
-
-
-#ifdef MICRIUM
-
-/* Micrium uTCP/IP port, using the NetSock API
- * TCP and UDP are currently supported with the callbacks below.
- *
- * WOLFSSL_SESSION_EXPORT is not yet supported, would need EmbedGetPeer()
- * and EmbedSetPeer() callbacks implemented.
- *
- * HAVE_CRL is not yet supported, would need an EmbedCrlLookup()
- * callback implemented.
- *
- * HAVE_OCSP is not yet supported, would need an EmbedOCSPLookup()
- * callback implemented.
- */
-
-/* The Micrium uTCP/IP send callback
- * return : bytes sent, or error
- */
-int MicriumSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
-{
- NET_SOCK_ID sd = *(int*)ctx;
- NET_SOCK_RTN_CODE ret;
- NET_ERR err;
-
- ret = NetSock_TxData(sd, buf, sz, ssl->wflags, &err);
- if (ret < 0) {
- WOLFSSL_MSG("Embed Send error");
-
- if (err == NET_ERR_TX) {
- WOLFSSL_MSG("\tWould block");
- return WOLFSSL_CBIO_ERR_WANT_WRITE;
-
- } else {
- WOLFSSL_MSG("\tGeneral error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- }
-
- return ret;
-}
-
-/* The Micrium uTCP/IP receive callback
- * return : nb bytes read, or error
- */
-int MicriumReceive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
-{
- NET_SOCK_ID sd = *(int*)ctx;
- NET_SOCK_RTN_CODE ret;
- NET_ERR err;
-
-#ifdef WOLFSSL_DTLS
- {
- int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
- if (wolfSSL_dtls(ssl)
- && !wolfSSL_dtls_get_using_nonblock(ssl)
- && dtls_timeout != 0) {
- /* needs timeout in milliseconds */
- NetSock_CfgTimeoutRxQ_Set(sd, dtls_timeout * 1000, &err);
- if (err != NET_SOCK_ERR_NONE) {
- WOLFSSL_MSG("NetSock_CfgTimeoutRxQ_Set failed");
- }
- }
- }
-#endif
-
- ret = NetSock_RxData(sd, buf, sz, ssl->rflags, &err);
- if (ret < 0) {
- WOLFSSL_MSG("Embed Receive error");
-
- if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY ||
- err == NET_ERR_FAULT_LOCK_ACQUIRE) {
- if (!wolfSSL_dtls(ssl) || wolfSSL_dtls_get_using_nonblock(ssl)) {
- WOLFSSL_MSG("\tWould block");
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- else {
- WOLFSSL_MSG("\tSocket timeout");
- return WOLFSSL_CBIO_ERR_TIMEOUT;
- }
-
- } else if (err == NET_SOCK_ERR_CLOSED) {
- WOLFSSL_MSG("Embed receive connection closed");
- return WOLFSSL_CBIO_ERR_CONN_CLOSE;
-
- } else {
- WOLFSSL_MSG("\tGeneral error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- }
-
- return ret;
-}
-
-/* The Micrium uTCP/IP receivefrom callback
- * return : nb bytes read, or error
- */
-int MicriumReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *ctx)
-{
- WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx;
- NET_SOCK_ID sd = dtlsCtx->rfd;
- NET_SOCK_ADDR peer;
- NET_SOCK_ADDR_LEN peerSz = sizeof(peer);
- NET_SOCK_RTN_CODE ret;
- NET_ERR err;
- int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
-
- WOLFSSL_ENTER("MicriumReceiveFrom()");
-
- if (ssl->options.handShakeDone)
- dtls_timeout = 0;
-
- if (!wolfSSL_dtls_get_using_nonblock(ssl)) {
- /* needs timeout in milliseconds */
- NetSock_CfgTimeoutRxQ_Set(sd, dtls_timeout * 1000, &err);
- if (err != NET_SOCK_ERR_NONE) {
- WOLFSSL_MSG("NetSock_CfgTimeoutRxQ_Set failed");
- }
- }
-
- ret = NetSock_RxDataFrom(sd, buf, sz, ssl->rflags, &peer, &peerSz,
- 0, 0, 0, &err);
- if (ret < 0) {
- WOLFSSL_MSG("Embed Receive From error");
-
- if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY ||
- err == NET_ERR_FAULT_LOCK_ACQUIRE) {
- if (wolfSSL_dtls_get_using_nonblock(ssl)) {
- WOLFSSL_MSG("\tWould block");
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- else {
- WOLFSSL_MSG("\tSocket timeout");
- return WOLFSSL_CBIO_ERR_TIMEOUT;
- }
- } else {
- WOLFSSL_MSG("\tGeneral error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- }
- else {
- if (dtlsCtx->peer.sz > 0
- && peerSz != (NET_SOCK_ADDR_LEN)dtlsCtx->peer.sz
- && XMEMCMP(&peer, dtlsCtx->peer.sa, peerSz) != 0) {
- WOLFSSL_MSG("\tIgnored packet from invalid peer");
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- }
-
- return ret;
-}
-
-/* The Micrium uTCP/IP sendto callback
- * return : nb bytes sent, or error
- */
-int MicriumSendTo(WOLFSSL* ssl, char *buf, int sz, void *ctx)
-{
- WOLFSSL_DTLS_CTX* dtlsCtx = (WOLFSSL_DTLS_CTX*)ctx;
- NET_SOCK_ID sd = dtlsCtx->wfd;
- NET_SOCK_RTN_CODE ret;
- NET_ERR err;
-
- WOLFSSL_ENTER("MicriumSendTo()");
-
- ret = NetSock_TxDataTo(sd, buf, sz, ssl->wflags,
- (NET_SOCK_ADDR*)dtlsCtx->peer.sa,
- (NET_SOCK_ADDR_LEN)dtlsCtx->peer.sz,
- &err);
- if (err < 0) {
- WOLFSSL_MSG("Embed Send To error");
-
- if (err == NET_ERR_TX) {
- WOLFSSL_MSG("\tWould block");
- return WOLFSSL_CBIO_ERR_WANT_WRITE;
-
- } else {
- WOLFSSL_MSG("\tGeneral error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- }
-
- return ret;
-}
-
-/* Micrium DTLS Generate Cookie callback
- * return : number of bytes copied into buf, or error
- */
-int MicriumGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *ctx)
-{
- NET_SOCK_ADDR peer;
- NET_SOCK_ADDR_LEN peerSz = sizeof(peer);
- byte digest[WC_SHA_DIGEST_SIZE];
- int ret = 0;
-
- (void)ctx;
-
- XMEMSET(&peer, 0, sizeof(peer));
- if (wolfSSL_dtls_get_peer(ssl, (void*)&peer,
- (unsigned int*)&peerSz) != WOLFSSL_SUCCESS) {
- WOLFSSL_MSG("getpeername failed in MicriumGenerateCookie");
- return GEN_COOKIE_E;
- }
-
- ret = wc_ShaHash((byte*)&peer, peerSz, digest);
- if (ret != 0)
- return ret;
-
- if (sz > WC_SHA_DIGEST_SIZE)
- sz = WC_SHA_DIGEST_SIZE;
- XMEMCPY(buf, digest, sz);
-
- return sz;
-}
-
-#endif /* MICRIUM */
-
-#if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
-
-#include
-#include
-#include
-
-#define MB_NAME "wolfssl_mb"
-
-typedef struct Mynewt_Ctx {
- struct mn_socket *mnSocket; /* send/recv socket handler */
- struct mn_sockaddr_in mnSockAddrIn; /* socket address */
- struct os_mbuf *mnPacket; /* incoming packet handle
- for short reads */
- int reading; /* reading flag */
-
- /* private */
- void *mnMemBuffer; /* memory buffer for mempool */
- struct os_mempool mnMempool; /* mempool */
- struct os_mbuf_pool mnMbufpool; /* mbuf pool */
-} Mynewt_Ctx;
-
-void mynewt_ctx_clear(void *ctx) {
- Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx;
- if(!mynewt_ctx) return;
-
- if(mynewt_ctx->mnPacket) {
- os_mbuf_free_chain(mynewt_ctx->mnPacket);
- mynewt_ctx->mnPacket = NULL;
- }
- os_mempool_clear(&mynewt_ctx->mnMempool);
- XFREE(mynewt_ctx->mnMemBuffer, 0, 0);
- XFREE(mynewt_ctx, 0, 0);
-}
-
-/* return Mynewt_Ctx instance */
-void* mynewt_ctx_new() {
- int rc = 0;
- Mynewt_Ctx *mynewt_ctx;
- int mem_buf_count = MYNEWT_VAL(WOLFSSL_MNSOCK_MEM_BUF_COUNT);
- int mem_buf_size = MYNEWT_VAL(WOLFSSL_MNSOCK_MEM_BUF_SIZE);
- int mempool_bytes = OS_MEMPOOL_BYTES(mem_buf_count, mem_buf_size);
-
- mynewt_ctx = (Mynewt_Ctx *)XMALLOC(sizeof(struct Mynewt_Ctx),
- NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if(!mynewt_ctx) return NULL;
-
- XMEMSET(mynewt_ctx, 0, sizeof(Mynewt_Ctx));
- mynewt_ctx->mnMemBuffer = XMALLOC(mempool_bytes, 0, 0);
- if(!mynewt_ctx->mnMemBuffer) {
- mynewt_ctx_clear((void*)mynewt_ctx);
- return NULL;
- }
-
- rc = os_mempool_init(&mynewt_ctx->mnMempool,
- mem_buf_count, mem_buf_size,
- mynewt_ctx->mnMemBuffer, MB_NAME);
- if(rc != 0) {
- mynewt_ctx_clear((void*)mynewt_ctx);
- return NULL;
- }
- rc = os_mbuf_pool_init(&mynewt_ctx->mnMbufpool, &mynewt_ctx->mnMempool,
- mem_buf_count, mem_buf_size);
- if(rc != 0) {
- mynewt_ctx_clear((void*)mynewt_ctx);
- return NULL;
- }
-
- return mynewt_ctx;
-}
-
-static void mynewt_sock_writable(void *arg, int err);
-static void mynewt_sock_readable(void *arg, int err);
-static const union mn_socket_cb mynewt_sock_cbs = {
- .socket.writable = mynewt_sock_writable,
- .socket.readable = mynewt_sock_readable,
-};
-static void mynewt_sock_writable(void *arg, int err)
-{
- /* do nothing */
-}
-static void mynewt_sock_readable(void *arg, int err)
-{
- Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx *)arg;
- if (err && mynewt_ctx->reading) {
- mynewt_ctx->reading = 0;
- }
-}
-
-/* The Mynewt receive callback
- * return : bytes read, or error
- */
-int Mynewt_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx)
-{
- Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx;
- int rc = 0;
- struct mn_sockaddr_in from;
- struct os_mbuf *m;
- int read_sz = 0;
- uint16_t total;
-
- if (mynewt_ctx == NULL || mynewt_ctx->mnSocket == NULL) {
- WOLFSSL_MSG("Mynewt Recv NULL parameters");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
-
- if(mynewt_ctx->mnPacket == NULL) {
- mynewt_ctx->mnPacket = os_mbuf_get_pkthdr(&mynewt_ctx->mnMbufpool, 0);
- if(mynewt_ctx->mnPacket == NULL) {
- return MEMORY_E;
- }
-
- mynewt_ctx->reading = 1;
- while(mynewt_ctx->reading && rc == 0) {
- rc = mn_recvfrom(mynewt_ctx->mnSocket, &m, (struct mn_sockaddr *) &from);
- if(rc == MN_ECONNABORTED) {
- rc = 0;
- mynewt_ctx->reading = 0;
- break;
- }
- if (!(rc == 0 || rc == MN_EAGAIN)) {
- WOLFSSL_MSG("Mynewt Recv receive error");
- mynewt_ctx->reading = 0;
- break;
- }
- if(rc == 0) {
- int len = OS_MBUF_PKTLEN(m);
- if(len == 0) {
- break;
- }
- rc = os_mbuf_appendfrom(mynewt_ctx->mnPacket, m, 0, len);
- if(rc != 0) {
- WOLFSSL_MSG("Mynewt Recv os_mbuf_appendfrom error");
- break;
- }
- os_mbuf_free_chain(m);
- m = NULL;
- } else if(rc == MN_EAGAIN) {
- /* continue to until reading all of packet data. */
- rc = 0;
- break;
- }
- }
- if(rc != 0) {
- mynewt_ctx->reading = 0;
- os_mbuf_free_chain(mynewt_ctx->mnPacket);
- mynewt_ctx->mnPacket = NULL;
- return rc;
- }
- }
-
- if(mynewt_ctx->mnPacket) {
- total = OS_MBUF_PKTLEN(mynewt_ctx->mnPacket);
- read_sz = (total >= sz)? sz : total;
-
- os_mbuf_copydata(mynewt_ctx->mnPacket, 0, read_sz, (void*)buf);
- os_mbuf_adj(mynewt_ctx->mnPacket, read_sz);
-
- if (read_sz == total) {
- WOLFSSL_MSG("Mynewt Recv Drained packet");
- os_mbuf_free_chain(mynewt_ctx->mnPacket);
- mynewt_ctx->mnPacket = NULL;
- }
- }
-
- return read_sz;
-}
-
-/* The Mynewt send callback
- * return : bytes sent, or error
- */
-int Mynewt_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx)
-{
- Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx;
- int rc = 0;
- struct os_mbuf *m;
- int write_sz = 0;
- m = os_msys_get_pkthdr(sz, 0);
- if (!m) {
- WOLFSSL_MSG("Mynewt Send os_msys_get_pkthdr error");
- return WOLFSSL_CBIO_ERR_GENERAL;
- }
- rc = os_mbuf_copyinto(m, 0, buf, sz);
- if (rc != 0) {
- WOLFSSL_MSG("Mynewt Send os_mbuf_copyinto error");
- os_mbuf_free_chain(m);
- return rc;
- }
- rc = mn_sendto(mynewt_ctx->mnSocket, m, (struct mn_sockaddr *)&mynewt_ctx->mnSockAddrIn);
- if(rc != 0) {
- WOLFSSL_MSG("Mynewt Send mn_sendto error");
- os_mbuf_free_chain(m);
- return rc;
- }
- write_sz = sz;
- return write_sz;
-}
-
-/* like set_fd, but for default NetX context */
-void wolfSSL_SetIO_Mynewt(WOLFSSL* ssl, struct mn_socket* mnSocket, struct mn_sockaddr_in* mnSockAddrIn)
-{
- if (ssl && ssl->mnCtx) {
- Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx *)ssl->mnCtx;
- mynewt_ctx->mnSocket = mnSocket;
- memcpy(&mynewt_ctx->mnSockAddrIn, mnSockAddrIn, sizeof(struct mn_sockaddr_in));
- mn_socket_set_cbs(mynewt_ctx->mnSocket, mnSocket, &mynewt_sock_cbs);
- }
-}
-
-#endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */
-
-#ifdef WOLFSSL_UIP
-#include
-#include
-
-/* uIP TCP/IP port, using the native tcp/udp socket api.
- * TCP and UDP are currently supported with the callbacks below.
- *
- */
-/* The uIP tcp send callback
- * return : bytes sent, or error
- */
-int uIPSend(WOLFSSL* ssl, char* buf, int sz, void* _ctx)
-{
- uip_wolfssl_ctx *ctx = (struct uip_wolfssl_ctx *)_ctx;
- int ret;
- unsigned int max_sendlen;
- int total_written = 0;
- (void)ssl;
- do {
- unsigned int bytes_left = sz - total_written;
- max_sendlen = tcp_socket_max_sendlen(&ctx->conn.tcp);
- if (bytes_left > max_sendlen) {
- printf("Send limited by buffer\r\n");
- bytes_left = max_sendlen;
- }
- if (bytes_left == 0) {
- printf("Buffer full!\r\n");
- break;
- }
- ret = tcp_socket_send(&ctx->conn.tcp, (unsigned char *)buf + total_written, bytes_left);
- if (ret <= 0)
- break;
- total_written += ret;
- } while(total_written < sz);
- if (total_written == 0)
- return WOLFSSL_CBIO_ERR_WANT_WRITE;
- return total_written;
-}
-
-int uIPSendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx)
-{
- uip_wolfssl_ctx *ctx = (struct uip_wolfssl_ctx *)_ctx;
- int ret = 0;
- (void)ssl;
- ret = udp_socket_sendto(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr, ctx->peer_port );
- if (ret == 0)
- return WOLFSSL_CBIO_ERR_WANT_WRITE;
- return ret;
-}
-
-/* The uIP uTCP/IP receive callback
- * return : nb bytes read, or error
- */
-int uIPReceive(WOLFSSL *ssl, char *buf, int sz, void *_ctx)
-{
- uip_wolfssl_ctx *ctx = (uip_wolfssl_ctx *)_ctx;
- if (!ctx || !ctx->ssl_rx_databuf)
- return -1;
- (void)ssl;
- if (ctx->ssl_rb_len > 0) {
- if (sz > ctx->ssl_rb_len - ctx->ssl_rb_off)
- sz = ctx->ssl_rb_len - ctx->ssl_rb_off;
- XMEMCPY(buf, ctx->ssl_rx_databuf + ctx->ssl_rb_off, sz);
- ctx->ssl_rb_off += sz;
- if (ctx->ssl_rb_off >= ctx->ssl_rb_len) {
- ctx->ssl_rb_len = 0;
- ctx->ssl_rb_off = 0;
- }
- return sz;
- } else {
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
-}
-
-/* uIP DTLS Generate Cookie callback
- * return : number of bytes copied into buf, or error
- */
-int uIPGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx)
-{
- uip_wolfssl_ctx *ctx = (uip_wolfssl_ctx *)_ctx;
- byte token[32];
- byte digest[WC_SHA_DIGEST_SIZE];
- int ret = 0;
- XMEMSET(token, 0, sizeof(token));
- XMEMCPY(token, &ctx->peer_addr, sizeof(uip_ipaddr_t));
- XMEMCPY(token + sizeof(uip_ipaddr_t), &ctx->peer_port, sizeof(word16));
- ret = wc_ShaHash(token, sizeof(uip_ipaddr_t) + sizeof(word16), digest);
- if (ret != 0)
- return ret;
- if (sz > WC_SHA_DIGEST_SIZE)
- sz = WC_SHA_DIGEST_SIZE;
- XMEMCPY(buf, digest, sz);
- return sz;
-}
-
-#endif /* WOLFSSL_UIP */
-
-#ifdef WOLFSSL_GNRC
-
-#include
-#include
-#include
-
-/* GNRC TCP/IP port, using the native tcp/udp socket api.
- * TCP and UDP are currently supported with the callbacks below.
- *
- */
-/* The GNRC tcp send callback
- * return : bytes sent, or error
- */
-
-int GNRC_SendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx)
-{
- sock_tls_t *ctx = (sock_tls_t *)_ctx;
- int ret = 0;
- (void)ssl;
- if (!ctx)
- return WOLFSSL_CBIO_ERR_GENERAL;
- ret = sock_udp_send(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr);
- if (ret == 0)
- return WOLFSSL_CBIO_ERR_WANT_WRITE;
- return ret;
-}
-
-/* The GNRC TCP/IP receive callback
- * return : nb bytes read, or error
- */
-int GNRC_ReceiveFrom(WOLFSSL *ssl, char *buf, int sz, void *_ctx)
-{
- sock_udp_ep_t ep;
- int ret;
- uint32_t timeout = wolfSSL_dtls_get_current_timeout(ssl) * 1000000;
- sock_tls_t *ctx = (sock_tls_t *)_ctx;
- if (!ctx)
- return WOLFSSL_CBIO_ERR_GENERAL;
- (void)ssl;
- if (wolfSSL_get_using_nonblock(ctx->ssl)) {
- timeout = 0;
- }
- ret = sock_udp_recv(&ctx->conn.udp, buf, sz, timeout, &ep);
- if (ret > 0) {
- if (ctx->peer_addr.port == 0)
- XMEMCPY(&ctx->peer_addr, &ep, sizeof(sock_udp_ep_t));
- }
- if (ret == -ETIMEDOUT) {
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- return ret;
-}
-
-/* GNRC DTLS Generate Cookie callback
- * return : number of bytes copied into buf, or error
- */
-#define GNRC_MAX_TOKEN_SIZE (32)
-int GNRC_GenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx)
-{
- sock_tls_t *ctx = (sock_tls_t *)_ctx;
- if (!ctx)
- return WOLFSSL_CBIO_ERR_GENERAL;
- byte token[GNRC_MAX_TOKEN_SIZE];
- byte digest[WC_SHA_DIGEST_SIZE];
- int ret = 0;
- size_t token_size = sizeof(sock_udp_ep_t);
- (void)ssl;
- if (token_size > GNRC_MAX_TOKEN_SIZE)
- token_size = GNRC_MAX_TOKEN_SIZE;
- XMEMSET(token, 0, GNRC_MAX_TOKEN_SIZE);
- XMEMCPY(token, &ctx->peer_addr, token_size);
- ret = wc_ShaHash(token, token_size, digest);
- if (ret != 0)
- return ret;
- if (sz > WC_SHA_DIGEST_SIZE)
- sz = WC_SHA_DIGEST_SIZE;
- XMEMCPY(buf, digest, sz);
- return sz;
-}
-
-#endif /* WOLFSSL_GNRC */
-
-#endif /* WOLFCRYPT_ONLY */
diff --git a/client/wolfssl/wolfcrypt/src/aes.c b/client/wolfssl/wolfcrypt/src/aes.c
deleted file mode 100644
index 4b5b437..0000000
--- a/client/wolfssl/wolfcrypt/src/aes.c
+++ /dev/null
@@ -1,8704 +0,0 @@
-/* aes.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-#include
-
-#if !defined(NO_AES)
-
-/* Tip: Locate the software cipher modes by searching for "Software AES" */
-
-#if defined(HAVE_FIPS) && \
- defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
-
- /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
- #define FIPS_NO_WRAPPERS
-
- #ifdef USE_WINDOWS_API
- #pragma code_seg(".fipsA$g")
- #pragma const_seg(".fipsB$g")
- #endif
-#endif
-
-#include
-#include
-
-#ifdef WOLF_CRYPTO_CB
- #include
-#endif
-
-
-/* fips wrapper calls, user can call direct */
-#if defined(HAVE_FIPS) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2))
-
- int wc_AesSetKey(Aes* aes, const byte* key, word32 len, const byte* iv,
- int dir)
- {
- if (aes == NULL || !( (len == 16) || (len == 24) || (len == 32)) ) {
- return BAD_FUNC_ARG;
- }
-
- return AesSetKey_fips(aes, key, len, iv, dir);
- }
- int wc_AesSetIV(Aes* aes, const byte* iv)
- {
- if (aes == NULL) {
- return BAD_FUNC_ARG;
- }
-
- return AesSetIV_fips(aes, iv);
- }
- #ifdef HAVE_AES_CBC
- int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- if (aes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
- return AesCbcEncrypt_fips(aes, out, in, sz);
- }
- #ifdef HAVE_AES_DECRYPT
- int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- if (aes == NULL || out == NULL || in == NULL
- || sz % AES_BLOCK_SIZE != 0) {
- return BAD_FUNC_ARG;
- }
-
- return AesCbcDecrypt_fips(aes, out, in, sz);
- }
- #endif /* HAVE_AES_DECRYPT */
- #endif /* HAVE_AES_CBC */
-
- /* AES-CTR */
- #ifdef WOLFSSL_AES_COUNTER
- int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- if (aes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
- return AesCtrEncrypt(aes, out, in, sz);
- }
- #endif
-
- /* AES-DIRECT */
- #if defined(WOLFSSL_AES_DIRECT)
- void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
- {
- AesEncryptDirect(aes, out, in);
- }
-
- #ifdef HAVE_AES_DECRYPT
- void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
- {
- AesDecryptDirect(aes, out, in);
- }
- #endif /* HAVE_AES_DECRYPT */
-
- int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
- const byte* iv, int dir)
- {
- return AesSetKeyDirect(aes, key, len, iv, dir);
- }
- #endif /* WOLFSSL_AES_DIRECT */
-
- /* AES-GCM */
- #ifdef HAVE_AESGCM
- int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
- {
- if (aes == NULL || !( (len == 16) || (len == 24) || (len == 32)) ) {
- return BAD_FUNC_ARG;
- }
-
- return AesGcmSetKey_fips(aes, key, len);
- }
- int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
- {
- if (aes == NULL || authTagSz > AES_BLOCK_SIZE ||
- authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ ||
- ivSz == 0 || ivSz > AES_BLOCK_SIZE) {
- return BAD_FUNC_ARG;
- }
-
- return AesGcmEncrypt_fips(aes, out, in, sz, iv, ivSz, authTag,
- authTagSz, authIn, authInSz);
- }
-
- #ifdef HAVE_AES_DECRYPT
- int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- const byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
- {
- if (aes == NULL || out == NULL || in == NULL || iv == NULL
- || authTag == NULL || authTagSz > AES_BLOCK_SIZE ||
- ivSz == 0 || ivSz > AES_BLOCK_SIZE) {
- return BAD_FUNC_ARG;
- }
-
- return AesGcmDecrypt_fips(aes, out, in, sz, iv, ivSz, authTag,
- authTagSz, authIn, authInSz);
- }
- #endif /* HAVE_AES_DECRYPT */
-
- int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
- {
- if (gmac == NULL || key == NULL || !((len == 16) ||
- (len == 24) || (len == 32)) ) {
- return BAD_FUNC_ARG;
- }
-
- return GmacSetKey(gmac, key, len);
- }
- int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
- const byte* authIn, word32 authInSz,
- byte* authTag, word32 authTagSz)
- {
- if (gmac == NULL || authTagSz > AES_BLOCK_SIZE ||
- authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ) {
- return BAD_FUNC_ARG;
- }
-
- return GmacUpdate(gmac, iv, ivSz, authIn, authInSz,
- authTag, authTagSz);
- }
- #endif /* HAVE_AESGCM */
-
- /* AES-CCM */
- #if defined(HAVE_AESCCM) && \
- defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
- int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
- {
- return AesCcmSetKey(aes, key, keySz);
- }
- int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
- const byte* nonce, word32 nonceSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
- {
- /* sanity check on arguments */
- if (aes == NULL || out == NULL || in == NULL || nonce == NULL
- || authTag == NULL || nonceSz < 7 || nonceSz > 13)
- return BAD_FUNC_ARG;
-
- AesCcmEncrypt(aes, out, in, inSz, nonce, nonceSz, authTag,
- authTagSz, authIn, authInSz);
- return 0;
- }
-
- #ifdef HAVE_AES_DECRYPT
- int wc_AesCcmDecrypt(Aes* aes, byte* out,
- const byte* in, word32 inSz,
- const byte* nonce, word32 nonceSz,
- const byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
- {
-
- if (aes == NULL || out == NULL || in == NULL || nonce == NULL
- || authTag == NULL || nonceSz < 7 || nonceSz > 13) {
- return BAD_FUNC_ARG;
- }
-
- return AesCcmDecrypt(aes, out, in, inSz, nonce, nonceSz,
- authTag, authTagSz, authIn, authInSz);
- }
- #endif /* HAVE_AES_DECRYPT */
- #endif /* HAVE_AESCCM && HAVE_FIPS_VERSION 2 */
-
- int wc_AesInit(Aes* aes, void* h, int i)
- {
- if (aes == NULL)
- return BAD_FUNC_ARG;
-
- (void)h;
- (void)i;
-
- /* FIPS doesn't support:
- return AesInit(aes, h, i); */
- return 0;
- }
- void wc_AesFree(Aes* aes)
- {
- (void)aes;
- /* FIPS doesn't support:
- AesFree(aes); */
- }
-
-#else /* else build without fips, or for FIPS v2 */
-
-
-#if defined(WOLFSSL_TI_CRYPT)
- #include
-#else
-
-#include
-
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include
-#endif
-
-#if !defined(WOLFSSL_ARMASM)
-
-#ifdef WOLFSSL_IMX6_CAAM_BLOB
- /* case of possibly not using hardware acceleration for AES but using key
- blobs */
- #include
-#endif
-
-#ifdef DEBUG_AESNI
- #include
-#endif
-
-#ifdef _MSC_VER
- /* 4127 warning constant while(1) */
- #pragma warning(disable: 4127)
-#endif
-
-
-/* Define AES implementation includes and functions */
-#if defined(STM32_CRYPTO)
- /* STM32F2/F4/F7/L4 hardware AES support for ECB, CBC, CTR and GCM modes */
-
-#if defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
-
- static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- int ret = 0;
- #ifdef WOLFSSL_STM32_CUBEMX
- CRYP_HandleTypeDef hcryp;
- #else
- CRYP_InitTypeDef cryptInit;
- CRYP_KeyInitTypeDef keyInit;
- #endif
-
- #ifdef WOLFSSL_STM32_CUBEMX
- ret = wc_Stm32_Aes_Init(aes, &hcryp);
- if (ret != 0)
- return ret;
-
- #ifdef STM32_CRYPTO_AES_ONLY
- hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
- hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
- hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
- #elif defined(STM32_HAL_V2)
- hcryp.Init.Algorithm = CRYP_AES_ECB;
- #endif
- HAL_CRYP_Init(&hcryp);
-
- #ifdef STM32_CRYPTO_AES_ONLY
- ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
- outBlock, STM32_HAL_TIMEOUT);
- #elif defined(STM32_HAL_V2)
- ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)inBlock, AES_BLOCK_SIZE,
- (uint32_t*)outBlock, STM32_HAL_TIMEOUT);
- #else
- ret = HAL_CRYP_AESECB_Encrypt(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
- outBlock, STM32_HAL_TIMEOUT);
- #endif
- if (ret != HAL_OK) {
- ret = WC_TIMEOUT_E;
- }
- HAL_CRYP_DeInit(&hcryp);
-
- #else /* STD_PERI_LIB */
- ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
- if (ret != 0)
- return ret;
-
- /* reset registers to their default values */
- CRYP_DeInit();
-
- /* setup key */
- CRYP_KeyInit(&keyInit);
-
- /* set direction and mode */
- cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
- cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_ECB;
- CRYP_Init(&cryptInit);
-
- /* enable crypto processor */
- CRYP_Cmd(ENABLE);
-
- /* flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
-
- CRYP_DataIn(*(uint32_t*)&inBlock[0]);
- CRYP_DataIn(*(uint32_t*)&inBlock[4]);
- CRYP_DataIn(*(uint32_t*)&inBlock[8]);
- CRYP_DataIn(*(uint32_t*)&inBlock[12]);
-
- /* wait until the complete message has been processed */
- while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
-
- *(uint32_t*)&outBlock[0] = CRYP_DataOut();
- *(uint32_t*)&outBlock[4] = CRYP_DataOut();
- *(uint32_t*)&outBlock[8] = CRYP_DataOut();
- *(uint32_t*)&outBlock[12] = CRYP_DataOut();
-
- /* disable crypto processor */
- CRYP_Cmd(DISABLE);
- #endif /* WOLFSSL_STM32_CUBEMX */
-
- return ret;
- }
-#endif /* WOLFSSL_AES_DIRECT || HAVE_AESGCM || HAVE_AESCCM */
-
-#ifdef HAVE_AES_DECRYPT
- #if defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESCCM)
- static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- int ret = 0;
- #ifdef WOLFSSL_STM32_CUBEMX
- CRYP_HandleTypeDef hcryp;
- #else
- CRYP_InitTypeDef cryptInit;
- CRYP_KeyInitTypeDef keyInit;
- #endif
-
- #ifdef WOLFSSL_STM32_CUBEMX
- ret = wc_Stm32_Aes_Init(aes, &hcryp);
- if (ret != 0)
- return ret;
-
- #ifdef STM32_CRYPTO_AES_ONLY
- hcryp.Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
- hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
- hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
- #elif defined(STM32_HAL_V2)
- hcryp.Init.Algorithm = CRYP_AES_ECB;
- #endif
- HAL_CRYP_Init(&hcryp);
-
- #ifdef STM32_CRYPTO_AES_ONLY
- ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
- outBlock, STM32_HAL_TIMEOUT);
- #elif defined(STM32_HAL_V2)
- ret = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)inBlock, AES_BLOCK_SIZE,
- (uint32_t*)outBlock, STM32_HAL_TIMEOUT);
- #else
- ret = HAL_CRYP_AESECB_Decrypt(&hcryp, (uint8_t*)inBlock, AES_BLOCK_SIZE,
- outBlock, STM32_HAL_TIMEOUT);
- #endif
- if (ret != HAL_OK) {
- ret = WC_TIMEOUT_E;
- }
- HAL_CRYP_DeInit(&hcryp);
-
- #else /* STD_PERI_LIB */
- ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
- if (ret != 0)
- return ret;
-
- /* reset registers to their default values */
- CRYP_DeInit();
-
- /* set direction and key */
- CRYP_KeyInit(&keyInit);
- cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
- CRYP_Init(&cryptInit);
-
- /* enable crypto processor */
- CRYP_Cmd(ENABLE);
-
- /* wait until decrypt key has been initialized */
- while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
-
- /* set direction and mode */
- cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_ECB;
- CRYP_Init(&cryptInit);
-
- /* enable crypto processor */
- CRYP_Cmd(ENABLE);
-
- /* flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
-
- CRYP_DataIn(*(uint32_t*)&inBlock[0]);
- CRYP_DataIn(*(uint32_t*)&inBlock[4]);
- CRYP_DataIn(*(uint32_t*)&inBlock[8]);
- CRYP_DataIn(*(uint32_t*)&inBlock[12]);
-
- /* wait until the complete message has been processed */
- while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
-
- *(uint32_t*)&outBlock[0] = CRYP_DataOut();
- *(uint32_t*)&outBlock[4] = CRYP_DataOut();
- *(uint32_t*)&outBlock[8] = CRYP_DataOut();
- *(uint32_t*)&outBlock[12] = CRYP_DataOut();
-
- /* disable crypto processor */
- CRYP_Cmd(DISABLE);
- #endif /* WOLFSSL_STM32_CUBEMX */
-
- return ret;
- }
- #endif /* WOLFSSL_AES_DIRECT || HAVE_AESCCM */
-#endif /* HAVE_AES_DECRYPT */
-
-#elif defined(HAVE_COLDFIRE_SEC)
- /* Freescale Coldfire SEC support for CBC mode.
- * NOTE: no support for AES-CTR/GCM/CCM/Direct */
- #include
- #include "sec.h"
- #include "mcf5475_sec.h"
- #include "mcf5475_siu.h"
-#elif defined(FREESCALE_LTC)
- #include "fsl_ltc.h"
- #if defined(FREESCALE_LTC_AES_GCM)
- #undef NEED_AES_TABLES
- #undef GCM_TABLE
- #else
- /* if LTC doesn't have GCM, use software with LTC AES ECB mode */
- static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- wc_AesEncryptDirect(aes, outBlock, inBlock);
- return 0;
- }
- static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- wc_AesDecryptDirect(aes, outBlock, inBlock);
- return 0;
- }
- #endif
-#elif defined(FREESCALE_MMCAU)
- /* Freescale mmCAU hardware AES support for Direct, CBC, CCM, GCM modes
- * through the CAU/mmCAU library. Documentation located in
- * ColdFire/ColdFire+ CAU and Kinetis mmCAU Software Library User
- * Guide (See note in README). */
- #ifdef FREESCALE_MMCAU_CLASSIC
- /* MMCAU 1.4 library used with non-KSDK / classic MQX builds */
- #include "cau_api.h"
- #else
- #include "fsl_mmcau.h"
- #endif
-
- static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- int ret;
-
- #ifdef FREESCALE_MMCAU_CLASSIC
- if ((wolfssl_word)outBlock % WOLFSSL_MMCAU_ALIGNMENT) {
- WOLFSSL_MSG("Bad cau_aes_encrypt alignment");
- return BAD_ALIGN_E;
- }
- #endif
-
- ret = wolfSSL_CryptHwMutexLock();
- if(ret == 0) {
- #ifdef FREESCALE_MMCAU_CLASSIC
- cau_aes_encrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
- #else
- MMCAU_AES_EncryptEcb(inBlock, (byte*)aes->key, aes->rounds,
- outBlock);
- #endif
- wolfSSL_CryptHwMutexUnLock();
- }
- return ret;
- }
- #ifdef HAVE_AES_DECRYPT
- static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- int ret;
-
- #ifdef FREESCALE_MMCAU_CLASSIC
- if ((wolfssl_word)outBlock % WOLFSSL_MMCAU_ALIGNMENT) {
- WOLFSSL_MSG("Bad cau_aes_decrypt alignment");
- return BAD_ALIGN_E;
- }
- #endif
-
- ret = wolfSSL_CryptHwMutexLock();
- if(ret == 0) {
- #ifdef FREESCALE_MMCAU_CLASSIC
- cau_aes_decrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
- #else
- MMCAU_AES_DecryptEcb(inBlock, (byte*)aes->key, aes->rounds,
- outBlock);
- #endif
- wolfSSL_CryptHwMutexUnLock();
- }
- return ret;
- }
- #endif /* HAVE_AES_DECRYPT */
-
-#elif defined(WOLFSSL_PIC32MZ_CRYPT)
-
- #include
-
- #if defined(HAVE_AESGCM) || defined(WOLFSSL_AES_DIRECT)
- static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- return wc_Pic32AesCrypt(aes->key, aes->keylen, NULL, 0,
- outBlock, inBlock, AES_BLOCK_SIZE,
- PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RECB);
- }
- #endif
-
- #if defined(HAVE_AES_DECRYPT) && defined(WOLFSSL_AES_DIRECT)
- static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- return wc_Pic32AesCrypt(aes->key, aes->keylen, NULL, 0,
- outBlock, inBlock, AES_BLOCK_SIZE,
- PIC32_DECRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RECB);
- }
- #endif
-
-#elif defined(WOLFSSL_NRF51_AES)
- /* Use built-in AES hardware - AES 128 ECB Encrypt Only */
- #include "wolfssl/wolfcrypt/port/nrf51.h"
-
- static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- return nrf51_aes_encrypt(inBlock, (byte*)aes->key, aes->rounds, outBlock);
- }
-
- #ifdef HAVE_AES_DECRYPT
- #error nRF51 AES Hardware does not support decrypt
- #endif /* HAVE_AES_DECRYPT */
-
-#elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
- !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
-
- #include "wolfssl/wolfcrypt/port/Espressif/esp32-crypt.h"
-
- #if defined(HAVE_AESGCM) || defined(WOLFSSL_AES_DIRECT)
- static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- return wc_esp32AesEncrypt(aes, inBlock, outBlock);
- }
- #endif
-
- #if defined(HAVE_AES_DECRYPT) && defined(WOLFSSL_AES_DIRECT)
- static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- return wc_esp32AesDecrypt(aes, inBlock, outBlock);
- }
- #endif
-
-#elif defined(WOLFSSL_AESNI)
-
- #define NEED_AES_TABLES
-
- /* Each platform needs to query info type 1 from cpuid to see if aesni is
- * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
- */
-
- #ifndef AESNI_ALIGN
- #define AESNI_ALIGN 16
- #endif
-
- #ifdef _MSC_VER
- #define XASM_LINK(f)
- #elif defined(__APPLE__)
- #define XASM_LINK(f) asm("_" f)
- #else
- #define XASM_LINK(f) asm(f)
- #endif /* _MSC_VER */
-
- static int checkAESNI = 0;
- static int haveAESNI = 0;
- static word32 intel_flags = 0;
-
- static int Check_CPU_support_AES(void)
- {
- intel_flags = cpuid_get_flags();
-
- return IS_INTEL_AESNI(intel_flags) != 0;
- }
-
-
- /* tell C compiler these are asm functions in case any mix up of ABI underscore
- prefix between clang/gcc/llvm etc */
- #ifdef HAVE_AES_CBC
- void AES_CBC_encrypt(const unsigned char* in, unsigned char* out,
- unsigned char* ivec, unsigned long length,
- const unsigned char* KS, int nr)
- XASM_LINK("AES_CBC_encrypt");
-
- #ifdef HAVE_AES_DECRYPT
- #if defined(WOLFSSL_AESNI_BY4)
- void AES_CBC_decrypt_by4(const unsigned char* in, unsigned char* out,
- unsigned char* ivec, unsigned long length,
- const unsigned char* KS, int nr)
- XASM_LINK("AES_CBC_decrypt_by4");
- #elif defined(WOLFSSL_AESNI_BY6)
- void AES_CBC_decrypt_by6(const unsigned char* in, unsigned char* out,
- unsigned char* ivec, unsigned long length,
- const unsigned char* KS, int nr)
- XASM_LINK("AES_CBC_decrypt_by6");
- #else /* WOLFSSL_AESNI_BYx */
- void AES_CBC_decrypt_by8(const unsigned char* in, unsigned char* out,
- unsigned char* ivec, unsigned long length,
- const unsigned char* KS, int nr)
- XASM_LINK("AES_CBC_decrypt_by8");
- #endif /* WOLFSSL_AESNI_BYx */
- #endif /* HAVE_AES_DECRYPT */
- #endif /* HAVE_AES_CBC */
-
- void AES_ECB_encrypt(const unsigned char* in, unsigned char* out,
- unsigned long length, const unsigned char* KS, int nr)
- XASM_LINK("AES_ECB_encrypt");
-
- #ifdef HAVE_AES_DECRYPT
- void AES_ECB_decrypt(const unsigned char* in, unsigned char* out,
- unsigned long length, const unsigned char* KS, int nr)
- XASM_LINK("AES_ECB_decrypt");
- #endif
-
- void AES_128_Key_Expansion(const unsigned char* userkey,
- unsigned char* key_schedule)
- XASM_LINK("AES_128_Key_Expansion");
-
- void AES_192_Key_Expansion(const unsigned char* userkey,
- unsigned char* key_schedule)
- XASM_LINK("AES_192_Key_Expansion");
-
- void AES_256_Key_Expansion(const unsigned char* userkey,
- unsigned char* key_schedule)
- XASM_LINK("AES_256_Key_Expansion");
-
-
- static int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
- Aes* aes)
- {
- int ret;
-
- if (!userKey || !aes)
- return BAD_FUNC_ARG;
-
- switch (bits) {
- case 128:
- AES_128_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 10;
- return 0;
- case 192:
- AES_192_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 12;
- return 0;
- case 256:
- AES_256_Key_Expansion (userKey,(byte*)aes->key); aes->rounds = 14;
- return 0;
- default:
- ret = BAD_FUNC_ARG;
- }
-
- return ret;
- }
-
- #ifdef HAVE_AES_DECRYPT
- static int AES_set_decrypt_key(const unsigned char* userKey,
- const int bits, Aes* aes)
- {
- int nr;
- Aes temp_key;
- __m128i *Key_Schedule = (__m128i*)aes->key;
- __m128i *Temp_Key_Schedule = (__m128i*)temp_key.key;
-
- if (!userKey || !aes)
- return BAD_FUNC_ARG;
-
- if (AES_set_encrypt_key(userKey,bits,&temp_key) == BAD_FUNC_ARG)
- return BAD_FUNC_ARG;
-
- nr = temp_key.rounds;
- aes->rounds = nr;
-
- Key_Schedule[nr] = Temp_Key_Schedule[0];
- Key_Schedule[nr-1] = _mm_aesimc_si128(Temp_Key_Schedule[1]);
- Key_Schedule[nr-2] = _mm_aesimc_si128(Temp_Key_Schedule[2]);
- Key_Schedule[nr-3] = _mm_aesimc_si128(Temp_Key_Schedule[3]);
- Key_Schedule[nr-4] = _mm_aesimc_si128(Temp_Key_Schedule[4]);
- Key_Schedule[nr-5] = _mm_aesimc_si128(Temp_Key_Schedule[5]);
- Key_Schedule[nr-6] = _mm_aesimc_si128(Temp_Key_Schedule[6]);
- Key_Schedule[nr-7] = _mm_aesimc_si128(Temp_Key_Schedule[7]);
- Key_Schedule[nr-8] = _mm_aesimc_si128(Temp_Key_Schedule[8]);
- Key_Schedule[nr-9] = _mm_aesimc_si128(Temp_Key_Schedule[9]);
-
- if (nr>10) {
- Key_Schedule[nr-10] = _mm_aesimc_si128(Temp_Key_Schedule[10]);
- Key_Schedule[nr-11] = _mm_aesimc_si128(Temp_Key_Schedule[11]);
- }
-
- if (nr>12) {
- Key_Schedule[nr-12] = _mm_aesimc_si128(Temp_Key_Schedule[12]);
- Key_Schedule[nr-13] = _mm_aesimc_si128(Temp_Key_Schedule[13]);
- }
-
- Key_Schedule[0] = Temp_Key_Schedule[nr];
-
- return 0;
- }
- #endif /* HAVE_AES_DECRYPT */
-
-#elif (defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)) || \
- ((defined(WOLFSSL_AFALG) || defined(WOLFSSL_DEVCRYPTO_AES)) && \
- defined(HAVE_AESCCM))
- static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- wc_AesEncryptDirect(aes, outBlock, inBlock);
- return 0;
- }
-
-#elif defined(WOLFSSL_AFALG)
-#elif defined(WOLFSSL_DEVCRYPTO_AES)
-
-#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES)
- #include "hal_data.h"
-
- #ifndef WOLFSSL_SCE_AES256_HANDLE
- #define WOLFSSL_SCE_AES256_HANDLE g_sce_aes_256
- #endif
-
- #ifndef WOLFSSL_SCE_AES192_HANDLE
- #define WOLFSSL_SCE_AES192_HANDLE g_sce_aes_192
- #endif
-
- #ifndef WOLFSSL_SCE_AES128_HANDLE
- #define WOLFSSL_SCE_AES128_HANDLE g_sce_aes_128
- #endif
-
- static int AES_ECB_encrypt(Aes* aes, const byte* inBlock, byte* outBlock,
- int sz)
- {
- uint32_t ret;
-
- if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
- CRYPTO_WORD_ENDIAN_BIG) {
- ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz);
- }
-
- switch (aes->keylen) {
- #ifdef WOLFSSL_AES_128
- case AES_128_KEY_SIZE:
- ret = WOLFSSL_SCE_AES128_HANDLE.p_api->encrypt(
- WOLFSSL_SCE_AES128_HANDLE.p_ctrl, aes->key,
- NULL, (sz / sizeof(word32)), (word32*)inBlock,
- (word32*)outBlock);
- break;
- #endif
- #ifdef WOLFSSL_AES_192
- case AES_192_KEY_SIZE:
- ret = WOLFSSL_SCE_AES192_HANDLE.p_api->encrypt(
- WOLFSSL_SCE_AES192_HANDLE.p_ctrl, aes->key,
- NULL, (sz / sizeof(word32)), (word32*)inBlock,
- (word32*)outBlock);
- break;
- #endif
- #ifdef WOLFSSL_AES_256
- case AES_256_KEY_SIZE:
- ret = WOLFSSL_SCE_AES256_HANDLE.p_api->encrypt(
- WOLFSSL_SCE_AES256_HANDLE.p_ctrl, aes->key,
- NULL, (sz / sizeof(word32)), (word32*)inBlock,
- (word32*)outBlock);
- break;
- #endif
- default:
- WOLFSSL_MSG("Unknown key size");
- return BAD_FUNC_ARG;
- }
-
- if (ret != SSP_SUCCESS) {
- /* revert input */
- ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz);
- return WC_HW_E;
- }
-
- if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
- CRYPTO_WORD_ENDIAN_BIG) {
- ByteReverseWords((word32*)outBlock, (word32*)outBlock, sz);
- if (inBlock != outBlock) {
- /* revert input */
- ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz);
- }
- }
- return 0;
- }
-
- #if defined(HAVE_AES_DECRYPT)
- static int AES_ECB_decrypt(Aes* aes, const byte* inBlock, byte* outBlock,
- int sz)
- {
- uint32_t ret;
-
- if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
- CRYPTO_WORD_ENDIAN_BIG) {
- ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz);
- }
-
- switch (aes->keylen) {
- #ifdef WOLFSSL_AES_128
- case AES_128_KEY_SIZE:
- ret = WOLFSSL_SCE_AES128_HANDLE.p_api->decrypt(
- WOLFSSL_SCE_AES128_HANDLE.p_ctrl, aes->key, aes->reg,
- (sz / sizeof(word32)), (word32*)inBlock,
- (word32*)outBlock);
- break;
- #endif
- #ifdef WOLFSSL_AES_192
- case AES_192_KEY_SIZE:
- ret = WOLFSSL_SCE_AES192_HANDLE.p_api->decrypt(
- WOLFSSL_SCE_AES192_HANDLE.p_ctrl, aes->key, aes->reg,
- (sz / sizeof(word32)), (word32*)inBlock,
- (word32*)outBlock);
- break;
- #endif
- #ifdef WOLFSSL_AES_256
- case AES_256_KEY_SIZE:
- ret = WOLFSSL_SCE_AES256_HANDLE.p_api->decrypt(
- WOLFSSL_SCE_AES256_HANDLE.p_ctrl, aes->key, aes->reg,
- (sz / sizeof(word32)), (word32*)inBlock,
- (word32*)outBlock);
- break;
- #endif
- default:
- WOLFSSL_MSG("Unknown key size");
- return BAD_FUNC_ARG;
- }
- if (ret != SSP_SUCCESS) {
- return WC_HW_E;
- }
-
- if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag ==
- CRYPTO_WORD_ENDIAN_BIG) {
- ByteReverseWords((word32*)outBlock, (word32*)outBlock, sz);
- if (inBlock != outBlock) {
- /* revert input */
- ByteReverseWords((word32*)inBlock, (word32*)inBlock, sz);
- }
- }
-
- return 0;
- }
-
- #endif
-
- #if defined(HAVE_AESGCM) || defined(WOLFSSL_AES_DIRECT)
- static int wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- return AES_ECB_encrypt(aes, inBlock, outBlock, AES_BLOCK_SIZE);
- }
- #endif
-
- #if defined(HAVE_AES_DECRYPT) && defined(WOLFSSL_AES_DIRECT)
- static int wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
- {
- return AES_ECB_decrypt(aes, inBlock, outBlock, AES_BLOCK_SIZE);
- }
- #endif
-#else
-
- /* using wolfCrypt software implementation */
- #define NEED_AES_TABLES
-#endif
-
-
-
-#ifdef NEED_AES_TABLES
-
-static const word32 rcon[] = {
- 0x01000000, 0x02000000, 0x04000000, 0x08000000,
- 0x10000000, 0x20000000, 0x40000000, 0x80000000,
- 0x1B000000, 0x36000000,
- /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
-};
-
-#ifndef WOLFSSL_AES_SMALL_TABLES
-static const word32 Te[4][256] = {
-{
- 0xc66363a5U, 0xf87c7c84U, 0xee777799U, 0xf67b7b8dU,
- 0xfff2f20dU, 0xd66b6bbdU, 0xde6f6fb1U, 0x91c5c554U,
- 0x60303050U, 0x02010103U, 0xce6767a9U, 0x562b2b7dU,
- 0xe7fefe19U, 0xb5d7d762U, 0x4dababe6U, 0xec76769aU,
- 0x8fcaca45U, 0x1f82829dU, 0x89c9c940U, 0xfa7d7d87U,
- 0xeffafa15U, 0xb25959ebU, 0x8e4747c9U, 0xfbf0f00bU,
- 0x41adadecU, 0xb3d4d467U, 0x5fa2a2fdU, 0x45afafeaU,
- 0x239c9cbfU, 0x53a4a4f7U, 0xe4727296U, 0x9bc0c05bU,
- 0x75b7b7c2U, 0xe1fdfd1cU, 0x3d9393aeU, 0x4c26266aU,
- 0x6c36365aU, 0x7e3f3f41U, 0xf5f7f702U, 0x83cccc4fU,
- 0x6834345cU, 0x51a5a5f4U, 0xd1e5e534U, 0xf9f1f108U,
- 0xe2717193U, 0xabd8d873U, 0x62313153U, 0x2a15153fU,
- 0x0804040cU, 0x95c7c752U, 0x46232365U, 0x9dc3c35eU,
- 0x30181828U, 0x379696a1U, 0x0a05050fU, 0x2f9a9ab5U,
- 0x0e070709U, 0x24121236U, 0x1b80809bU, 0xdfe2e23dU,
- 0xcdebeb26U, 0x4e272769U, 0x7fb2b2cdU, 0xea75759fU,
- 0x1209091bU, 0x1d83839eU, 0x582c2c74U, 0x341a1a2eU,
- 0x361b1b2dU, 0xdc6e6eb2U, 0xb45a5aeeU, 0x5ba0a0fbU,
- 0xa45252f6U, 0x763b3b4dU, 0xb7d6d661U, 0x7db3b3ceU,
- 0x5229297bU, 0xdde3e33eU, 0x5e2f2f71U, 0x13848497U,
- 0xa65353f5U, 0xb9d1d168U, 0x00000000U, 0xc1eded2cU,
- 0x40202060U, 0xe3fcfc1fU, 0x79b1b1c8U, 0xb65b5bedU,
- 0xd46a6abeU, 0x8dcbcb46U, 0x67bebed9U, 0x7239394bU,
- 0x944a4adeU, 0x984c4cd4U, 0xb05858e8U, 0x85cfcf4aU,
- 0xbbd0d06bU, 0xc5efef2aU, 0x4faaaae5U, 0xedfbfb16U,
- 0x864343c5U, 0x9a4d4dd7U, 0x66333355U, 0x11858594U,
- 0x8a4545cfU, 0xe9f9f910U, 0x04020206U, 0xfe7f7f81U,
- 0xa05050f0U, 0x783c3c44U, 0x259f9fbaU, 0x4ba8a8e3U,
- 0xa25151f3U, 0x5da3a3feU, 0x804040c0U, 0x058f8f8aU,
- 0x3f9292adU, 0x219d9dbcU, 0x70383848U, 0xf1f5f504U,
- 0x63bcbcdfU, 0x77b6b6c1U, 0xafdada75U, 0x42212163U,
- 0x20101030U, 0xe5ffff1aU, 0xfdf3f30eU, 0xbfd2d26dU,
- 0x81cdcd4cU, 0x180c0c14U, 0x26131335U, 0xc3ecec2fU,
- 0xbe5f5fe1U, 0x359797a2U, 0x884444ccU, 0x2e171739U,
- 0x93c4c457U, 0x55a7a7f2U, 0xfc7e7e82U, 0x7a3d3d47U,
- 0xc86464acU, 0xba5d5de7U, 0x3219192bU, 0xe6737395U,
- 0xc06060a0U, 0x19818198U, 0x9e4f4fd1U, 0xa3dcdc7fU,
- 0x44222266U, 0x542a2a7eU, 0x3b9090abU, 0x0b888883U,
- 0x8c4646caU, 0xc7eeee29U, 0x6bb8b8d3U, 0x2814143cU,
- 0xa7dede79U, 0xbc5e5ee2U, 0x160b0b1dU, 0xaddbdb76U,
- 0xdbe0e03bU, 0x64323256U, 0x743a3a4eU, 0x140a0a1eU,
- 0x924949dbU, 0x0c06060aU, 0x4824246cU, 0xb85c5ce4U,
- 0x9fc2c25dU, 0xbdd3d36eU, 0x43acacefU, 0xc46262a6U,
- 0x399191a8U, 0x319595a4U, 0xd3e4e437U, 0xf279798bU,
- 0xd5e7e732U, 0x8bc8c843U, 0x6e373759U, 0xda6d6db7U,
- 0x018d8d8cU, 0xb1d5d564U, 0x9c4e4ed2U, 0x49a9a9e0U,
- 0xd86c6cb4U, 0xac5656faU, 0xf3f4f407U, 0xcfeaea25U,
- 0xca6565afU, 0xf47a7a8eU, 0x47aeaee9U, 0x10080818U,
- 0x6fbabad5U, 0xf0787888U, 0x4a25256fU, 0x5c2e2e72U,
- 0x381c1c24U, 0x57a6a6f1U, 0x73b4b4c7U, 0x97c6c651U,
- 0xcbe8e823U, 0xa1dddd7cU, 0xe874749cU, 0x3e1f1f21U,
- 0x964b4bddU, 0x61bdbddcU, 0x0d8b8b86U, 0x0f8a8a85U,
- 0xe0707090U, 0x7c3e3e42U, 0x71b5b5c4U, 0xcc6666aaU,
- 0x904848d8U, 0x06030305U, 0xf7f6f601U, 0x1c0e0e12U,
- 0xc26161a3U, 0x6a35355fU, 0xae5757f9U, 0x69b9b9d0U,
- 0x17868691U, 0x99c1c158U, 0x3a1d1d27U, 0x279e9eb9U,
- 0xd9e1e138U, 0xebf8f813U, 0x2b9898b3U, 0x22111133U,
- 0xd26969bbU, 0xa9d9d970U, 0x078e8e89U, 0x339494a7U,
- 0x2d9b9bb6U, 0x3c1e1e22U, 0x15878792U, 0xc9e9e920U,
- 0x87cece49U, 0xaa5555ffU, 0x50282878U, 0xa5dfdf7aU,
- 0x038c8c8fU, 0x59a1a1f8U, 0x09898980U, 0x1a0d0d17U,
- 0x65bfbfdaU, 0xd7e6e631U, 0x844242c6U, 0xd06868b8U,
- 0x824141c3U, 0x299999b0U, 0x5a2d2d77U, 0x1e0f0f11U,
- 0x7bb0b0cbU, 0xa85454fcU, 0x6dbbbbd6U, 0x2c16163aU,
-},
-{
- 0xa5c66363U, 0x84f87c7cU, 0x99ee7777U, 0x8df67b7bU,
- 0x0dfff2f2U, 0xbdd66b6bU, 0xb1de6f6fU, 0x5491c5c5U,
- 0x50603030U, 0x03020101U, 0xa9ce6767U, 0x7d562b2bU,
- 0x19e7fefeU, 0x62b5d7d7U, 0xe64dababU, 0x9aec7676U,
- 0x458fcacaU, 0x9d1f8282U, 0x4089c9c9U, 0x87fa7d7dU,
- 0x15effafaU, 0xebb25959U, 0xc98e4747U, 0x0bfbf0f0U,
- 0xec41adadU, 0x67b3d4d4U, 0xfd5fa2a2U, 0xea45afafU,
- 0xbf239c9cU, 0xf753a4a4U, 0x96e47272U, 0x5b9bc0c0U,
- 0xc275b7b7U, 0x1ce1fdfdU, 0xae3d9393U, 0x6a4c2626U,
- 0x5a6c3636U, 0x417e3f3fU, 0x02f5f7f7U, 0x4f83ccccU,
- 0x5c683434U, 0xf451a5a5U, 0x34d1e5e5U, 0x08f9f1f1U,
- 0x93e27171U, 0x73abd8d8U, 0x53623131U, 0x3f2a1515U,
- 0x0c080404U, 0x5295c7c7U, 0x65462323U, 0x5e9dc3c3U,
- 0x28301818U, 0xa1379696U, 0x0f0a0505U, 0xb52f9a9aU,
- 0x090e0707U, 0x36241212U, 0x9b1b8080U, 0x3ddfe2e2U,
- 0x26cdebebU, 0x694e2727U, 0xcd7fb2b2U, 0x9fea7575U,
- 0x1b120909U, 0x9e1d8383U, 0x74582c2cU, 0x2e341a1aU,
- 0x2d361b1bU, 0xb2dc6e6eU, 0xeeb45a5aU, 0xfb5ba0a0U,
- 0xf6a45252U, 0x4d763b3bU, 0x61b7d6d6U, 0xce7db3b3U,
- 0x7b522929U, 0x3edde3e3U, 0x715e2f2fU, 0x97138484U,
- 0xf5a65353U, 0x68b9d1d1U, 0x00000000U, 0x2cc1ededU,
- 0x60402020U, 0x1fe3fcfcU, 0xc879b1b1U, 0xedb65b5bU,
- 0xbed46a6aU, 0x468dcbcbU, 0xd967bebeU, 0x4b723939U,
- 0xde944a4aU, 0xd4984c4cU, 0xe8b05858U, 0x4a85cfcfU,
- 0x6bbbd0d0U, 0x2ac5efefU, 0xe54faaaaU, 0x16edfbfbU,
- 0xc5864343U, 0xd79a4d4dU, 0x55663333U, 0x94118585U,
- 0xcf8a4545U, 0x10e9f9f9U, 0x06040202U, 0x81fe7f7fU,
- 0xf0a05050U, 0x44783c3cU, 0xba259f9fU, 0xe34ba8a8U,
- 0xf3a25151U, 0xfe5da3a3U, 0xc0804040U, 0x8a058f8fU,
- 0xad3f9292U, 0xbc219d9dU, 0x48703838U, 0x04f1f5f5U,
- 0xdf63bcbcU, 0xc177b6b6U, 0x75afdadaU, 0x63422121U,
- 0x30201010U, 0x1ae5ffffU, 0x0efdf3f3U, 0x6dbfd2d2U,
- 0x4c81cdcdU, 0x14180c0cU, 0x35261313U, 0x2fc3ececU,
- 0xe1be5f5fU, 0xa2359797U, 0xcc884444U, 0x392e1717U,
- 0x5793c4c4U, 0xf255a7a7U, 0x82fc7e7eU, 0x477a3d3dU,
- 0xacc86464U, 0xe7ba5d5dU, 0x2b321919U, 0x95e67373U,
- 0xa0c06060U, 0x98198181U, 0xd19e4f4fU, 0x7fa3dcdcU,
- 0x66442222U, 0x7e542a2aU, 0xab3b9090U, 0x830b8888U,
- 0xca8c4646U, 0x29c7eeeeU, 0xd36bb8b8U, 0x3c281414U,
- 0x79a7dedeU, 0xe2bc5e5eU, 0x1d160b0bU, 0x76addbdbU,
- 0x3bdbe0e0U, 0x56643232U, 0x4e743a3aU, 0x1e140a0aU,
- 0xdb924949U, 0x0a0c0606U, 0x6c482424U, 0xe4b85c5cU,
- 0x5d9fc2c2U, 0x6ebdd3d3U, 0xef43acacU, 0xa6c46262U,
- 0xa8399191U, 0xa4319595U, 0x37d3e4e4U, 0x8bf27979U,
- 0x32d5e7e7U, 0x438bc8c8U, 0x596e3737U, 0xb7da6d6dU,
- 0x8c018d8dU, 0x64b1d5d5U, 0xd29c4e4eU, 0xe049a9a9U,
- 0xb4d86c6cU, 0xfaac5656U, 0x07f3f4f4U, 0x25cfeaeaU,
- 0xafca6565U, 0x8ef47a7aU, 0xe947aeaeU, 0x18100808U,
- 0xd56fbabaU, 0x88f07878U, 0x6f4a2525U, 0x725c2e2eU,
- 0x24381c1cU, 0xf157a6a6U, 0xc773b4b4U, 0x5197c6c6U,
- 0x23cbe8e8U, 0x7ca1ddddU, 0x9ce87474U, 0x213e1f1fU,
- 0xdd964b4bU, 0xdc61bdbdU, 0x860d8b8bU, 0x850f8a8aU,
- 0x90e07070U, 0x427c3e3eU, 0xc471b5b5U, 0xaacc6666U,
- 0xd8904848U, 0x05060303U, 0x01f7f6f6U, 0x121c0e0eU,
- 0xa3c26161U, 0x5f6a3535U, 0xf9ae5757U, 0xd069b9b9U,
- 0x91178686U, 0x5899c1c1U, 0x273a1d1dU, 0xb9279e9eU,
- 0x38d9e1e1U, 0x13ebf8f8U, 0xb32b9898U, 0x33221111U,
- 0xbbd26969U, 0x70a9d9d9U, 0x89078e8eU, 0xa7339494U,
- 0xb62d9b9bU, 0x223c1e1eU, 0x92158787U, 0x20c9e9e9U,
- 0x4987ceceU, 0xffaa5555U, 0x78502828U, 0x7aa5dfdfU,
- 0x8f038c8cU, 0xf859a1a1U, 0x80098989U, 0x171a0d0dU,
- 0xda65bfbfU, 0x31d7e6e6U, 0xc6844242U, 0xb8d06868U,
- 0xc3824141U, 0xb0299999U, 0x775a2d2dU, 0x111e0f0fU,
- 0xcb7bb0b0U, 0xfca85454U, 0xd66dbbbbU, 0x3a2c1616U,
-},
-{
- 0x63a5c663U, 0x7c84f87cU, 0x7799ee77U, 0x7b8df67bU,
- 0xf20dfff2U, 0x6bbdd66bU, 0x6fb1de6fU, 0xc55491c5U,
- 0x30506030U, 0x01030201U, 0x67a9ce67U, 0x2b7d562bU,
- 0xfe19e7feU, 0xd762b5d7U, 0xabe64dabU, 0x769aec76U,
- 0xca458fcaU, 0x829d1f82U, 0xc94089c9U, 0x7d87fa7dU,
- 0xfa15effaU, 0x59ebb259U, 0x47c98e47U, 0xf00bfbf0U,
- 0xadec41adU, 0xd467b3d4U, 0xa2fd5fa2U, 0xafea45afU,
- 0x9cbf239cU, 0xa4f753a4U, 0x7296e472U, 0xc05b9bc0U,
- 0xb7c275b7U, 0xfd1ce1fdU, 0x93ae3d93U, 0x266a4c26U,
- 0x365a6c36U, 0x3f417e3fU, 0xf702f5f7U, 0xcc4f83ccU,
- 0x345c6834U, 0xa5f451a5U, 0xe534d1e5U, 0xf108f9f1U,
- 0x7193e271U, 0xd873abd8U, 0x31536231U, 0x153f2a15U,
- 0x040c0804U, 0xc75295c7U, 0x23654623U, 0xc35e9dc3U,
- 0x18283018U, 0x96a13796U, 0x050f0a05U, 0x9ab52f9aU,
- 0x07090e07U, 0x12362412U, 0x809b1b80U, 0xe23ddfe2U,
- 0xeb26cdebU, 0x27694e27U, 0xb2cd7fb2U, 0x759fea75U,
- 0x091b1209U, 0x839e1d83U, 0x2c74582cU, 0x1a2e341aU,
- 0x1b2d361bU, 0x6eb2dc6eU, 0x5aeeb45aU, 0xa0fb5ba0U,
- 0x52f6a452U, 0x3b4d763bU, 0xd661b7d6U, 0xb3ce7db3U,
- 0x297b5229U, 0xe33edde3U, 0x2f715e2fU, 0x84971384U,
- 0x53f5a653U, 0xd168b9d1U, 0x00000000U, 0xed2cc1edU,
- 0x20604020U, 0xfc1fe3fcU, 0xb1c879b1U, 0x5bedb65bU,
- 0x6abed46aU, 0xcb468dcbU, 0xbed967beU, 0x394b7239U,
- 0x4ade944aU, 0x4cd4984cU, 0x58e8b058U, 0xcf4a85cfU,
- 0xd06bbbd0U, 0xef2ac5efU, 0xaae54faaU, 0xfb16edfbU,
- 0x43c58643U, 0x4dd79a4dU, 0x33556633U, 0x85941185U,
- 0x45cf8a45U, 0xf910e9f9U, 0x02060402U, 0x7f81fe7fU,
- 0x50f0a050U, 0x3c44783cU, 0x9fba259fU, 0xa8e34ba8U,
- 0x51f3a251U, 0xa3fe5da3U, 0x40c08040U, 0x8f8a058fU,
- 0x92ad3f92U, 0x9dbc219dU, 0x38487038U, 0xf504f1f5U,
- 0xbcdf63bcU, 0xb6c177b6U, 0xda75afdaU, 0x21634221U,
- 0x10302010U, 0xff1ae5ffU, 0xf30efdf3U, 0xd26dbfd2U,
- 0xcd4c81cdU, 0x0c14180cU, 0x13352613U, 0xec2fc3ecU,
- 0x5fe1be5fU, 0x97a23597U, 0x44cc8844U, 0x17392e17U,
- 0xc45793c4U, 0xa7f255a7U, 0x7e82fc7eU, 0x3d477a3dU,
- 0x64acc864U, 0x5de7ba5dU, 0x192b3219U, 0x7395e673U,
- 0x60a0c060U, 0x81981981U, 0x4fd19e4fU, 0xdc7fa3dcU,
- 0x22664422U, 0x2a7e542aU, 0x90ab3b90U, 0x88830b88U,
- 0x46ca8c46U, 0xee29c7eeU, 0xb8d36bb8U, 0x143c2814U,
- 0xde79a7deU, 0x5ee2bc5eU, 0x0b1d160bU, 0xdb76addbU,
- 0xe03bdbe0U, 0x32566432U, 0x3a4e743aU, 0x0a1e140aU,
- 0x49db9249U, 0x060a0c06U, 0x246c4824U, 0x5ce4b85cU,
- 0xc25d9fc2U, 0xd36ebdd3U, 0xacef43acU, 0x62a6c462U,
- 0x91a83991U, 0x95a43195U, 0xe437d3e4U, 0x798bf279U,
- 0xe732d5e7U, 0xc8438bc8U, 0x37596e37U, 0x6db7da6dU,
- 0x8d8c018dU, 0xd564b1d5U, 0x4ed29c4eU, 0xa9e049a9U,
- 0x6cb4d86cU, 0x56faac56U, 0xf407f3f4U, 0xea25cfeaU,
- 0x65afca65U, 0x7a8ef47aU, 0xaee947aeU, 0x08181008U,
- 0xbad56fbaU, 0x7888f078U, 0x256f4a25U, 0x2e725c2eU,
- 0x1c24381cU, 0xa6f157a6U, 0xb4c773b4U, 0xc65197c6U,
- 0xe823cbe8U, 0xdd7ca1ddU, 0x749ce874U, 0x1f213e1fU,
- 0x4bdd964bU, 0xbddc61bdU, 0x8b860d8bU, 0x8a850f8aU,
- 0x7090e070U, 0x3e427c3eU, 0xb5c471b5U, 0x66aacc66U,
- 0x48d89048U, 0x03050603U, 0xf601f7f6U, 0x0e121c0eU,
- 0x61a3c261U, 0x355f6a35U, 0x57f9ae57U, 0xb9d069b9U,
- 0x86911786U, 0xc15899c1U, 0x1d273a1dU, 0x9eb9279eU,
- 0xe138d9e1U, 0xf813ebf8U, 0x98b32b98U, 0x11332211U,
- 0x69bbd269U, 0xd970a9d9U, 0x8e89078eU, 0x94a73394U,
- 0x9bb62d9bU, 0x1e223c1eU, 0x87921587U, 0xe920c9e9U,
- 0xce4987ceU, 0x55ffaa55U, 0x28785028U, 0xdf7aa5dfU,
- 0x8c8f038cU, 0xa1f859a1U, 0x89800989U, 0x0d171a0dU,
- 0xbfda65bfU, 0xe631d7e6U, 0x42c68442U, 0x68b8d068U,
- 0x41c38241U, 0x99b02999U, 0x2d775a2dU, 0x0f111e0fU,
- 0xb0cb7bb0U, 0x54fca854U, 0xbbd66dbbU, 0x163a2c16U,
-},
-{
- 0x6363a5c6U, 0x7c7c84f8U, 0x777799eeU, 0x7b7b8df6U,
- 0xf2f20dffU, 0x6b6bbdd6U, 0x6f6fb1deU, 0xc5c55491U,
- 0x30305060U, 0x01010302U, 0x6767a9ceU, 0x2b2b7d56U,
- 0xfefe19e7U, 0xd7d762b5U, 0xababe64dU, 0x76769aecU,
- 0xcaca458fU, 0x82829d1fU, 0xc9c94089U, 0x7d7d87faU,
- 0xfafa15efU, 0x5959ebb2U, 0x4747c98eU, 0xf0f00bfbU,
- 0xadadec41U, 0xd4d467b3U, 0xa2a2fd5fU, 0xafafea45U,
- 0x9c9cbf23U, 0xa4a4f753U, 0x727296e4U, 0xc0c05b9bU,
- 0xb7b7c275U, 0xfdfd1ce1U, 0x9393ae3dU, 0x26266a4cU,
- 0x36365a6cU, 0x3f3f417eU, 0xf7f702f5U, 0xcccc4f83U,
- 0x34345c68U, 0xa5a5f451U, 0xe5e534d1U, 0xf1f108f9U,
- 0x717193e2U, 0xd8d873abU, 0x31315362U, 0x15153f2aU,
- 0x04040c08U, 0xc7c75295U, 0x23236546U, 0xc3c35e9dU,
- 0x18182830U, 0x9696a137U, 0x05050f0aU, 0x9a9ab52fU,
- 0x0707090eU, 0x12123624U, 0x80809b1bU, 0xe2e23ddfU,
- 0xebeb26cdU, 0x2727694eU, 0xb2b2cd7fU, 0x75759feaU,
- 0x09091b12U, 0x83839e1dU, 0x2c2c7458U, 0x1a1a2e34U,
- 0x1b1b2d36U, 0x6e6eb2dcU, 0x5a5aeeb4U, 0xa0a0fb5bU,
- 0x5252f6a4U, 0x3b3b4d76U, 0xd6d661b7U, 0xb3b3ce7dU,
- 0x29297b52U, 0xe3e33eddU, 0x2f2f715eU, 0x84849713U,
- 0x5353f5a6U, 0xd1d168b9U, 0x00000000U, 0xeded2cc1U,
- 0x20206040U, 0xfcfc1fe3U, 0xb1b1c879U, 0x5b5bedb6U,
- 0x6a6abed4U, 0xcbcb468dU, 0xbebed967U, 0x39394b72U,
- 0x4a4ade94U, 0x4c4cd498U, 0x5858e8b0U, 0xcfcf4a85U,
- 0xd0d06bbbU, 0xefef2ac5U, 0xaaaae54fU, 0xfbfb16edU,
- 0x4343c586U, 0x4d4dd79aU, 0x33335566U, 0x85859411U,
- 0x4545cf8aU, 0xf9f910e9U, 0x02020604U, 0x7f7f81feU,
- 0x5050f0a0U, 0x3c3c4478U, 0x9f9fba25U, 0xa8a8e34bU,
- 0x5151f3a2U, 0xa3a3fe5dU, 0x4040c080U, 0x8f8f8a05U,
- 0x9292ad3fU, 0x9d9dbc21U, 0x38384870U, 0xf5f504f1U,
- 0xbcbcdf63U, 0xb6b6c177U, 0xdada75afU, 0x21216342U,
- 0x10103020U, 0xffff1ae5U, 0xf3f30efdU, 0xd2d26dbfU,
- 0xcdcd4c81U, 0x0c0c1418U, 0x13133526U, 0xecec2fc3U,
- 0x5f5fe1beU, 0x9797a235U, 0x4444cc88U, 0x1717392eU,
- 0xc4c45793U, 0xa7a7f255U, 0x7e7e82fcU, 0x3d3d477aU,
- 0x6464acc8U, 0x5d5de7baU, 0x19192b32U, 0x737395e6U,
- 0x6060a0c0U, 0x81819819U, 0x4f4fd19eU, 0xdcdc7fa3U,
- 0x22226644U, 0x2a2a7e54U, 0x9090ab3bU, 0x8888830bU,
- 0x4646ca8cU, 0xeeee29c7U, 0xb8b8d36bU, 0x14143c28U,
- 0xdede79a7U, 0x5e5ee2bcU, 0x0b0b1d16U, 0xdbdb76adU,
- 0xe0e03bdbU, 0x32325664U, 0x3a3a4e74U, 0x0a0a1e14U,
- 0x4949db92U, 0x06060a0cU, 0x24246c48U, 0x5c5ce4b8U,
- 0xc2c25d9fU, 0xd3d36ebdU, 0xacacef43U, 0x6262a6c4U,
- 0x9191a839U, 0x9595a431U, 0xe4e437d3U, 0x79798bf2U,
- 0xe7e732d5U, 0xc8c8438bU, 0x3737596eU, 0x6d6db7daU,
- 0x8d8d8c01U, 0xd5d564b1U, 0x4e4ed29cU, 0xa9a9e049U,
- 0x6c6cb4d8U, 0x5656faacU, 0xf4f407f3U, 0xeaea25cfU,
- 0x6565afcaU, 0x7a7a8ef4U, 0xaeaee947U, 0x08081810U,
- 0xbabad56fU, 0x787888f0U, 0x25256f4aU, 0x2e2e725cU,
- 0x1c1c2438U, 0xa6a6f157U, 0xb4b4c773U, 0xc6c65197U,
- 0xe8e823cbU, 0xdddd7ca1U, 0x74749ce8U, 0x1f1f213eU,
- 0x4b4bdd96U, 0xbdbddc61U, 0x8b8b860dU, 0x8a8a850fU,
- 0x707090e0U, 0x3e3e427cU, 0xb5b5c471U, 0x6666aaccU,
- 0x4848d890U, 0x03030506U, 0xf6f601f7U, 0x0e0e121cU,
- 0x6161a3c2U, 0x35355f6aU, 0x5757f9aeU, 0xb9b9d069U,
- 0x86869117U, 0xc1c15899U, 0x1d1d273aU, 0x9e9eb927U,
- 0xe1e138d9U, 0xf8f813ebU, 0x9898b32bU, 0x11113322U,
- 0x6969bbd2U, 0xd9d970a9U, 0x8e8e8907U, 0x9494a733U,
- 0x9b9bb62dU, 0x1e1e223cU, 0x87879215U, 0xe9e920c9U,
- 0xcece4987U, 0x5555ffaaU, 0x28287850U, 0xdfdf7aa5U,
- 0x8c8c8f03U, 0xa1a1f859U, 0x89898009U, 0x0d0d171aU,
- 0xbfbfda65U, 0xe6e631d7U, 0x4242c684U, 0x6868b8d0U,
- 0x4141c382U, 0x9999b029U, 0x2d2d775aU, 0x0f0f111eU,
- 0xb0b0cb7bU, 0x5454fca8U, 0xbbbbd66dU, 0x16163a2cU,
-}
-};
-
-#ifdef HAVE_AES_DECRYPT
-static const word32 Td[4][256] = {
-{
- 0x51f4a750U, 0x7e416553U, 0x1a17a4c3U, 0x3a275e96U,
- 0x3bab6bcbU, 0x1f9d45f1U, 0xacfa58abU, 0x4be30393U,
- 0x2030fa55U, 0xad766df6U, 0x88cc7691U, 0xf5024c25U,
- 0x4fe5d7fcU, 0xc52acbd7U, 0x26354480U, 0xb562a38fU,
- 0xdeb15a49U, 0x25ba1b67U, 0x45ea0e98U, 0x5dfec0e1U,
- 0xc32f7502U, 0x814cf012U, 0x8d4697a3U, 0x6bd3f9c6U,
- 0x038f5fe7U, 0x15929c95U, 0xbf6d7aebU, 0x955259daU,
- 0xd4be832dU, 0x587421d3U, 0x49e06929U, 0x8ec9c844U,
- 0x75c2896aU, 0xf48e7978U, 0x99583e6bU, 0x27b971ddU,
- 0xbee14fb6U, 0xf088ad17U, 0xc920ac66U, 0x7dce3ab4U,
- 0x63df4a18U, 0xe51a3182U, 0x97513360U, 0x62537f45U,
- 0xb16477e0U, 0xbb6bae84U, 0xfe81a01cU, 0xf9082b94U,
- 0x70486858U, 0x8f45fd19U, 0x94de6c87U, 0x527bf8b7U,
- 0xab73d323U, 0x724b02e2U, 0xe31f8f57U, 0x6655ab2aU,
- 0xb2eb2807U, 0x2fb5c203U, 0x86c57b9aU, 0xd33708a5U,
- 0x302887f2U, 0x23bfa5b2U, 0x02036abaU, 0xed16825cU,
- 0x8acf1c2bU, 0xa779b492U, 0xf307f2f0U, 0x4e69e2a1U,
- 0x65daf4cdU, 0x0605bed5U, 0xd134621fU, 0xc4a6fe8aU,
- 0x342e539dU, 0xa2f355a0U, 0x058ae132U, 0xa4f6eb75U,
- 0x0b83ec39U, 0x4060efaaU, 0x5e719f06U, 0xbd6e1051U,
- 0x3e218af9U, 0x96dd063dU, 0xdd3e05aeU, 0x4de6bd46U,
- 0x91548db5U, 0x71c45d05U, 0x0406d46fU, 0x605015ffU,
- 0x1998fb24U, 0xd6bde997U, 0x894043ccU, 0x67d99e77U,
- 0xb0e842bdU, 0x07898b88U, 0xe7195b38U, 0x79c8eedbU,
- 0xa17c0a47U, 0x7c420fe9U, 0xf8841ec9U, 0x00000000U,
- 0x09808683U, 0x322bed48U, 0x1e1170acU, 0x6c5a724eU,
- 0xfd0efffbU, 0x0f853856U, 0x3daed51eU, 0x362d3927U,
- 0x0a0fd964U, 0x685ca621U, 0x9b5b54d1U, 0x24362e3aU,
- 0x0c0a67b1U, 0x9357e70fU, 0xb4ee96d2U, 0x1b9b919eU,
- 0x80c0c54fU, 0x61dc20a2U, 0x5a774b69U, 0x1c121a16U,
- 0xe293ba0aU, 0xc0a02ae5U, 0x3c22e043U, 0x121b171dU,
- 0x0e090d0bU, 0xf28bc7adU, 0x2db6a8b9U, 0x141ea9c8U,
- 0x57f11985U, 0xaf75074cU, 0xee99ddbbU, 0xa37f60fdU,
- 0xf701269fU, 0x5c72f5bcU, 0x44663bc5U, 0x5bfb7e34U,
- 0x8b432976U, 0xcb23c6dcU, 0xb6edfc68U, 0xb8e4f163U,
- 0xd731dccaU, 0x42638510U, 0x13972240U, 0x84c61120U,
- 0x854a247dU, 0xd2bb3df8U, 0xaef93211U, 0xc729a16dU,
- 0x1d9e2f4bU, 0xdcb230f3U, 0x0d8652ecU, 0x77c1e3d0U,
- 0x2bb3166cU, 0xa970b999U, 0x119448faU, 0x47e96422U,
- 0xa8fc8cc4U, 0xa0f03f1aU, 0x567d2cd8U, 0x223390efU,
- 0x87494ec7U, 0xd938d1c1U, 0x8ccaa2feU, 0x98d40b36U,
- 0xa6f581cfU, 0xa57ade28U, 0xdab78e26U, 0x3fadbfa4U,
- 0x2c3a9de4U, 0x5078920dU, 0x6a5fcc9bU, 0x547e4662U,
- 0xf68d13c2U, 0x90d8b8e8U, 0x2e39f75eU, 0x82c3aff5U,
- 0x9f5d80beU, 0x69d0937cU, 0x6fd52da9U, 0xcf2512b3U,
- 0xc8ac993bU, 0x10187da7U, 0xe89c636eU, 0xdb3bbb7bU,
- 0xcd267809U, 0x6e5918f4U, 0xec9ab701U, 0x834f9aa8U,
- 0xe6956e65U, 0xaaffe67eU, 0x21bccf08U, 0xef15e8e6U,
- 0xbae79bd9U, 0x4a6f36ceU, 0xea9f09d4U, 0x29b07cd6U,
- 0x31a4b2afU, 0x2a3f2331U, 0xc6a59430U, 0x35a266c0U,
- 0x744ebc37U, 0xfc82caa6U, 0xe090d0b0U, 0x33a7d815U,
- 0xf104984aU, 0x41ecdaf7U, 0x7fcd500eU, 0x1791f62fU,
- 0x764dd68dU, 0x43efb04dU, 0xccaa4d54U, 0xe49604dfU,
- 0x9ed1b5e3U, 0x4c6a881bU, 0xc12c1fb8U, 0x4665517fU,
- 0x9d5eea04U, 0x018c355dU, 0xfa877473U, 0xfb0b412eU,
- 0xb3671d5aU, 0x92dbd252U, 0xe9105633U, 0x6dd64713U,
- 0x9ad7618cU, 0x37a10c7aU, 0x59f8148eU, 0xeb133c89U,
- 0xcea927eeU, 0xb761c935U, 0xe11ce5edU, 0x7a47b13cU,
- 0x9cd2df59U, 0x55f2733fU, 0x1814ce79U, 0x73c737bfU,
- 0x53f7cdeaU, 0x5ffdaa5bU, 0xdf3d6f14U, 0x7844db86U,
- 0xcaaff381U, 0xb968c43eU, 0x3824342cU, 0xc2a3405fU,
- 0x161dc372U, 0xbce2250cU, 0x283c498bU, 0xff0d9541U,
- 0x39a80171U, 0x080cb3deU, 0xd8b4e49cU, 0x6456c190U,
- 0x7bcb8461U, 0xd532b670U, 0x486c5c74U, 0xd0b85742U,
-},
-{
- 0x5051f4a7U, 0x537e4165U, 0xc31a17a4U, 0x963a275eU,
- 0xcb3bab6bU, 0xf11f9d45U, 0xabacfa58U, 0x934be303U,
- 0x552030faU, 0xf6ad766dU, 0x9188cc76U, 0x25f5024cU,
- 0xfc4fe5d7U, 0xd7c52acbU, 0x80263544U, 0x8fb562a3U,
- 0x49deb15aU, 0x6725ba1bU, 0x9845ea0eU, 0xe15dfec0U,
- 0x02c32f75U, 0x12814cf0U, 0xa38d4697U, 0xc66bd3f9U,
- 0xe7038f5fU, 0x9515929cU, 0xebbf6d7aU, 0xda955259U,
- 0x2dd4be83U, 0xd3587421U, 0x2949e069U, 0x448ec9c8U,
- 0x6a75c289U, 0x78f48e79U, 0x6b99583eU, 0xdd27b971U,
- 0xb6bee14fU, 0x17f088adU, 0x66c920acU, 0xb47dce3aU,
- 0x1863df4aU, 0x82e51a31U, 0x60975133U, 0x4562537fU,
- 0xe0b16477U, 0x84bb6baeU, 0x1cfe81a0U, 0x94f9082bU,
- 0x58704868U, 0x198f45fdU, 0x8794de6cU, 0xb7527bf8U,
- 0x23ab73d3U, 0xe2724b02U, 0x57e31f8fU, 0x2a6655abU,
- 0x07b2eb28U, 0x032fb5c2U, 0x9a86c57bU, 0xa5d33708U,
- 0xf2302887U, 0xb223bfa5U, 0xba02036aU, 0x5ced1682U,
- 0x2b8acf1cU, 0x92a779b4U, 0xf0f307f2U, 0xa14e69e2U,
- 0xcd65daf4U, 0xd50605beU, 0x1fd13462U, 0x8ac4a6feU,
- 0x9d342e53U, 0xa0a2f355U, 0x32058ae1U, 0x75a4f6ebU,
- 0x390b83ecU, 0xaa4060efU, 0x065e719fU, 0x51bd6e10U,
- 0xf93e218aU, 0x3d96dd06U, 0xaedd3e05U, 0x464de6bdU,
- 0xb591548dU, 0x0571c45dU, 0x6f0406d4U, 0xff605015U,
- 0x241998fbU, 0x97d6bde9U, 0xcc894043U, 0x7767d99eU,
- 0xbdb0e842U, 0x8807898bU, 0x38e7195bU, 0xdb79c8eeU,
- 0x47a17c0aU, 0xe97c420fU, 0xc9f8841eU, 0x00000000U,
- 0x83098086U, 0x48322bedU, 0xac1e1170U, 0x4e6c5a72U,
- 0xfbfd0effU, 0x560f8538U, 0x1e3daed5U, 0x27362d39U,
- 0x640a0fd9U, 0x21685ca6U, 0xd19b5b54U, 0x3a24362eU,
- 0xb10c0a67U, 0x0f9357e7U, 0xd2b4ee96U, 0x9e1b9b91U,
- 0x4f80c0c5U, 0xa261dc20U, 0x695a774bU, 0x161c121aU,
- 0x0ae293baU, 0xe5c0a02aU, 0x433c22e0U, 0x1d121b17U,
- 0x0b0e090dU, 0xadf28bc7U, 0xb92db6a8U, 0xc8141ea9U,
- 0x8557f119U, 0x4caf7507U, 0xbbee99ddU, 0xfda37f60U,
- 0x9ff70126U, 0xbc5c72f5U, 0xc544663bU, 0x345bfb7eU,
- 0x768b4329U, 0xdccb23c6U, 0x68b6edfcU, 0x63b8e4f1U,
- 0xcad731dcU, 0x10426385U, 0x40139722U, 0x2084c611U,
- 0x7d854a24U, 0xf8d2bb3dU, 0x11aef932U, 0x6dc729a1U,
- 0x4b1d9e2fU, 0xf3dcb230U, 0xec0d8652U, 0xd077c1e3U,
- 0x6c2bb316U, 0x99a970b9U, 0xfa119448U, 0x2247e964U,
- 0xc4a8fc8cU, 0x1aa0f03fU, 0xd8567d2cU, 0xef223390U,
- 0xc787494eU, 0xc1d938d1U, 0xfe8ccaa2U, 0x3698d40bU,
- 0xcfa6f581U, 0x28a57adeU, 0x26dab78eU, 0xa43fadbfU,
- 0xe42c3a9dU, 0x0d507892U, 0x9b6a5fccU, 0x62547e46U,
- 0xc2f68d13U, 0xe890d8b8U, 0x5e2e39f7U, 0xf582c3afU,
- 0xbe9f5d80U, 0x7c69d093U, 0xa96fd52dU, 0xb3cf2512U,
- 0x3bc8ac99U, 0xa710187dU, 0x6ee89c63U, 0x7bdb3bbbU,
- 0x09cd2678U, 0xf46e5918U, 0x01ec9ab7U, 0xa8834f9aU,
- 0x65e6956eU, 0x7eaaffe6U, 0x0821bccfU, 0xe6ef15e8U,
- 0xd9bae79bU, 0xce4a6f36U, 0xd4ea9f09U, 0xd629b07cU,
- 0xaf31a4b2U, 0x312a3f23U, 0x30c6a594U, 0xc035a266U,
- 0x37744ebcU, 0xa6fc82caU, 0xb0e090d0U, 0x1533a7d8U,
- 0x4af10498U, 0xf741ecdaU, 0x0e7fcd50U, 0x2f1791f6U,
- 0x8d764dd6U, 0x4d43efb0U, 0x54ccaa4dU, 0xdfe49604U,
- 0xe39ed1b5U, 0x1b4c6a88U, 0xb8c12c1fU, 0x7f466551U,
- 0x049d5eeaU, 0x5d018c35U, 0x73fa8774U, 0x2efb0b41U,
- 0x5ab3671dU, 0x5292dbd2U, 0x33e91056U, 0x136dd647U,
- 0x8c9ad761U, 0x7a37a10cU, 0x8e59f814U, 0x89eb133cU,
- 0xeecea927U, 0x35b761c9U, 0xede11ce5U, 0x3c7a47b1U,
- 0x599cd2dfU, 0x3f55f273U, 0x791814ceU, 0xbf73c737U,
- 0xea53f7cdU, 0x5b5ffdaaU, 0x14df3d6fU, 0x867844dbU,
- 0x81caaff3U, 0x3eb968c4U, 0x2c382434U, 0x5fc2a340U,
- 0x72161dc3U, 0x0cbce225U, 0x8b283c49U, 0x41ff0d95U,
- 0x7139a801U, 0xde080cb3U, 0x9cd8b4e4U, 0x906456c1U,
- 0x617bcb84U, 0x70d532b6U, 0x74486c5cU, 0x42d0b857U,
-},
-{
- 0xa75051f4U, 0x65537e41U, 0xa4c31a17U, 0x5e963a27U,
- 0x6bcb3babU, 0x45f11f9dU, 0x58abacfaU, 0x03934be3U,
- 0xfa552030U, 0x6df6ad76U, 0x769188ccU, 0x4c25f502U,
- 0xd7fc4fe5U, 0xcbd7c52aU, 0x44802635U, 0xa38fb562U,
- 0x5a49deb1U, 0x1b6725baU, 0x0e9845eaU, 0xc0e15dfeU,
- 0x7502c32fU, 0xf012814cU, 0x97a38d46U, 0xf9c66bd3U,
- 0x5fe7038fU, 0x9c951592U, 0x7aebbf6dU, 0x59da9552U,
- 0x832dd4beU, 0x21d35874U, 0x692949e0U, 0xc8448ec9U,
- 0x896a75c2U, 0x7978f48eU, 0x3e6b9958U, 0x71dd27b9U,
- 0x4fb6bee1U, 0xad17f088U, 0xac66c920U, 0x3ab47dceU,
- 0x4a1863dfU, 0x3182e51aU, 0x33609751U, 0x7f456253U,
- 0x77e0b164U, 0xae84bb6bU, 0xa01cfe81U, 0x2b94f908U,
- 0x68587048U, 0xfd198f45U, 0x6c8794deU, 0xf8b7527bU,
- 0xd323ab73U, 0x02e2724bU, 0x8f57e31fU, 0xab2a6655U,
- 0x2807b2ebU, 0xc2032fb5U, 0x7b9a86c5U, 0x08a5d337U,
- 0x87f23028U, 0xa5b223bfU, 0x6aba0203U, 0x825ced16U,
- 0x1c2b8acfU, 0xb492a779U, 0xf2f0f307U, 0xe2a14e69U,
- 0xf4cd65daU, 0xbed50605U, 0x621fd134U, 0xfe8ac4a6U,
- 0x539d342eU, 0x55a0a2f3U, 0xe132058aU, 0xeb75a4f6U,
- 0xec390b83U, 0xefaa4060U, 0x9f065e71U, 0x1051bd6eU,
-
- 0x8af93e21U, 0x063d96ddU, 0x05aedd3eU, 0xbd464de6U,
- 0x8db59154U, 0x5d0571c4U, 0xd46f0406U, 0x15ff6050U,
- 0xfb241998U, 0xe997d6bdU, 0x43cc8940U, 0x9e7767d9U,
- 0x42bdb0e8U, 0x8b880789U, 0x5b38e719U, 0xeedb79c8U,
- 0x0a47a17cU, 0x0fe97c42U, 0x1ec9f884U, 0x00000000U,
- 0x86830980U, 0xed48322bU, 0x70ac1e11U, 0x724e6c5aU,
- 0xfffbfd0eU, 0x38560f85U, 0xd51e3daeU, 0x3927362dU,
- 0xd9640a0fU, 0xa621685cU, 0x54d19b5bU, 0x2e3a2436U,
- 0x67b10c0aU, 0xe70f9357U, 0x96d2b4eeU, 0x919e1b9bU,
- 0xc54f80c0U, 0x20a261dcU, 0x4b695a77U, 0x1a161c12U,
- 0xba0ae293U, 0x2ae5c0a0U, 0xe0433c22U, 0x171d121bU,
- 0x0d0b0e09U, 0xc7adf28bU, 0xa8b92db6U, 0xa9c8141eU,
- 0x198557f1U, 0x074caf75U, 0xddbbee99U, 0x60fda37fU,
- 0x269ff701U, 0xf5bc5c72U, 0x3bc54466U, 0x7e345bfbU,
- 0x29768b43U, 0xc6dccb23U, 0xfc68b6edU, 0xf163b8e4U,
- 0xdccad731U, 0x85104263U, 0x22401397U, 0x112084c6U,
- 0x247d854aU, 0x3df8d2bbU, 0x3211aef9U, 0xa16dc729U,
- 0x2f4b1d9eU, 0x30f3dcb2U, 0x52ec0d86U, 0xe3d077c1U,
- 0x166c2bb3U, 0xb999a970U, 0x48fa1194U, 0x642247e9U,
- 0x8cc4a8fcU, 0x3f1aa0f0U, 0x2cd8567dU, 0x90ef2233U,
- 0x4ec78749U, 0xd1c1d938U, 0xa2fe8ccaU, 0x0b3698d4U,
- 0x81cfa6f5U, 0xde28a57aU, 0x8e26dab7U, 0xbfa43fadU,
- 0x9de42c3aU, 0x920d5078U, 0xcc9b6a5fU, 0x4662547eU,
- 0x13c2f68dU, 0xb8e890d8U, 0xf75e2e39U, 0xaff582c3U,
- 0x80be9f5dU, 0x937c69d0U, 0x2da96fd5U, 0x12b3cf25U,
- 0x993bc8acU, 0x7da71018U, 0x636ee89cU, 0xbb7bdb3bU,
- 0x7809cd26U, 0x18f46e59U, 0xb701ec9aU, 0x9aa8834fU,
- 0x6e65e695U, 0xe67eaaffU, 0xcf0821bcU, 0xe8e6ef15U,
- 0x9bd9bae7U, 0x36ce4a6fU, 0x09d4ea9fU, 0x7cd629b0U,
- 0xb2af31a4U, 0x23312a3fU, 0x9430c6a5U, 0x66c035a2U,
- 0xbc37744eU, 0xcaa6fc82U, 0xd0b0e090U, 0xd81533a7U,
- 0x984af104U, 0xdaf741ecU, 0x500e7fcdU, 0xf62f1791U,
- 0xd68d764dU, 0xb04d43efU, 0x4d54ccaaU, 0x04dfe496U,
- 0xb5e39ed1U, 0x881b4c6aU, 0x1fb8c12cU, 0x517f4665U,
- 0xea049d5eU, 0x355d018cU, 0x7473fa87U, 0x412efb0bU,
- 0x1d5ab367U, 0xd25292dbU, 0x5633e910U, 0x47136dd6U,
- 0x618c9ad7U, 0x0c7a37a1U, 0x148e59f8U, 0x3c89eb13U,
- 0x27eecea9U, 0xc935b761U, 0xe5ede11cU, 0xb13c7a47U,
- 0xdf599cd2U, 0x733f55f2U, 0xce791814U, 0x37bf73c7U,
- 0xcdea53f7U, 0xaa5b5ffdU, 0x6f14df3dU, 0xdb867844U,
- 0xf381caafU, 0xc43eb968U, 0x342c3824U, 0x405fc2a3U,
- 0xc372161dU, 0x250cbce2U, 0x498b283cU, 0x9541ff0dU,
- 0x017139a8U, 0xb3de080cU, 0xe49cd8b4U, 0xc1906456U,
- 0x84617bcbU, 0xb670d532U, 0x5c74486cU, 0x5742d0b8U,
-},
-{
- 0xf4a75051U, 0x4165537eU, 0x17a4c31aU, 0x275e963aU,
- 0xab6bcb3bU, 0x9d45f11fU, 0xfa58abacU, 0xe303934bU,
- 0x30fa5520U, 0x766df6adU, 0xcc769188U, 0x024c25f5U,
- 0xe5d7fc4fU, 0x2acbd7c5U, 0x35448026U, 0x62a38fb5U,
- 0xb15a49deU, 0xba1b6725U, 0xea0e9845U, 0xfec0e15dU,
- 0x2f7502c3U, 0x4cf01281U, 0x4697a38dU, 0xd3f9c66bU,
- 0x8f5fe703U, 0x929c9515U, 0x6d7aebbfU, 0x5259da95U,
- 0xbe832dd4U, 0x7421d358U, 0xe0692949U, 0xc9c8448eU,
- 0xc2896a75U, 0x8e7978f4U, 0x583e6b99U, 0xb971dd27U,
- 0xe14fb6beU, 0x88ad17f0U, 0x20ac66c9U, 0xce3ab47dU,
- 0xdf4a1863U, 0x1a3182e5U, 0x51336097U, 0x537f4562U,
- 0x6477e0b1U, 0x6bae84bbU, 0x81a01cfeU, 0x082b94f9U,
- 0x48685870U, 0x45fd198fU, 0xde6c8794U, 0x7bf8b752U,
- 0x73d323abU, 0x4b02e272U, 0x1f8f57e3U, 0x55ab2a66U,
- 0xeb2807b2U, 0xb5c2032fU, 0xc57b9a86U, 0x3708a5d3U,
- 0x2887f230U, 0xbfa5b223U, 0x036aba02U, 0x16825cedU,
- 0xcf1c2b8aU, 0x79b492a7U, 0x07f2f0f3U, 0x69e2a14eU,
- 0xdaf4cd65U, 0x05bed506U, 0x34621fd1U, 0xa6fe8ac4U,
- 0x2e539d34U, 0xf355a0a2U, 0x8ae13205U, 0xf6eb75a4U,
- 0x83ec390bU, 0x60efaa40U, 0x719f065eU, 0x6e1051bdU,
- 0x218af93eU, 0xdd063d96U, 0x3e05aeddU, 0xe6bd464dU,
- 0x548db591U, 0xc45d0571U, 0x06d46f04U, 0x5015ff60U,
- 0x98fb2419U, 0xbde997d6U, 0x4043cc89U, 0xd99e7767U,
- 0xe842bdb0U, 0x898b8807U, 0x195b38e7U, 0xc8eedb79U,
- 0x7c0a47a1U, 0x420fe97cU, 0x841ec9f8U, 0x00000000U,
- 0x80868309U, 0x2bed4832U, 0x1170ac1eU, 0x5a724e6cU,
- 0x0efffbfdU, 0x8538560fU, 0xaed51e3dU, 0x2d392736U,
- 0x0fd9640aU, 0x5ca62168U, 0x5b54d19bU, 0x362e3a24U,
- 0x0a67b10cU, 0x57e70f93U, 0xee96d2b4U, 0x9b919e1bU,
- 0xc0c54f80U, 0xdc20a261U, 0x774b695aU, 0x121a161cU,
- 0x93ba0ae2U, 0xa02ae5c0U, 0x22e0433cU, 0x1b171d12U,
- 0x090d0b0eU, 0x8bc7adf2U, 0xb6a8b92dU, 0x1ea9c814U,
- 0xf1198557U, 0x75074cafU, 0x99ddbbeeU, 0x7f60fda3U,
- 0x01269ff7U, 0x72f5bc5cU, 0x663bc544U, 0xfb7e345bU,
- 0x4329768bU, 0x23c6dccbU, 0xedfc68b6U, 0xe4f163b8U,
- 0x31dccad7U, 0x63851042U, 0x97224013U, 0xc6112084U,
- 0x4a247d85U, 0xbb3df8d2U, 0xf93211aeU, 0x29a16dc7U,
- 0x9e2f4b1dU, 0xb230f3dcU, 0x8652ec0dU, 0xc1e3d077U,
- 0xb3166c2bU, 0x70b999a9U, 0x9448fa11U, 0xe9642247U,
- 0xfc8cc4a8U, 0xf03f1aa0U, 0x7d2cd856U, 0x3390ef22U,
- 0x494ec787U, 0x38d1c1d9U, 0xcaa2fe8cU, 0xd40b3698U,
- 0xf581cfa6U, 0x7ade28a5U, 0xb78e26daU, 0xadbfa43fU,
- 0x3a9de42cU, 0x78920d50U, 0x5fcc9b6aU, 0x7e466254U,
- 0x8d13c2f6U, 0xd8b8e890U, 0x39f75e2eU, 0xc3aff582U,
- 0x5d80be9fU, 0xd0937c69U, 0xd52da96fU, 0x2512b3cfU,
- 0xac993bc8U, 0x187da710U, 0x9c636ee8U, 0x3bbb7bdbU,
- 0x267809cdU, 0x5918f46eU, 0x9ab701ecU, 0x4f9aa883U,
- 0x956e65e6U, 0xffe67eaaU, 0xbccf0821U, 0x15e8e6efU,
- 0xe79bd9baU, 0x6f36ce4aU, 0x9f09d4eaU, 0xb07cd629U,
- 0xa4b2af31U, 0x3f23312aU, 0xa59430c6U, 0xa266c035U,
- 0x4ebc3774U, 0x82caa6fcU, 0x90d0b0e0U, 0xa7d81533U,
- 0x04984af1U, 0xecdaf741U, 0xcd500e7fU, 0x91f62f17U,
- 0x4dd68d76U, 0xefb04d43U, 0xaa4d54ccU, 0x9604dfe4U,
- 0xd1b5e39eU, 0x6a881b4cU, 0x2c1fb8c1U, 0x65517f46U,
- 0x5eea049dU, 0x8c355d01U, 0x877473faU, 0x0b412efbU,
- 0x671d5ab3U, 0xdbd25292U, 0x105633e9U, 0xd647136dU,
- 0xd7618c9aU, 0xa10c7a37U, 0xf8148e59U, 0x133c89ebU,
- 0xa927eeceU, 0x61c935b7U, 0x1ce5ede1U, 0x47b13c7aU,
- 0xd2df599cU, 0xf2733f55U, 0x14ce7918U, 0xc737bf73U,
- 0xf7cdea53U, 0xfdaa5b5fU, 0x3d6f14dfU, 0x44db8678U,
- 0xaff381caU, 0x68c43eb9U, 0x24342c38U, 0xa3405fc2U,
- 0x1dc37216U, 0xe2250cbcU, 0x3c498b28U, 0x0d9541ffU,
- 0xa8017139U, 0x0cb3de08U, 0xb4e49cd8U, 0x56c19064U,
- 0xcb84617bU, 0x32b670d5U, 0x6c5c7448U, 0xb85742d0U,
-}
-};
-#endif /* HAVE_AES_DECRYPT */
-#endif
-
-#ifdef HAVE_AES_DECRYPT
-#if (defined(HAVE_AES_CBC) && !defined(WOLFSSL_DEVCRYPTO_CBC)) \
- || defined(WOLFSSL_AES_DIRECT)
-static const byte Td4[256] =
-{
- 0x52U, 0x09U, 0x6aU, 0xd5U, 0x30U, 0x36U, 0xa5U, 0x38U,
- 0xbfU, 0x40U, 0xa3U, 0x9eU, 0x81U, 0xf3U, 0xd7U, 0xfbU,
- 0x7cU, 0xe3U, 0x39U, 0x82U, 0x9bU, 0x2fU, 0xffU, 0x87U,
- 0x34U, 0x8eU, 0x43U, 0x44U, 0xc4U, 0xdeU, 0xe9U, 0xcbU,
- 0x54U, 0x7bU, 0x94U, 0x32U, 0xa6U, 0xc2U, 0x23U, 0x3dU,
- 0xeeU, 0x4cU, 0x95U, 0x0bU, 0x42U, 0xfaU, 0xc3U, 0x4eU,
- 0x08U, 0x2eU, 0xa1U, 0x66U, 0x28U, 0xd9U, 0x24U, 0xb2U,
- 0x76U, 0x5bU, 0xa2U, 0x49U, 0x6dU, 0x8bU, 0xd1U, 0x25U,
- 0x72U, 0xf8U, 0xf6U, 0x64U, 0x86U, 0x68U, 0x98U, 0x16U,
- 0xd4U, 0xa4U, 0x5cU, 0xccU, 0x5dU, 0x65U, 0xb6U, 0x92U,
- 0x6cU, 0x70U, 0x48U, 0x50U, 0xfdU, 0xedU, 0xb9U, 0xdaU,
- 0x5eU, 0x15U, 0x46U, 0x57U, 0xa7U, 0x8dU, 0x9dU, 0x84U,
- 0x90U, 0xd8U, 0xabU, 0x00U, 0x8cU, 0xbcU, 0xd3U, 0x0aU,
- 0xf7U, 0xe4U, 0x58U, 0x05U, 0xb8U, 0xb3U, 0x45U, 0x06U,
- 0xd0U, 0x2cU, 0x1eU, 0x8fU, 0xcaU, 0x3fU, 0x0fU, 0x02U,
- 0xc1U, 0xafU, 0xbdU, 0x03U, 0x01U, 0x13U, 0x8aU, 0x6bU,
- 0x3aU, 0x91U, 0x11U, 0x41U, 0x4fU, 0x67U, 0xdcU, 0xeaU,
- 0x97U, 0xf2U, 0xcfU, 0xceU, 0xf0U, 0xb4U, 0xe6U, 0x73U,
- 0x96U, 0xacU, 0x74U, 0x22U, 0xe7U, 0xadU, 0x35U, 0x85U,
- 0xe2U, 0xf9U, 0x37U, 0xe8U, 0x1cU, 0x75U, 0xdfU, 0x6eU,
- 0x47U, 0xf1U, 0x1aU, 0x71U, 0x1dU, 0x29U, 0xc5U, 0x89U,
- 0x6fU, 0xb7U, 0x62U, 0x0eU, 0xaaU, 0x18U, 0xbeU, 0x1bU,
- 0xfcU, 0x56U, 0x3eU, 0x4bU, 0xc6U, 0xd2U, 0x79U, 0x20U,
- 0x9aU, 0xdbU, 0xc0U, 0xfeU, 0x78U, 0xcdU, 0x5aU, 0xf4U,
- 0x1fU, 0xddU, 0xa8U, 0x33U, 0x88U, 0x07U, 0xc7U, 0x31U,
- 0xb1U, 0x12U, 0x10U, 0x59U, 0x27U, 0x80U, 0xecU, 0x5fU,
- 0x60U, 0x51U, 0x7fU, 0xa9U, 0x19U, 0xb5U, 0x4aU, 0x0dU,
- 0x2dU, 0xe5U, 0x7aU, 0x9fU, 0x93U, 0xc9U, 0x9cU, 0xefU,
- 0xa0U, 0xe0U, 0x3bU, 0x4dU, 0xaeU, 0x2aU, 0xf5U, 0xb0U,
- 0xc8U, 0xebU, 0xbbU, 0x3cU, 0x83U, 0x53U, 0x99U, 0x61U,
- 0x17U, 0x2bU, 0x04U, 0x7eU, 0xbaU, 0x77U, 0xd6U, 0x26U,
- 0xe1U, 0x69U, 0x14U, 0x63U, 0x55U, 0x21U, 0x0cU, 0x7dU,
-};
-#endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
-#endif /* HAVE_AES_DECRYPT */
-
-#define GETBYTE(x, y) (word32)((byte)((x) >> (8 * (y))))
-
-#ifdef WOLFSSL_AES_SMALL_TABLES
-static const byte Tsbox[256] = {
- 0x63U, 0x7cU, 0x77U, 0x7bU, 0xf2U, 0x6bU, 0x6fU, 0xc5U,
- 0x30U, 0x01U, 0x67U, 0x2bU, 0xfeU, 0xd7U, 0xabU, 0x76U,
- 0xcaU, 0x82U, 0xc9U, 0x7dU, 0xfaU, 0x59U, 0x47U, 0xf0U,
- 0xadU, 0xd4U, 0xa2U, 0xafU, 0x9cU, 0xa4U, 0x72U, 0xc0U,
- 0xb7U, 0xfdU, 0x93U, 0x26U, 0x36U, 0x3fU, 0xf7U, 0xccU,
- 0x34U, 0xa5U, 0xe5U, 0xf1U, 0x71U, 0xd8U, 0x31U, 0x15U,
- 0x04U, 0xc7U, 0x23U, 0xc3U, 0x18U, 0x96U, 0x05U, 0x9aU,
- 0x07U, 0x12U, 0x80U, 0xe2U, 0xebU, 0x27U, 0xb2U, 0x75U,
- 0x09U, 0x83U, 0x2cU, 0x1aU, 0x1bU, 0x6eU, 0x5aU, 0xa0U,
- 0x52U, 0x3bU, 0xd6U, 0xb3U, 0x29U, 0xe3U, 0x2fU, 0x84U,
- 0x53U, 0xd1U, 0x00U, 0xedU, 0x20U, 0xfcU, 0xb1U, 0x5bU,
- 0x6aU, 0xcbU, 0xbeU, 0x39U, 0x4aU, 0x4cU, 0x58U, 0xcfU,
- 0xd0U, 0xefU, 0xaaU, 0xfbU, 0x43U, 0x4dU, 0x33U, 0x85U,
- 0x45U, 0xf9U, 0x02U, 0x7fU, 0x50U, 0x3cU, 0x9fU, 0xa8U,
- 0x51U, 0xa3U, 0x40U, 0x8fU, 0x92U, 0x9dU, 0x38U, 0xf5U,
- 0xbcU, 0xb6U, 0xdaU, 0x21U, 0x10U, 0xffU, 0xf3U, 0xd2U,
- 0xcdU, 0x0cU, 0x13U, 0xecU, 0x5fU, 0x97U, 0x44U, 0x17U,
- 0xc4U, 0xa7U, 0x7eU, 0x3dU, 0x64U, 0x5dU, 0x19U, 0x73U,
- 0x60U, 0x81U, 0x4fU, 0xdcU, 0x22U, 0x2aU, 0x90U, 0x88U,
- 0x46U, 0xeeU, 0xb8U, 0x14U, 0xdeU, 0x5eU, 0x0bU, 0xdbU,
- 0xe0U, 0x32U, 0x3aU, 0x0aU, 0x49U, 0x06U, 0x24U, 0x5cU,
- 0xc2U, 0xd3U, 0xacU, 0x62U, 0x91U, 0x95U, 0xe4U, 0x79U,
- 0xe7U, 0xc8U, 0x37U, 0x6dU, 0x8dU, 0xd5U, 0x4eU, 0xa9U,
- 0x6cU, 0x56U, 0xf4U, 0xeaU, 0x65U, 0x7aU, 0xaeU, 0x08U,
- 0xbaU, 0x78U, 0x25U, 0x2eU, 0x1cU, 0xa6U, 0xb4U, 0xc6U,
- 0xe8U, 0xddU, 0x74U, 0x1fU, 0x4bU, 0xbdU, 0x8bU, 0x8aU,
- 0x70U, 0x3eU, 0xb5U, 0x66U, 0x48U, 0x03U, 0xf6U, 0x0eU,
- 0x61U, 0x35U, 0x57U, 0xb9U, 0x86U, 0xc1U, 0x1dU, 0x9eU,
- 0xe1U, 0xf8U, 0x98U, 0x11U, 0x69U, 0xd9U, 0x8eU, 0x94U,
- 0x9bU, 0x1eU, 0x87U, 0xe9U, 0xceU, 0x55U, 0x28U, 0xdfU,
- 0x8cU, 0xa1U, 0x89U, 0x0dU, 0xbfU, 0xe6U, 0x42U, 0x68U,
- 0x41U, 0x99U, 0x2dU, 0x0fU, 0xb0U, 0x54U, 0xbbU, 0x16U
-};
-
-#define AES_XTIME(x) ((byte)((byte)((x) << 1) ^ ((0 - ((x) >> 7)) & 0x1b)))
-
-static word32 col_mul(word32 t, int i2, int i3, int ia, int ib)
-{
- byte t3 = GETBYTE(t, i3);
- byte tm = AES_XTIME(GETBYTE(t, i2) ^ t3);
-
- return GETBYTE(t, ia) ^ GETBYTE(t, ib) ^ t3 ^ tm;
-}
-
-static word32 inv_col_mul(word32 t, int i9, int ib, int id, int ie)
-{
- byte t9 = GETBYTE(t, i9);
- byte tb = GETBYTE(t, ib);
- byte td = GETBYTE(t, id);
- byte te = GETBYTE(t, ie);
- byte t0 = t9 ^ tb ^ td;
- return t0 ^ AES_XTIME(AES_XTIME(AES_XTIME(t0 ^ te) ^ td ^ te) ^ tb ^ te);
-}
-#endif
-
-#if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT) || defined(HAVE_AESGCM)
-
-#ifndef WC_CACHE_LINE_SZ
- #if defined(__x86_64__) || defined(_M_X64) || \
- (defined(__ILP32__) && (__ILP32__ >= 1))
- #define WC_CACHE_LINE_SZ 64
- #else
- /* default cache line size */
- #define WC_CACHE_LINE_SZ 32
- #endif
-#endif
-
-
-#ifndef WOLFSSL_AES_SMALL_TABLES
-/* load 4 Te Tables into cache by cache line stride */
-static WC_INLINE word32 PreFetchTe(void)
-{
- word32 x = 0;
- int i,j;
-
- for (i = 0; i < 4; i++) {
- /* 256 elements, each one is 4 bytes */
- for (j = 0; j < 256; j += WC_CACHE_LINE_SZ/4) {
- x &= Te[i][j];
- }
- }
- return x;
-}
-#else
-/* load sbox into cache by cache line stride */
-static WC_INLINE word32 PreFetchSBox(void)
-{
- word32 x = 0;
- int i;
-
- for (i = 0; i < 256; i += WC_CACHE_LINE_SZ/4) {
- x &= Tsbox[i];
- }
- return x;
-}
-#endif
-
-/* Software AES - ECB Encrypt */
-static void wc_AesEncrypt(Aes* aes, const byte* inBlock, byte* outBlock)
-{
- word32 s0, s1, s2, s3;
- word32 t0, t1, t2, t3;
- word32 r = aes->rounds >> 1;
- const word32* rk = aes->key;
-
- if (r > 7 || r == 0) {
- WOLFSSL_MSG("AesEncrypt encountered improper key, set it up");
- return; /* stop instead of seg-faulting, set up your keys! */
- }
-
-#ifdef WOLFSSL_AESNI
- if (haveAESNI && aes->use_aesni) {
- #ifdef DEBUG_AESNI
- printf("about to aes encrypt\n");
- printf("in = %p\n", inBlock);
- printf("out = %p\n", outBlock);
- printf("aes->key = %p\n", aes->key);
- printf("aes->rounds = %d\n", aes->rounds);
- printf("sz = %d\n", AES_BLOCK_SIZE);
- #endif
-
- /* check alignment, decrypt doesn't need alignment */
- if ((wolfssl_word)inBlock % AESNI_ALIGN) {
- #ifndef NO_WOLFSSL_ALLOC_ALIGN
- byte* tmp = (byte*)XMALLOC(AES_BLOCK_SIZE + AESNI_ALIGN, aes->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- byte* tmp_align;
- if (tmp == NULL) return;
-
- tmp_align = tmp + (AESNI_ALIGN - ((size_t)tmp % AESNI_ALIGN));
-
- XMEMCPY(tmp_align, inBlock, AES_BLOCK_SIZE);
- AES_ECB_encrypt(tmp_align, tmp_align, AES_BLOCK_SIZE,
- (byte*)aes->key, aes->rounds);
- XMEMCPY(outBlock, tmp_align, AES_BLOCK_SIZE);
- XFREE(tmp, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return;
- #else
- WOLFSSL_MSG("AES-ECB encrypt with bad alignment");
- return;
- #endif
- }
-
- AES_ECB_encrypt(inBlock, outBlock, AES_BLOCK_SIZE, (byte*)aes->key,
- aes->rounds);
-
- return;
- }
- else {
- #ifdef DEBUG_AESNI
- printf("Skipping AES-NI\n");
- #endif
- }
-#endif
-#if defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES)
- AES_ECB_encrypt(aes, inBlock, outBlock, AES_BLOCK_SIZE);
- return;
-#endif
-
- /*
- * map byte array block to cipher state
- * and add initial round key:
- */
- XMEMCPY(&s0, inBlock, sizeof(s0));
- XMEMCPY(&s1, inBlock + sizeof(s0), sizeof(s1));
- XMEMCPY(&s2, inBlock + 2 * sizeof(s0), sizeof(s2));
- XMEMCPY(&s3, inBlock + 3 * sizeof(s0), sizeof(s3));
-
-#ifdef LITTLE_ENDIAN_ORDER
- s0 = ByteReverseWord32(s0);
- s1 = ByteReverseWord32(s1);
- s2 = ByteReverseWord32(s2);
- s3 = ByteReverseWord32(s3);
-#endif
-
- /* AddRoundKey */
- s0 ^= rk[0];
- s1 ^= rk[1];
- s2 ^= rk[2];
- s3 ^= rk[3];
-
-#ifndef WOLFSSL_AES_SMALL_TABLES
- s0 |= PreFetchTe();
-
- /*
- * Nr - 1 full rounds:
- */
-
- for (;;) {
- t0 =
- Te[0][GETBYTE(s0, 3)] ^
- Te[1][GETBYTE(s1, 2)] ^
- Te[2][GETBYTE(s2, 1)] ^
- Te[3][GETBYTE(s3, 0)] ^
- rk[4];
- t1 =
- Te[0][GETBYTE(s1, 3)] ^
- Te[1][GETBYTE(s2, 2)] ^
- Te[2][GETBYTE(s3, 1)] ^
- Te[3][GETBYTE(s0, 0)] ^
- rk[5];
- t2 =
- Te[0][GETBYTE(s2, 3)] ^
- Te[1][GETBYTE(s3, 2)] ^
- Te[2][GETBYTE(s0, 1)] ^
- Te[3][GETBYTE(s1, 0)] ^
- rk[6];
- t3 =
- Te[0][GETBYTE(s3, 3)] ^
- Te[1][GETBYTE(s0, 2)] ^
- Te[2][GETBYTE(s1, 1)] ^
- Te[3][GETBYTE(s2, 0)] ^
- rk[7];
-
- rk += 8;
- if (--r == 0) {
- break;
- }
-
- s0 =
- Te[0][GETBYTE(t0, 3)] ^
- Te[1][GETBYTE(t1, 2)] ^
- Te[2][GETBYTE(t2, 1)] ^
- Te[3][GETBYTE(t3, 0)] ^
- rk[0];
- s1 =
- Te[0][GETBYTE(t1, 3)] ^
- Te[1][GETBYTE(t2, 2)] ^
- Te[2][GETBYTE(t3, 1)] ^
- Te[3][GETBYTE(t0, 0)] ^
- rk[1];
- s2 =
- Te[0][GETBYTE(t2, 3)] ^
- Te[1][GETBYTE(t3, 2)] ^
- Te[2][GETBYTE(t0, 1)] ^
- Te[3][GETBYTE(t1, 0)] ^
- rk[2];
- s3 =
- Te[0][GETBYTE(t3, 3)] ^
- Te[1][GETBYTE(t0, 2)] ^
- Te[2][GETBYTE(t1, 1)] ^
- Te[3][GETBYTE(t2, 0)] ^
- rk[3];
- }
-
- /*
- * apply last round and
- * map cipher state to byte array block:
- */
-
- s0 =
- (Te[2][GETBYTE(t0, 3)] & 0xff000000) ^
- (Te[3][GETBYTE(t1, 2)] & 0x00ff0000) ^
- (Te[0][GETBYTE(t2, 1)] & 0x0000ff00) ^
- (Te[1][GETBYTE(t3, 0)] & 0x000000ff) ^
- rk[0];
- s1 =
- (Te[2][GETBYTE(t1, 3)] & 0xff000000) ^
- (Te[3][GETBYTE(t2, 2)] & 0x00ff0000) ^
- (Te[0][GETBYTE(t3, 1)] & 0x0000ff00) ^
- (Te[1][GETBYTE(t0, 0)] & 0x000000ff) ^
- rk[1];
- s2 =
- (Te[2][GETBYTE(t2, 3)] & 0xff000000) ^
- (Te[3][GETBYTE(t3, 2)] & 0x00ff0000) ^
- (Te[0][GETBYTE(t0, 1)] & 0x0000ff00) ^
- (Te[1][GETBYTE(t1, 0)] & 0x000000ff) ^
- rk[2];
- s3 =
- (Te[2][GETBYTE(t3, 3)] & 0xff000000) ^
- (Te[3][GETBYTE(t0, 2)] & 0x00ff0000) ^
- (Te[0][GETBYTE(t1, 1)] & 0x0000ff00) ^
- (Te[1][GETBYTE(t2, 0)] & 0x000000ff) ^
- rk[3];
-#else
- s0 |= PreFetchSBox();
-
- r *= 2;
- /* Two rounds at a time */
- for (rk += 4; r > 1; r--, rk += 4) {
- t0 =
- ((word32)Tsbox[GETBYTE(s0, 3)] << 24) ^
- ((word32)Tsbox[GETBYTE(s1, 2)] << 16) ^
- ((word32)Tsbox[GETBYTE(s2, 1)] << 8) ^
- ((word32)Tsbox[GETBYTE(s3, 0)]);
- t1 =
- ((word32)Tsbox[GETBYTE(s1, 3)] << 24) ^
- ((word32)Tsbox[GETBYTE(s2, 2)] << 16) ^
- ((word32)Tsbox[GETBYTE(s3, 1)] << 8) ^
- ((word32)Tsbox[GETBYTE(s0, 0)]);
- t2 =
- ((word32)Tsbox[GETBYTE(s2, 3)] << 24) ^
- ((word32)Tsbox[GETBYTE(s3, 2)] << 16) ^
- ((word32)Tsbox[GETBYTE(s0, 1)] << 8) ^
- ((word32)Tsbox[GETBYTE(s1, 0)]);
- t3 =
- ((word32)Tsbox[GETBYTE(s3, 3)] << 24) ^
- ((word32)Tsbox[GETBYTE(s0, 2)] << 16) ^
- ((word32)Tsbox[GETBYTE(s1, 1)] << 8) ^
- ((word32)Tsbox[GETBYTE(s2, 0)]);
-
- s0 =
- (col_mul(t0, 3, 2, 0, 1) << 24) ^
- (col_mul(t0, 2, 1, 0, 3) << 16) ^
- (col_mul(t0, 1, 0, 2, 3) << 8) ^
- (col_mul(t0, 0, 3, 2, 1) ) ^
- rk[0];
- s1 =
- (col_mul(t1, 3, 2, 0, 1) << 24) ^
- (col_mul(t1, 2, 1, 0, 3) << 16) ^
- (col_mul(t1, 1, 0, 2, 3) << 8) ^
- (col_mul(t1, 0, 3, 2, 1) ) ^
- rk[1];
- s2 =
- (col_mul(t2, 3, 2, 0, 1) << 24) ^
- (col_mul(t2, 2, 1, 0, 3) << 16) ^
- (col_mul(t2, 1, 0, 2, 3) << 8) ^
- (col_mul(t2, 0, 3, 2, 1) ) ^
- rk[2];
- s3 =
- (col_mul(t3, 3, 2, 0, 1) << 24) ^
- (col_mul(t3, 2, 1, 0, 3) << 16) ^
- (col_mul(t3, 1, 0, 2, 3) << 8) ^
- (col_mul(t3, 0, 3, 2, 1) ) ^
- rk[3];
- }
-
- t0 =
- ((word32)Tsbox[GETBYTE(s0, 3)] << 24) ^
- ((word32)Tsbox[GETBYTE(s1, 2)] << 16) ^
- ((word32)Tsbox[GETBYTE(s2, 1)] << 8) ^
- ((word32)Tsbox[GETBYTE(s3, 0)]);
- t1 =
- ((word32)Tsbox[GETBYTE(s1, 3)] << 24) ^
- ((word32)Tsbox[GETBYTE(s2, 2)] << 16) ^
- ((word32)Tsbox[GETBYTE(s3, 1)] << 8) ^
- ((word32)Tsbox[GETBYTE(s0, 0)]);
- t2 =
- ((word32)Tsbox[GETBYTE(s2, 3)] << 24) ^
- ((word32)Tsbox[GETBYTE(s3, 2)] << 16) ^
- ((word32)Tsbox[GETBYTE(s0, 1)] << 8) ^
- ((word32)Tsbox[GETBYTE(s1, 0)]);
- t3 =
- ((word32)Tsbox[GETBYTE(s3, 3)] << 24) ^
- ((word32)Tsbox[GETBYTE(s0, 2)] << 16) ^
- ((word32)Tsbox[GETBYTE(s1, 1)] << 8) ^
- ((word32)Tsbox[GETBYTE(s2, 0)]);
- s0 = t0 ^ rk[0];
- s1 = t1 ^ rk[1];
- s2 = t2 ^ rk[2];
- s3 = t3 ^ rk[3];
-#endif
-
- /* write out */
-#ifdef LITTLE_ENDIAN_ORDER
- s0 = ByteReverseWord32(s0);
- s1 = ByteReverseWord32(s1);
- s2 = ByteReverseWord32(s2);
- s3 = ByteReverseWord32(s3);
-#endif
-
- XMEMCPY(outBlock, &s0, sizeof(s0));
- XMEMCPY(outBlock + sizeof(s0), &s1, sizeof(s1));
- XMEMCPY(outBlock + 2 * sizeof(s0), &s2, sizeof(s2));
- XMEMCPY(outBlock + 3 * sizeof(s0), &s3, sizeof(s3));
-
-}
-#endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT || HAVE_AESGCM */
-
-#if defined(HAVE_AES_DECRYPT)
-#if (defined(HAVE_AES_CBC) && !defined(WOLFSSL_DEVCRYPTO_CBC)) || \
- defined(WOLFSSL_AES_DIRECT)
-
-#ifndef WOLFSSL_AES_SMALL_TABLES
-/* load 4 Td Tables into cache by cache line stride */
-static WC_INLINE word32 PreFetchTd(void)
-{
- word32 x = 0;
- int i,j;
-
- for (i = 0; i < 4; i++) {
- /* 256 elements, each one is 4 bytes */
- for (j = 0; j < 256; j += WC_CACHE_LINE_SZ/4) {
- x &= Td[i][j];
- }
- }
- return x;
-}
-#endif
-
-/* load Td Table4 into cache by cache line stride */
-static WC_INLINE word32 PreFetchTd4(void)
-{
- word32 x = 0;
- int i;
-
- for (i = 0; i < 256; i += WC_CACHE_LINE_SZ) {
- x &= (word32)Td4[i];
- }
- return x;
-}
-
-/* Software AES - ECB Decrypt */
-static void wc_AesDecrypt(Aes* aes, const byte* inBlock, byte* outBlock)
-{
- word32 s0, s1, s2, s3;
- word32 t0, t1, t2, t3;
- word32 r = aes->rounds >> 1;
-
- const word32* rk = aes->key;
- if (r > 7 || r == 0) {
- WOLFSSL_MSG("AesDecrypt encountered improper key, set it up");
- return; /* stop instead of seg-faulting, set up your keys! */
- }
-#ifdef WOLFSSL_AESNI
- if (haveAESNI && aes->use_aesni) {
- #ifdef DEBUG_AESNI
- printf("about to aes decrypt\n");
- printf("in = %p\n", inBlock);
- printf("out = %p\n", outBlock);
- printf("aes->key = %p\n", aes->key);
- printf("aes->rounds = %d\n", aes->rounds);
- printf("sz = %d\n", AES_BLOCK_SIZE);
- #endif
-
- /* if input and output same will overwrite input iv */
- if ((const byte*)aes->tmp != inBlock)
- XMEMCPY(aes->tmp, inBlock, AES_BLOCK_SIZE);
- AES_ECB_decrypt(inBlock, outBlock, AES_BLOCK_SIZE, (byte*)aes->key,
- aes->rounds);
- return;
- }
- else {
- #ifdef DEBUG_AESNI
- printf("Skipping AES-NI\n");
- #endif
- }
-#endif /* WOLFSSL_AESNI */
-#if defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES)
- return AES_ECB_decrypt(aes, inBlock, outBlock, AES_BLOCK_SIZE);
-#endif
-
- /*
- * map byte array block to cipher state
- * and add initial round key:
- */
- XMEMCPY(&s0, inBlock, sizeof(s0));
- XMEMCPY(&s1, inBlock + sizeof(s0), sizeof(s1));
- XMEMCPY(&s2, inBlock + 2 * sizeof(s0), sizeof(s2));
- XMEMCPY(&s3, inBlock + 3 * sizeof(s0), sizeof(s3));
-
-#ifdef LITTLE_ENDIAN_ORDER
- s0 = ByteReverseWord32(s0);
- s1 = ByteReverseWord32(s1);
- s2 = ByteReverseWord32(s2);
- s3 = ByteReverseWord32(s3);
-#endif
-
- s0 ^= rk[0];
- s1 ^= rk[1];
- s2 ^= rk[2];
- s3 ^= rk[3];
-
-#ifndef WOLFSSL_AES_SMALL_TABLES
- s0 |= PreFetchTd();
-
- /*
- * Nr - 1 full rounds:
- */
-
- for (;;) {
- t0 =
- Td[0][GETBYTE(s0, 3)] ^
- Td[1][GETBYTE(s3, 2)] ^
- Td[2][GETBYTE(s2, 1)] ^
- Td[3][GETBYTE(s1, 0)] ^
- rk[4];
- t1 =
- Td[0][GETBYTE(s1, 3)] ^
- Td[1][GETBYTE(s0, 2)] ^
- Td[2][GETBYTE(s3, 1)] ^
- Td[3][GETBYTE(s2, 0)] ^
- rk[5];
- t2 =
- Td[0][GETBYTE(s2, 3)] ^
- Td[1][GETBYTE(s1, 2)] ^
- Td[2][GETBYTE(s0, 1)] ^
- Td[3][GETBYTE(s3, 0)] ^
- rk[6];
- t3 =
- Td[0][GETBYTE(s3, 3)] ^
- Td[1][GETBYTE(s2, 2)] ^
- Td[2][GETBYTE(s1, 1)] ^
- Td[3][GETBYTE(s0, 0)] ^
- rk[7];
-
- rk += 8;
- if (--r == 0) {
- break;
- }
-
- s0 =
- Td[0][GETBYTE(t0, 3)] ^
- Td[1][GETBYTE(t3, 2)] ^
- Td[2][GETBYTE(t2, 1)] ^
- Td[3][GETBYTE(t1, 0)] ^
- rk[0];
- s1 =
- Td[0][GETBYTE(t1, 3)] ^
- Td[1][GETBYTE(t0, 2)] ^
- Td[2][GETBYTE(t3, 1)] ^
- Td[3][GETBYTE(t2, 0)] ^
- rk[1];
- s2 =
- Td[0][GETBYTE(t2, 3)] ^
- Td[1][GETBYTE(t1, 2)] ^
- Td[2][GETBYTE(t0, 1)] ^
- Td[3][GETBYTE(t3, 0)] ^
- rk[2];
- s3 =
- Td[0][GETBYTE(t3, 3)] ^
- Td[1][GETBYTE(t2, 2)] ^
- Td[2][GETBYTE(t1, 1)] ^
- Td[3][GETBYTE(t0, 0)] ^
- rk[3];
- }
- /*
- * apply last round and
- * map cipher state to byte array block:
- */
-
- t0 |= PreFetchTd4();
-
- s0 =
- ((word32)Td4[GETBYTE(t0, 3)] << 24) ^
- ((word32)Td4[GETBYTE(t3, 2)] << 16) ^
- ((word32)Td4[GETBYTE(t2, 1)] << 8) ^
- ((word32)Td4[GETBYTE(t1, 0)]) ^
- rk[0];
- s1 =
- ((word32)Td4[GETBYTE(t1, 3)] << 24) ^
- ((word32)Td4[GETBYTE(t0, 2)] << 16) ^
- ((word32)Td4[GETBYTE(t3, 1)] << 8) ^
- ((word32)Td4[GETBYTE(t2, 0)]) ^
- rk[1];
- s2 =
- ((word32)Td4[GETBYTE(t2, 3)] << 24) ^
- ((word32)Td4[GETBYTE(t1, 2)] << 16) ^
- ((word32)Td4[GETBYTE(t0, 1)] << 8) ^
- ((word32)Td4[GETBYTE(t3, 0)]) ^
- rk[2];
- s3 =
- ((word32)Td4[GETBYTE(t3, 3)] << 24) ^
- ((word32)Td4[GETBYTE(t2, 2)] << 16) ^
- ((word32)Td4[GETBYTE(t1, 1)] << 8) ^
- ((word32)Td4[GETBYTE(t0, 0)]) ^
- rk[3];
-#else
- s0 |= PreFetchTd4();
-
- r *= 2;
- for (rk += 4; r > 1; r--, rk += 4) {
- t0 =
- ((word32)Td4[GETBYTE(s0, 3)] << 24) ^
- ((word32)Td4[GETBYTE(s3, 2)] << 16) ^
- ((word32)Td4[GETBYTE(s2, 1)] << 8) ^
- ((word32)Td4[GETBYTE(s1, 0)]) ^
- rk[0];
- t1 =
- ((word32)Td4[GETBYTE(s1, 3)] << 24) ^
- ((word32)Td4[GETBYTE(s0, 2)] << 16) ^
- ((word32)Td4[GETBYTE(s3, 1)] << 8) ^
- ((word32)Td4[GETBYTE(s2, 0)]) ^
- rk[1];
- t2 =
- ((word32)Td4[GETBYTE(s2, 3)] << 24) ^
- ((word32)Td4[GETBYTE(s1, 2)] << 16) ^
- ((word32)Td4[GETBYTE(s0, 1)] << 8) ^
- ((word32)Td4[GETBYTE(s3, 0)]) ^
- rk[2];
- t3 =
- ((word32)Td4[GETBYTE(s3, 3)] << 24) ^
- ((word32)Td4[GETBYTE(s2, 2)] << 16) ^
- ((word32)Td4[GETBYTE(s1, 1)] << 8) ^
- ((word32)Td4[GETBYTE(s0, 0)]) ^
- rk[3];
-
- s0 =
- (inv_col_mul(t0, 0, 2, 1, 3) << 24) ^
- (inv_col_mul(t0, 3, 1, 0, 2) << 16) ^
- (inv_col_mul(t0, 2, 0, 3, 1) << 8) ^
- (inv_col_mul(t0, 1, 3, 2, 0) );
- s1 =
- (inv_col_mul(t1, 0, 2, 1, 3) << 24) ^
- (inv_col_mul(t1, 3, 1, 0, 2) << 16) ^
- (inv_col_mul(t1, 2, 0, 3, 1) << 8) ^
- (inv_col_mul(t1, 1, 3, 2, 0) );
- s2 =
- (inv_col_mul(t2, 0, 2, 1, 3) << 24) ^
- (inv_col_mul(t2, 3, 1, 0, 2) << 16) ^
- (inv_col_mul(t2, 2, 0, 3, 1) << 8) ^
- (inv_col_mul(t2, 1, 3, 2, 0) );
- s3 =
- (inv_col_mul(t3, 0, 2, 1, 3) << 24) ^
- (inv_col_mul(t3, 3, 1, 0, 2) << 16) ^
- (inv_col_mul(t3, 2, 0, 3, 1) << 8) ^
- (inv_col_mul(t3, 1, 3, 2, 0) );
- }
-
- t0 =
- ((word32)Td4[GETBYTE(s0, 3)] << 24) ^
- ((word32)Td4[GETBYTE(s3, 2)] << 16) ^
- ((word32)Td4[GETBYTE(s2, 1)] << 8) ^
- ((word32)Td4[GETBYTE(s1, 0)]);
- t1 =
- ((word32)Td4[GETBYTE(s1, 3)] << 24) ^
- ((word32)Td4[GETBYTE(s0, 2)] << 16) ^
- ((word32)Td4[GETBYTE(s3, 1)] << 8) ^
- ((word32)Td4[GETBYTE(s2, 0)]);
- t2 =
- ((word32)Td4[GETBYTE(s2, 3)] << 24) ^
- ((word32)Td4[GETBYTE(s1, 2)] << 16) ^
- ((word32)Td4[GETBYTE(s0, 1)] << 8) ^
- ((word32)Td4[GETBYTE(s3, 0)]);
- t3 =
- ((word32)Td4[GETBYTE(s3, 3)] << 24) ^
- ((word32)Td4[GETBYTE(s2, 2)] << 16) ^
- ((word32)Td4[GETBYTE(s1, 1)] << 8) ^
- ((word32)Td4[GETBYTE(s0, 0)]);
- s0 = t0 ^ rk[0];
- s1 = t1 ^ rk[1];
- s2 = t2 ^ rk[2];
- s3 = t3 ^ rk[3];
-#endif
-
- /* write out */
-#ifdef LITTLE_ENDIAN_ORDER
- s0 = ByteReverseWord32(s0);
- s1 = ByteReverseWord32(s1);
- s2 = ByteReverseWord32(s2);
- s3 = ByteReverseWord32(s3);
-#endif
-
- XMEMCPY(outBlock, &s0, sizeof(s0));
- XMEMCPY(outBlock + sizeof(s0), &s1, sizeof(s1));
- XMEMCPY(outBlock + 2 * sizeof(s0), &s2, sizeof(s2));
- XMEMCPY(outBlock + 3 * sizeof(s0), &s3, sizeof(s3));
-}
-#endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
-#endif /* HAVE_AES_DECRYPT */
-
-#endif /* NEED_AES_TABLES */
-
-
-
-/* wc_AesSetKey */
-#if defined(STM32_CRYPTO)
-
- int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- word32 *rk;
-
- (void)dir;
-
- if (aes == NULL || (keylen != 16 &&
- #ifdef WOLFSSL_AES_192
- keylen != 24 &&
- #endif
- keylen != 32)) {
- return BAD_FUNC_ARG;
- }
-
- rk = aes->key;
- aes->keylen = keylen;
- aes->rounds = keylen/4 + 6;
- XMEMCPY(rk, userKey, keylen);
- #if !defined(WOLFSSL_STM32_CUBEMX) || defined(STM32_HAL_V2)
- ByteReverseWords(rk, rk, keylen);
- #endif
- #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER) || \
- defined(WOLFSSL_AES_OFB)
- aes->left = 0;
- #endif
-
- return wc_AesSetIV(aes, iv);
- }
- #if defined(WOLFSSL_AES_DIRECT)
- int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- return wc_AesSetKey(aes, userKey, keylen, iv, dir);
- }
- #endif
-
-#elif defined(HAVE_COLDFIRE_SEC)
- #if defined (HAVE_THREADX)
- #include "memory_pools.h"
- extern TX_BYTE_POOL mp_ncached; /* Non Cached memory pool */
- #endif
-
- #define AES_BUFFER_SIZE (AES_BLOCK_SIZE * 64)
- static unsigned char *AESBuffIn = NULL;
- static unsigned char *AESBuffOut = NULL;
- static byte *secReg;
- static byte *secKey;
- static volatile SECdescriptorType *secDesc;
-
- static wolfSSL_Mutex Mutex_AesSEC;
-
- #define SEC_DESC_AES_CBC_ENCRYPT 0x60300010
- #define SEC_DESC_AES_CBC_DECRYPT 0x60200010
-
- extern volatile unsigned char __MBAR[];
-
- int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- if (AESBuffIn == NULL) {
- #if defined (HAVE_THREADX)
- int s1, s2, s3, s4, s5;
- s5 = tx_byte_allocate(&mp_ncached,(void *)&secDesc,
- sizeof(SECdescriptorType), TX_NO_WAIT);
- s1 = tx_byte_allocate(&mp_ncached, (void *)&AESBuffIn,
- AES_BUFFER_SIZE, TX_NO_WAIT);
- s2 = tx_byte_allocate(&mp_ncached, (void *)&AESBuffOut,
- AES_BUFFER_SIZE, TX_NO_WAIT);
- s3 = tx_byte_allocate(&mp_ncached, (void *)&secKey,
- AES_BLOCK_SIZE*2, TX_NO_WAIT);
- s4 = tx_byte_allocate(&mp_ncached, (void *)&secReg,
- AES_BLOCK_SIZE, TX_NO_WAIT);
-
- if (s1 || s2 || s3 || s4 || s5)
- return BAD_FUNC_ARG;
- #else
- #warning "Allocate non-Cache buffers"
- #endif
-
- wc_InitMutex(&Mutex_AesSEC);
- }
-
- if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
- return BAD_FUNC_ARG;
-
- if (aes == NULL)
- return BAD_FUNC_ARG;
-
- aes->keylen = keylen;
- aes->rounds = keylen/4 + 6;
- XMEMCPY(aes->key, userKey, keylen);
-
- if (iv)
- XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
-
- #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER) || \
- defined(WOLFSSL_AES_OFB)
- aes->left = 0;
- #endif
-
- return 0;
- }
-#elif defined(FREESCALE_LTC)
- int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
- int dir)
- {
- if (aes == NULL || !((keylen == 16) || (keylen == 24) || (keylen == 32)))
- return BAD_FUNC_ARG;
-
- aes->rounds = keylen/4 + 6;
- XMEMCPY(aes->key, userKey, keylen);
-
- #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER) || \
- defined(WOLFSSL_AES_OFB)
- aes->left = 0;
- #endif
-
- return wc_AesSetIV(aes, iv);
- }
-
- int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- return wc_AesSetKey(aes, userKey, keylen, iv, dir);
- }
-#elif defined(FREESCALE_MMCAU)
- int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- int ret;
- byte* rk;
- byte* tmpKey = (byte*)userKey;
- int tmpKeyDynamic = 0;
- word32 alignOffset = 0;
-
- (void)dir;
-
- if (!((keylen == 16) || (keylen == 24) || (keylen == 32)))
- return BAD_FUNC_ARG;
- if (aes == NULL)
- return BAD_FUNC_ARG;
-
- rk = (byte*)aes->key;
- if (rk == NULL)
- return BAD_FUNC_ARG;
-
- #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER) || \
- defined(WOLFSSL_AES_OFB)
- aes->left = 0;
- #endif
-
- aes->rounds = keylen/4 + 6;
-
- #ifdef FREESCALE_MMCAU_CLASSIC
- if ((wolfssl_word)userKey % WOLFSSL_MMCAU_ALIGNMENT) {
- #ifndef NO_WOLFSSL_ALLOC_ALIGN
- byte* tmp = (byte*)XMALLOC(keylen + WOLFSSL_MMCAU_ALIGNMENT,
- aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmp == NULL) {
- return MEMORY_E;
- }
- alignOffset = WOLFSSL_MMCAU_ALIGNMENT -
- ((wolfssl_word)tmp % WOLFSSL_MMCAU_ALIGNMENT);
- tmpKey = tmp + alignOffset;
- XMEMCPY(tmpKey, userKey, keylen);
- tmpKeyDynamic = 1;
- #else
- WOLFSSL_MSG("Bad cau_aes_set_key alignment");
- return BAD_ALIGN_E;
- #endif
- }
- #endif
-
- ret = wolfSSL_CryptHwMutexLock();
- if(ret == 0) {
- #ifdef FREESCALE_MMCAU_CLASSIC
- cau_aes_set_key(tmpKey, keylen*8, rk);
- #else
- MMCAU_AES_SetKey(tmpKey, keylen, rk);
- #endif
- wolfSSL_CryptHwMutexUnLock();
-
- ret = wc_AesSetIV(aes, iv);
- }
-
- if (tmpKeyDynamic == 1) {
- XFREE(tmpKey - alignOffset, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- return ret;
- }
-
- int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- return wc_AesSetKey(aes, userKey, keylen, iv, dir);
- }
-
-#elif defined(WOLFSSL_NRF51_AES)
- int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- int ret;
-
- (void)dir;
- (void)iv;
-
- if (aes == NULL || keylen != 16)
- return BAD_FUNC_ARG;
-
- aes->keylen = keylen;
- aes->rounds = keylen/4 + 6;
- ret = nrf51_aes_set_key(userKey);
-
- #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER) || \
- defined(WOLFSSL_AES_OFB)
- aes->left = 0;
- #endif
-
- return ret;
- }
-
- int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- return wc_AesSetKey(aes, userKey, keylen, iv, dir);
- }
-#elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
- !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
-
- int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- (void)dir;
- (void)iv;
-
- if (aes == NULL || (keylen != 16 && keylen != 24 && keylen != 32)) {
- return BAD_FUNC_ARG;
- }
-
- aes->keylen = keylen;
- aes->rounds = keylen/4 + 6;
-
- XMEMCPY(aes->key, userKey, keylen);
- #if defined(WOLFSSL_AES_COUNTER)
- aes->left = 0;
- #endif
- return wc_AesSetIV(aes, iv);
- }
-
- int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- return wc_AesSetKey(aes, userKey, keylen, iv, dir);
- }
-#elif defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES)
-
- int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen, const byte* iv,
- int dir)
- {
- SaSiError_t ret = SASI_OK;
- SaSiAesIv_t iv_aes;
-
- if (aes == NULL ||
- (keylen != AES_128_KEY_SIZE &&
- keylen != AES_192_KEY_SIZE &&
- keylen != AES_256_KEY_SIZE)) {
- return BAD_FUNC_ARG;
- }
- #if defined(AES_MAX_KEY_SIZE)
- if (keylen > (AES_MAX_KEY_SIZE/8)) {
- return BAD_FUNC_ARG;
- }
- #endif
- if (dir != AES_ENCRYPTION &&
- dir != AES_DECRYPTION) {
- return BAD_FUNC_ARG;
- }
-
- if (dir == AES_ENCRYPTION) {
- aes->ctx.mode = SASI_AES_ENCRYPT;
- SaSi_AesInit(&aes->ctx.user_ctx,
- SASI_AES_ENCRYPT,
- SASI_AES_MODE_CBC,
- SASI_AES_PADDING_NONE);
- }
- else {
- aes->ctx.mode = SASI_AES_DECRYPT;
- SaSi_AesInit(&aes->ctx.user_ctx,
- SASI_AES_DECRYPT,
- SASI_AES_MODE_CBC,
- SASI_AES_PADDING_NONE);
- }
-
- aes->keylen = keylen;
- aes->rounds = keylen/4 + 6;
- XMEMCPY(aes->key, userKey, keylen);
-
- aes->ctx.key.pKey = (uint8_t*)aes->key;
- aes->ctx.key.keySize= keylen;
-
- ret = SaSi_AesSetKey(&aes->ctx.user_ctx,
- SASI_AES_USER_KEY,
- &aes->ctx.key,
- sizeof(aes->ctx.key));
- if (ret != SASI_OK) {
- return BAD_FUNC_ARG;
- }
-
- ret = wc_AesSetIV(aes, iv);
-
- if (iv)
- XMEMCPY(iv_aes, iv, AES_BLOCK_SIZE);
- else
- XMEMSET(iv_aes, 0, AES_BLOCK_SIZE);
-
-
- ret = SaSi_AesSetIv(&aes->ctx.user_ctx, iv_aes);
- if (ret != SASI_OK) {
- return ret;
- }
- return ret;
- }
- #if defined(WOLFSSL_AES_DIRECT)
- int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- return wc_AesSetKey(aes, userKey, keylen, iv, dir);
- }
- #endif
-
-#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
- /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
-
-#elif defined(WOLFSSL_AFALG)
- /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
-
-#elif defined(WOLFSSL_DEVCRYPTO_AES)
- /* implemented in wolfcrypt/src/port/devcrypto/devcrypto_aes.c */
-
-#else
-
- /* Software AES - SetKey */
- static int wc_AesSetKeyLocal(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- word32 *rk = aes->key;
- #ifdef NEED_AES_TABLES
- word32 temp;
- unsigned int i = 0;
- #endif
-
- #ifdef WOLFSSL_AESNI
- aes->use_aesni = 0;
- #endif /* WOLFSSL_AESNI */
- #if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_COUNTER) || \
- defined(WOLFSSL_AES_OFB)
- aes->left = 0;
- #endif
-
- aes->keylen = keylen;
- aes->rounds = (keylen/4) + 6;
-
- XMEMCPY(rk, userKey, keylen);
- #if defined(LITTLE_ENDIAN_ORDER) && !defined(WOLFSSL_PIC32MZ_CRYPT) && \
- (!defined(WOLFSSL_ESP32WROOM32_CRYPT) || \
- defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES))
- ByteReverseWords(rk, rk, keylen);
- #endif
-
-#ifdef NEED_AES_TABLES
- switch (keylen) {
- #if defined(AES_MAX_KEY_SIZE) && AES_MAX_KEY_SIZE >= 128 && \
- defined(WOLFSSL_AES_128)
- case 16:
- while (1)
- {
- temp = rk[3];
- rk[4] = rk[0] ^
- #ifndef WOLFSSL_AES_SMALL_TABLES
- (Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
- (Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
- (Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
- (Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
- #else
- ((word32)Tsbox[GETBYTE(temp, 2)] << 24) ^
- ((word32)Tsbox[GETBYTE(temp, 1)] << 16) ^
- ((word32)Tsbox[GETBYTE(temp, 0)] << 8) ^
- ((word32)Tsbox[GETBYTE(temp, 3)]) ^
- #endif
- rcon[i];
- rk[5] = rk[1] ^ rk[4];
- rk[6] = rk[2] ^ rk[5];
- rk[7] = rk[3] ^ rk[6];
- if (++i == 10)
- break;
- rk += 4;
- }
- break;
- #endif /* 128 */
-
- #if defined(AES_MAX_KEY_SIZE) && AES_MAX_KEY_SIZE >= 192 && \
- defined(WOLFSSL_AES_192)
- case 24:
- /* for (;;) here triggers a bug in VC60 SP4 w/ Pro Pack */
- while (1)
- {
- temp = rk[ 5];
- rk[ 6] = rk[ 0] ^
- #ifndef WOLFSSL_AES_SMALL_TABLES
- (Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
- (Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
- (Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
- (Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
- #else
- ((word32)Tsbox[GETBYTE(temp, 2)] << 24) ^
- ((word32)Tsbox[GETBYTE(temp, 1)] << 16) ^
- ((word32)Tsbox[GETBYTE(temp, 0)] << 8) ^
- ((word32)Tsbox[GETBYTE(temp, 3)]) ^
- #endif
- rcon[i];
- rk[ 7] = rk[ 1] ^ rk[ 6];
- rk[ 8] = rk[ 2] ^ rk[ 7];
- rk[ 9] = rk[ 3] ^ rk[ 8];
- if (++i == 8)
- break;
- rk[10] = rk[ 4] ^ rk[ 9];
- rk[11] = rk[ 5] ^ rk[10];
- rk += 6;
- }
- break;
- #endif /* 192 */
-
- #if defined(AES_MAX_KEY_SIZE) && AES_MAX_KEY_SIZE >= 256 && \
- defined(WOLFSSL_AES_256)
- case 32:
- while (1)
- {
- temp = rk[ 7];
- rk[ 8] = rk[ 0] ^
- #ifndef WOLFSSL_AES_SMALL_TABLES
- (Te[2][GETBYTE(temp, 2)] & 0xff000000) ^
- (Te[3][GETBYTE(temp, 1)] & 0x00ff0000) ^
- (Te[0][GETBYTE(temp, 0)] & 0x0000ff00) ^
- (Te[1][GETBYTE(temp, 3)] & 0x000000ff) ^
- #else
- ((word32)Tsbox[GETBYTE(temp, 2)] << 24) ^
- ((word32)Tsbox[GETBYTE(temp, 1)] << 16) ^
- ((word32)Tsbox[GETBYTE(temp, 0)] << 8) ^
- ((word32)Tsbox[GETBYTE(temp, 3)]) ^
- #endif
- rcon[i];
- rk[ 9] = rk[ 1] ^ rk[ 8];
- rk[10] = rk[ 2] ^ rk[ 9];
- rk[11] = rk[ 3] ^ rk[10];
- if (++i == 7)
- break;
- temp = rk[11];
- rk[12] = rk[ 4] ^
- #ifndef WOLFSSL_AES_SMALL_TABLES
- (Te[2][GETBYTE(temp, 3)] & 0xff000000) ^
- (Te[3][GETBYTE(temp, 2)] & 0x00ff0000) ^
- (Te[0][GETBYTE(temp, 1)] & 0x0000ff00) ^
- (Te[1][GETBYTE(temp, 0)] & 0x000000ff);
- #else
- ((word32)Tsbox[GETBYTE(temp, 3)] << 24) ^
- ((word32)Tsbox[GETBYTE(temp, 2)] << 16) ^
- ((word32)Tsbox[GETBYTE(temp, 1)] << 8) ^
- ((word32)Tsbox[GETBYTE(temp, 0)]);
- #endif
- rk[13] = rk[ 5] ^ rk[12];
- rk[14] = rk[ 6] ^ rk[13];
- rk[15] = rk[ 7] ^ rk[14];
-
- rk += 8;
- }
- break;
- #endif /* 256 */
-
- default:
- return BAD_FUNC_ARG;
- } /* switch */
-
- #if defined(HAVE_AES_DECRYPT)
- if (dir == AES_DECRYPTION) {
- unsigned int j;
- rk = aes->key;
-
- /* invert the order of the round keys: */
- for (i = 0, j = 4* aes->rounds; i < j; i += 4, j -= 4) {
- temp = rk[i ]; rk[i ] = rk[j ]; rk[j ] = temp;
- temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;
- temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;
- temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;
- }
- #if !defined(WOLFSSL_AES_SMALL_TABLES)
- /* apply the inverse MixColumn transform to all round keys but the
- first and the last: */
- for (i = 1; i < aes->rounds; i++) {
- rk += 4;
- rk[0] =
- Td[0][Te[1][GETBYTE(rk[0], 3)] & 0xff] ^
- Td[1][Te[1][GETBYTE(rk[0], 2)] & 0xff] ^
- Td[2][Te[1][GETBYTE(rk[0], 1)] & 0xff] ^
- Td[3][Te[1][GETBYTE(rk[0], 0)] & 0xff];
- rk[1] =
- Td[0][Te[1][GETBYTE(rk[1], 3)] & 0xff] ^
- Td[1][Te[1][GETBYTE(rk[1], 2)] & 0xff] ^
- Td[2][Te[1][GETBYTE(rk[1], 1)] & 0xff] ^
- Td[3][Te[1][GETBYTE(rk[1], 0)] & 0xff];
- rk[2] =
- Td[0][Te[1][GETBYTE(rk[2], 3)] & 0xff] ^
- Td[1][Te[1][GETBYTE(rk[2], 2)] & 0xff] ^
- Td[2][Te[1][GETBYTE(rk[2], 1)] & 0xff] ^
- Td[3][Te[1][GETBYTE(rk[2], 0)] & 0xff];
- rk[3] =
- Td[0][Te[1][GETBYTE(rk[3], 3)] & 0xff] ^
- Td[1][Te[1][GETBYTE(rk[3], 2)] & 0xff] ^
- Td[2][Te[1][GETBYTE(rk[3], 1)] & 0xff] ^
- Td[3][Te[1][GETBYTE(rk[3], 0)] & 0xff];
- }
- #endif
- }
- #else
- (void)dir;
- #endif /* HAVE_AES_DECRYPT */
- (void)temp;
-#endif /* NEED_AES_TABLES */
-
-#if defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES)
- XMEMCPY((byte*)aes->key, userKey, keylen);
- if (WOLFSSL_SCE_GSCE_HANDLE.p_cfg->endian_flag == CRYPTO_WORD_ENDIAN_BIG) {
- ByteReverseWords(aes->key, aes->key, 32);
- }
-#endif
-
- return wc_AesSetIV(aes, iv);
- }
-
- int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- int ret;
- #if defined(AES_MAX_KEY_SIZE)
- const word32 max_key_len = (AES_MAX_KEY_SIZE / 8);
- #endif
-
- #ifdef WOLFSSL_IMX6_CAAM_BLOB
- byte local[32];
- word32 localSz = 32;
-
- if (keylen == (16 + WC_CAAM_BLOB_SZ) ||
- keylen == (24 + WC_CAAM_BLOB_SZ) ||
- keylen == (32 + WC_CAAM_BLOB_SZ)) {
- if (wc_caamOpenBlob((byte*)userKey, keylen, local, &localSz) != 0) {
- return BAD_FUNC_ARG;
- }
-
- /* set local values */
- userKey = local;
- keylen = localSz;
- }
- #endif
- if (aes == NULL ||
- !((keylen == 16) || (keylen == 24) || (keylen == 32))) {
- return BAD_FUNC_ARG;
- }
-
- #if defined(AES_MAX_KEY_SIZE)
- /* Check key length */
- if (keylen > max_key_len) {
- return BAD_FUNC_ARG;
- }
- #endif
- aes->keylen = keylen;
- aes->rounds = keylen/4 + 6;
-
- #if defined(WOLF_CRYPTO_CB) || (defined(WOLFSSL_DEVCRYPTO) && \
- (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))) || \
- (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES))
- #ifdef WOLF_CRYPTO_CB
- if (aes->devId != INVALID_DEVID)
- #endif
- {
- XMEMCPY(aes->devKey, userKey, keylen);
- }
- #endif
-
- #ifdef WOLFSSL_AESNI
- if (checkAESNI == 0) {
- haveAESNI = Check_CPU_support_AES();
- checkAESNI = 1;
- }
- if (haveAESNI) {
- #if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB) || \
- defined(WOLFSSL_AES_OFB)
- aes->left = 0;
- #endif /* WOLFSSL_AES_COUNTER */
- aes->use_aesni = 1;
- if (iv)
- XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
- else
- XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
- if (dir == AES_ENCRYPTION)
- return AES_set_encrypt_key(userKey, keylen * 8, aes);
- #ifdef HAVE_AES_DECRYPT
- else
- return AES_set_decrypt_key(userKey, keylen * 8, aes);
- #endif
- }
- #endif /* WOLFSSL_AESNI */
-
- ret = wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir);
-
- #if defined(WOLFSSL_DEVCRYPTO) && \
- (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
- aes->ctx.cfd = -1;
- #endif
- #ifdef WOLFSSL_IMX6_CAAM_BLOB
- ForceZero(local, sizeof(local));
- #endif
- return ret;
- }
-
- #if defined(WOLFSSL_AES_DIRECT) || defined(WOLFSSL_AES_COUNTER)
- /* AES-CTR and AES-DIRECT need to use this for key setup, no aesni yet */
- int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
- const byte* iv, int dir)
- {
- int ret;
-
- #ifdef WOLFSSL_IMX6_CAAM_BLOB
- byte local[32];
- word32 localSz = 32;
-
- if (keylen == (16 + WC_CAAM_BLOB_SZ) ||
- keylen == (24 + WC_CAAM_BLOB_SZ) ||
- keylen == (32 + WC_CAAM_BLOB_SZ)) {
- if (wc_caamOpenBlob((byte*)userKey, keylen, local, &localSz)
- != 0) {
- return BAD_FUNC_ARG;
- }
-
- /* set local values */
- userKey = local;
- keylen = localSz;
- }
- #endif
- ret = wc_AesSetKeyLocal(aes, userKey, keylen, iv, dir);
-
- #ifdef WOLFSSL_IMX6_CAAM_BLOB
- ForceZero(local, sizeof(local));
- #endif
-
- return ret;
- }
- #endif /* WOLFSSL_AES_DIRECT || WOLFSSL_AES_COUNTER */
-#endif /* wc_AesSetKey block */
-
-
-/* wc_AesSetIV is shared between software and hardware */
-int wc_AesSetIV(Aes* aes, const byte* iv)
-{
- if (aes == NULL)
- return BAD_FUNC_ARG;
-
- if (iv)
- XMEMCPY(aes->reg, iv, AES_BLOCK_SIZE);
- else
- XMEMSET(aes->reg, 0, AES_BLOCK_SIZE);
-
- return 0;
-}
-
-/* AES-DIRECT */
-#if defined(WOLFSSL_AES_DIRECT)
- #if defined(HAVE_COLDFIRE_SEC)
- #error "Coldfire SEC doesn't yet support AES direct"
-
- #elif defined(FREESCALE_LTC)
- /* Allow direct access to one block encrypt */
- void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
- {
- byte *key;
- uint32_t keySize;
-
- key = (byte*)aes->key;
- wc_AesGetKeySize(aes, &keySize);
-
- LTC_AES_EncryptEcb(LTC_BASE, in, out, AES_BLOCK_SIZE,
- key, keySize);
- }
-
- /* Allow direct access to one block decrypt */
- void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
- {
- byte *key;
- uint32_t keySize;
-
- key = (byte*)aes->key;
- wc_AesGetKeySize(aes, &keySize);
-
- LTC_AES_DecryptEcb(LTC_BASE, in, out, AES_BLOCK_SIZE,
- key, keySize, kLTC_EncryptKey);
- }
-
- #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
- /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
-
- #elif defined(WOLFSSL_AFALG)
- /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
-
- #elif defined(WOLFSSL_DEVCRYPTO_AES)
- /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
-
- #elif defined(STM32_CRYPTO)
- /* Allow direct access to one block encrypt */
- void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
- {
- if (wolfSSL_CryptHwMutexLock() == 0) {
- wc_AesEncrypt(aes, in, out);
- wolfSSL_CryptHwMutexUnLock();
- }
- }
- #ifdef HAVE_AES_DECRYPT
- /* Allow direct access to one block decrypt */
- void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
- {
- if (wolfSSL_CryptHwMutexLock() == 0) {
- wc_AesDecrypt(aes, in, out);
- wolfSSL_CryptHwMutexUnLock();
- }
- }
- #endif /* HAVE_AES_DECRYPT */
-
- #elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
- !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
-
- /* Allow direct access to one block encrypt */
- void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
- {
- wc_AesEncrypt(aes, in, out);
- }
- #ifdef HAVE_AES_DECRYPT
- /* Allow direct access to one block decrypt */
- void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
- {
- wc_AesDecrypt(aes, in, out);
- }
- #endif /* HAVE_AES_DECRYPT */
- #else
- /* Allow direct access to one block encrypt */
- void wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in)
- {
- wc_AesEncrypt(aes, in, out);
- }
- #ifdef HAVE_AES_DECRYPT
- /* Allow direct access to one block decrypt */
- void wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in)
- {
- wc_AesDecrypt(aes, in, out);
- }
- #endif /* HAVE_AES_DECRYPT */
- #endif /* AES direct block */
-#endif /* WOLFSSL_AES_DIRECT */
-
-
-/* AES-CBC */
-#ifdef HAVE_AES_CBC
-#if defined(STM32_CRYPTO)
-
-#ifdef WOLFSSL_STM32_CUBEMX
- int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- int ret = 0;
- word32 blocks = (sz / AES_BLOCK_SIZE);
- CRYP_HandleTypeDef hcryp;
-
- ret = wc_Stm32_Aes_Init(aes, &hcryp);
- if (ret != 0)
- return ret;
-
- ret = wolfSSL_CryptHwMutexLock();
- if (ret != 0) {
- return ret;
- }
-
- #ifdef STM32_CRYPTO_AES_ONLY
- hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
- hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
- hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
- #elif defined(STM32_HAL_V2)
- hcryp.Init.Algorithm = CRYP_AES_CBC;
- ByteReverseWords(aes->reg, aes->reg, AES_BLOCK_SIZE);
- #endif
- hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
- HAL_CRYP_Init(&hcryp);
-
- while (blocks--) {
- #ifdef STM32_CRYPTO_AES_ONLY
- ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
- out, STM32_HAL_TIMEOUT);
- #elif defined(STM32_HAL_V2)
- ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in, AES_BLOCK_SIZE,
- (uint32_t*)out, STM32_HAL_TIMEOUT);
- #else
- ret = HAL_CRYP_AESCBC_Encrypt(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
- out, STM32_HAL_TIMEOUT);
- #endif
- if (ret != HAL_OK) {
- ret = WC_TIMEOUT_E;
- break;
- }
-
- /* store iv for next call */
- XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
-
- sz -= AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- out += AES_BLOCK_SIZE;
- }
-
- HAL_CRYP_DeInit(&hcryp);
-
- wolfSSL_CryptHwMutexUnLock();
-
- return ret;
- }
- #ifdef HAVE_AES_DECRYPT
- int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- int ret = 0;
- word32 blocks = (sz / AES_BLOCK_SIZE);
- CRYP_HandleTypeDef hcryp;
-
- ret = wc_Stm32_Aes_Init(aes, &hcryp);
- if (ret != 0)
- return ret;
-
- ret = wolfSSL_CryptHwMutexLock();
- if (ret != 0) {
- return ret;
- }
-
- /* if input and output same will overwrite input iv */
- XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
-
- #ifdef STM32_CRYPTO_AES_ONLY
- hcryp.Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
- hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
- hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
- #elif defined(STM32_HAL_V2)
- hcryp.Init.Algorithm = CRYP_AES_CBC;
- ByteReverseWords(aes->reg, aes->reg, AES_BLOCK_SIZE);
- #endif
-
- hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
- HAL_CRYP_Init(&hcryp);
-
- while (blocks--) {
- #ifdef STM32_CRYPTO_AES_ONLY
- ret = HAL_CRYPEx_AES(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
- out, STM32_HAL_TIMEOUT);
- #elif defined(STM32_HAL_V2)
- ret = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)in, AES_BLOCK_SIZE,
- (uint32_t*)out, STM32_HAL_TIMEOUT);
- #else
- ret = HAL_CRYP_AESCBC_Decrypt(&hcryp, (uint8_t*)in, AES_BLOCK_SIZE,
- out, STM32_HAL_TIMEOUT);
- #endif
- if (ret != HAL_OK) {
- ret = WC_TIMEOUT_E;
- break;
- }
-
- /* store iv for next call */
- XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
-
- in += AES_BLOCK_SIZE;
- out += AES_BLOCK_SIZE;
- }
-
- HAL_CRYP_DeInit(&hcryp);
- wolfSSL_CryptHwMutexUnLock();
-
- return ret;
- }
- #endif /* HAVE_AES_DECRYPT */
-
-#else /* STD_PERI_LIB */
- int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- int ret;
- word32 *iv;
- word32 blocks = (sz / AES_BLOCK_SIZE);
- CRYP_InitTypeDef cryptInit;
- CRYP_KeyInitTypeDef keyInit;
- CRYP_IVInitTypeDef ivInit;
-
- ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
- if (ret != 0)
- return ret;
-
- ret = wolfSSL_CryptHwMutexLock();
- if (ret != 0) {
- return ret;
- }
-
- /* reset registers to their default values */
- CRYP_DeInit();
-
- /* set key */
- CRYP_KeyInit(&keyInit);
-
- /* set iv */
- iv = aes->reg;
- CRYP_IVStructInit(&ivInit);
- ByteReverseWords(iv, iv, AES_BLOCK_SIZE);
- ivInit.CRYP_IV0Left = iv[0];
- ivInit.CRYP_IV0Right = iv[1];
- ivInit.CRYP_IV1Left = iv[2];
- ivInit.CRYP_IV1Right = iv[3];
- CRYP_IVInit(&ivInit);
-
- /* set direction and mode */
- cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
- cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;
- CRYP_Init(&cryptInit);
-
- /* enable crypto processor */
- CRYP_Cmd(ENABLE);
-
- while (blocks--) {
- /* flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
-
- CRYP_DataIn(*(uint32_t*)&in[0]);
- CRYP_DataIn(*(uint32_t*)&in[4]);
- CRYP_DataIn(*(uint32_t*)&in[8]);
- CRYP_DataIn(*(uint32_t*)&in[12]);
-
- /* wait until the complete message has been processed */
- while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
-
- *(uint32_t*)&out[0] = CRYP_DataOut();
- *(uint32_t*)&out[4] = CRYP_DataOut();
- *(uint32_t*)&out[8] = CRYP_DataOut();
- *(uint32_t*)&out[12] = CRYP_DataOut();
-
- /* store iv for next call */
- XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
-
- sz -= AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- out += AES_BLOCK_SIZE;
- }
-
- /* disable crypto processor */
- CRYP_Cmd(DISABLE);
- wolfSSL_CryptHwMutexUnLock();
-
- return ret;
- }
-
- #ifdef HAVE_AES_DECRYPT
- int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- int ret;
- word32 *iv;
- word32 blocks = (sz / AES_BLOCK_SIZE);
- CRYP_InitTypeDef cryptInit;
- CRYP_KeyInitTypeDef keyInit;
- CRYP_IVInitTypeDef ivInit;
-
- ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
- if (ret != 0)
- return ret;
-
- ret = wolfSSL_CryptHwMutexLock();
- if (ret != 0) {
- return ret;
- }
-
- /* if input and output same will overwrite input iv */
- XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
-
- /* reset registers to their default values */
- CRYP_DeInit();
-
- /* set direction and key */
- CRYP_KeyInit(&keyInit);
- cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_Key;
- CRYP_Init(&cryptInit);
-
- /* enable crypto processor */
- CRYP_Cmd(ENABLE);
-
- /* wait until key has been prepared */
- while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
-
- /* set direction and mode */
- cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Decrypt;
- cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_CBC;
- CRYP_Init(&cryptInit);
-
- /* set iv */
- iv = aes->reg;
- CRYP_IVStructInit(&ivInit);
- ByteReverseWords(iv, iv, AES_BLOCK_SIZE);
- ivInit.CRYP_IV0Left = iv[0];
- ivInit.CRYP_IV0Right = iv[1];
- ivInit.CRYP_IV1Left = iv[2];
- ivInit.CRYP_IV1Right = iv[3];
- CRYP_IVInit(&ivInit);
-
- /* enable crypto processor */
- CRYP_Cmd(ENABLE);
-
- while (blocks--) {
- /* flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
-
- CRYP_DataIn(*(uint32_t*)&in[0]);
- CRYP_DataIn(*(uint32_t*)&in[4]);
- CRYP_DataIn(*(uint32_t*)&in[8]);
- CRYP_DataIn(*(uint32_t*)&in[12]);
-
- /* wait until the complete message has been processed */
- while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
-
- *(uint32_t*)&out[0] = CRYP_DataOut();
- *(uint32_t*)&out[4] = CRYP_DataOut();
- *(uint32_t*)&out[8] = CRYP_DataOut();
- *(uint32_t*)&out[12] = CRYP_DataOut();
-
- /* store iv for next call */
- XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
-
- in += AES_BLOCK_SIZE;
- out += AES_BLOCK_SIZE;
- }
-
- /* disable crypto processor */
- CRYP_Cmd(DISABLE);
- wolfSSL_CryptHwMutexUnLock();
-
- return ret;
- }
- #endif /* HAVE_AES_DECRYPT */
-#endif /* WOLFSSL_STM32_CUBEMX */
-
-#elif defined(HAVE_COLDFIRE_SEC)
- static int wc_AesCbcCrypt(Aes* aes, byte* po, const byte* pi, word32 sz,
- word32 descHeader)
- {
- #ifdef DEBUG_WOLFSSL
- int i; int stat1, stat2; int ret;
- #endif
-
- int size;
- volatile int v;
-
- if ((pi == NULL) || (po == NULL))
- return BAD_FUNC_ARG; /*wrong pointer*/
-
- wc_LockMutex(&Mutex_AesSEC);
-
- /* Set descriptor for SEC */
- secDesc->length1 = 0x0;
- secDesc->pointer1 = NULL;
-
- secDesc->length2 = AES_BLOCK_SIZE;
- secDesc->pointer2 = (byte *)secReg; /* Initial Vector */
-
- switch(aes->rounds) {
- case 10: secDesc->length3 = 16; break;
- case 12: secDesc->length3 = 24; break;
- case 14: secDesc->length3 = 32; break;
- }
- XMEMCPY(secKey, aes->key, secDesc->length3);
-
- secDesc->pointer3 = (byte *)secKey;
- secDesc->pointer4 = AESBuffIn;
- secDesc->pointer5 = AESBuffOut;
- secDesc->length6 = 0x0;
- secDesc->pointer6 = NULL;
- secDesc->length7 = 0x0;
- secDesc->pointer7 = NULL;
- secDesc->nextDescriptorPtr = NULL;
-
- while (sz) {
- secDesc->header = descHeader;
- XMEMCPY(secReg, aes->reg, AES_BLOCK_SIZE);
- if ((sz % AES_BUFFER_SIZE) == sz) {
- size = sz;
- sz = 0;
- } else {
- size = AES_BUFFER_SIZE;
- sz -= AES_BUFFER_SIZE;
- }
- secDesc->length4 = size;
- secDesc->length5 = size;
-
- XMEMCPY(AESBuffIn, pi, size);
- if(descHeader == SEC_DESC_AES_CBC_DECRYPT) {
- XMEMCPY((void*)aes->tmp, (void*)&(pi[size-AES_BLOCK_SIZE]),
- AES_BLOCK_SIZE);
- }
-
- /* Point SEC to the location of the descriptor */
- MCF_SEC_FR0 = (uint32)secDesc;
- /* Initialize SEC and wait for encryption to complete */
- MCF_SEC_CCCR0 = 0x0000001a;
- /* poll SISR to determine when channel is complete */
- v=0;
-
- while ((secDesc->header>> 24) != 0xff) v++;
-
- #ifdef DEBUG_WOLFSSL
- ret = MCF_SEC_SISRH;
- stat1 = MCF_SEC_AESSR;
- stat2 = MCF_SEC_AESISR;
- if (ret & 0xe0000000) {
- db_printf("Aes_Cbc(i=%d):ISRH=%08x, AESSR=%08x, "
- "AESISR=%08x\n", i, ret, stat1, stat2);
- }
- #endif
-
- XMEMCPY(po, AESBuffOut, size);
-
- if (descHeader == SEC_DESC_AES_CBC_ENCRYPT) {
- XMEMCPY((void*)aes->reg, (void*)&(po[size-AES_BLOCK_SIZE]),
- AES_BLOCK_SIZE);
- } else {
- XMEMCPY((void*)aes->reg, (void*)aes->tmp, AES_BLOCK_SIZE);
- }
-
- pi += size;
- po += size;
- }
-
- wc_UnLockMutex(&Mutex_AesSEC);
- return 0;
- }
-
- int wc_AesCbcEncrypt(Aes* aes, byte* po, const byte* pi, word32 sz)
- {
- return (wc_AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_ENCRYPT));
- }
-
- #ifdef HAVE_AES_DECRYPT
- int wc_AesCbcDecrypt(Aes* aes, byte* po, const byte* pi, word32 sz)
- {
- return (wc_AesCbcCrypt(aes, po, pi, sz, SEC_DESC_AES_CBC_DECRYPT));
- }
- #endif /* HAVE_AES_DECRYPT */
-
-#elif defined(FREESCALE_LTC)
- int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- uint32_t keySize;
- status_t status;
- byte *iv, *enc_key;
- word32 blocks = (sz / AES_BLOCK_SIZE);
-
- iv = (byte*)aes->reg;
- enc_key = (byte*)aes->key;
-
- status = wc_AesGetKeySize(aes, &keySize);
- if (status != 0) {
- return status;
- }
-
- status = LTC_AES_EncryptCbc(LTC_BASE, in, out, blocks * AES_BLOCK_SIZE,
- iv, enc_key, keySize);
-
- /* store iv for next call */
- if (status == kStatus_Success) {
- XMEMCPY(iv, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
- }
-
- return (status == kStatus_Success) ? 0 : -1;
- }
-
- #ifdef HAVE_AES_DECRYPT
- int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- uint32_t keySize;
- status_t status;
- byte* iv, *dec_key;
- word32 blocks = (sz / AES_BLOCK_SIZE);
- byte temp_block[AES_BLOCK_SIZE];
-
- iv = (byte*)aes->reg;
- dec_key = (byte*)aes->key;
-
- status = wc_AesGetKeySize(aes, &keySize);
- if (status != 0) {
- return status;
- }
-
- /* get IV for next call */
- XMEMCPY(temp_block, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
-
- status = LTC_AES_DecryptCbc(LTC_BASE, in, out, blocks * AES_BLOCK_SIZE,
- iv, dec_key, keySize, kLTC_EncryptKey);
-
- /* store IV for next call */
- if (status == kStatus_Success) {
- XMEMCPY(iv, temp_block, AES_BLOCK_SIZE);
- }
-
- return (status == kStatus_Success) ? 0 : -1;
- }
- #endif /* HAVE_AES_DECRYPT */
-
-#elif defined(FREESCALE_MMCAU)
- int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- int i;
- int offset = 0;
- word32 blocks = (sz / AES_BLOCK_SIZE);
- byte *iv;
- byte temp_block[AES_BLOCK_SIZE];
-
- iv = (byte*)aes->reg;
-
- while (blocks--) {
- XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
-
- /* XOR block with IV for CBC */
- for (i = 0; i < AES_BLOCK_SIZE; i++)
- temp_block[i] ^= iv[i];
-
- wc_AesEncrypt(aes, temp_block, out + offset);
-
- offset += AES_BLOCK_SIZE;
-
- /* store IV for next block */
- XMEMCPY(iv, out + offset - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
- }
-
- return 0;
- }
- #ifdef HAVE_AES_DECRYPT
- int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- int i;
- int offset = 0;
- word32 blocks = (sz / AES_BLOCK_SIZE);
- byte* iv;
- byte temp_block[AES_BLOCK_SIZE];
-
- iv = (byte*)aes->reg;
-
- while (blocks--) {
- XMEMCPY(temp_block, in + offset, AES_BLOCK_SIZE);
-
- wc_AesDecrypt(aes, in + offset, out + offset);
-
- /* XOR block with IV for CBC */
- for (i = 0; i < AES_BLOCK_SIZE; i++)
- (out + offset)[i] ^= iv[i];
-
- /* store IV for next block */
- XMEMCPY(iv, temp_block, AES_BLOCK_SIZE);
-
- offset += AES_BLOCK_SIZE;
- }
-
- return 0;
- }
- #endif /* HAVE_AES_DECRYPT */
-
-#elif defined(WOLFSSL_PIC32MZ_CRYPT)
-
- int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- int ret;
-
- /* hardware fails on input that is not a multiple of AES block size */
- if (sz % AES_BLOCK_SIZE != 0) {
- return BAD_FUNC_ARG;
- }
-
- ret = wc_Pic32AesCrypt(
- aes->key, aes->keylen, aes->reg, AES_BLOCK_SIZE,
- out, in, sz, PIC32_ENCRYPTION,
- PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCBC);
-
- /* store iv for next call */
- if (ret == 0) {
- XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
- }
-
- return ret;
- }
- #ifdef HAVE_AES_DECRYPT
- int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- int ret;
- byte scratch[AES_BLOCK_SIZE];
-
- /* hardware fails on input that is not a multiple of AES block size */
- if (sz % AES_BLOCK_SIZE != 0) {
- return BAD_FUNC_ARG;
- }
- XMEMCPY(scratch, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
-
- ret = wc_Pic32AesCrypt(
- aes->key, aes->keylen, aes->reg, AES_BLOCK_SIZE,
- out, in, sz, PIC32_DECRYPTION,
- PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCBC);
-
- /* store iv for next call */
- if (ret == 0) {
- XMEMCPY((byte*)aes->reg, scratch, AES_BLOCK_SIZE);
- }
-
- return ret;
- }
- #endif /* HAVE_AES_DECRYPT */
-#elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
- !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
-
- int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- return wc_esp32AesCbcEncrypt(aes, out, in, sz);
- }
- int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- return wc_esp32AesCbcDecrypt(aes, out, in, sz);
- }
-#elif defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES)
- int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- return SaSi_AesBlock(&aes->ctx.user_ctx, (uint8_t* )in, sz, out);
- }
- int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- return SaSi_AesBlock(&aes->ctx.user_ctx, (uint8_t* )in, sz, out);
- }
-#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
- /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
-
-#elif defined(WOLFSSL_AFALG)
- /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
-
-#elif defined(WOLFSSL_DEVCRYPTO_CBC)
- /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
-
-#else
-
- /* Software AES - CBC Encrypt */
- int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- word32 blocks = (sz / AES_BLOCK_SIZE);
-
- if (aes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
- #ifdef WOLF_CRYPTO_CB
- if (aes->devId != INVALID_DEVID) {
- int ret = wc_CryptoCb_AesCbcEncrypt(aes, out, in, sz);
- if (ret != CRYPTOCB_UNAVAILABLE)
- return ret;
- /* fall-through when unavailable */
- }
- #endif
- #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
- /* if async and byte count above threshold */
- if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
- sz >= WC_ASYNC_THRESH_AES_CBC) {
- #if defined(HAVE_CAVIUM)
- return NitroxAesCbcEncrypt(aes, out, in, sz);
- #elif defined(HAVE_INTEL_QA)
- return IntelQaSymAesCbcEncrypt(&aes->asyncDev, out, in, sz,
- (const byte*)aes->devKey, aes->keylen,
- (byte*)aes->reg, AES_BLOCK_SIZE);
- #else /* WOLFSSL_ASYNC_CRYPT_TEST */
- if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_CBC_ENCRYPT)) {
- WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
- testDev->aes.aes = aes;
- testDev->aes.out = out;
- testDev->aes.in = in;
- testDev->aes.sz = sz;
- return WC_PENDING_E;
- }
- #endif
- }
- #endif /* WOLFSSL_ASYNC_CRYPT */
-
- #ifdef WOLFSSL_AESNI
- if (haveAESNI) {
- #ifdef DEBUG_AESNI
- printf("about to aes cbc encrypt\n");
- printf("in = %p\n", in);
- printf("out = %p\n", out);
- printf("aes->key = %p\n", aes->key);
- printf("aes->reg = %p\n", aes->reg);
- printf("aes->rounds = %d\n", aes->rounds);
- printf("sz = %d\n", sz);
- #endif
-
- /* check alignment, decrypt doesn't need alignment */
- if ((wolfssl_word)in % AESNI_ALIGN) {
- #ifndef NO_WOLFSSL_ALLOC_ALIGN
- byte* tmp = (byte*)XMALLOC(sz + AES_BLOCK_SIZE + AESNI_ALIGN,
- aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
- byte* tmp_align;
- if (tmp == NULL) return MEMORY_E;
-
- tmp_align = tmp + (AESNI_ALIGN - ((size_t)tmp % AESNI_ALIGN));
- XMEMCPY(tmp_align, in, sz);
- AES_CBC_encrypt(tmp_align, tmp_align, (byte*)aes->reg, sz,
- (byte*)aes->key, aes->rounds);
- /* store iv for next call */
- XMEMCPY(aes->reg, tmp_align + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
-
- XMEMCPY(out, tmp_align, sz);
- XFREE(tmp, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return 0;
- #else
- WOLFSSL_MSG("AES-CBC encrypt with bad alignment");
- return BAD_ALIGN_E;
- #endif
- }
-
- AES_CBC_encrypt(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
- aes->rounds);
- /* store iv for next call */
- XMEMCPY(aes->reg, out + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
-
- return 0;
- }
- #endif
-
- while (blocks--) {
- xorbuf((byte*)aes->reg, in, AES_BLOCK_SIZE);
- wc_AesEncrypt(aes, (byte*)aes->reg, (byte*)aes->reg);
- XMEMCPY(out, aes->reg, AES_BLOCK_SIZE);
-
- out += AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- }
-
- return 0;
- }
-
- #ifdef HAVE_AES_DECRYPT
- /* Software AES - CBC Decrypt */
- int wc_AesCbcDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- word32 blocks;
-
- if (aes == NULL || out == NULL || in == NULL
- || sz % AES_BLOCK_SIZE != 0) {
- return BAD_FUNC_ARG;
- }
-
- #ifdef WOLF_CRYPTO_CB
- if (aes->devId != INVALID_DEVID) {
- int ret = wc_CryptoCb_AesCbcDecrypt(aes, out, in, sz);
- if (ret != CRYPTOCB_UNAVAILABLE)
- return ret;
- /* fall-through when unavailable */
- }
- #endif
- #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
- /* if async and byte count above threshold */
- if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
- sz >= WC_ASYNC_THRESH_AES_CBC) {
- #if defined(HAVE_CAVIUM)
- return NitroxAesCbcDecrypt(aes, out, in, sz);
- #elif defined(HAVE_INTEL_QA)
- return IntelQaSymAesCbcDecrypt(&aes->asyncDev, out, in, sz,
- (const byte*)aes->devKey, aes->keylen,
- (byte*)aes->reg, AES_BLOCK_SIZE);
- #else /* WOLFSSL_ASYNC_CRYPT_TEST */
- if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_CBC_DECRYPT)) {
- WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
- testDev->aes.aes = aes;
- testDev->aes.out = out;
- testDev->aes.in = in;
- testDev->aes.sz = sz;
- return WC_PENDING_E;
- }
- #endif
- }
- #endif
-
- #ifdef WOLFSSL_AESNI
- if (haveAESNI) {
- #ifdef DEBUG_AESNI
- printf("about to aes cbc decrypt\n");
- printf("in = %p\n", in);
- printf("out = %p\n", out);
- printf("aes->key = %p\n", aes->key);
- printf("aes->reg = %p\n", aes->reg);
- printf("aes->rounds = %d\n", aes->rounds);
- printf("sz = %d\n", sz);
- #endif
-
- /* if input and output same will overwrite input iv */
- XMEMCPY(aes->tmp, in + sz - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
- #if defined(WOLFSSL_AESNI_BY4)
- AES_CBC_decrypt_by4(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
- aes->rounds);
- #elif defined(WOLFSSL_AESNI_BY6)
- AES_CBC_decrypt_by6(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
- aes->rounds);
- #else /* WOLFSSL_AESNI_BYx */
- AES_CBC_decrypt_by8(in, out, (byte*)aes->reg, sz, (byte*)aes->key,
- aes->rounds);
- #endif /* WOLFSSL_AESNI_BYx */
- /* store iv for next call */
- XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
- return 0;
- }
- #endif
-
- blocks = sz / AES_BLOCK_SIZE;
- while (blocks--) {
- XMEMCPY(aes->tmp, in, AES_BLOCK_SIZE);
- wc_AesDecrypt(aes, (byte*)aes->tmp, out);
- xorbuf(out, (byte*)aes->reg, AES_BLOCK_SIZE);
- /* store iv for next call */
- XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
-
- out += AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- }
-
- return 0;
- }
- #endif
-
-#endif /* AES-CBC block */
-#endif /* HAVE_AES_CBC */
-
-/* AES-CTR */
-#if defined(WOLFSSL_AES_COUNTER)
-
- #ifdef STM32_CRYPTO
- #define NEED_AES_CTR_SOFT
- #define XTRANSFORM_AESCTRBLOCK wc_AesCtrEncryptBlock
-
- int wc_AesCtrEncryptBlock(Aes* aes, byte* out, const byte* in)
- {
- int ret = 0;
- #ifdef WOLFSSL_STM32_CUBEMX
- CRYP_HandleTypeDef hcryp;
- #ifdef STM32_HAL_V2
- word32 iv[AES_BLOCK_SIZE/sizeof(word32)];
- #endif
- #else
- word32 *iv;
- CRYP_InitTypeDef cryptInit;
- CRYP_KeyInitTypeDef keyInit;
- CRYP_IVInitTypeDef ivInit;
- #endif
-
- ret = wolfSSL_CryptHwMutexLock();
- if (ret != 0) {
- return ret;
- }
-
- #ifdef WOLFSSL_STM32_CUBEMX
- ret = wc_Stm32_Aes_Init(aes, &hcryp);
- if (ret != 0) {
- wolfSSL_CryptHwMutexUnLock();
- return ret;
- }
-
- #ifdef STM32_CRYPTO_AES_ONLY
- hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
- hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
- hcryp.Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
- hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
- #elif defined(STM32_HAL_V2)
- hcryp.Init.Algorithm = CRYP_AES_CTR;
- ByteReverseWords(iv, aes->reg, AES_BLOCK_SIZE);
- hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)iv;
- #else
- hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)aes->reg;
- #endif
- HAL_CRYP_Init(&hcryp);
-
- #ifdef STM32_CRYPTO_AES_ONLY
- ret = HAL_CRYPEx_AES(&hcryp, (byte*)in, AES_BLOCK_SIZE,
- out, STM32_HAL_TIMEOUT);
- #elif defined(STM32_HAL_V2)
- ret = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in, AES_BLOCK_SIZE,
- (uint32_t*)out, STM32_HAL_TIMEOUT);
- #else
- ret = HAL_CRYP_AESCTR_Encrypt(&hcryp, (byte*)in, AES_BLOCK_SIZE,
- out, STM32_HAL_TIMEOUT);
- #endif
- if (ret != HAL_OK) {
- ret = WC_TIMEOUT_E;
- }
- HAL_CRYP_DeInit(&hcryp);
-
- #else /* STD_PERI_LIB */
- ret = wc_Stm32_Aes_Init(aes, &cryptInit, &keyInit);
- if (ret != 0) {
- wolfSSL_CryptHwMutexUnLock();
- return ret;
- }
-
- /* reset registers to their default values */
- CRYP_DeInit();
-
- /* set key */
- CRYP_KeyInit(&keyInit);
-
- /* set iv */
- iv = aes->reg;
- CRYP_IVStructInit(&ivInit);
- ivInit.CRYP_IV0Left = ByteReverseWord32(iv[0]);
- ivInit.CRYP_IV0Right = ByteReverseWord32(iv[1]);
- ivInit.CRYP_IV1Left = ByteReverseWord32(iv[2]);
- ivInit.CRYP_IV1Right = ByteReverseWord32(iv[3]);
- CRYP_IVInit(&ivInit);
-
- /* set direction and mode */
- cryptInit.CRYP_AlgoDir = CRYP_AlgoDir_Encrypt;
- cryptInit.CRYP_AlgoMode = CRYP_AlgoMode_AES_CTR;
- CRYP_Init(&cryptInit);
-
- /* enable crypto processor */
- CRYP_Cmd(ENABLE);
-
- /* flush IN/OUT FIFOs */
- CRYP_FIFOFlush();
-
- CRYP_DataIn(*(uint32_t*)&in[0]);
- CRYP_DataIn(*(uint32_t*)&in[4]);
- CRYP_DataIn(*(uint32_t*)&in[8]);
- CRYP_DataIn(*(uint32_t*)&in[12]);
-
- /* wait until the complete message has been processed */
- while (CRYP_GetFlagStatus(CRYP_FLAG_BUSY) != RESET) {}
-
- *(uint32_t*)&out[0] = CRYP_DataOut();
- *(uint32_t*)&out[4] = CRYP_DataOut();
- *(uint32_t*)&out[8] = CRYP_DataOut();
- *(uint32_t*)&out[12] = CRYP_DataOut();
-
- /* disable crypto processor */
- CRYP_Cmd(DISABLE);
-
- #endif /* WOLFSSL_STM32_CUBEMX */
-
- wolfSSL_CryptHwMutexUnLock();
- return ret;
- }
-
-
- #elif defined(WOLFSSL_PIC32MZ_CRYPT)
-
- #define NEED_AES_CTR_SOFT
- #define XTRANSFORM_AESCTRBLOCK wc_AesCtrEncryptBlock
-
- int wc_AesCtrEncryptBlock(Aes* aes, byte* out, const byte* in)
- {
- word32 tmpIv[AES_BLOCK_SIZE / sizeof(word32)];
- XMEMCPY(tmpIv, aes->reg, AES_BLOCK_SIZE);
- return wc_Pic32AesCrypt(
- aes->key, aes->keylen, tmpIv, AES_BLOCK_SIZE,
- out, in, AES_BLOCK_SIZE,
- PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_RCTR);
- }
-
- #elif defined(HAVE_COLDFIRE_SEC)
- #error "Coldfire SEC doesn't currently support AES-CTR mode"
-
- #elif defined(FREESCALE_LTC)
- int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- uint32_t keySize;
- byte *iv, *enc_key;
- byte* tmp;
-
- if (aes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* consume any unused bytes left in aes->tmp */
- tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
- while (aes->left && sz) {
- *(out++) = *(in++) ^ *(tmp++);
- aes->left--;
- sz--;
- }
-
- if (sz) {
- iv = (byte*)aes->reg;
- enc_key = (byte*)aes->key;
-
- wc_AesGetKeySize(aes, &keySize);
-
- LTC_AES_CryptCtr(LTC_BASE, in, out, sz,
- iv, enc_key, keySize, (byte*)aes->tmp,
- (uint32_t*)&aes->left);
- }
-
- return 0;
- }
-
- #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
- /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
-
- #elif defined(WOLFSSL_AFALG)
- /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
-
- #elif defined(WOLFSSL_DEVCRYPTO_AES)
- /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
-
- #elif defined(WOLFSSL_ESP32WROOM32_CRYPT) && \
- !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_AES)
- /* esp32 doesn't support CRT mode by hw. */
- /* use aes ecnryption plus sw implementation */
- #define NEED_AES_CTR_SOFT
-
- #else
-
- /* Use software based AES counter */
- #define NEED_AES_CTR_SOFT
- #endif
-
- #ifdef NEED_AES_CTR_SOFT
- /* Increment AES counter */
- static WC_INLINE void IncrementAesCounter(byte* inOutCtr)
- {
- /* in network byte order so start at end and work back */
- int i;
- for (i = AES_BLOCK_SIZE - 1; i >= 0; i--) {
- if (++inOutCtr[i]) /* we're done unless we overflow */
- return;
- }
- }
-
- /* Software AES - CTR Encrypt */
- int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
- {
- byte* tmp;
- byte scratch[AES_BLOCK_SIZE];
-
- if (aes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* consume any unused bytes left in aes->tmp */
- tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
- while (aes->left && sz) {
- *(out++) = *(in++) ^ *(tmp++);
- aes->left--;
- sz--;
- }
-
- /* do as many block size ops as possible */
- while (sz >= AES_BLOCK_SIZE) {
- #ifdef XTRANSFORM_AESCTRBLOCK
- XTRANSFORM_AESCTRBLOCK(aes, out, in);
- #else
- wc_AesEncrypt(aes, (byte*)aes->reg, scratch);
- xorbuf(scratch, in, AES_BLOCK_SIZE);
- XMEMCPY(out, scratch, AES_BLOCK_SIZE);
- #endif
- IncrementAesCounter((byte*)aes->reg);
-
- out += AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- sz -= AES_BLOCK_SIZE;
- aes->left = 0;
- }
- ForceZero(scratch, AES_BLOCK_SIZE);
-
- /* handle non block size remaining and store unused byte count in left */
- if (sz) {
- wc_AesEncrypt(aes, (byte*)aes->reg, (byte*)aes->tmp);
- IncrementAesCounter((byte*)aes->reg);
-
- aes->left = AES_BLOCK_SIZE;
- tmp = (byte*)aes->tmp;
-
- while (sz--) {
- *(out++) = *(in++) ^ *(tmp++);
- aes->left--;
- }
- }
-
- return 0;
- }
-
- #endif /* NEED_AES_CTR_SOFT */
-
-#endif /* WOLFSSL_AES_COUNTER */
-#endif /* !WOLFSSL_ARMASM */
-
-
-/*
- * The IV for AES GCM and CCM, stored in struct Aes's member reg, is comprised
- * of two parts in order:
- * 1. The fixed field which may be 0 or 4 bytes long. In TLS, this is set
- * to the implicit IV.
- * 2. The explicit IV is generated by wolfCrypt. It needs to be managed
- * by wolfCrypt to ensure the IV is unique for each call to encrypt.
- * The IV may be a 96-bit random value, or the 32-bit fixed value and a
- * 64-bit set of 0 or random data. The final 32-bits of reg is used as a
- * block counter during the encryption.
- */
-
-#if (defined(HAVE_AESGCM) && !defined(WC_NO_RNG)) || defined(HAVE_AESCCM)
-static WC_INLINE void IncCtr(byte* ctr, word32 ctrSz)
-{
- int i;
- for (i = ctrSz-1; i >= 0; i--) {
- if (++ctr[i])
- break;
- }
-}
-#endif /* HAVE_AESGCM || HAVE_AESCCM */
-
-
-#ifdef HAVE_AESGCM
-
-#if defined(HAVE_COLDFIRE_SEC)
- #error "Coldfire SEC doesn't currently support AES-GCM mode"
-
-#elif defined(WOLFSSL_NRF51_AES)
- #error "nRF51 doesn't currently support AES-GCM mode"
-
-#endif
-
-#ifdef WOLFSSL_ARMASM
- /* implementation is located in wolfcrypt/src/port/arm/armv8-aes.c */
-
-#elif defined(WOLFSSL_AFALG)
- /* implemented in wolfcrypt/src/port/afalg/afalg_aes.c */
-
-#elif defined(WOLFSSL_DEVCRYPTO_AES)
- /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
-
-#else /* software + AESNI implementation */
-
-#if !defined(FREESCALE_LTC_AES_GCM)
-static WC_INLINE void IncrementGcmCounter(byte* inOutCtr)
-{
- int i;
-
- /* in network byte order so start at end and work back */
- for (i = AES_BLOCK_SIZE - 1; i >= AES_BLOCK_SIZE - CTR_SZ; i--) {
- if (++inOutCtr[i]) /* we're done unless we overflow */
- return;
- }
-}
-#ifdef STM32_CRYPTO_AES_GCM
-static WC_INLINE void DecrementGcmCounter(byte* inOutCtr)
-{
- int i;
-
- /* in network byte order so start at end and work back */
- for (i = AES_BLOCK_SIZE - 1; i >= AES_BLOCK_SIZE - CTR_SZ; i--) {
- if (--inOutCtr[i] != 0xFF) /* we're done unless we underflow */
- return;
- }
-}
-#endif /* STM32_CRYPTO_AES_GCM */
-#endif /* !FREESCALE_LTC_AES_GCM */
-
-#if defined(GCM_SMALL) || defined(GCM_TABLE)
-
-static WC_INLINE void FlattenSzInBits(byte* buf, word32 sz)
-{
- /* Multiply the sz by 8 */
- word32 szHi = (sz >> (8*sizeof(sz) - 3));
- sz <<= 3;
-
- /* copy over the words of the sz into the destination buffer */
- buf[0] = (szHi >> 24) & 0xff;
- buf[1] = (szHi >> 16) & 0xff;
- buf[2] = (szHi >> 8) & 0xff;
- buf[3] = szHi & 0xff;
- buf[4] = (sz >> 24) & 0xff;
- buf[5] = (sz >> 16) & 0xff;
- buf[6] = (sz >> 8) & 0xff;
- buf[7] = sz & 0xff;
-}
-
-
-static WC_INLINE void RIGHTSHIFTX(byte* x)
-{
- int i;
- int carryOut = 0;
- int carryIn = 0;
- int borrow = x[15] & 0x01;
-
- for (i = 0; i < AES_BLOCK_SIZE; i++) {
- carryOut = x[i] & 0x01;
- x[i] = (x[i] >> 1) | (carryIn ? 0x80 : 0);
- carryIn = carryOut;
- }
- if (borrow) x[0] ^= 0xE1;
-}
-
-#endif /* defined(GCM_SMALL) || defined(GCM_TABLE) */
-
-
-#ifdef GCM_TABLE
-
-static void GenerateM0(Aes* aes)
-{
- int i, j;
- byte (*m)[AES_BLOCK_SIZE] = aes->M0;
-
- XMEMCPY(m[128], aes->H, AES_BLOCK_SIZE);
-
- for (i = 64; i > 0; i /= 2) {
- XMEMCPY(m[i], m[i*2], AES_BLOCK_SIZE);
- RIGHTSHIFTX(m[i]);
- }
-
- for (i = 2; i < 256; i *= 2) {
- for (j = 1; j < i; j++) {
- XMEMCPY(m[i+j], m[i], AES_BLOCK_SIZE);
- xorbuf(m[i+j], m[j], AES_BLOCK_SIZE);
- }
- }
-
- XMEMSET(m[0], 0, AES_BLOCK_SIZE);
-}
-
-#endif /* GCM_TABLE */
-
-/* Software AES - GCM SetKey */
-int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len)
-{
- int ret;
- byte iv[AES_BLOCK_SIZE];
-
- #ifdef WOLFSSL_IMX6_CAAM_BLOB
- byte local[32];
- word32 localSz = 32;
-
- if (len == (16 + WC_CAAM_BLOB_SZ) ||
- len == (24 + WC_CAAM_BLOB_SZ) ||
- len == (32 + WC_CAAM_BLOB_SZ)) {
- if (wc_caamOpenBlob((byte*)key, len, local, &localSz) != 0) {
- return BAD_FUNC_ARG;
- }
-
- /* set local values */
- key = local;
- len = localSz;
- }
- #endif
-
- if (!((len == 16) || (len == 24) || (len == 32)))
- return BAD_FUNC_ARG;
-
-#ifdef OPENSSL_EXTRA
- if (aes != NULL) {
- XMEMSET(aes->aadH, 0, sizeof(aes->aadH));
- aes->aadLen = 0;
- }
-#endif
- XMEMSET(iv, 0, AES_BLOCK_SIZE);
- ret = wc_AesSetKey(aes, key, len, iv, AES_ENCRYPTION);
-
- #ifdef WOLFSSL_AESNI
- /* AES-NI code generates its own H value. */
- if (haveAESNI)
- return ret;
- #endif /* WOLFSSL_AESNI */
-
-#if !defined(FREESCALE_LTC_AES_GCM)
- if (ret == 0) {
- wc_AesEncrypt(aes, iv, aes->H);
- #ifdef GCM_TABLE
- GenerateM0(aes);
- #endif /* GCM_TABLE */
- }
-#endif /* FREESCALE_LTC_AES_GCM */
-
-#if defined(WOLFSSL_XILINX_CRYPT)
- wc_AesGcmSetKey_ex(aes, key, len, XSECURE_CSU_AES_KEY_SRC_KUP);
-#elif defined(WOLFSSL_AFALG_XILINX_AES)
- wc_AesGcmSetKey_ex(aes, key, len, 0);
-#endif
-
-#ifdef WOLF_CRYPTO_CB
- if (aes->devId != INVALID_DEVID) {
- XMEMCPY(aes->devKey, key, len);
- }
-#endif
-
-#ifdef WOLFSSL_IMX6_CAAM_BLOB
- ForceZero(local, sizeof(local));
-#endif
-
- return ret;
-}
-
-
-#ifdef WOLFSSL_AESNI
-
-#if defined(USE_INTEL_SPEEDUP)
- #define HAVE_INTEL_AVX1
- #define HAVE_INTEL_AVX2
-#endif /* USE_INTEL_SPEEDUP */
-
-#ifndef _MSC_VER
-
-void AES_GCM_encrypt(const unsigned char *in, unsigned char *out,
- const unsigned char* addt, const unsigned char* ivec,
- unsigned char *tag, unsigned int nbytes,
- unsigned int abytes, unsigned int ibytes,
- unsigned int tbytes, const unsigned char* key, int nr)
- XASM_LINK("AES_GCM_encrypt");
-#ifdef HAVE_INTEL_AVX1
-void AES_GCM_encrypt_avx1(const unsigned char *in, unsigned char *out,
- const unsigned char* addt, const unsigned char* ivec,
- unsigned char *tag, unsigned int nbytes,
- unsigned int abytes, unsigned int ibytes,
- unsigned int tbytes, const unsigned char* key,
- int nr)
- XASM_LINK("AES_GCM_encrypt_avx1");
-#ifdef HAVE_INTEL_AVX2
-void AES_GCM_encrypt_avx2(const unsigned char *in, unsigned char *out,
- const unsigned char* addt, const unsigned char* ivec,
- unsigned char *tag, unsigned int nbytes,
- unsigned int abytes, unsigned int ibytes,
- unsigned int tbytes, const unsigned char* key,
- int nr)
- XASM_LINK("AES_GCM_encrypt_avx2");
-#endif /* HAVE_INTEL_AVX2 */
-#endif /* HAVE_INTEL_AVX1 */
-
-#ifdef HAVE_AES_DECRYPT
-void AES_GCM_decrypt(const unsigned char *in, unsigned char *out,
- const unsigned char* addt, const unsigned char* ivec,
- const unsigned char *tag, int nbytes, int abytes,
- int ibytes, int tbytes, const unsigned char* key, int nr,
- int* res)
- XASM_LINK("AES_GCM_decrypt");
-#ifdef HAVE_INTEL_AVX1
-void AES_GCM_decrypt_avx1(const unsigned char *in, unsigned char *out,
- const unsigned char* addt, const unsigned char* ivec,
- const unsigned char *tag, int nbytes, int abytes,
- int ibytes, int tbytes, const unsigned char* key,
- int nr, int* res)
- XASM_LINK("AES_GCM_decrypt_avx1");
-#ifdef HAVE_INTEL_AVX2
-void AES_GCM_decrypt_avx2(const unsigned char *in, unsigned char *out,
- const unsigned char* addt, const unsigned char* ivec,
- const unsigned char *tag, int nbytes, int abytes,
- int ibytes, int tbytes, const unsigned char* key,
- int nr, int* res)
- XASM_LINK("AES_GCM_decrypt_avx2");
-#endif /* HAVE_INTEL_AVX2 */
-#endif /* HAVE_INTEL_AVX1 */
-#endif /* HAVE_AES_DECRYPT */
-
-#else /* _MSC_VER */
-
-#define S(w,z) ((char)((unsigned long long)(w) >> (8*(7-(z))) & 0xFF))
-#define M128_INIT(x,y) { S((x),7), S((x),6), S((x),5), S((x),4), \
- S((x),3), S((x),2), S((x),1), S((x),0), \
- S((y),7), S((y),6), S((y),5), S((y),4), \
- S((y),3), S((y),2), S((y),1), S((y),0) }
-
-static const __m128i MOD2_128 =
- M128_INIT(0x1, (long long int)0xc200000000000000UL);
-
-
-/* See Intel® Carry-Less Multiplication Instruction
- * and its Usage for Computing the GCM Mode White Paper
- * by Shay Gueron, Intel Mobility Group, Israel Development Center;
- * and Michael E. Kounavis, Intel Labs, Circuits and Systems Research */
-
-
-/* Figure 9. AES-GCM – Encrypt With Single Block Ghash at a Time */
-
-static const __m128i ONE = M128_INIT(0x0, 0x1);
-#ifndef AES_GCM_AESNI_NO_UNROLL
-static const __m128i TWO = M128_INIT(0x0, 0x2);
-static const __m128i THREE = M128_INIT(0x0, 0x3);
-static const __m128i FOUR = M128_INIT(0x0, 0x4);
-static const __m128i FIVE = M128_INIT(0x0, 0x5);
-static const __m128i SIX = M128_INIT(0x0, 0x6);
-static const __m128i SEVEN = M128_INIT(0x0, 0x7);
-static const __m128i EIGHT = M128_INIT(0x0, 0x8);
-#endif
-static const __m128i BSWAP_EPI64 =
- M128_INIT(0x0001020304050607, 0x08090a0b0c0d0e0f);
-static const __m128i BSWAP_MASK =
- M128_INIT(0x08090a0b0c0d0e0f, 0x0001020304050607);
-
-
-/* The following are for MSC based builds which do not allow
- * inline assembly. Intrinsic functions are used instead. */
-
-#define aes_gcm_calc_iv_12(KEY, ivec, nr, H, Y, T) \
-do \
-{ \
- word32 iv12[4]; \
- iv12[0] = *(word32*)&ivec[0]; \
- iv12[1] = *(word32*)&ivec[4]; \
- iv12[2] = *(word32*)&ivec[8]; \
- iv12[3] = 0x01000000; \
- Y = _mm_loadu_si128((__m128i*)iv12); \
- \
- /* (Compute E[ZERO, KS] and E[Y0, KS] together */ \
- tmp1 = _mm_load_si128(&KEY[0]); \
- tmp2 = _mm_xor_si128(Y, KEY[0]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[1]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[1]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[2]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[2]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[3]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[3]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[4]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[4]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[5]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[5]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[6]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[6]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[7]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[7]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[8]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[8]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[9]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[9]); \
- lastKey = KEY[10]; \
- if (nr > 10) { \
- tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
- tmp2 = _mm_aesenc_si128(tmp2, lastKey); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[11]); \
- lastKey = KEY[12]; \
- if (nr > 12) { \
- tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
- tmp2 = _mm_aesenc_si128(tmp2, lastKey); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[13]); \
- lastKey = KEY[14]; \
- } \
- } \
- H = _mm_aesenclast_si128(tmp1, lastKey); \
- T = _mm_aesenclast_si128(tmp2, lastKey); \
- H = _mm_shuffle_epi8(H, BSWAP_MASK); \
-} \
-while (0)
-
-#define aes_gcm_calc_iv(KEY, ivec, ibytes, nr, H, Y, T) \
-do \
-{ \
- if (ibytes % 16) { \
- i = ibytes / 16; \
- for (j=0; j < (int)(ibytes%16); j++) \
- ((unsigned char*)&last_block)[j] = ivec[i*16+j]; \
- } \
- tmp1 = _mm_load_si128(&KEY[0]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[1]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[2]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[3]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[4]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[5]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[6]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[7]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[8]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[9]); \
- lastKey = KEY[10]; \
- if (nr > 10) { \
- tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]); \
- lastKey = KEY[12]; \
- if (nr > 12) { \
- tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]); \
- lastKey = KEY[14]; \
- } \
- } \
- H = _mm_aesenclast_si128(tmp1, lastKey); \
- H = _mm_shuffle_epi8(H, BSWAP_MASK); \
- Y = _mm_setzero_si128(); \
- for (i=0; i < (int)(ibytes/16); i++) { \
- tmp1 = _mm_loadu_si128(&((__m128i*)ivec)[i]); \
- tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK); \
- Y = _mm_xor_si128(Y, tmp1); \
- Y = gfmul_sw(Y, H); \
- } \
- if (ibytes % 16) { \
- tmp1 = last_block; \
- tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK); \
- Y = _mm_xor_si128(Y, tmp1); \
- Y = gfmul_sw(Y, H); \
- } \
- tmp1 = _mm_insert_epi64(tmp1, ibytes*8, 0); \
- tmp1 = _mm_insert_epi64(tmp1, 0, 1); \
- Y = _mm_xor_si128(Y, tmp1); \
- Y = gfmul_sw(Y, H); \
- Y = _mm_shuffle_epi8(Y, BSWAP_MASK); /* Compute E(K, Y0) */ \
- tmp1 = _mm_xor_si128(Y, KEY[0]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[1]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[2]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[3]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[4]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[5]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[6]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[7]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[8]); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[9]); \
- lastKey = KEY[10]; \
- if (nr > 10) { \
- tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]); \
- lastKey = KEY[12]; \
- if (nr > 12) { \
- tmp1 = _mm_aesenc_si128(tmp1, lastKey); \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]); \
- lastKey = KEY[14]; \
- } \
- } \
- T = _mm_aesenclast_si128(tmp1, lastKey); \
-} \
-while (0)
-
-#define AES_ENC_8(j) \
- tmp1 = _mm_aesenc_si128(tmp1, KEY[j]); \
- tmp2 = _mm_aesenc_si128(tmp2, KEY[j]); \
- tmp3 = _mm_aesenc_si128(tmp3, KEY[j]); \
- tmp4 = _mm_aesenc_si128(tmp4, KEY[j]); \
- tmp5 = _mm_aesenc_si128(tmp5, KEY[j]); \
- tmp6 = _mm_aesenc_si128(tmp6, KEY[j]); \
- tmp7 = _mm_aesenc_si128(tmp7, KEY[j]); \
- tmp8 = _mm_aesenc_si128(tmp8, KEY[j]);
-
-#define AES_ENC_LAST_8() \
- tmp1 =_mm_aesenclast_si128(tmp1, lastKey); \
- tmp2 =_mm_aesenclast_si128(tmp2, lastKey); \
- tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[i*8+0])); \
- tmp2 = _mm_xor_si128(tmp2, _mm_loadu_si128(&((__m128i*)in)[i*8+1])); \
- _mm_storeu_si128(&((__m128i*)out)[i*8+0], tmp1); \
- _mm_storeu_si128(&((__m128i*)out)[i*8+1], tmp2); \
- tmp3 =_mm_aesenclast_si128(tmp3, lastKey); \
- tmp4 =_mm_aesenclast_si128(tmp4, lastKey); \
- tmp3 = _mm_xor_si128(tmp3, _mm_loadu_si128(&((__m128i*)in)[i*8+2])); \
- tmp4 = _mm_xor_si128(tmp4, _mm_loadu_si128(&((__m128i*)in)[i*8+3])); \
- _mm_storeu_si128(&((__m128i*)out)[i*8+2], tmp3); \
- _mm_storeu_si128(&((__m128i*)out)[i*8+3], tmp4); \
- tmp5 =_mm_aesenclast_si128(tmp5, lastKey); \
- tmp6 =_mm_aesenclast_si128(tmp6, lastKey); \
- tmp5 = _mm_xor_si128(tmp5, _mm_loadu_si128(&((__m128i*)in)[i*8+4])); \
- tmp6 = _mm_xor_si128(tmp6, _mm_loadu_si128(&((__m128i*)in)[i*8+5])); \
- _mm_storeu_si128(&((__m128i*)out)[i*8+4], tmp5); \
- _mm_storeu_si128(&((__m128i*)out)[i*8+5], tmp6); \
- tmp7 =_mm_aesenclast_si128(tmp7, lastKey); \
- tmp8 =_mm_aesenclast_si128(tmp8, lastKey); \
- tmp7 = _mm_xor_si128(tmp7, _mm_loadu_si128(&((__m128i*)in)[i*8+6])); \
- tmp8 = _mm_xor_si128(tmp8, _mm_loadu_si128(&((__m128i*)in)[i*8+7])); \
- _mm_storeu_si128(&((__m128i*)out)[i*8+6], tmp7); \
- _mm_storeu_si128(&((__m128i*)out)[i*8+7], tmp8);
-
-
-static __m128i gfmul_sw(__m128i a, __m128i b)
-{
- __m128i r, t1, t2, t3, t4, t5, t6, t7;
- t2 = _mm_shuffle_epi32(b, 78);
- t3 = _mm_shuffle_epi32(a, 78);
- t2 = _mm_xor_si128(t2, b);
- t3 = _mm_xor_si128(t3, a);
- t4 = _mm_clmulepi64_si128(b, a, 0x11);
- t1 = _mm_clmulepi64_si128(b, a, 0x00);
- t2 = _mm_clmulepi64_si128(t2, t3, 0x00);
- t2 = _mm_xor_si128(t2, t1);
- t2 = _mm_xor_si128(t2, t4);
- t3 = _mm_slli_si128(t2, 8);
- t2 = _mm_srli_si128(t2, 8);
- t1 = _mm_xor_si128(t1, t3);
- t4 = _mm_xor_si128(t4, t2);
-
- t5 = _mm_srli_epi32(t1, 31);
- t6 = _mm_srli_epi32(t4, 31);
- t1 = _mm_slli_epi32(t1, 1);
- t4 = _mm_slli_epi32(t4, 1);
- t7 = _mm_srli_si128(t5, 12);
- t5 = _mm_slli_si128(t5, 4);
- t6 = _mm_slli_si128(t6, 4);
- t4 = _mm_or_si128(t4, t7);
- t1 = _mm_or_si128(t1, t5);
- t4 = _mm_or_si128(t4, t6);
-
- t5 = _mm_slli_epi32(t1, 31);
- t6 = _mm_slli_epi32(t1, 30);
- t7 = _mm_slli_epi32(t1, 25);
- t5 = _mm_xor_si128(t5, t6);
- t5 = _mm_xor_si128(t5, t7);
-
- t6 = _mm_srli_si128(t5, 4);
- t5 = _mm_slli_si128(t5, 12);
- t1 = _mm_xor_si128(t1, t5);
- t7 = _mm_srli_epi32(t1, 1);
- t3 = _mm_srli_epi32(t1, 2);
- t2 = _mm_srli_epi32(t1, 7);
-
- t7 = _mm_xor_si128(t7, t3);
- t7 = _mm_xor_si128(t7, t2);
- t7 = _mm_xor_si128(t7, t6);
- t7 = _mm_xor_si128(t7, t1);
- r = _mm_xor_si128(t4, t7);
-
- return r;
-}
-
-static void gfmul_only(__m128i a, __m128i b, __m128i* r0, __m128i* r1)
-{
- __m128i t1, t2, t3, t4;
-
- /* 128 x 128 Carryless Multiply */
- t2 = _mm_shuffle_epi32(b, 78);
- t3 = _mm_shuffle_epi32(a, 78);
- t2 = _mm_xor_si128(t2, b);
- t3 = _mm_xor_si128(t3, a);
- t4 = _mm_clmulepi64_si128(b, a, 0x11);
- t1 = _mm_clmulepi64_si128(b, a, 0x00);
- t2 = _mm_clmulepi64_si128(t2, t3, 0x00);
- t2 = _mm_xor_si128(t2, t1);
- t2 = _mm_xor_si128(t2, t4);
- t3 = _mm_slli_si128(t2, 8);
- t2 = _mm_srli_si128(t2, 8);
- t1 = _mm_xor_si128(t1, t3);
- t4 = _mm_xor_si128(t4, t2);
- *r0 = _mm_xor_si128(t1, *r0);
- *r1 = _mm_xor_si128(t4, *r1);
-}
-
-static __m128i gfmul_shl1(__m128i a)
-{
- __m128i t1 = a, t2;
- t2 = _mm_srli_epi64(t1, 63);
- t1 = _mm_slli_epi64(t1, 1);
- t2 = _mm_slli_si128(t2, 8);
- t1 = _mm_or_si128(t1, t2);
- /* if (a[1] >> 63) t1 = _mm_xor_si128(t1, MOD2_128); */
- a = _mm_shuffle_epi32(a, 0xff);
- a = _mm_srai_epi32(a, 31);
- a = _mm_and_si128(a, MOD2_128);
- t1 = _mm_xor_si128(t1, a);
- return t1;
-}
-
-static __m128i ghash_red(__m128i r0, __m128i r1)
-{
- __m128i t2, t3;
- __m128i t5, t6, t7;
-
- t5 = _mm_slli_epi32(r0, 31);
- t6 = _mm_slli_epi32(r0, 30);
- t7 = _mm_slli_epi32(r0, 25);
- t5 = _mm_xor_si128(t5, t6);
- t5 = _mm_xor_si128(t5, t7);
-
- t6 = _mm_srli_si128(t5, 4);
- t5 = _mm_slli_si128(t5, 12);
- r0 = _mm_xor_si128(r0, t5);
- t7 = _mm_srli_epi32(r0, 1);
- t3 = _mm_srli_epi32(r0, 2);
- t2 = _mm_srli_epi32(r0, 7);
-
- t7 = _mm_xor_si128(t7, t3);
- t7 = _mm_xor_si128(t7, t2);
- t7 = _mm_xor_si128(t7, t6);
- t7 = _mm_xor_si128(t7, r0);
- return _mm_xor_si128(r1, t7);
-}
-
-static __m128i gfmul_shifted(__m128i a, __m128i b)
-{
- __m128i t0 = _mm_setzero_si128(), t1 = _mm_setzero_si128();
- gfmul_only(a, b, &t0, &t1);
- return ghash_red(t0, t1);
-}
-
-#ifndef AES_GCM_AESNI_NO_UNROLL
-static __m128i gfmul8(__m128i a1, __m128i a2, __m128i a3, __m128i a4,
- __m128i a5, __m128i a6, __m128i a7, __m128i a8,
- __m128i b1, __m128i b2, __m128i b3, __m128i b4,
- __m128i b5, __m128i b6, __m128i b7, __m128i b8)
-{
- __m128i t0 = _mm_setzero_si128(), t1 = _mm_setzero_si128();
- gfmul_only(a1, b8, &t0, &t1);
- gfmul_only(a2, b7, &t0, &t1);
- gfmul_only(a3, b6, &t0, &t1);
- gfmul_only(a4, b5, &t0, &t1);
- gfmul_only(a5, b4, &t0, &t1);
- gfmul_only(a6, b3, &t0, &t1);
- gfmul_only(a7, b2, &t0, &t1);
- gfmul_only(a8, b1, &t0, &t1);
- return ghash_red(t0, t1);
-}
-#endif
-
-
-static void AES_GCM_encrypt(const unsigned char *in,
- unsigned char *out,
- const unsigned char* addt,
- const unsigned char* ivec,
- unsigned char *tag, unsigned int nbytes,
- unsigned int abytes, unsigned int ibytes,
- unsigned int tbytes,
- const unsigned char* key, int nr)
-{
- int i, j ,k;
- __m128i ctr1;
- __m128i H, Y, T;
- __m128i X = _mm_setzero_si128();
- __m128i *KEY = (__m128i*)key, lastKey;
- __m128i last_block = _mm_setzero_si128();
- __m128i tmp1, tmp2;
-#ifndef AES_GCM_AESNI_NO_UNROLL
- __m128i HT[8];
- __m128i r0, r1;
- __m128i XV;
- __m128i tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
-#endif
-
- if (ibytes == GCM_NONCE_MID_SZ)
- aes_gcm_calc_iv_12(KEY, ivec, nr, H, Y, T);
- else
- aes_gcm_calc_iv(KEY, ivec, ibytes, nr, H, Y, T);
-
- for (i=0; i < (int)(abytes/16); i++) {
- tmp1 = _mm_loadu_si128(&((__m128i*)addt)[i]);
- tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
- X = _mm_xor_si128(X, tmp1);
- X = gfmul_sw(X, H);
- }
- if (abytes%16) {
- last_block = _mm_setzero_si128();
- for (j=0; j < (int)(abytes%16); j++)
- ((unsigned char*)&last_block)[j] = addt[i*16+j];
- tmp1 = last_block;
- tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
- X = _mm_xor_si128(X, tmp1);
- X = gfmul_sw(X, H);
- }
- tmp1 = _mm_shuffle_epi8(Y, BSWAP_EPI64);
- ctr1 = _mm_add_epi32(tmp1, ONE);
- H = gfmul_shl1(H);
-
-#ifndef AES_GCM_AESNI_NO_UNROLL
- i = 0;
- if (nbytes >= 16*8) {
- HT[0] = H;
- HT[1] = gfmul_shifted(H, H);
- HT[2] = gfmul_shifted(H, HT[1]);
- HT[3] = gfmul_shifted(HT[1], HT[1]);
- HT[4] = gfmul_shifted(HT[1], HT[2]);
- HT[5] = gfmul_shifted(HT[2], HT[2]);
- HT[6] = gfmul_shifted(HT[2], HT[3]);
- HT[7] = gfmul_shifted(HT[3], HT[3]);
-
- tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
- tmp2 = _mm_add_epi32(ctr1, ONE);
- tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_EPI64);
- tmp3 = _mm_add_epi32(ctr1, TWO);
- tmp3 = _mm_shuffle_epi8(tmp3, BSWAP_EPI64);
- tmp4 = _mm_add_epi32(ctr1, THREE);
- tmp4 = _mm_shuffle_epi8(tmp4, BSWAP_EPI64);
- tmp5 = _mm_add_epi32(ctr1, FOUR);
- tmp5 = _mm_shuffle_epi8(tmp5, BSWAP_EPI64);
- tmp6 = _mm_add_epi32(ctr1, FIVE);
- tmp6 = _mm_shuffle_epi8(tmp6, BSWAP_EPI64);
- tmp7 = _mm_add_epi32(ctr1, SIX);
- tmp7 = _mm_shuffle_epi8(tmp7, BSWAP_EPI64);
- tmp8 = _mm_add_epi32(ctr1, SEVEN);
- tmp8 = _mm_shuffle_epi8(tmp8, BSWAP_EPI64);
- ctr1 = _mm_add_epi32(ctr1, EIGHT);
- tmp1 =_mm_xor_si128(tmp1, KEY[0]);
- tmp2 =_mm_xor_si128(tmp2, KEY[0]);
- tmp3 =_mm_xor_si128(tmp3, KEY[0]);
- tmp4 =_mm_xor_si128(tmp4, KEY[0]);
- tmp5 =_mm_xor_si128(tmp5, KEY[0]);
- tmp6 =_mm_xor_si128(tmp6, KEY[0]);
- tmp7 =_mm_xor_si128(tmp7, KEY[0]);
- tmp8 =_mm_xor_si128(tmp8, KEY[0]);
- AES_ENC_8(1);
- AES_ENC_8(2);
- AES_ENC_8(3);
- AES_ENC_8(4);
- AES_ENC_8(5);
- AES_ENC_8(6);
- AES_ENC_8(7);
- AES_ENC_8(8);
- AES_ENC_8(9);
- lastKey = KEY[10];
- if (nr > 10) {
- AES_ENC_8(10);
- AES_ENC_8(11);
- lastKey = KEY[12];
- if (nr > 12) {
- AES_ENC_8(12);
- AES_ENC_8(13);
- lastKey = KEY[14];
- }
- }
- AES_ENC_LAST_8();
-
- for (i=1; i < (int)(nbytes/16/8); i++) {
- r0 = _mm_setzero_si128();
- r1 = _mm_setzero_si128();
- tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
- tmp2 = _mm_add_epi32(ctr1, ONE);
- tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_EPI64);
- tmp3 = _mm_add_epi32(ctr1, TWO);
- tmp3 = _mm_shuffle_epi8(tmp3, BSWAP_EPI64);
- tmp4 = _mm_add_epi32(ctr1, THREE);
- tmp4 = _mm_shuffle_epi8(tmp4, BSWAP_EPI64);
- tmp5 = _mm_add_epi32(ctr1, FOUR);
- tmp5 = _mm_shuffle_epi8(tmp5, BSWAP_EPI64);
- tmp6 = _mm_add_epi32(ctr1, FIVE);
- tmp6 = _mm_shuffle_epi8(tmp6, BSWAP_EPI64);
- tmp7 = _mm_add_epi32(ctr1, SIX);
- tmp7 = _mm_shuffle_epi8(tmp7, BSWAP_EPI64);
- tmp8 = _mm_add_epi32(ctr1, SEVEN);
- tmp8 = _mm_shuffle_epi8(tmp8, BSWAP_EPI64);
- ctr1 = _mm_add_epi32(ctr1, EIGHT);
- tmp1 =_mm_xor_si128(tmp1, KEY[0]);
- tmp2 =_mm_xor_si128(tmp2, KEY[0]);
- tmp3 =_mm_xor_si128(tmp3, KEY[0]);
- tmp4 =_mm_xor_si128(tmp4, KEY[0]);
- tmp5 =_mm_xor_si128(tmp5, KEY[0]);
- tmp6 =_mm_xor_si128(tmp6, KEY[0]);
- tmp7 =_mm_xor_si128(tmp7, KEY[0]);
- tmp8 =_mm_xor_si128(tmp8, KEY[0]);
- /* 128 x 128 Carryless Multiply */
- XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+0]);
- XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
- XV = _mm_xor_si128(XV, X);
- gfmul_only(XV, HT[7], &r0, &r1);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[1]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[1]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[1]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[1]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[1]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[1]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[1]);
- /* 128 x 128 Carryless Multiply */
- XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+1]);
- XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
- gfmul_only(XV, HT[6], &r0, &r1);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[2]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[2]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[2]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[2]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[2]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[2]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[2]);
- /* 128 x 128 Carryless Multiply */
- XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+2]);
- XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
- gfmul_only(XV, HT[5], &r0, &r1);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[3]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[3]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[3]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[3]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[3]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[3]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[3]);
- /* 128 x 128 Carryless Multiply */
- XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+3]);
- XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
- gfmul_only(XV, HT[4], &r0, &r1);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[4]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[4]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[4]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[4]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[4]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[4]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[4]);
- /* 128 x 128 Carryless Multiply */
- XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+4]);
- XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
- gfmul_only(XV, HT[3], &r0, &r1);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[5]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[5]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[5]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[5]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[5]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[5]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[5]);
- /* 128 x 128 Carryless Multiply */
- XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+5]);
- XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
- gfmul_only(XV, HT[2], &r0, &r1);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[6]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[6]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[6]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[6]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[6]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[6]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[6]);
- /* 128 x 128 Carryless Multiply */
- XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+6]);
- XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
- gfmul_only(XV, HT[1], &r0, &r1);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[7]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[7]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[7]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[7]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[7]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[7]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[7]);
- /* 128 x 128 Carryless Multiply */
- XV = _mm_loadu_si128(&((__m128i*)out)[(i-1)*8+7]);
- XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
- gfmul_only(XV, HT[0], &r0, &r1);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[8]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[8]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[8]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[8]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[8]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[8]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[8]);
- /* Reduction */
- X = ghash_red(r0, r1);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[9]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[9]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[9]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[9]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[9]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[9]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[9]);
- lastKey = KEY[10];
- if (nr > 10) {
- tmp1 = _mm_aesenc_si128(tmp1, KEY[10]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[10]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[10]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[10]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[10]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[10]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[10]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[10]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[11]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[11]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[11]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[11]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[11]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[11]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[11]);
- lastKey = KEY[12];
- if (nr > 12) {
- tmp1 = _mm_aesenc_si128(tmp1, KEY[12]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[12]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[12]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[12]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[12]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[12]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[12]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[12]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[13]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[13]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[13]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[13]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[13]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[13]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[13]);
- lastKey = KEY[14];
- }
- }
- AES_ENC_LAST_8();
- }
-
- tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
- tmp2 = _mm_shuffle_epi8(tmp2, BSWAP_MASK);
- tmp3 = _mm_shuffle_epi8(tmp3, BSWAP_MASK);
- tmp4 = _mm_shuffle_epi8(tmp4, BSWAP_MASK);
- tmp5 = _mm_shuffle_epi8(tmp5, BSWAP_MASK);
- tmp6 = _mm_shuffle_epi8(tmp6, BSWAP_MASK);
- tmp7 = _mm_shuffle_epi8(tmp7, BSWAP_MASK);
- tmp8 = _mm_shuffle_epi8(tmp8, BSWAP_MASK);
- tmp1 = _mm_xor_si128(X, tmp1);
- X = gfmul8(tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8,
- HT[0], HT[1], HT[2], HT[3], HT[4], HT[5], HT[6], HT[7]);
- }
- for (k = i*8; k < (int)(nbytes/16); k++) {
- tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
- ctr1 = _mm_add_epi32(ctr1, ONE);
- tmp1 = _mm_xor_si128(tmp1, KEY[0]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
- lastKey = KEY[10];
- if (nr > 10) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
- lastKey = KEY[12];
- if (nr > 12) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
- lastKey = KEY[14];
- }
- }
- tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
- tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[k]));
- _mm_storeu_si128(&((__m128i*)out)[k], tmp1);
- tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
- X =_mm_xor_si128(X, tmp1);
- X = gfmul_shifted(X, H);
- }
-#else /* AES_GCM_AESNI_NO_UNROLL */
- for (k = 0; k < (int)(nbytes/16) && k < 1; k++) {
- tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
- ctr1 = _mm_add_epi32(ctr1, ONE);
- tmp1 = _mm_xor_si128(tmp1, KEY[0]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
- lastKey = KEY[10];
- if (nr > 10) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
- lastKey = KEY[12];
- if (nr > 12) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
- lastKey = KEY[14];
- }
- }
- tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
- tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[k]));
- _mm_storeu_si128(&((__m128i*)out)[k], tmp1);
- tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
- X =_mm_xor_si128(X, tmp1);
- }
- for (; k < (int)(nbytes/16); k++) {
- tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
- ctr1 = _mm_add_epi32(ctr1, ONE);
- tmp1 = _mm_xor_si128(tmp1, KEY[0]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
- X = gfmul_shifted(X, H);
- lastKey = KEY[10];
- if (nr > 10) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
- lastKey = KEY[12];
- if (nr > 12) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
- lastKey = KEY[14];
- }
- }
- tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
- tmp1 = _mm_xor_si128(tmp1, _mm_loadu_si128(&((__m128i*)in)[k]));
- _mm_storeu_si128(&((__m128i*)out)[k], tmp1);
- tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
- X =_mm_xor_si128(X, tmp1);
- }
- if (k > 0) {
- X = gfmul_shifted(X, H);
- }
-#endif /* AES_GCM_AESNI_NO_UNROLL */
-
- /* If one partial block remains */
- if (nbytes % 16) {
- tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
- tmp1 = _mm_xor_si128(tmp1, KEY[0]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
- lastKey = KEY[10];
- if (nr > 10) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
- lastKey = KEY[12];
- if (nr > 12) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
- lastKey = KEY[14];
- }
- }
- tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
- last_block = tmp1;
- for (j=0; j < (int)(nbytes%16); j++)
- ((unsigned char*)&last_block)[j] = in[k*16+j];
- tmp1 = _mm_xor_si128(tmp1, last_block);
- last_block = tmp1;
- for (j=0; j < (int)(nbytes%16); j++)
- out[k*16+j] = ((unsigned char*)&last_block)[j];
- tmp1 = last_block;
- tmp1 = _mm_shuffle_epi8(tmp1, BSWAP_MASK);
- X =_mm_xor_si128(X, tmp1);
- X = gfmul_shifted(X, H);
- }
- tmp1 = _mm_insert_epi64(tmp1, nbytes*8, 0);
- tmp1 = _mm_insert_epi64(tmp1, abytes*8, 1);
- X = _mm_xor_si128(X, tmp1);
- X = gfmul_shifted(X, H);
- X = _mm_shuffle_epi8(X, BSWAP_MASK);
- T = _mm_xor_si128(X, T);
- /*_mm_storeu_si128((__m128i*)tag, T);*/
- XMEMCPY(tag, &T, tbytes);
-}
-
-#ifdef HAVE_AES_DECRYPT
-
-static void AES_GCM_decrypt(const unsigned char *in,
- unsigned char *out,
- const unsigned char* addt,
- const unsigned char* ivec,
- const unsigned char *tag, int nbytes, int abytes,
- int ibytes, word32 tbytes, const unsigned char* key,
- int nr, int* res)
-{
- int i, j ,k;
- __m128i H, Y, T;
- __m128i *KEY = (__m128i*)key, lastKey;
- __m128i ctr1;
- __m128i last_block = _mm_setzero_si128();
- __m128i X = _mm_setzero_si128();
- __m128i tmp1, tmp2, XV;
-#ifndef AES_GCM_AESNI_NO_UNROLL
- __m128i HT[8];
- __m128i r0, r1;
- __m128i tmp3, tmp4, tmp5, tmp6, tmp7, tmp8;
-#endif /* AES_GCM_AESNI_NO_UNROLL */
-
- if (ibytes == GCM_NONCE_MID_SZ)
- aes_gcm_calc_iv_12(KEY, ivec, nr, H, Y, T);
- else
- aes_gcm_calc_iv(KEY, ivec, ibytes, nr, H, Y, T);
-
- for (i=0; i 10) {
- tmp1 = _mm_aesenc_si128(tmp1, KEY[10]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[10]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[10]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[10]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[10]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[10]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[10]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[10]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[11]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[11]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[11]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[11]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[11]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[11]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[11]);
- lastKey = KEY[12];
- if (nr > 12) {
- tmp1 = _mm_aesenc_si128(tmp1, KEY[12]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[12]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[12]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[12]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[12]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[12]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[12]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[12]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
- tmp2 = _mm_aesenc_si128(tmp2, KEY[13]);
- tmp3 = _mm_aesenc_si128(tmp3, KEY[13]);
- tmp4 = _mm_aesenc_si128(tmp4, KEY[13]);
- tmp5 = _mm_aesenc_si128(tmp5, KEY[13]);
- tmp6 = _mm_aesenc_si128(tmp6, KEY[13]);
- tmp7 = _mm_aesenc_si128(tmp7, KEY[13]);
- tmp8 = _mm_aesenc_si128(tmp8, KEY[13]);
- lastKey = KEY[14];
- }
- }
- AES_ENC_LAST_8();
- }
- }
-
-#endif /* AES_GCM_AESNI_NO_UNROLL */
-
- for (k = i*8; k < nbytes/16; k++) {
- tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
- ctr1 = _mm_add_epi32(ctr1, ONE);
- tmp1 = _mm_xor_si128(tmp1, KEY[0]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
- /* 128 x 128 Carryless Multiply */
- XV = _mm_loadu_si128(&((__m128i*)in)[k]);
- XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
- XV = _mm_xor_si128(XV, X);
- X = gfmul_shifted(XV, H);
- lastKey = KEY[10];
- if (nr > 10) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
- lastKey = KEY[12];
- if (nr > 12) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
- lastKey = KEY[14];
- }
- }
- tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
- tmp2 = _mm_loadu_si128(&((__m128i*)in)[k]);
- tmp1 = _mm_xor_si128(tmp1, tmp2);
- _mm_storeu_si128(&((__m128i*)out)[k], tmp1);
- }
-
- /* If one partial block remains */
- if (nbytes % 16) {
- tmp1 = _mm_shuffle_epi8(ctr1, BSWAP_EPI64);
- tmp1 = _mm_xor_si128(tmp1, KEY[0]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[1]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[2]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[3]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[4]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[5]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[6]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[7]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[8]);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[9]);
- lastKey = KEY[10];
- if (nr > 10) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[11]);
- lastKey = KEY[12];
- if (nr > 12) {
- tmp1 = _mm_aesenc_si128(tmp1, lastKey);
- tmp1 = _mm_aesenc_si128(tmp1, KEY[13]);
- lastKey = KEY[14];
- }
- }
- tmp1 = _mm_aesenclast_si128(tmp1, lastKey);
- last_block = _mm_setzero_si128();
- for (j=0; j < nbytes%16; j++)
- ((unsigned char*)&last_block)[j] = in[k*16+j];
- XV = last_block;
- tmp1 = _mm_xor_si128(tmp1, last_block);
- last_block = tmp1;
- for (j=0; j < nbytes%16; j++)
- out[k*16+j] = ((unsigned char*)&last_block)[j];
- XV = _mm_shuffle_epi8(XV, BSWAP_MASK);
- XV = _mm_xor_si128(XV, X);
- X = gfmul_shifted(XV, H);
- }
-
- tmp1 = _mm_insert_epi64(tmp1, nbytes*8, 0);
- tmp1 = _mm_insert_epi64(tmp1, abytes*8, 1);
- /* 128 x 128 Carryless Multiply */
- X = _mm_xor_si128(X, tmp1);
- X = gfmul_shifted(X, H);
- X = _mm_shuffle_epi8(X, BSWAP_MASK);
- T = _mm_xor_si128(X, T);
-
-/* if (0xffff !=
- _mm_movemask_epi8(_mm_cmpeq_epi8(T, _mm_loadu_si128((__m128i*)tag)))) */
- if (XMEMCMP(tag, &T, tbytes) != 0)
- *res = 0; /* in case the authentication failed */
- else
- *res = 1; /* when successful returns 1 */
-}
-
-#endif /* HAVE_AES_DECRYPT */
-#endif /* _MSC_VER */
-#endif /* WOLFSSL_AESNI */
-
-
-#if defined(GCM_SMALL)
-static void GMULT(byte* X, byte* Y)
-{
- byte Z[AES_BLOCK_SIZE];
- byte V[AES_BLOCK_SIZE];
- int i, j;
-
- XMEMSET(Z, 0, AES_BLOCK_SIZE);
- XMEMCPY(V, X, AES_BLOCK_SIZE);
- for (i = 0; i < AES_BLOCK_SIZE; i++)
- {
- byte y = Y[i];
- for (j = 0; j < 8; j++)
- {
- if (y & 0x80) {
- xorbuf(Z, V, AES_BLOCK_SIZE);
- }
-
- RIGHTSHIFTX(V);
- y = y << 1;
- }
- }
- XMEMCPY(X, Z, AES_BLOCK_SIZE);
-}
-
-
-void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
- word32 cSz, byte* s, word32 sSz)
-{
- byte x[AES_BLOCK_SIZE];
- byte scratch[AES_BLOCK_SIZE];
- word32 blocks, partial;
- byte* h = aes->H;
-
- XMEMSET(x, 0, AES_BLOCK_SIZE);
-
- /* Hash in A, the Additional Authentication Data */
- if (aSz != 0 && a != NULL) {
- blocks = aSz / AES_BLOCK_SIZE;
- partial = aSz % AES_BLOCK_SIZE;
- while (blocks--) {
- xorbuf(x, a, AES_BLOCK_SIZE);
- GMULT(x, h);
- a += AES_BLOCK_SIZE;
- }
- if (partial != 0) {
- XMEMSET(scratch, 0, AES_BLOCK_SIZE);
- XMEMCPY(scratch, a, partial);
- xorbuf(x, scratch, AES_BLOCK_SIZE);
- GMULT(x, h);
- }
- }
-
- /* Hash in C, the Ciphertext */
- if (cSz != 0 && c != NULL) {
- blocks = cSz / AES_BLOCK_SIZE;
- partial = cSz % AES_BLOCK_SIZE;
- while (blocks--) {
- xorbuf(x, c, AES_BLOCK_SIZE);
- GMULT(x, h);
- c += AES_BLOCK_SIZE;
- }
- if (partial != 0) {
- XMEMSET(scratch, 0, AES_BLOCK_SIZE);
- XMEMCPY(scratch, c, partial);
- xorbuf(x, scratch, AES_BLOCK_SIZE);
- GMULT(x, h);
- }
- }
-
- /* Hash in the lengths of A and C in bits */
- FlattenSzInBits(&scratch[0], aSz);
- FlattenSzInBits(&scratch[8], cSz);
- xorbuf(x, scratch, AES_BLOCK_SIZE);
- GMULT(x, h);
-
- /* Copy the result into s. */
- XMEMCPY(s, x, sSz);
-}
-
-/* end GCM_SMALL */
-#elif defined(GCM_TABLE)
-
-static const byte R[256][2] = {
- {0x00, 0x00}, {0x01, 0xc2}, {0x03, 0x84}, {0x02, 0x46},
- {0x07, 0x08}, {0x06, 0xca}, {0x04, 0x8c}, {0x05, 0x4e},
- {0x0e, 0x10}, {0x0f, 0xd2}, {0x0d, 0x94}, {0x0c, 0x56},
- {0x09, 0x18}, {0x08, 0xda}, {0x0a, 0x9c}, {0x0b, 0x5e},
- {0x1c, 0x20}, {0x1d, 0xe2}, {0x1f, 0xa4}, {0x1e, 0x66},
- {0x1b, 0x28}, {0x1a, 0xea}, {0x18, 0xac}, {0x19, 0x6e},
- {0x12, 0x30}, {0x13, 0xf2}, {0x11, 0xb4}, {0x10, 0x76},
- {0x15, 0x38}, {0x14, 0xfa}, {0x16, 0xbc}, {0x17, 0x7e},
- {0x38, 0x40}, {0x39, 0x82}, {0x3b, 0xc4}, {0x3a, 0x06},
- {0x3f, 0x48}, {0x3e, 0x8a}, {0x3c, 0xcc}, {0x3d, 0x0e},
- {0x36, 0x50}, {0x37, 0x92}, {0x35, 0xd4}, {0x34, 0x16},
- {0x31, 0x58}, {0x30, 0x9a}, {0x32, 0xdc}, {0x33, 0x1e},
- {0x24, 0x60}, {0x25, 0xa2}, {0x27, 0xe4}, {0x26, 0x26},
- {0x23, 0x68}, {0x22, 0xaa}, {0x20, 0xec}, {0x21, 0x2e},
- {0x2a, 0x70}, {0x2b, 0xb2}, {0x29, 0xf4}, {0x28, 0x36},
- {0x2d, 0x78}, {0x2c, 0xba}, {0x2e, 0xfc}, {0x2f, 0x3e},
- {0x70, 0x80}, {0x71, 0x42}, {0x73, 0x04}, {0x72, 0xc6},
- {0x77, 0x88}, {0x76, 0x4a}, {0x74, 0x0c}, {0x75, 0xce},
- {0x7e, 0x90}, {0x7f, 0x52}, {0x7d, 0x14}, {0x7c, 0xd6},
- {0x79, 0x98}, {0x78, 0x5a}, {0x7a, 0x1c}, {0x7b, 0xde},
- {0x6c, 0xa0}, {0x6d, 0x62}, {0x6f, 0x24}, {0x6e, 0xe6},
- {0x6b, 0xa8}, {0x6a, 0x6a}, {0x68, 0x2c}, {0x69, 0xee},
- {0x62, 0xb0}, {0x63, 0x72}, {0x61, 0x34}, {0x60, 0xf6},
- {0x65, 0xb8}, {0x64, 0x7a}, {0x66, 0x3c}, {0x67, 0xfe},
- {0x48, 0xc0}, {0x49, 0x02}, {0x4b, 0x44}, {0x4a, 0x86},
- {0x4f, 0xc8}, {0x4e, 0x0a}, {0x4c, 0x4c}, {0x4d, 0x8e},
- {0x46, 0xd0}, {0x47, 0x12}, {0x45, 0x54}, {0x44, 0x96},
- {0x41, 0xd8}, {0x40, 0x1a}, {0x42, 0x5c}, {0x43, 0x9e},
- {0x54, 0xe0}, {0x55, 0x22}, {0x57, 0x64}, {0x56, 0xa6},
- {0x53, 0xe8}, {0x52, 0x2a}, {0x50, 0x6c}, {0x51, 0xae},
- {0x5a, 0xf0}, {0x5b, 0x32}, {0x59, 0x74}, {0x58, 0xb6},
- {0x5d, 0xf8}, {0x5c, 0x3a}, {0x5e, 0x7c}, {0x5f, 0xbe},
- {0xe1, 0x00}, {0xe0, 0xc2}, {0xe2, 0x84}, {0xe3, 0x46},
- {0xe6, 0x08}, {0xe7, 0xca}, {0xe5, 0x8c}, {0xe4, 0x4e},
- {0xef, 0x10}, {0xee, 0xd2}, {0xec, 0x94}, {0xed, 0x56},
- {0xe8, 0x18}, {0xe9, 0xda}, {0xeb, 0x9c}, {0xea, 0x5e},
- {0xfd, 0x20}, {0xfc, 0xe2}, {0xfe, 0xa4}, {0xff, 0x66},
- {0xfa, 0x28}, {0xfb, 0xea}, {0xf9, 0xac}, {0xf8, 0x6e},
- {0xf3, 0x30}, {0xf2, 0xf2}, {0xf0, 0xb4}, {0xf1, 0x76},
- {0xf4, 0x38}, {0xf5, 0xfa}, {0xf7, 0xbc}, {0xf6, 0x7e},
- {0xd9, 0x40}, {0xd8, 0x82}, {0xda, 0xc4}, {0xdb, 0x06},
- {0xde, 0x48}, {0xdf, 0x8a}, {0xdd, 0xcc}, {0xdc, 0x0e},
- {0xd7, 0x50}, {0xd6, 0x92}, {0xd4, 0xd4}, {0xd5, 0x16},
- {0xd0, 0x58}, {0xd1, 0x9a}, {0xd3, 0xdc}, {0xd2, 0x1e},
- {0xc5, 0x60}, {0xc4, 0xa2}, {0xc6, 0xe4}, {0xc7, 0x26},
- {0xc2, 0x68}, {0xc3, 0xaa}, {0xc1, 0xec}, {0xc0, 0x2e},
- {0xcb, 0x70}, {0xca, 0xb2}, {0xc8, 0xf4}, {0xc9, 0x36},
- {0xcc, 0x78}, {0xcd, 0xba}, {0xcf, 0xfc}, {0xce, 0x3e},
- {0x91, 0x80}, {0x90, 0x42}, {0x92, 0x04}, {0x93, 0xc6},
- {0x96, 0x88}, {0x97, 0x4a}, {0x95, 0x0c}, {0x94, 0xce},
- {0x9f, 0x90}, {0x9e, 0x52}, {0x9c, 0x14}, {0x9d, 0xd6},
- {0x98, 0x98}, {0x99, 0x5a}, {0x9b, 0x1c}, {0x9a, 0xde},
- {0x8d, 0xa0}, {0x8c, 0x62}, {0x8e, 0x24}, {0x8f, 0xe6},
- {0x8a, 0xa8}, {0x8b, 0x6a}, {0x89, 0x2c}, {0x88, 0xee},
- {0x83, 0xb0}, {0x82, 0x72}, {0x80, 0x34}, {0x81, 0xf6},
- {0x84, 0xb8}, {0x85, 0x7a}, {0x87, 0x3c}, {0x86, 0xfe},
- {0xa9, 0xc0}, {0xa8, 0x02}, {0xaa, 0x44}, {0xab, 0x86},
- {0xae, 0xc8}, {0xaf, 0x0a}, {0xad, 0x4c}, {0xac, 0x8e},
- {0xa7, 0xd0}, {0xa6, 0x12}, {0xa4, 0x54}, {0xa5, 0x96},
- {0xa0, 0xd8}, {0xa1, 0x1a}, {0xa3, 0x5c}, {0xa2, 0x9e},
- {0xb5, 0xe0}, {0xb4, 0x22}, {0xb6, 0x64}, {0xb7, 0xa6},
- {0xb2, 0xe8}, {0xb3, 0x2a}, {0xb1, 0x6c}, {0xb0, 0xae},
- {0xbb, 0xf0}, {0xba, 0x32}, {0xb8, 0x74}, {0xb9, 0xb6},
- {0xbc, 0xf8}, {0xbd, 0x3a}, {0xbf, 0x7c}, {0xbe, 0xbe} };
-
-
-static void GMULT(byte *x, byte m[256][AES_BLOCK_SIZE])
-{
- int i, j;
- byte Z[AES_BLOCK_SIZE];
- byte a;
-
- XMEMSET(Z, 0, sizeof(Z));
-
- for (i = 15; i > 0; i--) {
- xorbuf(Z, m[x[i]], AES_BLOCK_SIZE);
- a = Z[15];
-
- for (j = 15; j > 0; j--) {
- Z[j] = Z[j-1];
- }
-
- Z[0] = R[a][0];
- Z[1] ^= R[a][1];
- }
- xorbuf(Z, m[x[0]], AES_BLOCK_SIZE);
-
- XMEMCPY(x, Z, AES_BLOCK_SIZE);
-}
-
-
-void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
- word32 cSz, byte* s, word32 sSz)
-{
- byte x[AES_BLOCK_SIZE];
- byte scratch[AES_BLOCK_SIZE];
- word32 blocks, partial;
-
- XMEMSET(x, 0, AES_BLOCK_SIZE);
-
- /* Hash in A, the Additional Authentication Data */
- if (aSz != 0 && a != NULL) {
- blocks = aSz / AES_BLOCK_SIZE;
- partial = aSz % AES_BLOCK_SIZE;
- while (blocks--) {
- xorbuf(x, a, AES_BLOCK_SIZE);
- GMULT(x, aes->M0);
- a += AES_BLOCK_SIZE;
- }
- if (partial != 0) {
- XMEMSET(scratch, 0, AES_BLOCK_SIZE);
- XMEMCPY(scratch, a, partial);
- xorbuf(x, scratch, AES_BLOCK_SIZE);
- GMULT(x, aes->M0);
- }
- }
-
- /* Hash in C, the Ciphertext */
- if (cSz != 0 && c != NULL) {
- blocks = cSz / AES_BLOCK_SIZE;
- partial = cSz % AES_BLOCK_SIZE;
- while (blocks--) {
- xorbuf(x, c, AES_BLOCK_SIZE);
- GMULT(x, aes->M0);
- c += AES_BLOCK_SIZE;
- }
- if (partial != 0) {
- XMEMSET(scratch, 0, AES_BLOCK_SIZE);
- XMEMCPY(scratch, c, partial);
- xorbuf(x, scratch, AES_BLOCK_SIZE);
- GMULT(x, aes->M0);
- }
- }
-
- /* Hash in the lengths of A and C in bits */
- FlattenSzInBits(&scratch[0], aSz);
- FlattenSzInBits(&scratch[8], cSz);
- xorbuf(x, scratch, AES_BLOCK_SIZE);
- GMULT(x, aes->M0);
-
- /* Copy the result into s. */
- XMEMCPY(s, x, sSz);
-}
-
-/* end GCM_TABLE */
-#elif defined(WORD64_AVAILABLE) && !defined(GCM_WORD32)
-
-#if !defined(FREESCALE_LTC_AES_GCM)
-static void GMULT(word64* X, word64* Y)
-{
- word64 Z[2] = {0,0};
- word64 V[2];
- int i, j;
- V[0] = X[0]; V[1] = X[1];
-
- for (i = 0; i < 2; i++)
- {
- word64 y = Y[i];
- for (j = 0; j < 64; j++)
- {
- if (y & 0x8000000000000000ULL) {
- Z[0] ^= V[0];
- Z[1] ^= V[1];
- }
-
- if (V[1] & 0x0000000000000001) {
- V[1] >>= 1;
- V[1] |= ((V[0] & 0x0000000000000001) ?
- 0x8000000000000000ULL : 0);
- V[0] >>= 1;
- V[0] ^= 0xE100000000000000ULL;
- }
- else {
- V[1] >>= 1;
- V[1] |= ((V[0] & 0x0000000000000001) ?
- 0x8000000000000000ULL : 0);
- V[0] >>= 1;
- }
- y <<= 1;
- }
- }
- X[0] = Z[0];
- X[1] = Z[1];
-}
-
-
-void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
- word32 cSz, byte* s, word32 sSz)
-{
- word64 x[2] = {0,0};
- word32 blocks, partial;
- word64 bigH[2];
-
- XMEMCPY(bigH, aes->H, AES_BLOCK_SIZE);
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords64(bigH, bigH, AES_BLOCK_SIZE);
- #endif
-
- /* Hash in A, the Additional Authentication Data */
- if (aSz != 0 && a != NULL) {
- word64 bigA[2];
- blocks = aSz / AES_BLOCK_SIZE;
- partial = aSz % AES_BLOCK_SIZE;
- while (blocks--) {
- XMEMCPY(bigA, a, AES_BLOCK_SIZE);
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords64(bigA, bigA, AES_BLOCK_SIZE);
- #endif
- x[0] ^= bigA[0];
- x[1] ^= bigA[1];
- GMULT(x, bigH);
- a += AES_BLOCK_SIZE;
- }
- if (partial != 0) {
- XMEMSET(bigA, 0, AES_BLOCK_SIZE);
- XMEMCPY(bigA, a, partial);
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords64(bigA, bigA, AES_BLOCK_SIZE);
- #endif
- x[0] ^= bigA[0];
- x[1] ^= bigA[1];
- GMULT(x, bigH);
- }
-#ifdef OPENSSL_EXTRA
- /* store AAD partial tag for next call */
- aes->aadH[0] = (word32)((x[0] & 0xFFFFFFFF00000000) >> 32);
- aes->aadH[1] = (word32)(x[0] & 0xFFFFFFFF);
- aes->aadH[2] = (word32)((x[1] & 0xFFFFFFFF00000000) >> 32);
- aes->aadH[3] = (word32)(x[1] & 0xFFFFFFFF);
-#endif
- }
-
- /* Hash in C, the Ciphertext */
- if (cSz != 0 && c != NULL) {
- word64 bigC[2];
- blocks = cSz / AES_BLOCK_SIZE;
- partial = cSz % AES_BLOCK_SIZE;
-#ifdef OPENSSL_EXTRA
- /* Start from last AAD partial tag */
- if(aes->aadLen) {
- x[0] = ((word64)aes->aadH[0]) << 32 | aes->aadH[1];
- x[1] = ((word64)aes->aadH[2]) << 32 | aes->aadH[3];
- }
-#endif
- while (blocks--) {
- XMEMCPY(bigC, c, AES_BLOCK_SIZE);
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords64(bigC, bigC, AES_BLOCK_SIZE);
- #endif
- x[0] ^= bigC[0];
- x[1] ^= bigC[1];
- GMULT(x, bigH);
- c += AES_BLOCK_SIZE;
- }
- if (partial != 0) {
- XMEMSET(bigC, 0, AES_BLOCK_SIZE);
- XMEMCPY(bigC, c, partial);
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords64(bigC, bigC, AES_BLOCK_SIZE);
- #endif
- x[0] ^= bigC[0];
- x[1] ^= bigC[1];
- GMULT(x, bigH);
- }
- }
-
- /* Hash in the lengths in bits of A and C */
- {
- word64 len[2];
- len[0] = aSz; len[1] = cSz;
-#ifdef OPENSSL_EXTRA
- if (aes->aadLen)
- len[0] = (word64)aes->aadLen;
-#endif
- /* Lengths are in bytes. Convert to bits. */
- len[0] *= 8;
- len[1] *= 8;
-
- x[0] ^= len[0];
- x[1] ^= len[1];
- GMULT(x, bigH);
- }
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords64(x, x, AES_BLOCK_SIZE);
- #endif
- XMEMCPY(s, x, sSz);
-}
-#endif /* !FREESCALE_LTC_AES_GCM */
-
-/* end defined(WORD64_AVAILABLE) && !defined(GCM_WORD32) */
-#else /* GCM_WORD32 */
-
-static void GMULT(word32* X, word32* Y)
-{
- word32 Z[4] = {0,0,0,0};
- word32 V[4];
- int i, j;
-
- V[0] = X[0]; V[1] = X[1]; V[2] = X[2]; V[3] = X[3];
-
- for (i = 0; i < 4; i++)
- {
- word32 y = Y[i];
- for (j = 0; j < 32; j++)
- {
- if (y & 0x80000000) {
- Z[0] ^= V[0];
- Z[1] ^= V[1];
- Z[2] ^= V[2];
- Z[3] ^= V[3];
- }
-
- if (V[3] & 0x00000001) {
- V[3] >>= 1;
- V[3] |= ((V[2] & 0x00000001) ? 0x80000000 : 0);
- V[2] >>= 1;
- V[2] |= ((V[1] & 0x00000001) ? 0x80000000 : 0);
- V[1] >>= 1;
- V[1] |= ((V[0] & 0x00000001) ? 0x80000000 : 0);
- V[0] >>= 1;
- V[0] ^= 0xE1000000;
- } else {
- V[3] >>= 1;
- V[3] |= ((V[2] & 0x00000001) ? 0x80000000 : 0);
- V[2] >>= 1;
- V[2] |= ((V[1] & 0x00000001) ? 0x80000000 : 0);
- V[1] >>= 1;
- V[1] |= ((V[0] & 0x00000001) ? 0x80000000 : 0);
- V[0] >>= 1;
- }
- y <<= 1;
- }
- }
- X[0] = Z[0];
- X[1] = Z[1];
- X[2] = Z[2];
- X[3] = Z[3];
-}
-
-
-void GHASH(Aes* aes, const byte* a, word32 aSz, const byte* c,
- word32 cSz, byte* s, word32 sSz)
-{
- word32 x[4] = {0,0,0,0};
- word32 blocks, partial;
- word32 bigH[4];
-
- XMEMCPY(bigH, aes->H, AES_BLOCK_SIZE);
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords(bigH, bigH, AES_BLOCK_SIZE);
- #endif
-
- /* Hash in A, the Additional Authentication Data */
- if (aSz != 0 && a != NULL) {
- word32 bigA[4];
- blocks = aSz / AES_BLOCK_SIZE;
- partial = aSz % AES_BLOCK_SIZE;
- while (blocks--) {
- XMEMCPY(bigA, a, AES_BLOCK_SIZE);
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords(bigA, bigA, AES_BLOCK_SIZE);
- #endif
- x[0] ^= bigA[0];
- x[1] ^= bigA[1];
- x[2] ^= bigA[2];
- x[3] ^= bigA[3];
- GMULT(x, bigH);
- a += AES_BLOCK_SIZE;
- }
- if (partial != 0) {
- XMEMSET(bigA, 0, AES_BLOCK_SIZE);
- XMEMCPY(bigA, a, partial);
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords(bigA, bigA, AES_BLOCK_SIZE);
- #endif
- x[0] ^= bigA[0];
- x[1] ^= bigA[1];
- x[2] ^= bigA[2];
- x[3] ^= bigA[3];
- GMULT(x, bigH);
- }
- }
-
- /* Hash in C, the Ciphertext */
- if (cSz != 0 && c != NULL) {
- word32 bigC[4];
- blocks = cSz / AES_BLOCK_SIZE;
- partial = cSz % AES_BLOCK_SIZE;
- while (blocks--) {
- XMEMCPY(bigC, c, AES_BLOCK_SIZE);
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords(bigC, bigC, AES_BLOCK_SIZE);
- #endif
- x[0] ^= bigC[0];
- x[1] ^= bigC[1];
- x[2] ^= bigC[2];
- x[3] ^= bigC[3];
- GMULT(x, bigH);
- c += AES_BLOCK_SIZE;
- }
- if (partial != 0) {
- XMEMSET(bigC, 0, AES_BLOCK_SIZE);
- XMEMCPY(bigC, c, partial);
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords(bigC, bigC, AES_BLOCK_SIZE);
- #endif
- x[0] ^= bigC[0];
- x[1] ^= bigC[1];
- x[2] ^= bigC[2];
- x[3] ^= bigC[3];
- GMULT(x, bigH);
- }
- }
-
- /* Hash in the lengths in bits of A and C */
- {
- word32 len[4];
-
- /* Lengths are in bytes. Convert to bits. */
- len[0] = (aSz >> (8*sizeof(aSz) - 3));
- len[1] = aSz << 3;
- len[2] = (cSz >> (8*sizeof(cSz) - 3));
- len[3] = cSz << 3;
-
- x[0] ^= len[0];
- x[1] ^= len[1];
- x[2] ^= len[2];
- x[3] ^= len[3];
- GMULT(x, bigH);
- }
- #ifdef LITTLE_ENDIAN_ORDER
- ByteReverseWords(x, x, AES_BLOCK_SIZE);
- #endif
- XMEMCPY(s, x, sSz);
-}
-
-#endif /* end GCM_WORD32 */
-
-
-#if !defined(WOLFSSL_XILINX_CRYPT) && !defined(WOLFSSL_AFALG_XILINX_AES)
-#ifdef FREESCALE_LTC_AES_GCM
-int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- status_t status;
- word32 keySize;
-
- /* argument checks */
- if (aes == NULL || authTagSz > AES_BLOCK_SIZE || ivSz == 0) {
- return BAD_FUNC_ARG;
- }
-
- if (authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ) {
- WOLFSSL_MSG("GcmEncrypt authTagSz too small error");
- return BAD_FUNC_ARG;
- }
-
- status = wc_AesGetKeySize(aes, &keySize);
- if (status)
- return status;
-
- status = LTC_AES_EncryptTagGcm(LTC_BASE, in, out, sz, iv, ivSz,
- authIn, authInSz, (byte*)aes->key, keySize, authTag, authTagSz);
-
- return (status == kStatus_Success) ? 0 : AES_GCM_AUTH_E;
-}
-
-#else
-
-#ifdef STM32_CRYPTO_AES_GCM
-
-/* this function supports inline encrypt */
-static int wc_AesGcmEncrypt_STM32(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- int ret;
-#ifdef WOLFSSL_STM32_CUBEMX
- CRYP_HandleTypeDef hcryp;
-#else
- word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)];
-#endif
- word32 keySize;
- int status = HAL_OK;
- word32 blocks = sz / AES_BLOCK_SIZE;
- word32 partial = sz % AES_BLOCK_SIZE;
- byte tag[AES_BLOCK_SIZE];
- byte partialBlock[AES_BLOCK_SIZE];
- byte ctr[AES_BLOCK_SIZE];
- byte* authInPadded = NULL;
- int authPadSz;
-
- ret = wc_AesGetKeySize(aes, &keySize);
- if (ret != 0)
- return ret;
-
-#ifdef WOLFSSL_STM32_CUBEMX
- ret = wc_Stm32_Aes_Init(aes, &hcryp);
- if (ret != 0)
- return ret;
-#endif
-
- ret = wolfSSL_CryptHwMutexLock();
- if (ret != 0) {
- return ret;
- }
-
- XMEMSET(ctr, 0, AES_BLOCK_SIZE);
- if (ivSz == GCM_NONCE_MID_SZ) {
- XMEMCPY(ctr, iv, ivSz);
- ctr[AES_BLOCK_SIZE - 1] = 1;
- }
- else {
- GHASH(aes, NULL, 0, iv, ivSz, ctr, AES_BLOCK_SIZE);
- }
- /* Hardware requires counter + 1 */
- IncrementGcmCounter(ctr);
-
- if (authInSz == 0 || (authInSz % AES_BLOCK_SIZE) != 0) {
- /* Need to pad the AAD to a full block with zeros. */
- authPadSz = ((authInSz / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
- authInPadded = (byte*)XMALLOC(authPadSz, aes->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (authInPadded == NULL) {
- wolfSSL_CryptHwMutexUnLock();
- return MEMORY_E;
- }
- XMEMSET(authInPadded, 0, authPadSz);
- XMEMCPY(authInPadded, authIn, authInSz);
- } else {
- authPadSz = authInSz;
- authInPadded = (byte*)authIn;
- }
-
-#ifdef WOLFSSL_STM32_CUBEMX
- hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ctr;
- hcryp.Init.Header = (STM_CRYPT_TYPE*)authInPadded;
- hcryp.Init.HeaderSize = authInSz;
-
-#ifdef STM32_CRYPTO_AES_ONLY
- /* Set the CRYP parameters */
- hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_GCM_GMAC;
- hcryp.Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
- hcryp.Init.GCMCMACPhase = CRYP_INIT_PHASE;
- HAL_CRYP_Init(&hcryp);
-
- /* GCM init phase */
- status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, 0, NULL, STM32_HAL_TIMEOUT);
- if (status == HAL_OK) {
- /* GCM header phase */
- hcryp.Init.GCMCMACPhase = CRYP_HEADER_PHASE;
- status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, 0, NULL, STM32_HAL_TIMEOUT);
- }
- if (status == HAL_OK) {
- /* GCM payload phase - blocks */
- hcryp.Init.GCMCMACPhase = CRYP_PAYLOAD_PHASE;
- if (blocks) {
- status = HAL_CRYPEx_AES_Auth(&hcryp, (byte*)in,
- (blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT);
- }
- }
- if (status == HAL_OK && (partial != 0 || blocks == 0)) {
- /* GCM payload phase - partial remainder */
- XMEMSET(partialBlock, 0, sizeof(partialBlock));
- XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
- status = HAL_CRYPEx_AES_Auth(&hcryp, partialBlock, partial,
- partialBlock, STM32_HAL_TIMEOUT);
- XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
- }
- if (status == HAL_OK) {
- /* GCM final phase */
- hcryp.Init.GCMCMACPhase = CRYP_FINAL_PHASE;
- status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, tag, STM32_HAL_TIMEOUT);
- }
-#elif defined(STM32_HAL_V2)
- hcryp.Init.Algorithm = CRYP_AES_GCM;
- ByteReverseWords((word32*)partialBlock, (word32*)ctr, AES_BLOCK_SIZE);
- hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock;
- HAL_CRYP_Init(&hcryp);
-
- /* GCM payload phase - can handle partial blocks */
- status = HAL_CRYP_Encrypt(&hcryp, (uint32_t*)in,
- (blocks * AES_BLOCK_SIZE) + partial, (uint32_t*)out, STM32_HAL_TIMEOUT);
- if (status == HAL_OK) {
- /* Compute the authTag */
- status = HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp, (uint32_t*)tag,
- STM32_HAL_TIMEOUT);
- }
-#else
- HAL_CRYP_Init(&hcryp);
- if (blocks) {
- /* GCM payload phase - blocks */
- status = HAL_CRYPEx_AESGCM_Encrypt(&hcryp, (byte*)in,
- (blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT);
- }
- if (status == HAL_OK && (partial != 0 || blocks == 0)) {
- /* GCM payload phase - partial remainder */
- XMEMSET(partialBlock, 0, sizeof(partialBlock));
- XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
- status = HAL_CRYPEx_AESGCM_Encrypt(&hcryp, partialBlock, partial,
- partialBlock, STM32_HAL_TIMEOUT);
- XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
- }
- if (status == HAL_OK) {
- /* Compute the authTag */
- status = HAL_CRYPEx_AESGCM_Finish(&hcryp, sz, tag, STM32_HAL_TIMEOUT);
- }
-#endif
-
- if (status != HAL_OK)
- ret = AES_GCM_AUTH_E;
- HAL_CRYP_DeInit(&hcryp);
-
-#else /* STD_PERI_LIB */
- ByteReverseWords(keyCopy, (word32*)aes->key, keySize);
- status = CRYP_AES_GCM(MODE_ENCRYPT, (uint8_t*)ctr,
- (uint8_t*)keyCopy, keySize * 8,
- (uint8_t*)in, sz,
- (uint8_t*)authInPadded, authInSz,
- (uint8_t*)out, tag);
- if (status != SUCCESS)
- ret = AES_GCM_AUTH_E;
-#endif /* WOLFSSL_STM32_CUBEMX */
-
- if (ret == 0) {
- /* return authTag */
- if (authTag) {
- /* STM32 GCM won't compute Auth correctly for partial or
- when IV != 12, so use software here */
- if (sz == 0 || partial != 0 || ivSz != GCM_NONCE_MID_SZ) {
- DecrementGcmCounter(ctr); /* hardware requires +1, so subtract it */
- GHASH(aes, authIn, authInSz, out, sz, authTag, authTagSz);
- wc_AesEncrypt(aes, ctr, tag);
- xorbuf(authTag, tag, authTagSz);
- }
- else {
- XMEMCPY(authTag, tag, authTagSz);
- }
- }
- }
-
- /* Free memory if not a multiple of AES_BLOCK_SZ */
- if (authInPadded != authIn) {
- XFREE(authInPadded, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- wolfSSL_CryptHwMutexUnLock();
-
- return ret;
-}
-
-#endif /* STM32_CRYPTO_AES_GCM */
-
-#ifdef WOLFSSL_AESNI
-int AES_GCM_encrypt_C(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz);
-#else
-static
-#endif
-int AES_GCM_encrypt_C(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- int ret = 0;
- word32 blocks = sz / AES_BLOCK_SIZE;
- word32 partial = sz % AES_BLOCK_SIZE;
- const byte* p = in;
- byte* c = out;
- byte counter[AES_BLOCK_SIZE];
- byte initialCounter[AES_BLOCK_SIZE];
- byte *ctr;
- byte scratch[AES_BLOCK_SIZE];
-#ifdef OPENSSL_EXTRA
- word32 aadTemp;
-#endif
- ctr = counter;
- XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
- XMEMSET(scratch, 0, AES_BLOCK_SIZE);
- if (ivSz == GCM_NONCE_MID_SZ) {
- XMEMCPY(initialCounter, iv, ivSz);
- initialCounter[AES_BLOCK_SIZE - 1] = 1;
- }
- else {
-#ifdef OPENSSL_EXTRA
- aadTemp = aes->aadLen;
- aes->aadLen = 0;
-#endif
- GHASH(aes, NULL, 0, iv, ivSz, initialCounter, AES_BLOCK_SIZE);
-#ifdef OPENSSL_EXTRA
- aes->aadLen = aadTemp;
-#endif
- }
- XMEMCPY(ctr, initialCounter, AES_BLOCK_SIZE);
-
-#ifdef WOLFSSL_PIC32MZ_CRYPT
- if (blocks) {
- /* use initial IV for HW, but don't use it below */
- XMEMCPY(aes->reg, ctr, AES_BLOCK_SIZE);
-
- ret = wc_Pic32AesCrypt(
- aes->key, aes->keylen, aes->reg, AES_BLOCK_SIZE,
- out, in, (blocks * AES_BLOCK_SIZE),
- PIC32_ENCRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_AES_GCM);
- if (ret != 0)
- return ret;
- }
- /* process remainder using partial handling */
-#endif
-
-#if defined(HAVE_AES_ECB) && !defined(WOLFSSL_PIC32MZ_CRYPT)
- /* some hardware acceleration can gain performance from doing AES encryption
- * of the whole buffer at once */
- if (c != p && blocks > 0) { /* can not handle inline encryption */
- while (blocks--) {
- IncrementGcmCounter(ctr);
- XMEMCPY(c, ctr, AES_BLOCK_SIZE);
- c += AES_BLOCK_SIZE;
- }
-
- /* reset number of blocks and then do encryption */
- blocks = sz / AES_BLOCK_SIZE;
- wc_AesEcbEncrypt(aes, out, out, AES_BLOCK_SIZE * blocks);
- xorbuf(out, p, AES_BLOCK_SIZE * blocks);
- p += AES_BLOCK_SIZE * blocks;
- }
- else
-#endif /* HAVE_AES_ECB && !WOLFSSL_PIC32MZ_CRYPT */
-
- while (blocks--) {
- IncrementGcmCounter(ctr);
- #if !defined(WOLFSSL_PIC32MZ_CRYPT)
- wc_AesEncrypt(aes, ctr, scratch);
- xorbuf(scratch, p, AES_BLOCK_SIZE);
- XMEMCPY(c, scratch, AES_BLOCK_SIZE);
- #endif
- p += AES_BLOCK_SIZE;
- c += AES_BLOCK_SIZE;
- }
-
- if (partial != 0) {
- IncrementGcmCounter(ctr);
- wc_AesEncrypt(aes, ctr, scratch);
- xorbuf(scratch, p, partial);
- XMEMCPY(c, scratch, partial);
- }
- if (authTag) {
- GHASH(aes, authIn, authInSz, out, sz, authTag, authTagSz);
- wc_AesEncrypt(aes, initialCounter, scratch);
- xorbuf(authTag, scratch, authTagSz);
-#ifdef OPENSSL_EXTRA
- if (!in && !sz)
- /* store AAD size for next call */
- aes->aadLen = authInSz;
-#endif
- }
-
- return ret;
-}
-
-/* Software AES - GCM Encrypt */
-int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- /* argument checks */
- if (aes == NULL || authTagSz > AES_BLOCK_SIZE || ivSz == 0) {
- return BAD_FUNC_ARG;
- }
-
- if (authTagSz < WOLFSSL_MIN_AUTH_TAG_SZ) {
- WOLFSSL_MSG("GcmEncrypt authTagSz too small error");
- return BAD_FUNC_ARG;
- }
-
-#ifdef WOLF_CRYPTO_CB
- if (aes->devId != INVALID_DEVID) {
- int ret = wc_CryptoCb_AesGcmEncrypt(aes, out, in, sz, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
- if (ret != CRYPTOCB_UNAVAILABLE)
- return ret;
- /* fall-through when unavailable */
- }
-#endif
-
-#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
- /* if async and byte count above threshold */
- /* only 12-byte IV is supported in HW */
- if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
- sz >= WC_ASYNC_THRESH_AES_GCM && ivSz == GCM_NONCE_MID_SZ) {
- #if defined(HAVE_CAVIUM)
- #ifdef HAVE_CAVIUM_V
- if (authInSz == 20) { /* Nitrox V GCM is only working with 20 byte AAD */
- return NitroxAesGcmEncrypt(aes, out, in, sz,
- (const byte*)aes->devKey, aes->keylen, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
- }
- #endif
- #elif defined(HAVE_INTEL_QA)
- return IntelQaSymAesGcmEncrypt(&aes->asyncDev, out, in, sz,
- (const byte*)aes->devKey, aes->keylen, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
- #else /* WOLFSSL_ASYNC_CRYPT_TEST */
- if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_GCM_ENCRYPT)) {
- WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
- testDev->aes.aes = aes;
- testDev->aes.out = out;
- testDev->aes.in = in;
- testDev->aes.sz = sz;
- testDev->aes.iv = iv;
- testDev->aes.ivSz = ivSz;
- testDev->aes.authTag = authTag;
- testDev->aes.authTagSz = authTagSz;
- testDev->aes.authIn = authIn;
- testDev->aes.authInSz = authInSz;
- return WC_PENDING_E;
- }
- #endif
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
-#ifdef STM32_CRYPTO_AES_GCM
- /* The STM standard peripheral library API's doesn't support partial blocks */
- #ifdef STD_PERI_LIB
- if (partial == 0)
- #endif
- {
- return wc_AesGcmEncrypt_STM32(
- aes, out, in, sz, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
- }
-#endif /* STM32_CRYPTO_AES_GCM */
-
-#ifdef WOLFSSL_AESNI
- #ifdef HAVE_INTEL_AVX2
- if (IS_INTEL_AVX2(intel_flags)) {
- AES_GCM_encrypt_avx2(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
- authTagSz, (const byte*)aes->key, aes->rounds);
- return 0;
- }
- else
- #endif
- #ifdef HAVE_INTEL_AVX1
- if (IS_INTEL_AVX1(intel_flags)) {
- AES_GCM_encrypt_avx1(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
- authTagSz, (const byte*)aes->key, aes->rounds);
- return 0;
- }
- else
- #endif
- if (haveAESNI) {
- AES_GCM_encrypt(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
- authTagSz, (const byte*)aes->key, aes->rounds);
- return 0;
- }
- else
-#endif
- {
- return AES_GCM_encrypt_C(aes, out, in, sz, iv, ivSz, authTag, authTagSz,
- authIn, authInSz);
- }
-}
-#endif
-
-
-
-/* AES GCM Decrypt */
-#if defined(HAVE_AES_DECRYPT) || defined(HAVE_AESGCM_DECRYPT)
-#ifdef FREESCALE_LTC_AES_GCM
-int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- const byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- int ret;
- word32 keySize;
- status_t status;
-
- /* argument checks */
- /* If the sz is non-zero, both in and out must be set. If sz is 0,
- * in and out are don't cares, as this is is the GMAC case. */
- if (aes == NULL || iv == NULL || (sz != 0 && (in == NULL || out == NULL)) ||
- authTag == NULL || authTagSz > AES_BLOCK_SIZE || authTagSz == 0 ||
- ivSz == 0) {
-
- return BAD_FUNC_ARG;
- }
-
- ret = wc_AesGetKeySize(aes, &keySize);
- if (ret != 0) {
- return ret;
- }
-
- status = LTC_AES_DecryptTagGcm(LTC_BASE, in, out, sz, iv, ivSz,
- authIn, authInSz, (byte*)aes->key, keySize, authTag, authTagSz);
-
- return (status == kStatus_Success) ? 0 : AES_GCM_AUTH_E;
-}
-
-#else
-
-#ifdef STM32_CRYPTO_AES_GCM
-/* this function supports inline decrypt */
-static int wc_AesGcmDecrypt_STM32(Aes* aes, byte* out,
- const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- const byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- int ret;
-#ifdef WOLFSSL_STM32_CUBEMX
- CRYP_HandleTypeDef hcryp;
-#else
- word32 keyCopy[AES_256_KEY_SIZE/sizeof(word32)];
-#endif
- word32 keySize;
- int status = HAL_OK;
- word32 blocks = sz / AES_BLOCK_SIZE;
- word32 partial = sz % AES_BLOCK_SIZE;
- byte tag[AES_BLOCK_SIZE];
- byte partialBlock[AES_BLOCK_SIZE];
- byte ctr[AES_BLOCK_SIZE];
- byte* authInPadded = NULL;
- int authPadSz;
-
- ret = wc_AesGetKeySize(aes, &keySize);
- if (ret != 0)
- return ret;
-
-#ifdef WOLFSSL_STM32_CUBEMX
- ret = wc_Stm32_Aes_Init(aes, &hcryp);
- if (ret != 0)
- return ret;
-#endif
-
- ret = wolfSSL_CryptHwMutexLock();
- if (ret != 0) {
- return ret;
- }
-
- XMEMSET(ctr, 0, AES_BLOCK_SIZE);
- if (ivSz == GCM_NONCE_MID_SZ) {
- XMEMCPY(ctr, iv, ivSz);
- ctr[AES_BLOCK_SIZE - 1] = 1;
- }
- else {
- GHASH(aes, NULL, 0, iv, ivSz, ctr, AES_BLOCK_SIZE);
- }
- /* Hardware requires counter + 1 */
- IncrementGcmCounter(ctr);
-
- if (authInSz == 0 || (authInSz % AES_BLOCK_SIZE) != 0) {
- /* Need to pad the AAD to a full block with zeros. */
- authPadSz = ((authInSz / AES_BLOCK_SIZE) + 1) * AES_BLOCK_SIZE;
- authInPadded = (byte*)XMALLOC(authPadSz, aes->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (authInPadded == NULL) {
- wolfSSL_CryptHwMutexUnLock();
- return MEMORY_E;
- }
- XMEMSET(authInPadded, 0, authPadSz);
- XMEMCPY(authInPadded, authIn, authInSz);
- } else {
- authPadSz = authInSz;
- authInPadded = (byte*)authIn;
- }
-
-#ifdef WOLFSSL_STM32_CUBEMX
- hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)ctr;
- hcryp.Init.Header = (STM_CRYPT_TYPE*)authInPadded;
- hcryp.Init.HeaderSize = authInSz;
-
-#ifdef STM32_CRYPTO_AES_ONLY
- /* Set the CRYP parameters */
- hcryp.Init.ChainingMode = CRYP_CHAINMODE_AES_GCM_GMAC;
- hcryp.Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
- hcryp.Init.GCMCMACPhase = CRYP_INIT_PHASE;
- HAL_CRYP_Init(&hcryp);
-
- /* GCM init phase */
- status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, 0, NULL, STM32_HAL_TIMEOUT);
- if (status == HAL_OK) {
- /* GCM header phase */
- hcryp.Init.GCMCMACPhase = CRYP_HEADER_PHASE;
- status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, 0, NULL, STM32_HAL_TIMEOUT);
- }
- if (status == HAL_OK) {
- /* GCM payload phase - blocks */
- hcryp.Init.GCMCMACPhase = CRYP_PAYLOAD_PHASE;
- if (blocks) {
- status = HAL_CRYPEx_AES_Auth(&hcryp, (byte*)in,
- (blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT);
- }
- }
- if (status == HAL_OK && (partial != 0 || blocks == 0)) {
- /* GCM payload phase - partial remainder */
- XMEMSET(partialBlock, 0, sizeof(partialBlock));
- XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
- status = HAL_CRYPEx_AES_Auth(&hcryp, partialBlock, partial,
- partialBlock, STM32_HAL_TIMEOUT);
- XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
- }
- if (status == HAL_OK) {
- /* GCM final phase */
- hcryp.Init.GCMCMACPhase = CRYP_FINAL_PHASE;
- status = HAL_CRYPEx_AES_Auth(&hcryp, NULL, sz, tag, STM32_HAL_TIMEOUT);
- }
-#elif defined(STM32_HAL_V2)
- hcryp.Init.Algorithm = CRYP_AES_GCM;
- ByteReverseWords((word32*)partialBlock, (word32*)ctr, AES_BLOCK_SIZE);
- hcryp.Init.pInitVect = (STM_CRYPT_TYPE*)partialBlock;
- HAL_CRYP_Init(&hcryp);
-
- /* GCM payload phase - can handle partial blocks */
- status = HAL_CRYP_Decrypt(&hcryp, (uint32_t*)in,
- (blocks * AES_BLOCK_SIZE) + partial, (uint32_t*)out, STM32_HAL_TIMEOUT);
- if (status == HAL_OK) {
- /* Compute the authTag */
- status = HAL_CRYPEx_AESGCM_GenerateAuthTAG(&hcryp, (uint32_t*)tag,
- STM32_HAL_TIMEOUT);
- }
-#else
- HAL_CRYP_Init(&hcryp);
- if (blocks) {
- /* GCM payload phase - blocks */
- status = HAL_CRYPEx_AESGCM_Decrypt(&hcryp, (byte*)in,
- (blocks * AES_BLOCK_SIZE), out, STM32_HAL_TIMEOUT);
- }
- if (status == HAL_OK && (partial != 0 || blocks == 0)) {
- /* GCM payload phase - partial remainder */
- XMEMSET(partialBlock, 0, sizeof(partialBlock));
- XMEMCPY(partialBlock, in + (blocks * AES_BLOCK_SIZE), partial);
- status = HAL_CRYPEx_AESGCM_Decrypt(&hcryp, partialBlock, partial,
- partialBlock, STM32_HAL_TIMEOUT);
- XMEMCPY(out + (blocks * AES_BLOCK_SIZE), partialBlock, partial);
- }
- if (status == HAL_OK) {
- /* Compute the authTag */
- status = HAL_CRYPEx_AESGCM_Finish(&hcryp, sz, tag, STM32_HAL_TIMEOUT);
- }
-#endif
-
- if (status != HAL_OK)
- ret = AES_GCM_AUTH_E;
-
- HAL_CRYP_DeInit(&hcryp);
-
-#else /* STD_PERI_LIB */
- ByteReverseWords(keyCopy, (word32*)aes->key, aes->keylen);
-
- /* Input size and auth size need to be the actual sizes, even though
- * they are not block aligned, because this length (in bits) is used
- * in the final GHASH. */
- status = CRYP_AES_GCM(MODE_DECRYPT, (uint8_t*)ctr,
- (uint8_t*)keyCopy, keySize * 8,
- (uint8_t*)in, sz,
- (uint8_t*)authInPadded, authInSz,
- (uint8_t*)out, tag);
- if (status != SUCCESS)
- ret = AES_GCM_AUTH_E;
-#endif /* WOLFSSL_STM32_CUBEMX */
-
- /* STM32 GCM hardware only supports IV of 12 bytes, so use software for auth */
- if (sz == 0 || ivSz != GCM_NONCE_MID_SZ) {
- DecrementGcmCounter(ctr); /* hardware requires +1, so subtract it */
- GHASH(aes, authIn, authInSz, in, sz, tag, sizeof(tag));
- wc_AesEncrypt(aes, ctr, partialBlock);
- xorbuf(tag, partialBlock, sizeof(tag));
- }
-
- if (ConstantCompare(authTag, tag, authTagSz) != 0) {
- ret = AES_GCM_AUTH_E;
- }
-
- /* Free memory if not a multiple of AES_BLOCK_SZ */
- if (authInPadded != authIn) {
- XFREE(authInPadded, aes->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- wolfSSL_CryptHwMutexUnLock();
-
- return ret;
-}
-
-#endif /* STM32_CRYPTO_AES_GCM */
-
-#ifdef WOLFSSL_AESNI
-int AES_GCM_decrypt_C(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- const byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz);
-#else
-static
-#endif
-int AES_GCM_decrypt_C(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- const byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- int ret = 0;
- word32 blocks = sz / AES_BLOCK_SIZE;
- word32 partial = sz % AES_BLOCK_SIZE;
- const byte* c = in;
- byte* p = out;
- byte counter[AES_BLOCK_SIZE];
- byte initialCounter[AES_BLOCK_SIZE];
- byte *ctr;
- byte scratch[AES_BLOCK_SIZE];
- byte Tprime[AES_BLOCK_SIZE];
- byte EKY0[AES_BLOCK_SIZE];
-#ifdef OPENSSL_EXTRA
- word32 aadTemp;
-#endif
- ctr = counter;
- XMEMSET(initialCounter, 0, AES_BLOCK_SIZE);
- if (ivSz == GCM_NONCE_MID_SZ) {
- XMEMCPY(initialCounter, iv, ivSz);
- initialCounter[AES_BLOCK_SIZE - 1] = 1;
- }
- else {
-#ifdef OPENSSL_EXTRA
- aadTemp = aes->aadLen;
- aes->aadLen = 0;
-#endif
- GHASH(aes, NULL, 0, iv, ivSz, initialCounter, AES_BLOCK_SIZE);
-#ifdef OPENSSL_EXTRA
- aes->aadLen = aadTemp;
-#endif
- }
- XMEMCPY(ctr, initialCounter, AES_BLOCK_SIZE);
-
- /* Calc the authTag again using the received auth data and the cipher text */
- GHASH(aes, authIn, authInSz, in, sz, Tprime, sizeof(Tprime));
- wc_AesEncrypt(aes, ctr, EKY0);
- xorbuf(Tprime, EKY0, sizeof(Tprime));
-
-#ifdef OPENSSL_EXTRA
- if (!out) {
- /* authenticated, non-confidential data */
- /* store AAD size for next call */
- aes->aadLen = authInSz;
- }
-#endif
- if (ConstantCompare(authTag, Tprime, authTagSz) != 0) {
- return AES_GCM_AUTH_E;
- }
-
-#if defined(WOLFSSL_PIC32MZ_CRYPT)
- if (blocks) {
- /* use initial IV for HW, but don't use it below */
- XMEMCPY(aes->reg, ctr, AES_BLOCK_SIZE);
-
- ret = wc_Pic32AesCrypt(
- aes->key, aes->keylen, aes->reg, AES_BLOCK_SIZE,
- out, in, (blocks * AES_BLOCK_SIZE),
- PIC32_DECRYPTION, PIC32_ALGO_AES, PIC32_CRYPTOALGO_AES_GCM);
- if (ret != 0)
- return ret;
- }
- /* process remainder using partial handling */
-#endif
-
-#if defined(HAVE_AES_ECB) && !defined(WOLFSSL_PIC32MZ_CRYPT)
- /* some hardware acceleration can gain performance from doing AES encryption
- * of the whole buffer at once */
- if (c != p && blocks > 0) { /* can not handle inline decryption */
- while (blocks--) {
- IncrementGcmCounter(ctr);
- XMEMCPY(p, ctr, AES_BLOCK_SIZE);
- p += AES_BLOCK_SIZE;
- }
-
- /* reset number of blocks and then do encryption */
- blocks = sz / AES_BLOCK_SIZE;
-
- wc_AesEcbEncrypt(aes, out, out, AES_BLOCK_SIZE * blocks);
- xorbuf(out, c, AES_BLOCK_SIZE * blocks);
- c += AES_BLOCK_SIZE * blocks;
- }
- else
-#endif /* HAVE_AES_ECB && !PIC32MZ */
- while (blocks--) {
- IncrementGcmCounter(ctr);
- #if !defined(WOLFSSL_PIC32MZ_CRYPT)
- wc_AesEncrypt(aes, ctr, scratch);
- xorbuf(scratch, c, AES_BLOCK_SIZE);
- XMEMCPY(p, scratch, AES_BLOCK_SIZE);
- #endif
- p += AES_BLOCK_SIZE;
- c += AES_BLOCK_SIZE;
- }
-
- if (partial != 0) {
- IncrementGcmCounter(ctr);
- wc_AesEncrypt(aes, ctr, scratch);
- xorbuf(scratch, c, partial);
- XMEMCPY(p, scratch, partial);
- }
-
- return ret;
-}
-
-/* Software AES - GCM Decrypt */
-int wc_AesGcmDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
- const byte* iv, word32 ivSz,
- const byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
-#ifdef WOLFSSL_AESNI
- int res = AES_GCM_AUTH_E;
-#endif
-
- /* argument checks */
- /* If the sz is non-zero, both in and out must be set. If sz is 0,
- * in and out are don't cares, as this is is the GMAC case. */
- if (aes == NULL || iv == NULL || (sz != 0 && (in == NULL || out == NULL)) ||
- authTag == NULL || authTagSz > AES_BLOCK_SIZE || authTagSz == 0 ||
- ivSz == 0) {
-
- return BAD_FUNC_ARG;
- }
-
-#ifdef WOLF_CRYPTO_CB
- if (aes->devId != INVALID_DEVID) {
- int ret = wc_CryptoCb_AesGcmDecrypt(aes, out, in, sz, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
- if (ret != CRYPTOCB_UNAVAILABLE)
- return ret;
- /* fall-through when unavailable */
- }
-#endif
-
-#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
- /* if async and byte count above threshold */
- /* only 12-byte IV is supported in HW */
- if (aes->asyncDev.marker == WOLFSSL_ASYNC_MARKER_AES &&
- sz >= WC_ASYNC_THRESH_AES_GCM && ivSz == GCM_NONCE_MID_SZ) {
- #if defined(HAVE_CAVIUM)
- #ifdef HAVE_CAVIUM_V
- if (authInSz == 20) { /* Nitrox V GCM is only working with 20 byte AAD */
- return NitroxAesGcmDecrypt(aes, out, in, sz,
- (const byte*)aes->devKey, aes->keylen, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
- }
- #endif
- #elif defined(HAVE_INTEL_QA)
- return IntelQaSymAesGcmDecrypt(&aes->asyncDev, out, in, sz,
- (const byte*)aes->devKey, aes->keylen, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
- #else /* WOLFSSL_ASYNC_CRYPT_TEST */
- if (wc_AsyncTestInit(&aes->asyncDev, ASYNC_TEST_AES_GCM_DECRYPT)) {
- WC_ASYNC_TEST* testDev = &aes->asyncDev.test;
- testDev->aes.aes = aes;
- testDev->aes.out = out;
- testDev->aes.in = in;
- testDev->aes.sz = sz;
- testDev->aes.iv = iv;
- testDev->aes.ivSz = ivSz;
- testDev->aes.authTag = (byte*)authTag;
- testDev->aes.authTagSz = authTagSz;
- testDev->aes.authIn = authIn;
- testDev->aes.authInSz = authInSz;
- return WC_PENDING_E;
- }
- #endif
- }
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
-#ifdef STM32_CRYPTO_AES_GCM
- /* The STM standard peripheral library API's doesn't support partial blocks */
- #ifdef STD_PERI_LIB
- if (partial == 0)
- #endif
- {
- return wc_AesGcmDecrypt_STM32(
- aes, out, in, sz, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
- }
-#endif /* STM32_CRYPTO_AES_GCM */
-
-#ifdef WOLFSSL_AESNI
- #ifdef HAVE_INTEL_AVX2
- if (IS_INTEL_AVX2(intel_flags)) {
- AES_GCM_decrypt_avx2(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
- authTagSz, (byte*)aes->key, aes->rounds, &res);
- if (res == 0)
- return AES_GCM_AUTH_E;
- return 0;
- }
- else
- #endif
- #ifdef HAVE_INTEL_AVX1
- if (IS_INTEL_AVX1(intel_flags)) {
- AES_GCM_decrypt_avx1(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
- authTagSz, (byte*)aes->key, aes->rounds, &res);
- if (res == 0)
- return AES_GCM_AUTH_E;
- return 0;
- }
- else
- #endif
- if (haveAESNI) {
- AES_GCM_decrypt(in, out, authIn, iv, authTag, sz, authInSz, ivSz,
- authTagSz, (byte*)aes->key, aes->rounds, &res);
- if (res == 0)
- return AES_GCM_AUTH_E;
- return 0;
- }
- else
-#endif
- {
- return AES_GCM_decrypt_C(aes, out, in, sz, iv, ivSz, authTag, authTagSz,
- authIn, authInSz);
- }
-}
-#endif
-#endif /* HAVE_AES_DECRYPT || HAVE_AESGCM_DECRYPT */
-#endif /* WOLFSSL_XILINX_CRYPT */
-#endif /* end of block for AESGCM implementation selection */
-
-
-/* Common to all, abstract functions that build off of lower level AESGCM
- * functions */
-#ifndef WC_NO_RNG
-
-int wc_AesGcmSetExtIV(Aes* aes, const byte* iv, word32 ivSz)
-{
- int ret = 0;
-
- if (aes == NULL || iv == NULL ||
- (ivSz != GCM_NONCE_MIN_SZ && ivSz != GCM_NONCE_MID_SZ &&
- ivSz != GCM_NONCE_MAX_SZ)) {
-
- ret = BAD_FUNC_ARG;
- }
-
- if (ret == 0) {
- XMEMCPY((byte*)aes->reg, iv, ivSz);
-
- /* If the IV is 96, allow for a 2^64 invocation counter.
- * For any other size for the nonce, limit the invocation
- * counter to 32-bits. (SP 800-38D 8.3) */
- aes->invokeCtr[0] = 0;
- aes->invokeCtr[1] = (ivSz == GCM_NONCE_MID_SZ) ? 0 : 0xFFFFFFFF;
- aes->nonceSz = ivSz;
- }
-
- return ret;
-}
-
-
-int wc_AesGcmSetIV(Aes* aes, word32 ivSz,
- const byte* ivFixed, word32 ivFixedSz,
- WC_RNG* rng)
-{
- int ret = 0;
-
- if (aes == NULL || rng == NULL ||
- (ivSz != GCM_NONCE_MIN_SZ && ivSz != GCM_NONCE_MID_SZ &&
- ivSz != GCM_NONCE_MAX_SZ) ||
- (ivFixed == NULL && ivFixedSz != 0) ||
- (ivFixed != NULL && ivFixedSz != AES_IV_FIXED_SZ)) {
-
- ret = BAD_FUNC_ARG;
- }
-
- if (ret == 0) {
- byte* iv = (byte*)aes->reg;
-
- if (ivFixedSz)
- XMEMCPY(iv, ivFixed, ivFixedSz);
-
- ret = wc_RNG_GenerateBlock(rng, iv + ivFixedSz, ivSz - ivFixedSz);
- }
-
- if (ret == 0) {
- /* If the IV is 96, allow for a 2^64 invocation counter.
- * For any other size for the nonce, limit the invocation
- * counter to 32-bits. (SP 800-38D 8.3) */
- aes->invokeCtr[0] = 0;
- aes->invokeCtr[1] = (ivSz == GCM_NONCE_MID_SZ) ? 0 : 0xFFFFFFFF;
- aes->nonceSz = ivSz;
- }
-
- return ret;
-}
-
-
-int wc_AesGcmEncrypt_ex(Aes* aes, byte* out, const byte* in, word32 sz,
- byte* ivOut, word32 ivOutSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- int ret = 0;
-
- if (aes == NULL || (sz != 0 && (in == NULL || out == NULL)) ||
- ivOut == NULL || ivOutSz != aes->nonceSz ||
- (authIn == NULL && authInSz != 0)) {
-
- ret = BAD_FUNC_ARG;
- }
-
- if (ret == 0) {
- aes->invokeCtr[0]++;
- if (aes->invokeCtr[0] == 0) {
- aes->invokeCtr[1]++;
- if (aes->invokeCtr[1] == 0)
- ret = AES_GCM_OVERFLOW_E;
- }
- }
-
- if (ret == 0) {
- XMEMCPY(ivOut, aes->reg, ivOutSz);
- ret = wc_AesGcmEncrypt(aes, out, in, sz,
- (byte*)aes->reg, ivOutSz,
- authTag, authTagSz,
- authIn, authInSz);
- if (ret == 0)
- IncCtr((byte*)aes->reg, ivOutSz);
- }
-
- return ret;
-}
-
-int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
- const byte* authIn, word32 authInSz,
- byte* authTag, word32 authTagSz, WC_RNG* rng)
-{
- Aes aes;
- int ret;
-
- if (key == NULL || iv == NULL || (authIn == NULL && authInSz != 0) ||
- authTag == NULL || authTagSz == 0 || rng == NULL) {
-
- return BAD_FUNC_ARG;
- }
-
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret == 0) {
- ret = wc_AesGcmSetKey(&aes, key, keySz);
- if (ret == 0)
- ret = wc_AesGcmSetIV(&aes, ivSz, NULL, 0, rng);
- if (ret == 0)
- ret = wc_AesGcmEncrypt_ex(&aes, NULL, NULL, 0, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
- wc_AesFree(&aes);
- }
- ForceZero(&aes, sizeof(aes));
-
- return ret;
-}
-
-int wc_GmacVerify(const byte* key, word32 keySz,
- const byte* iv, word32 ivSz,
- const byte* authIn, word32 authInSz,
- const byte* authTag, word32 authTagSz)
-{
- int ret;
-#ifndef NO_AES_DECRYPT
- Aes aes;
-
- if (key == NULL || iv == NULL || (authIn == NULL && authInSz != 0) ||
- authTag == NULL || authTagSz == 0 || authTagSz > AES_BLOCK_SIZE) {
-
- return BAD_FUNC_ARG;
- }
-
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret == 0) {
- ret = wc_AesGcmSetKey(&aes, key, keySz);
- if (ret == 0)
- ret = wc_AesGcmDecrypt(&aes, NULL, NULL, 0, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
- wc_AesFree(&aes);
- }
- ForceZero(&aes, sizeof(aes));
-#else
- (void)key;
- (void)keySz;
- (void)iv;
- (void)ivSz;
- (void)authIn;
- (void)authInSz;
- (void)authTag;
- (void)authTagSz;
- ret = NOT_COMPILED_IN;
-#endif
- return ret;
-}
-
-#endif /* WC_NO_RNG */
-
-
-WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len)
-{
- if (gmac == NULL || key == NULL) {
- return BAD_FUNC_ARG;
- }
- return wc_AesGcmSetKey(&gmac->aes, key, len);
-}
-
-
-WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
- const byte* authIn, word32 authInSz,
- byte* authTag, word32 authTagSz)
-{
- return wc_AesGcmEncrypt(&gmac->aes, NULL, NULL, 0, iv, ivSz,
- authTag, authTagSz, authIn, authInSz);
-}
-
-#endif /* HAVE_AESGCM */
-
-
-#ifdef HAVE_AESCCM
-
-int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz)
-{
- if (!((keySz == 16) || (keySz == 24) || (keySz == 32)))
- return BAD_FUNC_ARG;
-
- return wc_AesSetKey(aes, key, keySz, NULL, AES_ENCRYPTION);
-}
-
-#ifdef WOLFSSL_ARMASM
- /* implementation located in wolfcrypt/src/port/arm/armv8-aes.c */
-
-#elif defined(HAVE_COLDFIRE_SEC)
- #error "Coldfire SEC doesn't currently support AES-CCM mode"
-
-#elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
- /* implemented in wolfcrypt/src/port/caam_aes.c */
-
-#elif defined(FREESCALE_LTC)
-
-/* return 0 on success */
-int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
- const byte* nonce, word32 nonceSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- byte *key;
- uint32_t keySize;
- status_t status;
-
- /* sanity check on arguments */
- if (aes == NULL || out == NULL || in == NULL || nonce == NULL
- || authTag == NULL || nonceSz < 7 || nonceSz > 13)
- return BAD_FUNC_ARG;
-
- key = (byte*)aes->key;
-
- status = wc_AesGetKeySize(aes, &keySize);
- if (status != 0) {
- return status;
- }
-
- status = LTC_AES_EncryptTagCcm(LTC_BASE, in, out, inSz,
- nonce, nonceSz, authIn, authInSz, key, keySize, authTag, authTagSz);
-
- return (kStatus_Success == status) ? 0 : BAD_FUNC_ARG;
-}
-
-#ifdef HAVE_AES_DECRYPT
-int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
- const byte* nonce, word32 nonceSz,
- const byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- byte *key;
- uint32_t keySize;
- status_t status;
-
- /* sanity check on arguments */
- if (aes == NULL || out == NULL || in == NULL || nonce == NULL
- || authTag == NULL || nonceSz < 7 || nonceSz > 13)
- return BAD_FUNC_ARG;
-
- key = (byte*)aes->key;
-
- status = wc_AesGetKeySize(aes, &keySize);
- if (status != 0) {
- return status;
- }
-
- status = LTC_AES_DecryptTagCcm(LTC_BASE, in, out, inSz,
- nonce, nonceSz, authIn, authInSz, key, keySize, authTag, authTagSz);
-
- if (status == kStatus_Success) {
- return 0;
- }
- else {
- XMEMSET(out, 0, inSz);
- return AES_CCM_AUTH_E;
- }
-}
-#endif /* HAVE_AES_DECRYPT */
-
-#else
-
-/* Software CCM */
-static void roll_x(Aes* aes, const byte* in, word32 inSz, byte* out)
-{
- /* process the bulk of the data */
- while (inSz >= AES_BLOCK_SIZE) {
- xorbuf(out, in, AES_BLOCK_SIZE);
- in += AES_BLOCK_SIZE;
- inSz -= AES_BLOCK_SIZE;
-
- wc_AesEncrypt(aes, out, out);
- }
-
- /* process remainder of the data */
- if (inSz > 0) {
- xorbuf(out, in, inSz);
- wc_AesEncrypt(aes, out, out);
- }
-}
-
-static void roll_auth(Aes* aes, const byte* in, word32 inSz, byte* out)
-{
- word32 authLenSz;
- word32 remainder;
-
- /* encode the length in */
- if (inSz <= 0xFEFF) {
- authLenSz = 2;
- out[0] ^= ((inSz & 0xFF00) >> 8);
- out[1] ^= (inSz & 0x00FF);
- }
- else if (inSz <= 0xFFFFFFFF) {
- authLenSz = 6;
- out[0] ^= 0xFF; out[1] ^= 0xFE;
- out[2] ^= ((inSz & 0xFF000000) >> 24);
- out[3] ^= ((inSz & 0x00FF0000) >> 16);
- out[4] ^= ((inSz & 0x0000FF00) >> 8);
- out[5] ^= (inSz & 0x000000FF);
- }
- /* Note, the protocol handles auth data up to 2^64, but we are
- * using 32-bit sizes right now, so the bigger data isn't handled
- * else if (inSz <= 0xFFFFFFFFFFFFFFFF) {} */
- else
- return;
-
- /* start fill out the rest of the first block */
- remainder = AES_BLOCK_SIZE - authLenSz;
- if (inSz >= remainder) {
- /* plenty of bulk data to fill the remainder of this block */
- xorbuf(out + authLenSz, in, remainder);
- inSz -= remainder;
- in += remainder;
- }
- else {
- /* not enough bulk data, copy what is available, and pad zero */
- xorbuf(out + authLenSz, in, inSz);
- inSz = 0;
- }
- wc_AesEncrypt(aes, out, out);
-
- if (inSz > 0)
- roll_x(aes, in, inSz, out);
-}
-
-
-static WC_INLINE void AesCcmCtrInc(byte* B, word32 lenSz)
-{
- word32 i;
-
- for (i = 0; i < lenSz; i++) {
- if (++B[AES_BLOCK_SIZE - 1 - i] != 0) return;
- }
-}
-
-#ifdef WOLFSSL_AESNI
-static WC_INLINE void AesCcmCtrIncSet4(byte* B, word32 lenSz)
-{
- word32 i;
-
- /* B+1 = B */
- XMEMCPY(B + AES_BLOCK_SIZE * 1, B, AES_BLOCK_SIZE);
- /* B+2,B+3 = B,B+1 */
- XMEMCPY(B + AES_BLOCK_SIZE * 2, B, AES_BLOCK_SIZE * 2);
-
- for (i = 0; i < lenSz; i++) {
- if (++B[AES_BLOCK_SIZE * 1 - 1 - i] != 0) break;
- }
- B[AES_BLOCK_SIZE * 2 - 1] += 2;
- if (B[AES_BLOCK_SIZE * 2 - 1] < 2) {
- for (i = 1; i < lenSz; i++) {
- if (++B[AES_BLOCK_SIZE * 2 - 1 - i] != 0) break;
- }
- }
- B[AES_BLOCK_SIZE * 3 - 1] += 3;
- if (B[AES_BLOCK_SIZE * 3 - 1] < 3) {
- for (i = 1; i < lenSz; i++) {
- if (++B[AES_BLOCK_SIZE * 3 - 1 - i] != 0) break;
- }
- }
-}
-
-static WC_INLINE void AesCcmCtrInc4(byte* B, word32 lenSz)
-{
- word32 i;
-
- B[AES_BLOCK_SIZE - 1] += 4;
- if (B[AES_BLOCK_SIZE - 1] < 4) {
- for (i = 1; i < lenSz; i++) {
- if (++B[AES_BLOCK_SIZE - 1 - i] != 0) break;
- }
- }
-}
-#endif
-
-/* Software AES - CCM Encrypt */
-/* return 0 on success */
-int wc_AesCcmEncrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
- const byte* nonce, word32 nonceSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
-#ifndef WOLFSSL_AESNI
- byte A[AES_BLOCK_SIZE];
- byte B[AES_BLOCK_SIZE];
-#else
- ALIGN128 byte A[AES_BLOCK_SIZE * 4];
- ALIGN128 byte B[AES_BLOCK_SIZE * 4];
-#endif
- byte lenSz;
- word32 i;
- byte mask = 0xFF;
- const word32 wordSz = (word32)sizeof(word32);
-
- /* sanity check on arguments */
- if (aes == NULL || out == NULL || in == NULL || nonce == NULL
- || authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
- authTagSz > AES_BLOCK_SIZE)
- return BAD_FUNC_ARG;
-
- XMEMSET(A, 0, sizeof(A));
- XMEMCPY(B+1, nonce, nonceSz);
- lenSz = AES_BLOCK_SIZE - 1 - (byte)nonceSz;
- B[0] = (authInSz > 0 ? 64 : 0)
- + (8 * (((byte)authTagSz - 2) / 2))
- + (lenSz - 1);
- for (i = 0; i < lenSz; i++) {
- if (mask && i >= wordSz)
- mask = 0x00;
- B[AES_BLOCK_SIZE - 1 - i] = (inSz >> ((8 * i) & mask)) & mask;
- }
-
- wc_AesEncrypt(aes, B, A);
-
- if (authInSz > 0)
- roll_auth(aes, authIn, authInSz, A);
- if (inSz > 0)
- roll_x(aes, in, inSz, A);
- XMEMCPY(authTag, A, authTagSz);
-
- B[0] = lenSz - 1;
- for (i = 0; i < lenSz; i++)
- B[AES_BLOCK_SIZE - 1 - i] = 0;
- wc_AesEncrypt(aes, B, A);
- xorbuf(authTag, A, authTagSz);
-
- B[15] = 1;
-#ifdef WOLFSSL_AESNI
- if (haveAESNI && aes->use_aesni) {
- while (inSz >= AES_BLOCK_SIZE * 4) {
- AesCcmCtrIncSet4(B, lenSz);
-
- AES_ECB_encrypt(B, A, AES_BLOCK_SIZE * 4, (byte*)aes->key,
- aes->rounds);
- xorbuf(A, in, AES_BLOCK_SIZE * 4);
- XMEMCPY(out, A, AES_BLOCK_SIZE * 4);
-
- inSz -= AES_BLOCK_SIZE * 4;
- in += AES_BLOCK_SIZE * 4;
- out += AES_BLOCK_SIZE * 4;
-
- if (inSz < AES_BLOCK_SIZE * 4) {
- AesCcmCtrInc4(B, lenSz);
- }
- }
- }
-#endif
- while (inSz >= AES_BLOCK_SIZE) {
- wc_AesEncrypt(aes, B, A);
- xorbuf(A, in, AES_BLOCK_SIZE);
- XMEMCPY(out, A, AES_BLOCK_SIZE);
-
- AesCcmCtrInc(B, lenSz);
- inSz -= AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- out += AES_BLOCK_SIZE;
- }
- if (inSz > 0) {
- wc_AesEncrypt(aes, B, A);
- xorbuf(A, in, inSz);
- XMEMCPY(out, A, inSz);
- }
-
- ForceZero(A, AES_BLOCK_SIZE);
- ForceZero(B, AES_BLOCK_SIZE);
-
- return 0;
-}
-
-#ifdef HAVE_AES_DECRYPT
-/* Software AES - CCM Decrypt */
-int wc_AesCcmDecrypt(Aes* aes, byte* out, const byte* in, word32 inSz,
- const byte* nonce, word32 nonceSz,
- const byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
-#ifndef WOLFSSL_AESNI
- byte A[AES_BLOCK_SIZE];
- byte B[AES_BLOCK_SIZE];
-#else
- ALIGN128 byte B[AES_BLOCK_SIZE * 4];
- ALIGN128 byte A[AES_BLOCK_SIZE * 4];
-#endif
- byte* o;
- byte lenSz;
- word32 i, oSz;
- int result = 0;
- byte mask = 0xFF;
- const word32 wordSz = (word32)sizeof(word32);
-
- /* sanity check on arguments */
- if (aes == NULL || out == NULL || in == NULL || nonce == NULL
- || authTag == NULL || nonceSz < 7 || nonceSz > 13 ||
- authTagSz > AES_BLOCK_SIZE)
- return BAD_FUNC_ARG;
-
- o = out;
- oSz = inSz;
- XMEMCPY(B+1, nonce, nonceSz);
- lenSz = AES_BLOCK_SIZE - 1 - (byte)nonceSz;
-
- B[0] = lenSz - 1;
- for (i = 0; i < lenSz; i++)
- B[AES_BLOCK_SIZE - 1 - i] = 0;
- B[15] = 1;
-
-#ifdef WOLFSSL_AESNI
- if (haveAESNI && aes->use_aesni) {
- while (oSz >= AES_BLOCK_SIZE * 4) {
- AesCcmCtrIncSet4(B, lenSz);
-
- AES_ECB_encrypt(B, A, AES_BLOCK_SIZE * 4, (byte*)aes->key,
- aes->rounds);
- xorbuf(A, in, AES_BLOCK_SIZE * 4);
- XMEMCPY(o, A, AES_BLOCK_SIZE * 4);
-
- oSz -= AES_BLOCK_SIZE * 4;
- in += AES_BLOCK_SIZE * 4;
- o += AES_BLOCK_SIZE * 4;
-
- if (oSz < AES_BLOCK_SIZE * 4) {
- AesCcmCtrInc4(B, lenSz);
- }
- }
- }
-#endif
- while (oSz >= AES_BLOCK_SIZE) {
- wc_AesEncrypt(aes, B, A);
- xorbuf(A, in, AES_BLOCK_SIZE);
- XMEMCPY(o, A, AES_BLOCK_SIZE);
-
- AesCcmCtrInc(B, lenSz);
- oSz -= AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- o += AES_BLOCK_SIZE;
- }
- if (inSz > 0) {
- wc_AesEncrypt(aes, B, A);
- xorbuf(A, in, oSz);
- XMEMCPY(o, A, oSz);
- }
-
- for (i = 0; i < lenSz; i++)
- B[AES_BLOCK_SIZE - 1 - i] = 0;
- wc_AesEncrypt(aes, B, A);
-
- o = out;
- oSz = inSz;
-
- B[0] = (authInSz > 0 ? 64 : 0)
- + (8 * (((byte)authTagSz - 2) / 2))
- + (lenSz - 1);
- for (i = 0; i < lenSz; i++) {
- if (mask && i >= wordSz)
- mask = 0x00;
- B[AES_BLOCK_SIZE - 1 - i] = (inSz >> ((8 * i) & mask)) & mask;
- }
-
- wc_AesEncrypt(aes, B, A);
-
- if (authInSz > 0)
- roll_auth(aes, authIn, authInSz, A);
- if (inSz > 0)
- roll_x(aes, o, oSz, A);
-
- B[0] = lenSz - 1;
- for (i = 0; i < lenSz; i++)
- B[AES_BLOCK_SIZE - 1 - i] = 0;
- wc_AesEncrypt(aes, B, B);
- xorbuf(A, B, authTagSz);
-
- if (ConstantCompare(A, authTag, authTagSz) != 0) {
- /* If the authTag check fails, don't keep the decrypted data.
- * Unfortunately, you need the decrypted data to calculate the
- * check value. */
- XMEMSET(out, 0, inSz);
- result = AES_CCM_AUTH_E;
- }
-
- ForceZero(A, AES_BLOCK_SIZE);
- ForceZero(B, AES_BLOCK_SIZE);
- o = NULL;
-
- return result;
-}
-
-#endif /* HAVE_AES_DECRYPT */
-#endif /* software CCM */
-
-/* abstract functions that call lower level AESCCM functions */
-#ifndef WC_NO_RNG
-
-int wc_AesCcmSetNonce(Aes* aes, const byte* nonce, word32 nonceSz)
-{
- int ret = 0;
-
- if (aes == NULL || nonce == NULL ||
- nonceSz < CCM_NONCE_MIN_SZ || nonceSz > CCM_NONCE_MAX_SZ) {
-
- ret = BAD_FUNC_ARG;
- }
-
- if (ret == 0) {
- XMEMCPY(aes->reg, nonce, nonceSz);
- aes->nonceSz = nonceSz;
-
- /* Invocation counter should be 2^61 */
- aes->invokeCtr[0] = 0;
- aes->invokeCtr[1] = 0xE0000000;
- }
-
- return ret;
-}
-
-
-int wc_AesCcmEncrypt_ex(Aes* aes, byte* out, const byte* in, word32 sz,
- byte* ivOut, word32 ivOutSz,
- byte* authTag, word32 authTagSz,
- const byte* authIn, word32 authInSz)
-{
- int ret = 0;
-
- if (aes == NULL || out == NULL ||
- (in == NULL && sz != 0) ||
- ivOut == NULL ||
- (authIn == NULL && authInSz != 0) ||
- (ivOutSz != aes->nonceSz)) {
-
- ret = BAD_FUNC_ARG;
- }
-
- if (ret == 0) {
- aes->invokeCtr[0]++;
- if (aes->invokeCtr[0] == 0) {
- aes->invokeCtr[1]++;
- if (aes->invokeCtr[1] == 0)
- ret = AES_CCM_OVERFLOW_E;
- }
- }
-
- if (ret == 0) {
- ret = wc_AesCcmEncrypt(aes, out, in, sz,
- (byte*)aes->reg, aes->nonceSz,
- authTag, authTagSz,
- authIn, authInSz);
- if (ret == 0) {
- XMEMCPY(ivOut, aes->reg, aes->nonceSz);
- IncCtr((byte*)aes->reg, aes->nonceSz);
- }
- }
-
- return ret;
-}
-
-#endif /* WC_NO_RNG */
-
-#endif /* HAVE_AESCCM */
-
-
-/* Initialize Aes for use with async hardware */
-int wc_AesInit(Aes* aes, void* heap, int devId)
-{
- int ret = 0;
-
- if (aes == NULL)
- return BAD_FUNC_ARG;
-
- aes->heap = heap;
-
-#ifdef WOLF_CRYPTO_CB
- aes->devId = devId;
- aes->devCtx = NULL;
-#else
- (void)devId;
-#endif
-#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
- ret = wolfAsync_DevCtxInit(&aes->asyncDev, WOLFSSL_ASYNC_MARKER_AES,
- aes->heap, devId);
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
-#ifdef WOLFSSL_AFALG
- aes->alFd = -1;
- aes->rdFd = -1;
-#endif
-#if defined(WOLFSSL_DEVCRYPTO) && \
- (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
- aes->ctx.cfd = -1;
-#endif
-#if defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES)
- XMEMSET(&aes->ctx, 0, sizeof(aes->ctx));
-#endif
-#ifdef HAVE_AESGCM
-#ifdef OPENSSL_EXTRA
- XMEMSET(aes->aadH, 0, sizeof(aes->aadH));
- aes->aadLen = 0;
-#endif
-#endif
- return ret;
-}
-
-#ifdef HAVE_PKCS11
-int wc_AesInit_Id(Aes* aes, unsigned char* id, int len, void* heap, int devId)
-{
- int ret = 0;
-
- if (aes == NULL)
- ret = BAD_FUNC_ARG;
- if (ret == 0 && (len < 0 || len > AES_MAX_ID_LEN))
- ret = BUFFER_E;
-
- if (ret == 0)
- ret = wc_AesInit(aes, heap, devId);
- if (ret == 0) {
- XMEMCPY(aes->id, id, len);
- aes->idLen = len;
- }
-
- return ret;
-}
-#endif
-
-/* Free Aes from use with async hardware */
-void wc_AesFree(Aes* aes)
-{
- if (aes == NULL)
- return;
-
-#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
- wolfAsync_DevCtxFree(&aes->asyncDev, WOLFSSL_ASYNC_MARKER_AES);
-#endif /* WOLFSSL_ASYNC_CRYPT */
-#if defined(WOLFSSL_AFALG) || defined(WOLFSSL_AFALG_XILINX_AES)
- if (aes->rdFd > 0) { /* negative is error case */
- close(aes->rdFd);
- }
- if (aes->alFd > 0) {
- close(aes->alFd);
- }
-#endif /* WOLFSSL_AFALG */
-#if defined(WOLFSSL_DEVCRYPTO) && \
- (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
- wc_DevCryptoFree(&aes->ctx);
-#endif
-#if defined(WOLF_CRYPTO_CB) || (defined(WOLFSSL_DEVCRYPTO) && \
- (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))) || \
- (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES))
- ForceZero((byte*)aes->devKey, AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE);
-#endif
-}
-
-
-int wc_AesGetKeySize(Aes* aes, word32* keySize)
-{
- int ret = 0;
-
- if (aes == NULL || keySize == NULL) {
- return BAD_FUNC_ARG;
- }
-#if defined(WOLFSSL_CRYPTOCELL) && defined(WOLFSSL_CRYPTOCELL_AES)
- *keySize = aes->ctx.key.keySize;
- return ret;
-#endif
- switch (aes->rounds) {
-#ifdef WOLFSSL_AES_128
- case 10:
- *keySize = 16;
- break;
-#endif
-#ifdef WOLFSSL_AES_192
- case 12:
- *keySize = 24;
- break;
-#endif
-#ifdef WOLFSSL_AES_256
- case 14:
- *keySize = 32;
- break;
-#endif
- default:
- *keySize = 0;
- ret = BAD_FUNC_ARG;
- }
-
- return ret;
-}
-
-#endif /* !WOLFSSL_TI_CRYPT */
-
-#ifdef HAVE_AES_ECB
-#if defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_AES)
- /* implemented in wolfcrypt/src/port/caam/caam_aes.c */
-
-#elif defined(WOLFSSL_AFALG)
- /* implemented in wolfcrypt/src/port/af_alg/afalg_aes.c */
-
-#elif defined(WOLFSSL_DEVCRYPTO_AES)
- /* implemented in wolfcrypt/src/port/devcrypt/devcrypto_aes.c */
-
-#elif defined(WOLFSSL_SCE) && !defined(WOLFSSL_SCE_NO_AES)
-
-/* Software AES - ECB */
-int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- if ((in == NULL) || (out == NULL) || (aes == NULL))
- return BAD_FUNC_ARG;
-
- return AES_ECB_encrypt(aes, in, out, sz);
-}
-
-
-int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- if ((in == NULL) || (out == NULL) || (aes == NULL))
- return BAD_FUNC_ARG;
-
- return AES_ECB_decrypt(aes, in, out, sz);
-}
-
-#else
-
-/* Software AES - ECB */
-int wc_AesEcbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- word32 blocks = sz / AES_BLOCK_SIZE;
-
- if ((in == NULL) || (out == NULL) || (aes == NULL))
- return BAD_FUNC_ARG;
- while (blocks>0) {
- wc_AesEncryptDirect(aes, out, in);
- out += AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- sz -= AES_BLOCK_SIZE;
- blocks--;
- }
- return 0;
-}
-
-
-int wc_AesEcbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- word32 blocks = sz / AES_BLOCK_SIZE;
-
- if ((in == NULL) || (out == NULL) || (aes == NULL))
- return BAD_FUNC_ARG;
- while (blocks>0) {
- wc_AesDecryptDirect(aes, out, in);
- out += AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- sz -= AES_BLOCK_SIZE;
- blocks--;
- }
- return 0;
-}
-#endif
-#endif /* HAVE_AES_ECB */
-
-#if defined(WOLFSSL_AES_CFB) || defined(WOLFSSL_AES_OFB)
-/* Feedback AES mode
- *
- * aes structure holding key to use for encryption
- * out buffer to hold result of encryption (must be at least as large as input
- * buffer)
- * in buffer to encrypt
- * sz size of input buffer
- * mode flag to specify AES mode
- *
- * returns 0 on success and negative error values on failure
- */
-/* Software AES - CFB Encrypt */
-static int wc_AesFeedbackEncrypt(Aes* aes, byte* out, const byte* in,
- word32 sz, byte mode)
-{
- byte* tmp = NULL;
-#ifdef WOLFSSL_AES_CFB
- byte* reg = NULL;
-#endif
-
- if (aes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
-#ifdef WOLFSSL_AES_CFB
- if (aes->left && sz) {
- reg = (byte*)aes->reg + AES_BLOCK_SIZE - aes->left;
- }
-#endif
-
- /* consume any unused bytes left in aes->tmp */
- tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
- while (aes->left && sz) {
- *(out) = *(in++) ^ *(tmp++);
- #ifdef WOLFSSL_AES_CFB
- if (mode == AES_CFB_MODE) {
- *(reg++) = *out;
- }
- #endif
- out++;
- aes->left--;
- sz--;
- }
-
- while (sz >= AES_BLOCK_SIZE) {
- /* Using aes->tmp here for inline case i.e. in=out */
- wc_AesEncryptDirect(aes, (byte*)aes->tmp, (byte*)aes->reg);
- #ifdef WOLFSSL_AES_OFB
- if (mode == AES_OFB_MODE) {
- XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
- }
- #endif
- xorbuf((byte*)aes->tmp, in, AES_BLOCK_SIZE);
- #ifdef WOLFSSL_AES_CFB
- if (mode == AES_CFB_MODE) {
- XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
- }
- #endif
- XMEMCPY(out, aes->tmp, AES_BLOCK_SIZE);
- out += AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- sz -= AES_BLOCK_SIZE;
- aes->left = 0;
- }
-
- /* encrypt left over data */
- if (sz) {
- wc_AesEncryptDirect(aes, (byte*)aes->tmp, (byte*)aes->reg);
- aes->left = AES_BLOCK_SIZE;
- tmp = (byte*)aes->tmp;
- #ifdef WOLFSSL_AES_OFB
- if (mode == AES_OFB_MODE) {
- XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
- }
- #endif
- #ifdef WOLFSSL_AES_CFB
- reg = (byte*)aes->reg;
- #endif
-
- while (sz--) {
- *(out) = *(in++) ^ *(tmp++);
- #ifdef WOLFSSL_AES_CFB
- if (mode == AES_CFB_MODE) {
- *(reg++) = *out;
- }
- #endif
- out++;
- aes->left--;
- }
- }
-
- return 0;
-}
-
-
-#ifdef HAVE_AES_DECRYPT
-/* CFB 128
- *
- * aes structure holding key to use for decryption
- * out buffer to hold result of decryption (must be at least as large as input
- * buffer)
- * in buffer to decrypt
- * sz size of input buffer
- *
- * returns 0 on success and negative error values on failure
- */
-/* Software AES - CFB Decrypt */
-static int wc_AesFeedbackDecrypt(Aes* aes, byte* out, const byte* in, word32 sz,
- byte mode)
-{
- byte* tmp;
-
- if (aes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
- #ifdef WOLFSSL_AES_CFB
- /* check if more input needs copied over to aes->reg */
- if (aes->left && sz && mode == AES_CFB_MODE) {
- int size = min(aes->left, sz);
- XMEMCPY((byte*)aes->reg + AES_BLOCK_SIZE - aes->left, in, size);
- }
- #endif
-
- /* consume any unused bytes left in aes->tmp */
- tmp = (byte*)aes->tmp + AES_BLOCK_SIZE - aes->left;
- while (aes->left && sz) {
- *(out++) = *(in++) ^ *(tmp++);
- aes->left--;
- sz--;
- }
-
- while (sz > AES_BLOCK_SIZE) {
- /* Using aes->tmp here for inline case i.e. in=out */
- wc_AesEncryptDirect(aes, (byte*)aes->tmp, (byte*)aes->reg);
- #ifdef WOLFSSL_AES_OFB
- if (mode == AES_OFB_MODE) {
- XMEMCPY((byte*)aes->reg, (byte*)aes->tmp, AES_BLOCK_SIZE);
- }
- #endif
- xorbuf((byte*)aes->tmp, in, AES_BLOCK_SIZE);
- #ifdef WOLFSSL_AES_CFB
- if (mode == AES_CFB_MODE) {
- XMEMCPY(aes->reg, in, AES_BLOCK_SIZE);
- }
- #endif
- XMEMCPY(out, (byte*)aes->tmp, AES_BLOCK_SIZE);
- out += AES_BLOCK_SIZE;
- in += AES_BLOCK_SIZE;
- sz -= AES_BLOCK_SIZE;
- aes->left = 0;
- }
-
- /* decrypt left over data */
- if (sz) {
- wc_AesEncryptDirect(aes, (byte*)aes->tmp, (byte*)aes->reg);
- #ifdef WOLFSSL_AES_CFB
- if (mode == AES_CFB_MODE) {
- XMEMCPY(aes->reg, in, sz);
- }
- #endif
- #ifdef WOLFSSL_AES_OFB
- if (mode == AES_OFB_MODE) {
- XMEMCPY(aes->reg, aes->tmp, AES_BLOCK_SIZE);
- }
- #endif
-
- aes->left = AES_BLOCK_SIZE;
- tmp = (byte*)aes->tmp;
-
- while (sz--) {
- *(out++) = *(in++) ^ *(tmp++);
- aes->left--;
- }
- }
-
- return 0;
-}
-#endif /* HAVE_AES_DECRYPT */
-#endif /* WOLFSSL_AES_CFB */
-
-#ifdef WOLFSSL_AES_CFB
-/* CFB 128
- *
- * aes structure holding key to use for encryption
- * out buffer to hold result of encryption (must be at least as large as input
- * buffer)
- * in buffer to encrypt
- * sz size of input buffer
- *
- * returns 0 on success and negative error values on failure
- */
-/* Software AES - CFB Encrypt */
-int wc_AesCfbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- return wc_AesFeedbackEncrypt(aes, out, in, sz, AES_CFB_MODE);
-}
-
-
-#ifdef HAVE_AES_DECRYPT
-/* CFB 128
- *
- * aes structure holding key to use for decryption
- * out buffer to hold result of decryption (must be at least as large as input
- * buffer)
- * in buffer to decrypt
- * sz size of input buffer
- *
- * returns 0 on success and negative error values on failure
- */
-/* Software AES - CFB Decrypt */
-int wc_AesCfbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- return wc_AesFeedbackDecrypt(aes, out, in, sz, AES_CFB_MODE);
-}
-#endif /* HAVE_AES_DECRYPT */
-
-
-/* shift the whole AES_BLOCK_SIZE array left by 8 or 1 bits */
-static void shiftLeftArray(byte* ary, byte shift)
-{
- int i;
-
- if (shift == WOLFSSL_BIT_SIZE) {
- /* shifting over by 8 bits */
- for (i = 0; i < AES_BLOCK_SIZE - 1; i++) {
- ary[i] = ary[i+1];
- }
- ary[i] = 0;
- }
- else {
- byte carry = 0;
-
- /* shifting over by 7 or less bits */
- for (i = 0; i < AES_BLOCK_SIZE - 1; i++) {
- carry = ary[i+1] & (0XFF << (WOLFSSL_BIT_SIZE - shift));
- carry >>= (WOLFSSL_BIT_SIZE - shift);
- ary[i] = (ary[i] << shift) + carry;
- }
- ary[i] = ary[i] << shift;
- }
-}
-
-
-/* returns 0 on success and negative values on failure */
-static int wc_AesFeedbackCFB8(Aes* aes, byte* out, const byte* in,
- word32 sz, byte dir)
-{
- byte *pt;
-
- if (aes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (sz == 0) {
- return 0;
- }
-
- while (sz > 0) {
- wc_AesEncryptDirect(aes, (byte*)aes->tmp, (byte*)aes->reg);
- if (dir == AES_DECRYPTION) {
- pt = (byte*)aes->reg;
-
- /* LSB + CAT */
- shiftLeftArray(pt, WOLFSSL_BIT_SIZE);
- pt[AES_BLOCK_SIZE - 1] = in[0];
- }
-
- /* MSB + XOR */
- out[0] = aes->tmp[0] ^ in[0];
- if (dir == AES_ENCRYPTION) {
- pt = (byte*)aes->reg;
-
- /* LSB + CAT */
- shiftLeftArray(pt, WOLFSSL_BIT_SIZE);
- pt[AES_BLOCK_SIZE - 1] = out[0];
- }
-
- out += 1;
- in += 1;
- sz -= 1;
- }
-
- return 0;
-}
-
-
-/* returns 0 on success and negative values on failure */
-static int wc_AesFeedbackCFB1(Aes* aes, byte* out, const byte* in,
- word32 sz, byte dir)
-{
- byte tmp;
- byte cur = 0; /* hold current work in order to handle inline in=out */
- byte* pt;
- int bit = 7;
-
- if (aes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (sz == 0) {
- return 0;
- }
-
- while (sz > 0) {
- wc_AesEncryptDirect(aes, (byte*)aes->tmp, (byte*)aes->reg);
- if (dir == AES_DECRYPTION) {
- pt = (byte*)aes->reg;
-
- /* LSB + CAT */
- tmp = (0X01 << bit) & in[0];
- tmp = tmp >> bit;
- tmp &= 0x01;
- shiftLeftArray((byte*)aes->reg, 1);
- pt[AES_BLOCK_SIZE - 1] |= tmp;
- }
-
- /* MSB + XOR */
- tmp = (0X01 << bit) & in[0];
- pt = (byte*)aes->tmp;
- tmp = (pt[0] >> 7) ^ (tmp >> bit);
- tmp &= 0x01;
- cur |= (tmp << bit);
-
-
- if (dir == AES_ENCRYPTION) {
- pt = (byte*)aes->reg;
-
- /* LSB + CAT */
- shiftLeftArray((byte*)aes->reg, 1);
- pt[AES_BLOCK_SIZE - 1] |= tmp;
- }
-
- bit--;
- if (bit < 0) {
- out[0] = cur;
- out += 1;
- in += 1;
- sz -= 1;
- bit = 7;
- cur = 0;
- }
- else {
- sz -= 1;
- }
- }
-
- if (bit > 0 && bit < 7) {
- out[0] = cur;
- }
-
- return 0;
-}
-
-
-/* CFB 1
- *
- * aes structure holding key to use for encryption
- * out buffer to hold result of encryption (must be at least as large as input
- * buffer)
- * in buffer to encrypt (packed to left, i.e. 101 is 0x90)
- * sz size of input buffer in bits (0x1 would be size of 1 and 0xFF size of 8)
- *
- * returns 0 on success and negative values on failure
- */
-int wc_AesCfb1Encrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- return wc_AesFeedbackCFB1(aes, out, in, sz, AES_ENCRYPTION);
-}
-
-
-/* CFB 8
- *
- * aes structure holding key to use for encryption
- * out buffer to hold result of encryption (must be at least as large as input
- * buffer)
- * in buffer to encrypt
- * sz size of input buffer
- *
- * returns 0 on success and negative values on failure
- */
-int wc_AesCfb8Encrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- return wc_AesFeedbackCFB8(aes, out, in, sz, AES_ENCRYPTION);
-}
-#ifdef HAVE_AES_DECRYPT
-
-/* CFB 1
- *
- * aes structure holding key to use for encryption
- * out buffer to hold result of encryption (must be at least as large as input
- * buffer)
- * in buffer to encrypt
- * sz size of input buffer in bits (0x1 would be size of 1 and 0xFF size of 8)
- *
- * returns 0 on success and negative values on failure
- */
-int wc_AesCfb1Decrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- return wc_AesFeedbackCFB1(aes, out, in, sz, AES_DECRYPTION);
-}
-
-
-/* CFB 8
- *
- * aes structure holding key to use for encryption
- * out buffer to hold result of encryption (must be at least as large as input
- * buffer)
- * in buffer to encrypt
- * sz size of input buffer
- *
- * returns 0 on success and negative values on failure
- */
-int wc_AesCfb8Decrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- return wc_AesFeedbackCFB8(aes, out, in, sz, AES_DECRYPTION);
-}
-#endif /* HAVE_AES_DECRYPT */
-#endif /* WOLFSSL_AES_CFB */
-
-#ifdef WOLFSSL_AES_OFB
-/* OFB
- *
- * aes structure holding key to use for encryption
- * out buffer to hold result of encryption (must be at least as large as input
- * buffer)
- * in buffer to encrypt
- * sz size of input buffer
- *
- * returns 0 on success and negative error values on failure
- */
-/* Software AES - CFB Encrypt */
-int wc_AesOfbEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- return wc_AesFeedbackEncrypt(aes, out, in, sz, AES_OFB_MODE);
-}
-
-
-#ifdef HAVE_AES_DECRYPT
-/* OFB
- *
- * aes structure holding key to use for decryption
- * out buffer to hold result of decryption (must be at least as large as input
- * buffer)
- * in buffer to decrypt
- * sz size of input buffer
- *
- * returns 0 on success and negative error values on failure
- */
-/* Software AES - OFB Decrypt */
-int wc_AesOfbDecrypt(Aes* aes, byte* out, const byte* in, word32 sz)
-{
- return wc_AesFeedbackDecrypt(aes, out, in, sz, AES_OFB_MODE);
-}
-#endif /* HAVE_AES_DECRYPT */
-#endif /* WOLFSSL_AES_OFB */
-
-
-#ifdef HAVE_AES_KEYWRAP
-
-/* Initialize key wrap counter with value */
-static WC_INLINE void InitKeyWrapCounter(byte* inOutCtr, word32 value)
-{
- int i;
- word32 bytes;
-
- bytes = sizeof(word32);
- for (i = 0; i < (int)sizeof(word32); i++) {
- inOutCtr[i+sizeof(word32)] = (value >> ((bytes - 1) * 8)) & 0xFF;
- bytes--;
- }
-}
-
-/* Increment key wrap counter */
-static WC_INLINE void IncrementKeyWrapCounter(byte* inOutCtr)
-{
- int i;
-
- /* in network byte order so start at end and work back */
- for (i = KEYWRAP_BLOCK_SIZE - 1; i >= 0; i--) {
- if (++inOutCtr[i]) /* we're done unless we overflow */
- return;
- }
-}
-
-/* Decrement key wrap counter */
-static WC_INLINE void DecrementKeyWrapCounter(byte* inOutCtr)
-{
- int i;
-
- for (i = KEYWRAP_BLOCK_SIZE - 1; i >= 0; i--) {
- if (--inOutCtr[i] != 0xFF) /* we're done unless we underflow */
- return;
- }
-}
-
-/* perform AES key wrap (RFC3394), return out sz on success, negative on err */
-int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
- byte* out, word32 outSz, const byte* iv)
-{
- Aes aes;
- byte* r;
- word32 i;
- int ret, j;
-
- byte t[KEYWRAP_BLOCK_SIZE];
- byte tmp[AES_BLOCK_SIZE];
-
- /* n must be at least 2, output size is n + 8 bytes */
- if (key == NULL || in == NULL || inSz < 2 ||
- out == NULL || outSz < (inSz + KEYWRAP_BLOCK_SIZE))
- return BAD_FUNC_ARG;
-
- /* input must be multiple of 64-bits */
- if (inSz % KEYWRAP_BLOCK_SIZE != 0)
- return BAD_FUNC_ARG;
-
- /* user IV is optional */
- if (iv == NULL) {
- XMEMSET(tmp, 0xA6, KEYWRAP_BLOCK_SIZE);
- } else {
- XMEMCPY(tmp, iv, KEYWRAP_BLOCK_SIZE);
- }
-
- r = out + 8;
- XMEMCPY(r, in, inSz);
- XMEMSET(t, 0, sizeof(t));
-
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
-
- ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_ENCRYPTION);
- if (ret != 0)
- return ret;
-
- for (j = 0; j <= 5; j++) {
- for (i = 1; i <= inSz / KEYWRAP_BLOCK_SIZE; i++) {
-
- /* load R[i] */
- XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
-
- wc_AesEncryptDirect(&aes, tmp, tmp);
-
- /* calculate new A */
- IncrementKeyWrapCounter(t);
- xorbuf(tmp, t, KEYWRAP_BLOCK_SIZE);
-
- /* save R[i] */
- XMEMCPY(r, tmp + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
- r += KEYWRAP_BLOCK_SIZE;
- }
- r = out + KEYWRAP_BLOCK_SIZE;
- }
-
- /* C[0] = A */
- XMEMCPY(out, tmp, KEYWRAP_BLOCK_SIZE);
-
- wc_AesFree(&aes);
-
- return inSz + KEYWRAP_BLOCK_SIZE;
-}
-
-int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
- byte* out, word32 outSz, const byte* iv)
-{
- Aes aes;
- byte* r;
- word32 i, n;
- int ret, j;
-
- byte t[KEYWRAP_BLOCK_SIZE];
- byte tmp[AES_BLOCK_SIZE];
-
- const byte* expIv;
- const byte defaultIV[] = {
- 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6
- };
-
- (void)iv;
-
- if (key == NULL || in == NULL || inSz < 3 ||
- out == NULL || outSz < (inSz - KEYWRAP_BLOCK_SIZE))
- return BAD_FUNC_ARG;
-
- /* input must be multiple of 64-bits */
- if (inSz % KEYWRAP_BLOCK_SIZE != 0)
- return BAD_FUNC_ARG;
-
- /* user IV optional */
- if (iv != NULL) {
- expIv = iv;
- } else {
- expIv = defaultIV;
- }
-
- /* A = C[0], R[i] = C[i] */
- XMEMCPY(tmp, in, KEYWRAP_BLOCK_SIZE);
- XMEMCPY(out, in + KEYWRAP_BLOCK_SIZE, inSz - KEYWRAP_BLOCK_SIZE);
- XMEMSET(t, 0, sizeof(t));
-
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
-
- ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_DECRYPTION);
- if (ret != 0)
- return ret;
-
- /* initialize counter to 6n */
- n = (inSz - 1) / KEYWRAP_BLOCK_SIZE;
- InitKeyWrapCounter(t, 6 * n);
-
- for (j = 5; j >= 0; j--) {
- for (i = n; i >= 1; i--) {
-
- /* calculate A */
- xorbuf(tmp, t, KEYWRAP_BLOCK_SIZE);
- DecrementKeyWrapCounter(t);
-
- /* load R[i], starting at end of R */
- r = out + ((i - 1) * KEYWRAP_BLOCK_SIZE);
- XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
- wc_AesDecryptDirect(&aes, tmp, tmp);
-
- /* save R[i] */
- XMEMCPY(r, tmp + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
- }
- }
-
- wc_AesFree(&aes);
-
- /* verify IV */
- if (XMEMCMP(tmp, expIv, KEYWRAP_BLOCK_SIZE) != 0)
- return BAD_KEYWRAP_IV_E;
-
- return inSz - KEYWRAP_BLOCK_SIZE;
-}
-
-#endif /* HAVE_AES_KEYWRAP */
-
-#ifdef WOLFSSL_AES_XTS
-
-/* Galios Field to use */
-#define GF_XTS 0x87
-
-/* This is to help with setting keys to correct encrypt or decrypt type.
- *
- * tweak AES key for tweak in XTS
- * aes AES key for encrypt/decrypt process
- * key buffer holding aes key | tweak key
- * len length of key buffer in bytes. Should be twice that of key size. i.e.
- * 32 for a 16 byte key.
- * dir direction, either AES_ENCRYPTION or AES_DECRYPTION
- * heap heap hint to use for memory. Can be NULL
- * devId id to use with async crypto. Can be 0
- *
- * Note: is up to user to call wc_AesFree on tweak and aes key when done.
- *
- * return 0 on success
- */
-int wc_AesXtsSetKey(XtsAes* aes, const byte* key, word32 len, int dir,
- void* heap, int devId)
-{
- word32 keySz;
- int ret = 0;
-
- if (aes == NULL || key == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if ((ret = wc_AesInit(&aes->tweak, heap, devId)) != 0) {
- return ret;
- }
- if ((ret = wc_AesInit(&aes->aes, heap, devId)) != 0) {
- return ret;
- }
-
- keySz = len/2;
- if (keySz != 16 && keySz != 32) {
- WOLFSSL_MSG("Unsupported key size");
- return WC_KEY_SIZE_E;
- }
-
- if ((ret = wc_AesSetKey(&aes->aes, key, keySz, NULL, dir)) == 0) {
- ret = wc_AesSetKey(&aes->tweak, key + keySz, keySz, NULL,
- AES_ENCRYPTION);
- if (ret != 0) {
- wc_AesFree(&aes->aes);
- }
- }
-
- return ret;
-}
-
-
-/* This is used to free up resources used by Aes structs
- *
- * aes AES keys to free
- *
- * return 0 on success
- */
-int wc_AesXtsFree(XtsAes* aes)
-{
- if (aes != NULL) {
- wc_AesFree(&aes->aes);
- wc_AesFree(&aes->tweak);
- }
-
- return 0;
-}
-
-
-/* Same process as wc_AesXtsEncrypt but uses a word64 type as the tweak value
- * instead of a byte array. This just converts the word64 to a byte array and
- * calls wc_AesXtsEncrypt.
- *
- * aes AES keys to use for block encrypt/decrypt
- * out output buffer to hold cipher text
- * in input plain text buffer to encrypt
- * sz size of both out and in buffers
- * sector value to use for tweak
- *
- * returns 0 on success
- */
-int wc_AesXtsEncryptSector(XtsAes* aes, byte* out, const byte* in,
- word32 sz, word64 sector)
-{
- byte* pt;
- byte i[AES_BLOCK_SIZE];
-
- XMEMSET(i, 0, AES_BLOCK_SIZE);
-#ifdef BIG_ENDIAN_ORDER
- sector = ByteReverseWord64(sector);
-#endif
- pt = (byte*)§or;
- XMEMCPY(i, pt, sizeof(word64));
-
- return wc_AesXtsEncrypt(aes, out, in, sz, (const byte*)i, AES_BLOCK_SIZE);
-}
-
-
-/* Same process as wc_AesXtsDecrypt but uses a word64 type as the tweak value
- * instead of a byte array. This just converts the word64 to a byte array.
- *
- * aes AES keys to use for block encrypt/decrypt
- * out output buffer to hold plain text
- * in input cipher text buffer to encrypt
- * sz size of both out and in buffers
- * sector value to use for tweak
- *
- * returns 0 on success
- */
-int wc_AesXtsDecryptSector(XtsAes* aes, byte* out, const byte* in, word32 sz,
- word64 sector)
-{
- byte* pt;
- byte i[AES_BLOCK_SIZE];
-
- XMEMSET(i, 0, AES_BLOCK_SIZE);
-#ifdef BIG_ENDIAN_ORDER
- sector = ByteReverseWord64(sector);
-#endif
- pt = (byte*)§or;
- XMEMCPY(i, pt, sizeof(word64));
-
- return wc_AesXtsDecrypt(aes, out, in, sz, (const byte*)i, AES_BLOCK_SIZE);
-}
-
-#ifdef HAVE_AES_ECB
-/* helper function for encrypting / decrypting full buffer at once */
-static int _AesXtsHelper(Aes* aes, byte* out, const byte* in, word32 sz, int dir)
-{
- word32 outSz = sz;
- word32 totalSz = (sz / AES_BLOCK_SIZE) * AES_BLOCK_SIZE; /* total bytes */
- byte* pt = out;
-
- outSz -= AES_BLOCK_SIZE;
-
- while (outSz > 0) {
- word32 j;
- byte carry = 0;
-
- /* multiply by shift left and propagate carry */
- for (j = 0; j < AES_BLOCK_SIZE && outSz > 0; j++, outSz--) {
- byte tmpC;
-
- tmpC = (pt[j] >> 7) & 0x01;
- pt[j+AES_BLOCK_SIZE] = ((pt[j] << 1) + carry) & 0xFF;
- carry = tmpC;
- }
- if (carry) {
- pt[AES_BLOCK_SIZE] ^= GF_XTS;
- }
-
- pt += AES_BLOCK_SIZE;
- }
-
- xorbuf(out, in, totalSz);
- if (dir == AES_ENCRYPTION) {
- return wc_AesEcbEncrypt(aes, out, out, totalSz);
- }
- else {
- return wc_AesEcbDecrypt(aes, out, out, totalSz);
- }
-}
-#endif /* HAVE_AES_ECB */
-
-
-/* AES with XTS mode. (XTS) XEX encryption with Tweak and cipher text Stealing.
- *
- * xaes AES keys to use for block encrypt/decrypt
- * out output buffer to hold cipher text
- * in input plain text buffer to encrypt
- * sz size of both out and in buffers
- * i value to use for tweak
- * iSz size of i buffer, should always be AES_BLOCK_SIZE but having this input
- * adds a sanity check on how the user calls the function.
- *
- * returns 0 on success
- */
-/* Software AES - XTS Encrypt */
-int wc_AesXtsEncrypt(XtsAes* xaes, byte* out, const byte* in, word32 sz,
- const byte* i, word32 iSz)
-{
- int ret = 0;
- word32 blocks = (sz / AES_BLOCK_SIZE);
- Aes *aes, *tweak;
-
- if (xaes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
- aes = &xaes->aes;
- tweak = &xaes->tweak;
-
- if (iSz < AES_BLOCK_SIZE) {
- return BAD_FUNC_ARG;
- }
-
- if (blocks > 0) {
- byte tmp[AES_BLOCK_SIZE];
-
- XMEMSET(tmp, 0, AES_BLOCK_SIZE); /* set to 0's in case of improper AES
- * key setup passed to encrypt direct*/
-
- wc_AesEncryptDirect(tweak, tmp, i);
-
- #ifdef HAVE_AES_ECB
- /* encrypt all of buffer at once when possible */
- if (in != out) { /* can not handle inline */
- XMEMCPY(out, tmp, AES_BLOCK_SIZE);
- if ((ret = _AesXtsHelper(aes, out, in, sz, AES_ENCRYPTION)) != 0) {
- return ret;
- }
- }
- #endif
-
- while (blocks > 0) {
- word32 j;
- byte carry = 0;
- byte buf[AES_BLOCK_SIZE];
-
- #ifdef HAVE_AES_ECB
- if (in == out) { /* check for if inline */
- #endif
- XMEMCPY(buf, in, AES_BLOCK_SIZE);
- xorbuf(buf, tmp, AES_BLOCK_SIZE);
- wc_AesEncryptDirect(aes, out, buf);
- #ifdef HAVE_AES_ECB
- }
- #endif
- xorbuf(out, tmp, AES_BLOCK_SIZE);
-
- /* multiply by shift left and propagate carry */
- for (j = 0; j < AES_BLOCK_SIZE; j++) {
- byte tmpC;
-
- tmpC = (tmp[j] >> 7) & 0x01;
- tmp[j] = ((tmp[j] << 1) + carry) & 0xFF;
- carry = tmpC;
- }
- if (carry) {
- tmp[0] ^= GF_XTS;
- }
-
- in += AES_BLOCK_SIZE;
- out += AES_BLOCK_SIZE;
- sz -= AES_BLOCK_SIZE;
- blocks--;
- }
-
- /* stealing operation of XTS to handle left overs */
- if (sz > 0) {
- byte buf[AES_BLOCK_SIZE];
-
- XMEMCPY(buf, out - AES_BLOCK_SIZE, AES_BLOCK_SIZE);
- if (sz >= AES_BLOCK_SIZE) { /* extra sanity check before copy */
- return BUFFER_E;
- }
- XMEMCPY(out, buf, sz);
- XMEMCPY(buf, in, sz);
-
- xorbuf(buf, tmp, AES_BLOCK_SIZE);
- wc_AesEncryptDirect(aes, out - AES_BLOCK_SIZE, buf);
- xorbuf(out - AES_BLOCK_SIZE, tmp, AES_BLOCK_SIZE);
- }
- }
- else {
- WOLFSSL_MSG("Plain text input too small for encryption");
- return BAD_FUNC_ARG;
- }
-
- return ret;
-}
-
-
-/* Same process as encryption but Aes key is AES_DECRYPTION type.
- *
- * xaes AES keys to use for block encrypt/decrypt
- * out output buffer to hold plain text
- * in input cipher text buffer to decrypt
- * sz size of both out and in buffers
- * i value to use for tweak
- * iSz size of i buffer, should always be AES_BLOCK_SIZE but having this input
- * adds a sanity check on how the user calls the function.
- *
- * returns 0 on success
- */
-/* Software AES - XTS Decrypt */
-int wc_AesXtsDecrypt(XtsAes* xaes, byte* out, const byte* in, word32 sz,
- const byte* i, word32 iSz)
-{
- int ret = 0;
- word32 blocks = (sz / AES_BLOCK_SIZE);
- Aes *aes, *tweak;
-
- if (xaes == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
- aes = &xaes->aes;
- tweak = &xaes->tweak;
-
- if (iSz < AES_BLOCK_SIZE) {
- return BAD_FUNC_ARG;
- }
-
- if (blocks > 0) {
- word32 j;
- byte carry = 0;
- byte tmp[AES_BLOCK_SIZE];
- byte stl = (sz % AES_BLOCK_SIZE);
-
- XMEMSET(tmp, 0, AES_BLOCK_SIZE); /* set to 0's in case of improper AES
- * key setup passed to decrypt direct*/
-
- wc_AesEncryptDirect(tweak, tmp, i);
-
- /* if Stealing then break out of loop one block early to handle special
- * case */
- if (stl > 0) {
- blocks--;
- }
-
- #ifdef HAVE_AES_ECB
- /* decrypt all of buffer at once when possible */
- if (in != out) { /* can not handle inline */
- XMEMCPY(out, tmp, AES_BLOCK_SIZE);
- if ((ret = _AesXtsHelper(aes, out, in, sz, AES_DECRYPTION)) != 0) {
- return ret;
- }
- }
- #endif
-
- while (blocks > 0) {
- byte buf[AES_BLOCK_SIZE];
-
- #ifdef HAVE_AES_ECB
- if (in == out) { /* check for if inline */
- #endif
- XMEMCPY(buf, in, AES_BLOCK_SIZE);
- xorbuf(buf, tmp, AES_BLOCK_SIZE);
- wc_AesDecryptDirect(aes, out, buf);
- #ifdef HAVE_AES_ECB
- }
- #endif
- xorbuf(out, tmp, AES_BLOCK_SIZE);
-
- /* multiply by shift left and propagate carry */
- for (j = 0; j < AES_BLOCK_SIZE; j++) {
- byte tmpC;
-
- tmpC = (tmp[j] >> 7) & 0x01;
- tmp[j] = ((tmp[j] << 1) + carry) & 0xFF;
- carry = tmpC;
- }
- if (carry) {
- tmp[0] ^= GF_XTS;
- }
- carry = 0;
-
- in += AES_BLOCK_SIZE;
- out += AES_BLOCK_SIZE;
- sz -= AES_BLOCK_SIZE;
- blocks--;
- }
-
- /* stealing operation of XTS to handle left overs */
- if (sz > 0) {
- byte buf[AES_BLOCK_SIZE];
- byte tmp2[AES_BLOCK_SIZE];
-
- /* multiply by shift left and propagate carry */
- for (j = 0; j < AES_BLOCK_SIZE; j++) {
- byte tmpC;
-
- tmpC = (tmp[j] >> 7) & 0x01;
- tmp2[j] = ((tmp[j] << 1) + carry) & 0xFF;
- carry = tmpC;
- }
- if (carry) {
- tmp2[0] ^= GF_XTS;
- }
-
- XMEMCPY(buf, in, AES_BLOCK_SIZE);
- xorbuf(buf, tmp2, AES_BLOCK_SIZE);
- wc_AesDecryptDirect(aes, out, buf);
- xorbuf(out, tmp2, AES_BLOCK_SIZE);
-
- /* tmp2 holds partial | last */
- XMEMCPY(tmp2, out, AES_BLOCK_SIZE);
- in += AES_BLOCK_SIZE;
- out += AES_BLOCK_SIZE;
- sz -= AES_BLOCK_SIZE;
-
- /* Make buffer with end of cipher text | last */
- XMEMCPY(buf, tmp2, AES_BLOCK_SIZE);
- if (sz >= AES_BLOCK_SIZE) { /* extra sanity check before copy */
- return BUFFER_E;
- }
- XMEMCPY(buf, in, sz);
- XMEMCPY(out, tmp2, sz);
-
- xorbuf(buf, tmp, AES_BLOCK_SIZE);
- wc_AesDecryptDirect(aes, tmp2, buf);
- xorbuf(tmp2, tmp, AES_BLOCK_SIZE);
- XMEMCPY(out - AES_BLOCK_SIZE, tmp2, AES_BLOCK_SIZE);
- }
- }
- else {
- WOLFSSL_MSG("Plain text input too small for encryption");
- return BAD_FUNC_ARG;
- }
-
- return ret;
-}
-
-#endif /* WOLFSSL_AES_XTS */
-
-#endif /* HAVE_FIPS */
-#endif /* !NO_AES */
diff --git a/client/wolfssl/wolfcrypt/src/aes_asm.S b/client/wolfssl/wolfcrypt/src/aes_asm.S
deleted file mode 100644
index ae1c801..0000000
--- a/client/wolfssl/wolfcrypt/src/aes_asm.S
+++ /dev/null
@@ -1,1338 +0,0 @@
-/* aes_asm.S
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-
-/* This file is in at&t asm syntax, see .asm for intel syntax */
-
-/* See Intel® Advanced Encryption Standard (AES) Instructions Set White Paper
- * by Intel Mobility Group, Israel Development Center, Israel Shay Gueron
- */
-
-
-/*
-AES_CBC_encrypt (const unsigned char *in,
- unsigned char *out,
- unsigned char ivec[16],
- unsigned long length,
- const unsigned char *KS,
- int nr)
-*/
-#ifndef __APPLE__
-.globl AES_CBC_encrypt
-AES_CBC_encrypt:
-#else
-.globl _AES_CBC_encrypt
-_AES_CBC_encrypt:
-#endif
-# parameter 1: %rdi
-# parameter 2: %rsi
-# parameter 3: %rdx
-# parameter 4: %rcx
-# parameter 5: %r8
-# parameter 6: %r9d
-movq %rcx, %r10
-shrq $4, %rcx
-shlq $60, %r10
-je NO_PARTS
-addq $1, %rcx
-NO_PARTS:
-subq $16, %rsi
-movdqa (%rdx), %xmm1
-LOOP:
-pxor (%rdi), %xmm1
-pxor (%r8), %xmm1
-addq $16,%rsi
-addq $16,%rdi
-cmpl $12, %r9d
-aesenc 16(%r8),%xmm1
-aesenc 32(%r8),%xmm1
-aesenc 48(%r8),%xmm1
-aesenc 64(%r8),%xmm1
-aesenc 80(%r8),%xmm1
-aesenc 96(%r8),%xmm1
-aesenc 112(%r8),%xmm1
-aesenc 128(%r8),%xmm1
-aesenc 144(%r8),%xmm1
-movdqa 160(%r8),%xmm2
-jb LAST
-cmpl $14, %r9d
-
-aesenc 160(%r8),%xmm1
-aesenc 176(%r8),%xmm1
-movdqa 192(%r8),%xmm2
-jb LAST
-aesenc 192(%r8),%xmm1
-aesenc 208(%r8),%xmm1
-movdqa 224(%r8),%xmm2
-LAST:
-decq %rcx
-aesenclast %xmm2,%xmm1
-movdqu %xmm1,(%rsi)
-jne LOOP
-ret
-
-
-#if defined(WOLFSSL_AESNI_BY4)
-
-/*
-AES_CBC_decrypt_by4 (const unsigned char *in,
- unsigned char *out,
- unsigned char ivec[16],
- unsigned long length,
- const unsigned char *KS,
- int nr)
-*/
-#ifndef __APPLE__
-.globl AES_CBC_decrypt_by4
-AES_CBC_decrypt_by4:
-#else
-.globl _AES_CBC_decrypt_by4
-_AES_CBC_decrypt_by4:
-#endif
-# parameter 1: %rdi
-# parameter 2: %rsi
-# parameter 3: %rdx
-# parameter 4: %rcx
-# parameter 5: %r8
-# parameter 6: %r9d
-
- movq %rcx, %r10
- shrq $4, %rcx
- shlq $60, %r10
- je DNO_PARTS_4
- addq $1, %rcx
-DNO_PARTS_4:
- movq %rcx, %r10
- shlq $62, %r10
- shrq $62, %r10
- shrq $2, %rcx
- movdqu (%rdx),%xmm5
- je DREMAINDER_4
- subq $64, %rsi
-DLOOP_4:
- movdqu (%rdi), %xmm1
- movdqu 16(%rdi), %xmm2
- movdqu 32(%rdi), %xmm3
- movdqu 48(%rdi), %xmm4
- movdqa %xmm1, %xmm6
- movdqa %xmm2, %xmm7
- movdqa %xmm3, %xmm8
- movdqa %xmm4, %xmm15
- movdqa (%r8), %xmm9
- movdqa 16(%r8), %xmm10
- movdqa 32(%r8), %xmm11
- movdqa 48(%r8), %xmm12
- pxor %xmm9, %xmm1
- pxor %xmm9, %xmm2
- pxor %xmm9, %xmm3
- pxor %xmm9, %xmm4
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm12, %xmm1
- aesdec %xmm12, %xmm2
- aesdec %xmm12, %xmm3
- aesdec %xmm12, %xmm4
- movdqa 64(%r8), %xmm9
- movdqa 80(%r8), %xmm10
- movdqa 96(%r8), %xmm11
- movdqa 112(%r8), %xmm12
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm12, %xmm1
- aesdec %xmm12, %xmm2
- aesdec %xmm12, %xmm3
- aesdec %xmm12, %xmm4
- movdqa 128(%r8), %xmm9
- movdqa 144(%r8), %xmm10
- movdqa 160(%r8), %xmm11
- cmpl $12, %r9d
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- jb DLAST_4
- movdqa 160(%r8), %xmm9
- movdqa 176(%r8), %xmm10
- movdqa 192(%r8), %xmm11
- cmpl $14, %r9d
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- jb DLAST_4
- movdqa 192(%r8), %xmm9
- movdqa 208(%r8), %xmm10
- movdqa 224(%r8), %xmm11
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
-DLAST_4:
- addq $64, %rdi
- addq $64, %rsi
- decq %rcx
- aesdeclast %xmm11, %xmm1
- aesdeclast %xmm11, %xmm2
- aesdeclast %xmm11, %xmm3
- aesdeclast %xmm11, %xmm4
- pxor %xmm5, %xmm1
- pxor %xmm6, %xmm2
- pxor %xmm7, %xmm3
- pxor %xmm8, %xmm4
- movdqu %xmm1, (%rsi)
- movdqu %xmm2, 16(%rsi)
- movdqu %xmm3, 32(%rsi)
- movdqu %xmm4, 48(%rsi)
- movdqa %xmm15,%xmm5
- jne DLOOP_4
- addq $64, %rsi
-DREMAINDER_4:
- cmpq $0, %r10
- je DEND_4
-DLOOP_4_2:
- movdqu (%rdi), %xmm1
- movdqa %xmm1, %xmm15
- addq $16, %rdi
- pxor (%r8), %xmm1
- movdqu 160(%r8), %xmm2
- cmpl $12, %r9d
- aesdec 16(%r8), %xmm1
- aesdec 32(%r8), %xmm1
- aesdec 48(%r8), %xmm1
- aesdec 64(%r8), %xmm1
- aesdec 80(%r8), %xmm1
- aesdec 96(%r8), %xmm1
- aesdec 112(%r8), %xmm1
- aesdec 128(%r8), %xmm1
- aesdec 144(%r8), %xmm1
- jb DLAST_4_2
- movdqu 192(%r8), %xmm2
- cmpl $14, %r9d
- aesdec 160(%r8), %xmm1
- aesdec 176(%r8), %xmm1
- jb DLAST_4_2
- movdqu 224(%r8), %xmm2
- aesdec 192(%r8), %xmm1
- aesdec 208(%r8), %xmm1
-DLAST_4_2:
- aesdeclast %xmm2, %xmm1
- pxor %xmm5, %xmm1
- movdqa %xmm15, %xmm5
- movdqu %xmm1, (%rsi)
- addq $16, %rsi
- decq %r10
- jne DLOOP_4_2
-DEND_4:
- ret
-
-#elif defined(WOLFSSL_AESNI_BY6)
-
-/*
-AES_CBC_decrypt_by6 (const unsigned char *in,
- unsigned char *out,
- unsigned char ivec[16],
- unsigned long length,
- const unsigned char *KS,
- int nr)
-*/
-#ifndef __APPLE__
-.globl AES_CBC_decrypt_by6
-AES_CBC_decrypt_by6:
-#else
-.globl _AES_CBC_decrypt_by6
-_AES_CBC_decrypt_by6:
-#endif
-# parameter 1: %rdi - in
-# parameter 2: %rsi - out
-# parameter 3: %rdx - ivec
-# parameter 4: %rcx - length
-# parameter 5: %r8 - KS
-# parameter 6: %r9d - nr
-
- movq %rcx, %r10
- shrq $4, %rcx
- shlq $60, %r10
- je DNO_PARTS_6
- addq $1, %rcx
-DNO_PARTS_6:
- movq %rax, %r12
- movq %rdx, %r13
- movq %rbx, %r14
- movq $0, %rdx
- movq %rcx, %rax
- movq $6, %rbx
- div %rbx
- movq %rax, %rcx
- movq %rdx, %r10
- movq %r12, %rax
- movq %r13, %rdx
- movq %r14, %rbx
- cmpq $0, %rcx
- movdqu (%rdx), %xmm7
- je DREMAINDER_6
- subq $96, %rsi
-DLOOP_6:
- movdqu (%rdi), %xmm1
- movdqu 16(%rdi), %xmm2
- movdqu 32(%rdi), %xmm3
- movdqu 48(%rdi), %xmm4
- movdqu 64(%rdi), %xmm5
- movdqu 80(%rdi), %xmm6
- movdqa (%r8), %xmm8
- movdqa 16(%r8), %xmm9
- movdqa 32(%r8), %xmm10
- movdqa 48(%r8), %xmm11
- pxor %xmm8, %xmm1
- pxor %xmm8, %xmm2
- pxor %xmm8, %xmm3
- pxor %xmm8, %xmm4
- pxor %xmm8, %xmm5
- pxor %xmm8, %xmm6
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm9, %xmm5
- aesdec %xmm9, %xmm6
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- aesdec %xmm10, %xmm5
- aesdec %xmm10, %xmm6
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm11, %xmm5
- aesdec %xmm11, %xmm6
- movdqa 64(%r8), %xmm8
- movdqa 80(%r8), %xmm9
- movdqa 96(%r8), %xmm10
- movdqa 112(%r8), %xmm11
- aesdec %xmm8, %xmm1
- aesdec %xmm8, %xmm2
- aesdec %xmm8, %xmm3
- aesdec %xmm8, %xmm4
- aesdec %xmm8, %xmm5
- aesdec %xmm8, %xmm6
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm9, %xmm5
- aesdec %xmm9, %xmm6
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- aesdec %xmm10, %xmm5
- aesdec %xmm10, %xmm6
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm11, %xmm5
- aesdec %xmm11, %xmm6
- movdqa 128(%r8), %xmm8
- movdqa 144(%r8), %xmm9
- movdqa 160(%r8), %xmm10
- cmpl $12, %r9d
- aesdec %xmm8, %xmm1
- aesdec %xmm8, %xmm2
- aesdec %xmm8, %xmm3
- aesdec %xmm8, %xmm4
- aesdec %xmm8, %xmm5
- aesdec %xmm8, %xmm6
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm9, %xmm5
- aesdec %xmm9, %xmm6
- jb DLAST_6
- movdqa 160(%r8), %xmm8
- movdqa 176(%r8), %xmm9
- movdqa 192(%r8), %xmm10
- cmpl $14, %r9d
- aesdec %xmm8, %xmm1
- aesdec %xmm8, %xmm2
- aesdec %xmm8, %xmm3
- aesdec %xmm8, %xmm4
- aesdec %xmm8, %xmm5
- aesdec %xmm8, %xmm6
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm9, %xmm5
- aesdec %xmm9, %xmm6
- jb DLAST_6
- movdqa 192(%r8), %xmm8
- movdqa 208(%r8), %xmm9
- movdqa 224(%r8), %xmm10
- aesdec %xmm8, %xmm1
- aesdec %xmm8, %xmm2
- aesdec %xmm8, %xmm3
- aesdec %xmm8, %xmm4
- aesdec %xmm8, %xmm5
- aesdec %xmm8, %xmm6
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm9, %xmm5
- aesdec %xmm9, %xmm6
-DLAST_6:
- addq $96, %rsi
- aesdeclast %xmm10, %xmm1
- aesdeclast %xmm10, %xmm2
- aesdeclast %xmm10, %xmm3
- aesdeclast %xmm10, %xmm4
- aesdeclast %xmm10, %xmm5
- aesdeclast %xmm10, %xmm6
- movdqu (%rdi), %xmm8
- movdqu 16(%rdi), %xmm9
- movdqu 32(%rdi), %xmm10
- movdqu 48(%rdi), %xmm11
- movdqu 64(%rdi), %xmm12
- movdqu 80(%rdi), %xmm13
- pxor %xmm7, %xmm1
- pxor %xmm8, %xmm2
- pxor %xmm9, %xmm3
- pxor %xmm10, %xmm4
- pxor %xmm11, %xmm5
- pxor %xmm12, %xmm6
- movdqu %xmm13, %xmm7
- movdqu %xmm1, (%rsi)
- movdqu %xmm2, 16(%rsi)
- movdqu %xmm3, 32(%rsi)
- movdqu %xmm4, 48(%rsi)
- movdqu %xmm5, 64(%rsi)
- movdqu %xmm6, 80(%rsi)
- addq $96, %rdi
- decq %rcx
- jne DLOOP_6
- addq $96, %rsi
-DREMAINDER_6:
- cmpq $0, %r10
- je DEND_6
-DLOOP_6_2:
- movdqu (%rdi), %xmm1
- movdqa %xmm1, %xmm10
- addq $16, %rdi
- pxor (%r8), %xmm1
- movdqu 160(%r8), %xmm2
- cmpl $12, %r9d
- aesdec 16(%r8), %xmm1
- aesdec 32(%r8), %xmm1
- aesdec 48(%r8), %xmm1
- aesdec 64(%r8), %xmm1
- aesdec 80(%r8), %xmm1
- aesdec 96(%r8), %xmm1
- aesdec 112(%r8), %xmm1
- aesdec 128(%r8), %xmm1
- aesdec 144(%r8), %xmm1
- jb DLAST_6_2
- movdqu 192(%r8), %xmm2
- cmpl $14, %r9d
- aesdec 160(%r8), %xmm1
- aesdec 176(%r8), %xmm1
- jb DLAST_6_2
- movdqu 224(%r8), %xmm2
- aesdec 192(%r8), %xmm1
- aesdec 208(%r8), %xmm1
-DLAST_6_2:
- aesdeclast %xmm2, %xmm1
- pxor %xmm7, %xmm1
- movdqa %xmm10, %xmm7
- movdqu %xmm1, (%rsi)
- addq $16, %rsi
- decq %r10
- jne DLOOP_6_2
-DEND_6:
- ret
-
-#else /* WOLFSSL_AESNI_BYx */
-
-/*
-AES_CBC_decrypt_by8 (const unsigned char *in,
- unsigned char *out,
- unsigned char ivec[16],
- unsigned long length,
- const unsigned char *KS,
- int nr)
-*/
-#ifndef __APPLE__
-.globl AES_CBC_decrypt_by8
-AES_CBC_decrypt_by8:
-#else
-.globl _AES_CBC_decrypt_by8
-_AES_CBC_decrypt_by8:
-#endif
-# parameter 1: %rdi - in
-# parameter 2: %rsi - out
-# parameter 3: %rdx - ivec
-# parameter 4: %rcx - length
-# parameter 5: %r8 - KS
-# parameter 6: %r9d - nr
-
- movq %rcx, %r10
- shrq $4, %rcx
- shlq $60, %r10
- je DNO_PARTS_8
- addq $1, %rcx
-DNO_PARTS_8:
- movq %rcx, %r10
- shlq $61, %r10
- shrq $61, %r10
- shrq $3, %rcx
- movdqu (%rdx), %xmm9
- je DREMAINDER_8
- subq $128, %rsi
-DLOOP_8:
- movdqu (%rdi), %xmm1
- movdqu 16(%rdi), %xmm2
- movdqu 32(%rdi), %xmm3
- movdqu 48(%rdi), %xmm4
- movdqu 64(%rdi), %xmm5
- movdqu 80(%rdi), %xmm6
- movdqu 96(%rdi), %xmm7
- movdqu 112(%rdi), %xmm8
- movdqa (%r8), %xmm10
- movdqa 16(%r8), %xmm11
- movdqa 32(%r8), %xmm12
- movdqa 48(%r8), %xmm13
- pxor %xmm10, %xmm1
- pxor %xmm10, %xmm2
- pxor %xmm10, %xmm3
- pxor %xmm10, %xmm4
- pxor %xmm10, %xmm5
- pxor %xmm10, %xmm6
- pxor %xmm10, %xmm7
- pxor %xmm10, %xmm8
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm11, %xmm5
- aesdec %xmm11, %xmm6
- aesdec %xmm11, %xmm7
- aesdec %xmm11, %xmm8
- aesdec %xmm12, %xmm1
- aesdec %xmm12, %xmm2
- aesdec %xmm12, %xmm3
- aesdec %xmm12, %xmm4
- aesdec %xmm12, %xmm5
- aesdec %xmm12, %xmm6
- aesdec %xmm12, %xmm7
- aesdec %xmm12, %xmm8
- aesdec %xmm13, %xmm1
- aesdec %xmm13, %xmm2
- aesdec %xmm13, %xmm3
- aesdec %xmm13, %xmm4
- aesdec %xmm13, %xmm5
- aesdec %xmm13, %xmm6
- aesdec %xmm13, %xmm7
- aesdec %xmm13, %xmm8
- movdqa 64(%r8), %xmm10
- movdqa 80(%r8), %xmm11
- movdqa 96(%r8), %xmm12
- movdqa 112(%r8), %xmm13
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- aesdec %xmm10, %xmm5
- aesdec %xmm10, %xmm6
- aesdec %xmm10, %xmm7
- aesdec %xmm10, %xmm8
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm11, %xmm5
- aesdec %xmm11, %xmm6
- aesdec %xmm11, %xmm7
- aesdec %xmm11, %xmm8
- aesdec %xmm12, %xmm1
- aesdec %xmm12, %xmm2
- aesdec %xmm12, %xmm3
- aesdec %xmm12, %xmm4
- aesdec %xmm12, %xmm5
- aesdec %xmm12, %xmm6
- aesdec %xmm12, %xmm7
- aesdec %xmm12, %xmm8
- aesdec %xmm13, %xmm1
- aesdec %xmm13, %xmm2
- aesdec %xmm13, %xmm3
- aesdec %xmm13, %xmm4
- aesdec %xmm13, %xmm5
- aesdec %xmm13, %xmm6
- aesdec %xmm13, %xmm7
- aesdec %xmm13, %xmm8
- movdqa 128(%r8), %xmm10
- movdqa 144(%r8), %xmm11
- movdqa 160(%r8), %xmm12
- cmpl $12, %r9d
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- aesdec %xmm10, %xmm5
- aesdec %xmm10, %xmm6
- aesdec %xmm10, %xmm7
- aesdec %xmm10, %xmm8
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm11, %xmm5
- aesdec %xmm11, %xmm6
- aesdec %xmm11, %xmm7
- aesdec %xmm11, %xmm8
- jb DLAST_8
- movdqa 160(%r8), %xmm10
- movdqa 176(%r8), %xmm11
- movdqa 192(%r8), %xmm12
- cmpl $14, %r9d
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- aesdec %xmm10, %xmm5
- aesdec %xmm10, %xmm6
- aesdec %xmm10, %xmm7
- aesdec %xmm10, %xmm8
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm11, %xmm5
- aesdec %xmm11, %xmm6
- aesdec %xmm11, %xmm7
- aesdec %xmm11, %xmm8
- jb DLAST_8
- movdqa 192(%r8), %xmm10
- movdqa 208(%r8), %xmm11
- movdqa 224(%r8), %xmm12
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- aesdec %xmm10, %xmm5
- aesdec %xmm10, %xmm6
- aesdec %xmm10, %xmm7
- aesdec %xmm10, %xmm8
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm11, %xmm5
- aesdec %xmm11, %xmm6
- aesdec %xmm11, %xmm7
- aesdec %xmm11, %xmm8
-DLAST_8:
- addq $128, %rsi
- aesdeclast %xmm12, %xmm1
- aesdeclast %xmm12, %xmm2
- aesdeclast %xmm12, %xmm3
- aesdeclast %xmm12, %xmm4
- aesdeclast %xmm12, %xmm5
- aesdeclast %xmm12, %xmm6
- aesdeclast %xmm12, %xmm7
- aesdeclast %xmm12, %xmm8
- movdqu (%rdi), %xmm10
- movdqu 16(%rdi), %xmm11
- movdqu 32(%rdi), %xmm12
- movdqu 48(%rdi), %xmm13
- pxor %xmm9, %xmm1
- pxor %xmm10, %xmm2
- pxor %xmm11, %xmm3
- pxor %xmm12, %xmm4
- pxor %xmm13, %xmm5
- movdqu 64(%rdi), %xmm10
- movdqu 80(%rdi), %xmm11
- movdqu 96(%rdi), %xmm12
- movdqu 112(%rdi), %xmm9
- pxor %xmm10, %xmm6
- pxor %xmm11, %xmm7
- pxor %xmm12, %xmm8
- movdqu %xmm1, (%rsi)
- movdqu %xmm2, 16(%rsi)
- movdqu %xmm3, 32(%rsi)
- movdqu %xmm4, 48(%rsi)
- movdqu %xmm5, 64(%rsi)
- movdqu %xmm6, 80(%rsi)
- movdqu %xmm7, 96(%rsi)
- movdqu %xmm8, 112(%rsi)
- addq $128, %rdi
- decq %rcx
- jne DLOOP_8
- addq $128, %rsi
-DREMAINDER_8:
- cmpq $0, %r10
- je DEND_8
-DLOOP_8_2:
- movdqu (%rdi), %xmm1
- movdqa %xmm1, %xmm10
- addq $16, %rdi
- pxor (%r8), %xmm1
- movdqu 160(%r8), %xmm2
- cmpl $12, %r9d
- aesdec 16(%r8), %xmm1
- aesdec 32(%r8), %xmm1
- aesdec 48(%r8), %xmm1
- aesdec 64(%r8), %xmm1
- aesdec 80(%r8), %xmm1
- aesdec 96(%r8), %xmm1
- aesdec 112(%r8), %xmm1
- aesdec 128(%r8), %xmm1
- aesdec 144(%r8), %xmm1
- jb DLAST_8_2
- movdqu 192(%r8), %xmm2
- cmpl $14, %r9d
- aesdec 160(%r8), %xmm1
- aesdec 176(%r8), %xmm1
- jb DLAST_8_2
- movdqu 224(%r8), %xmm2
- aesdec 192(%r8), %xmm1
- aesdec 208(%r8), %xmm1
-DLAST_8_2:
- aesdeclast %xmm2, %xmm1
- pxor %xmm9, %xmm1
- movdqa %xmm10, %xmm9
- movdqu %xmm1, (%rsi)
- addq $16, %rsi
- decq %r10
- jne DLOOP_8_2
-DEND_8:
- ret
-
-#endif /* WOLFSSL_AESNI_BYx */
-
-
-/*
-AES_ECB_encrypt (const unsigned char *in,
- unsigned char *out,
- unsigned long length,
- const unsigned char *KS,
- int nr)
-*/
-#ifndef __APPLE__
-.globl AES_ECB_encrypt
-AES_ECB_encrypt:
-#else
-.globl _AES_ECB_encrypt
-_AES_ECB_encrypt:
-#endif
-# parameter 1: %rdi
-# parameter 2: %rsi
-# parameter 3: %rdx
-# parameter 4: %rcx
-# parameter 5: %r8d
- movq %rdx, %r10
- shrq $4, %rdx
- shlq $60, %r10
- je EECB_NO_PARTS_4
- addq $1, %rdx
-EECB_NO_PARTS_4:
- movq %rdx, %r10
- shlq $62, %r10
- shrq $62, %r10
- shrq $2, %rdx
- je EECB_REMAINDER_4
- subq $64, %rsi
-EECB_LOOP_4:
- movdqu (%rdi), %xmm1
- movdqu 16(%rdi), %xmm2
- movdqu 32(%rdi), %xmm3
- movdqu 48(%rdi), %xmm4
- movdqa (%rcx), %xmm9
- movdqa 16(%rcx), %xmm10
- movdqa 32(%rcx), %xmm11
- movdqa 48(%rcx), %xmm12
- pxor %xmm9, %xmm1
- pxor %xmm9, %xmm2
- pxor %xmm9, %xmm3
- pxor %xmm9, %xmm4
- aesenc %xmm10, %xmm1
- aesenc %xmm10, %xmm2
- aesenc %xmm10, %xmm3
- aesenc %xmm10, %xmm4
- aesenc %xmm11, %xmm1
- aesenc %xmm11, %xmm2
- aesenc %xmm11, %xmm3
- aesenc %xmm11, %xmm4
- aesenc %xmm12, %xmm1
- aesenc %xmm12, %xmm2
- aesenc %xmm12, %xmm3
- aesenc %xmm12, %xmm4
- movdqa 64(%rcx), %xmm9
- movdqa 80(%rcx), %xmm10
- movdqa 96(%rcx), %xmm11
- movdqa 112(%rcx), %xmm12
- aesenc %xmm9, %xmm1
- aesenc %xmm9, %xmm2
- aesenc %xmm9, %xmm3
- aesenc %xmm9, %xmm4
- aesenc %xmm10, %xmm1
- aesenc %xmm10, %xmm2
- aesenc %xmm10, %xmm3
- aesenc %xmm10, %xmm4
- aesenc %xmm11, %xmm1
- aesenc %xmm11, %xmm2
- aesenc %xmm11, %xmm3
- aesenc %xmm11, %xmm4
- aesenc %xmm12, %xmm1
- aesenc %xmm12, %xmm2
- aesenc %xmm12, %xmm3
- aesenc %xmm12, %xmm4
- movdqa 128(%rcx), %xmm9
- movdqa 144(%rcx), %xmm10
- movdqa 160(%rcx), %xmm11
- cmpl $12, %r8d
- aesenc %xmm9, %xmm1
- aesenc %xmm9, %xmm2
- aesenc %xmm9, %xmm3
- aesenc %xmm9, %xmm4
- aesenc %xmm10, %xmm1
- aesenc %xmm10, %xmm2
- aesenc %xmm10, %xmm3
- aesenc %xmm10, %xmm4
- jb EECB_LAST_4
- movdqa 160(%rcx), %xmm9
- movdqa 176(%rcx), %xmm10
- movdqa 192(%rcx), %xmm11
- cmpl $14, %r8d
- aesenc %xmm9, %xmm1
- aesenc %xmm9, %xmm2
- aesenc %xmm9, %xmm3
- aesenc %xmm9, %xmm4
- aesenc %xmm10, %xmm1
- aesenc %xmm10, %xmm2
- aesenc %xmm10, %xmm3
- aesenc %xmm10, %xmm4
- jb EECB_LAST_4
- movdqa 192(%rcx), %xmm9
- movdqa 208(%rcx), %xmm10
- movdqa 224(%rcx), %xmm11
- aesenc %xmm9, %xmm1
- aesenc %xmm9, %xmm2
- aesenc %xmm9, %xmm3
- aesenc %xmm9, %xmm4
- aesenc %xmm10, %xmm1
- aesenc %xmm10, %xmm2
- aesenc %xmm10, %xmm3
- aesenc %xmm10, %xmm4
-EECB_LAST_4:
- addq $64, %rdi
- addq $64, %rsi
- decq %rdx
- aesenclast %xmm11, %xmm1
- aesenclast %xmm11, %xmm2
- aesenclast %xmm11, %xmm3
- aesenclast %xmm11, %xmm4
- movdqu %xmm1, (%rsi)
- movdqu %xmm2, 16(%rsi)
- movdqu %xmm3, 32(%rsi)
- movdqu %xmm4, 48(%rsi)
- jne EECB_LOOP_4
- addq $64, %rsi
-EECB_REMAINDER_4:
- cmpq $0, %r10
- je EECB_END_4
-EECB_LOOP_4_2:
- movdqu (%rdi), %xmm1
- addq $16, %rdi
- pxor (%rcx), %xmm1
- movdqu 160(%rcx), %xmm2
- aesenc 16(%rcx), %xmm1
- aesenc 32(%rcx), %xmm1
- aesenc 48(%rcx), %xmm1
- aesenc 64(%rcx), %xmm1
- aesenc 80(%rcx), %xmm1
- aesenc 96(%rcx), %xmm1
- aesenc 112(%rcx), %xmm1
- aesenc 128(%rcx), %xmm1
- aesenc 144(%rcx), %xmm1
- cmpl $12, %r8d
- jb EECB_LAST_4_2
- movdqu 192(%rcx), %xmm2
- aesenc 160(%rcx), %xmm1
- aesenc 176(%rcx), %xmm1
- cmpl $14, %r8d
- jb EECB_LAST_4_2
- movdqu 224(%rcx), %xmm2
- aesenc 192(%rcx), %xmm1
- aesenc 208(%rcx), %xmm1
-EECB_LAST_4_2:
- aesenclast %xmm2, %xmm1
- movdqu %xmm1, (%rsi)
- addq $16, %rsi
- decq %r10
- jne EECB_LOOP_4_2
-EECB_END_4:
- ret
-
-
-/*
-AES_ECB_decrypt (const unsigned char *in,
- unsigned char *out,
- unsigned long length,
- const unsigned char *KS,
- int nr)
-*/
-#ifndef __APPLE__
-.globl AES_ECB_decrypt
-AES_ECB_decrypt:
-#else
-.globl _AES_ECB_decrypt
-_AES_ECB_decrypt:
-#endif
-# parameter 1: %rdi
-# parameter 2: %rsi
-# parameter 3: %rdx
-# parameter 4: %rcx
-# parameter 5: %r8d
-
- movq %rdx, %r10
- shrq $4, %rdx
- shlq $60, %r10
- je DECB_NO_PARTS_4
- addq $1, %rdx
-DECB_NO_PARTS_4:
- movq %rdx, %r10
- shlq $62, %r10
- shrq $62, %r10
- shrq $2, %rdx
- je DECB_REMAINDER_4
- subq $64, %rsi
-DECB_LOOP_4:
- movdqu (%rdi), %xmm1
- movdqu 16(%rdi), %xmm2
- movdqu 32(%rdi), %xmm3
- movdqu 48(%rdi), %xmm4
- movdqa (%rcx), %xmm9
- movdqa 16(%rcx), %xmm10
- movdqa 32(%rcx), %xmm11
- movdqa 48(%rcx), %xmm12
- pxor %xmm9, %xmm1
- pxor %xmm9, %xmm2
- pxor %xmm9, %xmm3
- pxor %xmm9, %xmm4
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm12, %xmm1
- aesdec %xmm12, %xmm2
- aesdec %xmm12, %xmm3
- aesdec %xmm12, %xmm4
- movdqa 64(%rcx), %xmm9
- movdqa 80(%rcx), %xmm10
- movdqa 96(%rcx), %xmm11
- movdqa 112(%rcx), %xmm12
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- aesdec %xmm11, %xmm1
- aesdec %xmm11, %xmm2
- aesdec %xmm11, %xmm3
- aesdec %xmm11, %xmm4
- aesdec %xmm12, %xmm1
- aesdec %xmm12, %xmm2
- aesdec %xmm12, %xmm3
- aesdec %xmm12, %xmm4
- movdqa 128(%rcx), %xmm9
- movdqa 144(%rcx), %xmm10
- movdqa 160(%rcx), %xmm11
- cmpl $12, %r8d
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- jb DECB_LAST_4
- movdqa 160(%rcx), %xmm9
- movdqa 176(%rcx), %xmm10
- movdqa 192(%rcx), %xmm11
- cmpl $14, %r8d
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
- jb DECB_LAST_4
- movdqa 192(%rcx), %xmm9
- movdqa 208(%rcx), %xmm10
- movdqa 224(%rcx), %xmm11
- aesdec %xmm9, %xmm1
- aesdec %xmm9, %xmm2
- aesdec %xmm9, %xmm3
- aesdec %xmm9, %xmm4
- aesdec %xmm10, %xmm1
- aesdec %xmm10, %xmm2
- aesdec %xmm10, %xmm3
- aesdec %xmm10, %xmm4
-DECB_LAST_4:
- addq $64, %rdi
- addq $64, %rsi
- decq %rdx
- aesdeclast %xmm11, %xmm1
- aesdeclast %xmm11, %xmm2
- aesdeclast %xmm11, %xmm3
- aesdeclast %xmm11, %xmm4
- movdqu %xmm1, (%rsi)
- movdqu %xmm2, 16(%rsi)
- movdqu %xmm3, 32(%rsi)
- movdqu %xmm4, 48(%rsi)
- jne DECB_LOOP_4
- addq $64, %rsi
-DECB_REMAINDER_4:
- cmpq $0, %r10
- je DECB_END_4
-DECB_LOOP_4_2:
- movdqu (%rdi), %xmm1
- addq $16, %rdi
- pxor (%rcx), %xmm1
- movdqu 160(%rcx), %xmm2
- cmpl $12, %r8d
- aesdec 16(%rcx), %xmm1
- aesdec 32(%rcx), %xmm1
- aesdec 48(%rcx), %xmm1
- aesdec 64(%rcx), %xmm1
- aesdec 80(%rcx), %xmm1
- aesdec 96(%rcx), %xmm1
- aesdec 112(%rcx), %xmm1
- aesdec 128(%rcx), %xmm1
- aesdec 144(%rcx), %xmm1
- jb DECB_LAST_4_2
- cmpl $14, %r8d
- movdqu 192(%rcx), %xmm2
- aesdec 160(%rcx), %xmm1
- aesdec 176(%rcx), %xmm1
- jb DECB_LAST_4_2
- movdqu 224(%rcx), %xmm2
- aesdec 192(%rcx), %xmm1
- aesdec 208(%rcx), %xmm1
-DECB_LAST_4_2:
- aesdeclast %xmm2, %xmm1
- movdqu %xmm1, (%rsi)
- addq $16, %rsi
- decq %r10
- jne DECB_LOOP_4_2
-DECB_END_4:
- ret
-
-
-
-
-/*
-void AES_128_Key_Expansion(const unsigned char* userkey,
- unsigned char* key_schedule);
-*/
-.align 16,0x90
-#ifndef __APPLE__
-.globl AES_128_Key_Expansion
-AES_128_Key_Expansion:
-#else
-.globl _AES_128_Key_Expansion
-_AES_128_Key_Expansion:
-#endif
-# parameter 1: %rdi
-# parameter 2: %rsi
-movl $10, 240(%rsi)
-
-movdqu (%rdi), %xmm1
-movdqa %xmm1, (%rsi)
-
-
-ASSISTS:
-aeskeygenassist $1, %xmm1, %xmm2
-call PREPARE_ROUNDKEY_128
-movdqa %xmm1, 16(%rsi)
-aeskeygenassist $2, %xmm1, %xmm2
-call PREPARE_ROUNDKEY_128
-movdqa %xmm1, 32(%rsi)
-aeskeygenassist $4, %xmm1, %xmm2
-call PREPARE_ROUNDKEY_128
-movdqa %xmm1, 48(%rsi)
-aeskeygenassist $8, %xmm1, %xmm2
-call PREPARE_ROUNDKEY_128
-movdqa %xmm1, 64(%rsi)
-aeskeygenassist $16, %xmm1, %xmm2
-call PREPARE_ROUNDKEY_128
-movdqa %xmm1, 80(%rsi)
-aeskeygenassist $32, %xmm1, %xmm2
-call PREPARE_ROUNDKEY_128
-movdqa %xmm1, 96(%rsi)
-aeskeygenassist $64, %xmm1, %xmm2
-call PREPARE_ROUNDKEY_128
-movdqa %xmm1, 112(%rsi)
-aeskeygenassist $0x80, %xmm1, %xmm2
-call PREPARE_ROUNDKEY_128
-movdqa %xmm1, 128(%rsi)
-aeskeygenassist $0x1b, %xmm1, %xmm2
-call PREPARE_ROUNDKEY_128
-movdqa %xmm1, 144(%rsi)
-aeskeygenassist $0x36, %xmm1, %xmm2
-call PREPARE_ROUNDKEY_128
-movdqa %xmm1, 160(%rsi)
-ret
-
-PREPARE_ROUNDKEY_128:
-pshufd $255, %xmm2, %xmm2
-movdqa %xmm1, %xmm3
-pslldq $4, %xmm3
-pxor %xmm3, %xmm1
-pslldq $4, %xmm3
-pxor %xmm3, %xmm1
-pslldq $4, %xmm3
-pxor %xmm3, %xmm1
-pxor %xmm2, %xmm1
-ret
-
-
-/*
-void AES_192_Key_Expansion (const unsigned char *userkey,
- unsigned char *key)
-*/
-#ifndef __APPLE__
-.globl AES_192_Key_Expansion
-AES_192_Key_Expansion:
-#else
-.globl _AES_192_Key_Expansion
-_AES_192_Key_Expansion:
-#endif
-# parameter 1: %rdi
-# parameter 2: %rsi
-
-movdqu (%rdi), %xmm1
-movq 16(%rdi), %xmm3
-movdqa %xmm1, (%rsi)
-movdqa %xmm3, %xmm5
-
-aeskeygenassist $0x1, %xmm3, %xmm2
-call PREPARE_ROUNDKEY_192
-shufpd $0, %xmm1, %xmm5
-movdqa %xmm5, 16(%rsi)
-movdqa %xmm1, %xmm6
-shufpd $1, %xmm3, %xmm6
-movdqa %xmm6, 32(%rsi)
-
-aeskeygenassist $0x2, %xmm3, %xmm2
-call PREPARE_ROUNDKEY_192
-movdqa %xmm1, 48(%rsi)
-movdqa %xmm3, %xmm5
-
-aeskeygenassist $0x4, %xmm3, %xmm2
-call PREPARE_ROUNDKEY_192
-shufpd $0, %xmm1, %xmm5
-movdqa %xmm5, 64(%rsi)
-movdqa %xmm1, %xmm6
-shufpd $1, %xmm3, %xmm6
-movdqa %xmm6, 80(%rsi)
-
-aeskeygenassist $0x8, %xmm3, %xmm2
-call PREPARE_ROUNDKEY_192
-movdqa %xmm1, 96(%rsi)
-movdqa %xmm3, %xmm5
-
-aeskeygenassist $0x10, %xmm3, %xmm2
-call PREPARE_ROUNDKEY_192
-shufpd $0, %xmm1, %xmm5
-movdqa %xmm5, 112(%rsi)
-movdqa %xmm1, %xmm6
-shufpd $1, %xmm3, %xmm6
-movdqa %xmm6, 128(%rsi)
-
-aeskeygenassist $0x20, %xmm3, %xmm2
-call PREPARE_ROUNDKEY_192
-movdqa %xmm1, 144(%rsi)
-movdqa %xmm3, %xmm5
-
-aeskeygenassist $0x40, %xmm3, %xmm2
-call PREPARE_ROUNDKEY_192
-shufpd $0, %xmm1, %xmm5
-movdqa %xmm5, 160(%rsi)
-movdqa %xmm1, %xmm6
-shufpd $1, %xmm3, %xmm6
-movdqa %xmm6, 176(%rsi)
-
-aeskeygenassist $0x80, %xmm3, %xmm2
-call PREPARE_ROUNDKEY_192
-movdqa %xmm1, 192(%rsi)
-movdqa %xmm3, 208(%rsi)
-ret
-
-PREPARE_ROUNDKEY_192:
-pshufd $0x55, %xmm2, %xmm2
-movdqu %xmm1, %xmm4
-pslldq $4, %xmm4
-pxor %xmm4, %xmm1
-
-pslldq $4, %xmm4
-pxor %xmm4, %xmm1
-pslldq $4, %xmm4
-pxor %xmm4, %xmm1
-pxor %xmm2, %xmm1
-pshufd $0xff, %xmm1, %xmm2
-movdqu %xmm3, %xmm4
-pslldq $4, %xmm4
-pxor %xmm4, %xmm3
-pxor %xmm2, %xmm3
-ret
-
-
-/*
-void AES_256_Key_Expansion (const unsigned char *userkey,
- unsigned char *key)
-*/
-#ifndef __APPLE__
-.globl AES_256_Key_Expansion
-AES_256_Key_Expansion:
-#else
-.globl _AES_256_Key_Expansion
-_AES_256_Key_Expansion:
-#endif
-# parameter 1: %rdi
-# parameter 2: %rsi
-
-movdqu (%rdi), %xmm1
-movdqu 16(%rdi), %xmm3
-movdqa %xmm1, (%rsi)
-movdqa %xmm3, 16(%rsi)
-
-aeskeygenassist $0x1, %xmm3, %xmm2
-call MAKE_RK256_a
-movdqa %xmm1, 32(%rsi)
-aeskeygenassist $0x0, %xmm1, %xmm2
-call MAKE_RK256_b
-movdqa %xmm3, 48(%rsi)
-aeskeygenassist $0x2, %xmm3, %xmm2
-call MAKE_RK256_a
-movdqa %xmm1, 64(%rsi)
-aeskeygenassist $0x0, %xmm1, %xmm2
-call MAKE_RK256_b
-movdqa %xmm3, 80(%rsi)
-aeskeygenassist $0x4, %xmm3, %xmm2
-call MAKE_RK256_a
-movdqa %xmm1, 96(%rsi)
-aeskeygenassist $0x0, %xmm1, %xmm2
-call MAKE_RK256_b
-movdqa %xmm3, 112(%rsi)
-aeskeygenassist $0x8, %xmm3, %xmm2
-call MAKE_RK256_a
-movdqa %xmm1, 128(%rsi)
-aeskeygenassist $0x0, %xmm1, %xmm2
-call MAKE_RK256_b
-movdqa %xmm3, 144(%rsi)
-aeskeygenassist $0x10, %xmm3, %xmm2
-call MAKE_RK256_a
-movdqa %xmm1, 160(%rsi)
-aeskeygenassist $0x0, %xmm1, %xmm2
-call MAKE_RK256_b
-movdqa %xmm3, 176(%rsi)
-aeskeygenassist $0x20, %xmm3, %xmm2
-call MAKE_RK256_a
-movdqa %xmm1, 192(%rsi)
-
-aeskeygenassist $0x0, %xmm1, %xmm2
-call MAKE_RK256_b
-movdqa %xmm3, 208(%rsi)
-aeskeygenassist $0x40, %xmm3, %xmm2
-call MAKE_RK256_a
-movdqa %xmm1, 224(%rsi)
-
-ret
-
-MAKE_RK256_a:
-pshufd $0xff, %xmm2, %xmm2
-movdqa %xmm1, %xmm4
-pslldq $4, %xmm4
-pxor %xmm4, %xmm1
-pslldq $4, %xmm4
-pxor %xmm4, %xmm1
-pslldq $4, %xmm4
-pxor %xmm4, %xmm1
-pxor %xmm2, %xmm1
-ret
-
-MAKE_RK256_b:
-pshufd $0xaa, %xmm2, %xmm2
-movdqa %xmm3, %xmm4
-pslldq $4, %xmm4
-pxor %xmm4, %xmm3
-pslldq $4, %xmm4
-pxor %xmm4, %xmm3
-pslldq $4, %xmm4
-pxor %xmm4, %xmm3
-pxor %xmm2, %xmm3
-ret
-
-#if defined(__linux__) && defined(__ELF__)
- .section .note.GNU-stack,"",%progbits
-#endif
diff --git a/client/wolfssl/wolfcrypt/src/aes_asm.asm b/client/wolfssl/wolfcrypt/src/aes_asm.asm
deleted file mode 100644
index ab3c22a..0000000
--- a/client/wolfssl/wolfcrypt/src/aes_asm.asm
+++ /dev/null
@@ -1,1529 +0,0 @@
-; /* aes_asm.asm
-; *
-; * Copyright (C) 2006-2020 wolfSSL Inc.
-; *
-; * This file is part of wolfSSL.
-; *
-; * wolfSSL is free software; you can redistribute it and/or modify
-; * it under the terms of the GNU General Public License as published by
-; * the Free Software Foundation; either version 2 of the License, or
-; * (at your option) any later version.
-; *
-; * wolfSSL is distributed in the hope that it will be useful,
-; * but WITHOUT ANY WARRANTY; without even the implied warranty of
-; * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-; * GNU General Public License for more details.
-; *
-; * You should have received a copy of the GNU General Public License
-; * along with this program; if not, write to the Free Software
-; * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
-; */
-
-;
-;
-; /* See Intel Advanced Encryption Standard (AES) Instructions Set White Paper
-; * by Israel, Intel Mobility Group Development Center, Israel Shay Gueron
-; */
-;
-; /* This file is in intel asm syntax, see .s for at&t syntax */
-;
-
-
-fips_version = 0
-IFDEF HAVE_FIPS
- fips_version = 1
- IFDEF HAVE_FIPS_VERSION
- fips_version = HAVE_FIPS_VERSION
- ENDIF
-ENDIF
-
-IF fips_version GE 2
- fipsAh SEGMENT ALIAS(".fipsA$h") 'CODE'
-ELSE
- _text SEGMENT
-ENDIF
-
-
-; /*
-; AES_CBC_encrypt[const ,unsigned char*in
-; unsigned ,char*out
-; unsigned ,char ivec+16
-; unsigned ,long length
-; const ,unsigned char*KS
-; int nr]
-; */
-AES_CBC_encrypt PROC
-;# parameter 1: rdi
-;# parameter 2: rsi
-;# parameter 3: rdx
-;# parameter 4: rcx
-;# parameter 5: r8
-;# parameter 6: r9d
-
-; save rdi and rsi to rax and r11, restore before ret
- mov rax,rdi
- mov r11,rsi
-
-; convert to what we had for att&t convention
- mov rdi,rcx
- mov rsi,rdx
- mov rdx,r8
- mov rcx,r9
- mov r8,[rsp+40]
- mov r9d,[rsp+48]
-
- mov r10,rcx
- shr rcx,4
- shl r10,60
- je NO_PARTS
- add rcx,1
-NO_PARTS:
- sub rsi,16
- movdqa xmm1,[rdx]
-LOOP_1:
- pxor xmm1,[rdi]
- pxor xmm1,[r8]
- add rsi,16
- add rdi,16
- cmp r9d,12
- aesenc xmm1,16[r8]
- aesenc xmm1,32[r8]
- aesenc xmm1,48[r8]
- aesenc xmm1,64[r8]
- aesenc xmm1,80[r8]
- aesenc xmm1,96[r8]
- aesenc xmm1,112[r8]
- aesenc xmm1,128[r8]
- aesenc xmm1,144[r8]
- movdqa xmm2,160[r8]
- jb LAST
- cmp r9d,14
-
- aesenc xmm1,160[r8]
- aesenc xmm1,176[r8]
- movdqa xmm2,192[r8]
- jb LAST
- aesenc xmm1,192[r8]
- aesenc xmm1,208[r8]
- movdqa xmm2,224[r8]
-LAST:
- dec rcx
- aesenclast xmm1,xmm2
- movdqu [rsi],xmm1
- jne LOOP_1
- ; restore non volatile rdi,rsi
- mov rdi,rax
- mov rsi,r11
- ret
-AES_CBC_encrypt ENDP
-
-
-; void AES_CBC_decrypt_by4(const unsigned char* in,
-; unsigned char* out,
-; unsigned char ivec[16],
-; unsigned long length,
-; const unsigned char* KS,
-; int nr)
-AES_CBC_decrypt_by4 PROC
-; parameter 1: rdi
-; parameter 2: rsi
-; parameter 3: rdx
-; parameter 4: rcx
-; parameter 5: r8
-; parameter 6: r9d
-
- ; save rdi and rsi to rax and r11, restore before ret
- mov rax, rdi
- mov r11, rsi
- ; convert to what we had for att&t convention
- mov rdi, rcx
- mov rsi, rdx
- mov rdx, r8
- mov rcx,r9
- mov r8, [rsp+40]
- mov r9d, [rsp+48]
- ; on microsoft xmm6-xmm15 are non volatile,
- ; let's save on stack and restore at end
- sub rsp, 8+8*16 ; 8 = align stack , 8 xmm6-12,15 16 bytes each
- movdqa [rsp+0], xmm6
- movdqa [rsp+16], xmm7
- movdqa [rsp+32], xmm8
- movdqa [rsp+48], xmm9
- movdqa [rsp+64], xmm10
- movdqa [rsp+80], xmm11
- movdqa [rsp+96], xmm12
- movdqa [rsp+112], xmm15
- ; back to our original code, more or less
- mov r10, rcx
- shr rcx, 4
- shl r10, 60
- je DNO_PARTS_4
- add rcx, 1
-DNO_PARTS_4:
- mov r10, rcx
- shl r10, 62
- shr r10, 62
- shr rcx, 2
- movdqu xmm5, [rdx]
- je DREMAINDER_4
- sub rsi, 64
-DLOOP_4:
- movdqu xmm1, [rdi]
- movdqu xmm2, 16[rdi]
- movdqu xmm3, 32[rdi]
- movdqu xmm4, 48[rdi]
- movdqa xmm6, xmm1
- movdqa xmm7, xmm2
- movdqa xmm8, xmm3
- movdqa xmm15, xmm4
- movdqa xmm9, [r8]
- movdqa xmm10, 16[r8]
- movdqa xmm11, 32[r8]
- movdqa xmm12, 48[r8]
- pxor xmm1, xmm9
- pxor xmm2, xmm9
- pxor xmm3, xmm9
- pxor xmm4, xmm9
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
- aesdec xmm1, xmm11
- aesdec xmm2, xmm11
- aesdec xmm3, xmm11
- aesdec xmm4, xmm11
- aesdec xmm1, xmm12
- aesdec xmm2, xmm12
- aesdec xmm3, xmm12
- aesdec xmm4, xmm12
- movdqa xmm9, 64[r8]
- movdqa xmm10, 80[r8]
- movdqa xmm11, 96[r8]
- movdqa xmm12, 112[r8]
- aesdec xmm1, xmm9
- aesdec xmm2, xmm9
- aesdec xmm3, xmm9
- aesdec xmm4, xmm9
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
- aesdec xmm1, xmm11
- aesdec xmm2, xmm11
- aesdec xmm3, xmm11
- aesdec xmm4, xmm11
- aesdec xmm1, xmm12
- aesdec xmm2, xmm12
- aesdec xmm3, xmm12
- aesdec xmm4, xmm12
- movdqa xmm9, 128[r8]
- movdqa xmm10, 144[r8]
- movdqa xmm11, 160[r8]
- cmp r9d, 12
- aesdec xmm1, xmm9
- aesdec xmm2, xmm9
- aesdec xmm3, xmm9
- aesdec xmm4, xmm9
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
- jb DLAST_4
- movdqa xmm9, 160[r8]
- movdqa xmm10, 176[r8]
- movdqa xmm11, 192[r8]
- cmp r9d, 14
- aesdec xmm1, xmm9
- aesdec xmm2, xmm9
- aesdec xmm3, xmm9
- aesdec xmm4, xmm9
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
- jb DLAST_4
- movdqa xmm9, 192[r8]
- movdqa xmm10, 208[r8]
- movdqa xmm11, 224[r8]
- aesdec xmm1, xmm9
- aesdec xmm2, xmm9
- aesdec xmm3, xmm9
- aesdec xmm4, xmm9
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
-DLAST_4:
- add rdi, 64
- add rsi, 64
- dec rcx
- aesdeclast xmm1, xmm11
- aesdeclast xmm2, xmm11
- aesdeclast xmm3, xmm11
- aesdeclast xmm4, xmm11
- pxor xmm1, xmm5
- pxor xmm2, xmm6
- pxor xmm3, xmm7
- pxor xmm4, xmm8
- movdqu [rsi], xmm1
- movdqu 16[rsi], xmm2
- movdqu 32[rsi], xmm3
- movdqu 48[rsi], xmm4
- movdqa xmm5, xmm15
- jne DLOOP_4
- add rsi, 64
-DREMAINDER_4:
- cmp r10, 0
- je DEND_4
-DLOOP_4_2:
- movdqu xmm1, [rdi]
- movdqa xmm15, xmm1
- add rdi, 16
- pxor xmm1, [r8]
- movdqu xmm2, 160[r8]
- cmp r9d, 12
- aesdec xmm1, 16[r8]
- aesdec xmm1, 32[r8]
- aesdec xmm1, 48[r8]
- aesdec xmm1, 64[r8]
- aesdec xmm1, 80[r8]
- aesdec xmm1, 96[r8]
- aesdec xmm1, 112[r8]
- aesdec xmm1, 128[r8]
- aesdec xmm1, 144[r8]
- jb DLAST_4_2
- movdqu xmm2, 192[r8]
- cmp r9d, 14
- aesdec xmm1, 160[r8]
- aesdec xmm1, 176[r8]
- jb DLAST_4_2
- movdqu xmm2, 224[r8]
- aesdec xmm1, 192[r8]
- aesdec xmm1, 208[r8]
-DLAST_4_2:
- aesdeclast xmm1, xmm2
- pxor xmm1, xmm5
- movdqa xmm5, xmm15
- movdqu [rsi], xmm1
- add rsi, 16
- dec r10
- jne DLOOP_4_2
-DEND_4:
- ; restore non volatile rdi,rsi
- mov rdi, rax
- mov rsi, r11
- ; restore non volatile xmms from stack
- movdqa xmm6, [rsp+0]
- movdqa xmm7, [rsp+16]
- movdqa xmm8, [rsp+32]
- movdqa xmm9, [rsp+48]
- movdqa xmm10, [rsp+64]
- movdqa xmm11, [rsp+80]
- movdqa xmm12, [rsp+96]
- movdqa xmm15, [rsp+112]
- add rsp, 8+8*16 ; 8 = align stack , 8 xmm6-12,15 16 bytes each
- ret
-AES_CBC_decrypt_by4 ENDP
-
-
-; void AES_CBC_decrypt_by6(const unsigned char *in,
-; unsigned char *out,
-; unsigned char ivec[16],
-; unsigned long length,
-; const unsigned char *KS,
-; int nr)
-AES_CBC_decrypt_by6 PROC
-; parameter 1: rdi - in
-; parameter 2: rsi - out
-; parameter 3: rdx - ivec
-; parameter 4: rcx - length
-; parameter 5: r8 - KS
-; parameter 6: r9d - nr
-
- ; save rdi and rsi to rax and r11, restore before ret
- mov rax, rdi
- mov r11, rsi
- ; convert to what we had for att&t convention
- mov rdi, rcx
- mov rsi, rdx
- mov rdx, r8
- mov rcx, r9
- mov r8, [rsp+40]
- mov r9d, [rsp+48]
- ; on microsoft xmm6-xmm15 are non volatile,
- ; let's save on stack and restore at end
- sub rsp, 8+9*16 ; 8 = align stack , 9 xmm6-14 16 bytes each
- movdqa [rsp+0], xmm6
- movdqa [rsp+16], xmm7
- movdqa [rsp+32], xmm8
- movdqa [rsp+48], xmm9
- movdqa [rsp+64], xmm10
- movdqa [rsp+80], xmm11
- movdqa [rsp+96], xmm12
- movdqa [rsp+112], xmm13
- movdqa [rsp+128], xmm14
- ; back to our original code, more or less
- mov r10, rcx
- shr rcx, 4
- shl r10, 60
- je DNO_PARTS_6
- add rcx, 1
-DNO_PARTS_6:
- mov r12, rax
- mov r13, rdx
- mov r14, rbx
- mov rdx, 0
- mov rax, rcx
- mov rbx, 6
- div rbx
- mov rcx, rax
- mov r10, rdx
- mov rax, r12
- mov rdx, r13
- mov rbx, r14
- cmp rcx, 0
- movdqu xmm7, [rdx]
- je DREMAINDER_6
- sub rsi, 96
-DLOOP_6:
- movdqu xmm1, [rdi]
- movdqu xmm2, 16[rdi]
- movdqu xmm3, 32[rdi]
- movdqu xmm4, 48[rdi]
- movdqu xmm5, 64[rdi]
- movdqu xmm6, 80[rdi]
- movdqa xmm8, [r8]
- movdqa xmm9, 16[r8]
- movdqa xmm10, 32[r8]
- movdqa xmm11, 48[r8]
- pxor xmm1, xmm8
- pxor xmm2, xmm8
- pxor xmm3, xmm8
- pxor xmm4, xmm8
- pxor xmm5, xmm8
- pxor xmm6, xmm8
- aesdec xmm1, xmm9
- aesdec xmm2, xmm9
- aesdec xmm3, xmm9
- aesdec xmm4, xmm9
- aesdec xmm5, xmm9
- aesdec xmm6, xmm9
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
- aesdec xmm5, xmm10
- aesdec xmm6, xmm10
- aesdec xmm1, xmm11
- aesdec xmm2, xmm11
- aesdec xmm3, xmm11
- aesdec xmm4, xmm11
- aesdec xmm5, xmm11
- aesdec xmm6, xmm11
- movdqa xmm8, 64[r8]
- movdqa xmm9, 80[r8]
- movdqa xmm10, 96[r8]
- movdqa xmm11, 112[r8]
- aesdec xmm1, xmm8
- aesdec xmm2, xmm8
- aesdec xmm3, xmm8
- aesdec xmm4, xmm8
- aesdec xmm5, xmm8
- aesdec xmm6, xmm8
- aesdec xmm1, xmm9
- aesdec xmm2, xmm9
- aesdec xmm3, xmm9
- aesdec xmm4, xmm9
- aesdec xmm5, xmm9
- aesdec xmm6, xmm9
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
- aesdec xmm5, xmm10
- aesdec xmm6, xmm10
- aesdec xmm1, xmm11
- aesdec xmm2, xmm11
- aesdec xmm3, xmm11
- aesdec xmm4, xmm11
- aesdec xmm5, xmm11
- aesdec xmm6, xmm11
- movdqa xmm8, 128[r8]
- movdqa xmm9, 144[r8]
- movdqa xmm10, 160[r8]
- cmp r9d, 12
- aesdec xmm1, xmm8
- aesdec xmm2, xmm8
- aesdec xmm3, xmm8
- aesdec xmm4, xmm8
- aesdec xmm5, xmm8
- aesdec xmm6, xmm8
- aesdec xmm1, xmm9
- aesdec xmm2, xmm9
- aesdec xmm3, xmm9
- aesdec xmm4, xmm9
- aesdec xmm5, xmm9
- aesdec xmm6, xmm9
- jb DLAST_6
- movdqa xmm8, 160[r8]
- movdqa xmm9, 176[r8]
- movdqa xmm10, 192[r8]
- cmp r9d, 14
- aesdec xmm1, xmm8
- aesdec xmm2, xmm8
- aesdec xmm3, xmm8
- aesdec xmm4, xmm8
- aesdec xmm5, xmm8
- aesdec xmm6, xmm8
- aesdec xmm1, xmm9
- aesdec xmm2, xmm9
- aesdec xmm3, xmm9
- aesdec xmm4, xmm9
- aesdec xmm5, xmm9
- aesdec xmm6, xmm9
- jb DLAST_6
- movdqa xmm8, 192[r8]
- movdqa xmm9, 208[r8]
- movdqa xmm10, 224[r8]
- aesdec xmm1, xmm8
- aesdec xmm2, xmm8
- aesdec xmm3, xmm8
- aesdec xmm4, xmm8
- aesdec xmm5, xmm8
- aesdec xmm6, xmm8
- aesdec xmm1, xmm9
- aesdec xmm2, xmm9
- aesdec xmm3, xmm9
- aesdec xmm4, xmm9
- aesdec xmm5, xmm9
- aesdec xmm6, xmm9
-DLAST_6:
- add rsi, 96
- aesdeclast xmm1, xmm10
- aesdeclast xmm2, xmm10
- aesdeclast xmm3, xmm10
- aesdeclast xmm4, xmm10
- aesdeclast xmm5, xmm10
- aesdeclast xmm6, xmm10
- movdqu xmm8, [rdi]
- movdqu xmm9, 16[rdi]
- movdqu xmm10, 32[rdi]
- movdqu xmm11, 48[rdi]
- movdqu xmm12, 64[rdi]
- movdqu xmm13, 80[rdi]
- pxor xmm1, xmm7
- pxor xmm2, xmm8
- pxor xmm3, xmm9
- pxor xmm4, xmm10
- pxor xmm5, xmm11
- pxor xmm6, xmm12
- movdqu xmm7, xmm13
- movdqu [rsi], xmm1
- movdqu 16[rsi], xmm2
- movdqu 32[rsi], xmm3
- movdqu 48[rsi], xmm4
- movdqu 64[rsi], xmm5
- movdqu 80[rsi], xmm6
- add rdi, 96
- dec rcx
- jne DLOOP_6
- add rsi, 96
-DREMAINDER_6:
- cmp r10, 0
- je DEND_6
-DLOOP_6_2:
- movdqu xmm1, [rdi]
- movdqa xmm10, xmm1
- add rdi, 16
- pxor xmm1, [r8]
- movdqu xmm2, 160[r8]
- cmp r9d, 12
- aesdec xmm1, 16[r8]
- aesdec xmm1, 32[r8]
- aesdec xmm1, 48[r8]
- aesdec xmm1, 64[r8]
- aesdec xmm1, 80[r8]
- aesdec xmm1, 96[r8]
- aesdec xmm1, 112[r8]
- aesdec xmm1, 128[r8]
- aesdec xmm1, 144[r8]
- jb DLAST_6_2
- movdqu xmm2, 192[r8]
- cmp r9d, 14
- aesdec xmm1, 160[r8]
- aesdec xmm1, 176[r8]
- jb DLAST_6_2
- movdqu xmm2, 224[r8]
- aesdec xmm1, 192[r8]
- aesdec xmm1, 208[r8]
-DLAST_6_2:
- aesdeclast xmm1, xmm2
- pxor xmm1, xmm7
- movdqa xmm7, xmm10
- movdqu [rsi], xmm1
- add rsi, 16
- dec r10
- jne DLOOP_6_2
-DEND_6:
- ; restore non volatile rdi,rsi
- mov rdi, rax
- mov rsi, r11
- ; restore non volatile xmms from stack
- movdqa xmm6, [rsp+0]
- movdqa xmm7, [rsp+16]
- movdqa xmm8, [rsp+32]
- movdqa xmm9, [rsp+48]
- movdqa xmm10, [rsp+64]
- movdqa xmm11, [rsp+80]
- movdqa xmm12, [rsp+96]
- movdqa xmm13, [rsp+112]
- movdqa xmm14, [rsp+128]
- add rsp, 8+9*16 ; 8 = align stack , 9 xmm6-14 16 bytes each
- ret
-AES_CBC_decrypt_by6 ENDP
-
-
-; void AES_CBC_decrypt_by8(const unsigned char *in,
-; unsigned char *out,
-; unsigned char ivec[16],
-; unsigned long length,
-; const unsigned char *KS,
-; int nr)
-AES_CBC_decrypt_by8 PROC
-; parameter 1: rdi - in
-; parameter 2: rsi - out
-; parameter 3: rdx - ivec
-; parameter 4: rcx - length
-; parameter 5: r8 - KS
-; parameter 6: r9d - nr
-
- ; save rdi and rsi to rax and r11, restore before ret
- mov rax, rdi
- mov r11, rsi
- ; convert to what we had for att&t convention
- mov rdi, rcx
- mov rsi, rdx
- mov rdx, r8
- mov rcx,r9
- mov r8, [rsp+40]
- mov r9d, [rsp+48]
- ; on microsoft xmm6-xmm15 are non volatile,
- ; let's save on stack and restore at end
- sub rsp, 8+8*16 ; 8 = align stack , 8 xmm6-13 16 bytes each
- movdqa [rsp+0], xmm6
- movdqa [rsp+16], xmm7
- movdqa [rsp+32], xmm8
- movdqa [rsp+48], xmm9
- movdqa [rsp+64], xmm10
- movdqa [rsp+80], xmm11
- movdqa [rsp+96], xmm12
- movdqa [rsp+112], xmm13
- ; back to our original code, more or less
- mov r10, rcx
- shr rcx, 4
- shl r10, 60
- je DNO_PARTS_8
- add rcx, 1
-DNO_PARTS_8:
- mov r10, rcx
- shl r10, 61
- shr r10, 61
- shr rcx, 3
- movdqu xmm9, [rdx]
- je DREMAINDER_8
- sub rsi, 128
-DLOOP_8:
- movdqu xmm1, [rdi]
- movdqu xmm2, 16[rdi]
- movdqu xmm3, 32[rdi]
- movdqu xmm4, 48[rdi]
- movdqu xmm5, 64[rdi]
- movdqu xmm6, 80[rdi]
- movdqu xmm7, 96[rdi]
- movdqu xmm8, 112[rdi]
- movdqa xmm10, [r8]
- movdqa xmm11, 16[r8]
- movdqa xmm12, 32[r8]
- movdqa xmm13, 48[r8]
- pxor xmm1, xmm10
- pxor xmm2, xmm10
- pxor xmm3, xmm10
- pxor xmm4, xmm10
- pxor xmm5, xmm10
- pxor xmm6, xmm10
- pxor xmm7, xmm10
- pxor xmm8, xmm10
- aesdec xmm1, xmm11
- aesdec xmm2, xmm11
- aesdec xmm3, xmm11
- aesdec xmm4, xmm11
- aesdec xmm5, xmm11
- aesdec xmm6, xmm11
- aesdec xmm7, xmm11
- aesdec xmm8, xmm11
- aesdec xmm1, xmm12
- aesdec xmm2, xmm12
- aesdec xmm3, xmm12
- aesdec xmm4, xmm12
- aesdec xmm5, xmm12
- aesdec xmm6, xmm12
- aesdec xmm7, xmm12
- aesdec xmm8, xmm12
- aesdec xmm1, xmm13
- aesdec xmm2, xmm13
- aesdec xmm3, xmm13
- aesdec xmm4, xmm13
- aesdec xmm5, xmm13
- aesdec xmm6, xmm13
- aesdec xmm7, xmm13
- aesdec xmm8, xmm13
- movdqa xmm10, 64[r8]
- movdqa xmm11, 80[r8]
- movdqa xmm12, 96[r8]
- movdqa xmm13, 112[r8]
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
- aesdec xmm5, xmm10
- aesdec xmm6, xmm10
- aesdec xmm7, xmm10
- aesdec xmm8, xmm10
- aesdec xmm1, xmm11
- aesdec xmm2, xmm11
- aesdec xmm3, xmm11
- aesdec xmm4, xmm11
- aesdec xmm5, xmm11
- aesdec xmm6, xmm11
- aesdec xmm7, xmm11
- aesdec xmm8, xmm11
- aesdec xmm1, xmm12
- aesdec xmm2, xmm12
- aesdec xmm3, xmm12
- aesdec xmm4, xmm12
- aesdec xmm5, xmm12
- aesdec xmm6, xmm12
- aesdec xmm7, xmm12
- aesdec xmm8, xmm12
- aesdec xmm1, xmm13
- aesdec xmm2, xmm13
- aesdec xmm3, xmm13
- aesdec xmm4, xmm13
- aesdec xmm5, xmm13
- aesdec xmm6, xmm13
- aesdec xmm7, xmm13
- aesdec xmm8, xmm13
- movdqa xmm10, 128[r8]
- movdqa xmm11, 144[r8]
- movdqa xmm12, 160[r8]
- cmp r9d, 12
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
- aesdec xmm5, xmm10
- aesdec xmm6, xmm10
- aesdec xmm7, xmm10
- aesdec xmm8, xmm10
- aesdec xmm1, xmm11
- aesdec xmm2, xmm11
- aesdec xmm3, xmm11
- aesdec xmm4, xmm11
- aesdec xmm5, xmm11
- aesdec xmm6, xmm11
- aesdec xmm7, xmm11
- aesdec xmm8, xmm11
- jb DLAST_8
- movdqa xmm10, 160[r8]
- movdqa xmm11, 176[r8]
- movdqa xmm12, 192[r8]
- cmp r9d, 14
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
- aesdec xmm5, xmm10
- aesdec xmm6, xmm10
- aesdec xmm7, xmm10
- aesdec xmm8, xmm10
- aesdec xmm1, xmm11
- aesdec xmm2, xmm11
- aesdec xmm3, xmm11
- aesdec xmm4, xmm11
- aesdec xmm5, xmm11
- aesdec xmm6, xmm11
- aesdec xmm7, xmm11
- aesdec xmm8, xmm11
- jb DLAST_8
- movdqa xmm10, 192[r8]
- movdqa xmm11, 208[r8]
- movdqa xmm12, 224[r8]
- aesdec xmm1, xmm10
- aesdec xmm2, xmm10
- aesdec xmm3, xmm10
- aesdec xmm4, xmm10
- aesdec xmm5, xmm10
- aesdec xmm6, xmm10
- aesdec xmm7, xmm10
- aesdec xmm8, xmm10
- aesdec xmm1, xmm11
- aesdec xmm2, xmm11
- aesdec xmm3, xmm11
- aesdec xmm4, xmm11
- aesdec xmm5, xmm11
- aesdec xmm6, xmm11
- aesdec xmm7, xmm11
- aesdec xmm8, xmm11
-DLAST_8:
- add rsi, 128
- aesdeclast xmm1, xmm12
- aesdeclast xmm2, xmm12
- aesdeclast xmm3, xmm12
- aesdeclast xmm4, xmm12
- aesdeclast xmm5, xmm12
- aesdeclast xmm6, xmm12
- aesdeclast xmm7, xmm12
- aesdeclast xmm8, xmm12
- movdqu xmm10, [rdi]
- movdqu xmm11, 16[rdi]
- movdqu xmm12, 32[rdi]
- movdqu xmm13, 48[rdi]
- pxor xmm1, xmm9
- pxor xmm2, xmm10
- pxor xmm3, xmm11
- pxor xmm4, xmm12
- pxor xmm5, xmm13
- movdqu xmm10, 64[rdi]
- movdqu xmm11, 80[rdi]
- movdqu xmm12, 96[rdi]
- movdqu xmm9, 112[rdi]
- pxor xmm6, xmm10
- pxor xmm7, xmm11
- pxor xmm8, xmm12
- movdqu [rsi], xmm1
- movdqu 16[rsi], xmm2
- movdqu 32[rsi], xmm3
- movdqu 48[rsi], xmm4
- movdqu 64[rsi], xmm5
- movdqu 80[rsi], xmm6
- movdqu 96[rsi], xmm7
- movdqu 112[rsi], xmm8
- add rdi, 128
- dec rcx
- jne DLOOP_8
- add rsi, 128
-DREMAINDER_8:
- cmp r10, 0
- je DEND_8
-DLOOP_8_2:
- movdqu xmm1, [rdi]
- movdqa xmm10, xmm1
- add rdi, 16
- pxor xmm1, [r8]
- movdqu xmm2, 160[r8]
- cmp r9d, 12
- aesdec xmm1, 16[r8]
- aesdec xmm1, 32[r8]
- aesdec xmm1, 48[r8]
- aesdec xmm1, 64[r8]
- aesdec xmm1, 80[r8]
- aesdec xmm1, 96[r8]
- aesdec xmm1, 112[r8]
- aesdec xmm1, 128[r8]
- aesdec xmm1, 144[r8]
- jb DLAST_8_2
- movdqu xmm2, 192[r8]
- cmp r9d, 14
- aesdec xmm1, 160[r8]
- aesdec xmm1, 176[r8]
- jb DLAST_8_2
- movdqu xmm2, 224[r8]
- aesdec xmm1, 192[r8]
- aesdec xmm1, 208[r8]
-DLAST_8_2:
- aesdeclast xmm1, xmm2
- pxor xmm1, xmm9
- movdqa xmm9, xmm10
- movdqu [rsi], xmm1
- add rsi, 16
- dec r10
- jne DLOOP_8_2
-DEND_8:
- ; restore non volatile rdi,rsi
- mov rdi, rax
- mov rsi, r11
- ; restore non volatile xmms from stack
- movdqa xmm6, [rsp+0]
- movdqa xmm7, [rsp+16]
- movdqa xmm8, [rsp+32]
- movdqa xmm9, [rsp+48]
- movdqa xmm10, [rsp+64]
- movdqa xmm11, [rsp+80]
- movdqa xmm12, [rsp+96]
- movdqa xmm13, [rsp+112]
- add rsp, 8+8*16 ; 8 = align stack , 8 xmm6-13 16 bytes each
- ret
-AES_CBC_decrypt_by8 ENDP
-
-
-; /*
-; AES_ECB_encrypt[const ,unsigned char*in
-; unsigned ,char*out
-; unsigned ,long length
-; const ,unsigned char*KS
-; int nr]
-; */
-; . globl AES_ECB_encrypt
-AES_ECB_encrypt PROC
-;# parameter 1: rdi
-;# parameter 2: rsi
-;# parameter 3: rdx
-;# parameter 4: rcx
-;# parameter 5: r8d
-
-; save rdi and rsi to rax and r11, restore before ret
- mov rax,rdi
- mov r11,rsi
-
-; convert to what we had for att&t convention
- mov rdi,rcx
- mov rsi,rdx
- mov rdx,r8
- mov rcx,r9
- mov r8d,[rsp+40]
-
-; on microsoft xmm6-xmm15 are non volaitle, let's save on stack and restore at end
- sub rsp,8+4*16 ; 8 = align stack , 4 xmm9-12, 16 bytes each
- movdqa [rsp+0], xmm9
- movdqa [rsp+16], xmm10
- movdqa [rsp+32], xmm11
- movdqa [rsp+48], xmm12
-
-
- mov r10,rdx
- shr rdx,4
- shl r10,60
- je EECB_NO_PARTS_4
- add rdx,1
-EECB_NO_PARTS_4:
- mov r10,rdx
- shl r10,62
- shr r10,62
- shr rdx,2
- je EECB_REMAINDER_4
- sub rsi,64
-EECB_LOOP_4:
- movdqu xmm1,[rdi]
- movdqu xmm2,16[rdi]
- movdqu xmm3,32[rdi]
- movdqu xmm4,48[rdi]
- movdqa xmm9,[rcx]
- movdqa xmm10,16[rcx]
- movdqa xmm11,32[rcx]
- movdqa xmm12,48[rcx]
- pxor xmm1,xmm9
- pxor xmm2,xmm9
- pxor xmm3,xmm9
- pxor xmm4,xmm9
- aesenc xmm1,xmm10
- aesenc xmm2,xmm10
- aesenc xmm3,xmm10
- aesenc xmm4,xmm10
- aesenc xmm1,xmm11
- aesenc xmm2,xmm11
- aesenc xmm3,xmm11
- aesenc xmm4,xmm11
- aesenc xmm1,xmm12
- aesenc xmm2,xmm12
- aesenc xmm3,xmm12
- aesenc xmm4,xmm12
- movdqa xmm9,64[rcx]
- movdqa xmm10,80[rcx]
- movdqa xmm11,96[rcx]
- movdqa xmm12,112[rcx]
- aesenc xmm1,xmm9
- aesenc xmm2,xmm9
- aesenc xmm3,xmm9
- aesenc xmm4,xmm9
- aesenc xmm1,xmm10
- aesenc xmm2,xmm10
- aesenc xmm3,xmm10
- aesenc xmm4,xmm10
- aesenc xmm1,xmm11
- aesenc xmm2,xmm11
- aesenc xmm3,xmm11
- aesenc xmm4,xmm11
- aesenc xmm1,xmm12
- aesenc xmm2,xmm12
- aesenc xmm3,xmm12
- aesenc xmm4,xmm12
- movdqa xmm9,128[rcx]
- movdqa xmm10,144[rcx]
- movdqa xmm11,160[rcx]
- cmp r8d,12
- aesenc xmm1,xmm9
- aesenc xmm2,xmm9
- aesenc xmm3,xmm9
- aesenc xmm4,xmm9
- aesenc xmm1,xmm10
- aesenc xmm2,xmm10
- aesenc xmm3,xmm10
- aesenc xmm4,xmm10
- jb EECB_LAST_4
- movdqa xmm9,160[rcx]
- movdqa xmm10,176[rcx]
- movdqa xmm11,192[rcx]
- cmp r8d,14
- aesenc xmm1,xmm9
- aesenc xmm2,xmm9
- aesenc xmm3,xmm9
- aesenc xmm4,xmm9
- aesenc xmm1,xmm10
- aesenc xmm2,xmm10
- aesenc xmm3,xmm10
- aesenc xmm4,xmm10
- jb EECB_LAST_4
- movdqa xmm9,192[rcx]
- movdqa xmm10,208[rcx]
- movdqa xmm11,224[rcx]
- aesenc xmm1,xmm9
- aesenc xmm2,xmm9
- aesenc xmm3,xmm9
- aesenc xmm4,xmm9
- aesenc xmm1,xmm10
- aesenc xmm2,xmm10
- aesenc xmm3,xmm10
- aesenc xmm4,xmm10
-EECB_LAST_4:
- add rdi,64
- add rsi,64
- dec rdx
- aesenclast xmm1,xmm11
- aesenclast xmm2,xmm11
- aesenclast xmm3,xmm11
- aesenclast xmm4,xmm11
- movdqu [rsi],xmm1
- movdqu 16[rsi],xmm2
- movdqu 32[rsi],xmm3
- movdqu 48[rsi],xmm4
- jne EECB_LOOP_4
- add rsi,64
-EECB_REMAINDER_4:
- cmp r10,0
- je EECB_END_4
-EECB_LOOP_4_2:
- movdqu xmm1,[rdi]
- add rdi,16
- pxor xmm1,[rcx]
- movdqu xmm2,160[rcx]
- aesenc xmm1,16[rcx]
- aesenc xmm1,32[rcx]
- aesenc xmm1,48[rcx]
- aesenc xmm1,64[rcx]
- aesenc xmm1,80[rcx]
- aesenc xmm1,96[rcx]
- aesenc xmm1,112[rcx]
- aesenc xmm1,128[rcx]
- aesenc xmm1,144[rcx]
- cmp r8d,12
- jb EECB_LAST_4_2
- movdqu xmm2,192[rcx]
- aesenc xmm1,160[rcx]
- aesenc xmm1,176[rcx]
- cmp r8d,14
- jb EECB_LAST_4_2
- movdqu xmm2,224[rcx]
- aesenc xmm1,192[rcx]
- aesenc xmm1,208[rcx]
-EECB_LAST_4_2:
- aesenclast xmm1,xmm2
- movdqu [rsi],xmm1
- add rsi,16
- dec r10
- jne EECB_LOOP_4_2
-EECB_END_4:
- ; restore non volatile rdi,rsi
- mov rdi,rax
- mov rsi,r11
- ; restore non volatile xmms from stack
- movdqa xmm9, [rsp+0]
- movdqa xmm10, [rsp+16]
- movdqa xmm11, [rsp+32]
- movdqa xmm12, [rsp+48]
- add rsp,8+4*16 ; 8 = align stack , 4 xmm9-12 16 bytes each
- ret
-AES_ECB_encrypt ENDP
-
-; /*
-; AES_ECB_decrypt[const ,unsigned char*in
-; unsigned ,char*out
-; unsigned ,long length
-; const ,unsigned char*KS
-; int nr]
-; */
-; . globl AES_ECB_decrypt
-AES_ECB_decrypt PROC
-;# parameter 1: rdi
-;# parameter 2: rsi
-;# parameter 3: rdx
-;# parameter 4: rcx
-;# parameter 5: r8d
-
-; save rdi and rsi to rax and r11, restore before ret
- mov rax,rdi
- mov r11,rsi
-
-; convert to what we had for att&t convention
- mov rdi,rcx
- mov rsi,rdx
- mov rdx,r8
- mov rcx,r9
- mov r8d,[rsp+40]
-
-; on microsoft xmm6-xmm15 are non volaitle, let's save on stack and restore at end
- sub rsp,8+4*16 ; 8 = align stack , 4 xmm9-12, 16 bytes each
- movdqa [rsp+0], xmm9
- movdqa [rsp+16], xmm10
- movdqa [rsp+32], xmm11
- movdqa [rsp+48], xmm12
-
- mov r10,rdx
- shr rdx,4
- shl r10,60
- je DECB_NO_PARTS_4
- add rdx,1
-DECB_NO_PARTS_4:
- mov r10,rdx
- shl r10,62
- shr r10,62
- shr rdx,2
- je DECB_REMAINDER_4
- sub rsi,64
-DECB_LOOP_4:
- movdqu xmm1,[rdi]
- movdqu xmm2,16[rdi]
- movdqu xmm3,32[rdi]
- movdqu xmm4,48[rdi]
- movdqa xmm9,[rcx]
- movdqa xmm10,16[rcx]
- movdqa xmm11,32[rcx]
- movdqa xmm12,48[rcx]
- pxor xmm1,xmm9
- pxor xmm2,xmm9
- pxor xmm3,xmm9
- pxor xmm4,xmm9
- aesdec xmm1,xmm10
- aesdec xmm2,xmm10
- aesdec xmm3,xmm10
- aesdec xmm4,xmm10
- aesdec xmm1,xmm11
- aesdec xmm2,xmm11
- aesdec xmm3,xmm11
- aesdec xmm4,xmm11
- aesdec xmm1,xmm12
- aesdec xmm2,xmm12
- aesdec xmm3,xmm12
- aesdec xmm4,xmm12
- movdqa xmm9,64[rcx]
- movdqa xmm10,80[rcx]
- movdqa xmm11,96[rcx]
- movdqa xmm12,112[rcx]
- aesdec xmm1,xmm9
- aesdec xmm2,xmm9
- aesdec xmm3,xmm9
- aesdec xmm4,xmm9
- aesdec xmm1,xmm10
- aesdec xmm2,xmm10
- aesdec xmm3,xmm10
- aesdec xmm4,xmm10
- aesdec xmm1,xmm11
- aesdec xmm2,xmm11
- aesdec xmm3,xmm11
- aesdec xmm4,xmm11
- aesdec xmm1,xmm12
- aesdec xmm2,xmm12
- aesdec xmm3,xmm12
- aesdec xmm4,xmm12
- movdqa xmm9,128[rcx]
- movdqa xmm10,144[rcx]
- movdqa xmm11,160[rcx]
- cmp r8d,12
- aesdec xmm1,xmm9
- aesdec xmm2,xmm9
- aesdec xmm3,xmm9
- aesdec xmm4,xmm9
- aesdec xmm1,xmm10
- aesdec xmm2,xmm10
- aesdec xmm3,xmm10
- aesdec xmm4,xmm10
- jb DECB_LAST_4
- movdqa xmm9,160[rcx]
- movdqa xmm10,176[rcx]
- movdqa xmm11,192[rcx]
- cmp r8d,14
- aesdec xmm1,xmm9
- aesdec xmm2,xmm9
- aesdec xmm3,xmm9
- aesdec xmm4,xmm9
- aesdec xmm1,xmm10
- aesdec xmm2,xmm10
- aesdec xmm3,xmm10
- aesdec xmm4,xmm10
- jb DECB_LAST_4
- movdqa xmm9,192[rcx]
- movdqa xmm10,208[rcx]
- movdqa xmm11,224[rcx]
- aesdec xmm1,xmm9
- aesdec xmm2,xmm9
- aesdec xmm3,xmm9
- aesdec xmm4,xmm9
- aesdec xmm1,xmm10
- aesdec xmm2,xmm10
- aesdec xmm3,xmm10
- aesdec xmm4,xmm10
-DECB_LAST_4:
- add rdi,64
- add rsi,64
- dec rdx
- aesdeclast xmm1,xmm11
- aesdeclast xmm2,xmm11
- aesdeclast xmm3,xmm11
- aesdeclast xmm4,xmm11
- movdqu [rsi],xmm1
- movdqu 16[rsi],xmm2
- movdqu 32[rsi],xmm3
- movdqu 48[rsi],xmm4
- jne DECB_LOOP_4
- add rsi,64
-DECB_REMAINDER_4:
- cmp r10,0
- je DECB_END_4
-DECB_LOOP_4_2:
- movdqu xmm1,[rdi]
- add rdi,16
- pxor xmm1,[rcx]
- movdqu xmm2,160[rcx]
- cmp r8d,12
- aesdec xmm1,16[rcx]
- aesdec xmm1,32[rcx]
- aesdec xmm1,48[rcx]
- aesdec xmm1,64[rcx]
- aesdec xmm1,80[rcx]
- aesdec xmm1,96[rcx]
- aesdec xmm1,112[rcx]
- aesdec xmm1,128[rcx]
- aesdec xmm1,144[rcx]
- jb DECB_LAST_4_2
- cmp r8d,14
- movdqu xmm2,192[rcx]
- aesdec xmm1,160[rcx]
- aesdec xmm1,176[rcx]
- jb DECB_LAST_4_2
- movdqu xmm2,224[rcx]
- aesdec xmm1,192[rcx]
- aesdec xmm1,208[rcx]
-DECB_LAST_4_2:
- aesdeclast xmm1,xmm2
- movdqu [rsi],xmm1
- add rsi,16
- dec r10
- jne DECB_LOOP_4_2
-DECB_END_4:
- ; restore non volatile rdi,rsi
- mov rdi,rax
- mov rsi,r11
- ; restore non volatile xmms from stack
- movdqa xmm9, [rsp+0]
- movdqa xmm10, [rsp+16]
- movdqa xmm11, [rsp+32]
- movdqa xmm12, [rsp+48]
- add rsp,8+4*16 ; 8 = align stack , 4 xmm9-12 16 bytes each
- ret
-AES_ECB_decrypt ENDP
-
-
-
-; /*
-; void ,AES_128_Key_Expansion[const unsigned char*userkey
-; unsigned char*key_schedule]/
-; */
-; . align 16,0x90
-; . globl AES_128_Key_Expansion
-AES_128_Key_Expansion PROC
-;# parameter 1: rdi
-;# parameter 2: rsi
-
-; save rdi and rsi to rax and r11, restore before ret
- mov rax,rdi
- mov r11,rsi
-
-; convert to what we had for att&t convention
- mov rdi,rcx
- mov rsi,rdx
-
- mov dword ptr 240[rsi],10
-
- movdqu xmm1,[rdi]
- movdqa [rsi],xmm1
-
-
-ASSISTS:
- aeskeygenassist xmm2,xmm1,1
- call PREPARE_ROUNDKEY_128
- movdqa 16[rsi],xmm1
-
- aeskeygenassist xmm2,xmm1,2
- call PREPARE_ROUNDKEY_128
- movdqa 32[rsi],xmm1
-
- aeskeygenassist xmm2,xmm1,4
- call PREPARE_ROUNDKEY_128
- movdqa 48[rsi],xmm1
-
- aeskeygenassist xmm2,xmm1,8
- call PREPARE_ROUNDKEY_128
- movdqa 64[rsi],xmm1
-
- aeskeygenassist xmm2,xmm1,16
- call PREPARE_ROUNDKEY_128
- movdqa 80[rsi],xmm1
-
- aeskeygenassist xmm2,xmm1,32
- call PREPARE_ROUNDKEY_128
- movdqa 96[rsi],xmm1
-
- aeskeygenassist xmm2,xmm1,64
- call PREPARE_ROUNDKEY_128
- movdqa 112[rsi],xmm1
- aeskeygenassist xmm2,xmm1,80h
- call PREPARE_ROUNDKEY_128
- movdqa 128[rsi],xmm1
- aeskeygenassist xmm2,xmm1,1bh
- call PREPARE_ROUNDKEY_128
- movdqa 144[rsi],xmm1
- aeskeygenassist xmm2,xmm1,36h
- call PREPARE_ROUNDKEY_128
- movdqa 160[rsi],xmm1
- ; restore non volatile rdi,rsi
- mov rdi,rax
- mov rsi,r11
- ret
-
-PREPARE_ROUNDKEY_128:
- pshufd xmm2,xmm2,255
- movdqa xmm3,xmm1
- pslldq xmm3,4
- pxor xmm1,xmm3
- pslldq xmm3,4
- pxor xmm1,xmm3
- pslldq xmm3,4
- pxor xmm1,xmm3
- pxor xmm1,xmm2
- ret
-AES_128_Key_Expansion ENDP
-
-; /*
-; void ,AES_192_Key_Expansion[const unsigned char*userkey
-; unsigned char*key]
-; */
-; . globl AES_192_Key_Expansion
-AES_192_Key_Expansion PROC
-;# parameter 1: rdi
-;# parameter 2: rsi
-
-; save rdi and rsi to rax and r11, restore before ret
- mov rax,rdi
- mov r11,rsi
-
-; convert to what we had for att&t convention
- mov rdi,rcx
- mov rsi,rdx
-
-; on microsoft xmm6-xmm15 are non volaitle, let's save on stack and restore at end
- sub rsp,8+1*16 ; 8 = align stack , 1 xmm6, 16 bytes each
- movdqa [rsp+0], xmm6
-
- movdqu xmm1,[rdi]
- movq xmm3,qword ptr 16[rdi]
- movdqa [rsi],xmm1
- movdqa xmm5,xmm3
-
- aeskeygenassist xmm2,xmm3,1h
- call PREPARE_ROUNDKEY_192
- shufpd xmm5,xmm1,0
- movdqa 16[rsi],xmm5
- movdqa xmm6,xmm1
- shufpd xmm6,xmm3,1
- movdqa 32[rsi],xmm6
-
- aeskeygenassist xmm2,xmm3,2h
- call PREPARE_ROUNDKEY_192
- movdqa 48[rsi],xmm1
- movdqa xmm5,xmm3
-
- aeskeygenassist xmm2,xmm3,4h
- call PREPARE_ROUNDKEY_192
- shufpd xmm5,xmm1,0
- movdqa 64[rsi],xmm5
- movdqa xmm6,xmm1
- shufpd xmm6,xmm3,1
- movdqa 80[rsi],xmm6
-
- aeskeygenassist xmm2,xmm3,8h
- call PREPARE_ROUNDKEY_192
- movdqa 96[rsi],xmm1
- movdqa xmm5,xmm3
-
- aeskeygenassist xmm2,xmm3,10h
- call PREPARE_ROUNDKEY_192
- shufpd xmm5,xmm1,0
- movdqa 112[rsi],xmm5
- movdqa xmm6,xmm1
- shufpd xmm6,xmm3,1
- movdqa 128[rsi],xmm6
-
- aeskeygenassist xmm2,xmm3,20h
- call PREPARE_ROUNDKEY_192
- movdqa 144[rsi],xmm1
- movdqa xmm5,xmm3
-
- aeskeygenassist xmm2,xmm3,40h
- call PREPARE_ROUNDKEY_192
- shufpd xmm5,xmm1,0
- movdqa 160[rsi],xmm5
- movdqa xmm6,xmm1
- shufpd xmm6,xmm3,1
- movdqa 176[rsi],xmm6
-
- aeskeygenassist xmm2,xmm3,80h
- call PREPARE_ROUNDKEY_192
- movdqa 192[rsi],xmm1
- movdqa 208[rsi],xmm3
- ; restore non volatile rdi,rsi
- mov rdi,rax
- mov rsi,r11
-; restore non volatile xmms from stack
- movdqa xmm6, [rsp+0]
- add rsp,8+1*16 ; 8 = align stack , 1 xmm6 16 bytes each
- ret
-
-PREPARE_ROUNDKEY_192:
- pshufd xmm2,xmm2,55h
- movdqu xmm4,xmm1
- pslldq xmm4,4
- pxor xmm1,xmm4
-
- pslldq xmm4,4
- pxor xmm1,xmm4
- pslldq xmm4,4
- pxor xmm1,xmm4
- pxor xmm1,xmm2
- pshufd xmm2,xmm1,0ffh
- movdqu xmm4,xmm3
- pslldq xmm4,4
- pxor xmm3,xmm4
- pxor xmm3,xmm2
- ret
-AES_192_Key_Expansion ENDP
-
-; /*
-; void ,AES_256_Key_Expansion[const unsigned char*userkey
-; unsigned char*key]
-; */
-; . globl AES_256_Key_Expansion
-AES_256_Key_Expansion PROC
-;# parameter 1: rdi
-;# parameter 2: rsi
-
-; save rdi and rsi to rax and r11, restore before ret
- mov rax,rdi
- mov r11,rsi
-
-; convert to what we had for att&t convention
- mov rdi,rcx
- mov rsi,rdx
-
- movdqu xmm1,[rdi]
- movdqu xmm3,16[rdi]
- movdqa [rsi],xmm1
- movdqa 16[rsi],xmm3
-
- aeskeygenassist xmm2,xmm3,1h
- call MAKE_RK256_a
- movdqa 32[rsi],xmm1
- aeskeygenassist xmm2,xmm1,0h
- call MAKE_RK256_b
- movdqa 48[rsi],xmm3
- aeskeygenassist xmm2,xmm3,2h
- call MAKE_RK256_a
- movdqa 64[rsi],xmm1
- aeskeygenassist xmm2,xmm1,0h
- call MAKE_RK256_b
- movdqa 80[rsi],xmm3
- aeskeygenassist xmm2,xmm3,4h
- call MAKE_RK256_a
- movdqa 96[rsi],xmm1
- aeskeygenassist xmm2,xmm1,0h
- call MAKE_RK256_b
- movdqa 112[rsi],xmm3
- aeskeygenassist xmm2,xmm3,8h
- call MAKE_RK256_a
- movdqa 128[rsi],xmm1
- aeskeygenassist xmm2,xmm1,0h
- call MAKE_RK256_b
- movdqa 144[rsi],xmm3
- aeskeygenassist xmm2,xmm3,10h
- call MAKE_RK256_a
- movdqa 160[rsi],xmm1
- aeskeygenassist xmm2,xmm1,0h
- call MAKE_RK256_b
- movdqa 176[rsi],xmm3
- aeskeygenassist xmm2,xmm3,20h
- call MAKE_RK256_a
- movdqa 192[rsi],xmm1
-
- aeskeygenassist xmm2,xmm1,0h
- call MAKE_RK256_b
- movdqa 208[rsi],xmm3
- aeskeygenassist xmm2,xmm3,40h
- call MAKE_RK256_a
- movdqa 224[rsi],xmm1
-
- ; restore non volatile rdi,rsi
- mov rdi,rax
- mov rsi,r11
- ret
-AES_256_Key_Expansion ENDP
-
-MAKE_RK256_a:
- pshufd xmm2,xmm2,0ffh
- movdqa xmm4,xmm1
- pslldq xmm4,4
- pxor xmm1,xmm4
- pslldq xmm4,4
- pxor xmm1,xmm4
- pslldq xmm4,4
- pxor xmm1,xmm4
- pxor xmm1,xmm2
- ret
-
-MAKE_RK256_b:
- pshufd xmm2,xmm2,0aah
- movdqa xmm4,xmm3
- pslldq xmm4,4
- pxor xmm3,xmm4
- pslldq xmm4,4
- pxor xmm3,xmm4
- pslldq xmm4,4
- pxor xmm3,xmm4
- pxor xmm3,xmm2
- ret
-
-
-IF fips_version GE 2
- fipsAh ENDS
-ELSE
- _text ENDS
-ENDIF
-
-END
diff --git a/client/wolfssl/wolfcrypt/src/aes_gcm_asm.S b/client/wolfssl/wolfcrypt/src/aes_gcm_asm.S
deleted file mode 100644
index e878690..0000000
--- a/client/wolfssl/wolfcrypt/src/aes_gcm_asm.S
+++ /dev/null
@@ -1,8733 +0,0 @@
-/* aes_gcm_asm
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-#ifndef HAVE_INTEL_AVX1
-#define HAVE_INTEL_AVX1
-#endif /* HAVE_INTEL_AVX1 */
-#ifndef NO_AVX2_SUPPORT
-#define HAVE_INTEL_AVX2
-#endif /* NO_AVX2_SUPPORT */
-
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_one:
-.quad 0x0, 0x1
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_two:
-.quad 0x0, 0x2
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_three:
-.quad 0x0, 0x3
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_four:
-.quad 0x0, 0x4
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_five:
-.quad 0x0, 0x5
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_six:
-.quad 0x0, 0x6
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_seven:
-.quad 0x0, 0x7
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_eight:
-.quad 0x0, 0x8
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_bswap_epi64:
-.quad 0x1020304050607, 0x8090a0b0c0d0e0f
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_bswap_mask:
-.quad 0x8090a0b0c0d0e0f, 0x1020304050607
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_aes_gcm_mod2_128:
-.quad 0x1, 0xc200000000000000
-#ifndef __APPLE__
-.text
-.globl AES_GCM_encrypt
-.type AES_GCM_encrypt,@function
-.align 4
-AES_GCM_encrypt:
-#else
-.section __TEXT,__text
-.globl _AES_GCM_encrypt
-.p2align 2
-_AES_GCM_encrypt:
-#endif /* __APPLE__ */
- pushq %r13
- pushq %r12
- pushq %rbx
- pushq %r14
- pushq %r15
- movq %rdx, %r12
- movq %rcx, %rax
- movl 48(%rsp), %r11d
- movl 56(%rsp), %ebx
- movl 64(%rsp), %r14d
- movq 72(%rsp), %r15
- movl 80(%rsp), %r10d
- subq $0xa0, %rsp
- pxor %xmm4, %xmm4
- pxor %xmm6, %xmm6
- cmpl $12, %ebx
- movl %ebx, %edx
- jne L_AES_GCM_encrypt_iv_not_12
- # # Calculate values when IV is 12 bytes
- # Set counter based on IV
- movl $0x1000000, %ecx
- pinsrq $0x00, (%rax), %xmm4
- pinsrd $2, 8(%rax), %xmm4
- pinsrd $3, %ecx, %xmm4
- # H = Encrypt X(=0) and T = Encrypt counter
- movdqa %xmm4, %xmm1
- movdqa (%r15), %xmm5
- pxor %xmm5, %xmm1
- movdqa 16(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 32(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 48(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 64(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 80(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 96(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 112(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 128(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 144(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm7
- jl L_AES_GCM_encrypt_calc_iv_12_last
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 176(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm7
- jl L_AES_GCM_encrypt_calc_iv_12_last
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 208(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 224(%r15), %xmm7
-L_AES_GCM_encrypt_calc_iv_12_last:
- aesenclast %xmm7, %xmm5
- aesenclast %xmm7, %xmm1
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm5
- movdqa %xmm1, 144(%rsp)
- jmp L_AES_GCM_encrypt_iv_done
-L_AES_GCM_encrypt_iv_not_12:
- # Calculate values when IV is not 12 bytes
- # H = Encrypt X(=0)
- movdqa (%r15), %xmm5
- aesenc 16(%r15), %xmm5
- aesenc 32(%r15), %xmm5
- aesenc 48(%r15), %xmm5
- aesenc 64(%r15), %xmm5
- aesenc 80(%r15), %xmm5
- aesenc 96(%r15), %xmm5
- aesenc 112(%r15), %xmm5
- aesenc 128(%r15), %xmm5
- aesenc 144(%r15), %xmm5
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm9
- jl L_AES_GCM_encrypt_calc_iv_1_aesenc_avx_last
- aesenc %xmm9, %xmm5
- aesenc 176(%r15), %xmm5
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm9
- jl L_AES_GCM_encrypt_calc_iv_1_aesenc_avx_last
- aesenc %xmm9, %xmm5
- aesenc 208(%r15), %xmm5
- movdqa 224(%r15), %xmm9
-L_AES_GCM_encrypt_calc_iv_1_aesenc_avx_last:
- aesenclast %xmm9, %xmm5
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm5
- # Calc counter
- # Initialization vector
- cmpl $0x00, %edx
- movq $0x00, %rcx
- je L_AES_GCM_encrypt_calc_iv_done
- cmpl $16, %edx
- jl L_AES_GCM_encrypt_calc_iv_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_encrypt_calc_iv_16_loop:
- movdqu (%rax,%rcx,1), %xmm8
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm8
- pxor %xmm8, %xmm4
- pshufd $0x4e, %xmm4, %xmm1
- pshufd $0x4e, %xmm5, %xmm2
- movdqa %xmm5, %xmm3
- movdqa %xmm5, %xmm0
- pclmulqdq $0x11, %xmm4, %xmm3
- pclmulqdq $0x00, %xmm4, %xmm0
- pxor %xmm4, %xmm1
- pxor %xmm5, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm7
- movdqa %xmm3, %xmm4
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm7
- pxor %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm4, %xmm1
- psrld $31, %xmm0
- psrld $31, %xmm1
- pslld $0x01, %xmm7
- pslld $0x01, %xmm4
- movdqa %xmm0, %xmm2
- pslldq $4, %xmm0
- psrldq $12, %xmm2
- pslldq $4, %xmm1
- por %xmm2, %xmm4
- por %xmm0, %xmm7
- por %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm7, %xmm1
- movdqa %xmm7, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm7
- movdqa %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm7, %xmm2
- pxor %xmm2, %xmm4
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_calc_iv_16_loop
- movl %ebx, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_encrypt_calc_iv_done
-L_AES_GCM_encrypt_calc_iv_lt16:
- subq $16, %rsp
- pxor %xmm8, %xmm8
- xorl %ebx, %ebx
- movdqa %xmm8, (%rsp)
-L_AES_GCM_encrypt_calc_iv_loop:
- movzbl (%rax,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_calc_iv_loop
- movdqa (%rsp), %xmm8
- addq $16, %rsp
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm8
- pxor %xmm8, %xmm4
- pshufd $0x4e, %xmm4, %xmm1
- pshufd $0x4e, %xmm5, %xmm2
- movdqa %xmm5, %xmm3
- movdqa %xmm5, %xmm0
- pclmulqdq $0x11, %xmm4, %xmm3
- pclmulqdq $0x00, %xmm4, %xmm0
- pxor %xmm4, %xmm1
- pxor %xmm5, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm7
- movdqa %xmm3, %xmm4
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm7
- pxor %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm4, %xmm1
- psrld $31, %xmm0
- psrld $31, %xmm1
- pslld $0x01, %xmm7
- pslld $0x01, %xmm4
- movdqa %xmm0, %xmm2
- pslldq $4, %xmm0
- psrldq $12, %xmm2
- pslldq $4, %xmm1
- por %xmm2, %xmm4
- por %xmm0, %xmm7
- por %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm7, %xmm1
- movdqa %xmm7, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm7
- movdqa %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm7, %xmm2
- pxor %xmm2, %xmm4
-L_AES_GCM_encrypt_calc_iv_done:
- # T = Encrypt counter
- pxor %xmm0, %xmm0
- shll $3, %edx
- pinsrq $0x00, %rdx, %xmm0
- pxor %xmm0, %xmm4
- pshufd $0x4e, %xmm4, %xmm1
- pshufd $0x4e, %xmm5, %xmm2
- movdqa %xmm5, %xmm3
- movdqa %xmm5, %xmm0
- pclmulqdq $0x11, %xmm4, %xmm3
- pclmulqdq $0x00, %xmm4, %xmm0
- pxor %xmm4, %xmm1
- pxor %xmm5, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm7
- movdqa %xmm3, %xmm4
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm7
- pxor %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm4, %xmm1
- psrld $31, %xmm0
- psrld $31, %xmm1
- pslld $0x01, %xmm7
- pslld $0x01, %xmm4
- movdqa %xmm0, %xmm2
- pslldq $4, %xmm0
- psrldq $12, %xmm2
- pslldq $4, %xmm1
- por %xmm2, %xmm4
- por %xmm0, %xmm7
- por %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm7, %xmm1
- movdqa %xmm7, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm7
- movdqa %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm7, %xmm2
- pxor %xmm2, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm4
- # Encrypt counter
- movdqa (%r15), %xmm8
- pxor %xmm4, %xmm8
- aesenc 16(%r15), %xmm8
- aesenc 32(%r15), %xmm8
- aesenc 48(%r15), %xmm8
- aesenc 64(%r15), %xmm8
- aesenc 80(%r15), %xmm8
- aesenc 96(%r15), %xmm8
- aesenc 112(%r15), %xmm8
- aesenc 128(%r15), %xmm8
- aesenc 144(%r15), %xmm8
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm9
- jl L_AES_GCM_encrypt_calc_iv_2_aesenc_avx_last
- aesenc %xmm9, %xmm8
- aesenc 176(%r15), %xmm8
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm9
- jl L_AES_GCM_encrypt_calc_iv_2_aesenc_avx_last
- aesenc %xmm9, %xmm8
- aesenc 208(%r15), %xmm8
- movdqa 224(%r15), %xmm9
-L_AES_GCM_encrypt_calc_iv_2_aesenc_avx_last:
- aesenclast %xmm9, %xmm8
- movdqa %xmm8, 144(%rsp)
-L_AES_GCM_encrypt_iv_done:
- # Additional authentication data
- movl %r11d, %edx
- cmpl $0x00, %edx
- je L_AES_GCM_encrypt_calc_aad_done
- xorl %ecx, %ecx
- cmpl $16, %edx
- jl L_AES_GCM_encrypt_calc_aad_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_encrypt_calc_aad_16_loop:
- movdqu (%r12,%rcx,1), %xmm8
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm8
- pxor %xmm8, %xmm6
- pshufd $0x4e, %xmm6, %xmm1
- pshufd $0x4e, %xmm5, %xmm2
- movdqa %xmm5, %xmm3
- movdqa %xmm5, %xmm0
- pclmulqdq $0x11, %xmm6, %xmm3
- pclmulqdq $0x00, %xmm6, %xmm0
- pxor %xmm6, %xmm1
- pxor %xmm5, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm7
- movdqa %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm7
- pxor %xmm1, %xmm6
- movdqa %xmm7, %xmm0
- movdqa %xmm6, %xmm1
- psrld $31, %xmm0
- psrld $31, %xmm1
- pslld $0x01, %xmm7
- pslld $0x01, %xmm6
- movdqa %xmm0, %xmm2
- pslldq $4, %xmm0
- psrldq $12, %xmm2
- pslldq $4, %xmm1
- por %xmm2, %xmm6
- por %xmm0, %xmm7
- por %xmm1, %xmm6
- movdqa %xmm7, %xmm0
- movdqa %xmm7, %xmm1
- movdqa %xmm7, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm7
- movdqa %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm7, %xmm2
- pxor %xmm2, %xmm6
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_calc_aad_16_loop
- movl %r11d, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_encrypt_calc_aad_done
-L_AES_GCM_encrypt_calc_aad_lt16:
- subq $16, %rsp
- pxor %xmm8, %xmm8
- xorl %ebx, %ebx
- movdqa %xmm8, (%rsp)
-L_AES_GCM_encrypt_calc_aad_loop:
- movzbl (%r12,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_calc_aad_loop
- movdqa (%rsp), %xmm8
- addq $16, %rsp
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm8
- pxor %xmm8, %xmm6
- pshufd $0x4e, %xmm6, %xmm1
- pshufd $0x4e, %xmm5, %xmm2
- movdqa %xmm5, %xmm3
- movdqa %xmm5, %xmm0
- pclmulqdq $0x11, %xmm6, %xmm3
- pclmulqdq $0x00, %xmm6, %xmm0
- pxor %xmm6, %xmm1
- pxor %xmm5, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm7
- movdqa %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm7
- pxor %xmm1, %xmm6
- movdqa %xmm7, %xmm0
- movdqa %xmm6, %xmm1
- psrld $31, %xmm0
- psrld $31, %xmm1
- pslld $0x01, %xmm7
- pslld $0x01, %xmm6
- movdqa %xmm0, %xmm2
- pslldq $4, %xmm0
- psrldq $12, %xmm2
- pslldq $4, %xmm1
- por %xmm2, %xmm6
- por %xmm0, %xmm7
- por %xmm1, %xmm6
- movdqa %xmm7, %xmm0
- movdqa %xmm7, %xmm1
- movdqa %xmm7, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm7
- movdqa %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm7, %xmm2
- pxor %xmm2, %xmm6
-L_AES_GCM_encrypt_calc_aad_done:
- # Calculate counter and H
- pshufb L_aes_gcm_bswap_epi64(%rip), %xmm4
- movdqa %xmm5, %xmm9
- paddd L_aes_gcm_one(%rip), %xmm4
- movdqa %xmm5, %xmm8
- movdqa %xmm4, 128(%rsp)
- psrlq $63, %xmm9
- psllq $0x01, %xmm8
- pslldq $8, %xmm9
- por %xmm9, %xmm8
- pshufd $0xff, %xmm5, %xmm5
- psrad $31, %xmm5
- pand L_aes_gcm_mod2_128(%rip), %xmm5
- pxor %xmm8, %xmm5
- xorq %rbx, %rbx
- cmpl $0x80, %r9d
- movl %r9d, %r13d
- jl L_AES_GCM_encrypt_done_128
- andl $0xffffff80, %r13d
- movdqa %xmm6, %xmm2
- # H ^ 1
- movdqa %xmm5, (%rsp)
- # H ^ 2
- pshufd $0x4e, %xmm5, %xmm9
- pshufd $0x4e, %xmm5, %xmm10
- movdqa %xmm5, %xmm11
- movdqa %xmm5, %xmm8
- pclmulqdq $0x11, %xmm5, %xmm11
- pclmulqdq $0x00, %xmm5, %xmm8
- pxor %xmm5, %xmm9
- pxor %xmm5, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm0
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm0
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm0
- movdqa %xmm0, 16(%rsp)
- # H ^ 3
- pshufd $0x4e, %xmm5, %xmm9
- pshufd $0x4e, %xmm0, %xmm10
- movdqa %xmm0, %xmm11
- movdqa %xmm0, %xmm8
- pclmulqdq $0x11, %xmm5, %xmm11
- pclmulqdq $0x00, %xmm5, %xmm8
- pxor %xmm5, %xmm9
- pxor %xmm0, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm1
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm1
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm1
- movdqa %xmm1, 32(%rsp)
- # H ^ 4
- pshufd $0x4e, %xmm0, %xmm9
- pshufd $0x4e, %xmm0, %xmm10
- movdqa %xmm0, %xmm11
- movdqa %xmm0, %xmm8
- pclmulqdq $0x11, %xmm0, %xmm11
- pclmulqdq $0x00, %xmm0, %xmm8
- pxor %xmm0, %xmm9
- pxor %xmm0, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm3
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm3
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm3
- movdqa %xmm3, 48(%rsp)
- # H ^ 5
- pshufd $0x4e, %xmm0, %xmm9
- pshufd $0x4e, %xmm1, %xmm10
- movdqa %xmm1, %xmm11
- movdqa %xmm1, %xmm8
- pclmulqdq $0x11, %xmm0, %xmm11
- pclmulqdq $0x00, %xmm0, %xmm8
- pxor %xmm0, %xmm9
- pxor %xmm1, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm7
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm7
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm7
- movdqa %xmm7, 64(%rsp)
- # H ^ 6
- pshufd $0x4e, %xmm1, %xmm9
- pshufd $0x4e, %xmm1, %xmm10
- movdqa %xmm1, %xmm11
- movdqa %xmm1, %xmm8
- pclmulqdq $0x11, %xmm1, %xmm11
- pclmulqdq $0x00, %xmm1, %xmm8
- pxor %xmm1, %xmm9
- pxor %xmm1, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm7
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm7
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm7
- movdqa %xmm7, 80(%rsp)
- # H ^ 7
- pshufd $0x4e, %xmm1, %xmm9
- pshufd $0x4e, %xmm3, %xmm10
- movdqa %xmm3, %xmm11
- movdqa %xmm3, %xmm8
- pclmulqdq $0x11, %xmm1, %xmm11
- pclmulqdq $0x00, %xmm1, %xmm8
- pxor %xmm1, %xmm9
- pxor %xmm3, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm7
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm7
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm7
- movdqa %xmm7, 96(%rsp)
- # H ^ 8
- pshufd $0x4e, %xmm3, %xmm9
- pshufd $0x4e, %xmm3, %xmm10
- movdqa %xmm3, %xmm11
- movdqa %xmm3, %xmm8
- pclmulqdq $0x11, %xmm3, %xmm11
- pclmulqdq $0x00, %xmm3, %xmm8
- pxor %xmm3, %xmm9
- pxor %xmm3, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm7
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm7
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm7
- movdqa %xmm7, 112(%rsp)
- # First 128 bytes of input
- movdqa 128(%rsp), %xmm8
- movdqa L_aes_gcm_bswap_epi64(%rip), %xmm1
- movdqa %xmm8, %xmm0
- pshufb %xmm1, %xmm8
- movdqa %xmm0, %xmm9
- paddd L_aes_gcm_one(%rip), %xmm9
- pshufb %xmm1, %xmm9
- movdqa %xmm0, %xmm10
- paddd L_aes_gcm_two(%rip), %xmm10
- pshufb %xmm1, %xmm10
- movdqa %xmm0, %xmm11
- paddd L_aes_gcm_three(%rip), %xmm11
- pshufb %xmm1, %xmm11
- movdqa %xmm0, %xmm12
- paddd L_aes_gcm_four(%rip), %xmm12
- pshufb %xmm1, %xmm12
- movdqa %xmm0, %xmm13
- paddd L_aes_gcm_five(%rip), %xmm13
- pshufb %xmm1, %xmm13
- movdqa %xmm0, %xmm14
- paddd L_aes_gcm_six(%rip), %xmm14
- pshufb %xmm1, %xmm14
- movdqa %xmm0, %xmm15
- paddd L_aes_gcm_seven(%rip), %xmm15
- pshufb %xmm1, %xmm15
- paddd L_aes_gcm_eight(%rip), %xmm0
- movdqa (%r15), %xmm7
- movdqa %xmm0, 128(%rsp)
- pxor %xmm7, %xmm8
- pxor %xmm7, %xmm9
- pxor %xmm7, %xmm10
- pxor %xmm7, %xmm11
- pxor %xmm7, %xmm12
- pxor %xmm7, %xmm13
- pxor %xmm7, %xmm14
- pxor %xmm7, %xmm15
- movdqa 16(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 32(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 48(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 64(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 80(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 96(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 112(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 128(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 144(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm7
- jl L_AES_GCM_encrypt_enc_done
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 176(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm7
- jl L_AES_GCM_encrypt_enc_done
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 208(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 224(%r15), %xmm7
-L_AES_GCM_encrypt_enc_done:
- aesenclast %xmm7, %xmm8
- aesenclast %xmm7, %xmm9
- movdqu (%rdi), %xmm0
- movdqu 16(%rdi), %xmm1
- pxor %xmm0, %xmm8
- pxor %xmm1, %xmm9
- movdqu %xmm8, (%rsi)
- movdqu %xmm9, 16(%rsi)
- aesenclast %xmm7, %xmm10
- aesenclast %xmm7, %xmm11
- movdqu 32(%rdi), %xmm0
- movdqu 48(%rdi), %xmm1
- pxor %xmm0, %xmm10
- pxor %xmm1, %xmm11
- movdqu %xmm10, 32(%rsi)
- movdqu %xmm11, 48(%rsi)
- aesenclast %xmm7, %xmm12
- aesenclast %xmm7, %xmm13
- movdqu 64(%rdi), %xmm0
- movdqu 80(%rdi), %xmm1
- pxor %xmm0, %xmm12
- pxor %xmm1, %xmm13
- movdqu %xmm12, 64(%rsi)
- movdqu %xmm13, 80(%rsi)
- aesenclast %xmm7, %xmm14
- aesenclast %xmm7, %xmm15
- movdqu 96(%rdi), %xmm0
- movdqu 112(%rdi), %xmm1
- pxor %xmm0, %xmm14
- pxor %xmm1, %xmm15
- movdqu %xmm14, 96(%rsi)
- movdqu %xmm15, 112(%rsi)
- cmpl $0x80, %r13d
- movl $0x80, %ebx
- jle L_AES_GCM_encrypt_end_128
- # More 128 bytes of input
-L_AES_GCM_encrypt_ghash_128:
- leaq (%rdi,%rbx,1), %rcx
- leaq (%rsi,%rbx,1), %rdx
- movdqa 128(%rsp), %xmm8
- movdqa L_aes_gcm_bswap_epi64(%rip), %xmm1
- movdqa %xmm8, %xmm0
- pshufb %xmm1, %xmm8
- movdqa %xmm0, %xmm9
- paddd L_aes_gcm_one(%rip), %xmm9
- pshufb %xmm1, %xmm9
- movdqa %xmm0, %xmm10
- paddd L_aes_gcm_two(%rip), %xmm10
- pshufb %xmm1, %xmm10
- movdqa %xmm0, %xmm11
- paddd L_aes_gcm_three(%rip), %xmm11
- pshufb %xmm1, %xmm11
- movdqa %xmm0, %xmm12
- paddd L_aes_gcm_four(%rip), %xmm12
- pshufb %xmm1, %xmm12
- movdqa %xmm0, %xmm13
- paddd L_aes_gcm_five(%rip), %xmm13
- pshufb %xmm1, %xmm13
- movdqa %xmm0, %xmm14
- paddd L_aes_gcm_six(%rip), %xmm14
- pshufb %xmm1, %xmm14
- movdqa %xmm0, %xmm15
- paddd L_aes_gcm_seven(%rip), %xmm15
- pshufb %xmm1, %xmm15
- paddd L_aes_gcm_eight(%rip), %xmm0
- movdqa (%r15), %xmm7
- movdqa %xmm0, 128(%rsp)
- pxor %xmm7, %xmm8
- pxor %xmm7, %xmm9
- pxor %xmm7, %xmm10
- pxor %xmm7, %xmm11
- pxor %xmm7, %xmm12
- pxor %xmm7, %xmm13
- pxor %xmm7, %xmm14
- pxor %xmm7, %xmm15
- movdqa 112(%rsp), %xmm7
- movdqu -128(%rdx), %xmm0
- aesenc 16(%r15), %xmm8
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- pxor %xmm2, %xmm0
- pshufd $0x4e, %xmm7, %xmm1
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm7, %xmm1
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm3
- pclmulqdq $0x11, %xmm7, %xmm3
- aesenc 16(%r15), %xmm9
- aesenc 16(%r15), %xmm10
- movdqa %xmm0, %xmm2
- pclmulqdq $0x00, %xmm7, %xmm2
- aesenc 16(%r15), %xmm11
- aesenc 16(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm1
- aesenc 16(%r15), %xmm13
- aesenc 16(%r15), %xmm14
- aesenc 16(%r15), %xmm15
- pxor %xmm2, %xmm1
- pxor %xmm3, %xmm1
- movdqa 96(%rsp), %xmm7
- movdqu -112(%rdx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 32(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 32(%r15), %xmm9
- aesenc 32(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 32(%r15), %xmm11
- aesenc 32(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 32(%r15), %xmm13
- aesenc 32(%r15), %xmm14
- aesenc 32(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa 80(%rsp), %xmm7
- movdqu -96(%rdx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 48(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 48(%r15), %xmm9
- aesenc 48(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 48(%r15), %xmm11
- aesenc 48(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 48(%r15), %xmm13
- aesenc 48(%r15), %xmm14
- aesenc 48(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa 64(%rsp), %xmm7
- movdqu -80(%rdx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 64(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 64(%r15), %xmm9
- aesenc 64(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 64(%r15), %xmm11
- aesenc 64(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 64(%r15), %xmm13
- aesenc 64(%r15), %xmm14
- aesenc 64(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa 48(%rsp), %xmm7
- movdqu -64(%rdx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 80(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 80(%r15), %xmm9
- aesenc 80(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 80(%r15), %xmm11
- aesenc 80(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 80(%r15), %xmm13
- aesenc 80(%r15), %xmm14
- aesenc 80(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa 32(%rsp), %xmm7
- movdqu -48(%rdx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 96(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 96(%r15), %xmm9
- aesenc 96(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 96(%r15), %xmm11
- aesenc 96(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 96(%r15), %xmm13
- aesenc 96(%r15), %xmm14
- aesenc 96(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa 16(%rsp), %xmm7
- movdqu -32(%rdx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 112(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 112(%r15), %xmm9
- aesenc 112(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 112(%r15), %xmm11
- aesenc 112(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 112(%r15), %xmm13
- aesenc 112(%r15), %xmm14
- aesenc 112(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa (%rsp), %xmm7
- movdqu -16(%rdx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 128(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 128(%r15), %xmm9
- aesenc 128(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 128(%r15), %xmm11
- aesenc 128(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 128(%r15), %xmm13
- aesenc 128(%r15), %xmm14
- aesenc 128(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa %xmm1, %xmm5
- psrldq $8, %xmm1
- pslldq $8, %xmm5
- aesenc 144(%r15), %xmm8
- pxor %xmm5, %xmm2
- pxor %xmm1, %xmm3
- movdqa %xmm2, %xmm7
- movdqa %xmm2, %xmm4
- movdqa %xmm2, %xmm5
- aesenc 144(%r15), %xmm9
- pslld $31, %xmm7
- pslld $30, %xmm4
- pslld $25, %xmm5
- aesenc 144(%r15), %xmm10
- pxor %xmm4, %xmm7
- pxor %xmm5, %xmm7
- aesenc 144(%r15), %xmm11
- movdqa %xmm7, %xmm4
- pslldq $12, %xmm7
- psrldq $4, %xmm4
- aesenc 144(%r15), %xmm12
- pxor %xmm7, %xmm2
- movdqa %xmm2, %xmm5
- movdqa %xmm2, %xmm1
- movdqa %xmm2, %xmm0
- aesenc 144(%r15), %xmm13
- psrld $0x01, %xmm5
- psrld $2, %xmm1
- psrld $7, %xmm0
- aesenc 144(%r15), %xmm14
- pxor %xmm1, %xmm5
- pxor %xmm0, %xmm5
- aesenc 144(%r15), %xmm15
- pxor %xmm4, %xmm5
- pxor %xmm5, %xmm2
- pxor %xmm3, %xmm2
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm7
- jl L_AES_GCM_encrypt_aesenc_128_ghash_avx_done
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 176(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm7
- jl L_AES_GCM_encrypt_aesenc_128_ghash_avx_done
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 208(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 224(%r15), %xmm7
-L_AES_GCM_encrypt_aesenc_128_ghash_avx_done:
- aesenclast %xmm7, %xmm8
- aesenclast %xmm7, %xmm9
- movdqu (%rcx), %xmm0
- movdqu 16(%rcx), %xmm1
- pxor %xmm0, %xmm8
- pxor %xmm1, %xmm9
- movdqu %xmm8, (%rdx)
- movdqu %xmm9, 16(%rdx)
- aesenclast %xmm7, %xmm10
- aesenclast %xmm7, %xmm11
- movdqu 32(%rcx), %xmm0
- movdqu 48(%rcx), %xmm1
- pxor %xmm0, %xmm10
- pxor %xmm1, %xmm11
- movdqu %xmm10, 32(%rdx)
- movdqu %xmm11, 48(%rdx)
- aesenclast %xmm7, %xmm12
- aesenclast %xmm7, %xmm13
- movdqu 64(%rcx), %xmm0
- movdqu 80(%rcx), %xmm1
- pxor %xmm0, %xmm12
- pxor %xmm1, %xmm13
- movdqu %xmm12, 64(%rdx)
- movdqu %xmm13, 80(%rdx)
- aesenclast %xmm7, %xmm14
- aesenclast %xmm7, %xmm15
- movdqu 96(%rcx), %xmm0
- movdqu 112(%rcx), %xmm1
- pxor %xmm0, %xmm14
- pxor %xmm1, %xmm15
- movdqu %xmm14, 96(%rdx)
- movdqu %xmm15, 112(%rdx)
- addl $0x80, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_encrypt_ghash_128
-L_AES_GCM_encrypt_end_128:
- movdqa L_aes_gcm_bswap_mask(%rip), %xmm4
- pshufb %xmm4, %xmm8
- pshufb %xmm4, %xmm9
- pshufb %xmm4, %xmm10
- pshufb %xmm4, %xmm11
- pxor %xmm2, %xmm8
- pshufb %xmm4, %xmm12
- pshufb %xmm4, %xmm13
- pshufb %xmm4, %xmm14
- pshufb %xmm4, %xmm15
- movdqa 112(%rsp), %xmm7
- pshufd $0x4e, %xmm8, %xmm1
- pshufd $0x4e, %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- pclmulqdq $0x11, %xmm8, %xmm3
- pclmulqdq $0x00, %xmm8, %xmm0
- pxor %xmm8, %xmm1
- pxor %xmm7, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm4
- movdqa %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm4
- pxor %xmm1, %xmm6
- movdqa 96(%rsp), %xmm7
- pshufd $0x4e, %xmm9, %xmm1
- pshufd $0x4e, %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- pclmulqdq $0x11, %xmm9, %xmm3
- pclmulqdq $0x00, %xmm9, %xmm0
- pxor %xmm9, %xmm1
- pxor %xmm7, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- pxor %xmm0, %xmm4
- pxor %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm4
- pxor %xmm1, %xmm6
- movdqa 80(%rsp), %xmm7
- pshufd $0x4e, %xmm10, %xmm1
- pshufd $0x4e, %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- pclmulqdq $0x11, %xmm10, %xmm3
- pclmulqdq $0x00, %xmm10, %xmm0
- pxor %xmm10, %xmm1
- pxor %xmm7, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- pxor %xmm0, %xmm4
- pxor %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm4
- pxor %xmm1, %xmm6
- movdqa 64(%rsp), %xmm7
- pshufd $0x4e, %xmm11, %xmm1
- pshufd $0x4e, %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- pclmulqdq $0x11, %xmm11, %xmm3
- pclmulqdq $0x00, %xmm11, %xmm0
- pxor %xmm11, %xmm1
- pxor %xmm7, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- pxor %xmm0, %xmm4
- pxor %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm4
- pxor %xmm1, %xmm6
- movdqa 48(%rsp), %xmm7
- pshufd $0x4e, %xmm12, %xmm1
- pshufd $0x4e, %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- pclmulqdq $0x11, %xmm12, %xmm3
- pclmulqdq $0x00, %xmm12, %xmm0
- pxor %xmm12, %xmm1
- pxor %xmm7, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- pxor %xmm0, %xmm4
- pxor %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm4
- pxor %xmm1, %xmm6
- movdqa 32(%rsp), %xmm7
- pshufd $0x4e, %xmm13, %xmm1
- pshufd $0x4e, %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- pclmulqdq $0x11, %xmm13, %xmm3
- pclmulqdq $0x00, %xmm13, %xmm0
- pxor %xmm13, %xmm1
- pxor %xmm7, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- pxor %xmm0, %xmm4
- pxor %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm4
- pxor %xmm1, %xmm6
- movdqa 16(%rsp), %xmm7
- pshufd $0x4e, %xmm14, %xmm1
- pshufd $0x4e, %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- pclmulqdq $0x11, %xmm14, %xmm3
- pclmulqdq $0x00, %xmm14, %xmm0
- pxor %xmm14, %xmm1
- pxor %xmm7, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- pxor %xmm0, %xmm4
- pxor %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm4
- pxor %xmm1, %xmm6
- movdqa (%rsp), %xmm7
- pshufd $0x4e, %xmm15, %xmm1
- pshufd $0x4e, %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- pclmulqdq $0x11, %xmm15, %xmm3
- pclmulqdq $0x00, %xmm15, %xmm0
- pxor %xmm15, %xmm1
- pxor %xmm7, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- pxor %xmm0, %xmm4
- pxor %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm4
- pxor %xmm1, %xmm6
- movdqa %xmm4, %xmm0
- movdqa %xmm4, %xmm1
- movdqa %xmm4, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm4
- movdqa %xmm4, %xmm2
- movdqa %xmm4, %xmm3
- movdqa %xmm4, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm4, %xmm2
- pxor %xmm2, %xmm6
- movdqa (%rsp), %xmm5
-L_AES_GCM_encrypt_done_128:
- movl %r9d, %edx
- cmpl %edx, %ebx
- jge L_AES_GCM_encrypt_done_enc
- movl %r9d, %r13d
- andl $0xfffffff0, %r13d
- cmpl %r13d, %ebx
- jge L_AES_GCM_encrypt_last_block_done
- leaq (%rdi,%rbx,1), %rcx
- leaq (%rsi,%rbx,1), %rdx
- movdqa 128(%rsp), %xmm8
- movdqa %xmm8, %xmm9
- pshufb L_aes_gcm_bswap_epi64(%rip), %xmm8
- paddd L_aes_gcm_one(%rip), %xmm9
- pxor (%r15), %xmm8
- movdqa %xmm9, 128(%rsp)
- aesenc 16(%r15), %xmm8
- aesenc 32(%r15), %xmm8
- aesenc 48(%r15), %xmm8
- aesenc 64(%r15), %xmm8
- aesenc 80(%r15), %xmm8
- aesenc 96(%r15), %xmm8
- aesenc 112(%r15), %xmm8
- aesenc 128(%r15), %xmm8
- aesenc 144(%r15), %xmm8
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm9
- jl L_AES_GCM_encrypt_aesenc_block_aesenc_avx_last
- aesenc %xmm9, %xmm8
- aesenc 176(%r15), %xmm8
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm9
- jl L_AES_GCM_encrypt_aesenc_block_aesenc_avx_last
- aesenc %xmm9, %xmm8
- aesenc 208(%r15), %xmm8
- movdqa 224(%r15), %xmm9
-L_AES_GCM_encrypt_aesenc_block_aesenc_avx_last:
- aesenclast %xmm9, %xmm8
- movdqu (%rcx), %xmm9
- pxor %xmm9, %xmm8
- movdqu %xmm8, (%rdx)
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm8
- pxor %xmm8, %xmm6
- addl $16, %ebx
- cmpl %r13d, %ebx
- jge L_AES_GCM_encrypt_last_block_ghash
-L_AES_GCM_encrypt_last_block_start:
- leaq (%rdi,%rbx,1), %rcx
- leaq (%rsi,%rbx,1), %rdx
- movdqa 128(%rsp), %xmm8
- movdqa %xmm8, %xmm9
- pshufb L_aes_gcm_bswap_epi64(%rip), %xmm8
- paddd L_aes_gcm_one(%rip), %xmm9
- pxor (%r15), %xmm8
- movdqa %xmm9, 128(%rsp)
- movdqa %xmm6, %xmm10
- pclmulqdq $16, %xmm5, %xmm10
- aesenc 16(%r15), %xmm8
- aesenc 32(%r15), %xmm8
- movdqa %xmm6, %xmm11
- pclmulqdq $0x01, %xmm5, %xmm11
- aesenc 48(%r15), %xmm8
- aesenc 64(%r15), %xmm8
- movdqa %xmm6, %xmm12
- pclmulqdq $0x00, %xmm5, %xmm12
- aesenc 80(%r15), %xmm8
- movdqa %xmm6, %xmm1
- pclmulqdq $0x11, %xmm5, %xmm1
- aesenc 96(%r15), %xmm8
- pxor %xmm11, %xmm10
- movdqa %xmm10, %xmm2
- psrldq $8, %xmm10
- pslldq $8, %xmm2
- aesenc 112(%r15), %xmm8
- movdqa %xmm1, %xmm3
- pxor %xmm12, %xmm2
- pxor %xmm10, %xmm3
- movdqa L_aes_gcm_mod2_128(%rip), %xmm0
- movdqa %xmm2, %xmm11
- pclmulqdq $16, %xmm0, %xmm11
- aesenc 128(%r15), %xmm8
- pshufd $0x4e, %xmm2, %xmm10
- pxor %xmm11, %xmm10
- movdqa %xmm10, %xmm11
- pclmulqdq $16, %xmm0, %xmm11
- aesenc 144(%r15), %xmm8
- pshufd $0x4e, %xmm10, %xmm6
- pxor %xmm11, %xmm6
- pxor %xmm3, %xmm6
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm9
- jl L_AES_GCM_encrypt_aesenc_gfmul_last
- aesenc %xmm9, %xmm8
- aesenc 176(%r15), %xmm8
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm9
- jl L_AES_GCM_encrypt_aesenc_gfmul_last
- aesenc %xmm9, %xmm8
- aesenc 208(%r15), %xmm8
- movdqa 224(%r15), %xmm9
-L_AES_GCM_encrypt_aesenc_gfmul_last:
- aesenclast %xmm9, %xmm8
- movdqu (%rcx), %xmm9
- pxor %xmm9, %xmm8
- movdqu %xmm8, (%rdx)
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm8
- pxor %xmm8, %xmm6
- addl $16, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_encrypt_last_block_start
-L_AES_GCM_encrypt_last_block_ghash:
- pshufd $0x4e, %xmm5, %xmm9
- pshufd $0x4e, %xmm6, %xmm10
- movdqa %xmm6, %xmm11
- movdqa %xmm6, %xmm8
- pclmulqdq $0x11, %xmm5, %xmm11
- pclmulqdq $0x00, %xmm5, %xmm8
- pxor %xmm5, %xmm9
- pxor %xmm6, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm6
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm6
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm6
-L_AES_GCM_encrypt_last_block_done:
- movl %r9d, %ecx
- movl %ecx, %edx
- andl $15, %ecx
- jz L_AES_GCM_encrypt_aesenc_last15_enc_avx_done
- movdqa 128(%rsp), %xmm4
- pshufb L_aes_gcm_bswap_epi64(%rip), %xmm4
- pxor (%r15), %xmm4
- aesenc 16(%r15), %xmm4
- aesenc 32(%r15), %xmm4
- aesenc 48(%r15), %xmm4
- aesenc 64(%r15), %xmm4
- aesenc 80(%r15), %xmm4
- aesenc 96(%r15), %xmm4
- aesenc 112(%r15), %xmm4
- aesenc 128(%r15), %xmm4
- aesenc 144(%r15), %xmm4
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm9
- jl L_AES_GCM_encrypt_aesenc_last15_enc_avx_aesenc_avx_last
- aesenc %xmm9, %xmm4
- aesenc 176(%r15), %xmm4
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm9
- jl L_AES_GCM_encrypt_aesenc_last15_enc_avx_aesenc_avx_last
- aesenc %xmm9, %xmm4
- aesenc 208(%r15), %xmm4
- movdqa 224(%r15), %xmm9
-L_AES_GCM_encrypt_aesenc_last15_enc_avx_aesenc_avx_last:
- aesenclast %xmm9, %xmm4
- subq $16, %rsp
- xorl %ecx, %ecx
- movdqa %xmm4, (%rsp)
-L_AES_GCM_encrypt_aesenc_last15_enc_avx_loop:
- movzbl (%rdi,%rbx,1), %r13d
- xorb (%rsp,%rcx,1), %r13b
- movb %r13b, (%rsi,%rbx,1)
- movb %r13b, (%rsp,%rcx,1)
- incl %ebx
- incl %ecx
- cmpl %edx, %ebx
- jl L_AES_GCM_encrypt_aesenc_last15_enc_avx_loop
- xorq %r13, %r13
- cmpl $16, %ecx
- je L_AES_GCM_encrypt_aesenc_last15_enc_avx_finish_enc
-L_AES_GCM_encrypt_aesenc_last15_enc_avx_byte_loop:
- movb %r13b, (%rsp,%rcx,1)
- incl %ecx
- cmpl $16, %ecx
- jl L_AES_GCM_encrypt_aesenc_last15_enc_avx_byte_loop
-L_AES_GCM_encrypt_aesenc_last15_enc_avx_finish_enc:
- movdqa (%rsp), %xmm4
- addq $16, %rsp
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm4
- pxor %xmm4, %xmm6
- pshufd $0x4e, %xmm5, %xmm9
- pshufd $0x4e, %xmm6, %xmm10
- movdqa %xmm6, %xmm11
- movdqa %xmm6, %xmm8
- pclmulqdq $0x11, %xmm5, %xmm11
- pclmulqdq $0x00, %xmm5, %xmm8
- pxor %xmm5, %xmm9
- pxor %xmm6, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm6
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm6
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm6
-L_AES_GCM_encrypt_aesenc_last15_enc_avx_done:
-L_AES_GCM_encrypt_done_enc:
- movl %r9d, %edx
- movl %r11d, %ecx
- shlq $3, %rdx
- shlq $3, %rcx
- pinsrq $0x00, %rdx, %xmm0
- pinsrq $0x01, %rcx, %xmm0
- pxor %xmm0, %xmm6
- pshufd $0x4e, %xmm5, %xmm9
- pshufd $0x4e, %xmm6, %xmm10
- movdqa %xmm6, %xmm11
- movdqa %xmm6, %xmm8
- pclmulqdq $0x11, %xmm5, %xmm11
- pclmulqdq $0x00, %xmm5, %xmm8
- pxor %xmm5, %xmm9
- pxor %xmm6, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm6
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm6
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm6
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm6
- movdqa 144(%rsp), %xmm0
- pxor %xmm6, %xmm0
- cmpl $16, %r14d
- je L_AES_GCM_encrypt_store_tag_16
- xorq %rcx, %rcx
- movdqa %xmm0, (%rsp)
-L_AES_GCM_encrypt_store_tag_loop:
- movzbl (%rsp,%rcx,1), %r13d
- movb %r13b, (%r8,%rcx,1)
- incl %ecx
- cmpl %r14d, %ecx
- jne L_AES_GCM_encrypt_store_tag_loop
- jmp L_AES_GCM_encrypt_store_tag_done
-L_AES_GCM_encrypt_store_tag_16:
- movdqu %xmm0, (%r8)
-L_AES_GCM_encrypt_store_tag_done:
- addq $0xa0, %rsp
- popq %r15
- popq %r14
- popq %rbx
- popq %r12
- popq %r13
- repz retq
-#ifndef __APPLE__
-.size AES_GCM_encrypt,.-AES_GCM_encrypt
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.text
-.globl AES_GCM_decrypt
-.type AES_GCM_decrypt,@function
-.align 4
-AES_GCM_decrypt:
-#else
-.section __TEXT,__text
-.globl _AES_GCM_decrypt
-.p2align 2
-_AES_GCM_decrypt:
-#endif /* __APPLE__ */
- pushq %r13
- pushq %r12
- pushq %rbx
- pushq %r14
- pushq %r15
- pushq %rbp
- movq %rdx, %r12
- movq %rcx, %rax
- movl 56(%rsp), %r11d
- movl 64(%rsp), %ebx
- movl 72(%rsp), %r14d
- movq 80(%rsp), %r15
- movl 88(%rsp), %r10d
- movq 96(%rsp), %rbp
- subq $0xa8, %rsp
- pxor %xmm4, %xmm4
- pxor %xmm6, %xmm6
- cmpl $12, %ebx
- movl %ebx, %edx
- jne L_AES_GCM_decrypt_iv_not_12
- # # Calculate values when IV is 12 bytes
- # Set counter based on IV
- movl $0x1000000, %ecx
- pinsrq $0x00, (%rax), %xmm4
- pinsrd $2, 8(%rax), %xmm4
- pinsrd $3, %ecx, %xmm4
- # H = Encrypt X(=0) and T = Encrypt counter
- movdqa %xmm4, %xmm1
- movdqa (%r15), %xmm5
- pxor %xmm5, %xmm1
- movdqa 16(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 32(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 48(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 64(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 80(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 96(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 112(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 128(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 144(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm7
- jl L_AES_GCM_decrypt_calc_iv_12_last
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 176(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm7
- jl L_AES_GCM_decrypt_calc_iv_12_last
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 208(%r15), %xmm7
- aesenc %xmm7, %xmm5
- aesenc %xmm7, %xmm1
- movdqa 224(%r15), %xmm7
-L_AES_GCM_decrypt_calc_iv_12_last:
- aesenclast %xmm7, %xmm5
- aesenclast %xmm7, %xmm1
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm5
- movdqa %xmm1, 144(%rsp)
- jmp L_AES_GCM_decrypt_iv_done
-L_AES_GCM_decrypt_iv_not_12:
- # Calculate values when IV is not 12 bytes
- # H = Encrypt X(=0)
- movdqa (%r15), %xmm5
- aesenc 16(%r15), %xmm5
- aesenc 32(%r15), %xmm5
- aesenc 48(%r15), %xmm5
- aesenc 64(%r15), %xmm5
- aesenc 80(%r15), %xmm5
- aesenc 96(%r15), %xmm5
- aesenc 112(%r15), %xmm5
- aesenc 128(%r15), %xmm5
- aesenc 144(%r15), %xmm5
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm9
- jl L_AES_GCM_decrypt_calc_iv_1_aesenc_avx_last
- aesenc %xmm9, %xmm5
- aesenc 176(%r15), %xmm5
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm9
- jl L_AES_GCM_decrypt_calc_iv_1_aesenc_avx_last
- aesenc %xmm9, %xmm5
- aesenc 208(%r15), %xmm5
- movdqa 224(%r15), %xmm9
-L_AES_GCM_decrypt_calc_iv_1_aesenc_avx_last:
- aesenclast %xmm9, %xmm5
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm5
- # Calc counter
- # Initialization vector
- cmpl $0x00, %edx
- movq $0x00, %rcx
- je L_AES_GCM_decrypt_calc_iv_done
- cmpl $16, %edx
- jl L_AES_GCM_decrypt_calc_iv_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_decrypt_calc_iv_16_loop:
- movdqu (%rax,%rcx,1), %xmm8
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm8
- pxor %xmm8, %xmm4
- pshufd $0x4e, %xmm4, %xmm1
- pshufd $0x4e, %xmm5, %xmm2
- movdqa %xmm5, %xmm3
- movdqa %xmm5, %xmm0
- pclmulqdq $0x11, %xmm4, %xmm3
- pclmulqdq $0x00, %xmm4, %xmm0
- pxor %xmm4, %xmm1
- pxor %xmm5, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm7
- movdqa %xmm3, %xmm4
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm7
- pxor %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm4, %xmm1
- psrld $31, %xmm0
- psrld $31, %xmm1
- pslld $0x01, %xmm7
- pslld $0x01, %xmm4
- movdqa %xmm0, %xmm2
- pslldq $4, %xmm0
- psrldq $12, %xmm2
- pslldq $4, %xmm1
- por %xmm2, %xmm4
- por %xmm0, %xmm7
- por %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm7, %xmm1
- movdqa %xmm7, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm7
- movdqa %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm7, %xmm2
- pxor %xmm2, %xmm4
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_calc_iv_16_loop
- movl %ebx, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_decrypt_calc_iv_done
-L_AES_GCM_decrypt_calc_iv_lt16:
- subq $16, %rsp
- pxor %xmm8, %xmm8
- xorl %ebx, %ebx
- movdqa %xmm8, (%rsp)
-L_AES_GCM_decrypt_calc_iv_loop:
- movzbl (%rax,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_calc_iv_loop
- movdqa (%rsp), %xmm8
- addq $16, %rsp
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm8
- pxor %xmm8, %xmm4
- pshufd $0x4e, %xmm4, %xmm1
- pshufd $0x4e, %xmm5, %xmm2
- movdqa %xmm5, %xmm3
- movdqa %xmm5, %xmm0
- pclmulqdq $0x11, %xmm4, %xmm3
- pclmulqdq $0x00, %xmm4, %xmm0
- pxor %xmm4, %xmm1
- pxor %xmm5, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm7
- movdqa %xmm3, %xmm4
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm7
- pxor %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm4, %xmm1
- psrld $31, %xmm0
- psrld $31, %xmm1
- pslld $0x01, %xmm7
- pslld $0x01, %xmm4
- movdqa %xmm0, %xmm2
- pslldq $4, %xmm0
- psrldq $12, %xmm2
- pslldq $4, %xmm1
- por %xmm2, %xmm4
- por %xmm0, %xmm7
- por %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm7, %xmm1
- movdqa %xmm7, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm7
- movdqa %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm7, %xmm2
- pxor %xmm2, %xmm4
-L_AES_GCM_decrypt_calc_iv_done:
- # T = Encrypt counter
- pxor %xmm0, %xmm0
- shll $3, %edx
- pinsrq $0x00, %rdx, %xmm0
- pxor %xmm0, %xmm4
- pshufd $0x4e, %xmm4, %xmm1
- pshufd $0x4e, %xmm5, %xmm2
- movdqa %xmm5, %xmm3
- movdqa %xmm5, %xmm0
- pclmulqdq $0x11, %xmm4, %xmm3
- pclmulqdq $0x00, %xmm4, %xmm0
- pxor %xmm4, %xmm1
- pxor %xmm5, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm7
- movdqa %xmm3, %xmm4
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm7
- pxor %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm4, %xmm1
- psrld $31, %xmm0
- psrld $31, %xmm1
- pslld $0x01, %xmm7
- pslld $0x01, %xmm4
- movdqa %xmm0, %xmm2
- pslldq $4, %xmm0
- psrldq $12, %xmm2
- pslldq $4, %xmm1
- por %xmm2, %xmm4
- por %xmm0, %xmm7
- por %xmm1, %xmm4
- movdqa %xmm7, %xmm0
- movdqa %xmm7, %xmm1
- movdqa %xmm7, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm7
- movdqa %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm7, %xmm2
- pxor %xmm2, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm4
- # Encrypt counter
- movdqa (%r15), %xmm8
- pxor %xmm4, %xmm8
- aesenc 16(%r15), %xmm8
- aesenc 32(%r15), %xmm8
- aesenc 48(%r15), %xmm8
- aesenc 64(%r15), %xmm8
- aesenc 80(%r15), %xmm8
- aesenc 96(%r15), %xmm8
- aesenc 112(%r15), %xmm8
- aesenc 128(%r15), %xmm8
- aesenc 144(%r15), %xmm8
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm9
- jl L_AES_GCM_decrypt_calc_iv_2_aesenc_avx_last
- aesenc %xmm9, %xmm8
- aesenc 176(%r15), %xmm8
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm9
- jl L_AES_GCM_decrypt_calc_iv_2_aesenc_avx_last
- aesenc %xmm9, %xmm8
- aesenc 208(%r15), %xmm8
- movdqa 224(%r15), %xmm9
-L_AES_GCM_decrypt_calc_iv_2_aesenc_avx_last:
- aesenclast %xmm9, %xmm8
- movdqa %xmm8, 144(%rsp)
-L_AES_GCM_decrypt_iv_done:
- # Additional authentication data
- movl %r11d, %edx
- cmpl $0x00, %edx
- je L_AES_GCM_decrypt_calc_aad_done
- xorl %ecx, %ecx
- cmpl $16, %edx
- jl L_AES_GCM_decrypt_calc_aad_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_decrypt_calc_aad_16_loop:
- movdqu (%r12,%rcx,1), %xmm8
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm8
- pxor %xmm8, %xmm6
- pshufd $0x4e, %xmm6, %xmm1
- pshufd $0x4e, %xmm5, %xmm2
- movdqa %xmm5, %xmm3
- movdqa %xmm5, %xmm0
- pclmulqdq $0x11, %xmm6, %xmm3
- pclmulqdq $0x00, %xmm6, %xmm0
- pxor %xmm6, %xmm1
- pxor %xmm5, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm7
- movdqa %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm7
- pxor %xmm1, %xmm6
- movdqa %xmm7, %xmm0
- movdqa %xmm6, %xmm1
- psrld $31, %xmm0
- psrld $31, %xmm1
- pslld $0x01, %xmm7
- pslld $0x01, %xmm6
- movdqa %xmm0, %xmm2
- pslldq $4, %xmm0
- psrldq $12, %xmm2
- pslldq $4, %xmm1
- por %xmm2, %xmm6
- por %xmm0, %xmm7
- por %xmm1, %xmm6
- movdqa %xmm7, %xmm0
- movdqa %xmm7, %xmm1
- movdqa %xmm7, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm7
- movdqa %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm7, %xmm2
- pxor %xmm2, %xmm6
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_calc_aad_16_loop
- movl %r11d, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_decrypt_calc_aad_done
-L_AES_GCM_decrypt_calc_aad_lt16:
- subq $16, %rsp
- pxor %xmm8, %xmm8
- xorl %ebx, %ebx
- movdqa %xmm8, (%rsp)
-L_AES_GCM_decrypt_calc_aad_loop:
- movzbl (%r12,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_calc_aad_loop
- movdqa (%rsp), %xmm8
- addq $16, %rsp
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm8
- pxor %xmm8, %xmm6
- pshufd $0x4e, %xmm6, %xmm1
- pshufd $0x4e, %xmm5, %xmm2
- movdqa %xmm5, %xmm3
- movdqa %xmm5, %xmm0
- pclmulqdq $0x11, %xmm6, %xmm3
- pclmulqdq $0x00, %xmm6, %xmm0
- pxor %xmm6, %xmm1
- pxor %xmm5, %xmm2
- pclmulqdq $0x00, %xmm2, %xmm1
- pxor %xmm0, %xmm1
- pxor %xmm3, %xmm1
- movdqa %xmm1, %xmm2
- movdqa %xmm0, %xmm7
- movdqa %xmm3, %xmm6
- pslldq $8, %xmm2
- psrldq $8, %xmm1
- pxor %xmm2, %xmm7
- pxor %xmm1, %xmm6
- movdqa %xmm7, %xmm0
- movdqa %xmm6, %xmm1
- psrld $31, %xmm0
- psrld $31, %xmm1
- pslld $0x01, %xmm7
- pslld $0x01, %xmm6
- movdqa %xmm0, %xmm2
- pslldq $4, %xmm0
- psrldq $12, %xmm2
- pslldq $4, %xmm1
- por %xmm2, %xmm6
- por %xmm0, %xmm7
- por %xmm1, %xmm6
- movdqa %xmm7, %xmm0
- movdqa %xmm7, %xmm1
- movdqa %xmm7, %xmm2
- pslld $31, %xmm0
- pslld $30, %xmm1
- pslld $25, %xmm2
- pxor %xmm1, %xmm0
- pxor %xmm2, %xmm0
- movdqa %xmm0, %xmm1
- psrldq $4, %xmm1
- pslldq $12, %xmm0
- pxor %xmm0, %xmm7
- movdqa %xmm7, %xmm2
- movdqa %xmm7, %xmm3
- movdqa %xmm7, %xmm0
- psrld $0x01, %xmm2
- psrld $2, %xmm3
- psrld $7, %xmm0
- pxor %xmm3, %xmm2
- pxor %xmm0, %xmm2
- pxor %xmm1, %xmm2
- pxor %xmm7, %xmm2
- pxor %xmm2, %xmm6
-L_AES_GCM_decrypt_calc_aad_done:
- # Calculate counter and H
- pshufb L_aes_gcm_bswap_epi64(%rip), %xmm4
- movdqa %xmm5, %xmm9
- paddd L_aes_gcm_one(%rip), %xmm4
- movdqa %xmm5, %xmm8
- movdqa %xmm4, 128(%rsp)
- psrlq $63, %xmm9
- psllq $0x01, %xmm8
- pslldq $8, %xmm9
- por %xmm9, %xmm8
- pshufd $0xff, %xmm5, %xmm5
- psrad $31, %xmm5
- pand L_aes_gcm_mod2_128(%rip), %xmm5
- pxor %xmm8, %xmm5
- xorl %ebx, %ebx
- cmpl $0x80, %r9d
- movl %r9d, %r13d
- jl L_AES_GCM_decrypt_done_128
- andl $0xffffff80, %r13d
- movdqa %xmm6, %xmm2
- # H ^ 1
- movdqa %xmm5, (%rsp)
- # H ^ 2
- pshufd $0x4e, %xmm5, %xmm9
- pshufd $0x4e, %xmm5, %xmm10
- movdqa %xmm5, %xmm11
- movdqa %xmm5, %xmm8
- pclmulqdq $0x11, %xmm5, %xmm11
- pclmulqdq $0x00, %xmm5, %xmm8
- pxor %xmm5, %xmm9
- pxor %xmm5, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm0
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm0
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm0
- movdqa %xmm0, 16(%rsp)
- # H ^ 3
- pshufd $0x4e, %xmm5, %xmm9
- pshufd $0x4e, %xmm0, %xmm10
- movdqa %xmm0, %xmm11
- movdqa %xmm0, %xmm8
- pclmulqdq $0x11, %xmm5, %xmm11
- pclmulqdq $0x00, %xmm5, %xmm8
- pxor %xmm5, %xmm9
- pxor %xmm0, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm1
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm1
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm1
- movdqa %xmm1, 32(%rsp)
- # H ^ 4
- pshufd $0x4e, %xmm0, %xmm9
- pshufd $0x4e, %xmm0, %xmm10
- movdqa %xmm0, %xmm11
- movdqa %xmm0, %xmm8
- pclmulqdq $0x11, %xmm0, %xmm11
- pclmulqdq $0x00, %xmm0, %xmm8
- pxor %xmm0, %xmm9
- pxor %xmm0, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm3
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm3
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm3
- movdqa %xmm3, 48(%rsp)
- # H ^ 5
- pshufd $0x4e, %xmm0, %xmm9
- pshufd $0x4e, %xmm1, %xmm10
- movdqa %xmm1, %xmm11
- movdqa %xmm1, %xmm8
- pclmulqdq $0x11, %xmm0, %xmm11
- pclmulqdq $0x00, %xmm0, %xmm8
- pxor %xmm0, %xmm9
- pxor %xmm1, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm7
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm7
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm7
- movdqa %xmm7, 64(%rsp)
- # H ^ 6
- pshufd $0x4e, %xmm1, %xmm9
- pshufd $0x4e, %xmm1, %xmm10
- movdqa %xmm1, %xmm11
- movdqa %xmm1, %xmm8
- pclmulqdq $0x11, %xmm1, %xmm11
- pclmulqdq $0x00, %xmm1, %xmm8
- pxor %xmm1, %xmm9
- pxor %xmm1, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm7
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm7
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm7
- movdqa %xmm7, 80(%rsp)
- # H ^ 7
- pshufd $0x4e, %xmm1, %xmm9
- pshufd $0x4e, %xmm3, %xmm10
- movdqa %xmm3, %xmm11
- movdqa %xmm3, %xmm8
- pclmulqdq $0x11, %xmm1, %xmm11
- pclmulqdq $0x00, %xmm1, %xmm8
- pxor %xmm1, %xmm9
- pxor %xmm3, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm7
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm7
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm7
- movdqa %xmm7, 96(%rsp)
- # H ^ 8
- pshufd $0x4e, %xmm3, %xmm9
- pshufd $0x4e, %xmm3, %xmm10
- movdqa %xmm3, %xmm11
- movdqa %xmm3, %xmm8
- pclmulqdq $0x11, %xmm3, %xmm11
- pclmulqdq $0x00, %xmm3, %xmm8
- pxor %xmm3, %xmm9
- pxor %xmm3, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm7
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm7
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm7
- movdqa %xmm7, 112(%rsp)
-L_AES_GCM_decrypt_ghash_128:
- leaq (%rdi,%rbx,1), %rcx
- leaq (%rsi,%rbx,1), %rdx
- movdqa 128(%rsp), %xmm8
- movdqa L_aes_gcm_bswap_epi64(%rip), %xmm1
- movdqa %xmm8, %xmm0
- pshufb %xmm1, %xmm8
- movdqa %xmm0, %xmm9
- paddd L_aes_gcm_one(%rip), %xmm9
- pshufb %xmm1, %xmm9
- movdqa %xmm0, %xmm10
- paddd L_aes_gcm_two(%rip), %xmm10
- pshufb %xmm1, %xmm10
- movdqa %xmm0, %xmm11
- paddd L_aes_gcm_three(%rip), %xmm11
- pshufb %xmm1, %xmm11
- movdqa %xmm0, %xmm12
- paddd L_aes_gcm_four(%rip), %xmm12
- pshufb %xmm1, %xmm12
- movdqa %xmm0, %xmm13
- paddd L_aes_gcm_five(%rip), %xmm13
- pshufb %xmm1, %xmm13
- movdqa %xmm0, %xmm14
- paddd L_aes_gcm_six(%rip), %xmm14
- pshufb %xmm1, %xmm14
- movdqa %xmm0, %xmm15
- paddd L_aes_gcm_seven(%rip), %xmm15
- pshufb %xmm1, %xmm15
- paddd L_aes_gcm_eight(%rip), %xmm0
- movdqa (%r15), %xmm7
- movdqa %xmm0, 128(%rsp)
- pxor %xmm7, %xmm8
- pxor %xmm7, %xmm9
- pxor %xmm7, %xmm10
- pxor %xmm7, %xmm11
- pxor %xmm7, %xmm12
- pxor %xmm7, %xmm13
- pxor %xmm7, %xmm14
- pxor %xmm7, %xmm15
- movdqa 112(%rsp), %xmm7
- movdqu (%rcx), %xmm0
- aesenc 16(%r15), %xmm8
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- pxor %xmm2, %xmm0
- pshufd $0x4e, %xmm7, %xmm1
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm7, %xmm1
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm3
- pclmulqdq $0x11, %xmm7, %xmm3
- aesenc 16(%r15), %xmm9
- aesenc 16(%r15), %xmm10
- movdqa %xmm0, %xmm2
- pclmulqdq $0x00, %xmm7, %xmm2
- aesenc 16(%r15), %xmm11
- aesenc 16(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm1
- aesenc 16(%r15), %xmm13
- aesenc 16(%r15), %xmm14
- aesenc 16(%r15), %xmm15
- pxor %xmm2, %xmm1
- pxor %xmm3, %xmm1
- movdqa 96(%rsp), %xmm7
- movdqu 16(%rcx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 32(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 32(%r15), %xmm9
- aesenc 32(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 32(%r15), %xmm11
- aesenc 32(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 32(%r15), %xmm13
- aesenc 32(%r15), %xmm14
- aesenc 32(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa 80(%rsp), %xmm7
- movdqu 32(%rcx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 48(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 48(%r15), %xmm9
- aesenc 48(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 48(%r15), %xmm11
- aesenc 48(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 48(%r15), %xmm13
- aesenc 48(%r15), %xmm14
- aesenc 48(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa 64(%rsp), %xmm7
- movdqu 48(%rcx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 64(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 64(%r15), %xmm9
- aesenc 64(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 64(%r15), %xmm11
- aesenc 64(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 64(%r15), %xmm13
- aesenc 64(%r15), %xmm14
- aesenc 64(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa 48(%rsp), %xmm7
- movdqu 64(%rcx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 80(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 80(%r15), %xmm9
- aesenc 80(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 80(%r15), %xmm11
- aesenc 80(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 80(%r15), %xmm13
- aesenc 80(%r15), %xmm14
- aesenc 80(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa 32(%rsp), %xmm7
- movdqu 80(%rcx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 96(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 96(%r15), %xmm9
- aesenc 96(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 96(%r15), %xmm11
- aesenc 96(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 96(%r15), %xmm13
- aesenc 96(%r15), %xmm14
- aesenc 96(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa 16(%rsp), %xmm7
- movdqu 96(%rcx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 112(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 112(%r15), %xmm9
- aesenc 112(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 112(%r15), %xmm11
- aesenc 112(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 112(%r15), %xmm13
- aesenc 112(%r15), %xmm14
- aesenc 112(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa (%rsp), %xmm7
- movdqu 112(%rcx), %xmm0
- pshufd $0x4e, %xmm7, %xmm4
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm0
- aesenc 128(%r15), %xmm8
- pxor %xmm7, %xmm4
- pshufd $0x4e, %xmm0, %xmm5
- pxor %xmm0, %xmm5
- movdqa %xmm0, %xmm6
- pclmulqdq $0x11, %xmm7, %xmm6
- aesenc 128(%r15), %xmm9
- aesenc 128(%r15), %xmm10
- pclmulqdq $0x00, %xmm0, %xmm7
- aesenc 128(%r15), %xmm11
- aesenc 128(%r15), %xmm12
- pclmulqdq $0x00, %xmm5, %xmm4
- aesenc 128(%r15), %xmm13
- aesenc 128(%r15), %xmm14
- aesenc 128(%r15), %xmm15
- pxor %xmm7, %xmm1
- pxor %xmm7, %xmm2
- pxor %xmm6, %xmm1
- pxor %xmm6, %xmm3
- pxor %xmm4, %xmm1
- movdqa %xmm1, %xmm5
- psrldq $8, %xmm1
- pslldq $8, %xmm5
- aesenc 144(%r15), %xmm8
- pxor %xmm5, %xmm2
- pxor %xmm1, %xmm3
- movdqa %xmm2, %xmm7
- movdqa %xmm2, %xmm4
- movdqa %xmm2, %xmm5
- aesenc 144(%r15), %xmm9
- pslld $31, %xmm7
- pslld $30, %xmm4
- pslld $25, %xmm5
- aesenc 144(%r15), %xmm10
- pxor %xmm4, %xmm7
- pxor %xmm5, %xmm7
- aesenc 144(%r15), %xmm11
- movdqa %xmm7, %xmm4
- pslldq $12, %xmm7
- psrldq $4, %xmm4
- aesenc 144(%r15), %xmm12
- pxor %xmm7, %xmm2
- movdqa %xmm2, %xmm5
- movdqa %xmm2, %xmm1
- movdqa %xmm2, %xmm0
- aesenc 144(%r15), %xmm13
- psrld $0x01, %xmm5
- psrld $2, %xmm1
- psrld $7, %xmm0
- aesenc 144(%r15), %xmm14
- pxor %xmm1, %xmm5
- pxor %xmm0, %xmm5
- aesenc 144(%r15), %xmm15
- pxor %xmm4, %xmm5
- pxor %xmm5, %xmm2
- pxor %xmm3, %xmm2
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm7
- jl L_AES_GCM_decrypt_aesenc_128_ghash_avx_done
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 176(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm7
- jl L_AES_GCM_decrypt_aesenc_128_ghash_avx_done
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 208(%r15), %xmm7
- aesenc %xmm7, %xmm8
- aesenc %xmm7, %xmm9
- aesenc %xmm7, %xmm10
- aesenc %xmm7, %xmm11
- aesenc %xmm7, %xmm12
- aesenc %xmm7, %xmm13
- aesenc %xmm7, %xmm14
- aesenc %xmm7, %xmm15
- movdqa 224(%r15), %xmm7
-L_AES_GCM_decrypt_aesenc_128_ghash_avx_done:
- aesenclast %xmm7, %xmm8
- aesenclast %xmm7, %xmm9
- movdqu (%rcx), %xmm0
- movdqu 16(%rcx), %xmm1
- pxor %xmm0, %xmm8
- pxor %xmm1, %xmm9
- movdqu %xmm8, (%rdx)
- movdqu %xmm9, 16(%rdx)
- aesenclast %xmm7, %xmm10
- aesenclast %xmm7, %xmm11
- movdqu 32(%rcx), %xmm0
- movdqu 48(%rcx), %xmm1
- pxor %xmm0, %xmm10
- pxor %xmm1, %xmm11
- movdqu %xmm10, 32(%rdx)
- movdqu %xmm11, 48(%rdx)
- aesenclast %xmm7, %xmm12
- aesenclast %xmm7, %xmm13
- movdqu 64(%rcx), %xmm0
- movdqu 80(%rcx), %xmm1
- pxor %xmm0, %xmm12
- pxor %xmm1, %xmm13
- movdqu %xmm12, 64(%rdx)
- movdqu %xmm13, 80(%rdx)
- aesenclast %xmm7, %xmm14
- aesenclast %xmm7, %xmm15
- movdqu 96(%rcx), %xmm0
- movdqu 112(%rcx), %xmm1
- pxor %xmm0, %xmm14
- pxor %xmm1, %xmm15
- movdqu %xmm14, 96(%rdx)
- movdqu %xmm15, 112(%rdx)
- addl $0x80, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_decrypt_ghash_128
- movdqa %xmm2, %xmm6
- movdqa (%rsp), %xmm5
-L_AES_GCM_decrypt_done_128:
- movl %r9d, %edx
- cmpl %edx, %ebx
- jge L_AES_GCM_decrypt_done_dec
- movl %r9d, %r13d
- andl $0xfffffff0, %r13d
- cmpl %r13d, %ebx
- jge L_AES_GCM_decrypt_last_block_done
-L_AES_GCM_decrypt_last_block_start:
- leaq (%rdi,%rbx,1), %rcx
- leaq (%rsi,%rbx,1), %rdx
- movdqu (%rcx), %xmm1
- movdqa %xmm5, %xmm0
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm1
- pxor %xmm6, %xmm1
- movdqa 128(%rsp), %xmm8
- movdqa %xmm8, %xmm9
- pshufb L_aes_gcm_bswap_epi64(%rip), %xmm8
- paddd L_aes_gcm_one(%rip), %xmm9
- pxor (%r15), %xmm8
- movdqa %xmm9, 128(%rsp)
- movdqa %xmm1, %xmm10
- pclmulqdq $16, %xmm0, %xmm10
- aesenc 16(%r15), %xmm8
- aesenc 32(%r15), %xmm8
- movdqa %xmm1, %xmm11
- pclmulqdq $0x01, %xmm0, %xmm11
- aesenc 48(%r15), %xmm8
- aesenc 64(%r15), %xmm8
- movdqa %xmm1, %xmm12
- pclmulqdq $0x00, %xmm0, %xmm12
- aesenc 80(%r15), %xmm8
- movdqa %xmm1, %xmm1
- pclmulqdq $0x11, %xmm0, %xmm1
- aesenc 96(%r15), %xmm8
- pxor %xmm11, %xmm10
- movdqa %xmm10, %xmm2
- psrldq $8, %xmm10
- pslldq $8, %xmm2
- aesenc 112(%r15), %xmm8
- movdqa %xmm1, %xmm3
- pxor %xmm12, %xmm2
- pxor %xmm10, %xmm3
- movdqa L_aes_gcm_mod2_128(%rip), %xmm0
- movdqa %xmm2, %xmm11
- pclmulqdq $16, %xmm0, %xmm11
- aesenc 128(%r15), %xmm8
- pshufd $0x4e, %xmm2, %xmm10
- pxor %xmm11, %xmm10
- movdqa %xmm10, %xmm11
- pclmulqdq $16, %xmm0, %xmm11
- aesenc 144(%r15), %xmm8
- pshufd $0x4e, %xmm10, %xmm6
- pxor %xmm11, %xmm6
- pxor %xmm3, %xmm6
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm9
- jl L_AES_GCM_decrypt_aesenc_gfmul_last
- aesenc %xmm9, %xmm8
- aesenc 176(%r15), %xmm8
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm9
- jl L_AES_GCM_decrypt_aesenc_gfmul_last
- aesenc %xmm9, %xmm8
- aesenc 208(%r15), %xmm8
- movdqa 224(%r15), %xmm9
-L_AES_GCM_decrypt_aesenc_gfmul_last:
- aesenclast %xmm9, %xmm8
- movdqu (%rcx), %xmm9
- pxor %xmm9, %xmm8
- movdqu %xmm8, (%rdx)
- addl $16, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_decrypt_last_block_start
-L_AES_GCM_decrypt_last_block_done:
- movl %r9d, %ecx
- movl %ecx, %edx
- andl $15, %ecx
- jz L_AES_GCM_decrypt_aesenc_last15_dec_avx_done
- movdqa 128(%rsp), %xmm4
- pshufb L_aes_gcm_bswap_epi64(%rip), %xmm4
- pxor (%r15), %xmm4
- aesenc 16(%r15), %xmm4
- aesenc 32(%r15), %xmm4
- aesenc 48(%r15), %xmm4
- aesenc 64(%r15), %xmm4
- aesenc 80(%r15), %xmm4
- aesenc 96(%r15), %xmm4
- aesenc 112(%r15), %xmm4
- aesenc 128(%r15), %xmm4
- aesenc 144(%r15), %xmm4
- cmpl $11, %r10d
- movdqa 160(%r15), %xmm9
- jl L_AES_GCM_decrypt_aesenc_last15_dec_avx_aesenc_avx_last
- aesenc %xmm9, %xmm4
- aesenc 176(%r15), %xmm4
- cmpl $13, %r10d
- movdqa 192(%r15), %xmm9
- jl L_AES_GCM_decrypt_aesenc_last15_dec_avx_aesenc_avx_last
- aesenc %xmm9, %xmm4
- aesenc 208(%r15), %xmm4
- movdqa 224(%r15), %xmm9
-L_AES_GCM_decrypt_aesenc_last15_dec_avx_aesenc_avx_last:
- aesenclast %xmm9, %xmm4
- subq $32, %rsp
- xorl %ecx, %ecx
- movdqa %xmm4, (%rsp)
- pxor %xmm0, %xmm0
- movdqa %xmm0, 16(%rsp)
-L_AES_GCM_decrypt_aesenc_last15_dec_avx_loop:
- movzbl (%rdi,%rbx,1), %r13d
- movb %r13b, 16(%rsp,%rcx,1)
- xorb (%rsp,%rcx,1), %r13b
- movb %r13b, (%rsi,%rbx,1)
- incl %ebx
- incl %ecx
- cmpl %edx, %ebx
- jl L_AES_GCM_decrypt_aesenc_last15_dec_avx_loop
- movdqa 16(%rsp), %xmm4
- addq $32, %rsp
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm4
- pxor %xmm4, %xmm6
- pshufd $0x4e, %xmm5, %xmm9
- pshufd $0x4e, %xmm6, %xmm10
- movdqa %xmm6, %xmm11
- movdqa %xmm6, %xmm8
- pclmulqdq $0x11, %xmm5, %xmm11
- pclmulqdq $0x00, %xmm5, %xmm8
- pxor %xmm5, %xmm9
- pxor %xmm6, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm6
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm6
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm6
-L_AES_GCM_decrypt_aesenc_last15_dec_avx_done:
-L_AES_GCM_decrypt_done_dec:
- movl %r9d, %edx
- movl %r11d, %ecx
- shlq $3, %rdx
- shlq $3, %rcx
- pinsrq $0x00, %rdx, %xmm0
- pinsrq $0x01, %rcx, %xmm0
- pxor %xmm0, %xmm6
- pshufd $0x4e, %xmm5, %xmm9
- pshufd $0x4e, %xmm6, %xmm10
- movdqa %xmm6, %xmm11
- movdqa %xmm6, %xmm8
- pclmulqdq $0x11, %xmm5, %xmm11
- pclmulqdq $0x00, %xmm5, %xmm8
- pxor %xmm5, %xmm9
- pxor %xmm6, %xmm10
- pclmulqdq $0x00, %xmm10, %xmm9
- pxor %xmm8, %xmm9
- pxor %xmm11, %xmm9
- movdqa %xmm9, %xmm10
- movdqa %xmm11, %xmm6
- pslldq $8, %xmm10
- psrldq $8, %xmm9
- pxor %xmm10, %xmm8
- pxor %xmm9, %xmm6
- movdqa %xmm8, %xmm12
- movdqa %xmm8, %xmm13
- movdqa %xmm8, %xmm14
- pslld $31, %xmm12
- pslld $30, %xmm13
- pslld $25, %xmm14
- pxor %xmm13, %xmm12
- pxor %xmm14, %xmm12
- movdqa %xmm12, %xmm13
- psrldq $4, %xmm13
- pslldq $12, %xmm12
- pxor %xmm12, %xmm8
- movdqa %xmm8, %xmm14
- movdqa %xmm8, %xmm10
- movdqa %xmm8, %xmm9
- psrld $0x01, %xmm14
- psrld $2, %xmm10
- psrld $7, %xmm9
- pxor %xmm10, %xmm14
- pxor %xmm9, %xmm14
- pxor %xmm13, %xmm14
- pxor %xmm8, %xmm14
- pxor %xmm14, %xmm6
- pshufb L_aes_gcm_bswap_mask(%rip), %xmm6
- movdqa 144(%rsp), %xmm0
- pxor %xmm6, %xmm0
- cmpl $16, %r14d
- je L_AES_GCM_decrypt_cmp_tag_16
- subq $16, %rsp
- xorq %rcx, %rcx
- xorq %rbx, %rbx
- movdqa %xmm0, (%rsp)
-L_AES_GCM_decrypt_cmp_tag_loop:
- movzbl (%rsp,%rcx,1), %r13d
- xorb (%r8,%rcx,1), %r13b
- orb %r13b, %bl
- incl %ecx
- cmpl %r14d, %ecx
- jne L_AES_GCM_decrypt_cmp_tag_loop
- cmpb $0x00, %bl
- sete %bl
- addq $16, %rsp
- xorq %rcx, %rcx
- jmp L_AES_GCM_decrypt_cmp_tag_done
-L_AES_GCM_decrypt_cmp_tag_16:
- movdqu (%r8), %xmm1
- pcmpeqb %xmm1, %xmm0
- pmovmskb %xmm0, %rdx
- # %%edx == 0xFFFF then return 1 else => return 0
- xorl %ebx, %ebx
- cmpl $0xffff, %edx
- sete %bl
-L_AES_GCM_decrypt_cmp_tag_done:
- movl %ebx, (%rbp)
- addq $0xa8, %rsp
- popq %rbp
- popq %r15
- popq %r14
- popq %rbx
- popq %r12
- popq %r13
- repz retq
-#ifndef __APPLE__
-.size AES_GCM_decrypt,.-AES_GCM_decrypt
-#endif /* __APPLE__ */
-#ifdef HAVE_INTEL_AVX1
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_one:
-.quad 0x0, 0x1
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_two:
-.quad 0x0, 0x2
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_three:
-.quad 0x0, 0x3
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_four:
-.quad 0x0, 0x4
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_five:
-.quad 0x0, 0x5
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_six:
-.quad 0x0, 0x6
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_seven:
-.quad 0x0, 0x7
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_eight:
-.quad 0x0, 0x8
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_bswap_epi64:
-.quad 0x1020304050607, 0x8090a0b0c0d0e0f
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_bswap_mask:
-.quad 0x8090a0b0c0d0e0f, 0x1020304050607
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx1_aes_gcm_mod2_128:
-.quad 0x1, 0xc200000000000000
-#ifndef __APPLE__
-.text
-.globl AES_GCM_encrypt_avx1
-.type AES_GCM_encrypt_avx1,@function
-.align 4
-AES_GCM_encrypt_avx1:
-#else
-.section __TEXT,__text
-.globl _AES_GCM_encrypt_avx1
-.p2align 2
-_AES_GCM_encrypt_avx1:
-#endif /* __APPLE__ */
- pushq %r13
- pushq %r12
- pushq %rbx
- pushq %r14
- pushq %r15
- movq %rdx, %r12
- movq %rcx, %rax
- movl 48(%rsp), %r11d
- movl 56(%rsp), %ebx
- movl 64(%rsp), %r14d
- movq 72(%rsp), %r15
- movl 80(%rsp), %r10d
- subq $0xa0, %rsp
- vpxor %xmm4, %xmm4, %xmm4
- vpxor %xmm6, %xmm6, %xmm6
- movl %ebx, %edx
- cmpl $12, %edx
- jne L_AES_GCM_encrypt_avx1_iv_not_12
- # # Calculate values when IV is 12 bytes
- # Set counter based on IV
- movl $0x1000000, %ecx
- vpinsrq $0x00, (%rax), %xmm4, %xmm4
- vpinsrd $2, 8(%rax), %xmm4, %xmm4
- vpinsrd $3, %ecx, %xmm4, %xmm4
- # H = Encrypt X(=0) and T = Encrypt counter
- vmovdqa (%r15), %xmm5
- vpxor %xmm5, %xmm4, %xmm1
- vmovdqa 16(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 32(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 48(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 64(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 80(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 96(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 112(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 128(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 144(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm7
- jl L_AES_GCM_encrypt_avx1_calc_iv_12_last
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 176(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm7
- jl L_AES_GCM_encrypt_avx1_calc_iv_12_last
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 208(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 224(%r15), %xmm7
-L_AES_GCM_encrypt_avx1_calc_iv_12_last:
- vaesenclast %xmm7, %xmm5, %xmm5
- vaesenclast %xmm7, %xmm1, %xmm1
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm5, %xmm5
- vmovdqa %xmm1, 144(%rsp)
- jmp L_AES_GCM_encrypt_avx1_iv_done
-L_AES_GCM_encrypt_avx1_iv_not_12:
- # Calculate values when IV is not 12 bytes
- # H = Encrypt X(=0)
- vmovdqa (%r15), %xmm5
- vaesenc 16(%r15), %xmm5, %xmm5
- vaesenc 32(%r15), %xmm5, %xmm5
- vaesenc 48(%r15), %xmm5, %xmm5
- vaesenc 64(%r15), %xmm5, %xmm5
- vaesenc 80(%r15), %xmm5, %xmm5
- vaesenc 96(%r15), %xmm5, %xmm5
- vaesenc 112(%r15), %xmm5, %xmm5
- vaesenc 128(%r15), %xmm5, %xmm5
- vaesenc 144(%r15), %xmm5, %xmm5
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm9
- jl L_AES_GCM_encrypt_avx1_calc_iv_1_aesenc_avx_last
- vaesenc %xmm9, %xmm5, %xmm5
- vaesenc 176(%r15), %xmm5, %xmm5
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm9
- jl L_AES_GCM_encrypt_avx1_calc_iv_1_aesenc_avx_last
- vaesenc %xmm9, %xmm5, %xmm5
- vaesenc 208(%r15), %xmm5, %xmm5
- vmovdqa 224(%r15), %xmm9
-L_AES_GCM_encrypt_avx1_calc_iv_1_aesenc_avx_last:
- vaesenclast %xmm9, %xmm5, %xmm5
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm5, %xmm5
- # Calc counter
- # Initialization vector
- cmpl $0x00, %edx
- movq $0x00, %rcx
- je L_AES_GCM_encrypt_avx1_calc_iv_done
- cmpl $16, %edx
- jl L_AES_GCM_encrypt_avx1_calc_iv_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_encrypt_avx1_calc_iv_16_loop:
- vmovdqu (%rax,%rcx,1), %xmm8
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm8, %xmm8
- vpxor %xmm8, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm4, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpxor %xmm4, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm7
- vmovdqa %xmm3, %xmm4
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm7, %xmm7
- vpxor %xmm1, %xmm4, %xmm4
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- vpslld $31, %xmm7, %xmm0
- vpslld $30, %xmm7, %xmm1
- vpslld $25, %xmm7, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm7, %xmm7
- vpsrld $0x01, %xmm7, %xmm2
- vpsrld $2, %xmm7, %xmm3
- vpsrld $7, %xmm7, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm4, %xmm4
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_avx1_calc_iv_16_loop
- movl %ebx, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_encrypt_avx1_calc_iv_done
-L_AES_GCM_encrypt_avx1_calc_iv_lt16:
- subq $16, %rsp
- vpxor %xmm8, %xmm8, %xmm8
- xorl %ebx, %ebx
- vmovdqa %xmm8, (%rsp)
-L_AES_GCM_encrypt_avx1_calc_iv_loop:
- movzbl (%rax,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_avx1_calc_iv_loop
- vmovdqa (%rsp), %xmm8
- addq $16, %rsp
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm8, %xmm8
- vpxor %xmm8, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm4, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpxor %xmm4, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm7
- vmovdqa %xmm3, %xmm4
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm7, %xmm7
- vpxor %xmm1, %xmm4, %xmm4
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- vpslld $31, %xmm7, %xmm0
- vpslld $30, %xmm7, %xmm1
- vpslld $25, %xmm7, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm7, %xmm7
- vpsrld $0x01, %xmm7, %xmm2
- vpsrld $2, %xmm7, %xmm3
- vpsrld $7, %xmm7, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm4, %xmm4
-L_AES_GCM_encrypt_avx1_calc_iv_done:
- # T = Encrypt counter
- vpxor %xmm0, %xmm0, %xmm0
- shll $3, %edx
- vpinsrq $0x00, %rdx, %xmm0, %xmm0
- vpxor %xmm0, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm4, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpxor %xmm4, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm7
- vmovdqa %xmm3, %xmm4
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm7, %xmm7
- vpxor %xmm1, %xmm4, %xmm4
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- vpslld $31, %xmm7, %xmm0
- vpslld $30, %xmm7, %xmm1
- vpslld $25, %xmm7, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm7, %xmm7
- vpsrld $0x01, %xmm7, %xmm2
- vpsrld $2, %xmm7, %xmm3
- vpsrld $7, %xmm7, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm4, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm4, %xmm4
- # Encrypt counter
- vmovdqa (%r15), %xmm8
- vpxor %xmm4, %xmm8, %xmm8
- vaesenc 16(%r15), %xmm8, %xmm8
- vaesenc 32(%r15), %xmm8, %xmm8
- vaesenc 48(%r15), %xmm8, %xmm8
- vaesenc 64(%r15), %xmm8, %xmm8
- vaesenc 80(%r15), %xmm8, %xmm8
- vaesenc 96(%r15), %xmm8, %xmm8
- vaesenc 112(%r15), %xmm8, %xmm8
- vaesenc 128(%r15), %xmm8, %xmm8
- vaesenc 144(%r15), %xmm8, %xmm8
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm9
- jl L_AES_GCM_encrypt_avx1_calc_iv_2_aesenc_avx_last
- vaesenc %xmm9, %xmm8, %xmm8
- vaesenc 176(%r15), %xmm8, %xmm8
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm9
- jl L_AES_GCM_encrypt_avx1_calc_iv_2_aesenc_avx_last
- vaesenc %xmm9, %xmm8, %xmm8
- vaesenc 208(%r15), %xmm8, %xmm8
- vmovdqa 224(%r15), %xmm9
-L_AES_GCM_encrypt_avx1_calc_iv_2_aesenc_avx_last:
- vaesenclast %xmm9, %xmm8, %xmm8
- vmovdqa %xmm8, 144(%rsp)
-L_AES_GCM_encrypt_avx1_iv_done:
- # Additional authentication data
- movl %r11d, %edx
- cmpl $0x00, %edx
- je L_AES_GCM_encrypt_avx1_calc_aad_done
- xorl %ecx, %ecx
- cmpl $16, %edx
- jl L_AES_GCM_encrypt_avx1_calc_aad_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_encrypt_avx1_calc_aad_16_loop:
- vmovdqu (%r12,%rcx,1), %xmm8
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm8, %xmm8
- vpxor %xmm8, %xmm6, %xmm6
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm6, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm6, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm6, %xmm5, %xmm0
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm7
- vmovdqa %xmm3, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm7, %xmm7
- vpxor %xmm1, %xmm6, %xmm6
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm6, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm6, %xmm6
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm6, %xmm6
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm6, %xmm6
- vpslld $31, %xmm7, %xmm0
- vpslld $30, %xmm7, %xmm1
- vpslld $25, %xmm7, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm7, %xmm7
- vpsrld $0x01, %xmm7, %xmm2
- vpsrld $2, %xmm7, %xmm3
- vpsrld $7, %xmm7, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm6, %xmm6
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_avx1_calc_aad_16_loop
- movl %r11d, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_encrypt_avx1_calc_aad_done
-L_AES_GCM_encrypt_avx1_calc_aad_lt16:
- subq $16, %rsp
- vpxor %xmm8, %xmm8, %xmm8
- xorl %ebx, %ebx
- vmovdqa %xmm8, (%rsp)
-L_AES_GCM_encrypt_avx1_calc_aad_loop:
- movzbl (%r12,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_avx1_calc_aad_loop
- vmovdqa (%rsp), %xmm8
- addq $16, %rsp
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm8, %xmm8
- vpxor %xmm8, %xmm6, %xmm6
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm6, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm6, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm6, %xmm5, %xmm0
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm7
- vmovdqa %xmm3, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm7, %xmm7
- vpxor %xmm1, %xmm6, %xmm6
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm6, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm6, %xmm6
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm6, %xmm6
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm6, %xmm6
- vpslld $31, %xmm7, %xmm0
- vpslld $30, %xmm7, %xmm1
- vpslld $25, %xmm7, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm7, %xmm7
- vpsrld $0x01, %xmm7, %xmm2
- vpsrld $2, %xmm7, %xmm3
- vpsrld $7, %xmm7, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm6, %xmm6
-L_AES_GCM_encrypt_avx1_calc_aad_done:
- # Calculate counter and H
- vpsrlq $63, %xmm5, %xmm9
- vpsllq $0x01, %xmm5, %xmm8
- vpslldq $8, %xmm9, %xmm9
- vpor %xmm9, %xmm8, %xmm8
- vpshufd $0xff, %xmm5, %xmm5
- vpsrad $31, %xmm5, %xmm5
- vpshufb L_avx1_aes_gcm_bswap_epi64(%rip), %xmm4, %xmm4
- vpand L_avx1_aes_gcm_mod2_128(%rip), %xmm5, %xmm5
- vpaddd L_avx1_aes_gcm_one(%rip), %xmm4, %xmm4
- vpxor %xmm8, %xmm5, %xmm5
- vmovdqa %xmm4, 128(%rsp)
- xorl %ebx, %ebx
- cmpl $0x80, %r9d
- movl %r9d, %r13d
- jl L_AES_GCM_encrypt_avx1_done_128
- andl $0xffffff80, %r13d
- vmovdqa %xmm6, %xmm2
- # H ^ 1
- vmovdqa %xmm5, (%rsp)
- # H ^ 2
- vpclmulqdq $0x00, %xmm5, %xmm5, %xmm8
- vpclmulqdq $0x11, %xmm5, %xmm5, %xmm0
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm0, %xmm0
- vmovdqa %xmm0, 16(%rsp)
- # H ^ 3
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm5, %xmm9
- vpshufd $0x4e, %xmm0, %xmm10
- vpclmulqdq $0x11, %xmm5, %xmm0, %xmm11
- vpclmulqdq $0x00, %xmm5, %xmm0, %xmm8
- vpxor %xmm5, %xmm9, %xmm9
- vpxor %xmm0, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm1
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm1, %xmm1
- vmovdqa %xmm1, 32(%rsp)
- # H ^ 4
- vpclmulqdq $0x00, %xmm0, %xmm0, %xmm8
- vpclmulqdq $0x11, %xmm0, %xmm0, %xmm3
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm3, %xmm3
- vmovdqa %xmm3, 48(%rsp)
- # H ^ 5
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm0, %xmm9
- vpshufd $0x4e, %xmm1, %xmm10
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm11
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm8
- vpxor %xmm0, %xmm9, %xmm9
- vpxor %xmm1, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm7
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm7, %xmm7
- vmovdqa %xmm7, 64(%rsp)
- # H ^ 6
- vpclmulqdq $0x00, %xmm1, %xmm1, %xmm8
- vpclmulqdq $0x11, %xmm1, %xmm1, %xmm7
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm7, %xmm7
- vmovdqa %xmm7, 80(%rsp)
- # H ^ 7
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm1, %xmm9
- vpshufd $0x4e, %xmm3, %xmm10
- vpclmulqdq $0x11, %xmm1, %xmm3, %xmm11
- vpclmulqdq $0x00, %xmm1, %xmm3, %xmm8
- vpxor %xmm1, %xmm9, %xmm9
- vpxor %xmm3, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm7
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm7, %xmm7
- vmovdqa %xmm7, 96(%rsp)
- # H ^ 8
- vpclmulqdq $0x00, %xmm3, %xmm3, %xmm8
- vpclmulqdq $0x11, %xmm3, %xmm3, %xmm7
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm7, %xmm7
- vmovdqa %xmm7, 112(%rsp)
- # First 128 bytes of input
- vmovdqa 128(%rsp), %xmm0
- vmovdqa L_avx1_aes_gcm_bswap_epi64(%rip), %xmm1
- vpshufb %xmm1, %xmm0, %xmm8
- vpaddd L_avx1_aes_gcm_one(%rip), %xmm0, %xmm9
- vpshufb %xmm1, %xmm9, %xmm9
- vpaddd L_avx1_aes_gcm_two(%rip), %xmm0, %xmm10
- vpshufb %xmm1, %xmm10, %xmm10
- vpaddd L_avx1_aes_gcm_three(%rip), %xmm0, %xmm11
- vpshufb %xmm1, %xmm11, %xmm11
- vpaddd L_avx1_aes_gcm_four(%rip), %xmm0, %xmm12
- vpshufb %xmm1, %xmm12, %xmm12
- vpaddd L_avx1_aes_gcm_five(%rip), %xmm0, %xmm13
- vpshufb %xmm1, %xmm13, %xmm13
- vpaddd L_avx1_aes_gcm_six(%rip), %xmm0, %xmm14
- vpshufb %xmm1, %xmm14, %xmm14
- vpaddd L_avx1_aes_gcm_seven(%rip), %xmm0, %xmm15
- vpshufb %xmm1, %xmm15, %xmm15
- vpaddd L_avx1_aes_gcm_eight(%rip), %xmm0, %xmm0
- vmovdqa (%r15), %xmm7
- vmovdqa %xmm0, 128(%rsp)
- vpxor %xmm7, %xmm8, %xmm8
- vpxor %xmm7, %xmm9, %xmm9
- vpxor %xmm7, %xmm10, %xmm10
- vpxor %xmm7, %xmm11, %xmm11
- vpxor %xmm7, %xmm12, %xmm12
- vpxor %xmm7, %xmm13, %xmm13
- vpxor %xmm7, %xmm14, %xmm14
- vpxor %xmm7, %xmm15, %xmm15
- vmovdqa 16(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 32(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 48(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 64(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 80(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 96(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 112(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 128(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 144(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm7
- jl L_AES_GCM_encrypt_avx1_aesenc_128_enc_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 176(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm7
- jl L_AES_GCM_encrypt_avx1_aesenc_128_enc_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 208(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 224(%r15), %xmm7
-L_AES_GCM_encrypt_avx1_aesenc_128_enc_done:
- vaesenclast %xmm7, %xmm8, %xmm8
- vaesenclast %xmm7, %xmm9, %xmm9
- vmovdqu (%rdi), %xmm0
- vmovdqu 16(%rdi), %xmm1
- vpxor %xmm0, %xmm8, %xmm8
- vpxor %xmm1, %xmm9, %xmm9
- vmovdqu %xmm8, (%rsi)
- vmovdqu %xmm9, 16(%rsi)
- vaesenclast %xmm7, %xmm10, %xmm10
- vaesenclast %xmm7, %xmm11, %xmm11
- vmovdqu 32(%rdi), %xmm0
- vmovdqu 48(%rdi), %xmm1
- vpxor %xmm0, %xmm10, %xmm10
- vpxor %xmm1, %xmm11, %xmm11
- vmovdqu %xmm10, 32(%rsi)
- vmovdqu %xmm11, 48(%rsi)
- vaesenclast %xmm7, %xmm12, %xmm12
- vaesenclast %xmm7, %xmm13, %xmm13
- vmovdqu 64(%rdi), %xmm0
- vmovdqu 80(%rdi), %xmm1
- vpxor %xmm0, %xmm12, %xmm12
- vpxor %xmm1, %xmm13, %xmm13
- vmovdqu %xmm12, 64(%rsi)
- vmovdqu %xmm13, 80(%rsi)
- vaesenclast %xmm7, %xmm14, %xmm14
- vaesenclast %xmm7, %xmm15, %xmm15
- vmovdqu 96(%rdi), %xmm0
- vmovdqu 112(%rdi), %xmm1
- vpxor %xmm0, %xmm14, %xmm14
- vpxor %xmm1, %xmm15, %xmm15
- vmovdqu %xmm14, 96(%rsi)
- vmovdqu %xmm15, 112(%rsi)
- cmpl $0x80, %r13d
- movl $0x80, %ebx
- jle L_AES_GCM_encrypt_avx1_end_128
- # More 128 bytes of input
-L_AES_GCM_encrypt_avx1_ghash_128:
- leaq (%rdi,%rbx,1), %rcx
- leaq (%rsi,%rbx,1), %rdx
- vmovdqa 128(%rsp), %xmm0
- vmovdqa L_avx1_aes_gcm_bswap_epi64(%rip), %xmm1
- vpshufb %xmm1, %xmm0, %xmm8
- vpaddd L_avx1_aes_gcm_one(%rip), %xmm0, %xmm9
- vpshufb %xmm1, %xmm9, %xmm9
- vpaddd L_avx1_aes_gcm_two(%rip), %xmm0, %xmm10
- vpshufb %xmm1, %xmm10, %xmm10
- vpaddd L_avx1_aes_gcm_three(%rip), %xmm0, %xmm11
- vpshufb %xmm1, %xmm11, %xmm11
- vpaddd L_avx1_aes_gcm_four(%rip), %xmm0, %xmm12
- vpshufb %xmm1, %xmm12, %xmm12
- vpaddd L_avx1_aes_gcm_five(%rip), %xmm0, %xmm13
- vpshufb %xmm1, %xmm13, %xmm13
- vpaddd L_avx1_aes_gcm_six(%rip), %xmm0, %xmm14
- vpshufb %xmm1, %xmm14, %xmm14
- vpaddd L_avx1_aes_gcm_seven(%rip), %xmm0, %xmm15
- vpshufb %xmm1, %xmm15, %xmm15
- vpaddd L_avx1_aes_gcm_eight(%rip), %xmm0, %xmm0
- vmovdqa (%r15), %xmm7
- vmovdqa %xmm0, 128(%rsp)
- vpxor %xmm7, %xmm8, %xmm8
- vpxor %xmm7, %xmm9, %xmm9
- vpxor %xmm7, %xmm10, %xmm10
- vpxor %xmm7, %xmm11, %xmm11
- vpxor %xmm7, %xmm12, %xmm12
- vpxor %xmm7, %xmm13, %xmm13
- vpxor %xmm7, %xmm14, %xmm14
- vpxor %xmm7, %xmm15, %xmm15
- vmovdqa 112(%rsp), %xmm7
- vmovdqu -128(%rdx), %xmm0
- vaesenc 16(%r15), %xmm8, %xmm8
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm3
- vaesenc 16(%r15), %xmm9, %xmm9
- vaesenc 16(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm2
- vaesenc 16(%r15), %xmm11, %xmm11
- vaesenc 16(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm1, %xmm1
- vaesenc 16(%r15), %xmm13, %xmm13
- vaesenc 16(%r15), %xmm14, %xmm14
- vaesenc 16(%r15), %xmm15, %xmm15
- vpxor %xmm2, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa 96(%rsp), %xmm7
- vmovdqu -112(%rdx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 32(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 32(%r15), %xmm9, %xmm9
- vaesenc 32(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 32(%r15), %xmm11, %xmm11
- vaesenc 32(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 32(%r15), %xmm13, %xmm13
- vaesenc 32(%r15), %xmm14, %xmm14
- vaesenc 32(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa 80(%rsp), %xmm7
- vmovdqu -96(%rdx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 48(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 48(%r15), %xmm9, %xmm9
- vaesenc 48(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 48(%r15), %xmm11, %xmm11
- vaesenc 48(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 48(%r15), %xmm13, %xmm13
- vaesenc 48(%r15), %xmm14, %xmm14
- vaesenc 48(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa 64(%rsp), %xmm7
- vmovdqu -80(%rdx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 64(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 64(%r15), %xmm9, %xmm9
- vaesenc 64(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 64(%r15), %xmm11, %xmm11
- vaesenc 64(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 64(%r15), %xmm13, %xmm13
- vaesenc 64(%r15), %xmm14, %xmm14
- vaesenc 64(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa 48(%rsp), %xmm7
- vmovdqu -64(%rdx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 80(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 80(%r15), %xmm9, %xmm9
- vaesenc 80(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 80(%r15), %xmm11, %xmm11
- vaesenc 80(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 80(%r15), %xmm13, %xmm13
- vaesenc 80(%r15), %xmm14, %xmm14
- vaesenc 80(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa 32(%rsp), %xmm7
- vmovdqu -48(%rdx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 96(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 96(%r15), %xmm9, %xmm9
- vaesenc 96(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 96(%r15), %xmm11, %xmm11
- vaesenc 96(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 96(%r15), %xmm13, %xmm13
- vaesenc 96(%r15), %xmm14, %xmm14
- vaesenc 96(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa 16(%rsp), %xmm7
- vmovdqu -32(%rdx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 112(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 112(%r15), %xmm9, %xmm9
- vaesenc 112(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 112(%r15), %xmm11, %xmm11
- vaesenc 112(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 112(%r15), %xmm13, %xmm13
- vaesenc 112(%r15), %xmm14, %xmm14
- vaesenc 112(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa (%rsp), %xmm7
- vmovdqu -16(%rdx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 128(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 128(%r15), %xmm9, %xmm9
- vaesenc 128(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 128(%r15), %xmm11, %xmm11
- vaesenc 128(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 128(%r15), %xmm13, %xmm13
- vaesenc 128(%r15), %xmm14, %xmm14
- vaesenc 128(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vpslldq $8, %xmm1, %xmm5
- vpsrldq $8, %xmm1, %xmm1
- vaesenc 144(%r15), %xmm8, %xmm8
- vpxor %xmm5, %xmm2, %xmm2
- vpxor %xmm1, %xmm3, %xmm3
- vaesenc 144(%r15), %xmm9, %xmm9
- vpslld $31, %xmm2, %xmm7
- vpslld $30, %xmm2, %xmm4
- vpslld $25, %xmm2, %xmm5
- vaesenc 144(%r15), %xmm10, %xmm10
- vpxor %xmm4, %xmm7, %xmm7
- vpxor %xmm5, %xmm7, %xmm7
- vaesenc 144(%r15), %xmm11, %xmm11
- vpsrldq $4, %xmm7, %xmm4
- vpslldq $12, %xmm7, %xmm7
- vaesenc 144(%r15), %xmm12, %xmm12
- vpxor %xmm7, %xmm2, %xmm2
- vpsrld $0x01, %xmm2, %xmm5
- vaesenc 144(%r15), %xmm13, %xmm13
- vpsrld $2, %xmm2, %xmm1
- vpsrld $7, %xmm2, %xmm0
- vaesenc 144(%r15), %xmm14, %xmm14
- vpxor %xmm1, %xmm5, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vaesenc 144(%r15), %xmm15, %xmm15
- vpxor %xmm4, %xmm5, %xmm5
- vpxor %xmm5, %xmm2, %xmm2
- vpxor %xmm3, %xmm2, %xmm2
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm7
- jl L_AES_GCM_encrypt_avx1_aesenc_128_ghash_avx_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 176(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm7
- jl L_AES_GCM_encrypt_avx1_aesenc_128_ghash_avx_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 208(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 224(%r15), %xmm7
-L_AES_GCM_encrypt_avx1_aesenc_128_ghash_avx_done:
- vaesenclast %xmm7, %xmm8, %xmm8
- vaesenclast %xmm7, %xmm9, %xmm9
- vmovdqu (%rcx), %xmm0
- vmovdqu 16(%rcx), %xmm1
- vpxor %xmm0, %xmm8, %xmm8
- vpxor %xmm1, %xmm9, %xmm9
- vmovdqu %xmm8, (%rdx)
- vmovdqu %xmm9, 16(%rdx)
- vaesenclast %xmm7, %xmm10, %xmm10
- vaesenclast %xmm7, %xmm11, %xmm11
- vmovdqu 32(%rcx), %xmm0
- vmovdqu 48(%rcx), %xmm1
- vpxor %xmm0, %xmm10, %xmm10
- vpxor %xmm1, %xmm11, %xmm11
- vmovdqu %xmm10, 32(%rdx)
- vmovdqu %xmm11, 48(%rdx)
- vaesenclast %xmm7, %xmm12, %xmm12
- vaesenclast %xmm7, %xmm13, %xmm13
- vmovdqu 64(%rcx), %xmm0
- vmovdqu 80(%rcx), %xmm1
- vpxor %xmm0, %xmm12, %xmm12
- vpxor %xmm1, %xmm13, %xmm13
- vmovdqu %xmm12, 64(%rdx)
- vmovdqu %xmm13, 80(%rdx)
- vaesenclast %xmm7, %xmm14, %xmm14
- vaesenclast %xmm7, %xmm15, %xmm15
- vmovdqu 96(%rcx), %xmm0
- vmovdqu 112(%rcx), %xmm1
- vpxor %xmm0, %xmm14, %xmm14
- vpxor %xmm1, %xmm15, %xmm15
- vmovdqu %xmm14, 96(%rdx)
- vmovdqu %xmm15, 112(%rdx)
- addl $0x80, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_encrypt_avx1_ghash_128
-L_AES_GCM_encrypt_avx1_end_128:
- vmovdqa L_avx1_aes_gcm_bswap_mask(%rip), %xmm4
- vpshufb %xmm4, %xmm8, %xmm8
- vpshufb %xmm4, %xmm9, %xmm9
- vpshufb %xmm4, %xmm10, %xmm10
- vpshufb %xmm4, %xmm11, %xmm11
- vpxor %xmm2, %xmm8, %xmm8
- vpshufb %xmm4, %xmm12, %xmm12
- vpshufb %xmm4, %xmm13, %xmm13
- vpshufb %xmm4, %xmm14, %xmm14
- vpshufb %xmm4, %xmm15, %xmm15
- vmovdqa (%rsp), %xmm7
- vmovdqa 16(%rsp), %xmm5
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm15, %xmm1
- vpshufd $0x4e, %xmm7, %xmm2
- vpclmulqdq $0x11, %xmm15, %xmm7, %xmm3
- vpclmulqdq $0x00, %xmm15, %xmm7, %xmm0
- vpxor %xmm15, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm4
- vmovdqa %xmm3, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm4, %xmm4
- vpxor %xmm1, %xmm6, %xmm6
- # ghash_gfmul_xor_avx
- vpshufd $0x4e, %xmm14, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm14, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm14, %xmm5, %xmm0
- vpxor %xmm14, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vpxor %xmm0, %xmm4, %xmm4
- vpxor %xmm3, %xmm6, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm4, %xmm4
- vpxor %xmm1, %xmm6, %xmm6
- vmovdqa 32(%rsp), %xmm7
- vmovdqa 48(%rsp), %xmm5
- # ghash_gfmul_xor_avx
- vpshufd $0x4e, %xmm13, %xmm1
- vpshufd $0x4e, %xmm7, %xmm2
- vpclmulqdq $0x11, %xmm13, %xmm7, %xmm3
- vpclmulqdq $0x00, %xmm13, %xmm7, %xmm0
- vpxor %xmm13, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vpxor %xmm0, %xmm4, %xmm4
- vpxor %xmm3, %xmm6, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm4, %xmm4
- vpxor %xmm1, %xmm6, %xmm6
- # ghash_gfmul_xor_avx
- vpshufd $0x4e, %xmm12, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm12, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm12, %xmm5, %xmm0
- vpxor %xmm12, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vpxor %xmm0, %xmm4, %xmm4
- vpxor %xmm3, %xmm6, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm4, %xmm4
- vpxor %xmm1, %xmm6, %xmm6
- vmovdqa 64(%rsp), %xmm7
- vmovdqa 80(%rsp), %xmm5
- # ghash_gfmul_xor_avx
- vpshufd $0x4e, %xmm11, %xmm1
- vpshufd $0x4e, %xmm7, %xmm2
- vpclmulqdq $0x11, %xmm11, %xmm7, %xmm3
- vpclmulqdq $0x00, %xmm11, %xmm7, %xmm0
- vpxor %xmm11, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vpxor %xmm0, %xmm4, %xmm4
- vpxor %xmm3, %xmm6, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm4, %xmm4
- vpxor %xmm1, %xmm6, %xmm6
- # ghash_gfmul_xor_avx
- vpshufd $0x4e, %xmm10, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm10, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm10, %xmm5, %xmm0
- vpxor %xmm10, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vpxor %xmm0, %xmm4, %xmm4
- vpxor %xmm3, %xmm6, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm4, %xmm4
- vpxor %xmm1, %xmm6, %xmm6
- vmovdqa 96(%rsp), %xmm7
- vmovdqa 112(%rsp), %xmm5
- # ghash_gfmul_xor_avx
- vpshufd $0x4e, %xmm9, %xmm1
- vpshufd $0x4e, %xmm7, %xmm2
- vpclmulqdq $0x11, %xmm9, %xmm7, %xmm3
- vpclmulqdq $0x00, %xmm9, %xmm7, %xmm0
- vpxor %xmm9, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vpxor %xmm0, %xmm4, %xmm4
- vpxor %xmm3, %xmm6, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm4, %xmm4
- vpxor %xmm1, %xmm6, %xmm6
- # ghash_gfmul_xor_avx
- vpshufd $0x4e, %xmm8, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm8, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm8, %xmm5, %xmm0
- vpxor %xmm8, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vpxor %xmm0, %xmm4, %xmm4
- vpxor %xmm3, %xmm6, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm4, %xmm4
- vpxor %xmm1, %xmm6, %xmm6
- vpslld $31, %xmm4, %xmm0
- vpslld $30, %xmm4, %xmm1
- vpslld $25, %xmm4, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm4, %xmm4
- vpsrld $0x01, %xmm4, %xmm2
- vpsrld $2, %xmm4, %xmm3
- vpsrld $7, %xmm4, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm4, %xmm2, %xmm2
- vpxor %xmm2, %xmm6, %xmm6
- vmovdqa (%rsp), %xmm5
-L_AES_GCM_encrypt_avx1_done_128:
- movl %r9d, %edx
- cmpl %edx, %ebx
- jge L_AES_GCM_encrypt_avx1_done_enc
- movl %r9d, %r13d
- andl $0xfffffff0, %r13d
- cmpl %r13d, %ebx
- jge L_AES_GCM_encrypt_avx1_last_block_done
- vmovdqa 128(%rsp), %xmm9
- vpshufb L_avx1_aes_gcm_bswap_epi64(%rip), %xmm9, %xmm8
- vpaddd L_avx1_aes_gcm_one(%rip), %xmm9, %xmm9
- vmovdqa %xmm9, 128(%rsp)
- vpxor (%r15), %xmm8, %xmm8
- vaesenc 16(%r15), %xmm8, %xmm8
- vaesenc 32(%r15), %xmm8, %xmm8
- vaesenc 48(%r15), %xmm8, %xmm8
- vaesenc 64(%r15), %xmm8, %xmm8
- vaesenc 80(%r15), %xmm8, %xmm8
- vaesenc 96(%r15), %xmm8, %xmm8
- vaesenc 112(%r15), %xmm8, %xmm8
- vaesenc 128(%r15), %xmm8, %xmm8
- vaesenc 144(%r15), %xmm8, %xmm8
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm9
- jl L_AES_GCM_encrypt_avx1_aesenc_block_last
- vaesenc %xmm9, %xmm8, %xmm8
- vaesenc 176(%r15), %xmm8, %xmm8
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm9
- jl L_AES_GCM_encrypt_avx1_aesenc_block_last
- vaesenc %xmm9, %xmm8, %xmm8
- vaesenc 208(%r15), %xmm8, %xmm8
- vmovdqa 224(%r15), %xmm9
-L_AES_GCM_encrypt_avx1_aesenc_block_last:
- vaesenclast %xmm9, %xmm8, %xmm8
- vmovdqu (%rdi,%rbx,1), %xmm9
- vpxor %xmm9, %xmm8, %xmm8
- vmovdqu %xmm8, (%rsi,%rbx,1)
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm8, %xmm8
- vpxor %xmm8, %xmm6, %xmm6
- addl $16, %ebx
- cmpl %r13d, %ebx
- jge L_AES_GCM_encrypt_avx1_last_block_ghash
-L_AES_GCM_encrypt_avx1_last_block_start:
- vmovdqu (%rdi,%rbx,1), %xmm13
- vmovdqa 128(%rsp), %xmm9
- vpshufb L_avx1_aes_gcm_bswap_epi64(%rip), %xmm9, %xmm8
- vpaddd L_avx1_aes_gcm_one(%rip), %xmm9, %xmm9
- vmovdqa %xmm9, 128(%rsp)
- vpxor (%r15), %xmm8, %xmm8
- vpclmulqdq $16, %xmm5, %xmm6, %xmm10
- vaesenc 16(%r15), %xmm8, %xmm8
- vaesenc 32(%r15), %xmm8, %xmm8
- vpclmulqdq $0x01, %xmm5, %xmm6, %xmm11
- vaesenc 48(%r15), %xmm8, %xmm8
- vaesenc 64(%r15), %xmm8, %xmm8
- vpclmulqdq $0x00, %xmm5, %xmm6, %xmm12
- vaesenc 80(%r15), %xmm8, %xmm8
- vpclmulqdq $0x11, %xmm5, %xmm6, %xmm1
- vaesenc 96(%r15), %xmm8, %xmm8
- vpxor %xmm11, %xmm10, %xmm10
- vpslldq $8, %xmm10, %xmm2
- vpsrldq $8, %xmm10, %xmm10
- vaesenc 112(%r15), %xmm8, %xmm8
- vpxor %xmm12, %xmm2, %xmm2
- vpxor %xmm10, %xmm1, %xmm3
- vmovdqa L_avx1_aes_gcm_mod2_128(%rip), %xmm0
- vpclmulqdq $16, %xmm0, %xmm2, %xmm11
- vaesenc 128(%r15), %xmm8, %xmm8
- vpshufd $0x4e, %xmm2, %xmm10
- vpxor %xmm11, %xmm10, %xmm10
- vpclmulqdq $16, %xmm0, %xmm10, %xmm11
- vaesenc 144(%r15), %xmm8, %xmm8
- vpshufd $0x4e, %xmm10, %xmm10
- vpxor %xmm11, %xmm10, %xmm10
- vpxor %xmm3, %xmm10, %xmm6
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm9
- jl L_AES_GCM_encrypt_avx1_aesenc_gfmul_last
- vaesenc %xmm9, %xmm8, %xmm8
- vaesenc 176(%r15), %xmm8, %xmm8
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm9
- jl L_AES_GCM_encrypt_avx1_aesenc_gfmul_last
- vaesenc %xmm9, %xmm8, %xmm8
- vaesenc 208(%r15), %xmm8, %xmm8
- vmovdqa 224(%r15), %xmm9
-L_AES_GCM_encrypt_avx1_aesenc_gfmul_last:
- vaesenclast %xmm9, %xmm8, %xmm8
- vmovdqa %xmm13, %xmm0
- vpxor %xmm0, %xmm8, %xmm8
- vmovdqu %xmm8, (%rsi,%rbx,1)
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm8, %xmm8
- addl $16, %ebx
- vpxor %xmm8, %xmm6, %xmm6
- cmpl %r13d, %ebx
- jl L_AES_GCM_encrypt_avx1_last_block_start
-L_AES_GCM_encrypt_avx1_last_block_ghash:
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm5, %xmm9
- vpshufd $0x4e, %xmm6, %xmm10
- vpclmulqdq $0x11, %xmm5, %xmm6, %xmm11
- vpclmulqdq $0x00, %xmm5, %xmm6, %xmm8
- vpxor %xmm5, %xmm9, %xmm9
- vpxor %xmm6, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm6
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm6, %xmm6
-L_AES_GCM_encrypt_avx1_last_block_done:
- movl %r9d, %ecx
- movl %ecx, %edx
- andl $15, %ecx
- jz L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_done
- vmovdqa 128(%rsp), %xmm4
- vpshufb L_avx1_aes_gcm_bswap_epi64(%rip), %xmm4, %xmm4
- vpxor (%r15), %xmm4, %xmm4
- vaesenc 16(%r15), %xmm4, %xmm4
- vaesenc 32(%r15), %xmm4, %xmm4
- vaesenc 48(%r15), %xmm4, %xmm4
- vaesenc 64(%r15), %xmm4, %xmm4
- vaesenc 80(%r15), %xmm4, %xmm4
- vaesenc 96(%r15), %xmm4, %xmm4
- vaesenc 112(%r15), %xmm4, %xmm4
- vaesenc 128(%r15), %xmm4, %xmm4
- vaesenc 144(%r15), %xmm4, %xmm4
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm9
- jl L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_aesenc_avx_last
- vaesenc %xmm9, %xmm4, %xmm4
- vaesenc 176(%r15), %xmm4, %xmm4
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm9
- jl L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_aesenc_avx_last
- vaesenc %xmm9, %xmm4, %xmm4
- vaesenc 208(%r15), %xmm4, %xmm4
- vmovdqa 224(%r15), %xmm9
-L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_aesenc_avx_last:
- vaesenclast %xmm9, %xmm4, %xmm4
- subq $16, %rsp
- xorl %ecx, %ecx
- vmovdqa %xmm4, (%rsp)
-L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_loop:
- movzbl (%rdi,%rbx,1), %r13d
- xorb (%rsp,%rcx,1), %r13b
- movb %r13b, (%rsi,%rbx,1)
- movb %r13b, (%rsp,%rcx,1)
- incl %ebx
- incl %ecx
- cmpl %edx, %ebx
- jl L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_loop
- xorq %r13, %r13
- cmpl $16, %ecx
- je L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_finish_enc
-L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_byte_loop:
- movb %r13b, (%rsp,%rcx,1)
- incl %ecx
- cmpl $16, %ecx
- jl L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_byte_loop
-L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_finish_enc:
- vmovdqa (%rsp), %xmm4
- addq $16, %rsp
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm4, %xmm4
- vpxor %xmm4, %xmm6, %xmm6
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm5, %xmm9
- vpshufd $0x4e, %xmm6, %xmm10
- vpclmulqdq $0x11, %xmm5, %xmm6, %xmm11
- vpclmulqdq $0x00, %xmm5, %xmm6, %xmm8
- vpxor %xmm5, %xmm9, %xmm9
- vpxor %xmm6, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm6
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm6, %xmm6
-L_AES_GCM_encrypt_avx1_aesenc_last15_enc_avx_done:
-L_AES_GCM_encrypt_avx1_done_enc:
- movl %r9d, %edx
- movl %r11d, %ecx
- shlq $3, %rdx
- shlq $3, %rcx
- vpinsrq $0x00, %rdx, %xmm0, %xmm0
- vpinsrq $0x01, %rcx, %xmm0, %xmm0
- vpxor %xmm0, %xmm6, %xmm6
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm5, %xmm9
- vpshufd $0x4e, %xmm6, %xmm10
- vpclmulqdq $0x11, %xmm5, %xmm6, %xmm11
- vpclmulqdq $0x00, %xmm5, %xmm6, %xmm8
- vpxor %xmm5, %xmm9, %xmm9
- vpxor %xmm6, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm6
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm6, %xmm6
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm6, %xmm6
- vpxor 144(%rsp), %xmm6, %xmm0
- cmpl $16, %r14d
- je L_AES_GCM_encrypt_avx1_store_tag_16
- xorq %rcx, %rcx
- vmovdqa %xmm0, (%rsp)
-L_AES_GCM_encrypt_avx1_store_tag_loop:
- movzbl (%rsp,%rcx,1), %r13d
- movb %r13b, (%r8,%rcx,1)
- incl %ecx
- cmpl %r14d, %ecx
- jne L_AES_GCM_encrypt_avx1_store_tag_loop
- jmp L_AES_GCM_encrypt_avx1_store_tag_done
-L_AES_GCM_encrypt_avx1_store_tag_16:
- vmovdqu %xmm0, (%r8)
-L_AES_GCM_encrypt_avx1_store_tag_done:
- vzeroupper
- addq $0xa0, %rsp
- popq %r15
- popq %r14
- popq %rbx
- popq %r12
- popq %r13
- repz retq
-#ifndef __APPLE__
-.size AES_GCM_encrypt_avx1,.-AES_GCM_encrypt_avx1
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.text
-.globl AES_GCM_decrypt_avx1
-.type AES_GCM_decrypt_avx1,@function
-.align 4
-AES_GCM_decrypt_avx1:
-#else
-.section __TEXT,__text
-.globl _AES_GCM_decrypt_avx1
-.p2align 2
-_AES_GCM_decrypt_avx1:
-#endif /* __APPLE__ */
- pushq %r13
- pushq %r12
- pushq %rbx
- pushq %r14
- pushq %r15
- pushq %rbp
- movq %rdx, %r12
- movq %rcx, %rax
- movl 56(%rsp), %r11d
- movl 64(%rsp), %ebx
- movl 72(%rsp), %r14d
- movq 80(%rsp), %r15
- movl 88(%rsp), %r10d
- movq 96(%rsp), %rbp
- subq $0xa8, %rsp
- vpxor %xmm4, %xmm4, %xmm4
- vpxor %xmm6, %xmm6, %xmm6
- cmpl $12, %ebx
- movl %ebx, %edx
- jne L_AES_GCM_decrypt_avx1_iv_not_12
- # # Calculate values when IV is 12 bytes
- # Set counter based on IV
- movl $0x1000000, %ecx
- vpinsrq $0x00, (%rax), %xmm4, %xmm4
- vpinsrd $2, 8(%rax), %xmm4, %xmm4
- vpinsrd $3, %ecx, %xmm4, %xmm4
- # H = Encrypt X(=0) and T = Encrypt counter
- vmovdqa (%r15), %xmm5
- vpxor %xmm5, %xmm4, %xmm1
- vmovdqa 16(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 32(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 48(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 64(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 80(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 96(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 112(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 128(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 144(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm7
- jl L_AES_GCM_decrypt_avx1_calc_iv_12_last
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 176(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm7
- jl L_AES_GCM_decrypt_avx1_calc_iv_12_last
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 208(%r15), %xmm7
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm1, %xmm1
- vmovdqa 224(%r15), %xmm7
-L_AES_GCM_decrypt_avx1_calc_iv_12_last:
- vaesenclast %xmm7, %xmm5, %xmm5
- vaesenclast %xmm7, %xmm1, %xmm1
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm5, %xmm5
- vmovdqa %xmm1, 144(%rsp)
- jmp L_AES_GCM_decrypt_avx1_iv_done
-L_AES_GCM_decrypt_avx1_iv_not_12:
- # Calculate values when IV is not 12 bytes
- # H = Encrypt X(=0)
- vmovdqa (%r15), %xmm5
- vaesenc 16(%r15), %xmm5, %xmm5
- vaesenc 32(%r15), %xmm5, %xmm5
- vaesenc 48(%r15), %xmm5, %xmm5
- vaesenc 64(%r15), %xmm5, %xmm5
- vaesenc 80(%r15), %xmm5, %xmm5
- vaesenc 96(%r15), %xmm5, %xmm5
- vaesenc 112(%r15), %xmm5, %xmm5
- vaesenc 128(%r15), %xmm5, %xmm5
- vaesenc 144(%r15), %xmm5, %xmm5
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm9
- jl L_AES_GCM_decrypt_avx1_calc_iv_1_aesenc_avx_last
- vaesenc %xmm9, %xmm5, %xmm5
- vaesenc 176(%r15), %xmm5, %xmm5
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm9
- jl L_AES_GCM_decrypt_avx1_calc_iv_1_aesenc_avx_last
- vaesenc %xmm9, %xmm5, %xmm5
- vaesenc 208(%r15), %xmm5, %xmm5
- vmovdqa 224(%r15), %xmm9
-L_AES_GCM_decrypt_avx1_calc_iv_1_aesenc_avx_last:
- vaesenclast %xmm9, %xmm5, %xmm5
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm5, %xmm5
- # Calc counter
- # Initialization vector
- cmpl $0x00, %edx
- movq $0x00, %rcx
- je L_AES_GCM_decrypt_avx1_calc_iv_done
- cmpl $16, %edx
- jl L_AES_GCM_decrypt_avx1_calc_iv_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_decrypt_avx1_calc_iv_16_loop:
- vmovdqu (%rax,%rcx,1), %xmm8
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm8, %xmm8
- vpxor %xmm8, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm4, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpxor %xmm4, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm7
- vmovdqa %xmm3, %xmm4
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm7, %xmm7
- vpxor %xmm1, %xmm4, %xmm4
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- vpslld $31, %xmm7, %xmm0
- vpslld $30, %xmm7, %xmm1
- vpslld $25, %xmm7, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm7, %xmm7
- vpsrld $0x01, %xmm7, %xmm2
- vpsrld $2, %xmm7, %xmm3
- vpsrld $7, %xmm7, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm4, %xmm4
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_avx1_calc_iv_16_loop
- movl %ebx, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_decrypt_avx1_calc_iv_done
-L_AES_GCM_decrypt_avx1_calc_iv_lt16:
- subq $16, %rsp
- vpxor %xmm8, %xmm8, %xmm8
- xorl %ebx, %ebx
- vmovdqa %xmm8, (%rsp)
-L_AES_GCM_decrypt_avx1_calc_iv_loop:
- movzbl (%rax,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_avx1_calc_iv_loop
- vmovdqa (%rsp), %xmm8
- addq $16, %rsp
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm8, %xmm8
- vpxor %xmm8, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm4, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpxor %xmm4, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm7
- vmovdqa %xmm3, %xmm4
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm7, %xmm7
- vpxor %xmm1, %xmm4, %xmm4
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- vpslld $31, %xmm7, %xmm0
- vpslld $30, %xmm7, %xmm1
- vpslld $25, %xmm7, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm7, %xmm7
- vpsrld $0x01, %xmm7, %xmm2
- vpsrld $2, %xmm7, %xmm3
- vpsrld $7, %xmm7, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm4, %xmm4
-L_AES_GCM_decrypt_avx1_calc_iv_done:
- # T = Encrypt counter
- vpxor %xmm0, %xmm0, %xmm0
- shll $3, %edx
- vpinsrq $0x00, %rdx, %xmm0, %xmm0
- vpxor %xmm0, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm4, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpxor %xmm4, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm7
- vmovdqa %xmm3, %xmm4
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm7, %xmm7
- vpxor %xmm1, %xmm4, %xmm4
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- vpslld $31, %xmm7, %xmm0
- vpslld $30, %xmm7, %xmm1
- vpslld $25, %xmm7, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm7, %xmm7
- vpsrld $0x01, %xmm7, %xmm2
- vpsrld $2, %xmm7, %xmm3
- vpsrld $7, %xmm7, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm4, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm4, %xmm4
- # Encrypt counter
- vmovdqa (%r15), %xmm8
- vpxor %xmm4, %xmm8, %xmm8
- vaesenc 16(%r15), %xmm8, %xmm8
- vaesenc 32(%r15), %xmm8, %xmm8
- vaesenc 48(%r15), %xmm8, %xmm8
- vaesenc 64(%r15), %xmm8, %xmm8
- vaesenc 80(%r15), %xmm8, %xmm8
- vaesenc 96(%r15), %xmm8, %xmm8
- vaesenc 112(%r15), %xmm8, %xmm8
- vaesenc 128(%r15), %xmm8, %xmm8
- vaesenc 144(%r15), %xmm8, %xmm8
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm9
- jl L_AES_GCM_decrypt_avx1_calc_iv_2_aesenc_avx_last
- vaesenc %xmm9, %xmm8, %xmm8
- vaesenc 176(%r15), %xmm8, %xmm8
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm9
- jl L_AES_GCM_decrypt_avx1_calc_iv_2_aesenc_avx_last
- vaesenc %xmm9, %xmm8, %xmm8
- vaesenc 208(%r15), %xmm8, %xmm8
- vmovdqa 224(%r15), %xmm9
-L_AES_GCM_decrypt_avx1_calc_iv_2_aesenc_avx_last:
- vaesenclast %xmm9, %xmm8, %xmm8
- vmovdqa %xmm8, 144(%rsp)
-L_AES_GCM_decrypt_avx1_iv_done:
- # Additional authentication data
- movl %r11d, %edx
- cmpl $0x00, %edx
- je L_AES_GCM_decrypt_avx1_calc_aad_done
- xorl %ecx, %ecx
- cmpl $16, %edx
- jl L_AES_GCM_decrypt_avx1_calc_aad_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_decrypt_avx1_calc_aad_16_loop:
- vmovdqu (%r12,%rcx,1), %xmm8
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm8, %xmm8
- vpxor %xmm8, %xmm6, %xmm6
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm6, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm6, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm6, %xmm5, %xmm0
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm7
- vmovdqa %xmm3, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm7, %xmm7
- vpxor %xmm1, %xmm6, %xmm6
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm6, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm6, %xmm6
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm6, %xmm6
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm6, %xmm6
- vpslld $31, %xmm7, %xmm0
- vpslld $30, %xmm7, %xmm1
- vpslld $25, %xmm7, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm7, %xmm7
- vpsrld $0x01, %xmm7, %xmm2
- vpsrld $2, %xmm7, %xmm3
- vpsrld $7, %xmm7, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm6, %xmm6
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_avx1_calc_aad_16_loop
- movl %r11d, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_decrypt_avx1_calc_aad_done
-L_AES_GCM_decrypt_avx1_calc_aad_lt16:
- subq $16, %rsp
- vpxor %xmm8, %xmm8, %xmm8
- xorl %ebx, %ebx
- vmovdqa %xmm8, (%rsp)
-L_AES_GCM_decrypt_avx1_calc_aad_loop:
- movzbl (%r12,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_avx1_calc_aad_loop
- vmovdqa (%rsp), %xmm8
- addq $16, %rsp
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm8, %xmm8
- vpxor %xmm8, %xmm6, %xmm6
- # ghash_gfmul_avx
- vpshufd $0x4e, %xmm6, %xmm1
- vpshufd $0x4e, %xmm5, %xmm2
- vpclmulqdq $0x11, %xmm6, %xmm5, %xmm3
- vpclmulqdq $0x00, %xmm6, %xmm5, %xmm0
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm5, %xmm2, %xmm2
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa %xmm0, %xmm7
- vmovdqa %xmm3, %xmm6
- vpslldq $8, %xmm1, %xmm2
- vpsrldq $8, %xmm1, %xmm1
- vpxor %xmm2, %xmm7, %xmm7
- vpxor %xmm1, %xmm6, %xmm6
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm6, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm6, %xmm6
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm6, %xmm6
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm6, %xmm6
- vpslld $31, %xmm7, %xmm0
- vpslld $30, %xmm7, %xmm1
- vpslld $25, %xmm7, %xmm2
- vpxor %xmm1, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vmovdqa %xmm0, %xmm1
- vpsrldq $4, %xmm1, %xmm1
- vpslldq $12, %xmm0, %xmm0
- vpxor %xmm0, %xmm7, %xmm7
- vpsrld $0x01, %xmm7, %xmm2
- vpsrld $2, %xmm7, %xmm3
- vpsrld $7, %xmm7, %xmm0
- vpxor %xmm3, %xmm2, %xmm2
- vpxor %xmm0, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm6, %xmm6
-L_AES_GCM_decrypt_avx1_calc_aad_done:
- # Calculate counter and H
- vpsrlq $63, %xmm5, %xmm9
- vpsllq $0x01, %xmm5, %xmm8
- vpslldq $8, %xmm9, %xmm9
- vpor %xmm9, %xmm8, %xmm8
- vpshufd $0xff, %xmm5, %xmm5
- vpsrad $31, %xmm5, %xmm5
- vpshufb L_avx1_aes_gcm_bswap_epi64(%rip), %xmm4, %xmm4
- vpand L_avx1_aes_gcm_mod2_128(%rip), %xmm5, %xmm5
- vpaddd L_avx1_aes_gcm_one(%rip), %xmm4, %xmm4
- vpxor %xmm8, %xmm5, %xmm5
- vmovdqa %xmm4, 128(%rsp)
- xorl %ebx, %ebx
- cmpl $0x80, %r9d
- movl %r9d, %r13d
- jl L_AES_GCM_decrypt_avx1_done_128
- andl $0xffffff80, %r13d
- vmovdqa %xmm6, %xmm2
- # H ^ 1
- vmovdqa %xmm5, (%rsp)
- # H ^ 2
- vpclmulqdq $0x00, %xmm5, %xmm5, %xmm8
- vpclmulqdq $0x11, %xmm5, %xmm5, %xmm0
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm0, %xmm0
- vmovdqa %xmm0, 16(%rsp)
- # H ^ 3
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm5, %xmm9
- vpshufd $0x4e, %xmm0, %xmm10
- vpclmulqdq $0x11, %xmm5, %xmm0, %xmm11
- vpclmulqdq $0x00, %xmm5, %xmm0, %xmm8
- vpxor %xmm5, %xmm9, %xmm9
- vpxor %xmm0, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm1
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm1, %xmm1
- vmovdqa %xmm1, 32(%rsp)
- # H ^ 4
- vpclmulqdq $0x00, %xmm0, %xmm0, %xmm8
- vpclmulqdq $0x11, %xmm0, %xmm0, %xmm3
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm3, %xmm3
- vmovdqa %xmm3, 48(%rsp)
- # H ^ 5
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm0, %xmm9
- vpshufd $0x4e, %xmm1, %xmm10
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm11
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm8
- vpxor %xmm0, %xmm9, %xmm9
- vpxor %xmm1, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm7
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm7, %xmm7
- vmovdqa %xmm7, 64(%rsp)
- # H ^ 6
- vpclmulqdq $0x00, %xmm1, %xmm1, %xmm8
- vpclmulqdq $0x11, %xmm1, %xmm1, %xmm7
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm7, %xmm7
- vmovdqa %xmm7, 80(%rsp)
- # H ^ 7
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm1, %xmm9
- vpshufd $0x4e, %xmm3, %xmm10
- vpclmulqdq $0x11, %xmm1, %xmm3, %xmm11
- vpclmulqdq $0x00, %xmm1, %xmm3, %xmm8
- vpxor %xmm1, %xmm9, %xmm9
- vpxor %xmm3, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm7
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm7, %xmm7
- vmovdqa %xmm7, 96(%rsp)
- # H ^ 8
- vpclmulqdq $0x00, %xmm3, %xmm3, %xmm8
- vpclmulqdq $0x11, %xmm3, %xmm3, %xmm7
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm7, %xmm7
- vmovdqa %xmm7, 112(%rsp)
-L_AES_GCM_decrypt_avx1_ghash_128:
- leaq (%rdi,%rbx,1), %rcx
- leaq (%rsi,%rbx,1), %rdx
- vmovdqa 128(%rsp), %xmm0
- vmovdqa L_avx1_aes_gcm_bswap_epi64(%rip), %xmm1
- vpshufb %xmm1, %xmm0, %xmm8
- vpaddd L_avx1_aes_gcm_one(%rip), %xmm0, %xmm9
- vpshufb %xmm1, %xmm9, %xmm9
- vpaddd L_avx1_aes_gcm_two(%rip), %xmm0, %xmm10
- vpshufb %xmm1, %xmm10, %xmm10
- vpaddd L_avx1_aes_gcm_three(%rip), %xmm0, %xmm11
- vpshufb %xmm1, %xmm11, %xmm11
- vpaddd L_avx1_aes_gcm_four(%rip), %xmm0, %xmm12
- vpshufb %xmm1, %xmm12, %xmm12
- vpaddd L_avx1_aes_gcm_five(%rip), %xmm0, %xmm13
- vpshufb %xmm1, %xmm13, %xmm13
- vpaddd L_avx1_aes_gcm_six(%rip), %xmm0, %xmm14
- vpshufb %xmm1, %xmm14, %xmm14
- vpaddd L_avx1_aes_gcm_seven(%rip), %xmm0, %xmm15
- vpshufb %xmm1, %xmm15, %xmm15
- vpaddd L_avx1_aes_gcm_eight(%rip), %xmm0, %xmm0
- vmovdqa (%r15), %xmm7
- vmovdqa %xmm0, 128(%rsp)
- vpxor %xmm7, %xmm8, %xmm8
- vpxor %xmm7, %xmm9, %xmm9
- vpxor %xmm7, %xmm10, %xmm10
- vpxor %xmm7, %xmm11, %xmm11
- vpxor %xmm7, %xmm12, %xmm12
- vpxor %xmm7, %xmm13, %xmm13
- vpxor %xmm7, %xmm14, %xmm14
- vpxor %xmm7, %xmm15, %xmm15
- vmovdqa 112(%rsp), %xmm7
- vmovdqu (%rcx), %xmm0
- vaesenc 16(%r15), %xmm8, %xmm8
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm3
- vaesenc 16(%r15), %xmm9, %xmm9
- vaesenc 16(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm2
- vaesenc 16(%r15), %xmm11, %xmm11
- vaesenc 16(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm1, %xmm1
- vaesenc 16(%r15), %xmm13, %xmm13
- vaesenc 16(%r15), %xmm14, %xmm14
- vaesenc 16(%r15), %xmm15, %xmm15
- vpxor %xmm2, %xmm1, %xmm1
- vpxor %xmm3, %xmm1, %xmm1
- vmovdqa 96(%rsp), %xmm7
- vmovdqu 16(%rcx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 32(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 32(%r15), %xmm9, %xmm9
- vaesenc 32(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 32(%r15), %xmm11, %xmm11
- vaesenc 32(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 32(%r15), %xmm13, %xmm13
- vaesenc 32(%r15), %xmm14, %xmm14
- vaesenc 32(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa 80(%rsp), %xmm7
- vmovdqu 32(%rcx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 48(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 48(%r15), %xmm9, %xmm9
- vaesenc 48(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 48(%r15), %xmm11, %xmm11
- vaesenc 48(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 48(%r15), %xmm13, %xmm13
- vaesenc 48(%r15), %xmm14, %xmm14
- vaesenc 48(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa 64(%rsp), %xmm7
- vmovdqu 48(%rcx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 64(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 64(%r15), %xmm9, %xmm9
- vaesenc 64(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 64(%r15), %xmm11, %xmm11
- vaesenc 64(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 64(%r15), %xmm13, %xmm13
- vaesenc 64(%r15), %xmm14, %xmm14
- vaesenc 64(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa 48(%rsp), %xmm7
- vmovdqu 64(%rcx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 80(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 80(%r15), %xmm9, %xmm9
- vaesenc 80(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 80(%r15), %xmm11, %xmm11
- vaesenc 80(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 80(%r15), %xmm13, %xmm13
- vaesenc 80(%r15), %xmm14, %xmm14
- vaesenc 80(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa 32(%rsp), %xmm7
- vmovdqu 80(%rcx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 96(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 96(%r15), %xmm9, %xmm9
- vaesenc 96(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 96(%r15), %xmm11, %xmm11
- vaesenc 96(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 96(%r15), %xmm13, %xmm13
- vaesenc 96(%r15), %xmm14, %xmm14
- vaesenc 96(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa 16(%rsp), %xmm7
- vmovdqu 96(%rcx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 112(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 112(%r15), %xmm9, %xmm9
- vaesenc 112(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 112(%r15), %xmm11, %xmm11
- vaesenc 112(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 112(%r15), %xmm13, %xmm13
- vaesenc 112(%r15), %xmm14, %xmm14
- vaesenc 112(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vmovdqa (%rsp), %xmm7
- vmovdqu 112(%rcx), %xmm0
- vpshufd $0x4e, %xmm7, %xmm4
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vaesenc 128(%r15), %xmm8, %xmm8
- vpxor %xmm7, %xmm4, %xmm4
- vpshufd $0x4e, %xmm0, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vpclmulqdq $0x11, %xmm7, %xmm0, %xmm6
- vaesenc 128(%r15), %xmm9, %xmm9
- vaesenc 128(%r15), %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm7, %xmm0, %xmm7
- vaesenc 128(%r15), %xmm11, %xmm11
- vaesenc 128(%r15), %xmm12, %xmm12
- vpclmulqdq $0x00, %xmm5, %xmm4, %xmm4
- vaesenc 128(%r15), %xmm13, %xmm13
- vaesenc 128(%r15), %xmm14, %xmm14
- vaesenc 128(%r15), %xmm15, %xmm15
- vpxor %xmm7, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm6, %xmm3, %xmm3
- vpxor %xmm4, %xmm1, %xmm1
- vpslldq $8, %xmm1, %xmm5
- vpsrldq $8, %xmm1, %xmm1
- vaesenc 144(%r15), %xmm8, %xmm8
- vpxor %xmm5, %xmm2, %xmm2
- vpxor %xmm1, %xmm3, %xmm3
- vaesenc 144(%r15), %xmm9, %xmm9
- vpslld $31, %xmm2, %xmm7
- vpslld $30, %xmm2, %xmm4
- vpslld $25, %xmm2, %xmm5
- vaesenc 144(%r15), %xmm10, %xmm10
- vpxor %xmm4, %xmm7, %xmm7
- vpxor %xmm5, %xmm7, %xmm7
- vaesenc 144(%r15), %xmm11, %xmm11
- vpsrldq $4, %xmm7, %xmm4
- vpslldq $12, %xmm7, %xmm7
- vaesenc 144(%r15), %xmm12, %xmm12
- vpxor %xmm7, %xmm2, %xmm2
- vpsrld $0x01, %xmm2, %xmm5
- vaesenc 144(%r15), %xmm13, %xmm13
- vpsrld $2, %xmm2, %xmm1
- vpsrld $7, %xmm2, %xmm0
- vaesenc 144(%r15), %xmm14, %xmm14
- vpxor %xmm1, %xmm5, %xmm5
- vpxor %xmm0, %xmm5, %xmm5
- vaesenc 144(%r15), %xmm15, %xmm15
- vpxor %xmm4, %xmm5, %xmm5
- vpxor %xmm5, %xmm2, %xmm2
- vpxor %xmm3, %xmm2, %xmm2
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm7
- jl L_AES_GCM_decrypt_avx1_aesenc_128_ghash_avx_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 176(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm7
- jl L_AES_GCM_decrypt_avx1_aesenc_128_ghash_avx_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 208(%r15), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 224(%r15), %xmm7
-L_AES_GCM_decrypt_avx1_aesenc_128_ghash_avx_done:
- vaesenclast %xmm7, %xmm8, %xmm8
- vaesenclast %xmm7, %xmm9, %xmm9
- vmovdqu (%rcx), %xmm0
- vmovdqu 16(%rcx), %xmm1
- vpxor %xmm0, %xmm8, %xmm8
- vpxor %xmm1, %xmm9, %xmm9
- vmovdqu %xmm8, (%rdx)
- vmovdqu %xmm9, 16(%rdx)
- vaesenclast %xmm7, %xmm10, %xmm10
- vaesenclast %xmm7, %xmm11, %xmm11
- vmovdqu 32(%rcx), %xmm0
- vmovdqu 48(%rcx), %xmm1
- vpxor %xmm0, %xmm10, %xmm10
- vpxor %xmm1, %xmm11, %xmm11
- vmovdqu %xmm10, 32(%rdx)
- vmovdqu %xmm11, 48(%rdx)
- vaesenclast %xmm7, %xmm12, %xmm12
- vaesenclast %xmm7, %xmm13, %xmm13
- vmovdqu 64(%rcx), %xmm0
- vmovdqu 80(%rcx), %xmm1
- vpxor %xmm0, %xmm12, %xmm12
- vpxor %xmm1, %xmm13, %xmm13
- vmovdqu %xmm12, 64(%rdx)
- vmovdqu %xmm13, 80(%rdx)
- vaesenclast %xmm7, %xmm14, %xmm14
- vaesenclast %xmm7, %xmm15, %xmm15
- vmovdqu 96(%rcx), %xmm0
- vmovdqu 112(%rcx), %xmm1
- vpxor %xmm0, %xmm14, %xmm14
- vpxor %xmm1, %xmm15, %xmm15
- vmovdqu %xmm14, 96(%rdx)
- vmovdqu %xmm15, 112(%rdx)
- addl $0x80, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_decrypt_avx1_ghash_128
- vmovdqa %xmm2, %xmm6
- vmovdqa (%rsp), %xmm5
-L_AES_GCM_decrypt_avx1_done_128:
- movl %r9d, %edx
- cmpl %edx, %ebx
- jge L_AES_GCM_decrypt_avx1_done_dec
- movl %r9d, %r13d
- andl $0xfffffff0, %r13d
- cmpl %r13d, %ebx
- jge L_AES_GCM_decrypt_avx1_last_block_done
-L_AES_GCM_decrypt_avx1_last_block_start:
- vmovdqu (%rdi,%rbx,1), %xmm13
- vmovdqa %xmm5, %xmm0
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm13, %xmm1
- vpxor %xmm6, %xmm1, %xmm1
- vmovdqa 128(%rsp), %xmm9
- vpshufb L_avx1_aes_gcm_bswap_epi64(%rip), %xmm9, %xmm8
- vpaddd L_avx1_aes_gcm_one(%rip), %xmm9, %xmm9
- vmovdqa %xmm9, 128(%rsp)
- vpxor (%r15), %xmm8, %xmm8
- vpclmulqdq $16, %xmm0, %xmm1, %xmm10
- vaesenc 16(%r15), %xmm8, %xmm8
- vaesenc 32(%r15), %xmm8, %xmm8
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm11
- vaesenc 48(%r15), %xmm8, %xmm8
- vaesenc 64(%r15), %xmm8, %xmm8
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm12
- vaesenc 80(%r15), %xmm8, %xmm8
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vaesenc 96(%r15), %xmm8, %xmm8
- vpxor %xmm11, %xmm10, %xmm10
- vpslldq $8, %xmm10, %xmm2
- vpsrldq $8, %xmm10, %xmm10
- vaesenc 112(%r15), %xmm8, %xmm8
- vpxor %xmm12, %xmm2, %xmm2
- vpxor %xmm10, %xmm1, %xmm3
- vmovdqa L_avx1_aes_gcm_mod2_128(%rip), %xmm0
- vpclmulqdq $16, %xmm0, %xmm2, %xmm11
- vaesenc 128(%r15), %xmm8, %xmm8
- vpshufd $0x4e, %xmm2, %xmm10
- vpxor %xmm11, %xmm10, %xmm10
- vpclmulqdq $16, %xmm0, %xmm10, %xmm11
- vaesenc 144(%r15), %xmm8, %xmm8
- vpshufd $0x4e, %xmm10, %xmm10
- vpxor %xmm11, %xmm10, %xmm10
- vpxor %xmm3, %xmm10, %xmm6
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm9
- jl L_AES_GCM_decrypt_avx1_aesenc_gfmul_last
- vaesenc %xmm9, %xmm8, %xmm8
- vaesenc 176(%r15), %xmm8, %xmm8
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm9
- jl L_AES_GCM_decrypt_avx1_aesenc_gfmul_last
- vaesenc %xmm9, %xmm8, %xmm8
- vaesenc 208(%r15), %xmm8, %xmm8
- vmovdqa 224(%r15), %xmm9
-L_AES_GCM_decrypt_avx1_aesenc_gfmul_last:
- vaesenclast %xmm9, %xmm8, %xmm8
- vmovdqa %xmm13, %xmm0
- vpxor %xmm0, %xmm8, %xmm8
- vmovdqu %xmm8, (%rsi,%rbx,1)
- addl $16, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_decrypt_avx1_last_block_start
-L_AES_GCM_decrypt_avx1_last_block_done:
- movl %r9d, %ecx
- movl %ecx, %edx
- andl $15, %ecx
- jz L_AES_GCM_decrypt_avx1_aesenc_last15_dec_avx_done
- vmovdqa 128(%rsp), %xmm4
- vpshufb L_avx1_aes_gcm_bswap_epi64(%rip), %xmm4, %xmm4
- vpxor (%r15), %xmm4, %xmm4
- vaesenc 16(%r15), %xmm4, %xmm4
- vaesenc 32(%r15), %xmm4, %xmm4
- vaesenc 48(%r15), %xmm4, %xmm4
- vaesenc 64(%r15), %xmm4, %xmm4
- vaesenc 80(%r15), %xmm4, %xmm4
- vaesenc 96(%r15), %xmm4, %xmm4
- vaesenc 112(%r15), %xmm4, %xmm4
- vaesenc 128(%r15), %xmm4, %xmm4
- vaesenc 144(%r15), %xmm4, %xmm4
- cmpl $11, %r10d
- vmovdqa 160(%r15), %xmm9
- jl L_AES_GCM_decrypt_avx1_aesenc_last15_dec_avx_aesenc_avx_last
- vaesenc %xmm9, %xmm4, %xmm4
- vaesenc 176(%r15), %xmm4, %xmm4
- cmpl $13, %r10d
- vmovdqa 192(%r15), %xmm9
- jl L_AES_GCM_decrypt_avx1_aesenc_last15_dec_avx_aesenc_avx_last
- vaesenc %xmm9, %xmm4, %xmm4
- vaesenc 208(%r15), %xmm4, %xmm4
- vmovdqa 224(%r15), %xmm9
-L_AES_GCM_decrypt_avx1_aesenc_last15_dec_avx_aesenc_avx_last:
- vaesenclast %xmm9, %xmm4, %xmm4
- subq $32, %rsp
- xorl %ecx, %ecx
- vmovdqa %xmm4, (%rsp)
- vpxor %xmm0, %xmm0, %xmm0
- vmovdqa %xmm0, 16(%rsp)
-L_AES_GCM_decrypt_avx1_aesenc_last15_dec_avx_loop:
- movzbl (%rdi,%rbx,1), %r13d
- movb %r13b, 16(%rsp,%rcx,1)
- xorb (%rsp,%rcx,1), %r13b
- movb %r13b, (%rsi,%rbx,1)
- incl %ebx
- incl %ecx
- cmpl %edx, %ebx
- jl L_AES_GCM_decrypt_avx1_aesenc_last15_dec_avx_loop
- vmovdqa 16(%rsp), %xmm4
- addq $32, %rsp
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm4, %xmm4
- vpxor %xmm4, %xmm6, %xmm6
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm5, %xmm9
- vpshufd $0x4e, %xmm6, %xmm10
- vpclmulqdq $0x11, %xmm5, %xmm6, %xmm11
- vpclmulqdq $0x00, %xmm5, %xmm6, %xmm8
- vpxor %xmm5, %xmm9, %xmm9
- vpxor %xmm6, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm6
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm6, %xmm6
-L_AES_GCM_decrypt_avx1_aesenc_last15_dec_avx_done:
-L_AES_GCM_decrypt_avx1_done_dec:
- movl %r9d, %edx
- movl %r11d, %ecx
- shlq $3, %rdx
- shlq $3, %rcx
- vpinsrq $0x00, %rdx, %xmm0, %xmm0
- vpinsrq $0x01, %rcx, %xmm0, %xmm0
- vpxor %xmm0, %xmm6, %xmm6
- # ghash_gfmul_red_avx
- vpshufd $0x4e, %xmm5, %xmm9
- vpshufd $0x4e, %xmm6, %xmm10
- vpclmulqdq $0x11, %xmm5, %xmm6, %xmm11
- vpclmulqdq $0x00, %xmm5, %xmm6, %xmm8
- vpxor %xmm5, %xmm9, %xmm9
- vpxor %xmm6, %xmm10, %xmm10
- vpclmulqdq $0x00, %xmm10, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm11, %xmm9, %xmm9
- vpslldq $8, %xmm9, %xmm10
- vpsrldq $8, %xmm9, %xmm9
- vpxor %xmm10, %xmm8, %xmm8
- vpxor %xmm9, %xmm11, %xmm6
- vpslld $31, %xmm8, %xmm12
- vpslld $30, %xmm8, %xmm13
- vpslld $25, %xmm8, %xmm14
- vpxor %xmm13, %xmm12, %xmm12
- vpxor %xmm14, %xmm12, %xmm12
- vpsrldq $4, %xmm12, %xmm13
- vpslldq $12, %xmm12, %xmm12
- vpxor %xmm12, %xmm8, %xmm8
- vpsrld $0x01, %xmm8, %xmm14
- vpsrld $2, %xmm8, %xmm10
- vpsrld $7, %xmm8, %xmm9
- vpxor %xmm10, %xmm14, %xmm14
- vpxor %xmm9, %xmm14, %xmm14
- vpxor %xmm13, %xmm14, %xmm14
- vpxor %xmm8, %xmm14, %xmm14
- vpxor %xmm14, %xmm6, %xmm6
- vpshufb L_avx1_aes_gcm_bswap_mask(%rip), %xmm6, %xmm6
- vpxor 144(%rsp), %xmm6, %xmm0
- cmpl $16, %r14d
- je L_AES_GCM_decrypt_avx1_cmp_tag_16
- subq $16, %rsp
- xorq %rcx, %rcx
- xorq %rbx, %rbx
- vmovdqa %xmm0, (%rsp)
-L_AES_GCM_decrypt_avx1_cmp_tag_loop:
- movzbl (%rsp,%rcx,1), %r13d
- xorb (%r8,%rcx,1), %r13b
- orb %r13b, %bl
- incl %ecx
- cmpl %r14d, %ecx
- jne L_AES_GCM_decrypt_avx1_cmp_tag_loop
- cmpb $0x00, %bl
- sete %bl
- addq $16, %rsp
- xorq %rcx, %rcx
- jmp L_AES_GCM_decrypt_avx1_cmp_tag_done
-L_AES_GCM_decrypt_avx1_cmp_tag_16:
- vmovdqu (%r8), %xmm1
- vpcmpeqb %xmm1, %xmm0, %xmm0
- vpmovmskb %xmm0, %rdx
- # %%edx == 0xFFFF then return 1 else => return 0
- xorl %ebx, %ebx
- cmpl $0xffff, %edx
- sete %bl
-L_AES_GCM_decrypt_avx1_cmp_tag_done:
- movl %ebx, (%rbp)
- vzeroupper
- addq $0xa8, %rsp
- popq %rbp
- popq %r15
- popq %r14
- popq %rbx
- popq %r12
- popq %r13
- repz retq
-#ifndef __APPLE__
-.size AES_GCM_decrypt_avx1,.-AES_GCM_decrypt_avx1
-#endif /* __APPLE__ */
-#endif /* HAVE_INTEL_AVX1 */
-#ifdef HAVE_INTEL_AVX2
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_one:
-.quad 0x0, 0x1
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_two:
-.quad 0x0, 0x2
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_three:
-.quad 0x0, 0x3
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_four:
-.quad 0x0, 0x4
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_five:
-.quad 0x0, 0x5
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_six:
-.quad 0x0, 0x6
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_seven:
-.quad 0x0, 0x7
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_eight:
-.quad 0x0, 0x8
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_bswap_one:
-.quad 0x0, 0x100000000000000
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_bswap_epi64:
-.quad 0x1020304050607, 0x8090a0b0c0d0e0f
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_bswap_mask:
-.quad 0x8090a0b0c0d0e0f, 0x1020304050607
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_avx2_aes_gcm_mod2_128:
-.quad 0x1, 0xc200000000000000
-#ifndef __APPLE__
-.text
-.globl AES_GCM_encrypt_avx2
-.type AES_GCM_encrypt_avx2,@function
-.align 4
-AES_GCM_encrypt_avx2:
-#else
-.section __TEXT,__text
-.globl _AES_GCM_encrypt_avx2
-.p2align 2
-_AES_GCM_encrypt_avx2:
-#endif /* __APPLE__ */
- pushq %r13
- pushq %r12
- pushq %r15
- pushq %rbx
- pushq %r14
- movq %rdx, %r12
- movq %rcx, %rax
- movq %r8, %r15
- movq %rsi, %r8
- movl %r9d, %r10d
- movl 48(%rsp), %r11d
- movl 56(%rsp), %ebx
- movl 64(%rsp), %r14d
- movq 72(%rsp), %rsi
- movl 80(%rsp), %r9d
- subq $0xa0, %rsp
- vpxor %xmm4, %xmm4, %xmm4
- vpxor %xmm6, %xmm6, %xmm6
- movl %ebx, %edx
- cmpl $12, %edx
- je L_AES_GCM_encrypt_avx2_iv_12
- # Calculate values when IV is not 12 bytes
- # H = Encrypt X(=0)
- vmovdqa (%rsi), %xmm5
- vaesenc 16(%rsi), %xmm5, %xmm5
- vaesenc 32(%rsi), %xmm5, %xmm5
- vaesenc 48(%rsi), %xmm5, %xmm5
- vaesenc 64(%rsi), %xmm5, %xmm5
- vaesenc 80(%rsi), %xmm5, %xmm5
- vaesenc 96(%rsi), %xmm5, %xmm5
- vaesenc 112(%rsi), %xmm5, %xmm5
- vaesenc 128(%rsi), %xmm5, %xmm5
- vaesenc 144(%rsi), %xmm5, %xmm5
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm0
- jl L_AES_GCM_encrypt_avx2_calc_iv_1_aesenc_avx_last
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc 176(%rsi), %xmm5, %xmm5
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm0
- jl L_AES_GCM_encrypt_avx2_calc_iv_1_aesenc_avx_last
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc 208(%rsi), %xmm5, %xmm5
- vmovdqa 224(%rsi), %xmm0
-L_AES_GCM_encrypt_avx2_calc_iv_1_aesenc_avx_last:
- vaesenclast %xmm0, %xmm5, %xmm5
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm5, %xmm5
- # Calc counter
- # Initialization vector
- cmpl $0x00, %edx
- movq $0x00, %rcx
- je L_AES_GCM_encrypt_avx2_calc_iv_done
- cmpl $16, %edx
- jl L_AES_GCM_encrypt_avx2_calc_iv_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_encrypt_avx2_calc_iv_16_loop:
- vmovdqu (%rax,%rcx,1), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm0, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpclmulqdq $16, %xmm4, %xmm5, %xmm2
- vpclmulqdq $0x01, %xmm4, %xmm5, %xmm1
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm1, %xmm0, %xmm7
- vpxor %xmm2, %xmm3, %xmm4
- # ghash_mid
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm7, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm4, %xmm4
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_avx2_calc_iv_16_loop
- movl %ebx, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_encrypt_avx2_calc_iv_done
-L_AES_GCM_encrypt_avx2_calc_iv_lt16:
- vpxor %xmm0, %xmm0, %xmm0
- xorl %ebx, %ebx
- vmovdqa %xmm0, (%rsp)
-L_AES_GCM_encrypt_avx2_calc_iv_loop:
- movzbl (%rax,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_avx2_calc_iv_loop
- vmovdqa (%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm0, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpclmulqdq $16, %xmm4, %xmm5, %xmm2
- vpclmulqdq $0x01, %xmm4, %xmm5, %xmm1
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm1, %xmm0, %xmm7
- vpxor %xmm2, %xmm3, %xmm4
- # ghash_mid
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm7, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm4, %xmm4
-L_AES_GCM_encrypt_avx2_calc_iv_done:
- # T = Encrypt counter
- vpxor %xmm0, %xmm0, %xmm0
- shll $3, %edx
- vpinsrq $0x00, %rdx, %xmm0, %xmm0
- vpxor %xmm0, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpclmulqdq $16, %xmm4, %xmm5, %xmm2
- vpclmulqdq $0x01, %xmm4, %xmm5, %xmm1
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm1, %xmm0, %xmm7
- vpxor %xmm2, %xmm3, %xmm4
- # ghash_mid
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm7, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm4, %xmm4
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm4, %xmm4
- # Encrypt counter
- vmovdqa (%rsi), %xmm15
- vpxor %xmm4, %xmm15, %xmm15
- vaesenc 16(%rsi), %xmm15, %xmm15
- vaesenc 32(%rsi), %xmm15, %xmm15
- vaesenc 48(%rsi), %xmm15, %xmm15
- vaesenc 64(%rsi), %xmm15, %xmm15
- vaesenc 80(%rsi), %xmm15, %xmm15
- vaesenc 96(%rsi), %xmm15, %xmm15
- vaesenc 112(%rsi), %xmm15, %xmm15
- vaesenc 128(%rsi), %xmm15, %xmm15
- vaesenc 144(%rsi), %xmm15, %xmm15
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm0
- jl L_AES_GCM_encrypt_avx2_calc_iv_2_aesenc_avx_last
- vaesenc %xmm0, %xmm15, %xmm15
- vaesenc 176(%rsi), %xmm15, %xmm15
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm0
- jl L_AES_GCM_encrypt_avx2_calc_iv_2_aesenc_avx_last
- vaesenc %xmm0, %xmm15, %xmm15
- vaesenc 208(%rsi), %xmm15, %xmm15
- vmovdqa 224(%rsi), %xmm0
-L_AES_GCM_encrypt_avx2_calc_iv_2_aesenc_avx_last:
- vaesenclast %xmm0, %xmm15, %xmm15
- jmp L_AES_GCM_encrypt_avx2_iv_done
-L_AES_GCM_encrypt_avx2_iv_12:
- # # Calculate values when IV is 12 bytes
- # Set counter based on IV
- vmovdqa L_avx2_aes_gcm_bswap_one(%rip), %xmm4
- vmovdqa (%rsi), %xmm5
- vpblendd $7, (%rax), %xmm4, %xmm4
- # H = Encrypt X(=0) and T = Encrypt counter
- vmovdqa 16(%rsi), %xmm7
- vpxor %xmm5, %xmm4, %xmm15
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 32(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 48(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 64(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 80(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 96(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 112(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 128(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 144(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm0
- jl L_AES_GCM_encrypt_avx2_calc_iv_12_last
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 176(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm0
- jl L_AES_GCM_encrypt_avx2_calc_iv_12_last
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 208(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 224(%rsi), %xmm0
-L_AES_GCM_encrypt_avx2_calc_iv_12_last:
- vaesenclast %xmm0, %xmm5, %xmm5
- vaesenclast %xmm0, %xmm15, %xmm15
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm5, %xmm5
-L_AES_GCM_encrypt_avx2_iv_done:
- # Additional authentication data
- movl %r11d, %edx
- cmpl $0x00, %edx
- je L_AES_GCM_encrypt_avx2_calc_aad_done
- xorl %ecx, %ecx
- cmpl $16, %edx
- jl L_AES_GCM_encrypt_avx2_calc_aad_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_encrypt_avx2_calc_aad_16_loop:
- vmovdqu (%r12,%rcx,1), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm0, %xmm6, %xmm6
- # ghash_gfmul_avx
- vpclmulqdq $16, %xmm6, %xmm5, %xmm2
- vpclmulqdq $0x01, %xmm6, %xmm5, %xmm1
- vpclmulqdq $0x00, %xmm6, %xmm5, %xmm0
- vpclmulqdq $0x11, %xmm6, %xmm5, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm1, %xmm0, %xmm7
- vpxor %xmm2, %xmm3, %xmm6
- # ghash_mid
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm6, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm6, %xmm6
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm6, %xmm6
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm6, %xmm6
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm7, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm6, %xmm6
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_avx2_calc_aad_16_loop
- movl %r11d, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_encrypt_avx2_calc_aad_done
-L_AES_GCM_encrypt_avx2_calc_aad_lt16:
- vpxor %xmm0, %xmm0, %xmm0
- xorl %ebx, %ebx
- vmovdqa %xmm0, (%rsp)
-L_AES_GCM_encrypt_avx2_calc_aad_loop:
- movzbl (%r12,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_encrypt_avx2_calc_aad_loop
- vmovdqa (%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm0, %xmm6, %xmm6
- # ghash_gfmul_avx
- vpclmulqdq $16, %xmm6, %xmm5, %xmm2
- vpclmulqdq $0x01, %xmm6, %xmm5, %xmm1
- vpclmulqdq $0x00, %xmm6, %xmm5, %xmm0
- vpclmulqdq $0x11, %xmm6, %xmm5, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm1, %xmm0, %xmm7
- vpxor %xmm2, %xmm3, %xmm6
- # ghash_mid
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm6, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm6, %xmm6
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm6, %xmm6
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm6, %xmm6
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm7, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm6, %xmm6
-L_AES_GCM_encrypt_avx2_calc_aad_done:
- # Calculate counter and H
- vpsrlq $63, %xmm5, %xmm1
- vpsllq $0x01, %xmm5, %xmm0
- vpslldq $8, %xmm1, %xmm1
- vpor %xmm1, %xmm0, %xmm0
- vpshufd $0xff, %xmm5, %xmm5
- vpsrad $31, %xmm5, %xmm5
- vpshufb L_avx2_aes_gcm_bswap_epi64(%rip), %xmm4, %xmm4
- vpand L_avx2_aes_gcm_mod2_128(%rip), %xmm5, %xmm5
- vpaddd L_avx2_aes_gcm_one(%rip), %xmm4, %xmm4
- vpxor %xmm0, %xmm5, %xmm5
- xorl %ebx, %ebx
- cmpl $0x80, %r10d
- movl %r10d, %r13d
- jl L_AES_GCM_encrypt_avx2_done_128
- andl $0xffffff80, %r13d
- vmovdqa %xmm4, 128(%rsp)
- vmovdqa %xmm15, 144(%rsp)
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm3
- # H ^ 1 and H ^ 2
- vpclmulqdq $0x00, %xmm5, %xmm5, %xmm9
- vpclmulqdq $0x11, %xmm5, %xmm5, %xmm10
- vpclmulqdq $16, %xmm3, %xmm9, %xmm8
- vpshufd $0x4e, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpclmulqdq $16, %xmm3, %xmm9, %xmm8
- vpshufd $0x4e, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm9, %xmm10, %xmm0
- vmovdqa %xmm5, (%rsp)
- vmovdqa %xmm0, 16(%rsp)
- # H ^ 3 and H ^ 4
- vpclmulqdq $16, %xmm5, %xmm0, %xmm11
- vpclmulqdq $0x01, %xmm5, %xmm0, %xmm10
- vpclmulqdq $0x00, %xmm5, %xmm0, %xmm9
- vpclmulqdq $0x11, %xmm5, %xmm0, %xmm12
- vpclmulqdq $0x00, %xmm0, %xmm0, %xmm13
- vpclmulqdq $0x11, %xmm0, %xmm0, %xmm14
- vpxor %xmm10, %xmm11, %xmm11
- vpslldq $8, %xmm11, %xmm10
- vpsrldq $8, %xmm11, %xmm11
- vpxor %xmm9, %xmm10, %xmm10
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm9, %xmm10, %xmm10
- vpxor %xmm8, %xmm13, %xmm13
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm11, %xmm12, %xmm12
- vpxor %xmm8, %xmm13, %xmm13
- vpxor %xmm12, %xmm10, %xmm10
- vpxor %xmm14, %xmm13, %xmm2
- vpxor %xmm9, %xmm10, %xmm1
- vmovdqa %xmm1, 32(%rsp)
- vmovdqa %xmm2, 48(%rsp)
- # H ^ 5 and H ^ 6
- vpclmulqdq $16, %xmm0, %xmm1, %xmm11
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm10
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm9
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm12
- vpclmulqdq $0x00, %xmm1, %xmm1, %xmm13
- vpclmulqdq $0x11, %xmm1, %xmm1, %xmm14
- vpxor %xmm10, %xmm11, %xmm11
- vpslldq $8, %xmm11, %xmm10
- vpsrldq $8, %xmm11, %xmm11
- vpxor %xmm9, %xmm10, %xmm10
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm9, %xmm10, %xmm10
- vpxor %xmm8, %xmm13, %xmm13
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm11, %xmm12, %xmm12
- vpxor %xmm8, %xmm13, %xmm13
- vpxor %xmm12, %xmm10, %xmm10
- vpxor %xmm14, %xmm13, %xmm0
- vpxor %xmm9, %xmm10, %xmm7
- vmovdqa %xmm7, 64(%rsp)
- vmovdqa %xmm0, 80(%rsp)
- # H ^ 7 and H ^ 8
- vpclmulqdq $16, %xmm1, %xmm2, %xmm11
- vpclmulqdq $0x01, %xmm1, %xmm2, %xmm10
- vpclmulqdq $0x00, %xmm1, %xmm2, %xmm9
- vpclmulqdq $0x11, %xmm1, %xmm2, %xmm12
- vpclmulqdq $0x00, %xmm2, %xmm2, %xmm13
- vpclmulqdq $0x11, %xmm2, %xmm2, %xmm14
- vpxor %xmm10, %xmm11, %xmm11
- vpslldq $8, %xmm11, %xmm10
- vpsrldq $8, %xmm11, %xmm11
- vpxor %xmm9, %xmm10, %xmm10
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm9, %xmm10, %xmm10
- vpxor %xmm8, %xmm13, %xmm13
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm11, %xmm12, %xmm12
- vpxor %xmm8, %xmm13, %xmm13
- vpxor %xmm12, %xmm10, %xmm10
- vpxor %xmm14, %xmm13, %xmm0
- vpxor %xmm9, %xmm10, %xmm7
- vmovdqa %xmm7, 96(%rsp)
- vmovdqa %xmm0, 112(%rsp)
- # First 128 bytes of input
- # aesenc_128
- # aesenc_ctr
- vmovdqa 128(%rsp), %xmm0
- vmovdqa L_avx2_aes_gcm_bswap_epi64(%rip), %xmm1
- vpaddd L_avx2_aes_gcm_one(%rip), %xmm0, %xmm9
- vpshufb %xmm1, %xmm0, %xmm8
- vpaddd L_avx2_aes_gcm_two(%rip), %xmm0, %xmm10
- vpshufb %xmm1, %xmm9, %xmm9
- vpaddd L_avx2_aes_gcm_three(%rip), %xmm0, %xmm11
- vpshufb %xmm1, %xmm10, %xmm10
- vpaddd L_avx2_aes_gcm_four(%rip), %xmm0, %xmm12
- vpshufb %xmm1, %xmm11, %xmm11
- vpaddd L_avx2_aes_gcm_five(%rip), %xmm0, %xmm13
- vpshufb %xmm1, %xmm12, %xmm12
- vpaddd L_avx2_aes_gcm_six(%rip), %xmm0, %xmm14
- vpshufb %xmm1, %xmm13, %xmm13
- vpaddd L_avx2_aes_gcm_seven(%rip), %xmm0, %xmm15
- vpshufb %xmm1, %xmm14, %xmm14
- vpaddd L_avx2_aes_gcm_eight(%rip), %xmm0, %xmm0
- vpshufb %xmm1, %xmm15, %xmm15
- # aesenc_xor
- vmovdqa (%rsi), %xmm7
- vmovdqa %xmm0, 128(%rsp)
- vpxor %xmm7, %xmm8, %xmm8
- vpxor %xmm7, %xmm9, %xmm9
- vpxor %xmm7, %xmm10, %xmm10
- vpxor %xmm7, %xmm11, %xmm11
- vpxor %xmm7, %xmm12, %xmm12
- vpxor %xmm7, %xmm13, %xmm13
- vpxor %xmm7, %xmm14, %xmm14
- vpxor %xmm7, %xmm15, %xmm15
- vmovdqa 16(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 32(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 48(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 64(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 80(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 96(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 112(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 128(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 144(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm7
- jl L_AES_GCM_encrypt_avx2_aesenc_128_enc_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 176(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm7
- jl L_AES_GCM_encrypt_avx2_aesenc_128_enc_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 208(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 224(%rsi), %xmm7
-L_AES_GCM_encrypt_avx2_aesenc_128_enc_done:
- # aesenc_last
- vaesenclast %xmm7, %xmm8, %xmm8
- vaesenclast %xmm7, %xmm9, %xmm9
- vaesenclast %xmm7, %xmm10, %xmm10
- vaesenclast %xmm7, %xmm11, %xmm11
- vmovdqu (%rdi), %xmm0
- vmovdqu 16(%rdi), %xmm1
- vmovdqu 32(%rdi), %xmm2
- vmovdqu 48(%rdi), %xmm3
- vpxor %xmm0, %xmm8, %xmm8
- vpxor %xmm1, %xmm9, %xmm9
- vpxor %xmm2, %xmm10, %xmm10
- vpxor %xmm3, %xmm11, %xmm11
- vmovdqu %xmm8, (%r8)
- vmovdqu %xmm9, 16(%r8)
- vmovdqu %xmm10, 32(%r8)
- vmovdqu %xmm11, 48(%r8)
- vaesenclast %xmm7, %xmm12, %xmm12
- vaesenclast %xmm7, %xmm13, %xmm13
- vaesenclast %xmm7, %xmm14, %xmm14
- vaesenclast %xmm7, %xmm15, %xmm15
- vmovdqu 64(%rdi), %xmm0
- vmovdqu 80(%rdi), %xmm1
- vmovdqu 96(%rdi), %xmm2
- vmovdqu 112(%rdi), %xmm3
- vpxor %xmm0, %xmm12, %xmm12
- vpxor %xmm1, %xmm13, %xmm13
- vpxor %xmm2, %xmm14, %xmm14
- vpxor %xmm3, %xmm15, %xmm15
- vmovdqu %xmm12, 64(%r8)
- vmovdqu %xmm13, 80(%r8)
- vmovdqu %xmm14, 96(%r8)
- vmovdqu %xmm15, 112(%r8)
- cmpl $0x80, %r13d
- movl $0x80, %ebx
- jle L_AES_GCM_encrypt_avx2_end_128
- # More 128 bytes of input
-L_AES_GCM_encrypt_avx2_ghash_128:
- # aesenc_128_ghash
- leaq (%rdi,%rbx,1), %rcx
- leaq (%r8,%rbx,1), %rdx
- # aesenc_ctr
- vmovdqa 128(%rsp), %xmm0
- vmovdqa L_avx2_aes_gcm_bswap_epi64(%rip), %xmm1
- vpaddd L_avx2_aes_gcm_one(%rip), %xmm0, %xmm9
- vpshufb %xmm1, %xmm0, %xmm8
- vpaddd L_avx2_aes_gcm_two(%rip), %xmm0, %xmm10
- vpshufb %xmm1, %xmm9, %xmm9
- vpaddd L_avx2_aes_gcm_three(%rip), %xmm0, %xmm11
- vpshufb %xmm1, %xmm10, %xmm10
- vpaddd L_avx2_aes_gcm_four(%rip), %xmm0, %xmm12
- vpshufb %xmm1, %xmm11, %xmm11
- vpaddd L_avx2_aes_gcm_five(%rip), %xmm0, %xmm13
- vpshufb %xmm1, %xmm12, %xmm12
- vpaddd L_avx2_aes_gcm_six(%rip), %xmm0, %xmm14
- vpshufb %xmm1, %xmm13, %xmm13
- vpaddd L_avx2_aes_gcm_seven(%rip), %xmm0, %xmm15
- vpshufb %xmm1, %xmm14, %xmm14
- vpaddd L_avx2_aes_gcm_eight(%rip), %xmm0, %xmm0
- vpshufb %xmm1, %xmm15, %xmm15
- # aesenc_xor
- vmovdqa (%rsi), %xmm7
- vmovdqa %xmm0, 128(%rsp)
- vpxor %xmm7, %xmm8, %xmm8
- vpxor %xmm7, %xmm9, %xmm9
- vpxor %xmm7, %xmm10, %xmm10
- vpxor %xmm7, %xmm11, %xmm11
- vpxor %xmm7, %xmm12, %xmm12
- vpxor %xmm7, %xmm13, %xmm13
- vpxor %xmm7, %xmm14, %xmm14
- vpxor %xmm7, %xmm15, %xmm15
- # aesenc_pclmul_1
- vmovdqu -128(%rdx), %xmm1
- vmovdqu 16(%rsi), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vmovdqa 112(%rsp), %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm5
- vpclmulqdq $0x01, %xmm2, %xmm1, %xmm3
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm6
- vpclmulqdq $0x11, %xmm2, %xmm1, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_2
- vmovdqu -112(%rdx), %xmm1
- vmovdqa 96(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 32(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu -96(%rdx), %xmm1
- vmovdqa 80(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 48(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu -80(%rdx), %xmm1
- vmovdqa 64(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 64(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu -64(%rdx), %xmm1
- vmovdqa 48(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 80(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu -48(%rdx), %xmm1
- vmovdqa 32(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 96(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu -32(%rdx), %xmm1
- vmovdqa 16(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 112(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu -16(%rdx), %xmm1
- vmovdqa (%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 128(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_l
- vpxor %xmm2, %xmm5, %xmm5
- vpxor %xmm4, %xmm6, %xmm6
- vpxor %xmm3, %xmm5, %xmm5
- vpslldq $8, %xmm5, %xmm1
- vpsrldq $8, %xmm5, %xmm5
- vmovdqa 144(%rsi), %xmm4
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm0
- vaesenc %xmm4, %xmm8, %xmm8
- vpxor %xmm1, %xmm6, %xmm6
- vpxor %xmm5, %xmm7, %xmm7
- vpclmulqdq $16, %xmm0, %xmm6, %xmm3
- vaesenc %xmm4, %xmm9, %xmm9
- vaesenc %xmm4, %xmm10, %xmm10
- vaesenc %xmm4, %xmm11, %xmm11
- vpshufd $0x4e, %xmm6, %xmm6
- vpxor %xmm3, %xmm6, %xmm6
- vpclmulqdq $16, %xmm0, %xmm6, %xmm3
- vaesenc %xmm4, %xmm12, %xmm12
- vaesenc %xmm4, %xmm13, %xmm13
- vaesenc %xmm4, %xmm14, %xmm14
- vpshufd $0x4e, %xmm6, %xmm6
- vpxor %xmm3, %xmm6, %xmm6
- vpxor %xmm7, %xmm6, %xmm6
- vaesenc %xmm4, %xmm15, %xmm15
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm7
- jl L_AES_GCM_encrypt_avx2_aesenc_128_ghash_avx_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 176(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm7
- jl L_AES_GCM_encrypt_avx2_aesenc_128_ghash_avx_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 208(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 224(%rsi), %xmm7
-L_AES_GCM_encrypt_avx2_aesenc_128_ghash_avx_done:
- # aesenc_last
- vaesenclast %xmm7, %xmm8, %xmm8
- vaesenclast %xmm7, %xmm9, %xmm9
- vaesenclast %xmm7, %xmm10, %xmm10
- vaesenclast %xmm7, %xmm11, %xmm11
- vmovdqu (%rcx), %xmm0
- vmovdqu 16(%rcx), %xmm1
- vmovdqu 32(%rcx), %xmm2
- vmovdqu 48(%rcx), %xmm3
- vpxor %xmm0, %xmm8, %xmm8
- vpxor %xmm1, %xmm9, %xmm9
- vpxor %xmm2, %xmm10, %xmm10
- vpxor %xmm3, %xmm11, %xmm11
- vmovdqu %xmm8, (%rdx)
- vmovdqu %xmm9, 16(%rdx)
- vmovdqu %xmm10, 32(%rdx)
- vmovdqu %xmm11, 48(%rdx)
- vaesenclast %xmm7, %xmm12, %xmm12
- vaesenclast %xmm7, %xmm13, %xmm13
- vaesenclast %xmm7, %xmm14, %xmm14
- vaesenclast %xmm7, %xmm15, %xmm15
- vmovdqu 64(%rcx), %xmm0
- vmovdqu 80(%rcx), %xmm1
- vmovdqu 96(%rcx), %xmm2
- vmovdqu 112(%rcx), %xmm3
- vpxor %xmm0, %xmm12, %xmm12
- vpxor %xmm1, %xmm13, %xmm13
- vpxor %xmm2, %xmm14, %xmm14
- vpxor %xmm3, %xmm15, %xmm15
- vmovdqu %xmm12, 64(%rdx)
- vmovdqu %xmm13, 80(%rdx)
- vmovdqu %xmm14, 96(%rdx)
- vmovdqu %xmm15, 112(%rdx)
- # aesenc_128_ghash - end
- addl $0x80, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_encrypt_avx2_ghash_128
-L_AES_GCM_encrypt_avx2_end_128:
- vmovdqa L_avx2_aes_gcm_bswap_mask(%rip), %xmm4
- vpshufb %xmm4, %xmm8, %xmm8
- vpshufb %xmm4, %xmm9, %xmm9
- vpshufb %xmm4, %xmm10, %xmm10
- vpshufb %xmm4, %xmm11, %xmm11
- vpshufb %xmm4, %xmm12, %xmm12
- vpshufb %xmm4, %xmm13, %xmm13
- vpshufb %xmm4, %xmm14, %xmm14
- vpshufb %xmm4, %xmm15, %xmm15
- vpxor %xmm6, %xmm8, %xmm8
- vmovdqu (%rsp), %xmm7
- vpclmulqdq $16, %xmm15, %xmm7, %xmm5
- vpclmulqdq $0x01, %xmm15, %xmm7, %xmm1
- vpclmulqdq $0x00, %xmm15, %xmm7, %xmm4
- vpclmulqdq $0x11, %xmm15, %xmm7, %xmm6
- vpxor %xmm1, %xmm5, %xmm5
- vmovdqu 16(%rsp), %xmm7
- vpclmulqdq $16, %xmm14, %xmm7, %xmm2
- vpclmulqdq $0x01, %xmm14, %xmm7, %xmm1
- vpclmulqdq $0x00, %xmm14, %xmm7, %xmm0
- vpclmulqdq $0x11, %xmm14, %xmm7, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm3, %xmm6, %xmm6
- vpxor %xmm2, %xmm5, %xmm5
- vpxor %xmm0, %xmm4, %xmm4
- vmovdqu 32(%rsp), %xmm15
- vmovdqu 48(%rsp), %xmm7
- vpclmulqdq $16, %xmm13, %xmm15, %xmm2
- vpclmulqdq $0x01, %xmm13, %xmm15, %xmm1
- vpclmulqdq $0x00, %xmm13, %xmm15, %xmm0
- vpclmulqdq $0x11, %xmm13, %xmm15, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm3, %xmm6, %xmm6
- vpxor %xmm2, %xmm5, %xmm5
- vpxor %xmm0, %xmm4, %xmm4
- vpclmulqdq $16, %xmm12, %xmm7, %xmm2
- vpclmulqdq $0x01, %xmm12, %xmm7, %xmm1
- vpclmulqdq $0x00, %xmm12, %xmm7, %xmm0
- vpclmulqdq $0x11, %xmm12, %xmm7, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm3, %xmm6, %xmm6
- vpxor %xmm2, %xmm5, %xmm5
- vpxor %xmm0, %xmm4, %xmm4
- vmovdqu 64(%rsp), %xmm15
- vmovdqu 80(%rsp), %xmm7
- vpclmulqdq $16, %xmm11, %xmm15, %xmm2
- vpclmulqdq $0x01, %xmm11, %xmm15, %xmm1
- vpclmulqdq $0x00, %xmm11, %xmm15, %xmm0
- vpclmulqdq $0x11, %xmm11, %xmm15, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm3, %xmm6, %xmm6
- vpxor %xmm2, %xmm5, %xmm5
- vpxor %xmm0, %xmm4, %xmm4
- vpclmulqdq $16, %xmm10, %xmm7, %xmm2
- vpclmulqdq $0x01, %xmm10, %xmm7, %xmm1
- vpclmulqdq $0x00, %xmm10, %xmm7, %xmm0
- vpclmulqdq $0x11, %xmm10, %xmm7, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm3, %xmm6, %xmm6
- vpxor %xmm2, %xmm5, %xmm5
- vpxor %xmm0, %xmm4, %xmm4
- vmovdqu 96(%rsp), %xmm15
- vmovdqu 112(%rsp), %xmm7
- vpclmulqdq $16, %xmm9, %xmm15, %xmm2
- vpclmulqdq $0x01, %xmm9, %xmm15, %xmm1
- vpclmulqdq $0x00, %xmm9, %xmm15, %xmm0
- vpclmulqdq $0x11, %xmm9, %xmm15, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm3, %xmm6, %xmm6
- vpxor %xmm2, %xmm5, %xmm5
- vpxor %xmm0, %xmm4, %xmm4
- vpclmulqdq $16, %xmm8, %xmm7, %xmm2
- vpclmulqdq $0x01, %xmm8, %xmm7, %xmm1
- vpclmulqdq $0x00, %xmm8, %xmm7, %xmm0
- vpclmulqdq $0x11, %xmm8, %xmm7, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpxor %xmm3, %xmm6, %xmm6
- vpxor %xmm2, %xmm5, %xmm5
- vpxor %xmm0, %xmm4, %xmm4
- vpslldq $8, %xmm5, %xmm7
- vpsrldq $8, %xmm5, %xmm5
- vpxor %xmm7, %xmm4, %xmm4
- vpxor %xmm5, %xmm6, %xmm6
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm4, %xmm0
- vpshufd $0x4e, %xmm4, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm6, %xmm6
- vmovdqa (%rsp), %xmm5
- vmovdqu 128(%rsp), %xmm4
- vmovdqu 144(%rsp), %xmm15
-L_AES_GCM_encrypt_avx2_done_128:
- cmpl %r10d, %ebx
- je L_AES_GCM_encrypt_avx2_done_enc
- movl %r10d, %r13d
- andl $0xfffffff0, %r13d
- cmpl %r13d, %ebx
- jge L_AES_GCM_encrypt_avx2_last_block_done
- # aesenc_block
- vmovdqa %xmm4, %xmm1
- vpshufb L_avx2_aes_gcm_bswap_epi64(%rip), %xmm1, %xmm0
- vpaddd L_avx2_aes_gcm_one(%rip), %xmm1, %xmm1
- vpxor (%rsi), %xmm0, %xmm0
- vmovdqa 16(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- vmovdqa 32(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- vmovdqa 48(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- vmovdqa 64(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- vmovdqa 80(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- vmovdqa 96(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- vmovdqa 112(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- vmovdqa 128(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- vmovdqa 144(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- vmovdqa %xmm1, %xmm4
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm1
- jl L_AES_GCM_encrypt_avx2_aesenc_block_last
- vaesenc %xmm1, %xmm0, %xmm0
- vmovdqa 176(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm1
- jl L_AES_GCM_encrypt_avx2_aesenc_block_last
- vaesenc %xmm1, %xmm0, %xmm0
- vmovdqa 208(%rsi), %xmm2
- vaesenc %xmm2, %xmm0, %xmm0
- vmovdqa 224(%rsi), %xmm1
-L_AES_GCM_encrypt_avx2_aesenc_block_last:
- vaesenclast %xmm1, %xmm0, %xmm0
- vmovdqu (%rdi,%rbx,1), %xmm1
- vpxor %xmm1, %xmm0, %xmm0
- vmovdqu %xmm0, (%r8,%rbx,1)
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm0, %xmm6, %xmm6
- addl $16, %ebx
- cmpl %r13d, %ebx
- jge L_AES_GCM_encrypt_avx2_last_block_ghash
-L_AES_GCM_encrypt_avx2_last_block_start:
- vmovdqu (%rdi,%rbx,1), %xmm12
- vpshufb L_avx2_aes_gcm_bswap_epi64(%rip), %xmm4, %xmm11
- vpaddd L_avx2_aes_gcm_one(%rip), %xmm4, %xmm4
- # aesenc_gfmul_sb
- vpclmulqdq $0x01, %xmm5, %xmm6, %xmm2
- vpclmulqdq $16, %xmm5, %xmm6, %xmm3
- vpclmulqdq $0x00, %xmm5, %xmm6, %xmm1
- vpclmulqdq $0x11, %xmm5, %xmm6, %xmm8
- vpxor (%rsi), %xmm11, %xmm11
- vaesenc 16(%rsi), %xmm11, %xmm11
- vpxor %xmm2, %xmm3, %xmm3
- vpslldq $8, %xmm3, %xmm2
- vpsrldq $8, %xmm3, %xmm3
- vaesenc 32(%rsi), %xmm11, %xmm11
- vpxor %xmm1, %xmm2, %xmm2
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm2, %xmm1
- vaesenc 48(%rsi), %xmm11, %xmm11
- vaesenc 64(%rsi), %xmm11, %xmm11
- vaesenc 80(%rsi), %xmm11, %xmm11
- vpshufd $0x4e, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm2, %xmm1
- vaesenc 96(%rsi), %xmm11, %xmm11
- vaesenc 112(%rsi), %xmm11, %xmm11
- vaesenc 128(%rsi), %xmm11, %xmm11
- vpshufd $0x4e, %xmm2, %xmm2
- vaesenc 144(%rsi), %xmm11, %xmm11
- vpxor %xmm3, %xmm8, %xmm8
- vpxor %xmm8, %xmm2, %xmm2
- vmovdqa 160(%rsi), %xmm0
- cmpl $11, %r9d
- jl L_AES_GCM_encrypt_avx2_aesenc_gfmul_sb_last
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc 176(%rsi), %xmm11, %xmm11
- vmovdqa 192(%rsi), %xmm0
- cmpl $13, %r9d
- jl L_AES_GCM_encrypt_avx2_aesenc_gfmul_sb_last
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc 208(%rsi), %xmm11, %xmm11
- vmovdqa 224(%rsi), %xmm0
-L_AES_GCM_encrypt_avx2_aesenc_gfmul_sb_last:
- vaesenclast %xmm0, %xmm11, %xmm11
- vpxor %xmm1, %xmm2, %xmm6
- vpxor %xmm12, %xmm11, %xmm11
- vmovdqu %xmm11, (%r8,%rbx,1)
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm11, %xmm11
- vpxor %xmm11, %xmm6, %xmm6
- addl $16, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_encrypt_avx2_last_block_start
-L_AES_GCM_encrypt_avx2_last_block_ghash:
- # ghash_gfmul_red
- vpclmulqdq $16, %xmm5, %xmm6, %xmm10
- vpclmulqdq $0x01, %xmm5, %xmm6, %xmm9
- vpclmulqdq $0x00, %xmm5, %xmm6, %xmm8
- vpxor %xmm9, %xmm10, %xmm10
- vpslldq $8, %xmm10, %xmm9
- vpsrldq $8, %xmm10, %xmm10
- vpxor %xmm8, %xmm9, %xmm9
- vpclmulqdq $0x11, %xmm5, %xmm6, %xmm6
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm9, %xmm8
- vpshufd $0x4e, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm9, %xmm8
- vpshufd $0x4e, %xmm9, %xmm9
- vpxor %xmm10, %xmm6, %xmm6
- vpxor %xmm9, %xmm6, %xmm6
- vpxor %xmm8, %xmm6, %xmm6
-L_AES_GCM_encrypt_avx2_last_block_done:
- movl %r10d, %ecx
- movl %r10d, %edx
- andl $15, %ecx
- jz L_AES_GCM_encrypt_avx2_done_enc
- # aesenc_last15_enc
- vpshufb L_avx2_aes_gcm_bswap_epi64(%rip), %xmm4, %xmm4
- vpxor (%rsi), %xmm4, %xmm4
- vaesenc 16(%rsi), %xmm4, %xmm4
- vaesenc 32(%rsi), %xmm4, %xmm4
- vaesenc 48(%rsi), %xmm4, %xmm4
- vaesenc 64(%rsi), %xmm4, %xmm4
- vaesenc 80(%rsi), %xmm4, %xmm4
- vaesenc 96(%rsi), %xmm4, %xmm4
- vaesenc 112(%rsi), %xmm4, %xmm4
- vaesenc 128(%rsi), %xmm4, %xmm4
- vaesenc 144(%rsi), %xmm4, %xmm4
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm0
- jl L_AES_GCM_encrypt_avx2_aesenc_last15_enc_avx_aesenc_avx_last
- vaesenc %xmm0, %xmm4, %xmm4
- vaesenc 176(%rsi), %xmm4, %xmm4
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm0
- jl L_AES_GCM_encrypt_avx2_aesenc_last15_enc_avx_aesenc_avx_last
- vaesenc %xmm0, %xmm4, %xmm4
- vaesenc 208(%rsi), %xmm4, %xmm4
- vmovdqa 224(%rsi), %xmm0
-L_AES_GCM_encrypt_avx2_aesenc_last15_enc_avx_aesenc_avx_last:
- vaesenclast %xmm0, %xmm4, %xmm4
- xorl %ecx, %ecx
- vpxor %xmm0, %xmm0, %xmm0
- vmovdqa %xmm4, (%rsp)
- vmovdqa %xmm0, 16(%rsp)
-L_AES_GCM_encrypt_avx2_aesenc_last15_enc_avx_loop:
- movzbl (%rdi,%rbx,1), %r13d
- xorb (%rsp,%rcx,1), %r13b
- movb %r13b, 16(%rsp,%rcx,1)
- movb %r13b, (%r8,%rbx,1)
- incl %ebx
- incl %ecx
- cmpl %edx, %ebx
- jl L_AES_GCM_encrypt_avx2_aesenc_last15_enc_avx_loop
-L_AES_GCM_encrypt_avx2_aesenc_last15_enc_avx_finish_enc:
- vmovdqa 16(%rsp), %xmm4
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm4, %xmm4
- vpxor %xmm4, %xmm6, %xmm6
- # ghash_gfmul_red
- vpclmulqdq $16, %xmm5, %xmm6, %xmm2
- vpclmulqdq $0x01, %xmm5, %xmm6, %xmm1
- vpclmulqdq $0x00, %xmm5, %xmm6, %xmm0
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $0x11, %xmm5, %xmm6, %xmm6
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm2, %xmm6, %xmm6
- vpxor %xmm1, %xmm6, %xmm6
- vpxor %xmm0, %xmm6, %xmm6
-L_AES_GCM_encrypt_avx2_done_enc:
- # calc_tag
- shlq $3, %r10
- vpinsrq $0x00, %r10, %xmm0, %xmm0
- shlq $3, %r11
- vpinsrq $0x01, %r11, %xmm1, %xmm1
- vpblendd $12, %xmm1, %xmm0, %xmm0
- vpxor %xmm6, %xmm0, %xmm0
- # ghash_gfmul_red
- vpclmulqdq $16, %xmm5, %xmm0, %xmm4
- vpclmulqdq $0x01, %xmm5, %xmm0, %xmm3
- vpclmulqdq $0x00, %xmm5, %xmm0, %xmm2
- vpxor %xmm3, %xmm4, %xmm4
- vpslldq $8, %xmm4, %xmm3
- vpsrldq $8, %xmm4, %xmm4
- vpxor %xmm2, %xmm3, %xmm3
- vpclmulqdq $0x11, %xmm5, %xmm0, %xmm0
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm3, %xmm2
- vpshufd $0x4e, %xmm3, %xmm3
- vpxor %xmm2, %xmm3, %xmm3
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm3, %xmm2
- vpshufd $0x4e, %xmm3, %xmm3
- vpxor %xmm4, %xmm0, %xmm0
- vpxor %xmm3, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm15, %xmm0, %xmm0
- # store_tag
- cmpl $16, %r14d
- je L_AES_GCM_encrypt_avx2_store_tag_16
- xorq %rcx, %rcx
- vmovdqa %xmm0, (%rsp)
-L_AES_GCM_encrypt_avx2_store_tag_loop:
- movzbl (%rsp,%rcx,1), %r13d
- movb %r13b, (%r15,%rcx,1)
- incl %ecx
- cmpl %r14d, %ecx
- jne L_AES_GCM_encrypt_avx2_store_tag_loop
- jmp L_AES_GCM_encrypt_avx2_store_tag_done
-L_AES_GCM_encrypt_avx2_store_tag_16:
- vmovdqu %xmm0, (%r15)
-L_AES_GCM_encrypt_avx2_store_tag_done:
- vzeroupper
- addq $0xa0, %rsp
- popq %r14
- popq %rbx
- popq %r15
- popq %r12
- popq %r13
- repz retq
-#ifndef __APPLE__
-.size AES_GCM_encrypt_avx2,.-AES_GCM_encrypt_avx2
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.text
-.globl AES_GCM_decrypt_avx2
-.type AES_GCM_decrypt_avx2,@function
-.align 4
-AES_GCM_decrypt_avx2:
-#else
-.section __TEXT,__text
-.globl _AES_GCM_decrypt_avx2
-.p2align 2
-_AES_GCM_decrypt_avx2:
-#endif /* __APPLE__ */
- pushq %r13
- pushq %r12
- pushq %r14
- pushq %rbx
- pushq %r15
- pushq %rbp
- movq %rdx, %r12
- movq %rcx, %rax
- movq %r8, %r14
- movq %rsi, %r8
- movl %r9d, %r10d
- movl 56(%rsp), %r11d
- movl 64(%rsp), %ebx
- movl 72(%rsp), %r15d
- movq 80(%rsp), %rsi
- movl 88(%rsp), %r9d
- movq 96(%rsp), %rbp
- subq $0xa8, %rsp
- vpxor %xmm4, %xmm4, %xmm4
- vpxor %xmm6, %xmm6, %xmm6
- movl %ebx, %edx
- cmpl $12, %edx
- je L_AES_GCM_decrypt_avx2_iv_12
- # Calculate values when IV is not 12 bytes
- # H = Encrypt X(=0)
- vmovdqa (%rsi), %xmm5
- vaesenc 16(%rsi), %xmm5, %xmm5
- vaesenc 32(%rsi), %xmm5, %xmm5
- vaesenc 48(%rsi), %xmm5, %xmm5
- vaesenc 64(%rsi), %xmm5, %xmm5
- vaesenc 80(%rsi), %xmm5, %xmm5
- vaesenc 96(%rsi), %xmm5, %xmm5
- vaesenc 112(%rsi), %xmm5, %xmm5
- vaesenc 128(%rsi), %xmm5, %xmm5
- vaesenc 144(%rsi), %xmm5, %xmm5
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm0
- jl L_AES_GCM_decrypt_avx2_calc_iv_1_aesenc_avx_last
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc 176(%rsi), %xmm5, %xmm5
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm0
- jl L_AES_GCM_decrypt_avx2_calc_iv_1_aesenc_avx_last
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc 208(%rsi), %xmm5, %xmm5
- vmovdqa 224(%rsi), %xmm0
-L_AES_GCM_decrypt_avx2_calc_iv_1_aesenc_avx_last:
- vaesenclast %xmm0, %xmm5, %xmm5
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm5, %xmm5
- # Calc counter
- # Initialization vector
- cmpl $0x00, %edx
- movq $0x00, %rcx
- je L_AES_GCM_decrypt_avx2_calc_iv_done
- cmpl $16, %edx
- jl L_AES_GCM_decrypt_avx2_calc_iv_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_decrypt_avx2_calc_iv_16_loop:
- vmovdqu (%rax,%rcx,1), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm0, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpclmulqdq $16, %xmm4, %xmm5, %xmm2
- vpclmulqdq $0x01, %xmm4, %xmm5, %xmm1
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm1, %xmm0, %xmm7
- vpxor %xmm2, %xmm3, %xmm4
- # ghash_mid
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm7, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm4, %xmm4
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_avx2_calc_iv_16_loop
- movl %ebx, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_decrypt_avx2_calc_iv_done
-L_AES_GCM_decrypt_avx2_calc_iv_lt16:
- vpxor %xmm0, %xmm0, %xmm0
- xorl %ebx, %ebx
- vmovdqa %xmm0, (%rsp)
-L_AES_GCM_decrypt_avx2_calc_iv_loop:
- movzbl (%rax,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_avx2_calc_iv_loop
- vmovdqa (%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm0, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpclmulqdq $16, %xmm4, %xmm5, %xmm2
- vpclmulqdq $0x01, %xmm4, %xmm5, %xmm1
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm1, %xmm0, %xmm7
- vpxor %xmm2, %xmm3, %xmm4
- # ghash_mid
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm7, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm4, %xmm4
-L_AES_GCM_decrypt_avx2_calc_iv_done:
- # T = Encrypt counter
- vpxor %xmm0, %xmm0, %xmm0
- shll $3, %edx
- vpinsrq $0x00, %rdx, %xmm0, %xmm0
- vpxor %xmm0, %xmm4, %xmm4
- # ghash_gfmul_avx
- vpclmulqdq $16, %xmm4, %xmm5, %xmm2
- vpclmulqdq $0x01, %xmm4, %xmm5, %xmm1
- vpclmulqdq $0x00, %xmm4, %xmm5, %xmm0
- vpclmulqdq $0x11, %xmm4, %xmm5, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm1, %xmm0, %xmm7
- vpxor %xmm2, %xmm3, %xmm4
- # ghash_mid
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm4, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm4, %xmm4
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm4, %xmm4
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm4, %xmm4
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm7, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm4, %xmm4
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm4, %xmm4
- # Encrypt counter
- vmovdqa (%rsi), %xmm15
- vpxor %xmm4, %xmm15, %xmm15
- vaesenc 16(%rsi), %xmm15, %xmm15
- vaesenc 32(%rsi), %xmm15, %xmm15
- vaesenc 48(%rsi), %xmm15, %xmm15
- vaesenc 64(%rsi), %xmm15, %xmm15
- vaesenc 80(%rsi), %xmm15, %xmm15
- vaesenc 96(%rsi), %xmm15, %xmm15
- vaesenc 112(%rsi), %xmm15, %xmm15
- vaesenc 128(%rsi), %xmm15, %xmm15
- vaesenc 144(%rsi), %xmm15, %xmm15
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm0
- jl L_AES_GCM_decrypt_avx2_calc_iv_2_aesenc_avx_last
- vaesenc %xmm0, %xmm15, %xmm15
- vaesenc 176(%rsi), %xmm15, %xmm15
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm0
- jl L_AES_GCM_decrypt_avx2_calc_iv_2_aesenc_avx_last
- vaesenc %xmm0, %xmm15, %xmm15
- vaesenc 208(%rsi), %xmm15, %xmm15
- vmovdqa 224(%rsi), %xmm0
-L_AES_GCM_decrypt_avx2_calc_iv_2_aesenc_avx_last:
- vaesenclast %xmm0, %xmm15, %xmm15
- jmp L_AES_GCM_decrypt_avx2_iv_done
-L_AES_GCM_decrypt_avx2_iv_12:
- # # Calculate values when IV is 12 bytes
- # Set counter based on IV
- vmovdqa L_avx2_aes_gcm_bswap_one(%rip), %xmm4
- vmovdqa (%rsi), %xmm5
- vpblendd $7, (%rax), %xmm4, %xmm4
- # H = Encrypt X(=0) and T = Encrypt counter
- vmovdqa 16(%rsi), %xmm7
- vpxor %xmm5, %xmm4, %xmm15
- vaesenc %xmm7, %xmm5, %xmm5
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 32(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 48(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 64(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 80(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 96(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 112(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 128(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 144(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm0
- jl L_AES_GCM_decrypt_avx2_calc_iv_12_last
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 176(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm0
- jl L_AES_GCM_decrypt_avx2_calc_iv_12_last
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 208(%rsi), %xmm0
- vaesenc %xmm0, %xmm5, %xmm5
- vaesenc %xmm0, %xmm15, %xmm15
- vmovdqa 224(%rsi), %xmm0
-L_AES_GCM_decrypt_avx2_calc_iv_12_last:
- vaesenclast %xmm0, %xmm5, %xmm5
- vaesenclast %xmm0, %xmm15, %xmm15
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm5, %xmm5
-L_AES_GCM_decrypt_avx2_iv_done:
- # Additional authentication data
- movl %r11d, %edx
- cmpl $0x00, %edx
- je L_AES_GCM_decrypt_avx2_calc_aad_done
- xorl %ecx, %ecx
- cmpl $16, %edx
- jl L_AES_GCM_decrypt_avx2_calc_aad_lt16
- andl $0xfffffff0, %edx
-L_AES_GCM_decrypt_avx2_calc_aad_16_loop:
- vmovdqu (%r12,%rcx,1), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm0, %xmm6, %xmm6
- # ghash_gfmul_avx
- vpclmulqdq $16, %xmm6, %xmm5, %xmm2
- vpclmulqdq $0x01, %xmm6, %xmm5, %xmm1
- vpclmulqdq $0x00, %xmm6, %xmm5, %xmm0
- vpclmulqdq $0x11, %xmm6, %xmm5, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm1, %xmm0, %xmm7
- vpxor %xmm2, %xmm3, %xmm6
- # ghash_mid
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm6, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm6, %xmm6
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm6, %xmm6
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm6, %xmm6
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm7, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm6, %xmm6
- addl $16, %ecx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_avx2_calc_aad_16_loop
- movl %r11d, %edx
- cmpl %edx, %ecx
- je L_AES_GCM_decrypt_avx2_calc_aad_done
-L_AES_GCM_decrypt_avx2_calc_aad_lt16:
- vpxor %xmm0, %xmm0, %xmm0
- xorl %ebx, %ebx
- vmovdqa %xmm0, (%rsp)
-L_AES_GCM_decrypt_avx2_calc_aad_loop:
- movzbl (%r12,%rcx,1), %r13d
- movb %r13b, (%rsp,%rbx,1)
- incl %ecx
- incl %ebx
- cmpl %edx, %ecx
- jl L_AES_GCM_decrypt_avx2_calc_aad_loop
- vmovdqa (%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm0, %xmm6, %xmm6
- # ghash_gfmul_avx
- vpclmulqdq $16, %xmm6, %xmm5, %xmm2
- vpclmulqdq $0x01, %xmm6, %xmm5, %xmm1
- vpclmulqdq $0x00, %xmm6, %xmm5, %xmm0
- vpclmulqdq $0x11, %xmm6, %xmm5, %xmm3
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm1, %xmm0, %xmm7
- vpxor %xmm2, %xmm3, %xmm6
- # ghash_mid
- vpsrld $31, %xmm7, %xmm0
- vpsrld $31, %xmm6, %xmm1
- vpslld $0x01, %xmm7, %xmm7
- vpslld $0x01, %xmm6, %xmm6
- vpsrldq $12, %xmm0, %xmm2
- vpslldq $4, %xmm0, %xmm0
- vpslldq $4, %xmm1, %xmm1
- vpor %xmm2, %xmm6, %xmm6
- vpor %xmm0, %xmm7, %xmm7
- vpor %xmm1, %xmm6, %xmm6
- # ghash_red
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm2
- vpclmulqdq $16, %xmm2, %xmm7, %xmm0
- vpshufd $0x4e, %xmm7, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpxor %xmm1, %xmm6, %xmm6
-L_AES_GCM_decrypt_avx2_calc_aad_done:
- # Calculate counter and H
- vpsrlq $63, %xmm5, %xmm1
- vpsllq $0x01, %xmm5, %xmm0
- vpslldq $8, %xmm1, %xmm1
- vpor %xmm1, %xmm0, %xmm0
- vpshufd $0xff, %xmm5, %xmm5
- vpsrad $31, %xmm5, %xmm5
- vpshufb L_avx2_aes_gcm_bswap_epi64(%rip), %xmm4, %xmm4
- vpand L_avx2_aes_gcm_mod2_128(%rip), %xmm5, %xmm5
- vpaddd L_avx2_aes_gcm_one(%rip), %xmm4, %xmm4
- vpxor %xmm0, %xmm5, %xmm5
- xorl %ebx, %ebx
- cmpl $0x80, %r10d
- movl %r10d, %r13d
- jl L_AES_GCM_decrypt_avx2_done_128
- andl $0xffffff80, %r13d
- vmovdqa %xmm4, 128(%rsp)
- vmovdqa %xmm15, 144(%rsp)
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm3
- # H ^ 1 and H ^ 2
- vpclmulqdq $0x00, %xmm5, %xmm5, %xmm9
- vpclmulqdq $0x11, %xmm5, %xmm5, %xmm10
- vpclmulqdq $16, %xmm3, %xmm9, %xmm8
- vpshufd $0x4e, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpclmulqdq $16, %xmm3, %xmm9, %xmm8
- vpshufd $0x4e, %xmm9, %xmm9
- vpxor %xmm8, %xmm9, %xmm9
- vpxor %xmm9, %xmm10, %xmm0
- vmovdqa %xmm5, (%rsp)
- vmovdqa %xmm0, 16(%rsp)
- # H ^ 3 and H ^ 4
- vpclmulqdq $16, %xmm5, %xmm0, %xmm11
- vpclmulqdq $0x01, %xmm5, %xmm0, %xmm10
- vpclmulqdq $0x00, %xmm5, %xmm0, %xmm9
- vpclmulqdq $0x11, %xmm5, %xmm0, %xmm12
- vpclmulqdq $0x00, %xmm0, %xmm0, %xmm13
- vpclmulqdq $0x11, %xmm0, %xmm0, %xmm14
- vpxor %xmm10, %xmm11, %xmm11
- vpslldq $8, %xmm11, %xmm10
- vpsrldq $8, %xmm11, %xmm11
- vpxor %xmm9, %xmm10, %xmm10
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm9, %xmm10, %xmm10
- vpxor %xmm8, %xmm13, %xmm13
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm11, %xmm12, %xmm12
- vpxor %xmm8, %xmm13, %xmm13
- vpxor %xmm12, %xmm10, %xmm10
- vpxor %xmm14, %xmm13, %xmm2
- vpxor %xmm9, %xmm10, %xmm1
- vmovdqa %xmm1, 32(%rsp)
- vmovdqa %xmm2, 48(%rsp)
- # H ^ 5 and H ^ 6
- vpclmulqdq $16, %xmm0, %xmm1, %xmm11
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm10
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm9
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm12
- vpclmulqdq $0x00, %xmm1, %xmm1, %xmm13
- vpclmulqdq $0x11, %xmm1, %xmm1, %xmm14
- vpxor %xmm10, %xmm11, %xmm11
- vpslldq $8, %xmm11, %xmm10
- vpsrldq $8, %xmm11, %xmm11
- vpxor %xmm9, %xmm10, %xmm10
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm9, %xmm10, %xmm10
- vpxor %xmm8, %xmm13, %xmm13
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm11, %xmm12, %xmm12
- vpxor %xmm8, %xmm13, %xmm13
- vpxor %xmm12, %xmm10, %xmm10
- vpxor %xmm14, %xmm13, %xmm0
- vpxor %xmm9, %xmm10, %xmm7
- vmovdqa %xmm7, 64(%rsp)
- vmovdqa %xmm0, 80(%rsp)
- # H ^ 7 and H ^ 8
- vpclmulqdq $16, %xmm1, %xmm2, %xmm11
- vpclmulqdq $0x01, %xmm1, %xmm2, %xmm10
- vpclmulqdq $0x00, %xmm1, %xmm2, %xmm9
- vpclmulqdq $0x11, %xmm1, %xmm2, %xmm12
- vpclmulqdq $0x00, %xmm2, %xmm2, %xmm13
- vpclmulqdq $0x11, %xmm2, %xmm2, %xmm14
- vpxor %xmm10, %xmm11, %xmm11
- vpslldq $8, %xmm11, %xmm10
- vpsrldq $8, %xmm11, %xmm11
- vpxor %xmm9, %xmm10, %xmm10
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm9, %xmm10, %xmm10
- vpxor %xmm8, %xmm13, %xmm13
- vpclmulqdq $16, %xmm3, %xmm10, %xmm9
- vpclmulqdq $16, %xmm3, %xmm13, %xmm8
- vpshufd $0x4e, %xmm10, %xmm10
- vpshufd $0x4e, %xmm13, %xmm13
- vpxor %xmm11, %xmm12, %xmm12
- vpxor %xmm8, %xmm13, %xmm13
- vpxor %xmm12, %xmm10, %xmm10
- vpxor %xmm14, %xmm13, %xmm0
- vpxor %xmm9, %xmm10, %xmm7
- vmovdqa %xmm7, 96(%rsp)
- vmovdqa %xmm0, 112(%rsp)
-L_AES_GCM_decrypt_avx2_ghash_128:
- # aesenc_128_ghash
- leaq (%rdi,%rbx,1), %rcx
- leaq (%r8,%rbx,1), %rdx
- # aesenc_ctr
- vmovdqa 128(%rsp), %xmm0
- vmovdqa L_avx2_aes_gcm_bswap_epi64(%rip), %xmm1
- vpaddd L_avx2_aes_gcm_one(%rip), %xmm0, %xmm9
- vpshufb %xmm1, %xmm0, %xmm8
- vpaddd L_avx2_aes_gcm_two(%rip), %xmm0, %xmm10
- vpshufb %xmm1, %xmm9, %xmm9
- vpaddd L_avx2_aes_gcm_three(%rip), %xmm0, %xmm11
- vpshufb %xmm1, %xmm10, %xmm10
- vpaddd L_avx2_aes_gcm_four(%rip), %xmm0, %xmm12
- vpshufb %xmm1, %xmm11, %xmm11
- vpaddd L_avx2_aes_gcm_five(%rip), %xmm0, %xmm13
- vpshufb %xmm1, %xmm12, %xmm12
- vpaddd L_avx2_aes_gcm_six(%rip), %xmm0, %xmm14
- vpshufb %xmm1, %xmm13, %xmm13
- vpaddd L_avx2_aes_gcm_seven(%rip), %xmm0, %xmm15
- vpshufb %xmm1, %xmm14, %xmm14
- vpaddd L_avx2_aes_gcm_eight(%rip), %xmm0, %xmm0
- vpshufb %xmm1, %xmm15, %xmm15
- # aesenc_xor
- vmovdqa (%rsi), %xmm7
- vmovdqa %xmm0, 128(%rsp)
- vpxor %xmm7, %xmm8, %xmm8
- vpxor %xmm7, %xmm9, %xmm9
- vpxor %xmm7, %xmm10, %xmm10
- vpxor %xmm7, %xmm11, %xmm11
- vpxor %xmm7, %xmm12, %xmm12
- vpxor %xmm7, %xmm13, %xmm13
- vpxor %xmm7, %xmm14, %xmm14
- vpxor %xmm7, %xmm15, %xmm15
- # aesenc_pclmul_1
- vmovdqu (%rcx), %xmm1
- vmovdqu 16(%rsi), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vmovdqa 112(%rsp), %xmm2
- vpxor %xmm6, %xmm1, %xmm1
- vpclmulqdq $16, %xmm2, %xmm1, %xmm5
- vpclmulqdq $0x01, %xmm2, %xmm1, %xmm3
- vpclmulqdq $0x00, %xmm2, %xmm1, %xmm6
- vpclmulqdq $0x11, %xmm2, %xmm1, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_2
- vmovdqu 16(%rcx), %xmm1
- vmovdqa 96(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 32(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu 32(%rcx), %xmm1
- vmovdqa 80(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 48(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu 48(%rcx), %xmm1
- vmovdqa 64(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 64(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu 64(%rcx), %xmm1
- vmovdqa 48(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 80(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu 80(%rcx), %xmm1
- vmovdqa 32(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 96(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu 96(%rcx), %xmm1
- vmovdqa 16(%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 112(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_n
- vmovdqu 112(%rcx), %xmm1
- vmovdqa (%rsp), %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm1, %xmm1
- vpxor %xmm2, %xmm5, %xmm5
- vpclmulqdq $16, %xmm0, %xmm1, %xmm2
- vpxor %xmm3, %xmm5, %xmm5
- vpclmulqdq $0x01, %xmm0, %xmm1, %xmm3
- vpxor %xmm4, %xmm6, %xmm6
- vpclmulqdq $0x00, %xmm0, %xmm1, %xmm4
- vpclmulqdq $0x11, %xmm0, %xmm1, %xmm1
- vmovdqu 128(%rsi), %xmm0
- vpxor %xmm1, %xmm7, %xmm7
- vaesenc %xmm0, %xmm8, %xmm8
- vaesenc %xmm0, %xmm9, %xmm9
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc %xmm0, %xmm11, %xmm11
- vaesenc %xmm0, %xmm12, %xmm12
- vaesenc %xmm0, %xmm13, %xmm13
- vaesenc %xmm0, %xmm14, %xmm14
- vaesenc %xmm0, %xmm15, %xmm15
- # aesenc_pclmul_l
- vpxor %xmm2, %xmm5, %xmm5
- vpxor %xmm4, %xmm6, %xmm6
- vpxor %xmm3, %xmm5, %xmm5
- vpslldq $8, %xmm5, %xmm1
- vpsrldq $8, %xmm5, %xmm5
- vmovdqa 144(%rsi), %xmm4
- vmovdqa L_avx2_aes_gcm_mod2_128(%rip), %xmm0
- vaesenc %xmm4, %xmm8, %xmm8
- vpxor %xmm1, %xmm6, %xmm6
- vpxor %xmm5, %xmm7, %xmm7
- vpclmulqdq $16, %xmm0, %xmm6, %xmm3
- vaesenc %xmm4, %xmm9, %xmm9
- vaesenc %xmm4, %xmm10, %xmm10
- vaesenc %xmm4, %xmm11, %xmm11
- vpshufd $0x4e, %xmm6, %xmm6
- vpxor %xmm3, %xmm6, %xmm6
- vpclmulqdq $16, %xmm0, %xmm6, %xmm3
- vaesenc %xmm4, %xmm12, %xmm12
- vaesenc %xmm4, %xmm13, %xmm13
- vaesenc %xmm4, %xmm14, %xmm14
- vpshufd $0x4e, %xmm6, %xmm6
- vpxor %xmm3, %xmm6, %xmm6
- vpxor %xmm7, %xmm6, %xmm6
- vaesenc %xmm4, %xmm15, %xmm15
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm7
- jl L_AES_GCM_decrypt_avx2_aesenc_128_ghash_avx_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 176(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm7
- jl L_AES_GCM_decrypt_avx2_aesenc_128_ghash_avx_done
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 208(%rsi), %xmm7
- vaesenc %xmm7, %xmm8, %xmm8
- vaesenc %xmm7, %xmm9, %xmm9
- vaesenc %xmm7, %xmm10, %xmm10
- vaesenc %xmm7, %xmm11, %xmm11
- vaesenc %xmm7, %xmm12, %xmm12
- vaesenc %xmm7, %xmm13, %xmm13
- vaesenc %xmm7, %xmm14, %xmm14
- vaesenc %xmm7, %xmm15, %xmm15
- vmovdqa 224(%rsi), %xmm7
-L_AES_GCM_decrypt_avx2_aesenc_128_ghash_avx_done:
- # aesenc_last
- vaesenclast %xmm7, %xmm8, %xmm8
- vaesenclast %xmm7, %xmm9, %xmm9
- vaesenclast %xmm7, %xmm10, %xmm10
- vaesenclast %xmm7, %xmm11, %xmm11
- vmovdqu (%rcx), %xmm0
- vmovdqu 16(%rcx), %xmm1
- vmovdqu 32(%rcx), %xmm2
- vmovdqu 48(%rcx), %xmm3
- vpxor %xmm0, %xmm8, %xmm8
- vpxor %xmm1, %xmm9, %xmm9
- vpxor %xmm2, %xmm10, %xmm10
- vpxor %xmm3, %xmm11, %xmm11
- vmovdqu %xmm8, (%rdx)
- vmovdqu %xmm9, 16(%rdx)
- vmovdqu %xmm10, 32(%rdx)
- vmovdqu %xmm11, 48(%rdx)
- vaesenclast %xmm7, %xmm12, %xmm12
- vaesenclast %xmm7, %xmm13, %xmm13
- vaesenclast %xmm7, %xmm14, %xmm14
- vaesenclast %xmm7, %xmm15, %xmm15
- vmovdqu 64(%rcx), %xmm0
- vmovdqu 80(%rcx), %xmm1
- vmovdqu 96(%rcx), %xmm2
- vmovdqu 112(%rcx), %xmm3
- vpxor %xmm0, %xmm12, %xmm12
- vpxor %xmm1, %xmm13, %xmm13
- vpxor %xmm2, %xmm14, %xmm14
- vpxor %xmm3, %xmm15, %xmm15
- vmovdqu %xmm12, 64(%rdx)
- vmovdqu %xmm13, 80(%rdx)
- vmovdqu %xmm14, 96(%rdx)
- vmovdqu %xmm15, 112(%rdx)
- # aesenc_128_ghash - end
- addl $0x80, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_decrypt_avx2_ghash_128
- vmovdqa (%rsp), %xmm5
- vmovdqa 128(%rsp), %xmm4
- vmovdqa 144(%rsp), %xmm15
-L_AES_GCM_decrypt_avx2_done_128:
- cmpl %r10d, %ebx
- jge L_AES_GCM_decrypt_avx2_done_dec
- movl %r10d, %r13d
- andl $0xfffffff0, %r13d
- cmpl %r13d, %ebx
- jge L_AES_GCM_decrypt_avx2_last_block_done
-L_AES_GCM_decrypt_avx2_last_block_start:
- vmovdqu (%rdi,%rbx,1), %xmm11
- vpshufb L_avx2_aes_gcm_bswap_epi64(%rip), %xmm4, %xmm10
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm11, %xmm12
- vpaddd L_avx2_aes_gcm_one(%rip), %xmm4, %xmm4
- vpxor %xmm6, %xmm12, %xmm12
- # aesenc_gfmul_sb
- vpclmulqdq $0x01, %xmm5, %xmm12, %xmm2
- vpclmulqdq $16, %xmm5, %xmm12, %xmm3
- vpclmulqdq $0x00, %xmm5, %xmm12, %xmm1
- vpclmulqdq $0x11, %xmm5, %xmm12, %xmm8
- vpxor (%rsi), %xmm10, %xmm10
- vaesenc 16(%rsi), %xmm10, %xmm10
- vpxor %xmm2, %xmm3, %xmm3
- vpslldq $8, %xmm3, %xmm2
- vpsrldq $8, %xmm3, %xmm3
- vaesenc 32(%rsi), %xmm10, %xmm10
- vpxor %xmm1, %xmm2, %xmm2
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm2, %xmm1
- vaesenc 48(%rsi), %xmm10, %xmm10
- vaesenc 64(%rsi), %xmm10, %xmm10
- vaesenc 80(%rsi), %xmm10, %xmm10
- vpshufd $0x4e, %xmm2, %xmm2
- vpxor %xmm1, %xmm2, %xmm2
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm2, %xmm1
- vaesenc 96(%rsi), %xmm10, %xmm10
- vaesenc 112(%rsi), %xmm10, %xmm10
- vaesenc 128(%rsi), %xmm10, %xmm10
- vpshufd $0x4e, %xmm2, %xmm2
- vaesenc 144(%rsi), %xmm10, %xmm10
- vpxor %xmm3, %xmm8, %xmm8
- vpxor %xmm8, %xmm2, %xmm2
- vmovdqa 160(%rsi), %xmm0
- cmpl $11, %r9d
- jl L_AES_GCM_decrypt_avx2_aesenc_gfmul_sb_last
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc 176(%rsi), %xmm10, %xmm10
- vmovdqa 192(%rsi), %xmm0
- cmpl $13, %r9d
- jl L_AES_GCM_decrypt_avx2_aesenc_gfmul_sb_last
- vaesenc %xmm0, %xmm10, %xmm10
- vaesenc 208(%rsi), %xmm10, %xmm10
- vmovdqa 224(%rsi), %xmm0
-L_AES_GCM_decrypt_avx2_aesenc_gfmul_sb_last:
- vaesenclast %xmm0, %xmm10, %xmm10
- vpxor %xmm1, %xmm2, %xmm6
- vpxor %xmm11, %xmm10, %xmm10
- vmovdqu %xmm10, (%r8,%rbx,1)
- addl $16, %ebx
- cmpl %r13d, %ebx
- jl L_AES_GCM_decrypt_avx2_last_block_start
-L_AES_GCM_decrypt_avx2_last_block_done:
- movl %r10d, %ecx
- movl %r10d, %edx
- andl $15, %ecx
- jz L_AES_GCM_decrypt_avx2_done_dec
- # aesenc_last15_dec
- vpshufb L_avx2_aes_gcm_bswap_epi64(%rip), %xmm4, %xmm4
- vpxor (%rsi), %xmm4, %xmm4
- vaesenc 16(%rsi), %xmm4, %xmm4
- vaesenc 32(%rsi), %xmm4, %xmm4
- vaesenc 48(%rsi), %xmm4, %xmm4
- vaesenc 64(%rsi), %xmm4, %xmm4
- vaesenc 80(%rsi), %xmm4, %xmm4
- vaesenc 96(%rsi), %xmm4, %xmm4
- vaesenc 112(%rsi), %xmm4, %xmm4
- vaesenc 128(%rsi), %xmm4, %xmm4
- vaesenc 144(%rsi), %xmm4, %xmm4
- cmpl $11, %r9d
- vmovdqa 160(%rsi), %xmm1
- jl L_AES_GCM_decrypt_avx2_aesenc_last15_dec_avx_aesenc_avx_last
- vaesenc %xmm1, %xmm4, %xmm4
- vaesenc 176(%rsi), %xmm4, %xmm4
- cmpl $13, %r9d
- vmovdqa 192(%rsi), %xmm1
- jl L_AES_GCM_decrypt_avx2_aesenc_last15_dec_avx_aesenc_avx_last
- vaesenc %xmm1, %xmm4, %xmm4
- vaesenc 208(%rsi), %xmm4, %xmm4
- vmovdqa 224(%rsi), %xmm1
-L_AES_GCM_decrypt_avx2_aesenc_last15_dec_avx_aesenc_avx_last:
- vaesenclast %xmm1, %xmm4, %xmm4
- xorl %ecx, %ecx
- vpxor %xmm0, %xmm0, %xmm0
- vmovdqa %xmm4, (%rsp)
- vmovdqa %xmm0, 16(%rsp)
-L_AES_GCM_decrypt_avx2_aesenc_last15_dec_avx_loop:
- movzbl (%rdi,%rbx,1), %r13d
- movb %r13b, 16(%rsp,%rcx,1)
- xorb (%rsp,%rcx,1), %r13b
- movb %r13b, (%r8,%rbx,1)
- incl %ebx
- incl %ecx
- cmpl %edx, %ebx
- jl L_AES_GCM_decrypt_avx2_aesenc_last15_dec_avx_loop
- vmovdqa 16(%rsp), %xmm4
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm4, %xmm4
- vpxor %xmm4, %xmm6, %xmm6
- # ghash_gfmul_red
- vpclmulqdq $16, %xmm5, %xmm6, %xmm2
- vpclmulqdq $0x01, %xmm5, %xmm6, %xmm1
- vpclmulqdq $0x00, %xmm5, %xmm6, %xmm0
- vpxor %xmm1, %xmm2, %xmm2
- vpslldq $8, %xmm2, %xmm1
- vpsrldq $8, %xmm2, %xmm2
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $0x11, %xmm5, %xmm6, %xmm6
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm0, %xmm1, %xmm1
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm1, %xmm0
- vpshufd $0x4e, %xmm1, %xmm1
- vpxor %xmm2, %xmm6, %xmm6
- vpxor %xmm1, %xmm6, %xmm6
- vpxor %xmm0, %xmm6, %xmm6
-L_AES_GCM_decrypt_avx2_done_dec:
- # calc_tag
- shlq $3, %r10
- vpinsrq $0x00, %r10, %xmm0, %xmm0
- shlq $3, %r11
- vpinsrq $0x01, %r11, %xmm1, %xmm1
- vpblendd $12, %xmm1, %xmm0, %xmm0
- vpxor %xmm6, %xmm0, %xmm0
- # ghash_gfmul_red
- vpclmulqdq $16, %xmm5, %xmm0, %xmm4
- vpclmulqdq $0x01, %xmm5, %xmm0, %xmm3
- vpclmulqdq $0x00, %xmm5, %xmm0, %xmm2
- vpxor %xmm3, %xmm4, %xmm4
- vpslldq $8, %xmm4, %xmm3
- vpsrldq $8, %xmm4, %xmm4
- vpxor %xmm2, %xmm3, %xmm3
- vpclmulqdq $0x11, %xmm5, %xmm0, %xmm0
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm3, %xmm2
- vpshufd $0x4e, %xmm3, %xmm3
- vpxor %xmm2, %xmm3, %xmm3
- vpclmulqdq $16, L_avx2_aes_gcm_mod2_128(%rip), %xmm3, %xmm2
- vpshufd $0x4e, %xmm3, %xmm3
- vpxor %xmm4, %xmm0, %xmm0
- vpxor %xmm3, %xmm0, %xmm0
- vpxor %xmm2, %xmm0, %xmm0
- vpshufb L_avx2_aes_gcm_bswap_mask(%rip), %xmm0, %xmm0
- vpxor %xmm15, %xmm0, %xmm0
- # cmp_tag
- cmpl $16, %r15d
- je L_AES_GCM_decrypt_avx2_cmp_tag_16
- xorq %rdx, %rdx
- xorq %rax, %rax
- vmovdqa %xmm0, (%rsp)
-L_AES_GCM_decrypt_avx2_cmp_tag_loop:
- movzbl (%rsp,%rdx,1), %r13d
- xorb (%r14,%rdx,1), %r13b
- orb %r13b, %al
- incl %edx
- cmpl %r15d, %edx
- jne L_AES_GCM_decrypt_avx2_cmp_tag_loop
- cmpb $0x00, %al
- sete %al
- jmp L_AES_GCM_decrypt_avx2_cmp_tag_done
-L_AES_GCM_decrypt_avx2_cmp_tag_16:
- vmovdqu (%r14), %xmm1
- vpcmpeqb %xmm1, %xmm0, %xmm0
- vpmovmskb %xmm0, %rdx
- # %%edx == 0xFFFF then return 1 else => return 0
- xorl %eax, %eax
- cmpl $0xffff, %edx
- sete %al
-L_AES_GCM_decrypt_avx2_cmp_tag_done:
- movl %eax, (%rbp)
- vzeroupper
- addq $0xa8, %rsp
- popq %rbp
- popq %r15
- popq %rbx
- popq %r14
- popq %r12
- popq %r13
- repz retq
-#ifndef __APPLE__
-.size AES_GCM_decrypt_avx2,.-AES_GCM_decrypt_avx2
-#endif /* __APPLE__ */
-#endif /* HAVE_INTEL_AVX2 */
diff --git a/client/wolfssl/wolfcrypt/src/arc4.c b/client/wolfssl/wolfcrypt/src/arc4.c
deleted file mode 100644
index 7eb8268..0000000
--- a/client/wolfssl/wolfcrypt/src/arc4.c
+++ /dev/null
@@ -1,149 +0,0 @@
-/* arc4.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifndef NO_RC4
-
-#include
-#include
-
-
-int wc_Arc4SetKey(Arc4* arc4, const byte* key, word32 length)
-{
- int ret = 0;
- word32 i;
- word32 keyIndex = 0, stateIndex = 0;
-
- if (arc4 == NULL || key == NULL || length == 0) {
- return BAD_FUNC_ARG;
- }
-
-#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
- defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
- if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
- return NitroxArc4SetKey(arc4, key, length);
- }
-#endif
-
- arc4->x = 1;
- arc4->y = 0;
-
- for (i = 0; i < ARC4_STATE_SIZE; i++)
- arc4->state[i] = (byte)i;
-
- for (i = 0; i < ARC4_STATE_SIZE; i++) {
- word32 a = arc4->state[i];
- stateIndex += key[keyIndex] + a;
- stateIndex &= 0xFF;
- arc4->state[i] = arc4->state[stateIndex];
- arc4->state[stateIndex] = (byte)a;
-
- if (++keyIndex >= length)
- keyIndex = 0;
- }
-
- return ret;
-}
-
-
-static WC_INLINE byte MakeByte(word32* x, word32* y, byte* s)
-{
- word32 a = s[*x], b;
- *y = (*y+a) & 0xff;
-
- b = s[*y];
- s[*x] = (byte)b;
- s[*y] = (byte)a;
- *x = (*x+1) & 0xff;
-
- return s[(a+b) & 0xff];
-}
-
-
-int wc_Arc4Process(Arc4* arc4, byte* out, const byte* in, word32 length)
-{
- int ret = 0;
- word32 x;
- word32 y;
-
- if (arc4 == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
-
-#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4) && \
- defined(HAVE_CAVIUM) && !defined(HAVE_CAVIUM_V)
- if (arc4->asyncDev.marker == WOLFSSL_ASYNC_MARKER_ARC4) {
- return NitroxArc4Process(arc4, out, in, length);
- }
-#endif
-
- x = arc4->x;
- y = arc4->y;
-
- while(length--)
- *out++ = *in++ ^ MakeByte(&x, &y, arc4->state);
-
- arc4->x = (byte)x;
- arc4->y = (byte)y;
-
- return ret;
-}
-
-/* Initialize Arc4 for use with async device */
-int wc_Arc4Init(Arc4* arc4, void* heap, int devId)
-{
- int ret = 0;
-
- if (arc4 == NULL)
- return BAD_FUNC_ARG;
-
- arc4->heap = heap;
-
-#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
- ret = wolfAsync_DevCtxInit(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4,
- arc4->heap, devId);
-#else
- (void)devId;
-#endif /* WOLFSSL_ASYNC_CRYPT */
-
- return ret;
-}
-
-
-/* Free Arc4 from use with async device */
-void wc_Arc4Free(Arc4* arc4)
-{
- if (arc4 == NULL)
- return;
-
-#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ARC4)
- wolfAsync_DevCtxFree(&arc4->asyncDev, WOLFSSL_ASYNC_MARKER_ARC4);
-#endif /* WOLFSSL_ASYNC_CRYPT */
-}
-
-#endif /* NO_RC4 */
-
diff --git a/client/wolfssl/wolfcrypt/src/asm.c b/client/wolfssl/wolfcrypt/src/asm.c
deleted file mode 100644
index 0af4447..0000000
--- a/client/wolfssl/wolfcrypt/src/asm.c
+++ /dev/null
@@ -1,1783 +0,0 @@
-/* asm.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-/*
- * Based on public domain TomsFastMath 0.10 by Tom St Denis, tomstdenis@iahu.ca,
- * http://math.libtomcrypt.com
- */
-
-
-/******************************************************************/
-/* fp_montgomery_reduce.c asm or generic */
-
-
-/* Each platform needs to query info type 1 from cpuid to see if aesni is
- * supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
- */
-
-#if defined(HAVE_INTEL_MULX)
-#ifndef _MSC_VER
- #define cpuid(reg, leaf, sub)\
- __asm__ __volatile__ ("cpuid":\
- "=a" (reg[0]), "=b" (reg[1]), "=c" (reg[2]), "=d" (reg[3]) :\
- "a" (leaf), "c"(sub));
-
- #define XASM_LINK(f) asm(f)
-#else
-
- #include
- #define cpuid(a,b,c) __cpuidex((int*)a,b,c)
-
- #define XASM_LINK(f)
-
-#endif /* _MSC_VER */
-
-#define EAX 0
-#define EBX 1
-#define ECX 2
-#define EDX 3
-
-#define CPUID_AVX1 0x1
-#define CPUID_AVX2 0x2
-#define CPUID_RDRAND 0x4
-#define CPUID_RDSEED 0x8
-#define CPUID_BMI2 0x10 /* MULX, RORX */
-#define CPUID_ADX 0x20 /* ADCX, ADOX */
-
-#define IS_INTEL_AVX1 (cpuid_flags&CPUID_AVX1)
-#define IS_INTEL_AVX2 (cpuid_flags&CPUID_AVX2)
-#define IS_INTEL_BMI2 (cpuid_flags&CPUID_BMI2)
-#define IS_INTEL_ADX (cpuid_flags&CPUID_ADX)
-#define IS_INTEL_RDRAND (cpuid_flags&CPUID_RDRAND)
-#define IS_INTEL_RDSEED (cpuid_flags&CPUID_RDSEED)
-#define SET_FLAGS
-
-static word32 cpuid_check = 0 ;
-static word32 cpuid_flags = 0 ;
-
-static word32 cpuid_flag(word32 leaf, word32 sub, word32 num, word32 bit) {
- int got_intel_cpu = 0;
- int got_amd_cpu = 0;
- unsigned int reg[5];
-
- reg[4] = '\0' ;
- cpuid(reg, 0, 0);
-
- /* check for intel cpu */
- if( memcmp((char *)&(reg[EBX]), "Genu", 4) == 0 &&
- memcmp((char *)&(reg[EDX]), "ineI", 4) == 0 &&
- memcmp((char *)&(reg[ECX]), "ntel", 4) == 0) {
- got_intel_cpu = 1;
- }
-
- /* check for AMD cpu */
- if( memcmp((char *)&(reg[EBX]), "Auth", 4) == 0 &&
- memcmp((char *)&(reg[EDX]), "enti", 4) == 0 &&
- memcmp((char *)&(reg[ECX]), "cAMD", 4) == 0) {
- got_amd_cpu = 1;
- }
- if (got_intel_cpu || got_amd_cpu) {
- cpuid(reg, leaf, sub);
- return((reg[num]>>bit)&0x1) ;
- }
- return 0 ;
-}
-
-WC_INLINE static int set_cpuid_flags(void) {
- if(cpuid_check == 0) {
- if(cpuid_flag(7, 0, EBX, 8)){ cpuid_flags |= CPUID_BMI2 ; }
- if(cpuid_flag(7, 0, EBX,19)){ cpuid_flags |= CPUID_ADX ; }
- cpuid_check = 1 ;
- return 0 ;
- }
- return 1 ;
-}
-
-#define RETURN return
-#define IF_HAVE_INTEL_MULX(func, ret) \
- if(cpuid_check==0)set_cpuid_flags() ; \
- if(IS_INTEL_BMI2 && IS_INTEL_ADX){ func; ret ; }
-
-#else
- #define IF_HAVE_INTEL_MULX(func, ret)
-#endif
-
-#if defined(TFM_X86) && !defined(TFM_SSE2)
-/* x86-32 code */
-
-#define MONT_START
-#define MONT_FINI
-#define LOOP_END
-#define LOOP_START \
- mu = c[x] * mp
-
-#define INNERMUL \
-__asm__( \
- "movl %5,%%eax \n\t" \
- "mull %4 \n\t" \
- "addl %1,%%eax \n\t" \
- "adcl $0,%%edx \n\t" \
- "addl %%eax,%0 \n\t" \
- "adcl $0,%%edx \n\t" \
- "movl %%edx,%1 \n\t" \
-:"=g"(_c[LO]), "=r"(cy) \
-:"0"(_c[LO]), "1"(cy), "r"(mu), "r"(*tmpm++) \
-: "%eax", "%edx", "cc")
-
-#define PROPCARRY \
-__asm__( \
- "addl %1,%0 \n\t" \
- "setb %%al \n\t" \
- "movzbl %%al,%1 \n\t" \
-:"=g"(_c[LO]), "=r"(cy) \
-:"0"(_c[LO]), "1"(cy) \
-: "%eax", "cc")
-
-/******************************************************************/
-#elif defined(TFM_X86_64)
-/* x86-64 code */
-
-#define MONT_START
-#define MONT_FINI
-#define LOOP_END
-#define LOOP_START \
- mu = c[x] * mp
-
-#define INNERMUL \
-__asm__( \
- "movq %5,%%rax \n\t" \
- "mulq %4 \n\t" \
- "addq %1,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "addq %%rax,%0 \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq %%rdx,%1 \n\t" \
-:"=g"(_c[LO]), "=r"(cy) \
-:"0"(_c[LO]), "1"(cy), "r"(mu), "r"(*tmpm++) \
-: "%rax", "%rdx", "cc")
-
-#if defined(HAVE_INTEL_MULX)
-#define MULX_INNERMUL8(x,y,z,cy) \
- __asm__ volatile ( \
- "movq %[yn], %%rdx\n\t" \
- "xorq %%rcx, %%rcx\n\t" \
- "movq 0(%[c]), %%r8\n\t" \
- "movq 8(%[c]), %%r9\n\t" \
- "movq 16(%[c]), %%r10\n\t" \
- "movq 24(%[c]), %%r11\n\t" \
- "movq 32(%[c]), %%r12\n\t" \
- "movq 40(%[c]), %%r13\n\t" \
- "movq 48(%[c]), %%r14\n\t" \
- "movq 56(%[c]), %%r15\n\t" \
- \
- "mulx 0(%[xp]), %%rax, %%rcx\n\t" \
- "adcxq %[cy], %%r8\n\t" \
- "adoxq %%rax, %%r8\n\t" \
- "mulx 8(%[xp]), %%rax, %[cy]\n\t" \
- "adcxq %%rcx, %%r9\n\t" \
- "adoxq %%rax, %%r9\n\t" \
- "mulx 16(%[xp]), %%rax, %%rcx\n\t" \
- "adcxq %[cy], %%r10\n\t" \
- "adoxq %%rax, %%r10\n\t" \
- "mulx 24(%[xp]), %%rax, %[cy]\n\t" \
- "adcxq %%rcx, %%r11\n\t" \
- "adoxq %%rax, %%r11\n\t" \
- "mulx 32(%[xp]), %%rax, %%rcx\n\t" \
- "adcxq %[cy], %%r12\n\t" \
- "adoxq %%rax, %%r12\n\t" \
- "mulx 40(%[xp]), %%rax, %[cy]\n\t" \
- "adcxq %%rcx, %%r13\n\t" \
- "adoxq %%rax, %%r13\n\t" \
- "mulx 48(%[xp]), %%rax, %%rcx\n\t" \
- "adcxq %[cy], %%r14\n\t" \
- "adoxq %%rax, %%r14\n\t" \
- "adcxq %%rcx, %%r15\n\t" \
- "mulx 56(%[xp]), %%rax, %[cy]\n\t" \
- "movq $0, %%rdx\n\t" \
- "adoxq %%rdx, %%rax\n\t" \
- "adcxq %%rdx, %[cy]\n\t" \
- "adoxq %%rdx, %[cy]\n\t" \
- "addq %%rax, %%r15\n\t" \
- "adcq $0, %[cy]\n\t" \
- \
- "movq %%r8, 0(%[c])\n\t" \
- "movq %%r9, 8(%[c])\n\t" \
- "movq %%r10, 16(%[c])\n\t" \
- "movq %%r11, 24(%[c])\n\t" \
- "movq %%r12, 32(%[c])\n\t" \
- "movq %%r13, 40(%[c])\n\t" \
- "movq %%r14, 48(%[c])\n\t" \
- "movq %%r15, 56(%[c])\n\t" \
- : [cy] "+r" (cy) \
- : [xp] "r" (x), [c] "r" (c_mulx), [yn] "rm" (y) \
- :"%r8", "%r9", "%r10", "%r11", "%r12", "%r13", "%r14", "%r15", \
- "%rdx", "%rax", "%rcx" \
- )
-
-#define INNERMUL8_MULX \
-{\
- MULX_INNERMUL8(tmpm, mu, _c, cy);\
-}
-#endif
-
-#define INNERMUL8 \
- __asm__( \
- "movq 0(%5),%%rax \n\t" \
- "movq 0(%2),%%r10 \n\t" \
- "movq 0x8(%5),%%r11 \n\t" \
- "mulq %4 \n\t" \
- "addq %%r10,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq 0x8(%2),%%r10 \n\t" \
- "addq %3,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq %%rax,0(%0) \n\t" \
- "movq %%rdx,%1 \n\t" \
- \
- "movq %%r11,%%rax \n\t" \
- "movq 0x10(%5),%%r11 \n\t" \
- "mulq %4 \n\t" \
- "addq %%r10,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq 0x10(%2),%%r10 \n\t" \
- "addq %3,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq %%rax,0x8(%0) \n\t" \
- "movq %%rdx,%1 \n\t" \
- \
- "movq %%r11,%%rax \n\t" \
- "movq 0x18(%5),%%r11 \n\t" \
- "mulq %4 \n\t" \
- "addq %%r10,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq 0x18(%2),%%r10 \n\t" \
- "addq %3,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq %%rax,0x10(%0) \n\t" \
- "movq %%rdx,%1 \n\t" \
- \
- "movq %%r11,%%rax \n\t" \
- "movq 0x20(%5),%%r11 \n\t" \
- "mulq %4 \n\t" \
- "addq %%r10,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq 0x20(%2),%%r10 \n\t" \
- "addq %3,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq %%rax,0x18(%0) \n\t" \
- "movq %%rdx,%1 \n\t" \
- \
- "movq %%r11,%%rax \n\t" \
- "movq 0x28(%5),%%r11 \n\t" \
- "mulq %4 \n\t" \
- "addq %%r10,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq 0x28(%2),%%r10 \n\t" \
- "addq %3,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq %%rax,0x20(%0) \n\t" \
- "movq %%rdx,%1 \n\t" \
- \
- "movq %%r11,%%rax \n\t" \
- "movq 0x30(%5),%%r11 \n\t" \
- "mulq %4 \n\t" \
- "addq %%r10,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq 0x30(%2),%%r10 \n\t" \
- "addq %3,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq %%rax,0x28(%0) \n\t" \
- "movq %%rdx,%1 \n\t" \
- \
- "movq %%r11,%%rax \n\t" \
- "movq 0x38(%5),%%r11 \n\t" \
- "mulq %4 \n\t" \
- "addq %%r10,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq 0x38(%2),%%r10 \n\t" \
- "addq %3,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq %%rax,0x30(%0) \n\t" \
- "movq %%rdx,%1 \n\t" \
- \
- "movq %%r11,%%rax \n\t" \
- "mulq %4 \n\t" \
- "addq %%r10,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "addq %3,%%rax \n\t" \
- "adcq $0,%%rdx \n\t" \
- "movq %%rax,0x38(%0) \n\t" \
- "movq %%rdx,%1 \n\t" \
- \
-:"=r"(_c), "=r"(cy) \
-: "0"(_c), "1"(cy), "g"(mu), "r"(tmpm)\
-: "%rax", "%rdx", "%r10", "%r11", "cc")
-
-#define PROPCARRY \
-__asm__( \
- "addq %1,%0 \n\t" \
- "setb %%al \n\t" \
- "movzbq %%al,%1 \n\t" \
-:"=g"(_c[LO]), "=r"(cy) \
-:"0"(_c[LO]), "1"(cy) \
-: "%rax", "cc")
-
-/******************************************************************/
-#elif defined(TFM_SSE2)
-/* SSE2 code (assumes 32-bit fp_digits) */
-/* XMM register assignments:
- * xmm0 *tmpm++, then Mu * (*tmpm++)
- * xmm1 c[x], then Mu
- * xmm2 mp
- * xmm3 cy
- * xmm4 _c[LO]
- */
-
-#define MONT_START \
- __asm__("movd %0,%%mm2"::"g"(mp))
-
-#define MONT_FINI \
- __asm__("emms")
-
-#define LOOP_START \
-__asm__( \
-"movd %0,%%mm1 \n\t" \
-"pxor %%mm3,%%mm3 \n\t" \
-"pmuludq %%mm2,%%mm1 \n\t" \
-:: "g"(c[x]))
-
-/* pmuludq on mmx registers does a 32x32->64 multiply. */
-#define INNERMUL \
-__asm__( \
- "movd %1,%%mm4 \n\t" \
- "movd %2,%%mm0 \n\t" \
- "paddq %%mm4,%%mm3 \n\t" \
- "pmuludq %%mm1,%%mm0 \n\t" \
- "paddq %%mm0,%%mm3 \n\t" \
- "movd %%mm3,%0 \n\t" \
- "psrlq $32, %%mm3 \n\t" \
-:"=g"(_c[LO]) : "0"(_c[LO]), "g"(*tmpm++) );
-
-#define INNERMUL8 \
-__asm__( \
- "movd 0(%1),%%mm4 \n\t" \
- "movd 0(%2),%%mm0 \n\t" \
- "paddq %%mm4,%%mm3 \n\t" \
- "pmuludq %%mm1,%%mm0 \n\t" \
- "movd 4(%2),%%mm5 \n\t" \
- "paddq %%mm0,%%mm3 \n\t" \
- "movd 4(%1),%%mm6 \n\t" \
- "movd %%mm3,0(%0) \n\t" \
- "psrlq $32, %%mm3 \n\t" \
-\
- "paddq %%mm6,%%mm3 \n\t" \
- "pmuludq %%mm1,%%mm5 \n\t" \
- "movd 8(%2),%%mm6 \n\t" \
- "paddq %%mm5,%%mm3 \n\t" \
- "movd 8(%1),%%mm7 \n\t" \
- "movd %%mm3,4(%0) \n\t" \
- "psrlq $32, %%mm3 \n\t" \
-\
- "paddq %%mm7,%%mm3 \n\t" \
- "pmuludq %%mm1,%%mm6 \n\t" \
- "movd 12(%2),%%mm7 \n\t" \
- "paddq %%mm6,%%mm3 \n\t" \
- "movd 12(%1),%%mm5 \n\t" \
- "movd %%mm3,8(%0) \n\t" \
- "psrlq $32, %%mm3 \n\t" \
-\
- "paddq %%mm5,%%mm3 \n\t" \
- "pmuludq %%mm1,%%mm7 \n\t" \
- "movd 16(%2),%%mm5 \n\t" \
- "paddq %%mm7,%%mm3 \n\t" \
- "movd 16(%1),%%mm6 \n\t" \
- "movd %%mm3,12(%0) \n\t" \
- "psrlq $32, %%mm3 \n\t" \
-\
- "paddq %%mm6,%%mm3 \n\t" \
- "pmuludq %%mm1,%%mm5 \n\t" \
- "movd 20(%2),%%mm6 \n\t" \
- "paddq %%mm5,%%mm3 \n\t" \
- "movd 20(%1),%%mm7 \n\t" \
- "movd %%mm3,16(%0) \n\t" \
- "psrlq $32, %%mm3 \n\t" \
-\
- "paddq %%mm7,%%mm3 \n\t" \
- "pmuludq %%mm1,%%mm6 \n\t" \
- "movd 24(%2),%%mm7 \n\t" \
- "paddq %%mm6,%%mm3 \n\t" \
- "movd 24(%1),%%mm5 \n\t" \
- "movd %%mm3,20(%0) \n\t" \
- "psrlq $32, %%mm3 \n\t" \
-\
- "paddq %%mm5,%%mm3 \n\t" \
- "pmuludq %%mm1,%%mm7 \n\t" \
- "movd 28(%2),%%mm5 \n\t" \
- "paddq %%mm7,%%mm3 \n\t" \
- "movd 28(%1),%%mm6 \n\t" \
- "movd %%mm3,24(%0) \n\t" \
- "psrlq $32, %%mm3 \n\t" \
-\
- "paddq %%mm6,%%mm3 \n\t" \
- "pmuludq %%mm1,%%mm5 \n\t" \
- "paddq %%mm5,%%mm3 \n\t" \
- "movd %%mm3,28(%0) \n\t" \
- "psrlq $32, %%mm3 \n\t" \
-:"=r"(_c) : "0"(_c), "r"(tmpm) );
-
-/* TAO switched tmpm from "g" to "r" after gcc tried to index the indexed stack
- pointer */
-
-#define LOOP_END \
-__asm__( "movd %%mm3,%0 \n" :"=r"(cy))
-
-#define PROPCARRY \
-__asm__( \
- "addl %1,%0 \n\t" \
- "setb %%al \n\t" \
- "movzbl %%al,%1 \n\t" \
-:"=g"(_c[LO]), "=r"(cy) \
-:"0"(_c[LO]), "1"(cy) \
-: "%eax", "cc")
-
-/******************************************************************/
-#elif defined(TFM_ARM)
- /* ARMv4 code */
-
-#define MONT_START
-#define MONT_FINI
-#define LOOP_END
-#define LOOP_START \
- mu = c[x] * mp
-
-
-#ifdef __thumb__
-
-#define INNERMUL \
-__asm__( \
- " LDR r0,%1 \n\t" \
- " ADDS r0,r0,%0 \n\t" \
- " ITE CS \n\t" \
- " MOVCS %0,#1 \n\t" \
- " MOVCC %0,#0 \n\t" \
- " UMLAL r0,%0,%3,%4 \n\t" \
- " STR r0,%1 \n\t" \
-:"=r"(cy),"=m"(_c[0]):"0"(cy),"r"(mu),"r"(*tmpm++),"m"(_c[0]):"r0","cc");
-
-#define PROPCARRY \
-__asm__( \
- " LDR r0,%1 \n\t" \
- " ADDS r0,r0,%0 \n\t" \
- " STR r0,%1 \n\t" \
- " ITE CS \n\t" \
- " MOVCS %0,#1 \n\t" \
- " MOVCC %0,#0 \n\t" \
-:"=r"(cy),"=m"(_c[0]):"0"(cy),"m"(_c[0]):"r0","cc");
-
-
-/* TAO thumb mode uses ite (if then else) to detect carry directly
- * fixed unmatched constraint warning by changing 1 to m */
-
-#else /* __thumb__ */
-
-#define INNERMUL \
-__asm__( \
- " LDR r0,%1 \n\t" \
- " ADDS r0,r0,%0 \n\t" \
- " MOVCS %0,#1 \n\t" \
- " MOVCC %0,#0 \n\t" \
- " UMLAL r0,%0,%3,%4 \n\t" \
- " STR r0,%1 \n\t" \
-:"=r"(cy),"=m"(_c[0]):"0"(cy),"r"(mu),"r"(*tmpm++),"1"(_c[0]):"r0","cc");
-
-#define PROPCARRY \
-__asm__( \
- " LDR r0,%1 \n\t" \
- " ADDS r0,r0,%0 \n\t" \
- " STR r0,%1 \n\t" \
- " MOVCS %0,#1 \n\t" \
- " MOVCC %0,#0 \n\t" \
-:"=r"(cy),"=m"(_c[0]):"0"(cy),"1"(_c[0]):"r0","cc");
-
-#endif /* __thumb__ */
-
-#elif defined(TFM_PPC32)
-
-/* PPC32 */
-#define MONT_START
-#define MONT_FINI
-#define LOOP_END
-#define LOOP_START \
- mu = c[x] * mp
-
-#define INNERMUL \
-__asm__( \
- " mullw 16,%3,%4 \n\t" \
- " mulhwu 17,%3,%4 \n\t" \
- " addc 16,16,%2 \n\t" \
- " addze 17,17 \n\t" \
- " addc %1,16,%5 \n\t" \
- " addze %0,17 \n\t" \
-:"=r"(cy),"=r"(_c[0]):"0"(cy),"r"(mu),"r"(tmpm[0]),"1"(_c[0]):"16", "17", "cc"); ++tmpm;
-
-#define PROPCARRY \
-__asm__( \
- " addc %1,%3,%2 \n\t" \
- " xor %0,%2,%2 \n\t" \
- " addze %0,%2 \n\t" \
-:"=r"(cy),"=r"(_c[0]):"0"(cy),"1"(_c[0]):"cc");
-
-#elif defined(TFM_PPC64)
-
-/* PPC64 */
-#define MONT_START
-#define MONT_FINI
-#define LOOP_END
-#define LOOP_START \
- mu = c[x] * mp
-
-#define INNERMUL \
-__asm__( \
- " mulld r16,%3,%4 \n\t" \
- " mulhdu r17,%3,%4 \n\t" \
- " addc r16,16,%0 \n\t" \
- " addze r17,r17 \n\t" \
- " ldx r18,0,%1 \n\t" \
- " addc r16,r16,r18 \n\t" \
- " addze %0,r17 \n\t" \
- " sdx r16,0,%1 \n\t" \
-:"=r"(cy),"=m"(_c[0]):"0"(cy),"r"(mu),"r"(tmpm[0]),"1"(_c[0]):"r16", "r17", "r18","cc"); ++tmpm;
-
-#define PROPCARRY \
-__asm__( \
- " ldx r16,0,%1 \n\t" \
- " addc r16,r16,%0 \n\t" \
- " sdx r16,0,%1 \n\t" \
- " xor %0,%0,%0 \n\t" \
- " addze %0,%0 \n\t" \
-:"=r"(cy),"=m"(_c[0]):"0"(cy),"1"(_c[0]):"r16","cc");
-
-/******************************************************************/
-
-#elif defined(TFM_AVR32)
-
-/* AVR32 */
-#define MONT_START
-#define MONT_FINI
-#define LOOP_END
-#define LOOP_START \
- mu = c[x] * mp
-
-#define INNERMUL \
-__asm__( \
- " ld.w r2,%1 \n\t" \
- " add r2,%0 \n\t" \
- " eor r3,r3 \n\t" \
- " acr r3 \n\t" \
- " macu.d r2,%3,%4 \n\t" \
- " st.w %1,r2 \n\t" \
- " mov %0,r3 \n\t" \
-:"=r"(cy),"=r"(_c):"0"(cy),"r"(mu),"r"(*tmpm++),"1"(_c):"r2","r3");
-
-#define PROPCARRY \
-__asm__( \
- " ld.w r2,%1 \n\t" \
- " add r2,%0 \n\t" \
- " st.w %1,r2 \n\t" \
- " eor %0,%0 \n\t" \
- " acr %0 \n\t" \
-:"=r"(cy),"=r"(&_c[0]):"0"(cy),"1"(&_c[0]):"r2","cc");
-
-/******************************************************************/
-#elif defined(TFM_MIPS)
-
-/* MIPS */
-#define MONT_START
-#define MONT_FINI
-#define LOOP_END
-#define LOOP_START \
- mu = c[x] * mp
-
-#define INNERMUL \
-__asm__( \
- " multu %3,%4 \n\t" \
- " mflo $12 \n\t" \
- " mfhi $13 \n\t" \
- " addu $12,$12,%0 \n\t" \
- " sltu $10,$12,%0 \n\t" \
- " addu $13,$13,$10 \n\t" \
- " lw $10,%1 \n\t" \
- " addu $12,$12,$10 \n\t" \
- " sltu $10,$12,$10 \n\t" \
- " addu %0,$13,$10 \n\t" \
- " sw $12,%1 \n\t" \
-:"+r"(cy),"+m"(_c[0]):""(cy),"r"(mu),"r"(tmpm[0]),""(_c[0]):"$10","$12","$13"); ++tmpm;
-
-#define PROPCARRY \
-__asm__( \
- " lw $10,%1 \n\t" \
- " addu $10,$10,%0 \n\t" \
- " sw $10,%1 \n\t" \
- " sltu %0,$10,%0 \n\t" \
-:"+r"(cy),"+m"(_c[0]):""(cy),""(_c[0]):"$10");
-
-/******************************************************************/
-#else
-
-/* ISO C code */
-#define MONT_START
-#define MONT_FINI
-#define LOOP_END
-#define LOOP_START \
- mu = c[x] * mp
-
-#define INNERMUL \
- do { fp_word t; \
- t = ((fp_word)_c[0] + (fp_word)cy) + \
- (((fp_word)mu) * ((fp_word)*tmpm++)); \
- _c[0] = (fp_digit)t; \
- cy = (fp_digit)(t >> DIGIT_BIT); \
- } while (0)
-
-#define PROPCARRY \
- do { fp_digit t = _c[0] += cy; cy = (t < cy); } while (0)
-
-#endif
-/******************************************************************/
-
-
-#define LO 0
-/* end fp_montogomery_reduce.c asm */
-
-
-/* start fp_sqr_comba.c asm */
-#if defined(TFM_X86)
-
-/* x86-32 optimized */
-
-#define COMBA_START
-
-#define CLEAR_CARRY \
- c0 = c1 = c2 = 0;
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define CARRY_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_FINI
-
-#define SQRADD(i, j) \
-__asm__( \
- "movl %6,%%eax \n\t" \
- "mull %%eax \n\t" \
- "addl %%eax,%0 \n\t" \
- "adcl %%edx,%1 \n\t" \
- "adcl $0,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "m"(i) :"%eax","%edx","cc");
-
-#define SQRADD2(i, j) \
-__asm__( \
- "movl %6,%%eax \n\t" \
- "mull %7 \n\t" \
- "addl %%eax,%0 \n\t" \
- "adcl %%edx,%1 \n\t" \
- "adcl $0,%2 \n\t" \
- "addl %%eax,%0 \n\t" \
- "adcl %%edx,%1 \n\t" \
- "adcl $0,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "m"(i), "m"(j) :"%eax","%edx", "cc");
-
-#define SQRADDSC(i, j) \
-__asm__( \
- "movl %3,%%eax \n\t" \
- "mull %4 \n\t" \
- "movl %%eax,%0 \n\t" \
- "movl %%edx,%1 \n\t" \
- "xorl %2,%2 \n\t" \
- :"=r"(sc0), "=r"(sc1), "=r"(sc2): "g"(i), "g"(j) :"%eax","%edx","cc");
-
-#define SQRADDAC(i, j) \
-__asm__( \
- "movl %6,%%eax \n\t" \
- "mull %7 \n\t" \
- "addl %%eax,%0 \n\t" \
- "adcl %%edx,%1 \n\t" \
- "adcl $0,%2 \n\t" \
- :"=r"(sc0), "=r"(sc1), "=r"(sc2): "0"(sc0), "1"(sc1), "2"(sc2), "g"(i), "g"(j) :"%eax","%edx","cc");
-
-#define SQRADDDB \
-__asm__( \
- "addl %6,%0 \n\t" \
- "adcl %7,%1 \n\t" \
- "adcl %8,%2 \n\t" \
- "addl %6,%0 \n\t" \
- "adcl %7,%1 \n\t" \
- "adcl %8,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2) : "0"(c0), "1"(c1), "2"(c2), "r"(sc0), "r"(sc1), "r"(sc2) : "cc");
-
-#elif defined(TFM_X86_64)
-/* x86-64 optimized */
-
-#define COMBA_START
-
-#define CLEAR_CARRY \
- c0 = c1 = c2 = 0;
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define CARRY_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_FINI
-
-#define SQRADD(i, j) \
-__asm__( \
- "movq %6,%%rax \n\t" \
- "mulq %%rax \n\t" \
- "addq %%rax,%0 \n\t" \
- "adcq %%rdx,%1 \n\t" \
- "adcq $0,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "x"(i) :"%rax","%rdx","cc");
-
-#define SQRADD2(i, j) \
-__asm__( \
- "movq %6,%%rax \n\t" \
- "mulq %7 \n\t" \
- "addq %%rax,%0 \n\t" \
- "adcq %%rdx,%1 \n\t" \
- "adcq $0,%2 \n\t" \
- "addq %%rax,%0 \n\t" \
- "adcq %%rdx,%1 \n\t" \
- "adcq $0,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "g"(i), "g"(j) :"%rax","%rdx","cc");
-
-#define SQRADDSC(i, j) \
-__asm__( \
- "movq %3,%%rax \n\t" \
- "mulq %4 \n\t" \
- "movq %%rax,%0 \n\t" \
- "movq %%rdx,%1 \n\t" \
- "xorq %2,%2 \n\t" \
- :"=r"(sc0), "=r"(sc1), "=r"(sc2): "g"(i), "g"(j) :"%rax","%rdx","cc");
-
-#define SQRADDAC(i, j) \
-__asm__( \
- "movq %6,%%rax \n\t" \
- "mulq %7 \n\t" \
- "addq %%rax,%0 \n\t" \
- "adcq %%rdx,%1 \n\t" \
- "adcq $0,%2 \n\t" \
- :"=r"(sc0), "=r"(sc1), "=r"(sc2): "0"(sc0), "1"(sc1), "2"(sc2), "g"(i), "g"(j) :"%rax","%rdx","cc");
-
-#define SQRADDDB \
-__asm__( \
- "addq %6,%0 \n\t" \
- "adcq %7,%1 \n\t" \
- "adcq %8,%2 \n\t" \
- "addq %6,%0 \n\t" \
- "adcq %7,%1 \n\t" \
- "adcq %8,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2) : "0"(c0), "1"(c1), "2"(c2), "r"(sc0), "r"(sc1), "r"(sc2) : "cc");
-
-#elif defined(TFM_SSE2)
-
-/* SSE2 Optimized */
-#define COMBA_START
-
-#define CLEAR_CARRY \
- c0 = c1 = c2 = 0;
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define CARRY_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_FINI \
- __asm__("emms");
-
-#define SQRADD(i, j) \
-__asm__( \
- "movd %6,%%mm0 \n\t" \
- "pmuludq %%mm0,%%mm0\n\t" \
- "movd %%mm0,%%eax \n\t" \
- "psrlq $32,%%mm0 \n\t" \
- "addl %%eax,%0 \n\t" \
- "movd %%mm0,%%eax \n\t" \
- "adcl %%eax,%1 \n\t" \
- "adcl $0,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "m"(i) :"%eax","cc");
-
-#define SQRADD2(i, j) \
-__asm__( \
- "movd %6,%%mm0 \n\t" \
- "movd %7,%%mm1 \n\t" \
- "pmuludq %%mm1,%%mm0\n\t" \
- "movd %%mm0,%%eax \n\t" \
- "psrlq $32,%%mm0 \n\t" \
- "movd %%mm0,%%edx \n\t" \
- "addl %%eax,%0 \n\t" \
- "adcl %%edx,%1 \n\t" \
- "adcl $0,%2 \n\t" \
- "addl %%eax,%0 \n\t" \
- "adcl %%edx,%1 \n\t" \
- "adcl $0,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "m"(i), "m"(j) :"%eax","%edx","cc");
-
-#define SQRADDSC(i, j) \
-__asm__( \
- "movd %3,%%mm0 \n\t" \
- "movd %4,%%mm1 \n\t" \
- "pmuludq %%mm1,%%mm0\n\t" \
- "movd %%mm0,%0 \n\t" \
- "psrlq $32,%%mm0 \n\t" \
- "movd %%mm0,%1 \n\t" \
- "xorl %2,%2 \n\t" \
- :"=r"(sc0), "=r"(sc1), "=r"(sc2): "m"(i), "m"(j));
-
-/* TAO removed sc0,1,2 as input to remove warning so %6,%7 become %3,%4 */
-
-#define SQRADDAC(i, j) \
-__asm__( \
- "movd %6,%%mm0 \n\t" \
- "movd %7,%%mm1 \n\t" \
- "pmuludq %%mm1,%%mm0\n\t" \
- "movd %%mm0,%%eax \n\t" \
- "psrlq $32,%%mm0 \n\t" \
- "movd %%mm0,%%edx \n\t" \
- "addl %%eax,%0 \n\t" \
- "adcl %%edx,%1 \n\t" \
- "adcl $0,%2 \n\t" \
- :"=r"(sc0), "=r"(sc1), "=r"(sc2): "0"(sc0), "1"(sc1), "2"(sc2), "m"(i), "m"(j) :"%eax","%edx","cc");
-
-#define SQRADDDB \
-__asm__( \
- "addl %6,%0 \n\t" \
- "adcl %7,%1 \n\t" \
- "adcl %8,%2 \n\t" \
- "addl %6,%0 \n\t" \
- "adcl %7,%1 \n\t" \
- "adcl %8,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2) : "0"(c0), "1"(c1), "2"(c2), "r"(sc0), "r"(sc1), "r"(sc2) : "cc");
-
-#elif defined(TFM_ARM)
-
-/* ARM code */
-
-#define COMBA_START
-
-#define CLEAR_CARRY \
- c0 = c1 = c2 = 0;
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define CARRY_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_FINI
-
-/* multiplies point i and j, updates carry "c1" and digit c2 */
-#define SQRADD(i, j) \
-__asm__( \
-" UMULL r0,r1,%6,%6 \n\t" \
-" ADDS %0,%0,r0 \n\t" \
-" ADCS %1,%1,r1 \n\t" \
-" ADC %2,%2,#0 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2) : "0"(c0), "1"(c1), "2"(c2), "r"(i) : "r0", "r1", "cc");
-
-/* for squaring some of the terms are doubled... */
-#define SQRADD2(i, j) \
-__asm__( \
-" UMULL r0,r1,%6,%7 \n\t" \
-" ADDS %0,%0,r0 \n\t" \
-" ADCS %1,%1,r1 \n\t" \
-" ADC %2,%2,#0 \n\t" \
-" ADDS %0,%0,r0 \n\t" \
-" ADCS %1,%1,r1 \n\t" \
-" ADC %2,%2,#0 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2) : "0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j) : "r0", "r1", "cc");
-
-#define SQRADDSC(i, j) \
-__asm__( \
-" UMULL %0,%1,%3,%4 \n\t" \
-" SUB %2,%2,%2 \n\t" \
-:"=r"(sc0), "=r"(sc1), "=r"(sc2) : "r"(i), "r"(j) : "cc");
-
-/* TAO removed sc0,1,2 as input to remove warning so %6,%7 become %3,%4 */
-
-#define SQRADDAC(i, j) \
-__asm__( \
-" UMULL r0,r1,%6,%7 \n\t" \
-" ADDS %0,%0,r0 \n\t" \
-" ADCS %1,%1,r1 \n\t" \
-" ADC %2,%2,#0 \n\t" \
-:"=r"(sc0), "=r"(sc1), "=r"(sc2) : "0"(sc0), "1"(sc1), "2"(sc2), "r"(i), "r"(j) : "r0", "r1", "cc");
-
-#define SQRADDDB \
-__asm__( \
-" ADDS %0,%0,%3 \n\t" \
-" ADCS %1,%1,%4 \n\t" \
-" ADC %2,%2,%5 \n\t" \
-" ADDS %0,%0,%3 \n\t" \
-" ADCS %1,%1,%4 \n\t" \
-" ADC %2,%2,%5 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2) : "r"(sc0), "r"(sc1), "r"(sc2), "0"(c0), "1"(c1), "2"(c2) : "cc");
-
-#elif defined(TFM_PPC32)
-
-/* PPC32 */
-
-#define COMBA_START
-
-#define CLEAR_CARRY \
- c0 = c1 = c2 = 0;
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define CARRY_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_FINI
-
-/* multiplies point i and j, updates carry "c1" and digit c2 */
-#define SQRADD(i, j) \
-__asm__( \
- " mullw 16,%6,%6 \n\t" \
- " addc %0,%0,16 \n\t" \
- " mulhwu 16,%6,%6 \n\t" \
- " adde %1,%1,16 \n\t" \
- " addze %2,%2 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i):"16","cc");
-
-/* for squaring some of the terms are doubled... */
-#define SQRADD2(i, j) \
-__asm__( \
- " mullw 16,%6,%7 \n\t" \
- " mulhwu 17,%6,%7 \n\t" \
- " addc %0,%0,16 \n\t" \
- " adde %1,%1,17 \n\t" \
- " addze %2,%2 \n\t" \
- " addc %0,%0,16 \n\t" \
- " adde %1,%1,17 \n\t" \
- " addze %2,%2 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"16", "17","cc");
-
-#define SQRADDSC(i, j) \
-__asm__( \
- " mullw %0,%6,%7 \n\t" \
- " mulhwu %1,%6,%7 \n\t" \
- " xor %2,%2,%2 \n\t" \
-:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i),"r"(j) : "cc");
-
-#define SQRADDAC(i, j) \
-__asm__( \
- " mullw 16,%6,%7 \n\t" \
- " addc %0,%0,16 \n\t" \
- " mulhwu 16,%6,%7 \n\t" \
- " adde %1,%1,16 \n\t" \
- " addze %2,%2 \n\t" \
-:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i), "r"(j):"16", "cc");
-
-#define SQRADDDB \
-__asm__( \
- " addc %0,%0,%3 \n\t" \
- " adde %1,%1,%4 \n\t" \
- " adde %2,%2,%5 \n\t" \
- " addc %0,%0,%3 \n\t" \
- " adde %1,%1,%4 \n\t" \
- " adde %2,%2,%5 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2) : "r"(sc0), "r"(sc1), "r"(sc2), "0"(c0), "1"(c1), "2"(c2) : "cc");
-
-#elif defined(TFM_PPC64)
-/* PPC64 */
-
-#define COMBA_START
-
-#define CLEAR_CARRY \
- c0 = c1 = c2 = 0;
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define CARRY_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_FINI
-
-/* multiplies point i and j, updates carry "c1" and digit c2 */
-#define SQRADD(i, j) \
-__asm__( \
- " mulld r16,%6,%6 \n\t" \
- " addc %0,%0,r16 \n\t" \
- " mulhdu r16,%6,%6 \n\t" \
- " adde %1,%1,r16 \n\t" \
- " addze %2,%2 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i):"r16","cc");
-
-/* for squaring some of the terms are doubled... */
-#define SQRADD2(i, j) \
-__asm__( \
- " mulld r16,%6,%7 \n\t" \
- " mulhdu r17,%6,%7 \n\t" \
- " addc %0,%0,r16 \n\t" \
- " adde %1,%1,r17 \n\t" \
- " addze %2,%2 \n\t" \
- " addc %0,%0,r16 \n\t" \
- " adde %1,%1,r17 \n\t" \
- " addze %2,%2 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"r16", "r17","cc");
-
-#define SQRADDSC(i, j) \
-__asm__( \
- " mulld %0,%6,%7 \n\t" \
- " mulhdu %1,%6,%7 \n\t" \
- " xor %2,%2,%2 \n\t" \
-:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i),"r"(j) : "cc");
-
-#define SQRADDAC(i, j) \
-__asm__( \
- " mulld r16,%6,%7 \n\t" \
- " addc %0,%0,r16 \n\t" \
- " mulhdu r16,%6,%7 \n\t" \
- " adde %1,%1,r16 \n\t" \
- " addze %2,%2 \n\t" \
-:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i), "r"(j):"r16", "cc");
-
-#define SQRADDDB \
-__asm__( \
- " addc %0,%0,%3 \n\t" \
- " adde %1,%1,%4 \n\t" \
- " adde %2,%2,%5 \n\t" \
- " addc %0,%0,%3 \n\t" \
- " adde %1,%1,%4 \n\t" \
- " adde %2,%2,%5 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2) : "r"(sc0), "r"(sc1), "r"(sc2), "0"(c0), "1"(c1), "2"(c2) : "cc");
-
-
-#elif defined(TFM_AVR32)
-
-/* AVR32 */
-
-#define COMBA_START
-
-#define CLEAR_CARRY \
- c0 = c1 = c2 = 0;
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define CARRY_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_FINI
-
-/* multiplies point i and j, updates carry "c1" and digit c2 */
-#define SQRADD(i, j) \
-__asm__( \
- " mulu.d r2,%6,%6 \n\t" \
- " add %0,%0,r2 \n\t" \
- " adc %1,%1,r3 \n\t" \
- " acr %2 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i):"r2","r3");
-
-/* for squaring some of the terms are doubled... */
-#define SQRADD2(i, j) \
-__asm__( \
- " mulu.d r2,%6,%7 \n\t" \
- " add %0,%0,r2 \n\t" \
- " adc %1,%1,r3 \n\t" \
- " acr %2, \n\t" \
- " add %0,%0,r2 \n\t" \
- " adc %1,%1,r3 \n\t" \
- " acr %2, \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"r2", "r3");
-
-#define SQRADDSC(i, j) \
-__asm__( \
- " mulu.d r2,%6,%7 \n\t" \
- " mov %0,r2 \n\t" \
- " mov %1,r3 \n\t" \
- " eor %2,%2 \n\t" \
-:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i),"r"(j) : "r2", "r3");
-
-#define SQRADDAC(i, j) \
-__asm__( \
- " mulu.d r2,%6,%7 \n\t" \
- " add %0,%0,r2 \n\t" \
- " adc %1,%1,r3 \n\t" \
- " acr %2 \n\t" \
-:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i), "r"(j):"r2", "r3");
-
-#define SQRADDDB \
-__asm__( \
- " add %0,%0,%3 \n\t" \
- " adc %1,%1,%4 \n\t" \
- " adc %2,%2,%5 \n\t" \
- " add %0,%0,%3 \n\t" \
- " adc %1,%1,%4 \n\t" \
- " adc %2,%2,%5 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2) : "r"(sc0), "r"(sc1), "r"(sc2), "0"(c0), "1"(c1), "2"(c2) : "cc");
-
-#elif defined(TFM_MIPS)
-
-/* MIPS */
-#define COMBA_START
-
-#define CLEAR_CARRY \
- c0 = c1 = c2 = 0;
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define CARRY_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_FINI
-
-/* multiplies point i and j, updates carry "c1" and digit c2 */
-#define SQRADD(i, j) \
-__asm__( \
- " multu %6,%6 \n\t" \
- " mflo $12 \n\t" \
- " mfhi $13 \n\t" \
- " addu %0,%0,$12 \n\t" \
- " sltu $12,%0,$12 \n\t" \
- " addu %1,%1,$13 \n\t" \
- " sltu $13,%1,$13 \n\t" \
- " addu %1,%1,$12 \n\t" \
- " sltu $12,%1,$12 \n\t" \
- " addu %2,%2,$13 \n\t" \
- " addu %2,%2,$12 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i):"$12","$13");
-
-/* for squaring some of the terms are doubled... */
-#define SQRADD2(i, j) \
-__asm__( \
- " multu %6,%7 \n\t" \
- " mflo $12 \n\t" \
- " mfhi $13 \n\t" \
- \
- " addu %0,%0,$12 \n\t" \
- " sltu $14,%0,$12 \n\t" \
- " addu %1,%1,$13 \n\t" \
- " sltu $15,%1,$13 \n\t" \
- " addu %1,%1,$14 \n\t" \
- " sltu $14,%1,$14 \n\t" \
- " addu %2,%2,$15 \n\t" \
- " addu %2,%2,$14 \n\t" \
- \
- " addu %0,%0,$12 \n\t" \
- " sltu $14,%0,$12 \n\t" \
- " addu %1,%1,$13 \n\t" \
- " sltu $15,%1,$13 \n\t" \
- " addu %1,%1,$14 \n\t" \
- " sltu $14,%1,$14 \n\t" \
- " addu %2,%2,$15 \n\t" \
- " addu %2,%2,$14 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"$12", "$13", "$14", "$15");
-
-#define SQRADDSC(i, j) \
-__asm__( \
- " multu %6,%7 \n\t" \
- " mflo %0 \n\t" \
- " mfhi %1 \n\t" \
- " xor %2,%2,%2 \n\t" \
-:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i),"r"(j) : "cc");
-
-#define SQRADDAC(i, j) \
-__asm__( \
- " multu %6,%7 \n\t" \
- " mflo $12 \n\t" \
- " mfhi $13 \n\t" \
- " addu %0,%0,$12 \n\t" \
- " sltu $12,%0,$12 \n\t" \
- " addu %1,%1,$13 \n\t" \
- " sltu $13,%1,$13 \n\t" \
- " addu %1,%1,$12 \n\t" \
- " sltu $12,%1,$12 \n\t" \
- " addu %2,%2,$13 \n\t" \
- " addu %2,%2,$12 \n\t" \
-:"=r"(sc0), "=r"(sc1), "=r"(sc2):"0"(sc0), "1"(sc1), "2"(sc2), "r"(i), "r"(j):"$12", "$13", "$14");
-
-#define SQRADDDB \
-__asm__( \
- " addu %0,%0,%3 \n\t" \
- " sltu $10,%0,%3 \n\t" \
- " addu %1,%1,$10 \n\t" \
- " sltu $10,%1,$10 \n\t" \
- " addu %1,%1,%4 \n\t" \
- " sltu $11,%1,%4 \n\t" \
- " addu %2,%2,$10 \n\t" \
- " addu %2,%2,$11 \n\t" \
- " addu %2,%2,%5 \n\t" \
- \
- " addu %0,%0,%3 \n\t" \
- " sltu $10,%0,%3 \n\t" \
- " addu %1,%1,$10 \n\t" \
- " sltu $10,%1,$10 \n\t" \
- " addu %1,%1,%4 \n\t" \
- " sltu $11,%1,%4 \n\t" \
- " addu %2,%2,$10 \n\t" \
- " addu %2,%2,$11 \n\t" \
- " addu %2,%2,%5 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2) : "r"(sc0), "r"(sc1), "r"(sc2), "0"(c0), "1"(c1), "2"(c2) : "$10", "$11");
-
-#else
-
-#define TFM_ISO
-
-/* ISO C portable code */
-
-#define COMBA_START
-
-#define CLEAR_CARRY \
- c0 = c1 = c2 = 0;
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define CARRY_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_FINI
-
-/* multiplies point i and j, updates carry "c1" and digit c2 */
-#define SQRADD(i, j) \
- do { fp_word t; \
- t = c0 + ((fp_word)i) * ((fp_word)j); c0 = (fp_digit)t; \
- t = c1 + (t >> DIGIT_BIT); c1 = (fp_digit)t; \
- c2 +=(fp_digit) (t >> DIGIT_BIT); \
- } while (0);
-
-
-/* for squaring some of the terms are doubled... */
-#define SQRADD2(i, j) \
- do { fp_word t; \
- t = ((fp_word)i) * ((fp_word)j); \
- tt = (fp_word)c0 + t; c0 = (fp_digit)tt; \
- tt = (fp_word)c1 + (tt >> DIGIT_BIT); c1 = (fp_digit)tt; \
- c2 +=(fp_digit)(tt >> DIGIT_BIT); \
- tt = (fp_word)c0 + t; c0 = (fp_digit)tt; \
- tt = (fp_word)c1 + (tt >> DIGIT_BIT); c1 = (fp_digit)tt; \
- c2 +=(fp_digit)(tt >> DIGIT_BIT); \
- } while (0);
-
-#define SQRADDSC(i, j) \
- do { fp_word t; \
- t = ((fp_word)i) * ((fp_word)j); \
- sc0 = (fp_digit)t; sc1 = (t >> DIGIT_BIT); sc2 = 0; \
- } while (0);
-
-#define SQRADDAC(i, j) \
- do { fp_word t; \
- t = sc0 + ((fp_word)i) * ((fp_word)j); sc0 = (fp_digit)t; \
- t = sc1 + (t >> DIGIT_BIT); sc1 = (fp_digit)t; \
- sc2 += (fp_digit)(t >> DIGIT_BIT); \
- } while (0);
-
-#define SQRADDDB \
- do { fp_word t; \
- t = ((fp_word)sc0) + ((fp_word)sc0) + c0; c0 = (fp_digit)t; \
- t = ((fp_word)sc1) + ((fp_word)sc1) + c1 + (t >> DIGIT_BIT); \
- c1 = (fp_digit)t; \
- c2 = c2 + (fp_digit)(((fp_word)sc2) + ((fp_word)sc2) + (t >> DIGIT_BIT)); \
- } while (0);
-
-#endif
-
-#ifdef TFM_SMALL_SET
- #include "fp_sqr_comba_small_set.i"
-#endif
-
-#if defined(TFM_SQR3) && FP_SIZE >= 6
- #include "fp_sqr_comba_3.i"
-#endif
-#if defined(TFM_SQR4) && FP_SIZE >= 8
- #include "fp_sqr_comba_4.i"
-#endif
-#if defined(TFM_SQR6) && FP_SIZE >= 12
- #include "fp_sqr_comba_6.i"
-#endif
-#if defined(TFM_SQR7) && FP_SIZE >= 14
- #include "fp_sqr_comba_7.i"
-#endif
-#if defined(TFM_SQR8) && FP_SIZE >= 16
- #include "fp_sqr_comba_8.i"
-#endif
-#if defined(TFM_SQR9) && FP_SIZE >= 18
- #include "fp_sqr_comba_9.i"
-#endif
-#if defined(TFM_SQR12) && FP_SIZE >= 24
- #include "fp_sqr_comba_12.i"
-#endif
-#if defined(TFM_SQR17) && FP_SIZE >= 34
- #include "fp_sqr_comba_17.i"
-#endif
-#if defined(TFM_SQR20) && FP_SIZE >= 40
- #include "fp_sqr_comba_20.i"
-#endif
-#if defined(TFM_SQR24) && FP_SIZE >= 48
- #include "fp_sqr_comba_24.i"
-#endif
-#if defined(TFM_SQR28) && FP_SIZE >= 56
- #include "fp_sqr_comba_28.i"
-#endif
-#if defined(TFM_SQR32) && FP_SIZE >= 64
- #include "fp_sqr_comba_32.i"
-#endif
-#if defined(TFM_SQR48) && FP_SIZE >= 96
- #include "fp_sqr_comba_48.i"
-#endif
-#if defined(TFM_SQR64) && FP_SIZE >= 128
- #include "fp_sqr_comba_64.i"
-#endif
-/* end fp_sqr_comba.c asm */
-
-/* start fp_mul_comba.c asm */
-/* these are the combas. Worship them. */
-#if defined(TFM_X86)
-/* Generic x86 optimized code */
-
-/* anything you need at the start */
-#define COMBA_START
-
-/* clear the chaining variables */
-#define COMBA_CLEAR \
- c0 = c1 = c2 = 0;
-
-/* forward the carry to the next digit */
-#define COMBA_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-/* store the first sum */
-#define COMBA_STORE(x) \
- x = c0;
-
-/* store the second sum [carry] */
-#define COMBA_STORE2(x) \
- x = c1;
-
-/* anything you need at the end */
-#define COMBA_FINI
-
-/* this should multiply i and j */
-#define MULADD(i, j) \
-__asm__( \
- "movl %6,%%eax \n\t" \
- "mull %7 \n\t" \
- "addl %%eax,%0 \n\t" \
- "adcl %%edx,%1 \n\t" \
- "adcl $0,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "m"(i), "m"(j) :"%eax","%edx","cc");
-
-#elif defined(TFM_X86_64)
-/* x86-64 optimized */
-
-/* anything you need at the start */
-#define COMBA_START
-
-/* clear the chaining variables */
-#define COMBA_CLEAR \
- c0 = c1 = c2 = 0;
-
-/* forward the carry to the next digit */
-#define COMBA_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-/* store the first sum */
-#define COMBA_STORE(x) \
- x = c0;
-
-/* store the second sum [carry] */
-#define COMBA_STORE2(x) \
- x = c1;
-
-/* anything you need at the end */
-#define COMBA_FINI
-
-/* this should multiply i and j */
-#define MULADD(i, j) \
-__asm__ ( \
- "movq %6,%%rax \n\t" \
- "mulq %7 \n\t" \
- "addq %%rax,%0 \n\t" \
- "adcq %%rdx,%1 \n\t" \
- "adcq $0,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "g"(i), "g"(j) :"%rax","%rdx","cc");
-
-
-#if defined(HAVE_INTEL_MULX)
-#define MULADD_BODY(a,b,c) \
- __asm__ volatile( \
- "movq %[a0],%%rdx\n\t" \
- "xorq %%rcx, %%rcx\n\t" \
- "movq 0(%[cp]),%%r8\n\t" \
- "movq 8(%[cp]),%%r9\n\t" \
- "movq 16(%[cp]),%%r10\n\t" \
- "movq 24(%[cp]),%%r11\n\t" \
- "movq 32(%[cp]),%%r12\n\t" \
- "movq 40(%[cp]),%%r13\n\t" \
- \
- "mulx (%[bp]),%%rax, %%rbx\n\t" \
- "adoxq %%rax, %%r8\n\t" \
- "mulx 8(%[bp]),%%rax, %%rcx\n\t" \
- "adcxq %%rbx, %%r9\n\t" \
- "adoxq %%rax, %%r9\n\t" \
- "mulx 16(%[bp]),%%rax, %%rbx\n\t" \
- "adcxq %%rcx, %%r10\n\t" \
- "adoxq %%rax, %%r10\n\t" \
- "mulx 24(%[bp]),%%rax, %%rcx\n\t" \
- "adcxq %%rbx, %%r11\n\t" \
- "adoxq %%rax, %%r11\n\t" \
- "adcxq %%rcx, %%r12\n\t" \
- "mov $0, %%rdx\n\t" \
- "adox %%rdx, %%r12\n\t" \
- "adcx %%rdx, %%r13\n\t" \
- \
- "movq %%r8, 0(%[cp])\n\t" \
- "movq %%r9, 8(%[cp])\n\t" \
- "movq %%r10, 16(%[cp])\n\t" \
- "movq %%r11, 24(%[cp])\n\t" \
- "movq %%r12, 32(%[cp])\n\t" \
- "movq %%r13, 40(%[cp])\n\t" \
- : \
- : [a0] "r" (a->dp[ix]), [bp] "r" (&(b->dp[iy])), \
- [cp] "r" (&(c->dp[iz])) \
- : "%r8", "%r9", "%r10", "%r11", "%r12", "%r13", \
- "%rdx", "%rax", "%rcx", "%rbx" \
- )
-
-#define TFM_INTEL_MUL_COMBA(a, b, c) \
- for (iz=0; izdp[iz] = 0; \
- for (ix=0; ixused; ix++) { \
- for (iy=0; iyused; iy+=4) { \
- iz = ix + iy; \
- MULADD_BODY(a, b, c); \
- } \
- }
-#endif
-
-#elif defined(TFM_SSE2)
-/* use SSE2 optimizations */
-
-/* anything you need at the start */
-#define COMBA_START
-
-/* clear the chaining variables */
-#define COMBA_CLEAR \
- c0 = c1 = c2 = 0;
-
-/* forward the carry to the next digit */
-#define COMBA_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-/* store the first sum */
-#define COMBA_STORE(x) \
- x = c0;
-
-/* store the second sum [carry] */
-#define COMBA_STORE2(x) \
- x = c1;
-
-/* anything you need at the end */
-#define COMBA_FINI \
- __asm__("emms");
-
-/* this should multiply i and j */
-#define MULADD(i, j) \
-__asm__( \
- "movd %6,%%mm0 \n\t" \
- "movd %7,%%mm1 \n\t" \
- "pmuludq %%mm1,%%mm0\n\t" \
- "movd %%mm0,%%eax \n\t" \
- "psrlq $32,%%mm0 \n\t" \
- "addl %%eax,%0 \n\t" \
- "movd %%mm0,%%eax \n\t" \
- "adcl %%eax,%1 \n\t" \
- "adcl $0,%2 \n\t" \
- :"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "m"(i), "m"(j) :"%eax","cc");
-
-#elif defined(TFM_ARM)
-/* ARM code */
-
-#define COMBA_START
-
-#define COMBA_CLEAR \
- c0 = c1 = c2 = 0;
-
-#define COMBA_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define COMBA_FINI
-
-#define MULADD(i, j) \
-__asm__( \
-" UMULL r0,r1,%6,%7 \n\t" \
-" ADDS %0,%0,r0 \n\t" \
-" ADCS %1,%1,r1 \n\t" \
-" ADC %2,%2,#0 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2) : "0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j) : "r0", "r1", "cc");
-
-#elif defined(TFM_PPC32)
-/* For 32-bit PPC */
-
-#define COMBA_START
-
-#define COMBA_CLEAR \
- c0 = c1 = c2 = 0;
-
-#define COMBA_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define COMBA_FINI
-
-/* untested: will mulhwu change the flags? Docs say no */
-#define MULADD(i, j) \
-__asm__( \
- " mullw 16,%6,%7 \n\t" \
- " addc %0,%0,16 \n\t" \
- " mulhwu 16,%6,%7 \n\t" \
- " adde %1,%1,16 \n\t" \
- " addze %2,%2 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"16");
-
-#elif defined(TFM_PPC64)
-/* For 64-bit PPC */
-
-#define COMBA_START
-
-#define COMBA_CLEAR \
- c0 = c1 = c2 = 0;
-
-#define COMBA_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define COMBA_FINI
-
-/* untested: will mulhdu change the flags? Docs say no */
-#define MULADD(i, j) \
-____asm__( \
- " mulld r16,%6,%7 \n\t" \
- " addc %0,%0,16 \n\t" \
- " mulhdu r16,%6,%7 \n\t" \
- " adde %1,%1,16 \n\t" \
- " addze %2,%2 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"r16");
-
-#elif defined(TFM_AVR32)
-
-/* ISO C code */
-
-#define COMBA_START
-
-#define COMBA_CLEAR \
- c0 = c1 = c2 = 0;
-
-#define COMBA_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define COMBA_FINI
-
-#define MULADD(i, j) \
-____asm__( \
- " mulu.d r2,%6,%7 \n\t"\
- " add %0,r2 \n\t"\
- " adc %1,%1,r3 \n\t"\
- " acr %2 \n\t"\
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"r2","r3");
-
-#elif defined(TFM_MIPS)
-
-/* MIPS */
-#define COMBA_START
-
-#define COMBA_CLEAR \
- c0 = c1 = c2 = 0;
-
-#define COMBA_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define COMBA_FINI
-
-#define MULADD(i, j) \
-__asm__( \
- " multu %6,%7 \n\t" \
- " mflo $12 \n\t" \
- " mfhi $13 \n\t" \
- " addu %0,%0,$12 \n\t" \
- " sltu $12,%0,$12 \n\t" \
- " addu %1,%1,$13 \n\t" \
- " sltu $13,%1,$13 \n\t" \
- " addu %1,%1,$12 \n\t" \
- " sltu $12,%1,$12 \n\t" \
- " addu %2,%2,$13 \n\t" \
- " addu %2,%2,$12 \n\t" \
-:"=r"(c0), "=r"(c1), "=r"(c2):"0"(c0), "1"(c1), "2"(c2), "r"(i), "r"(j):"$12","$13");
-
-#else
-/* ISO C code */
-
-#define COMBA_START
-
-#define COMBA_CLEAR \
- c0 = c1 = c2 = 0;
-
-#define COMBA_FORWARD \
- do { c0 = c1; c1 = c2; c2 = 0; } while (0);
-
-#define COMBA_STORE(x) \
- x = c0;
-
-#define COMBA_STORE2(x) \
- x = c1;
-
-#define COMBA_FINI
-
-#define MULADD(i, j) \
- do { fp_word t; \
- t = (fp_word)c0 + ((fp_word)i) * ((fp_word)j); \
- c0 = (fp_digit)t; \
- t = (fp_word)c1 + (t >> DIGIT_BIT); \
- c1 = (fp_digit)t; \
- c2 += (fp_digit)(t >> DIGIT_BIT); \
- } while (0);
-
-#endif
-
-
-#ifdef TFM_SMALL_SET
- #include "fp_mul_comba_small_set.i"
-#endif
-
-#if defined(TFM_MUL3) && FP_SIZE >= 6
- #include "fp_mul_comba_3.i"
-#endif
-#if defined(TFM_MUL4) && FP_SIZE >= 8
- #include "fp_mul_comba_4.i"
-#endif
-#if defined(TFM_MUL6) && FP_SIZE >= 12
- #include "fp_mul_comba_6.i"
-#endif
-#if defined(TFM_MUL7) && FP_SIZE >= 14
- #include "fp_mul_comba_7.i"
-#endif
-#if defined(TFM_MUL8) && FP_SIZE >= 16
- #include "fp_mul_comba_8.i"
-#endif
-#if defined(TFM_MUL9) && FP_SIZE >= 18
- #include "fp_mul_comba_9.i"
-#endif
-#if defined(TFM_MUL12) && FP_SIZE >= 24
- #include "fp_mul_comba_12.i"
-#endif
-#if defined(TFM_MUL17) && FP_SIZE >= 34
- #include "fp_mul_comba_17.i"
-#endif
-#if defined(TFM_MUL20) && FP_SIZE >= 40
- #include "fp_mul_comba_20.i"
-#endif
-#if defined(TFM_MUL24) && FP_SIZE >= 48
- #include "fp_mul_comba_24.i"
-#endif
-#if defined(TFM_MUL28) && FP_SIZE >= 56
- #include "fp_mul_comba_28.i"
-#endif
-#if defined(TFM_MUL32) && FP_SIZE >= 64
- #include "fp_mul_comba_32.i"
-#endif
-#if defined(TFM_MUL48) && FP_SIZE >= 96
- #include "fp_mul_comba_48.i"
-#endif
-#if defined(TFM_MUL64) && FP_SIZE >= 128
- #include "fp_mul_comba_64.i"
-#endif
-
-/* end fp_mul_comba.c asm */
-
diff --git a/client/wolfssl/wolfcrypt/src/asn.c b/client/wolfssl/wolfcrypt/src/asn.c
deleted file mode 100644
index c4e6004..0000000
--- a/client/wolfssl/wolfcrypt/src/asn.c
+++ /dev/null
@@ -1,17525 +0,0 @@
-/* asn.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-/*
-ASN Options:
- * NO_ASN_TIME: Disables time parts of the ASN code for systems without an RTC
- or wishing to save space.
- * IGNORE_NAME_CONSTRAINTS: Skip ASN name checks.
- * ASN_DUMP_OID: Allows dump of OID information for debugging.
- * RSA_DECODE_EXTRA: Decodes extra information in RSA public key.
- * WOLFSSL_CERT_GEN: Cert generation. Saves extra certificate info in GetName.
- * WOLFSSL_NO_ASN_STRICT: Disable strict RFC compliance checks to
- restore 3.13.0 behavior.
- * WOLFSSL_NO_OCSP_OPTIONAL_CERTS: Skip optional OCSP certs (responder issuer
- must still be trusted)
- * WOLFSSL_NO_TRUSTED_CERTS_VERIFY: Workaround for situation where entire cert
- chain is not loaded. This only matches on subject and public key and
- does not perform a PKI validation, so it is not a secure solution.
- Only enabled for OCSP.
- * WOLFSSL_NO_OCSP_ISSUER_CHECK: Can be defined for backwards compatibility to
- disable checking of OCSP subject hash with issuer hash.
- * WOLFSSL_SMALL_CERT_VERIFY: Verify the certificate signature without using
- DecodedCert. Doubles up on some code but allows smaller dynamic memory
- usage.
- * WOLFSSL_NO_OCSP_DATE_CHECK: Disable date checks for OCSP responses. This
- may be required when the system's real-time clock is not very accurate.
- It is recommended to enforce the nonce check instead if possible.
- * WOLFSSL_FORCE_OCSP_NONCE_CHECK: Require nonces to be available in OCSP
- responses. The nonces are optional and may not be supported by all
- responders. If it can be ensured that the used responder sends nonces this
- option may improve security.
-*/
-
-#ifndef NO_ASN
-
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-
-#include
-#include
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include
-#endif
-
-#ifndef NO_RC4
- #include
-#endif
-
-#ifdef HAVE_NTRU
- #include "libntruencrypt/ntru_crypto.h"
-#endif
-
-#if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
- #include
-#endif
-
-#ifndef NO_SHA256
- #include
-#endif
-
-#ifdef HAVE_ECC
- #include
-#endif
-
-#ifdef HAVE_ED25519
- #include
-#endif
-
-#ifdef HAVE_ED448
- #include
-#endif
-
-#ifndef NO_RSA
- #include
-#if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_CRYPTOCELL)
-extern int wc_InitRsaHw(RsaKey* key);
-#endif
-#endif
-
-#ifdef WOLF_CRYPTO_CB
- #include
-#endif
-
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- #include
-#endif
-
-#ifdef _MSC_VER
- /* 4996 warning to use MS extensions e.g., strcpy_s instead of XSTRNCPY */
- #pragma warning(disable: 4996)
-#endif
-
-#define ERROR_OUT(err, eLabel) { ret = (err); goto eLabel; }
-
-#if defined(HAVE_SELFTEST) || ( !defined(NO_SKID) && \
- ( !defined(HAVE_FIPS) || \
- !defined(HAVE_FIPS_VERSION) ))
- #ifndef WOLFSSL_AES_KEY_SIZE_ENUM
- #define WOLFSSL_AES_KEY_SIZE_ENUM
- enum Asn_Misc {
- AES_IV_SIZE = 16,
- AES_128_KEY_SIZE = 16,
- AES_192_KEY_SIZE = 24,
- AES_256_KEY_SIZE = 32
- };
- #endif
-#endif
-#ifdef WOLFSSL_RENESAS_TSIP_TLS
-void tsip_inform_key_position(const word32 key_n_start,
- const word32 key_n_len, const word32 key_e_start,
- const word32 key_e_len);
-int tsip_tls_CertVerify(const byte *cert, word32 certSz,
- const byte *signature, word32 sigSz,
- word32 key_n_start, word32 key_n_len,
- word32 key_e_start, word32 key_e_len,
- byte *tsip_encRsaKeyIdx);
-#endif
-int GetLength(const byte* input, word32* inOutIdx, int* len,
- word32 maxIdx)
-{
- return GetLength_ex(input, inOutIdx, len, maxIdx, 1);
-}
-
-
-/* give option to check length value found against index. 1 to check 0 to not */
-int GetLength_ex(const byte* input, word32* inOutIdx, int* len,
- word32 maxIdx, int check)
-{
- int length = 0;
- word32 idx = *inOutIdx;
- byte b;
-
- *len = 0; /* default length */
-
- if ((idx + 1) > maxIdx) { /* for first read */
- WOLFSSL_MSG("GetLength bad index on input");
- return BUFFER_E;
- }
-
- b = input[idx++];
- if (b >= ASN_LONG_LENGTH) {
- word32 bytes = b & 0x7F;
-
- if ((idx + bytes) > maxIdx) { /* for reading bytes */
- WOLFSSL_MSG("GetLength bad long length");
- return BUFFER_E;
- }
-
- if (bytes > sizeof(length)) {
- return ASN_PARSE_E;
- }
- while (bytes--) {
- b = input[idx++];
- length = (length << 8) | b;
- }
- if (length < 0) {
- return ASN_PARSE_E;
- }
- }
- else
- length = b;
-
- if (check && (idx + length) > maxIdx) { /* for user of length */
- WOLFSSL_MSG("GetLength value exceeds buffer length");
- return BUFFER_E;
- }
-
- *inOutIdx = idx;
- if (length > 0)
- *len = length;
-
- return length;
-}
-
-
-/* input : buffer to read from
- * inOutIdx : index to start reading from, gets advanced by 1 if successful
- * maxIdx : maximum index value
- * tag : ASN tag value found
- *
- * returns 0 on success
- */
-int GetASNTag(const byte* input, word32* inOutIdx, byte* tag, word32 maxIdx)
-{
- word32 idx;
-
- if (tag == NULL || inOutIdx == NULL || input == NULL) {
- return BAD_FUNC_ARG;
- }
-
- idx = *inOutIdx;
- if (idx + ASN_TAG_SZ > maxIdx) {
- WOLFSSL_MSG("Buffer too small for ASN tag");
- return BUFFER_E;
- }
-
- *tag = input[idx];
- *inOutIdx = idx + ASN_TAG_SZ;
- return 0;
-}
-
-
-static int GetASNHeader_ex(const byte* input, byte tag, word32* inOutIdx, int* len,
- word32 maxIdx, int check)
-{
- word32 idx = *inOutIdx;
- byte tagFound;
- int length;
-
- if (GetASNTag(input, &idx, &tagFound, maxIdx) != 0)
- return ASN_PARSE_E;
-
- if (tagFound != tag)
- return ASN_PARSE_E;
-
- if (GetLength_ex(input, &idx, &length, maxIdx, check) < 0)
- return ASN_PARSE_E;
-
- *len = length;
- *inOutIdx = idx;
- return length;
-}
-
-
-/* Get the DER/BER encoding of an ASN.1 header.
- *
- * input Buffer holding DER/BER encoded data.
- * tag ASN.1 tag value expected in header.
- * inOutIdx Current index into buffer to parse.
- * len The number of bytes in the ASN.1 data.
- * maxIdx Length of data in buffer.
- * returns BUFFER_E when there is not enough data to parse.
- * ASN_PARSE_E when the expected tag is not found or length is invalid.
- * Otherwise, the number of bytes in the ASN.1 data.
- */
-static int GetASNHeader(const byte* input, byte tag, word32* inOutIdx, int* len,
- word32 maxIdx)
-{
- return GetASNHeader_ex(input, tag, inOutIdx, len, maxIdx, 1);
-}
-
-static int GetHeader(const byte* input, byte* tag, word32* inOutIdx, int* len,
- word32 maxIdx, int check)
-{
- word32 idx = *inOutIdx;
- int length;
-
- if ((idx + 1) > maxIdx)
- return BUFFER_E;
-
- *tag = input[idx++];
-
- if (GetLength_ex(input, &idx, &length, maxIdx, check) < 0)
- return ASN_PARSE_E;
-
- *len = length;
- *inOutIdx = idx;
- return length;
-}
-
-int GetSequence(const byte* input, word32* inOutIdx, int* len,
- word32 maxIdx)
-{
- return GetASNHeader(input, ASN_SEQUENCE | ASN_CONSTRUCTED, inOutIdx, len,
- maxIdx);
-}
-
-
-int GetSequence_ex(const byte* input, word32* inOutIdx, int* len,
- word32 maxIdx, int check)
-{
- return GetASNHeader_ex(input, ASN_SEQUENCE | ASN_CONSTRUCTED, inOutIdx, len,
- maxIdx, check);
-}
-
-
-int GetSet(const byte* input, word32* inOutIdx, int* len,
- word32 maxIdx)
-{
- return GetASNHeader(input, ASN_SET | ASN_CONSTRUCTED, inOutIdx, len,
- maxIdx);
-}
-
-
-int GetSet_ex(const byte* input, word32* inOutIdx, int* len,
- word32 maxIdx, int check)
-{
- return GetASNHeader_ex(input, ASN_SET | ASN_CONSTRUCTED, inOutIdx, len,
- maxIdx, check);
-}
-
-/* Get the DER/BER encoded ASN.1 NULL element.
- * Ensure that the all fields are as expected and move index past the element.
- *
- * input Buffer holding DER/BER encoded data.
- * inOutIdx Current index into buffer to parse.
- * maxIdx Length of data in buffer.
- * returns BUFFER_E when there is not enough data to parse.
- * ASN_TAG_NULL_E when the NULL tag is not found.
- * ASN_EXPECT_0_E when the length is not zero.
- * Otherwise, 0 to indicate success.
- */
-static int GetASNNull(const byte* input, word32* inOutIdx, word32 maxIdx)
-{
- word32 idx = *inOutIdx;
- byte b;
-
- if ((idx + 2) > maxIdx)
- return BUFFER_E;
-
- b = input[idx++];
- if (b != ASN_TAG_NULL)
- return ASN_TAG_NULL_E;
-
- if (input[idx++] != 0)
- return ASN_EXPECT_0_E;
-
- *inOutIdx = idx;
- return 0;
-}
-
-/* Set the DER/BER encoding of the ASN.1 NULL element.
- *
- * output Buffer to write into.
- * returns the number of bytes added to the buffer.
- */
-static int SetASNNull(byte* output)
-{
- output[0] = ASN_TAG_NULL;
- output[1] = 0;
-
- return 2;
-}
-
-/* Get the DER/BER encoding of an ASN.1 BOOLEAN.
- *
- * input Buffer holding DER/BER encoded data.
- * inOutIdx Current index into buffer to parse.
- * maxIdx Length of data in buffer.
- * returns BUFFER_E when there is not enough data to parse.
- * ASN_PARSE_E when the BOOLEAN tag is not found or length is not 1.
- * Otherwise, 0 to indicate the value was false and 1 to indicate true.
- */
-static int GetBoolean(const byte* input, word32* inOutIdx, word32 maxIdx)
-{
- word32 idx = *inOutIdx;
- byte b;
-
- if ((idx + 3) > maxIdx)
- return BUFFER_E;
-
- b = input[idx++];
- if (b != ASN_BOOLEAN)
- return ASN_PARSE_E;
-
- if (input[idx++] != 1)
- return ASN_PARSE_E;
-
- b = input[idx++] != 0;
-
- *inOutIdx = idx;
- return b;
-}
-
-#ifdef ASN1_SET_BOOLEAN
-/* Set the DER/BER encoding of the ASN.1 NULL element.
- * Note: Function not required as yet.
- *
- * val Boolean value to encode.
- * output Buffer to write into.
- * returns the number of bytes added to the buffer.
- */
-static int SetBoolean(int val, byte* output)
-{
- output[0] = ASN_BOOLEAN;
- output[1] = 1;
- output[2] = val ? -1 : 0;
-
- return 3;
-}
-#endif
-
-/* Get the DER/BER encoding of an ASN.1 OCTET_STRING header.
- *
- * input Buffer holding DER/BER encoded data.
- * inOutIdx Current index into buffer to parse.
- * len The number of bytes in the ASN.1 data.
- * maxIdx Length of data in buffer.
- * returns BUFFER_E when there is not enough data to parse.
- * ASN_PARSE_E when the OCTET_STRING tag is not found or length is
- * invalid.
- * Otherwise, the number of bytes in the ASN.1 data.
- */
-int GetOctetString(const byte* input, word32* inOutIdx, int* len,
- word32 maxIdx)
-{
- return GetASNHeader(input, ASN_OCTET_STRING, inOutIdx, len, maxIdx);
-}
-
-/* Get the DER/BER encoding of an ASN.1 INTEGER header.
- * Removes the leading zero byte when found.
- *
- * input Buffer holding DER/BER encoded data.
- * inOutIdx Current index into buffer to parse.
- * len The number of bytes in the ASN.1 data (excluding any leading zero).
- * maxIdx Length of data in buffer.
- * returns BUFFER_E when there is not enough data to parse.
- * ASN_PARSE_E when the INTEGER tag is not found, length is invalid,
- * or invalid use of or missing leading zero.
- * Otherwise, 0 to indicate success.
- */
-static int GetASNInt(const byte* input, word32* inOutIdx, int* len,
- word32 maxIdx)
-{
- int ret;
-
- ret = GetASNHeader(input, ASN_INTEGER, inOutIdx, len, maxIdx);
- if (ret < 0)
- return ret;
-
- if (*len > 0) {
- /* remove leading zero, unless there is only one 0x00 byte */
- if ((input[*inOutIdx] == 0x00) && (*len > 1)) {
- (*inOutIdx)++;
- (*len)--;
-
- if (*len > 0 && (input[*inOutIdx] & 0x80) == 0)
- return ASN_PARSE_E;
- }
- }
-
- return 0;
-}
-
-/* Get the DER/BER encoding of an ASN.1 INTEGER that has a value of no more than
- * 7 bits.
- *
- * input Buffer holding DER/BER encoded data.
- * inOutIdx Current index into buffer to parse.
- * maxIdx Length of data in buffer.
- * returns BUFFER_E when there is not enough data to parse.
- * ASN_PARSE_E when the INTEGER tag is not found or length is invalid.
- * Otherwise, the 7-bit value.
- */
-static int GetInteger7Bit(const byte* input, word32* inOutIdx, word32 maxIdx)
-{
- word32 idx = *inOutIdx;
- byte b;
-
- if ((idx + 3) > maxIdx)
- return BUFFER_E;
-
- if (GetASNTag(input, &idx, &b, maxIdx) != 0)
- return ASN_PARSE_E;
- if (b != ASN_INTEGER)
- return ASN_PARSE_E;
- if (input[idx++] != 1)
- return ASN_PARSE_E;
- b = input[idx++];
-
- *inOutIdx = idx;
- return b;
-}
-
-
-#if !defined(NO_DSA) && !defined(NO_SHA)
-static const char sigSha1wDsaName[] = "SHAwDSA";
-#endif /* NO_DSA */
-#ifndef NO_RSA
-#ifdef WOLFSSL_MD2
- static const char sigMd2wRsaName[] = "md2WithRSAEncryption";
-#endif
-#ifndef NO_MD5
- static const char sigMd5wRsaName[] = "md5WithRSAEncryption";
-#endif
-#ifndef NO_SHA
- static const char sigSha1wRsaName[] = "sha1WithRSAEncryption";
-#endif
-#ifdef WOLFSSL_SHA224
- static const char sigSha224wRsaName[] = "sha224WithRSAEncryption";
-#endif
-#ifndef NO_SHA256
- static const char sigSha256wRsaName[] = "sha256WithRSAEncryption";
-#endif
-#ifdef WOLFSSL_SHA384
- static const char sigSha384wRsaName[] = "sha384WithRSAEncryption";
-#endif
-#ifdef WOLFSSL_SHA512
- static const char sigSha512wRsaName[] = "sha512WithRSAEncryption";
-#endif
-#endif /* NO_RSA */
-#ifdef HAVE_ECC
-#ifndef NO_SHA
- static const char sigSha1wEcdsaName[] = "SHAwECDSA";
-#endif
-#ifdef WOLFSSL_SHA224
- static const char sigSha224wEcdsaName[] = "SHA224wECDSA";
-#endif
-#ifndef NO_SHA256
- static const char sigSha256wEcdsaName[] = "SHA256wECDSA";
-#endif
-#ifdef WOLFSSL_SHA384
- static const char sigSha384wEcdsaName[] = "SHA384wECDSA";
-#endif
-#ifdef WOLFSSL_SHA512
- static const char sigSha512wEcdsaName[] = "SHA512wECDSA";
-#endif
-#endif /* HAVE_ECC */
-static const char sigUnknownName[] = "Unknown";
-
-
-/* Get the human readable string for a signature type
- *
- * oid Oid value for signature
- */
-const char* GetSigName(int oid) {
- switch (oid) {
- #if !defined(NO_DSA) && !defined(NO_SHA)
- case CTC_SHAwDSA:
- return sigSha1wDsaName;
- #endif /* NO_DSA && NO_SHA */
- #ifndef NO_RSA
- #ifdef WOLFSSL_MD2
- case CTC_MD2wRSA:
- return sigMd2wRsaName;
- #endif
- #ifndef NO_MD5
- case CTC_MD5wRSA:
- return sigMd5wRsaName;
- #endif
- #ifndef NO_SHA
- case CTC_SHAwRSA:
- return sigSha1wRsaName;
- #endif
- #ifdef WOLFSSL_SHA224
- case CTC_SHA224wRSA:
- return sigSha224wRsaName;
- #endif
- #ifndef NO_SHA256
- case CTC_SHA256wRSA:
- return sigSha256wRsaName;
- #endif
- #ifdef WOLFSSL_SHA384
- case CTC_SHA384wRSA:
- return sigSha384wRsaName;
- #endif
- #ifdef WOLFSSL_SHA512
- case CTC_SHA512wRSA:
- return sigSha512wRsaName;
- #endif
- #endif /* NO_RSA */
- #ifdef HAVE_ECC
- #ifndef NO_SHA
- case CTC_SHAwECDSA:
- return sigSha1wEcdsaName;
- #endif
- #ifdef WOLFSSL_SHA224
- case CTC_SHA224wECDSA:
- return sigSha224wEcdsaName;
- #endif
- #ifndef NO_SHA256
- case CTC_SHA256wECDSA:
- return sigSha256wEcdsaName;
- #endif
- #ifdef WOLFSSL_SHA384
- case CTC_SHA384wECDSA:
- return sigSha384wEcdsaName;
- #endif
- #ifdef WOLFSSL_SHA512
- case CTC_SHA512wECDSA:
- return sigSha512wEcdsaName;
- #endif
- #endif /* HAVE_ECC */
- default:
- return sigUnknownName;
- }
-}
-
-
-#if !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_CERTS) || \
- (!defined(NO_RSA) && \
- (defined(WOLFSSL_CERT_GEN) || \
- ((defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)) && !defined(HAVE_USER_RSA))))
-/* Set the DER/BER encoding of the ASN.1 INTEGER header.
- *
- * len Length of data to encode.
- * firstByte First byte of data, most significant byte of integer, to encode.
- * output Buffer to write into.
- * returns the number of bytes added to the buffer.
- */
-static int SetASNInt(int len, byte firstByte, byte* output)
-{
- word32 idx = 0;
-
- if (output)
- output[idx] = ASN_INTEGER;
- idx++;
- if (firstByte & 0x80)
- len++;
- idx += SetLength(len, output ? output + idx : NULL);
- if (firstByte & 0x80) {
- if (output)
- output[idx] = 0x00;
- idx++;
- }
-
- return idx;
-}
-#endif
-
-#if !defined(NO_DSA) || defined(HAVE_ECC) || (defined(WOLFSSL_CERT_GEN) && \
- !defined(NO_RSA)) || ((defined(WOLFSSL_KEY_GEN) || \
- defined(OPENSSL_EXTRA)) && !defined(NO_RSA) && !defined(HAVE_USER_RSA))
-/* Set the DER/BER encoding of the ASN.1 INTEGER element with an mp_int.
- * The number is assumed to be positive.
- *
- * n Multi-precision integer to encode.
- * maxSz Maximum size of the encoded integer.
- * A negative value indicates no check of length requested.
- * output Buffer to write into.
- * returns BUFFER_E when the data is too long for the buffer.
- * MP_TO_E when encoding the integer fails.
- * Otherwise, the number of bytes added to the buffer.
- */
-static int SetASNIntMP(mp_int* n, int maxSz, byte* output)
-{
- int idx = 0;
- int leadingBit;
- int length;
- int err;
-
- leadingBit = mp_leading_bit(n);
- length = mp_unsigned_bin_size(n);
- idx = SetASNInt(length, leadingBit ? 0x80 : 0x00, output);
- if (maxSz >= 0 && (idx + length) > maxSz)
- return BUFFER_E;
-
- if (output) {
- err = mp_to_unsigned_bin(n, output + idx);
- if (err != MP_OKAY)
- return MP_TO_E;
- }
- idx += length;
-
- return idx;
-}
-#endif
-
-#if !defined(NO_RSA) && defined(HAVE_USER_RSA) && \
- (defined(WOLFSSL_CERT_GEN) || defined(OPENSSL_EXTRA))
-/* Set the DER/BER encoding of the ASN.1 INTEGER element with an mp_int from
- * an RSA key.
- * The number is assumed to be positive.
- *
- * n Multi-precision integer to encode.
- * output Buffer to write into.
- * returns BUFFER_E when the data is too long for the buffer.
- * MP_TO_E when encoding the integer fails.
- * Otherwise, the number of bytes added to the buffer.
- */
-static int SetASNIntRSA(void* n, byte* output)
-{
- int idx = 0;
- int leadingBit;
- int length;
- int err;
-
- leadingBit = wc_Rsa_leading_bit(n);
- length = wc_Rsa_unsigned_bin_size(n);
- idx = SetASNInt(length, leadingBit ? 0x80 : 0x00, output);
- if ((idx + length) > MAX_RSA_INT_SZ)
- return BUFFER_E;
-
- if (output) {
- err = wc_Rsa_to_unsigned_bin(n, output + idx, length);
- if (err != MP_OKAY)
- return MP_TO_E;
- }
- idx += length;
-
- return idx;
-}
-#endif /* !NO_RSA && HAVE_USER_RSA && WOLFSSL_CERT_GEN */
-
-/* Windows header clash for WinCE using GetVersion */
-int GetMyVersion(const byte* input, word32* inOutIdx,
- int* version, word32 maxIdx)
-{
- word32 idx = *inOutIdx;
- byte tag;
-
- if ((idx + MIN_VERSION_SZ) > maxIdx)
- return ASN_PARSE_E;
-
- if (GetASNTag(input, &idx, &tag, maxIdx) != 0)
- return ASN_PARSE_E;
-
- if (tag != ASN_INTEGER)
- return ASN_PARSE_E;
-
- if (input[idx++] != 0x01)
- return ASN_VERSION_E;
-
- *version = input[idx++];
- *inOutIdx = idx;
-
- return *version;
-}
-
-
-#ifndef NO_PWDBASED
-/* Get small count integer, 32 bits or less */
-int GetShortInt(const byte* input, word32* inOutIdx, int* number, word32 maxIdx)
-{
- word32 idx = *inOutIdx;
- word32 len;
- byte tag;
-
- *number = 0;
-
- /* check for type and length bytes */
- if ((idx + 2) > maxIdx)
- return BUFFER_E;
-
- if (GetASNTag(input, &idx, &tag, maxIdx) != 0)
- return ASN_PARSE_E;
-
- if (tag != ASN_INTEGER)
- return ASN_PARSE_E;
-
- len = input[idx++];
- if (len > 4)
- return ASN_PARSE_E;
-
- if (len + idx > maxIdx)
- return ASN_PARSE_E;
-
- while (len--) {
- *number = *number << 8 | input[idx++];
- }
-
- *inOutIdx = idx;
-
- return *number;
-}
-
-
-/* Set small integer, 32 bits or less. DER encoding with no leading 0s
- * returns total amount written including ASN tag and length byte on success */
-int SetShortInt(byte* input, word32* inOutIdx, word32 number, word32 maxIdx)
-{
- word32 idx = *inOutIdx;
- word32 len = 0;
- int i;
- byte ar[MAX_LENGTH_SZ];
-
- /* check for room for type and length bytes */
- if ((idx + 2) > maxIdx)
- return BUFFER_E;
-
- input[idx++] = ASN_INTEGER;
- idx++; /* place holder for length byte */
- if (MAX_LENGTH_SZ + idx > maxIdx)
- return ASN_PARSE_E;
-
- /* find first non zero byte */
- XMEMSET(ar, 0, MAX_LENGTH_SZ);
- c32toa(number, ar);
- for (i = 0; i < MAX_LENGTH_SZ; i++) {
- if (ar[i] != 0) {
- break;
- }
- }
-
- /* handle case of 0 */
- if (i == MAX_LENGTH_SZ) {
- input[idx++] = 0; len++;
- }
-
- for (; i < MAX_LENGTH_SZ && idx < maxIdx; i++) {
- input[idx++] = ar[i]; len++;
- }
-
- /* jump back to beginning of input buffer using unaltered inOutIdx value
- * and set number of bytes for integer, then update the index value */
- input[*inOutIdx + 1] = (byte)len;
- *inOutIdx = idx;
-
- return len + 2; /* size of integer bytes plus ASN TAG and length byte */
-}
-#endif /* !NO_PWDBASED */
-
-/* May not have one, not an error */
-static int GetExplicitVersion(const byte* input, word32* inOutIdx, int* version,
- word32 maxIdx)
-{
- word32 idx = *inOutIdx;
- byte tag;
-
- WOLFSSL_ENTER("GetExplicitVersion");
-
- if (GetASNTag(input, &idx, &tag, maxIdx) != 0)
- return ASN_PARSE_E;
-
- if (tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) {
- *inOutIdx = ++idx; /* skip header */
- return GetMyVersion(input, inOutIdx, version, maxIdx);
- }
-
- /* go back as is */
- *version = 0;
-
- return 0;
-}
-
-int GetInt(mp_int* mpi, const byte* input, word32* inOutIdx, word32 maxIdx)
-{
- word32 idx = *inOutIdx;
- int ret;
- int length;
-
- ret = GetASNInt(input, &idx, &length, maxIdx);
- if (ret != 0)
- return ret;
-
- if (mp_init(mpi) != MP_OKAY)
- return MP_INIT_E;
-
- if (mp_read_unsigned_bin(mpi, (byte*)input + idx, length) != 0) {
- mp_clear(mpi);
- return ASN_GETINT_E;
- }
-
-#ifdef HAVE_WOLF_BIGINT
- if (wc_bigint_from_unsigned_bin(&mpi->raw, input + idx, length) != 0) {
- mp_clear(mpi);
- return ASN_GETINT_E;
- }
-#endif /* HAVE_WOLF_BIGINT */
-
- *inOutIdx = idx + length;
-
- return 0;
-}
-
-#if (!defined(WOLFSSL_KEY_GEN) && !defined(OPENSSL_EXTRA) && defined(RSA_LOW_MEM)) \
- || defined(WOLFSSL_RSA_PUBLIC_ONLY) || (!defined(NO_DSA) && defined(WOLFSSL_QT))
-#if !defined(NO_RSA) && !defined(HAVE_USER_RSA)
-static int SkipInt(const byte* input, word32* inOutIdx, word32 maxIdx)
-{
- word32 idx = *inOutIdx;
- int ret;
- int length;
-
- ret = GetASNInt(input, &idx, &length, maxIdx);
- if (ret != 0)
- return ret;
-
- *inOutIdx = idx + length;
-
- return 0;
-}
-#endif
-#endif
-
-static int CheckBitString(const byte* input, word32* inOutIdx, int* len,
- word32 maxIdx, int zeroBits, byte* unusedBits)
-{
- word32 idx = *inOutIdx;
- int length;
- byte b;
-
- if (GetASNTag(input, &idx, &b, maxIdx) != 0) {
- return ASN_BITSTR_E;
- }
-
- if (b != ASN_BIT_STRING) {
- return ASN_BITSTR_E;
- }
-
- if (GetLength(input, &idx, &length, maxIdx) < 0)
- return ASN_PARSE_E;
-
- /* extra sanity check that length is greater than 0 */
- if (length <= 0) {
- WOLFSSL_MSG("Error length was 0 in CheckBitString");
- return BUFFER_E;
- }
-
- if (idx + 1 > maxIdx) {
- WOLFSSL_MSG("Attempted buffer read larger than input buffer");
- return BUFFER_E;
- }
-
- b = input[idx];
- if (zeroBits && b != 0x00)
- return ASN_EXPECT_0_E;
- if (b >= 0x08)
- return ASN_PARSE_E;
- if (b != 0) {
- if ((byte)(input[idx + length - 1] << (8 - b)) != 0)
- return ASN_PARSE_E;
- }
- idx++;
- length--; /* length has been checked for greater than 0 */
-
- *inOutIdx = idx;
- if (len != NULL)
- *len = length;
- if (unusedBits != NULL)
- *unusedBits = b;
-
- return 0;
-}
-
-/* RSA (with CertGen or KeyGen) OR ECC OR ED25519 OR ED448 (with CertGen or
- * KeyGen) */
-#if (!defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
- (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA))) || \
- (defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)) || \
- ((defined(HAVE_ED25519) || defined(HAVE_ED448)) && \
- (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)))
-
-/* Set the DER/BER encoding of the ASN.1 BIT_STRING header.
- *
- * len Length of data to encode.
- * unusedBits The number of unused bits in the last byte of data.
- * That is, the number of least significant zero bits before a one.
- * The last byte is the most-significant non-zero byte of a number.
- * output Buffer to write into.
- * returns the number of bytes added to the buffer.
- */
-word32 SetBitString(word32 len, byte unusedBits, byte* output)
-{
- word32 idx = 0;
-
- if (output)
- output[idx] = ASN_BIT_STRING;
- idx++;
-
- idx += SetLength(len + 1, output ? output + idx : NULL);
- if (output)
- output[idx] = unusedBits;
- idx++;
-
- return idx;
-}
-#endif /* !NO_RSA || HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
-
-#ifdef ASN_BER_TO_DER
-/* Pull informtation from the ASN.1 BER encoded item header */
-static int GetBerHeader(const byte* data, word32* idx, word32 maxIdx,
- byte* pTag, word32* pLen, int* indef)
-{
- int len = 0;
- byte tag;
- word32 i = *idx;
-
- *indef = 0;
-
- /* Check there is enough data for a minimal header */
- if (i + 2 > maxIdx) {
- return ASN_PARSE_E;
- }
-
- /* Retrieve tag */
- tag = data[i++];
-
- /* Indefinite length handled specially */
- if (data[i] == 0x80) {
- /* Check valid tag for indefinite */
- if (((tag & 0xc0) == 0) && ((tag & ASN_CONSTRUCTED) == 0x00)) {
- return ASN_PARSE_E;
- }
- i++;
- *indef = 1;
- }
- else if (GetLength(data, &i, &len, maxIdx) < 0) {
- return ASN_PARSE_E;
- }
-
- /* Return tag, length and index after BER item header */
- *pTag = tag;
- *pLen = len;
- *idx = i;
- return 0;
-}
-
-#ifndef INDEF_ITEMS_MAX
-#define INDEF_ITEMS_MAX 20
-#endif
-
-/* Indef length item data */
-typedef struct Indef {
- word32 start;
- int depth;
- int headerLen;
- word32 len;
-} Indef;
-
-/* Indef length items */
-typedef struct IndefItems
-{
- Indef len[INDEF_ITEMS_MAX];
- int cnt;
- int idx;
- int depth;
-} IndefItems;
-
-
-/* Get header length of current item */
-static int IndefItems_HeaderLen(IndefItems* items)
-{
- return items->len[items->idx].headerLen;
-}
-
-/* Get data length of current item */
-static word32 IndefItems_Len(IndefItems* items)
-{
- return items->len[items->idx].len;
-}
-
-/* Add a indefinite length item */
-static int IndefItems_AddItem(IndefItems* items, word32 start)
-{
- int ret = 0;
- int i;
-
- if (items->cnt == INDEF_ITEMS_MAX) {
- ret = MEMORY_E;
- }
- else {
- i = items->cnt++;
- items->len[i].start = start;
- items->len[i].depth = items->depth++;
- items->len[i].headerLen = 1;
- items->len[i].len = 0;
- items->idx = i;
- }
-
- return ret;
-}
-
-/* Increase data length of current item */
-static void IndefItems_AddData(IndefItems* items, word32 length)
-{
- items->len[items->idx].len += length;
-}
-
-/* Update header length of current item to reflect data length */
-static void IndefItems_UpdateHeaderLen(IndefItems* items)
-{
- items->len[items->idx].headerLen +=
- SetLength(items->len[items->idx].len, NULL);
-}
-
-/* Go to indefinite parent of current item */
-static void IndefItems_Up(IndefItems* items)
-{
- int i;
- int depth = items->len[items->idx].depth - 1;
-
- for (i = items->cnt - 1; i >= 0; i--) {
- if (items->len[i].depth == depth) {
- break;
- }
- }
- items->idx = i;
- items->depth = depth + 1;
-}
-
-/* Calculate final length by adding length of indefinite child items */
-static void IndefItems_CalcLength(IndefItems* items)
-{
- int i;
- int idx = items->idx;
-
- for (i = idx + 1; i < items->cnt; i++) {
- if (items->len[i].depth == items->depth) {
- items->len[idx].len += items->len[i].headerLen;
- items->len[idx].len += items->len[i].len;
- }
- }
- items->len[idx].headerLen += SetLength(items->len[idx].len, NULL);
-}
-
-/* Add more data to indefinite length item */
-static void IndefItems_MoreData(IndefItems* items, word32 length)
-{
- if (items->cnt > 0 && items->idx >= 0) {
- items->len[items->idx].len += length;
- }
-}
-
-/* Convert a BER encoding with indefinite length items to DER.
- *
- * ber BER encoded data.
- * berSz Length of BER encoded data.
- * der Buffer to hold DER encoded version of data.
- * NULL indicates only the length is required.
- * derSz The size of the buffer to hold the DER encoded data.
- * Will be set if der is NULL, otherwise the value is checked as der is
- * filled.
- * returns ASN_PARSE_E if the BER data is invalid and BAD_FUNC_ARG if ber or
- * derSz are NULL.
- */
-int wc_BerToDer(const byte* ber, word32 berSz, byte* der, word32* derSz)
-{
- int ret = 0;
- word32 i, j;
-#ifdef WOLFSSL_SMALL_STACK
- IndefItems* indefItems = NULL;
-#else
- IndefItems indefItems[1];
-#endif
- byte tag, basic;
- word32 length;
- int indef;
-
- if (ber == NULL || derSz == NULL)
- return BAD_FUNC_ARG;
-
-#ifdef WOLFSSL_SMALL_STACK
- indefItems = XMALLOC(sizeof(IndefItems), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (indefItems == NULL) {
- ret = MEMORY_E;
- goto end;
- }
-#endif
-
- XMEMSET(indefItems, 0, sizeof(*indefItems));
-
- /* Calculate indefinite item lengths */
- for (i = 0; i < berSz; ) {
- word32 start = i;
-
- /* Get next BER item */
- ret = GetBerHeader(ber, &i, berSz, &tag, &length, &indef);
- if (ret != 0) {
- goto end;
- }
-
- if (indef) {
- /* Indefinite item - add to list */
- ret = IndefItems_AddItem(indefItems, i);
- if (ret != 0) {
- goto end;
- }
-
- if ((tag & 0xC0) == 0 &&
- tag != (ASN_SEQUENCE | ASN_CONSTRUCTED) &&
- tag != (ASN_SET | ASN_CONSTRUCTED)) {
- /* Constructed basic type - get repeating tag */
- basic = tag & (~ASN_CONSTRUCTED);
-
- /* Add up lengths of each item below */
- for (; i < berSz; ) {
- /* Get next BER_item */
- ret = GetBerHeader(ber, &i, berSz, &tag, &length, &indef);
- if (ret != 0) {
- goto end;
- }
-
- /* End of content closes item */
- if (tag == ASN_EOC) {
- /* Must be zero length */
- if (length != 0) {
- ret = ASN_PARSE_E;
- goto end;
- }
- break;
- }
-
- /* Must not be indefinite and tag must match parent */
- if (indef || tag != basic) {
- ret = ASN_PARSE_E;
- goto end;
- }
-
- /* Add to length */
- IndefItems_AddData(indefItems, length);
- /* Skip data */
- i += length;
- }
-
- /* Ensure we got an EOC and not end of data */
- if (tag != ASN_EOC) {
- ret = ASN_PARSE_E;
- goto end;
- }
-
- /* Set the header length to include the length field */
- IndefItems_UpdateHeaderLen(indefItems);
- /* Go to indefinte parent item */
- IndefItems_Up(indefItems);
- }
- }
- else if (tag == ASN_EOC) {
- /* End-of-content must be 0 length */
- if (length != 0) {
- ret = ASN_PARSE_E;
- goto end;
- }
- /* Check there is an item to close - missing EOC */
- if (indefItems->depth == 0) {
- ret = ASN_PARSE_E;
- goto end;
- }
-
- /* Finish calculation of data length for indefinite item */
- IndefItems_CalcLength(indefItems);
- /* Go to indefinte parent item */
- IndefItems_Up(indefItems);
- }
- else {
- /* Known length item to add in - make sure enough data for it */
- if (i + length > berSz) {
- ret = ASN_PARSE_E;
- goto end;
- }
-
- /* Include all data - can't have indefinite inside definite */
- i += length;
- /* Add entire item to current indefinite item */
- IndefItems_MoreData(indefItems, i - start);
- }
- }
- /* Check we had a EOC for each indefinite item */
- if (indefItems->depth != 0) {
- ret = ASN_PARSE_E;
- goto end;
- }
-
- /* Write out DER */
-
- j = 0;
- /* Reset index */
- indefItems->idx = 0;
- for (i = 0; i < berSz; ) {
- word32 start = i;
-
- /* Get item - checked above */
- (void)GetBerHeader(ber, &i, berSz, &tag, &length, &indef);
- if (indef) {
- if (der != NULL) {
- /* Check enough space for header */
- if (j + IndefItems_HeaderLen(indefItems) > *derSz) {
- ret = BUFFER_E;
- goto end;
- }
-
- if ((tag & 0xC0) == 0 &&
- tag != (ASN_SEQUENCE | ASN_CONSTRUCTED) &&
- tag != (ASN_SET | ASN_CONSTRUCTED)) {
- /* Remove constructed tag for basic types */
- tag &= ~ASN_CONSTRUCTED;
- }
- /* Add tag and length */
- der[j] = tag;
- (void)SetLength(IndefItems_Len(indefItems), der + j + 1);
- }
- /* Add header length of indefinite item */
- j += IndefItems_HeaderLen(indefItems);
-
- if ((tag & 0xC0) == 0 &&
- tag != (ASN_SEQUENCE | ASN_CONSTRUCTED) &&
- tag != (ASN_SET | ASN_CONSTRUCTED)) {
- /* For basic type - get each child item and add data */
- for (; i < berSz; ) {
- (void)GetBerHeader(ber, &i, berSz, &tag, &length, &indef);
- if (tag == ASN_EOC) {
- break;
- }
- if (der != NULL) {
- if (j + length > *derSz) {
- ret = BUFFER_E;
- goto end;
- }
- XMEMCPY(der + j, ber + i, length);
- }
- j += length;
- i += length;
- }
- }
-
- /* Move to next indef item in list */
- indefItems->idx++;
- }
- else if (tag == ASN_EOC) {
- /* End-Of-Content is not written out in DER */
- }
- else {
- /* Write out definite length item as is. */
- i += length;
- if (der != NULL) {
- /* Ensure space for item */
- if (j + i - start > *derSz) {
- ret = BUFFER_E;
- goto end;
- }
- /* Copy item as is */
- XMEMCPY(der + j, ber + start, i - start);
- }
- j += i - start;
- }
- }
-
- /* Return the length of the DER encoded ASN.1 */
- *derSz = j;
- if (der == NULL) {
- ret = LENGTH_ONLY_E;
- }
-end:
-#ifdef WOLFSSL_SMALL_STACK
- if (indefItems != NULL) {
- XFREE(indefItems, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
-#endif
- return ret;
-}
-#endif
-
-#if defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN)
-
-#if (!defined(NO_RSA) && !defined(HAVE_USER_RSA)) || \
- defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
-
-#ifdef WOLFSSL_CERT_EXT
-/* Set the DER/BER encoding of the ASN.1 BIT_STRING with a 16-bit value.
- *
- * val 16-bit value to encode.
- * output Buffer to write into.
- * returns the number of bytes added to the buffer.
- */
-static word32 SetBitString16Bit(word16 val, byte* output)
-{
- word32 idx;
- int len;
- byte lastByte;
- byte unusedBits = 0;
-
- if ((val >> 8) != 0) {
- len = 2;
- lastByte = (byte)(val >> 8);
- }
- else {
- len = 1;
- lastByte = (byte)val;
- }
-
- while (((lastByte >> unusedBits) & 0x01) == 0x00)
- unusedBits++;
-
- idx = SetBitString(len, unusedBits, output);
- output[idx++] = (byte)val;
- if (len > 1)
- output[idx++] = (byte)(val >> 8);
-
- return idx;
-}
-#endif /* WOLFSSL_CERT_EXT */
-#endif /* !NO_RSA || HAVE_ECC || HAVE_ED25519 || defined(HAVE_ED448) */
-#endif /* WOLFSSL_CERT_GEN || WOLFSSL_KEY_GEN */
-
-
-
-/* hashType */
-#ifdef WOLFSSL_MD2
- static const byte hashMd2hOid[] = {42, 134, 72, 134, 247, 13, 2, 2};
-#endif
-#ifndef NO_MD5
- static const byte hashMd5hOid[] = {42, 134, 72, 134, 247, 13, 2, 5};
-#endif
-#ifndef NO_SHA
- static const byte hashSha1hOid[] = {43, 14, 3, 2, 26};
-#endif
-#ifdef WOLFSSL_SHA224
- static const byte hashSha224hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 4};
-#endif
-#ifndef NO_SHA256
- static const byte hashSha256hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 1};
-#endif
-#ifdef WOLFSSL_SHA384
- static const byte hashSha384hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 2};
-#endif
-#ifdef WOLFSSL_SHA512
- static const byte hashSha512hOid[] = {96, 134, 72, 1, 101, 3, 4, 2, 3};
-#endif
-
-/* hmacType */
-#ifndef NO_HMAC
- #ifdef WOLFSSL_SHA224
- static const byte hmacSha224Oid[] = {42, 134, 72, 134, 247, 13, 2, 8};
- #endif
- #ifndef NO_SHA256
- static const byte hmacSha256Oid[] = {42, 134, 72, 134, 247, 13, 2, 9};
- #endif
- #ifdef WOLFSSL_SHA384
- static const byte hmacSha384Oid[] = {42, 134, 72, 134, 247, 13, 2, 10};
- #endif
- #ifdef WOLFSSL_SHA512
- static const byte hmacSha512Oid[] = {42, 134, 72, 134, 247, 13, 2, 11};
- #endif
-#endif
-
-/* sigType */
-#if !defined(NO_DSA) && !defined(NO_SHA)
- static const byte sigSha1wDsaOid[] = {42, 134, 72, 206, 56, 4, 3};
-#endif /* NO_DSA */
-#ifndef NO_RSA
- #ifdef WOLFSSL_MD2
- static const byte sigMd2wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 2};
- #endif
- #ifndef NO_MD5
- static const byte sigMd5wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 4};
- #endif
- #ifndef NO_SHA
- static const byte sigSha1wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 5};
- #endif
- #ifdef WOLFSSL_SHA224
- static const byte sigSha224wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,14};
- #endif
- #ifndef NO_SHA256
- static const byte sigSha256wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,11};
- #endif
- #ifdef WOLFSSL_SHA384
- static const byte sigSha384wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,12};
- #endif
- #ifdef WOLFSSL_SHA512
- static const byte sigSha512wRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1,13};
- #endif
-#endif /* NO_RSA */
-#ifdef HAVE_ECC
- #ifndef NO_SHA
- static const byte sigSha1wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 1};
- #endif
- #ifdef WOLFSSL_SHA224
- static const byte sigSha224wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 1};
- #endif
- #ifndef NO_SHA256
- static const byte sigSha256wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 2};
- #endif
- #ifdef WOLFSSL_SHA384
- static const byte sigSha384wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 3};
- #endif
- #ifdef WOLFSSL_SHA512
- static const byte sigSha512wEcdsaOid[] = {42, 134, 72, 206, 61, 4, 3, 4};
- #endif
-#endif /* HAVE_ECC */
-#ifdef HAVE_ED25519
- static const byte sigEd25519Oid[] = {43, 101, 112};
-#endif /* HAVE_ED25519 */
-#ifdef HAVE_ED448
- static const byte sigEd448Oid[] = {43, 101, 113};
-#endif /* HAVE_ED448 */
-
-/* keyType */
-#ifndef NO_DSA
- static const byte keyDsaOid[] = {42, 134, 72, 206, 56, 4, 1};
-#endif /* NO_DSA */
-#ifndef NO_RSA
- static const byte keyRsaOid[] = {42, 134, 72, 134, 247, 13, 1, 1, 1};
-#endif /* NO_RSA */
-#ifdef HAVE_NTRU
- static const byte keyNtruOid[] = {43, 6, 1, 4, 1, 193, 22, 1, 1, 1, 1};
-#endif /* HAVE_NTRU */
-#ifdef HAVE_ECC
- static const byte keyEcdsaOid[] = {42, 134, 72, 206, 61, 2, 1};
-#endif /* HAVE_ECC */
-#ifdef HAVE_ED25519
- static const byte keyEd25519Oid[] = {43, 101, 112};
-#endif /* HAVE_ED25519 */
-#ifdef HAVE_ED448
- static const byte keyEd448Oid[] = {43, 101, 113};
-#endif /* HAVE_ED448 */
-#if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL))
- static const byte keyDhOid[] = {42, 134, 72, 134, 247, 13, 1, 3, 1};
-#endif /* ! NO_DH ... */
-
-/* curveType */
-#ifdef HAVE_ECC
- /* See "ecc_sets" table in ecc.c */
-#endif /* HAVE_ECC */
-
-#ifdef HAVE_AES_CBC
-/* blkType */
- #ifdef WOLFSSL_AES_128
- static const byte blkAes128CbcOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 2};
- #endif
- #ifdef WOLFSSL_AES_192
- static const byte blkAes192CbcOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 22};
- #endif
- #ifdef WOLFSSL_AES_256
- static const byte blkAes256CbcOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 42};
- #endif
-#endif /* HAVE_AES_CBC */
-#ifdef HAVE_AESGCM
- #ifdef WOLFSSL_AES_128
- static const byte blkAes128GcmOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 6};
- #endif
- #ifdef WOLFSSL_AES_192
- static const byte blkAes192GcmOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 26};
- #endif
- #ifdef WOLFSSL_AES_256
- static const byte blkAes256GcmOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 46};
- #endif
-#endif /* HAVE_AESGCM */
-#ifdef HAVE_AESCCM
- #ifdef WOLFSSL_AES_128
- static const byte blkAes128CcmOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 7};
- #endif
- #ifdef WOLFSSL_AES_192
- static const byte blkAes192CcmOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 27};
- #endif
- #ifdef WOLFSSL_AES_256
- static const byte blkAes256CcmOid[] = {96, 134, 72, 1, 101, 3, 4, 1, 47};
- #endif
-#endif /* HAVE_AESCCM */
-
-#ifndef NO_DES3
- static const byte blkDesCbcOid[] = {43, 14, 3, 2, 7};
- static const byte blkDes3CbcOid[] = {42, 134, 72, 134, 247, 13, 3, 7};
-#endif
-
-/* keyWrapType */
-#ifdef WOLFSSL_AES_128
- static const byte wrapAes128Oid[] = {96, 134, 72, 1, 101, 3, 4, 1, 5};
-#endif
-#ifdef WOLFSSL_AES_192
- static const byte wrapAes192Oid[] = {96, 134, 72, 1, 101, 3, 4, 1, 25};
-#endif
-#ifdef WOLFSSL_AES_256
- static const byte wrapAes256Oid[] = {96, 134, 72, 1, 101, 3, 4, 1, 45};
-#endif
-#ifdef HAVE_PKCS7
-/* From RFC 3211 */
-static const byte wrapPwriKekOid[] = {42, 134, 72, 134, 247, 13, 1, 9, 16, 3,9};
-#endif
-
-/* cmsKeyAgreeType */
-#ifndef NO_SHA
- static const byte dhSinglePass_stdDH_sha1kdf_Oid[] =
- {43, 129, 5, 16, 134, 72, 63, 0, 2};
-#endif
-#ifdef WOLFSSL_SHA224
- static const byte dhSinglePass_stdDH_sha224kdf_Oid[] = {43, 129, 4, 1, 11, 0};
-#endif
-#ifndef NO_SHA256
- static const byte dhSinglePass_stdDH_sha256kdf_Oid[] = {43, 129, 4, 1, 11, 1};
-#endif
-#ifdef WOLFSSL_SHA384
- static const byte dhSinglePass_stdDH_sha384kdf_Oid[] = {43, 129, 4, 1, 11, 2};
-#endif
-#ifdef WOLFSSL_SHA512
- static const byte dhSinglePass_stdDH_sha512kdf_Oid[] = {43, 129, 4, 1, 11, 3};
-#endif
-
-/* ocspType */
-#ifdef HAVE_OCSP
- static const byte ocspBasicOid[] = {43, 6, 1, 5, 5, 7, 48, 1, 1};
- static const byte ocspNonceOid[] = {43, 6, 1, 5, 5, 7, 48, 1, 2};
-#endif /* HAVE_OCSP */
-
-/* certExtType */
-static const byte extBasicCaOid[] = {85, 29, 19};
-static const byte extAltNamesOid[] = {85, 29, 17};
-static const byte extCrlDistOid[] = {85, 29, 31};
-static const byte extAuthInfoOid[] = {43, 6, 1, 5, 5, 7, 1, 1};
-static const byte extAuthKeyOid[] = {85, 29, 35};
-static const byte extSubjKeyOid[] = {85, 29, 14};
-static const byte extCertPolicyOid[] = {85, 29, 32};
-static const byte extKeyUsageOid[] = {85, 29, 15};
-static const byte extInhibitAnyOid[] = {85, 29, 54};
-static const byte extExtKeyUsageOid[] = {85, 29, 37};
-#ifndef IGNORE_NAME_CONSTRAINTS
- static const byte extNameConsOid[] = {85, 29, 30};
-#endif
-
-/* certAuthInfoType */
-#ifdef HAVE_OCSP
- static const byte extAuthInfoOcspOid[] = {43, 6, 1, 5, 5, 7, 48, 1};
-#endif
-static const byte extAuthInfoCaIssuerOid[] = {43, 6, 1, 5, 5, 7, 48, 2};
-
-/* certPolicyType */
-static const byte extCertPolicyAnyOid[] = {85, 29, 32, 0};
-
-/* certKeyUseType */
-static const byte extAltNamesHwNameOid[] = {43, 6, 1, 5, 5, 7, 8, 4};
-
-/* certKeyUseType */
-static const byte extExtKeyUsageAnyOid[] = {85, 29, 37, 0};
-static const byte extExtKeyUsageServerAuthOid[] = {43, 6, 1, 5, 5, 7, 3, 1};
-static const byte extExtKeyUsageClientAuthOid[] = {43, 6, 1, 5, 5, 7, 3, 2};
-static const byte extExtKeyUsageCodeSigningOid[] = {43, 6, 1, 5, 5, 7, 3, 3};
-static const byte extExtKeyUsageEmailProtectOid[] = {43, 6, 1, 5, 5, 7, 3, 4};
-static const byte extExtKeyUsageTimestampOid[] = {43, 6, 1, 5, 5, 7, 3, 8};
-static const byte extExtKeyUsageOcspSignOid[] = {43, 6, 1, 5, 5, 7, 3, 9};
-
-/* kdfType */
-static const byte pbkdf2Oid[] = {42, 134, 72, 134, 247, 13, 1, 5, 12};
-
-/* PKCS5 */
-#if !defined(NO_DES3) && !defined(NO_SHA)
-static const byte pbeSha1Des[] = {42, 134, 72, 134, 247, 13, 1, 5, 10};
-#endif
-static const byte pbes2[] = {42, 134, 72, 134, 247, 13, 1, 5, 13};
-
-/* PKCS12 */
-#if !defined(NO_RC4) && !defined(NO_SHA)
-static const byte pbeSha1RC4128[] = {42, 134, 72, 134, 247, 13, 1, 12, 1, 1};
-#endif
-#if !defined(NO_DES3) && !defined(NO_SHA)
-static const byte pbeSha1Des3[] = {42, 134, 72, 134, 247, 13, 1, 12, 1, 3};
-#endif
-
-#ifdef HAVE_LIBZ
-/* zlib compression */
-static const byte zlibCompress[] = {42, 134, 72, 134, 247, 13, 1, 9, 16, 3, 8};
-#endif
-#ifdef WOLFSSL_APACHE_HTTPD
-/* tlsExtType */
-static const byte tlsFeatureOid[] = {43, 6, 1, 5, 5, 7, 1, 24};
-/* certNameType */
-static const byte dnsSRVOid[] = {43, 6, 1, 5, 5, 7, 8, 7};
-#endif
-
-
-/* returns a pointer to the OID string on success and NULL on fail */
-const byte* OidFromId(word32 id, word32 type, word32* oidSz)
-{
- const byte* oid = NULL;
-
- *oidSz = 0;
-
- switch (type) {
-
- case oidHashType:
- switch (id) {
- #ifdef WOLFSSL_MD2
- case MD2h:
- oid = hashMd2hOid;
- *oidSz = sizeof(hashMd2hOid);
- break;
- #endif
- #ifndef NO_MD5
- case MD5h:
- oid = hashMd5hOid;
- *oidSz = sizeof(hashMd5hOid);
- break;
- #endif
- #ifndef NO_SHA
- case SHAh:
- oid = hashSha1hOid;
- *oidSz = sizeof(hashSha1hOid);
- break;
- #endif
- #ifdef WOLFSSL_SHA224
- case SHA224h:
- oid = hashSha224hOid;
- *oidSz = sizeof(hashSha224hOid);
- break;
- #endif
- #ifndef NO_SHA256
- case SHA256h:
- oid = hashSha256hOid;
- *oidSz = sizeof(hashSha256hOid);
- break;
- #endif
- #ifdef WOLFSSL_SHA384
- case SHA384h:
- oid = hashSha384hOid;
- *oidSz = sizeof(hashSha384hOid);
- break;
- #endif
- #ifdef WOLFSSL_SHA512
- case SHA512h:
- oid = hashSha512hOid;
- *oidSz = sizeof(hashSha512hOid);
- break;
- #endif
- }
- break;
-
- case oidSigType:
- switch (id) {
- #if !defined(NO_DSA) && !defined(NO_SHA)
- case CTC_SHAwDSA:
- oid = sigSha1wDsaOid;
- *oidSz = sizeof(sigSha1wDsaOid);
- break;
- #endif /* NO_DSA */
- #ifndef NO_RSA
- #ifdef WOLFSSL_MD2
- case CTC_MD2wRSA:
- oid = sigMd2wRsaOid;
- *oidSz = sizeof(sigMd2wRsaOid);
- break;
- #endif
- #ifndef NO_MD5
- case CTC_MD5wRSA:
- oid = sigMd5wRsaOid;
- *oidSz = sizeof(sigMd5wRsaOid);
- break;
- #endif
- #ifndef NO_SHA
- case CTC_SHAwRSA:
- oid = sigSha1wRsaOid;
- *oidSz = sizeof(sigSha1wRsaOid);
- break;
- #endif
- #ifdef WOLFSSL_SHA224
- case CTC_SHA224wRSA:
- oid = sigSha224wRsaOid;
- *oidSz = sizeof(sigSha224wRsaOid);
- break;
- #endif
- #ifndef NO_SHA256
- case CTC_SHA256wRSA:
- oid = sigSha256wRsaOid;
- *oidSz = sizeof(sigSha256wRsaOid);
- break;
- #endif
- #ifdef WOLFSSL_SHA384
- case CTC_SHA384wRSA:
- oid = sigSha384wRsaOid;
- *oidSz = sizeof(sigSha384wRsaOid);
- break;
- #endif
- #ifdef WOLFSSL_SHA512
- case CTC_SHA512wRSA:
- oid = sigSha512wRsaOid;
- *oidSz = sizeof(sigSha512wRsaOid);
- break;
- #endif /* WOLFSSL_SHA512 */
- #endif /* NO_RSA */
- #ifdef HAVE_ECC
- #ifndef NO_SHA
- case CTC_SHAwECDSA:
- oid = sigSha1wEcdsaOid;
- *oidSz = sizeof(sigSha1wEcdsaOid);
- break;
- #endif
- #ifdef WOLFSSL_SHA224
- case CTC_SHA224wECDSA:
- oid = sigSha224wEcdsaOid;
- *oidSz = sizeof(sigSha224wEcdsaOid);
- break;
- #endif
- #ifndef NO_SHA256
- case CTC_SHA256wECDSA:
- oid = sigSha256wEcdsaOid;
- *oidSz = sizeof(sigSha256wEcdsaOid);
- break;
- #endif
- #ifdef WOLFSSL_SHA384
- case CTC_SHA384wECDSA:
- oid = sigSha384wEcdsaOid;
- *oidSz = sizeof(sigSha384wEcdsaOid);
- break;
- #endif
- #ifdef WOLFSSL_SHA512
- case CTC_SHA512wECDSA:
- oid = sigSha512wEcdsaOid;
- *oidSz = sizeof(sigSha512wEcdsaOid);
- break;
- #endif
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case CTC_ED25519:
- oid = sigEd25519Oid;
- *oidSz = sizeof(sigEd25519Oid);
- break;
- #endif
- #ifdef HAVE_ED448
- case CTC_ED448:
- oid = sigEd448Oid;
- *oidSz = sizeof(sigEd448Oid);
- break;
- #endif
- default:
- break;
- }
- break;
-
- case oidKeyType:
- switch (id) {
- #ifndef NO_DSA
- case DSAk:
- oid = keyDsaOid;
- *oidSz = sizeof(keyDsaOid);
- break;
- #endif /* NO_DSA */
- #ifndef NO_RSA
- case RSAk:
- oid = keyRsaOid;
- *oidSz = sizeof(keyRsaOid);
- break;
- #endif /* NO_RSA */
- #ifdef HAVE_NTRU
- case NTRUk:
- oid = keyNtruOid;
- *oidSz = sizeof(keyNtruOid);
- break;
- #endif /* HAVE_NTRU */
- #ifdef HAVE_ECC
- case ECDSAk:
- oid = keyEcdsaOid;
- *oidSz = sizeof(keyEcdsaOid);
- break;
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ED25519k:
- oid = keyEd25519Oid;
- *oidSz = sizeof(keyEd25519Oid);
- break;
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- case ED448k:
- oid = keyEd448Oid;
- *oidSz = sizeof(keyEd448Oid);
- break;
- #endif /* HAVE_ED448 */
- #if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL))
- case DHk:
- oid = keyDhOid;
- *oidSz = sizeof(keyDhOid);
- break;
- #endif /* ! NO_DH && (WOLFSSL_QT || OPENSSL_ALL */
- default:
- break;
- }
- break;
-
- #ifdef HAVE_ECC
- case oidCurveType:
- if (wc_ecc_get_oid(id, &oid, oidSz) < 0) {
- WOLFSSL_MSG("ECC OID not found");
- }
- break;
- #endif /* HAVE_ECC */
-
- case oidBlkType:
- switch (id) {
- #ifdef HAVE_AES_CBC
- #ifdef WOLFSSL_AES_128
- case AES128CBCb:
- oid = blkAes128CbcOid;
- *oidSz = sizeof(blkAes128CbcOid);
- break;
- #endif
- #ifdef WOLFSSL_AES_192
- case AES192CBCb:
- oid = blkAes192CbcOid;
- *oidSz = sizeof(blkAes192CbcOid);
- break;
- #endif
- #ifdef WOLFSSL_AES_256
- case AES256CBCb:
- oid = blkAes256CbcOid;
- *oidSz = sizeof(blkAes256CbcOid);
- break;
- #endif
- #endif /* HAVE_AES_CBC */
- #ifdef HAVE_AESGCM
- #ifdef WOLFSSL_AES_128
- case AES128GCMb:
- oid = blkAes128GcmOid;
- *oidSz = sizeof(blkAes128GcmOid);
- break;
- #endif
- #ifdef WOLFSSL_AES_192
- case AES192GCMb:
- oid = blkAes192GcmOid;
- *oidSz = sizeof(blkAes192GcmOid);
- break;
- #endif
- #ifdef WOLFSSL_AES_256
- case AES256GCMb:
- oid = blkAes256GcmOid;
- *oidSz = sizeof(blkAes256GcmOid);
- break;
- #endif
- #endif /* HAVE_AESGCM */
- #ifdef HAVE_AESCCM
- #ifdef WOLFSSL_AES_128
- case AES128CCMb:
- oid = blkAes128CcmOid;
- *oidSz = sizeof(blkAes128CcmOid);
- break;
- #endif
- #ifdef WOLFSSL_AES_192
- case AES192CCMb:
- oid = blkAes192CcmOid;
- *oidSz = sizeof(blkAes192CcmOid);
- break;
- #endif
- #ifdef WOLFSSL_AES_256
- case AES256CCMb:
- oid = blkAes256CcmOid;
- *oidSz = sizeof(blkAes256CcmOid);
- break;
- #endif
- #endif /* HAVE_AESCCM */
- #ifndef NO_DES3
- case DESb:
- oid = blkDesCbcOid;
- *oidSz = sizeof(blkDesCbcOid);
- break;
- case DES3b:
- oid = blkDes3CbcOid;
- *oidSz = sizeof(blkDes3CbcOid);
- break;
- #endif /* !NO_DES3 */
- }
- break;
-
- #ifdef HAVE_OCSP
- case oidOcspType:
- switch (id) {
- case OCSP_BASIC_OID:
- oid = ocspBasicOid;
- *oidSz = sizeof(ocspBasicOid);
- break;
- case OCSP_NONCE_OID:
- oid = ocspNonceOid;
- *oidSz = sizeof(ocspNonceOid);
- break;
- }
- break;
- #endif /* HAVE_OCSP */
-
- case oidCertExtType:
- switch (id) {
- case BASIC_CA_OID:
- oid = extBasicCaOid;
- *oidSz = sizeof(extBasicCaOid);
- break;
- case ALT_NAMES_OID:
- oid = extAltNamesOid;
- *oidSz = sizeof(extAltNamesOid);
- break;
- case CRL_DIST_OID:
- oid = extCrlDistOid;
- *oidSz = sizeof(extCrlDistOid);
- break;
- case AUTH_INFO_OID:
- oid = extAuthInfoOid;
- *oidSz = sizeof(extAuthInfoOid);
- break;
- case AUTH_KEY_OID:
- oid = extAuthKeyOid;
- *oidSz = sizeof(extAuthKeyOid);
- break;
- case SUBJ_KEY_OID:
- oid = extSubjKeyOid;
- *oidSz = sizeof(extSubjKeyOid);
- break;
- case CERT_POLICY_OID:
- oid = extCertPolicyOid;
- *oidSz = sizeof(extCertPolicyOid);
- break;
- case KEY_USAGE_OID:
- oid = extKeyUsageOid;
- *oidSz = sizeof(extKeyUsageOid);
- break;
- case INHIBIT_ANY_OID:
- oid = extInhibitAnyOid;
- *oidSz = sizeof(extInhibitAnyOid);
- break;
- case EXT_KEY_USAGE_OID:
- oid = extExtKeyUsageOid;
- *oidSz = sizeof(extExtKeyUsageOid);
- break;
- #ifndef IGNORE_NAME_CONSTRAINTS
- case NAME_CONS_OID:
- oid = extNameConsOid;
- *oidSz = sizeof(extNameConsOid);
- break;
- #endif
- }
- break;
-
- case oidCrlExtType:
- #ifdef HAVE_CRL
- switch (id) {
- case AUTH_KEY_OID:
- oid = extAuthKeyOid;
- *oidSz = sizeof(extAuthKeyOid);
- break;
- }
- #endif
- break;
-
- case oidCertAuthInfoType:
- switch (id) {
- #ifdef HAVE_OCSP
- case AIA_OCSP_OID:
- oid = extAuthInfoOcspOid;
- *oidSz = sizeof(extAuthInfoOcspOid);
- break;
- #endif
- case AIA_CA_ISSUER_OID:
- oid = extAuthInfoCaIssuerOid;
- *oidSz = sizeof(extAuthInfoCaIssuerOid);
- break;
- }
- break;
-
- case oidCertPolicyType:
- switch (id) {
- case CP_ANY_OID:
- oid = extCertPolicyAnyOid;
- *oidSz = sizeof(extCertPolicyAnyOid);
- break;
- }
- break;
-
- case oidCertAltNameType:
- switch (id) {
- case HW_NAME_OID:
- oid = extAltNamesHwNameOid;
- *oidSz = sizeof(extAltNamesHwNameOid);
- break;
- }
- break;
-
- case oidCertKeyUseType:
- switch (id) {
- case EKU_ANY_OID:
- oid = extExtKeyUsageAnyOid;
- *oidSz = sizeof(extExtKeyUsageAnyOid);
- break;
- case EKU_SERVER_AUTH_OID:
- oid = extExtKeyUsageServerAuthOid;
- *oidSz = sizeof(extExtKeyUsageServerAuthOid);
- break;
- case EKU_CLIENT_AUTH_OID:
- oid = extExtKeyUsageClientAuthOid;
- *oidSz = sizeof(extExtKeyUsageClientAuthOid);
- break;
- case EKU_CODESIGNING_OID:
- oid = extExtKeyUsageCodeSigningOid;
- *oidSz = sizeof(extExtKeyUsageCodeSigningOid);
- break;
- case EKU_EMAILPROTECT_OID:
- oid = extExtKeyUsageEmailProtectOid;
- *oidSz = sizeof(extExtKeyUsageEmailProtectOid);
- break;
- case EKU_TIMESTAMP_OID:
- oid = extExtKeyUsageTimestampOid;
- *oidSz = sizeof(extExtKeyUsageTimestampOid);
- break;
- case EKU_OCSP_SIGN_OID:
- oid = extExtKeyUsageOcspSignOid;
- *oidSz = sizeof(extExtKeyUsageOcspSignOid);
- break;
- }
- break;
-
- case oidKdfType:
- switch (id) {
- case PBKDF2_OID:
- oid = pbkdf2Oid;
- *oidSz = sizeof(pbkdf2Oid);
- break;
- }
- break;
-
- case oidPBEType:
- switch (id) {
- #if !defined(NO_SHA) && !defined(NO_RC4)
- case PBE_SHA1_RC4_128:
- oid = pbeSha1RC4128;
- *oidSz = sizeof(pbeSha1RC4128);
- break;
- #endif
- #if !defined(NO_SHA) && !defined(NO_DES3)
- case PBE_SHA1_DES:
- oid = pbeSha1Des;
- *oidSz = sizeof(pbeSha1Des);
- break;
-
- #endif
- #if !defined(NO_SHA) && !defined(NO_DES3)
- case PBE_SHA1_DES3:
- oid = pbeSha1Des3;
- *oidSz = sizeof(pbeSha1Des3);
- break;
- #endif
- case PBES2:
- oid = pbes2;
- *oidSz = sizeof(pbes2);
- break;
- }
- break;
-
- case oidKeyWrapType:
- switch (id) {
- #ifdef WOLFSSL_AES_128
- case AES128_WRAP:
- oid = wrapAes128Oid;
- *oidSz = sizeof(wrapAes128Oid);
- break;
- #endif
- #ifdef WOLFSSL_AES_192
- case AES192_WRAP:
- oid = wrapAes192Oid;
- *oidSz = sizeof(wrapAes192Oid);
- break;
- #endif
- #ifdef WOLFSSL_AES_256
- case AES256_WRAP:
- oid = wrapAes256Oid;
- *oidSz = sizeof(wrapAes256Oid);
- break;
- #endif
- #ifdef HAVE_PKCS7
- case PWRI_KEK_WRAP:
- oid = wrapPwriKekOid;
- *oidSz = sizeof(wrapPwriKekOid);
- break;
- #endif
- }
- break;
-
- case oidCmsKeyAgreeType:
- switch (id) {
- #ifndef NO_SHA
- case dhSinglePass_stdDH_sha1kdf_scheme:
- oid = dhSinglePass_stdDH_sha1kdf_Oid;
- *oidSz = sizeof(dhSinglePass_stdDH_sha1kdf_Oid);
- break;
- #endif
- #ifdef WOLFSSL_SHA224
- case dhSinglePass_stdDH_sha224kdf_scheme:
- oid = dhSinglePass_stdDH_sha224kdf_Oid;
- *oidSz = sizeof(dhSinglePass_stdDH_sha224kdf_Oid);
- break;
- #endif
- #ifndef NO_SHA256
- case dhSinglePass_stdDH_sha256kdf_scheme:
- oid = dhSinglePass_stdDH_sha256kdf_Oid;
- *oidSz = sizeof(dhSinglePass_stdDH_sha256kdf_Oid);
- break;
- #endif
- #ifdef WOLFSSL_SHA384
- case dhSinglePass_stdDH_sha384kdf_scheme:
- oid = dhSinglePass_stdDH_sha384kdf_Oid;
- *oidSz = sizeof(dhSinglePass_stdDH_sha384kdf_Oid);
- break;
- #endif
- #ifdef WOLFSSL_SHA512
- case dhSinglePass_stdDH_sha512kdf_scheme:
- oid = dhSinglePass_stdDH_sha512kdf_Oid;
- *oidSz = sizeof(dhSinglePass_stdDH_sha512kdf_Oid);
- break;
- #endif
- }
- break;
-
-#ifndef NO_HMAC
- case oidHmacType:
- switch (id) {
- #ifdef WOLFSSL_SHA224
- case HMAC_SHA224_OID:
- oid = hmacSha224Oid;
- *oidSz = sizeof(hmacSha224Oid);
- break;
- #endif
- #ifndef NO_SHA256
- case HMAC_SHA256_OID:
- oid = hmacSha256Oid;
- *oidSz = sizeof(hmacSha256Oid);
- break;
- #endif
- #ifdef WOLFSSL_SHA384
- case HMAC_SHA384_OID:
- oid = hmacSha384Oid;
- *oidSz = sizeof(hmacSha384Oid);
- break;
- #endif
- #ifdef WOLFSSL_SHA512
- case HMAC_SHA512_OID:
- oid = hmacSha512Oid;
- *oidSz = sizeof(hmacSha512Oid);
- break;
- #endif
- }
- break;
-#endif /* !NO_HMAC */
-
-#ifdef HAVE_LIBZ
- case oidCompressType:
- switch (id) {
- case ZLIBc:
- oid = zlibCompress;
- *oidSz = sizeof(zlibCompress);
- break;
- }
- break;
-#endif /* HAVE_LIBZ */
-#ifdef WOLFSSL_APACHE_HTTPD
- case oidCertNameType:
- switch (id) {
- case NID_id_on_dnsSRV:
- oid = dnsSRVOid;
- *oidSz = sizeof(dnsSRVOid);
- break;
- }
- break;
- case oidTlsExtType:
- switch (id) {
- case TLS_FEATURE_OID:
- oid = tlsFeatureOid;
- *oidSz = sizeof(tlsFeatureOid);
- break;
- }
- break;
-#endif /* WOLFSSL_APACHE_HTTPD */
- case oidIgnoreType:
- default:
- break;
- }
-
- return oid;
-}
-
-#ifdef HAVE_OID_ENCODING
-int EncodeObjectId(const word16* in, word32 inSz, byte* out, word32* outSz)
-{
- int i, x, len;
- word32 d, t;
-
- /* check args */
- if (in == NULL || outSz == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* compute length of encoded OID */
- d = (in[0] * 40) + in[1];
- len = 0;
- for (i = 1; i < (int)inSz; i++) {
- x = 0;
- t = d;
- while (t) {
- x++;
- t >>= 1;
- }
- len += (x / 7) + ((x % 7) ? 1 : 0) + (d == 0 ? 1 : 0);
-
- if (i < (int)inSz - 1) {
- d = in[i + 1];
- }
- }
-
- if (out) {
- /* verify length */
- if ((int)*outSz < len) {
- return BUFFER_E; /* buffer provided is not large enough */
- }
-
- /* calc first byte */
- d = (in[0] * 40) + in[1];
-
- /* encode bytes */
- x = 0;
- for (i = 1; i < (int)inSz; i++) {
- if (d) {
- int y = x, z;
- byte mask = 0;
- while (d) {
- out[x++] = (byte)((d & 0x7F) | mask);
- d >>= 7;
- mask |= 0x80; /* upper bit is set on all but the last byte */
- }
- /* now swap bytes y...x-1 */
- z = x - 1;
- while (y < z) {
- mask = out[y];
- out[y] = out[z];
- out[z] = mask;
- ++y;
- --z;
- }
- }
- else {
- out[x++] = 0x00; /* zero value */
- }
-
- /* next word */
- if (i < (int)inSz - 1) {
- d = in[i + 1];
- }
- }
- }
-
- /* return length */
- *outSz = len;
-
- return 0;
-}
-#endif /* HAVE_OID_ENCODING */
-
-#ifdef HAVE_OID_DECODING
-int DecodeObjectId(const byte* in, word32 inSz, word16* out, word32* outSz)
-{
- int x = 0, y = 0;
- word32 t = 0;
-
- /* check args */
- if (in == NULL || outSz == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* decode bytes */
- while (inSz--) {
- t = (t << 7) | (in[x] & 0x7F);
- if (!(in[x] & 0x80)) {
- if (y >= (int)*outSz) {
- return BUFFER_E;
- }
- if (y == 0) {
- out[0] = (t / 40);
- out[1] = (t % 40);
- y = 2;
- }
- else {
- out[y++] = t;
- }
- t = 0; /* reset tmp */
- }
- x++;
- }
-
- /* return length */
- *outSz = y;
-
- return 0;
-}
-#endif /* HAVE_OID_DECODING */
-
-/* Get the DER/BER encoding of an ASN.1 OBJECT_ID header.
- *
- * input Buffer holding DER/BER encoded data.
- * inOutIdx Current index into buffer to parse.
- * len The number of bytes in the ASN.1 data.
- * maxIdx Length of data in buffer.
- * returns BUFFER_E when there is not enough data to parse.
- * ASN_OBJECt_ID_E when the OBJECT_ID tag is not found.
- * ASN_PARSE_E when length is invalid.
- * Otherwise, 0 to indicate success.
- */
-int GetASNObjectId(const byte* input, word32* inOutIdx, int* len,
- word32 maxIdx)
-{
- word32 idx = *inOutIdx;
- int length;
- byte tag;
-
- if ((idx + 1) > maxIdx)
- return BUFFER_E;
-
- if (GetASNTag(input, &idx, &tag, maxIdx) != 0)
- return ASN_PARSE_E;
-
- if (tag != ASN_OBJECT_ID)
- return ASN_OBJECT_ID_E;
-
- if (GetLength(input, &idx, &length, maxIdx) < 0)
- return ASN_PARSE_E;
-
- *len = length;
- *inOutIdx = idx;
- return 0;
-}
-
-/* Set the DER/BER encoding of the ASN.1 OBJECT_ID header.
- *
- * len Length of the OBJECT_ID data.
- * output Buffer to write into.
- * returns the number of bytes added to the buffer.
- */
-int SetObjectId(int len, byte* output)
-{
- int idx = 0;
-
- output[idx++] = ASN_OBJECT_ID;
- idx += SetLength(len, output + idx);
-
- return idx;
-}
-
-int GetObjectId(const byte* input, word32* inOutIdx, word32* oid,
- word32 oidType, word32 maxIdx)
-{
- int ret = 0, length;
- word32 idx = *inOutIdx;
-#ifndef NO_VERIFY_OID
- word32 actualOidSz = 0;
- const byte* actualOid;
-#endif /* NO_VERIFY_OID */
-
- (void)oidType;
- WOLFSSL_ENTER("GetObjectId()");
- *oid = 0;
-
- ret = GetASNObjectId(input, &idx, &length, maxIdx);
- if (ret != 0)
- return ret;
-
-#ifndef NO_VERIFY_OID
- actualOid = &input[idx];
- if (length > 0)
- actualOidSz = (word32)length;
-#endif /* NO_VERIFY_OID */
-
- while (length--) {
- /* odd HC08 compiler behavior here when input[idx++] */
- *oid += (word32)input[idx];
- idx++;
- }
- /* just sum it up for now */
-
- *inOutIdx = idx;
-
-#ifndef NO_VERIFY_OID
- {
- const byte* checkOid = NULL;
- word32 checkOidSz;
- #ifdef ASN_DUMP_OID
- word32 i;
- #endif
-
- if (oidType != oidIgnoreType) {
- checkOid = OidFromId(*oid, oidType, &checkOidSz);
-
- #ifdef ASN_DUMP_OID
- /* support for dumping OID information */
- printf("OID (Type %d, Sz %d, Sum %d): ", oidType, actualOidSz, *oid);
- for (i=0; itype = RSA_PRIVATE;
-
- if (GetInt(&key->n, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->e, input, inOutIdx, inSz) < 0 ||
-#ifndef WOLFSSL_RSA_PUBLIC_ONLY
- GetInt(&key->d, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->q, input, inOutIdx, inSz) < 0)
-#else
- SkipInt(input, inOutIdx, inSz) < 0 ||
- SkipInt(input, inOutIdx, inSz) < 0 ||
- SkipInt(input, inOutIdx, inSz) < 0 )
-
-#endif
- return ASN_RSA_KEY_E;
-#if (defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)) \
- && !defined(WOLFSSL_RSA_PUBLIC_ONLY)
- if (GetInt(&key->dP, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->dQ, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->u, input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E;
-#else
- if (SkipInt(input, inOutIdx, inSz) < 0 ||
- SkipInt(input, inOutIdx, inSz) < 0 ||
- SkipInt(input, inOutIdx, inSz) < 0 ) return ASN_RSA_KEY_E;
-#endif
-
-#if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_CRYPTOCELL)
- if (wc_InitRsaHw(key) != 0) {
- return BAD_STATE_E;
- }
-#endif
-
- return 0;
-}
-#endif /* HAVE_USER_RSA */
-#endif /* NO_RSA */
-
-#if defined(HAVE_PKCS8) || defined(HAVE_PKCS12)
-
-/* Remove PKCS8 header, place inOutIdx at beginning of traditional,
- * return traditional length on success, negative on error */
-int ToTraditionalInline_ex(const byte* input, word32* inOutIdx, word32 sz,
- word32* algId)
-{
- word32 idx;
- int version, length;
- int ret;
- byte tag;
-
- if (input == NULL || inOutIdx == NULL)
- return BAD_FUNC_ARG;
-
- idx = *inOutIdx;
-
- if (GetSequence(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- if (GetMyVersion(input, &idx, &version, sz) < 0)
- return ASN_PARSE_E;
-
- if (GetAlgoId(input, &idx, algId, oidKeyType, sz) < 0)
- return ASN_PARSE_E;
-
- if (GetASNTag(input, &idx, &tag, sz) < 0)
- return ASN_PARSE_E;
- idx = idx - 1; /* reset idx after finding tag */
-
- if (tag == ASN_OBJECT_ID) {
- if (SkipObjectId(input, &idx, sz) < 0)
- return ASN_PARSE_E;
- }
-
- ret = GetOctetString(input, &idx, &length, sz);
- if (ret < 0) {
- if (ret == BUFFER_E)
- return ASN_PARSE_E;
- /* Some private keys don't expect an octet string */
- WOLFSSL_MSG("Couldn't find Octet string");
- }
-
- *inOutIdx = idx;
-
- return length;
-}
-
-int ToTraditionalInline(const byte* input, word32* inOutIdx, word32 sz)
-{
- word32 oid;
-
- return ToTraditionalInline_ex(input, inOutIdx, sz, &oid);
-}
-
-/* Remove PKCS8 header, move beginning of traditional to beginning of input */
-int ToTraditional_ex(byte* input, word32 sz, word32* algId)
-{
- word32 inOutIdx = 0;
- int length;
-
- if (input == NULL)
- return BAD_FUNC_ARG;
-
- length = ToTraditionalInline_ex(input, &inOutIdx, sz, algId);
- if (length < 0)
- return length;
-
- XMEMMOVE(input, input + inOutIdx, length);
-
- return length;
-}
-
-int ToTraditional(byte* input, word32 sz)
-{
- word32 oid;
-
- return ToTraditional_ex(input, sz, &oid);
-}
-
-#endif /* HAVE_PKCS8 || HAVE_PKCS12 */
-
-#ifdef HAVE_PKCS8
-
-/* find beginning of traditional key inside PKCS#8 unencrypted buffer
- * return traditional length on success, with inOutIdx at beginning of
- * traditional
- * return negative on failure/error */
-int wc_GetPkcs8TraditionalOffset(byte* input, word32* inOutIdx, word32 sz)
-{
- int length;
- word32 algId;
-
- if (input == NULL || inOutIdx == NULL || (*inOutIdx > sz))
- return BAD_FUNC_ARG;
-
- length = ToTraditionalInline_ex(input, inOutIdx, sz, &algId);
-
- return length;
-}
-
-
-/* PKCS#8 from RFC 5208
- * This function takes in a DER key and converts it to PKCS#8 format. Used
- * in creating PKCS#12 shrouded key bags.
- * Reverse of ToTraditional
- *
- * PrivateKeyInfo ::= SEQUENCE {
- * version Version,
- * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
- * privateKey PrivateKey,
- * attributes optional
- * }
- * Version ::= INTEGER
- * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
- * PrivateKey ::= OCTET STRING
- *
- * out buffer to place result in
- * outSz size of out buffer
- * key buffer with DER key
- * keySz size of key buffer
- * algoID algorithm ID i.e. RSAk
- * curveOID ECC curve oid if used. Should be NULL for RSA keys.
- * oidSz size of curve oid. Is set to 0 if curveOID is NULL.
- *
- * Returns the size of PKCS#8 placed into out. In error cases returns negative
- * values.
- */
-int wc_CreatePKCS8Key(byte* out, word32* outSz, byte* key, word32 keySz,
- int algoID, const byte* curveOID, word32 oidSz)
-{
- word32 keyIdx = 0;
- word32 tmpSz = 0;
- word32 sz;
-
-
- /* If out is NULL then return the max size needed
- * + 2 for ASN_OBJECT_ID and ASN_OCTET_STRING tags */
- if (out == NULL && outSz != NULL) {
- *outSz = keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ
- + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 2;
-
- if (curveOID != NULL)
- *outSz += oidSz + MAX_LENGTH_SZ + 1;
-
- WOLFSSL_MSG("Checking size of PKCS8");
-
- return LENGTH_ONLY_E;
- }
-
- WOLFSSL_ENTER("wc_CreatePKCS8Key()");
-
- if (key == NULL || out == NULL || outSz == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* check the buffer has enough room for largest possible size */
- if (curveOID != NULL) {
- if (*outSz < (keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ
- + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 3 + oidSz + MAX_LENGTH_SZ))
- return BUFFER_E;
- }
- else {
- oidSz = 0; /* with no curveOID oid size must be 0 */
- if (*outSz < (keySz + MAX_SEQ_SZ + MAX_VERSION_SZ + MAX_ALGO_SZ
- + MAX_LENGTH_SZ + MAX_LENGTH_SZ + 2))
- return BUFFER_E;
- }
-
- /* PrivateKeyInfo ::= SEQUENCE */
- keyIdx += MAX_SEQ_SZ; /* save room for sequence */
-
- /* version Version
- * no header information just INTEGER */
- sz = SetMyVersion(PKCS8v0, out + keyIdx, 0);
- tmpSz += sz; keyIdx += sz;
-
- /* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier */
- sz = 0; /* set sz to 0 and get privateKey oid buffer size needed */
- if (curveOID != NULL && oidSz > 0) {
- byte buf[MAX_LENGTH_SZ];
- sz = SetLength(oidSz, buf);
- sz += 1; /* plus one for ASN object id */
- }
- sz = SetAlgoID(algoID, out + keyIdx, oidKeyType, oidSz + sz);
- tmpSz += sz; keyIdx += sz;
-
- /* privateKey PrivateKey *
- * pkcs8 ecc uses slightly different format. Places curve oid in
- * buffer */
- if (curveOID != NULL && oidSz > 0) {
- sz = SetObjectId(oidSz, out + keyIdx);
- keyIdx += sz; tmpSz += sz;
- XMEMCPY(out + keyIdx, curveOID, oidSz);
- keyIdx += oidSz; tmpSz += oidSz;
- }
-
- sz = SetOctetString(keySz, out + keyIdx);
- keyIdx += sz; tmpSz += sz;
- XMEMCPY(out + keyIdx, key, keySz);
- tmpSz += keySz;
-
- /* attributes optional
- * No attributes currently added */
-
- /* rewind and add sequence */
- sz = SetSequence(tmpSz, out);
- XMEMMOVE(out + sz, out + MAX_SEQ_SZ, tmpSz);
-
- return tmpSz + sz;
-}
-
-#endif /* HAVE_PKCS8 */
-
-#if defined(HAVE_PKCS12) || !defined(NO_CHECK_PRIVATE_KEY)
-/* check that the private key is a pair for the public key in certificate
- * return 1 (true) on match
- * return 0 or negative value on failure/error
- *
- * key : buffer holding DER format key
- * keySz : size of key buffer
- * der : a initialized and parsed DecodedCert holding a certificate */
-int wc_CheckPrivateKey(byte* key, word32 keySz, DecodedCert* der)
-{
- int ret;
- (void)keySz;
-
- if (key == NULL || der == NULL) {
- return BAD_FUNC_ARG;
- }
-
- #if !defined(NO_RSA) && !defined(NO_ASN_CRYPT)
- /* test if RSA key */
- if (der->keyOID == RSAk) {
- #ifdef WOLFSSL_SMALL_STACK
- RsaKey* a;
- RsaKey* b = NULL;
- #else
- RsaKey a[1], b[1];
- #endif
- word32 keyIdx = 0;
-
- #ifdef WOLFSSL_SMALL_STACK
- a = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
- if (a == NULL)
- return MEMORY_E;
- b = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
- if (b == NULL) {
- XFREE(a, NULL, DYNAMIC_TYPE_RSA);
- return MEMORY_E;
- }
- #endif
-
- if ((ret = wc_InitRsaKey(a, NULL)) < 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(b, NULL, DYNAMIC_TYPE_RSA);
- XFREE(a, NULL, DYNAMIC_TYPE_RSA);
- #endif
- return ret;
- }
- if ((ret = wc_InitRsaKey(b, NULL)) < 0) {
- wc_FreeRsaKey(a);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(b, NULL, DYNAMIC_TYPE_RSA);
- XFREE(a, NULL, DYNAMIC_TYPE_RSA);
- #endif
- return ret;
- }
- if ((ret = wc_RsaPrivateKeyDecode(key, &keyIdx, a, keySz)) == 0) {
- WOLFSSL_MSG("Checking RSA key pair");
- keyIdx = 0; /* reset to 0 for parsing public key */
-
- if ((ret = wc_RsaPublicKeyDecode(der->publicKey, &keyIdx, b,
- der->pubKeySize)) == 0) {
- /* limit for user RSA crypto because of RsaKey
- * dereference. */
- #if defined(HAVE_USER_RSA)
- WOLFSSL_MSG("Cannot verify RSA pair with user RSA");
- ret = 1; /* return first RSA cert as match */
- #else
- /* both keys extracted successfully now check n and e
- * values are the same. This is dereferencing RsaKey */
- if (mp_cmp(&(a->n), &(b->n)) != MP_EQ ||
- mp_cmp(&(a->e), &(b->e)) != MP_EQ) {
- ret = MP_CMP_E;
- }
- else
- ret = 1;
- #endif
- }
- }
- wc_FreeRsaKey(b);
- wc_FreeRsaKey(a);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(b, NULL, DYNAMIC_TYPE_RSA);
- XFREE(a, NULL, DYNAMIC_TYPE_RSA);
- #endif
- }
- else
- #endif /* !NO_RSA && !NO_ASN_CRYPT */
-
- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT)
- if (der->keyOID == ECDSAk) {
- #ifdef WOLFSSL_SMALL_STACK
- ecc_key* key_pair;
- byte* privDer;
- #else
- ecc_key key_pair[1];
- byte privDer[MAX_ECC_BYTES];
- #endif
- word32 privSz = MAX_ECC_BYTES;
- word32 keyIdx = 0;
-
- #ifdef WOLFSSL_SMALL_STACK
- key_pair = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL, DYNAMIC_TYPE_ECC);
- if (key_pair == NULL)
- return MEMORY_E;
- privDer = (byte*)XMALLOC(MAX_ECC_BYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (privDer == NULL) {
- XFREE(key_pair, NULL, DYNAMIC_TYPE_ECC);
- return MEMORY_E;
- }
- #endif
-
- if ((ret = wc_ecc_init(key_pair)) < 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(privDer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(key_pair, NULL, DYNAMIC_TYPE_ECC);
- #endif
- return ret;
- }
-
- if ((ret = wc_EccPrivateKeyDecode(key, &keyIdx, key_pair,
- keySz)) == 0) {
- WOLFSSL_MSG("Checking ECC key pair");
-
- if ((ret = wc_ecc_export_private_only(key_pair, privDer, &privSz))
- == 0) {
- wc_ecc_free(key_pair);
- ret = wc_ecc_init(key_pair);
- if (ret == 0) {
- ret = wc_ecc_import_private_key((const byte*)privDer,
- privSz, (const byte*)der->publicKey,
- der->pubKeySize, key_pair);
- }
-
- /* public and private extracted successfully now check if is
- * a pair and also do sanity checks on key. wc_ecc_check_key
- * checks that private * base generator equals pubkey */
- if (ret == 0) {
- if ((ret = wc_ecc_check_key(key_pair)) == 0) {
- ret = 1;
- }
- }
- ForceZero(privDer, privSz);
- }
- }
- wc_ecc_free(key_pair);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(privDer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(key_pair, NULL, DYNAMIC_TYPE_ECC);
- #endif
- }
- else
- #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
-
- #if defined(HAVE_ED25519) && !defined(NO_ASN_CRYPT)
- if (der->keyOID == ED25519k) {
- #ifdef WOLFSSL_SMALL_STACK
- ed25519_key* key_pair;
- #else
- ed25519_key key_pair[1];
- #endif
- word32 keyIdx = 0;
-
- #ifdef WOLFSSL_SMALL_STACK
- key_pair = (ed25519_key*)XMALLOC(sizeof(ed25519_key), NULL,
- DYNAMIC_TYPE_ED25519);
- if (key_pair == NULL)
- return MEMORY_E;
- #endif
-
- if ((ret = wc_ed25519_init(key_pair)) < 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(key_pair, NULL, DYNAMIC_TYPE_ED25519);
- #endif
- return ret;
- }
- if ((ret = wc_Ed25519PrivateKeyDecode(key, &keyIdx, key_pair,
- keySz)) == 0) {
- WOLFSSL_MSG("Checking ED25519 key pair");
- keyIdx = 0;
- if ((ret = wc_ed25519_import_public(der->publicKey, der->pubKeySize,
- key_pair)) == 0) {
- /* public and private extracted successfully no check if is
- * a pair and also do sanity checks on key. wc_ecc_check_key
- * checks that private * base generator equals pubkey */
- if ((ret = wc_ed25519_check_key(key_pair)) == 0)
- ret = 1;
- }
- }
- wc_ed25519_free(key_pair);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(key_pair, NULL, DYNAMIC_TYPE_ED25519);
- #endif
- }
- else
- #endif /* HAVE_ED25519 && !NO_ASN_CRYPT */
-
- #if defined(HAVE_ED448) && !defined(NO_ASN_CRYPT)
- if (der->keyOID == ED448k) {
- #ifdef WOLFSSL_SMALL_STACK
- ed448_key* key_pair = NULL;
- #else
- ed448_key key_pair[1];
- #endif
- word32 keyIdx = 0;
-
- #ifdef WOLFSSL_SMALL_STACK
- key_pair = (ed448_key*)XMALLOC(sizeof(ed448_key), NULL,
- DYNAMIC_TYPE_ED448);
- if (key_pair == NULL)
- return MEMORY_E;
- #endif
-
- if ((ret = wc_ed448_init(key_pair)) < 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(key_pair, NULL, DYNAMIC_TYPE_ED448);
- #endif
- return ret;
- }
- if ((ret = wc_Ed448PrivateKeyDecode(key, &keyIdx, key_pair,
- keySz)) == 0) {
- WOLFSSL_MSG("Checking ED448 key pair");
- keyIdx = 0;
- if ((ret = wc_ed448_import_public(der->publicKey, der->pubKeySize,
- key_pair)) == 0) {
- /* public and private extracted successfully no check if is
- * a pair and also do sanity checks on key. wc_ecc_check_key
- * checks that private * base generator equals pubkey */
- if ((ret = wc_ed448_check_key(key_pair)) == 0)
- ret = 1;
- }
- }
- wc_ed448_free(key_pair);
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(key_pair, NULL, DYNAMIC_TYPE_ED448);
- #endif
- }
- else
- #endif /* HAVE_ED448 && !NO_ASN_CRYPT */
- {
- ret = 0;
- }
-
- (void)keySz;
-
- return ret;
-}
-
-#endif /* HAVE_PKCS12 || !NO_CHECK_PRIVATE_KEY */
-
-#ifndef NO_PWDBASED
-
-#if defined(HAVE_PKCS8) || defined(HAVE_PKCS12)
-/* Check To see if PKCS version algo is supported, set id if it is return 0
- < 0 on error */
-static int CheckAlgo(int first, int second, int* id, int* version, int* blockSz)
-{
- *id = ALGO_ID_E;
- *version = PKCS5; /* default */
- if (blockSz) *blockSz = 8; /* default */
-
- if (first == 1) {
- switch (second) {
-#if !defined(NO_SHA)
- #ifndef NO_RC4
- case PBE_SHA1_RC4_128:
- *id = PBE_SHA1_RC4_128;
- *version = PKCS12v1;
- return 0;
- #endif
- #ifndef NO_DES3
- case PBE_SHA1_DES3:
- *id = PBE_SHA1_DES3;
- *version = PKCS12v1;
- if (blockSz) *blockSz = DES_BLOCK_SIZE;
- return 0;
- case PBE_SHA1_DES:
- *id = PBE_SHA1_DES;
- *version = PKCS12v1;
- if (blockSz) *blockSz = DES_BLOCK_SIZE;
- return 0;
- #endif
-#endif /* !NO_SHA */
- default:
- return ALGO_ID_E;
- }
- }
-
- if (first != PKCS5)
- return ASN_INPUT_E; /* VERSION ERROR */
-
- if (second == PBES2) {
- *version = PKCS5v2;
- return 0;
- }
-
- switch (second) {
-#ifndef NO_DES3
- #ifndef NO_MD5
- case 3: /* see RFC 2898 for ids */
- *id = PBE_MD5_DES;
- if (blockSz) *blockSz = DES_BLOCK_SIZE;
- return 0;
- #endif
- #ifndef NO_SHA
- case 10:
- *id = PBE_SHA1_DES;
- if (blockSz) *blockSz = DES_BLOCK_SIZE;
- return 0;
- #endif
-#endif /* !NO_DES3 */
- default:
- return ALGO_ID_E;
-
- }
-}
-
-/* Check To see if PKCS v2 algo is supported, set id if it is return 0
- < 0 on error */
-static int CheckAlgoV2(int oid, int* id, int* blockSz)
-{
- if (blockSz) *blockSz = 8; /* default */
- (void)id; /* not used if AES and DES3 disabled */
- switch (oid) {
-#if !defined(NO_DES3) && !defined(NO_SHA)
- case DESb:
- *id = PBE_SHA1_DES;
- if (blockSz) *blockSz = DES_BLOCK_SIZE;
- return 0;
- case DES3b:
- *id = PBE_SHA1_DES3;
- if (blockSz) *blockSz = DES_BLOCK_SIZE;
- return 0;
-#endif
-#ifdef WOLFSSL_AES_256
- case AES256CBCb:
- *id = PBE_AES256_CBC;
- if (blockSz) *blockSz = AES_BLOCK_SIZE;
- return 0;
-#endif
-#ifdef WOLFSSL_AES_128
- case AES128CBCb:
- *id = PBE_AES128_CBC;
- if (blockSz) *blockSz = AES_BLOCK_SIZE;
- return 0;
-#endif
- default:
- WOLFSSL_MSG("No PKCS v2 algo found");
- return ALGO_ID_E;
-
- }
-}
-
-#endif /* HAVE_PKCS8 || HAVE_PKCS12 */
-
-#ifdef HAVE_PKCS8
-
-int wc_GetKeyOID(byte* key, word32 keySz, const byte** curveOID, word32* oidSz,
- int* algoID, void* heap)
-{
- word32 tmpIdx = 0;
-
- if (key == NULL || algoID == NULL)
- return BAD_FUNC_ARG;
-
- *algoID = 0;
-
- #if !defined(NO_RSA) && !defined(NO_ASN_CRYPT)
- {
- RsaKey rsa;
-
- wc_InitRsaKey(&rsa, heap);
- if (wc_RsaPrivateKeyDecode(key, &tmpIdx, &rsa, keySz) == 0) {
- *algoID = RSAk;
- }
- else {
- WOLFSSL_MSG("Not RSA DER key");
- }
- wc_FreeRsaKey(&rsa);
- }
- #endif /* !NO_RSA && !NO_ASN_CRYPT */
- #if defined(HAVE_ECC) && !defined(NO_ASN_CRYPT)
- if (*algoID == 0) {
- ecc_key ecc;
-
- tmpIdx = 0;
- wc_ecc_init_ex(&ecc, heap, INVALID_DEVID);
- if (wc_EccPrivateKeyDecode(key, &tmpIdx, &ecc, keySz) == 0) {
- *algoID = ECDSAk;
-
- /* now find oid */
- if (wc_ecc_get_oid(ecc.dp->oidSum, curveOID, oidSz) < 0) {
- WOLFSSL_MSG("Error getting ECC curve OID");
- wc_ecc_free(&ecc);
- return BAD_FUNC_ARG;
- }
- }
- else {
- WOLFSSL_MSG("Not ECC DER key either");
- }
- wc_ecc_free(&ecc);
- }
-#endif /* HAVE_ECC && !NO_ASN_CRYPT */
-#if defined(HAVE_ED25519) && !defined(NO_ASN_CRYPT)
- if (*algoID != RSAk && *algoID != ECDSAk) {
- ed25519_key ed25519;
-
- tmpIdx = 0;
- if (wc_ed25519_init(&ed25519) == 0) {
- if (wc_Ed25519PrivateKeyDecode(key, &tmpIdx, &ed25519, keySz)
- == 0) {
- *algoID = ED25519k;
- }
- else {
- WOLFSSL_MSG("Not ED25519 DER key");
- }
- wc_ed25519_free(&ed25519);
- }
- else {
- WOLFSSL_MSG("GetKeyOID wc_ed25519_init failed");
- }
- }
-#endif /* HAVE_ED25519 && !NO_ASN_CRYPT */
-#if defined(HAVE_ED448) && !defined(NO_ASN_CRYPT)
- if (*algoID != RSAk && *algoID != ECDSAk && *algoID != ED25519k) {
- ed448_key ed448;
-
- tmpIdx = 0;
- if (wc_ed448_init(&ed448) == 0) {
- if (wc_Ed448PrivateKeyDecode(key, &tmpIdx, &ed448, keySz) == 0) {
- *algoID = ED448k;
- }
- else {
- WOLFSSL_MSG("Not ED448 DER key");
- }
- wc_ed448_free(&ed448);
- }
- else {
- WOLFSSL_MSG("GetKeyOID wc_ed448_init failed");
- }
- }
-#endif /* HAVE_ED448 && !NO_ASN_CRYPT */
-
- /* if flag is not set then is neither RSA or ECC key that could be
- * found */
- if (*algoID == 0) {
- WOLFSSL_MSG("Bad key DER or compile options");
- return BAD_FUNC_ARG;
- }
-
- (void)tmpIdx;
- (void)curveOID;
- (void)oidSz;
- (void)keySz;
- (void)heap;
-
- return 1;
-}
-
-#endif /* HAVE_PKCS8 */
-
-#if defined(HAVE_PKCS8) || defined(HAVE_PKCS12)
-
-#define PKCS8_MIN_BLOCK_SIZE 8
-static int Pkcs8Pad(byte* buf, int sz, int blockSz)
-{
- int i, padSz;
-
- /* calculate pad size */
- padSz = blockSz - (sz & (blockSz - 1));
-
- /* pad with padSz value */
- if (buf) {
- for (i = 0; i < padSz; i++) {
- buf[sz+i] = (byte)(padSz & 0xFF);
- }
- }
-
- /* return adjusted length */
- return sz + padSz;
-}
-
-#endif /* HAVE_PKCS8 || HAVE_PKCS12 */
-
-#ifdef HAVE_PKCS8
-
-/*
- * Used when creating PKCS12 shrouded key bags
- * vPKCS is the version of PKCS to use
- * vAlgo is the algorithm version to use
- *
- * if salt is NULL a random number is generated
- *
- * returns the size of encrypted data on success
- */
-int UnTraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz,
- const char* password, int passwordSz, int vPKCS, int vAlgo,
- byte* salt, word32 saltSz, int itt, WC_RNG* rng, void* heap)
-{
- int algoID = 0;
- byte* tmp;
- word32 tmpSz = 0;
- word32 sz;
- word32 seqSz;
- word32 inOutIdx = 0;
- word32 totalSz = 0;
- int version, id;
- int ret;
- int blockSz = 0;
-
- const byte* curveOID = NULL;
- word32 oidSz = 0;
-
-#ifdef WOLFSSL_SMALL_STACK
- byte* saltTmp = NULL;
- byte* cbcIv = NULL;
-#else
- byte saltTmp[MAX_IV_SIZE];
- byte cbcIv[MAX_IV_SIZE];
-#endif
-
- WOLFSSL_ENTER("UnTraditionalEnc()");
-
- if (saltSz > MAX_SALT_SIZE)
- return ASN_PARSE_E;
-
-
- inOutIdx += MAX_SEQ_SZ; /* leave room for size of finished shroud */
- if (CheckAlgo(vPKCS, vAlgo, &id, &version, &blockSz) < 0) {
- WOLFSSL_MSG("Bad/Unsupported algorithm ID");
- return ASN_INPUT_E; /* Algo ID error */
- }
-
- if (out != NULL) {
- if (*outSz < inOutIdx + MAX_ALGO_SZ + MAX_SALT_SIZE + MAX_SEQ_SZ + 1 +
- MAX_LENGTH_SZ + MAX_SHORT_SZ + 1)
- return BUFFER_E;
-
- if (version == PKCS5v2) {
- WOLFSSL_MSG("PKCS5v2 Not supported yet\n");
- return ASN_VERSION_E;
- }
-
- if (salt == NULL || saltSz == 0) {
- saltSz = 8;
- #ifdef WOLFSSL_SMALL_STACK
- saltTmp = (byte*)XMALLOC(saltSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (saltTmp == NULL)
- return MEMORY_E;
- #endif
- salt = saltTmp;
-
- if ((ret = wc_RNG_GenerateBlock(rng, saltTmp, saltSz)) != 0) {
- WOLFSSL_MSG("Error generating random salt");
- #ifdef WOLFSSL_SMALL_STACK
- if (saltTmp != NULL)
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- }
-
-
- /* leave room for a sequence (contains salt and iterations int) */
- inOutIdx += MAX_SEQ_SZ; sz = 0;
- inOutIdx += MAX_ALGO_SZ;
-
- /* place salt in buffer */
- out[inOutIdx++] = ASN_OCTET_STRING; sz++;
- tmpSz = SetLength(saltSz, out + inOutIdx);
- inOutIdx += tmpSz; sz += tmpSz;
- XMEMCPY(out + inOutIdx, salt, saltSz);
- inOutIdx += saltSz; sz += saltSz;
-
- /* place iteration count in buffer */
- ret = SetShortInt(out, &inOutIdx, itt, *outSz);
- if (ret < 0) {
- return ret;
- }
- sz += (word32)ret;
-
- /* wind back index and set sequence then clean up buffer */
- inOutIdx -= (sz + MAX_SEQ_SZ);
- tmpSz = SetSequence(sz, out + inOutIdx);
- XMEMMOVE(out + inOutIdx + tmpSz, out + inOutIdx + MAX_SEQ_SZ, sz);
- totalSz += tmpSz + sz; sz += tmpSz;
-
- /* add in algo ID */
- inOutIdx -= MAX_ALGO_SZ;
- tmpSz = SetAlgoID(id, out + inOutIdx, oidPBEType, sz);
- XMEMMOVE(out + inOutIdx + tmpSz, out + inOutIdx + MAX_ALGO_SZ, sz);
- totalSz += tmpSz; inOutIdx += tmpSz + sz;
-
- /* octet string containing encrypted key */
- out[inOutIdx++] = ASN_OCTET_STRING; totalSz++;
- }
-
- /* check key type and get OID if ECC */
- if ((ret = wc_GetKeyOID(key, keySz, &curveOID, &oidSz, &algoID, heap))< 0) {
- WOLFSSL_MSG("Error getting key OID");
- return ret;
- }
-
- /* PKCS#8 wrapping around key */
- if (wc_CreatePKCS8Key(NULL, &tmpSz, key, keySz, algoID, curveOID, oidSz)
- != LENGTH_ONLY_E) {
- #ifdef WOLFSSL_SMALL_STACK
- if (saltTmp != NULL)
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return MEMORY_E;
- }
-
- /* check if should return max size */
- if (out == NULL) {
- /* account for salt size */
- if (salt == NULL || saltSz == 0) {
- tmpSz += MAX_SALT_SIZE;
- }
- else {
- tmpSz += saltSz;
- }
-
- /* plus 3 for tags */
- *outSz = tmpSz + MAX_ALGO_SZ + MAX_LENGTH_SZ +MAX_LENGTH_SZ + MAX_SEQ_SZ
- + MAX_LENGTH_SZ + MAX_SEQ_SZ + 3;
- return LENGTH_ONLY_E;
- }
-
- /* reserve buffer for crypto and make sure it supports full blocks */
- tmp = (byte*)XMALLOC(tmpSz + (blockSz-1), heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmp == NULL) {
- #ifdef WOLFSSL_SMALL_STACK
- if (saltTmp != NULL)
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return MEMORY_E;
- }
-
- if ((ret = wc_CreatePKCS8Key(tmp, &tmpSz, key, keySz, algoID, curveOID,
- oidSz)) < 0) {
- XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- WOLFSSL_MSG("Error wrapping key with PKCS#8");
- #ifdef WOLFSSL_SMALL_STACK
- if (saltTmp != NULL)
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- tmpSz = ret;
-
- /* adjust size to pad */
- tmpSz = Pkcs8Pad(tmp, tmpSz, blockSz);
-
-#ifdef WOLFSSL_SMALL_STACK
- cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (cbcIv == NULL) {
- if (saltTmp != NULL)
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(salt, heap, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#endif
-
- /* encrypt PKCS#8 wrapped key */
- if ((ret = wc_CryptKey(password, passwordSz, salt, saltSz, itt, id,
- tmp, tmpSz, version, cbcIv, 1, 0)) < 0) {
- XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- WOLFSSL_MSG("Error encrypting key");
- #ifdef WOLFSSL_SMALL_STACK
- if (saltTmp != NULL)
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (cbcIv != NULL)
- XFREE(cbcIv, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret; /* encryption failure */
- }
- totalSz += tmpSz;
-
-#ifdef WOLFSSL_SMALL_STACK
- if (saltTmp != NULL)
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (cbcIv != NULL)
- XFREE(cbcIv, heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- if (*outSz < inOutIdx + tmpSz + MAX_LENGTH_SZ) {
- XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- return BUFFER_E;
- }
-
- /* set length of key and copy over encrypted key */
- seqSz = SetLength(tmpSz, out + inOutIdx);
- inOutIdx += seqSz; totalSz += seqSz;
- XMEMCPY(out + inOutIdx, tmp, tmpSz);
- XFREE(tmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
-
- /* set total size at beginning */
- sz = SetSequence(totalSz, out);
- XMEMMOVE(out + sz, out + MAX_SEQ_SZ, totalSz);
-
- (void)rng;
-
- return totalSz + sz;
-}
-
-static int GetAlgoV2(int encAlgId, const byte** oid, int *len, int* id,
- int *blkSz)
-{
- int ret = 0;
-
- switch (encAlgId) {
-#if !defined(NO_DES3) && !defined(NO_SHA)
- case DESb:
- *len = sizeof(blkDesCbcOid);
- *oid = blkDesCbcOid;
- *id = PBE_SHA1_DES;
- *blkSz = 8;
- break;
- case DES3b:
- *len = sizeof(blkDes3CbcOid);
- *oid = blkDes3CbcOid;
- *id = PBE_SHA1_DES3;
- *blkSz = 8;
- break;
-#endif
-#if defined(WOLFSSL_AES_256) && defined(HAVE_AES_CBC)
- case AES256CBCb:
- *len = sizeof(blkAes256CbcOid);
- *oid = blkAes256CbcOid;
- *id = PBE_AES256_CBC;
- *blkSz = 16;
- break;
-#endif
- default:
- (void)len;
- (void)oid;
- (void)id;
- (void)blkSz;
- ret = ALGO_ID_E;
- }
-
- return ret;
-}
-
-/* Converts Encrypted PKCS#8 to 'traditional' (i.e. PKCS#8 removed from
- * decrypted key.)
- */
-int TraditionalEnc(byte* key, word32 keySz, byte* out, word32* outSz,
- const char* password, int passwordSz, int vPKCS, int vAlgo,
- int encAlgId, byte* salt, word32 saltSz, int itt, WC_RNG* rng,
- void* heap)
-{
- int ret = 0;
- int version, blockSz, id;
- word32 idx = 0, encIdx;
-#ifdef WOLFSSL_SMALL_STACK
- byte* saltTmp = NULL;
-#else
- byte saltTmp[MAX_SALT_SIZE];
-#endif
- byte cbcIv[MAX_IV_SIZE];
- byte *pkcs8Key = NULL;
- word32 pkcs8KeySz = 0, padSz = 0;
- int algId = 0;
- const byte* curveOid = NULL;
- word32 curveOidSz = 0;
- const byte* pbeOid = NULL;
- word32 pbeOidSz = 0;
- const byte* encOid = NULL;
- int encOidSz = 0;
- word32 pbeLen = 0, kdfLen = 0, encLen = 0;
- word32 innerLen = 0, outerLen;
-
- ret = CheckAlgo(vPKCS, vAlgo, &id, &version, &blockSz);
- /* create random salt if one not provided */
- if (ret == 0 && (salt == NULL || saltSz == 0)) {
- saltSz = 8;
- #ifdef WOLFSSL_SMALL_STACK
- saltTmp = (byte*)XMALLOC(saltSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (saltTmp == NULL)
- return MEMORY_E;
- #endif
- salt = saltTmp;
-
- if ((ret = wc_RNG_GenerateBlock(rng, saltTmp, saltSz)) != 0) {
- WOLFSSL_MSG("Error generating random salt");
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- }
-
- if (ret == 0) {
- /* check key type and get OID if ECC */
- ret = wc_GetKeyOID(key, keySz, &curveOid, &curveOidSz, &algId, heap);
- if (ret == 1)
- ret = 0;
- }
- if (ret == 0) {
- ret = wc_CreatePKCS8Key(NULL, &pkcs8KeySz, key, keySz, algId, curveOid,
- curveOidSz);
- if (ret == LENGTH_ONLY_E)
- ret = 0;
- }
- if (ret == 0) {
- pkcs8Key = (byte*)XMALLOC(pkcs8KeySz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (pkcs8Key == NULL)
- ret = MEMORY_E;
- }
- if (ret == 0) {
- ret = wc_CreatePKCS8Key(pkcs8Key, &pkcs8KeySz, key, keySz, algId,
- curveOid, curveOidSz);
- if (ret >= 0) {
- pkcs8KeySz = ret;
- ret = 0;
- }
- }
-
- if (ret == 0 && version == PKCS5v2)
- ret = GetAlgoV2(encAlgId, &encOid, &encOidSz, &id, &blockSz);
-
- if (ret == 0) {
- padSz = (blockSz - (pkcs8KeySz & (blockSz - 1))) & (blockSz - 1);
- /* inner = OCT salt INT itt */
- innerLen = 2 + saltSz + 2 + (itt < 256 ? 1 : 2);
-
- if (version != PKCS5v2) {
- pbeOid = OidFromId(id, oidPBEType, &pbeOidSz);
- /* pbe = OBJ pbse1 SEQ [ inner ] */
- pbeLen = 2 + pbeOidSz + 2 + innerLen;
- }
- else {
- pbeOid = pbes2;
- pbeOidSz = sizeof(pbes2);
- /* kdf = OBJ pbkdf2 [ SEQ innerLen ] */
- kdfLen = 2 + sizeof(pbkdf2Oid) + 2 + innerLen;
- /* enc = OBJ enc_alg OCT iv */
- encLen = 2 + encOidSz + 2 + blockSz;
- /* pbe = OBJ pbse2 SEQ [ SEQ [ kdf ] SEQ [ enc ] ] */
- pbeLen = 2 + sizeof(pbes2) + 2 + 2 + kdfLen + 2 + encLen;
-
- ret = wc_RNG_GenerateBlock(rng, cbcIv, blockSz);
- }
- }
- if (ret == 0) {
- /* outer = SEQ [ pbe ] OCT encrypted_PKCS#8_key */
- outerLen = 2 + pbeLen;
- outerLen += SetOctetString(pkcs8KeySz + padSz, out);
- outerLen += pkcs8KeySz + padSz;
-
- idx += SetSequence(outerLen, out + idx);
-
- encIdx = idx + outerLen - pkcs8KeySz - padSz;
- /* Put Encrypted content in place. */
- XMEMCPY(out + encIdx, pkcs8Key, pkcs8KeySz);
- if (padSz > 0) {
- XMEMSET(out + encIdx + pkcs8KeySz, padSz, padSz);
- pkcs8KeySz += padSz;
- }
- ret = wc_CryptKey(password, passwordSz, salt, saltSz, itt, id,
- out + encIdx, pkcs8KeySz, version, cbcIv, 1, 0);
- }
- if (ret == 0) {
- if (version != PKCS5v2) {
- /* PBE algorithm */
- idx += SetSequence(pbeLen, out + idx);
- idx += SetObjectId(pbeOidSz, out + idx);
- XMEMCPY(out + idx, pbeOid, pbeOidSz);
- idx += pbeOidSz;
- }
- else {
- /* PBES2 algorithm identifier */
- idx += SetSequence(pbeLen, out + idx);
- idx += SetObjectId(pbeOidSz, out + idx);
- XMEMCPY(out + idx, pbeOid, pbeOidSz);
- idx += pbeOidSz;
- /* PBES2 Parameters: SEQ [ kdf ] SEQ [ enc ] */
- idx += SetSequence(2 + kdfLen + 2 + encLen, out + idx);
- /* KDF Algorithm Identifier */
- idx += SetSequence(kdfLen, out + idx);
- idx += SetObjectId(sizeof(pbkdf2Oid), out + idx);
- XMEMCPY(out + idx, pbkdf2Oid, sizeof(pbkdf2Oid));
- idx += sizeof(pbkdf2Oid);
- }
- idx += SetSequence(innerLen, out + idx);
- idx += SetOctetString(saltSz, out + idx);
- XMEMCPY(out + idx, salt, saltSz); idx += saltSz;
- ret = SetShortInt(out, &idx, itt, *outSz);
- if (ret > 0)
- ret = 0;
- }
- if (ret == 0) {
- if (version == PKCS5v2) {
- /* Encryption Algorithm Identifier */
- idx += SetSequence(encLen, out + idx);
- idx += SetObjectId(encOidSz, out + idx);
- XMEMCPY(out + idx, encOid, encOidSz);
- idx += encOidSz;
- /* Encryption Algorithm Parameter: CBC IV */
- idx += SetOctetString(blockSz, out + idx);
- XMEMCPY(out + idx, cbcIv, blockSz);
- idx += blockSz;
- }
- idx += SetOctetString(pkcs8KeySz, out + idx);
- /* Default PRF - no need to write out OID */
- idx += pkcs8KeySz;
-
- ret = idx;
- }
-
- if (pkcs8Key != NULL) {
- ForceZero(pkcs8Key, pkcs8KeySz);
- XFREE(pkcs8Key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
-#ifdef WOLFSSL_SMALL_STACK
- if (saltTmp != NULL) {
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-#endif
-
- (void)rng;
-
- return ret;
-}
-
-#endif /* HAVE_PKCS8 */
-
-#if defined(HAVE_PKCS8) || defined(HAVE_PKCS12)
-/* decrypt PKCS
- *
- * NOTE: input buffer is overwritten with decrypted data!
- *
- * input[in/out] data to decrypt and results are written to
- * sz size of input buffer
- * password password if used. Can be NULL for no password
- * passwordSz size of password buffer
- *
- * returns the total size of decrypted content on success.
- */
-int DecryptContent(byte* input, word32 sz, const char* password, int passwordSz)
-{
- word32 inOutIdx = 0, seqEnd, oid, shaOid = 0;
- int ret = 0, first, second, length = 0, version, saltSz, id;
- int iterations = 0, keySz = 0;
-#ifdef WOLFSSL_SMALL_STACK
- byte* salt = NULL;
- byte* cbcIv = NULL;
-#else
- byte salt[MAX_SALT_SIZE];
- byte cbcIv[MAX_IV_SIZE];
-#endif
- byte tag;
-
- if (passwordSz < 0) {
- WOLFSSL_MSG("Bad password size");
- return BAD_FUNC_ARG;
- }
-
- if (GetAlgoId(input, &inOutIdx, &oid, oidIgnoreType, sz) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- first = input[inOutIdx - 2]; /* PKCS version always 2nd to last byte */
- second = input[inOutIdx - 1]; /* version.algo, algo id last byte */
-
- if (CheckAlgo(first, second, &id, &version, NULL) < 0) {
- ERROR_OUT(ASN_INPUT_E, exit_dc); /* Algo ID error */
- }
-
- if (version == PKCS5v2) {
- if (GetSequence(input, &inOutIdx, &length, sz) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- if (GetAlgoId(input, &inOutIdx, &oid, oidKdfType, sz) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- if (oid != PBKDF2_OID) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
- }
-
- if (GetSequence(input, &inOutIdx, &length, sz) <= 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
- /* Find the end of this SEQUENCE so we can check for the OPTIONAL and
- * DEFAULT items. */
- seqEnd = inOutIdx + length;
-
- ret = GetOctetString(input, &inOutIdx, &saltSz, sz);
- if (ret < 0)
- goto exit_dc;
-
- if (saltSz > MAX_SALT_SIZE) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- salt = (byte*)XMALLOC(MAX_SALT_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (salt == NULL) {
- ERROR_OUT(MEMORY_E, exit_dc);
- }
-#endif
-
- XMEMCPY(salt, &input[inOutIdx], saltSz);
- inOutIdx += saltSz;
-
- if (GetShortInt(input, &inOutIdx, &iterations, sz) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- /* OPTIONAL key length */
- if (seqEnd > inOutIdx) {
- word32 localIdx = inOutIdx;
-
- if (GetASNTag(input, &localIdx, &tag, sz) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- if (tag == ASN_INTEGER &&
- GetShortInt(input, &inOutIdx, &keySz, sz) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
- }
-
- /* DEFAULT HMAC is SHA-1 */
- if (seqEnd > inOutIdx) {
- if (GetAlgoId(input, &inOutIdx, &oid, oidHmacType, sz) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- shaOid = oid;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (cbcIv == NULL) {
- ERROR_OUT(MEMORY_E, exit_dc);
- }
-#endif
-
- if (version == PKCS5v2) {
- /* get encryption algo */
- if (GetAlgoId(input, &inOutIdx, &oid, oidBlkType, sz) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- if (CheckAlgoV2(oid, &id, NULL) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc); /* PKCS v2 algo id error */
- }
-
- if (shaOid == 0)
- shaOid = oid;
-
- ret = GetOctetString(input, &inOutIdx, &length, sz);
- if (ret < 0)
- goto exit_dc;
-
- if (length > MAX_IV_SIZE) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- XMEMCPY(cbcIv, &input[inOutIdx], length);
- inOutIdx += length;
- }
-
- if (GetASNTag(input, &inOutIdx, &tag, sz) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- if (tag != (ASN_CONTEXT_SPECIFIC | 0) && tag != ASN_OCTET_STRING) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- if (GetLength(input, &inOutIdx, &length, sz) < 0) {
- ERROR_OUT(ASN_PARSE_E, exit_dc);
- }
-
- ret = wc_CryptKey(password, passwordSz, salt, saltSz, iterations, id,
- input + inOutIdx, length, version, cbcIv, 0, shaOid);
-
-exit_dc:
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- if (ret == 0) {
- XMEMMOVE(input, input + inOutIdx, length);
- ret = length;
- }
-
- return ret;
-}
-
-
-/* Remove Encrypted PKCS8 header, move beginning of traditional to beginning
- of input */
-int ToTraditionalEnc(byte* input, word32 sz,const char* password,
- int passwordSz, word32* algId)
-{
- int ret, length;
- word32 inOutIdx = 0;
-
- if (GetSequence(input, &inOutIdx, &length, sz) < 0) {
- ret = ASN_PARSE_E;
- }
- else {
- ret = DecryptContent(input + inOutIdx, sz - inOutIdx, password,
- passwordSz);
- if (ret > 0) {
- XMEMMOVE(input, input + inOutIdx, ret);
- ret = ToTraditional_ex(input, ret, algId);
- }
- }
-
- return ret;
-}
-
-#endif /* HAVE_PKCS8 || HAVE_PKCS12 */
-
-#ifdef HAVE_PKCS12
-
-/* encrypt PKCS 12 content
- *
- * NOTE: if out is NULL then outSz is set with the total buffer size needed and
- * the error value LENGTH_ONLY_E is returned.
- *
- * input data to encrypt
- * inputSz size of input buffer
- * out buffer to hold the result
- * outSz size of out buffer
- * password password if used. Can be NULL for no password
- * passwordSz size of password buffer
- * vPKCS version of PKCS i.e. PKCS5v2
- * vAlgo algorithm version
- * salt buffer holding salt if used. If NULL then a random salt is created
- * saltSz size of salt buffer if it is not NULL
- * itt number of iterations used
- * rng random number generator to use
- * heap possible heap hint for mallocs/frees
- *
- * returns the total size of encrypted content on success.
- *
- * data returned is :
- * [ seq - obj [ seq -salt,itt]] , construct with encrypted data
- */
-int EncryptContent(byte* input, word32 inputSz, byte* out, word32* outSz,
- const char* password, int passwordSz, int vPKCS, int vAlgo,
- byte* salt, word32 saltSz, int itt, WC_RNG* rng, void* heap)
-{
- word32 sz;
- word32 inOutIdx = 0;
- word32 tmpIdx = 0;
- word32 totalSz = 0;
- word32 seqSz;
- word32 innerSz;
- int ret;
- int version, id, blockSz = 0;
-#ifdef WOLFSSL_SMALL_STACK
- byte* saltTmp = NULL;
- byte* cbcIv = NULL;
-#else
- byte saltTmp[MAX_SALT_SIZE];
- byte cbcIv[MAX_IV_SIZE];
-#endif
- byte seq[MAX_SEQ_SZ];
- byte shr[MAX_SHORT_SZ];
- word32 maxShr = MAX_SHORT_SZ;
- word32 algoSz;
- const byte* algoName;
-
- (void)heap;
-
- WOLFSSL_ENTER("EncryptContent()");
-
- if (CheckAlgo(vPKCS, vAlgo, &id, &version, &blockSz) < 0)
- return ASN_INPUT_E; /* Algo ID error */
-
- if (version == PKCS5v2) {
- WOLFSSL_MSG("PKCS#5 version 2 not supported yet");
- return BAD_FUNC_ARG;
- }
-
- if (saltSz > MAX_SALT_SIZE)
- return ASN_PARSE_E;
-
- if (outSz == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* calculate size */
- /* size of constructed string at end */
- sz = Pkcs8Pad(NULL, inputSz, blockSz);
- totalSz = ASN_TAG_SZ;
- totalSz += SetLength(sz, seq);
- totalSz += sz;
-
- /* size of sequence holding object id and sub sequence of salt and itt */
- algoName = OidFromId(id, oidPBEType, &algoSz);
- if (algoName == NULL) {
- WOLFSSL_MSG("Unknown Algorithm");
- return 0;
- }
- innerSz = SetObjectId(algoSz, seq);
- innerSz += algoSz;
-
- /* get subsequence of salt and itt */
- if (salt == NULL || saltSz == 0) {
- sz = 8;
- }
- else {
- sz = saltSz;
- }
- seqSz = SetOctetString(sz, seq);
- seqSz += sz;
-
- tmpIdx = 0;
- seqSz += SetShortInt(shr, &tmpIdx, itt, maxShr);
- innerSz += seqSz + SetSequence(seqSz, seq);
- totalSz += innerSz + SetSequence(innerSz, seq);
-
- if (out == NULL) {
- *outSz = totalSz;
- return LENGTH_ONLY_E;
- }
-
- inOutIdx = 0;
- if (totalSz > *outSz)
- return BUFFER_E;
-
- inOutIdx += SetSequence(innerSz, out + inOutIdx);
- inOutIdx += SetObjectId(algoSz, out + inOutIdx);
- XMEMCPY(out + inOutIdx, algoName, algoSz);
- inOutIdx += algoSz;
- inOutIdx += SetSequence(seqSz, out + inOutIdx);
-
- /* create random salt if one not provided */
- if (salt == NULL || saltSz == 0) {
- saltSz = 8;
- #ifdef WOLFSSL_SMALL_STACK
- saltTmp = (byte*)XMALLOC(saltSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (saltTmp == NULL)
- return MEMORY_E;
- #endif
- salt = saltTmp;
-
- if ((ret = wc_RNG_GenerateBlock(rng, saltTmp, saltSz)) != 0) {
- WOLFSSL_MSG("Error generating random salt");
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
- }
- inOutIdx += SetOctetString(saltSz, out + inOutIdx);
- if (saltSz + inOutIdx > *outSz) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return BUFFER_E;
- }
- XMEMCPY(out + inOutIdx, salt, saltSz);
- inOutIdx += saltSz;
-
- /* place iteration setting in buffer */
- ret = SetShortInt(out, &inOutIdx, itt, *outSz);
- if (ret < 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret;
- }
-
- if (inOutIdx + 1 > *outSz) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return BUFFER_E;
- }
- out[inOutIdx++] = ASN_CONTEXT_SPECIFIC | 0;
-
- /* get pad size and verify buffer room */
- sz = Pkcs8Pad(NULL, inputSz, blockSz);
- if (sz + inOutIdx > *outSz) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return BUFFER_E;
- }
- inOutIdx += SetLength(sz, out + inOutIdx);
-
- /* copy input to output buffer and pad end */
- XMEMCPY(out + inOutIdx, input, inputSz);
- sz = Pkcs8Pad(out + inOutIdx, inputSz, blockSz);
-#ifdef WOLFSSL_SMALL_STACK
- cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (cbcIv == NULL) {
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#endif
-
- /* encrypt */
- if ((ret = wc_CryptKey(password, passwordSz, salt, saltSz, itt, id,
- out + inOutIdx, sz, version, cbcIv, 1, 0)) < 0) {
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(cbcIv, heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return ret; /* encrypt failure */
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(cbcIv, heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(saltTmp, heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- (void)rng;
-
- return inOutIdx + sz;
-}
-
-
-#endif /* HAVE_PKCS12 */
-#endif /* NO_PWDBASED */
-
-#ifndef NO_RSA
-
-#ifndef HAVE_USER_RSA
-#ifdef WOLFSSL_RENESAS_TSIP
-/* This function is to retrieve key position information in a cert.*
- * The information will be used to call TSIP TLS-linked API for *
- * certificate verification. */
-static int RsaPublicKeyDecodeRawIndex(const byte* input, word32* inOutIdx,
- word32 inSz, word32* key_n,
- word32* key_n_len, word32* key_e,
- word32* key_e_len)
-{
-
- int ret = 0;
- int length = 0;
-#if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
- byte b;
-#endif
-
- if (input == NULL || inOutIdx == NULL)
- return BAD_FUNC_ARG;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
-#if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
- if ((*inOutIdx + 1) > inSz)
- return BUFFER_E;
-
- b = input[*inOutIdx];
- if (b != ASN_INTEGER) {
- /* not from decoded cert, will have algo id, skip past */
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (SkipObjectId(input, inOutIdx, inSz) < 0)
- return ASN_PARSE_E;
-
- /* Option NULL ASN.1 tag */
- if (*inOutIdx >= inSz) {
- return BUFFER_E;
- }
- if (input[*inOutIdx] == ASN_TAG_NULL) {
- ret = GetASNNull(input, inOutIdx, inSz);
- if (ret != 0)
- return ret;
- }
-
- /* should have bit tag length and seq next */
- ret = CheckBitString(input, inOutIdx, NULL, inSz, 1, NULL);
- if (ret != 0)
- return ret;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
- }
-#endif /* OPENSSL_EXTRA */
-
- /* Get modulus */
- ret = GetASNInt(input, inOutIdx, &length, inSz);
- *key_n += *inOutIdx;
- if (ret < 0) {
- return ASN_RSA_KEY_E;
- }
- if (key_n_len)
- *key_n_len = length;
- *inOutIdx += length;
-
- /* Get exponent */
- ret = GetASNInt(input, inOutIdx, &length, inSz);
- *key_e += *inOutIdx;
- if (ret < 0) {
- return ASN_RSA_KEY_E;
- }
- if (key_e_len)
- *key_e_len = length;
-
- return ret;
-}
-#endif /* WOLFSSL_RENESAS_TSIP */
-
-int wc_RsaPublicKeyDecode_ex(const byte* input, word32* inOutIdx, word32 inSz,
- const byte** n, word32* nSz, const byte** e, word32* eSz)
-{
- int ret = 0;
- int length = 0;
-#if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
- word32 localIdx;
- byte tag;
-#endif
-
- if (input == NULL || inOutIdx == NULL)
- return BAD_FUNC_ARG;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
-#if defined(OPENSSL_EXTRA) || defined(RSA_DECODE_EXTRA)
- localIdx = *inOutIdx;
- if (GetASNTag(input, &localIdx, &tag, inSz) < 0)
- return BUFFER_E;
-
- if (tag != ASN_INTEGER) {
- /* not from decoded cert, will have algo id, skip past */
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (SkipObjectId(input, inOutIdx, inSz) < 0)
- return ASN_PARSE_E;
-
- /* Option NULL ASN.1 tag */
- if (*inOutIdx >= inSz) {
- return BUFFER_E;
- }
-
- localIdx = *inOutIdx;
- if (GetASNTag(input, &localIdx, &tag, inSz) < 0)
- return ASN_PARSE_E;
-
- if (tag == ASN_TAG_NULL) {
- ret = GetASNNull(input, inOutIdx, inSz);
- if (ret != 0)
- return ret;
- }
-
- /* should have bit tag length and seq next */
- ret = CheckBitString(input, inOutIdx, NULL, inSz, 1, NULL);
- if (ret != 0)
- return ret;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
- }
-#endif /* OPENSSL_EXTRA */
-
- /* Get modulus */
- ret = GetASNInt(input, inOutIdx, &length, inSz);
- if (ret < 0) {
- return ASN_RSA_KEY_E;
- }
- if (nSz)
- *nSz = length;
- if (n)
- *n = &input[*inOutIdx];
- *inOutIdx += length;
-
- /* Get exponent */
- ret = GetASNInt(input, inOutIdx, &length, inSz);
- if (ret < 0) {
- return ASN_RSA_KEY_E;
- }
- if (eSz)
- *eSz = length;
- if (e)
- *e = &input[*inOutIdx];
- *inOutIdx += length;
-
- return ret;
-}
-
-int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx, RsaKey* key,
- word32 inSz)
-{
- int ret;
- const byte *n = NULL, *e = NULL;
- word32 nSz = 0, eSz = 0;
-
- if (key == NULL)
- return BAD_FUNC_ARG;
-
- ret = wc_RsaPublicKeyDecode_ex(input, inOutIdx, inSz, &n, &nSz, &e, &eSz);
- if (ret == 0) {
- ret = wc_RsaPublicKeyDecodeRaw(n, nSz, e, eSz, key);
- }
-
- return ret;
-}
-
-/* import RSA public key elements (n, e) into RsaKey structure (key) */
-int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz, const byte* e,
- word32 eSz, RsaKey* key)
-{
- if (n == NULL || e == NULL || key == NULL)
- return BAD_FUNC_ARG;
-
- key->type = RSA_PUBLIC;
-
- if (mp_init(&key->n) != MP_OKAY)
- return MP_INIT_E;
-
- if (mp_read_unsigned_bin(&key->n, n, nSz) != 0) {
- mp_clear(&key->n);
- return ASN_GETINT_E;
- }
-#ifdef HAVE_WOLF_BIGINT
- if ((int)nSz > 0 && wc_bigint_from_unsigned_bin(&key->n.raw, n, nSz) != 0) {
- mp_clear(&key->n);
- return ASN_GETINT_E;
- }
-#endif /* HAVE_WOLF_BIGINT */
-
- if (mp_init(&key->e) != MP_OKAY) {
- mp_clear(&key->n);
- return MP_INIT_E;
- }
-
- if (mp_read_unsigned_bin(&key->e, e, eSz) != 0) {
- mp_clear(&key->n);
- mp_clear(&key->e);
- return ASN_GETINT_E;
- }
-#ifdef HAVE_WOLF_BIGINT
- if ((int)eSz > 0 && wc_bigint_from_unsigned_bin(&key->e.raw, e, eSz) != 0) {
- mp_clear(&key->n);
- mp_clear(&key->e);
- return ASN_GETINT_E;
- }
-#endif /* HAVE_WOLF_BIGINT */
-
-#ifdef WOLFSSL_XILINX_CRYPT
- if (wc_InitRsaHw(key) != 0) {
- return BAD_STATE_E;
- }
-#endif
-
- return 0;
-}
-#endif /* HAVE_USER_RSA */
-#endif /* !NO_RSA */
-
-#ifndef NO_DH
-
-int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz)
-{
- int ret = 0;
- int length;
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- word32 oid = 0, temp = 0;
- #endif
-
- WOLFSSL_ENTER("wc_DhKeyDecode");
-
- if (inOutIdx == NULL)
- return BAD_FUNC_ARG;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- temp = *inOutIdx;
- #endif
-
- /* Assume input started after 1.2.840.113549.1.3.1 dhKeyAgreement */
- if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->g, input, inOutIdx, inSz) < 0) {
- ret = ASN_DH_KEY_E;
- }
-
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- /* If ASN_DH_KEY_E: Check if input started at beginning of key */
- if (ret == ASN_DH_KEY_E) {
- /* rewind back to after the first sequence */
- *inOutIdx = temp;
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- /* Check for dhKeyAgreement */
- ret = GetObjectId(input, inOutIdx, &oid, oidKeyType, inSz);
- if (oid != DHk || ret < 0)
- return ASN_DH_KEY_E;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->g, input, inOutIdx, inSz) < 0) {
- return ASN_DH_KEY_E;
- }
- }
-
- temp = *inOutIdx;
- ret = (CheckBitString(input, inOutIdx, &length, inSz, 0, NULL) == 0);
- if (ret > 0) {
- /* Found Bit String */
- if (GetInt(&key->pub, input, inOutIdx, inSz) == 0) {
- WOLFSSL_MSG("Found Public Key");
- ret = 0;
- }
- } else {
- *inOutIdx = temp;
- ret = (GetOctetString(input, inOutIdx, &length, inSz) >= 0);
- if (ret > 0) {
- /* Found Octet String */
- if (GetInt(&key->priv, input, inOutIdx, inSz) == 0) {
- WOLFSSL_MSG("Found Private Key");
- ret = 0;
- }
- } else {
- /* Don't use length from failed CheckBitString/GetOctetString */
- *inOutIdx = temp;
- ret = 0;
- }
- }
- #endif /* WOLFSSL_QT || OPENSSL_ALL */
-
- WOLFSSL_MSG("wc_DhKeyDecode Success");
-
- return ret;
-}
-
-
-int wc_DhParamsLoad(const byte* input, word32 inSz, byte* p, word32* pInOutSz,
- byte* g, word32* gInOutSz)
-{
- word32 idx = 0;
- int ret;
- int length;
-
- if (GetSequence(input, &idx, &length, inSz) <= 0)
- return ASN_PARSE_E;
-
- ret = GetASNInt(input, &idx, &length, inSz);
- if (ret != 0)
- return ret;
-
- if (length <= (int)*pInOutSz) {
- XMEMCPY(p, &input[idx], length);
- *pInOutSz = length;
- }
- else {
- return BUFFER_E;
- }
- idx += length;
-
- ret = GetASNInt(input, &idx, &length, inSz);
- if (ret != 0)
- return ret;
-
- if (length <= (int)*gInOutSz) {
- XMEMCPY(g, &input[idx], length);
- *gInOutSz = length;
- }
- else {
- return BUFFER_E;
- }
-
- return 0;
-}
-#endif /* NO_DH */
-
-
-#ifndef NO_DSA
-
-int DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
- word32 inSz)
-{
- int length;
- int ret = 0;
- word32 oid;
-
- if (input == NULL || inOutIdx == NULL || key == NULL)
- return BAD_FUNC_ARG;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->q, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->g, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->y, input, inOutIdx, inSz) < 0 )
- ret = ASN_DH_KEY_E;
-
- if (ret != 0) {
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- ret = GetObjectId(input, inOutIdx, &oid, oidIgnoreType, inSz);
- if (ret != 0)
- return ret;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->q, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->g, input, inOutIdx, inSz) < 0)
- return ASN_DH_KEY_E;
-
- if (CheckBitString(input, inOutIdx, &length, inSz, 0, NULL) < 0)
- return ASN_PARSE_E;
-
- if (GetInt(&key->y, input, inOutIdx, inSz) < 0 )
- return ASN_DH_KEY_E;
-
- ret = 0;
- }
-
- key->type = DSA_PUBLIC;
- return ret;
-}
-
-
-int DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key,
- word32 inSz)
-{
- int length, version, ret = 0, temp = 0;
-
- /* Sanity checks on input */
- if (input == NULL || inOutIdx == NULL || key == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- temp = (int)*inOutIdx;
-
- /* Default case expects a certificate with OctetString but no version ID */
- ret = GetInt(&key->p, input, inOutIdx, inSz);
- if (ret < 0) {
- mp_clear(&key->p);
- ret = ASN_PARSE_E;
- }
- else {
- ret = GetInt(&key->q, input, inOutIdx, inSz);
- if (ret < 0) {
- mp_clear(&key->p);
- mp_clear(&key->q);
- ret = ASN_PARSE_E;
- }
- else {
- ret = GetInt(&key->g, input, inOutIdx, inSz);
- if (ret < 0) {
- mp_clear(&key->p);
- mp_clear(&key->q);
- mp_clear(&key->g);
- ret = ASN_PARSE_E;
- }
- else {
- ret = GetOctetString(input, inOutIdx, &length, inSz);
- if (ret < 0) {
- mp_clear(&key->p);
- mp_clear(&key->q);
- mp_clear(&key->g);
- ret = ASN_PARSE_E;
- }
- else {
- ret = GetInt(&key->y, input, inOutIdx, inSz);
- if (ret < 0) {
- mp_clear(&key->p);
- mp_clear(&key->q);
- mp_clear(&key->g);
- mp_clear(&key->y);
- ret = ASN_PARSE_E;
- }
- }
- }
- }
- }
- /* An alternate pass if default certificate fails parsing */
- if (ret == ASN_PARSE_E) {
- *inOutIdx = temp;
- if (GetMyVersion(input, inOutIdx, &version, inSz) < 0)
- return ASN_PARSE_E;
-
- if (GetInt(&key->p, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->q, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->g, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->y, input, inOutIdx, inSz) < 0 ||
- GetInt(&key->x, input, inOutIdx, inSz) < 0 )
- return ASN_DH_KEY_E;
- }
-
- key->type = DSA_PRIVATE;
- return 0;
-}
-
-static mp_int* GetDsaInt(DsaKey* key, int idx)
-{
- if (idx == 0)
- return &key->p;
- if (idx == 1)
- return &key->q;
- if (idx == 2)
- return &key->g;
- if (idx == 3)
- return &key->y;
- if (idx == 4)
- return &key->x;
-
- return NULL;
-}
-
-/* Release Tmp DSA resources */
-static WC_INLINE void FreeTmpDsas(byte** tmps, void* heap)
-{
- int i;
-
- for (i = 0; i < DSA_INTS; i++)
- XFREE(tmps[i], heap, DYNAMIC_TYPE_DSA);
-
- (void)heap;
-}
-
-#if !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
-/* Write a public DSA key to output */
-int wc_SetDsaPublicKey(byte* output, DsaKey* key,
- int outLen, int with_header)
-{
- /* p, g, q = DSA params, y = public exponent */
-#ifdef WOLFSSL_SMALL_STACK
- byte* p = NULL;
- byte* g = NULL;
- byte* q = NULL;
- byte* y = NULL;
-#else
- byte p[MAX_DSA_INT_SZ];
- byte g[MAX_DSA_INT_SZ];
- byte q[MAX_DSA_INT_SZ];
- byte y[MAX_DSA_INT_SZ];
-#endif
- byte innerSeq[MAX_SEQ_SZ];
- byte outerSeq[MAX_SEQ_SZ];
- byte bitString[1 + MAX_LENGTH_SZ + 1];
- int idx, pSz, gSz, qSz, ySz, innerSeqSz, outerSeqSz, bitStringSz = 0;
-
- WOLFSSL_ENTER("wc_SetDsaPublicKey");
-
- if (output == NULL || key == NULL || outLen < MAX_SEQ_SZ) {
- return BAD_FUNC_ARG;
- }
-
- /* p */
-#ifdef WOLFSSL_SMALL_STACK
- p = (byte*)XMALLOC(MAX_DSA_INT_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (p == NULL)
- return MEMORY_E;
-#endif
- if ((pSz = SetASNIntMP(&key->p, MAX_DSA_INT_SZ, p)) < 0) {
- WOLFSSL_MSG("SetASNIntMP Error with p");
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(p, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return pSz;
- }
-
- /* q */
-#ifdef WOLFSSL_SMALL_STACK
- q = (byte*)XMALLOC(MAX_DSA_INT_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (q == NULL)
- return MEMORY_E;
-#endif
- if ((qSz = SetASNIntMP(&key->q, MAX_DSA_INT_SZ, q)) < 0) {
- WOLFSSL_MSG("SetASNIntMP Error with q");
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(p, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(q, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return qSz;
- }
-
- /* g */
-#ifdef WOLFSSL_SMALL_STACK
- g = (byte*)XMALLOC(MAX_DSA_INT_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (g == NULL)
- return MEMORY_E;
-#endif
- if ((gSz = SetASNIntMP(&key->g, MAX_DSA_INT_SZ, g)) < 0) {
- WOLFSSL_MSG("SetASNIntMP Error with g");
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(p, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(q, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(g, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return gSz;
- }
-
- /* y */
-#ifdef WOLFSSL_SMALL_STACK
- y = (byte*)XMALLOC(MAX_DSA_INT_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (y == NULL)
- return MEMORY_E;
-#endif
- if ((ySz = SetASNIntMP(&key->y, MAX_DSA_INT_SZ, y)) < 0) {
- WOLFSSL_MSG("SetASNIntMP Error with y");
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(p, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(q, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(g, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(y, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return ySz;
- }
-
- innerSeqSz = SetSequence(pSz + qSz + gSz, innerSeq);
-
- /* check output size */
- if ((innerSeqSz + pSz + qSz + gSz) > outLen) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(p, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(q, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(g, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(y, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- WOLFSSL_MSG("Error, output size smaller than outlen");
- return BUFFER_E;
- }
-
- if (with_header) {
- int algoSz;
-#ifdef WOLFSSL_SMALL_STACK
- byte* algo = NULL;
-
- algo = (byte*)XMALLOC(MAX_ALGO_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (algo == NULL) {
- XFREE(p, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(q, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(g, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(y, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#else
- byte algo[MAX_ALGO_SZ];
-#endif
- algoSz = SetAlgoID(DSAk, algo, oidKeyType, 0);
- bitStringSz = SetBitString(ySz, 0, bitString);
- outerSeqSz = SetSequence(algoSz + innerSeqSz + pSz + qSz + gSz,
- outerSeq);
-
- idx = SetSequence(algoSz + innerSeqSz + pSz + qSz + gSz + bitStringSz +
- ySz + outerSeqSz, output);
-
- /* check output size */
- if ((idx + algoSz + bitStringSz + innerSeqSz + pSz + qSz + gSz + ySz) >
- outLen) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(p, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(q, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(g, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(y, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(algo, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- WOLFSSL_MSG("Error, output size smaller than outlen");
- return BUFFER_E;
- }
-
- /* outerSeq */
- XMEMCPY(output + idx, outerSeq, outerSeqSz);
- idx += outerSeqSz;
- /* algo */
- XMEMCPY(output + idx, algo, algoSz);
- idx += algoSz;
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(algo, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- } else {
- idx = 0;
- }
-
- /* innerSeq */
- XMEMCPY(output + idx, innerSeq, innerSeqSz);
- idx += innerSeqSz;
- /* p */
- XMEMCPY(output + idx, p, pSz);
- idx += pSz;
- /* q */
- XMEMCPY(output + idx, q, qSz);
- idx += qSz;
- /* g */
- XMEMCPY(output + idx, g, gSz);
- idx += gSz;
- /* bit string */
- XMEMCPY(output + idx, bitString, bitStringSz);
- idx += bitStringSz;
- /* y */
- XMEMCPY(output + idx, y, ySz);
- idx += ySz;
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(p, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(q, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(g, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(y, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return idx;
-}
-
-/* Convert DSA Public key to DER format, write to output (inLen), return bytes
- written */
-int wc_DsaKeyToPublicDer(DsaKey* key, byte* output, word32 inLen)
-{
- return wc_SetDsaPublicKey(output, key, inLen, 1);
-}
-#endif /* !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
-
-/* Convert private DsaKey key to DER format, write to output (inLen),
- return bytes written */
-int wc_DsaKeyToDer(DsaKey* key, byte* output, word32 inLen)
-{
- word32 seqSz, verSz, rawLen, intTotalLen = 0;
- word32 sizes[DSA_INTS];
- int i, j, outLen, ret = 0, mpSz;
-
- byte seq[MAX_SEQ_SZ];
- byte ver[MAX_VERSION_SZ];
- byte* tmps[DSA_INTS];
-
- if (!key || !output)
- return BAD_FUNC_ARG;
-
- if (key->type != DSA_PRIVATE)
- return BAD_FUNC_ARG;
-
- for (i = 0; i < DSA_INTS; i++)
- tmps[i] = NULL;
-
- /* write all big ints from key to DER tmps */
- for (i = 0; i < DSA_INTS; i++) {
- mp_int* keyInt = GetDsaInt(key, i);
-
- rawLen = mp_unsigned_bin_size(keyInt) + 1;
- tmps[i] = (byte*)XMALLOC(rawLen + MAX_SEQ_SZ, key->heap,
- DYNAMIC_TYPE_DSA);
- if (tmps[i] == NULL) {
- ret = MEMORY_E;
- break;
- }
-
- mpSz = SetASNIntMP(keyInt, -1, tmps[i]);
- if (mpSz < 0) {
- ret = mpSz;
- break;
- }
- intTotalLen += (sizes[i] = mpSz);
- }
-
- if (ret != 0) {
- FreeTmpDsas(tmps, key->heap);
- return ret;
- }
-
- /* make headers */
- verSz = SetMyVersion(0, ver, FALSE);
- seqSz = SetSequence(verSz + intTotalLen, seq);
-
- outLen = seqSz + verSz + intTotalLen;
- if (outLen > (int)inLen) {
- FreeTmpDsas(tmps, key->heap);
- return BAD_FUNC_ARG;
- }
-
- /* write to output */
- XMEMCPY(output, seq, seqSz);
- j = seqSz;
- XMEMCPY(output + j, ver, verSz);
- j += verSz;
-
- for (i = 0; i < DSA_INTS; i++) {
- XMEMCPY(output + j, tmps[i], sizes[i]);
- j += sizes[i];
- }
- FreeTmpDsas(tmps, key->heap);
-
- return outLen;
-}
-
-#endif /* NO_DSA */
-
-void InitDecodedCert(DecodedCert* cert,
- const byte* source, word32 inSz, void* heap)
-{
- if (cert != NULL) {
- XMEMSET(cert, 0, sizeof(DecodedCert));
-
- cert->subjectCNEnc = CTC_UTF8;
- cert->issuer[0] = '\0';
- cert->subject[0] = '\0';
- cert->source = source; /* don't own */
- cert->maxIdx = inSz; /* can't go over this index */
- cert->heap = heap;
- cert->maxPathLen = WOLFSSL_MAX_PATH_LEN;
- #ifdef WOLFSSL_CERT_GEN
- cert->subjectSNEnc = CTC_UTF8;
- cert->subjectCEnc = CTC_PRINTABLE;
- cert->subjectLEnc = CTC_UTF8;
- cert->subjectSTEnc = CTC_UTF8;
- cert->subjectOEnc = CTC_UTF8;
- cert->subjectOUEnc = CTC_UTF8;
- #endif /* WOLFSSL_CERT_GEN */
-
- #ifndef NO_CERTS
- InitSignatureCtx(&cert->sigCtx, heap, INVALID_DEVID);
- #endif
- }
-}
-
-
-void FreeAltNames(DNS_entry* altNames, void* heap)
-{
- (void)heap;
-
- while (altNames) {
- DNS_entry* tmp = altNames->next;
-
- XFREE(altNames->name, heap, DYNAMIC_TYPE_ALTNAME);
- XFREE(altNames, heap, DYNAMIC_TYPE_ALTNAME);
- altNames = tmp;
- }
-}
-
-#ifndef IGNORE_NAME_CONSTRAINTS
-
-void FreeNameSubtrees(Base_entry* names, void* heap)
-{
- (void)heap;
-
- while (names) {
- Base_entry* tmp = names->next;
-
- XFREE(names->name, heap, DYNAMIC_TYPE_ALTNAME);
- XFREE(names, heap, DYNAMIC_TYPE_ALTNAME);
- names = tmp;
- }
-}
-
-#endif /* IGNORE_NAME_CONSTRAINTS */
-
-void FreeDecodedCert(DecodedCert* cert)
-{
- if (cert == NULL)
- return;
- if (cert->subjectCNStored == 1)
- XFREE(cert->subjectCN, cert->heap, DYNAMIC_TYPE_SUBJECT_CN);
- if (cert->pubKeyStored == 1)
- XFREE((void*)cert->publicKey, cert->heap, DYNAMIC_TYPE_PUBLIC_KEY);
- if (cert->weOwnAltNames && cert->altNames)
- FreeAltNames(cert->altNames, cert->heap);
-#ifndef IGNORE_NAME_CONSTRAINTS
- if (cert->altEmailNames)
- FreeAltNames(cert->altEmailNames, cert->heap);
- if (cert->permittedNames)
- FreeNameSubtrees(cert->permittedNames, cert->heap);
- if (cert->excludedNames)
- FreeNameSubtrees(cert->excludedNames, cert->heap);
-#endif /* IGNORE_NAME_CONSTRAINTS */
-#ifdef WOLFSSL_SEP
- XFREE(cert->deviceType, cert->heap, DYNAMIC_TYPE_X509_EXT);
- XFREE(cert->hwType, cert->heap, DYNAMIC_TYPE_X509_EXT);
- XFREE(cert->hwSerialNum, cert->heap, DYNAMIC_TYPE_X509_EXT);
-#endif /* WOLFSSL_SEP */
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- if (cert->issuerName.fullName != NULL)
- XFREE(cert->issuerName.fullName, cert->heap, DYNAMIC_TYPE_X509);
- if (cert->subjectName.fullName != NULL)
- XFREE(cert->subjectName.fullName, cert->heap, DYNAMIC_TYPE_X509);
-#endif /* OPENSSL_EXTRA */
-#ifdef WOLFSSL_RENESAS_TSIP_TLS
- if (cert->tsip_encRsaKeyIdx != NULL)
- XFREE(cert->tsip_encRsaKeyIdx, cert->heap, DYNAMIC_TYPE_RSA);
-#endif
-#ifndef NO_CERTS
- FreeSignatureCtx(&cert->sigCtx);
-#endif
-}
-
-static int GetCertHeader(DecodedCert* cert)
-{
- int ret = 0, len;
-
- if (GetSequence(cert->source, &cert->srcIdx, &len, cert->maxIdx) < 0)
- return ASN_PARSE_E;
-
- /* Reset the max index for the size indicated in the outer wrapper. */
- cert->maxIdx = len + cert->srcIdx;
- cert->certBegin = cert->srcIdx;
-
- if (GetSequence(cert->source, &cert->srcIdx, &len, cert->maxIdx) < 0)
- return ASN_PARSE_E;
-
- cert->sigIndex = len + cert->srcIdx;
- if (cert->sigIndex > cert->maxIdx)
- return ASN_PARSE_E;
-
- if (GetExplicitVersion(cert->source, &cert->srcIdx, &cert->version,
- cert->sigIndex) < 0)
- return ASN_PARSE_E;
-
- if (GetSerialNumber(cert->source, &cert->srcIdx, cert->serial,
- &cert->serialSz, cert->sigIndex) < 0)
- return ASN_PARSE_E;
-
- return ret;
-}
-
-#if !defined(NO_RSA)
-/* Store Rsa Key, may save later, Dsa could use in future */
-static int StoreRsaKey(DecodedCert* cert, word32 bitStringEnd)
-{
- int length;
- word32 recvd = cert->srcIdx;
-
- if (GetSequence(cert->source, &cert->srcIdx, &length, bitStringEnd) < 0)
- return ASN_PARSE_E;
-
- recvd = cert->srcIdx - recvd;
- length += recvd;
-
- while (recvd--)
- cert->srcIdx--;
-#if defined(WOLFSSL_RENESAS_TSIP)
- cert->sigCtx.pubkey_n_start = cert->sigCtx.pubkey_e_start = cert->srcIdx;
-#endif
- cert->pubKeySize = length;
- cert->publicKey = cert->source + cert->srcIdx;
- cert->srcIdx += length;
-
- return 0;
-}
-#endif /* !NO_RSA */
-
-#ifdef HAVE_ECC
-
- /* return 0 on success if the ECC curve oid sum is supported */
- static int CheckCurve(word32 oid)
- {
- int ret = 0;
- word32 oidSz = 0;
-
- ret = wc_ecc_get_oid(oid, NULL, &oidSz);
- if (ret < 0 || oidSz == 0) {
- WOLFSSL_MSG("CheckCurve not found");
- ret = ALGO_ID_E;
- }
-
- return ret;
- }
-
-#endif /* HAVE_ECC */
-
-static int GetKey(DecodedCert* cert)
-{
- int length;
-#if !defined(NO_DSA) && defined(WOLFSSL_QT)
- int tmpLen;
-#endif
-#if defined(HAVE_ECC) || defined(HAVE_NTRU)
- int tmpIdx = cert->srcIdx;
-#endif
-
- if (GetSequence(cert->source, &cert->srcIdx, &length, cert->maxIdx) < 0)
- return ASN_PARSE_E;
-
-#if !defined(NO_DSA) && defined(WOLFSSL_QT)
- tmpLen = length + 4;
-#endif
-
- if (GetAlgoId(cert->source, &cert->srcIdx,
- &cert->keyOID, oidKeyType, cert->maxIdx) < 0)
- return ASN_PARSE_E;
-
- switch (cert->keyOID) {
- #ifndef NO_RSA
- case RSAk:
- {
- int ret;
-
- ret = CheckBitString(cert->source, &cert->srcIdx, &length,
- cert->maxIdx, 1, NULL);
- if (ret != 0)
- return ret;
-
- #ifdef HAVE_OCSP
- ret = CalcHashId(cert->source + cert->srcIdx, length,
- cert->subjectKeyHash);
- if (ret != 0)
- return ret;
- #endif
-
- return StoreRsaKey(cert, cert->srcIdx + length);
- }
-
- #endif /* NO_RSA */
- #ifdef HAVE_NTRU
- case NTRUk:
- {
- const byte* key = &cert->source[tmpIdx];
- byte* next = (byte*)key;
- word16 keyLen;
- word32 rc;
- word32 remaining = cert->maxIdx - cert->srcIdx;
- byte* publicKey;
-#ifdef WOLFSSL_SMALL_STACK
- byte* keyBlob = NULL;
-#else
- byte keyBlob[MAX_NTRU_KEY_SZ];
-#endif
- rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key,
- &keyLen, NULL, &next, &remaining);
- if (rc != NTRU_OK)
- return ASN_NTRU_KEY_E;
- if (keyLen > MAX_NTRU_KEY_SZ)
- return ASN_NTRU_KEY_E;
-
-#ifdef WOLFSSL_SMALL_STACK
- keyBlob = (byte*)XMALLOC(MAX_NTRU_KEY_SZ, cert->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (keyBlob == NULL)
- return MEMORY_E;
-#endif
-
- rc = ntru_crypto_ntru_encrypt_subjectPublicKeyInfo2PublicKey(key,
- &keyLen, keyBlob, &next, &remaining);
- if (rc != NTRU_OK) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(keyBlob, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return ASN_NTRU_KEY_E;
- }
-
- if ( (next - key) < 0) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(keyBlob, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return ASN_NTRU_KEY_E;
- }
-
- cert->srcIdx = tmpIdx + (int)(next - key);
-
- publicKey = (byte*)XMALLOC(keyLen, cert->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (publicKey == NULL) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(keyBlob, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return MEMORY_E;
- }
- XMEMCPY(publicKey, keyBlob, keyLen);
- cert->publicKey = publicKey;
- cert->pubKeyStored = 1;
- cert->pubKeySize = keyLen;
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(keyBlob, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return 0;
- }
- #endif /* HAVE_NTRU */
- #ifdef HAVE_ECC
- case ECDSAk:
- {
- int ret;
- byte seq[5];
- int pubLen = length + 1 + SetLength(length, seq);
- word32 localIdx;
- byte* publicKey;
- byte tag;
-
- localIdx = cert->srcIdx;
- if (GetASNTag(cert->source, &localIdx, &tag, cert->maxIdx) < 0)
- return ASN_PARSE_E;
-
- if (tag != (ASN_SEQUENCE | ASN_CONSTRUCTED)) {
- if (GetObjectId(cert->source, &cert->srcIdx,
- &cert->pkCurveOID, oidCurveType, cert->maxIdx) < 0)
- return ASN_PARSE_E;
-
- if (CheckCurve(cert->pkCurveOID) < 0)
- return ECC_CURVE_OID_E;
-
- /* key header */
- ret = CheckBitString(cert->source, &cert->srcIdx, &length,
- cert->maxIdx, 1, NULL);
- if (ret != 0)
- return ret;
- #ifdef HAVE_OCSP
- ret = CalcHashId(cert->source + cert->srcIdx, length,
- cert->subjectKeyHash);
- if (ret != 0)
- return ret;
- #endif
- }
-
- publicKey = (byte*)XMALLOC(pubLen, cert->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (publicKey == NULL)
- return MEMORY_E;
- XMEMCPY(publicKey, &cert->source[tmpIdx], pubLen);
- cert->publicKey = publicKey;
- cert->pubKeyStored = 1;
- cert->pubKeySize = pubLen;
-
- cert->srcIdx = tmpIdx + pubLen;
-
- return 0;
- }
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ED25519k:
- {
- byte* publicKey;
- int ret;
-
- cert->pkCurveOID = ED25519k;
-
- ret = CheckBitString(cert->source, &cert->srcIdx, &length,
- cert->maxIdx, 1, NULL);
- if (ret != 0)
- return ret;
-
- #ifdef HAVE_OCSP
- ret = CalcHashId(cert->source + cert->srcIdx, length,
- cert->subjectKeyHash);
- if (ret != 0)
- return ret;
- #endif
-
- publicKey = (byte*) XMALLOC(length, cert->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (publicKey == NULL)
- return MEMORY_E;
- XMEMCPY(publicKey, &cert->source[cert->srcIdx], length);
- cert->publicKey = publicKey;
- cert->pubKeyStored = 1;
- cert->pubKeySize = length;
-
- cert->srcIdx += length;
-
- return 0;
- }
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- case ED448k:
- {
- byte* publicKey;
- int ret;
-
- cert->pkCurveOID = ED448k;
-
- ret = CheckBitString(cert->source, &cert->srcIdx, &length,
- cert->maxIdx, 1, NULL);
- if (ret != 0)
- return ret;
-
- #ifdef HAVE_OCSP
- ret = CalcHashId(cert->source + cert->srcIdx, length,
- cert->subjectKeyHash);
- if (ret != 0)
- return ret;
- #endif
-
- publicKey = (byte*) XMALLOC(length, cert->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (publicKey == NULL)
- return MEMORY_E;
- XMEMCPY(publicKey, &cert->source[cert->srcIdx], length);
- cert->publicKey = publicKey;
- cert->pubKeyStored = 1;
- cert->pubKeySize = length;
-
- cert->srcIdx += length;
-
- return 0;
- }
- #endif /* HAVE_ED448 */
- #if !defined(NO_DSA) && defined(WOLFSSL_QT)
- case DSAk:
- {
- int ret;
- ret = GetSequence(cert->source, &cert->srcIdx, &length,
- cert->maxIdx);
- if (ret < 0)
- return ret;
-
- ret = SkipInt(cert->source, &cert->srcIdx, cert->maxIdx);
- if (ret != 0)
- return ret;
- ret = SkipInt(cert->source, &cert->srcIdx, cert->maxIdx);
- if (ret != 0)
- return ret;
- ret = SkipInt(cert->source, &cert->srcIdx, cert->maxIdx);
- if (ret != 0)
- return ret;
-
- ret = CheckBitString(cert->source, &cert->srcIdx, &length,
- cert->maxIdx, 1, NULL);
- if (ret != 0)
- return ret;
-
- ret = GetASNInt(cert->source, &cert->srcIdx, &length, cert->maxIdx);
- if (ret !=0)
- return ASN_PARSE_E;
-
- cert->publicKey = cert->source + tmpIdx;
- cert->pubKeySize = tmpLen;
- cert->srcIdx += length;
- return 0;
- }
- #endif /* NO_DSA && QT */
- default:
- return ASN_UNKNOWN_OID_E;
- }
-}
-
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
-#if defined(HAVE_ECC)
-/* Converts ECC curve enum values in ecc_curve_id to the associated OpenSSL NID
- value */
-WOLFSSL_API int EccEnumToNID(int n)
-{
- WOLFSSL_ENTER("EccEnumToNID()");
-
- switch(n) {
- case ECC_SECP192R1:
- return NID_X9_62_prime192v1;
- case ECC_PRIME192V2:
- return NID_X9_62_prime192v2;
- case ECC_PRIME192V3:
- return NID_X9_62_prime192v3;
- case ECC_PRIME239V1:
- return NID_X9_62_prime239v1;
- case ECC_PRIME239V2:
- return NID_X9_62_prime239v2;
- case ECC_PRIME239V3:
- return NID_X9_62_prime239v3;
- case ECC_SECP256R1:
- return NID_X9_62_prime256v1;
- case ECC_SECP112R1:
- return NID_secp112r1;
- case ECC_SECP112R2:
- return NID_secp112r2;
- case ECC_SECP128R1:
- return NID_secp128r1;
- case ECC_SECP128R2:
- return NID_secp128r2;
- case ECC_SECP160R1:
- return NID_secp160r1;
- case ECC_SECP160R2:
- return NID_secp160r2;
- case ECC_SECP224R1:
- return NID_secp224r1;
- case ECC_SECP384R1:
- return NID_secp384r1;
- case ECC_SECP521R1:
- return NID_secp521r1;
- case ECC_SECP160K1:
- return NID_secp160k1;
- case ECC_SECP192K1:
- return NID_secp192k1;
- case ECC_SECP224K1:
- return NID_secp224k1;
- case ECC_SECP256K1:
- return NID_secp256k1;
- case ECC_BRAINPOOLP160R1:
- return NID_brainpoolP160r1;
- case ECC_BRAINPOOLP192R1:
- return NID_brainpoolP192r1;
- case ECC_BRAINPOOLP224R1:
- return NID_brainpoolP224r1;
- case ECC_BRAINPOOLP256R1:
- return NID_brainpoolP256r1;
- case ECC_BRAINPOOLP320R1:
- return NID_brainpoolP320r1;
- case ECC_BRAINPOOLP384R1:
- return NID_brainpoolP384r1;
- case ECC_BRAINPOOLP512R1:
- return NID_brainpoolP512r1;
- default:
- WOLFSSL_MSG("NID not found");
- return -1;
- }
-}
-#endif /* HAVE_ECC */
-#endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
-
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
-int wc_OBJ_sn2nid(const char *sn)
-{
- const struct {
- const char *sn;
- int nid;
- } sn2nid[] = {
- {WOLFSSL_COMMON_NAME, NID_commonName},
- {WOLFSSL_COUNTRY_NAME, NID_countryName},
- {WOLFSSL_LOCALITY_NAME, NID_localityName},
- {WOLFSSL_STATE_NAME, NID_stateOrProvinceName},
- {WOLFSSL_ORG_NAME, NID_organizationName},
- {WOLFSSL_ORGUNIT_NAME, NID_organizationalUnitName},
- {WOLFSSL_EMAIL_ADDR, NID_emailAddress},
- {NULL, -1}};
-
- int i;
- #ifdef HAVE_ECC
- int eccEnum;
- #endif
- WOLFSSL_ENTER("OBJ_sn2nid");
- for(i=0; sn2nid[i].sn != NULL; i++) {
- if(XSTRNCMP(sn, sn2nid[i].sn, XSTRLEN(sn2nid[i].sn)) == 0) {
- return sn2nid[i].nid;
- }
- }
- #ifdef HAVE_ECC
- /* Nginx uses this OpenSSL string. */
- if (XSTRNCMP(sn, "prime256v1", 10) == 0)
- sn = "SECP256R1";
- if (XSTRNCMP(sn, "secp384r1", 10) == 0)
- sn = "SECP384R1";
- /* find based on name and return NID */
- for (i = 0; ecc_sets[i].size != 0 && ecc_sets[i].name != NULL; i++) {
- if (XSTRNCMP(sn, ecc_sets[i].name, ECC_MAXNAME) == 0) {
- eccEnum = ecc_sets[i].id;
- /* Convert enum value in ecc_curve_id to OpenSSL NID */
- return EccEnumToNID(eccEnum);
- }
- }
- #endif
-
- return NID_undef;
-}
-#endif
-
-/* Routine for calculating hashId */
-int CalcHashId(const byte* data, word32 len, byte* hash)
-{
- int ret;
-
-#ifdef WOLF_CRYPTO_CB
- /* try to use a registered crypto callback */
- ret = wc_CryptoCb_Sha256Hash(NULL, data, len, hash);
- if (ret != CRYPTOCB_UNAVAILABLE)
- return ret;
- /* fall-through when unavailable */
-#endif
-
-#if defined(NO_SHA) && !defined(NO_SHA256)
- ret = wc_Sha256Hash(data, len, hash);
-#elif !defined(NO_SHA)
- ret = wc_ShaHash(data, len, hash);
-#else
- ret = NOT_COMPILED_IN;
-#endif
-
- return ret;
-}
-
-/* process NAME, either issuer or subject */
-static int GetName(DecodedCert* cert, int nameType, int maxIdx)
-{
- int length; /* length of all distinguished names */
- int dummy;
- int ret;
- char* full;
- byte* hash;
- word32 idx, localIdx = 0;
- byte tag;
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- DecodedName* dName =
- (nameType == ISSUER) ? &cert->issuerName : &cert->subjectName;
- int dcnum = 0;
- #ifdef OPENSSL_EXTRA
- int count = 0;
- #endif
- #endif /* OPENSSL_EXTRA */
-
- WOLFSSL_MSG("Getting Cert Name");
-
- if (nameType == ISSUER) {
- full = cert->issuer;
- hash = cert->issuerHash;
- }
- else {
- full = cert->subject;
- hash = cert->subjectHash;
- }
-
- if (cert->srcIdx >= (word32)maxIdx) {
- return BUFFER_E;
- }
-
- localIdx = cert->srcIdx;
- if (GetASNTag(cert->source, &localIdx, &tag, maxIdx) < 0) {
- return ASN_PARSE_E;
- }
-
- if (tag == ASN_OBJECT_ID) {
- WOLFSSL_MSG("Trying optional prefix...");
-
- if (SkipObjectId(cert->source, &cert->srcIdx, maxIdx) < 0)
- return ASN_PARSE_E;
- WOLFSSL_MSG("Got optional prefix");
- }
-
- /* For OCSP, RFC2560 section 4.1.1 states the issuer hash should be
- * calculated over the entire DER encoding of the Name field, including
- * the tag and length. */
- idx = cert->srcIdx;
- if (GetSequence(cert->source, &cert->srcIdx, &length, maxIdx) < 0)
- return ASN_PARSE_E;
-
- ret = CalcHashId(&cert->source[idx], length + cert->srcIdx - idx, hash);
- if (ret != 0)
- return ret;
-
- length += cert->srcIdx;
- idx = 0;
-
-#if defined(HAVE_PKCS7) || defined(WOLFSSL_CERT_EXT)
- /* store pointer to raw issuer */
- if (nameType == ISSUER) {
- cert->issuerRaw = &cert->source[cert->srcIdx];
- cert->issuerRawLen = length - cert->srcIdx;
- }
-#endif
-#ifndef IGNORE_NAME_CONSTRAINTS
- if (nameType == SUBJECT) {
- cert->subjectRaw = &cert->source[cert->srcIdx];
- cert->subjectRawLen = length - cert->srcIdx;
- }
-#endif
-
- while (cert->srcIdx < (word32)length) {
- byte b = 0;
- byte joint[3];
- byte tooBig = FALSE;
- int oidSz;
- const char* copy = NULL;
- int copyLen = 0;
- int strLen = 0;
- byte id = 0;
-
- if (GetSet(cert->source, &cert->srcIdx, &dummy, maxIdx) < 0) {
- WOLFSSL_MSG("Cert name lacks set header, trying sequence");
- }
-
- if (GetSequence(cert->source, &cert->srcIdx, &dummy, maxIdx) <= 0)
- return ASN_PARSE_E;
-
- ret = GetASNObjectId(cert->source, &cert->srcIdx, &oidSz, maxIdx);
- if (ret != 0)
- return ret;
-
- /* make sure there is room for joint */
- if ((cert->srcIdx + sizeof(joint)) > (word32)maxIdx)
- return ASN_PARSE_E;
-
- XMEMCPY(joint, &cert->source[cert->srcIdx], sizeof(joint));
-
- /* v1 name types */
- if (joint[0] == 0x55 && joint[1] == 0x04) {
- cert->srcIdx += 3;
- id = joint[2];
- if (GetHeader(cert->source, &b, &cert->srcIdx, &strLen,
- maxIdx, 1) < 0) {
- return ASN_PARSE_E;
- }
-
- if (id == ASN_COMMON_NAME) {
- if (nameType == SUBJECT) {
- cert->subjectCN = (char *)&cert->source[cert->srcIdx];
- cert->subjectCNLen = strLen;
- cert->subjectCNEnc = b;
- }
-
- copy = WOLFSSL_COMMON_NAME;
- copyLen = sizeof(WOLFSSL_COMMON_NAME) - 1;
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->cnIdx = cert->srcIdx;
- dName->cnLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
- else if (id == ASN_SUR_NAME) {
- copy = WOLFSSL_SUR_NAME;
- copyLen = sizeof(WOLFSSL_SUR_NAME) - 1;
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectSN = (char*)&cert->source[cert->srcIdx];
- cert->subjectSNLen = strLen;
- cert->subjectSNEnc = b;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->snIdx = cert->srcIdx;
- dName->snLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
- else if (id == ASN_COUNTRY_NAME) {
- copy = WOLFSSL_COUNTRY_NAME;
- copyLen = sizeof(WOLFSSL_COUNTRY_NAME) - 1;
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectC = (char*)&cert->source[cert->srcIdx];
- cert->subjectCLen = strLen;
- cert->subjectCEnc = b;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->cIdx = cert->srcIdx;
- dName->cLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
- else if (id == ASN_LOCALITY_NAME) {
- copy = WOLFSSL_LOCALITY_NAME;
- copyLen = sizeof(WOLFSSL_LOCALITY_NAME) - 1;
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectL = (char*)&cert->source[cert->srcIdx];
- cert->subjectLLen = strLen;
- cert->subjectLEnc = b;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->lIdx = cert->srcIdx;
- dName->lLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
- else if (id == ASN_STATE_NAME) {
- copy = WOLFSSL_STATE_NAME;
- copyLen = sizeof(WOLFSSL_STATE_NAME) - 1;
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectST = (char*)&cert->source[cert->srcIdx];
- cert->subjectSTLen = strLen;
- cert->subjectSTEnc = b;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->stIdx = cert->srcIdx;
- dName->stLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
- else if (id == ASN_ORG_NAME) {
- copy = WOLFSSL_ORG_NAME;
- copyLen = sizeof(WOLFSSL_ORG_NAME) - 1;
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectO = (char*)&cert->source[cert->srcIdx];
- cert->subjectOLen = strLen;
- cert->subjectOEnc = b;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->oIdx = cert->srcIdx;
- dName->oLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
- else if (id == ASN_ORGUNIT_NAME) {
- copy = WOLFSSL_ORGUNIT_NAME;
- copyLen = sizeof(WOLFSSL_ORGUNIT_NAME) - 1;
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectOU = (char*)&cert->source[cert->srcIdx];
- cert->subjectOULen = strLen;
- cert->subjectOUEnc = b;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->ouIdx = cert->srcIdx;
- dName->ouLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
- else if (id == ASN_SERIAL_NUMBER) {
- copy = WOLFSSL_SERIAL_NUMBER;
- copyLen = sizeof(WOLFSSL_SERIAL_NUMBER) - 1;
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectSND = (char*)&cert->source[cert->srcIdx];
- cert->subjectSNDLen = strLen;
- cert->subjectSNDEnc = b;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->snIdx = cert->srcIdx;
- dName->snLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
- #ifdef WOLFSSL_CERT_EXT
- else if (id == ASN_BUS_CAT) {
- copy = WOLFSSL_BUS_CAT;
- copyLen = sizeof(WOLFSSL_BUS_CAT) - 1;
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectBC = (char*)&cert->source[cert->srcIdx];
- cert->subjectBCLen = strLen;
- cert->subjectBCEnc = b;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->bcIdx = cert->srcIdx;
- dName->bcLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
- #endif /* WOLFSSL_CERT_EXT */
- }
- #ifdef WOLFSSL_CERT_EXT
- else if ((cert->srcIdx + ASN_JOI_PREFIX_SZ + 2 <= (word32)maxIdx) &&
- (0 == XMEMCMP(&cert->source[cert->srcIdx], ASN_JOI_PREFIX,
- ASN_JOI_PREFIX_SZ)) &&
- ((cert->source[cert->srcIdx+ASN_JOI_PREFIX_SZ] == ASN_JOI_C) ||
- (cert->source[cert->srcIdx+ASN_JOI_PREFIX_SZ] == ASN_JOI_ST)))
- {
- cert->srcIdx += ASN_JOI_PREFIX_SZ;
- id = cert->source[cert->srcIdx++];
- b = cert->source[cert->srcIdx++]; /* encoding */
-
- if (GetLength(cert->source, &cert->srcIdx, &strLen,
- maxIdx) < 0)
- return ASN_PARSE_E;
-
- /* Check for jurisdiction of incorporation country name */
- if (id == ASN_JOI_C) {
- copy = WOLFSSL_JOI_C;
- copyLen = sizeof(WOLFSSL_JOI_C) - 1;
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectJC = (char*)&cert->source[cert->srcIdx];
- cert->subjectJCLen = strLen;
- cert->subjectJCEnc = b;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->jcIdx = cert->srcIdx;
- dName->jcLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
-
- /* Check for jurisdiction of incorporation state name */
- else if (id == ASN_JOI_ST) {
- copy = WOLFSSL_JOI_ST;
- copyLen = sizeof(WOLFSSL_JOI_ST) - 1;
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectJS = (char*)&cert->source[cert->srcIdx];
- cert->subjectJSLen = strLen;
- cert->subjectJSEnc = b;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->jsIdx = cert->srcIdx;
- dName->jsLen = strLen;
- #endif /* OPENSSL_EXTRA */
- }
-
- if ((strLen + copyLen) > (int)(ASN_NAME_MAX - idx)) {
- WOLFSSL_MSG("ASN Name too big, skipping");
- tooBig = TRUE;
- }
- }
- #endif /* WOLFSSL_CERT_EXT */
- else {
- /* skip */
- byte email = FALSE;
- byte pilot = FALSE;
-
- if (joint[0] == 0x2a && joint[1] == 0x86) { /* email id hdr */
- id = ASN_EMAIL_NAME;
- email = TRUE;
- }
-
- if (joint[0] == 0x9 && joint[1] == 0x92) { /* uid id hdr */
- /* last value of OID is the type of pilot attribute */
- id = cert->source[cert->srcIdx + oidSz - 1];
- pilot = TRUE;
- }
-
- cert->srcIdx += oidSz + 1;
-
- if (GetLength(cert->source, &cert->srcIdx, &strLen, maxIdx) < 0)
- return ASN_PARSE_E;
-
- if (strLen > (int)(ASN_NAME_MAX - idx)) {
- WOLFSSL_MSG("ASN name too big, skipping");
- tooBig = TRUE;
- }
-
- if (email) {
- copyLen = sizeof(WOLFSSL_EMAIL_ADDR) - 1;
- if ((copyLen + strLen) > (int)(ASN_NAME_MAX - idx)) {
- WOLFSSL_MSG("ASN name too big, skipping");
- tooBig = TRUE;
- }
- else {
- copy = WOLFSSL_EMAIL_ADDR;
- }
-
- #ifdef WOLFSSL_CERT_GEN
- if (nameType == SUBJECT) {
- cert->subjectEmail = (char*)&cert->source[cert->srcIdx];
- cert->subjectEmailLen = strLen;
- }
- #endif /* WOLFSSL_CERT_GEN */
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- dName->emailIdx = cert->srcIdx;
- dName->emailLen = strLen;
- #endif /* OPENSSL_EXTRA */
- #ifndef IGNORE_NAME_CONSTRAINTS
- {
- DNS_entry* emailName;
-
- emailName = (DNS_entry*)XMALLOC(sizeof(DNS_entry),
- cert->heap, DYNAMIC_TYPE_ALTNAME);
- if (emailName == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- return MEMORY_E;
- }
- emailName->type = 0;
- emailName->name = (char*)XMALLOC(strLen + 1,
- cert->heap, DYNAMIC_TYPE_ALTNAME);
- if (emailName->name == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- XFREE(emailName, cert->heap, DYNAMIC_TYPE_ALTNAME);
- return MEMORY_E;
- }
- emailName->len = strLen;
- XMEMCPY(emailName->name, &cert->source[cert->srcIdx],
- strLen);
- emailName->name[strLen] = '\0';
-
- emailName->next = cert->altEmailNames;
- cert->altEmailNames = emailName;
- }
- #endif /* IGNORE_NAME_CONSTRAINTS */
- }
-
- if (pilot) {
- switch (id) {
- case ASN_USER_ID:
- copy = WOLFSSL_USER_ID;
- copyLen = sizeof(WOLFSSL_USER_ID) - 1;
- #if defined(OPENSSL_EXTRA) || \
- defined(OPENSSL_EXTRA_X509_SMALL)
- dName->uidIdx = cert->srcIdx;
- dName->uidLen = strLen;
- #endif /* OPENSSL_EXTRA */
- break;
-
- case ASN_DOMAIN_COMPONENT:
- copy = WOLFSSL_DOMAIN_COMPONENT;
- copyLen = sizeof(WOLFSSL_DOMAIN_COMPONENT) - 1;
- #if defined(OPENSSL_EXTRA) || \
- defined(OPENSSL_EXTRA_X509_SMALL)
- dName->dcIdx[dcnum] = cert->srcIdx;
- dName->dcLen[dcnum] = strLen;
- dName->dcNum = dcnum + 1;
- dcnum++;
- #endif /* OPENSSL_EXTRA */
- break;
-
- default:
- WOLFSSL_MSG("Unknown pilot attribute type");
- return ASN_PARSE_E;
- }
- }
- }
- if ((copyLen + strLen) > (int)(ASN_NAME_MAX - idx))
- {
- WOLFSSL_MSG("ASN Name too big, skipping");
- tooBig = TRUE;
- }
- if ((copy != NULL) && !tooBig) {
- XMEMCPY(&full[idx], copy, copyLen);
- idx += copyLen;
- XMEMCPY(&full[idx], &cert->source[cert->srcIdx], strLen);
- idx += strLen;
-
- #ifdef OPENSSL_EXTRA
- if (count < DOMAIN_COMPONENT_MAX) {
- /* store order that DN was parsed */
- dName->loc[count++] = id;
- }
- #endif
- }
- cert->srcIdx += strLen;
- }
- full[idx++] = 0;
-#if defined(OPENSSL_EXTRA)
- /* store order that DN was parsed */
- dName->locSz = count;
-#endif
-
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- {
- int totalLen = 0;
- int i = 0;
-
- if (dName->cnLen != 0)
- totalLen += dName->cnLen + 4;
- if (dName->snLen != 0)
- totalLen += dName->snLen + 4;
- if (dName->cLen != 0)
- totalLen += dName->cLen + 3;
- if (dName->lLen != 0)
- totalLen += dName->lLen + 3;
- if (dName->stLen != 0)
- totalLen += dName->stLen + 4;
- if (dName->oLen != 0)
- totalLen += dName->oLen + 3;
- if (dName->ouLen != 0)
- totalLen += dName->ouLen + 4;
- if (dName->emailLen != 0)
- totalLen += dName->emailLen + 14;
- if (dName->uidLen != 0)
- totalLen += dName->uidLen + 5;
- if (dName->serialLen != 0)
- totalLen += dName->serialLen + 14;
- if (dName->dcNum != 0){
- for (i = 0;i < dName->dcNum;i++)
- totalLen += dName->dcLen[i] + 4;
- }
-
- dName->fullName = (char*)XMALLOC(totalLen + 1, cert->heap,
- DYNAMIC_TYPE_X509);
- if (dName->fullName != NULL) {
- idx = 0;
-
- if (dName->cnLen != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], WOLFSSL_COMMON_NAME, 4);
- dName->cnNid = wc_OBJ_sn2nid((const char *)WOLFSSL_COMMON_NAME);
- idx += 4;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->cnIdx], dName->cnLen);
- dName->cnIdx = idx;
- idx += dName->cnLen;
- }
- if (dName->snLen != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], WOLFSSL_SUR_NAME, 4);
- dName->snNid = wc_OBJ_sn2nid((const char *)WOLFSSL_SUR_NAME);
- idx += 4;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->snIdx], dName->snLen);
- dName->snIdx = idx;
- idx += dName->snLen;
- }
- if (dName->cLen != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], WOLFSSL_COUNTRY_NAME, 3);
- dName->cNid = wc_OBJ_sn2nid((const char *)WOLFSSL_COUNTRY_NAME);
- idx += 3;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->cIdx], dName->cLen);
- dName->cIdx = idx;
- idx += dName->cLen;
- }
- if (dName->lLen != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], WOLFSSL_LOCALITY_NAME, 3);
- dName->lNid = wc_OBJ_sn2nid((const char *)WOLFSSL_LOCALITY_NAME);
- idx += 3;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->lIdx], dName->lLen);
- dName->lIdx = idx;
- idx += dName->lLen;
- }
- if (dName->stLen != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], WOLFSSL_STATE_NAME, 4);
- dName->stNid = wc_OBJ_sn2nid((const char *)WOLFSSL_STATE_NAME);
- idx += 4;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->stIdx], dName->stLen);
- dName->stIdx = idx;
- idx += dName->stLen;
- }
- if (dName->oLen != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], WOLFSSL_ORG_NAME, 3);
- dName->oNid = wc_OBJ_sn2nid((const char *)WOLFSSL_ORG_NAME);
- idx += 3;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->oIdx], dName->oLen);
- dName->oIdx = idx;
- idx += dName->oLen;
- }
- if (dName->ouLen != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], WOLFSSL_ORGUNIT_NAME, 4);
- dName->ouNid = wc_OBJ_sn2nid((const char *)WOLFSSL_ORGUNIT_NAME);
- idx += 4;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->ouIdx], dName->ouLen);
- dName->ouIdx = idx;
- idx += dName->ouLen;
- }
- if (dName->emailLen != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], "/emailAddress=", 14);
- dName->emailNid = wc_OBJ_sn2nid((const char *)"/emailAddress=");
- idx += 14;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->emailIdx], dName->emailLen);
- dName->emailIdx = idx;
- idx += dName->emailLen;
- }
- for (i = 0;i < dName->dcNum;i++){
- if (dName->dcLen[i] != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], WOLFSSL_DOMAIN_COMPONENT, 4);
- idx += 4;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->dcIdx[i]], dName->dcLen[i]);
- dName->dcIdx[i] = idx;
- idx += dName->dcLen[i];
- }
- }
- if (dName->uidLen != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], "/UID=", 5);
- dName->uidNid = wc_OBJ_sn2nid((const char *)"/UID=");
- idx += 5;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->uidIdx], dName->uidLen);
- dName->uidIdx = idx;
- idx += dName->uidLen;
- }
- if (dName->serialLen != 0) {
- dName->entryCount++;
- XMEMCPY(&dName->fullName[idx], WOLFSSL_SERIAL_NUMBER, 14);
- dName->serialNid = wc_OBJ_sn2nid((const char *)WOLFSSL_SERIAL_NUMBER);
- idx += 14;
- XMEMCPY(&dName->fullName[idx],
- &cert->source[dName->serialIdx], dName->serialLen);
- dName->serialIdx = idx;
- idx += dName->serialLen;
- }
- dName->fullName[idx] = '\0';
- dName->fullNameLen = totalLen;
- }
- }
- #endif /* OPENSSL_EXTRA */
-
- return 0;
-}
-
-
-#ifndef NO_ASN_TIME
-
-/* two byte date/time, add to value */
-static WC_INLINE int GetTime(int* value, const byte* date, int* idx)
-{
- int i = *idx;
-
- if (date[i] < 0x30 || date[i] > 0x39 || date[i+1] < 0x30 ||
- date[i+1] > 0x39) {
- return ASN_PARSE_E;
- }
-
- *value += btoi(date[i++]) * 10;
- *value += btoi(date[i++]);
-
- *idx = i;
-
- return 0;
-}
-
-int ExtractDate(const unsigned char* date, unsigned char format,
- struct tm* certTime, int* idx)
-{
- XMEMSET(certTime, 0, sizeof(struct tm));
-
- if (format == ASN_UTC_TIME) {
- if (btoi(date[*idx]) >= 5)
- certTime->tm_year = 1900;
- else
- certTime->tm_year = 2000;
- }
- else { /* format == GENERALIZED_TIME */
- if (GetTime(&certTime->tm_year, date, idx) != 0) return 0;
- certTime->tm_year *= 100;
- }
-
- /* adjust tm_year, tm_mon */
- if (GetTime(&certTime->tm_year, date, idx) != 0) return 0;
- certTime->tm_year -= 1900;
- if (GetTime(&certTime->tm_mon , date, idx) != 0) return 0;
- certTime->tm_mon -= 1;
- if (GetTime(&certTime->tm_mday, date, idx) != 0) return 0;
- if (GetTime(&certTime->tm_hour, date, idx) != 0) return 0;
- if (GetTime(&certTime->tm_min , date, idx) != 0) return 0;
- if (GetTime(&certTime->tm_sec , date, idx) != 0) return 0;
-
- return 1;
-}
-
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_MYSQL_COMPATIBLE) || \
- defined(OPENSSL_EXTRA) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
-int GetTimeString(byte* date, int format, char* buf, int len)
-{
- struct tm t;
- int idx = 0;
-
- if (!ExtractDate(date, (unsigned char)format, &t, &idx)) {
- return 0;
- }
-
- if (date[idx] != 'Z') {
- WOLFSSL_MSG("UTCtime, not Zulu") ;
- return 0;
- }
-
- /* place month in buffer */
- buf[0] = '\0';
- switch(t.tm_mon) {
- case 0: XSTRNCAT(buf, "Jan ", 5); break;
- case 1: XSTRNCAT(buf, "Feb ", 5); break;
- case 2: XSTRNCAT(buf, "Mar ", 5); break;
- case 3: XSTRNCAT(buf, "Apr ", 5); break;
- case 4: XSTRNCAT(buf, "May ", 5); break;
- case 5: XSTRNCAT(buf, "Jun ", 5); break;
- case 6: XSTRNCAT(buf, "Jul ", 5); break;
- case 7: XSTRNCAT(buf, "Aug ", 5); break;
- case 8: XSTRNCAT(buf, "Sep ", 5); break;
- case 9: XSTRNCAT(buf, "Oct ", 5); break;
- case 10: XSTRNCAT(buf, "Nov ", 5); break;
- case 11: XSTRNCAT(buf, "Dec ", 5); break;
- default:
- return 0;
-
- }
- idx = 4; /* use idx now for char buffer */
-
- XSNPRINTF(buf + idx, len - idx, "%2d %02d:%02d:%02d %d GMT",
- t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, t.tm_year + 1900);
-
- return 1;
-}
-#endif /* OPENSSL_ALL || WOLFSSL_MYSQL_COMPATIBLE || WOLFSSL_NGINX || WOLFSSL_HAPROXY */
-
-
-#if !defined(NO_ASN_TIME) && defined(HAVE_PKCS7)
-
-/* Set current time string, either UTC or GeneralizedTime.
- * (void*) tm should be a pointer to time_t, output is placed in buf.
- *
- * Return time string length placed in buf on success, negative on error */
-int GetAsnTimeString(void* currTime, byte* buf, word32 len)
-{
- struct tm* ts = NULL;
- struct tm* tmpTime = NULL;
-#if defined(NEED_TMP_TIME)
- struct tm tmpTimeStorage;
- tmpTime = &tmpTimeStorage;
-#else
- (void)tmpTime;
-#endif
- byte* data_ptr = buf;
- word32 data_len = 0;
- int year, mon, day, hour, mini, sec;
-
- WOLFSSL_ENTER("SetAsnTimeString");
-
- if (buf == NULL || len == 0)
- return BAD_FUNC_ARG;
-
- ts = (struct tm *)XGMTIME((time_t*)currTime, tmpTime);
- if (ts == NULL){
- WOLFSSL_MSG("failed to get time data.");
- return ASN_TIME_E;
- }
-
- /* Note ASN_UTC_TIME_SIZE and ASN_GENERALIZED_TIME_SIZE include space for
- * the null terminator. ASN encoded values leave off the terminator. */
-
- if (ts->tm_year >= 50 && ts->tm_year < 150) {
- /* UTC Time */
- char utc_str[ASN_UTC_TIME_SIZE];
- data_len = ASN_UTC_TIME_SIZE - 1 + 2;
-
- if (len < data_len)
- return BUFFER_E;
-
- if (ts->tm_year >= 50 && ts->tm_year < 100) {
- year = ts->tm_year;
- } else if (ts->tm_year >= 100 && ts->tm_year < 150) {
- year = ts->tm_year - 100;
- }
- else {
- WOLFSSL_MSG("unsupported year range");
- return BAD_FUNC_ARG;
- }
- mon = ts->tm_mon + 1;
- day = ts->tm_mday;
- hour = ts->tm_hour;
- mini = ts->tm_min;
- sec = ts->tm_sec;
- XSNPRINTF((char *)utc_str, ASN_UTC_TIME_SIZE,
- "%02d%02d%02d%02d%02d%02dZ", year, mon, day, hour, mini, sec);
- *data_ptr = (byte) ASN_UTC_TIME; data_ptr++;
- /* -1 below excludes null terminator */
- *data_ptr = (byte) ASN_UTC_TIME_SIZE - 1; data_ptr++;
- XMEMCPY(data_ptr,(byte *)utc_str, ASN_UTC_TIME_SIZE - 1);
-
- } else {
- /* GeneralizedTime */
- char gt_str[ASN_GENERALIZED_TIME_SIZE];
- data_len = ASN_GENERALIZED_TIME_SIZE - 1 + 2;
-
- if (len < data_len)
- return BUFFER_E;
-
- year = ts->tm_year + 1900;
- mon = ts->tm_mon + 1;
- day = ts->tm_mday;
- hour = ts->tm_hour;
- mini = ts->tm_min;
- sec = ts->tm_sec;
- XSNPRINTF((char *)gt_str, ASN_GENERALIZED_TIME_SIZE,
- "%4d%02d%02d%02d%02d%02dZ", year, mon, day, hour, mini, sec);
- *data_ptr = (byte) ASN_GENERALIZED_TIME; data_ptr++;
- /* -1 below excludes null terminator */
- *data_ptr = (byte) ASN_GENERALIZED_TIME_SIZE - 1; data_ptr++;
- XMEMCPY(data_ptr,(byte *)gt_str, ASN_GENERALIZED_TIME_SIZE - 1);
- }
-
- return data_len;
-}
-
-#endif /* !NO_ASN_TIME && HAVE_PKCS7 */
-
-
-#if defined(USE_WOLF_VALIDDATE)
-
-/* to the second */
-int DateGreaterThan(const struct tm* a, const struct tm* b)
-{
- if (a->tm_year > b->tm_year)
- return 1;
-
- if (a->tm_year == b->tm_year && a->tm_mon > b->tm_mon)
- return 1;
-
- if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
- a->tm_mday > b->tm_mday)
- return 1;
-
- if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
- a->tm_mday == b->tm_mday && a->tm_hour > b->tm_hour)
- return 1;
-
- if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
- a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour &&
- a->tm_min > b->tm_min)
- return 1;
-
- if (a->tm_year == b->tm_year && a->tm_mon == b->tm_mon &&
- a->tm_mday == b->tm_mday && a->tm_hour == b->tm_hour &&
- a->tm_min == b->tm_min && a->tm_sec > b->tm_sec)
- return 1;
-
- return 0; /* false */
-}
-
-
-static WC_INLINE int DateLessThan(const struct tm* a, const struct tm* b)
-{
- return DateGreaterThan(b,a);
-}
-
-/* like atoi but only use first byte */
-/* Make sure before and after dates are valid */
-int ValidateDate(const byte* date, byte format, int dateType)
-{
- time_t ltime;
- struct tm certTime;
- struct tm* localTime;
- struct tm* tmpTime;
- int i = 0;
- int timeDiff = 0 ;
- int diffHH = 0 ; int diffMM = 0 ;
- int diffSign = 0 ;
-
-#if defined(NEED_TMP_TIME)
- struct tm tmpTimeStorage;
- tmpTime = &tmpTimeStorage;
-#else
- tmpTime = NULL;
-#endif
- (void)tmpTime;
-
- ltime = XTIME(0);
-
-#ifdef WOLFSSL_BEFORE_DATE_CLOCK_SKEW
- if (dateType == BEFORE) {
- WOLFSSL_MSG("Skewing local time for before date check");
- ltime += WOLFSSL_BEFORE_DATE_CLOCK_SKEW;
- }
-#endif
-
-#ifdef WOLFSSL_AFTER_DATE_CLOCK_SKEW
- if (dateType == AFTER) {
- WOLFSSL_MSG("Skewing local time for after date check");
- ltime -= WOLFSSL_AFTER_DATE_CLOCK_SKEW;
- }
-#endif
-
- if (!ExtractDate(date, format, &certTime, &i)) {
- WOLFSSL_MSG("Error extracting the date");
- return 0;
- }
-
- if ((date[i] == '+') || (date[i] == '-')) {
- WOLFSSL_MSG("Using time differential, not Zulu") ;
- diffSign = date[i++] == '+' ? 1 : -1 ;
- if (GetTime(&diffHH, date, &i) != 0)
- return 0;
- if (GetTime(&diffMM, date, &i) != 0)
- return 0;
- timeDiff = diffSign * (diffHH*60 + diffMM) * 60 ;
- } else if (date[i] != 'Z') {
- WOLFSSL_MSG("UTCtime, neither Zulu or time differential") ;
- return 0;
- }
-
- ltime -= (time_t)timeDiff ;
- localTime = XGMTIME(<ime, tmpTime);
-
- if (localTime == NULL) {
- WOLFSSL_MSG("XGMTIME failed");
- return 0;
- }
-
- if (dateType == BEFORE) {
- if (DateLessThan(localTime, &certTime)) {
- WOLFSSL_MSG("Date BEFORE check failed");
- return 0;
- }
- }
- else { /* dateType == AFTER */
- if (DateGreaterThan(localTime, &certTime)) {
- WOLFSSL_MSG("Date AFTER check failed");
- return 0;
- }
- }
-
- return 1;
-}
-#endif /* USE_WOLF_VALIDDATE */
-
-int wc_GetTime(void* timePtr, word32 timeSize)
-{
- time_t* ltime = (time_t*)timePtr;
-
- if (timePtr == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if ((word32)sizeof(time_t) > timeSize) {
- return BUFFER_E;
- }
-
- *ltime = XTIME(0);
-
- return 0;
-}
-
-#endif /* !NO_ASN_TIME */
-
-
-/* Get date buffer, format and length. Returns 0=success or error */
-static int GetDateInfo(const byte* source, word32* idx, const byte** pDate,
- byte* pFormat, int* pLength, word32 maxIdx)
-{
- int length;
- byte format;
-
- if (source == NULL || idx == NULL)
- return BAD_FUNC_ARG;
-
- /* get ASN format header */
- if (*idx+1 > maxIdx)
- return BUFFER_E;
- format = source[*idx];
- *idx += 1;
- if (format != ASN_UTC_TIME && format != ASN_GENERALIZED_TIME)
- return ASN_TIME_E;
-
- /* get length */
- if (GetLength(source, idx, &length, maxIdx) < 0)
- return ASN_PARSE_E;
- if (length > MAX_DATE_SIZE || length < MIN_DATE_SIZE)
- return ASN_DATE_SZ_E;
-
- /* return format, date and length */
- if (pFormat)
- *pFormat = format;
- if (pDate)
- *pDate = &source[*idx];
- if (pLength)
- *pLength = length;
-
- *idx += length;
-
- return 0;
-}
-
-static int GetDate(DecodedCert* cert, int dateType, int verify, int maxIdx)
-{
- int ret, length;
- const byte *datePtr = NULL;
- byte date[MAX_DATE_SIZE];
- byte format;
- word32 startIdx = 0;
-
- if (dateType == BEFORE)
- cert->beforeDate = &cert->source[cert->srcIdx];
- else
- cert->afterDate = &cert->source[cert->srcIdx];
- startIdx = cert->srcIdx;
-
- ret = GetDateInfo(cert->source, &cert->srcIdx, &datePtr, &format,
- &length, maxIdx);
- if (ret < 0)
- return ret;
-
- XMEMSET(date, 0, MAX_DATE_SIZE);
- XMEMCPY(date, datePtr, length);
-
- if (dateType == BEFORE)
- cert->beforeDateLen = cert->srcIdx - startIdx;
- else
- cert->afterDateLen = cert->srcIdx - startIdx;
-
-#ifndef NO_ASN_TIME
- if (verify != NO_VERIFY && verify != VERIFY_SKIP_DATE &&
- !XVALIDATE_DATE(date, format, dateType)) {
- if (dateType == BEFORE)
- return ASN_BEFORE_DATE_E;
- else
- return ASN_AFTER_DATE_E;
- }
-#else
- (void)verify;
-#endif
-
- return 0;
-}
-
-static int GetValidity(DecodedCert* cert, int verify, int maxIdx)
-{
- int length;
- int badDate = 0;
-
- if (GetSequence(cert->source, &cert->srcIdx, &length, maxIdx) < 0)
- return ASN_PARSE_E;
-
- maxIdx = cert->srcIdx + length;
-
- if (GetDate(cert, BEFORE, verify, maxIdx) < 0)
- badDate = ASN_BEFORE_DATE_E; /* continue parsing */
-
- if (GetDate(cert, AFTER, verify, maxIdx) < 0)
- return ASN_AFTER_DATE_E;
-
- if (badDate != 0)
- return badDate;
-
- return 0;
-}
-
-
-int wc_GetDateInfo(const byte* certDate, int certDateSz, const byte** date,
- byte* format, int* length)
-{
- int ret;
- word32 idx = 0;
-
- ret = GetDateInfo(certDate, &idx, date, format, length, certDateSz);
- if (ret < 0)
- return ret;
-
- return 0;
-}
-
-#ifndef NO_ASN_TIME
-int wc_GetDateAsCalendarTime(const byte* date, int length, byte format,
- struct tm* timearg)
-{
- int idx = 0;
- (void)length;
- if (!ExtractDate(date, format, timearg, &idx))
- return ASN_TIME_E;
- return 0;
-}
-
-#if defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES)
-int wc_GetCertDates(Cert* cert, struct tm* before, struct tm* after)
-{
- int ret = 0;
- const byte* date;
- byte format;
- int length;
-
- if (cert == NULL)
- return BAD_FUNC_ARG;
-
- if (before && cert->beforeDateSz > 0) {
- ret = wc_GetDateInfo(cert->beforeDate, cert->beforeDateSz, &date,
- &format, &length);
- if (ret == 0)
- ret = wc_GetDateAsCalendarTime(date, length, format, before);
- }
- if (after && cert->afterDateSz > 0) {
- ret = wc_GetDateInfo(cert->afterDate, cert->afterDateSz, &date,
- &format, &length);
- if (ret == 0)
- ret = wc_GetDateAsCalendarTime(date, length, format, after);
- }
-
- return ret;
-}
-#endif /* WOLFSSL_CERT_GEN && WOLFSSL_ALT_NAMES */
-#endif /* !NO_ASN_TIME */
-
-/* parses certificate up to point of X.509 public key
- *
- * if cert date is invalid then badDate gets set to error value, otherwise is 0
- *
- * returns a negative value on fail case
- */
-int wc_GetPubX509(DecodedCert* cert, int verify, int* badDate)
-{
- int ret;
-
- if (cert == NULL || badDate == NULL)
- return BAD_FUNC_ARG;
-
- *badDate = 0;
- if ( (ret = GetCertHeader(cert)) < 0)
- return ret;
-
- WOLFSSL_MSG("Got Cert Header");
-
- /* Using the sigIndex as the upper bound because that's where the
- * actual certificate data ends. */
- if ( (ret = GetAlgoId(cert->source, &cert->srcIdx, &cert->signatureOID,
- oidSigType, cert->sigIndex)) < 0)
- return ret;
-
- WOLFSSL_MSG("Got Algo ID");
-
- if ( (ret = GetName(cert, ISSUER, cert->sigIndex)) < 0)
- return ret;
-
- if ( (ret = GetValidity(cert, verify, cert->sigIndex)) < 0)
- *badDate = ret;
-
- if ( (ret = GetName(cert, SUBJECT, cert->sigIndex)) < 0)
- return ret;
-
- WOLFSSL_MSG("Got Subject Name");
- return ret;
-}
-
-int DecodeToKey(DecodedCert* cert, int verify)
-{
- int badDate = 0;
- int ret;
-
- if ( (ret = wc_GetPubX509(cert, verify, &badDate)) < 0)
- return ret;
-
- /* Determine if self signed */
- cert->selfSigned = XMEMCMP(cert->issuerHash,
- cert->subjectHash,
- KEYID_SIZE) == 0 ? 1 : 0;
-
- if ( (ret = GetKey(cert)) < 0)
- return ret;
-
- WOLFSSL_MSG("Got Key");
-
- if (badDate != 0)
- return badDate;
-
- return ret;
-}
-
-static int GetSignature(DecodedCert* cert)
-{
- int length;
- int ret;
- ret = CheckBitString(cert->source, &cert->srcIdx, &length, cert->maxIdx, 1,
- NULL);
- if (ret != 0)
- return ret;
-
- cert->sigLength = length;
- cert->signature = &cert->source[cert->srcIdx];
- cert->srcIdx += cert->sigLength;
-
- return 0;
-}
-
-static word32 SetOctetString8Bit(word32 len, byte* output)
-{
- output[0] = ASN_OCTET_STRING;
- output[1] = (byte)len;
- return 2;
-}
-
-static word32 SetDigest(const byte* digest, word32 digSz, byte* output)
-{
- word32 idx = SetOctetString8Bit(digSz, output);
- XMEMCPY(&output[idx], digest, digSz);
-
- return idx + digSz;
-}
-
-
-static word32 BytePrecision(word32 value)
-{
- word32 i;
- for (i = sizeof(value); i; --i)
- if (value >> ((i - 1) * WOLFSSL_BIT_SIZE))
- break;
-
- return i;
-}
-
-
-word32 SetLength(word32 length, byte* output)
-{
- word32 i = 0, j;
-
- if (length < ASN_LONG_LENGTH) {
- if (output)
- output[i] = (byte)length;
- i++;
- }
- else {
- if (output)
- output[i] = (byte)(BytePrecision(length) | ASN_LONG_LENGTH);
- i++;
-
- for (j = BytePrecision(length); j; --j) {
- if (output)
- output[i] = (byte)(length >> ((j - 1) * WOLFSSL_BIT_SIZE));
- i++;
- }
- }
-
- return i;
-}
-
-word32 SetSequence(word32 len, byte* output)
-{
- if (output)
- output[0] = ASN_SEQUENCE | ASN_CONSTRUCTED;
- return SetLength(len, output ? output + 1 : NULL) + 1;
-}
-
-word32 SetOctetString(word32 len, byte* output)
-{
- output[0] = ASN_OCTET_STRING;
- return SetLength(len, output + 1) + 1;
-}
-
-/* Write a set header to output */
-word32 SetSet(word32 len, byte* output)
-{
- output[0] = ASN_SET | ASN_CONSTRUCTED;
- return SetLength(len, output + 1) + 1;
-}
-
-word32 SetImplicit(byte tag, byte number, word32 len, byte* output)
-{
-
- output[0] = ((tag == ASN_SEQUENCE || tag == ASN_SET) ? ASN_CONSTRUCTED : 0)
- | ASN_CONTEXT_SPECIFIC | number;
- return SetLength(len, output + 1) + 1;
-}
-
-word32 SetExplicit(byte number, word32 len, byte* output)
-{
- output[0] = ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | number;
- return SetLength(len, output + 1) + 1;
-}
-
-
-#if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
-
-static int SetCurve(ecc_key* key, byte* output)
-{
-#ifdef HAVE_OID_ENCODING
- int ret;
-#endif
- int idx = 0;
- word32 oidSz = 0;
-
- /* validate key */
- if (key == NULL || key->dp == NULL) {
- return BAD_FUNC_ARG;
- }
-
-#ifdef HAVE_OID_ENCODING
- ret = EncodeObjectId(key->dp->oid, key->dp->oidSz, NULL, &oidSz);
- if (ret != 0) {
- return ret;
- }
-#else
- oidSz = key->dp->oidSz;
-#endif
-
- idx += SetObjectId(oidSz, output);
-
-#ifdef HAVE_OID_ENCODING
- ret = EncodeObjectId(key->dp->oid, key->dp->oidSz, output+idx, &oidSz);
- if (ret != 0) {
- return ret;
- }
-#else
- XMEMCPY(output+idx, key->dp->oid, oidSz);
-#endif
- idx += oidSz;
-
- return idx;
-}
-
-#endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT */
-
-
-#ifdef HAVE_ECC
-static WC_INLINE int IsSigAlgoECDSA(int algoOID)
-{
- /* ECDSA sigAlgo must not have ASN1 NULL parameters */
- if (algoOID == CTC_SHAwECDSA || algoOID == CTC_SHA256wECDSA ||
- algoOID == CTC_SHA384wECDSA || algoOID == CTC_SHA512wECDSA) {
- return 1;
- }
-
- return 0;
-}
-#endif
-
-word32 SetAlgoID(int algoOID, byte* output, int type, int curveSz)
-{
- word32 tagSz, idSz, seqSz, algoSz = 0;
- const byte* algoName = 0;
- byte ID_Length[1 + MAX_LENGTH_SZ];
- byte seqArray[MAX_SEQ_SZ + 1]; /* add object_id to end */
- int length = 0;
-
- tagSz = (type == oidHashType ||
- (type == oidSigType
- #ifdef HAVE_ECC
- && !IsSigAlgoECDSA(algoOID)
- #endif
- #ifdef HAVE_ED25519
- && algoOID != ED25519k
- #endif
- #ifdef HAVE_ED448
- && algoOID != ED448k
- #endif
- ) ||
- (type == oidKeyType && algoOID == RSAk)) ? 2 : 0;
-
- algoName = OidFromId(algoOID, type, &algoSz);
-
- if (algoName == NULL) {
- WOLFSSL_MSG("Unknown Algorithm");
- return 0;
- }
-
- idSz = SetObjectId(algoSz, ID_Length);
- seqSz = SetSequence(idSz + algoSz + tagSz + curveSz, seqArray);
-
- /* Copy only algo to output for DSA keys */
- if (algoOID == DSAk && output) {
- XMEMCPY(output, ID_Length, idSz);
- XMEMCPY(output + idSz, algoName, algoSz);
- if (tagSz == 2)
- SetASNNull(&output[seqSz + idSz + algoSz]);
- }
- else if (output) {
- XMEMCPY(output, seqArray, seqSz);
- XMEMCPY(output + seqSz, ID_Length, idSz);
- XMEMCPY(output + seqSz + idSz, algoName, algoSz);
- if (tagSz == 2)
- SetASNNull(&output[seqSz + idSz + algoSz]);
- }
-
- if (algoOID == DSAk)
- length = idSz + algoSz + tagSz;
- else
- length = seqSz + idSz + algoSz + tagSz;
-
- return length;
-}
-
-
-word32 wc_EncodeSignature(byte* out, const byte* digest, word32 digSz,
- int hashOID)
-{
- byte digArray[MAX_ENCODED_DIG_SZ];
- byte algoArray[MAX_ALGO_SZ];
- byte seqArray[MAX_SEQ_SZ];
- word32 encDigSz, algoSz, seqSz;
-
- encDigSz = SetDigest(digest, digSz, digArray);
- algoSz = SetAlgoID(hashOID, algoArray, oidHashType, 0);
- seqSz = SetSequence(encDigSz + algoSz, seqArray);
-
- XMEMCPY(out, seqArray, seqSz);
- XMEMCPY(out + seqSz, algoArray, algoSz);
- XMEMCPY(out + seqSz + algoSz, digArray, encDigSz);
-
- return encDigSz + algoSz + seqSz;
-}
-
-
-#ifndef NO_CERTS
-
-int wc_GetCTC_HashOID(int type)
-{
- int ret;
- enum wc_HashType hType;
-
- hType = wc_HashTypeConvert(type);
- ret = wc_HashGetOID(hType);
- if (ret < 0)
- ret = 0; /* backwards compatibility */
-
- return ret;
-}
-
-void InitSignatureCtx(SignatureCtx* sigCtx, void* heap, int devId)
-{
- if (sigCtx) {
- XMEMSET(sigCtx, 0, sizeof(SignatureCtx));
- sigCtx->devId = devId;
- sigCtx->heap = heap;
- }
-}
-
-void FreeSignatureCtx(SignatureCtx* sigCtx)
-{
- if (sigCtx == NULL)
- return;
-
- if (sigCtx->digest) {
- XFREE(sigCtx->digest, sigCtx->heap, DYNAMIC_TYPE_DIGEST);
- sigCtx->digest = NULL;
- }
-#ifndef NO_RSA
- if (sigCtx->plain) {
- XFREE(sigCtx->plain, sigCtx->heap, DYNAMIC_TYPE_SIGNATURE);
- sigCtx->plain = NULL;
- }
-#endif
-#ifndef NO_ASN_CRYPT
- if (sigCtx->key.ptr) {
- switch (sigCtx->keyOID) {
- #ifndef NO_RSA
- case RSAk:
- wc_FreeRsaKey(sigCtx->key.rsa);
- XFREE(sigCtx->key.ptr, sigCtx->heap, DYNAMIC_TYPE_RSA);
- break;
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ECDSAk:
- wc_ecc_free(sigCtx->key.ecc);
- XFREE(sigCtx->key.ecc, sigCtx->heap, DYNAMIC_TYPE_ECC);
- break;
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ED25519k:
- wc_ed25519_free(sigCtx->key.ed25519);
- XFREE(sigCtx->key.ed25519, sigCtx->heap, DYNAMIC_TYPE_ED25519);
- break;
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- case ED448k:
- wc_ed448_free(sigCtx->key.ed448);
- XFREE(sigCtx->key.ed448, sigCtx->heap, DYNAMIC_TYPE_ED448);
- break;
- #endif /* HAVE_ED448 */
- default:
- break;
- } /* switch (keyOID) */
- sigCtx->key.ptr = NULL;
- }
-#endif
-
- /* reset state, we are done */
- sigCtx->state = SIG_STATE_BEGIN;
-}
-
-#ifndef NO_ASN_CRYPT
-static int HashForSignature(const byte* buf, word32 bufSz, word32 sigOID,
- byte* digest, int* typeH, int* digestSz, int verify)
-{
- int ret = 0;
-
- (void)verify;
-
- switch (sigOID) {
- #if defined(WOLFSSL_MD2)
- case CTC_MD2wRSA:
- if (!verify) {
- ret = HASH_TYPE_E;
- WOLFSSL_MSG("MD2 not supported for signing");
- }
- else if ((ret = wc_Md2Hash(buf, bufSz, digest)) == 0) {
- *typeH = MD2h;
- *digestSz = MD2_DIGEST_SIZE;
- }
- break;
- #endif
- #ifndef NO_MD5
- case CTC_MD5wRSA:
- if ((ret = wc_Md5Hash(buf, bufSz, digest)) == 0) {
- *typeH = MD5h;
- *digestSz = WC_MD5_DIGEST_SIZE;
- }
- break;
- #endif
- #ifndef NO_SHA
- case CTC_SHAwRSA:
- case CTC_SHAwDSA:
- case CTC_SHAwECDSA:
- if ((ret = wc_ShaHash(buf, bufSz, digest)) == 0) {
- *typeH = SHAh;
- *digestSz = WC_SHA_DIGEST_SIZE;
- }
- break;
- #endif
- #ifdef WOLFSSL_SHA224
- case CTC_SHA224wRSA:
- case CTC_SHA224wECDSA:
- if ((ret = wc_Sha224Hash(buf, bufSz, digest)) == 0) {
- *typeH = SHA224h;
- *digestSz = WC_SHA224_DIGEST_SIZE;
- }
- break;
- #endif
- #ifndef NO_SHA256
- case CTC_SHA256wRSA:
- case CTC_SHA256wECDSA:
- if ((ret = wc_Sha256Hash(buf, bufSz, digest)) == 0) {
- *typeH = SHA256h;
- *digestSz = WC_SHA256_DIGEST_SIZE;
- }
- break;
- #endif
- #ifdef WOLFSSL_SHA384
- case CTC_SHA384wRSA:
- case CTC_SHA384wECDSA:
- if ((ret = wc_Sha384Hash(buf, bufSz, digest)) == 0) {
- *typeH = SHA384h;
- *digestSz = WC_SHA384_DIGEST_SIZE;
- }
- break;
- #endif
- #ifdef WOLFSSL_SHA512
- case CTC_SHA512wRSA:
- case CTC_SHA512wECDSA:
- if ((ret = wc_Sha512Hash(buf, bufSz, digest)) == 0) {
- *typeH = SHA512h;
- *digestSz = WC_SHA512_DIGEST_SIZE;
- }
- break;
- #endif
- #ifdef HAVE_ED25519
- case CTC_ED25519:
- /* Hashes done in signing operation.
- * Two dependent hashes with prefixes performed.
- */
- break;
- #endif
- #ifdef HAVE_ED448
- case CTC_ED448:
- /* Hashes done in signing operation.
- * Two dependent hashes with prefixes performed.
- */
- break;
- #endif
- default:
- ret = HASH_TYPE_E;
- WOLFSSL_MSG("Hash for Signature has unsupported type");
- }
-
- return ret;
-}
-#endif /* !NO_ASN_CRYPT */
-
-/* Return codes: 0=Success, Negative (see error-crypt.h), ASN_SIG_CONFIRM_E */
-static int ConfirmSignature(SignatureCtx* sigCtx,
- const byte* buf, word32 bufSz,
- const byte* key, word32 keySz, word32 keyOID,
- const byte* sig, word32 sigSz, word32 sigOID, byte* rsaKeyIdx)
-{
- int ret = 0;
-#ifndef WOLFSSL_RENESAS_TSIP_TLS
- (void)rsaKeyIdx;
-#endif
- if (sigCtx == NULL || buf == NULL || bufSz == 0 || key == NULL ||
- keySz == 0 || sig == NULL || sigSz == 0) {
- return BAD_FUNC_ARG;
- }
-
- (void)key;
- (void)keySz;
- (void)sig;
- (void)sigSz;
-
- WOLFSSL_ENTER("ConfirmSignature");
-
-#ifndef NO_ASN_CRYPT
- switch (sigCtx->state) {
- case SIG_STATE_BEGIN:
- {
- sigCtx->keyOID = keyOID; /* must set early for cleanup */
-
- sigCtx->digest = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, sigCtx->heap,
- DYNAMIC_TYPE_DIGEST);
- if (sigCtx->digest == NULL) {
- ERROR_OUT(MEMORY_E, exit_cs);
- }
-
- sigCtx->state = SIG_STATE_HASH;
- } /* SIG_STATE_BEGIN */
- FALL_THROUGH;
-
- case SIG_STATE_HASH:
- {
- ret = HashForSignature(buf, bufSz, sigOID, sigCtx->digest,
- &sigCtx->typeH, &sigCtx->digestSz, 1);
- if (ret != 0) {
- goto exit_cs;
- }
-
- sigCtx->state = SIG_STATE_KEY;
- } /* SIG_STATE_HASH */
- FALL_THROUGH;
-
- case SIG_STATE_KEY:
- {
- switch (keyOID) {
- #ifndef NO_RSA
- case RSAk:
- {
- word32 idx = 0;
-
- sigCtx->key.rsa = (RsaKey*)XMALLOC(sizeof(RsaKey),
- sigCtx->heap, DYNAMIC_TYPE_RSA);
- sigCtx->plain = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ,
- sigCtx->heap, DYNAMIC_TYPE_SIGNATURE);
- if (sigCtx->key.rsa == NULL || sigCtx->plain == NULL) {
- ERROR_OUT(MEMORY_E, exit_cs);
- }
- if ((ret = wc_InitRsaKey_ex(sigCtx->key.rsa, sigCtx->heap,
- sigCtx->devId)) != 0) {
- goto exit_cs;
- }
- if (sigSz > MAX_ENCODED_SIG_SZ) {
- WOLFSSL_MSG("Verify Signature is too big");
- ERROR_OUT(BUFFER_E, exit_cs);
- }
- if ((ret = wc_RsaPublicKeyDecode(key, &idx, sigCtx->key.rsa,
- keySz)) != 0) {
- WOLFSSL_MSG("ASN Key decode error RSA");
- goto exit_cs;
- }
- XMEMCPY(sigCtx->plain, sig, sigSz);
- sigCtx->out = NULL;
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- sigCtx->asyncDev = &sigCtx->key.rsa->asyncDev;
- #endif
- break;
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ECDSAk:
- {
- word32 idx = 0;
-
- sigCtx->verify = 0;
- sigCtx->key.ecc = (ecc_key*)XMALLOC(sizeof(ecc_key),
- sigCtx->heap, DYNAMIC_TYPE_ECC);
- if (sigCtx->key.ecc == NULL) {
- ERROR_OUT(MEMORY_E, exit_cs);
- }
- if ((ret = wc_ecc_init_ex(sigCtx->key.ecc, sigCtx->heap,
- sigCtx->devId)) < 0) {
- goto exit_cs;
- }
- ret = wc_EccPublicKeyDecode(key, &idx, sigCtx->key.ecc,
- keySz);
- if (ret < 0) {
- WOLFSSL_MSG("ASN Key import error ECC");
- goto exit_cs;
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- sigCtx->asyncDev = &sigCtx->key.ecc->asyncDev;
- #endif
- break;
- }
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ED25519k:
- {
- sigCtx->verify = 0;
- sigCtx->key.ed25519 = (ed25519_key*)XMALLOC(
- sizeof(ed25519_key), sigCtx->heap,
- DYNAMIC_TYPE_ED25519);
- if (sigCtx->key.ed25519 == NULL) {
- ERROR_OUT(MEMORY_E, exit_cs);
- }
- if ((ret = wc_ed25519_init(sigCtx->key.ed25519)) < 0) {
- goto exit_cs;
- }
- if ((ret = wc_ed25519_import_public(key, keySz,
- sigCtx->key.ed25519)) < 0) {
- WOLFSSL_MSG("ASN Key import error ED25519");
- goto exit_cs;
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- sigCtx->asyncDev = &sigCtx->key.ed25519->asyncDev;
- #endif
- break;
- }
- #endif
- #ifdef HAVE_ED448
- case ED448k:
- {
- sigCtx->verify = 0;
- sigCtx->key.ed448 = (ed448_key*)XMALLOC(
- sizeof(ed448_key), sigCtx->heap,
- DYNAMIC_TYPE_ED448);
- if (sigCtx->key.ed448 == NULL) {
- ERROR_OUT(MEMORY_E, exit_cs);
- }
- if ((ret = wc_ed448_init(sigCtx->key.ed448)) < 0) {
- goto exit_cs;
- }
- if ((ret = wc_ed448_import_public(key, keySz,
- sigCtx->key.ed448)) < 0) {
- WOLFSSL_MSG("ASN Key import error ED448");
- goto exit_cs;
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- sigCtx->asyncDev = &sigCtx->key.ed448->asyncDev;
- #endif
- break;
- }
- #endif
- default:
- WOLFSSL_MSG("Verify Key type unknown");
- ret = ASN_UNKNOWN_OID_E;
- break;
- } /* switch (keyOID) */
-
- if (ret != 0) {
- goto exit_cs;
- }
-
- sigCtx->state = SIG_STATE_DO;
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (sigCtx->devId != INVALID_DEVID && sigCtx->asyncDev && sigCtx->asyncCtx) {
- /* make sure event is initialized */
- WOLF_EVENT* event = &sigCtx->asyncDev->event;
- ret = wolfAsync_EventInit(event, WOLF_EVENT_TYPE_ASYNC_WOLFSSL,
- sigCtx->asyncCtx, WC_ASYNC_FLAG_CALL_AGAIN);
- }
- #endif
- } /* SIG_STATE_KEY */
- FALL_THROUGH;
-
- case SIG_STATE_DO:
- {
- switch (keyOID) {
- #ifndef NO_RSA
- case RSAk:
- {
- #ifdef HAVE_PK_CALLBACKS
- if (sigCtx->pkCbRsa) {
- ret = sigCtx->pkCbRsa(
- sigCtx->plain, sigSz, &sigCtx->out,
- key, keySz,
- sigCtx->pkCtxRsa);
- }
- else
- #endif /* HAVE_PK_CALLBACKS */
- {
- #ifdef WOLFSSL_RENESAS_TSIP_TLS
- if (rsaKeyIdx != NULL)
- {
- ret = tsip_tls_CertVerify(buf, bufSz, sigCtx->plain,
- sigSz,
- sigCtx->pubkey_n_start - sigCtx->certBegin,
- sigCtx->pubkey_n_len - 1,
- sigCtx->pubkey_e_start - sigCtx->certBegin,
- sigCtx->pubkey_e_len - 1,
- rsaKeyIdx);
-
- if (ret == 0){
- sigCtx->verifyByTSIP = 1;
- ret = 0;
- } else {
- WOLFSSL_MSG("RSA Verify by tsip didn't match");
- ret = ASN_SIG_CONFIRM_E;
- }
- } else
- #endif
- ret = wc_RsaSSL_VerifyInline(sigCtx->plain, sigSz,
- &sigCtx->out, sigCtx->key.rsa);
- }
- break;
- }
- #endif /* !NO_RSA */
- #if defined(HAVE_ECC)
- case ECDSAk:
- {
- #ifdef HAVE_PK_CALLBACKS
- if (sigCtx->pkCbEcc) {
- ret = sigCtx->pkCbEcc(
- sig, sigSz,
- sigCtx->digest, sigCtx->digestSz,
- key, keySz, &sigCtx->verify,
- sigCtx->pkCtxEcc);
- }
- else
- #endif /* HAVE_PK_CALLBACKS */
- {
- ret = wc_ecc_verify_hash(sig, sigSz, sigCtx->digest,
- sigCtx->digestSz, &sigCtx->verify,
- sigCtx->key.ecc);
- }
- break;
- }
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ED25519k:
- {
- ret = wc_ed25519_verify_msg(sig, sigSz, buf, bufSz,
- &sigCtx->verify, sigCtx->key.ed25519);
- break;
- }
- #endif
- #ifdef HAVE_ED448
- case ED448k:
- {
- ret = wc_ed448_verify_msg(sig, sigSz, buf, bufSz,
- &sigCtx->verify, sigCtx->key.ed448,
- NULL, 0);
- break;
- }
- #endif
- default:
- break;
- } /* switch (keyOID) */
-
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- goto exit_cs;
- }
- #endif
-
- if (ret < 0) {
- /* treat all RSA errors as ASN_SIG_CONFIRM_E */
- ret = ASN_SIG_CONFIRM_E;
- goto exit_cs;
- }
-
- sigCtx->state = SIG_STATE_CHECK;
- } /* SIG_STATE_DO */
- FALL_THROUGH;
-
- case SIG_STATE_CHECK:
- {
- switch (keyOID) {
- #ifndef NO_RSA
- case RSAk:
- {
- int encodedSigSz, verifySz;
- #ifdef WOLFSSL_RENESAS_TSIP
- if (sigCtx->verifyByTSIP == 1) break;
- #endif
- #ifdef WOLFSSL_SMALL_STACK
- byte* encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ,
- sigCtx->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (encodedSig == NULL) {
- ERROR_OUT(MEMORY_E, exit_cs);
- }
- #else
- byte encodedSig[MAX_ENCODED_SIG_SZ];
- #endif
-
- verifySz = ret;
-
- /* make sure we're right justified */
- encodedSigSz = wc_EncodeSignature(encodedSig,
- sigCtx->digest, sigCtx->digestSz, sigCtx->typeH);
- if (encodedSigSz == verifySz && sigCtx->out != NULL &&
- XMEMCMP(sigCtx->out, encodedSig, encodedSigSz) == 0) {
- ret = 0;
- }
- else {
- WOLFSSL_MSG("RSA SSL verify match encode error");
- ret = ASN_SIG_CONFIRM_E;
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(encodedSig, sigCtx->heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- break;
- }
- #endif /* NO_RSA */
- #ifdef HAVE_ECC
- case ECDSAk:
- {
- if (sigCtx->verify == 1) {
- ret = 0;
- }
- else {
- WOLFSSL_MSG("ECC Verify didn't match");
- ret = ASN_SIG_CONFIRM_E;
- }
- break;
- }
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- case ED25519k:
- {
- if (sigCtx->verify == 1) {
- ret = 0;
- }
- else {
- WOLFSSL_MSG("ED25519 Verify didn't match");
- ret = ASN_SIG_CONFIRM_E;
- }
- break;
- }
- #endif /* HAVE_ED25519 */
- #ifdef HAVE_ED448
- case ED448k:
- {
- if (sigCtx->verify == 1) {
- ret = 0;
- }
- else {
- WOLFSSL_MSG("ED448 Verify didn't match");
- ret = ASN_SIG_CONFIRM_E;
- }
- break;
- }
- #endif /* HAVE_ED448 */
- default:
- break;
- } /* switch (keyOID) */
-
- break;
- } /* SIG_STATE_CHECK */
- } /* switch (sigCtx->state) */
-
-exit_cs:
-
-#endif /* !NO_ASN_CRYPT */
-
- (void)keyOID;
- (void)sigOID;
-
- WOLFSSL_LEAVE("ConfirmSignature", ret);
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- return ret;
-#endif
-
- FreeSignatureCtx(sigCtx);
-
- return ret;
-}
-
-
-#ifndef IGNORE_NAME_CONSTRAINTS
-
-static int MatchBaseName(int type, const char* name, int nameSz,
- const char* base, int baseSz)
-{
- if (base == NULL || baseSz <= 0 || name == NULL || nameSz <= 0 ||
- name[0] == '.' || nameSz < baseSz ||
- (type != ASN_RFC822_TYPE && type != ASN_DNS_TYPE))
- return 0;
-
- /* If an email type, handle special cases where the base is only
- * a domain, or is an email address itself. */
- if (type == ASN_RFC822_TYPE) {
- const char* p = NULL;
- int count = 0;
-
- if (base[0] != '.') {
- p = base;
- count = 0;
-
- /* find the '@' in the base */
- while (*p != '@' && count < baseSz) {
- count++;
- p++;
- }
-
- /* No '@' in base, reset p to NULL */
- if (count >= baseSz)
- p = NULL;
- }
-
- if (p == NULL) {
- /* Base isn't an email address, it is a domain name,
- * wind the name forward one character past its '@'. */
- p = name;
- count = 0;
- while (*p != '@' && count < baseSz) {
- count++;
- p++;
- }
-
- if (count < baseSz && *p == '@') {
- name = p + 1;
- nameSz -= count + 1;
- }
- }
- }
-
- if ((type == ASN_DNS_TYPE || type == ASN_RFC822_TYPE) && base[0] == '.') {
- int szAdjust = nameSz - baseSz;
- name += szAdjust;
- nameSz -= szAdjust;
- }
-
- while (nameSz > 0) {
- if (XTOLOWER((unsigned char)*name++) !=
- XTOLOWER((unsigned char)*base++))
- return 0;
- nameSz--;
- }
-
- return 1;
-}
-
-
-static int ConfirmNameConstraints(Signer* signer, DecodedCert* cert)
-{
- if (signer == NULL || cert == NULL)
- return 0;
-
- /* Check against the excluded list */
- if (signer->excludedNames) {
- Base_entry* base = signer->excludedNames;
-
- while (base != NULL) {
- switch (base->type) {
- case ASN_DNS_TYPE:
- {
- DNS_entry* name = cert->altNames;
- while (name != NULL) {
- if (MatchBaseName(ASN_DNS_TYPE,
- name->name, name->len,
- base->name, base->nameSz)) {
- return 0;
- }
- name = name->next;
- }
- break;
- }
- case ASN_RFC822_TYPE:
- {
- DNS_entry* name = cert->altEmailNames;
- while (name != NULL) {
- if (MatchBaseName(ASN_RFC822_TYPE,
- name->name, name->len,
- base->name, base->nameSz)) {
- return 0;
- }
- name = name->next;
- }
- break;
- }
- case ASN_DIR_TYPE:
- {
- /* allow permitted dirName smaller than actual subject */
- if (cert->subjectRawLen >= base->nameSz &&
- XMEMCMP(cert->subjectRaw, base->name,
- base->nameSz) == 0) {
- return 0;
- }
- break;
- }
- }; /* switch */
- base = base->next;
- }
- }
-
- /* Check against the permitted list */
- if (signer->permittedNames != NULL) {
- int needDns = 0;
- int matchDns = 0;
- int needEmail = 0;
- int matchEmail = 0;
- int needDir = 0;
- int matchDir = 0;
- Base_entry* base = signer->permittedNames;
-
- while (base != NULL) {
- switch (base->type) {
- case ASN_DNS_TYPE:
- {
- DNS_entry* name = cert->altNames;
-
- if (name != NULL)
- needDns = 1;
-
- while (name != NULL) {
- matchDns = MatchBaseName(ASN_DNS_TYPE,
- name->name, name->len,
- base->name, base->nameSz);
- name = name->next;
- }
- break;
- }
- case ASN_RFC822_TYPE:
- {
- DNS_entry* name = cert->altEmailNames;
-
- if (name != NULL)
- needEmail = 1;
-
- while (name != NULL) {
- matchEmail = MatchBaseName(ASN_DNS_TYPE,
- name->name, name->len,
- base->name, base->nameSz);
- name = name->next;
- }
- break;
- }
- case ASN_DIR_TYPE:
- {
- /* allow permitted dirName smaller than actual subject */
- needDir = 1;
- if (cert->subjectRaw != NULL &&
- cert->subjectRawLen >= base->nameSz &&
- XMEMCMP(cert->subjectRaw, base->name,
- base->nameSz) == 0) {
- matchDir = 1;
- }
- break;
- }
- } /* switch */
- base = base->next;
- }
-
- if ((needDns && !matchDns) ||
- (needEmail && !matchEmail) ||
- (needDir && !matchDir)) {
- return 0;
- }
- }
-
- return 1;
-}
-
-#endif /* IGNORE_NAME_CONSTRAINTS */
-
-static int DecodeAltNames(const byte* input, int sz, DecodedCert* cert)
-{
- word32 idx = 0;
- int length = 0;
-
- WOLFSSL_ENTER("DecodeAltNames");
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tBad Sequence");
- return ASN_PARSE_E;
- }
-
- if (length == 0) {
- /* RFC 5280 4.2.1.6. Subject Alternative Name
- If the subjectAltName extension is present, the sequence MUST
- contain at least one entry. */
- return ASN_PARSE_E;
- }
-
- cert->weOwnAltNames = 1;
-
- while (length > 0) {
- byte b = input[idx++];
-
- length--;
-
- /* Save DNS Type names in the altNames list. */
- /* Save Other Type names in the cert's OidMap */
- if (b == (ASN_CONTEXT_SPECIFIC | ASN_DNS_TYPE)) {
- DNS_entry* dnsEntry;
- int strLen;
- word32 lenStartIdx = idx;
-
- if (GetLength(input, &idx, &strLen, sz) < 0) {
- WOLFSSL_MSG("\tfail: str length");
- return ASN_PARSE_E;
- }
- length -= (idx - lenStartIdx);
-
- dnsEntry = (DNS_entry*)XMALLOC(sizeof(DNS_entry), cert->heap,
- DYNAMIC_TYPE_ALTNAME);
- if (dnsEntry == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- return MEMORY_E;
- }
-
- dnsEntry->type = ASN_DNS_TYPE;
- dnsEntry->name = (char*)XMALLOC(strLen + 1, cert->heap,
- DYNAMIC_TYPE_ALTNAME);
- if (dnsEntry->name == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- XFREE(dnsEntry, cert->heap, DYNAMIC_TYPE_ALTNAME);
- return MEMORY_E;
- }
- dnsEntry->len = strLen;
- XMEMCPY(dnsEntry->name, &input[idx], strLen);
- dnsEntry->name[strLen] = '\0';
-
- dnsEntry->next = cert->altNames;
- cert->altNames = dnsEntry;
-
- length -= strLen;
- idx += strLen;
- }
- #ifndef IGNORE_NAME_CONSTRAINTS
- else if (b == (ASN_CONTEXT_SPECIFIC | ASN_RFC822_TYPE)) {
- DNS_entry* emailEntry;
- int strLen;
- word32 lenStartIdx = idx;
-
- if (GetLength(input, &idx, &strLen, sz) < 0) {
- WOLFSSL_MSG("\tfail: str length");
- return ASN_PARSE_E;
- }
- length -= (idx - lenStartIdx);
-
- emailEntry = (DNS_entry*)XMALLOC(sizeof(DNS_entry), cert->heap,
- DYNAMIC_TYPE_ALTNAME);
- if (emailEntry == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- return MEMORY_E;
- }
-
- emailEntry->type = ASN_RFC822_TYPE;
- emailEntry->name = (char*)XMALLOC(strLen + 1, cert->heap,
- DYNAMIC_TYPE_ALTNAME);
- if (emailEntry->name == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- XFREE(emailEntry, cert->heap, DYNAMIC_TYPE_ALTNAME);
- return MEMORY_E;
- }
- emailEntry->len = strLen;
- XMEMCPY(emailEntry->name, &input[idx], strLen);
- emailEntry->name[strLen] = '\0';
-
- emailEntry->next = cert->altEmailNames;
- cert->altEmailNames = emailEntry;
-
- length -= strLen;
- idx += strLen;
- }
- else if (b == (ASN_CONTEXT_SPECIFIC | ASN_URI_TYPE)) {
- DNS_entry* uriEntry;
- int strLen;
- word32 lenStartIdx = idx;
-
- WOLFSSL_MSG("\tPutting URI into list but not using");
- if (GetLength(input, &idx, &strLen, sz) < 0) {
- WOLFSSL_MSG("\tfail: str length");
- return ASN_PARSE_E;
- }
- length -= (idx - lenStartIdx);
-
- /* check that strLen at index is not past input buffer */
- if (strLen + (int)idx > sz) {
- return BUFFER_E;
- }
-
- #ifndef WOLFSSL_NO_ASN_STRICT
- /* Verify RFC 5280 Sec 4.2.1.6 rule:
- "The name MUST NOT be a relative URI" */
-
- {
- int i;
-
- /* skip past scheme (i.e http,ftp,...) finding first ':' char */
- for (i = 0; i < strLen; i++) {
- if (input[idx + i] == ':') {
- break;
- }
- if (input[idx + i] == '/') {
- i = strLen; /* error, found relative path since '/' was
- * encountered before ':'. Returning error
- * value in next if statement. */
- }
- }
-
- /* test if no ':' char was found and test that the next two
- * chars are // to match the pattern "://" */
- if (i >= strLen - 2 || (input[idx + i + 1] != '/' ||
- input[idx + i + 2] != '/')) {
- WOLFSSL_MSG("\tAlt Name must be absolute URI");
- return ASN_ALT_NAME_E;
- }
- }
- #endif
-
- uriEntry = (DNS_entry*)XMALLOC(sizeof(DNS_entry), cert->heap,
- DYNAMIC_TYPE_ALTNAME);
- if (uriEntry == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- return MEMORY_E;
- }
-
- uriEntry->type = ASN_URI_TYPE;
- uriEntry->name = (char*)XMALLOC(strLen + 1, cert->heap,
- DYNAMIC_TYPE_ALTNAME);
- if (uriEntry->name == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- XFREE(uriEntry, cert->heap, DYNAMIC_TYPE_ALTNAME);
- return MEMORY_E;
- }
- uriEntry->len = strLen;
- XMEMCPY(uriEntry->name, &input[idx], strLen);
- uriEntry->name[strLen] = '\0';
-
- uriEntry->next = cert->altNames;
- cert->altNames = uriEntry;
-
- length -= strLen;
- idx += strLen;
- }
-#if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
- else if (b == (ASN_CONTEXT_SPECIFIC | ASN_IP_TYPE)) {
- DNS_entry* ipAddr;
- int strLen;
- word32 lenStartIdx = idx;
- WOLFSSL_MSG("Decoding Subject Alt. Name: IP Address");
-
- if (GetLength(input, &idx, &strLen, sz) < 0) {
- WOLFSSL_MSG("\tfail: str length");
- return ASN_PARSE_E;
- }
- length -= (idx - lenStartIdx);
- /* check that strLen at index is not past input buffer */
- if (strLen + (int)idx > sz) {
- return BUFFER_E;
- }
-
- ipAddr = (DNS_entry*)XMALLOC(sizeof(DNS_entry), cert->heap,
- DYNAMIC_TYPE_ALTNAME);
- if (ipAddr == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- return MEMORY_E;
- }
-
- ipAddr->type = ASN_IP_TYPE;
- ipAddr->name = (char*)XMALLOC(strLen + 1, cert->heap,
- DYNAMIC_TYPE_ALTNAME);
- if (ipAddr->name == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- XFREE(ipAddr, cert->heap, DYNAMIC_TYPE_ALTNAME);
- return MEMORY_E;
- }
- ipAddr->len = strLen;
- XMEMCPY(ipAddr->name, &input[idx], strLen);
- ipAddr->name[strLen] = '\0';
-
- ipAddr->next = cert->altNames;
- cert->altNames = ipAddr;
-
- length -= strLen;
- idx += strLen;
- }
-#endif /* WOLFSSL_QT || OPENSSL_ALL */
-#endif /* IGNORE_NAME_CONSTRAINTS */
-#ifdef WOLFSSL_SEP
- else if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | ASN_OTHER_TYPE))
- {
- int strLen;
- word32 lenStartIdx = idx;
- word32 oid = 0;
- int ret;
- byte tag;
-
- if (GetLength(input, &idx, &strLen, sz) < 0) {
- WOLFSSL_MSG("\tfail: other name length");
- return ASN_PARSE_E;
- }
- /* Consume the rest of this sequence. */
- length -= (strLen + idx - lenStartIdx);
-
- if (GetObjectId(input, &idx, &oid, oidCertAltNameType, sz) < 0) {
- WOLFSSL_MSG("\tbad OID");
- return ASN_PARSE_E;
- }
-
- if (oid != HW_NAME_OID) {
- WOLFSSL_MSG("\tincorrect OID");
- return ASN_PARSE_E;
- }
-
- if (GetASNTag(input, &idx, &tag, sz) < 0) {
- return ASN_PARSE_E;
- }
-
- if (tag != (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) {
- WOLFSSL_MSG("\twrong type");
- return ASN_PARSE_E;
- }
-
- if (GetLength(input, &idx, &strLen, sz) < 0) {
- WOLFSSL_MSG("\tfail: str len");
- return ASN_PARSE_E;
- }
-
- if (GetSequence(input, &idx, &strLen, sz) < 0) {
- WOLFSSL_MSG("\tBad Sequence");
- return ASN_PARSE_E;
- }
-
- ret = GetASNObjectId(input, &idx, &strLen, sz);
- if (ret != 0) {
- WOLFSSL_MSG("\tbad OID");
- return ret;
- }
-
- cert->hwType = (byte*)XMALLOC(strLen, cert->heap,
- DYNAMIC_TYPE_X509_EXT);
- if (cert->hwType == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- return MEMORY_E;
- }
-
- XMEMCPY(cert->hwType, &input[idx], strLen);
- cert->hwTypeSz = strLen;
- idx += strLen;
-
- ret = GetOctetString(input, &idx, &strLen, sz);
- if (ret < 0)
- return ret;
-
- cert->hwSerialNum = (byte*)XMALLOC(strLen + 1, cert->heap,
- DYNAMIC_TYPE_X509_EXT);
- if (cert->hwSerialNum == NULL) {
- WOLFSSL_MSG("\tOut of Memory");
- return MEMORY_E;
- }
-
- XMEMCPY(cert->hwSerialNum, &input[idx], strLen);
- cert->hwSerialNum[strLen] = '\0';
- cert->hwSerialNumSz = strLen;
- idx += strLen;
- }
- #endif /* WOLFSSL_SEP */
- else {
- int strLen;
- word32 lenStartIdx = idx;
-
- WOLFSSL_MSG("\tUnsupported name type, skipping");
-
- if (GetLength(input, &idx, &strLen, sz) < 0) {
- WOLFSSL_MSG("\tfail: unsupported name length");
- return ASN_PARSE_E;
- }
- length -= (strLen + idx - lenStartIdx);
- idx += strLen;
- }
- }
- return 0;
-}
-
-static int DecodeBasicCaConstraint(const byte* input, int sz, DecodedCert* cert)
-{
- word32 idx = 0;
- int length = 0;
- int ret;
-
- WOLFSSL_ENTER("DecodeBasicCaConstraint");
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: bad SEQUENCE");
- return ASN_PARSE_E;
- }
-
- if (length == 0)
- return 0;
-
- /* If the basic ca constraint is false, this extension may be named, but
- * left empty. So, if the length is 0, just return. */
-
- ret = GetBoolean(input, &idx, sz);
-
-#ifndef WOLFSSL_X509_BASICCONS_INT
- if (ret < 0) {
- WOLFSSL_MSG("\tfail: constraint not valid BOOLEAN");
- return ret;
- }
-
- cert->isCA = (byte)ret;
-#else
- if (ret < 0) {
- if(input[idx] == ASN_INTEGER) {
- /* For OpenSSL compatibility, if ASN_INTEGER it is valid format */
- cert->isCA = FALSE;
- } else return ret;
- } else
- cert->isCA = (byte)ret;
-#endif
-
- /* If there isn't any more data, return. */
- if (idx >= (word32)sz) {
- return 0;
- }
-
- ret = GetInteger7Bit(input, &idx, sz);
- if (ret < 0)
- return ret;
- cert->pathLength = (byte)ret;
- cert->pathLengthSet = 1;
-
- return 0;
-}
-
-
-#define CRLDP_FULL_NAME 0
- /* From RFC3280 SS4.2.1.14, Distribution Point Name*/
-#define GENERALNAME_URI 6
- /* From RFC3280 SS4.2.1.7, GeneralName */
-
-static int DecodeCrlDist(const byte* input, int sz, DecodedCert* cert)
-{
- word32 idx = 0, localIdx;
- int length = 0;
- byte tag = 0;
-
- WOLFSSL_ENTER("DecodeCrlDist");
-
- /* Unwrap the list of Distribution Points*/
- if (GetSequence(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- /* Unwrap a single Distribution Point */
- if (GetSequence(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- /* The Distribution Point has three explicit optional members
- * First check for a DistributionPointName
- */
- localIdx = idx;
- if (GetASNTag(input, &localIdx, &tag, sz) == 0 &&
- tag == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0))
- {
- idx++;
- if (GetLength(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- localIdx = idx;
- if (GetASNTag(input, &localIdx, &tag, sz) == 0 &&
- tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED |
- CRLDP_FULL_NAME))
- {
- idx++;
- if (GetLength(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- localIdx = idx;
- if (GetASNTag(input, &localIdx, &tag, sz) == 0 &&
- tag == (ASN_CONTEXT_SPECIFIC | GENERALNAME_URI))
- {
- idx++;
- if (GetLength(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- cert->extCrlInfoSz = length;
- cert->extCrlInfo = input + idx;
- idx += length;
- }
- else
- /* This isn't a URI, skip it. */
- idx += length;
- }
- else {
- /* This isn't a FULLNAME, skip it. */
- idx += length;
- }
- }
-
- /* Check for reasonFlags */
- localIdx = idx;
- if (idx < (word32)sz &&
- GetASNTag(input, &localIdx, &tag, sz) == 0 &&
- tag == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1))
- {
- idx++;
- if (GetLength(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
- idx += length;
- }
-
- /* Check for cRLIssuer */
- localIdx = idx;
- if (idx < (word32)sz &&
- GetASNTag(input, &localIdx, &tag, sz) == 0 &&
- tag == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 2))
- {
- idx++;
- if (GetLength(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
- idx += length;
- }
-
- if (idx < (word32)sz)
- {
- WOLFSSL_MSG("\tThere are more CRL Distribution Point records, "
- "but we only use the first one.");
- }
-
- return 0;
-}
-
-
-static int DecodeAuthInfo(const byte* input, int sz, DecodedCert* cert)
-/*
- * Read Authority Information Access records. If there are
- * any issues, return without saving the record.
- */
-{
- word32 idx = 0;
- int length = 0;
- int count = 0;
- byte b = 0;
- word32 oid;
-
- WOLFSSL_ENTER("DecodeAuthInfo");
-
- /* Unwrap the list of AIAs */
- if (GetSequence(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- while ((idx < (word32)sz) && (count < MAX_AIA_SZ)) {
- /* Unwrap a single AIA */
- if (GetSequence(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- oid = 0;
- if (GetObjectId(input, &idx, &oid, oidCertAuthInfoType, sz) < 0)
- return ASN_PARSE_E;
-
- /* Only supporting URIs right now. */
- if (GetASNTag(input, &idx, &b, sz) < 0)
- return ASN_PARSE_E;
-
- if (GetLength(input, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- /* Set ocsp entry */
- if (b == (ASN_CONTEXT_SPECIFIC | GENERALNAME_URI) &&
- oid == AIA_OCSP_OID)
- {
- cert->extAuthInfoSz = length;
- cert->extAuthInfo = input + idx;
- count++;
- #if !defined(OPENSSL_ALL) || !defined(WOLFSSL_QT)
- break;
- #endif
- }
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- /* Set CaIssuers entry */
- else if ((b == (ASN_CONTEXT_SPECIFIC | GENERALNAME_URI)) &&
- oid == AIA_CA_ISSUER_OID)
- {
- cert->extAuthInfoCaIssuerSz = length;
- cert->extAuthInfoCaIssuer = input + idx;
- count++;
- }
- #endif
- idx += length;
- }
-
- return 0;
-}
-
-
-static int DecodeAuthKeyId(const byte* input, int sz, DecodedCert* cert)
-{
- word32 idx = 0;
- int length = 0, ret = 0;
- byte tag;
-
- WOLFSSL_ENTER("DecodeAuthKeyId");
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE\n");
- return ASN_PARSE_E;
- }
-
- if (GetASNTag(input, &idx, &tag, sz) < 0) {
- return ASN_PARSE_E;
- }
-
- if (tag != (ASN_CONTEXT_SPECIFIC | 0)) {
- WOLFSSL_MSG("\tinfo: OPTIONAL item 0, not available\n");
- cert->extAuthKeyIdSet = 0;
- return 0;
- }
-
- if (GetLength(input, &idx, &length, sz) <= 0) {
- WOLFSSL_MSG("\tfail: extension data length");
- return ASN_PARSE_E;
- }
-
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extAuthKeyIdSrc = &input[idx];
- cert->extAuthKeyIdSz = length;
-#endif /* OPENSSL_EXTRA */
-
- if (length == KEYID_SIZE) {
- XMEMCPY(cert->extAuthKeyId, input + idx, length);
- }
- else
- ret = CalcHashId(input + idx, length, cert->extAuthKeyId);
-
- return ret;
-}
-
-
-static int DecodeSubjKeyId(const byte* input, int sz, DecodedCert* cert)
-{
- word32 idx = 0;
- int length = 0, ret = 0;
-
- WOLFSSL_ENTER("DecodeSubjKeyId");
-
- if (sz <= 0)
- return ASN_PARSE_E;
-
- ret = GetOctetString(input, &idx, &length, sz);
- if (ret < 0)
- return ret;
-
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extSubjKeyIdSrc = &input[idx];
- cert->extSubjKeyIdSz = length;
- #endif /* OPENSSL_EXTRA */
-
- if (length == KEYID_SIZE) {
- XMEMCPY(cert->extSubjKeyId, input + idx, length);
- }
- else
- ret = CalcHashId(input + idx, length, cert->extSubjKeyId);
-
- return ret;
-}
-
-
-static int DecodeKeyUsage(const byte* input, int sz, DecodedCert* cert)
-{
- word32 idx = 0;
- int length;
- int ret;
- WOLFSSL_ENTER("DecodeKeyUsage");
-
- ret = CheckBitString(input, &idx, &length, sz, 0, NULL);
- if (ret != 0)
- return ret;
-
- cert->extKeyUsage = (word16)(input[idx]);
- if (length == 2)
- cert->extKeyUsage |= (word16)(input[idx+1] << 8);
-
- return 0;
-}
-
-
-static int DecodeExtKeyUsage(const byte* input, int sz, DecodedCert* cert)
-{
- word32 idx = 0, oid;
- int length, ret;
-
- WOLFSSL_MSG("DecodeExtKeyUsage");
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE");
- return ASN_PARSE_E;
- }
-
-#if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extExtKeyUsageSrc = input + idx;
- cert->extExtKeyUsageSz = length;
-#endif
-
- while (idx < (word32)sz) {
- ret = GetObjectId(input, &idx, &oid, oidCertKeyUseType, sz);
- if (ret == ASN_UNKNOWN_OID_E)
- continue;
- else if (ret < 0)
- return ret;
-
- switch (oid) {
- case EKU_ANY_OID:
- cert->extExtKeyUsage |= EXTKEYUSE_ANY;
- break;
- case EKU_SERVER_AUTH_OID:
- cert->extExtKeyUsage |= EXTKEYUSE_SERVER_AUTH;
- break;
- case EKU_CLIENT_AUTH_OID:
- cert->extExtKeyUsage |= EXTKEYUSE_CLIENT_AUTH;
- break;
- case EKU_CODESIGNING_OID:
- cert->extExtKeyUsage |= EXTKEYUSE_CODESIGN;
- break;
- case EKU_EMAILPROTECT_OID:
- cert->extExtKeyUsage |= EXTKEYUSE_EMAILPROT;
- break;
- case EKU_TIMESTAMP_OID:
- cert->extExtKeyUsage |= EXTKEYUSE_TIMESTAMP;
- break;
- case EKU_OCSP_SIGN_OID:
- cert->extExtKeyUsage |= EXTKEYUSE_OCSP_SIGN;
- break;
- }
-
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extExtKeyUsageCount++;
- #endif
- }
-
- return 0;
-}
-
-
-#ifndef IGNORE_NAME_CONSTRAINTS
-#define ASN_TYPE_MASK 0xF
-static int DecodeSubtree(const byte* input, int sz,
- Base_entry** head, void* heap)
-{
- word32 idx = 0;
-
- (void)heap;
-
- while (idx < (word32)sz) {
- int seqLength, strLength;
- word32 nameIdx;
- byte b, bType;
-
- if (GetSequence(input, &idx, &seqLength, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE");
- return ASN_PARSE_E;
- }
- nameIdx = idx;
- b = input[nameIdx++];
-
- if (GetLength(input, &nameIdx, &strLength, sz) <= 0) {
- WOLFSSL_MSG("\tinvalid length");
- return ASN_PARSE_E;
- }
-
- /* Get type, LSB 4-bits */
- bType = (b & ASN_TYPE_MASK);
-
- if (bType == ASN_DNS_TYPE || bType == ASN_RFC822_TYPE ||
- bType == ASN_DIR_TYPE) {
- Base_entry* entry;
-
- /* if constructed has leading sequence */
- if (b & ASN_CONSTRUCTED) {
- if (GetSequence(input, &nameIdx, &strLength, sz) < 0) {
- WOLFSSL_MSG("\tfail: constructed be a SEQUENCE");
- return ASN_PARSE_E;
- }
- }
-
- entry = (Base_entry*)XMALLOC(sizeof(Base_entry), heap,
- DYNAMIC_TYPE_ALTNAME);
- if (entry == NULL) {
- WOLFSSL_MSG("allocate error");
- return MEMORY_E;
- }
-
- entry->name = (char*)XMALLOC(strLength, heap, DYNAMIC_TYPE_ALTNAME);
- if (entry->name == NULL) {
- WOLFSSL_MSG("allocate error");
- XFREE(entry, heap, DYNAMIC_TYPE_ALTNAME);
- return MEMORY_E;
- }
-
- XMEMCPY(entry->name, &input[nameIdx], strLength);
- entry->nameSz = strLength;
- entry->type = bType;
-
- entry->next = *head;
- *head = entry;
- }
-
- idx += seqLength;
- }
-
- return 0;
-}
-
-
-static int DecodeNameConstraints(const byte* input, int sz, DecodedCert* cert)
-{
- word32 idx = 0;
- int length = 0;
-
- WOLFSSL_ENTER("DecodeNameConstraints");
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE");
- return ASN_PARSE_E;
- }
-
- while (idx < (word32)sz) {
- byte b = input[idx++];
- Base_entry** subtree = NULL;
-
- if (GetLength(input, &idx, &length, sz) <= 0) {
- WOLFSSL_MSG("\tinvalid length");
- return ASN_PARSE_E;
- }
-
- if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 0))
- subtree = &cert->permittedNames;
- else if (b == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1))
- subtree = &cert->excludedNames;
- else {
- WOLFSSL_MSG("\tinvalid subtree");
- return ASN_PARSE_E;
- }
-
- DecodeSubtree(input + idx, length, subtree, cert->heap);
-
- idx += length;
- }
-
- return 0;
-}
-#endif /* IGNORE_NAME_CONSTRAINTS */
-
-#if (defined(WOLFSSL_CERT_EXT) && !defined(WOLFSSL_SEP)) || defined(OPENSSL_EXTRA)
-
-/* Decode ITU-T X.690 OID format to a string representation
- * return string length */
-int DecodePolicyOID(char *out, word32 outSz, const byte *in, word32 inSz)
-{
- word32 val, inIdx = 0, outIdx = 0;
- int w = 0;
-
- if (out == NULL || in == NULL || outSz < 4 || inSz < 2)
- return BAD_FUNC_ARG;
-
- /* The first byte expands into b/40 dot b%40. */
- val = in[inIdx++];
-
- w = XSNPRINTF(out, outSz, "%u.%u", val / 40, val % 40);
- if (w < 0)
- goto exit;
- outIdx += w;
- val = 0;
-
- while (inIdx < inSz && outIdx < outSz) {
- /* extract the next OID digit from in to val */
- /* first bit is used to set if value is coded on 1 or multiple bytes */
- if (in[inIdx] & 0x80) {
- val += in[inIdx] & 0x7F;
- val *= 128;
- }
- else {
- /* write val as text into out */
- val += in[inIdx];
- w = XSNPRINTF(out + outIdx, outSz - outIdx, ".%u", val);
- if (w < 0)
- goto exit;
- outIdx += w;
- val = 0;
- }
- inIdx++;
- }
- if (outIdx == outSz)
- outIdx--;
- out[outIdx] = 0;
-
- w = (int)outIdx;
-
-exit:
- return w;
-}
-#endif /* WOLFSSL_CERT_EXT && !WOLFSSL_SEP */
-
-#if defined(WOLFSSL_SEP) || defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_QT)
- /* Reference: https://tools.ietf.org/html/rfc5280#section-4.2.1.4 */
- static int DecodeCertPolicy(const byte* input, int sz, DecodedCert* cert)
- {
- word32 idx = 0;
- word32 oldIdx;
- int ret;
- int total_length = 0, policy_length = 0, length = 0;
- #if !defined(WOLFSSL_SEP) && defined(WOLFSSL_CERT_EXT) && \
- !defined(WOLFSSL_DUP_CERTPOL)
- int i;
- #endif
-
- WOLFSSL_ENTER("DecodeCertPolicy");
- #if defined(WOLFSSL_SEP) || defined(WOLFSSL_CERT_EXT)
- /* Check if cert is null before dereferencing below */
- if (cert == NULL)
- return BAD_FUNC_ARG;
- #endif
-
- #if defined(WOLFSSL_CERT_EXT)
- cert->extCertPoliciesNb = 0;
- #endif
-
- if (GetSequence(input, &idx, &total_length, sz) < 0) {
- WOLFSSL_MSG("\tGet CertPolicy total seq failed");
- return ASN_PARSE_E;
- }
-
- /* Validate total length */
- if (total_length > (sz - (int)idx)) {
- WOLFSSL_MSG("\tCertPolicy length mismatch");
- return ASN_PARSE_E;
- }
-
- /* Unwrap certificatePolicies */
- do {
- if (GetSequence(input, &idx, &policy_length, sz) < 0) {
- WOLFSSL_MSG("\tGet CertPolicy seq failed");
- return ASN_PARSE_E;
- }
-
- oldIdx = idx;
- ret = GetASNObjectId(input, &idx, &length, sz);
- if (ret != 0)
- return ret;
- policy_length -= idx - oldIdx;
-
- if (length > 0) {
- /* Verify length won't overrun buffer */
- if (length > (sz - (int)idx)) {
- WOLFSSL_MSG("\tCertPolicy length exceeds input buffer");
- return ASN_PARSE_E;
- }
-
- #if defined(WOLFSSL_SEP)
- cert->deviceType = (byte*)XMALLOC(length, cert->heap,
- DYNAMIC_TYPE_X509_EXT);
- if (cert->deviceType == NULL) {
- WOLFSSL_MSG("\tCouldn't alloc memory for deviceType");
- return MEMORY_E;
- }
- cert->deviceTypeSz = length;
- XMEMCPY(cert->deviceType, input + idx, length);
- break;
- #elif defined(WOLFSSL_CERT_EXT)
- /* decode cert policy */
- if (DecodePolicyOID(cert->extCertPolicies[
- cert->extCertPoliciesNb], MAX_CERTPOL_SZ,
- input + idx, length) <= 0) {
- WOLFSSL_MSG("\tCouldn't decode CertPolicy");
- return ASN_PARSE_E;
- }
- #ifndef WOLFSSL_DUP_CERTPOL
- /* From RFC 5280 section 4.2.1.3 "A certificate policy OID MUST
- * NOT appear more than once in a certificate policies
- * extension". This is a sanity check for duplicates.
- * extCertPolicies should only have OID values, additional
- * qualifiers need to be stored in a separate array. */
- for (i = 0; i < cert->extCertPoliciesNb; i++) {
- if (XMEMCMP(cert->extCertPolicies[i],
- cert->extCertPolicies[cert->extCertPoliciesNb],
- MAX_CERTPOL_SZ) == 0) {
- WOLFSSL_MSG("Duplicate policy OIDs not allowed");
- WOLFSSL_MSG("Use WOLFSSL_DUP_CERTPOL if wanted");
- return CERTPOLICIES_E;
- }
- }
- #endif /* !WOLFSSL_DUP_CERTPOL */
- cert->extCertPoliciesNb++;
- #else
- WOLFSSL_LEAVE("DecodeCertPolicy : unsupported mode", 0);
- return 0;
- #endif
- }
- idx += policy_length;
- } while((int)idx < total_length
- #if defined(WOLFSSL_CERT_EXT)
- && cert->extCertPoliciesNb < MAX_CERTPOL_NB
- #endif
- );
-
- WOLFSSL_LEAVE("DecodeCertPolicy", 0);
- return 0;
- }
-#endif /* WOLFSSL_SEP */
-
-/* Macro to check if bit is set, if not sets and return success.
- Otherwise returns failure */
-/* Macro required here because bit-field operation */
-#ifndef WOLFSSL_NO_ASN_STRICT
- #define VERIFY_AND_SET_OID(bit) \
- if (bit == 0) \
- bit = 1; \
- else \
- return ASN_OBJECT_ID_E;
-#else
- /* With no strict defined, the verify is skipped */
-#define VERIFY_AND_SET_OID(bit) bit = 1;
-#endif
-
-static int DecodeCertExtensions(DecodedCert* cert)
-/*
- * Processing the Certificate Extensions. This does not modify the current
- * index. It is works starting with the recorded extensions pointer.
- */
-{
- int ret = 0;
- word32 idx = 0;
- int sz = cert->extensionsSz;
- const byte* input = cert->extensions;
- int length;
- word32 oid;
- byte critical = 0;
- byte criticalFail = 0;
- byte tag = 0;
-
- WOLFSSL_ENTER("DecodeCertExtensions");
-
- if (input == NULL || sz == 0)
- return BAD_FUNC_ARG;
-
- if (GetASNTag(input, &idx, &tag, sz) < 0) {
- return ASN_PARSE_E;
- }
-
- if (tag != ASN_EXTENSIONS) {
- WOLFSSL_MSG("\tfail: should be an EXTENSIONS");
- return ASN_PARSE_E;
- }
-
- if (GetLength(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: invalid length");
- return ASN_PARSE_E;
- }
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE (1)");
- return ASN_PARSE_E;
- }
-
- while (idx < (word32)sz) {
- word32 localIdx;
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE");
- return ASN_PARSE_E;
- }
-
- oid = 0;
- if ((ret = GetObjectId(input, &idx, &oid, oidCertExtType, sz)) < 0) {
- WOLFSSL_MSG("\tfail: OBJECT ID");
- return ret;
- }
-
- /* check for critical flag */
- critical = 0;
- if ((idx + 1) > (word32)sz) {
- WOLFSSL_MSG("\tfail: malformed buffer");
- return BUFFER_E;
- }
-
- localIdx = idx;
- if (GetASNTag(input, &localIdx, &tag, sz) == 0) {
- if (tag == ASN_BOOLEAN) {
- ret = GetBoolean(input, &idx, sz);
- if (ret < 0) {
- WOLFSSL_MSG("\tfail: critical boolean");
- return ret;
- }
-
- critical = (byte)ret;
- }
- }
-
- /* process the extension based on the OID */
- ret = GetOctetString(input, &idx, &length, sz);
- if (ret < 0) {
- WOLFSSL_MSG("\tfail: bad OCTET STRING");
- return ret;
- }
-
- switch (oid) {
- case BASIC_CA_OID:
- VERIFY_AND_SET_OID(cert->extBasicConstSet);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extBasicConstCrit = critical;
- #endif
- if (DecodeBasicCaConstraint(&input[idx], length, cert) < 0)
- return ASN_PARSE_E;
- break;
-
- case CRL_DIST_OID:
- VERIFY_AND_SET_OID(cert->extCRLdistSet);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extCRLdistCrit = critical;
- #endif
- if (DecodeCrlDist(&input[idx], length, cert) < 0)
- return ASN_PARSE_E;
- break;
-
- case AUTH_INFO_OID:
- VERIFY_AND_SET_OID(cert->extAuthInfoSet);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extAuthInfoCrit = critical;
- #endif
- if (DecodeAuthInfo(&input[idx], length, cert) < 0)
- return ASN_PARSE_E;
- break;
-
- case ALT_NAMES_OID:
- VERIFY_AND_SET_OID(cert->extSubjAltNameSet);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extSubjAltNameCrit = critical;
- #endif
- ret = DecodeAltNames(&input[idx], length, cert);
- if (ret < 0)
- return ret;
- break;
-
- case AUTH_KEY_OID:
- VERIFY_AND_SET_OID(cert->extAuthKeyIdSet);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extAuthKeyIdCrit = critical;
- #endif
- #ifndef WOLFSSL_ALLOW_CRIT_SKID
- /* This check is added due to RFC 5280 section 4.2.1.1
- * stating that conforming CA's must mark this extension
- * as non-critical. When parsing extensions check that
- * certificate was made in compliance with this. */
- if (critical) {
- WOLFSSL_MSG("Critical Auth Key ID is not allowed");
- WOLFSSL_MSG("Use macro WOLFSSL_ALLOW_CRIT_SKID if wanted");
- return ASN_CRIT_EXT_E;
- }
- #endif
- if (DecodeAuthKeyId(&input[idx], length, cert) < 0)
- return ASN_PARSE_E;
- break;
-
- case SUBJ_KEY_OID:
- VERIFY_AND_SET_OID(cert->extSubjKeyIdSet);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extSubjKeyIdCrit = critical;
- #endif
- #ifndef WOLFSSL_ALLOW_CRIT_SKID
- /* This check is added due to RFC 5280 section 4.2.1.2
- * stating that conforming CA's must mark this extension
- * as non-critical. When parsing extensions check that
- * certificate was made in compliance with this. */
- if (critical) {
- WOLFSSL_MSG("Critical Subject Key ID is not allowed");
- WOLFSSL_MSG("Use macro WOLFSSL_ALLOW_CRIT_SKID if wanted");
- return ASN_CRIT_EXT_E;
- }
- #endif
-
- if (DecodeSubjKeyId(&input[idx], length, cert) < 0)
- return ASN_PARSE_E;
- break;
-
- case CERT_POLICY_OID:
- #if defined(WOLFSSL_SEP) || defined(WOLFSSL_QT)
- VERIFY_AND_SET_OID(cert->extCertPolicySet);
- #if defined(OPENSSL_EXTRA) || \
- defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extCertPolicyCrit = critical;
- #endif
- #endif
- #if defined(WOLFSSL_SEP) || defined(WOLFSSL_CERT_EXT) || \
- defined(WOLFSSL_QT)
- if (DecodeCertPolicy(&input[idx], length, cert) < 0) {
- return ASN_PARSE_E;
- }
- #else
- WOLFSSL_MSG("Certificate Policy extension not supported yet.");
- #endif
- break;
-
- case KEY_USAGE_OID:
- VERIFY_AND_SET_OID(cert->extKeyUsageSet);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extKeyUsageCrit = critical;
- #endif
- if (DecodeKeyUsage(&input[idx], length, cert) < 0)
- return ASN_PARSE_E;
- break;
-
- case EXT_KEY_USAGE_OID:
- VERIFY_AND_SET_OID(cert->extExtKeyUsageSet);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extExtKeyUsageCrit = critical;
- #endif
- if (DecodeExtKeyUsage(&input[idx], length, cert) < 0)
- return ASN_PARSE_E;
- break;
-
- #ifndef IGNORE_NAME_CONSTRAINTS
- case NAME_CONS_OID:
- #ifndef WOLFSSL_NO_ASN_STRICT
- /* Verify RFC 5280 Sec 4.2.1.10 rule:
- "The name constraints extension,
- which MUST be used only in a CA certificate" */
- if (!cert->isCA) {
- WOLFSSL_MSG("Name constraints allowed only for CA certs");
- return ASN_NAME_INVALID_E;
- }
- #endif
- VERIFY_AND_SET_OID(cert->extNameConstraintSet);
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- cert->extNameConstraintCrit = critical;
- #endif
- if (DecodeNameConstraints(&input[idx], length, cert) < 0)
- return ASN_PARSE_E;
- break;
- #endif /* IGNORE_NAME_CONSTRAINTS */
-
- case INHIBIT_ANY_OID:
- VERIFY_AND_SET_OID(cert->inhibitAnyOidSet);
- WOLFSSL_MSG("Inhibit anyPolicy extension not supported yet.");
- break;
-
- #ifndef IGNORE_NETSCAPE_CERT_TYPE
- case NETSCAPE_CT_OID:
- WOLFSSL_MSG("Netscape certificate type extension not supported "
- "yet.");
- if (CheckBitString(input, &idx, &length, idx + length, 0,
- NULL) < 0) {
- return ASN_PARSE_E;
- }
- break;
- #endif
-
- default:
- #ifndef WOLFSSL_NO_ASN_STRICT
- /* While it is a failure to not support critical extensions,
- * still parse the certificate ignoring the unsupported
- * extension to allow caller to accept it with the verify
- * callback. */
- if (critical)
- criticalFail = 1;
- #endif
- break;
- }
- idx += length;
- }
-
- return criticalFail ? ASN_CRIT_EXT_E : 0;
-}
-
-int ParseCert(DecodedCert* cert, int type, int verify, void* cm)
-{
- int ret;
- char* ptr;
-
- ret = ParseCertRelative(cert, type, verify, cm);
- if (ret < 0)
- return ret;
-
- if (cert->subjectCNLen > 0) {
- ptr = (char*) XMALLOC(cert->subjectCNLen + 1, cert->heap,
- DYNAMIC_TYPE_SUBJECT_CN);
- if (ptr == NULL)
- return MEMORY_E;
- XMEMCPY(ptr, cert->subjectCN, cert->subjectCNLen);
- ptr[cert->subjectCNLen] = '\0';
- cert->subjectCN = ptr;
- cert->subjectCNStored = 1;
- }
-
- if (cert->keyOID == RSAk &&
- cert->publicKey != NULL && cert->pubKeySize > 0) {
- ptr = (char*) XMALLOC(cert->pubKeySize, cert->heap,
- DYNAMIC_TYPE_PUBLIC_KEY);
- if (ptr == NULL)
- return MEMORY_E;
- XMEMCPY(ptr, cert->publicKey, cert->pubKeySize);
- cert->publicKey = (byte *)ptr;
- cert->pubKeyStored = 1;
- }
-
- return ret;
-}
-
-/* from SSL proper, for locking can't do find here anymore */
-#ifdef __cplusplus
- extern "C" {
-#endif
- Signer* GetCA(void* signers, byte* hash);
- #ifndef NO_SKID
- Signer* GetCAByName(void* signers, byte* hash);
- #endif
-#ifdef __cplusplus
- }
-#endif
-
-#if defined(WOLFCRYPT_ONLY) || defined(NO_CERTS)
-
-/* dummy functions, not using wolfSSL so don't need actual ones */
-Signer* GetCA(void* signers, byte* hash)
-{
- (void)hash;
-
- return (Signer*)signers;
-}
-
-#ifndef NO_SKID
-Signer* GetCAByName(void* signers, byte* hash)
-{
- (void)hash;
-
- return (Signer*)signers;
-}
-#endif /* NO_SKID */
-
-#endif /* WOLFCRYPT_ONLY || NO_CERTS */
-
-#if defined(WOLFSSL_NO_TRUSTED_CERTS_VERIFY) && !defined(NO_SKID)
-static Signer* GetCABySubjectAndPubKey(DecodedCert* cert, void* cm)
-{
- Signer* ca = NULL;
- if (cert->extSubjKeyIdSet)
- ca = GetCA(cm, cert->extSubjKeyId);
- if (ca == NULL)
- ca = GetCAByName(cm, cert->subjectHash);
- if (ca) {
- if ((ca->pubKeySize == cert->pubKeySize) &&
- (XMEMCMP(ca->publicKey, cert->publicKey, ca->pubKeySize) == 0)) {
- return ca;
- }
- }
- return NULL;
-}
-#endif
-
-#if defined(WOLFSSL_SMALL_CERT_VERIFY) || defined(OPENSSL_EXTRA)
-/* Only quick step through the certificate to find fields that are then used
- * in certificate signature verification.
- * Must use the signature OID from the signed part of the certificate.
- *
- * This is only for minimizing dynamic memory usage during TLS certificate
- * chain processing.
- * Doesn't support:
- * OCSP Only: alt lookup using subject and pub key w/o sig check
- */
-static int CheckCertSignature_ex(const byte* cert, word32 certSz, void* heap,
- void* cm, const byte* pubKey, word32 pubKeySz, int pubKeyOID)
-{
-#ifndef WOLFSSL_SMALL_STACK
- SignatureCtx sigCtx[1];
-#else
- SignatureCtx* sigCtx;
-#endif
- byte hash[KEYID_SIZE];
- Signer* ca = NULL;
- word32 idx = 0;
- int len;
- word32 tbsCertIdx = 0;
- word32 sigIndex = 0;
- word32 signatureOID = 0;
- word32 oid = 0;
- word32 issuerIdx = 0;
- word32 issuerSz = 0;
-#ifndef NO_SKID
- int extLen = 0;
- word32 extIdx = 0;
- word32 extEndIdx = 0;
- int extAuthKeyIdSet = 0;
-#endif
- int ret = 0;
- word32 localIdx;
- byte tag;
-
-
- if (cert == NULL) {
- return BAD_FUNC_ARG;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- sigCtx = (SignatureCtx*)XMALLOC(sizeof(*sigCtx), heap, DYNAMIC_TYPE_SIGNATURE);
- if (sigCtx == NULL)
- return MEMORY_E;
-#endif
- InitSignatureCtx(sigCtx, heap, INVALID_DEVID);
-
- /* Certificate SEQUENCE */
- if (GetSequence(cert, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- if (ret == 0) {
- tbsCertIdx = idx;
-
- /* TBSCertificate SEQUENCE */
- if (GetSequence(cert, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- }
- if (ret == 0) {
- sigIndex = len + idx;
-
- if ((idx + 1) > certSz)
- ret = BUFFER_E;
- }
- if (ret == 0) {
- /* version - optional */
- localIdx = idx;
- if (GetASNTag(cert, &localIdx, &tag, certSz) == 0) {
- if (tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED)) {
- idx++;
- if (GetLength(cert, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- idx += len;
- }
- }
- }
-
- if (ret == 0) {
- /* serialNumber */
- if (GetASNHeader(cert, ASN_INTEGER, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- }
- if (ret == 0) {
- idx += len;
-
- /* signature */
- if (GetAlgoId(cert, &idx, &signatureOID, oidSigType, certSz) < 0)
- ret = ASN_PARSE_E;
- }
-
- if (ret == 0) {
- issuerIdx = idx;
- /* issuer */
- if (GetSequence(cert, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- }
- if (ret == 0) {
- issuerSz = len + idx - issuerIdx;
- }
-#ifndef NO_SKID
- if (ret == 0) {
- idx += len;
-
- /* validity */
- if (GetSequence(cert, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- }
- if (ret == 0) {
- idx += len;
-
- /* subject */
- if (GetSequence(cert, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- }
- if (ret == 0) {
- idx += len;
-
- /* subjectPublicKeyInfo */
- if (GetSequence(cert, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- }
- if (ret == 0) {
- idx += len;
-
- if ((idx + 1) > certSz)
- ret = BUFFER_E;
- }
- if (ret == 0) {
- /* issuerUniqueID - optional */
- localIdx = idx;
- if (GetASNTag(cert, &localIdx, &tag, certSz) == 0) {
- if (tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1)) {
- idx++;
- if (GetLength(cert, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- idx += len;
- }
- }
- }
- if (ret == 0) {
- if ((idx + 1) > certSz)
- ret = BUFFER_E;
- }
- if (ret == 0) {
- /* subjectUniqueID - optional */
- localIdx = idx;
- if (GetASNTag(cert, &localIdx, &tag, certSz) == 0) {
- if (tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 2)) {
- idx++;
- if (GetLength(cert, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- idx += len;
- }
- }
- }
-
- if (ret == 0) {
- if ((idx + 1) > certSz)
- ret = BUFFER_E;
- }
- /* extensions - optional */
- localIdx = idx;
- if (ret == 0 && GetASNTag(cert, &localIdx, &tag, certSz) == 0 &&
- tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 3)) {
- idx++;
- if (GetLength(cert, &idx, &extLen, certSz) < 0)
- ret = ASN_PARSE_E;
- if (ret == 0) {
- if (GetSequence(cert, &idx, &extLen, certSz) < 0)
- ret = ASN_PARSE_E;
- }
- if (ret == 0) {
- extEndIdx = idx + extLen;
-
- /* Check each extension for the ones we want. */
- while (ret == 0 && idx < extEndIdx) {
- if (GetSequence(cert, &idx, &len, certSz) < 0)
- ret = ASN_PARSE_E;
- if (ret == 0) {
- extIdx = idx;
- if (GetObjectId(cert, &extIdx, &oid, oidCertExtType,
- certSz) < 0) {
- ret = ASN_PARSE_E;
- }
-
- if (ret == 0) {
- if ((extIdx + 1) > certSz)
- ret = BUFFER_E;
- }
- }
-
- if (ret == 0) {
- localIdx = extIdx;
- if (GetASNTag(cert, &localIdx, &tag, certSz) == 0 &&
- tag == ASN_BOOLEAN) {
- if (GetBoolean(cert, &extIdx, certSz) < 0)
- ret = ASN_PARSE_E;
- }
- }
- if (ret == 0) {
- if (GetOctetString(cert, &extIdx, &extLen, certSz) < 0)
- ret = ASN_PARSE_E;
- }
-
- if (ret == 0) {
- switch (oid) {
- case AUTH_KEY_OID:
- if (GetSequence(cert, &extIdx, &extLen, certSz) < 0)
- ret = ASN_PARSE_E;
-
- if (ret == 0 && (extIdx + 1) >= certSz)
- ret = BUFFER_E;
-
- if (ret == 0 &&
- GetASNTag(cert, &extIdx, &tag, certSz) == 0 &&
- tag == (ASN_CONTEXT_SPECIFIC | 0)) {
- if (GetLength(cert, &extIdx, &extLen, certSz) <= 0)
- ret = ASN_PARSE_E;
- if (ret == 0) {
- extAuthKeyIdSet = 1;
- if (extLen == KEYID_SIZE)
- XMEMCPY(hash, cert + extIdx, extLen);
- else {
- ret = CalcHashId(cert + extIdx, extLen,
- hash);
- }
- }
- }
- break;
-
- default:
- break;
- }
- }
- idx += len;
- }
- }
- }
-
- if (ret == 0 && pubKey == NULL) {
- if (extAuthKeyIdSet)
- ca = GetCA(cm, hash);
- if (ca == NULL) {
- ret = CalcHashId(cert + issuerIdx, issuerSz, hash);
- if (ret == 0)
- ca = GetCAByName(cm, hash);
- }
- }
-#else
- if (ret == 0 && pubKey == NULL) {
- ret = CalcHashId(cert + issuerIdx, issuerSz, hash);
- if (ret == 0)
- ca = GetCA(cm, hash);
- }
-#endif /* !NO_SKID */
- if (ca == NULL && pubKey == NULL)
- ret = ASN_NO_SIGNER_E;
-
- if (ret == 0) {
- idx = sigIndex;
- /* signatureAlgorithm */
- if (GetAlgoId(cert, &idx, &oid, oidSigType, certSz) < 0)
- ret = ASN_PARSE_E;
- }
- if (ret == 0) {
- if (oid != signatureOID)
- ret = ASN_SIG_OID_E;
- }
- if (ret == 0) {
- /* signatureValue */
- if (CheckBitString(cert, &idx, &len, certSz, 1, NULL) < 0)
- ret = ASN_PARSE_E;
- }
-
- if (ret == 0) {
- if (pubKey != NULL) {
- ret = ConfirmSignature(sigCtx, cert + tbsCertIdx,
- sigIndex - tbsCertIdx,
- pubKey, pubKeySz, pubKeyOID,
- cert + idx, len, signatureOID, NULL);
- }
- else {
- ret = ConfirmSignature(sigCtx, cert + tbsCertIdx,
- sigIndex - tbsCertIdx,
- ca->publicKey, ca->pubKeySize, ca->keyOID,
- cert + idx, len, signatureOID, NULL);
- }
- if (ret != 0) {
- WOLFSSL_MSG("Confirm signature failed");
- }
- }
-
- FreeSignatureCtx(sigCtx);
-#ifdef WOLFSSL_SMALL_STACK
- if (sigCtx != NULL)
- XFREE(sigCtx, heap, DYNAMIC_TYPE_SIGNATURE);
-#endif
- return ret;
-}
-
-#ifdef OPENSSL_EXTRA
-/* Call CheckCertSignature_ex using a public key buffer for verification
- */
-int CheckCertSignaturePubKey(const byte* cert, word32 certSz, void* heap,
- const byte* pubKey, word32 pubKeySz, int pubKeyOID)
-{
- return CheckCertSignature_ex(cert, certSz, heap, NULL,
- pubKey, pubKeySz, pubKeyOID);
-}
-#endif /* OPENSSL_EXTRA */
-#ifdef WOLFSSL_SMALL_CERT_VERIFY
-/* Call CheckCertSignature_ex using a certificate manager (cm)
- */
-int CheckCertSignature(const byte* cert, word32 certSz, void* heap, void* cm)
-{
- return CheckCertSignature_ex(cert, certSz, heap, cm, NULL, 0, 0);
-}
-#endif /* WOLFSSL_SMALL_CERT_VERIFY */
-#endif /* WOLFSSL_SMALL_CERT_VERIFY || OPENSSL_EXTRA */
-
-int ParseCertRelative(DecodedCert* cert, int type, int verify, void* cm)
-{
- int ret = 0;
- int checkPathLen = 0;
- int decrementMaxPathLen = 0;
- word32 confirmOID;
-#if defined(WOLFSSL_RENESAS_TSIP)
- int idx = 0;
-#endif
- byte* tsip_encRsaKeyIdx;
-
- if (cert == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (cert->sigCtx.state == SIG_STATE_BEGIN) {
- cert->badDate = 0;
- cert->criticalExt = 0;
- if ((ret = DecodeToKey(cert, verify)) < 0) {
- if (ret == ASN_BEFORE_DATE_E || ret == ASN_AFTER_DATE_E)
- cert->badDate = ret;
- else
- return ret;
- }
-
- WOLFSSL_MSG("Parsed Past Key");
-
- if (cert->srcIdx < cert->sigIndex) {
- #ifndef ALLOW_V1_EXTENSIONS
- if (cert->version < 2) {
- WOLFSSL_MSG("\tv1 and v2 certs not allowed extensions");
- return ASN_VERSION_E;
- }
- #endif
-
- /* save extensions */
- cert->extensions = &cert->source[cert->srcIdx];
- cert->extensionsSz = cert->sigIndex - cert->srcIdx;
- cert->extensionsIdx = cert->srcIdx; /* for potential later use */
-
- if ((ret = DecodeCertExtensions(cert)) < 0) {
- if (ret == ASN_CRIT_EXT_E)
- cert->criticalExt = ret;
- else
- return ret;
- }
-
- /* advance past extensions */
- cert->srcIdx = cert->sigIndex;
- }
-
- if ((ret = GetAlgoId(cert->source, &cert->srcIdx, &confirmOID,
- oidSigType, cert->maxIdx)) < 0)
- return ret;
-
- if ((ret = GetSignature(cert)) < 0)
- return ret;
-
- if (confirmOID != cert->signatureOID)
- return ASN_SIG_OID_E;
-
- #ifndef NO_SKID
- if (cert->extSubjKeyIdSet == 0 && cert->publicKey != NULL &&
- cert->pubKeySize > 0) {
- ret = CalcHashId(cert->publicKey, cert->pubKeySize,
- cert->extSubjKeyId);
- if (ret != 0)
- return ret;
- }
- #endif /* !NO_SKID */
-
- if (!cert->selfSigned || (verify != NO_VERIFY && type != CA_TYPE &&
- type != TRUSTED_PEER_TYPE)) {
- cert->ca = NULL;
- #ifndef NO_SKID
- if (cert->extAuthKeyIdSet) {
- cert->ca = GetCA(cm, cert->extAuthKeyId);
- }
- if (cert->ca == NULL && cert->extSubjKeyIdSet
- && verify != VERIFY_OCSP) {
- cert->ca = GetCA(cm, cert->extSubjKeyId);
- }
- if (cert->ca != NULL && XMEMCMP(cert->issuerHash,
- cert->ca->subjectNameHash, KEYID_SIZE) != 0) {
- cert->ca = NULL;
- }
- if (cert->ca == NULL) {
- cert->ca = GetCAByName(cm, cert->issuerHash);
- /* If AKID is available then this CA doesn't have the public
- * key required */
- if (cert->ca && cert->extAuthKeyIdSet) {
- WOLFSSL_MSG("CA SKID doesn't match AKID");
- cert->ca = NULL;
- }
- }
-
- /* OCSP Only: alt lookup using subject and pub key w/o sig check */
- #ifdef WOLFSSL_NO_TRUSTED_CERTS_VERIFY
- if (cert->ca == NULL && verify == VERIFY_OCSP) {
- cert->ca = GetCABySubjectAndPubKey(cert, cm);
- if (cert->ca) {
- ret = 0; /* success */
- goto exit_pcr;
- }
- }
- #endif /* WOLFSSL_NO_TRUSTED_CERTS_VERIFY */
- #else
- cert->ca = GetCA(cm, cert->issuerHash);
- #endif /* !NO_SKID */
- }
-
- if (cert->selfSigned) {
- cert->maxPathLen = WOLFSSL_MAX_PATH_LEN;
- } else {
- /* RFC 5280 Section 4.2.1.9:
- *
- * load/receive check
- *
- * 1) Is CA boolean set?
- * No - SKIP CHECK
- * Yes - Check key usage
- * 2) Is Key usage extension present?
- * No - goto 3
- * Yes - check keyCertSign assertion
- * 2.a) Is keyCertSign asserted?
- * No - goto 4
- * Yes - goto 3
- * 3) Is pathLen set?
- * No - goto 4
- * Yes - check pathLen against maxPathLen.
- * 3.a) Is pathLen less than maxPathLen?
- * No - goto 4
- * Yes - set maxPathLen to pathLen and EXIT
- * 4) Is maxPathLen > 0?
- * Yes - Reduce by 1
- * No - ERROR
- */
-
- if (cert->ca && cert->pathLengthSet) {
- cert->maxPathLen = cert->pathLength;
- if (cert->isCA) {
- WOLFSSL_MSG("\tCA boolean set");
- if (cert->extKeyUsageSet) {
- WOLFSSL_MSG("\tExtension Key Usage Set");
- if ((cert->extKeyUsage & KEYUSE_KEY_CERT_SIGN) != 0) {
- checkPathLen = 1;
- } else {
- decrementMaxPathLen = 1;
- }
- } else {
- checkPathLen = 1;
- } /* !cert->ca check */
- } /* cert is not a CA (assuming entity cert) */
-
- if (checkPathLen && cert->pathLengthSet) {
- if (cert->pathLength < cert->ca->maxPathLen) {
- WOLFSSL_MSG("\tmaxPathLen status: set to pathLength");
- cert->maxPathLen = cert->pathLength;
- } else {
- decrementMaxPathLen = 1;
- }
- }
-
- if (decrementMaxPathLen && cert->ca->maxPathLen > 0) {
- WOLFSSL_MSG("\tmaxPathLen status: reduce by 1");
- cert->maxPathLen = cert->ca->maxPathLen - 1;
- if (verify != NO_VERIFY && type != CA_TYPE &&
- type != TRUSTED_PEER_TYPE) {
- WOLFSSL_MSG("\tmaxPathLen status: OK");
- }
- } else if (decrementMaxPathLen && cert->ca->maxPathLen == 0) {
- cert->maxPathLen = 0;
- if (verify != NO_VERIFY && type != CA_TYPE &&
- type != TRUSTED_PEER_TYPE) {
- WOLFSSL_MSG("\tNon-entity cert, maxPathLen is 0");
- WOLFSSL_MSG("\tmaxPathLen status: ERROR");
- return ASN_PATHLEN_INV_E;
- }
- }
- } else if (cert->ca && cert->isCA) {
- /* case where cert->pathLength extension is not set */
- if (cert->ca->maxPathLen > 0) {
- cert->maxPathLen = cert->ca->maxPathLen - 1;
- } else {
- cert->maxPathLen = 0;
- if (verify != NO_VERIFY && type != CA_TYPE &&
- type != TRUSTED_PEER_TYPE) {
- WOLFSSL_MSG("\tNon-entity cert, maxPathLen is 0");
- WOLFSSL_MSG("\tmaxPathLen status: ERROR");
- return ASN_PATHLEN_INV_E;
- }
- }
- }
- #ifdef HAVE_OCSP
- if (verify != NO_VERIFY && type != CA_TYPE &&
- type != TRUSTED_PEER_TYPE) {
- if (cert->ca) {
- /* Need the CA's public key hash for OCSP */
- XMEMCPY(cert->issuerKeyHash, cert->ca->subjectKeyHash,
- KEYID_SIZE);
- }
-
- }
- #endif /* HAVE_OCSP */
- }
- }
-#if defined(WOLFSSL_RENESAS_TSIP)
- /* prepare for TSIP TLS cert verification API use */
- if (cert->keyOID == RSAk) {
- /* to call TSIP API, it needs keys position info in bytes */
- if ((ret = RsaPublicKeyDecodeRawIndex(cert->publicKey, (word32*)&idx,
- cert->pubKeySize,
- &cert->sigCtx.pubkey_n_start,
- &cert->sigCtx.pubkey_n_len,
- &cert->sigCtx.pubkey_e_start,
- &cert->sigCtx.pubkey_e_len)) != 0) {
- WOLFSSL_MSG("Decoding index from cert failed.");
- return ret;
- }
- cert->sigCtx.certBegin = cert->certBegin;
- }
- /* check if we can use TSIP for cert verification */
- /* if the ca is verified as tsip root ca. */
- /* TSIP can only handle 2048 bits(256 byte) key. */
- if (cert->ca && tsip_checkCA(cert->ca->cm_idx) != 0 &&
- cert->sigCtx.pubkey_n_len == 256) {
-
- /* assign memory to encrypted tsip Rsa key index */
- if (!cert->tsip_encRsaKeyIdx)
- cert->tsip_encRsaKeyIdx =
- (byte*)XMALLOC(TSIP_TLS_ENCPUBKEY_SZ_BY_CERTVRFY,
- cert->heap, DYNAMIC_TYPE_RSA);
- if (cert->tsip_encRsaKeyIdx == NULL)
- return MEMORY_E;
- } else {
- if (cert->ca) {
- /* TSIP isn't usable */
- if (tsip_checkCA(cert->ca->cm_idx) == 0)
- WOLFSSL_MSG("TSIP isn't usable because the ca isn't verified "
- "by TSIP.");
- else if (cert->sigCtx.pubkey_n_len != 256)
- WOLFSSL_MSG("TSIP isn't usable because the ca isn't signed by "
- "RSA 2048.");
- else
- WOLFSSL_MSG("TSIP isn't usable");
- }
- cert->tsip_encRsaKeyIdx = NULL;
- }
-
- tsip_encRsaKeyIdx = cert->tsip_encRsaKeyIdx;
-#else
- tsip_encRsaKeyIdx = NULL;
-#endif
-
- if (verify != NO_VERIFY && type != CA_TYPE && type != TRUSTED_PEER_TYPE) {
- if (cert->ca) {
- if (verify == VERIFY || verify == VERIFY_OCSP ||
- verify == VERIFY_SKIP_DATE) {
- /* try to confirm/verify signature */
- if ((ret = ConfirmSignature(&cert->sigCtx,
- cert->source + cert->certBegin,
- cert->sigIndex - cert->certBegin,
- cert->ca->publicKey, cert->ca->pubKeySize,
- cert->ca->keyOID, cert->signature,
- cert->sigLength, cert->signatureOID,
- tsip_encRsaKeyIdx)) != 0) {
- if (ret != 0 && ret != WC_PENDING_E) {
- WOLFSSL_MSG("Confirm signature failed");
- }
- return ret;
- }
- }
- #ifndef IGNORE_NAME_CONSTRAINTS
- if (verify == VERIFY || verify == VERIFY_OCSP ||
- verify == VERIFY_NAME || verify == VERIFY_SKIP_DATE) {
- /* check that this cert's name is permitted by the signer's
- * name constraints */
- if (!ConfirmNameConstraints(cert->ca, cert)) {
- WOLFSSL_MSG("Confirm name constraint failed");
- return ASN_NAME_INVALID_E;
- }
- }
- #endif /* IGNORE_NAME_CONSTRAINTS */
- }
- else {
- /* no signer */
- WOLFSSL_MSG("No CA signer to verify with");
- return ASN_NO_SIGNER_E;
- }
- }
-
-#if defined(WOLFSSL_NO_TRUSTED_CERTS_VERIFY) && !defined(NO_SKID)
-exit_pcr:
-#endif
-
- if (cert->badDate != 0) {
- if (verify != VERIFY_SKIP_DATE) {
- return cert->badDate;
- }
- WOLFSSL_MSG("Date error: Verify option is skipping");
- }
-
- if (cert->criticalExt != 0)
- return cert->criticalExt;
-
- return ret;
-}
-
-/* Create and init an new signer */
-Signer* MakeSigner(void* heap)
-{
- Signer* signer = (Signer*) XMALLOC(sizeof(Signer), heap,
- DYNAMIC_TYPE_SIGNER);
- if (signer) {
- XMEMSET(signer, 0, sizeof(Signer));
- }
- (void)heap;
-
- return signer;
-}
-
-
-/* Free an individual signer */
-void FreeSigner(Signer* signer, void* heap)
-{
- XFREE(signer->name, heap, DYNAMIC_TYPE_SUBJECT_CN);
- XFREE((void*)signer->publicKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
-#ifndef IGNORE_NAME_CONSTRAINTS
- if (signer->permittedNames)
- FreeNameSubtrees(signer->permittedNames, heap);
- if (signer->excludedNames)
- FreeNameSubtrees(signer->excludedNames, heap);
-#endif
-#ifdef WOLFSSL_SIGNER_DER_CERT
- FreeDer(&signer->derCert);
-#endif
- XFREE(signer, heap, DYNAMIC_TYPE_SIGNER);
-
- (void)heap;
-}
-
-
-/* Free the whole singer table with number of rows */
-void FreeSignerTable(Signer** table, int rows, void* heap)
-{
- int i;
-
- for (i = 0; i < rows; i++) {
- Signer* signer = table[i];
- while (signer) {
- Signer* next = signer->next;
- FreeSigner(signer, heap);
- signer = next;
- }
- table[i] = NULL;
- }
-}
-
-#ifdef WOLFSSL_TRUST_PEER_CERT
-/* Free an individual trusted peer cert */
-void FreeTrustedPeer(TrustedPeerCert* tp, void* heap)
-{
- if (tp == NULL) {
- return;
- }
-
- if (tp->name) {
- XFREE(tp->name, heap, DYNAMIC_TYPE_SUBJECT_CN);
- }
-
- if (tp->sig) {
- XFREE(tp->sig, heap, DYNAMIC_TYPE_SIGNATURE);
- }
-#ifndef IGNORE_NAME_CONSTRAINTS
- if (tp->permittedNames)
- FreeNameSubtrees(tp->permittedNames, heap);
- if (tp->excludedNames)
- FreeNameSubtrees(tp->excludedNames, heap);
-#endif
- XFREE(tp, heap, DYNAMIC_TYPE_CERT);
-
- (void)heap;
-}
-
-/* Free the whole Trusted Peer linked list */
-void FreeTrustedPeerTable(TrustedPeerCert** table, int rows, void* heap)
-{
- int i;
-
- for (i = 0; i < rows; i++) {
- TrustedPeerCert* tp = table[i];
- while (tp) {
- TrustedPeerCert* next = tp->next;
- FreeTrustedPeer(tp, heap);
- tp = next;
- }
- table[i] = NULL;
- }
-}
-#endif /* WOLFSSL_TRUST_PEER_CERT */
-
-int SetMyVersion(word32 version, byte* output, int header)
-{
- int i = 0;
-
- if (output == NULL)
- return BAD_FUNC_ARG;
-
- if (header) {
- output[i++] = ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED;
- output[i++] = 3;
- }
- output[i++] = ASN_INTEGER;
- output[i++] = 0x01;
- output[i++] = (byte)version;
-
- return i;
-}
-
-int SetSerialNumber(const byte* sn, word32 snSz, byte* output,
- word32 outputSz, int maxSnSz)
-{
- int i;
- int snSzInt = (int)snSz;
-
- if (sn == NULL || output == NULL || snSzInt < 0)
- return BAD_FUNC_ARG;
-
- /* remove leading zeros */
- while (snSzInt > 0 && sn[0] == 0) {
- snSzInt--;
- sn++;
- }
- /* RFC 5280 - 4.1.2.2:
- * Serial numbers must be a positive value (and not zero) */
- if (snSzInt == 0)
- return BAD_FUNC_ARG;
-
- if (sn[0] & 0x80)
- maxSnSz--;
- /* truncate if input is too long */
- if (snSzInt > maxSnSz)
- snSzInt = maxSnSz;
-
- i = SetASNInt(snSzInt, sn[0], NULL);
- /* truncate if input is too long */
- if (snSzInt > (int)outputSz - i)
- snSzInt = (int)outputSz - i;
- /* sanity check number of bytes to copy */
- if (snSzInt <= 0) {
- return BUFFER_E;
- }
-
- /* write out ASN.1 Integer */
- (void)SetASNInt(snSzInt, sn[0], output);
- XMEMCPY(output + i, sn, snSzInt);
-
- /* compute final length */
- i += snSzInt;
-
- return i;
-}
-
-#endif /* !NO_CERTS */
-
-int GetSerialNumber(const byte* input, word32* inOutIdx,
- byte* serial, int* serialSz, word32 maxIdx)
-{
- int result = 0;
- int ret;
-
- WOLFSSL_ENTER("GetSerialNumber");
-
- if (serial == NULL || input == NULL || serialSz == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* First byte is ASN type */
- if ((*inOutIdx+1) > maxIdx) {
- WOLFSSL_MSG("Bad idx first");
- return BUFFER_E;
- }
-
- ret = GetASNInt(input, inOutIdx, serialSz, maxIdx);
- if (ret != 0)
- return ret;
-
- if (*serialSz > EXTERNAL_SERIAL_SIZE) {
- WOLFSSL_MSG("Serial size bad");
- return ASN_PARSE_E;
- }
-
- /* return serial */
- XMEMCPY(serial, &input[*inOutIdx], *serialSz);
- *inOutIdx += *serialSz;
-
- return result;
-}
-
-#ifndef NO_CERTS
-
-int AllocDer(DerBuffer** pDer, word32 length, int type, void* heap)
-{
- int ret = BAD_FUNC_ARG;
- if (pDer) {
- int dynType = 0;
- DerBuffer* der;
-
- /* Determine dynamic type */
- switch (type) {
- case CA_TYPE: dynType = DYNAMIC_TYPE_CA; break;
- case CERT_TYPE: dynType = DYNAMIC_TYPE_CERT; break;
- case CRL_TYPE: dynType = DYNAMIC_TYPE_CRL; break;
- case DSA_TYPE: dynType = DYNAMIC_TYPE_DSA; break;
- case ECC_TYPE: dynType = DYNAMIC_TYPE_ECC; break;
- case RSA_TYPE: dynType = DYNAMIC_TYPE_RSA; break;
- default: dynType = DYNAMIC_TYPE_KEY; break;
- }
-
- /* Setup new buffer */
- *pDer = (DerBuffer*)XMALLOC(sizeof(DerBuffer) + length, heap, dynType);
- if (*pDer == NULL) {
- return MEMORY_E;
- }
- XMEMSET(*pDer, 0, sizeof(DerBuffer) + length);
-
- der = *pDer;
- der->type = type;
- der->dynType = dynType; /* Cache this for FreeDer */
- der->heap = heap;
- der->buffer = (byte*)der + sizeof(DerBuffer);
- der->length = length;
- ret = 0; /* Success */
- }
- return ret;
-}
-
-void FreeDer(DerBuffer** pDer)
-{
- if (pDer && *pDer)
- {
- DerBuffer* der = (DerBuffer*)*pDer;
-
- /* ForceZero private keys */
- if (der->type == PRIVATEKEY_TYPE) {
- ForceZero(der->buffer, der->length);
- }
- der->buffer = NULL;
- der->length = 0;
- XFREE(der, der->heap, der->dynType);
-
- *pDer = NULL;
- }
-}
-
-int wc_AllocDer(DerBuffer** pDer, word32 length, int type, void* heap)
-{
- return AllocDer(pDer, length, type, heap);
-}
-void wc_FreeDer(DerBuffer** pDer)
-{
- FreeDer(pDer);
-}
-
-
-#if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
-
-/* Max X509 header length indicates the max length + 2 ('\n', '\0') */
-#define MAX_X509_HEADER_SZ (37 + 2)
-
-wcchar BEGIN_CERT = "-----BEGIN CERTIFICATE-----";
-wcchar END_CERT = "-----END CERTIFICATE-----";
-#ifdef WOLFSSL_CERT_REQ
- wcchar BEGIN_CERT_REQ = "-----BEGIN CERTIFICATE REQUEST-----";
- wcchar END_CERT_REQ = "-----END CERTIFICATE REQUEST-----";
-#endif
-#ifndef NO_DH
- wcchar BEGIN_DH_PARAM = "-----BEGIN DH PARAMETERS-----";
- wcchar END_DH_PARAM = "-----END DH PARAMETERS-----";
-#endif
-#ifndef NO_DSA
- wcchar BEGIN_DSA_PARAM = "-----BEGIN DSA PARAMETERS-----";
- wcchar END_DSA_PARAM = "-----END DSA PARAMETERS-----";
-#endif
-wcchar BEGIN_X509_CRL = "-----BEGIN X509 CRL-----";
-wcchar END_X509_CRL = "-----END X509 CRL-----";
-wcchar BEGIN_RSA_PRIV = "-----BEGIN RSA PRIVATE KEY-----";
-wcchar END_RSA_PRIV = "-----END RSA PRIVATE KEY-----";
-wcchar BEGIN_PRIV_KEY = "-----BEGIN PRIVATE KEY-----";
-wcchar END_PRIV_KEY = "-----END PRIVATE KEY-----";
-wcchar BEGIN_ENC_PRIV_KEY = "-----BEGIN ENCRYPTED PRIVATE KEY-----";
-wcchar END_ENC_PRIV_KEY = "-----END ENCRYPTED PRIVATE KEY-----";
-#ifdef HAVE_ECC
- wcchar BEGIN_EC_PRIV = "-----BEGIN EC PRIVATE KEY-----";
- wcchar END_EC_PRIV = "-----END EC PRIVATE KEY-----";
-#endif
-#if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448) || \
- !defined(NO_DSA)
- wcchar BEGIN_DSA_PRIV = "-----BEGIN DSA PRIVATE KEY-----";
- wcchar END_DSA_PRIV = "-----END DSA PRIVATE KEY-----";
-#endif
-#ifdef OPENSSL_EXTRA
- const char BEGIN_PRIV_KEY_PREFIX[] = "-----BEGIN";
- const char PRIV_KEY_SUFFIX[] = "PRIVATE KEY-----";
- const char END_PRIV_KEY_PREFIX[] = "-----END";
-#endif
-wcchar BEGIN_PUB_KEY = "-----BEGIN PUBLIC KEY-----";
-wcchar END_PUB_KEY = "-----END PUBLIC KEY-----";
-#if defined(HAVE_ED25519) || defined(HAVE_ED448)
- wcchar BEGIN_EDDSA_PRIV = "-----BEGIN EDDSA PRIVATE KEY-----";
- wcchar END_EDDSA_PRIV = "-----END EDDSA PRIVATE KEY-----";
-#endif
-#ifdef HAVE_CRL
- const char *const BEGIN_CRL = "-----BEGIN X509 CRL-----";
- wcchar END_CRL = "-----END X509 CRL-----";
-#endif
-
-
-static WC_INLINE char* SkipEndOfLineChars(char* line, const char* endOfLine)
-{
- /* eat end of line characters */
- while (line < endOfLine &&
- (line[0] == '\r' || line[0] == '\n')) {
- line++;
- }
- return line;
-}
-
-int wc_PemGetHeaderFooter(int type, const char** header, const char** footer)
-{
- int ret = BAD_FUNC_ARG;
-
- switch (type) {
- case CA_TYPE: /* same as below */
- case TRUSTED_PEER_TYPE:
- case CERT_TYPE:
- if (header) *header = BEGIN_CERT;
- if (footer) *footer = END_CERT;
- ret = 0;
- break;
-
- case CRL_TYPE:
- if (header) *header = BEGIN_X509_CRL;
- if (footer) *footer = END_X509_CRL;
- ret = 0;
- break;
- #ifndef NO_DH
- case DH_PARAM_TYPE:
- if (header) *header = BEGIN_DH_PARAM;
- if (footer) *footer = END_DH_PARAM;
- ret = 0;
- break;
- #endif
- #ifndef NO_DSA
- case DSA_PARAM_TYPE:
- if (header) *header = BEGIN_DSA_PARAM;
- if (footer) *footer = END_DSA_PARAM;
- ret = 0;
- break;
- #endif
- #ifdef WOLFSSL_CERT_REQ
- case CERTREQ_TYPE:
- if (header) *header = BEGIN_CERT_REQ;
- if (footer) *footer = END_CERT_REQ;
- ret = 0;
- break;
- #endif
- #ifndef NO_DSA
- case DSA_TYPE:
- case DSA_PRIVATEKEY_TYPE:
- if (header) *header = BEGIN_DSA_PRIV;
- if (footer) *footer = END_DSA_PRIV;
- ret = 0;
- break;
- #endif
- #ifdef HAVE_ECC
- case ECC_TYPE:
- case ECC_PRIVATEKEY_TYPE:
- if (header) *header = BEGIN_EC_PRIV;
- if (footer) *footer = END_EC_PRIV;
- ret = 0;
- break;
- #endif
- case RSA_TYPE:
- case PRIVATEKEY_TYPE:
- if (header) *header = BEGIN_RSA_PRIV;
- if (footer) *footer = END_RSA_PRIV;
- ret = 0;
- break;
- #ifdef HAVE_ED25519
- case ED25519_TYPE:
- #endif
- #ifdef HAVE_ED448
- case ED448_TYPE:
- #endif
- #if defined(HAVE_ED25519) || defined(HAVE_ED448)
- case EDDSA_PRIVATEKEY_TYPE:
- if (header) *header = BEGIN_EDDSA_PRIV;
- if (footer) *footer = END_EDDSA_PRIV;
- ret = 0;
- break;
- #endif
- case PUBLICKEY_TYPE:
- case ECC_PUBLICKEY_TYPE:
- if (header) *header = BEGIN_PUB_KEY;
- if (footer) *footer = END_PUB_KEY;
- ret = 0;
- break;
- #if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL))
- case DH_PRIVATEKEY_TYPE:
- #endif
- case PKCS8_PRIVATEKEY_TYPE:
- if (header) *header = BEGIN_PRIV_KEY;
- if (footer) *footer = END_PRIV_KEY;
- ret = 0;
- break;
- case PKCS8_ENC_PRIVATEKEY_TYPE:
- if (header) *header = BEGIN_ENC_PRIV_KEY;
- if (footer) *footer = END_ENC_PRIV_KEY;
- ret = 0;
- break;
- default:
- break;
- }
- return ret;
-}
-
-#ifdef WOLFSSL_ENCRYPTED_KEYS
-
-static wcchar kProcTypeHeader = "Proc-Type";
-static wcchar kDecInfoHeader = "DEK-Info";
-
-#ifdef WOLFSSL_PEM_TO_DER
-#ifndef NO_DES3
- static wcchar kEncTypeDes = "DES-CBC";
- static wcchar kEncTypeDes3 = "DES-EDE3-CBC";
-#endif
-#if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
- static wcchar kEncTypeAesCbc128 = "AES-128-CBC";
-#endif
-#if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_192)
- static wcchar kEncTypeAesCbc192 = "AES-192-CBC";
-#endif
-#if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
- static wcchar kEncTypeAesCbc256 = "AES-256-CBC";
-#endif
-
-int wc_EncryptedInfoGet(EncryptedInfo* info, const char* cipherInfo)
-{
- int ret = 0;
-
- if (info == NULL || cipherInfo == NULL)
- return BAD_FUNC_ARG;
-
- /* determine cipher information */
-#ifndef NO_DES3
- if (XSTRNCMP(cipherInfo, kEncTypeDes, XSTRLEN(kEncTypeDes)) == 0) {
- info->cipherType = WC_CIPHER_DES;
- info->keySz = DES_KEY_SIZE;
- if (info->ivSz == 0) info->ivSz = DES_IV_SIZE;
- }
- else if (XSTRNCMP(cipherInfo, kEncTypeDes3, XSTRLEN(kEncTypeDes3)) == 0) {
- info->cipherType = WC_CIPHER_DES3;
- info->keySz = DES3_KEY_SIZE;
- if (info->ivSz == 0) info->ivSz = DES_IV_SIZE;
- }
- else
-#endif /* !NO_DES3 */
-#if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
- if (XSTRNCMP(cipherInfo, kEncTypeAesCbc128, XSTRLEN(kEncTypeAesCbc128)) == 0) {
- info->cipherType = WC_CIPHER_AES_CBC;
- info->keySz = AES_128_KEY_SIZE;
- if (info->ivSz == 0) info->ivSz = AES_IV_SIZE;
- }
- else
-#endif
-#if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_192)
- if (XSTRNCMP(cipherInfo, kEncTypeAesCbc192, XSTRLEN(kEncTypeAesCbc192)) == 0) {
- info->cipherType = WC_CIPHER_AES_CBC;
- info->keySz = AES_192_KEY_SIZE;
- if (info->ivSz == 0) info->ivSz = AES_IV_SIZE;
- }
- else
-#endif
-#if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
- if (XSTRNCMP(cipherInfo, kEncTypeAesCbc256, XSTRLEN(kEncTypeAesCbc256)) == 0) {
- info->cipherType = WC_CIPHER_AES_CBC;
- info->keySz = AES_256_KEY_SIZE;
- if (info->ivSz == 0) info->ivSz = AES_IV_SIZE;
- }
- else
-#endif
- {
- ret = NOT_COMPILED_IN;
- }
- return ret;
-}
-
-int wc_EncryptedInfoParse(EncryptedInfo* info, char** pBuffer, size_t bufSz)
-{
- int err = 0;
- char* bufferStart;
- char* bufferEnd;
- char* line;
- word32 lineSz;
- char* finish;
- word32 finishSz;
- char* start = NULL;
- word32 startSz;
- char* newline = NULL;
-
- if (info == NULL || pBuffer == NULL || bufSz == 0)
- return BAD_FUNC_ARG;
-
- bufferStart = *pBuffer;
- bufferEnd = bufferStart + bufSz;
-
- /* find encrypted info marker */
- line = XSTRNSTR(bufferStart, kProcTypeHeader,
- min((word32)bufSz, PEM_LINE_LEN));
- if (line != NULL) {
- if (line >= bufferEnd) {
- return BUFFER_E;
- }
-
- lineSz = (word32)(bufferEnd - line);
-
- /* find DEC-Info marker */
- start = XSTRNSTR(line, kDecInfoHeader, min(lineSz, PEM_LINE_LEN));
-
- if (start == NULL)
- return BUFFER_E;
-
- /* skip dec-info and ": " */
- start += XSTRLEN(kDecInfoHeader);
- if (start >= bufferEnd)
- return BUFFER_E;
-
- if (start[0] == ':') {
- start++;
- if (start >= bufferEnd)
- return BUFFER_E;
- }
- if (start[0] == ' ')
- start++;
-
- startSz = (word32)(bufferEnd - start);
- finish = XSTRNSTR(start, ",", min(startSz, PEM_LINE_LEN));
-
- if ((start != NULL) && (finish != NULL) && (start < finish)) {
- if (finish >= bufferEnd) {
- return BUFFER_E;
- }
-
- finishSz = (word32)(bufferEnd - finish);
- newline = XSTRNSTR(finish, "\r", min(finishSz, PEM_LINE_LEN));
-
- /* get cipher name */
- if (NAME_SZ < (finish - start)) /* buffer size of info->name */
- return BUFFER_E;
- if (XMEMCPY(info->name, start, finish - start) == NULL)
- return BUFFER_E;
- info->name[finish - start] = '\0'; /* null term */
-
- /* populate info */
- err = wc_EncryptedInfoGet(info, info->name);
- if (err != 0)
- return err;
-
- /* get IV */
- if (finishSz < info->ivSz + 1)
- return BUFFER_E;
-
- if (newline == NULL) {
- newline = XSTRNSTR(finish, "\n", min(finishSz,
- PEM_LINE_LEN));
- }
- if ((newline != NULL) && (newline > finish)) {
- finish++;
- info->ivSz = (word32)(newline - finish);
- if (info->ivSz > IV_SZ)
- return BUFFER_E;
- if (XMEMCPY(info->iv, finish, info->ivSz) == NULL)
- return BUFFER_E;
- info->set = 1;
- }
- else
- return BUFFER_E;
- }
- else
- return BUFFER_E;
-
- /* eat end of line characters */
- newline = SkipEndOfLineChars(newline, bufferEnd);
-
- /* return new headerEnd */
-
- *pBuffer = newline;
- }
-
- return err;
-}
-#endif /* WOLFSSL_PEM_TO_DER */
-
-#ifdef WOLFSSL_DER_TO_PEM
-static int wc_EncryptedInfoAppend(char* dest, int destSz, char* cipherInfo)
-{
- if (cipherInfo != NULL) {
- int cipherInfoStrLen = (int)XSTRLEN((char*)cipherInfo);
-
- if (cipherInfoStrLen > HEADER_ENCRYPTED_KEY_SIZE - (9+14+10+3))
- cipherInfoStrLen = HEADER_ENCRYPTED_KEY_SIZE - (9+14+10+3);
-
- if (destSz - (int)XSTRLEN(dest) >= cipherInfoStrLen + (9+14+8+2+2+1)) {
- /* strncat's src length needs to include the NULL */
- XSTRNCAT(dest, kProcTypeHeader, 10);
- XSTRNCAT(dest, ": 4,ENCRYPTED\n", 15);
- XSTRNCAT(dest, kDecInfoHeader, 9);
- XSTRNCAT(dest, ": ", 3);
- XSTRNCAT(dest, cipherInfo, destSz - (int)XSTRLEN(dest) - 1);
- XSTRNCAT(dest, "\n\n", 4);
- }
- }
- return 0;
-}
-#endif /* WOLFSSL_DER_TO_PEM */
-#endif /* WOLFSSL_ENCRYPTED_KEYS */
-
-#ifdef WOLFSSL_DER_TO_PEM
-
-/* Used for compatibility API */
-int wc_DerToPem(const byte* der, word32 derSz,
- byte* output, word32 outSz, int type)
-{
- return wc_DerToPemEx(der, derSz, output, outSz, NULL, type);
-}
-
-/* convert der buffer to pem into output, can't do inplace, der and output
- need to be different */
-int wc_DerToPemEx(const byte* der, word32 derSz, byte* output, word32 outSz,
- byte *cipher_info, int type)
-{
- const char* headerStr = NULL;
- const char* footerStr = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- char* header = NULL;
- char* footer = NULL;
-#else
- char header[MAX_X509_HEADER_SZ + HEADER_ENCRYPTED_KEY_SIZE];
- char footer[MAX_X509_HEADER_SZ];
-#endif
- int headerLen = MAX_X509_HEADER_SZ + HEADER_ENCRYPTED_KEY_SIZE;
- int footerLen = MAX_X509_HEADER_SZ;
- int i;
- int err;
- int outLen; /* return length or error */
-
- (void)cipher_info;
-
- if (der == output) /* no in place conversion */
- return BAD_FUNC_ARG;
-
- err = wc_PemGetHeaderFooter(type, &headerStr, &footerStr);
- if (err != 0)
- return err;
-
-#ifdef WOLFSSL_SMALL_STACK
- header = (char*)XMALLOC(headerLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (header == NULL)
- return MEMORY_E;
-
- footer = (char*)XMALLOC(footerLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (footer == NULL) {
- XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#endif
-
- /* build header and footer based on type */
- XSTRNCPY(header, headerStr, headerLen - 1);
- header[headerLen - 2] = 0;
- XSTRNCPY(footer, footerStr, footerLen - 1);
- footer[footerLen - 2] = 0;
-
- /* add new line to end */
- XSTRNCAT(header, "\n", 2);
- XSTRNCAT(footer, "\n", 2);
-
-#ifdef WOLFSSL_ENCRYPTED_KEYS
- err = wc_EncryptedInfoAppend(header, headerLen, (char*)cipher_info);
- if (err != 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return err;
- }
-#endif
-
- headerLen = (int)XSTRLEN(header);
- footerLen = (int)XSTRLEN(footer);
-
- /* if null output and 0 size passed in then return size needed */
- if (!output && outSz == 0) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- outLen = 0;
- if ((err = Base64_Encode(der, derSz, NULL, (word32*)&outLen))
- != LENGTH_ONLY_E) {
- return err;
- }
- return headerLen + footerLen + outLen;
- }
-
- if (!der || !output) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return BAD_FUNC_ARG;
- }
-
- /* don't even try if outSz too short */
- if (outSz < headerLen + footerLen + derSz) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return BAD_FUNC_ARG;
- }
-
- /* header */
- XMEMCPY(output, header, headerLen);
- i = headerLen;
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- /* body */
- outLen = outSz - (headerLen + footerLen); /* input to Base64_Encode */
- if ( (err = Base64_Encode(der, derSz, output + i, (word32*)&outLen)) < 0) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return err;
- }
- i += outLen;
-
- /* footer */
- if ( (i + footerLen) > (int)outSz) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return BAD_FUNC_ARG;
- }
- XMEMCPY(output + i, footer, footerLen);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(footer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return outLen + headerLen + footerLen;
-}
-
-#endif /* WOLFSSL_DER_TO_PEM */
-
-#ifdef WOLFSSL_PEM_TO_DER
-
-/* Remove PEM header/footer, convert to ASN1, store any encrypted data
- info->consumed tracks of PEM bytes consumed in case multiple parts */
-int PemToDer(const unsigned char* buff, long longSz, int type,
- DerBuffer** pDer, void* heap, EncryptedInfo* info, int* keyFormat)
-{
- const char* header = NULL;
- const char* footer = NULL;
- char* headerEnd;
- char* footerEnd;
- char* consumedEnd;
- char* bufferEnd = (char*)(buff + longSz);
- long neededSz;
- int ret = 0;
- int sz = (int)longSz;
- int encrypted_key = 0;
- DerBuffer* der;
-#if defined(HAVE_PKCS8) || defined(WOLFSSL_ENCRYPTED_KEYS)
- word32 algId = 0;
- #if defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_WOLFSSL_SKIP_TRAILING_PAD)
- int padVal = 0;
- #endif
-#endif
-#ifdef OPENSSL_EXTRA
- char beginBuf[PEM_LINE_LEN + 1]; /* add 1 for null terminator */
- char endBuf[PEM_LINE_LEN + 1]; /* add 1 for null terminator */
-#endif
-
- WOLFSSL_ENTER("PemToDer");
-
- /* get PEM header and footer based on type */
- ret = wc_PemGetHeaderFooter(type, &header, &footer);
- if (ret != 0)
- return ret;
-
- /* map header if not found for type */
- for (;;) {
- headerEnd = XSTRNSTR((char*)buff, header, sz);
-
- if (headerEnd) {
- break;
- } else
- if (type == PRIVATEKEY_TYPE) {
- if (header == BEGIN_RSA_PRIV) {
- header = BEGIN_PRIV_KEY; footer = END_PRIV_KEY;
- } else
- if (header == BEGIN_PRIV_KEY) {
- header = BEGIN_ENC_PRIV_KEY; footer = END_ENC_PRIV_KEY;
- } else
- #ifdef HAVE_ECC
- if (header == BEGIN_ENC_PRIV_KEY) {
- header = BEGIN_EC_PRIV; footer = END_EC_PRIV;
- } else
- if (header == BEGIN_EC_PRIV) {
- header = BEGIN_DSA_PRIV; footer = END_DSA_PRIV;
- } else
- #endif
- #if defined(HAVE_ED25519) || defined(HAVE_ED448)
- #ifdef HAVE_ECC
- if (header == BEGIN_DSA_PRIV)
- #else
- if (header == BEGIN_ENC_PRIV_KEY)
- #endif
- {
- header = BEGIN_EDDSA_PRIV; footer = END_EDDSA_PRIV;
- } else
- #endif
- {
- break;
- }
- } else
-#ifdef HAVE_CRL
- if ((type == CRL_TYPE) && (header != BEGIN_CRL)) {
- header = BEGIN_CRL; footer = END_CRL;
- } else
-#endif
- {
- break;
- }
- }
-
- if (!headerEnd) {
-#ifdef OPENSSL_EXTRA
- char* beginEnd;
- int endLen;
- /* see if there is a -----BEGIN * PRIVATE KEY----- header */
- headerEnd = XSTRNSTR((char*)buff, PRIV_KEY_SUFFIX, sz);
- if (headerEnd) {
- beginEnd = headerEnd + XSTR_SIZEOF(PRIV_KEY_SUFFIX);
- /* back up to BEGIN_PRIV_KEY_PREFIX */
- headerEnd -= XSTR_SIZEOF(BEGIN_PRIV_KEY_PREFIX);
- while (headerEnd > (char*)buff &&
- XSTRNCMP(headerEnd, BEGIN_PRIV_KEY_PREFIX,
- XSTR_SIZEOF(BEGIN_PRIV_KEY_PREFIX)) != 0) {
- headerEnd--;
- }
- if (headerEnd <= (char*)buff ||
- XSTRNCMP(headerEnd, BEGIN_PRIV_KEY_PREFIX,
- XSTR_SIZEOF(BEGIN_PRIV_KEY_PREFIX)) != 0 ||
- beginEnd - headerEnd > PEM_LINE_LEN) {
- WOLFSSL_MSG("Couldn't find PEM header");
- return ASN_NO_PEM_HEADER;
- }
- /* headerEnd now points to beginning of header */
- XMEMCPY(beginBuf, headerEnd, beginEnd - headerEnd);
- beginBuf[beginEnd - headerEnd] = '\0';
- /* look for matching footer */
- footer = XSTRNSTR(beginEnd,
- beginBuf + XSTR_SIZEOF(BEGIN_PRIV_KEY_PREFIX),
- (unsigned int)((char*)buff + sz - beginEnd));
- if (!footer) {
- WOLFSSL_MSG("Couldn't find PEM footer");
- return ASN_NO_PEM_HEADER;
- }
- footer -= XSTR_SIZEOF(END_PRIV_KEY_PREFIX);
- endLen = (unsigned int)(beginEnd - headerEnd -
- (XSTR_SIZEOF(BEGIN_PRIV_KEY_PREFIX) -
- XSTR_SIZEOF(END_PRIV_KEY_PREFIX)));
- XMEMCPY(endBuf, footer, endLen);
- endBuf[endLen] = '\0';
-
- header = beginBuf;
- footer = endBuf;
- headerEnd = beginEnd;
- } else {
- WOLFSSL_MSG("Couldn't find PEM header");
- return ASN_NO_PEM_HEADER;
- }
-#else
- WOLFSSL_MSG("Couldn't find PEM header");
- return ASN_NO_PEM_HEADER;
-#endif
- } else {
- headerEnd += XSTRLEN(header);
- }
-
- /* eat end of line characters */
- headerEnd = SkipEndOfLineChars(headerEnd, bufferEnd);
-
- if (type == PRIVATEKEY_TYPE) {
- /* keyFormat is Key_Sum enum */
- if (keyFormat) {
- #ifdef HAVE_ECC
- if (header == BEGIN_EC_PRIV)
- *keyFormat = ECDSAk;
- #endif
- #if !defined(NO_DSA)
- if (header == BEGIN_DSA_PRIV)
- *keyFormat = DSAk;
- #endif
- }
- }
-
-#ifdef WOLFSSL_ENCRYPTED_KEYS
- if (info) {
- ret = wc_EncryptedInfoParse(info, &headerEnd, bufferEnd - headerEnd);
- if (ret < 0)
- return ret;
- if (info->set)
- encrypted_key = 1;
- }
-#endif /* WOLFSSL_ENCRYPTED_KEYS */
-
- /* find footer */
- footerEnd = XSTRNSTR(headerEnd, footer, (unsigned int)((char*)buff + sz - headerEnd));
- if (!footerEnd) {
- if (info)
- info->consumed = longSz; /* No more certs if no footer */
- return BUFFER_E;
- }
-
- consumedEnd = footerEnd + XSTRLEN(footer);
-
- if (consumedEnd < bufferEnd) { /* handle no end of line on last line */
- /* eat end of line characters */
- consumedEnd = SkipEndOfLineChars(consumedEnd, bufferEnd);
- /* skip possible null term */
- if (consumedEnd < bufferEnd && consumedEnd[0] == '\0')
- consumedEnd++;
- }
-
- if (info)
- info->consumed = (long)(consumedEnd - (char*)buff);
-
- /* set up der buffer */
- neededSz = (long)(footerEnd - headerEnd);
- if (neededSz > sz || neededSz <= 0)
- return BUFFER_E;
-
- ret = AllocDer(pDer, (word32)neededSz, type, heap);
- if (ret < 0) {
- return ret;
- }
- der = *pDer;
-
- if (Base64_Decode((byte*)headerEnd, (word32)neededSz,
- der->buffer, &der->length) < 0)
- return BUFFER_E;
-
- if ((header == BEGIN_PRIV_KEY
-#ifdef OPENSSL_EXTRA
- || header == beginBuf
-#endif
-#ifdef HAVE_ECC
- || header == BEGIN_EC_PRIV
-#endif
- ) && !encrypted_key)
- {
- #ifdef HAVE_PKCS8
- /* pkcs8 key, convert and adjust length */
- if ((ret = ToTraditional_ex(der->buffer, der->length, &algId)) > 0) {
- der->length = ret;
- if (keyFormat) {
- *keyFormat = algId;
- }
- }
- else {
- /* ignore failure here and assume key is not pkcs8 wrapped */
- }
- #endif
-
- return 0;
- }
-
-#ifdef WOLFSSL_ENCRYPTED_KEYS
- if (encrypted_key || header == BEGIN_ENC_PRIV_KEY) {
- int passwordSz = NAME_SZ;
- #ifdef WOLFSSL_SMALL_STACK
- char* password = NULL;
- #else
- char password[NAME_SZ];
- #endif
-
- if (!info || !info->passwd_cb) {
- WOLFSSL_MSG("No password callback set");
- return NO_PASSWORD;
- }
-
- #ifdef WOLFSSL_SMALL_STACK
- password = (char*)XMALLOC(passwordSz, heap, DYNAMIC_TYPE_STRING);
- if (password == NULL)
- return MEMORY_E;
- #endif
-
- /* get password */
- ret = info->passwd_cb(password, passwordSz, PEM_PASS_READ,
- info->passwd_userdata);
- if (ret >= 0) {
- passwordSz = ret;
-
- /* convert and adjust length */
- if (header == BEGIN_ENC_PRIV_KEY) {
- #ifndef NO_PWDBASED
- ret = ToTraditionalEnc(der->buffer, der->length,
- password, passwordSz, &algId);
-
- if (ret >= 0) {
- der->length = ret;
- if (keyFormat) {
- *keyFormat = algId;
- }
- ret = 0;
- }
- #else
- ret = NOT_COMPILED_IN;
- #endif
- }
- /* decrypt the key */
- else {
- if (passwordSz == 0) {
- /* The key is encrypted but does not have a password */
- WOLFSSL_MSG("No password for encrypted key");
- ret = NO_PASSWORD;
- }
- else {
- ret = wc_BufferKeyDecrypt(info, der->buffer, der->length,
- (byte*)password, passwordSz, WC_MD5);
-
-#ifndef NO_WOLFSSL_SKIP_TRAILING_PAD
- #ifndef NO_DES3
- if (info->cipherType == WC_CIPHER_DES3) {
- padVal = der->buffer[der->length-1];
- if (padVal <= DES_BLOCK_SIZE) {
- der->length -= padVal;
- }
- }
- #endif /* !NO_DES3 */
-#endif /* !NO_WOLFSSL_SKIP_TRAILING_PAD */
- }
- }
-#ifdef OPENSSL_EXTRA
- if (ret) {
- PEMerr(0, PEM_R_BAD_DECRYPT);
- }
-#endif
- ForceZero(password, passwordSz);
- }
-#ifdef OPENSSL_EXTRA
- else {
- PEMerr(0, PEM_R_BAD_PASSWORD_READ);
- }
-#endif
-
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(password, heap, DYNAMIC_TYPE_STRING);
- #endif
- }
-#endif /* WOLFSSL_ENCRYPTED_KEYS */
-
- return ret;
-}
-
-int wc_PemToDer(const unsigned char* buff, long longSz, int type,
- DerBuffer** pDer, void* heap, EncryptedInfo* info, int* eccKey)
-{
- return PemToDer(buff, longSz, type, pDer, heap, info, eccKey);
-}
-
-
-/* our KeyPemToDer password callback, password in userData */
-static WC_INLINE int OurPasswordCb(char* passwd, int sz, int rw, void* userdata)
-{
- (void)rw;
-
- if (userdata == NULL)
- return 0;
-
- XSTRNCPY(passwd, (char*)userdata, sz);
- return min((word32)sz, (word32)XSTRLEN((char*)userdata));
-}
-
-/* Return bytes written to buff or < 0 for error */
-int wc_KeyPemToDer(const unsigned char* pem, int pemSz,
- unsigned char* buff, int buffSz, const char* pass)
-{
- int eccKey = 0;
- int ret;
- DerBuffer* der = NULL;
-#ifdef WOLFSSL_SMALL_STACK
- EncryptedInfo* info = NULL;
-#else
- EncryptedInfo info[1];
-#endif
-
- WOLFSSL_ENTER("wc_KeyPemToDer");
-
- if (pem == NULL || buff == NULL || buffSz <= 0) {
- WOLFSSL_MSG("Bad pem der args");
- return BAD_FUNC_ARG;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
- DYNAMIC_TYPE_ENCRYPTEDINFO);
- if (info == NULL)
- return MEMORY_E;
-#endif
-
- XMEMSET(info, 0, sizeof(EncryptedInfo));
- info->passwd_cb = OurPasswordCb;
- info->passwd_userdata = (void*)pass;
-
- ret = PemToDer(pem, pemSz, PRIVATEKEY_TYPE, &der, NULL, info, &eccKey);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(info, NULL, DYNAMIC_TYPE_ENCRYPTEDINFO);
-#endif
-
- if (ret < 0 || der == NULL) {
- WOLFSSL_MSG("Bad Pem To Der");
- }
- else {
- if (der->length <= (word32)buffSz) {
- XMEMCPY(buff, der->buffer, der->length);
- ret = der->length;
- }
- else {
- WOLFSSL_MSG("Bad der length");
- ret = BAD_FUNC_ARG;
- }
- }
-
- FreeDer(&der);
- return ret;
-}
-
-
-/* Return bytes written to buff or < 0 for error */
-int wc_CertPemToDer(const unsigned char* pem, int pemSz,
- unsigned char* buff, int buffSz, int type)
-{
- int eccKey = 0;
- int ret;
- DerBuffer* der = NULL;
-
- WOLFSSL_ENTER("wc_CertPemToDer");
-
- if (pem == NULL || buff == NULL || buffSz <= 0) {
- WOLFSSL_MSG("Bad pem der args");
- return BAD_FUNC_ARG;
- }
-
- if (type != CERT_TYPE && type != CA_TYPE && type != CERTREQ_TYPE) {
- WOLFSSL_MSG("Bad cert type");
- return BAD_FUNC_ARG;
- }
-
-
- ret = PemToDer(pem, pemSz, type, &der, NULL, NULL, &eccKey);
- if (ret < 0 || der == NULL) {
- WOLFSSL_MSG("Bad Pem To Der");
- }
- else {
- if (der->length <= (word32)buffSz) {
- XMEMCPY(buff, der->buffer, der->length);
- ret = der->length;
- }
- else {
- WOLFSSL_MSG("Bad der length");
- ret = BAD_FUNC_ARG;
- }
- }
-
- FreeDer(&der);
- return ret;
-}
-
-#endif /* WOLFSSL_PEM_TO_DER */
-#endif /* WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM */
-
-
-#ifdef WOLFSSL_PEM_TO_DER
-#if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
-/* Return bytes written to buff or < 0 for error */
-int wc_PubKeyPemToDer(const unsigned char* pem, int pemSz,
- unsigned char* buff, int buffSz)
-{
- int ret;
- DerBuffer* der = NULL;
-
- WOLFSSL_ENTER("wc_PubKeyPemToDer");
-
- if (pem == NULL || buff == NULL || buffSz <= 0) {
- WOLFSSL_MSG("Bad pem der args");
- return BAD_FUNC_ARG;
- }
-
- ret = PemToDer(pem, pemSz, PUBLICKEY_TYPE, &der, NULL, NULL, NULL);
- if (ret < 0 || der == NULL) {
- WOLFSSL_MSG("Bad Pem To Der");
- }
- else {
- if (der->length <= (word32)buffSz) {
- XMEMCPY(buff, der->buffer, der->length);
- ret = der->length;
- }
- else {
- WOLFSSL_MSG("Bad der length");
- ret = BAD_FUNC_ARG;
- }
- }
-
- FreeDer(&der);
- return ret;
-}
-#endif /* WOLFSSL_CERT_EXT || WOLFSSL_PUB_PEM_TO_DER */
-#endif /* WOLFSSL_PEM_TO_DER */
-
-#ifndef NO_FILESYSTEM
-
-#ifdef WOLFSSL_CERT_GEN
-/* load pem cert from file into der buffer, return der size or error */
-int wc_PemCertToDer(const char* fileName, unsigned char* derBuf, int derSz)
-{
-#ifdef WOLFSSL_SMALL_STACK
- byte staticBuffer[1]; /* force XMALLOC */
-#else
- byte staticBuffer[FILE_BUFFER_SIZE];
-#endif
- byte* fileBuf = staticBuffer;
- int dynamic = 0;
- int ret = 0;
- long sz = 0;
- XFILE file;
- DerBuffer* converted = NULL;
-
- WOLFSSL_ENTER("wc_PemCertToDer");
-
- if (fileName == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- file = XFOPEN(fileName, "rb");
- if (file == XBADFILE) {
- ret = BUFFER_E;
- }
- }
-
- if (ret == 0) {
- if(XFSEEK(file, 0, XSEEK_END) != 0)
- ret = BUFFER_E;
- sz = XFTELL(file);
- XREWIND(file);
-
- if (sz <= 0) {
- ret = BUFFER_E;
- }
- else if (sz > (long)sizeof(staticBuffer)) {
- #ifdef WOLFSSL_STATIC_MEMORY
- WOLFSSL_MSG("File was larger then static buffer");
- return MEMORY_E;
- #endif
- fileBuf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
- if (fileBuf == NULL)
- ret = MEMORY_E;
- else
- dynamic = 1;
- }
-
- if (ret == 0) {
- if ( (ret = (int)XFREAD(fileBuf, 1, sz, file)) != sz) {
- ret = BUFFER_E;
- }
- #ifdef WOLFSSL_PEM_TO_DER
- else {
- ret = PemToDer(fileBuf, sz, CA_TYPE, &converted, 0, NULL,NULL);
- }
- #endif
-
- if (ret == 0) {
- if (converted->length < (word32)derSz) {
- XMEMCPY(derBuf, converted->buffer, converted->length);
- ret = converted->length;
- }
- else
- ret = BUFFER_E;
- }
-
- FreeDer(&converted);
- }
-
- XFCLOSE(file);
- if (dynamic)
- XFREE(fileBuf, NULL, DYNAMIC_TYPE_FILE);
- }
-
- return ret;
-}
-#endif /* WOLFSSL_CERT_GEN */
-
-#if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
-/* load pem public key from file into der buffer, return der size or error */
-int wc_PemPubKeyToDer(const char* fileName,
- unsigned char* derBuf, int derSz)
-{
-#ifdef WOLFSSL_SMALL_STACK
- byte staticBuffer[1]; /* force XMALLOC */
-#else
- byte staticBuffer[FILE_BUFFER_SIZE];
-#endif
- byte* fileBuf = staticBuffer;
- int dynamic = 0;
- int ret = 0;
- long sz = 0;
- XFILE file;
- DerBuffer* converted = NULL;
-
- WOLFSSL_ENTER("wc_PemPubKeyToDer");
-
- if (fileName == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- file = XFOPEN(fileName, "rb");
- if (file == XBADFILE) {
- ret = BUFFER_E;
- }
- }
-
- if (ret == 0) {
- if(XFSEEK(file, 0, XSEEK_END) != 0)
- ret = BUFFER_E;
- sz = XFTELL(file);
- XREWIND(file);
-
- if (sz <= 0) {
- ret = BUFFER_E;
- }
- else if (sz > (long)sizeof(staticBuffer)) {
- #ifdef WOLFSSL_STATIC_MEMORY
- WOLFSSL_MSG("File was larger then static buffer");
- return MEMORY_E;
- #endif
- fileBuf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
- if (fileBuf == NULL)
- ret = MEMORY_E;
- else
- dynamic = 1;
- }
- if (ret == 0) {
- if ( (ret = (int)XFREAD(fileBuf, 1, sz, file)) != sz) {
- ret = BUFFER_E;
- }
- #ifdef WOLFSSL_PEM_TO_DER
- else {
- ret = PemToDer(fileBuf, sz, PUBLICKEY_TYPE, &converted,
- 0, NULL, NULL);
- }
- #endif
-
- if (ret == 0) {
- if (converted->length < (word32)derSz) {
- XMEMCPY(derBuf, converted->buffer, converted->length);
- ret = converted->length;
- }
- else
- ret = BUFFER_E;
- }
-
- FreeDer(&converted);
- }
-
- XFCLOSE(file);
- if (dynamic)
- XFREE(fileBuf, NULL, DYNAMIC_TYPE_FILE);
- }
-
- return ret;
-}
-#endif /* WOLFSSL_CERT_EXT || WOLFSSL_PUB_PEM_TO_DER */
-
-#endif /* !NO_FILESYSTEM */
-
-
-#if !defined(NO_RSA) && (defined(WOLFSSL_CERT_GEN) || \
- ((defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)) && !defined(HAVE_USER_RSA)))
-/* USER RSA ifdef portions used instead of refactor in consideration for
- possible fips build */
-/* Write a public RSA key to output */
-static int SetRsaPublicKey(byte* output, RsaKey* key,
- int outLen, int with_header)
-{
-#ifdef WOLFSSL_SMALL_STACK
- byte* n = NULL;
- byte* e = NULL;
-#else
- byte n[MAX_RSA_INT_SZ];
- byte e[MAX_RSA_E_SZ];
-#endif
- byte seq[MAX_SEQ_SZ];
- byte bitString[1 + MAX_LENGTH_SZ + 1];
- int nSz;
- int eSz;
- int seqSz;
- int bitStringSz;
- int idx;
-
- if (output == NULL || key == NULL || outLen < MAX_SEQ_SZ)
- return BAD_FUNC_ARG;
-
- /* n */
-#ifdef WOLFSSL_SMALL_STACK
- n = (byte*)XMALLOC(MAX_RSA_INT_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (n == NULL)
- return MEMORY_E;
-#endif
-
-#ifdef HAVE_USER_RSA
- nSz = SetASNIntRSA(key->n, n);
-#else
- nSz = SetASNIntMP(&key->n, MAX_RSA_INT_SZ, n);
-#endif
- if (nSz < 0) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return nSz;
- }
-
- /* e */
-#ifdef WOLFSSL_SMALL_STACK
- e = (byte*)XMALLOC(MAX_RSA_E_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (e == NULL) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return MEMORY_E;
- }
-#endif
-
-#ifdef HAVE_USER_RSA
- eSz = SetASNIntRSA(key->e, e);
-#else
- eSz = SetASNIntMP(&key->e, MAX_RSA_INT_SZ, e);
-#endif
- if (eSz < 0) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(e, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return eSz;
- }
-
- seqSz = SetSequence(nSz + eSz, seq);
-
- /* check output size */
- if ( (seqSz + nSz + eSz) > outLen) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(e, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return BUFFER_E;
- }
-
- /* headers */
- if (with_header) {
- int algoSz;
-#ifdef WOLFSSL_SMALL_STACK
- byte* algo;
-
- algo = (byte*)XMALLOC(MAX_ALGO_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (algo == NULL) {
- XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(e, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#else
- byte algo[MAX_ALGO_SZ];
-#endif
- algoSz = SetAlgoID(RSAk, algo, oidKeyType, 0);
- bitStringSz = SetBitString(seqSz + nSz + eSz, 0, bitString);
-
- idx = SetSequence(nSz + eSz + seqSz + bitStringSz + algoSz, output);
-
- /* check output size */
- if ( (idx + algoSz + bitStringSz + seqSz + nSz + eSz) > outLen) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(e, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(algo, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
-
- return BUFFER_E;
- }
-
- /* algo */
- XMEMCPY(output + idx, algo, algoSz);
- idx += algoSz;
- /* bit string */
- XMEMCPY(output + idx, bitString, bitStringSz);
- idx += bitStringSz;
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(algo, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- }
- else
- idx = 0;
-
- /* seq */
- XMEMCPY(output + idx, seq, seqSz);
- idx += seqSz;
- /* n */
- XMEMCPY(output + idx, n, nSz);
- idx += nSz;
- /* e */
- XMEMCPY(output + idx, e, eSz);
- idx += eSz;
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(n, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(e, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return idx;
-}
-
-#endif /* !NO_RSA && (WOLFSSL_CERT_GEN || (WOLFSSL_KEY_GEN &&
- !HAVE_USER_RSA))) */
-
-#if !defined(NO_RSA) && (defined(WOLFSSL_CERT_GEN) || defined(OPENSSL_EXTRA))
-int wc_RsaPublicKeyDerSize(RsaKey* key, int with_header)
-{
- int idx = 0;
- int nSz, eSz, seqSz, bitStringSz, algoSz;
-
- if (key == NULL)
- return BAD_FUNC_ARG;
-
- /* n */
-#ifdef HAVE_USER_RSA
- nSz = SetASNIntRSA(key->n, NULL);
-#else
- nSz = SetASNIntMP(&key->n, MAX_RSA_INT_SZ, NULL);
-#endif
- if (nSz < 0) {
- return nSz;
- }
-
- /* e */
-#ifdef HAVE_USER_RSA
- eSz = SetASNIntRSA(key->e, NULL);
-#else
- eSz = SetASNIntMP(&key->e, MAX_RSA_INT_SZ, NULL);
-#endif
- if (eSz < 0) {
- return eSz;
- }
-
- seqSz = SetSequence(nSz + eSz, NULL);
-
- /* headers */
- if (with_header) {
- algoSz = SetAlgoID(RSAk, NULL, oidKeyType, 0);
- bitStringSz = SetBitString(seqSz + nSz + eSz, 0, NULL);
-
- idx += SetSequence(nSz + eSz + seqSz + bitStringSz + algoSz, NULL);
-
- /* algo */
- idx += algoSz;
- /* bit string */
- idx += bitStringSz;
- }
-
- /* seq */
- idx += seqSz;
- /* n */
- idx += nSz;
- /* e */
- idx += eSz;
-
- return idx;
-}
-
-#endif /* !NO_RSA && WOLFSSL_CERT_GEN */
-
-
-#if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
-
-static mp_int* GetRsaInt(RsaKey* key, int idx)
-{
- if (idx == 0)
- return &key->n;
- if (idx == 1)
- return &key->e;
- if (idx == 2)
- return &key->d;
- if (idx == 3)
- return &key->p;
- if (idx == 4)
- return &key->q;
- if (idx == 5)
- return &key->dP;
- if (idx == 6)
- return &key->dQ;
- if (idx == 7)
- return &key->u;
-
- return NULL;
-}
-
-
-/* Release Tmp RSA resources */
-static WC_INLINE void FreeTmpRsas(byte** tmps, void* heap)
-{
- int i;
-
- (void)heap;
-
- for (i = 0; i < RSA_INTS; i++)
- XFREE(tmps[i], heap, DYNAMIC_TYPE_RSA);
-}
-
-
-/* Convert RsaKey key to DER format, write to output (inLen), return bytes
- written */
-int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen)
-{
- word32 seqSz, verSz, rawLen, intTotalLen = 0;
- word32 sizes[RSA_INTS];
- int i, j, outLen, ret = 0, mpSz;
-
- byte seq[MAX_SEQ_SZ];
- byte ver[MAX_VERSION_SZ];
- byte* tmps[RSA_INTS];
-
- if (!key)
- return BAD_FUNC_ARG;
-
- if (key->type != RSA_PRIVATE)
- return BAD_FUNC_ARG;
-
- for (i = 0; i < RSA_INTS; i++)
- tmps[i] = NULL;
-
- /* write all big ints from key to DER tmps */
- for (i = 0; i < RSA_INTS; i++) {
- mp_int* keyInt = GetRsaInt(key, i);
-
- rawLen = mp_unsigned_bin_size(keyInt) + 1;
- tmps[i] = (byte*)XMALLOC(rawLen + MAX_SEQ_SZ, key->heap,
- DYNAMIC_TYPE_RSA);
- if (tmps[i] == NULL) {
- ret = MEMORY_E;
- break;
- }
-
- mpSz = SetASNIntMP(keyInt, MAX_RSA_INT_SZ, tmps[i]);
- if (mpSz < 0) {
- ret = mpSz;
- break;
- }
- intTotalLen += (sizes[i] = mpSz);
- }
-
- if (ret != 0) {
- FreeTmpRsas(tmps, key->heap);
- return ret;
- }
-
- /* make headers */
- verSz = SetMyVersion(0, ver, FALSE);
- seqSz = SetSequence(verSz + intTotalLen, seq);
-
- outLen = seqSz + verSz + intTotalLen;
- if (output) {
- if (outLen > (int)inLen) {
- FreeTmpRsas(tmps, key->heap);
- return BAD_FUNC_ARG;
- }
-
- /* write to output */
- XMEMCPY(output, seq, seqSz);
- j = seqSz;
- XMEMCPY(output + j, ver, verSz);
- j += verSz;
-
- for (i = 0; i < RSA_INTS; i++) {
- XMEMCPY(output + j, tmps[i], sizes[i]);
- j += sizes[i];
- }
- }
- FreeTmpRsas(tmps, key->heap);
-
- return outLen;
-}
-#endif
-
-#if (defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA)) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
-/* Convert Rsa Public key to DER format, write to output (inLen), return bytes
- written */
-int wc_RsaKeyToPublicDer(RsaKey* key, byte* output, word32 inLen)
-{
- return SetRsaPublicKey(output, key, inLen, 1);
-}
-
-#endif /* (WOLFSSL_KEY_GEN || OPENSSL_EXTRA) && !NO_RSA && !HAVE_USER_RSA */
-
-
-#ifdef WOLFSSL_CERT_GEN
-
-/* Initialize and Set Certificate defaults:
- version = 3 (0x2)
- serial = 0
- sigType = SHA_WITH_RSA
- issuer = blank
- daysValid = 500
- selfSigned = 1 (true) use subject as issuer
- subject = blank
-*/
-int wc_InitCert(Cert* cert)
-{
-#ifdef WOLFSSL_MULTI_ATTRIB
- int i = 0;
-#endif
- if (cert == NULL) {
- return BAD_FUNC_ARG;
- }
-
- XMEMSET(cert, 0, sizeof(Cert));
-
- cert->version = 2; /* version 3 is hex 2 */
-#ifndef NO_SHA
- cert->sigType = CTC_SHAwRSA;
-#elif !defined(NO_SHA256)
- cert->sigType = CTC_SHA256wRSA;
-#else
- cert->sigType = 0;
-#endif
- cert->daysValid = 500;
- cert->selfSigned = 1;
- cert->keyType = RSA_KEY;
-
- cert->issuer.countryEnc = CTC_PRINTABLE;
- cert->issuer.stateEnc = CTC_UTF8;
- cert->issuer.localityEnc = CTC_UTF8;
- cert->issuer.surEnc = CTC_UTF8;
- cert->issuer.orgEnc = CTC_UTF8;
- cert->issuer.unitEnc = CTC_UTF8;
- cert->issuer.commonNameEnc = CTC_UTF8;
-
- cert->subject.countryEnc = CTC_PRINTABLE;
- cert->subject.stateEnc = CTC_UTF8;
- cert->subject.localityEnc = CTC_UTF8;
- cert->subject.surEnc = CTC_UTF8;
- cert->subject.orgEnc = CTC_UTF8;
- cert->subject.unitEnc = CTC_UTF8;
- cert->subject.commonNameEnc = CTC_UTF8;
-
-#ifdef WOLFSSL_MULTI_ATTRIB
- for (i = 0; i < CTC_MAX_ATTRIB; i++) {
- cert->issuer.name[i].type = CTC_UTF8;
- cert->subject.name[i].type = CTC_UTF8;
- }
-#endif /* WOLFSSL_MULTI_ATTRIB */
-
-#ifdef WOLFSSL_HEAP_TEST
- cert->heap = (void*)WOLFSSL_HEAP_TEST;
-#endif
-
- return 0;
-}
-
-
-/* DER encoded x509 Certificate */
-typedef struct DerCert {
- byte size[MAX_LENGTH_SZ]; /* length encoded */
- byte version[MAX_VERSION_SZ]; /* version encoded */
- byte serial[(int)CTC_SERIAL_SIZE + (int)MAX_LENGTH_SZ]; /* serial number encoded */
- byte sigAlgo[MAX_ALGO_SZ]; /* signature algo encoded */
- byte issuer[ASN_NAME_MAX]; /* issuer encoded */
- byte subject[ASN_NAME_MAX]; /* subject encoded */
- byte validity[MAX_DATE_SIZE*2 + MAX_SEQ_SZ*2]; /* before and after dates */
- byte publicKey[MAX_PUBLIC_KEY_SZ]; /* rsa / ntru public key encoded */
- byte ca[MAX_CA_SZ]; /* basic constraint CA true size */
- byte extensions[MAX_EXTENSIONS_SZ]; /* all extensions */
-#ifdef WOLFSSL_CERT_EXT
- byte skid[MAX_KID_SZ]; /* Subject Key Identifier extension */
- byte akid[MAX_KID_SZ]; /* Authority Key Identifier extension */
- byte keyUsage[MAX_KEYUSAGE_SZ]; /* Key Usage extension */
- byte extKeyUsage[MAX_EXTKEYUSAGE_SZ]; /* Extended Key Usage extension */
- byte certPolicies[MAX_CERTPOL_NB*MAX_CERTPOL_SZ]; /* Certificate Policies */
-#endif
-#ifdef WOLFSSL_CERT_REQ
- byte attrib[MAX_ATTRIB_SZ]; /* Cert req attributes encoded */
-#endif
-#ifdef WOLFSSL_ALT_NAMES
- byte altNames[CTC_MAX_ALT_SIZE]; /* Alternative Names encoded */
-#endif
- int sizeSz; /* encoded size length */
- int versionSz; /* encoded version length */
- int serialSz; /* encoded serial length */
- int sigAlgoSz; /* encoded sig algo length */
- int issuerSz; /* encoded issuer length */
- int subjectSz; /* encoded subject length */
- int validitySz; /* encoded validity length */
- int publicKeySz; /* encoded public key length */
- int caSz; /* encoded CA extension length */
-#ifdef WOLFSSL_CERT_EXT
- int skidSz; /* encoded SKID extension length */
- int akidSz; /* encoded SKID extension length */
- int keyUsageSz; /* encoded KeyUsage extension length */
- int extKeyUsageSz; /* encoded ExtendedKeyUsage extension length */
- int certPoliciesSz; /* encoded CertPolicies extension length*/
-#endif
-#ifdef WOLFSSL_ALT_NAMES
- int altNamesSz; /* encoded AltNames extension length */
-#endif
- int extensionsSz; /* encoded extensions total length */
- int total; /* total encoded lengths */
-#ifdef WOLFSSL_CERT_REQ
- int attribSz;
-#endif
-} DerCert;
-
-
-#ifdef WOLFSSL_CERT_REQ
-
-/* Write a set header to output */
-static word32 SetPrintableString(word32 len, byte* output)
-{
- output[0] = ASN_PRINTABLE_STRING;
- return SetLength(len, output + 1) + 1;
-}
-
-static word32 SetUTF8String(word32 len, byte* output)
-{
- output[0] = ASN_UTF8STRING;
- return SetLength(len, output + 1) + 1;
-}
-
-#endif /* WOLFSSL_CERT_REQ */
-
-
-#ifndef WOLFSSL_CERT_GEN_CACHE
-/* wc_SetCert_Free is only public when WOLFSSL_CERT_GEN_CACHE is not defined */
-static
-#endif
-void wc_SetCert_Free(Cert* cert)
-{
- if (cert != NULL) {
- cert->der = NULL;
- if (cert->decodedCert) {
- FreeDecodedCert((DecodedCert*)cert->decodedCert);
-
- XFREE(cert->decodedCert, cert->heap, DYNAMIC_TYPE_DCERT);
- cert->decodedCert = NULL;
- }
- }
-}
-
-static int wc_SetCert_LoadDer(Cert* cert, const byte* der, word32 derSz)
-{
- int ret;
-
- if (cert == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- /* Allocate DecodedCert struct and Zero */
- cert->decodedCert = (void*)XMALLOC(sizeof(DecodedCert), cert->heap,
- DYNAMIC_TYPE_DCERT);
-
- if (cert->decodedCert == NULL) {
- ret = MEMORY_E;
- }
- else {
- XMEMSET(cert->decodedCert, 0, sizeof(DecodedCert));
-
- InitDecodedCert((DecodedCert*)cert->decodedCert, der, derSz,
- cert->heap);
- ret = ParseCertRelative((DecodedCert*)cert->decodedCert,
- CERT_TYPE, 0, NULL);
- if (ret >= 0) {
- cert->der = (byte*)der;
- }
- else {
- wc_SetCert_Free(cert);
- }
- }
- }
-
- return ret;
-}
-
-#endif /* WOLFSSL_CERT_GEN */
-
-
-#if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
-
-/* Write a public ECC key to output */
-static int SetEccPublicKey(byte* output, ecc_key* key, int with_header)
-{
- byte bitString[1 + MAX_LENGTH_SZ + 1];
- int algoSz;
- int curveSz;
- int bitStringSz;
- int idx;
- word32 pubSz = ECC_BUFSIZE;
-#ifdef WOLFSSL_SMALL_STACK
- byte* algo = NULL;
- byte* curve = NULL;
- byte* pub;
-#else
- byte algo[MAX_ALGO_SZ];
- byte curve[MAX_ALGO_SZ];
- byte pub[ECC_BUFSIZE];
-#endif
- int ret;
-
-#ifdef WOLFSSL_SMALL_STACK
- pub = (byte*)XMALLOC(ECC_BUFSIZE, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (pub == NULL)
- return MEMORY_E;
-#endif
-
-#ifdef HAVE_SELFTEST
- /* older version of ecc.c can not handle dp being NULL */
- if (key != NULL && key->dp == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- ret = wc_ecc_export_x963(key, pub, &pubSz);
- }
-#else
- ret = wc_ecc_export_x963(key, pub, &pubSz);
-#endif
- if (ret != 0) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return ret;
- }
-
- /* headers */
- if (with_header) {
-#ifdef WOLFSSL_SMALL_STACK
- curve = (byte*)XMALLOC(MAX_ALGO_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (curve == NULL) {
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#endif
- curveSz = SetCurve(key, curve);
- if (curveSz <= 0) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(curve, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return curveSz;
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- algo = (byte*)XMALLOC(MAX_ALGO_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (algo == NULL) {
- XFREE(curve, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#endif
- algoSz = SetAlgoID(ECDSAk, algo, oidKeyType, curveSz);
-
- bitStringSz = SetBitString(pubSz, 0, bitString);
-
- idx = SetSequence(pubSz + curveSz + bitStringSz + algoSz, output);
- /* algo */
- if (output)
- XMEMCPY(output + idx, algo, algoSz);
- idx += algoSz;
- /* curve */
- if (output)
- XMEMCPY(output + idx, curve, curveSz);
- idx += curveSz;
- /* bit string */
- if (output)
- XMEMCPY(output + idx, bitString, bitStringSz);
- idx += bitStringSz;
- }
- else
- idx = 0;
-
- /* pub */
- if (output)
- XMEMCPY(output + idx, pub, pubSz);
- idx += pubSz;
-
-#ifdef WOLFSSL_SMALL_STACK
- if (with_header) {
- XFREE(algo, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(curve, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return idx;
-}
-
-
-/* returns the size of buffer used, the public ECC key in DER format is stored
- in output buffer
- with_AlgCurve is a flag for when to include a header that has the Algorithm
- and Curve information */
-int wc_EccPublicKeyToDer(ecc_key* key, byte* output, word32 inLen,
- int with_AlgCurve)
-{
- word32 infoSz = 0;
- word32 keySz = 0;
- int ret;
-
- if (key == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (with_AlgCurve) {
- /* buffer space for algorithm/curve */
- infoSz += MAX_SEQ_SZ;
- infoSz += 2 * MAX_ALGO_SZ;
-
- /* buffer space for public key sequence */
- infoSz += MAX_SEQ_SZ;
- infoSz += TRAILING_ZERO;
- }
-
-#ifdef HAVE_SELFTEST
- /* older version of ecc.c can not handle dp being NULL */
- if (key != NULL && key->dp == NULL) {
- keySz = 1 + 2 * MAX_ECC_BYTES;
- ret = LENGTH_ONLY_E;
- }
- else {
- ret = wc_ecc_export_x963(key, NULL, &keySz);
- }
-#else
- ret = wc_ecc_export_x963(key, NULL, &keySz);
-#endif
- if (ret != LENGTH_ONLY_E) {
- WOLFSSL_MSG("Error in getting ECC public key size");
- return ret;
- }
-
- /* if output null then just return size */
- if (output == NULL) {
- return keySz + infoSz;
- }
-
- if (inLen < keySz + infoSz) {
- return BUFFER_E;
- }
-
- return SetEccPublicKey(output, key, with_AlgCurve);
-}
-
-int wc_EccPublicKeyDerSize(ecc_key* key, int with_AlgCurve)
-{
- return wc_EccPublicKeyToDer(key, NULL, 0, with_AlgCurve);
-}
-
-#endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT */
-
-#if defined(HAVE_ED25519) && (defined(WOLFSSL_CERT_GEN) || \
- defined(WOLFSSL_KEY_GEN))
-
-/* Write a public ECC key to output */
-static int SetEd25519PublicKey(byte* output, ed25519_key* key, int with_header)
-{
- byte bitString[1 + MAX_LENGTH_SZ + 1];
- int algoSz;
- int bitStringSz;
- int idx;
- word32 pubSz = ED25519_PUB_KEY_SIZE;
-#ifdef WOLFSSL_SMALL_STACK
- byte* algo = NULL;
- byte* pub;
-#else
- byte algo[MAX_ALGO_SZ];
- byte pub[ED25519_PUB_KEY_SIZE];
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- pub = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (pub == NULL)
- return MEMORY_E;
-#endif
-
- idx = wc_ed25519_export_public(key, pub, &pubSz);
- if (idx != 0) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return idx;
- }
-
- /* headers */
- if (with_header) {
-#ifdef WOLFSSL_SMALL_STACK
- algo = (byte*)XMALLOC(MAX_ALGO_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (algo == NULL) {
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#endif
- algoSz = SetAlgoID(ED25519k, algo, oidKeyType, 0);
-
- bitStringSz = SetBitString(pubSz, 0, bitString);
-
- idx = SetSequence(pubSz + bitStringSz + algoSz, output);
- /* algo */
- XMEMCPY(output + idx, algo, algoSz);
- idx += algoSz;
- /* bit string */
- XMEMCPY(output + idx, bitString, bitStringSz);
- idx += bitStringSz;
- }
- else
- idx = 0;
-
- /* pub */
- XMEMCPY(output + idx, pub, pubSz);
- idx += pubSz;
-
-#ifdef WOLFSSL_SMALL_STACK
- if (with_header) {
- XFREE(algo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return idx;
-}
-
-int wc_Ed25519PublicKeyToDer(ed25519_key* key, byte* output, word32 inLen,
- int withAlg)
-{
- word32 infoSz = 0;
- word32 keySz = 0;
- int ret;
-
- if (output == NULL || key == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (withAlg) {
- /* buffer space for algorithm */
- infoSz += MAX_SEQ_SZ;
- infoSz += MAX_ALGO_SZ;
-
- /* buffer space for public key sequence */
- infoSz += MAX_SEQ_SZ;
- infoSz += TRAILING_ZERO;
- }
-
- if ((ret = wc_ed25519_export_public(key, output, &keySz)) != BUFFER_E) {
- WOLFSSL_MSG("Error in getting ECC public key size");
- return ret;
- }
-
- if (inLen < keySz + infoSz) {
- return BUFFER_E;
- }
-
- return SetEd25519PublicKey(output, key, withAlg);
-}
-#endif /* HAVE_ED25519 && (WOLFSSL_CERT_GEN || WOLFSSL_KEY_GEN) */
-#if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
- defined(WOLFSSL_KEY_GEN))
-
-/* Write a public ECC key to output */
-static int SetEd448PublicKey(byte* output, ed448_key* key, int with_header)
-{
- byte bitString[1 + MAX_LENGTH_SZ + 1];
- int algoSz;
- int bitStringSz;
- int idx;
- word32 pubSz = ED448_PUB_KEY_SIZE;
-#ifdef WOLFSSL_SMALL_STACK
- byte* algo = NULL;
- byte* pub = NULL;
-#else
- byte algo[MAX_ALGO_SZ];
- byte pub[ED448_PUB_KEY_SIZE];
-#endif
-
-#ifdef WOLFSSL_SMALL_STACK
- pub = (byte*)XMALLOC(ECC_BUFSIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (pub == NULL)
- return MEMORY_E;
-#endif
-
- idx = wc_ed448_export_public(key, pub, &pubSz);
- if (idx != 0) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return idx;
- }
-
- /* headers */
- if (with_header) {
-#ifdef WOLFSSL_SMALL_STACK
- algo = (byte*)XMALLOC(MAX_ALGO_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (algo == NULL) {
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#endif
- algoSz = SetAlgoID(ED448k, algo, oidKeyType, 0);
-
- bitStringSz = SetBitString(pubSz, 0, bitString);
-
- idx = SetSequence(pubSz + bitStringSz + algoSz, output);
- /* algo */
- XMEMCPY(output + idx, algo, algoSz);
- idx += algoSz;
- /* bit string */
- XMEMCPY(output + idx, bitString, bitStringSz);
- idx += bitStringSz;
- }
- else
- idx = 0;
-
- /* pub */
- XMEMCPY(output + idx, pub, pubSz);
- idx += pubSz;
-
-#ifdef WOLFSSL_SMALL_STACK
- if (with_header) {
- XFREE(algo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return idx;
-}
-
-int wc_Ed448PublicKeyToDer(ed448_key* key, byte* output, word32 inLen,
- int withAlg)
-{
- word32 infoSz = 0;
- word32 keySz = 0;
- int ret;
-
- if (output == NULL || key == NULL) {
- return BAD_FUNC_ARG;
- }
-
- if (withAlg) {
- /* buffer space for algorithm */
- infoSz += MAX_SEQ_SZ;
- infoSz += MAX_ALGO_SZ;
-
- /* buffer space for public key sequence */
- infoSz += MAX_SEQ_SZ;
- infoSz += TRAILING_ZERO;
- }
-
- if ((ret = wc_ed448_export_public(key, output, &keySz)) != BUFFER_E) {
- WOLFSSL_MSG("Error in getting ECC public key size");
- return ret;
- }
-
- if (inLen < keySz + infoSz) {
- return BUFFER_E;
- }
-
- return SetEd448PublicKey(output, key, withAlg);
-}
-#endif /* HAVE_ED448 && (WOLFSSL_CERT_GEN || WOLFSSL_KEY_GEN) */
-
-
-#ifdef WOLFSSL_CERT_GEN
-
-static WC_INLINE byte itob(int number)
-{
- return (byte)number + 0x30;
-}
-
-
-/* write time to output, format */
-static void SetTime(struct tm* date, byte* output)
-{
- int i = 0;
-
- output[i++] = itob((date->tm_year % 10000) / 1000);
- output[i++] = itob((date->tm_year % 1000) / 100);
- output[i++] = itob((date->tm_year % 100) / 10);
- output[i++] = itob( date->tm_year % 10);
-
- output[i++] = itob(date->tm_mon / 10);
- output[i++] = itob(date->tm_mon % 10);
-
- output[i++] = itob(date->tm_mday / 10);
- output[i++] = itob(date->tm_mday % 10);
-
- output[i++] = itob(date->tm_hour / 10);
- output[i++] = itob(date->tm_hour % 10);
-
- output[i++] = itob(date->tm_min / 10);
- output[i++] = itob(date->tm_min % 10);
-
- output[i++] = itob(date->tm_sec / 10);
- output[i++] = itob(date->tm_sec % 10);
-
- output[i] = 'Z'; /* Zulu profile */
-}
-
-
-#ifdef WOLFSSL_ALT_NAMES
-
-/* Copy Dates from cert, return bytes written */
-static int CopyValidity(byte* output, Cert* cert)
-{
- int seqSz;
-
- WOLFSSL_ENTER("CopyValidity");
-
- /* headers and output */
- seqSz = SetSequence(cert->beforeDateSz + cert->afterDateSz, output);
- if (output) {
- XMEMCPY(output + seqSz, cert->beforeDate, cert->beforeDateSz);
- XMEMCPY(output + seqSz + cert->beforeDateSz, cert->afterDate,
- cert->afterDateSz);
- }
- return seqSz + cert->beforeDateSz + cert->afterDateSz;
-}
-
-#endif
-
-
-/* Set Date validity from now until now + daysValid
- * return size in bytes written to output, 0 on error */
-static int SetValidity(byte* output, int daysValid)
-{
- byte before[MAX_DATE_SIZE];
- byte after[MAX_DATE_SIZE];
-
- int beforeSz;
- int afterSz;
- int seqSz;
-
- time_t now;
- time_t then;
- struct tm* tmpTime;
- struct tm* expandedTime;
- struct tm localTime;
-
-#if defined(NEED_TMP_TIME)
- /* for use with gmtime_r */
- struct tm tmpTimeStorage;
- tmpTime = &tmpTimeStorage;
-#else
- tmpTime = NULL;
-#endif
- (void)tmpTime;
-
- now = XTIME(0);
-
- /* before now */
- before[0] = ASN_GENERALIZED_TIME;
- beforeSz = SetLength(ASN_GEN_TIME_SZ, before + 1) + 1; /* gen tag */
-
- /* subtract 1 day of seconds for more compliance */
- then = now - 86400;
- expandedTime = XGMTIME(&then, tmpTime);
- if (expandedTime == NULL) {
- WOLFSSL_MSG("XGMTIME failed");
- return 0; /* error */
- }
- localTime = *expandedTime;
-
- /* adjust */
- localTime.tm_year += 1900;
- localTime.tm_mon += 1;
-
- SetTime(&localTime, before + beforeSz);
- beforeSz += ASN_GEN_TIME_SZ;
-
- after[0] = ASN_GENERALIZED_TIME;
- afterSz = SetLength(ASN_GEN_TIME_SZ, after + 1) + 1; /* gen tag */
-
- /* add daysValid of seconds */
- then = now + (daysValid * (time_t)86400);
- expandedTime = XGMTIME(&then, tmpTime);
- if (expandedTime == NULL) {
- WOLFSSL_MSG("XGMTIME failed");
- return 0; /* error */
- }
- localTime = *expandedTime;
-
- /* adjust */
- localTime.tm_year += 1900;
- localTime.tm_mon += 1;
-
- SetTime(&localTime, after + afterSz);
- afterSz += ASN_GEN_TIME_SZ;
-
- /* headers and output */
- seqSz = SetSequence(beforeSz + afterSz, output);
- XMEMCPY(output + seqSz, before, beforeSz);
- XMEMCPY(output + seqSz + beforeSz, after, afterSz);
-
- return seqSz + beforeSz + afterSz;
-}
-
-
-/* ASN Encoded Name field */
-typedef struct EncodedName {
- int nameLen; /* actual string value length */
- int totalLen; /* total encoded length */
- int type; /* type of name */
- int used; /* are we actually using this one */
- byte encoded[CTC_NAME_SIZE * 2]; /* encoding */
-} EncodedName;
-
-
-/* Get Which Name from index */
-static const char* GetOneName(CertName* name, int idx)
-{
- switch (idx) {
- case 0:
- return name->country;
-
- case 1:
- return name->state;
-
- case 2:
- return name->locality;
-
- case 3:
- return name->sur;
-
- case 4:
- return name->org;
-
- case 5:
- return name->unit;
-
- case 6:
- return name->commonName;
-
- case 7:
- return name->serialDev;
-
-#ifdef WOLFSSL_CERT_EXT
- case 8:
- return name->busCat;
-
- case 9:
-#else
- case 8:
-#endif
- return name->email;
-
- default:
- return 0;
- }
-}
-
-
-/* Get Which Name Encoding from index */
-static char GetNameType(CertName* name, int idx)
-{
- switch (idx) {
- case 0:
- return name->countryEnc;
-
- case 1:
- return name->stateEnc;
-
- case 2:
- return name->localityEnc;
-
- case 3:
- return name->surEnc;
-
- case 4:
- return name->orgEnc;
-
- case 5:
- return name->unitEnc;
-
- case 6:
- return name->commonNameEnc;
-
- case 7:
- return name->serialDevEnc;
-
-#ifdef WOLFSSL_CERT_EXT
- case 8:
- return name->busCatEnc;
-
- case 9:
-#else
- case 8:
-#endif
- /* FALL THROUGH */
- /* The last index, email name, does not have encoding type.
- The empty case here is to keep track of it for future reference. */
- default:
- return 0;
- }
-}
-
-
-/* Get ASN Name from index */
-static byte GetNameId(int idx)
-{
- switch (idx) {
- case 0:
- return ASN_COUNTRY_NAME;
-
- case 1:
- return ASN_STATE_NAME;
-
- case 2:
- return ASN_LOCALITY_NAME;
-
- case 3:
- return ASN_SUR_NAME;
-
- case 4:
- return ASN_ORG_NAME;
-
- case 5:
- return ASN_ORGUNIT_NAME;
-
- case 6:
- return ASN_COMMON_NAME;
-
- case 7:
- return ASN_SERIAL_NUMBER;
-
-#ifdef WOLFSSL_CERT_EXT
- case 8:
- return ASN_BUS_CAT;
-
- case 9:
-#else
- case 8:
-#endif
- return ASN_EMAIL_NAME;
-
- default:
- return 0;
- }
-}
-
-/*
- Extensions ::= SEQUENCE OF Extension
-
- Extension ::= SEQUENCE {
- extnId OBJECT IDENTIFIER,
- critical BOOLEAN DEFAULT FALSE,
- extnValue OCTET STRING }
- */
-
-/* encode all extensions, return total bytes written */
-static int SetExtensions(byte* out, word32 outSz, int *IdxInOut,
- const byte* ext, int extSz)
-{
- if (out == NULL || IdxInOut == NULL || ext == NULL)
- return BAD_FUNC_ARG;
-
- if (outSz < (word32)(*IdxInOut+extSz))
- return BUFFER_E;
-
- XMEMCPY(&out[*IdxInOut], ext, extSz); /* extensions */
- *IdxInOut += extSz;
-
- return *IdxInOut;
-}
-
-/* encode extensions header, return total bytes written */
-static int SetExtensionsHeader(byte* out, word32 outSz, int extSz)
-{
- byte sequence[MAX_SEQ_SZ];
- byte len[MAX_LENGTH_SZ];
- int seqSz, lenSz, idx = 0;
-
- if (out == NULL)
- return BAD_FUNC_ARG;
-
- if (outSz < 3)
- return BUFFER_E;
-
- seqSz = SetSequence(extSz, sequence);
-
- /* encode extensions length provided */
- lenSz = SetLength(extSz+seqSz, len);
-
- if (outSz < (word32)(lenSz+seqSz+1))
- return BUFFER_E;
-
- out[idx++] = ASN_EXTENSIONS; /* extensions id */
- XMEMCPY(&out[idx], len, lenSz); /* length */
- idx += lenSz;
-
- XMEMCPY(&out[idx], sequence, seqSz); /* sequence */
- idx += seqSz;
-
- return idx;
-}
-
-
-/* encode CA basic constraint true, return total bytes written */
-static int SetCa(byte* out, word32 outSz)
-{
- const byte ca[] = { 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04,
- 0x05, 0x30, 0x03, 0x01, 0x01, 0xff };
-
- if (out == NULL)
- return BAD_FUNC_ARG;
-
- if (outSz < sizeof(ca))
- return BUFFER_E;
-
- XMEMCPY(out, ca, sizeof(ca));
-
- return (int)sizeof(ca);
-}
-
-
-#ifdef WOLFSSL_CERT_EXT
-/* encode OID and associated value, return total bytes written */
-static int SetOidValue(byte* out, word32 outSz, const byte *oid, word32 oidSz,
- byte *in, word32 inSz)
-{
- int idx = 0;
-
- if (out == NULL || oid == NULL || in == NULL)
- return BAD_FUNC_ARG;
-
- if (outSz < 3)
- return BUFFER_E;
-
- /* sequence, + 1 => byte to put value size */
- idx = SetSequence(inSz + oidSz + 1, out);
-
- if ((idx + inSz + oidSz + 1) > outSz)
- return BUFFER_E;
-
- XMEMCPY(out+idx, oid, oidSz);
- idx += oidSz;
- out[idx++] = (byte)inSz;
- XMEMCPY(out+idx, in, inSz);
-
- return (idx+inSz);
-}
-
-/* encode Subject Key Identifier, return total bytes written
- * RFC5280 : non-critical */
-static int SetSKID(byte* output, word32 outSz, const byte *input, word32 length)
-{
- byte skid_len[1 + MAX_LENGTH_SZ];
- byte skid_enc_len[MAX_LENGTH_SZ];
- int idx = 0, skid_lenSz, skid_enc_lenSz;
- const byte skid_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04 };
-
- if (output == NULL || input == NULL)
- return BAD_FUNC_ARG;
-
- /* Octet String header */
- skid_lenSz = SetOctetString(length, skid_len);
-
- /* length of encoded value */
- skid_enc_lenSz = SetLength(length + skid_lenSz, skid_enc_len);
-
- if (outSz < 3)
- return BUFFER_E;
-
- idx = SetSequence(length + sizeof(skid_oid) + skid_lenSz + skid_enc_lenSz,
- output);
-
- if ((length + sizeof(skid_oid) + skid_lenSz + skid_enc_lenSz) > outSz)
- return BUFFER_E;
-
- /* put oid */
- XMEMCPY(output+idx, skid_oid, sizeof(skid_oid));
- idx += sizeof(skid_oid);
-
- /* put encoded len */
- XMEMCPY(output+idx, skid_enc_len, skid_enc_lenSz);
- idx += skid_enc_lenSz;
-
- /* put octet header */
- XMEMCPY(output+idx, skid_len, skid_lenSz);
- idx += skid_lenSz;
-
- /* put value */
- XMEMCPY(output+idx, input, length);
- idx += length;
-
- return idx;
-}
-
-/* encode Authority Key Identifier, return total bytes written
- * RFC5280 : non-critical */
-static int SetAKID(byte* output, word32 outSz,
- byte *input, word32 length, void* heap)
-{
- byte *enc_val;
- int ret, enc_valSz;
- const byte akid_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04 };
- const byte akid_cs[] = { 0x80 };
-
- (void)heap;
-
- if (output == NULL || input == NULL)
- return BAD_FUNC_ARG;
-
- enc_valSz = length + 3 + sizeof(akid_cs);
- enc_val = (byte *)XMALLOC(enc_valSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (enc_val == NULL)
- return MEMORY_E;
-
- /* sequence for ContentSpec & value */
- ret = SetOidValue(enc_val, enc_valSz, akid_cs, sizeof(akid_cs),
- input, length);
- if (ret > 0) {
- enc_valSz = ret;
-
- ret = SetOidValue(output, outSz, akid_oid, sizeof(akid_oid),
- enc_val, enc_valSz);
- }
-
- XFREE(enc_val, heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
-}
-
-/* encode Key Usage, return total bytes written
- * RFC5280 : critical */
-static int SetKeyUsage(byte* output, word32 outSz, word16 input)
-{
- byte ku[5];
- int idx;
- const byte keyusage_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x0f,
- 0x01, 0x01, 0xff, 0x04};
- if (output == NULL)
- return BAD_FUNC_ARG;
-
- idx = SetBitString16Bit(input, ku);
- return SetOidValue(output, outSz, keyusage_oid, sizeof(keyusage_oid),
- ku, idx);
-}
-
-static int SetOjectIdValue(byte* output, word32 outSz, int* idx,
- const byte* oid, word32 oidSz)
-{
- /* verify room */
- if (*idx + 2 + oidSz >= outSz)
- return ASN_PARSE_E;
-
- *idx += SetObjectId(oidSz, &output[*idx]);
- XMEMCPY(&output[*idx], oid, oidSz);
- *idx += oidSz;
-
- return 0;
-}
-
-/* encode Extended Key Usage (RFC 5280 4.2.1.12), return total bytes written */
-static int SetExtKeyUsage(Cert* cert, byte* output, word32 outSz, byte input)
-{
- int idx = 0, oidListSz = 0, totalSz, ret = 0;
- const byte extkeyusage_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x25 };
-
- if (output == NULL)
- return BAD_FUNC_ARG;
-
- /* Skip to OID List */
- totalSz = 2 + sizeof(extkeyusage_oid) + 4;
- idx = totalSz;
-
- /* Build OID List */
- /* If any set, then just use it */
- if (input & EXTKEYUSE_ANY) {
- ret |= SetOjectIdValue(output, outSz, &idx,
- extExtKeyUsageAnyOid, sizeof(extExtKeyUsageAnyOid));
- }
- else {
- if (input & EXTKEYUSE_SERVER_AUTH)
- ret |= SetOjectIdValue(output, outSz, &idx,
- extExtKeyUsageServerAuthOid, sizeof(extExtKeyUsageServerAuthOid));
- if (input & EXTKEYUSE_CLIENT_AUTH)
- ret |= SetOjectIdValue(output, outSz, &idx,
- extExtKeyUsageClientAuthOid, sizeof(extExtKeyUsageClientAuthOid));
- if (input & EXTKEYUSE_CODESIGN)
- ret |= SetOjectIdValue(output, outSz, &idx,
- extExtKeyUsageCodeSigningOid, sizeof(extExtKeyUsageCodeSigningOid));
- if (input & EXTKEYUSE_EMAILPROT)
- ret |= SetOjectIdValue(output, outSz, &idx,
- extExtKeyUsageEmailProtectOid, sizeof(extExtKeyUsageEmailProtectOid));
- if (input & EXTKEYUSE_TIMESTAMP)
- ret |= SetOjectIdValue(output, outSz, &idx,
- extExtKeyUsageTimestampOid, sizeof(extExtKeyUsageTimestampOid));
- if (input & EXTKEYUSE_OCSP_SIGN)
- ret |= SetOjectIdValue(output, outSz, &idx,
- extExtKeyUsageOcspSignOid, sizeof(extExtKeyUsageOcspSignOid));
- #ifdef WOLFSSL_EKU_OID
- /* iterate through OID values */
- if (input & EXTKEYUSE_USER) {
- int i, sz;
- for (i = 0; i < CTC_MAX_EKU_NB; i++) {
- sz = cert->extKeyUsageOIDSz[i];
- if (sz > 0) {
- ret |= SetOjectIdValue(output, outSz, &idx,
- cert->extKeyUsageOID[i], sz);
- }
- }
- }
- #endif /* WOLFSSL_EKU_OID */
- }
- if (ret != 0)
- return ASN_PARSE_E;
-
- /* Calculate Sizes */
- oidListSz = idx - totalSz;
- totalSz = idx - 2; /* exclude first seq/len (2) */
-
- /* 1. Seq + Total Len (2) */
- idx = SetSequence(totalSz, output);
-
- /* 2. Object ID (2) */
- XMEMCPY(&output[idx], extkeyusage_oid, sizeof(extkeyusage_oid));
- idx += sizeof(extkeyusage_oid);
-
- /* 3. Octet String (2) */
- idx += SetOctetString(totalSz - idx, &output[idx]);
-
- /* 4. Seq + OidListLen (2) */
- idx += SetSequence(oidListSz, &output[idx]);
-
- /* 5. Oid List (already set in-place above) */
- idx += oidListSz;
-
- (void)cert;
- return idx;
-}
-
-/* encode Certificate Policies, return total bytes written
- * each input value must be ITU-T X.690 formatted : a.b.c...
- * input must be an array of values with a NULL terminated for the latest
- * RFC5280 : non-critical */
-static int SetCertificatePolicies(byte *output,
- word32 outputSz,
- char input[MAX_CERTPOL_NB][MAX_CERTPOL_SZ],
- word16 nb_certpol,
- void* heap)
-{
- byte oid[MAX_OID_SZ],
- der_oid[MAX_CERTPOL_NB][MAX_OID_SZ],
- out[MAX_CERTPOL_SZ];
- word32 oidSz;
- word32 outSz, i = 0, der_oidSz[MAX_CERTPOL_NB];
- int ret;
-
- const byte certpol_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x20, 0x04 };
- const byte oid_oid[] = { 0x06 };
-
- if (output == NULL || input == NULL || nb_certpol > MAX_CERTPOL_NB)
- return BAD_FUNC_ARG;
-
- for (i = 0; i < nb_certpol; i++) {
- oidSz = sizeof(oid);
- XMEMSET(oid, 0, oidSz);
-
- ret = EncodePolicyOID(oid, &oidSz, input[i], heap);
- if (ret != 0)
- return ret;
-
- /* compute sequence value for the oid */
- ret = SetOidValue(der_oid[i], MAX_OID_SZ, oid_oid,
- sizeof(oid_oid), oid, oidSz);
- if (ret <= 0)
- return ret;
- else
- der_oidSz[i] = (word32)ret;
- }
-
- /* concatenate oid, keep two byte for sequence/size of the created value */
- for (i = 0, outSz = 2; i < nb_certpol; i++) {
- XMEMCPY(out+outSz, der_oid[i], der_oidSz[i]);
- outSz += der_oidSz[i];
- }
-
- /* add sequence */
- ret = SetSequence(outSz-2, out);
- if (ret <= 0)
- return ret;
-
- /* add Policy OID to compute final value */
- return SetOidValue(output, outputSz, certpol_oid, sizeof(certpol_oid),
- out, outSz);
-}
-#endif /* WOLFSSL_CERT_EXT */
-
-
-#ifdef WOLFSSL_ALT_NAMES
-
-/* encode Alternative Names, return total bytes written */
-static int SetAltNames(byte *output, word32 outSz,
- const byte *input, word32 length)
-{
- byte san_len[1 + MAX_LENGTH_SZ];
- int idx = 0, san_lenSz;
- const byte san_oid[] = { 0x06, 0x03, 0x55, 0x1d, 0x11 };
-
- if (output == NULL || input == NULL)
- return BAD_FUNC_ARG;
-
- if (outSz < length)
- return BUFFER_E;
-
- /* Octet String header */
- san_lenSz = SetOctetString(length, san_len);
-
- if (outSz < MAX_SEQ_SZ)
- return BUFFER_E;
-
- idx = SetSequence(length + sizeof(san_oid) + san_lenSz, output);
-
- if ((length + sizeof(san_oid) + san_lenSz) > outSz)
- return BUFFER_E;
-
- /* put oid */
- XMEMCPY(output+idx, san_oid, sizeof(san_oid));
- idx += sizeof(san_oid);
-
- /* put octet header */
- XMEMCPY(output+idx, san_len, san_lenSz);
- idx += san_lenSz;
-
- /* put value */
- XMEMCPY(output+idx, input, length);
- idx += length;
-
- return idx;
-}
-
-
-#ifdef WOLFSSL_CERT_GEN
-
-int FlattenAltNames(byte* output, word32 outputSz, const DNS_entry* names)
-{
- word32 idx;
- const DNS_entry* curName;
- word32 namesSz = 0;
-
- if (output == NULL)
- return BAD_FUNC_ARG;
-
- if (names == NULL)
- return 0;
-
- curName = names;
- do {
- namesSz += curName->len + 2 +
- ((curName->len < ASN_LONG_LENGTH) ? 0
- : BytePrecision(curName->len));
- curName = curName->next;
- } while (curName != NULL);
-
- if (outputSz < MAX_SEQ_SZ + namesSz)
- return BUFFER_E;
-
- idx = SetSequence(namesSz, output);
-
- curName = names;
- do {
- output[idx++] = ASN_CONTEXT_SPECIFIC | curName->type;
- idx += SetLength(curName->len, output + idx);
- XMEMCPY(output + idx, curName->name, curName->len);
- idx += curName->len;
- curName = curName->next;
- } while (curName != NULL);
-
- return idx;
-}
-
-#endif /* WOLFSSL_CERT_GEN */
-
-#endif /* WOLFSSL_ALT_NAMES */
-
-/* Encodes one attribute of the name (issuer/subject)
- *
- * name structure to hold result of encoding
- * nameStr value to be encoded
- * nameType type of encoding i.e CTC_UTF8
- * type id of attribute i.e ASN_COMMON_NAME
- *
- * returns length on success
- */
-static int wc_EncodeName(EncodedName* name, const char* nameStr, char nameType,
- byte type)
-{
- word32 idx = 0;
-
- if (nameStr) {
- /* bottom up */
- byte firstLen[1 + MAX_LENGTH_SZ];
- byte secondLen[MAX_LENGTH_SZ];
- byte sequence[MAX_SEQ_SZ];
- byte set[MAX_SET_SZ];
-
- int strLen = (int)XSTRLEN(nameStr);
- int thisLen = strLen;
- int firstSz, secondSz, seqSz, setSz;
-
- if (strLen == 0) { /* no user data for this item */
- name->used = 0;
- return 0;
- }
-
- /* Restrict country code size */
- if (ASN_COUNTRY_NAME == type && strLen != CTC_COUNTRY_SIZE) {
- return ASN_COUNTRY_SIZE_E;
- }
-
- secondSz = SetLength(strLen, secondLen);
- thisLen += secondSz;
- switch (type) {
- case ASN_EMAIL_NAME: /* email */
- thisLen += EMAIL_JOINT_LEN;
- firstSz = EMAIL_JOINT_LEN;
- break;
-
- case ASN_DOMAIN_COMPONENT:
- thisLen += PILOT_JOINT_LEN;
- firstSz = PILOT_JOINT_LEN;
- break;
-
- default:
- thisLen++; /* str type */
- thisLen += JOINT_LEN;
- firstSz = JOINT_LEN + 1;
- }
- thisLen++; /* id type */
- firstSz = SetObjectId(firstSz, firstLen);
- thisLen += firstSz;
-
- seqSz = SetSequence(thisLen, sequence);
- thisLen += seqSz;
- setSz = SetSet(thisLen, set);
- thisLen += setSz;
-
- if (thisLen > (int)sizeof(name->encoded)) {
- return BUFFER_E;
- }
-
- /* store it */
- idx = 0;
- /* set */
- XMEMCPY(name->encoded, set, setSz);
- idx += setSz;
- /* seq */
- XMEMCPY(name->encoded + idx, sequence, seqSz);
- idx += seqSz;
- /* asn object id */
- XMEMCPY(name->encoded + idx, firstLen, firstSz);
- idx += firstSz;
- switch (type) {
- case ASN_EMAIL_NAME:
- {
- const byte EMAIL_OID[] = { 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
- 0x01, 0x09, 0x01, 0x16 };
- /* email joint id */
- XMEMCPY(name->encoded + idx, EMAIL_OID, sizeof(EMAIL_OID));
- idx += (int)sizeof(EMAIL_OID);
- }
- break;
-
- case ASN_DOMAIN_COMPONENT:
- {
- const byte PILOT_OID[] = { 0x09, 0x92, 0x26, 0x89,
- 0x93, 0xF2, 0x2C, 0x64, 0x01
- };
-
- XMEMCPY(name->encoded + idx, PILOT_OID,
- sizeof(PILOT_OID));
- idx += (int)sizeof(PILOT_OID);
- /* id type */
- name->encoded[idx++] = type;
- /* str type */
- name->encoded[idx++] = nameType;
- }
- break;
-
- default:
- name->encoded[idx++] = 0x55;
- name->encoded[idx++] = 0x04;
- /* id type */
- name->encoded[idx++] = type;
- /* str type */
- name->encoded[idx++] = nameType;
- }
- /* second length */
- XMEMCPY(name->encoded + idx, secondLen, secondSz);
- idx += secondSz;
- /* str value */
- XMEMCPY(name->encoded + idx, nameStr, strLen);
- idx += strLen;
-
- name->type = type;
- name->totalLen = idx;
- name->used = 1;
- }
- else
- name->used = 0;
-
- return idx;
-}
-
-/* encode CertName into output, return total bytes written */
-int SetName(byte* output, word32 outputSz, CertName* name)
-{
- int totalBytes = 0, i, idx;
-#ifdef WOLFSSL_SMALL_STACK
- EncodedName* names = NULL;
-#else
- EncodedName names[NAME_ENTRIES];
-#endif
-#ifdef WOLFSSL_MULTI_ATTRIB
- EncodedName addNames[CTC_MAX_ATTRIB];
- int j, type;
-#endif
-
- if (output == NULL || name == NULL)
- return BAD_FUNC_ARG;
-
- if (outputSz < 3)
- return BUFFER_E;
-
-#ifdef WOLFSSL_SMALL_STACK
- names = (EncodedName*)XMALLOC(sizeof(EncodedName) * NAME_ENTRIES, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (names == NULL)
- return MEMORY_E;
-#endif
-
- for (i = 0; i < NAME_ENTRIES; i++) {
- int ret;
- const char* nameStr = GetOneName(name, i);
-
- ret = wc_EncodeName(&names[i], nameStr, GetNameType(name, i),
- GetNameId(i));
- if (ret < 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return BUFFER_E;
- }
- totalBytes += ret;
- }
-#ifdef WOLFSSL_MULTI_ATTRIB
- for (i = 0; i < CTC_MAX_ATTRIB; i++) {
- if (name->name[i].sz > 0) {
- int ret;
- ret = wc_EncodeName(&addNames[i], name->name[i].value,
- name->name[i].type, name->name[i].id);
- if (ret < 0) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return BUFFER_E;
- }
- totalBytes += ret;
- }
- else {
- addNames[i].used = 0;
- }
- }
-#endif /* WOLFSSL_MULTI_ATTRIB */
-
- /* header */
- idx = SetSequence(totalBytes, output);
- totalBytes += idx;
- if (totalBytes > ASN_NAME_MAX) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return BUFFER_E;
- }
-
- for (i = 0; i < NAME_ENTRIES; i++) {
- #ifdef WOLFSSL_MULTI_ATTRIB
- type = GetNameId(i);
-
- /* list all DC values before OUs */
- if (type == ASN_ORGUNIT_NAME) {
- type = ASN_DOMAIN_COMPONENT;
- for (j = 0; j < CTC_MAX_ATTRIB; j++) {
- if (name->name[j].sz > 0 && type == name->name[j].id) {
- if (outputSz < (word32)(idx+addNames[j].totalLen)) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return BUFFER_E;
- }
-
- XMEMCPY(output + idx, addNames[j].encoded,
- addNames[j].totalLen);
- idx += addNames[j].totalLen;
- }
- }
- type = ASN_ORGUNIT_NAME;
- }
-
- /* write all similar types to the buffer */
- for (j = 0; j < CTC_MAX_ATTRIB; j++) {
- if (name->name[j].sz > 0 && type == name->name[j].id) {
- if (outputSz < (word32)(idx+addNames[j].totalLen)) {
- #ifdef WOLFSSL_SMALL_STACK
- XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return BUFFER_E;
- }
-
- XMEMCPY(output + idx, addNames[j].encoded,
- addNames[j].totalLen);
- idx += addNames[j].totalLen;
- }
- }
- #endif /* WOLFSSL_MULTI_ATTRIB */
-
- if (names[i].used) {
- if (outputSz < (word32)(idx+names[i].totalLen)) {
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- return BUFFER_E;
- }
-
- XMEMCPY(output + idx, names[i].encoded, names[i].totalLen);
- idx += names[i].totalLen;
- }
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(names, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return totalBytes;
-}
-
-/* encode info from cert into DER encoded format */
-static int EncodeCert(Cert* cert, DerCert* der, RsaKey* rsaKey, ecc_key* eccKey,
- WC_RNG* rng, const byte* ntruKey, word16 ntruSz,
- ed25519_key* ed25519Key, ed448_key* ed448Key)
-{
- int ret;
-
- if (cert == NULL || der == NULL || rng == NULL)
- return BAD_FUNC_ARG;
-
- /* make sure at least one key type is provided */
- if (rsaKey == NULL && eccKey == NULL && ed25519Key == NULL &&
- ed448Key == NULL && ntruKey == NULL) {
- return PUBLIC_KEY_E;
- }
-
- /* init */
- XMEMSET(der, 0, sizeof(DerCert));
-
- /* version */
- der->versionSz = SetMyVersion(cert->version, der->version, TRUE);
-
- /* serial number (must be positive) */
- if (cert->serialSz == 0) {
- /* generate random serial */
- cert->serialSz = CTC_GEN_SERIAL_SZ;
- ret = wc_RNG_GenerateBlock(rng, cert->serial, cert->serialSz);
- if (ret != 0)
- return ret;
- /* Clear the top bit to avoid a negative value */
- cert->serial[0] &= 0x7f;
- }
- der->serialSz = SetSerialNumber(cert->serial, cert->serialSz, der->serial,
- sizeof(der->serial), CTC_SERIAL_SIZE);
- if (der->serialSz < 0)
- return der->serialSz;
-
- /* signature algo */
- der->sigAlgoSz = SetAlgoID(cert->sigType, der->sigAlgo, oidSigType, 0);
- if (der->sigAlgoSz <= 0)
- return ALGO_ID_E;
-
- /* public key */
-#ifndef NO_RSA
- if (cert->keyType == RSA_KEY) {
- if (rsaKey == NULL)
- return PUBLIC_KEY_E;
- der->publicKeySz = SetRsaPublicKey(der->publicKey, rsaKey,
- sizeof(der->publicKey), 1);
- }
-#endif
-
-#ifdef HAVE_ECC
- if (cert->keyType == ECC_KEY) {
- if (eccKey == NULL)
- return PUBLIC_KEY_E;
- der->publicKeySz = SetEccPublicKey(der->publicKey, eccKey, 1);
- }
-#endif
-
-#ifdef HAVE_ED25519
- if (cert->keyType == ED25519_KEY) {
- if (ed25519Key == NULL)
- return PUBLIC_KEY_E;
- der->publicKeySz = SetEd25519PublicKey(der->publicKey, ed25519Key, 1);
- }
-#endif
-
-#ifdef HAVE_ED448
- if (cert->keyType == ED448_KEY) {
- if (ed448Key == NULL)
- return PUBLIC_KEY_E;
- der->publicKeySz = SetEd448PublicKey(der->publicKey, ed448Key, 1);
- }
-#endif
-
-#ifdef HAVE_NTRU
- if (cert->keyType == NTRU_KEY) {
- word32 rc;
- word16 encodedSz;
-
- if (ntruKey == NULL)
- return PUBLIC_KEY_E;
-
- rc = ntru_crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo(ntruSz,
- ntruKey, &encodedSz, NULL);
- if (rc != NTRU_OK)
- return PUBLIC_KEY_E;
- if (encodedSz > MAX_PUBLIC_KEY_SZ)
- return PUBLIC_KEY_E;
-
- rc = ntru_crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo(ntruSz,
- ntruKey, &encodedSz, der->publicKey);
- if (rc != NTRU_OK)
- return PUBLIC_KEY_E;
-
- der->publicKeySz = encodedSz;
- }
-#else
- (void)ntruSz;
-#endif /* HAVE_NTRU */
-
- if (der->publicKeySz <= 0)
- return PUBLIC_KEY_E;
-
- der->validitySz = 0;
-#ifdef WOLFSSL_ALT_NAMES
- /* date validity copy ? */
- if (cert->beforeDateSz && cert->afterDateSz) {
- der->validitySz = CopyValidity(der->validity, cert);
- if (der->validitySz <= 0)
- return DATE_E;
- }
-#endif
-
- /* date validity */
- if (der->validitySz == 0) {
- der->validitySz = SetValidity(der->validity, cert->daysValid);
- if (der->validitySz <= 0)
- return DATE_E;
- }
-
- /* subject name */
-#ifdef WOLFSSL_CERT_EXT
- if (XSTRLEN((const char*)cert->sbjRaw) > 0) {
- /* Use the raw subject */
- int idx;
-
- der->subjectSz = min(sizeof(der->subject),
- (word32)XSTRLEN((const char*)cert->sbjRaw));
- /* header */
- idx = SetSequence(der->subjectSz, der->subject);
- if (der->subjectSz + idx > (int)sizeof(der->subject)) {
- return SUBJECT_E;
- }
-
- XMEMCPY((char*)der->subject + idx, (const char*)cert->sbjRaw,
- der->subjectSz);
- der->subjectSz += idx;
- }
- else
-#endif
- {
- /* Use the name structure */
- der->subjectSz = SetName(der->subject, sizeof(der->subject),
- &cert->subject);
- }
- if (der->subjectSz <= 0)
- return SUBJECT_E;
-
- /* issuer name */
-#ifdef WOLFSSL_CERT_EXT
- if (XSTRLEN((const char*)cert->issRaw) > 0) {
- /* Use the raw issuer */
- int idx;
-
- der->issuerSz = min(sizeof(der->issuer),
- (word32)XSTRLEN((const char*)cert->issRaw));
- /* header */
- idx = SetSequence(der->issuerSz, der->issuer);
- if (der->issuerSz + idx > (int)sizeof(der->issuer)) {
- return ISSUER_E;
- }
-
- XMEMCPY((char*)der->issuer + idx, (const char*)cert->issRaw,
- der->issuerSz);
- der->issuerSz += idx;
- }
- else
-#endif
- {
- /* Use the name structure */
- der->issuerSz = SetName(der->issuer, sizeof(der->issuer),
- cert->selfSigned ? &cert->subject : &cert->issuer);
- }
- if (der->issuerSz <= 0)
- return ISSUER_E;
-
- /* set the extensions */
- der->extensionsSz = 0;
-
- /* CA */
- if (cert->isCA) {
- der->caSz = SetCa(der->ca, sizeof(der->ca));
- if (der->caSz <= 0)
- return CA_TRUE_E;
-
- der->extensionsSz += der->caSz;
- }
- else
- der->caSz = 0;
-
-#ifdef WOLFSSL_ALT_NAMES
- /* Alternative Name */
- if (cert->altNamesSz) {
- der->altNamesSz = SetAltNames(der->altNames, sizeof(der->altNames),
- cert->altNames, cert->altNamesSz);
- if (der->altNamesSz <= 0)
- return ALT_NAME_E;
-
- der->extensionsSz += der->altNamesSz;
- }
- else
- der->altNamesSz = 0;
-#endif
-
-#ifdef WOLFSSL_CERT_EXT
- /* SKID */
- if (cert->skidSz) {
- /* check the provided SKID size */
- if (cert->skidSz > (int)min(CTC_MAX_SKID_SIZE, sizeof(der->skid)))
- return SKID_E;
-
- /* Note: different skid buffers sizes for der (MAX_KID_SZ) and
- cert (CTC_MAX_SKID_SIZE). */
- der->skidSz = SetSKID(der->skid, sizeof(der->skid),
- cert->skid, cert->skidSz);
- if (der->skidSz <= 0)
- return SKID_E;
-
- der->extensionsSz += der->skidSz;
- }
- else
- der->skidSz = 0;
-
- /* AKID */
- if (cert->akidSz) {
- /* check the provided AKID size */
- if (cert->akidSz > (int)min(CTC_MAX_AKID_SIZE, sizeof(der->akid)))
- return AKID_E;
-
- der->akidSz = SetAKID(der->akid, sizeof(der->akid),
- cert->akid, cert->akidSz, cert->heap);
- if (der->akidSz <= 0)
- return AKID_E;
-
- der->extensionsSz += der->akidSz;
- }
- else
- der->akidSz = 0;
-
- /* Key Usage */
- if (cert->keyUsage != 0){
- der->keyUsageSz = SetKeyUsage(der->keyUsage, sizeof(der->keyUsage),
- cert->keyUsage);
- if (der->keyUsageSz <= 0)
- return KEYUSAGE_E;
-
- der->extensionsSz += der->keyUsageSz;
- }
- else
- der->keyUsageSz = 0;
-
- /* Extended Key Usage */
- if (cert->extKeyUsage != 0){
- der->extKeyUsageSz = SetExtKeyUsage(cert, der->extKeyUsage,
- sizeof(der->extKeyUsage), cert->extKeyUsage);
- if (der->extKeyUsageSz <= 0)
- return EXTKEYUSAGE_E;
-
- der->extensionsSz += der->extKeyUsageSz;
- }
- else
- der->extKeyUsageSz = 0;
-
- /* Certificate Policies */
- if (cert->certPoliciesNb != 0) {
- der->certPoliciesSz = SetCertificatePolicies(der->certPolicies,
- sizeof(der->certPolicies),
- cert->certPolicies,
- cert->certPoliciesNb,
- cert->heap);
- if (der->certPoliciesSz <= 0)
- return CERTPOLICIES_E;
-
- der->extensionsSz += der->certPoliciesSz;
- }
- else
- der->certPoliciesSz = 0;
-#endif /* WOLFSSL_CERT_EXT */
-
- /* put extensions */
- if (der->extensionsSz > 0) {
-
- /* put the start of extensions sequence (ID, Size) */
- der->extensionsSz = SetExtensionsHeader(der->extensions,
- sizeof(der->extensions),
- der->extensionsSz);
- if (der->extensionsSz <= 0)
- return EXTENSIONS_E;
-
- /* put CA */
- if (der->caSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->ca, der->caSz);
- if (ret == 0)
- return EXTENSIONS_E;
- }
-
-#ifdef WOLFSSL_ALT_NAMES
- /* put Alternative Names */
- if (der->altNamesSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->altNames, der->altNamesSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-#endif
-
-#ifdef WOLFSSL_CERT_EXT
- /* put SKID */
- if (der->skidSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->skid, der->skidSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-
- /* put AKID */
- if (der->akidSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->akid, der->akidSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-
- /* put KeyUsage */
- if (der->keyUsageSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->keyUsage, der->keyUsageSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-
- /* put ExtendedKeyUsage */
- if (der->extKeyUsageSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->extKeyUsage, der->extKeyUsageSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-
- /* put Certificate Policies */
- if (der->certPoliciesSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->certPolicies, der->certPoliciesSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-#endif /* WOLFSSL_CERT_EXT */
- }
-
- der->total = der->versionSz + der->serialSz + der->sigAlgoSz +
- der->publicKeySz + der->validitySz + der->subjectSz + der->issuerSz +
- der->extensionsSz;
-
- return 0;
-}
-
-
-/* write DER encoded cert to buffer, size already checked */
-static int WriteCertBody(DerCert* der, byte* buf)
-{
- int idx;
-
- /* signed part header */
- idx = SetSequence(der->total, buf);
- /* version */
- XMEMCPY(buf + idx, der->version, der->versionSz);
- idx += der->versionSz;
- /* serial */
- XMEMCPY(buf + idx, der->serial, der->serialSz);
- idx += der->serialSz;
- /* sig algo */
- XMEMCPY(buf + idx, der->sigAlgo, der->sigAlgoSz);
- idx += der->sigAlgoSz;
- /* issuer */
- XMEMCPY(buf + idx, der->issuer, der->issuerSz);
- idx += der->issuerSz;
- /* validity */
- XMEMCPY(buf + idx, der->validity, der->validitySz);
- idx += der->validitySz;
- /* subject */
- XMEMCPY(buf + idx, der->subject, der->subjectSz);
- idx += der->subjectSz;
- /* public key */
- XMEMCPY(buf + idx, der->publicKey, der->publicKeySz);
- idx += der->publicKeySz;
- if (der->extensionsSz) {
- /* extensions */
- XMEMCPY(buf + idx, der->extensions, min(der->extensionsSz,
- (int)sizeof(der->extensions)));
- idx += der->extensionsSz;
- }
-
- return idx;
-}
-
-
-/* Make RSA signature from buffer (sz), write to sig (sigSz) */
-static int MakeSignature(CertSignCtx* certSignCtx, const byte* buf, int sz,
- byte* sig, int sigSz, RsaKey* rsaKey, ecc_key* eccKey,
- ed25519_key* ed25519Key, ed448_key* ed448Key, WC_RNG* rng, int sigAlgoType,
- void* heap)
-{
- int digestSz = 0, typeH = 0, ret = 0;
-
- (void)digestSz;
- (void)typeH;
- (void)buf;
- (void)sz;
- (void)sig;
- (void)sigSz;
- (void)rsaKey;
- (void)eccKey;
- (void)ed25519Key;
- (void)ed448Key;
- (void)rng;
- (void)heap;
-
- switch (certSignCtx->state) {
- case CERTSIGN_STATE_BEGIN:
- case CERTSIGN_STATE_DIGEST:
-
- certSignCtx->state = CERTSIGN_STATE_DIGEST;
- certSignCtx->digest = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (certSignCtx->digest == NULL) {
- ret = MEMORY_E; goto exit_ms;
- }
-
- ret = HashForSignature(buf, sz, sigAlgoType, certSignCtx->digest,
- &typeH, &digestSz, 0);
- /* set next state, since WC_PENDING_E rentry for these are not "call again" */
- certSignCtx->state = CERTSIGN_STATE_ENCODE;
- if (ret != 0) {
- goto exit_ms;
- }
- FALL_THROUGH;
-
- case CERTSIGN_STATE_ENCODE:
- #ifndef NO_RSA
- if (rsaKey) {
- certSignCtx->encSig = (byte*)XMALLOC(MAX_DER_DIGEST_SZ, heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (certSignCtx->encSig == NULL) {
- ret = MEMORY_E; goto exit_ms;
- }
-
- /* signature */
- certSignCtx->encSigSz = wc_EncodeSignature(certSignCtx->encSig,
- certSignCtx->digest, digestSz, typeH);
- }
- #endif /* !NO_RSA */
- FALL_THROUGH;
-
- case CERTSIGN_STATE_DO:
- certSignCtx->state = CERTSIGN_STATE_DO;
- ret = ALGO_ID_E; /* default to error */
-
- #ifndef NO_RSA
- if (rsaKey) {
- /* signature */
- ret = wc_RsaSSL_Sign(certSignCtx->encSig, certSignCtx->encSigSz,
- sig, sigSz, rsaKey, rng);
- }
- #endif /* !NO_RSA */
-
- #ifdef HAVE_ECC
- if (!rsaKey && eccKey) {
- word32 outSz = sigSz;
-
- ret = wc_ecc_sign_hash(certSignCtx->digest, digestSz,
- sig, &outSz, rng, eccKey);
- if (ret == 0)
- ret = outSz;
- }
- #endif /* HAVE_ECC */
-
- #ifdef HAVE_ED25519
- if (!rsaKey && !eccKey && ed25519Key) {
- word32 outSz = sigSz;
-
- ret = wc_ed25519_sign_msg(buf, sz, sig, &outSz, ed25519Key);
- if (ret == 0)
- ret = outSz;
- }
- #endif /* HAVE_ECC */
-
- #ifdef HAVE_ED448
- if (!rsaKey && !eccKey && !ed25519Key && ed448Key) {
- word32 outSz = sigSz;
-
- ret = wc_ed448_sign_msg(buf, sz, sig, &outSz, ed448Key, NULL, 0);
- if (ret == 0)
- ret = outSz;
- }
- #endif /* HAVE_ECC */
- break;
- }
-
-exit_ms:
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E) {
- return ret;
- }
-#endif
-
-#ifndef NO_RSA
- if (rsaKey) {
- XFREE(certSignCtx->encSig, heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-#endif /* !NO_RSA */
-
- XFREE(certSignCtx->digest, heap, DYNAMIC_TYPE_TMP_BUFFER);
- certSignCtx->digest = NULL;
-
- /* reset state */
- certSignCtx->state = CERTSIGN_STATE_BEGIN;
-
- return ret;
-}
-
-
-/* add signature to end of buffer, size of buffer assumed checked, return
- new length */
-static int AddSignature(byte* buf, int bodySz, const byte* sig, int sigSz,
- int sigAlgoType)
-{
- byte seq[MAX_SEQ_SZ];
- int idx = bodySz, seqSz;
-
- /* algo */
- idx += SetAlgoID(sigAlgoType, buf ? buf + idx : NULL, oidSigType, 0);
- /* bit string */
- idx += SetBitString(sigSz, 0, buf ? buf + idx : NULL);
- /* signature */
- if (buf)
- XMEMCPY(buf + idx, sig, sigSz);
- idx += sigSz;
-
- /* make room for overall header */
- seqSz = SetSequence(idx, seq);
- if (buf) {
- XMEMMOVE(buf + seqSz, buf, idx);
- XMEMCPY(buf, seq, seqSz);
- }
-
- return idx + seqSz;
-}
-
-
-/* Make an x509 Certificate v3 any key type from cert input, write to buffer */
-static int MakeAnyCert(Cert* cert, byte* derBuffer, word32 derSz,
- RsaKey* rsaKey, ecc_key* eccKey, WC_RNG* rng,
- const byte* ntruKey, word16 ntruSz,
- ed25519_key* ed25519Key, ed448_key* ed448Key)
-{
- int ret;
-#ifdef WOLFSSL_SMALL_STACK
- DerCert* der;
-#else
- DerCert der[1];
-#endif
-
- if (derBuffer == NULL) {
- return BAD_FUNC_ARG;
- }
-
- cert->keyType = eccKey ? ECC_KEY : (rsaKey ? RSA_KEY :
- (ed25519Key ? ED25519_KEY : (ed448Key ? ED448_KEY : NTRU_KEY)));
-
-#ifdef WOLFSSL_SMALL_STACK
- der = (DerCert*)XMALLOC(sizeof(DerCert), cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (der == NULL)
- return MEMORY_E;
-#endif
-
- ret = EncodeCert(cert, der, rsaKey, eccKey, rng, ntruKey, ntruSz,
- ed25519Key, ed448Key);
- if (ret == 0) {
- if (der->total + MAX_SEQ_SZ * 2 > (int)derSz)
- ret = BUFFER_E;
- else
- ret = cert->bodySz = WriteCertBody(der, derBuffer);
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(der, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return ret;
-}
-
-
-/* Make an x509 Certificate v3 RSA or ECC from cert input, write to buffer */
-int wc_MakeCert_ex(Cert* cert, byte* derBuffer, word32 derSz, int keyType,
- void* key, WC_RNG* rng)
-{
- RsaKey* rsaKey = NULL;
- ecc_key* eccKey = NULL;
- ed25519_key* ed25519Key = NULL;
- ed448_key* ed448Key = NULL;
-
- if (keyType == RSA_TYPE)
- rsaKey = (RsaKey*)key;
- else if (keyType == ECC_TYPE)
- eccKey = (ecc_key*)key;
- else if (keyType == ED25519_TYPE)
- ed25519Key = (ed25519_key*)key;
- else if (keyType == ED448_TYPE)
- ed448Key = (ed448_key*)key;
-
- return MakeAnyCert(cert, derBuffer, derSz, rsaKey, eccKey, rng, NULL, 0,
- ed25519Key, ed448Key);
-}
-/* Make an x509 Certificate v3 RSA or ECC from cert input, write to buffer */
-int wc_MakeCert(Cert* cert, byte* derBuffer, word32 derSz, RsaKey* rsaKey,
- ecc_key* eccKey, WC_RNG* rng)
-{
- return MakeAnyCert(cert, derBuffer, derSz, rsaKey, eccKey, rng, NULL, 0,
- NULL, NULL);
-}
-
-
-#ifdef HAVE_NTRU
-
-int wc_MakeNtruCert(Cert* cert, byte* derBuffer, word32 derSz,
- const byte* ntruKey, word16 keySz, WC_RNG* rng)
-{
- return MakeAnyCert(cert, derBuffer, derSz, NULL, NULL, rng, ntruKey, keySz, NULL);
-}
-
-#endif /* HAVE_NTRU */
-
-
-#ifdef WOLFSSL_CERT_REQ
-
-static int SetReqAttrib(byte* output, char* pw, int pwPrintableString,
- int extSz)
-{
- const byte cpOid[] =
- { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
- 0x09, 0x07 };
- const byte erOid[] =
- { ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
- 0x09, 0x0e };
-
- int sz = 0; /* overall size */
- int cpSz = 0; /* Challenge Password section size */
- int cpSeqSz = 0;
- int cpSetSz = 0;
- int cpStrSz = 0;
- int pwSz = 0;
- int erSz = 0; /* Extension Request section size */
- int erSeqSz = 0;
- int erSetSz = 0;
- byte cpSeq[MAX_SEQ_SZ];
- byte cpSet[MAX_SET_SZ];
- byte cpStr[MAX_PRSTR_SZ];
- byte erSeq[MAX_SEQ_SZ];
- byte erSet[MAX_SET_SZ];
-
- output[0] = 0xa0;
- sz++;
-
- if (pw && pw[0]) {
- pwSz = (int)XSTRLEN(pw);
- if (pwPrintableString) {
- cpStrSz = SetPrintableString(pwSz, cpStr);
- } else {
- cpStrSz = SetUTF8String(pwSz, cpStr);
- }
- cpSetSz = SetSet(cpStrSz + pwSz, cpSet);
- cpSeqSz = SetSequence(sizeof(cpOid) + cpSetSz + cpStrSz + pwSz, cpSeq);
- cpSz = cpSeqSz + sizeof(cpOid) + cpSetSz + cpStrSz + pwSz;
- }
-
- if (extSz) {
- erSetSz = SetSet(extSz, erSet);
- erSeqSz = SetSequence(erSetSz + sizeof(erOid) + extSz, erSeq);
- erSz = extSz + erSetSz + erSeqSz + sizeof(erOid);
- }
-
- /* Put the pieces together. */
- sz += SetLength(cpSz + erSz, &output[sz]);
-
- if (cpSz) {
- XMEMCPY(&output[sz], cpSeq, cpSeqSz);
- sz += cpSeqSz;
- XMEMCPY(&output[sz], cpOid, sizeof(cpOid));
- sz += sizeof(cpOid);
- XMEMCPY(&output[sz], cpSet, cpSetSz);
- sz += cpSetSz;
- XMEMCPY(&output[sz], cpStr, cpStrSz);
- sz += cpStrSz;
- XMEMCPY(&output[sz], pw, pwSz);
- sz += pwSz;
- }
-
- if (erSz) {
- XMEMCPY(&output[sz], erSeq, erSeqSz);
- sz += erSeqSz;
- XMEMCPY(&output[sz], erOid, sizeof(erOid));
- sz += sizeof(erOid);
- XMEMCPY(&output[sz], erSet, erSetSz);
- sz += erSetSz;
- /* The actual extension data will be tacked onto the output later. */
- }
-
- return sz;
-}
-
-
-/* encode info from cert into DER encoded format */
-static int EncodeCertReq(Cert* cert, DerCert* der, RsaKey* rsaKey,
- ecc_key* eccKey, ed25519_key* ed25519Key,
- ed448_key* ed448Key)
-{
- (void)eccKey;
- (void)ed25519Key;
- (void)ed448Key;
-
- if (cert == NULL || der == NULL)
- return BAD_FUNC_ARG;
-
- if (rsaKey == NULL && eccKey == NULL && ed25519Key == NULL &&
- ed448Key == NULL) {
- return PUBLIC_KEY_E;
- }
-
- /* init */
- XMEMSET(der, 0, sizeof(DerCert));
-
- /* version */
- der->versionSz = SetMyVersion(cert->version, der->version, FALSE);
-
- /* subject name */
- der->subjectSz = SetName(der->subject, sizeof(der->subject), &cert->subject);
- if (der->subjectSz <= 0)
- return SUBJECT_E;
-
- /* public key */
-#ifndef NO_RSA
- if (cert->keyType == RSA_KEY) {
- if (rsaKey == NULL)
- return PUBLIC_KEY_E;
- der->publicKeySz = SetRsaPublicKey(der->publicKey, rsaKey,
- sizeof(der->publicKey), 1);
- }
-#endif
-
-#ifdef HAVE_ECC
- if (cert->keyType == ECC_KEY) {
- der->publicKeySz = SetEccPublicKey(der->publicKey, eccKey, 1);
- }
-#endif
-
-#ifdef HAVE_ED25519
- if (cert->keyType == ED25519_KEY) {
- if (ed25519Key == NULL)
- return PUBLIC_KEY_E;
- der->publicKeySz = SetEd25519PublicKey(der->publicKey, ed25519Key, 1);
- }
-#endif
-
-#ifdef HAVE_ED448
- if (cert->keyType == ED448_KEY) {
- if (ed448Key == NULL)
- return PUBLIC_KEY_E;
- der->publicKeySz = SetEd448PublicKey(der->publicKey, ed448Key, 1);
- }
-#endif
- if (der->publicKeySz <= 0)
- return PUBLIC_KEY_E;
-
- /* set the extensions */
- der->extensionsSz = 0;
-
- /* CA */
- if (cert->isCA) {
- der->caSz = SetCa(der->ca, sizeof(der->ca));
- if (der->caSz <= 0)
- return CA_TRUE_E;
-
- der->extensionsSz += der->caSz;
- }
- else
- der->caSz = 0;
-
-#ifdef WOLFSSL_CERT_EXT
- /* SKID */
- if (cert->skidSz) {
- /* check the provided SKID size */
- if (cert->skidSz > (int)min(CTC_MAX_SKID_SIZE, sizeof(der->skid)))
- return SKID_E;
-
- der->skidSz = SetSKID(der->skid, sizeof(der->skid),
- cert->skid, cert->skidSz);
- if (der->skidSz <= 0)
- return SKID_E;
-
- der->extensionsSz += der->skidSz;
- }
- else
- der->skidSz = 0;
-
- /* Key Usage */
- if (cert->keyUsage != 0){
- der->keyUsageSz = SetKeyUsage(der->keyUsage, sizeof(der->keyUsage),
- cert->keyUsage);
- if (der->keyUsageSz <= 0)
- return KEYUSAGE_E;
-
- der->extensionsSz += der->keyUsageSz;
- }
- else
- der->keyUsageSz = 0;
-
- /* Extended Key Usage */
- if (cert->extKeyUsage != 0){
- der->extKeyUsageSz = SetExtKeyUsage(cert, der->extKeyUsage,
- sizeof(der->extKeyUsage), cert->extKeyUsage);
- if (der->extKeyUsageSz <= 0)
- return EXTKEYUSAGE_E;
-
- der->extensionsSz += der->extKeyUsageSz;
- }
- else
- der->extKeyUsageSz = 0;
-
-#endif /* WOLFSSL_CERT_EXT */
-
- /* put extensions */
- if (der->extensionsSz > 0) {
- int ret;
-
- /* put the start of sequence (ID, Size) */
- der->extensionsSz = SetSequence(der->extensionsSz, der->extensions);
- if (der->extensionsSz <= 0)
- return EXTENSIONS_E;
-
- /* put CA */
- if (der->caSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->ca, der->caSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-
-#ifdef WOLFSSL_CERT_EXT
- /* put SKID */
- if (der->skidSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->skid, der->skidSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-
- /* put AKID */
- if (der->akidSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->akid, der->akidSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-
- /* put KeyUsage */
- if (der->keyUsageSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->keyUsage, der->keyUsageSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-
- /* put ExtendedKeyUsage */
- if (der->extKeyUsageSz) {
- ret = SetExtensions(der->extensions, sizeof(der->extensions),
- &der->extensionsSz,
- der->extKeyUsage, der->extKeyUsageSz);
- if (ret <= 0)
- return EXTENSIONS_E;
- }
-
-#endif /* WOLFSSL_CERT_EXT */
- }
-
- der->attribSz = SetReqAttrib(der->attrib, cert->challengePw,
- cert->challengePwPrintableString,
- der->extensionsSz);
- if (der->attribSz <= 0)
- return REQ_ATTRIBUTE_E;
-
- der->total = der->versionSz + der->subjectSz + der->publicKeySz +
- der->extensionsSz + der->attribSz;
-
- return 0;
-}
-
-
-/* write DER encoded cert req to buffer, size already checked */
-static int WriteCertReqBody(DerCert* der, byte* buf)
-{
- int idx;
-
- /* signed part header */
- idx = SetSequence(der->total, buf);
- /* version */
- if (buf)
- XMEMCPY(buf + idx, der->version, der->versionSz);
- idx += der->versionSz;
- /* subject */
- if (buf)
- XMEMCPY(buf + idx, der->subject, der->subjectSz);
- idx += der->subjectSz;
- /* public key */
- if (buf)
- XMEMCPY(buf + idx, der->publicKey, der->publicKeySz);
- idx += der->publicKeySz;
- /* attributes */
- if (buf)
- XMEMCPY(buf + idx, der->attrib, der->attribSz);
- idx += der->attribSz;
- /* extensions */
- if (der->extensionsSz) {
- if (buf)
- XMEMCPY(buf + idx, der->extensions, min(der->extensionsSz,
- (int)sizeof(der->extensions)));
- idx += der->extensionsSz;
- }
-
- return idx;
-}
-
-
-static int MakeCertReq(Cert* cert, byte* derBuffer, word32 derSz,
- RsaKey* rsaKey, ecc_key* eccKey, ed25519_key* ed25519Key,
- ed448_key* ed448Key)
-{
- int ret;
-#ifdef WOLFSSL_SMALL_STACK
- DerCert* der;
-#else
- DerCert der[1];
-#endif
-
- cert->keyType = eccKey ? ECC_KEY : (ed25519Key ? ED25519_KEY :
- (ed448Key ? ED448_KEY: RSA_KEY));
-
-#ifdef WOLFSSL_SMALL_STACK
- der = (DerCert*)XMALLOC(sizeof(DerCert), cert->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (der == NULL)
- return MEMORY_E;
-#endif
-
- ret = EncodeCertReq(cert, der, rsaKey, eccKey, ed25519Key, ed448Key);
-
- if (ret == 0) {
- if (der->total + MAX_SEQ_SZ * 2 > (int)derSz)
- ret = BUFFER_E;
- else
- ret = cert->bodySz = WriteCertReqBody(der, derBuffer);
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(der, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return ret;
-}
-
-int wc_MakeCertReq_ex(Cert* cert, byte* derBuffer, word32 derSz, int keyType,
- void* key)
-{
- RsaKey* rsaKey = NULL;
- ecc_key* eccKey = NULL;
- ed25519_key* ed25519Key = NULL;
- ed448_key* ed448Key = NULL;
-
- if (keyType == RSA_TYPE)
- rsaKey = (RsaKey*)key;
- else if (keyType == ECC_TYPE)
- eccKey = (ecc_key*)key;
- else if (keyType == ED25519_TYPE)
- ed25519Key = (ed25519_key*)key;
- else if (keyType == ED448_TYPE)
- ed448Key = (ed448_key*)key;
-
- return MakeCertReq(cert, derBuffer, derSz, rsaKey, eccKey, ed25519Key,
- ed448Key);
-}
-
-int wc_MakeCertReq(Cert* cert, byte* derBuffer, word32 derSz,
- RsaKey* rsaKey, ecc_key* eccKey)
-{
- return MakeCertReq(cert, derBuffer, derSz, rsaKey, eccKey, NULL, NULL);
-}
-#endif /* WOLFSSL_CERT_REQ */
-
-
-static int SignCert(int requestSz, int sType, byte* buf, word32 buffSz,
- RsaKey* rsaKey, ecc_key* eccKey, ed25519_key* ed25519Key,
- ed448_key* ed448Key, WC_RNG* rng)
-{
- int sigSz = 0;
- void* heap = NULL;
- CertSignCtx* certSignCtx;
-#ifndef WOLFSSL_ASYNC_CRYPT
- CertSignCtx certSignCtx_lcl;
-
- certSignCtx = &certSignCtx_lcl;
- XMEMSET(certSignCtx, 0, sizeof(CertSignCtx));
-#else
- certSignCtx = NULL;
-#endif
-
- if (requestSz < 0)
- return requestSz;
-
- /* locate ctx */
- if (rsaKey) {
- #ifndef NO_RSA
- #ifdef WOLFSSL_ASYNC_CRYPT
- certSignCtx = &rsaKey->certSignCtx;
- #endif
- heap = rsaKey->heap;
- #else
- return NOT_COMPILED_IN;
- #endif /* NO_RSA */
- }
- else if (eccKey) {
- #ifdef HAVE_ECC
- #ifdef WOLFSSL_ASYNC_CRYPT
- certSignCtx = &eccKey->certSignCtx;
- #endif
- heap = eccKey->heap;
- #else
- return NOT_COMPILED_IN;
- #endif /* HAVE_ECC */
- }
-
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (certSignCtx == NULL) {
- return BAD_FUNC_ARG;
- }
-#endif
-
- if (certSignCtx->sig == NULL) {
- certSignCtx->sig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (certSignCtx->sig == NULL)
- return MEMORY_E;
- }
-
- sigSz = MakeSignature(certSignCtx, buf, requestSz, certSignCtx->sig,
- MAX_ENCODED_SIG_SZ, rsaKey, eccKey, ed25519Key, ed448Key, rng, sType,
- heap);
-#ifdef WOLFSSL_ASYNC_CRYPT
- if (sigSz == WC_PENDING_E) {
- /* Not free'ing certSignCtx->sig here because it could still be in use
- * with async operations. */
- return sigSz;
- }
-#endif
-
- if (sigSz >= 0) {
- if (requestSz + MAX_SEQ_SZ * 2 + sigSz > (int)buffSz)
- sigSz = BUFFER_E;
- else
- sigSz = AddSignature(buf, requestSz, certSignCtx->sig, sigSz,
- sType);
- }
-
- XFREE(certSignCtx->sig, heap, DYNAMIC_TYPE_TMP_BUFFER);
- certSignCtx->sig = NULL;
-
- return sigSz;
-}
-
-int wc_SignCert_ex(int requestSz, int sType, byte* buf, word32 buffSz,
- int keyType, void* key, WC_RNG* rng)
-{
- RsaKey* rsaKey = NULL;
- ecc_key* eccKey = NULL;
- ed25519_key* ed25519Key = NULL;
- ed448_key* ed448Key = NULL;
-
- if (keyType == RSA_TYPE)
- rsaKey = (RsaKey*)key;
- else if (keyType == ECC_TYPE)
- eccKey = (ecc_key*)key;
- else if (keyType == ED25519_TYPE)
- ed25519Key = (ed25519_key*)key;
- else if (keyType == ED448_TYPE)
- ed448Key = (ed448_key*)key;
-
- return SignCert(requestSz, sType, buf, buffSz, rsaKey, eccKey, ed25519Key,
- ed448Key, rng);
-}
-
-int wc_SignCert(int requestSz, int sType, byte* buf, word32 buffSz,
- RsaKey* rsaKey, ecc_key* eccKey, WC_RNG* rng)
-{
- return SignCert(requestSz, sType, buf, buffSz, rsaKey, eccKey, NULL, NULL,
- rng);
-}
-
-int wc_MakeSelfCert(Cert* cert, byte* buf, word32 buffSz,
- RsaKey* key, WC_RNG* rng)
-{
- int ret;
-
- ret = wc_MakeCert(cert, buf, buffSz, key, NULL, rng);
- if (ret < 0)
- return ret;
-
- return wc_SignCert(cert->bodySz, cert->sigType,
- buf, buffSz, key, NULL, rng);
-}
-
-
-#ifdef WOLFSSL_CERT_EXT
-
-/* Get raw subject from cert, which may contain OIDs not parsed by Decode.
- The raw subject pointer will only be valid while "cert" is valid. */
-int wc_GetSubjectRaw(byte **subjectRaw, Cert *cert)
-{
- int rc = BAD_FUNC_ARG;
- if ((subjectRaw != NULL) && (cert != NULL)) {
- *subjectRaw = cert->sbjRaw;
- rc = 0;
- }
- return rc;
-}
-
-/* Set KID from public key */
-static int SetKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey, ecc_key *eckey,
- byte *ntruKey, word16 ntruKeySz,
- ed25519_key* ed25519Key, ed448_key* ed448Key,
- int kid_type)
-{
- byte *buf;
- int bufferSz, ret;
-
- if (cert == NULL ||
- (rsakey == NULL && eckey == NULL && ntruKey == NULL &&
- ed25519Key == NULL && ed448Key == NULL) ||
- (kid_type != SKID_TYPE && kid_type != AKID_TYPE))
- return BAD_FUNC_ARG;
-
- buf = (byte *)XMALLOC(MAX_PUBLIC_KEY_SZ, cert->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (buf == NULL)
- return MEMORY_E;
-
- /* Public Key */
- bufferSz = -1;
-#ifndef NO_RSA
- /* RSA public key */
- if (rsakey != NULL)
- bufferSz = SetRsaPublicKey(buf, rsakey, MAX_PUBLIC_KEY_SZ, 0);
-#endif
-#ifdef HAVE_ECC
- /* ECC public key */
- if (eckey != NULL)
- bufferSz = SetEccPublicKey(buf, eckey, 0);
-#endif
-#ifdef HAVE_NTRU
- /* NTRU public key */
- if (ntruKey != NULL) {
- bufferSz = MAX_PUBLIC_KEY_SZ;
- ret = ntru_crypto_ntru_encrypt_publicKey2SubjectPublicKeyInfo(
- ntruKeySz, ntruKey, (word16 *)(&bufferSz), buf);
- if (ret != NTRU_OK)
- bufferSz = -1;
- }
-#else
- (void)ntruKeySz;
-#endif
-#ifdef HAVE_ED25519
- /* ED25519 public key */
- if (ed25519Key != NULL)
- bufferSz = SetEd25519PublicKey(buf, ed25519Key, 0);
-#endif
-#ifdef HAVE_ED448
- /* ED448 public key */
- if (ed448Key != NULL)
- bufferSz = SetEd448PublicKey(buffer, ed448Key, 0);
-#endif
-
- if (bufferSz <= 0) {
- XFREE(buf, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return PUBLIC_KEY_E;
- }
-
- /* Compute SKID by hashing public key */
- if (kid_type == SKID_TYPE) {
- ret = CalcHashId(buf, bufferSz, cert->skid);
- cert->skidSz = KEYID_SIZE;
- }
- else if (kid_type == AKID_TYPE) {
- ret = CalcHashId(buf, bufferSz, cert->akid);
- cert->akidSz = KEYID_SIZE;
- }
- else
- ret = BAD_FUNC_ARG;
-
- XFREE(buf, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
-}
-
-int wc_SetSubjectKeyIdFromPublicKey_ex(Cert *cert, int keyType, void* key)
-{
- RsaKey* rsaKey = NULL;
- ecc_key* eccKey = NULL;
- ed25519_key* ed25519Key = NULL;
- ed448_key* ed448Key = NULL;
-
- if (keyType == RSA_TYPE)
- rsaKey = (RsaKey*)key;
- else if (keyType == ECC_TYPE)
- eccKey = (ecc_key*)key;
- else if (keyType == ED25519_TYPE)
- ed25519Key = (ed25519_key*)key;
- else if (keyType == ED448_TYPE)
- ed448Key = (ed448_key*)key;
-
- return SetKeyIdFromPublicKey(cert, rsaKey, eccKey, NULL, 0, ed25519Key,
- ed448Key, SKID_TYPE);
-}
-
-/* Set SKID from RSA or ECC public key */
-int wc_SetSubjectKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey, ecc_key *eckey)
-{
- return SetKeyIdFromPublicKey(cert, rsakey, eckey, NULL, 0, NULL, NULL,
- SKID_TYPE);
-}
-
-#ifdef HAVE_NTRU
-/* Set SKID from NTRU public key */
-int wc_SetSubjectKeyIdFromNtruPublicKey(Cert *cert,
- byte *ntruKey, word16 ntruKeySz)
-{
- return SetKeyIdFromPublicKey(cert, NULL,NULL,ntruKey, ntruKeySz, NULL, NULL,
- SKID_TYPE);
-}
-#endif
-
-int wc_SetAuthKeyIdFromPublicKey_ex(Cert *cert, int keyType, void* key)
-{
- RsaKey* rsaKey = NULL;
- ecc_key* eccKey = NULL;
- ed25519_key* ed25519Key = NULL;
- ed448_key* ed448Key = NULL;
-
- if (keyType == RSA_TYPE)
- rsaKey = (RsaKey*)key;
- else if (keyType == ECC_TYPE)
- eccKey = (ecc_key*)key;
- else if (keyType == ED25519_TYPE)
- ed25519Key = (ed25519_key*)key;
- else if (keyType == ED448_TYPE)
- ed448Key = (ed448_key*)key;
-
- return SetKeyIdFromPublicKey(cert, rsaKey, eccKey, NULL, 0, ed25519Key,
- ed448Key, AKID_TYPE);
-}
-
-/* Set SKID from RSA or ECC public key */
-int wc_SetAuthKeyIdFromPublicKey(Cert *cert, RsaKey *rsakey, ecc_key *eckey)
-{
- return SetKeyIdFromPublicKey(cert, rsakey, eckey, NULL, 0, NULL, NULL,
- AKID_TYPE);
-}
-
-
-#if !defined(NO_FILESYSTEM) && !defined(NO_ASN_CRYPT)
-
-/* Set SKID from public key file in PEM */
-int wc_SetSubjectKeyId(Cert *cert, const char* file)
-{
- int ret, derSz;
- byte* der;
- word32 idx;
- RsaKey *rsakey = NULL;
- ecc_key *eckey = NULL;
-
- if (cert == NULL || file == NULL)
- return BAD_FUNC_ARG;
-
- der = (byte*)XMALLOC(MAX_PUBLIC_KEY_SZ, cert->heap, DYNAMIC_TYPE_CERT);
- if (der == NULL) {
- WOLFSSL_MSG("wc_SetSubjectKeyId memory Problem");
- return MEMORY_E;
- }
- derSz = MAX_PUBLIC_KEY_SZ;
-
- XMEMSET(der, 0, derSz);
- derSz = wc_PemPubKeyToDer(file, der, derSz);
- if (derSz <= 0) {
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
- return derSz;
- }
-
- /* Load PubKey in internal structure */
-#ifndef NO_RSA
- rsakey = (RsaKey*) XMALLOC(sizeof(RsaKey), cert->heap, DYNAMIC_TYPE_RSA);
- if (rsakey == NULL) {
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
- return MEMORY_E;
- }
-
- if (wc_InitRsaKey(rsakey, cert->heap) != 0) {
- WOLFSSL_MSG("wc_InitRsaKey failure");
- XFREE(rsakey, cert->heap, DYNAMIC_TYPE_RSA);
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
- return MEMORY_E;
- }
-
- idx = 0;
- ret = wc_RsaPublicKeyDecode(der, &idx, rsakey, derSz);
- if (ret != 0)
-#endif
- {
-#ifndef NO_RSA
- WOLFSSL_MSG("wc_RsaPublicKeyDecode failed");
- wc_FreeRsaKey(rsakey);
- XFREE(rsakey, cert->heap, DYNAMIC_TYPE_RSA);
- rsakey = NULL;
-#endif
-#ifdef HAVE_ECC
- /* Check to load ecc public key */
- eckey = (ecc_key*) XMALLOC(sizeof(ecc_key), cert->heap,
- DYNAMIC_TYPE_ECC);
- if (eckey == NULL) {
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
- return MEMORY_E;
- }
-
- if (wc_ecc_init(eckey) != 0) {
- WOLFSSL_MSG("wc_ecc_init failure");
- wc_ecc_free(eckey);
- XFREE(eckey, cert->heap, DYNAMIC_TYPE_ECC);
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
- return MEMORY_E;
- }
-
- idx = 0;
- ret = wc_EccPublicKeyDecode(der, &idx, eckey, derSz);
- if (ret != 0) {
- WOLFSSL_MSG("wc_EccPublicKeyDecode failed");
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
- wc_ecc_free(eckey);
- XFREE(eckey, cert->heap, DYNAMIC_TYPE_ECC);
- return PUBLIC_KEY_E;
- }
-#else
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
- return PUBLIC_KEY_E;
-#endif /* HAVE_ECC */
- }
-
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
-
- ret = wc_SetSubjectKeyIdFromPublicKey(cert, rsakey, eckey);
-
-#ifndef NO_RSA
- wc_FreeRsaKey(rsakey);
- XFREE(rsakey, cert->heap, DYNAMIC_TYPE_RSA);
-#endif
-#ifdef HAVE_ECC
- wc_ecc_free(eckey);
- XFREE(eckey, cert->heap, DYNAMIC_TYPE_ECC);
-#endif
- return ret;
-}
-
-#endif /* !NO_FILESYSTEM && !NO_ASN_CRYPT */
-
-static int SetAuthKeyIdFromDcert(Cert* cert, DecodedCert* decoded)
-{
- int ret = 0;
-
- /* Subject Key Id not found !! */
- if (decoded->extSubjKeyIdSet == 0) {
- ret = ASN_NO_SKID;
- }
-
- /* SKID invalid size */
- else if (sizeof(cert->akid) < sizeof(decoded->extSubjKeyId)) {
- ret = MEMORY_E;
- }
-
- else {
- /* Put the SKID of CA to AKID of certificate */
- XMEMCPY(cert->akid, decoded->extSubjKeyId, KEYID_SIZE);
- cert->akidSz = KEYID_SIZE;
- }
-
- return ret;
-}
-
-/* Set AKID from certificate contains in buffer (DER encoded) */
-int wc_SetAuthKeyIdFromCert(Cert *cert, const byte *der, int derSz)
-{
- int ret = 0;
-
- if (cert == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- /* Check if decodedCert is cached */
- if (cert->der != der) {
- /* Allocate cache for the decoded cert */
- ret = wc_SetCert_LoadDer(cert, der, derSz);
- }
-
- if (ret >= 0) {
- ret = SetAuthKeyIdFromDcert(cert, (DecodedCert*)cert->decodedCert);
-#ifndef WOLFSSL_CERT_GEN_CACHE
- wc_SetCert_Free(cert);
-#endif
- }
- }
-
- return ret;
-}
-
-
-#ifndef NO_FILESYSTEM
-
-/* Set AKID from certificate file in PEM */
-int wc_SetAuthKeyId(Cert *cert, const char* file)
-{
- int ret;
- int derSz;
- byte* der;
-
- if (cert == NULL || file == NULL)
- return BAD_FUNC_ARG;
-
- der = (byte*)XMALLOC(EIGHTK_BUF, cert->heap, DYNAMIC_TYPE_CERT);
- if (der == NULL) {
- WOLFSSL_MSG("wc_SetAuthKeyId OOF Problem");
- return MEMORY_E;
- }
-
- derSz = wc_PemCertToDer(file, der, EIGHTK_BUF);
- if (derSz <= 0)
- {
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
- return derSz;
- }
-
- ret = wc_SetAuthKeyIdFromCert(cert, der, derSz);
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
-
- return ret;
-}
-
-#endif /* !NO_FILESYSTEM */
-
-/* Set KeyUsage from human readable string */
-int wc_SetKeyUsage(Cert *cert, const char *value)
-{
- int ret = 0;
- char *token, *str, *ptr;
- word32 len;
-
- if (cert == NULL || value == NULL)
- return BAD_FUNC_ARG;
-
- cert->keyUsage = 0;
-
- /* duplicate string (including terminator) */
- len = (word32)XSTRLEN(value);
- str = (char*)XMALLOC(len+1, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (str == NULL)
- return MEMORY_E;
- XMEMCPY(str, value, len+1);
-
- /* parse value, and set corresponding Key Usage value */
- if ((token = XSTRTOK(str, ",", &ptr)) == NULL) {
- XFREE(str, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return KEYUSAGE_E;
- }
- while (token != NULL)
- {
- len = (word32)XSTRLEN(token);
-
- if (!XSTRNCASECMP(token, "digitalSignature", len))
- cert->keyUsage |= KEYUSE_DIGITAL_SIG;
- else if (!XSTRNCASECMP(token, "nonRepudiation", len) ||
- !XSTRNCASECMP(token, "contentCommitment", len))
- cert->keyUsage |= KEYUSE_CONTENT_COMMIT;
- else if (!XSTRNCASECMP(token, "keyEncipherment", len))
- cert->keyUsage |= KEYUSE_KEY_ENCIPHER;
- else if (!XSTRNCASECMP(token, "dataEncipherment", len))
- cert->keyUsage |= KEYUSE_DATA_ENCIPHER;
- else if (!XSTRNCASECMP(token, "keyAgreement", len))
- cert->keyUsage |= KEYUSE_KEY_AGREE;
- else if (!XSTRNCASECMP(token, "keyCertSign", len))
- cert->keyUsage |= KEYUSE_KEY_CERT_SIGN;
- else if (!XSTRNCASECMP(token, "cRLSign", len))
- cert->keyUsage |= KEYUSE_CRL_SIGN;
- else if (!XSTRNCASECMP(token, "encipherOnly", len))
- cert->keyUsage |= KEYUSE_ENCIPHER_ONLY;
- else if (!XSTRNCASECMP(token, "decipherOnly", len))
- cert->keyUsage |= KEYUSE_DECIPHER_ONLY;
- else {
- ret = KEYUSAGE_E;
- break;
- }
-
- token = XSTRTOK(NULL, ",", &ptr);
- }
-
- XFREE(str, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
-}
-
-/* Set ExtendedKeyUsage from human readable string */
-int wc_SetExtKeyUsage(Cert *cert, const char *value)
-{
- int ret = 0;
- char *token, *str, *ptr;
- word32 len;
-
- if (cert == NULL || value == NULL)
- return BAD_FUNC_ARG;
-
- cert->extKeyUsage = 0;
-
- /* duplicate string (including terminator) */
- len = (word32)XSTRLEN(value);
- str = (char*)XMALLOC(len+1, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (str == NULL)
- return MEMORY_E;
- XMEMCPY(str, value, len+1);
-
- /* parse value, and set corresponding Key Usage value */
- if ((token = XSTRTOK(str, ",", &ptr)) == NULL) {
- XFREE(str, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return EXTKEYUSAGE_E;
- }
-
- while (token != NULL)
- {
- len = (word32)XSTRLEN(token);
-
- if (!XSTRNCASECMP(token, "any", len))
- cert->extKeyUsage |= EXTKEYUSE_ANY;
- else if (!XSTRNCASECMP(token, "serverAuth", len))
- cert->extKeyUsage |= EXTKEYUSE_SERVER_AUTH;
- else if (!XSTRNCASECMP(token, "clientAuth", len))
- cert->extKeyUsage |= EXTKEYUSE_CLIENT_AUTH;
- else if (!XSTRNCASECMP(token, "codeSigning", len))
- cert->extKeyUsage |= EXTKEYUSE_CODESIGN;
- else if (!XSTRNCASECMP(token, "emailProtection", len))
- cert->extKeyUsage |= EXTKEYUSE_EMAILPROT;
- else if (!XSTRNCASECMP(token, "timeStamping", len))
- cert->extKeyUsage |= EXTKEYUSE_TIMESTAMP;
- else if (!XSTRNCASECMP(token, "OCSPSigning", len))
- cert->extKeyUsage |= EXTKEYUSE_OCSP_SIGN;
- else {
- ret = EXTKEYUSAGE_E;
- break;
- }
-
- token = XSTRTOK(NULL, ",", &ptr);
- }
-
- XFREE(str, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
-}
-
-#ifdef WOLFSSL_EKU_OID
-/*
- * cert structure to set EKU oid in
- * oid the oid in byte representation
- * sz size of oid buffer
- * idx index of array to place oid
- *
- * returns 0 on success
- */
-int wc_SetExtKeyUsageOID(Cert *cert, const char *in, word32 sz, byte idx,
- void* heap)
-{
- byte oid[MAX_OID_SZ];
- word32 oidSz = MAX_OID_SZ;
-
- if (idx >= CTC_MAX_EKU_NB || sz >= CTC_MAX_EKU_OID_SZ) {
- WOLFSSL_MSG("Either idx or sz was too large");
- return BAD_FUNC_ARG;
- }
-
- if (EncodePolicyOID(oid, &oidSz, in, heap) != 0) {
- return BUFFER_E;
- }
-
- XMEMCPY(cert->extKeyUsageOID[idx], oid, oidSz);
- cert->extKeyUsageOIDSz[idx] = oidSz;
- cert->extKeyUsage |= EXTKEYUSE_USER;
-
- return 0;
-}
-#endif /* WOLFSSL_EKU_OID */
-#endif /* WOLFSSL_CERT_EXT */
-
-
-#ifdef WOLFSSL_ALT_NAMES
-
-static int SetAltNamesFromDcert(Cert* cert, DecodedCert* decoded)
-{
- int ret = 0;
- byte tag;
-
- if (decoded->extensions) {
- int length;
- word32 maxExtensionsIdx;
-
- decoded->srcIdx = decoded->extensionsIdx;
- if (GetASNTag(decoded->source, &decoded->srcIdx, &tag, decoded->maxIdx)
- != 0) {
- return ASN_PARSE_E;
- }
-
- if (tag != ASN_EXTENSIONS) {
- ret = ASN_PARSE_E;
- }
- else if (GetLength(decoded->source, &decoded->srcIdx, &length,
- decoded->maxIdx) < 0) {
- ret = ASN_PARSE_E;
- }
- else if (GetSequence(decoded->source, &decoded->srcIdx, &length,
- decoded->maxIdx) < 0) {
- ret = ASN_PARSE_E;
- }
- else {
- maxExtensionsIdx = decoded->srcIdx + length;
-
- while (decoded->srcIdx < maxExtensionsIdx) {
- word32 oid;
- word32 startIdx = decoded->srcIdx;
- word32 tmpIdx;
-
- if (GetSequence(decoded->source, &decoded->srcIdx, &length,
- decoded->maxIdx) < 0) {
- ret = ASN_PARSE_E;
- break;
- }
-
- tmpIdx = decoded->srcIdx;
- decoded->srcIdx = startIdx;
-
- if (GetAlgoId(decoded->source, &decoded->srcIdx, &oid,
- oidCertExtType, decoded->maxIdx) < 0) {
- ret = ASN_PARSE_E;
- break;
- }
-
- if (oid == ALT_NAMES_OID) {
- cert->altNamesSz = length + (tmpIdx - startIdx);
-
- if (cert->altNamesSz < (int)sizeof(cert->altNames))
- XMEMCPY(cert->altNames, &decoded->source[startIdx],
- cert->altNamesSz);
- else {
- cert->altNamesSz = 0;
- WOLFSSL_MSG("AltNames extensions too big");
- ret = ALT_NAME_E;
- break;
- }
- }
- decoded->srcIdx = tmpIdx + length;
- }
- }
- }
-
- return ret;
-}
-
-#ifndef NO_FILESYSTEM
-
-/* Set Alt Names from der cert, return 0 on success */
-static int SetAltNamesFromCert(Cert* cert, const byte* der, int derSz)
-{
- int ret;
-#ifdef WOLFSSL_SMALL_STACK
- DecodedCert* decoded;
-#else
- DecodedCert decoded[1];
-#endif
-
- if (derSz < 0)
- return derSz;
-
-#ifdef WOLFSSL_SMALL_STACK
- decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), cert->heap,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (decoded == NULL)
- return MEMORY_E;
-#endif
-
- InitDecodedCert(decoded, der, derSz, NULL);
- ret = ParseCertRelative(decoded, CA_TYPE, NO_VERIFY, 0);
-
- if (ret < 0) {
- WOLFSSL_MSG("ParseCertRelative error");
- }
- else {
- ret = SetAltNamesFromDcert(cert, decoded);
- }
-
- FreeDecodedCert(decoded);
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(decoded, cert->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return ret < 0 ? ret : 0;
-}
-
-#endif
-
-static int SetDatesFromDcert(Cert* cert, DecodedCert* decoded)
-{
- int ret = 0;
-
- if (decoded->beforeDate == NULL || decoded->afterDate == NULL) {
- WOLFSSL_MSG("Couldn't extract dates");
- ret = -1;
- }
- else if (decoded->beforeDateLen > MAX_DATE_SIZE ||
- decoded->afterDateLen > MAX_DATE_SIZE) {
- WOLFSSL_MSG("Bad date size");
- ret = -1;
- }
- else {
- XMEMCPY(cert->beforeDate, decoded->beforeDate, decoded->beforeDateLen);
- XMEMCPY(cert->afterDate, decoded->afterDate, decoded->afterDateLen);
-
- cert->beforeDateSz = decoded->beforeDateLen;
- cert->afterDateSz = decoded->afterDateLen;
- }
-
- return ret;
-}
-
-#endif /* WOLFSSL_ALT_NAMES */
-
-static void SetNameFromDcert(CertName* cn, DecodedCert* decoded)
-{
- int sz;
-
- if (decoded->subjectCN) {
- sz = (decoded->subjectCNLen < CTC_NAME_SIZE) ? decoded->subjectCNLen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->commonName, decoded->subjectCN, sz);
- cn->commonName[sz] = '\0';
- cn->commonNameEnc = decoded->subjectCNEnc;
- }
- if (decoded->subjectC) {
- sz = (decoded->subjectCLen < CTC_NAME_SIZE) ? decoded->subjectCLen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->country, decoded->subjectC, sz);
- cn->country[sz] = '\0';
- cn->countryEnc = decoded->subjectCEnc;
- }
- if (decoded->subjectST) {
- sz = (decoded->subjectSTLen < CTC_NAME_SIZE) ? decoded->subjectSTLen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->state, decoded->subjectST, sz);
- cn->state[sz] = '\0';
- cn->stateEnc = decoded->subjectSTEnc;
- }
- if (decoded->subjectL) {
- sz = (decoded->subjectLLen < CTC_NAME_SIZE) ? decoded->subjectLLen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->locality, decoded->subjectL, sz);
- cn->locality[sz] = '\0';
- cn->localityEnc = decoded->subjectLEnc;
- }
- if (decoded->subjectO) {
- sz = (decoded->subjectOLen < CTC_NAME_SIZE) ? decoded->subjectOLen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->org, decoded->subjectO, sz);
- cn->org[sz] = '\0';
- cn->orgEnc = decoded->subjectOEnc;
- }
- if (decoded->subjectOU) {
- sz = (decoded->subjectOULen < CTC_NAME_SIZE) ? decoded->subjectOULen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->unit, decoded->subjectOU, sz);
- cn->unit[sz] = '\0';
- cn->unitEnc = decoded->subjectOUEnc;
- }
- if (decoded->subjectSN) {
- sz = (decoded->subjectSNLen < CTC_NAME_SIZE) ? decoded->subjectSNLen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->sur, decoded->subjectSN, sz);
- cn->sur[sz] = '\0';
- cn->surEnc = decoded->subjectSNEnc;
- }
- if (decoded->subjectSND) {
- sz = (decoded->subjectSNDLen < CTC_NAME_SIZE) ? decoded->subjectSNDLen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->serialDev, decoded->subjectSND, sz);
- cn->serialDev[sz] = '\0';
- cn->serialDevEnc = decoded->subjectSNDEnc;
- }
-#ifdef WOLFSSL_CERT_EXT
- if (decoded->subjectBC) {
- sz = (decoded->subjectBCLen < CTC_NAME_SIZE) ? decoded->subjectBCLen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->busCat, decoded->subjectBC, sz);
- cn->busCat[sz] = '\0';
- cn->busCatEnc = decoded->subjectBCEnc;
- }
- if (decoded->subjectJC) {
- sz = (decoded->subjectJCLen < CTC_NAME_SIZE) ? decoded->subjectJCLen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->joiC, decoded->subjectJC, sz);
- cn->joiC[sz] = '\0';
- cn->joiCEnc = decoded->subjectJCEnc;
- }
- if (decoded->subjectJS) {
- sz = (decoded->subjectJSLen < CTC_NAME_SIZE) ? decoded->subjectJSLen
- : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->joiSt, decoded->subjectJS, sz);
- cn->joiSt[sz] = '\0';
- cn->joiStEnc = decoded->subjectJSEnc;
- }
-#endif
- if (decoded->subjectEmail) {
- sz = (decoded->subjectEmailLen < CTC_NAME_SIZE)
- ? decoded->subjectEmailLen : CTC_NAME_SIZE - 1;
- XSTRNCPY(cn->email, decoded->subjectEmail, sz);
- cn->email[sz] = '\0';
- }
-}
-
-#ifndef NO_FILESYSTEM
-
-/* Set cn name from der buffer, return 0 on success */
-static int SetNameFromCert(CertName* cn, const byte* der, int derSz)
-{
- int ret;
-#ifdef WOLFSSL_SMALL_STACK
- DecodedCert* decoded;
-#else
- DecodedCert decoded[1];
-#endif
-
- if (derSz < 0)
- return derSz;
-
-#ifdef WOLFSSL_SMALL_STACK
- decoded = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (decoded == NULL)
- return MEMORY_E;
-#endif
-
- InitDecodedCert(decoded, der, derSz, NULL);
- ret = ParseCertRelative(decoded, CA_TYPE, NO_VERIFY, 0);
-
- if (ret < 0) {
- WOLFSSL_MSG("ParseCertRelative error");
- }
- else {
- SetNameFromDcert(cn, decoded);
- }
-
- FreeDecodedCert(decoded);
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return ret < 0 ? ret : 0;
-}
-
-/* Set cert issuer from issuerFile in PEM */
-int wc_SetIssuer(Cert* cert, const char* issuerFile)
-{
- int ret;
- int derSz;
- byte* der;
-
- if (cert == NULL) {
- return BAD_FUNC_ARG;
- }
-
- der = (byte*)XMALLOC(EIGHTK_BUF, cert->heap, DYNAMIC_TYPE_CERT);
- if (der == NULL) {
- WOLFSSL_MSG("wc_SetIssuer OOF Problem");
- return MEMORY_E;
- }
- derSz = wc_PemCertToDer(issuerFile, der, EIGHTK_BUF);
- cert->selfSigned = 0;
- ret = SetNameFromCert(&cert->issuer, der, derSz);
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
-
- return ret;
-}
-
-
-/* Set cert subject from subjectFile in PEM */
-int wc_SetSubject(Cert* cert, const char* subjectFile)
-{
- int ret;
- int derSz;
- byte* der;
-
- if (cert == NULL) {
- return BAD_FUNC_ARG;
- }
-
- der = (byte*)XMALLOC(EIGHTK_BUF, cert->heap, DYNAMIC_TYPE_CERT);
- if (der == NULL) {
- WOLFSSL_MSG("wc_SetSubject OOF Problem");
- return MEMORY_E;
- }
-
- derSz = wc_PemCertToDer(subjectFile, der, EIGHTK_BUF);
- ret = SetNameFromCert(&cert->subject, der, derSz);
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
-
- return ret;
-}
-
-#ifdef WOLFSSL_ALT_NAMES
-
-/* Set alt names from file in PEM */
-int wc_SetAltNames(Cert* cert, const char* file)
-{
- int ret;
- int derSz;
- byte* der;
-
- if (cert == NULL) {
- return BAD_FUNC_ARG;
- }
-
- der = (byte*)XMALLOC(EIGHTK_BUF, cert->heap, DYNAMIC_TYPE_CERT);
- if (der == NULL) {
- WOLFSSL_MSG("wc_SetAltNames OOF Problem");
- return MEMORY_E;
- }
- derSz = wc_PemCertToDer(file, der, EIGHTK_BUF);
- ret = SetAltNamesFromCert(cert, der, derSz);
- XFREE(der, cert->heap, DYNAMIC_TYPE_CERT);
-
- return ret;
-}
-
-#endif /* WOLFSSL_ALT_NAMES */
-
-#endif /* !NO_FILESYSTEM */
-
-/* Set cert issuer from DER buffer */
-int wc_SetIssuerBuffer(Cert* cert, const byte* der, int derSz)
-{
- int ret = 0;
-
- if (cert == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- cert->selfSigned = 0;
-
- /* Check if decodedCert is cached */
- if (cert->der != der) {
- /* Allocate cache for the decoded cert */
- ret = wc_SetCert_LoadDer(cert, der, derSz);
- }
-
- if (ret >= 0) {
- SetNameFromDcert(&cert->issuer, (DecodedCert*)cert->decodedCert);
-#ifndef WOLFSSL_CERT_GEN_CACHE
- wc_SetCert_Free(cert);
-#endif
- }
- }
-
- return ret;
-}
-
-/* Set cert subject from DER buffer */
-int wc_SetSubjectBuffer(Cert* cert, const byte* der, int derSz)
-{
- int ret = 0;
-
- if (cert == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- /* Check if decodedCert is cached */
- if (cert->der != der) {
- /* Allocate cache for the decoded cert */
- ret = wc_SetCert_LoadDer(cert, der, derSz);
- }
-
- if (ret >= 0) {
- SetNameFromDcert(&cert->subject, (DecodedCert*)cert->decodedCert);
-#ifndef WOLFSSL_CERT_GEN_CACHE
- wc_SetCert_Free(cert);
-#endif
- }
- }
-
- return ret;
-}
-#ifdef WOLFSSL_CERT_EXT
-/* Set cert raw subject from DER buffer */
-int wc_SetSubjectRaw(Cert* cert, const byte* der, int derSz)
-{
- int ret = 0;
-
- if (cert == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- /* Check if decodedCert is cached */
- if (cert->der != der) {
- /* Allocate cache for the decoded cert */
- ret = wc_SetCert_LoadDer(cert, der, derSz);
- }
-
- if (ret >= 0) {
- if ((((DecodedCert*)cert->decodedCert)->subjectRaw) &&
- (((DecodedCert*)cert->decodedCert)->subjectRawLen <=
- (int)sizeof(CertName))) {
- XMEMCPY(cert->sbjRaw,
- ((DecodedCert*)cert->decodedCert)->subjectRaw,
- ((DecodedCert*)cert->decodedCert)->subjectRawLen);
- }
-#ifndef WOLFSSL_CERT_GEN_CACHE
- wc_SetCert_Free(cert);
-#endif
- }
- }
-
- return ret;
-}
-
-/* Set cert raw issuer from DER buffer */
-int wc_SetIssuerRaw(Cert* cert, const byte* der, int derSz)
-{
- int ret = 0;
-
- if (cert == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- /* Check if decodedCert is cached */
- if (cert->der != der) {
- /* Allocate cache for the decoded cert */
- ret = wc_SetCert_LoadDer(cert, der, derSz);
- }
-
- if (ret >= 0) {
- if ((((DecodedCert*)cert->decodedCert)->issuerRaw) &&
- (((DecodedCert*)cert->decodedCert)->issuerRawLen <=
- (int)sizeof(CertName))) {
- XMEMCPY(cert->issRaw,
- ((DecodedCert*)cert->decodedCert)->issuerRaw,
- ((DecodedCert*)cert->decodedCert)->issuerRawLen);
- }
-#ifndef WOLFSSL_CERT_GEN_CACHE
- wc_SetCert_Free(cert);
-#endif
- }
- }
- return ret;
-}
-#endif
-
-#ifdef WOLFSSL_ALT_NAMES
-
-/* Set cert alt names from DER buffer */
-int wc_SetAltNamesBuffer(Cert* cert, const byte* der, int derSz)
-{
- int ret = 0;
-
- if (cert == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- /* Check if decodedCert is cached */
- if (cert->der != der) {
- /* Allocate cache for the decoded cert */
- ret = wc_SetCert_LoadDer(cert, der, derSz);
- }
-
- if (ret >= 0) {
- ret = SetAltNamesFromDcert(cert, (DecodedCert*)cert->decodedCert);
-#ifndef WOLFSSL_CERT_GEN_CACHE
- wc_SetCert_Free(cert);
-#endif
- }
- }
-
- return(ret);
-}
-
-/* Set cert dates from DER buffer */
-int wc_SetDatesBuffer(Cert* cert, const byte* der, int derSz)
-{
- int ret = 0;
-
- if (cert == NULL) {
- ret = BAD_FUNC_ARG;
- }
- else {
- /* Check if decodedCert is cached */
- if (cert->der != der) {
- /* Allocate cache for the decoded cert */
- ret = wc_SetCert_LoadDer(cert, der, derSz);
- }
-
- if (ret >= 0) {
- ret = SetDatesFromDcert(cert, (DecodedCert*)cert->decodedCert);
-#ifndef WOLFSSL_CERT_GEN_CACHE
- wc_SetCert_Free(cert);
-#endif
- }
- }
-
- return(ret);
-}
-
-#endif /* WOLFSSL_ALT_NAMES */
-
-#endif /* WOLFSSL_CERT_GEN */
-
-#if (defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)) \
- || defined(OPENSSL_EXTRA)
-/* Encode OID string representation to ITU-T X.690 format */
-int EncodePolicyOID(byte *out, word32 *outSz, const char *in, void* heap)
-{
- word32 val, idx = 0, nb_val;
- char *token, *str, *ptr;
- word32 len;
-
- (void)heap;
-
- if (out == NULL || outSz == NULL || *outSz < 2 || in == NULL)
- return BAD_FUNC_ARG;
-
- /* duplicate string (including terminator) */
- len = (word32)XSTRLEN(in);
- str = (char *)XMALLOC(len+1, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (str == NULL)
- return MEMORY_E;
- XMEMCPY(str, in, len+1);
-
- nb_val = 0;
-
- /* parse value, and set corresponding Policy OID value */
- token = XSTRTOK(str, ".", &ptr);
- while (token != NULL)
- {
- val = (word32)XATOI(token);
-
- if (nb_val == 0) {
- if (val > 2) {
- XFREE(str, heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ASN_OBJECT_ID_E;
- }
-
- out[idx] = (byte)(40 * val);
- }
- else if (nb_val == 1) {
- if (val > 127) {
- XFREE(str, heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ASN_OBJECT_ID_E;
- }
-
- if (idx > *outSz) {
- XFREE(str, heap, DYNAMIC_TYPE_TMP_BUFFER);
- return BUFFER_E;
- }
-
- out[idx++] += (byte)val;
- }
- else {
- word32 tb = 0, x;
- int i = 0;
- byte oid[MAX_OID_SZ];
-
- while (val >= 128) {
- x = val % 128;
- val /= 128;
- oid[i++] = (byte) (((tb++) ? 0x80 : 0) | x);
- }
-
- if ((idx+(word32)i) > *outSz) {
- XFREE(str, heap, DYNAMIC_TYPE_TMP_BUFFER);
- return BUFFER_E;
- }
-
- oid[i] = (byte) (((tb++) ? 0x80 : 0) | val);
-
- /* push value in the right order */
- while (i >= 0)
- out[idx++] = oid[i--];
- }
-
- token = XSTRTOK(NULL, ".", &ptr);
- nb_val++;
- }
-
- *outSz = idx;
-
- XFREE(str, heap, DYNAMIC_TYPE_TMP_BUFFER);
- return 0;
-}
-#endif /* WOLFSSL_CERT_EXT || OPENSSL_EXTRA */
-
-#endif /* !NO_CERTS */
-
-#if !defined(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL))
-/* Helper function for wolfSSL_i2d_DHparams */
-int StoreDHparams(byte* out, word32* outLen, mp_int* p, mp_int* g)
-{
- word32 idx = 0;
- int pSz;
- int gSz;
- unsigned int tmp;
- word32 headerSz = 4; /* 2*ASN_TAG + 2*LEN(ENUM) */
-
- /* If the leading bit on the INTEGER is a 1, add a leading zero */
- int pLeadingZero = mp_leading_bit(p);
- int gLeadingZero = mp_leading_bit(g);
- int pLen = mp_unsigned_bin_size(p);
- int gLen = mp_unsigned_bin_size(g);
-
- WOLFSSL_ENTER("StoreDHparams");
- if (out == NULL) {
- WOLFSSL_MSG("Null buffer error");
- return BUFFER_E;
- }
-
- tmp = pLeadingZero + gLeadingZero + pLen + gLen;
- if (*outLen < (tmp + headerSz)) {
- return BUFFER_E;
- }
-
- /* Set sequence */
- idx = SetSequence(tmp + headerSz + 2, out);
-
- /* Encode p */
- pSz = SetASNIntMP(p, -1, &out[idx]);
- if (pSz < 0) {
- WOLFSSL_MSG("SetASNIntMP failed");
- return pSz;
- }
- idx += pSz;
-
- /* Encode g */
- gSz = SetASNIntMP(g, -1, &out[idx]);
- if (gSz < 0) {
- WOLFSSL_MSG("SetASNIntMP failed");
- return gSz;
- }
- idx += gSz;
-
- *outLen = idx;
-
- return 0;
-}
-#endif /* !NO_DH && WOLFSSL_QT || OPENSSL_ALL */
-
-#ifdef HAVE_ECC
-
-/* Der Encode r & s ints into out, outLen is (in/out) size */
-int StoreECC_DSA_Sig(byte* out, word32* outLen, mp_int* r, mp_int* s)
-{
- word32 idx = 0;
- int rSz; /* encoding size */
- int sSz;
- word32 headerSz = 4; /* 2*ASN_TAG + 2*LEN(ENUM) */
-
- /* If the leading bit on the INTEGER is a 1, add a leading zero */
- int rLeadingZero = mp_leading_bit(r);
- int sLeadingZero = mp_leading_bit(s);
- int rLen = mp_unsigned_bin_size(r); /* big int size */
- int sLen = mp_unsigned_bin_size(s);
-
- if (*outLen < (rLen + rLeadingZero + sLen + sLeadingZero +
- headerSz + 2)) /* SEQ_TAG + LEN(ENUM) */
- return BUFFER_E;
-
- idx = SetSequence(rLen + rLeadingZero + sLen+sLeadingZero + headerSz, out);
-
- /* store r */
- rSz = SetASNIntMP(r, -1, &out[idx]);
- if (rSz < 0)
- return rSz;
- idx += rSz;
-
- /* store s */
- sSz = SetASNIntMP(s, -1, &out[idx]);
- if (sSz < 0)
- return sSz;
- idx += sSz;
-
- *outLen = idx;
-
- return 0;
-}
-
-
-/* Der Decode ECC-DSA Signature, r & s stored as big ints */
-int DecodeECC_DSA_Sig(const byte* sig, word32 sigLen, mp_int* r, mp_int* s)
-{
- word32 idx = 0;
- int len = 0;
-
- if (GetSequence(sig, &idx, &len, sigLen) < 0) {
- return ASN_ECC_KEY_E;
- }
-
-#ifndef NO_STRICT_ECDSA_LEN
- /* enable strict length checking for signature */
- if (sigLen != idx + (word32)len) {
- return ASN_ECC_KEY_E;
- }
-#else
- /* allow extra signature bytes at end */
- if ((word32)len > (sigLen - idx)) {
- return ASN_ECC_KEY_E;
- }
-#endif
-
- if (GetInt(r, sig, &idx, sigLen) < 0) {
- return ASN_ECC_KEY_E;
- }
-
- if (GetInt(s, sig, &idx, sigLen) < 0) {
- return ASN_ECC_KEY_E;
- }
-
- return 0;
-}
-
-
-int wc_EccPrivateKeyDecode(const byte* input, word32* inOutIdx, ecc_key* key,
- word32 inSz)
-{
- word32 oidSum;
- int version, length;
- int privSz, pubSz = 0;
- byte b;
- int ret = 0;
- int curve_id = ECC_CURVE_DEF;
-#ifdef WOLFSSL_SMALL_STACK
- byte* priv;
- byte* pub;
-#else
- byte priv[ECC_MAXSIZE+1];
- byte pub[2*(ECC_MAXSIZE+1)]; /* public key has two parts plus header */
-#endif
- byte* pubData = NULL;
-
- if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0)
- return BAD_FUNC_ARG;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (GetMyVersion(input, inOutIdx, &version, inSz) < 0)
- return ASN_PARSE_E;
-
- if (*inOutIdx >= inSz)
- return ASN_PARSE_E;
-
- b = input[*inOutIdx];
- *inOutIdx += 1;
-
- /* priv type */
- if (b != 4 && b != 6 && b != 7)
- return ASN_PARSE_E;
-
- if (GetLength(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (length > ECC_MAXSIZE)
- return BUFFER_E;
-
-#ifdef WOLFSSL_SMALL_STACK
- priv = (byte*)XMALLOC(ECC_MAXSIZE+1, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (priv == NULL)
- return MEMORY_E;
-
- pub = (byte*)XMALLOC(2*(ECC_MAXSIZE+1), key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (pub == NULL) {
- XFREE(priv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#endif
-
- /* priv key */
- privSz = length;
- XMEMCPY(priv, &input[*inOutIdx], privSz);
- *inOutIdx += length;
-
- if ((*inOutIdx + 1) < inSz) {
- /* prefix 0, may have */
- b = input[*inOutIdx];
- if (b == ECC_PREFIX_0) {
- *inOutIdx += 1;
-
- if (GetLength(input, inOutIdx, &length, inSz) <= 0)
- ret = ASN_PARSE_E;
- else {
- ret = GetObjectId(input, inOutIdx, &oidSum, oidIgnoreType,
- inSz);
- if (ret == 0) {
- if ((ret = CheckCurve(oidSum)) < 0)
- ret = ECC_CURVE_OID_E;
- else {
- curve_id = ret;
- ret = 0;
- }
- }
- }
- }
- }
-
- if (ret == 0 && (*inOutIdx + 1) < inSz) {
- /* prefix 1 */
- b = input[*inOutIdx];
- *inOutIdx += 1;
-
- if (b != ECC_PREFIX_1) {
- ret = ASN_ECC_KEY_E;
- }
- else if (GetLength(input, inOutIdx, &length, inSz) <= 0) {
- ret = ASN_PARSE_E;
- }
- else {
- /* key header */
- ret = CheckBitString(input, inOutIdx, &length, inSz, 0, NULL);
- if (ret == 0) {
- /* pub key */
- pubSz = length;
- if (pubSz < 2*(ECC_MAXSIZE+1)) {
- XMEMCPY(pub, &input[*inOutIdx], pubSz);
- *inOutIdx += length;
- pubData = pub;
- }
- else
- ret = BUFFER_E;
- }
- }
- }
-
- if (ret == 0) {
- ret = wc_ecc_import_private_key_ex(priv, privSz, pubData, pubSz, key,
- curve_id);
- }
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(priv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return ret;
-}
-
-
-#ifdef WOLFSSL_CUSTOM_CURVES
-static void ByteToHex(byte n, char* str)
-{
- const char hexChar[] = { '0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
-
- str[0] = hexChar[n >> 4];
- str[1] = hexChar[n & 0xf];
-}
-
-/* returns 0 on success */
-static int ASNToHexString(const byte* input, word32* inOutIdx, char** out,
- word32 inSz, void* heap, int heapType)
-{
- int len;
- int i;
- char* str;
- word32 localIdx;
- byte tag;
-
- if (*inOutIdx >= inSz) {
- return BUFFER_E;
- }
-
- localIdx = *inOutIdx;
- if (GetASNTag(input, &localIdx, &tag, inSz) == 0 && tag == ASN_INTEGER) {
- if (GetASNInt(input, inOutIdx, &len, inSz) < 0)
- return ASN_PARSE_E;
- }
- else {
- if (GetOctetString(input, inOutIdx, &len, inSz) < 0)
- return ASN_PARSE_E;
- }
-
- str = (char*)XMALLOC(len * 2 + 1, heap, heapType);
- for (i=0; i MAX_ECC_STRING) {
- WOLFSSL_MSG("ECC Param too large for buffer");
- ret = BUFFER_E;
- }
- else {
- XSTRNCPY(*dst, src, length);
- }
- XFREE(src, key->heap, DYNAMIC_TYPE_ECC_BUFFER);
-#endif
-
- return ret;
-}
-#endif /* WOLFSSL_CUSTOM_CURVES */
-
-int wc_EccPublicKeyDecode(const byte* input, word32* inOutIdx,
- ecc_key* key, word32 inSz)
-{
- int length;
- int ret;
- int curve_id = ECC_CURVE_DEF;
- word32 oidSum, localIdx;
- byte tag;
-
- if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0)
- return BAD_FUNC_ARG;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- ret = SkipObjectId(input, inOutIdx, inSz);
- if (ret != 0)
- return ret;
-
- if (*inOutIdx >= inSz) {
- return BUFFER_E;
- }
-
- localIdx = *inOutIdx;
- if (GetASNTag(input, &localIdx, &tag, inSz) == 0 &&
- tag == (ASN_SEQUENCE | ASN_CONSTRUCTED)) {
-#ifdef WOLFSSL_CUSTOM_CURVES
- ecc_set_type* curve;
- int len;
- char* point = NULL;
-
- ret = 0;
-
- curve = (ecc_set_type*)XMALLOC(sizeof(*curve), key->heap,
- DYNAMIC_TYPE_ECC_BUFFER);
- if (curve == NULL)
- ret = MEMORY_E;
-
- if (ret == 0) {
- static const char customName[] = "Custom";
- XMEMSET(curve, 0, sizeof(*curve));
- #ifndef WOLFSSL_ECC_CURVE_STATIC
- curve->name = customName;
- #else
- XMEMCPY((void*)curve->name, customName, sizeof(customName));
- #endif
- curve->id = ECC_CURVE_CUSTOM;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- ret = ASN_PARSE_E;
- }
-
- if (ret == 0) {
- GetInteger7Bit(input, inOutIdx, inSz);
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- ret = ASN_PARSE_E;
- }
- if (ret == 0) {
- char* p = NULL;
- SkipObjectId(input, inOutIdx, inSz);
- ret = ASNToHexString(input, inOutIdx, &p, inSz,
- key->heap, DYNAMIC_TYPE_ECC_BUFFER);
- if (ret == 0)
- ret = EccKeyParamCopy((char**)&curve->prime, p);
- }
- if (ret == 0) {
- curve->size = (int)XSTRLEN(curve->prime) / 2;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- ret = ASN_PARSE_E;
- }
- if (ret == 0) {
- char* af = NULL;
- ret = ASNToHexString(input, inOutIdx, &af, inSz,
- key->heap, DYNAMIC_TYPE_ECC_BUFFER);
- if (ret == 0)
- ret = EccKeyParamCopy((char**)&curve->Af, af);
- }
- if (ret == 0) {
- char* bf = NULL;
- ret = ASNToHexString(input, inOutIdx, &bf, inSz,
- key->heap, DYNAMIC_TYPE_ECC_BUFFER);
- if (ret == 0)
- ret = EccKeyParamCopy((char**)&curve->Bf, bf);
- }
- if (ret == 0) {
- localIdx = *inOutIdx;
- if (*inOutIdx < inSz && GetASNTag(input, &localIdx, &tag, inSz)
- == 0 && tag == ASN_BIT_STRING) {
- len = 0;
- ret = GetASNHeader(input, ASN_BIT_STRING, inOutIdx, &len, inSz);
- *inOutIdx += len;
- }
- }
- if (ret == 0) {
- ret = ASNToHexString(input, inOutIdx, (char**)&point, inSz,
- key->heap, DYNAMIC_TYPE_ECC_BUFFER);
-
- /* sanity check that point buffer is not smaller than the expected
- * size to hold ( 0 4 || Gx || Gy )
- * where Gx and Gy are each the size of curve->size * 2 */
- if (ret == 0 && (int)XSTRLEN(point) < (curve->size * 4) + 2) {
- XFREE(point, key->heap, DYNAMIC_TYPE_ECC_BUFFER);
- ret = BUFFER_E;
- }
- }
- if (ret == 0) {
- #ifndef WOLFSSL_ECC_CURVE_STATIC
- curve->Gx = (const char*)XMALLOC(curve->size * 2 + 2, key->heap,
- DYNAMIC_TYPE_ECC_BUFFER);
- curve->Gy = (const char*)XMALLOC(curve->size * 2 + 2, key->heap,
- DYNAMIC_TYPE_ECC_BUFFER);
- if (curve->Gx == NULL || curve->Gy == NULL) {
- XFREE(point, key->heap, DYNAMIC_TYPE_ECC_BUFFER);
- ret = MEMORY_E;
- }
- #else
- if (curve->size * 2 + 2 > MAX_ECC_STRING) {
- WOLFSSL_MSG("curve size is too large to fit in buffer");
- ret = BUFFER_E;
- }
- #endif
- }
- if (ret == 0) {
- char* o = NULL;
-
- XMEMCPY((char*)curve->Gx, point + 2, curve->size * 2);
- XMEMCPY((char*)curve->Gy, point + curve->size * 2 + 2,
- curve->size * 2);
- ((char*)curve->Gx)[curve->size * 2] = '\0';
- ((char*)curve->Gy)[curve->size * 2] = '\0';
- XFREE(point, key->heap, DYNAMIC_TYPE_ECC_BUFFER);
- ret = ASNToHexString(input, inOutIdx, &o, inSz,
- key->heap, DYNAMIC_TYPE_ECC_BUFFER);
- if (ret == 0)
- ret = EccKeyParamCopy((char**)&curve->order, o);
- }
- if (ret == 0) {
- curve->cofactor = GetInteger7Bit(input, inOutIdx, inSz);
-
- #ifndef WOLFSSL_ECC_CURVE_STATIC
- curve->oid = NULL;
- #else
- XMEMSET((void*)curve->oid, 0, sizeof(curve->oid));
- #endif
- curve->oidSz = 0;
- curve->oidSum = 0;
-
- if (wc_ecc_set_custom_curve(key, curve) < 0) {
- ret = ASN_PARSE_E;
- }
- #ifdef WOLFSSL_CUSTOM_CURVES
- key->deallocSet = 1;
- #endif
- curve = NULL;
- }
- if (curve != NULL)
- wc_ecc_free_curve(curve, key->heap);
-
- if (ret < 0)
- return ret;
-#else
- return ASN_PARSE_E;
-#endif /* WOLFSSL_CUSTOM_CURVES */
- }
- else {
- /* ecc params information */
- ret = GetObjectId(input, inOutIdx, &oidSum, oidIgnoreType, inSz);
- if (ret != 0)
- return ret;
-
- /* get curve id */
- curve_id = wc_ecc_get_oid(oidSum, NULL, 0);
- if (curve_id < 0)
- return ECC_CURVE_OID_E;
- }
-
- /* key header */
- ret = CheckBitString(input, inOutIdx, &length, inSz, 1, NULL);
- if (ret != 0)
- return ret;
-
- /* This is the raw point data compressed or uncompressed. */
- if (wc_ecc_import_x963_ex(input + *inOutIdx, length, key,
- curve_id) != 0) {
- return ASN_ECC_KEY_E;
- }
-
- *inOutIdx += length;
-
- return 0;
-}
-
-#if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT)
-/* build DER formatted ECC key, include optional public key if requested,
- * return length on success, negative on error */
-static int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 inLen,
- int pubIn)
-{
- byte curve[MAX_ALGO_SZ+2];
- byte ver[MAX_VERSION_SZ];
- byte seq[MAX_SEQ_SZ];
- byte *prv = NULL, *pub = NULL;
- int ret, totalSz, curveSz, verSz;
- int privHdrSz = ASN_ECC_HEADER_SZ;
- int pubHdrSz = ASN_ECC_CONTEXT_SZ + ASN_ECC_HEADER_SZ;
-
- word32 idx = 0, prvidx = 0, pubidx = 0, curveidx = 0;
- word32 seqSz, privSz, pubSz = ECC_BUFSIZE;
-
- if (key == NULL || output == NULL || inLen == 0)
- return BAD_FUNC_ARG;
-
- /* curve */
- curve[curveidx++] = ECC_PREFIX_0;
- curveidx++ /* to put the size after computation */;
- curveSz = SetCurve(key, curve+curveidx);
- if (curveSz < 0)
- return curveSz;
- /* set computed size */
- curve[1] = (byte)curveSz;
- curveidx += curveSz;
-
- /* private */
- privSz = key->dp->size;
- prv = (byte*)XMALLOC(privSz + privHdrSz + MAX_SEQ_SZ,
- key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (prv == NULL) {
- return MEMORY_E;
- }
- prvidx += SetOctetString8Bit(key->dp->size, &prv[prvidx]);
- ret = wc_ecc_export_private_only(key, prv + prvidx, &privSz);
- if (ret < 0) {
- XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
- prvidx += privSz;
-
- /* pubIn */
- if (pubIn) {
- ret = wc_ecc_export_x963(key, NULL, &pubSz);
- if (ret != LENGTH_ONLY_E) {
- XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
-
- pub = (byte*)XMALLOC(pubSz + pubHdrSz + MAX_SEQ_SZ,
- key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (pub == NULL) {
- XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-
- pub[pubidx++] = ECC_PREFIX_1;
- if (pubSz > 128) /* leading zero + extra size byte */
- pubidx += SetLength(pubSz + ASN_ECC_CONTEXT_SZ + 2, pub+pubidx);
- else /* leading zero */
- pubidx += SetLength(pubSz + ASN_ECC_CONTEXT_SZ + 1, pub+pubidx);
-
- /* SetBitString adds leading zero */
- pubidx += SetBitString(pubSz, 0, pub + pubidx);
- ret = wc_ecc_export_x963(key, pub + pubidx, &pubSz);
- if (ret != 0) {
- XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
- pubidx += pubSz;
- }
-
- /* make headers */
- verSz = SetMyVersion(1, ver, FALSE);
- seqSz = SetSequence(verSz + prvidx + pubidx + curveidx, seq);
-
- totalSz = prvidx + pubidx + curveidx + verSz + seqSz;
- if (totalSz > (int)inLen) {
- XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (pubIn) {
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
- return BAD_FUNC_ARG;
- }
-
- /* write out */
- /* seq */
- XMEMCPY(output + idx, seq, seqSz);
- idx = seqSz;
-
- /* ver */
- XMEMCPY(output + idx, ver, verSz);
- idx += verSz;
-
- /* private */
- XMEMCPY(output + idx, prv, prvidx);
- idx += prvidx;
- XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-
- /* curve */
- XMEMCPY(output + idx, curve, curveidx);
- idx += curveidx;
-
- /* pubIn */
- if (pubIn) {
- XMEMCPY(output + idx, pub, pubidx);
- /* idx += pubidx; not used after write, if more data remove comment */
- XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- }
-
- return totalSz;
-}
-
-/* Write a Private ecc key, including public to DER format,
- * length on success else < 0 */
-int wc_EccKeyToDer(ecc_key* key, byte* output, word32 inLen)
-{
- return wc_BuildEccKeyDer(key, output, inLen, 1);
-}
-
-
-/* Write only private ecc key to DER format,
- * length on success else < 0 */
-int wc_EccPrivateKeyToDer(ecc_key* key, byte* output, word32 inLen)
-{
- return wc_BuildEccKeyDer(key, output, inLen, 0);
-}
-
-#ifdef HAVE_PKCS8
-/* Write only private ecc key to unencrypted PKCS#8 format.
- *
- * If output is NULL, places required PKCS#8 buffer size in outLen and
- * returns LENGTH_ONLY_E.
- *
- * return length on success else < 0 */
-int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen)
-{
- int ret, tmpDerSz;
- int algoID = 0;
- word32 oidSz = 0;
- word32 pkcs8Sz = 0;
- const byte* curveOID = NULL;
- byte* tmpDer = NULL;
-
- if (key == NULL || outLen == NULL)
- return BAD_FUNC_ARG;
-
- /* set algoID, get curve OID */
- algoID = ECDSAk;
- ret = wc_ecc_get_oid(key->dp->oidSum, &curveOID, &oidSz);
- if (ret < 0)
- return ret;
-
- /* temp buffer for plain DER key */
- tmpDer = (byte*)XMALLOC(ECC_BUFSIZE, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmpDer == NULL)
- return MEMORY_E;
-
- XMEMSET(tmpDer, 0, ECC_BUFSIZE);
-
- tmpDerSz = wc_BuildEccKeyDer(key, tmpDer, ECC_BUFSIZE, 0);
- if (tmpDerSz < 0) {
- XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return tmpDerSz;
- }
-
- /* get pkcs8 expected output size */
- ret = wc_CreatePKCS8Key(NULL, &pkcs8Sz, tmpDer, tmpDerSz, algoID,
- curveOID, oidSz);
- if (ret != LENGTH_ONLY_E) {
- XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
-
- if (output == NULL) {
- XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- *outLen = pkcs8Sz;
- return LENGTH_ONLY_E;
-
- } else if (*outLen < pkcs8Sz) {
- XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- WOLFSSL_MSG("Input buffer too small for ECC PKCS#8 key");
- return BUFFER_E;
- }
-
- ret = wc_CreatePKCS8Key(output, &pkcs8Sz, tmpDer, tmpDerSz,
- algoID, curveOID, oidSz);
- if (ret < 0) {
- XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
- return ret;
- }
-
- XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
-
- *outLen = ret;
- return ret;
-}
-#endif /* HAVE_PKCS8 */
-#endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */
-#endif /* HAVE_ECC */
-
-
-#ifdef HAVE_ED25519
-
-int wc_Ed25519PrivateKeyDecode(const byte* input, word32* inOutIdx,
- ed25519_key* key, word32 inSz)
-{
- word32 oid;
- int ret, version, length, endKeyIdx, privSz, pubSz;
- const byte* priv;
- const byte* pub;
-
- if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0)
- return BAD_FUNC_ARG;
-
- if (GetSequence(input, inOutIdx, &length, inSz) >= 0) {
- endKeyIdx = *inOutIdx + length;
-
- if (GetMyVersion(input, inOutIdx, &version, inSz) < 0)
- return ASN_PARSE_E;
- if (version != 0) {
- WOLFSSL_MSG("Unrecognized version of ED25519 private key");
- return ASN_PARSE_E;
- }
-
- if (GetAlgoId(input, inOutIdx, &oid, oidKeyType, inSz) < 0)
- return ASN_PARSE_E;
- if (oid != ED25519k)
- return ASN_PARSE_E;
-
- if (GetOctetString(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (GetOctetString(input, inOutIdx, &privSz, inSz) < 0)
- return ASN_PARSE_E;
-
- priv = input + *inOutIdx;
- *inOutIdx += privSz;
- }
- else {
- if (GetOctetString(input, inOutIdx, &privSz, inSz) < 0)
- return ASN_PARSE_E;
-
- if (privSz != 32)
- return ASN_PARSE_E;
-
- priv = input + *inOutIdx;
- *inOutIdx += privSz;
- endKeyIdx = *inOutIdx;
- }
-
- if (endKeyIdx == (int)*inOutIdx) {
- ret = wc_ed25519_import_private_only(priv, privSz, key);
- }
- else {
- if (GetASNHeader(input, ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1,
- inOutIdx, &length, inSz) < 0) {
- return ASN_PARSE_E;
- }
- if (GetOctetString(input, inOutIdx, &pubSz, inSz) < 0)
- return ASN_PARSE_E;
- pub = input + *inOutIdx;
- *inOutIdx += pubSz;
-
- ret = wc_ed25519_import_private_key(priv, privSz, pub, pubSz, key);
- }
- if (ret == 0 && endKeyIdx != (int)*inOutIdx)
- return ASN_PARSE_E;
-
- return ret;
-}
-
-
-int wc_Ed25519PublicKeyDecode(const byte* input, word32* inOutIdx,
- ed25519_key* key, word32 inSz)
-{
- int length;
- int ret;
-
- if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0)
- return BAD_FUNC_ARG;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- ret = SkipObjectId(input, inOutIdx, inSz);
- if (ret != 0)
- return ret;
-
- /* key header */
- ret = CheckBitString(input, inOutIdx, NULL, inSz, 1, NULL);
- if (ret != 0)
- return ret;
-
- /* This is the raw point data compressed or uncompressed. */
- if (wc_ed25519_import_public(input + *inOutIdx, inSz - *inOutIdx, key) != 0)
- return ASN_ECC_KEY_E;
-
- return 0;
-}
-
-
-#ifdef WOLFSSL_KEY_GEN
-
-/* build DER formatted ED25519 key,
- * return length on success, negative on error */
-static int wc_BuildEd25519KeyDer(ed25519_key* key, byte* output, word32 inLen,
- int pubOut)
-{
- byte algoArray[MAX_ALGO_SZ];
- byte ver[MAX_VERSION_SZ];
- byte seq[MAX_SEQ_SZ];
- int ret;
- word32 idx = 0, seqSz, verSz, algoSz, privSz, pubSz = 0;
-
- if (key == NULL || output == NULL || inLen == 0)
- return BAD_FUNC_ARG;
-
- if (pubOut)
- pubSz = 2 + 2 + ED25519_PUB_KEY_SIZE;
- privSz = 2 + 2 + ED25519_KEY_SIZE;
- algoSz = SetAlgoID(ED25519k, algoArray, oidKeyType, 0);
- verSz = SetMyVersion(0, ver, FALSE);
- seqSz = SetSequence(verSz + algoSz + privSz + pubSz, seq);
-
- if (seqSz + verSz + algoSz + privSz + pubSz > inLen)
- return BAD_FUNC_ARG;
-
- /* write out */
- /* seq */
- XMEMCPY(output + idx, seq, seqSz);
- idx = seqSz;
- /* ver */
- XMEMCPY(output + idx, ver, verSz);
- idx += verSz;
- /* algo */
- XMEMCPY(output + idx, algoArray, algoSz);
- idx += algoSz;
- /* privKey */
- idx += SetOctetString(2 + ED25519_KEY_SIZE, output + idx);
- idx += SetOctetString(ED25519_KEY_SIZE, output + idx);
- ret = wc_ed25519_export_private_only(key, output + idx, &privSz);
- if (ret != 0)
- return ret;
- idx += privSz;
- /* pubKey */
- if (pubOut) {
- idx += SetExplicit(1, 2 + ED25519_PUB_KEY_SIZE, output + idx);
- idx += SetOctetString(ED25519_KEY_SIZE, output + idx);
- ret = wc_ed25519_export_public(key, output + idx, &pubSz);
- if (ret != 0)
- return ret;
- idx += pubSz;
- }
-
- return idx;
-}
-
-/* Write a Private ecc key, including public to DER format,
- * length on success else < 0 */
-int wc_Ed25519KeyToDer(ed25519_key* key, byte* output, word32 inLen)
-{
- return wc_BuildEd25519KeyDer(key, output, inLen, 1);
-}
-
-
-
-/* Write only private ecc key to DER format,
- * length on success else < 0 */
-int wc_Ed25519PrivateKeyToDer(ed25519_key* key, byte* output, word32 inLen)
-{
- return wc_BuildEd25519KeyDer(key, output, inLen, 0);
-}
-
-#endif /* WOLFSSL_KEY_GEN */
-
-#endif /* HAVE_ED25519 */
-
-#ifdef HAVE_ED448
-
-int wc_Ed448PrivateKeyDecode(const byte* input, word32* inOutIdx,
- ed448_key* key, word32 inSz)
-{
- word32 oid;
- int ret, version, length, endKeyIdx, privSz, pubSz;
- const byte* priv;
- const byte* pub;
-
- if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0)
- return BAD_FUNC_ARG;
-
- if (GetSequence(input, inOutIdx, &length, inSz) >= 0) {
- endKeyIdx = *inOutIdx + length;
-
- if (GetMyVersion(input, inOutIdx, &version, inSz) < 0)
- return ASN_PARSE_E;
- if (version != 0) {
- WOLFSSL_MSG("Unrecognized version of ED448 private key");
- return ASN_PARSE_E;
- }
-
- if (GetAlgoId(input, inOutIdx, &oid, oidKeyType, inSz) < 0)
- return ASN_PARSE_E;
- if (oid != ED448k)
- return ASN_PARSE_E;
-
- if (GetOctetString(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (GetOctetString(input, inOutIdx, &privSz, inSz) < 0)
- return ASN_PARSE_E;
-
- priv = input + *inOutIdx;
- *inOutIdx += privSz;
- }
- else {
- if (GetOctetString(input, inOutIdx, &privSz, inSz) < 0)
- return ASN_PARSE_E;
-
- if (privSz != 57)
- return ASN_PARSE_E;
-
- priv = input + *inOutIdx;
- *inOutIdx += privSz;
- endKeyIdx = *inOutIdx;
- }
-
- if (endKeyIdx == (int)*inOutIdx) {
- ret = wc_ed448_import_private_only(priv, privSz, key);
- }
- else {
- if (GetASNHeader(input, ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1,
- inOutIdx, &length, inSz) < 0) {
- return ASN_PARSE_E;
- }
- if (GetOctetString(input, inOutIdx, &pubSz, inSz) < 0)
- return ASN_PARSE_E;
- pub = input + *inOutIdx;
- *inOutIdx += pubSz;
-
- ret = wc_ed448_import_private_key(priv, privSz, pub, pubSz, key);
- }
- if (ret == 0 && endKeyIdx != (int)*inOutIdx)
- return ASN_PARSE_E;
-
- return ret;
-}
-
-
-int wc_Ed448PublicKeyDecode(const byte* input, word32* inOutIdx,
- ed448_key* key, word32 inSz)
-{
- int length;
- int ret;
-
- if (input == NULL || inOutIdx == NULL || key == NULL || inSz == 0)
- return BAD_FUNC_ARG;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- if (GetSequence(input, inOutIdx, &length, inSz) < 0)
- return ASN_PARSE_E;
-
- ret = SkipObjectId(input, inOutIdx, inSz);
- if (ret != 0)
- return ret;
-
- /* key header */
- ret = CheckBitString(input, inOutIdx, NULL, inSz, 1, NULL);
- if (ret != 0)
- return ret;
-
- /* This is the raw point data compressed or uncompressed. */
- if (wc_ed448_import_public(input + *inOutIdx, inSz - *inOutIdx, key) != 0)
- return ASN_ECC_KEY_E;
-
- return 0;
-}
-
-
-#ifdef WOLFSSL_KEY_GEN
-
-/* build DER formatted ED448 key,
- * return length on success, negative on error */
-static int wc_BuildEd448KeyDer(ed448_key* key, byte* output, word32 inLen,
- int pubOut)
-{
- byte algoArray[MAX_ALGO_SZ];
- byte ver[MAX_VERSION_SZ];
- byte seq[MAX_SEQ_SZ];
- int ret;
- word32 idx = 0, seqSz, verSz, algoSz, privSz, pubSz = 0;
-
- if (key == NULL || output == NULL || inLen == 0)
- return BAD_FUNC_ARG;
-
- if (pubOut) {
- pubSz = 2 + 2 + ED448_PUB_KEY_SIZE;
- }
- privSz = 2 + 2 + ED448_KEY_SIZE;
- algoSz = SetAlgoID(ED448k, algoArray, oidKeyType, 0);
- verSz = SetMyVersion(0, ver, FALSE);
- seqSz = SetSequence(verSz + algoSz + privSz + pubSz, seq);
-
- if (seqSz + verSz + algoSz + privSz + pubSz > inLen)
- return BAD_FUNC_ARG;
-
- /* write out */
- /* seq */
- XMEMCPY(output + idx, seq, seqSz);
- idx = seqSz;
- /* ver */
- XMEMCPY(output + idx, ver, verSz);
- idx += verSz;
- /* algo */
- XMEMCPY(output + idx, algoArray, algoSz);
- idx += algoSz;
- /* privKey */
- idx += SetOctetString(2 + ED448_KEY_SIZE, output + idx);
- idx += SetOctetString(ED448_KEY_SIZE, output + idx);
- ret = wc_ed448_export_private_only(key, output + idx, &privSz);
- if (ret != 0)
- return ret;
- idx += privSz;
- /* pubKey */
- if (pubOut) {
- idx += SetExplicit(1, 2 + ED448_PUB_KEY_SIZE, output + idx);
- idx += SetOctetString(ED448_KEY_SIZE, output + idx);
- ret = wc_ed448_export_public(key, output + idx, &pubSz);
- if (ret != 0)
- return ret;
- idx += pubSz;
- }
-
- return idx;
-}
-
-/* Write a Private ecc key, including public to DER format,
- * length on success else < 0 */
-int wc_Ed448KeyToDer(ed448_key* key, byte* output, word32 inLen)
-{
- return wc_BuildEd448KeyDer(key, output, inLen, 1);
-}
-
-
-
-/* Write only private ecc key to DER format,
- * length on success else < 0 */
-int wc_Ed448PrivateKeyToDer(ed448_key* key, byte* output, word32 inLen)
-{
- return wc_BuildEd448KeyDer(key, output, inLen, 0);
-}
-
-#endif /* WOLFSSL_KEY_GEN */
-
-#endif /* HAVE_ED448 */
-
-#if defined(HAVE_OCSP) || defined(HAVE_CRL)
-
-/* Get raw Date only, no processing, 0 on success */
-static int GetBasicDate(const byte* source, word32* idx, byte* date,
- byte* format, int maxIdx)
-{
- int ret, length;
- const byte *datePtr = NULL;
-
- WOLFSSL_ENTER("GetBasicDate");
-
- ret = GetDateInfo(source, idx, &datePtr, format, &length, maxIdx);
- if (ret < 0)
- return ret;
-
- XMEMCPY(date, datePtr, length);
-
- return 0;
-}
-
-#endif /* HAVE_OCSP || HAVE_CRL */
-
-
-#ifdef HAVE_OCSP
-
-static int GetEnumerated(const byte* input, word32* inOutIdx, int *value,
- int sz)
-{
- word32 idx = *inOutIdx;
- word32 len;
- byte tag;
-
- WOLFSSL_ENTER("GetEnumerated");
-
- *value = 0;
-
- if (GetASNTag(input, &idx, &tag, sz) < 0)
- return ASN_PARSE_E;
-
- if (tag != ASN_ENUMERATED)
- return ASN_PARSE_E;
-
- if ((int)idx >= sz)
- return BUFFER_E;
-
- len = input[idx++];
- if (len > 4 || (int)(len + idx) > sz)
- return ASN_PARSE_E;
-
- while (len--) {
- *value = *value << 8 | input[idx++];
- }
-
- *inOutIdx = idx;
-
- return *value;
-}
-
-
-static int DecodeSingleResponse(byte* source,
- word32* ioIndex, OcspResponse* resp, word32 size)
-{
- word32 idx = *ioIndex, prevIndex, oid, localIdx;
- int length, wrapperSz;
- CertStatus* cs = resp->status;
- int ret;
- byte tag;
-
- WOLFSSL_ENTER("DecodeSingleResponse");
-
- /* Outer wrapper of the SEQUENCE OF Single Responses. */
- if (GetSequence(source, &idx, &wrapperSz, size) < 0)
- return ASN_PARSE_E;
-
- prevIndex = idx;
-
- /* When making a request, we only request one status on one certificate
- * at a time. There should only be one SingleResponse */
-
- /* Wrapper around the Single Response */
- if (GetSequence(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
-
- /* Wrapper around the CertID */
- if (GetSequence(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
- /* Skip the hash algorithm */
- if (GetAlgoId(source, &idx, &oid, oidIgnoreType, size) < 0)
- return ASN_PARSE_E;
- /* Save reference to the hash of CN */
- ret = GetOctetString(source, &idx, &length, size);
- if (ret < 0)
- return ret;
- resp->issuerHash = source + idx;
- idx += length;
- /* Save reference to the hash of the issuer public key */
- ret = GetOctetString(source, &idx, &length, size);
- if (ret < 0)
- return ret;
- resp->issuerKeyHash = source + idx;
- idx += length;
-
- /* Get serial number */
- if (GetSerialNumber(source, &idx, cs->serial, &cs->serialSz, size) < 0)
- return ASN_PARSE_E;
-
- if ( idx >= size )
- return BUFFER_E;
-
- /* CertStatus */
- switch (source[idx++])
- {
- case (ASN_CONTEXT_SPECIFIC | CERT_GOOD):
- cs->status = CERT_GOOD;
- idx++;
- break;
- case (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | CERT_REVOKED):
- cs->status = CERT_REVOKED;
- if (GetLength(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
- idx += length;
- break;
- case (ASN_CONTEXT_SPECIFIC | CERT_UNKNOWN):
- cs->status = CERT_UNKNOWN;
- idx++;
- break;
- default:
- return ASN_PARSE_E;
- }
-
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
- cs->thisDateAsn = source + idx;
- localIdx = 0;
- if (GetDateInfo(cs->thisDateAsn, &localIdx, NULL,
- (byte*)&cs->thisDateParsed.type,
- &cs->thisDateParsed.length, size) < 0)
- return ASN_PARSE_E;
- XMEMCPY(cs->thisDateParsed.data,
- cs->thisDateAsn + localIdx - cs->thisDateParsed.length,
- cs->thisDateParsed.length);
-#endif
- if (GetBasicDate(source, &idx, cs->thisDate,
- &cs->thisDateFormat, size) < 0)
- return ASN_PARSE_E;
-
-#ifndef NO_ASN_TIME
-#ifndef WOLFSSL_NO_OCSP_DATE_CHECK
- if (!XVALIDATE_DATE(cs->thisDate, cs->thisDateFormat, BEFORE))
- return ASN_BEFORE_DATE_E;
-#endif
-#endif
-
- /* The following items are optional. Only check for them if there is more
- * unprocessed data in the singleResponse wrapper. */
-
- localIdx = idx;
- if (((int)(idx - prevIndex) < wrapperSz) &&
- GetASNTag(source, &localIdx, &tag, size) == 0 &&
- tag == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0))
- {
- idx++;
- if (GetLength(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
-#if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
- cs->nextDateAsn = source + idx;
- localIdx = 0;
- if (GetDateInfo(cs->nextDateAsn, &localIdx, NULL,
- (byte*)&cs->nextDateParsed.type,
- &cs->nextDateParsed.length, size) < 0)
- return ASN_PARSE_E;
- XMEMCPY(cs->nextDateParsed.data,
- cs->nextDateAsn + localIdx - cs->nextDateParsed.length,
- cs->nextDateParsed.length);
-#endif
- if (GetBasicDate(source, &idx, cs->nextDate,
- &cs->nextDateFormat, size) < 0)
- return ASN_PARSE_E;
-
-#ifndef NO_ASN_TIME
-#ifndef WOLFSSL_NO_OCSP_DATE_CHECK
- if (!XVALIDATE_DATE(cs->nextDate, cs->nextDateFormat, AFTER))
- return ASN_AFTER_DATE_E;
-#endif
-#endif
- }
-
- localIdx = idx;
- if (((int)(idx - prevIndex) < wrapperSz) &&
- GetASNTag(source, &localIdx, &tag, size) == 0 &&
- tag == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1))
- {
- idx++;
- if (GetLength(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
- idx += length;
- }
-
- *ioIndex = idx;
-
- return 0;
-}
-
-static int DecodeOcspRespExtensions(byte* source,
- word32* ioIndex, OcspResponse* resp, word32 sz)
-{
- word32 idx = *ioIndex;
- int length;
- int ext_bound; /* boundary index for the sequence of extensions */
- word32 oid;
- int ret;
- byte tag;
-
- WOLFSSL_ENTER("DecodeOcspRespExtensions");
-
- if ((idx + 1) > sz)
- return BUFFER_E;
-
- if (GetASNTag(source, &idx, &tag, sz) < 0)
- return ASN_PARSE_E;
-
- if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 1))
- return ASN_PARSE_E;
-
- if (GetLength(source, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- if (GetSequence(source, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- ext_bound = idx + length;
-
- while (idx < (word32)ext_bound) {
- word32 localIdx;
-
- if (GetSequence(source, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE");
- return ASN_PARSE_E;
- }
-
- oid = 0;
- if (GetObjectId(source, &idx, &oid, oidOcspType, sz) < 0) {
- WOLFSSL_MSG("\tfail: OBJECT ID");
- return ASN_PARSE_E;
- }
-
- /* check for critical flag */
- if ((idx + 1) > (word32)sz) {
- WOLFSSL_MSG("\tfail: malformed buffer");
- return BUFFER_E;
- }
-
- localIdx = idx;
- if (GetASNTag(source, &localIdx, &tag, sz) == 0 && tag == ASN_BOOLEAN) {
- WOLFSSL_MSG("\tfound optional critical flag, moving past");
- ret = GetBoolean(source, &idx, sz);
- if (ret < 0)
- return ret;
- }
-
- ret = GetOctetString(source, &idx, &length, sz);
- if (ret < 0)
- return ret;
-
- if (oid == OCSP_NONCE_OID) {
- /* get data inside extra OCTET_STRING */
- ret = GetOctetString(source, &idx, &length, sz);
- if (ret < 0)
- return ret;
-
- resp->nonce = source + idx;
- resp->nonceSz = length;
- }
-
- idx += length;
- }
-
- *ioIndex = idx;
- return 0;
-}
-
-
-static int DecodeResponseData(byte* source,
- word32* ioIndex, OcspResponse* resp, word32 size)
-{
- word32 idx = *ioIndex, prev_idx, localIdx;
- int length;
- int version;
- int ret;
- byte tag;
-
- WOLFSSL_ENTER("DecodeResponseData");
-
- resp->response = source + idx;
- prev_idx = idx;
- if (GetSequence(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
- resp->responseSz = length + idx - prev_idx;
-
- /* Get version. It is an EXPLICIT[0] DEFAULT(0) value. If this
- * item isn't an EXPLICIT[0], then set version to zero and move
- * onto the next item.
- */
- localIdx = idx;
- if (GetASNTag(source, &localIdx, &tag, size) == 0 &&
- tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED))
- {
- idx += 2; /* Eat the value and length */
- if (GetMyVersion(source, &idx, &version, size) < 0)
- return ASN_PARSE_E;
- } else
- version = 0;
-
- localIdx = idx;
- if (GetASNTag(source, &localIdx, &tag, size) == 0 &&
- ( tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 1) ||
- tag == (ASN_CONTEXT_SPECIFIC | ASN_CONSTRUCTED | 2) ))
- {
- idx++; /* advance past ASN tag */
- if (GetLength(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
- idx += length;
- }
- else
- return ASN_PARSE_E;
-
- /* save pointer to the producedAt time */
- if (GetBasicDate(source, &idx, resp->producedDate,
- &resp->producedDateFormat, size) < 0)
- return ASN_PARSE_E;
-
- if ((ret = DecodeSingleResponse(source, &idx, resp, size)) < 0)
- return ret; /* ASN_PARSE_E, ASN_BEFORE_DATE_E, ASN_AFTER_DATE_E */
-
- /*
- * Check the length of the ResponseData against the current index to
- * see if there are extensions, they are optional.
- */
- if (idx - prev_idx < resp->responseSz)
- if (DecodeOcspRespExtensions(source, &idx, resp, size) < 0)
- return ASN_PARSE_E;
-
- *ioIndex = idx;
- return 0;
-}
-
-
-#ifndef WOLFSSL_NO_OCSP_OPTIONAL_CERTS
-
-static int DecodeCerts(byte* source,
- word32* ioIndex, OcspResponse* resp, word32 size)
-{
- word32 idx = *ioIndex;
- byte tag;
-
- WOLFSSL_ENTER("DecodeCerts");
-
- if (GetASNTag(source, &idx, &tag, size) < 0)
- return ASN_PARSE_E;
-
- if (tag == (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC))
- {
- int length;
-
- if (GetLength(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
-
- if (GetSequence(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
-
- resp->cert = source + idx;
- resp->certSz = length;
-
- idx += length;
- }
- *ioIndex = idx;
- return 0;
-}
-
-#endif /* WOLFSSL_NO_OCSP_OPTIONAL_CERTS */
-
-
-static int DecodeBasicOcspResponse(byte* source, word32* ioIndex,
- OcspResponse* resp, word32 size, void* cm, void* heap, int noVerify)
-{
- int length;
- word32 idx = *ioIndex;
- word32 end_index;
- int ret;
- int sigLength;
-
- WOLFSSL_ENTER("DecodeBasicOcspResponse");
- (void)heap;
-
- if (GetSequence(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
-
- if (idx + length > size)
- return ASN_INPUT_E;
- end_index = idx + length;
-
- if ((ret = DecodeResponseData(source, &idx, resp, size)) < 0)
- return ret; /* ASN_PARSE_E, ASN_BEFORE_DATE_E, ASN_AFTER_DATE_E */
-
- /* Get the signature algorithm */
- if (GetAlgoId(source, &idx, &resp->sigOID, oidSigType, size) < 0)
- return ASN_PARSE_E;
-
- ret = CheckBitString(source, &idx, &sigLength, size, 1, NULL);
- if (ret != 0)
- return ret;
-
- resp->sigSz = sigLength;
- resp->sig = source + idx;
- idx += sigLength;
-
- /*
- * Check the length of the BasicOcspResponse against the current index to
- * see if there are certificates, they are optional.
- */
-#ifndef WOLFSSL_NO_OCSP_OPTIONAL_CERTS
- if (idx < end_index)
- {
- DecodedCert cert;
-
- if (DecodeCerts(source, &idx, resp, size) < 0)
- return ASN_PARSE_E;
-
- InitDecodedCert(&cert, resp->cert, resp->certSz, heap);
-
- /* Don't verify if we don't have access to Cert Manager. */
- ret = ParseCertRelative(&cert, CERT_TYPE,
- noVerify ? NO_VERIFY : VERIFY_OCSP, cm);
- if (ret < 0) {
- WOLFSSL_MSG("\tOCSP Responder certificate parsing failed");
- FreeDecodedCert(&cert);
- return ret;
- }
-
-#ifndef WOLFSSL_NO_OCSP_ISSUER_CHECK
- if ((cert.extExtKeyUsage & EXTKEYUSE_OCSP_SIGN) == 0) {
- if (XMEMCMP(cert.subjectHash,
- resp->issuerHash, KEYID_SIZE) == 0) {
- WOLFSSL_MSG("\tOCSP Response signed by issuer");
- }
- else {
- WOLFSSL_MSG("\tOCSP Responder key usage check failed");
- #ifdef OPENSSL_EXTRA
- resp->verifyError = OCSP_BAD_ISSUER;
- #else
- FreeDecodedCert(&cert);
- return BAD_OCSP_RESPONDER;
- #endif
- }
- }
-#endif
-
- /* ConfirmSignature is blocking here */
- ret = ConfirmSignature(&cert.sigCtx,
- resp->response, resp->responseSz,
- cert.publicKey, cert.pubKeySize, cert.keyOID,
- resp->sig, resp->sigSz, resp->sigOID, NULL);
-
- FreeDecodedCert(&cert);
-
- if (ret != 0) {
- WOLFSSL_MSG("\tOCSP Confirm signature failed");
- return ASN_OCSP_CONFIRM_E;
- }
- }
- else
-#endif /* WOLFSSL_NO_OCSP_OPTIONAL_CERTS */
- {
- Signer* ca;
- int sigValid = -1;
-
- #ifndef NO_SKID
- ca = GetCA(cm, resp->issuerKeyHash);
- #else
- ca = GetCA(cm, resp->issuerHash);
- #endif
-
- if (ca) {
- SignatureCtx sigCtx;
- InitSignatureCtx(&sigCtx, heap, INVALID_DEVID);
-
- /* ConfirmSignature is blocking here */
- sigValid = ConfirmSignature(&sigCtx, resp->response,
- resp->responseSz, ca->publicKey, ca->pubKeySize, ca->keyOID,
- resp->sig, resp->sigSz, resp->sigOID, NULL);
- }
- if (ca == NULL || sigValid != 0) {
- WOLFSSL_MSG("\tOCSP Confirm signature failed");
- return ASN_OCSP_CONFIRM_E;
- }
-
- (void)noVerify;
- }
-
- *ioIndex = idx;
- return 0;
-}
-
-
-void InitOcspResponse(OcspResponse* resp, CertStatus* status,
- byte* source, word32 inSz)
-{
- WOLFSSL_ENTER("InitOcspResponse");
-
- XMEMSET(status, 0, sizeof(CertStatus));
- XMEMSET(resp, 0, sizeof(OcspResponse));
-
- resp->responseStatus = -1;
- resp->status = status;
- resp->source = source;
- resp->maxIdx = inSz;
-}
-
-
-int OcspResponseDecode(OcspResponse* resp, void* cm, void* heap, int noVerify)
-{
- int ret;
- int length = 0;
- word32 idx = 0;
- byte* source = resp->source;
- word32 size = resp->maxIdx;
- word32 oid;
- byte tag;
-
- WOLFSSL_ENTER("OcspResponseDecode");
-
- /* peel the outer SEQUENCE wrapper */
- if (GetSequence(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
-
- /* First get the responseStatus, an ENUMERATED */
- if (GetEnumerated(source, &idx, &resp->responseStatus, size) < 0)
- return ASN_PARSE_E;
-
- if (resp->responseStatus != OCSP_SUCCESSFUL)
- return 0;
-
- /* Next is an EXPLICIT record called ResponseBytes, OPTIONAL */
- if (idx >= size)
- return ASN_INPUT_E;
- if (GetASNTag(source, &idx, &tag, size) < 0)
- return ASN_PARSE_E;
- if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC))
- return ASN_PARSE_E;
- if (GetLength(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
-
- /* Get the responseBytes SEQUENCE */
- if (GetSequence(source, &idx, &length, size) < 0)
- return ASN_PARSE_E;
-
- /* Check ObjectID for the resposeBytes */
- if (GetObjectId(source, &idx, &oid, oidOcspType, size) < 0)
- return ASN_PARSE_E;
- if (oid != OCSP_BASIC_OID)
- return ASN_PARSE_E;
- ret = GetOctetString(source, &idx, &length, size);
- if (ret < 0)
- return ret;
-
- ret = DecodeBasicOcspResponse(source, &idx, resp, size, cm, heap, noVerify);
- if (ret < 0)
- return ret;
-
- return 0;
-}
-
-
-word32 EncodeOcspRequestExtensions(OcspRequest* req, byte* output, word32 size)
-{
- const byte NonceObjId[] = { 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
- 0x30, 0x01, 0x02 };
- byte seqArray[5][MAX_SEQ_SZ];
- word32 seqSz[5], totalSz = (word32)sizeof(NonceObjId);
-
- WOLFSSL_ENTER("SetOcspReqExtensions");
-
- if (!req || !output || !req->nonceSz)
- return 0;
-
- totalSz += req->nonceSz;
- totalSz += seqSz[0] = SetOctetString(req->nonceSz, seqArray[0]);
- totalSz += seqSz[1] = SetOctetString(req->nonceSz + seqSz[0], seqArray[1]);
- totalSz += seqSz[2] = SetObjectId(sizeof(NonceObjId), seqArray[2]);
- totalSz += seqSz[3] = SetSequence(totalSz, seqArray[3]);
- totalSz += seqSz[4] = SetSequence(totalSz, seqArray[4]);
-
- if (totalSz > size)
- return 0;
-
- totalSz = 0;
-
- XMEMCPY(output + totalSz, seqArray[4], seqSz[4]);
- totalSz += seqSz[4];
-
- XMEMCPY(output + totalSz, seqArray[3], seqSz[3]);
- totalSz += seqSz[3];
-
- XMEMCPY(output + totalSz, seqArray[2], seqSz[2]);
- totalSz += seqSz[2];
-
- XMEMCPY(output + totalSz, NonceObjId, sizeof(NonceObjId));
- totalSz += (word32)sizeof(NonceObjId);
-
- XMEMCPY(output + totalSz, seqArray[1], seqSz[1]);
- totalSz += seqSz[1];
-
- XMEMCPY(output + totalSz, seqArray[0], seqSz[0]);
- totalSz += seqSz[0];
-
- XMEMCPY(output + totalSz, req->nonce, req->nonceSz);
- totalSz += req->nonceSz;
-
- return totalSz;
-}
-
-
-int EncodeOcspRequest(OcspRequest* req, byte* output, word32 size)
-{
- byte seqArray[5][MAX_SEQ_SZ];
- /* The ASN.1 of the OCSP Request is an onion of sequences */
- byte algoArray[MAX_ALGO_SZ];
- byte issuerArray[MAX_ENCODED_DIG_SZ];
- byte issuerKeyArray[MAX_ENCODED_DIG_SZ];
- byte snArray[MAX_SN_SZ];
- byte extArray[MAX_OCSP_EXT_SZ];
- word32 seqSz[5], algoSz, issuerSz, issuerKeySz, extSz, totalSz;
- int i, snSz;
-
- WOLFSSL_ENTER("EncodeOcspRequest");
-
-#ifdef NO_SHA
- algoSz = SetAlgoID(SHA256h, algoArray, oidHashType, 0);
-#else
- algoSz = SetAlgoID(SHAh, algoArray, oidHashType, 0);
-#endif
-
- issuerSz = SetDigest(req->issuerHash, KEYID_SIZE, issuerArray);
- issuerKeySz = SetDigest(req->issuerKeyHash, KEYID_SIZE, issuerKeyArray);
- snSz = SetSerialNumber(req->serial, req->serialSz, snArray,
- MAX_SN_SZ, MAX_SN_SZ);
- extSz = 0;
-
- if (snSz < 0)
- return snSz;
-
- if (req->nonceSz) {
- /* TLS Extensions use this function too - put extensions after
- * ASN.1: Context Specific [2].
- */
- extSz = EncodeOcspRequestExtensions(req, extArray + 2,
- OCSP_NONCE_EXT_SZ);
- extSz += SetExplicit(2, extSz, extArray);
- }
-
- totalSz = algoSz + issuerSz + issuerKeySz + snSz;
- for (i = 4; i >= 0; i--) {
- seqSz[i] = SetSequence(totalSz, seqArray[i]);
- totalSz += seqSz[i];
- if (i == 2) totalSz += extSz;
- }
-
- if (output == NULL)
- return totalSz;
- if (totalSz > size)
- return BUFFER_E;
-
- totalSz = 0;
- for (i = 0; i < 5; i++) {
- XMEMCPY(output + totalSz, seqArray[i], seqSz[i]);
- totalSz += seqSz[i];
- }
-
- XMEMCPY(output + totalSz, algoArray, algoSz);
- totalSz += algoSz;
-
- XMEMCPY(output + totalSz, issuerArray, issuerSz);
- totalSz += issuerSz;
-
- XMEMCPY(output + totalSz, issuerKeyArray, issuerKeySz);
- totalSz += issuerKeySz;
-
- XMEMCPY(output + totalSz, snArray, snSz);
- totalSz += snSz;
-
- if (extSz != 0) {
- XMEMCPY(output + totalSz, extArray, extSz);
- totalSz += extSz;
- }
-
- return totalSz;
-}
-
-
-int InitOcspRequest(OcspRequest* req, DecodedCert* cert, byte useNonce,
- void* heap)
-{
- int ret;
-
- WOLFSSL_ENTER("InitOcspRequest");
-
- if (req == NULL)
- return BAD_FUNC_ARG;
-
- ForceZero(req, sizeof(OcspRequest));
- req->heap = heap;
-
- if (cert) {
- XMEMCPY(req->issuerHash, cert->issuerHash, KEYID_SIZE);
- XMEMCPY(req->issuerKeyHash, cert->issuerKeyHash, KEYID_SIZE);
-
- req->serial = (byte*)XMALLOC(cert->serialSz, req->heap,
- DYNAMIC_TYPE_OCSP_REQUEST);
- if (req->serial == NULL)
- return MEMORY_E;
-
- XMEMCPY(req->serial, cert->serial, cert->serialSz);
- req->serialSz = cert->serialSz;
-
- if (cert->extAuthInfoSz != 0 && cert->extAuthInfo != NULL) {
- req->url = (byte*)XMALLOC(cert->extAuthInfoSz + 1, req->heap,
- DYNAMIC_TYPE_OCSP_REQUEST);
- if (req->url == NULL) {
- XFREE(req->serial, req->heap, DYNAMIC_TYPE_OCSP);
- return MEMORY_E;
- }
-
- XMEMCPY(req->url, cert->extAuthInfo, cert->extAuthInfoSz);
- req->urlSz = cert->extAuthInfoSz;
- req->url[req->urlSz] = 0;
- }
- }
-
- if (useNonce) {
- WC_RNG rng;
-
- #ifndef HAVE_FIPS
- ret = wc_InitRng_ex(&rng, req->heap, INVALID_DEVID);
- #else
- ret = wc_InitRng(&rng);
- #endif
- if (ret != 0) {
- WOLFSSL_MSG("\tCannot initialize RNG. Skipping the OSCP Nonce.");
- } else {
- if (wc_RNG_GenerateBlock(&rng, req->nonce, MAX_OCSP_NONCE_SZ) != 0)
- WOLFSSL_MSG("\tCannot run RNG. Skipping the OSCP Nonce.");
- else
- req->nonceSz = MAX_OCSP_NONCE_SZ;
-
- wc_FreeRng(&rng);
- }
- }
-
- return 0;
-}
-
-void FreeOcspRequest(OcspRequest* req)
-{
- WOLFSSL_ENTER("FreeOcspRequest");
-
- if (req) {
- if (req->serial)
- XFREE(req->serial, req->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- req->serial = NULL;
-
-#ifdef OPENSSL_EXTRA
- if (req->serialInt) {
- if (req->serialInt->isDynamic) {
- XFREE(req->serialInt->data, NULL, DYNAMIC_TYPE_OPENSSL);
- }
- XFREE(req->serialInt, NULL, DYNAMIC_TYPE_OPENSSL);
- }
- req->serialInt = NULL;
-#endif
-
- if (req->url)
- XFREE(req->url, req->heap, DYNAMIC_TYPE_OCSP_REQUEST);
- req->url = NULL;
- }
-}
-
-
-int CompareOcspReqResp(OcspRequest* req, OcspResponse* resp)
-{
- int cmp;
-
- WOLFSSL_ENTER("CompareOcspReqResp");
-
- if (req == NULL)
- {
- WOLFSSL_MSG("\tReq missing");
- return -1;
- }
-
- if (resp == NULL)
- {
- WOLFSSL_MSG("\tResp missing");
- return 1;
- }
-
- /* Nonces are not critical. The responder may not necessarily add
- * the nonce to the response. */
- if (req->nonceSz
-#ifndef WOLFSSL_FORCE_OCSP_NONCE_CHECK
- && resp->nonceSz != 0
-#endif
- ) {
- cmp = req->nonceSz - resp->nonceSz;
- if (cmp != 0)
- {
- WOLFSSL_MSG("\tnonceSz mismatch");
- return cmp;
- }
-
- cmp = XMEMCMP(req->nonce, resp->nonce, req->nonceSz);
- if (cmp != 0)
- {
- WOLFSSL_MSG("\tnonce mismatch");
- return cmp;
- }
- }
-
- cmp = XMEMCMP(req->issuerHash, resp->issuerHash, KEYID_SIZE);
- if (cmp != 0)
- {
- WOLFSSL_MSG("\tissuerHash mismatch");
- return cmp;
- }
-
- cmp = XMEMCMP(req->issuerKeyHash, resp->issuerKeyHash, KEYID_SIZE);
- if (cmp != 0)
- {
- WOLFSSL_MSG("\tissuerKeyHash mismatch");
- return cmp;
- }
-
- cmp = req->serialSz - resp->status->serialSz;
- if (cmp != 0)
- {
- WOLFSSL_MSG("\tserialSz mismatch");
- return cmp;
- }
-
- cmp = XMEMCMP(req->serial, resp->status->serial, req->serialSz);
- if (cmp != 0)
- {
- WOLFSSL_MSG("\tserial mismatch");
- return cmp;
- }
-
- return 0;
-}
-
-#endif /* HAVE_OCSP */
-
-
-/* store WC_SHA hash of NAME */
-int GetNameHash(const byte* source, word32* idx, byte* hash,
- int maxIdx)
-{
- int length; /* length of all distinguished names */
- int ret;
- word32 dummy;
- byte tag;
-
- WOLFSSL_ENTER("GetNameHash");
-
- dummy = *idx;
- if (GetASNTag(source, &dummy, &tag, maxIdx) == 0 && tag == ASN_OBJECT_ID) {
- WOLFSSL_MSG("Trying optional prefix...");
-
- if (GetLength(source, idx, &length, maxIdx) < 0)
- return ASN_PARSE_E;
-
- *idx += length;
- WOLFSSL_MSG("Got optional prefix");
- }
-
- /* For OCSP, RFC2560 section 4.1.1 states the issuer hash should be
- * calculated over the entire DER encoding of the Name field, including
- * the tag and length. */
- dummy = *idx;
- if (GetSequence(source, idx, &length, maxIdx) < 0)
- return ASN_PARSE_E;
-
- ret = CalcHashId(source + dummy, length + *idx - dummy, hash);
-
- *idx += length;
-
- return ret;
-}
-
-
-#ifdef HAVE_CRL
-
-/* initialize decoded CRL */
-void InitDecodedCRL(DecodedCRL* dcrl, void* heap)
-{
- WOLFSSL_MSG("InitDecodedCRL");
-
- XMEMSET(dcrl, 0, sizeof(DecodedCRL));
- dcrl->heap = heap;
- #ifdef WOLFSSL_HEAP_TEST
- dcrl->heap = (void*)WOLFSSL_HEAP_TEST;
- #endif
-}
-
-
-/* free decoded CRL resources */
-void FreeDecodedCRL(DecodedCRL* dcrl)
-{
- RevokedCert* tmp = dcrl->certs;
-
- WOLFSSL_MSG("FreeDecodedCRL");
-
- while(tmp) {
- RevokedCert* next = tmp->next;
- XFREE(tmp, dcrl->heap, DYNAMIC_TYPE_REVOKED);
- tmp = next;
- }
-}
-
-
-/* Get Revoked Cert list, 0 on success */
-static int GetRevoked(const byte* buff, word32* idx, DecodedCRL* dcrl,
- int maxIdx)
-{
- int ret, len;
- word32 end;
- byte b;
- RevokedCert* rc;
-
- WOLFSSL_ENTER("GetRevoked");
-
- if (GetSequence(buff, idx, &len, maxIdx) < 0)
- return ASN_PARSE_E;
-
- end = *idx + len;
-
- rc = (RevokedCert*)XMALLOC(sizeof(RevokedCert), dcrl->heap,
- DYNAMIC_TYPE_REVOKED);
- if (rc == NULL) {
- WOLFSSL_MSG("Alloc Revoked Cert failed");
- return MEMORY_E;
- }
-
- if (GetSerialNumber(buff, idx, rc->serialNumber, &rc->serialSz,
- maxIdx) < 0) {
- XFREE(rc, dcrl->heap, DYNAMIC_TYPE_REVOKED);
- return ASN_PARSE_E;
- }
-
- /* add to list */
- rc->next = dcrl->certs;
- dcrl->certs = rc;
- dcrl->totalCerts++;
-
- /* get date */
- ret = GetDateInfo(buff, idx, NULL, &b, NULL, maxIdx);
- if (ret < 0) {
- WOLFSSL_MSG("Expecting Date");
- return ret;
- }
-
- /* skip extensions */
- *idx = end;
-
- return 0;
-}
-
-
-/* Get CRL Signature, 0 on success */
-static int GetCRL_Signature(const byte* source, word32* idx, DecodedCRL* dcrl,
- int maxIdx)
-{
- int length;
- int ret;
-
- WOLFSSL_ENTER("GetCRL_Signature");
-
- ret = CheckBitString(source, idx, &length, maxIdx, 1, NULL);
- if (ret != 0)
- return ret;
- dcrl->sigLength = length;
-
- dcrl->signature = (byte*)&source[*idx];
- *idx += dcrl->sigLength;
-
- return 0;
-}
-
-int VerifyCRL_Signature(SignatureCtx* sigCtx, const byte* toBeSigned,
- word32 tbsSz, const byte* signature, word32 sigSz,
- word32 signatureOID, Signer *ca, void* heap)
-{
- /* try to confirm/verify signature */
-#ifndef IGNORE_KEY_EXTENSIONS
- if ((ca->keyUsage & KEYUSE_CRL_SIGN) == 0) {
- WOLFSSL_MSG("CA cannot sign CRLs");
- return ASN_CRL_NO_SIGNER_E;
- }
-#endif /* IGNORE_KEY_EXTENSIONS */
-
- InitSignatureCtx(sigCtx, heap, INVALID_DEVID);
- if (ConfirmSignature(sigCtx, toBeSigned, tbsSz, ca->publicKey,
- ca->pubKeySize, ca->keyOID, signature, sigSz,
- signatureOID, NULL) != 0) {
- WOLFSSL_MSG("CRL Confirm signature failed");
- return ASN_CRL_CONFIRM_E;
- }
-
- return 0;
-}
-
-
-static int ParseCRL_CertList(DecodedCRL* dcrl, const byte* buf,
- word32* inOutIdx, int sz)
-{
- word32 oid, dateIdx, idx, checkIdx;
- int version, doNextDate = 1;
- byte tag;
-
- if (dcrl == NULL || inOutIdx == NULL || buf == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* may have version */
- idx = *inOutIdx;
-
- checkIdx = idx;
- if (GetASNTag(buf, &checkIdx, &tag, sz) == 0 && tag == ASN_INTEGER) {
- if (GetMyVersion(buf, &idx, &version, sz) < 0)
- return ASN_PARSE_E;
- }
-
- if (GetAlgoId(buf, &idx, &oid, oidIgnoreType, sz) < 0)
- return ASN_PARSE_E;
-
- if (GetNameHash(buf, &idx, dcrl->issuerHash, sz) < 0)
- return ASN_PARSE_E;
-
- if (GetBasicDate(buf, &idx, dcrl->lastDate, &dcrl->lastDateFormat, sz) < 0)
- return ASN_PARSE_E;
-
- dateIdx = idx;
-
- if (GetBasicDate(buf, &idx, dcrl->nextDate, &dcrl->nextDateFormat, sz) < 0)
- {
-#ifndef WOLFSSL_NO_CRL_NEXT_DATE
- (void)dateIdx;
- return ASN_PARSE_E;
-#else
- dcrl->nextDateFormat = ASN_OTHER_TYPE; /* skip flag */
- doNextDate = 0;
- idx = dateIdx;
-#endif
- }
-
- if (doNextDate) {
-#ifndef NO_ASN_TIME
- if (!XVALIDATE_DATE(dcrl->nextDate, dcrl->nextDateFormat, AFTER)) {
- WOLFSSL_MSG("CRL after date is no longer valid");
- return ASN_AFTER_DATE_E;
- }
-#endif
- }
-
- checkIdx = idx;
- if (idx != dcrl->sigIndex &&
- GetASNTag(buf, &checkIdx, &tag, sz) == 0 && tag != CRL_EXTENSIONS) {
-
- int len;
-
- if (GetSequence(buf, &idx, &len, sz) < 0)
- return ASN_PARSE_E;
- len += idx;
-
- while (idx < (word32)len) {
- if (GetRevoked(buf, &idx, dcrl, len) < 0)
- return ASN_PARSE_E;
- }
- }
-
- *inOutIdx = idx;
-
- return 0;
-}
-
-
-#ifndef NO_SKID
-static int ParseCRL_AuthKeyIdExt(const byte* input, int sz, DecodedCRL* dcrl)
-{
- word32 idx = 0;
- int length = 0, ret = 0;
- byte tag;
-
- WOLFSSL_ENTER("ParseCRL_AuthKeyIdExt");
-
- if (GetSequence(input, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE\n");
- return ASN_PARSE_E;
- }
-
- if (GetASNTag(input, &idx, &tag, sz) < 0) {
- return ASN_PARSE_E;
- }
-
- if (tag != (ASN_CONTEXT_SPECIFIC | 0)) {
- WOLFSSL_MSG("\tinfo: OPTIONAL item 0, not available\n");
- return 0;
- }
-
- if (GetLength(input, &idx, &length, sz) <= 0) {
- WOLFSSL_MSG("\tfail: extension data length");
- return ASN_PARSE_E;
- }
-
- dcrl->extAuthKeyIdSet = 1;
- if (length == KEYID_SIZE) {
- XMEMCPY(dcrl->extAuthKeyId, input + idx, length);
- }
- else {
- ret = CalcHashId(input + idx, length, dcrl->extAuthKeyId);
- }
-
- return ret;
-}
-#endif
-
-
-static int ParseCRL_Extensions(DecodedCRL* dcrl, const byte* buf,
- word32* inOutIdx, word32 sz)
-{
- int length;
- word32 idx;
- word32 ext_bound; /* boundary index for the sequence of extensions */
- word32 oid;
- byte tag;
-
- WOLFSSL_ENTER("ParseCRL_Extensions");
- (void)dcrl;
-
- if (inOutIdx == NULL)
- return BAD_FUNC_ARG;
-
- idx = *inOutIdx;
-
- /* CRL Extensions are optional */
- if ((idx + 1) > sz)
- return 0;
-
- /* CRL Extensions are optional */
- if (GetASNTag(buf, &idx, &tag, sz) < 0)
- return 0;
-
- /* CRL Extensions are optional */
- if (tag != (ASN_CONSTRUCTED | ASN_CONTEXT_SPECIFIC | 0))
- return 0;
-
- if (GetLength(buf, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- if (GetSequence(buf, &idx, &length, sz) < 0)
- return ASN_PARSE_E;
-
- ext_bound = idx + length;
-
- while (idx < (word32)ext_bound) {
- word32 localIdx;
- int ret;
-
- if (GetSequence(buf, &idx, &length, sz) < 0) {
- WOLFSSL_MSG("\tfail: should be a SEQUENCE");
- return ASN_PARSE_E;
- }
-
- oid = 0;
- if (GetObjectId(buf, &idx, &oid, oidCrlExtType, sz) < 0) {
- WOLFSSL_MSG("\tfail: OBJECT ID");
- return ASN_PARSE_E;
- }
-
- /* check for critical flag */
- if ((idx + 1) > (word32)sz) {
- WOLFSSL_MSG("\tfail: malformed buffer");
- return BUFFER_E;
- }
-
- localIdx = idx;
- if (GetASNTag(buf, &localIdx, &tag, sz) == 0 && tag == ASN_BOOLEAN) {
- WOLFSSL_MSG("\tfound optional critical flag, moving past");
- ret = GetBoolean(buf, &idx, sz);
- if (ret < 0)
- return ret;
- }
-
- ret = GetOctetString(buf, &idx, &length, sz);
- if (ret < 0)
- return ret;
-
- if (oid == AUTH_KEY_OID) {
- #ifndef NO_SKID
- ret = ParseCRL_AuthKeyIdExt(buf + idx, length, dcrl);
- if (ret < 0) {
- WOLFSSL_MSG("\tcouldn't parse AuthKeyId extension");
- return ret;
- }
- #endif
- }
-
- idx += length;
- }
-
- *inOutIdx = idx;
-
- return 0;
-}
-
-
-/* prase crl buffer into decoded state, 0 on success */
-int ParseCRL(DecodedCRL* dcrl, const byte* buff, word32 sz, void* cm)
-{
- int len;
- word32 idx = 0;
- Signer* ca = NULL;
- SignatureCtx sigCtx;
-
- WOLFSSL_MSG("ParseCRL");
-
- /* raw crl hash */
- /* hash here if needed for optimized comparisons
- * wc_Sha sha;
- * wc_InitSha(&sha);
- * wc_ShaUpdate(&sha, buff, sz);
- * wc_ShaFinal(&sha, dcrl->crlHash); */
-
- if (GetSequence(buff, &idx, &len, sz) < 0)
- return ASN_PARSE_E;
-
- dcrl->certBegin = idx;
- /* Normalize sz for the length inside the outer sequence. */
- sz = len + idx;
-
- if (GetSequence(buff, &idx, &len, sz) < 0)
- return ASN_PARSE_E;
- dcrl->sigIndex = len + idx;
-
- if (ParseCRL_CertList(dcrl, buff, &idx, idx + len) < 0)
- return ASN_PARSE_E;
-
- if (ParseCRL_Extensions(dcrl, buff, &idx, idx + len) < 0)
- return ASN_PARSE_E;
-
- idx = dcrl->sigIndex;
-
- if (GetAlgoId(buff, &idx, &dcrl->signatureOID, oidSigType, sz) < 0)
- return ASN_PARSE_E;
-
- if (GetCRL_Signature(buff, &idx, dcrl, sz) < 0)
- return ASN_PARSE_E;
-
- /* openssl doesn't add skid by default for CRLs cause firefox chokes
- if experiencing issues uncomment NO_SKID define in CRL section of
- wolfssl/wolfcrypt/settings.h */
-#ifndef NO_SKID
- if (dcrl->extAuthKeyIdSet) {
- ca = GetCA(cm, dcrl->extAuthKeyId); /* more unique than issuerHash */
- }
- if (ca != NULL && XMEMCMP(dcrl->issuerHash, ca->subjectNameHash,
- KEYID_SIZE) != 0) {
- ca = NULL;
- }
- if (ca == NULL) {
- ca = GetCAByName(cm, dcrl->issuerHash); /* last resort */
- /* If AKID is available then this CA doesn't have the public
- * key required */
- if (ca && dcrl->extAuthKeyIdSet) {
- WOLFSSL_MSG("CA SKID doesn't match AKID");
- ca = NULL;
- }
- }
-#else
- ca = GetCA(cm, dcrl->issuerHash);
-#endif /* !NO_SKID */
- WOLFSSL_MSG("About to verify CRL signature");
-
- if (ca == NULL) {
- WOLFSSL_MSG("Did NOT find CRL issuer CA");
- return ASN_CRL_NO_SIGNER_E;
- }
-
- WOLFSSL_MSG("Found CRL issuer CA");
- return VerifyCRL_Signature(&sigCtx, buff + dcrl->certBegin,
- dcrl->sigIndex - dcrl->certBegin, dcrl->signature, dcrl->sigLength,
- dcrl->signatureOID, ca, dcrl->heap);
-}
-
-#endif /* HAVE_CRL */
-
-
-
-#ifdef WOLFSSL_CERT_PIV
-
-int wc_ParseCertPIV(wc_CertPIV* piv, const byte* buf, word32 totalSz)
-{
- int length = 0;
- word32 idx = 0;
-
- WOLFSSL_ENTER("wc_ParseCertPIV");
-
- if (piv == NULL || buf == NULL || totalSz == 0)
- return BAD_FUNC_ARG;
-
- XMEMSET(piv, 0, sizeof(wc_CertPIV));
-
- /* Detect Identiv PIV (with 0x0A, 0x0B and 0x0C sections) */
- /* Certificate (0A 82 05FA) */
- if (GetASNHeader(buf, ASN_PIV_CERT, &idx, &length, totalSz) >= 0) {
- /* Identiv Type PIV card */
- piv->isIdentiv = 1;
-
- piv->cert = &buf[idx];
- piv->certSz = length;
- idx += length;
-
- /* Nonce (0B 14) */
- if (GetASNHeader(buf, ASN_PIV_NONCE, &idx, &length, totalSz) >= 0) {
- piv->nonce = &buf[idx];
- piv->nonceSz = length;
- idx += length;
- }
-
- /* Signed Nonce (0C 82 0100) */
- if (GetASNHeader(buf, ASN_PIV_SIGNED_NONCE, &idx, &length, totalSz) >= 0) {
- piv->signedNonce = &buf[idx];
- piv->signedNonceSz = length;
- }
-
- idx = 0;
- buf = piv->cert;
- totalSz = piv->certSz;
- }
-
- /* Certificate Buffer Total Size (53 82 05F6) */
- if (GetASNHeader(buf, ASN_APPLICATION | ASN_PRINTABLE_STRING, &idx,
- &length, totalSz) < 0) {
- return ASN_PARSE_E;
- }
- /* PIV Certificate (70 82 05ED) */
- if (GetASNHeader(buf, ASN_PIV_TAG_CERT, &idx, &length,
- totalSz) < 0) {
- return ASN_PARSE_E;
- }
-
- /* Capture certificate buffer pointer and length */
- piv->cert = &buf[idx];
- piv->certSz = length;
- idx += length;
-
- /* PIV Certificate Info (71 01 00) */
- if (GetASNHeader(buf, ASN_PIV_TAG_CERT_INFO, &idx, &length,
- totalSz) >= 0) {
- if (length >= 1) {
- piv->compression = (buf[idx] & ASN_PIV_CERT_INFO_COMPRESSED);
- piv->isX509 = (buf[idx] & ASN_PIV_CERT_INFO_ISX509);
- }
- idx += length;
- }
-
- /* PIV Error Detection (FE 00) */
- if (GetASNHeader(buf, ASN_PIV_TAG_ERR_DET, &idx, &length,
- totalSz) >= 0) {
- piv->certErrDet = &buf[idx];
- piv->certErrDetSz = length;
- idx += length;
- }
-
- return 0;
-}
-
-#endif /* WOLFSSL_CERT_PIV */
-
-
-#undef ERROR_OUT
-
-#endif /* !NO_ASN */
-
-#ifdef WOLFSSL_SEP
-
-
-#endif /* WOLFSSL_SEP */
diff --git a/client/wolfssl/wolfcrypt/src/async.c b/client/wolfssl/wolfcrypt/src/async.c
deleted file mode 100644
index e69de29..0000000
diff --git a/client/wolfssl/wolfcrypt/src/blake2b.c b/client/wolfssl/wolfcrypt/src/blake2b.c
deleted file mode 100644
index 1541947..0000000
--- a/client/wolfssl/wolfcrypt/src/blake2b.c
+++ /dev/null
@@ -1,452 +0,0 @@
-/*
- BLAKE2 reference source code package - reference C implementations
-
- Written in 2012 by Samuel Neves
-
- To the extent possible under law, the author(s) have dedicated all copyright
- and related and neighboring rights to this software to the public domain
- worldwide. This software is distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along with
- this software. If not, see .
-*/
-/* blake2b.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifdef HAVE_BLAKE2
-
-#include
-#include
-
-
-static const word64 blake2b_IV[8] =
-{
- 0x6a09e667f3bcc908ULL, 0xbb67ae8584caa73bULL,
- 0x3c6ef372fe94f82bULL, 0xa54ff53a5f1d36f1ULL,
- 0x510e527fade682d1ULL, 0x9b05688c2b3e6c1fULL,
- 0x1f83d9abfb41bd6bULL, 0x5be0cd19137e2179ULL
-};
-
-static const byte blake2b_sigma[12][16] =
-{
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
- { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
- { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
- { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
- { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
- { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
- { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
- { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
- { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
- { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 } ,
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
- { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
-};
-
-
-static WC_INLINE int blake2b_set_lastnode( blake2b_state *S )
-{
- S->f[1] = ~0ULL;
- return 0;
-}
-
-/* Some helper functions, not necessarily useful */
-static WC_INLINE int blake2b_set_lastblock( blake2b_state *S )
-{
- if( S->last_node ) blake2b_set_lastnode( S );
-
- S->f[0] = ~0ULL;
- return 0;
-}
-
-static WC_INLINE int blake2b_increment_counter( blake2b_state *S, const word64
- inc )
-{
- S->t[0] += inc;
- S->t[1] += ( S->t[0] < inc );
- return 0;
-}
-
-static WC_INLINE int blake2b_init0( blake2b_state *S )
-{
- int i;
- XMEMSET( S, 0, sizeof( blake2b_state ) );
-
- for( i = 0; i < 8; ++i ) S->h[i] = blake2b_IV[i];
-
- return 0;
-}
-
-/* init xors IV with input parameter block */
-int blake2b_init_param( blake2b_state *S, const blake2b_param *P )
-{
- word32 i;
- byte *p ;
- blake2b_init0( S );
- p = ( byte * )( P );
-
- /* IV XOR ParamBlock */
- for( i = 0; i < 8; ++i )
- S->h[i] ^= load64( p + sizeof( S->h[i] ) * i );
-
- return 0;
-}
-
-
-
-int blake2b_init( blake2b_state *S, const byte outlen )
-{
- blake2b_param P[1];
-
- if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
-
-#ifdef WOLFSSL_BLAKE2B_INIT_EACH_FIELD
- P->digest_length = outlen;
- P->key_length = 0;
- P->fanout = 1;
- P->depth = 1;
- store32( &P->leaf_length, 0 );
- store64( &P->node_offset, 0 );
- P->node_depth = 0;
- P->inner_length = 0;
- XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
- XMEMSET( P->salt, 0, sizeof( P->salt ) );
- XMEMSET( P->personal, 0, sizeof( P->personal ) );
-#else
- XMEMSET( P, 0, sizeof( *P ) );
- P->digest_length = outlen;
- P->fanout = 1;
- P->depth = 1;
-#endif
- return blake2b_init_param( S, P );
-}
-
-
-int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key,
- const byte keylen )
-{
- blake2b_param P[1];
-
- if ( ( !outlen ) || ( outlen > BLAKE2B_OUTBYTES ) ) return -1;
-
- if ( !key || !keylen || keylen > BLAKE2B_KEYBYTES ) return -1;
-
-#ifdef WOLFSSL_BLAKE2B_INIT_EACH_FIELD
- P->digest_length = outlen;
- P->key_length = keylen;
- P->fanout = 1;
- P->depth = 1;
- store32( &P->leaf_length, 0 );
- store64( &P->node_offset, 0 );
- P->node_depth = 0;
- P->inner_length = 0;
- XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
- XMEMSET( P->salt, 0, sizeof( P->salt ) );
- XMEMSET( P->personal, 0, sizeof( P->personal ) );
-#else
- XMEMSET( P, 0, sizeof( *P ) );
- P->digest_length = outlen;
- P->key_length = keylen;
- P->fanout = 1;
- P->depth = 1;
-#endif
-
- if( blake2b_init_param( S, P ) < 0 ) return -1;
-
- {
-#ifdef WOLFSSL_SMALL_STACK
- byte* block;
-
- block = (byte*)XMALLOC(BLAKE2B_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if ( block == NULL ) return -1;
-#else
- byte block[BLAKE2B_BLOCKBYTES];
-#endif
-
- XMEMSET( block, 0, BLAKE2B_BLOCKBYTES );
- XMEMCPY( block, key, keylen );
- blake2b_update( S, block, BLAKE2B_BLOCKBYTES );
- secure_zero_memory( block, BLAKE2B_BLOCKBYTES ); /* Burn the key from */
- /* memory */
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(block, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- }
- return 0;
-}
-
-static int blake2b_compress( blake2b_state *S,
- const byte block[BLAKE2B_BLOCKBYTES] )
-{
- int i;
-
-#ifdef WOLFSSL_SMALL_STACK
- word64* m;
- word64* v;
-
- m = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if ( m == NULL ) return -1;
-
- v = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if ( v == NULL )
- {
- XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return -1;
- }
-#else
- word64 m[16];
- word64 v[16];
-#endif
-
- for( i = 0; i < 16; ++i )
- m[i] = load64( block + i * sizeof( m[i] ) );
-
- for( i = 0; i < 8; ++i )
- v[i] = S->h[i];
-
- v[ 8] = blake2b_IV[0];
- v[ 9] = blake2b_IV[1];
- v[10] = blake2b_IV[2];
- v[11] = blake2b_IV[3];
- v[12] = S->t[0] ^ blake2b_IV[4];
- v[13] = S->t[1] ^ blake2b_IV[5];
- v[14] = S->f[0] ^ blake2b_IV[6];
- v[15] = S->f[1] ^ blake2b_IV[7];
-#define G(r,i,a,b,c,d) \
- do { \
- a = a + b + m[blake2b_sigma[r][2*i+0]]; \
- d = rotr64(d ^ a, 32); \
- c = c + d; \
- b = rotr64(b ^ c, 24); \
- a = a + b + m[blake2b_sigma[r][2*i+1]]; \
- d = rotr64(d ^ a, 16); \
- c = c + d; \
- b = rotr64(b ^ c, 63); \
- } while(0)
-#define ROUND(r) \
- do { \
- G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
- G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
- G(r,2,v[ 2],v[ 6],v[10],v[14]); \
- G(r,3,v[ 3],v[ 7],v[11],v[15]); \
- G(r,4,v[ 0],v[ 5],v[10],v[15]); \
- G(r,5,v[ 1],v[ 6],v[11],v[12]); \
- G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
- G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
- } while(0)
- ROUND( 0 );
- ROUND( 1 );
- ROUND( 2 );
- ROUND( 3 );
- ROUND( 4 );
- ROUND( 5 );
- ROUND( 6 );
- ROUND( 7 );
- ROUND( 8 );
- ROUND( 9 );
- ROUND( 10 );
- ROUND( 11 );
-
- for( i = 0; i < 8; ++i )
- S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
-
-#undef G
-#undef ROUND
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return 0;
-}
-
-/* inlen now in bytes */
-int blake2b_update( blake2b_state *S, const byte *in, word64 inlen )
-{
- while( inlen > 0 )
- {
- word64 left = S->buflen;
- word64 fill = 2 * BLAKE2B_BLOCKBYTES - left;
-
- if( inlen > fill )
- {
- XMEMCPY( S->buf + left, in, (wolfssl_word)fill ); /* Fill buffer */
- S->buflen += fill;
- blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
-
- if ( blake2b_compress( S, S->buf ) < 0 ) return -1; /* Compress */
-
- XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES );
- /* Shift buffer left */
- S->buflen -= BLAKE2B_BLOCKBYTES;
- in += fill;
- inlen -= fill;
- }
- else /* inlen <= fill */
- {
- XMEMCPY( S->buf + left, in, (wolfssl_word)inlen );
- S->buflen += inlen; /* Be lazy, do not compress */
- inlen = 0;
- }
- }
-
- return 0;
-}
-
-/* Is this correct? */
-int blake2b_final( blake2b_state *S, byte *out, byte outlen )
-{
- byte buffer[BLAKE2B_OUTBYTES];
- int i;
-
- if( S->buflen > BLAKE2B_BLOCKBYTES )
- {
- blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
-
- if ( blake2b_compress( S, S->buf ) < 0 ) return -1;
-
- S->buflen -= BLAKE2B_BLOCKBYTES;
- XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, (wolfssl_word)S->buflen );
- }
-
- blake2b_increment_counter( S, S->buflen );
- blake2b_set_lastblock( S );
- XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2B_BLOCKBYTES - S->buflen) );
- /* Padding */
- if ( blake2b_compress( S, S->buf ) < 0 ) return -1;
-
- for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
- store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
-
- XMEMCPY( out, buffer, outlen );
- return 0;
-}
-
-/* inlen, at least, should be word64. Others can be size_t. */
-int blake2b( byte *out, const void *in, const void *key, const byte outlen,
- const word64 inlen, byte keylen )
-{
- blake2b_state S[1];
-
- /* Verify parameters */
- if ( NULL == in ) return -1;
-
- if ( NULL == out ) return -1;
-
- if( NULL == key ) keylen = 0;
-
- if( keylen > 0 )
- {
- if( blake2b_init_key( S, outlen, key, keylen ) < 0 ) return -1;
- }
- else
- {
- if( blake2b_init( S, outlen ) < 0 ) return -1;
- }
-
- if ( blake2b_update( S, ( byte * )in, inlen ) < 0) return -1;
-
- return blake2b_final( S, out, outlen );
-}
-
-#if defined(BLAKE2B_SELFTEST)
-#include
-#include "blake2-kat.h"
-int main( int argc, char **argv )
-{
- byte key[BLAKE2B_KEYBYTES];
- byte buf[KAT_LENGTH];
-
- for( word32 i = 0; i < BLAKE2B_KEYBYTES; ++i )
- key[i] = ( byte )i;
-
- for( word32 i = 0; i < KAT_LENGTH; ++i )
- buf[i] = ( byte )i;
-
- for( word32 i = 0; i < KAT_LENGTH; ++i )
- {
- byte hash[BLAKE2B_OUTBYTES];
- if ( blake2b( hash, buf, key, BLAKE2B_OUTBYTES, i, BLAKE2B_KEYBYTES ) < 0 )
- {
- puts( "error" );
- return -1;
- }
-
- if( 0 != XMEMCMP( hash, blake2b_keyed_kat[i], BLAKE2B_OUTBYTES ) )
- {
- puts( "error" );
- return -1;
- }
- }
-
- puts( "ok" );
- return 0;
-}
-#endif
-
-
-/* wolfCrypt API */
-
-/* Init Blake2b digest, track size in case final doesn't want to "remember" */
-int wc_InitBlake2b(Blake2b* b2b, word32 digestSz)
-{
- if (b2b == NULL){
- return -1;
- }
- b2b->digestSz = digestSz;
-
- return blake2b_init(b2b->S, (byte)digestSz);
-}
-
-
-/* Blake2b Update */
-int wc_Blake2bUpdate(Blake2b* b2b, const byte* data, word32 sz)
-{
- return blake2b_update(b2b->S, data, sz);
-}
-
-
-/* Blake2b Final, if pass in zero size we use init digestSz */
-int wc_Blake2bFinal(Blake2b* b2b, byte* final, word32 requestSz)
-{
- word32 sz = requestSz ? requestSz : b2b->digestSz;
-
- return blake2b_final(b2b->S, final, (byte)sz);
-}
-
-
-/* end CTaoCrypt API */
-
-#endif /* HAVE_BLAKE2 */
-
diff --git a/client/wolfssl/wolfcrypt/src/blake2s.c b/client/wolfssl/wolfcrypt/src/blake2s.c
deleted file mode 100644
index 651a1d1..0000000
--- a/client/wolfssl/wolfcrypt/src/blake2s.c
+++ /dev/null
@@ -1,446 +0,0 @@
-/*
- BLAKE2 reference source code package - reference C implementations
-
- Written in 2012 by Samuel Neves
-
- To the extent possible under law, the author(s) have dedicated all copyright
- and related and neighboring rights to this software to the public domain
- worldwide. This software is distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along with
- this software. If not, see .
-*/
-/* blake2s.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifdef HAVE_BLAKE2S
-
-#include
-#include
-
-
-static const word32 blake2s_IV[8] =
-{
- 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
- 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
-};
-
-static const byte blake2s_sigma[10][16] =
-{
- { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ,
- { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 } ,
- { 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 } ,
- { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 } ,
- { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 } ,
- { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } ,
- { 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 } ,
- { 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 } ,
- { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 } ,
- { 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 }
-};
-
-
-static WC_INLINE int blake2s_set_lastnode( blake2s_state *S )
-{
- S->f[1] = ~0;
- return 0;
-}
-
-/* Some helper functions, not necessarily useful */
-static WC_INLINE int blake2s_set_lastblock( blake2s_state *S )
-{
- if( S->last_node ) blake2s_set_lastnode( S );
-
- S->f[0] = ~0;
- return 0;
-}
-
-static WC_INLINE int blake2s_increment_counter( blake2s_state *S, const word32
- inc )
-{
- S->t[0] += inc;
- S->t[1] += ( S->t[0] < inc );
- return 0;
-}
-
-static WC_INLINE int blake2s_init0( blake2s_state *S )
-{
- int i;
- XMEMSET( S, 0, sizeof( blake2s_state ) );
-
- for( i = 0; i < 8; ++i ) S->h[i] = blake2s_IV[i];
-
- return 0;
-}
-
-/* init xors IV with input parameter block */
-int blake2s_init_param( blake2s_state *S, const blake2s_param *P )
-{
- word32 i;
- byte *p ;
- blake2s_init0( S );
- p = ( byte * )( P );
-
- /* IV XOR ParamBlock */
- for( i = 0; i < 8; ++i )
- S->h[i] ^= load32( p + sizeof( S->h[i] ) * i );
-
- return 0;
-}
-
-
-
-int blake2s_init( blake2s_state *S, const byte outlen )
-{
- blake2s_param P[1];
-
- if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
-
-#ifdef WOLFSSL_BLAKE2S_INIT_EACH_FIELD
- P->digest_length = outlen;
- P->key_length = 0;
- P->fanout = 1;
- P->depth = 1;
- store32( &P->leaf_length, 0 );
- store32( &P->node_offset, 0 );
- P->node_depth = 0;
- P->inner_length = 0;
- XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
- XMEMSET( P->salt, 0, sizeof( P->salt ) );
- XMEMSET( P->personal, 0, sizeof( P->personal ) );
-#else
- XMEMSET( P, 0, sizeof( *P ) );
- P->digest_length = outlen;
- P->fanout = 1;
- P->depth = 1;
-#endif
- return blake2s_init_param( S, P );
-}
-
-
-int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key,
- const byte keylen )
-{
- blake2s_param P[1];
-
- if ( ( !outlen ) || ( outlen > BLAKE2S_OUTBYTES ) ) return -1;
-
- if ( !key || !keylen || keylen > BLAKE2S_KEYBYTES ) return -1;
-
-#ifdef WOLFSSL_BLAKE2S_INIT_EACH_FIELD
- P->digest_length = outlen;
- P->key_length = keylen;
- P->fanout = 1;
- P->depth = 1;
- store32( &P->leaf_length, 0 );
- store64( &P->node_offset, 0 );
- P->node_depth = 0;
- P->inner_length = 0;
- XMEMSET( P->reserved, 0, sizeof( P->reserved ) );
- XMEMSET( P->salt, 0, sizeof( P->salt ) );
- XMEMSET( P->personal, 0, sizeof( P->personal ) );
-#else
- XMEMSET( P, 0, sizeof( *P ) );
- P->digest_length = outlen;
- P->key_length = keylen;
- P->fanout = 1;
- P->depth = 1;
-#endif
-
- if( blake2s_init_param( S, P ) < 0 ) return -1;
-
- {
-#ifdef WOLFSSL_SMALL_STACK
- byte* block;
-
- block = (byte*)XMALLOC(BLAKE2S_BLOCKBYTES, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if ( block == NULL ) return -1;
-#else
- byte block[BLAKE2S_BLOCKBYTES];
-#endif
-
- XMEMSET( block, 0, BLAKE2S_BLOCKBYTES );
- XMEMCPY( block, key, keylen );
- blake2s_update( S, block, BLAKE2S_BLOCKBYTES );
- secure_zero_memory( block, BLAKE2S_BLOCKBYTES ); /* Burn the key from */
- /* memory */
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(block, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
- }
- return 0;
-}
-
-static int blake2s_compress( blake2s_state *S,
- const byte block[BLAKE2S_BLOCKBYTES] )
-{
- int i;
-
-#ifdef WOLFSSL_SMALL_STACK
- word32* m;
- word32* v;
-
- m = (word32*)XMALLOC(sizeof(word32) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if ( m == NULL ) return -1;
-
- v = (word32*)XMALLOC(sizeof(word32) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-
- if ( v == NULL )
- {
- XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return -1;
- }
-#else
- word32 m[16];
- word32 v[16];
-#endif
-
- for( i = 0; i < 16; ++i )
- m[i] = load32( block + i * sizeof( m[i] ) );
-
- for( i = 0; i < 8; ++i )
- v[i] = S->h[i];
-
- v[ 8] = blake2s_IV[0];
- v[ 9] = blake2s_IV[1];
- v[10] = blake2s_IV[2];
- v[11] = blake2s_IV[3];
- v[12] = S->t[0] ^ blake2s_IV[4];
- v[13] = S->t[1] ^ blake2s_IV[5];
- v[14] = S->f[0] ^ blake2s_IV[6];
- v[15] = S->f[1] ^ blake2s_IV[7];
-#define G(r,i,a,b,c,d) \
- do { \
- a = a + b + m[blake2s_sigma[r][2*i+0]]; \
- d = rotr32(d ^ a, 16); \
- c = c + d; \
- b = rotr32(b ^ c, 12); \
- a = a + b + m[blake2s_sigma[r][2*i+1]]; \
- d = rotr32(d ^ a, 8); \
- c = c + d; \
- b = rotr32(b ^ c, 7); \
- } while(0)
-#define ROUND(r) \
- do { \
- G(r,0,v[ 0],v[ 4],v[ 8],v[12]); \
- G(r,1,v[ 1],v[ 5],v[ 9],v[13]); \
- G(r,2,v[ 2],v[ 6],v[10],v[14]); \
- G(r,3,v[ 3],v[ 7],v[11],v[15]); \
- G(r,4,v[ 0],v[ 5],v[10],v[15]); \
- G(r,5,v[ 1],v[ 6],v[11],v[12]); \
- G(r,6,v[ 2],v[ 7],v[ 8],v[13]); \
- G(r,7,v[ 3],v[ 4],v[ 9],v[14]); \
- } while(0)
- ROUND( 0 );
- ROUND( 1 );
- ROUND( 2 );
- ROUND( 3 );
- ROUND( 4 );
- ROUND( 5 );
- ROUND( 6 );
- ROUND( 7 );
- ROUND( 8 );
- ROUND( 9 );
-
- for( i = 0; i < 8; ++i )
- S->h[i] = S->h[i] ^ v[i] ^ v[i + 8];
-
-#undef G
-#undef ROUND
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return 0;
-}
-
-/* inlen now in bytes */
-int blake2s_update( blake2s_state *S, const byte *in, word32 inlen )
-{
- while( inlen > 0 )
- {
- word32 left = S->buflen;
- word32 fill = 2 * BLAKE2S_BLOCKBYTES - left;
-
- if( inlen > fill )
- {
- XMEMCPY( S->buf + left, in, (wolfssl_word)fill ); /* Fill buffer */
- S->buflen += fill;
- blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
-
- if ( blake2s_compress( S, S->buf ) < 0 ) return -1; /* Compress */
-
- XMEMCPY( S->buf, S->buf + BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES );
- /* Shift buffer left */
- S->buflen -= BLAKE2S_BLOCKBYTES;
- in += fill;
- inlen -= fill;
- }
- else /* inlen <= fill */
- {
- XMEMCPY( S->buf + left, in, (wolfssl_word)inlen );
- S->buflen += inlen; /* Be lazy, do not compress */
- inlen = 0;
- }
- }
-
- return 0;
-}
-
-/* Is this correct? */
-int blake2s_final( blake2s_state *S, byte *out, byte outlen )
-{
- int i;
- byte buffer[BLAKE2S_BLOCKBYTES];
-
- if( S->buflen > BLAKE2S_BLOCKBYTES )
- {
- blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
-
- if ( blake2s_compress( S, S->buf ) < 0 ) return -1;
-
- S->buflen -= BLAKE2S_BLOCKBYTES;
- XMEMCPY( S->buf, S->buf + BLAKE2S_BLOCKBYTES, (wolfssl_word)S->buflen );
- }
-
- blake2s_increment_counter( S, S->buflen );
- blake2s_set_lastblock( S );
- XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2S_BLOCKBYTES - S->buflen) );
- /* Padding */
- if ( blake2s_compress( S, S->buf ) < 0 ) return -1;
-
- for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
- store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
-
- XMEMCPY( out, buffer, outlen );
- return 0;
-}
-
-/* inlen, at least, should be word32. Others can be size_t. */
-int blake2s( byte *out, const void *in, const void *key, const byte outlen,
- const word32 inlen, byte keylen )
-{
- blake2s_state S[1];
-
- /* Verify parameters */
- if ( NULL == in ) return -1;
-
- if ( NULL == out ) return -1;
-
- if( NULL == key ) keylen = 0;
-
- if( keylen > 0 )
- {
- if( blake2s_init_key( S, outlen, key, keylen ) < 0 ) return -1;
- }
- else
- {
- if( blake2s_init( S, outlen ) < 0 ) return -1;
- }
-
- if ( blake2s_update( S, ( byte * )in, inlen ) < 0) return -1;
-
- return blake2s_final( S, out, outlen );
-}
-
-#if defined(BLAKE2S_SELFTEST)
-#include
-#include "blake2-kat.h"
-int main( int argc, char **argv )
-{
- byte key[BLAKE2S_KEYBYTES];
- byte buf[KAT_LENGTH];
-
- for( word32 i = 0; i < BLAKE2S_KEYBYTES; ++i )
- key[i] = ( byte )i;
-
- for( word32 i = 0; i < KAT_LENGTH; ++i )
- buf[i] = ( byte )i;
-
- for( word32 i = 0; i < KAT_LENGTH; ++i )
- {
- byte hash[BLAKE2S_OUTBYTES];
- if ( blake2s( hash, buf, key, BLAKE2S_OUTBYTES, i, BLAKE2S_KEYBYTES ) < 0 )
- {
- puts( "error" );
- return -1;
- }
-
- if( 0 != XMEMCMP( hash, blake2s_keyed_kat[i], BLAKE2S_OUTBYTES ) )
- {
- puts( "error" );
- return -1;
- }
- }
-
- puts( "ok" );
- return 0;
-}
-#endif
-
-
-/* wolfCrypt API */
-
-/* Init Blake2s digest, track size in case final doesn't want to "remember" */
-int wc_InitBlake2s(Blake2s* b2s, word32 digestSz)
-{
- if (b2s == NULL){
- return -1;
- }
- b2s->digestSz = digestSz;
-
- return blake2s_init(b2s->S, (byte)digestSz);
-}
-
-
-/* Blake2s Update */
-int wc_Blake2sUpdate(Blake2s* b2s, const byte* data, word32 sz)
-{
- return blake2s_update(b2s->S, data, sz);
-}
-
-
-/* Blake2s Final, if pass in zero size we use init digestSz */
-int wc_Blake2sFinal(Blake2s* b2s, byte* final, word32 requestSz)
-{
- word32 sz = requestSz ? requestSz : b2s->digestSz;
-
- return blake2s_final(b2s->S, final, (byte)sz);
-}
-
-
-/* end CTaoCrypt API */
-
-#endif /* HAVE_BLAKE2S */
-
diff --git a/client/wolfssl/wolfcrypt/src/camellia.c b/client/wolfssl/wolfcrypt/src/camellia.c
deleted file mode 100644
index 89ee661..0000000
--- a/client/wolfssl/wolfcrypt/src/camellia.c
+++ /dev/null
@@ -1,1644 +0,0 @@
-/* camellia.c ver 1.2.0
- *
- * Copyright (c) 2006,2007
- * NTT (Nippon Telegraph and Telephone Corporation) . All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer as
- * the first lines of this file unmodified.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY NTT ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL NTT BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/* camellia.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-/*
- * Algorithm Specification
- * http://info.isl.ntt.co.jp/crypt/eng/camellia/specifications.html
- */
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#ifdef HAVE_CAMELLIA
-
-#include
-#include
-#include
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include
-#endif
-
-/* u32 must be 32bit word */
-typedef unsigned int u32;
-typedef unsigned char u8;
-
-/* key constants */
-
-#define CAMELLIA_SIGMA1L ((u32)0xA09E667FL)
-#define CAMELLIA_SIGMA1R ((u32)0x3BCC908BL)
-#define CAMELLIA_SIGMA2L ((u32)0xB67AE858L)
-#define CAMELLIA_SIGMA2R ((u32)0x4CAA73B2L)
-#define CAMELLIA_SIGMA3L ((u32)0xC6EF372FL)
-#define CAMELLIA_SIGMA3R ((u32)0xE94F82BEL)
-#define CAMELLIA_SIGMA4L ((u32)0x54FF53A5L)
-#define CAMELLIA_SIGMA4R ((u32)0xF1D36F1CL)
-#define CAMELLIA_SIGMA5L ((u32)0x10E527FAL)
-#define CAMELLIA_SIGMA5R ((u32)0xDE682D1DL)
-#define CAMELLIA_SIGMA6L ((u32)0xB05688C2L)
-#define CAMELLIA_SIGMA6R ((u32)0xB3E6C1FDL)
-
-/*
- * macros
- */
-
-
-#if defined(_MSC_VER)
-
-# define SWAP(x) (_lrotl(x, 8) & 0x00ff00ff | _lrotr(x, 8) & 0xff00ff00)
-# define GETU32(p) SWAP(*((u32 *)(p)))
-# define PUTU32(ct, st) {*((u32 *)(ct)) = SWAP((st));}
-
-#else /* not MS-VC */
-
-# define GETU32(pt) \
- (((u32)(pt)[0] << 24) \
- ^ ((u32)(pt)[1] << 16) \
- ^ ((u32)(pt)[2] << 8) \
- ^ ((u32)(pt)[3]))
-
-# define PUTU32(ct, st) { \
- (ct)[0] = (u8)((st) >> 24); \
- (ct)[1] = (u8)((st) >> 16); \
- (ct)[2] = (u8)((st) >> 8); \
- (ct)[3] = (u8)(st); }
-
-#endif
-
-#define CamelliaSubkeyL(INDEX) (subkey[(INDEX)*2])
-#define CamelliaSubkeyR(INDEX) (subkey[(INDEX)*2 + 1])
-
-/* rotation right shift 1byte */
-#define CAMELLIA_RR8(x) (((x) >> 8) + ((x) << 24))
-/* rotation left shift 1bit */
-#define CAMELLIA_RL1(x) (((x) << 1) + ((x) >> 31))
-/* rotation left shift 1byte */
-#define CAMELLIA_RL8(x) (((x) << 8) + ((x) >> 24))
-
-#define CAMELLIA_ROLDQ(ll, lr, rl, rr, w0, w1, bits) \
- do { \
- w0 = ll; \
- ll = (ll << bits) + (lr >> (32 - bits)); \
- lr = (lr << bits) + (rl >> (32 - bits)); \
- rl = (rl << bits) + (rr >> (32 - bits)); \
- rr = (rr << bits) + (w0 >> (32 - bits)); \
- } while(0)
-
-#define CAMELLIA_ROLDQo32(ll, lr, rl, rr, w0, w1, bits) \
- do { \
- w0 = ll; \
- w1 = lr; \
- ll = (lr << (bits - 32)) + (rl >> (64 - bits)); \
- lr = (rl << (bits - 32)) + (rr >> (64 - bits)); \
- rl = (rr << (bits - 32)) + (w0 >> (64 - bits)); \
- rr = (w0 << (bits - 32)) + (w1 >> (64 - bits)); \
- } while(0)
-
-#define CAMELLIA_SP1110(INDEX) (camellia_sp1110[(INDEX)])
-#define CAMELLIA_SP0222(INDEX) (camellia_sp0222[(INDEX)])
-#define CAMELLIA_SP3033(INDEX) (camellia_sp3033[(INDEX)])
-#define CAMELLIA_SP4404(INDEX) (camellia_sp4404[(INDEX)])
-
-#define CAMELLIA_F(xl, xr, kl, kr, yl, yr, il, ir, t0, t1) \
- do { \
- il = xl ^ kl; \
- ir = xr ^ kr; \
- t0 = il >> 16; \
- t1 = ir >> 16; \
- yl = CAMELLIA_SP1110(ir & 0xff) \
- ^ CAMELLIA_SP0222((t1 >> 8) & 0xff) \
- ^ CAMELLIA_SP3033(t1 & 0xff) \
- ^ CAMELLIA_SP4404((ir >> 8) & 0xff); \
- yr = CAMELLIA_SP1110((t0 >> 8) & 0xff) \
- ^ CAMELLIA_SP0222(t0 & 0xff) \
- ^ CAMELLIA_SP3033((il >> 8) & 0xff) \
- ^ CAMELLIA_SP4404(il & 0xff); \
- yl ^= yr; \
- yr = CAMELLIA_RR8(yr); \
- yr ^= yl; \
- } while(0)
-
-
-/*
- * for speed up
- *
- */
-#define CAMELLIA_FLS(ll, lr, rl, rr, kll, klr, krl, krr, t0, t1, t2, t3) \
- do { \
- t0 = kll; \
- t0 &= ll; \
- lr ^= CAMELLIA_RL1(t0); \
- t1 = klr; \
- t1 |= lr; \
- ll ^= t1; \
- \
- t2 = krr; \
- t2 |= rr; \
- rl ^= t2; \
- t3 = krl; \
- t3 &= rl; \
- rr ^= CAMELLIA_RL1(t3); \
- } while(0)
-
-#define CAMELLIA_ROUNDSM(xl, xr, kl, kr, yl, yr, il, ir, t0, t1) \
- do { \
- ir = CAMELLIA_SP1110(xr & 0xff) \
- ^ CAMELLIA_SP0222((xr >> 24) & 0xff) \
- ^ CAMELLIA_SP3033((xr >> 16) & 0xff) \
- ^ CAMELLIA_SP4404((xr >> 8) & 0xff); \
- il = CAMELLIA_SP1110((xl >> 24) & 0xff) \
- ^ CAMELLIA_SP0222((xl >> 16) & 0xff) \
- ^ CAMELLIA_SP3033((xl >> 8) & 0xff) \
- ^ CAMELLIA_SP4404(xl & 0xff); \
- il ^= kl; \
- ir ^= kr; \
- ir ^= il; \
- il = CAMELLIA_RR8(il); \
- il ^= ir; \
- yl ^= ir; \
- yr ^= il; \
- } while(0)
-
-
-static const u32 camellia_sp1110[256] = {
- 0x70707000,0x82828200,0x2c2c2c00,0xececec00,
- 0xb3b3b300,0x27272700,0xc0c0c000,0xe5e5e500,
- 0xe4e4e400,0x85858500,0x57575700,0x35353500,
- 0xeaeaea00,0x0c0c0c00,0xaeaeae00,0x41414100,
- 0x23232300,0xefefef00,0x6b6b6b00,0x93939300,
- 0x45454500,0x19191900,0xa5a5a500,0x21212100,
- 0xededed00,0x0e0e0e00,0x4f4f4f00,0x4e4e4e00,
- 0x1d1d1d00,0x65656500,0x92929200,0xbdbdbd00,
- 0x86868600,0xb8b8b800,0xafafaf00,0x8f8f8f00,
- 0x7c7c7c00,0xebebeb00,0x1f1f1f00,0xcecece00,
- 0x3e3e3e00,0x30303000,0xdcdcdc00,0x5f5f5f00,
- 0x5e5e5e00,0xc5c5c500,0x0b0b0b00,0x1a1a1a00,
- 0xa6a6a600,0xe1e1e100,0x39393900,0xcacaca00,
- 0xd5d5d500,0x47474700,0x5d5d5d00,0x3d3d3d00,
- 0xd9d9d900,0x01010100,0x5a5a5a00,0xd6d6d600,
- 0x51515100,0x56565600,0x6c6c6c00,0x4d4d4d00,
- 0x8b8b8b00,0x0d0d0d00,0x9a9a9a00,0x66666600,
- 0xfbfbfb00,0xcccccc00,0xb0b0b000,0x2d2d2d00,
- 0x74747400,0x12121200,0x2b2b2b00,0x20202000,
- 0xf0f0f000,0xb1b1b100,0x84848400,0x99999900,
- 0xdfdfdf00,0x4c4c4c00,0xcbcbcb00,0xc2c2c200,
- 0x34343400,0x7e7e7e00,0x76767600,0x05050500,
- 0x6d6d6d00,0xb7b7b700,0xa9a9a900,0x31313100,
- 0xd1d1d100,0x17171700,0x04040400,0xd7d7d700,
- 0x14141400,0x58585800,0x3a3a3a00,0x61616100,
- 0xdedede00,0x1b1b1b00,0x11111100,0x1c1c1c00,
- 0x32323200,0x0f0f0f00,0x9c9c9c00,0x16161600,
- 0x53535300,0x18181800,0xf2f2f200,0x22222200,
- 0xfefefe00,0x44444400,0xcfcfcf00,0xb2b2b200,
- 0xc3c3c300,0xb5b5b500,0x7a7a7a00,0x91919100,
- 0x24242400,0x08080800,0xe8e8e800,0xa8a8a800,
- 0x60606000,0xfcfcfc00,0x69696900,0x50505000,
- 0xaaaaaa00,0xd0d0d000,0xa0a0a000,0x7d7d7d00,
- 0xa1a1a100,0x89898900,0x62626200,0x97979700,
- 0x54545400,0x5b5b5b00,0x1e1e1e00,0x95959500,
- 0xe0e0e000,0xffffff00,0x64646400,0xd2d2d200,
- 0x10101000,0xc4c4c400,0x00000000,0x48484800,
- 0xa3a3a300,0xf7f7f700,0x75757500,0xdbdbdb00,
- 0x8a8a8a00,0x03030300,0xe6e6e600,0xdadada00,
- 0x09090900,0x3f3f3f00,0xdddddd00,0x94949400,
- 0x87878700,0x5c5c5c00,0x83838300,0x02020200,
- 0xcdcdcd00,0x4a4a4a00,0x90909000,0x33333300,
- 0x73737300,0x67676700,0xf6f6f600,0xf3f3f300,
- 0x9d9d9d00,0x7f7f7f00,0xbfbfbf00,0xe2e2e200,
- 0x52525200,0x9b9b9b00,0xd8d8d800,0x26262600,
- 0xc8c8c800,0x37373700,0xc6c6c600,0x3b3b3b00,
- 0x81818100,0x96969600,0x6f6f6f00,0x4b4b4b00,
- 0x13131300,0xbebebe00,0x63636300,0x2e2e2e00,
- 0xe9e9e900,0x79797900,0xa7a7a700,0x8c8c8c00,
- 0x9f9f9f00,0x6e6e6e00,0xbcbcbc00,0x8e8e8e00,
- 0x29292900,0xf5f5f500,0xf9f9f900,0xb6b6b600,
- 0x2f2f2f00,0xfdfdfd00,0xb4b4b400,0x59595900,
- 0x78787800,0x98989800,0x06060600,0x6a6a6a00,
- 0xe7e7e700,0x46464600,0x71717100,0xbababa00,
- 0xd4d4d400,0x25252500,0xababab00,0x42424200,
- 0x88888800,0xa2a2a200,0x8d8d8d00,0xfafafa00,
- 0x72727200,0x07070700,0xb9b9b900,0x55555500,
- 0xf8f8f800,0xeeeeee00,0xacacac00,0x0a0a0a00,
- 0x36363600,0x49494900,0x2a2a2a00,0x68686800,
- 0x3c3c3c00,0x38383800,0xf1f1f100,0xa4a4a400,
- 0x40404000,0x28282800,0xd3d3d300,0x7b7b7b00,
- 0xbbbbbb00,0xc9c9c900,0x43434300,0xc1c1c100,
- 0x15151500,0xe3e3e300,0xadadad00,0xf4f4f400,
- 0x77777700,0xc7c7c700,0x80808000,0x9e9e9e00,
-};
-
-static const u32 camellia_sp0222[256] = {
- 0x00e0e0e0,0x00050505,0x00585858,0x00d9d9d9,
- 0x00676767,0x004e4e4e,0x00818181,0x00cbcbcb,
- 0x00c9c9c9,0x000b0b0b,0x00aeaeae,0x006a6a6a,
- 0x00d5d5d5,0x00181818,0x005d5d5d,0x00828282,
- 0x00464646,0x00dfdfdf,0x00d6d6d6,0x00272727,
- 0x008a8a8a,0x00323232,0x004b4b4b,0x00424242,
- 0x00dbdbdb,0x001c1c1c,0x009e9e9e,0x009c9c9c,
- 0x003a3a3a,0x00cacaca,0x00252525,0x007b7b7b,
- 0x000d0d0d,0x00717171,0x005f5f5f,0x001f1f1f,
- 0x00f8f8f8,0x00d7d7d7,0x003e3e3e,0x009d9d9d,
- 0x007c7c7c,0x00606060,0x00b9b9b9,0x00bebebe,
- 0x00bcbcbc,0x008b8b8b,0x00161616,0x00343434,
- 0x004d4d4d,0x00c3c3c3,0x00727272,0x00959595,
- 0x00ababab,0x008e8e8e,0x00bababa,0x007a7a7a,
- 0x00b3b3b3,0x00020202,0x00b4b4b4,0x00adadad,
- 0x00a2a2a2,0x00acacac,0x00d8d8d8,0x009a9a9a,
- 0x00171717,0x001a1a1a,0x00353535,0x00cccccc,
- 0x00f7f7f7,0x00999999,0x00616161,0x005a5a5a,
- 0x00e8e8e8,0x00242424,0x00565656,0x00404040,
- 0x00e1e1e1,0x00636363,0x00090909,0x00333333,
- 0x00bfbfbf,0x00989898,0x00979797,0x00858585,
- 0x00686868,0x00fcfcfc,0x00ececec,0x000a0a0a,
- 0x00dadada,0x006f6f6f,0x00535353,0x00626262,
- 0x00a3a3a3,0x002e2e2e,0x00080808,0x00afafaf,
- 0x00282828,0x00b0b0b0,0x00747474,0x00c2c2c2,
- 0x00bdbdbd,0x00363636,0x00222222,0x00383838,
- 0x00646464,0x001e1e1e,0x00393939,0x002c2c2c,
- 0x00a6a6a6,0x00303030,0x00e5e5e5,0x00444444,
- 0x00fdfdfd,0x00888888,0x009f9f9f,0x00656565,
- 0x00878787,0x006b6b6b,0x00f4f4f4,0x00232323,
- 0x00484848,0x00101010,0x00d1d1d1,0x00515151,
- 0x00c0c0c0,0x00f9f9f9,0x00d2d2d2,0x00a0a0a0,
- 0x00555555,0x00a1a1a1,0x00414141,0x00fafafa,
- 0x00434343,0x00131313,0x00c4c4c4,0x002f2f2f,
- 0x00a8a8a8,0x00b6b6b6,0x003c3c3c,0x002b2b2b,
- 0x00c1c1c1,0x00ffffff,0x00c8c8c8,0x00a5a5a5,
- 0x00202020,0x00898989,0x00000000,0x00909090,
- 0x00474747,0x00efefef,0x00eaeaea,0x00b7b7b7,
- 0x00151515,0x00060606,0x00cdcdcd,0x00b5b5b5,
- 0x00121212,0x007e7e7e,0x00bbbbbb,0x00292929,
- 0x000f0f0f,0x00b8b8b8,0x00070707,0x00040404,
- 0x009b9b9b,0x00949494,0x00212121,0x00666666,
- 0x00e6e6e6,0x00cecece,0x00ededed,0x00e7e7e7,
- 0x003b3b3b,0x00fefefe,0x007f7f7f,0x00c5c5c5,
- 0x00a4a4a4,0x00373737,0x00b1b1b1,0x004c4c4c,
- 0x00919191,0x006e6e6e,0x008d8d8d,0x00767676,
- 0x00030303,0x002d2d2d,0x00dedede,0x00969696,
- 0x00262626,0x007d7d7d,0x00c6c6c6,0x005c5c5c,
- 0x00d3d3d3,0x00f2f2f2,0x004f4f4f,0x00191919,
- 0x003f3f3f,0x00dcdcdc,0x00797979,0x001d1d1d,
- 0x00525252,0x00ebebeb,0x00f3f3f3,0x006d6d6d,
- 0x005e5e5e,0x00fbfbfb,0x00696969,0x00b2b2b2,
- 0x00f0f0f0,0x00313131,0x000c0c0c,0x00d4d4d4,
- 0x00cfcfcf,0x008c8c8c,0x00e2e2e2,0x00757575,
- 0x00a9a9a9,0x004a4a4a,0x00575757,0x00848484,
- 0x00111111,0x00454545,0x001b1b1b,0x00f5f5f5,
- 0x00e4e4e4,0x000e0e0e,0x00737373,0x00aaaaaa,
- 0x00f1f1f1,0x00dddddd,0x00595959,0x00141414,
- 0x006c6c6c,0x00929292,0x00545454,0x00d0d0d0,
- 0x00787878,0x00707070,0x00e3e3e3,0x00494949,
- 0x00808080,0x00505050,0x00a7a7a7,0x00f6f6f6,
- 0x00777777,0x00939393,0x00868686,0x00838383,
- 0x002a2a2a,0x00c7c7c7,0x005b5b5b,0x00e9e9e9,
- 0x00eeeeee,0x008f8f8f,0x00010101,0x003d3d3d,
-};
-
-static const u32 camellia_sp3033[256] = {
- 0x38003838,0x41004141,0x16001616,0x76007676,
- 0xd900d9d9,0x93009393,0x60006060,0xf200f2f2,
- 0x72007272,0xc200c2c2,0xab00abab,0x9a009a9a,
- 0x75007575,0x06000606,0x57005757,0xa000a0a0,
- 0x91009191,0xf700f7f7,0xb500b5b5,0xc900c9c9,
- 0xa200a2a2,0x8c008c8c,0xd200d2d2,0x90009090,
- 0xf600f6f6,0x07000707,0xa700a7a7,0x27002727,
- 0x8e008e8e,0xb200b2b2,0x49004949,0xde00dede,
- 0x43004343,0x5c005c5c,0xd700d7d7,0xc700c7c7,
- 0x3e003e3e,0xf500f5f5,0x8f008f8f,0x67006767,
- 0x1f001f1f,0x18001818,0x6e006e6e,0xaf00afaf,
- 0x2f002f2f,0xe200e2e2,0x85008585,0x0d000d0d,
- 0x53005353,0xf000f0f0,0x9c009c9c,0x65006565,
- 0xea00eaea,0xa300a3a3,0xae00aeae,0x9e009e9e,
- 0xec00ecec,0x80008080,0x2d002d2d,0x6b006b6b,
- 0xa800a8a8,0x2b002b2b,0x36003636,0xa600a6a6,
- 0xc500c5c5,0x86008686,0x4d004d4d,0x33003333,
- 0xfd00fdfd,0x66006666,0x58005858,0x96009696,
- 0x3a003a3a,0x09000909,0x95009595,0x10001010,
- 0x78007878,0xd800d8d8,0x42004242,0xcc00cccc,
- 0xef00efef,0x26002626,0xe500e5e5,0x61006161,
- 0x1a001a1a,0x3f003f3f,0x3b003b3b,0x82008282,
- 0xb600b6b6,0xdb00dbdb,0xd400d4d4,0x98009898,
- 0xe800e8e8,0x8b008b8b,0x02000202,0xeb00ebeb,
- 0x0a000a0a,0x2c002c2c,0x1d001d1d,0xb000b0b0,
- 0x6f006f6f,0x8d008d8d,0x88008888,0x0e000e0e,
- 0x19001919,0x87008787,0x4e004e4e,0x0b000b0b,
- 0xa900a9a9,0x0c000c0c,0x79007979,0x11001111,
- 0x7f007f7f,0x22002222,0xe700e7e7,0x59005959,
- 0xe100e1e1,0xda00dada,0x3d003d3d,0xc800c8c8,
- 0x12001212,0x04000404,0x74007474,0x54005454,
- 0x30003030,0x7e007e7e,0xb400b4b4,0x28002828,
- 0x55005555,0x68006868,0x50005050,0xbe00bebe,
- 0xd000d0d0,0xc400c4c4,0x31003131,0xcb00cbcb,
- 0x2a002a2a,0xad00adad,0x0f000f0f,0xca00caca,
- 0x70007070,0xff00ffff,0x32003232,0x69006969,
- 0x08000808,0x62006262,0x00000000,0x24002424,
- 0xd100d1d1,0xfb00fbfb,0xba00baba,0xed00eded,
- 0x45004545,0x81008181,0x73007373,0x6d006d6d,
- 0x84008484,0x9f009f9f,0xee00eeee,0x4a004a4a,
- 0xc300c3c3,0x2e002e2e,0xc100c1c1,0x01000101,
- 0xe600e6e6,0x25002525,0x48004848,0x99009999,
- 0xb900b9b9,0xb300b3b3,0x7b007b7b,0xf900f9f9,
- 0xce00cece,0xbf00bfbf,0xdf00dfdf,0x71007171,
- 0x29002929,0xcd00cdcd,0x6c006c6c,0x13001313,
- 0x64006464,0x9b009b9b,0x63006363,0x9d009d9d,
- 0xc000c0c0,0x4b004b4b,0xb700b7b7,0xa500a5a5,
- 0x89008989,0x5f005f5f,0xb100b1b1,0x17001717,
- 0xf400f4f4,0xbc00bcbc,0xd300d3d3,0x46004646,
- 0xcf00cfcf,0x37003737,0x5e005e5e,0x47004747,
- 0x94009494,0xfa00fafa,0xfc00fcfc,0x5b005b5b,
- 0x97009797,0xfe00fefe,0x5a005a5a,0xac00acac,
- 0x3c003c3c,0x4c004c4c,0x03000303,0x35003535,
- 0xf300f3f3,0x23002323,0xb800b8b8,0x5d005d5d,
- 0x6a006a6a,0x92009292,0xd500d5d5,0x21002121,
- 0x44004444,0x51005151,0xc600c6c6,0x7d007d7d,
- 0x39003939,0x83008383,0xdc00dcdc,0xaa00aaaa,
- 0x7c007c7c,0x77007777,0x56005656,0x05000505,
- 0x1b001b1b,0xa400a4a4,0x15001515,0x34003434,
- 0x1e001e1e,0x1c001c1c,0xf800f8f8,0x52005252,
- 0x20002020,0x14001414,0xe900e9e9,0xbd00bdbd,
- 0xdd00dddd,0xe400e4e4,0xa100a1a1,0xe000e0e0,
- 0x8a008a8a,0xf100f1f1,0xd600d6d6,0x7a007a7a,
- 0xbb00bbbb,0xe300e3e3,0x40004040,0x4f004f4f,
-};
-
-static const u32 camellia_sp4404[256] = {
- 0x70700070,0x2c2c002c,0xb3b300b3,0xc0c000c0,
- 0xe4e400e4,0x57570057,0xeaea00ea,0xaeae00ae,
- 0x23230023,0x6b6b006b,0x45450045,0xa5a500a5,
- 0xeded00ed,0x4f4f004f,0x1d1d001d,0x92920092,
- 0x86860086,0xafaf00af,0x7c7c007c,0x1f1f001f,
- 0x3e3e003e,0xdcdc00dc,0x5e5e005e,0x0b0b000b,
- 0xa6a600a6,0x39390039,0xd5d500d5,0x5d5d005d,
- 0xd9d900d9,0x5a5a005a,0x51510051,0x6c6c006c,
- 0x8b8b008b,0x9a9a009a,0xfbfb00fb,0xb0b000b0,
- 0x74740074,0x2b2b002b,0xf0f000f0,0x84840084,
- 0xdfdf00df,0xcbcb00cb,0x34340034,0x76760076,
- 0x6d6d006d,0xa9a900a9,0xd1d100d1,0x04040004,
- 0x14140014,0x3a3a003a,0xdede00de,0x11110011,
- 0x32320032,0x9c9c009c,0x53530053,0xf2f200f2,
- 0xfefe00fe,0xcfcf00cf,0xc3c300c3,0x7a7a007a,
- 0x24240024,0xe8e800e8,0x60600060,0x69690069,
- 0xaaaa00aa,0xa0a000a0,0xa1a100a1,0x62620062,
- 0x54540054,0x1e1e001e,0xe0e000e0,0x64640064,
- 0x10100010,0x00000000,0xa3a300a3,0x75750075,
- 0x8a8a008a,0xe6e600e6,0x09090009,0xdddd00dd,
- 0x87870087,0x83830083,0xcdcd00cd,0x90900090,
- 0x73730073,0xf6f600f6,0x9d9d009d,0xbfbf00bf,
- 0x52520052,0xd8d800d8,0xc8c800c8,0xc6c600c6,
- 0x81810081,0x6f6f006f,0x13130013,0x63630063,
- 0xe9e900e9,0xa7a700a7,0x9f9f009f,0xbcbc00bc,
- 0x29290029,0xf9f900f9,0x2f2f002f,0xb4b400b4,
- 0x78780078,0x06060006,0xe7e700e7,0x71710071,
- 0xd4d400d4,0xabab00ab,0x88880088,0x8d8d008d,
- 0x72720072,0xb9b900b9,0xf8f800f8,0xacac00ac,
- 0x36360036,0x2a2a002a,0x3c3c003c,0xf1f100f1,
- 0x40400040,0xd3d300d3,0xbbbb00bb,0x43430043,
- 0x15150015,0xadad00ad,0x77770077,0x80800080,
- 0x82820082,0xecec00ec,0x27270027,0xe5e500e5,
- 0x85850085,0x35350035,0x0c0c000c,0x41410041,
- 0xefef00ef,0x93930093,0x19190019,0x21210021,
- 0x0e0e000e,0x4e4e004e,0x65650065,0xbdbd00bd,
- 0xb8b800b8,0x8f8f008f,0xebeb00eb,0xcece00ce,
- 0x30300030,0x5f5f005f,0xc5c500c5,0x1a1a001a,
- 0xe1e100e1,0xcaca00ca,0x47470047,0x3d3d003d,
- 0x01010001,0xd6d600d6,0x56560056,0x4d4d004d,
- 0x0d0d000d,0x66660066,0xcccc00cc,0x2d2d002d,
- 0x12120012,0x20200020,0xb1b100b1,0x99990099,
- 0x4c4c004c,0xc2c200c2,0x7e7e007e,0x05050005,
- 0xb7b700b7,0x31310031,0x17170017,0xd7d700d7,
- 0x58580058,0x61610061,0x1b1b001b,0x1c1c001c,
- 0x0f0f000f,0x16160016,0x18180018,0x22220022,
- 0x44440044,0xb2b200b2,0xb5b500b5,0x91910091,
- 0x08080008,0xa8a800a8,0xfcfc00fc,0x50500050,
- 0xd0d000d0,0x7d7d007d,0x89890089,0x97970097,
- 0x5b5b005b,0x95950095,0xffff00ff,0xd2d200d2,
- 0xc4c400c4,0x48480048,0xf7f700f7,0xdbdb00db,
- 0x03030003,0xdada00da,0x3f3f003f,0x94940094,
- 0x5c5c005c,0x02020002,0x4a4a004a,0x33330033,
- 0x67670067,0xf3f300f3,0x7f7f007f,0xe2e200e2,
- 0x9b9b009b,0x26260026,0x37370037,0x3b3b003b,
- 0x96960096,0x4b4b004b,0xbebe00be,0x2e2e002e,
- 0x79790079,0x8c8c008c,0x6e6e006e,0x8e8e008e,
- 0xf5f500f5,0xb6b600b6,0xfdfd00fd,0x59590059,
- 0x98980098,0x6a6a006a,0x46460046,0xbaba00ba,
- 0x25250025,0x42420042,0xa2a200a2,0xfafa00fa,
- 0x07070007,0x55550055,0xeeee00ee,0x0a0a000a,
- 0x49490049,0x68680068,0x38380038,0xa4a400a4,
- 0x28280028,0x7b7b007b,0xc9c900c9,0xc1c100c1,
- 0xe3e300e3,0xf4f400f4,0xc7c700c7,0x9e9e009e,
-};
-
-
-/**
- * Stuff related to the Camellia key schedule
- */
-#define subl(x) subL[(x)]
-#define subr(x) subR[(x)]
-
-static int camellia_setup128(const unsigned char *key, u32 *subkey)
-{
- u32 kll, klr, krl, krr;
- u32 il, ir, t0, t1, w0, w1;
- u32 kw4l, kw4r, dw, tl, tr;
-
-#ifdef WOLFSSL_SMALL_STACK
- u32* subL;
- u32* subR;
-
- subL = (u32*) XMALLOC(sizeof(u32) * 26, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (subL == NULL)
- return MEMORY_E;
-
- subR = (u32*) XMALLOC(sizeof(u32) * 26, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (subR == NULL) {
- XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#else
- u32 subL[26];
- u32 subR[26];
-#endif
-
- /**
- * k == kll || klr || krl || krr (|| is concatenation)
- */
- kll = GETU32(key );
- klr = GETU32(key + 4);
- krl = GETU32(key + 8);
- krr = GETU32(key + 12);
- /**
- * generate KL dependent subkeys
- */
- subl(0) = kll; subr(0) = klr;
- subl(1) = krl; subr(1) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
- subl(4) = kll; subr(4) = klr;
- subl(5) = krl; subr(5) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 30);
- subl(10) = kll; subr(10) = klr;
- subl(11) = krl; subr(11) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
- subl(13) = krl; subr(13) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
- subl(16) = kll; subr(16) = klr;
- subl(17) = krl; subr(17) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
- subl(18) = kll; subr(18) = klr;
- subl(19) = krl; subr(19) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
- subl(22) = kll; subr(22) = klr;
- subl(23) = krl; subr(23) = krr;
-
- /* generate KA */
- kll = subl(0); klr = subr(0);
- krl = subl(1); krr = subr(1);
- CAMELLIA_F(kll, klr,
- CAMELLIA_SIGMA1L, CAMELLIA_SIGMA1R,
- w0, w1, il, ir, t0, t1);
- krl ^= w0; krr ^= w1;
- CAMELLIA_F(krl, krr,
- CAMELLIA_SIGMA2L, CAMELLIA_SIGMA2R,
- kll, klr, il, ir, t0, t1);
- CAMELLIA_F(kll, klr,
- CAMELLIA_SIGMA3L, CAMELLIA_SIGMA3R,
- krl, krr, il, ir, t0, t1);
- krl ^= w0; krr ^= w1;
- CAMELLIA_F(krl, krr,
- CAMELLIA_SIGMA4L, CAMELLIA_SIGMA4R,
- w0, w1, il, ir, t0, t1);
- kll ^= w0; klr ^= w1;
-
- /* generate KA dependent subkeys */
- subl(2) = kll; subr(2) = klr;
- subl(3) = krl; subr(3) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
- subl(6) = kll; subr(6) = klr;
- subl(7) = krl; subr(7) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
- subl(8) = kll; subr(8) = klr;
- subl(9) = krl; subr(9) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
- subl(12) = kll; subr(12) = klr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
- subl(14) = kll; subr(14) = klr;
- subl(15) = krl; subr(15) = krr;
- CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 34);
- subl(20) = kll; subr(20) = klr;
- subl(21) = krl; subr(21) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
- subl(24) = kll; subr(24) = klr;
- subl(25) = krl; subr(25) = krr;
-
-
- /* absorb kw2 to other subkeys */
- subl(3) ^= subl(1); subr(3) ^= subr(1);
- subl(5) ^= subl(1); subr(5) ^= subr(1);
- subl(7) ^= subl(1); subr(7) ^= subr(1);
- subl(1) ^= subr(1) & ~subr(9);
- dw = subl(1) & subl(9), subr(1) ^= CAMELLIA_RL1(dw);
- subl(11) ^= subl(1); subr(11) ^= subr(1);
- subl(13) ^= subl(1); subr(13) ^= subr(1);
- subl(15) ^= subl(1); subr(15) ^= subr(1);
- subl(1) ^= subr(1) & ~subr(17);
- dw = subl(1) & subl(17), subr(1) ^= CAMELLIA_RL1(dw);
- subl(19) ^= subl(1); subr(19) ^= subr(1);
- subl(21) ^= subl(1); subr(21) ^= subr(1);
- subl(23) ^= subl(1); subr(23) ^= subr(1);
- subl(24) ^= subl(1); subr(24) ^= subr(1);
-
- /* absorb kw4 to other subkeys */
- kw4l = subl(25); kw4r = subr(25);
- subl(22) ^= kw4l; subr(22) ^= kw4r;
- subl(20) ^= kw4l; subr(20) ^= kw4r;
- subl(18) ^= kw4l; subr(18) ^= kw4r;
- kw4l ^= kw4r & ~subr(16);
- dw = kw4l & subl(16), kw4r ^= CAMELLIA_RL1(dw);
- subl(14) ^= kw4l; subr(14) ^= kw4r;
- subl(12) ^= kw4l; subr(12) ^= kw4r;
- subl(10) ^= kw4l; subr(10) ^= kw4r;
- kw4l ^= kw4r & ~subr(8);
- dw = kw4l & subl(8), kw4r ^= CAMELLIA_RL1(dw);
- subl(6) ^= kw4l; subr(6) ^= kw4r;
- subl(4) ^= kw4l; subr(4) ^= kw4r;
- subl(2) ^= kw4l; subr(2) ^= kw4r;
- subl(0) ^= kw4l; subr(0) ^= kw4r;
-
- /* key XOR is end of F-function */
- CamelliaSubkeyL(0) = subl(0) ^ subl(2);
- CamelliaSubkeyR(0) = subr(0) ^ subr(2);
- CamelliaSubkeyL(2) = subl(3);
- CamelliaSubkeyR(2) = subr(3);
- CamelliaSubkeyL(3) = subl(2) ^ subl(4);
- CamelliaSubkeyR(3) = subr(2) ^ subr(4);
- CamelliaSubkeyL(4) = subl(3) ^ subl(5);
- CamelliaSubkeyR(4) = subr(3) ^ subr(5);
- CamelliaSubkeyL(5) = subl(4) ^ subl(6);
- CamelliaSubkeyR(5) = subr(4) ^ subr(6);
- CamelliaSubkeyL(6) = subl(5) ^ subl(7);
- CamelliaSubkeyR(6) = subr(5) ^ subr(7);
- tl = subl(10) ^ (subr(10) & ~subr(8));
- dw = tl & subl(8), tr = subr(10) ^ CAMELLIA_RL1(dw);
- CamelliaSubkeyL(7) = subl(6) ^ tl;
- CamelliaSubkeyR(7) = subr(6) ^ tr;
- CamelliaSubkeyL(8) = subl(8);
- CamelliaSubkeyR(8) = subr(8);
- CamelliaSubkeyL(9) = subl(9);
- CamelliaSubkeyR(9) = subr(9);
- tl = subl(7) ^ (subr(7) & ~subr(9));
- dw = tl & subl(9), tr = subr(7) ^ CAMELLIA_RL1(dw);
- CamelliaSubkeyL(10) = tl ^ subl(11);
- CamelliaSubkeyR(10) = tr ^ subr(11);
- CamelliaSubkeyL(11) = subl(10) ^ subl(12);
- CamelliaSubkeyR(11) = subr(10) ^ subr(12);
- CamelliaSubkeyL(12) = subl(11) ^ subl(13);
- CamelliaSubkeyR(12) = subr(11) ^ subr(13);
- CamelliaSubkeyL(13) = subl(12) ^ subl(14);
- CamelliaSubkeyR(13) = subr(12) ^ subr(14);
- CamelliaSubkeyL(14) = subl(13) ^ subl(15);
- CamelliaSubkeyR(14) = subr(13) ^ subr(15);
- tl = subl(18) ^ (subr(18) & ~subr(16));
- dw = tl & subl(16), tr = subr(18) ^ CAMELLIA_RL1(dw);
- CamelliaSubkeyL(15) = subl(14) ^ tl;
- CamelliaSubkeyR(15) = subr(14) ^ tr;
- CamelliaSubkeyL(16) = subl(16);
- CamelliaSubkeyR(16) = subr(16);
- CamelliaSubkeyL(17) = subl(17);
- CamelliaSubkeyR(17) = subr(17);
- tl = subl(15) ^ (subr(15) & ~subr(17));
- dw = tl & subl(17), tr = subr(15) ^ CAMELLIA_RL1(dw);
- CamelliaSubkeyL(18) = tl ^ subl(19);
- CamelliaSubkeyR(18) = tr ^ subr(19);
- CamelliaSubkeyL(19) = subl(18) ^ subl(20);
- CamelliaSubkeyR(19) = subr(18) ^ subr(20);
- CamelliaSubkeyL(20) = subl(19) ^ subl(21);
- CamelliaSubkeyR(20) = subr(19) ^ subr(21);
- CamelliaSubkeyL(21) = subl(20) ^ subl(22);
- CamelliaSubkeyR(21) = subr(20) ^ subr(22);
- CamelliaSubkeyL(22) = subl(21) ^ subl(23);
- CamelliaSubkeyR(22) = subr(21) ^ subr(23);
- CamelliaSubkeyL(23) = subl(22);
- CamelliaSubkeyR(23) = subr(22);
- CamelliaSubkeyL(24) = subl(24) ^ subl(23);
- CamelliaSubkeyR(24) = subr(24) ^ subr(23);
-
- /* apply the inverse of the last half of P-function */
- dw = CamelliaSubkeyL(2) ^ CamelliaSubkeyR(2), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(2) = CamelliaSubkeyL(2) ^ dw, CamelliaSubkeyL(2) = dw;
- dw = CamelliaSubkeyL(3) ^ CamelliaSubkeyR(3), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(3) = CamelliaSubkeyL(3) ^ dw, CamelliaSubkeyL(3) = dw;
- dw = CamelliaSubkeyL(4) ^ CamelliaSubkeyR(4), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(4) = CamelliaSubkeyL(4) ^ dw, CamelliaSubkeyL(4) = dw;
- dw = CamelliaSubkeyL(5) ^ CamelliaSubkeyR(5), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(5) = CamelliaSubkeyL(5) ^ dw, CamelliaSubkeyL(5) = dw;
- dw = CamelliaSubkeyL(6) ^ CamelliaSubkeyR(6), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(6) = CamelliaSubkeyL(6) ^ dw, CamelliaSubkeyL(6) = dw;
- dw = CamelliaSubkeyL(7) ^ CamelliaSubkeyR(7), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(7) = CamelliaSubkeyL(7) ^ dw, CamelliaSubkeyL(7) = dw;
- dw = CamelliaSubkeyL(10) ^ CamelliaSubkeyR(10), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(10) = CamelliaSubkeyL(10) ^ dw, CamelliaSubkeyL(10) = dw;
- dw = CamelliaSubkeyL(11) ^ CamelliaSubkeyR(11), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(11) = CamelliaSubkeyL(11) ^ dw, CamelliaSubkeyL(11) = dw;
- dw = CamelliaSubkeyL(12) ^ CamelliaSubkeyR(12), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(12) = CamelliaSubkeyL(12) ^ dw, CamelliaSubkeyL(12) = dw;
- dw = CamelliaSubkeyL(13) ^ CamelliaSubkeyR(13), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(13) = CamelliaSubkeyL(13) ^ dw, CamelliaSubkeyL(13) = dw;
- dw = CamelliaSubkeyL(14) ^ CamelliaSubkeyR(14), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(14) = CamelliaSubkeyL(14) ^ dw, CamelliaSubkeyL(14) = dw;
- dw = CamelliaSubkeyL(15) ^ CamelliaSubkeyR(15), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(15) = CamelliaSubkeyL(15) ^ dw, CamelliaSubkeyL(15) = dw;
- dw = CamelliaSubkeyL(18) ^ CamelliaSubkeyR(18), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(18) = CamelliaSubkeyL(18) ^ dw, CamelliaSubkeyL(18) = dw;
- dw = CamelliaSubkeyL(19) ^ CamelliaSubkeyR(19), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(19) = CamelliaSubkeyL(19) ^ dw, CamelliaSubkeyL(19) = dw;
- dw = CamelliaSubkeyL(20) ^ CamelliaSubkeyR(20), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(20) = CamelliaSubkeyL(20) ^ dw, CamelliaSubkeyL(20) = dw;
- dw = CamelliaSubkeyL(21) ^ CamelliaSubkeyR(21), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(21) = CamelliaSubkeyL(21) ^ dw, CamelliaSubkeyL(21) = dw;
- dw = CamelliaSubkeyL(22) ^ CamelliaSubkeyR(22), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(22) = CamelliaSubkeyL(22) ^ dw, CamelliaSubkeyL(22) = dw;
- dw = CamelliaSubkeyL(23) ^ CamelliaSubkeyR(23), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(23) = CamelliaSubkeyL(23) ^ dw, CamelliaSubkeyL(23) = dw;
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(subR, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return 0;
-}
-
-static int camellia_setup256(const unsigned char *key, u32 *subkey)
-{
- u32 kll,klr,krl,krr; /* left half of key */
- u32 krll,krlr,krrl,krrr; /* right half of key */
- u32 il, ir, t0, t1, w0, w1; /* temporary variables */
- u32 kw4l, kw4r, dw, tl, tr;
-
-#ifdef WOLFSSL_SMALL_STACK
- u32* subL;
- u32* subR;
-
- subL = (u32*) XMALLOC(sizeof(u32) * 34, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (subL == NULL)
- return MEMORY_E;
-
- subR = (u32*) XMALLOC(sizeof(u32) * 34, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (subR == NULL) {
- XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- return MEMORY_E;
- }
-#else
- u32 subL[34];
- u32 subR[34];
-#endif
-
- /**
- * key = (kll || klr || krl || krr || krll || krlr || krrl || krrr)
- * (|| is concatenation)
- */
-
- kll = GETU32(key );
- klr = GETU32(key + 4);
- krl = GETU32(key + 8);
- krr = GETU32(key + 12);
- krll = GETU32(key + 16);
- krlr = GETU32(key + 20);
- krrl = GETU32(key + 24);
- krrr = GETU32(key + 28);
-
- /* generate KL dependent subkeys */
- subl(0) = kll; subr(0) = klr;
- subl(1) = krl; subr(1) = krr;
- CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 45);
- subl(12) = kll; subr(12) = klr;
- subl(13) = krl; subr(13) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
- subl(16) = kll; subr(16) = klr;
- subl(17) = krl; subr(17) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 17);
- subl(22) = kll; subr(22) = klr;
- subl(23) = krl; subr(23) = krr;
- CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 34);
- subl(30) = kll; subr(30) = klr;
- subl(31) = krl; subr(31) = krr;
-
- /* generate KR dependent subkeys */
- CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 15);
- subl(4) = krll; subr(4) = krlr;
- subl(5) = krrl; subr(5) = krrr;
- CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 15);
- subl(8) = krll; subr(8) = krlr;
- subl(9) = krrl; subr(9) = krrr;
- CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30);
- subl(18) = krll; subr(18) = krlr;
- subl(19) = krrl; subr(19) = krrr;
- CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 34);
- subl(26) = krll; subr(26) = krlr;
- subl(27) = krrl; subr(27) = krrr;
- CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 34);
-
- /* generate KA */
- kll = subl(0) ^ krll; klr = subr(0) ^ krlr;
- krl = subl(1) ^ krrl; krr = subr(1) ^ krrr;
- CAMELLIA_F(kll, klr,
- CAMELLIA_SIGMA1L, CAMELLIA_SIGMA1R,
- w0, w1, il, ir, t0, t1);
- krl ^= w0; krr ^= w1;
- CAMELLIA_F(krl, krr,
- CAMELLIA_SIGMA2L, CAMELLIA_SIGMA2R,
- kll, klr, il, ir, t0, t1);
- kll ^= krll; klr ^= krlr;
- CAMELLIA_F(kll, klr,
- CAMELLIA_SIGMA3L, CAMELLIA_SIGMA3R,
- krl, krr, il, ir, t0, t1);
- krl ^= w0 ^ krrl; krr ^= w1 ^ krrr;
- CAMELLIA_F(krl, krr,
- CAMELLIA_SIGMA4L, CAMELLIA_SIGMA4R,
- w0, w1, il, ir, t0, t1);
- kll ^= w0; klr ^= w1;
-
- /* generate KB */
- krll ^= kll; krlr ^= klr;
- krrl ^= krl; krrr ^= krr;
- CAMELLIA_F(krll, krlr,
- CAMELLIA_SIGMA5L, CAMELLIA_SIGMA5R,
- w0, w1, il, ir, t0, t1);
- krrl ^= w0; krrr ^= w1;
- CAMELLIA_F(krrl, krrr,
- CAMELLIA_SIGMA6L, CAMELLIA_SIGMA6R,
- w0, w1, il, ir, t0, t1);
- krll ^= w0; krlr ^= w1;
-
- /* generate KA dependent subkeys */
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 15);
- subl(6) = kll; subr(6) = klr;
- subl(7) = krl; subr(7) = krr;
- CAMELLIA_ROLDQ(kll, klr, krl, krr, w0, w1, 30);
- subl(14) = kll; subr(14) = klr;
- subl(15) = krl; subr(15) = krr;
- subl(24) = klr; subr(24) = krl;
- subl(25) = krr; subr(25) = kll;
- CAMELLIA_ROLDQo32(kll, klr, krl, krr, w0, w1, 49);
- subl(28) = kll; subr(28) = klr;
- subl(29) = krl; subr(29) = krr;
-
- /* generate KB dependent subkeys */
- subl(2) = krll; subr(2) = krlr;
- subl(3) = krrl; subr(3) = krrr;
- CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30);
- subl(10) = krll; subr(10) = krlr;
- subl(11) = krrl; subr(11) = krrr;
- CAMELLIA_ROLDQ(krll, krlr, krrl, krrr, w0, w1, 30);
- subl(20) = krll; subr(20) = krlr;
- subl(21) = krrl; subr(21) = krrr;
- CAMELLIA_ROLDQo32(krll, krlr, krrl, krrr, w0, w1, 51);
- subl(32) = krll; subr(32) = krlr;
- subl(33) = krrl; subr(33) = krrr;
-
- /* absorb kw2 to other subkeys */
- subl(3) ^= subl(1); subr(3) ^= subr(1);
- subl(5) ^= subl(1); subr(5) ^= subr(1);
- subl(7) ^= subl(1); subr(7) ^= subr(1);
- subl(1) ^= subr(1) & ~subr(9);
- dw = subl(1) & subl(9), subr(1) ^= CAMELLIA_RL1(dw);
- subl(11) ^= subl(1); subr(11) ^= subr(1);
- subl(13) ^= subl(1); subr(13) ^= subr(1);
- subl(15) ^= subl(1); subr(15) ^= subr(1);
- subl(1) ^= subr(1) & ~subr(17);
- dw = subl(1) & subl(17), subr(1) ^= CAMELLIA_RL1(dw);
- subl(19) ^= subl(1); subr(19) ^= subr(1);
- subl(21) ^= subl(1); subr(21) ^= subr(1);
- subl(23) ^= subl(1); subr(23) ^= subr(1);
- subl(1) ^= subr(1) & ~subr(25);
- dw = subl(1) & subl(25), subr(1) ^= CAMELLIA_RL1(dw);
- subl(27) ^= subl(1); subr(27) ^= subr(1);
- subl(29) ^= subl(1); subr(29) ^= subr(1);
- subl(31) ^= subl(1); subr(31) ^= subr(1);
- subl(32) ^= subl(1); subr(32) ^= subr(1);
-
- /* absorb kw4 to other subkeys */
- kw4l = subl(33); kw4r = subr(33);
- subl(30) ^= kw4l; subr(30) ^= kw4r;
- subl(28) ^= kw4l; subr(28) ^= kw4r;
- subl(26) ^= kw4l; subr(26) ^= kw4r;
- kw4l ^= kw4r & ~subr(24);
- dw = kw4l & subl(24), kw4r ^= CAMELLIA_RL1(dw);
- subl(22) ^= kw4l; subr(22) ^= kw4r;
- subl(20) ^= kw4l; subr(20) ^= kw4r;
- subl(18) ^= kw4l; subr(18) ^= kw4r;
- kw4l ^= kw4r & ~subr(16);
- dw = kw4l & subl(16), kw4r ^= CAMELLIA_RL1(dw);
- subl(14) ^= kw4l; subr(14) ^= kw4r;
- subl(12) ^= kw4l; subr(12) ^= kw4r;
- subl(10) ^= kw4l; subr(10) ^= kw4r;
- kw4l ^= kw4r & ~subr(8);
- dw = kw4l & subl(8), kw4r ^= CAMELLIA_RL1(dw);
- subl(6) ^= kw4l; subr(6) ^= kw4r;
- subl(4) ^= kw4l; subr(4) ^= kw4r;
- subl(2) ^= kw4l; subr(2) ^= kw4r;
- subl(0) ^= kw4l; subr(0) ^= kw4r;
-
- /* key XOR is end of F-function */
- CamelliaSubkeyL(0) = subl(0) ^ subl(2);
- CamelliaSubkeyR(0) = subr(0) ^ subr(2);
- CamelliaSubkeyL(2) = subl(3);
- CamelliaSubkeyR(2) = subr(3);
- CamelliaSubkeyL(3) = subl(2) ^ subl(4);
- CamelliaSubkeyR(3) = subr(2) ^ subr(4);
- CamelliaSubkeyL(4) = subl(3) ^ subl(5);
- CamelliaSubkeyR(4) = subr(3) ^ subr(5);
- CamelliaSubkeyL(5) = subl(4) ^ subl(6);
- CamelliaSubkeyR(5) = subr(4) ^ subr(6);
- CamelliaSubkeyL(6) = subl(5) ^ subl(7);
- CamelliaSubkeyR(6) = subr(5) ^ subr(7);
- tl = subl(10) ^ (subr(10) & ~subr(8));
- dw = tl & subl(8), tr = subr(10) ^ CAMELLIA_RL1(dw);
- CamelliaSubkeyL(7) = subl(6) ^ tl;
- CamelliaSubkeyR(7) = subr(6) ^ tr;
- CamelliaSubkeyL(8) = subl(8);
- CamelliaSubkeyR(8) = subr(8);
- CamelliaSubkeyL(9) = subl(9);
- CamelliaSubkeyR(9) = subr(9);
- tl = subl(7) ^ (subr(7) & ~subr(9));
- dw = tl & subl(9), tr = subr(7) ^ CAMELLIA_RL1(dw);
- CamelliaSubkeyL(10) = tl ^ subl(11);
- CamelliaSubkeyR(10) = tr ^ subr(11);
- CamelliaSubkeyL(11) = subl(10) ^ subl(12);
- CamelliaSubkeyR(11) = subr(10) ^ subr(12);
- CamelliaSubkeyL(12) = subl(11) ^ subl(13);
- CamelliaSubkeyR(12) = subr(11) ^ subr(13);
- CamelliaSubkeyL(13) = subl(12) ^ subl(14);
- CamelliaSubkeyR(13) = subr(12) ^ subr(14);
- CamelliaSubkeyL(14) = subl(13) ^ subl(15);
- CamelliaSubkeyR(14) = subr(13) ^ subr(15);
- tl = subl(18) ^ (subr(18) & ~subr(16));
- dw = tl & subl(16), tr = subr(18) ^ CAMELLIA_RL1(dw);
- CamelliaSubkeyL(15) = subl(14) ^ tl;
- CamelliaSubkeyR(15) = subr(14) ^ tr;
- CamelliaSubkeyL(16) = subl(16);
- CamelliaSubkeyR(16) = subr(16);
- CamelliaSubkeyL(17) = subl(17);
- CamelliaSubkeyR(17) = subr(17);
- tl = subl(15) ^ (subr(15) & ~subr(17));
- dw = tl & subl(17), tr = subr(15) ^ CAMELLIA_RL1(dw);
- CamelliaSubkeyL(18) = tl ^ subl(19);
- CamelliaSubkeyR(18) = tr ^ subr(19);
- CamelliaSubkeyL(19) = subl(18) ^ subl(20);
- CamelliaSubkeyR(19) = subr(18) ^ subr(20);
- CamelliaSubkeyL(20) = subl(19) ^ subl(21);
- CamelliaSubkeyR(20) = subr(19) ^ subr(21);
- CamelliaSubkeyL(21) = subl(20) ^ subl(22);
- CamelliaSubkeyR(21) = subr(20) ^ subr(22);
- CamelliaSubkeyL(22) = subl(21) ^ subl(23);
- CamelliaSubkeyR(22) = subr(21) ^ subr(23);
- tl = subl(26) ^ (subr(26) & ~subr(24));
- dw = tl & subl(24), tr = subr(26) ^ CAMELLIA_RL1(dw);
- CamelliaSubkeyL(23) = subl(22) ^ tl;
- CamelliaSubkeyR(23) = subr(22) ^ tr;
- CamelliaSubkeyL(24) = subl(24);
- CamelliaSubkeyR(24) = subr(24);
- CamelliaSubkeyL(25) = subl(25);
- CamelliaSubkeyR(25) = subr(25);
- tl = subl(23) ^ (subr(23) & ~subr(25));
- dw = tl & subl(25), tr = subr(23) ^ CAMELLIA_RL1(dw);
- CamelliaSubkeyL(26) = tl ^ subl(27);
- CamelliaSubkeyR(26) = tr ^ subr(27);
- CamelliaSubkeyL(27) = subl(26) ^ subl(28);
- CamelliaSubkeyR(27) = subr(26) ^ subr(28);
- CamelliaSubkeyL(28) = subl(27) ^ subl(29);
- CamelliaSubkeyR(28) = subr(27) ^ subr(29);
- CamelliaSubkeyL(29) = subl(28) ^ subl(30);
- CamelliaSubkeyR(29) = subr(28) ^ subr(30);
- CamelliaSubkeyL(30) = subl(29) ^ subl(31);
- CamelliaSubkeyR(30) = subr(29) ^ subr(31);
- CamelliaSubkeyL(31) = subl(30);
- CamelliaSubkeyR(31) = subr(30);
- CamelliaSubkeyL(32) = subl(32) ^ subl(31);
- CamelliaSubkeyR(32) = subr(32) ^ subr(31);
-
- /* apply the inverse of the last half of P-function */
- dw = CamelliaSubkeyL(2) ^ CamelliaSubkeyR(2), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(2) = CamelliaSubkeyL(2) ^ dw, CamelliaSubkeyL(2) = dw;
- dw = CamelliaSubkeyL(3) ^ CamelliaSubkeyR(3), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(3) = CamelliaSubkeyL(3) ^ dw, CamelliaSubkeyL(3) = dw;
- dw = CamelliaSubkeyL(4) ^ CamelliaSubkeyR(4), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(4) = CamelliaSubkeyL(4) ^ dw, CamelliaSubkeyL(4) = dw;
- dw = CamelliaSubkeyL(5) ^ CamelliaSubkeyR(5), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(5) = CamelliaSubkeyL(5) ^ dw, CamelliaSubkeyL(5) = dw;
- dw = CamelliaSubkeyL(6) ^ CamelliaSubkeyR(6), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(6) = CamelliaSubkeyL(6) ^ dw, CamelliaSubkeyL(6) = dw;
- dw = CamelliaSubkeyL(7) ^ CamelliaSubkeyR(7), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(7) = CamelliaSubkeyL(7) ^ dw, CamelliaSubkeyL(7) = dw;
- dw = CamelliaSubkeyL(10) ^ CamelliaSubkeyR(10), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(10) = CamelliaSubkeyL(10) ^ dw, CamelliaSubkeyL(10) = dw;
- dw = CamelliaSubkeyL(11) ^ CamelliaSubkeyR(11), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(11) = CamelliaSubkeyL(11) ^ dw, CamelliaSubkeyL(11) = dw;
- dw = CamelliaSubkeyL(12) ^ CamelliaSubkeyR(12), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(12) = CamelliaSubkeyL(12) ^ dw, CamelliaSubkeyL(12) = dw;
- dw = CamelliaSubkeyL(13) ^ CamelliaSubkeyR(13), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(13) = CamelliaSubkeyL(13) ^ dw, CamelliaSubkeyL(13) = dw;
- dw = CamelliaSubkeyL(14) ^ CamelliaSubkeyR(14), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(14) = CamelliaSubkeyL(14) ^ dw, CamelliaSubkeyL(14) = dw;
- dw = CamelliaSubkeyL(15) ^ CamelliaSubkeyR(15), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(15) = CamelliaSubkeyL(15) ^ dw, CamelliaSubkeyL(15) = dw;
- dw = CamelliaSubkeyL(18) ^ CamelliaSubkeyR(18), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(18) = CamelliaSubkeyL(18) ^ dw, CamelliaSubkeyL(18) = dw;
- dw = CamelliaSubkeyL(19) ^ CamelliaSubkeyR(19), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(19) = CamelliaSubkeyL(19) ^ dw, CamelliaSubkeyL(19) = dw;
- dw = CamelliaSubkeyL(20) ^ CamelliaSubkeyR(20), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(20) = CamelliaSubkeyL(20) ^ dw, CamelliaSubkeyL(20) = dw;
- dw = CamelliaSubkeyL(21) ^ CamelliaSubkeyR(21), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(21) = CamelliaSubkeyL(21) ^ dw, CamelliaSubkeyL(21) = dw;
- dw = CamelliaSubkeyL(22) ^ CamelliaSubkeyR(22), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(22) = CamelliaSubkeyL(22) ^ dw, CamelliaSubkeyL(22) = dw;
- dw = CamelliaSubkeyL(23) ^ CamelliaSubkeyR(23), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(23) = CamelliaSubkeyL(23) ^ dw, CamelliaSubkeyL(23) = dw;
- dw = CamelliaSubkeyL(26) ^ CamelliaSubkeyR(26), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(26) = CamelliaSubkeyL(26) ^ dw, CamelliaSubkeyL(26) = dw;
- dw = CamelliaSubkeyL(27) ^ CamelliaSubkeyR(27), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(27) = CamelliaSubkeyL(27) ^ dw, CamelliaSubkeyL(27) = dw;
- dw = CamelliaSubkeyL(28) ^ CamelliaSubkeyR(28), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(28) = CamelliaSubkeyL(28) ^ dw, CamelliaSubkeyL(28) = dw;
- dw = CamelliaSubkeyL(29) ^ CamelliaSubkeyR(29), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(29) = CamelliaSubkeyL(29) ^ dw, CamelliaSubkeyL(29) = dw;
- dw = CamelliaSubkeyL(30) ^ CamelliaSubkeyR(30), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(30) = CamelliaSubkeyL(30) ^ dw, CamelliaSubkeyL(30) = dw;
- dw = CamelliaSubkeyL(31) ^ CamelliaSubkeyR(31), dw = CAMELLIA_RL8(dw);
- CamelliaSubkeyR(31) = CamelliaSubkeyL(31) ^ dw,CamelliaSubkeyL(31) = dw;
-
-#ifdef WOLFSSL_SMALL_STACK
- XFREE(subL, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(subR, NULL, DYNAMIC_TYPE_TMP_BUFFER);
-#endif
-
- return 0;
-}
-
-static int camellia_setup192(const unsigned char *key, u32 *subkey)
-{
- unsigned char kk[32];
- u32 krll, krlr, krrl,krrr;
-
- XMEMCPY(kk, key, 24);
- XMEMCPY((unsigned char *)&krll, key+16,4);
- XMEMCPY((unsigned char *)&krlr, key+20,4);
- krrl = ~krll;
- krrr = ~krlr;
- XMEMCPY(kk+24, (unsigned char *)&krrl, 4);
- XMEMCPY(kk+28, (unsigned char *)&krrr, 4);
-
- return camellia_setup256(kk, subkey);
-}
-
-
-/**
- * Stuff related to camellia encryption/decryption
- *
- * "io" must be 4byte aligned and big-endian data.
- */
-static void camellia_encrypt128(const u32 *subkey, u32 *io)
-{
- u32 il, ir, t0, t1;
-
- /* pre whitening but absorb kw2*/
- io[0] ^= CamelliaSubkeyL(0);
- io[1] ^= CamelliaSubkeyR(0);
- /* main iteration */
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(2),CamelliaSubkeyR(2),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(3),CamelliaSubkeyR(3),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(4),CamelliaSubkeyR(4),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(5),CamelliaSubkeyR(5),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(6),CamelliaSubkeyR(6),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(7),CamelliaSubkeyR(7),
- io[0],io[1],il,ir,t0,t1);
-
- CAMELLIA_FLS(io[0],io[1],io[2],io[3],
- CamelliaSubkeyL(8),CamelliaSubkeyR(8),
- CamelliaSubkeyL(9),CamelliaSubkeyR(9),
- t0,t1,il,ir);
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(10),CamelliaSubkeyR(10),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(11),CamelliaSubkeyR(11),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(12),CamelliaSubkeyR(12),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(13),CamelliaSubkeyR(13),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(14),CamelliaSubkeyR(14),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(15),CamelliaSubkeyR(15),
- io[0],io[1],il,ir,t0,t1);
-
- CAMELLIA_FLS(io[0],io[1],io[2],io[3],
- CamelliaSubkeyL(16),CamelliaSubkeyR(16),
- CamelliaSubkeyL(17),CamelliaSubkeyR(17),
- t0,t1,il,ir);
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(18),CamelliaSubkeyR(18),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(19),CamelliaSubkeyR(19),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(20),CamelliaSubkeyR(20),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(21),CamelliaSubkeyR(21),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(22),CamelliaSubkeyR(22),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(23),CamelliaSubkeyR(23),
- io[0],io[1],il,ir,t0,t1);
-
- /* post whitening but kw4 */
- io[2] ^= CamelliaSubkeyL(24);
- io[3] ^= CamelliaSubkeyR(24);
-
- t0 = io[0];
- t1 = io[1];
- io[0] = io[2];
- io[1] = io[3];
- io[2] = t0;
- io[3] = t1;
-
- return;
-}
-
-static void camellia_decrypt128(const u32 *subkey, u32 *io)
-{
- u32 il,ir,t0,t1; /* temporary variables */
-
- /* pre whitening but absorb kw2*/
- io[0] ^= CamelliaSubkeyL(24);
- io[1] ^= CamelliaSubkeyR(24);
-
- /* main iteration */
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(23),CamelliaSubkeyR(23),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(22),CamelliaSubkeyR(22),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(21),CamelliaSubkeyR(21),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(20),CamelliaSubkeyR(20),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(19),CamelliaSubkeyR(19),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(18),CamelliaSubkeyR(18),
- io[0],io[1],il,ir,t0,t1);
-
- CAMELLIA_FLS(io[0],io[1],io[2],io[3],
- CamelliaSubkeyL(17),CamelliaSubkeyR(17),
- CamelliaSubkeyL(16),CamelliaSubkeyR(16),
- t0,t1,il,ir);
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(15),CamelliaSubkeyR(15),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(14),CamelliaSubkeyR(14),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(13),CamelliaSubkeyR(13),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(12),CamelliaSubkeyR(12),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(11),CamelliaSubkeyR(11),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(10),CamelliaSubkeyR(10),
- io[0],io[1],il,ir,t0,t1);
-
- CAMELLIA_FLS(io[0],io[1],io[2],io[3],
- CamelliaSubkeyL(9),CamelliaSubkeyR(9),
- CamelliaSubkeyL(8),CamelliaSubkeyR(8),
- t0,t1,il,ir);
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(7),CamelliaSubkeyR(7),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(6),CamelliaSubkeyR(6),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(5),CamelliaSubkeyR(5),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(4),CamelliaSubkeyR(4),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(3),CamelliaSubkeyR(3),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(2),CamelliaSubkeyR(2),
- io[0],io[1],il,ir,t0,t1);
-
- /* post whitening but kw4 */
- io[2] ^= CamelliaSubkeyL(0);
- io[3] ^= CamelliaSubkeyR(0);
-
- t0 = io[0];
- t1 = io[1];
- io[0] = io[2];
- io[1] = io[3];
- io[2] = t0;
- io[3] = t1;
-
- return;
-}
-
-/**
- * stuff for 192 and 256bit encryption/decryption
- */
-static void camellia_encrypt256(const u32 *subkey, u32 *io)
-{
- u32 il,ir,t0,t1; /* temporary variables */
-
- /* pre whitening but absorb kw2*/
- io[0] ^= CamelliaSubkeyL(0);
- io[1] ^= CamelliaSubkeyR(0);
-
- /* main iteration */
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(2),CamelliaSubkeyR(2),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(3),CamelliaSubkeyR(3),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(4),CamelliaSubkeyR(4),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(5),CamelliaSubkeyR(5),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(6),CamelliaSubkeyR(6),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(7),CamelliaSubkeyR(7),
- io[0],io[1],il,ir,t0,t1);
-
- CAMELLIA_FLS(io[0],io[1],io[2],io[3],
- CamelliaSubkeyL(8),CamelliaSubkeyR(8),
- CamelliaSubkeyL(9),CamelliaSubkeyR(9),
- t0,t1,il,ir);
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(10),CamelliaSubkeyR(10),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(11),CamelliaSubkeyR(11),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(12),CamelliaSubkeyR(12),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(13),CamelliaSubkeyR(13),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(14),CamelliaSubkeyR(14),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(15),CamelliaSubkeyR(15),
- io[0],io[1],il,ir,t0,t1);
-
- CAMELLIA_FLS(io[0],io[1],io[2],io[3],
- CamelliaSubkeyL(16),CamelliaSubkeyR(16),
- CamelliaSubkeyL(17),CamelliaSubkeyR(17),
- t0,t1,il,ir);
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(18),CamelliaSubkeyR(18),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(19),CamelliaSubkeyR(19),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(20),CamelliaSubkeyR(20),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(21),CamelliaSubkeyR(21),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(22),CamelliaSubkeyR(22),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(23),CamelliaSubkeyR(23),
- io[0],io[1],il,ir,t0,t1);
-
- CAMELLIA_FLS(io[0],io[1],io[2],io[3],
- CamelliaSubkeyL(24),CamelliaSubkeyR(24),
- CamelliaSubkeyL(25),CamelliaSubkeyR(25),
- t0,t1,il,ir);
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(26),CamelliaSubkeyR(26),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(27),CamelliaSubkeyR(27),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(28),CamelliaSubkeyR(28),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(29),CamelliaSubkeyR(29),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(30),CamelliaSubkeyR(30),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(31),CamelliaSubkeyR(31),
- io[0],io[1],il,ir,t0,t1);
-
- /* post whitening but kw4 */
- io[2] ^= CamelliaSubkeyL(32);
- io[3] ^= CamelliaSubkeyR(32);
-
- t0 = io[0];
- t1 = io[1];
- io[0] = io[2];
- io[1] = io[3];
- io[2] = t0;
- io[3] = t1;
-
- return;
-}
-
-static void camellia_decrypt256(const u32 *subkey, u32 *io)
-{
- u32 il,ir,t0,t1; /* temporary variables */
-
- /* pre whitening but absorb kw2*/
- io[0] ^= CamelliaSubkeyL(32);
- io[1] ^= CamelliaSubkeyR(32);
-
- /* main iteration */
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(31),CamelliaSubkeyR(31),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(30),CamelliaSubkeyR(30),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(29),CamelliaSubkeyR(29),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(28),CamelliaSubkeyR(28),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(27),CamelliaSubkeyR(27),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(26),CamelliaSubkeyR(26),
- io[0],io[1],il,ir,t0,t1);
-
- CAMELLIA_FLS(io[0],io[1],io[2],io[3],
- CamelliaSubkeyL(25),CamelliaSubkeyR(25),
- CamelliaSubkeyL(24),CamelliaSubkeyR(24),
- t0,t1,il,ir);
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(23),CamelliaSubkeyR(23),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(22),CamelliaSubkeyR(22),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(21),CamelliaSubkeyR(21),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(20),CamelliaSubkeyR(20),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(19),CamelliaSubkeyR(19),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(18),CamelliaSubkeyR(18),
- io[0],io[1],il,ir,t0,t1);
-
- CAMELLIA_FLS(io[0],io[1],io[2],io[3],
- CamelliaSubkeyL(17),CamelliaSubkeyR(17),
- CamelliaSubkeyL(16),CamelliaSubkeyR(16),
- t0,t1,il,ir);
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(15),CamelliaSubkeyR(15),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(14),CamelliaSubkeyR(14),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(13),CamelliaSubkeyR(13),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(12),CamelliaSubkeyR(12),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(11),CamelliaSubkeyR(11),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(10),CamelliaSubkeyR(10),
- io[0],io[1],il,ir,t0,t1);
-
- CAMELLIA_FLS(io[0],io[1],io[2],io[3],
- CamelliaSubkeyL(9),CamelliaSubkeyR(9),
- CamelliaSubkeyL(8),CamelliaSubkeyR(8),
- t0,t1,il,ir);
-
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(7),CamelliaSubkeyR(7),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(6),CamelliaSubkeyR(6),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(5),CamelliaSubkeyR(5),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(4),CamelliaSubkeyR(4),
- io[0],io[1],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[0],io[1],
- CamelliaSubkeyL(3),CamelliaSubkeyR(3),
- io[2],io[3],il,ir,t0,t1);
- CAMELLIA_ROUNDSM(io[2],io[3],
- CamelliaSubkeyL(2),CamelliaSubkeyR(2),
- io[0],io[1],il,ir,t0,t1);
-
- /* post whitening but kw4 */
- io[2] ^= CamelliaSubkeyL(0);
- io[3] ^= CamelliaSubkeyR(0);
-
- t0 = io[0];
- t1 = io[1];
- io[0] = io[2];
- io[1] = io[3];
- io[2] = t0;
- io[3] = t1;
-
- return;
-}
-
-/***
- *
- * API for compatibility
- */
-
-static void Camellia_EncryptBlock(const int keyBitLength,
- const unsigned char *plaintext,
- const KEY_TABLE_TYPE keyTable,
- unsigned char *ciphertext)
-{
- u32 tmp[4];
-
- tmp[0] = GETU32(plaintext);
- tmp[1] = GETU32(plaintext + 4);
- tmp[2] = GETU32(plaintext + 8);
- tmp[3] = GETU32(plaintext + 12);
-
- switch (keyBitLength) {
- case 128:
- camellia_encrypt128(keyTable, tmp);
- break;
- case 192:
- /* fall through */
- case 256:
- camellia_encrypt256(keyTable, tmp);
- break;
- default:
- break;
- }
-
- PUTU32(ciphertext, tmp[0]);
- PUTU32(ciphertext + 4, tmp[1]);
- PUTU32(ciphertext + 8, tmp[2]);
- PUTU32(ciphertext + 12, tmp[3]);
-}
-
-static void Camellia_DecryptBlock(const int keyBitLength,
- const unsigned char *ciphertext,
- const KEY_TABLE_TYPE keyTable,
- unsigned char *plaintext)
-{
- u32 tmp[4];
-
- tmp[0] = GETU32(ciphertext);
- tmp[1] = GETU32(ciphertext + 4);
- tmp[2] = GETU32(ciphertext + 8);
- tmp[3] = GETU32(ciphertext + 12);
-
- switch (keyBitLength) {
- case 128:
- camellia_decrypt128(keyTable, tmp);
- break;
- case 192:
- /* fall through */
- case 256:
- camellia_decrypt256(keyTable, tmp);
- break;
- default:
- break;
- }
- PUTU32(plaintext, tmp[0]);
- PUTU32(plaintext + 4, tmp[1]);
- PUTU32(plaintext + 8, tmp[2]);
- PUTU32(plaintext + 12, tmp[3]);
-}
-
-
-
-/* wolfCrypt wrappers to the Camellia code */
-
-int wc_CamelliaSetKey(Camellia* cam, const byte* key, word32 len, const byte* iv)
-{
- int ret = 0;
-
- if (cam == NULL) return BAD_FUNC_ARG;
-
- XMEMSET(cam->key, 0, sizeof(KEY_TABLE_TYPE));
-
- switch (len) {
- case 16:
- ret = camellia_setup128(key, cam->key);
- break;
- case 24:
- ret = camellia_setup192(key, cam->key);
- break;
- case 32:
- ret = camellia_setup256(key, cam->key);
- break;
- default:
- return BAD_FUNC_ARG;
- }
-
- if (ret != 0)
- return ret;
-
- cam->keySz = len * 8;
-
- return wc_CamelliaSetIV(cam, iv);
-}
-
-
-int wc_CamelliaSetIV(Camellia* cam, const byte* iv)
-{
- if (cam == NULL)
- return BAD_FUNC_ARG;
-
- if (iv)
- XMEMCPY(cam->reg, iv, CAMELLIA_BLOCK_SIZE);
- else
- XMEMSET(cam->reg, 0, CAMELLIA_BLOCK_SIZE);
-
- return 0;
-}
-
-
-int wc_CamelliaEncryptDirect(Camellia* cam, byte* out, const byte* in)
-{
- if (cam == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
- Camellia_EncryptBlock(cam->keySz, in, cam->key, out);
-
- return 0;
-}
-
-
-int wc_CamelliaDecryptDirect(Camellia* cam, byte* out, const byte* in)
-{
- if (cam == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
- Camellia_DecryptBlock(cam->keySz, in, cam->key, out);
-
- return 0;
-}
-
-
-int wc_CamelliaCbcEncrypt(Camellia* cam, byte* out, const byte* in, word32 sz)
-{
- word32 blocks;
- if (cam == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
- blocks = sz / CAMELLIA_BLOCK_SIZE;
-
- while (blocks--) {
- xorbuf((byte*)cam->reg, in, CAMELLIA_BLOCK_SIZE);
- Camellia_EncryptBlock(cam->keySz, (byte*)cam->reg,
- cam->key, (byte*)cam->reg);
- XMEMCPY(out, cam->reg, CAMELLIA_BLOCK_SIZE);
-
- out += CAMELLIA_BLOCK_SIZE;
- in += CAMELLIA_BLOCK_SIZE;
- }
-
- return 0;
-}
-
-
-int wc_CamelliaCbcDecrypt(Camellia* cam, byte* out, const byte* in, word32 sz)
-{
- word32 blocks;
- if (cam == NULL || out == NULL || in == NULL) {
- return BAD_FUNC_ARG;
- }
- blocks = sz / CAMELLIA_BLOCK_SIZE;
-
- while (blocks--) {
- XMEMCPY(cam->tmp, in, CAMELLIA_BLOCK_SIZE);
- Camellia_DecryptBlock(cam->keySz, (byte*)cam->tmp, cam->key, out);
- xorbuf(out, (byte*)cam->reg, CAMELLIA_BLOCK_SIZE);
- XMEMCPY(cam->reg, cam->tmp, CAMELLIA_BLOCK_SIZE);
-
- out += CAMELLIA_BLOCK_SIZE;
- in += CAMELLIA_BLOCK_SIZE;
- }
-
- return 0;
-}
-
-
-#endif /* HAVE_CAMELLIA */
-
diff --git a/client/wolfssl/wolfcrypt/src/chacha.c b/client/wolfssl/wolfcrypt/src/chacha.c
deleted file mode 100644
index 38a1ede..0000000
--- a/client/wolfssl/wolfcrypt/src/chacha.c
+++ /dev/null
@@ -1,348 +0,0 @@
-/* chacha.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-/*
- * based from
- * chacha-ref.c version 20080118
- * D. J. Bernstein
- * Public domain.
- */
-
-
-#ifdef WOLFSSL_ARMASM
- /* implementation is located in wolfcrypt/src/port/arm/armv8-chacha.c */
-
-#else
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#if defined(HAVE_CHACHA) && !defined(WOLFSSL_ARMASM)
-
-#include
-#include
-#include
-#include
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include
-#endif
-
-#ifdef CHACHA_AEAD_TEST
- #include
-#endif
-
-#ifdef USE_INTEL_CHACHA_SPEEDUP
- #include
- #include
-
- #if defined(__GNUC__) && ((__GNUC__ < 4) || \
- (__GNUC__ == 4 && __GNUC_MINOR__ <= 8))
- #undef NO_AVX2_SUPPORT
- #define NO_AVX2_SUPPORT
- #endif
- #if defined(__clang__) && ((__clang_major__ < 3) || \
- (__clang_major__ == 3 && __clang_minor__ <= 5))
- #undef NO_AVX2_SUPPORT
- #define NO_AVX2_SUPPORT
- #elif defined(__clang__) && defined(NO_AVX2_SUPPORT)
- #undef NO_AVX2_SUPPORT
- #endif
-
- #ifndef NO_AVX2_SUPPORT
- #define HAVE_INTEL_AVX2
- #endif
-
- static int cpuidFlagsSet = 0;
- static int cpuidFlags = 0;
-#endif
-
-#ifdef BIG_ENDIAN_ORDER
- #define LITTLE32(x) ByteReverseWord32(x)
-#else
- #define LITTLE32(x) (x)
-#endif
-
-/* Number of rounds */
-#define ROUNDS 20
-
-#define U32C(v) (v##U)
-#define U32V(v) ((word32)(v) & U32C(0xFFFFFFFF))
-#define U8TO32_LITTLE(p) LITTLE32(((word32*)(p))[0])
-
-#define ROTATE(v,c) rotlFixed(v, c)
-#define XOR(v,w) ((v) ^ (w))
-#define PLUS(v,w) (U32V((v) + (w)))
-#define PLUSONE(v) (PLUS((v),1))
-
-#define QUARTERROUND(a,b,c,d) \
- x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]),16); \
- x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]),12); \
- x[a] = PLUS(x[a],x[b]); x[d] = ROTATE(XOR(x[d],x[a]), 8); \
- x[c] = PLUS(x[c],x[d]); x[b] = ROTATE(XOR(x[b],x[c]), 7);
-
-
-/**
- * Set up iv(nonce). Earlier versions used 64 bits instead of 96, this version
- * uses the typical AEAD 96 bit nonce and can do record sizes of 256 GB.
- */
-int wc_Chacha_SetIV(ChaCha* ctx, const byte* inIv, word32 counter)
-{
- word32 temp[CHACHA_IV_WORDS];/* used for alignment of memory */
-
-#ifdef CHACHA_AEAD_TEST
- word32 i;
- printf("NONCE : ");
- for (i = 0; i < CHACHA_IV_BYTES; i++) {
- printf("%02x", inIv[i]);
- }
- printf("\n\n");
-#endif
-
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- XMEMCPY(temp, inIv, CHACHA_IV_BYTES);
-
- ctx->left = 0; /* resets state */
- ctx->X[CHACHA_IV_BYTES+0] = counter; /* block counter */
- ctx->X[CHACHA_IV_BYTES+1] = LITTLE32(temp[0]); /* fixed variable from nonce */
- ctx->X[CHACHA_IV_BYTES+2] = LITTLE32(temp[1]); /* counter from nonce */
- ctx->X[CHACHA_IV_BYTES+3] = LITTLE32(temp[2]); /* counter from nonce */
-
- return 0;
-}
-
-/* "expand 32-byte k" as unsigned 32 byte */
-static const word32 sigma[4] = {0x61707865, 0x3320646e, 0x79622d32, 0x6b206574};
-/* "expand 16-byte k" as unsigned 16 byte */
-static const word32 tau[4] = {0x61707865, 0x3120646e, 0x79622d36, 0x6b206574};
-
-/**
- * Key setup. 8 word iv (nonce)
- */
-int wc_Chacha_SetKey(ChaCha* ctx, const byte* key, word32 keySz)
-{
- const word32* constants;
- const byte* k;
-
-#ifdef XSTREAM_ALIGN
- word32 alignKey[8];
-#endif
-
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
- if (keySz != (CHACHA_MAX_KEY_SZ/2) && keySz != CHACHA_MAX_KEY_SZ)
- return BAD_FUNC_ARG;
-
-#ifdef XSTREAM_ALIGN
- if ((wolfssl_word)key % 4) {
- WOLFSSL_MSG("wc_ChachaSetKey unaligned key");
- XMEMCPY(alignKey, key, keySz);
- k = (byte*)alignKey;
- }
- else {
- k = key;
- }
-#else
- k = key;
-#endif /* XSTREAM_ALIGN */
-
-#ifdef CHACHA_AEAD_TEST
- word32 i;
- printf("ChaCha key used :\n");
- for (i = 0; i < keySz; i++) {
- printf("%02x", key[i]);
- if ((i + 1) % 8 == 0)
- printf("\n");
- }
- printf("\n\n");
-#endif
-
- ctx->X[4] = U8TO32_LITTLE(k + 0);
- ctx->X[5] = U8TO32_LITTLE(k + 4);
- ctx->X[6] = U8TO32_LITTLE(k + 8);
- ctx->X[7] = U8TO32_LITTLE(k + 12);
- if (keySz == CHACHA_MAX_KEY_SZ) {
- k += 16;
- constants = sigma;
- }
- else {
- constants = tau;
- }
- ctx->X[ 8] = U8TO32_LITTLE(k + 0);
- ctx->X[ 9] = U8TO32_LITTLE(k + 4);
- ctx->X[10] = U8TO32_LITTLE(k + 8);
- ctx->X[11] = U8TO32_LITTLE(k + 12);
- ctx->X[ 0] = constants[0];
- ctx->X[ 1] = constants[1];
- ctx->X[ 2] = constants[2];
- ctx->X[ 3] = constants[3];
- ctx->left = 0; /* resets state */
-
- return 0;
-}
-
-/**
- * Converts word into bytes with rotations having been done.
- */
-static WC_INLINE void wc_Chacha_wordtobyte(word32 output[CHACHA_CHUNK_WORDS],
- const word32 input[CHACHA_CHUNK_WORDS])
-{
- word32 x[CHACHA_CHUNK_WORDS];
- word32 i;
-
- for (i = 0; i < CHACHA_CHUNK_WORDS; i++) {
- x[i] = input[i];
- }
-
- for (i = (ROUNDS); i > 0; i -= 2) {
- QUARTERROUND(0, 4, 8, 12)
- QUARTERROUND(1, 5, 9, 13)
- QUARTERROUND(2, 6, 10, 14)
- QUARTERROUND(3, 7, 11, 15)
- QUARTERROUND(0, 5, 10, 15)
- QUARTERROUND(1, 6, 11, 12)
- QUARTERROUND(2, 7, 8, 13)
- QUARTERROUND(3, 4, 9, 14)
- }
-
- for (i = 0; i < CHACHA_CHUNK_WORDS; i++) {
- x[i] = PLUS(x[i], input[i]);
- }
-
- for (i = 0; i < CHACHA_CHUNK_WORDS; i++) {
- output[i] = LITTLE32(x[i]);
- }
-}
-
-#ifdef __cplusplus
- extern "C" {
-#endif
-
-extern void chacha_encrypt_x64(ChaCha* ctx, const byte* m, byte* c,
- word32 bytes);
-extern void chacha_encrypt_avx1(ChaCha* ctx, const byte* m, byte* c,
- word32 bytes);
-extern void chacha_encrypt_avx2(ChaCha* ctx, const byte* m, byte* c,
- word32 bytes);
-
-#ifdef __cplusplus
- } /* extern "C" */
-#endif
-
-
-/**
- * Encrypt a stream of bytes
- */
-static void wc_Chacha_encrypt_bytes(ChaCha* ctx, const byte* m, byte* c,
- word32 bytes)
-{
- byte* output;
- word32 temp[CHACHA_CHUNK_WORDS]; /* used to make sure aligned */
- word32 i;
-
- /* handle left overs */
- if (bytes > 0 && ctx->left > 0) {
- wc_Chacha_wordtobyte(temp, ctx->X); /* recreate the stream */
- output = (byte*)temp + CHACHA_CHUNK_BYTES - ctx->left;
- for (i = 0; i < bytes && i < ctx->left; i++) {
- c[i] = m[i] ^ output[i];
- }
- ctx->left = ctx->left - i;
-
- /* Used up all of the stream that was left, increment the counter */
- if (ctx->left == 0) {
- ctx->X[CHACHA_IV_BYTES] = PLUSONE(ctx->X[CHACHA_IV_BYTES]);
- }
- bytes = bytes - i;
- c += i;
- m += i;
- }
-
- output = (byte*)temp;
- while (bytes >= CHACHA_CHUNK_BYTES) {
- wc_Chacha_wordtobyte(temp, ctx->X);
- ctx->X[CHACHA_IV_BYTES] = PLUSONE(ctx->X[CHACHA_IV_BYTES]);
- for (i = 0; i < CHACHA_CHUNK_BYTES; ++i) {
- c[i] = m[i] ^ output[i];
- }
- bytes -= CHACHA_CHUNK_BYTES;
- c += CHACHA_CHUNK_BYTES;
- m += CHACHA_CHUNK_BYTES;
- }
-
- if (bytes) {
- /* in this case there will always be some left over since bytes is less
- * than CHACHA_CHUNK_BYTES, so do not increment counter after getting
- * stream in order for the stream to be recreated on next call */
- wc_Chacha_wordtobyte(temp, ctx->X);
- for (i = 0; i < bytes; ++i) {
- c[i] = m[i] ^ output[i];
- }
- ctx->left = CHACHA_CHUNK_BYTES - i;
- }
-}
-
-
-/**
- * API to encrypt/decrypt a message of any size.
- */
-int wc_Chacha_Process(ChaCha* ctx, byte* output, const byte* input,
- word32 msglen)
-{
- if (ctx == NULL)
- return BAD_FUNC_ARG;
-
-#ifdef USE_INTEL_CHACHA_SPEEDUP
- if (!cpuidFlagsSet) {
- cpuidFlags = cpuid_get_flags();
- cpuidFlagsSet = 1;
- }
-
- #ifdef HAVE_INTEL_AVX2
- if (IS_INTEL_AVX2(cpuidFlags)) {
- chacha_encrypt_avx2(ctx, input, output, msglen);
- return 0;
- }
- #endif
- if (IS_INTEL_AVX1(cpuidFlags)) {
- chacha_encrypt_avx1(ctx, input, output, msglen);
- return 0;
- }
- else {
- chacha_encrypt_x64(ctx, input, output, msglen);
- return 0;
- }
-#endif
- wc_Chacha_encrypt_bytes(ctx, input, output, msglen);
-
- return 0;
-}
-
-#endif /* HAVE_CHACHA*/
-
-#endif /* WOLFSSL_ARMASM */
diff --git a/client/wolfssl/wolfcrypt/src/chacha20_poly1305.c b/client/wolfssl/wolfcrypt/src/chacha20_poly1305.c
deleted file mode 100644
index 64bc4c1..0000000
--- a/client/wolfssl/wolfcrypt/src/chacha20_poly1305.c
+++ /dev/null
@@ -1,286 +0,0 @@
-/* chacha.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
-
-#include
-#include
-#include
-
-#ifdef NO_INLINE
-#include
-#else
-#define WOLFSSL_MISC_INCLUDED
-#include
-#endif
-
-#define CHACHA20_POLY1305_AEAD_INITIAL_COUNTER 0
-int wc_ChaCha20Poly1305_Encrypt(
- const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
- const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
- const byte* inAAD, const word32 inAADLen,
- const byte* inPlaintext, const word32 inPlaintextLen,
- byte* outCiphertext,
- byte outAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE])
-{
- int ret;
- ChaChaPoly_Aead aead;
-
- /* Validate function arguments */
- if (!inKey || !inIV ||
- !inPlaintext || !inPlaintextLen ||
- !outCiphertext ||
- !outAuthTag)
- {
- return BAD_FUNC_ARG;
- }
-
- ret = wc_ChaCha20Poly1305_Init(&aead, inKey, inIV,
- CHACHA20_POLY1305_AEAD_ENCRYPT);
- if (ret == 0)
- ret = wc_ChaCha20Poly1305_UpdateAad(&aead, inAAD, inAADLen);
- if (ret == 0)
- ret = wc_ChaCha20Poly1305_UpdateData(&aead, inPlaintext, outCiphertext,
- inPlaintextLen);
- if (ret == 0)
- ret = wc_ChaCha20Poly1305_Final(&aead, outAuthTag);
- return ret;
-}
-
-int wc_ChaCha20Poly1305_Decrypt(
- const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
- const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
- const byte* inAAD, const word32 inAADLen,
- const byte* inCiphertext, const word32 inCiphertextLen,
- const byte inAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE],
- byte* outPlaintext)
-{
- int ret;
- ChaChaPoly_Aead aead;
- byte calculatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
-
- /* Validate function arguments */
- if (!inKey || !inIV ||
- !inCiphertext || !inCiphertextLen ||
- !inAuthTag ||
- !outPlaintext)
- {
- return BAD_FUNC_ARG;
- }
-
- XMEMSET(calculatedAuthTag, 0, sizeof(calculatedAuthTag));
-
- ret = wc_ChaCha20Poly1305_Init(&aead, inKey, inIV,
- CHACHA20_POLY1305_AEAD_DECRYPT);
- if (ret == 0)
- ret = wc_ChaCha20Poly1305_UpdateAad(&aead, inAAD, inAADLen);
- if (ret == 0)
- ret = wc_ChaCha20Poly1305_UpdateData(&aead, inCiphertext, outPlaintext,
- inCiphertextLen);
- if (ret == 0)
- ret = wc_ChaCha20Poly1305_Final(&aead, calculatedAuthTag);
- if (ret == 0)
- ret = wc_ChaCha20Poly1305_CheckTag(inAuthTag, calculatedAuthTag);
- return ret;
-}
-
-int wc_ChaCha20Poly1305_CheckTag(
- const byte authTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE],
- const byte authTagChk[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE])
-{
- int ret = 0;
- if (authTag == NULL || authTagChk == NULL) {
- return BAD_FUNC_ARG;
- }
- if (ConstantCompare(authTag, authTagChk,
- CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE) != 0) {
- ret = MAC_CMP_FAILED_E;
- }
- return ret;
-}
-
-int wc_ChaCha20Poly1305_Init(ChaChaPoly_Aead* aead,
- const byte inKey[CHACHA20_POLY1305_AEAD_KEYSIZE],
- const byte inIV[CHACHA20_POLY1305_AEAD_IV_SIZE],
- int isEncrypt)
-{
- int ret;
- byte authKey[CHACHA20_POLY1305_AEAD_KEYSIZE];
-
- /* check arguments */
- if (aead == NULL || inKey == NULL || inIV == NULL) {
- return BAD_FUNC_ARG;
- }
-
- /* setup aead context */
- XMEMSET(aead, 0, sizeof(ChaChaPoly_Aead));
- XMEMSET(authKey, 0, sizeof(authKey));
- aead->isEncrypt = isEncrypt;
-
- /* Initialize the ChaCha20 context (key and iv) */
- ret = wc_Chacha_SetKey(&aead->chacha, inKey,
- CHACHA20_POLY1305_AEAD_KEYSIZE);
- if (ret == 0) {
- ret = wc_Chacha_SetIV(&aead->chacha, inIV,
- CHACHA20_POLY1305_AEAD_INITIAL_COUNTER);
- }
-
- /* Create the Poly1305 key */
- if (ret == 0) {
- ret = wc_Chacha_Process(&aead->chacha, authKey, authKey,
- CHACHA20_POLY1305_AEAD_KEYSIZE);
- }
-
- /* Initialize Poly1305 context */
- if (ret == 0) {
- ret = wc_Poly1305SetKey(&aead->poly, authKey,
- CHACHA20_POLY1305_AEAD_KEYSIZE);
- }
-
- /* advance counter by 1 after creating Poly1305 key */
- if (ret == 0) {
- ret = wc_Chacha_SetIV(&aead->chacha, inIV,
- CHACHA20_POLY1305_AEAD_INITIAL_COUNTER + 1);
- }
-
- if (ret == 0) {
- aead->state = CHACHA20_POLY1305_STATE_READY;
- }
-
- return ret;
-}
-
-/* optional additional authentication data */
-int wc_ChaCha20Poly1305_UpdateAad(ChaChaPoly_Aead* aead,
- const byte* inAAD, word32 inAADLen)
-{
- int ret = 0;
-
- if (aead == NULL || (inAAD == NULL && inAADLen > 0)) {
- return BAD_FUNC_ARG;
- }
- if (aead->state != CHACHA20_POLY1305_STATE_READY &&
- aead->state != CHACHA20_POLY1305_STATE_AAD) {
- return BAD_STATE_E;
- }
-
- if (inAAD && inAADLen > 0) {
- ret = wc_Poly1305Update(&aead->poly, inAAD, inAADLen);
- if (ret == 0) {
- aead->aadLen += inAADLen;
- aead->state = CHACHA20_POLY1305_STATE_AAD;
- }
- }
-
- return ret;
-}
-
-/* inData and outData can be same pointer (inline) */
-int wc_ChaCha20Poly1305_UpdateData(ChaChaPoly_Aead* aead,
- const byte* inData, byte* outData, word32 dataLen)
-{
- int ret = 0;
-
- if (aead == NULL || inData == NULL || outData == NULL) {
- return BAD_FUNC_ARG;
- }
- if (aead->state != CHACHA20_POLY1305_STATE_READY &&
- aead->state != CHACHA20_POLY1305_STATE_AAD &&
- aead->state != CHACHA20_POLY1305_STATE_DATA) {
- return BAD_STATE_E;
- }
-
- /* Pad the AAD */
- if (aead->state == CHACHA20_POLY1305_STATE_AAD) {
- ret = wc_Poly1305_Pad(&aead->poly, aead->aadLen);
- }
-
- /* advance state */
- aead->state = CHACHA20_POLY1305_STATE_DATA;
-
- /* Perform ChaCha20 encrypt/decrypt and Poly1305 auth calc */
- if (ret == 0) {
- if (aead->isEncrypt) {
- ret = wc_Chacha_Process(&aead->chacha, outData, inData, dataLen);
- if (ret == 0)
- ret = wc_Poly1305Update(&aead->poly, outData, dataLen);
- }
- else {
- ret = wc_Poly1305Update(&aead->poly, inData, dataLen);
- if (ret == 0)
- ret = wc_Chacha_Process(&aead->chacha, outData, inData, dataLen);
- }
- }
- if (ret == 0) {
- aead->dataLen += dataLen;
- }
- return ret;
-}
-
-int wc_ChaCha20Poly1305_Final(ChaChaPoly_Aead* aead,
- byte outAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE])
-{
- int ret = 0;
-
- if (aead == NULL || outAuthTag == NULL) {
- return BAD_FUNC_ARG;
- }
- if (aead->state != CHACHA20_POLY1305_STATE_AAD &&
- aead->state != CHACHA20_POLY1305_STATE_DATA) {
- return BAD_STATE_E;
- }
-
- /* Pad the AAD - Make sure it is done */
- if (aead->state == CHACHA20_POLY1305_STATE_AAD) {
- ret = wc_Poly1305_Pad(&aead->poly, aead->aadLen);
- }
-
- /* Pad the ciphertext to 16 bytes */
- if (ret == 0) {
- ret = wc_Poly1305_Pad(&aead->poly, aead->dataLen);
- }
-
- /* Add the aad length and plaintext/ciphertext length */
- if (ret == 0) {
- ret = wc_Poly1305_EncodeSizes(&aead->poly, aead->aadLen,
- aead->dataLen);
- }
-
- /* Finalize the auth tag */
- if (ret == 0) {
- ret = wc_Poly1305Final(&aead->poly, outAuthTag);
- }
-
- /* reset and cleanup sensitive context */
- ForceZero(aead, sizeof(ChaChaPoly_Aead));
-
- return ret;
-}
-
-#endif /* HAVE_CHACHA && HAVE_POLY1305 */
diff --git a/client/wolfssl/wolfcrypt/src/chacha_asm.S b/client/wolfssl/wolfcrypt/src/chacha_asm.S
deleted file mode 100644
index f9d5fff..0000000
--- a/client/wolfssl/wolfcrypt/src/chacha_asm.S
+++ /dev/null
@@ -1,1420 +0,0 @@
-/* chacha_asm
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-#ifndef HAVE_INTEL_AVX1
-#define HAVE_INTEL_AVX1
-#endif /* HAVE_INTEL_AVX1 */
-#ifndef NO_AVX2_SUPPORT
-#define HAVE_INTEL_AVX2
-#endif /* NO_AVX2_SUPPORT */
-
-#ifndef __APPLE__
-.text
-.globl chacha_encrypt_x64
-.type chacha_encrypt_x64,@function
-.align 4
-chacha_encrypt_x64:
-#else
-.section __TEXT,__text
-.globl _chacha_encrypt_x64
-.p2align 2
-_chacha_encrypt_x64:
-#endif /* __APPLE__ */
- pushq %rbx
- pushq %rbp
- pushq %r12
- pushq %r13
- pushq %r14
- pushq %r15
- subq $0x40, %rsp
- cmpl $0x40, %ecx
- jl L_chacha_x64_small
-L_chacha_x64_start:
- subq $48, %rsp
- movq %rdx, 24(%rsp)
- movq %rsi, 32(%rsp)
- movq %rcx, 40(%rsp)
- movq 32(%rdi), %rax
- movq 40(%rdi), %rbx
- movq %rax, 8(%rsp)
- movq %rbx, 16(%rsp)
- movl (%rdi), %eax
- movl 4(%rdi), %ebx
- movl 8(%rdi), %ecx
- movl 12(%rdi), %edx
- movl 16(%rdi), %r8d
- movl 20(%rdi), %r9d
- movl 24(%rdi), %r10d
- movl 28(%rdi), %r11d
- movl 48(%rdi), %r12d
- movl 52(%rdi), %r13d
- movl 56(%rdi), %r14d
- movl 60(%rdi), %r15d
- movb $10, (%rsp)
- movl 8(%rsp), %esi
- movl 12(%rsp), %ebp
-L_chacha_x64_block_crypt_start:
- addl %r8d, %eax
- addl %r9d, %ebx
- xorl %eax, %r12d
- xorl %ebx, %r13d
- roll $16, %r12d
- roll $16, %r13d
- addl %r12d, %esi
- addl %r13d, %ebp
- xorl %esi, %r8d
- xorl %ebp, %r9d
- roll $12, %r8d
- roll $12, %r9d
- addl %r8d, %eax
- addl %r9d, %ebx
- xorl %eax, %r12d
- xorl %ebx, %r13d
- roll $8, %r12d
- roll $8, %r13d
- addl %r12d, %esi
- addl %r13d, %ebp
- xorl %esi, %r8d
- xorl %ebp, %r9d
- roll $7, %r8d
- roll $7, %r9d
- movl %esi, 8(%rsp)
- movl %ebp, 12(%rsp)
- movl 16(%rsp), %esi
- movl 20(%rsp), %ebp
- addl %r10d, %ecx
- addl %r11d, %edx
- xorl %ecx, %r14d
- xorl %edx, %r15d
- roll $16, %r14d
- roll $16, %r15d
- addl %r14d, %esi
- addl %r15d, %ebp
- xorl %esi, %r10d
- xorl %ebp, %r11d
- roll $12, %r10d
- roll $12, %r11d
- addl %r10d, %ecx
- addl %r11d, %edx
- xorl %ecx, %r14d
- xorl %edx, %r15d
- roll $8, %r14d
- roll $8, %r15d
- addl %r14d, %esi
- addl %r15d, %ebp
- xorl %esi, %r10d
- xorl %ebp, %r11d
- roll $7, %r10d
- roll $7, %r11d
- addl %r9d, %eax
- addl %r10d, %ebx
- xorl %eax, %r15d
- xorl %ebx, %r12d
- roll $16, %r15d
- roll $16, %r12d
- addl %r15d, %esi
- addl %r12d, %ebp
- xorl %esi, %r9d
- xorl %ebp, %r10d
- roll $12, %r9d
- roll $12, %r10d
- addl %r9d, %eax
- addl %r10d, %ebx
- xorl %eax, %r15d
- xorl %ebx, %r12d
- roll $8, %r15d
- roll $8, %r12d
- addl %r15d, %esi
- addl %r12d, %ebp
- xorl %esi, %r9d
- xorl %ebp, %r10d
- roll $7, %r9d
- roll $7, %r10d
- movl %esi, 16(%rsp)
- movl %ebp, 20(%rsp)
- movl 8(%rsp), %esi
- movl 12(%rsp), %ebp
- addl %r11d, %ecx
- addl %r8d, %edx
- xorl %ecx, %r13d
- xorl %edx, %r14d
- roll $16, %r13d
- roll $16, %r14d
- addl %r13d, %esi
- addl %r14d, %ebp
- xorl %esi, %r11d
- xorl %ebp, %r8d
- roll $12, %r11d
- roll $12, %r8d
- addl %r11d, %ecx
- addl %r8d, %edx
- xorl %ecx, %r13d
- xorl %edx, %r14d
- roll $8, %r13d
- roll $8, %r14d
- addl %r13d, %esi
- addl %r14d, %ebp
- xorl %esi, %r11d
- xorl %ebp, %r8d
- roll $7, %r11d
- roll $7, %r8d
- decb (%rsp)
- jnz L_chacha_x64_block_crypt_start
- movl %esi, 8(%rsp)
- movl %ebp, 12(%rsp)
- movq 32(%rsp), %rsi
- movq 24(%rsp), %rbp
- addl (%rdi), %eax
- addl 4(%rdi), %ebx
- addl 8(%rdi), %ecx
- addl 12(%rdi), %edx
- addl 16(%rdi), %r8d
- addl 20(%rdi), %r9d
- addl 24(%rdi), %r10d
- addl 28(%rdi), %r11d
- addl 48(%rdi), %r12d
- addl 52(%rdi), %r13d
- addl 56(%rdi), %r14d
- addl 60(%rdi), %r15d
- xorl (%rsi), %eax
- xorl 4(%rsi), %ebx
- xorl 8(%rsi), %ecx
- xorl 12(%rsi), %edx
- xorl 16(%rsi), %r8d
- xorl 20(%rsi), %r9d
- xorl 24(%rsi), %r10d
- xorl 28(%rsi), %r11d
- xorl 48(%rsi), %r12d
- xorl 52(%rsi), %r13d
- xorl 56(%rsi), %r14d
- xorl 60(%rsi), %r15d
- movl %eax, (%rbp)
- movl %ebx, 4(%rbp)
- movl %ecx, 8(%rbp)
- movl %edx, 12(%rbp)
- movl %r8d, 16(%rbp)
- movl %r9d, 20(%rbp)
- movl %r10d, 24(%rbp)
- movl %r11d, 28(%rbp)
- movl %r12d, 48(%rbp)
- movl %r13d, 52(%rbp)
- movl %r14d, 56(%rbp)
- movl %r15d, 60(%rbp)
- movl 8(%rsp), %eax
- movl 12(%rsp), %ebx
- movl 16(%rsp), %ecx
- movl 20(%rsp), %edx
- addl 32(%rdi), %eax
- addl 36(%rdi), %ebx
- addl 40(%rdi), %ecx
- addl 44(%rdi), %edx
- xorl 32(%rsi), %eax
- xorl 36(%rsi), %ebx
- xorl 40(%rsi), %ecx
- xorl 44(%rsi), %edx
- movl %eax, 32(%rbp)
- movl %ebx, 36(%rbp)
- movl %ecx, 40(%rbp)
- movl %edx, 44(%rbp)
- movq 24(%rsp), %rdx
- movq 40(%rsp), %rcx
- addl $0x01, 48(%rdi)
- addq $48, %rsp
- subl $0x40, %ecx
- addq $0x40, %rsi
- addq $0x40, %rdx
- cmpl $0x40, %ecx
- jge L_chacha_x64_start
-L_chacha_x64_small:
- cmpl $0x00, %ecx
- je L_chacha_x64_done
- subq $48, %rsp
- movq %rdx, 24(%rsp)
- movq %rsi, 32(%rsp)
- movq %rcx, 40(%rsp)
- movq 32(%rdi), %rax
- movq 40(%rdi), %rbx
- movq %rax, 8(%rsp)
- movq %rbx, 16(%rsp)
- movl (%rdi), %eax
- movl 4(%rdi), %ebx
- movl 8(%rdi), %ecx
- movl 12(%rdi), %edx
- movl 16(%rdi), %r8d
- movl 20(%rdi), %r9d
- movl 24(%rdi), %r10d
- movl 28(%rdi), %r11d
- movl 48(%rdi), %r12d
- movl 52(%rdi), %r13d
- movl 56(%rdi), %r14d
- movl 60(%rdi), %r15d
- movb $10, (%rsp)
- movl 8(%rsp), %esi
- movl 12(%rsp), %ebp
-L_chacha_x64_partial_crypt_start:
- addl %r8d, %eax
- addl %r9d, %ebx
- xorl %eax, %r12d
- xorl %ebx, %r13d
- roll $16, %r12d
- roll $16, %r13d
- addl %r12d, %esi
- addl %r13d, %ebp
- xorl %esi, %r8d
- xorl %ebp, %r9d
- roll $12, %r8d
- roll $12, %r9d
- addl %r8d, %eax
- addl %r9d, %ebx
- xorl %eax, %r12d
- xorl %ebx, %r13d
- roll $8, %r12d
- roll $8, %r13d
- addl %r12d, %esi
- addl %r13d, %ebp
- xorl %esi, %r8d
- xorl %ebp, %r9d
- roll $7, %r8d
- roll $7, %r9d
- movl %esi, 8(%rsp)
- movl %ebp, 12(%rsp)
- movl 16(%rsp), %esi
- movl 20(%rsp), %ebp
- addl %r10d, %ecx
- addl %r11d, %edx
- xorl %ecx, %r14d
- xorl %edx, %r15d
- roll $16, %r14d
- roll $16, %r15d
- addl %r14d, %esi
- addl %r15d, %ebp
- xorl %esi, %r10d
- xorl %ebp, %r11d
- roll $12, %r10d
- roll $12, %r11d
- addl %r10d, %ecx
- addl %r11d, %edx
- xorl %ecx, %r14d
- xorl %edx, %r15d
- roll $8, %r14d
- roll $8, %r15d
- addl %r14d, %esi
- addl %r15d, %ebp
- xorl %esi, %r10d
- xorl %ebp, %r11d
- roll $7, %r10d
- roll $7, %r11d
- addl %r9d, %eax
- addl %r10d, %ebx
- xorl %eax, %r15d
- xorl %ebx, %r12d
- roll $16, %r15d
- roll $16, %r12d
- addl %r15d, %esi
- addl %r12d, %ebp
- xorl %esi, %r9d
- xorl %ebp, %r10d
- roll $12, %r9d
- roll $12, %r10d
- addl %r9d, %eax
- addl %r10d, %ebx
- xorl %eax, %r15d
- xorl %ebx, %r12d
- roll $8, %r15d
- roll $8, %r12d
- addl %r15d, %esi
- addl %r12d, %ebp
- xorl %esi, %r9d
- xorl %ebp, %r10d
- roll $7, %r9d
- roll $7, %r10d
- movl %esi, 16(%rsp)
- movl %ebp, 20(%rsp)
- movl 8(%rsp), %esi
- movl 12(%rsp), %ebp
- addl %r11d, %ecx
- addl %r8d, %edx
- xorl %ecx, %r13d
- xorl %edx, %r14d
- roll $16, %r13d
- roll $16, %r14d
- addl %r13d, %esi
- addl %r14d, %ebp
- xorl %esi, %r11d
- xorl %ebp, %r8d
- roll $12, %r11d
- roll $12, %r8d
- addl %r11d, %ecx
- addl %r8d, %edx
- xorl %ecx, %r13d
- xorl %edx, %r14d
- roll $8, %r13d
- roll $8, %r14d
- addl %r13d, %esi
- addl %r14d, %ebp
- xorl %esi, %r11d
- xorl %ebp, %r8d
- roll $7, %r11d
- roll $7, %r8d
- decb (%rsp)
- jnz L_chacha_x64_partial_crypt_start
- movl %esi, 8(%rsp)
- movl %ebp, 12(%rsp)
- movq 32(%rsp), %rsi
- addl (%rdi), %eax
- addl 4(%rdi), %ebx
- addl 8(%rdi), %ecx
- addl 12(%rdi), %edx
- addl 16(%rdi), %r8d
- addl 20(%rdi), %r9d
- addl 24(%rdi), %r10d
- addl 28(%rdi), %r11d
- addl 48(%rdi), %r12d
- addl 52(%rdi), %r13d
- addl 56(%rdi), %r14d
- addl 60(%rdi), %r15d
- movl %eax, 48(%rsp)
- movl %ebx, 52(%rsp)
- movl %ecx, 56(%rsp)
- movl %edx, 60(%rsp)
- movl %r8d, 64(%rsp)
- movl %r9d, 68(%rsp)
- movl %r10d, 72(%rsp)
- movl %r11d, 76(%rsp)
- movl %r12d, 96(%rsp)
- movl %r13d, 100(%rsp)
- movl %r14d, 104(%rsp)
- movl %r15d, 108(%rsp)
- movl 8(%rsp), %eax
- movl 12(%rsp), %ebx
- movl 16(%rsp), %ecx
- movl 20(%rsp), %edx
- addl 32(%rdi), %eax
- addl 36(%rdi), %ebx
- addl 40(%rdi), %ecx
- addl 44(%rdi), %edx
- movl %eax, 80(%rsp)
- movl %ebx, 84(%rsp)
- movl %ecx, 88(%rsp)
- movl %edx, 92(%rsp)
- movq 24(%rsp), %rdx
- movq 40(%rsp), %rcx
- addl $0x01, 48(%rdi)
- addq $48, %rsp
- movl %ecx, %r8d
- xorq %rbx, %rbx
- andl $7, %r8d
- jz L_chacha_x64_partial_start64
-L_chacha_x64_partial_start8:
- movzbl (%rsp,%rbx,1), %eax
- xorb (%rsi,%rbx,1), %al
- movb %al, (%rdx,%rbx,1)
- incl %ebx
- cmpl %r8d, %ebx
- jne L_chacha_x64_partial_start8
- je L_chacha_x64_partial_end64
-L_chacha_x64_partial_start64:
- movq (%rsp,%rbx,1), %rax
- xorq (%rsi,%rbx,1), %rax
- movq %rax, (%rdx,%rbx,1)
- addl $8, %ebx
-L_chacha_x64_partial_end64:
- cmpl %ecx, %ebx
- jne L_chacha_x64_partial_start64
-L_chacha_x64_done:
- addq $0x40, %rsp
- popq %r15
- popq %r14
- popq %r13
- popq %r12
- popq %rbp
- popq %rbx
- repz retq
-#ifndef __APPLE__
-.size chacha_encrypt_x64,.-chacha_encrypt_x64
-#endif /* __APPLE__ */
-#ifdef HAVE_INTEL_AVX1
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_chacha20_avx1_rotl8:
-.quad 0x605040702010003, 0xe0d0c0f0a09080b
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_chacha20_avx1_rotl16:
-.quad 0x504070601000302, 0xd0c0f0e09080b0a
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_chacha20_avx1_add:
-.quad 0x100000000, 0x300000002
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 16
-#else
-.p2align 4
-#endif /* __APPLE__ */
-L_chacha20_avx1_four:
-.quad 0x400000004, 0x400000004
-#ifndef __APPLE__
-.text
-.globl chacha_encrypt_avx1
-.type chacha_encrypt_avx1,@function
-.align 4
-chacha_encrypt_avx1:
-#else
-.section __TEXT,__text
-.globl _chacha_encrypt_avx1
-.p2align 2
-_chacha_encrypt_avx1:
-#endif /* __APPLE__ */
- subq $0x190, %rsp
- movq %rsp, %r9
- leaq 256(%rsp), %r10
- andq $-16, %r9
- andq $-16, %r10
- movl %ecx, %eax
- shrl $8, %eax
- jz L_chacha20_avx1_end128
- vpshufd $0x00, (%rdi), %xmm0
- vpshufd $0x00, 4(%rdi), %xmm1
- vpshufd $0x00, 8(%rdi), %xmm2
- vpshufd $0x00, 12(%rdi), %xmm3
- vpshufd $0x00, 16(%rdi), %xmm4
- vpshufd $0x00, 20(%rdi), %xmm5
- vpshufd $0x00, 24(%rdi), %xmm6
- vpshufd $0x00, 28(%rdi), %xmm7
- vpshufd $0x00, 32(%rdi), %xmm8
- vpshufd $0x00, 36(%rdi), %xmm9
- vpshufd $0x00, 40(%rdi), %xmm10
- vpshufd $0x00, 44(%rdi), %xmm11
- vpshufd $0x00, 48(%rdi), %xmm12
- vpshufd $0x00, 52(%rdi), %xmm13
- vpshufd $0x00, 56(%rdi), %xmm14
- vpshufd $0x00, 60(%rdi), %xmm15
- vpaddd L_chacha20_avx1_add(%rip), %xmm12, %xmm12
- vmovdqa %xmm0, (%r9)
- vmovdqa %xmm1, 16(%r9)
- vmovdqa %xmm2, 32(%r9)
- vmovdqa %xmm3, 48(%r9)
- vmovdqa %xmm4, 64(%r9)
- vmovdqa %xmm5, 80(%r9)
- vmovdqa %xmm6, 96(%r9)
- vmovdqa %xmm7, 112(%r9)
- vmovdqa %xmm8, 128(%r9)
- vmovdqa %xmm9, 144(%r9)
- vmovdqa %xmm10, 160(%r9)
- vmovdqa %xmm11, 176(%r9)
- vmovdqa %xmm12, 192(%r9)
- vmovdqa %xmm13, 208(%r9)
- vmovdqa %xmm14, 224(%r9)
- vmovdqa %xmm15, 240(%r9)
-L_chacha20_avx1_start128:
- vmovdqa %xmm11, 48(%r10)
- movb $10, %r8b
-L_chacha20_avx1_loop128:
- vpaddd %xmm4, %xmm0, %xmm0
- vpxor %xmm0, %xmm12, %xmm12
- vmovdqa 48(%r10), %xmm11
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm12, %xmm12
- vpaddd %xmm12, %xmm8, %xmm8
- vpxor %xmm8, %xmm4, %xmm4
- vpaddd %xmm5, %xmm1, %xmm1
- vpxor %xmm1, %xmm13, %xmm13
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm13, %xmm13
- vpaddd %xmm13, %xmm9, %xmm9
- vpxor %xmm9, %xmm5, %xmm5
- vpaddd %xmm6, %xmm2, %xmm2
- vpxor %xmm2, %xmm14, %xmm14
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm14, %xmm14
- vpaddd %xmm14, %xmm10, %xmm10
- vpxor %xmm10, %xmm6, %xmm6
- vpaddd %xmm7, %xmm3, %xmm3
- vpxor %xmm3, %xmm15, %xmm15
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm15, %xmm15
- vpaddd %xmm15, %xmm11, %xmm11
- vpxor %xmm11, %xmm7, %xmm7
- vmovdqa %xmm11, 48(%r10)
- vpsrld $20, %xmm4, %xmm11
- vpslld $12, %xmm4, %xmm4
- vpxor %xmm11, %xmm4, %xmm4
- vpsrld $20, %xmm5, %xmm11
- vpslld $12, %xmm5, %xmm5
- vpxor %xmm11, %xmm5, %xmm5
- vpsrld $20, %xmm6, %xmm11
- vpslld $12, %xmm6, %xmm6
- vpxor %xmm11, %xmm6, %xmm6
- vpsrld $20, %xmm7, %xmm11
- vpslld $12, %xmm7, %xmm7
- vpxor %xmm11, %xmm7, %xmm7
- vpaddd %xmm4, %xmm0, %xmm0
- vpxor %xmm0, %xmm12, %xmm12
- vmovdqa 48(%r10), %xmm11
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm12, %xmm12
- vpaddd %xmm12, %xmm8, %xmm8
- vpxor %xmm8, %xmm4, %xmm4
- vpaddd %xmm5, %xmm1, %xmm1
- vpxor %xmm1, %xmm13, %xmm13
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm13, %xmm13
- vpaddd %xmm13, %xmm9, %xmm9
- vpxor %xmm9, %xmm5, %xmm5
- vpaddd %xmm6, %xmm2, %xmm2
- vpxor %xmm2, %xmm14, %xmm14
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm14, %xmm14
- vpaddd %xmm14, %xmm10, %xmm10
- vpxor %xmm10, %xmm6, %xmm6
- vpaddd %xmm7, %xmm3, %xmm3
- vpxor %xmm3, %xmm15, %xmm15
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm15, %xmm15
- vpaddd %xmm15, %xmm11, %xmm11
- vpxor %xmm11, %xmm7, %xmm7
- vmovdqa %xmm11, 48(%r10)
- vpsrld $25, %xmm4, %xmm11
- vpslld $7, %xmm4, %xmm4
- vpxor %xmm11, %xmm4, %xmm4
- vpsrld $25, %xmm5, %xmm11
- vpslld $7, %xmm5, %xmm5
- vpxor %xmm11, %xmm5, %xmm5
- vpsrld $25, %xmm6, %xmm11
- vpslld $7, %xmm6, %xmm6
- vpxor %xmm11, %xmm6, %xmm6
- vpsrld $25, %xmm7, %xmm11
- vpslld $7, %xmm7, %xmm7
- vpxor %xmm11, %xmm7, %xmm7
- vpaddd %xmm5, %xmm0, %xmm0
- vpxor %xmm0, %xmm15, %xmm15
- vmovdqa 48(%r10), %xmm11
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm15, %xmm15
- vpaddd %xmm15, %xmm10, %xmm10
- vpxor %xmm10, %xmm5, %xmm5
- vpaddd %xmm6, %xmm1, %xmm1
- vpxor %xmm1, %xmm12, %xmm12
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm12, %xmm12
- vpaddd %xmm12, %xmm11, %xmm11
- vpxor %xmm11, %xmm6, %xmm6
- vpaddd %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm13, %xmm13
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm13, %xmm13
- vpaddd %xmm13, %xmm8, %xmm8
- vpxor %xmm8, %xmm7, %xmm7
- vpaddd %xmm4, %xmm3, %xmm3
- vpxor %xmm3, %xmm14, %xmm14
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm14, %xmm14
- vpaddd %xmm14, %xmm9, %xmm9
- vpxor %xmm9, %xmm4, %xmm4
- vmovdqa %xmm11, 48(%r10)
- vpsrld $20, %xmm5, %xmm11
- vpslld $12, %xmm5, %xmm5
- vpxor %xmm11, %xmm5, %xmm5
- vpsrld $20, %xmm6, %xmm11
- vpslld $12, %xmm6, %xmm6
- vpxor %xmm11, %xmm6, %xmm6
- vpsrld $20, %xmm7, %xmm11
- vpslld $12, %xmm7, %xmm7
- vpxor %xmm11, %xmm7, %xmm7
- vpsrld $20, %xmm4, %xmm11
- vpslld $12, %xmm4, %xmm4
- vpxor %xmm11, %xmm4, %xmm4
- vpaddd %xmm5, %xmm0, %xmm0
- vpxor %xmm0, %xmm15, %xmm15
- vmovdqa 48(%r10), %xmm11
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm15, %xmm15
- vpaddd %xmm15, %xmm10, %xmm10
- vpxor %xmm10, %xmm5, %xmm5
- vpaddd %xmm6, %xmm1, %xmm1
- vpxor %xmm1, %xmm12, %xmm12
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm12, %xmm12
- vpaddd %xmm12, %xmm11, %xmm11
- vpxor %xmm11, %xmm6, %xmm6
- vpaddd %xmm7, %xmm2, %xmm2
- vpxor %xmm2, %xmm13, %xmm13
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm13, %xmm13
- vpaddd %xmm13, %xmm8, %xmm8
- vpxor %xmm8, %xmm7, %xmm7
- vpaddd %xmm4, %xmm3, %xmm3
- vpxor %xmm3, %xmm14, %xmm14
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm14, %xmm14
- vpaddd %xmm14, %xmm9, %xmm9
- vpxor %xmm9, %xmm4, %xmm4
- vmovdqa %xmm11, 48(%r10)
- vpsrld $25, %xmm5, %xmm11
- vpslld $7, %xmm5, %xmm5
- vpxor %xmm11, %xmm5, %xmm5
- vpsrld $25, %xmm6, %xmm11
- vpslld $7, %xmm6, %xmm6
- vpxor %xmm11, %xmm6, %xmm6
- vpsrld $25, %xmm7, %xmm11
- vpslld $7, %xmm7, %xmm7
- vpxor %xmm11, %xmm7, %xmm7
- vpsrld $25, %xmm4, %xmm11
- vpslld $7, %xmm4, %xmm4
- vpxor %xmm11, %xmm4, %xmm4
- decb %r8b
- jnz L_chacha20_avx1_loop128
- vmovdqa 48(%r10), %xmm11
- vpaddd (%r9), %xmm0, %xmm0
- vpaddd 16(%r9), %xmm1, %xmm1
- vpaddd 32(%r9), %xmm2, %xmm2
- vpaddd 48(%r9), %xmm3, %xmm3
- vpaddd 64(%r9), %xmm4, %xmm4
- vpaddd 80(%r9), %xmm5, %xmm5
- vpaddd 96(%r9), %xmm6, %xmm6
- vpaddd 112(%r9), %xmm7, %xmm7
- vpaddd 128(%r9), %xmm8, %xmm8
- vpaddd 144(%r9), %xmm9, %xmm9
- vpaddd 160(%r9), %xmm10, %xmm10
- vpaddd 176(%r9), %xmm11, %xmm11
- vpaddd 192(%r9), %xmm12, %xmm12
- vpaddd 208(%r9), %xmm13, %xmm13
- vpaddd 224(%r9), %xmm14, %xmm14
- vpaddd 240(%r9), %xmm15, %xmm15
- vmovdqa %xmm8, (%r10)
- vmovdqa %xmm9, 16(%r10)
- vmovdqa %xmm10, 32(%r10)
- vmovdqa %xmm11, 48(%r10)
- vmovdqa %xmm12, 64(%r10)
- vmovdqa %xmm13, 80(%r10)
- vmovdqa %xmm14, 96(%r10)
- vmovdqa %xmm15, 112(%r10)
- vpunpckldq %xmm1, %xmm0, %xmm8
- vpunpckldq %xmm3, %xmm2, %xmm9
- vpunpckhdq %xmm1, %xmm0, %xmm12
- vpunpckhdq %xmm3, %xmm2, %xmm13
- vpunpckldq %xmm5, %xmm4, %xmm10
- vpunpckldq %xmm7, %xmm6, %xmm11
- vpunpckhdq %xmm5, %xmm4, %xmm14
- vpunpckhdq %xmm7, %xmm6, %xmm15
- vpunpcklqdq %xmm9, %xmm8, %xmm0
- vpunpcklqdq %xmm11, %xmm10, %xmm1
- vpunpckhqdq %xmm9, %xmm8, %xmm2
- vpunpckhqdq %xmm11, %xmm10, %xmm3
- vpunpcklqdq %xmm13, %xmm12, %xmm4
- vpunpcklqdq %xmm15, %xmm14, %xmm5
- vpunpckhqdq %xmm13, %xmm12, %xmm6
- vpunpckhqdq %xmm15, %xmm14, %xmm7
- vmovdqu (%rsi), %xmm8
- vmovdqu 16(%rsi), %xmm9
- vmovdqu 64(%rsi), %xmm10
- vmovdqu 80(%rsi), %xmm11
- vmovdqu 128(%rsi), %xmm12
- vmovdqu 144(%rsi), %xmm13
- vmovdqu 192(%rsi), %xmm14
- vmovdqu 208(%rsi), %xmm15
- vpxor %xmm8, %xmm0, %xmm0
- vpxor %xmm9, %xmm1, %xmm1
- vpxor %xmm10, %xmm2, %xmm2
- vpxor %xmm11, %xmm3, %xmm3
- vpxor %xmm12, %xmm4, %xmm4
- vpxor %xmm13, %xmm5, %xmm5
- vpxor %xmm14, %xmm6, %xmm6
- vpxor %xmm15, %xmm7, %xmm7
- vmovdqu %xmm0, (%rdx)
- vmovdqu %xmm1, 16(%rdx)
- vmovdqu %xmm2, 64(%rdx)
- vmovdqu %xmm3, 80(%rdx)
- vmovdqu %xmm4, 128(%rdx)
- vmovdqu %xmm5, 144(%rdx)
- vmovdqu %xmm6, 192(%rdx)
- vmovdqu %xmm7, 208(%rdx)
- vmovdqa (%r10), %xmm0
- vmovdqa 16(%r10), %xmm1
- vmovdqa 32(%r10), %xmm2
- vmovdqa 48(%r10), %xmm3
- vmovdqa 64(%r10), %xmm4
- vmovdqa 80(%r10), %xmm5
- vmovdqa 96(%r10), %xmm6
- vmovdqa 112(%r10), %xmm7
- vpunpckldq %xmm1, %xmm0, %xmm8
- vpunpckldq %xmm3, %xmm2, %xmm9
- vpunpckhdq %xmm1, %xmm0, %xmm12
- vpunpckhdq %xmm3, %xmm2, %xmm13
- vpunpckldq %xmm5, %xmm4, %xmm10
- vpunpckldq %xmm7, %xmm6, %xmm11
- vpunpckhdq %xmm5, %xmm4, %xmm14
- vpunpckhdq %xmm7, %xmm6, %xmm15
- vpunpcklqdq %xmm9, %xmm8, %xmm0
- vpunpcklqdq %xmm11, %xmm10, %xmm1
- vpunpckhqdq %xmm9, %xmm8, %xmm2
- vpunpckhqdq %xmm11, %xmm10, %xmm3
- vpunpcklqdq %xmm13, %xmm12, %xmm4
- vpunpcklqdq %xmm15, %xmm14, %xmm5
- vpunpckhqdq %xmm13, %xmm12, %xmm6
- vpunpckhqdq %xmm15, %xmm14, %xmm7
- vmovdqu 32(%rsi), %xmm8
- vmovdqu 48(%rsi), %xmm9
- vmovdqu 96(%rsi), %xmm10
- vmovdqu 112(%rsi), %xmm11
- vmovdqu 160(%rsi), %xmm12
- vmovdqu 176(%rsi), %xmm13
- vmovdqu 224(%rsi), %xmm14
- vmovdqu 240(%rsi), %xmm15
- vpxor %xmm8, %xmm0, %xmm0
- vpxor %xmm9, %xmm1, %xmm1
- vpxor %xmm10, %xmm2, %xmm2
- vpxor %xmm11, %xmm3, %xmm3
- vpxor %xmm12, %xmm4, %xmm4
- vpxor %xmm13, %xmm5, %xmm5
- vpxor %xmm14, %xmm6, %xmm6
- vpxor %xmm15, %xmm7, %xmm7
- vmovdqu %xmm0, 32(%rdx)
- vmovdqu %xmm1, 48(%rdx)
- vmovdqu %xmm2, 96(%rdx)
- vmovdqu %xmm3, 112(%rdx)
- vmovdqu %xmm4, 160(%rdx)
- vmovdqu %xmm5, 176(%rdx)
- vmovdqu %xmm6, 224(%rdx)
- vmovdqu %xmm7, 240(%rdx)
- vmovdqa 192(%r9), %xmm12
- addq $0x100, %rsi
- addq $0x100, %rdx
- vpaddd L_chacha20_avx1_four(%rip), %xmm12, %xmm12
- subl $0x100, %ecx
- vmovdqa %xmm12, 192(%r9)
- cmpl $0x100, %ecx
- jl L_chacha20_avx1_done128
- vmovdqa (%r9), %xmm0
- vmovdqa 16(%r9), %xmm1
- vmovdqa 32(%r9), %xmm2
- vmovdqa 48(%r9), %xmm3
- vmovdqa 64(%r9), %xmm4
- vmovdqa 80(%r9), %xmm5
- vmovdqa 96(%r9), %xmm6
- vmovdqa 112(%r9), %xmm7
- vmovdqa 128(%r9), %xmm8
- vmovdqa 144(%r9), %xmm9
- vmovdqa 160(%r9), %xmm10
- vmovdqa 176(%r9), %xmm11
- vmovdqa 192(%r9), %xmm12
- vmovdqa 208(%r9), %xmm13
- vmovdqa 224(%r9), %xmm14
- vmovdqa 240(%r9), %xmm15
- jmp L_chacha20_avx1_start128
-L_chacha20_avx1_done128:
- shl $2, %eax
- addl %eax, 48(%rdi)
-L_chacha20_avx1_end128:
- cmpl $0x40, %ecx
- jl L_chacha20_avx1_block_done
-L_chacha20_avx1_block_start:
- vmovdqu (%rdi), %xmm0
- vmovdqu 16(%rdi), %xmm1
- vmovdqu 32(%rdi), %xmm2
- vmovdqu 48(%rdi), %xmm3
- vmovdqa %xmm0, %xmm5
- vmovdqa %xmm1, %xmm6
- vmovdqa %xmm2, %xmm7
- vmovdqa %xmm3, %xmm8
- movb $10, %al
-L_chacha20_avx1_block_crypt_start:
- vpaddd %xmm1, %xmm0, %xmm0
- vpxor %xmm0, %xmm3, %xmm3
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm3, %xmm3
- vpaddd %xmm3, %xmm2, %xmm2
- vpxor %xmm2, %xmm1, %xmm1
- vpsrld $20, %xmm1, %xmm4
- vpslld $12, %xmm1, %xmm1
- vpxor %xmm4, %xmm1, %xmm1
- vpaddd %xmm1, %xmm0, %xmm0
- vpxor %xmm0, %xmm3, %xmm3
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm3, %xmm3
- vpaddd %xmm3, %xmm2, %xmm2
- vpxor %xmm2, %xmm1, %xmm1
- vpsrld $25, %xmm1, %xmm4
- vpslld $7, %xmm1, %xmm1
- vpxor %xmm4, %xmm1, %xmm1
- vpshufd $57, %xmm1, %xmm1
- vpshufd $0x4e, %xmm2, %xmm2
- vpshufd $0x93, %xmm3, %xmm3
- vpaddd %xmm1, %xmm0, %xmm0
- vpxor %xmm0, %xmm3, %xmm3
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm3, %xmm3
- vpaddd %xmm3, %xmm2, %xmm2
- vpxor %xmm2, %xmm1, %xmm1
- vpsrld $20, %xmm1, %xmm4
- vpslld $12, %xmm1, %xmm1
- vpxor %xmm4, %xmm1, %xmm1
- vpaddd %xmm1, %xmm0, %xmm0
- vpxor %xmm0, %xmm3, %xmm3
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm3, %xmm3
- vpaddd %xmm3, %xmm2, %xmm2
- vpxor %xmm2, %xmm1, %xmm1
- vpsrld $25, %xmm1, %xmm4
- vpslld $7, %xmm1, %xmm1
- vpxor %xmm4, %xmm1, %xmm1
- vpshufd $0x93, %xmm1, %xmm1
- vpshufd $0x4e, %xmm2, %xmm2
- vpshufd $57, %xmm3, %xmm3
- decb %al
- jnz L_chacha20_avx1_block_crypt_start
- vpaddd %xmm5, %xmm0, %xmm0
- vpaddd %xmm6, %xmm1, %xmm1
- vpaddd %xmm7, %xmm2, %xmm2
- vpaddd %xmm8, %xmm3, %xmm3
- vmovdqu (%rsi), %xmm5
- vmovdqu 16(%rsi), %xmm6
- vmovdqu 32(%rsi), %xmm7
- vmovdqu 48(%rsi), %xmm8
- vpxor %xmm5, %xmm0, %xmm0
- vpxor %xmm6, %xmm1, %xmm1
- vpxor %xmm7, %xmm2, %xmm2
- vpxor %xmm8, %xmm3, %xmm3
- vmovdqu %xmm0, (%rdx)
- vmovdqu %xmm1, 16(%rdx)
- vmovdqu %xmm2, 32(%rdx)
- vmovdqu %xmm3, 48(%rdx)
- addl $0x01, 48(%rdi)
- subl $0x40, %ecx
- addq $0x40, %rsi
- addq $0x40, %rdx
- cmpl $0x40, %ecx
- jge L_chacha20_avx1_block_start
-L_chacha20_avx1_block_done:
- cmpl $0x00, %ecx
- je L_chacha20_avx1_partial_done
- vmovdqu (%rdi), %xmm0
- vmovdqu 16(%rdi), %xmm1
- vmovdqu 32(%rdi), %xmm2
- vmovdqu 48(%rdi), %xmm3
- vmovdqa %xmm0, %xmm5
- vmovdqa %xmm1, %xmm6
- vmovdqa %xmm2, %xmm7
- vmovdqa %xmm3, %xmm8
- movb $10, %al
-L_chacha20_avx1_partial_crypt_start:
- vpaddd %xmm1, %xmm0, %xmm0
- vpxor %xmm0, %xmm3, %xmm3
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm3, %xmm3
- vpaddd %xmm3, %xmm2, %xmm2
- vpxor %xmm2, %xmm1, %xmm1
- vpsrld $20, %xmm1, %xmm4
- vpslld $12, %xmm1, %xmm1
- vpxor %xmm4, %xmm1, %xmm1
- vpaddd %xmm1, %xmm0, %xmm0
- vpxor %xmm0, %xmm3, %xmm3
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm3, %xmm3
- vpaddd %xmm3, %xmm2, %xmm2
- vpxor %xmm2, %xmm1, %xmm1
- vpsrld $25, %xmm1, %xmm4
- vpslld $7, %xmm1, %xmm1
- vpxor %xmm4, %xmm1, %xmm1
- vpshufd $57, %xmm1, %xmm1
- vpshufd $0x4e, %xmm2, %xmm2
- vpshufd $0x93, %xmm3, %xmm3
- vpaddd %xmm1, %xmm0, %xmm0
- vpxor %xmm0, %xmm3, %xmm3
- vpshufb L_chacha20_avx1_rotl16(%rip), %xmm3, %xmm3
- vpaddd %xmm3, %xmm2, %xmm2
- vpxor %xmm2, %xmm1, %xmm1
- vpsrld $20, %xmm1, %xmm4
- vpslld $12, %xmm1, %xmm1
- vpxor %xmm4, %xmm1, %xmm1
- vpaddd %xmm1, %xmm0, %xmm0
- vpxor %xmm0, %xmm3, %xmm3
- vpshufb L_chacha20_avx1_rotl8(%rip), %xmm3, %xmm3
- vpaddd %xmm3, %xmm2, %xmm2
- vpxor %xmm2, %xmm1, %xmm1
- vpsrld $25, %xmm1, %xmm4
- vpslld $7, %xmm1, %xmm1
- vpxor %xmm4, %xmm1, %xmm1
- vpshufd $0x93, %xmm1, %xmm1
- vpshufd $0x4e, %xmm2, %xmm2
- vpshufd $57, %xmm3, %xmm3
- decb %al
- jnz L_chacha20_avx1_partial_crypt_start
- vpaddd %xmm5, %xmm0, %xmm0
- vpaddd %xmm6, %xmm1, %xmm1
- vpaddd %xmm7, %xmm2, %xmm2
- vpaddd %xmm8, %xmm3, %xmm3
- vmovdqu %xmm0, (%r10)
- vmovdqu %xmm1, 16(%r10)
- vmovdqu %xmm2, 32(%r10)
- vmovdqu %xmm3, 48(%r10)
- addl $0x01, 48(%rdi)
- movl %ecx, %r8d
- xorq %r11, %r11
- andl $7, %r8d
- jz L_chacha20_avx1_partial_start64
-L_chacha20_avx1_partial_start8:
- movzbl (%r10,%r11,1), %eax
- xorb (%rsi,%r11,1), %al
- movb %al, (%rdx,%r11,1)
- incl %r11d
- cmpl %r8d, %r11d
- jne L_chacha20_avx1_partial_start8
- je L_chacha20_avx1_partial_end64
-L_chacha20_avx1_partial_start64:
- movq (%r10,%r11,1), %rax
- xorq (%rsi,%r11,1), %rax
- movq %rax, (%rdx,%r11,1)
- addl $8, %r11d
-L_chacha20_avx1_partial_end64:
- cmpl %ecx, %r11d
- jne L_chacha20_avx1_partial_start64
-L_chacha20_avx1_partial_done:
- addq $0x190, %rsp
- repz retq
-#ifndef __APPLE__
-.size chacha_encrypt_avx1,.-chacha_encrypt_avx1
-#endif /* __APPLE__ */
-#endif /* HAVE_INTEL_AVX1 */
-#ifdef HAVE_INTEL_AVX2
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 32
-#else
-.p2align 5
-#endif /* __APPLE__ */
-L_chacha20_avx2_rotl8:
-.quad 0x605040702010003, 0xe0d0c0f0a09080b
-.quad 0x605040702010003, 0xe0d0c0f0a09080b
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 32
-#else
-.p2align 5
-#endif /* __APPLE__ */
-L_chacha20_avx2_rotl16:
-.quad 0x504070601000302, 0xd0c0f0e09080b0a
-.quad 0x504070601000302, 0xd0c0f0e09080b0a
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 32
-#else
-.p2align 5
-#endif /* __APPLE__ */
-L_chacha20_avx2_add:
-.quad 0x100000000, 0x300000002
-.quad 0x500000004, 0x700000006
-#ifndef __APPLE__
-.data
-#else
-.section __DATA,__data
-#endif /* __APPLE__ */
-#ifndef __APPLE__
-.align 32
-#else
-.p2align 5
-#endif /* __APPLE__ */
-L_chacha20_avx2_eight:
-.quad 0x800000008, 0x800000008
-.quad 0x800000008, 0x800000008
-#ifndef __APPLE__
-.text
-.globl chacha_encrypt_avx2
-.type chacha_encrypt_avx2,@function
-.align 4
-chacha_encrypt_avx2:
-#else
-.section __TEXT,__text
-.globl _chacha_encrypt_avx2
-.p2align 2
-_chacha_encrypt_avx2:
-#endif /* __APPLE__ */
- subq $0x310, %rsp
- movq %rsp, %r9
- leaq 512(%rsp), %r10
- andq $-32, %r9
- andq $-32, %r10
- movl %ecx, %eax
- shrl $9, %eax
- jz L_chacha20_avx2_end256
- vpbroadcastd (%rdi), %ymm0
- vpbroadcastd 4(%rdi), %ymm1
- vpbroadcastd 8(%rdi), %ymm2
- vpbroadcastd 12(%rdi), %ymm3
- vpbroadcastd 16(%rdi), %ymm4
- vpbroadcastd 20(%rdi), %ymm5
- vpbroadcastd 24(%rdi), %ymm6
- vpbroadcastd 28(%rdi), %ymm7
- vpbroadcastd 32(%rdi), %ymm8
- vpbroadcastd 36(%rdi), %ymm9
- vpbroadcastd 40(%rdi), %ymm10
- vpbroadcastd 44(%rdi), %ymm11
- vpbroadcastd 48(%rdi), %ymm12
- vpbroadcastd 52(%rdi), %ymm13
- vpbroadcastd 56(%rdi), %ymm14
- vpbroadcastd 60(%rdi), %ymm15
- vpaddd L_chacha20_avx2_add(%rip), %ymm12, %ymm12
- vmovdqa %ymm0, (%r9)
- vmovdqa %ymm1, 32(%r9)
- vmovdqa %ymm2, 64(%r9)
- vmovdqa %ymm3, 96(%r9)
- vmovdqa %ymm4, 128(%r9)
- vmovdqa %ymm5, 160(%r9)
- vmovdqa %ymm6, 192(%r9)
- vmovdqa %ymm7, 224(%r9)
- vmovdqa %ymm8, 256(%r9)
- vmovdqa %ymm9, 288(%r9)
- vmovdqa %ymm10, 320(%r9)
- vmovdqa %ymm11, 352(%r9)
- vmovdqa %ymm12, 384(%r9)
- vmovdqa %ymm13, 416(%r9)
- vmovdqa %ymm14, 448(%r9)
- vmovdqa %ymm15, 480(%r9)
-L_chacha20_avx2_start256:
- movb $10, %r8b
- vmovdqa %ymm11, 96(%r10)
-L_chacha20_avx2_loop256:
- vpaddd %ymm4, %ymm0, %ymm0
- vpxor %ymm0, %ymm12, %ymm12
- vmovdqa 96(%r10), %ymm11
- vpshufb L_chacha20_avx2_rotl16(%rip), %ymm12, %ymm12
- vpaddd %ymm12, %ymm8, %ymm8
- vpxor %ymm8, %ymm4, %ymm4
- vpaddd %ymm5, %ymm1, %ymm1
- vpxor %ymm1, %ymm13, %ymm13
- vpshufb L_chacha20_avx2_rotl16(%rip), %ymm13, %ymm13
- vpaddd %ymm13, %ymm9, %ymm9
- vpxor %ymm9, %ymm5, %ymm5
- vpaddd %ymm6, %ymm2, %ymm2
- vpxor %ymm2, %ymm14, %ymm14
- vpshufb L_chacha20_avx2_rotl16(%rip), %ymm14, %ymm14
- vpaddd %ymm14, %ymm10, %ymm10
- vpxor %ymm10, %ymm6, %ymm6
- vpaddd %ymm7, %ymm3, %ymm3
- vpxor %ymm3, %ymm15, %ymm15
- vpshufb L_chacha20_avx2_rotl16(%rip), %ymm15, %ymm15
- vpaddd %ymm15, %ymm11, %ymm11
- vpxor %ymm11, %ymm7, %ymm7
- vmovdqa %ymm11, 96(%r10)
- vpsrld $20, %ymm4, %ymm11
- vpslld $12, %ymm4, %ymm4
- vpxor %ymm11, %ymm4, %ymm4
- vpsrld $20, %ymm5, %ymm11
- vpslld $12, %ymm5, %ymm5
- vpxor %ymm11, %ymm5, %ymm5
- vpsrld $20, %ymm6, %ymm11
- vpslld $12, %ymm6, %ymm6
- vpxor %ymm11, %ymm6, %ymm6
- vpsrld $20, %ymm7, %ymm11
- vpslld $12, %ymm7, %ymm7
- vpxor %ymm11, %ymm7, %ymm7
- vpaddd %ymm4, %ymm0, %ymm0
- vpxor %ymm0, %ymm12, %ymm12
- vmovdqa 96(%r10), %ymm11
- vpshufb L_chacha20_avx2_rotl8(%rip), %ymm12, %ymm12
- vpaddd %ymm12, %ymm8, %ymm8
- vpxor %ymm8, %ymm4, %ymm4
- vpaddd %ymm5, %ymm1, %ymm1
- vpxor %ymm1, %ymm13, %ymm13
- vpshufb L_chacha20_avx2_rotl8(%rip), %ymm13, %ymm13
- vpaddd %ymm13, %ymm9, %ymm9
- vpxor %ymm9, %ymm5, %ymm5
- vpaddd %ymm6, %ymm2, %ymm2
- vpxor %ymm2, %ymm14, %ymm14
- vpshufb L_chacha20_avx2_rotl8(%rip), %ymm14, %ymm14
- vpaddd %ymm14, %ymm10, %ymm10
- vpxor %ymm10, %ymm6, %ymm6
- vpaddd %ymm7, %ymm3, %ymm3
- vpxor %ymm3, %ymm15, %ymm15
- vpshufb L_chacha20_avx2_rotl8(%rip), %ymm15, %ymm15
- vpaddd %ymm15, %ymm11, %ymm11
- vpxor %ymm11, %ymm7, %ymm7
- vmovdqa %ymm11, 96(%r10)
- vpsrld $25, %ymm4, %ymm11
- vpslld $7, %ymm4, %ymm4
- vpxor %ymm11, %ymm4, %ymm4
- vpsrld $25, %ymm5, %ymm11
- vpslld $7, %ymm5, %ymm5
- vpxor %ymm11, %ymm5, %ymm5
- vpsrld $25, %ymm6, %ymm11
- vpslld $7, %ymm6, %ymm6
- vpxor %ymm11, %ymm6, %ymm6
- vpsrld $25, %ymm7, %ymm11
- vpslld $7, %ymm7, %ymm7
- vpxor %ymm11, %ymm7, %ymm7
- vpaddd %ymm5, %ymm0, %ymm0
- vpxor %ymm0, %ymm15, %ymm15
- vmovdqa 96(%r10), %ymm11
- vpshufb L_chacha20_avx2_rotl16(%rip), %ymm15, %ymm15
- vpaddd %ymm15, %ymm10, %ymm10
- vpxor %ymm10, %ymm5, %ymm5
- vpaddd %ymm6, %ymm1, %ymm1
- vpxor %ymm1, %ymm12, %ymm12
- vpshufb L_chacha20_avx2_rotl16(%rip), %ymm12, %ymm12
- vpaddd %ymm12, %ymm11, %ymm11
- vpxor %ymm11, %ymm6, %ymm6
- vpaddd %ymm7, %ymm2, %ymm2
- vpxor %ymm2, %ymm13, %ymm13
- vpshufb L_chacha20_avx2_rotl16(%rip), %ymm13, %ymm13
- vpaddd %ymm13, %ymm8, %ymm8
- vpxor %ymm8, %ymm7, %ymm7
- vpaddd %ymm4, %ymm3, %ymm3
- vpxor %ymm3, %ymm14, %ymm14
- vpshufb L_chacha20_avx2_rotl16(%rip), %ymm14, %ymm14
- vpaddd %ymm14, %ymm9, %ymm9
- vpxor %ymm9, %ymm4, %ymm4
- vmovdqa %ymm11, 96(%r10)
- vpsrld $20, %ymm5, %ymm11
- vpslld $12, %ymm5, %ymm5
- vpxor %ymm11, %ymm5, %ymm5
- vpsrld $20, %ymm6, %ymm11
- vpslld $12, %ymm6, %ymm6
- vpxor %ymm11, %ymm6, %ymm6
- vpsrld $20, %ymm7, %ymm11
- vpslld $12, %ymm7, %ymm7
- vpxor %ymm11, %ymm7, %ymm7
- vpsrld $20, %ymm4, %ymm11
- vpslld $12, %ymm4, %ymm4
- vpxor %ymm11, %ymm4, %ymm4
- vpaddd %ymm5, %ymm0, %ymm0
- vpxor %ymm0, %ymm15, %ymm15
- vmovdqa 96(%r10), %ymm11
- vpshufb L_chacha20_avx2_rotl8(%rip), %ymm15, %ymm15
- vpaddd %ymm15, %ymm10, %ymm10
- vpxor %ymm10, %ymm5, %ymm5
- vpaddd %ymm6, %ymm1, %ymm1
- vpxor %ymm1, %ymm12, %ymm12
- vpshufb L_chacha20_avx2_rotl8(%rip), %ymm12, %ymm12
- vpaddd %ymm12, %ymm11, %ymm11
- vpxor %ymm11, %ymm6, %ymm6
- vpaddd %ymm7, %ymm2, %ymm2
- vpxor %ymm2, %ymm13, %ymm13
- vpshufb L_chacha20_avx2_rotl8(%rip), %ymm13, %ymm13
- vpaddd %ymm13, %ymm8, %ymm8
- vpxor %ymm8, %ymm7, %ymm7
- vpaddd %ymm4, %ymm3, %ymm3
- vpxor %ymm3, %ymm14, %ymm14
- vpshufb L_chacha20_avx2_rotl8(%rip), %ymm14, %ymm14
- vpaddd %ymm14, %ymm9, %ymm9
- vpxor %ymm9, %ymm4, %ymm4
- vmovdqa %ymm11, 96(%r10)
- vpsrld $25, %ymm5, %ymm11
- vpslld $7, %ymm5, %ymm5
- vpxor %ymm11, %ymm5, %ymm5
- vpsrld $25, %ymm6, %ymm11
- vpslld $7, %ymm6, %ymm6
- vpxor %ymm11, %ymm6, %ymm6
- vpsrld $25, %ymm7, %ymm11
- vpslld $7, %ymm7, %ymm7
- vpxor %ymm11, %ymm7, %ymm7
- vpsrld $25, %ymm4, %ymm11
- vpslld $7, %ymm4, %ymm4
- vpxor %ymm11, %ymm4, %ymm4
- decb %r8b
- jnz L_chacha20_avx2_loop256
- vmovdqa 96(%r10), %ymm11
- vpaddd (%r9), %ymm0, %ymm0
- vpaddd 32(%r9), %ymm1, %ymm1
- vpaddd 64(%r9), %ymm2, %ymm2
- vpaddd 96(%r9), %ymm3, %ymm3
- vpaddd 128(%r9), %ymm4, %ymm4
- vpaddd 160(%r9), %ymm5, %ymm5
- vpaddd 192(%r9), %ymm6, %ymm6
- vpaddd 224(%r9), %ymm7, %ymm7
- vpaddd 256(%r9), %ymm8, %ymm8
- vpaddd 288(%r9), %ymm9, %ymm9
- vpaddd 320(%r9), %ymm10, %ymm10
- vpaddd 352(%r9), %ymm11, %ymm11
- vpaddd 384(%r9), %ymm12, %ymm12
- vpaddd 416(%r9), %ymm13, %ymm13
- vpaddd 448(%r9), %ymm14, %ymm14
- vpaddd 480(%r9), %ymm15, %ymm15
- vmovdqa %ymm8, (%r10)
- vmovdqa %ymm9, 32(%r10)
- vmovdqa %ymm10, 64(%r10)
- vmovdqa %ymm11, 96(%r10)
- vmovdqa %ymm12, 128(%r10)
- vmovdqa %ymm13, 160(%r10)
- vmovdqa %ymm14, 192(%r10)
- vmovdqa %ymm15, 224(%r10)
- vpunpckldq %ymm1, %ymm0, %ymm8
- vpunpckldq %ymm3, %ymm2, %ymm9
- vpunpckhdq %ymm1, %ymm0, %ymm12
- vpunpckhdq %ymm3, %ymm2, %ymm13
- vpunpckldq %ymm5, %ymm4, %ymm10
- vpunpckldq %ymm7, %ymm6, %ymm11
- vpunpckhdq %ymm5, %ymm4, %ymm14
- vpunpckhdq %ymm7, %ymm6, %ymm15
- vpunpcklqdq %ymm9, %ymm8, %ymm0
- vpunpcklqdq %ymm11, %ymm10, %ymm1
- vpunpckhqdq %ymm9, %ymm8, %ymm2
- vpunpckhqdq %ymm11, %ymm10, %ymm3
- vpunpcklqdq %ymm13, %ymm12, %ymm4
- vpunpcklqdq %ymm15, %ymm14, %ymm5
- vpunpckhqdq %ymm13, %ymm12, %ymm6
- vpunpckhqdq %ymm15, %ymm14, %ymm7
- vperm2i128 $32, %ymm1, %ymm0, %ymm8
- vperm2i128 $32, %ymm3, %ymm2, %ymm9
- vperm2i128 $49, %ymm1, %ymm0, %ymm12
- vperm2i128 $49, %ymm3, %ymm2, %ymm13
- vperm2i128 $32, %ymm5, %ymm4, %ymm10
- vperm2i128 $32, %ymm7, %ymm6, %ymm11
- vperm2i128 $49, %ymm5, %ymm4, %ymm14
- vperm2i128 $49, %ymm7, %ymm6, %ymm15
- vmovdqu (%rsi), %ymm0
- vmovdqu 64(%rsi), %ymm1
- vmovdqu 128(%rsi), %ymm2
- vmovdqu 192(%rsi), %ymm3
- vmovdqu 256(%rsi), %ymm4
- vmovdqu 320(%rsi), %ymm5
- vmovdqu 384(%rsi), %ymm6
- vmovdqu 448(%rsi), %ymm7
- vpxor %ymm0, %ymm8, %ymm8
- vpxor %ymm1, %ymm9, %ymm9
- vpxor %ymm2, %ymm10, %ymm10
- vpxor %ymm3, %ymm11, %ymm11
- vpxor %ymm4, %ymm12, %ymm12
- vpxor %ymm5, %ymm13, %ymm13
- vpxor %ymm6, %ymm14, %ymm14
- vpxor %ymm7, %ymm15, %ymm15
- vmovdqu %ymm8, (%rdx)
- vmovdqu %ymm9, 64(%rdx)
- vmovdqu %ymm10, 128(%rdx)
- vmovdqu %ymm11, 192(%rdx)
- vmovdqu %ymm12, 256(%rdx)
- vmovdqu %ymm13, 320(%rdx)
- vmovdqu %ymm14, 384(%rdx)
- vmovdqu %ymm15, 448(%rdx)
- vmovdqa (%r10), %ymm0
- vmovdqa 32(%r10), %ymm1
- vmovdqa 64(%r10), %ymm2
- vmovdqa 96(%r10), %ymm3
- vmovdqa 128(%r10), %ymm4
- vmovdqa 160(%r10), %ymm5
- vmovdqa 192(%r10), %ymm6
- vmovdqa 224(%r10), %ymm7
- vpunpckldq %ymm1, %ymm0, %ymm8
- vpunpckldq %ymm3, %ymm2, %ymm9
- vpunpckhdq %ymm1, %ymm0, %ymm12
- vpunpckhdq %ymm3, %ymm2, %ymm13
- vpunpckldq %ymm5, %ymm4, %ymm10
- vpunpckldq %ymm7, %ymm6, %ymm11
- vpunpckhdq %ymm5, %ymm4, %ymm14
- vpunpckhdq %ymm7, %ymm6, %ymm15
- vpunpcklqdq %ymm9, %ymm8, %ymm0
- vpunpcklqdq %ymm11, %ymm10, %ymm1
- vpunpckhqdq %ymm9, %ymm8, %ymm2
- vpunpckhqdq %ymm11, %ymm10, %ymm3
- vpunpcklqdq %ymm13, %ymm12, %ymm4
- vpunpcklqdq %ymm15, %ymm14, %ymm5
- vpunpckhqdq %ymm13, %ymm12, %ymm6
- vpunpckhqdq %ymm15, %ymm14, %ymm7
- vperm2i128 $32, %ymm1, %ymm0, %ymm8
- vperm2i128 $32, %ymm3, %ymm2, %ymm9
- vperm2i128 $49, %ymm1, %ymm0, %ymm12
- vperm2i128 $49, %ymm3, %ymm2, %ymm13
- vperm2i128 $32, %ymm5, %ymm4, %ymm10
- vperm2i128 $32, %ymm7, %ymm6, %ymm11
- vperm2i128 $49, %ymm5, %ymm4, %ymm14
- vperm2i128 $49, %ymm7, %ymm6, %ymm15
- vmovdqu 32(%rsi), %ymm0
- vmovdqu 96(%rsi), %ymm1
- vmovdqu 160(%rsi), %ymm2
- vmovdqu 224(%rsi), %ymm3
- vmovdqu 288(%rsi), %ymm4
- vmovdqu 352(%rsi), %ymm5
- vmovdqu 416(%rsi), %ymm6
- vmovdqu 480(%rsi), %ymm7
- vpxor %ymm0, %ymm8, %ymm8
- vpxor %ymm1, %ymm9, %ymm9
- vpxor %ymm2, %ymm10, %ymm10
- vpxor %ymm3, %ymm11, %ymm11
- vpxor %ymm4, %ymm12, %ymm12
- vpxor %ymm5, %ymm13, %ymm13
- vpxor %ymm6, %ymm14, %ymm14
- vpxor %ymm7, %ymm15, %ymm15
- vmovdqu %ymm8, 32(%rdx)
- vmovdqu %ymm9, 96(%rdx)
- vmovdqu %ymm10, 160(%rdx)
- vmovdqu %ymm11, 224(%rdx)
- vmovdqu %ymm12, 288(%rdx)
- vmovdqu %ymm13, 352(%rdx)
- vmovdqu %ymm14, 416(%rdx)
- vmovdqu %ymm15, 480(%rdx)
- vmovdqa 384(%r9), %ymm12
- addq $0x200, %rsi
- addq $0x200, %rdx
- vpaddd L_chacha20_avx2_eight(%rip), %ymm12, %ymm12
- subl $0x200, %ecx
- vmovdqa %ymm12, 384(%r9)
- cmpl $0x200, %ecx
- jl L_chacha20_avx2_done256
- vmovdqa (%r9), %ymm0
- vmovdqa 32(%r9), %ymm1
- vmovdqa 64(%r9), %ymm2
- vmovdqa 96(%r9), %ymm3
- vmovdqa 128(%r9), %ymm4
- vmovdqa 160(%r9), %ymm5
- vmovdqa 192(%r9), %ymm6
- vmovdqa 224(%r9), %ymm7
- vmovdqa 256(%r9), %ymm8
- vmovdqa 288(%r9), %ymm9
- vmovdqa 320(%r9), %ymm10
- vmovdqa 352(%r9), %ymm11
- vmovdqa 384(%r9), %ymm12
- vmovdqa 416(%r9), %ymm13
- vmovdqa 448(%r9), %ymm14
- vmovdqa 480(%r9), %ymm15
- jmp L_chacha20_avx2_start256
-L_chacha20_avx2_done256:
- shl $3, %eax
- addl %eax, 48(%rdi)
-L_chacha20_avx2_end256:
-#ifndef __APPLE__
- callq chacha_encrypt_avx1@plt
-#else
- callq _chacha_encrypt_avx1
-#endif /* __APPLE__ */
- addq $0x310, %rsp
- repz retq
-#ifndef __APPLE__
-.size chacha_encrypt_avx2,.-chacha_encrypt_avx2
-#endif /* __APPLE__ */
-#endif /* HAVE_INTEL_AVX2 */
diff --git a/client/wolfssl/wolfcrypt/src/cmac.c b/client/wolfssl/wolfcrypt/src/cmac.c
deleted file mode 100644
index 9d30bb5..0000000
--- a/client/wolfssl/wolfcrypt/src/cmac.c
+++ /dev/null
@@ -1,215 +0,0 @@
-/* cmac.c
- *
- * Copyright (C) 2006-2020 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
-
-
-#ifdef HAVE_CONFIG_H
- #include
-#endif
-
-#include
-
-#if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
-
-#if defined(HAVE_FIPS) && \
- defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
-
- /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
- #define FIPS_NO_WRAPPERS
-
- #ifdef USE_WINDOWS_API
- #pragma code_seg(".fipsA$n")
- #pragma const_seg(".fipsB$n")
- #endif
-#endif
-
-#ifdef NO_INLINE
- #include
-#else
- #define WOLFSSL_MISC_INCLUDED
- #include