react-native-nitro-bsx 0.0.0-stage → 0.2.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (931) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +7 -2
  3. package/android/CMakeLists.txt +28 -0
  4. package/android/build.gradle +99 -0
  5. package/android/src/main/AndroidManifest.xml +35 -0
  6. package/android/src/main/cpp/cpp-adapter.cpp +12 -0
  7. package/android/src/main/java/com/nitrobsx/BSXAlarmReceiver.kt +29 -0
  8. package/android/src/main/java/com/nitrobsx/BSXAlarmScheduler.kt +112 -0
  9. package/android/src/main/java/com/nitrobsx/BSXForegroundService.kt +78 -0
  10. package/android/src/main/java/com/nitrobsx/BSXNotifications.kt +47 -0
  11. package/android/src/main/java/com/nitrobsx/BSXServiceController.kt +90 -0
  12. package/android/src/main/java/com/nitrobsx/BSXWakeWorker.kt +136 -0
  13. package/android/src/main/java/com/nitrobsx/HeadlessJsTaskSupportShim.kt +41 -0
  14. package/android/src/main/java/com/nitrobsx/NitroBSXPackage.kt +28 -0
  15. package/android/src/main/res/drawable/bsx_ic_stat_swap.xml +9 -0
  16. package/cpp/BSXContext.cpp +263 -0
  17. package/cpp/BSXContext.h +49 -0
  18. package/cpp/BSXModule.cpp +21191 -0
  19. package/cpp/BSXModule.h +1523 -0
  20. package/cpp/CMakeLists.txt +463 -0
  21. package/cpp/HybridNitroBSX.cpp +1483 -0
  22. package/cpp/HybridNitroBSX.hpp +173 -0
  23. package/cpp/arith_uint256.cpp +232 -0
  24. package/cpp/arith_uint256.h +277 -0
  25. package/cpp/bsx/event.cpp +48 -0
  26. package/cpp/bsx/event.h +29 -0
  27. package/cpp/bsx/swap.h +293 -0
  28. package/cpp/bsx/types.cpp +486 -0
  29. package/cpp/bsx/types.h +308 -0
  30. package/cpp/bsx_db.cpp +4951 -0
  31. package/cpp/bsx_db.h +465 -0
  32. package/cpp/chain_client.h +143 -0
  33. package/cpp/cmake/GenVersion.cmake +52 -0
  34. package/cpp/coin_config.h.in +12 -0
  35. package/cpp/coins/block_index.cpp +33 -0
  36. package/cpp/coins/block_index.h +35 -0
  37. package/cpp/coins/btc/chainparams.cpp +70 -0
  38. package/cpp/coins/btc/chainparams.h +23 -0
  39. package/cpp/coins/btc/interface.h +30 -0
  40. package/cpp/coins/btc_base/interface.cpp +2200 -0
  41. package/cpp/coins/btc_base/interface.h +225 -0
  42. package/cpp/coins/btc_base/primitives.cpp +560 -0
  43. package/cpp/coins/btc_base/primitives.h +157 -0
  44. package/cpp/coins/btc_base/psbt.cpp +150 -0
  45. package/cpp/coins/btc_base/psbt.h +52 -0
  46. package/cpp/coins/btc_base/script.cpp +304 -0
  47. package/cpp/coins/btc_base/script.h +181 -0
  48. package/cpp/coins/chainparams.cpp +47 -0
  49. package/cpp/coins/chainparams.h +58 -0
  50. package/cpp/coins/firo/chainparams.cpp +52 -0
  51. package/cpp/coins/firo/chainparams.h +24 -0
  52. package/cpp/coins/firo/firo.cpp +118 -0
  53. package/cpp/coins/firo/firo.h +79 -0
  54. package/cpp/coins/firo/primitives.cpp +109 -0
  55. package/cpp/coins/firo/primitives.h +47 -0
  56. package/cpp/coins/interface.cpp +1815 -0
  57. package/cpp/coins/interface.h +554 -0
  58. package/cpp/coins/ltc/chainparams.cpp +70 -0
  59. package/cpp/coins/ltc/chainparams.h +22 -0
  60. package/cpp/coins/ltc/ltc.cpp +69 -0
  61. package/cpp/coins/ltc/ltc.h +82 -0
  62. package/cpp/coins/ltc/primitives.cpp +20 -0
  63. package/cpp/coins/ltc/primitives.h +18 -0
  64. package/cpp/coins/part/chainparams.cpp +89 -0
  65. package/cpp/coins/part/chainparams.h +47 -0
  66. package/cpp/coins/part/part.cpp +905 -0
  67. package/cpp/coins/part/part.h +164 -0
  68. package/cpp/coins/part/primitives.cpp +548 -0
  69. package/cpp/coins/part/primitives.h +187 -0
  70. package/cpp/coins/part/stake.cpp +330 -0
  71. package/cpp/coins/part/stake.h +92 -0
  72. package/cpp/coins/primitives.h +125 -0
  73. package/cpp/coins/registry.cpp +99 -0
  74. package/cpp/coins/registry.h +32 -0
  75. package/cpp/coins/script.h +18 -0
  76. package/cpp/coins/types.cpp +60 -0
  77. package/cpp/coins/types.h +41 -0
  78. package/cpp/coins/xmr/chainparams.cpp +28 -0
  79. package/cpp/coins/xmr/chainparams.h +24 -0
  80. package/cpp/coins/xmr/tx.cpp +670 -0
  81. package/cpp/coins/xmr/tx.h +77 -0
  82. package/cpp/coins/xmr/xmr.cpp +639 -0
  83. package/cpp/coins/xmr/xmr.h +223 -0
  84. package/cpp/compat/byteswap.h +74 -0
  85. package/cpp/compat/endian.h +98 -0
  86. package/cpp/compat/mbedtls/threading_alt.h +20 -0
  87. package/cpp/compat/sockets.h +160 -0
  88. package/cpp/contrib/mbedtls/CMakeLists.txt +566 -0
  89. package/cpp/contrib/mbedtls/LICENSE +553 -0
  90. package/cpp/contrib/mbedtls/cmake/MbedTLSConfig.cmake.in +3 -0
  91. package/cpp/contrib/mbedtls/doxygen/input/doc_encdec.h +52 -0
  92. package/cpp/contrib/mbedtls/doxygen/input/doc_hashing.h +30 -0
  93. package/cpp/contrib/mbedtls/doxygen/input/doc_mainpage.h +52 -0
  94. package/cpp/contrib/mbedtls/doxygen/input/doc_rng.h +27 -0
  95. package/cpp/contrib/mbedtls/doxygen/input/doc_ssltls.h +37 -0
  96. package/cpp/contrib/mbedtls/doxygen/input/doc_tcpip.h +32 -0
  97. package/cpp/contrib/mbedtls/doxygen/input/doc_x509.h +31 -0
  98. package/cpp/contrib/mbedtls/doxygen/mbedtls.doxyfile +56 -0
  99. package/cpp/contrib/mbedtls/framework/CMakeLists.txt +1 -0
  100. package/cpp/contrib/mbedtls/framework/LICENSE +553 -0
  101. package/cpp/contrib/mbedtls/include/CMakeLists.txt +21 -0
  102. package/cpp/contrib/mbedtls/include/mbedtls/build_info.h +93 -0
  103. package/cpp/contrib/mbedtls/include/mbedtls/debug.h +120 -0
  104. package/cpp/contrib/mbedtls/include/mbedtls/error.h +39 -0
  105. package/cpp/contrib/mbedtls/include/mbedtls/mbedtls_config.h +1189 -0
  106. package/cpp/contrib/mbedtls/include/mbedtls/net_sockets.h +299 -0
  107. package/cpp/contrib/mbedtls/include/mbedtls/oid.h +304 -0
  108. package/cpp/contrib/mbedtls/include/mbedtls/pkcs7.h +250 -0
  109. package/cpp/contrib/mbedtls/include/mbedtls/private/config_adjust_ssl.h +84 -0
  110. package/cpp/contrib/mbedtls/include/mbedtls/private/config_adjust_x509.h +35 -0
  111. package/cpp/contrib/mbedtls/include/mbedtls/ssl.h +5622 -0
  112. package/cpp/contrib/mbedtls/include/mbedtls/ssl_cache.h +187 -0
  113. package/cpp/contrib/mbedtls/include/mbedtls/ssl_ciphersuites.h +303 -0
  114. package/cpp/contrib/mbedtls/include/mbedtls/ssl_cookie.h +90 -0
  115. package/cpp/contrib/mbedtls/include/mbedtls/ssl_ticket.h +186 -0
  116. package/cpp/contrib/mbedtls/include/mbedtls/timing.h +105 -0
  117. package/cpp/contrib/mbedtls/include/mbedtls/version.h +69 -0
  118. package/cpp/contrib/mbedtls/include/mbedtls/x509.h +503 -0
  119. package/cpp/contrib/mbedtls/include/mbedtls/x509_crl.h +180 -0
  120. package/cpp/contrib/mbedtls/include/mbedtls/x509_crt.h +1174 -0
  121. package/cpp/contrib/mbedtls/include/mbedtls/x509_csr.h +368 -0
  122. package/cpp/contrib/mbedtls/library/CMakeLists.txt +375 -0
  123. package/cpp/contrib/mbedtls/library/Makefile +334 -0
  124. package/cpp/contrib/mbedtls/library/debug.c +285 -0
  125. package/cpp/contrib/mbedtls/library/debug_internal.h +108 -0
  126. package/cpp/contrib/mbedtls/library/error.c +505 -0
  127. package/cpp/contrib/mbedtls/library/mbedtls_check_config.h +376 -0
  128. package/cpp/contrib/mbedtls/library/mbedtls_common.h +51 -0
  129. package/cpp/contrib/mbedtls/library/mbedtls_config.c +40 -0
  130. package/cpp/contrib/mbedtls/library/mbedtls_config_check_before.h +1769 -0
  131. package/cpp/contrib/mbedtls/library/mbedtls_config_check_final.h +12 -0
  132. package/cpp/contrib/mbedtls/library/mbedtls_config_check_user.h +2436 -0
  133. package/cpp/contrib/mbedtls/library/mbedtls_platform_requirements.h +55 -0
  134. package/cpp/contrib/mbedtls/library/mbedtls_utils.h +23 -0
  135. package/cpp/contrib/mbedtls/library/mps_common.h +181 -0
  136. package/cpp/contrib/mbedtls/library/mps_error.h +89 -0
  137. package/cpp/contrib/mbedtls/library/mps_reader.c +538 -0
  138. package/cpp/contrib/mbedtls/library/mps_reader.h +366 -0
  139. package/cpp/contrib/mbedtls/library/mps_trace.c +112 -0
  140. package/cpp/contrib/mbedtls/library/mps_trace.h +154 -0
  141. package/cpp/contrib/mbedtls/library/net_sockets.c +684 -0
  142. package/cpp/contrib/mbedtls/library/pkcs7.c +784 -0
  143. package/cpp/contrib/mbedtls/library/ssl_cache.c +409 -0
  144. package/cpp/contrib/mbedtls/library/ssl_ciphersuites.c +996 -0
  145. package/cpp/contrib/mbedtls/library/ssl_ciphersuites_internal.h +108 -0
  146. package/cpp/contrib/mbedtls/library/ssl_client.c +1019 -0
  147. package/cpp/contrib/mbedtls/library/ssl_client.h +18 -0
  148. package/cpp/contrib/mbedtls/library/ssl_cookie.c +251 -0
  149. package/cpp/contrib/mbedtls/library/ssl_debug_helpers.h +83 -0
  150. package/cpp/contrib/mbedtls/library/ssl_debug_helpers_generated.c +243 -0
  151. package/cpp/contrib/mbedtls/library/ssl_misc.h +2851 -0
  152. package/cpp/contrib/mbedtls/library/ssl_msg.c +6275 -0
  153. package/cpp/contrib/mbedtls/library/ssl_ticket.c +453 -0
  154. package/cpp/contrib/mbedtls/library/ssl_tls.c +9048 -0
  155. package/cpp/contrib/mbedtls/library/ssl_tls12_client.c +3054 -0
  156. package/cpp/contrib/mbedtls/library/ssl_tls12_server.c +3546 -0
  157. package/cpp/contrib/mbedtls/library/ssl_tls13_client.c +3202 -0
  158. package/cpp/contrib/mbedtls/library/ssl_tls13_generic.c +1773 -0
  159. package/cpp/contrib/mbedtls/library/ssl_tls13_invasive.h +23 -0
  160. package/cpp/contrib/mbedtls/library/ssl_tls13_keys.c +1860 -0
  161. package/cpp/contrib/mbedtls/library/ssl_tls13_keys.h +668 -0
  162. package/cpp/contrib/mbedtls/library/ssl_tls13_server.c +3602 -0
  163. package/cpp/contrib/mbedtls/library/timing.c +76 -0
  164. package/cpp/contrib/mbedtls/library/version.c +30 -0
  165. package/cpp/contrib/mbedtls/library/version_features.c +244 -0
  166. package/cpp/contrib/mbedtls/library/x509.c +1848 -0
  167. package/cpp/contrib/mbedtls/library/x509_create.c +747 -0
  168. package/cpp/contrib/mbedtls/library/x509_crl.c +701 -0
  169. package/cpp/contrib/mbedtls/library/x509_crt.c +3278 -0
  170. package/cpp/contrib/mbedtls/library/x509_csr.c +642 -0
  171. package/cpp/contrib/mbedtls/library/x509_internal.h +97 -0
  172. package/cpp/contrib/mbedtls/library/x509_oid.c +603 -0
  173. package/cpp/contrib/mbedtls/library/x509_oid.h +150 -0
  174. package/cpp/contrib/mbedtls/library/x509write.c +171 -0
  175. package/cpp/contrib/mbedtls/library/x509write_crt.c +637 -0
  176. package/cpp/contrib/mbedtls/library/x509write_csr.c +317 -0
  177. package/cpp/contrib/mbedtls/pkgconfig/CMakeLists.txt +25 -0
  178. package/cpp/contrib/mbedtls/pkgconfig/JoinPaths.cmake +27 -0
  179. package/cpp/contrib/mbedtls/pkgconfig/mbedcrypto.pc.in +10 -0
  180. package/cpp/contrib/mbedtls/pkgconfig/mbedtls.pc.in +11 -0
  181. package/cpp/contrib/mbedtls/pkgconfig/mbedx509.pc.in +11 -0
  182. package/cpp/contrib/mbedtls/tf-psa-crypto/CMakeLists.txt +584 -0
  183. package/cpp/contrib/mbedtls/tf-psa-crypto/LICENSE +552 -0
  184. package/cpp/contrib/mbedtls/tf-psa-crypto/cmake/TF-PSA-CryptoConfig.cmake.in +3 -0
  185. package/cpp/contrib/mbedtls/tf-psa-crypto/core/CMakeLists.txt +193 -0
  186. package/cpp/contrib/mbedtls/tf-psa-crypto/core/alignment.h +700 -0
  187. package/cpp/contrib/mbedtls/tf-psa-crypto/core/check_crypto_config.h +111 -0
  188. package/cpp/contrib/mbedtls/tf-psa-crypto/core/crypto-library.make +57 -0
  189. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto.c +9820 -0
  190. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_client.c +22 -0
  191. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_core.h +972 -0
  192. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_core_common.h +52 -0
  193. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers.h +3395 -0
  194. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_driver_wrappers_no_static.c +247 -0
  195. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_invasive.h +85 -0
  196. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_its.h +131 -0
  197. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_random.c +168 -0
  198. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_random.h +72 -0
  199. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_random_impl.h +215 -0
  200. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_slot_management.c +998 -0
  201. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_slot_management.h +330 -0
  202. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_storage.c +400 -0
  203. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_crypto_storage.h +201 -0
  204. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_its_file.c +254 -0
  205. package/cpp/contrib/mbedtls/tf-psa-crypto/core/psa_util.c +295 -0
  206. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_check_config.h +598 -0
  207. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_common.h +628 -0
  208. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config.c +72 -0
  209. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config_check_before.h +12 -0
  210. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config_check_final.h +12 -0
  211. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_config_check_user.h +1220 -0
  212. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_platform_requirements.h +62 -0
  213. package/cpp/contrib/mbedtls/tf-psa-crypto/core/tf_psa_crypto_version.c +28 -0
  214. package/cpp/contrib/mbedtls/tf-psa-crypto/dispatch/psa_crypto_driver_wrappers_no_static.h +31 -0
  215. package/cpp/contrib/mbedtls/tf-psa-crypto/doxygen/input/doc_mainpage.h.in +51 -0
  216. package/cpp/contrib/mbedtls/tf-psa-crypto/doxygen/tfpsacrypto.doxyfile.in +54 -0
  217. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/CMakeLists.txt +142 -0
  218. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/CMakeLists.txt +3 -0
  219. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/aes.h +607 -0
  220. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/aria.h +342 -0
  221. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/bignum.h +1137 -0
  222. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/block_cipher.h +76 -0
  223. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/camellia.h +302 -0
  224. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ccm.h +523 -0
  225. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/chacha20.h +230 -0
  226. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/chachapoly.h +339 -0
  227. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/cipher.h +1173 -0
  228. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/cmac.h +209 -0
  229. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/config_adjust_test_accelerators.h +111 -0
  230. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_adjust_config_enable_builtins.h +820 -0
  231. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_adjust_config_tweak_builtins.h +203 -0
  232. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_builtin_composites.h +278 -0
  233. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_builtin_key_derivation.h +120 -0
  234. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/crypto_builtin_primitives.h +136 -0
  235. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ctr_drbg.h +554 -0
  236. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ecdsa.h +667 -0
  237. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ecjpake.h +296 -0
  238. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ecp.h +1452 -0
  239. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/entropy.h +253 -0
  240. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/error_common.h +79 -0
  241. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/gcm.h +382 -0
  242. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/hmac_drbg.h +426 -0
  243. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/md5.h +169 -0
  244. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/pkcs5.h +123 -0
  245. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/poly1305.h +165 -0
  246. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/ripemd160.h +120 -0
  247. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/rsa.h +821 -0
  248. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha1.h +196 -0
  249. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha256.h +180 -0
  250. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha3.h +189 -0
  251. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private/sha512.h +188 -0
  252. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/include/mbedtls/private_access.h +53 -0
  253. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aes.c +2236 -0
  254. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesce.c +624 -0
  255. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesce.h +135 -0
  256. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesni.c +846 -0
  257. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aesni.h +162 -0
  258. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/aria.c +966 -0
  259. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum.c +2635 -0
  260. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.c +1242 -0
  261. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core.h +885 -0
  262. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_core_invasive.h +38 -0
  263. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_internal.h +164 -0
  264. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod.c +394 -0
  265. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod.h +452 -0
  266. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod_raw.c +276 -0
  267. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod_raw.h +416 -0
  268. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bignum_mod_raw_invasive.h +34 -0
  269. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/block_cipher.c +207 -0
  270. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/block_cipher_internal.h +99 -0
  271. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/bn_mul.h +1094 -0
  272. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/camellia.c +1055 -0
  273. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ccm.c +773 -0
  274. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chacha20.c +576 -0
  275. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chacha20_neon.c +294 -0
  276. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chacha20_neon.h +62 -0
  277. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/chachapoly.c +479 -0
  278. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher.c +1126 -0
  279. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher_invasive.h +28 -0
  280. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher_wrap.c +1937 -0
  281. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cipher_wrap.h +173 -0
  282. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/cmac.c +911 -0
  283. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ctr.h +35 -0
  284. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ctr_drbg.c +1017 -0
  285. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecdsa.c +826 -0
  286. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecjpake.c +1206 -0
  287. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp.c +3478 -0
  288. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_curves.c +4798 -0
  289. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ecp_invasive.h +220 -0
  290. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/entropy.c +677 -0
  291. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/entropy_poll.c +89 -0
  292. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/entropy_poll.h +44 -0
  293. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/gcm.c +1314 -0
  294. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/hmac_drbg.c +639 -0
  295. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/md5.c +419 -0
  296. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/md_psa.h +26 -0
  297. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/poly1305.c +488 -0
  298. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_aead.c +648 -0
  299. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_aead.h +499 -0
  300. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_cipher.c +752 -0
  301. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_cipher.h +314 -0
  302. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.c +921 -0
  303. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ecp.h +516 -0
  304. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ffdh.c +781 -0
  305. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_ffdh.h +131 -0
  306. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_hash.c +458 -0
  307. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_hash.h +211 -0
  308. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_mac.c +495 -0
  309. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_mac.h +264 -0
  310. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_pake.c +568 -0
  311. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_pake.h +159 -0
  312. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.c +698 -0
  313. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_rsa.h +321 -0
  314. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_xof.c +149 -0
  315. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_crypto_xof.h +155 -0
  316. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_util_internal.c +282 -0
  317. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/psa_util_internal.h +132 -0
  318. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/ripemd160.c +482 -0
  319. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa.c +2720 -0
  320. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_alt_helpers.c +294 -0
  321. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_alt_helpers.h +185 -0
  322. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_internal.h +172 -0
  323. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/rsa_invasive.h +57 -0
  324. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha1.c +473 -0
  325. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha256.c +956 -0
  326. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha3.c +951 -0
  327. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/builtin/src/sha512.c +1089 -0
  328. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/driver.cmake +54 -0
  329. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/CMakeLists.txt +45 -0
  330. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/Makefile.inc +5 -0
  331. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/README.md +5 -0
  332. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/Hacl_Curve25519.h +25 -0
  333. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlib/FStar_UInt128.h +124 -0
  334. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlib/FStar_UInt64_FStar_UInt32_FStar_UInt16_FStar_UInt8.h +280 -0
  335. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlib.h +29 -0
  336. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/c_endianness.h +206 -0
  337. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/builtin.h +16 -0
  338. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/callconv.h +48 -0
  339. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/compat.h +34 -0
  340. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/debug.h +57 -0
  341. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/target.h +102 -0
  342. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/types.h +61 -0
  343. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/kremlin/internal/wasmsupport.h +5 -0
  344. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/vs2013/Hacl_Curve25519.h +21 -0
  345. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/include/tf-psa-crypto/private/everest/x25519.h +48 -0
  346. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/Hacl_Curve25519.c +760 -0
  347. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/Hacl_Curve25519_joined.c +50 -0
  348. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/kremlib/FStar_UInt128_extracted.c +413 -0
  349. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/kremlib/FStar_UInt64_FStar_UInt32_FStar_UInt16_FStar_UInt8.c +100 -0
  350. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/legacy/Hacl_Curve25519.c +805 -0
  351. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/everest/library/x25519.c +52 -0
  352. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/CMakeLists.txt +36 -0
  353. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/Makefile.inc +5 -0
  354. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/README.md +4 -0
  355. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m/README.md +544 -0
  356. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m/p256-m.c +1514 -0
  357. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m/p256-m.h +135 -0
  358. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m_driver_entrypoints.c +312 -0
  359. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/p256-m/p256-m_driver_entrypoints.h +219 -0
  360. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/CMakeLists.txt +14 -0
  361. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/Makefile.inc +6 -0
  362. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/include/tf-psa-crypto/private/crypto_struct_pqcp.h +52 -0
  363. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/fips202_psa.h +117 -0
  364. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/pqcp-config.h +68 -0
  365. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/psa_crypto_mldsa.c +528 -0
  366. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/psa_crypto_mldsa.h +301 -0
  367. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/wrap_mldsa_native.c +66 -0
  368. package/cpp/contrib/mbedtls/tf-psa-crypto/drivers/pqcp/src/wrap_mldsa_native.h +37 -0
  369. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/CMakeLists.txt +3 -0
  370. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/lmots.c +768 -0
  371. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/lmots.h +272 -0
  372. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/lms.c +772 -0
  373. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/md.c +1182 -0
  374. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/md_wrap.h +46 -0
  375. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/nist_kw.c +458 -0
  376. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk.c +1464 -0
  377. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_ecc.c +175 -0
  378. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_internal.h +188 -0
  379. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_rsa.c +106 -0
  380. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_wrap.c +667 -0
  381. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pk_wrap.h +101 -0
  382. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pkparse.c +1351 -0
  383. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pkwrite.c +574 -0
  384. package/cpp/contrib/mbedtls/tf-psa-crypto/extras/pkwrite.h +101 -0
  385. package/cpp/contrib/mbedtls/tf-psa-crypto/framework/CMakeLists.txt +1 -0
  386. package/cpp/contrib/mbedtls/tf-psa-crypto/framework/LICENSE +553 -0
  387. package/cpp/contrib/mbedtls/tf-psa-crypto/include/CMakeLists.txt +41 -0
  388. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/asn1.h +623 -0
  389. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/asn1write.h +397 -0
  390. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/base64.h +81 -0
  391. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/compat-3-crypto.h +43 -0
  392. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/constant_time.h +36 -0
  393. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/lms.h +431 -0
  394. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/md.h +538 -0
  395. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/memory_buffer_alloc.h +142 -0
  396. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/nist_kw.h +106 -0
  397. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/pem.h +157 -0
  398. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/pk.h +888 -0
  399. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/platform.h +513 -0
  400. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/platform_time.h +79 -0
  401. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/platform_util.h +197 -0
  402. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/private/pk_private.h +150 -0
  403. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/psa_util.h +199 -0
  404. package/cpp/contrib/mbedtls/tf-psa-crypto/include/mbedtls/threading.h +412 -0
  405. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto.h +6082 -0
  406. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_compat.h +74 -0
  407. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_config.h +2073 -0
  408. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_common.h +42 -0
  409. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_contexts_composites.h +182 -0
  410. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_contexts_key_derivation.h +54 -0
  411. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_contexts_primitives.h +135 -0
  412. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_driver_random.h +49 -0
  413. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_extra.h +2115 -0
  414. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_platform.h +94 -0
  415. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_sizes.h +1301 -0
  416. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_struct.h +682 -0
  417. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_types.h +466 -0
  418. package/cpp/contrib/mbedtls/tf-psa-crypto/include/psa/crypto_values.h +2827 -0
  419. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/build_info.h +193 -0
  420. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_auto_enabled.h +23 -0
  421. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_dependencies.h +58 -0
  422. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_derived.h +135 -0
  423. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_key_pair_types.h +93 -0
  424. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_support.h +211 -0
  425. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/private/crypto_adjust_config_synonyms.h +41 -0
  426. package/cpp/contrib/mbedtls/tf-psa-crypto/include/tf-psa-crypto/version.h +44 -0
  427. package/cpp/contrib/mbedtls/tf-psa-crypto/objlib.cmake +71 -0
  428. package/cpp/contrib/mbedtls/tf-psa-crypto/pkgconfig/CMakeLists.txt +15 -0
  429. package/cpp/contrib/mbedtls/tf-psa-crypto/pkgconfig/JoinPaths.cmake +27 -0
  430. package/cpp/contrib/mbedtls/tf-psa-crypto/pkgconfig/tfpsacrypto.pc.in +10 -0
  431. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/CMakeLists.txt +3 -0
  432. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/memory_buffer_alloc.c +751 -0
  433. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/platform.c +362 -0
  434. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/platform_util.c +428 -0
  435. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/threading.c +311 -0
  436. package/cpp/contrib/mbedtls/tf-psa-crypto/platform/threading_internal.h +75 -0
  437. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/CMakeLists.txt +3 -0
  438. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/asn1parse.c +483 -0
  439. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/asn1write.c +465 -0
  440. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/base64.c +322 -0
  441. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/base64_internal.h +45 -0
  442. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/constant_time.c +247 -0
  443. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/constant_time_impl.h +514 -0
  444. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/constant_time_internal.h +540 -0
  445. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/crypto_oid.h +354 -0
  446. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/oid.c +469 -0
  447. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/pem.c +458 -0
  448. package/cpp/contrib/mbedtls/tf-psa-crypto/utilities/pkcs5.c +475 -0
  449. package/cpp/contrib/monero/LICENSE +41 -0
  450. package/cpp/contrib/monero/bsx/common/perf_timer.h +15 -0
  451. package/cpp/contrib/monero/bsx/common/threadpool.h +43 -0
  452. package/cpp/contrib/monero/bsx/hex.h +25 -0
  453. package/cpp/contrib/monero/bsx/misc_log_ex.h +61 -0
  454. package/cpp/contrib/monero/bsx/random.cpp +20 -0
  455. package/cpp/contrib/monero/contrib/epee/include/int-util.h +320 -0
  456. package/cpp/contrib/monero/contrib/epee/include/memwipe.h +81 -0
  457. package/cpp/contrib/monero/contrib/epee/include/misc_language.h +109 -0
  458. package/cpp/contrib/monero/contrib/epee/include/mlocker.h +89 -0
  459. package/cpp/contrib/monero/contrib/epee/include/span.h +189 -0
  460. package/cpp/contrib/monero/contrib/epee/include/warnings.h +31 -0
  461. package/cpp/contrib/monero/contrib/epee/src/memwipe.c +115 -0
  462. package/cpp/contrib/monero/contrib/epee/src/mlocker.cpp +197 -0
  463. package/cpp/contrib/monero/src/common/aligned.c +139 -0
  464. package/cpp/contrib/monero/src/common/aligned.h +42 -0
  465. package/cpp/contrib/monero/src/common/base58.cpp +259 -0
  466. package/cpp/contrib/monero/src/common/base58.h +46 -0
  467. package/cpp/contrib/monero/src/common/pod-class.h +43 -0
  468. package/cpp/contrib/monero/src/common/varint.h +138 -0
  469. package/cpp/contrib/monero/src/crypto/chacha.h +96 -0
  470. package/cpp/contrib/monero/src/crypto/crypto-ops-data.c +881 -0
  471. package/cpp/contrib/monero/src/crypto/crypto-ops.c +3891 -0
  472. package/cpp/contrib/monero/src/crypto/crypto-ops.h +167 -0
  473. package/cpp/contrib/monero/src/crypto/crypto.cpp +792 -0
  474. package/cpp/contrib/monero/src/crypto/crypto.h +359 -0
  475. package/cpp/contrib/monero/src/crypto/generic-ops.h +103 -0
  476. package/cpp/contrib/monero/src/crypto/hash-ops.h +105 -0
  477. package/cpp/contrib/monero/src/crypto/hash.c +57 -0
  478. package/cpp/contrib/monero/src/crypto/hash.h +110 -0
  479. package/cpp/contrib/monero/src/crypto/keccak.c +239 -0
  480. package/cpp/contrib/monero/src/crypto/keccak.h +40 -0
  481. package/cpp/contrib/monero/src/crypto/random.h +46 -0
  482. package/cpp/contrib/monero/src/crypto/tree-hash.c +257 -0
  483. package/cpp/contrib/monero/src/crypto/wallet/crypto.h +57 -0
  484. package/cpp/contrib/monero/src/crypto/wallet/ops.h +32 -0
  485. package/cpp/contrib/monero/src/cryptonote_config.h +365 -0
  486. package/cpp/contrib/monero/src/device/device.cpp +105 -0
  487. package/cpp/contrib/monero/src/device/device.hpp +274 -0
  488. package/cpp/contrib/monero/src/device/device_default.cpp +343 -0
  489. package/cpp/contrib/monero/src/device/device_default.hpp +156 -0
  490. package/cpp/contrib/monero/src/ringct/bulletproofs.cc +1097 -0
  491. package/cpp/contrib/monero/src/ringct/bulletproofs.h +51 -0
  492. package/cpp/contrib/monero/src/ringct/bulletproofs_plus.cc +1120 -0
  493. package/cpp/contrib/monero/src/ringct/bulletproofs_plus.h +49 -0
  494. package/cpp/contrib/monero/src/ringct/multiexp.cc +708 -0
  495. package/cpp/contrib/monero/src/ringct/multiexp.h +72 -0
  496. package/cpp/contrib/monero/src/ringct/rctCryptoOps.c +221 -0
  497. package/cpp/contrib/monero/src/ringct/rctCryptoOps.h +37 -0
  498. package/cpp/contrib/monero/src/ringct/rctOps.cpp +727 -0
  499. package/cpp/contrib/monero/src/ringct/rctOps.h +191 -0
  500. package/cpp/contrib/monero/src/ringct/rctSigs.cpp +1652 -0
  501. package/cpp/contrib/monero/src/ringct/rctSigs.h +143 -0
  502. package/cpp/contrib/monero/src/ringct/rctTypes.cpp +338 -0
  503. package/cpp/contrib/monero/src/ringct/rctTypes.h +842 -0
  504. package/cpp/contrib/monero/src/serialization/binary_archive.h +243 -0
  505. package/cpp/contrib/monero/src/serialization/container.h +146 -0
  506. package/cpp/contrib/monero/src/serialization/containers.h +127 -0
  507. package/cpp/contrib/monero/src/serialization/crypto.h +97 -0
  508. package/cpp/contrib/monero/src/serialization/debug_archive.h +53 -0
  509. package/cpp/contrib/monero/src/serialization/json_archive.h +193 -0
  510. package/cpp/contrib/monero/src/serialization/serialization.h +328 -0
  511. package/cpp/contrib/monero/src/serialization/variant.h +50 -0
  512. package/cpp/contrib/nlohmann/LICENSE.MIT +21 -0
  513. package/cpp/contrib/nlohmann/json.hpp +25526 -0
  514. package/cpp/contrib/nlohmann/json_fwd.hpp +187 -0
  515. package/cpp/contrib/notes.txt +96 -0
  516. package/cpp/contrib/secp256k1/CHANGELOG.md +214 -0
  517. package/cpp/contrib/secp256k1/CMakeLists.txt +392 -0
  518. package/cpp/contrib/secp256k1/CMakePresets.json +18 -0
  519. package/cpp/contrib/secp256k1/CONTRIBUTING.md +111 -0
  520. package/cpp/contrib/secp256k1/COPYING +19 -0
  521. package/cpp/contrib/secp256k1/Makefile.am +332 -0
  522. package/cpp/contrib/secp256k1/README.md +176 -0
  523. package/cpp/contrib/secp256k1/SECURITY.md +15 -0
  524. package/cpp/contrib/secp256k1/autogen.sh +3 -0
  525. package/cpp/contrib/secp256k1/ci/ci.sh +147 -0
  526. package/cpp/contrib/secp256k1/ci/linux-debian.Dockerfile +84 -0
  527. package/cpp/contrib/secp256k1/ci/travis.sh +68 -0
  528. package/cpp/contrib/secp256k1/cmake/CheckArm32Assembly.cmake +6 -0
  529. package/cpp/contrib/secp256k1/cmake/CheckMemorySanitizer.cmake +18 -0
  530. package/cpp/contrib/secp256k1/cmake/CheckStringOptionValue.cmake +10 -0
  531. package/cpp/contrib/secp256k1/cmake/CheckX86_64Assembly.cmake +15 -0
  532. package/cpp/contrib/secp256k1/cmake/DiscoverTests.cmake +71 -0
  533. package/cpp/contrib/secp256k1/cmake/FindValgrind.cmake +41 -0
  534. package/cpp/contrib/secp256k1/cmake/GeneratePkgConfigFile.cmake +8 -0
  535. package/cpp/contrib/secp256k1/cmake/TryAppendCFlags.cmake +24 -0
  536. package/cpp/contrib/secp256k1/cmake/arm-linux-gnueabihf.toolchain.cmake +3 -0
  537. package/cpp/contrib/secp256k1/cmake/config.cmake.in +5 -0
  538. package/cpp/contrib/secp256k1/cmake/source_arm32.s +9 -0
  539. package/cpp/contrib/secp256k1/cmake/x86_64-w64-mingw32.toolchain.cmake +3 -0
  540. package/cpp/contrib/secp256k1/configure.ac +590 -0
  541. package/cpp/contrib/secp256k1/contrib/lax_der_parsing.c +148 -0
  542. package/cpp/contrib/secp256k1/contrib/lax_der_parsing.h +97 -0
  543. package/cpp/contrib/secp256k1/contrib/lax_der_privatekey_parsing.c +112 -0
  544. package/cpp/contrib/secp256k1/contrib/lax_der_privatekey_parsing.h +95 -0
  545. package/cpp/contrib/secp256k1/doc/ellswift.md +483 -0
  546. package/cpp/contrib/secp256k1/doc/musig.md +54 -0
  547. package/cpp/contrib/secp256k1/doc/release-process.md +94 -0
  548. package/cpp/contrib/secp256k1/doc/safegcd_implementation.md +819 -0
  549. package/cpp/contrib/secp256k1/examples/CMakeLists.txt +33 -0
  550. package/cpp/contrib/secp256k1/examples/EXAMPLES_COPYING +121 -0
  551. package/cpp/contrib/secp256k1/examples/ecdh.c +121 -0
  552. package/cpp/contrib/secp256k1/examples/ecdsa.c +138 -0
  553. package/cpp/contrib/secp256k1/examples/ellswift.c +122 -0
  554. package/cpp/contrib/secp256k1/examples/examples_util.h +108 -0
  555. package/cpp/contrib/secp256k1/examples/musig.c +261 -0
  556. package/cpp/contrib/secp256k1/examples/schnorr.c +154 -0
  557. package/cpp/contrib/secp256k1/include/secp256k1.h +877 -0
  558. package/cpp/contrib/secp256k1/include/secp256k1_dleag.h +150 -0
  559. package/cpp/contrib/secp256k1/include/secp256k1_ecdh.h +63 -0
  560. package/cpp/contrib/secp256k1/include/secp256k1_ecdsaotves.h +55 -0
  561. package/cpp/contrib/secp256k1/include/secp256k1_ed25519.h +239 -0
  562. package/cpp/contrib/secp256k1/include/secp256k1_ellswift.h +200 -0
  563. package/cpp/contrib/secp256k1/include/secp256k1_extrakeys.h +250 -0
  564. package/cpp/contrib/secp256k1/include/secp256k1_generator.h +106 -0
  565. package/cpp/contrib/secp256k1/include/secp256k1_hash.h +45 -0
  566. package/cpp/contrib/secp256k1/include/secp256k1_musig.h +588 -0
  567. package/cpp/contrib/secp256k1/include/secp256k1_preallocated.h +134 -0
  568. package/cpp/contrib/secp256k1/include/secp256k1_recovery.h +123 -0
  569. package/cpp/contrib/secp256k1/include/secp256k1_schnorrsig.h +190 -0
  570. package/cpp/contrib/secp256k1/include/visibility.h +58 -0
  571. package/cpp/contrib/secp256k1/libsecp256k1.pc.in +12 -0
  572. package/cpp/contrib/secp256k1/sage/gen_exhaustive_groups.sage +156 -0
  573. package/cpp/contrib/secp256k1/sage/gen_split_lambda_constants.sage +123 -0
  574. package/cpp/contrib/secp256k1/sage/group_prover.sage +353 -0
  575. package/cpp/contrib/secp256k1/sage/prove_group_implementations.sage +285 -0
  576. package/cpp/contrib/secp256k1/sage/secp256k1_params.sage +39 -0
  577. package/cpp/contrib/secp256k1/sage/weierstrass_prover.sage +275 -0
  578. package/cpp/contrib/secp256k1/src/CMakeLists.txt +253 -0
  579. package/cpp/contrib/secp256k1/src/asm/field_10x26_arm.s +916 -0
  580. package/cpp/contrib/secp256k1/src/assumptions.h +87 -0
  581. package/cpp/contrib/secp256k1/src/checkmem.h +117 -0
  582. package/cpp/contrib/secp256k1/src/ecdsa.h +21 -0
  583. package/cpp/contrib/secp256k1/src/ecdsa_impl.h +312 -0
  584. package/cpp/contrib/secp256k1/src/eckey.h +28 -0
  585. package/cpp/contrib/secp256k1/src/eckey_impl.h +94 -0
  586. package/cpp/contrib/secp256k1/src/ecmult.h +64 -0
  587. package/cpp/contrib/secp256k1/src/ecmult_compute_table.h +16 -0
  588. package/cpp/contrib/secp256k1/src/ecmult_compute_table_impl.h +49 -0
  589. package/cpp/contrib/secp256k1/src/ecmult_const.h +38 -0
  590. package/cpp/contrib/secp256k1/src/ecmult_const_impl.h +402 -0
  591. package/cpp/contrib/secp256k1/src/ecmult_gen.h +144 -0
  592. package/cpp/contrib/secp256k1/src/ecmult_gen_compute_table.h +14 -0
  593. package/cpp/contrib/secp256k1/src/ecmult_gen_compute_table_impl.h +108 -0
  594. package/cpp/contrib/secp256k1/src/ecmult_gen_impl.h +341 -0
  595. package/cpp/contrib/secp256k1/src/ecmult_impl.h +869 -0
  596. package/cpp/contrib/secp256k1/src/field.h +354 -0
  597. package/cpp/contrib/secp256k1/src/field_10x26.h +57 -0
  598. package/cpp/contrib/secp256k1/src/field_10x26_impl.h +1234 -0
  599. package/cpp/contrib/secp256k1/src/field_5x52.h +62 -0
  600. package/cpp/contrib/secp256k1/src/field_5x52_impl.h +524 -0
  601. package/cpp/contrib/secp256k1/src/field_5x52_int128_impl.h +274 -0
  602. package/cpp/contrib/secp256k1/src/field_impl.h +462 -0
  603. package/cpp/contrib/secp256k1/src/group.h +224 -0
  604. package/cpp/contrib/secp256k1/src/group_impl.h +1024 -0
  605. package/cpp/contrib/secp256k1/src/hash.h +54 -0
  606. package/cpp/contrib/secp256k1/src/hash_impl.h +360 -0
  607. package/cpp/contrib/secp256k1/src/hsort.h +33 -0
  608. package/cpp/contrib/secp256k1/src/hsort_impl.h +125 -0
  609. package/cpp/contrib/secp256k1/src/int128.h +90 -0
  610. package/cpp/contrib/secp256k1/src/int128_impl.h +18 -0
  611. package/cpp/contrib/secp256k1/src/int128_native.h +19 -0
  612. package/cpp/contrib/secp256k1/src/int128_native_impl.h +94 -0
  613. package/cpp/contrib/secp256k1/src/int128_struct.h +14 -0
  614. package/cpp/contrib/secp256k1/src/int128_struct_impl.h +205 -0
  615. package/cpp/contrib/secp256k1/src/modinv32.h +43 -0
  616. package/cpp/contrib/secp256k1/src/modinv32_impl.h +725 -0
  617. package/cpp/contrib/secp256k1/src/modinv64.h +47 -0
  618. package/cpp/contrib/secp256k1/src/modinv64_impl.h +780 -0
  619. package/cpp/contrib/secp256k1/src/modules/dleag/Makefile.am.include +3 -0
  620. package/cpp/contrib/secp256k1/src/modules/dleag/main_impl.h +1073 -0
  621. package/cpp/contrib/secp256k1/src/modules/dleag/tests_impl.h +68 -0
  622. package/cpp/contrib/secp256k1/src/modules/ecdh/Makefile.am.include +5 -0
  623. package/cpp/contrib/secp256k1/src/modules/ecdh/bench_impl.h +54 -0
  624. package/cpp/contrib/secp256k1/src/modules/ecdh/main_impl.h +79 -0
  625. package/cpp/contrib/secp256k1/src/modules/ecdh/tests_impl.h +218 -0
  626. package/cpp/contrib/secp256k1/src/modules/ecdsaotves/Makefile.am.include +3 -0
  627. package/cpp/contrib/secp256k1/src/modules/ecdsaotves/main_impl.h +365 -0
  628. package/cpp/contrib/secp256k1/src/modules/ecdsaotves/tests_impl.h +49 -0
  629. package/cpp/contrib/secp256k1/src/modules/ed25519/Makefile.am.include +16 -0
  630. package/cpp/contrib/secp256k1/src/modules/ed25519/common.h +68 -0
  631. package/cpp/contrib/secp256k1/src/modules/ed25519/core_ed25519.h +229 -0
  632. package/cpp/contrib/secp256k1/src/modules/ed25519/main_impl.h +98 -0
  633. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/common.h +21 -0
  634. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/ed25519_ref10.h +2698 -0
  635. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/ed25519_ref10_fe_25_5.h +1056 -0
  636. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/ed25519_ref10_fe_51.h +518 -0
  637. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/base.h +1344 -0
  638. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/base2.h +40 -0
  639. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/constants.h +40 -0
  640. package/cpp/contrib/secp256k1/src/modules/ed25519/ref10/fe_25_5/fe.h +220 -0
  641. package/cpp/contrib/secp256k1/src/modules/ed25519/scalarmult_ed25519_ref10.h +417 -0
  642. package/cpp/contrib/secp256k1/src/modules/ed25519/sha512/hash_sha512.h +14 -0
  643. package/cpp/contrib/secp256k1/src/modules/ed25519/sha512/hash_sha512_cp.h +287 -0
  644. package/cpp/contrib/secp256k1/src/modules/ed25519/tests_impl.h +109 -0
  645. package/cpp/contrib/secp256k1/src/modules/ed25519/utils.h +97 -0
  646. package/cpp/contrib/secp256k1/src/modules/ellswift/Makefile.am.include +5 -0
  647. package/cpp/contrib/secp256k1/src/modules/ellswift/bench_impl.h +106 -0
  648. package/cpp/contrib/secp256k1/src/modules/ellswift/main_impl.h +581 -0
  649. package/cpp/contrib/secp256k1/src/modules/ellswift/tests_exhaustive_impl.h +39 -0
  650. package/cpp/contrib/secp256k1/src/modules/ellswift/tests_impl.h +544 -0
  651. package/cpp/contrib/secp256k1/src/modules/extrakeys/Makefile.am.include +4 -0
  652. package/cpp/contrib/secp256k1/src/modules/extrakeys/main_impl.h +285 -0
  653. package/cpp/contrib/secp256k1/src/modules/extrakeys/tests_exhaustive_impl.h +68 -0
  654. package/cpp/contrib/secp256k1/src/modules/extrakeys/tests_impl.h +484 -0
  655. package/cpp/contrib/secp256k1/src/modules/generator/Makefile.am.include +9 -0
  656. package/cpp/contrib/secp256k1/src/modules/generator/main_impl.h +271 -0
  657. package/cpp/contrib/secp256k1/src/modules/generator/tests_impl.h +227 -0
  658. package/cpp/contrib/secp256k1/src/modules/musig/Makefile.am.include +8 -0
  659. package/cpp/contrib/secp256k1/src/modules/musig/keyagg.h +32 -0
  660. package/cpp/contrib/secp256k1/src/modules/musig/keyagg_impl.h +275 -0
  661. package/cpp/contrib/secp256k1/src/modules/musig/main_impl.h +12 -0
  662. package/cpp/contrib/secp256k1/src/modules/musig/session.h +24 -0
  663. package/cpp/contrib/secp256k1/src/modules/musig/session_impl.h +795 -0
  664. package/cpp/contrib/secp256k1/src/modules/musig/tests_impl.h +1161 -0
  665. package/cpp/contrib/secp256k1/src/modules/musig/vectors.h +346 -0
  666. package/cpp/contrib/secp256k1/src/modules/recovery/Makefile.am.include +5 -0
  667. package/cpp/contrib/secp256k1/src/modules/recovery/bench_impl.h +62 -0
  668. package/cpp/contrib/secp256k1/src/modules/recovery/main_impl.h +159 -0
  669. package/cpp/contrib/secp256k1/src/modules/recovery/tests_exhaustive_impl.h +148 -0
  670. package/cpp/contrib/secp256k1/src/modules/recovery/tests_impl.h +339 -0
  671. package/cpp/contrib/secp256k1/src/modules/schnorrsig/Makefile.am.include +5 -0
  672. package/cpp/contrib/secp256k1/src/modules/schnorrsig/bench_impl.h +104 -0
  673. package/cpp/contrib/secp256k1/src/modules/schnorrsig/main_impl.h +263 -0
  674. package/cpp/contrib/secp256k1/src/modules/schnorrsig/tests_exhaustive_impl.h +214 -0
  675. package/cpp/contrib/secp256k1/src/modules/schnorrsig/tests_impl.h +1010 -0
  676. package/cpp/contrib/secp256k1/src/precompute_ecmult.c +91 -0
  677. package/cpp/contrib/secp256k1/src/precompute_ecmult_gen.c +101 -0
  678. package/cpp/contrib/secp256k1/src/precomputed_ecmult.c +16456 -0
  679. package/cpp/contrib/secp256k1/src/precomputed_ecmult.h +40 -0
  680. package/cpp/contrib/secp256k1/src/precomputed_ecmult_gen.c +1779 -0
  681. package/cpp/contrib/secp256k1/src/precomputed_ecmult_gen.h +28 -0
  682. package/cpp/contrib/secp256k1/src/scalar.h +108 -0
  683. package/cpp/contrib/secp256k1/src/scalar_4x64.h +19 -0
  684. package/cpp/contrib/secp256k1/src/scalar_4x64_impl.h +1010 -0
  685. package/cpp/contrib/secp256k1/src/scalar_8x32.h +19 -0
  686. package/cpp/contrib/secp256k1/src/scalar_8x32_impl.h +830 -0
  687. package/cpp/contrib/secp256k1/src/scalar_impl.h +321 -0
  688. package/cpp/contrib/secp256k1/src/scalar_low.h +24 -0
  689. package/cpp/contrib/secp256k1/src/scalar_low_impl.h +215 -0
  690. package/cpp/contrib/secp256k1/src/scratch.h +44 -0
  691. package/cpp/contrib/secp256k1/src/scratch_impl.h +99 -0
  692. package/cpp/contrib/secp256k1/src/secp256k1.c +896 -0
  693. package/cpp/contrib/secp256k1/src/selftest.h +35 -0
  694. package/cpp/contrib/secp256k1/src/testrand.h +45 -0
  695. package/cpp/contrib/secp256k1/src/testrand_impl.h +162 -0
  696. package/cpp/contrib/secp256k1/src/testutil.h +161 -0
  697. package/cpp/contrib/secp256k1/src/unit_test.c +479 -0
  698. package/cpp/contrib/secp256k1/src/unit_test.h +147 -0
  699. package/cpp/contrib/secp256k1/src/util.h +469 -0
  700. package/cpp/contrib/secp256k1/src/util_local_visibility.h +12 -0
  701. package/cpp/contrib/secp256k1/tools/check-abi.sh +67 -0
  702. package/cpp/contrib/secp256k1/tools/symbol-check.py +72 -0
  703. package/cpp/contrib/secp256k1/tools/test_vectors_musig2_generate.py +656 -0
  704. package/cpp/contrib/secp256k1/tools/tests_wycheproof_generate_ecdh.py +166 -0
  705. package/cpp/contrib/secp256k1/tools/tests_wycheproof_generate_ecdsa.py +111 -0
  706. package/cpp/contrib/secp256k1/tools/wycheproof_utils.py +12 -0
  707. package/cpp/contrib/secp256k1/wasm/test_module.html +320 -0
  708. package/cpp/contrib/secp256k1/wasm/wrap.cpp +331 -0
  709. package/cpp/contrib/sqlite3.h +14349 -0
  710. package/cpp/contrib/sqlite3mc_amalgamation.c +369890 -0
  711. package/cpp/contrib/sqlite3mc_amalgamation.h +15548 -0
  712. package/cpp/contrib/unilib/uninorms.cpp +393 -0
  713. package/cpp/contrib/unilib/uninorms.h +56 -0
  714. package/cpp/contrib/unilib/utf8.cpp +78 -0
  715. package/cpp/contrib/unilib/utf8.h +260 -0
  716. package/cpp/contrib/update_mbedtls.sh +85 -0
  717. package/cpp/crypto/aes.cpp +141 -0
  718. package/cpp/crypto/aes.h +67 -0
  719. package/cpp/crypto/chacha20.cpp +342 -0
  720. package/cpp/crypto/chacha20.h +82 -0
  721. package/cpp/crypto/chacha20poly1305.cpp +91 -0
  722. package/cpp/crypto/chacha20poly1305.h +46 -0
  723. package/cpp/crypto/common.h +85 -0
  724. package/cpp/crypto/ctaes/COPYING +21 -0
  725. package/cpp/crypto/ctaes/README.md +41 -0
  726. package/cpp/crypto/ctaes/bench.c +170 -0
  727. package/cpp/crypto/ctaes/ctaes.c +556 -0
  728. package/cpp/crypto/ctaes/ctaes.h +41 -0
  729. package/cpp/crypto/ctaes/test.c +110 -0
  730. package/cpp/crypto/extkey.cpp +257 -0
  731. package/cpp/crypto/extkey.h +66 -0
  732. package/cpp/crypto/hmac_sha512.cpp +37 -0
  733. package/cpp/crypto/hmac_sha512.h +32 -0
  734. package/cpp/crypto/merkle.cpp +47 -0
  735. package/cpp/crypto/merkle.h +20 -0
  736. package/cpp/crypto/poly1305.cpp +223 -0
  737. package/cpp/crypto/poly1305.h +59 -0
  738. package/cpp/crypto/ripemd160.cpp +292 -0
  739. package/cpp/crypto/ripemd160.h +28 -0
  740. package/cpp/crypto/scrypt-sse2.cpp +141 -0
  741. package/cpp/crypto/scrypt.cpp +336 -0
  742. package/cpp/crypto/scrypt.h +49 -0
  743. package/cpp/crypto/sealed_key.cpp +62 -0
  744. package/cpp/crypto/sealed_key.h +34 -0
  745. package/cpp/crypto/secure_bytes.h +16 -0
  746. package/cpp/crypto/sha512.cpp +206 -0
  747. package/cpp/crypto/sha512.h +29 -0
  748. package/cpp/crypto/util.cpp +77 -0
  749. package/cpp/crypto/util.h +32 -0
  750. package/cpp/debug_rpc.cpp +703 -0
  751. package/cpp/debug_rpc.h +63 -0
  752. package/cpp/electrum.cpp +931 -0
  753. package/cpp/electrum.h +169 -0
  754. package/cpp/engine/xmr_engine.cpp +463 -0
  755. package/cpp/engine/xmr_engine.h +84 -0
  756. package/cpp/engine/xmr_engine_c.cpp +141 -0
  757. package/cpp/engine/xmr_engine_c.h +35 -0
  758. package/cpp/fees.cpp +270 -0
  759. package/cpp/fees.h +60 -0
  760. package/cpp/height_search.cpp +64 -0
  761. package/cpp/height_search.h +27 -0
  762. package/cpp/main.cpp +146 -0
  763. package/cpp/messages_npb.cpp +701 -0
  764. package/cpp/messages_npb.h +667 -0
  765. package/cpp/mnemonic/mnemonic.cpp +715 -0
  766. package/cpp/mnemonic/mnemonic.h +52 -0
  767. package/cpp/mnemonic/wordlists/english.h +1103 -0
  768. package/cpp/mnemonic/wordlists/japanese.h +2212 -0
  769. package/cpp/network/bridge.cpp +232 -0
  770. package/cpp/network/bridge.h +115 -0
  771. package/cpp/network/ca_roots.cpp +114 -0
  772. package/cpp/network/ca_roots.h +12 -0
  773. package/cpp/network/http_client.cpp +261 -0
  774. package/cpp/network/http_client.h +33 -0
  775. package/cpp/network/pem.cpp +84 -0
  776. package/cpp/network/pem.h +22 -0
  777. package/cpp/network/psa_user_config.h +15 -0
  778. package/cpp/network/socks5.cpp +180 -0
  779. package/cpp/network/socks5.h +26 -0
  780. package/cpp/network/tls_transport.cpp +502 -0
  781. package/cpp/network/tls_transport.h +62 -0
  782. package/cpp/network/transport.cpp +169 -0
  783. package/cpp/network/transport.h +115 -0
  784. package/cpp/network/url.cpp +88 -0
  785. package/cpp/network/url.h +25 -0
  786. package/cpp/network/websocket.cpp +253 -0
  787. package/cpp/network/websocket.h +68 -0
  788. package/cpp/network/ws_transport.cpp +361 -0
  789. package/cpp/network/ws_transport.h +72 -0
  790. package/cpp/node_client.cpp +1196 -0
  791. package/cpp/node_client.h +209 -0
  792. package/cpp/node_scan.h +35 -0
  793. package/cpp/schema.cpp +1249 -0
  794. package/cpp/schema.h +132 -0
  795. package/cpp/smsg.cpp +453 -0
  796. package/cpp/smsg.h +43 -0
  797. package/cpp/support/allocators/secure.h +85 -0
  798. package/cpp/support/cleanse.cpp +35 -0
  799. package/cpp/support/cleanse.h +15 -0
  800. package/cpp/support/coop.cpp +319 -0
  801. package/cpp/support/coop.h +157 -0
  802. package/cpp/support/lockedpool.cpp +406 -0
  803. package/cpp/support/lockedpool.h +240 -0
  804. package/cpp/uint256.cpp +62 -0
  805. package/cpp/uint256.h +121 -0
  806. package/cpp/util/base58.cpp +181 -0
  807. package/cpp/util/base58.h +45 -0
  808. package/cpp/util/bech32.cpp +578 -0
  809. package/cpp/util/bech32.h +96 -0
  810. package/cpp/util/debug.cpp +23 -0
  811. package/cpp/util/debug.h +12 -0
  812. package/cpp/util/int.cpp +118 -0
  813. package/cpp/util/int.h +18 -0
  814. package/cpp/util/json.h +26 -0
  815. package/cpp/util/log.cpp +137 -0
  816. package/cpp/util/log.h +37 -0
  817. package/cpp/util/mutex.h +89 -0
  818. package/cpp/util/platform.cpp +90 -0
  819. package/cpp/util/platform.h +17 -0
  820. package/cpp/util/random.cpp +136 -0
  821. package/cpp/util/random.h +28 -0
  822. package/cpp/util/rpc.cpp +192 -0
  823. package/cpp/util/rpc.h +454 -0
  824. package/cpp/util/str.cpp +254 -0
  825. package/cpp/util/str.h +54 -0
  826. package/cpp/util/thread.cpp +17 -0
  827. package/cpp/util/thread.h +12 -0
  828. package/cpp/util/time.cpp +30 -0
  829. package/cpp/util/time.h +30 -0
  830. package/cpp/util/tinyformat.h +1163 -0
  831. package/cpp/util/vector.h +83 -0
  832. package/cpp/version.h.in +12 -0
  833. package/cpp/view_key_chain.h +283 -0
  834. package/cpp/wallet/account_wallet.cpp +668 -0
  835. package/cpp/wallet/account_wallet.h +136 -0
  836. package/cpp/wallet/base_wallet.cpp +302 -0
  837. package/cpp/wallet/base_wallet.h +129 -0
  838. package/cpp/wallet/debug_wallet.cpp +40 -0
  839. package/cpp/wallet/debug_wallet.h +36 -0
  840. package/cpp/wallet/encrypted_seed.cpp +96 -0
  841. package/cpp/wallet/encrypted_seed.h +41 -0
  842. package/cpp/wallet/host_wallet.cpp +85 -0
  843. package/cpp/wallet/host_wallet.h +51 -0
  844. package/cpp/wallet/native_wallet.cpp +34 -0
  845. package/cpp/wallet/native_wallet.h +28 -0
  846. package/cpp/wallet/xmr/account_wallet.cpp +803 -0
  847. package/cpp/wallet/xmr/account_wallet.h +139 -0
  848. package/cpp/wallet/xmr/builder.cpp +568 -0
  849. package/cpp/wallet/xmr/builder.h +65 -0
  850. package/cpp/wallet/xmr/chain.cpp +1437 -0
  851. package/cpp/wallet/xmr/chain.h +213 -0
  852. package/cpp/wallet/xmr/daemon.cpp +583 -0
  853. package/cpp/wallet/xmr/daemon.h +153 -0
  854. package/cpp/wallet/xmr/decoys.cpp +197 -0
  855. package/cpp/wallet/xmr/decoys.h +64 -0
  856. package/cpp/wallet/xmr/epee.cpp +448 -0
  857. package/cpp/wallet/xmr/epee.h +25 -0
  858. package/cpp/wallet/xmr/fee.cpp +169 -0
  859. package/cpp/wallet/xmr/fee.h +61 -0
  860. package/cpp/wallet/xmr/reserve_proof.cpp +169 -0
  861. package/cpp/wallet/xmr/reserve_proof.h +24 -0
  862. package/cpp/wallet/xmr/scanner.cpp +860 -0
  863. package/cpp/wallet/xmr/scanner.h +239 -0
  864. package/lib/commonjs/NativeNitroBSX.js +128 -0
  865. package/lib/commonjs/NativeNitroBSX.js.map +1 -0
  866. package/lib/commonjs/format.js +21 -0
  867. package/lib/commonjs/format.js.map +1 -0
  868. package/lib/commonjs/index.js +50 -0
  869. package/lib/commonjs/index.js.map +1 -0
  870. package/lib/commonjs/service.js +75 -0
  871. package/lib/commonjs/service.js.map +1 -0
  872. package/lib/commonjs/specs/NitroBSX.nitro.js +6 -0
  873. package/lib/commonjs/specs/NitroBSX.nitro.js.map +1 -0
  874. package/lib/module/NativeNitroBSX.js +124 -0
  875. package/lib/module/NativeNitroBSX.js.map +1 -0
  876. package/lib/module/format.js +15 -0
  877. package/lib/module/format.js.map +1 -0
  878. package/lib/module/index.js +7 -0
  879. package/lib/module/index.js.map +1 -0
  880. package/lib/module/service.js +66 -0
  881. package/lib/module/service.js.map +1 -0
  882. package/lib/module/specs/NitroBSX.nitro.js +4 -0
  883. package/lib/module/specs/NitroBSX.nitro.js.map +1 -0
  884. package/lib/typescript/commonjs/package.json +1 -0
  885. package/lib/typescript/commonjs/src/NativeNitroBSX.d.ts +94 -0
  886. package/lib/typescript/commonjs/src/NativeNitroBSX.d.ts.map +1 -0
  887. package/lib/typescript/commonjs/src/format.d.ts +3 -0
  888. package/lib/typescript/commonjs/src/format.d.ts.map +1 -0
  889. package/lib/typescript/commonjs/src/index.d.ts +5 -0
  890. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  891. package/lib/typescript/commonjs/src/service.d.ts +28 -0
  892. package/lib/typescript/commonjs/src/service.d.ts.map +1 -0
  893. package/lib/typescript/commonjs/src/specs/NitroBSX.nitro.d.ts +251 -0
  894. package/lib/typescript/commonjs/src/specs/NitroBSX.nitro.d.ts.map +1 -0
  895. package/lib/typescript/module/package.json +1 -0
  896. package/lib/typescript/module/src/NativeNitroBSX.d.ts +94 -0
  897. package/lib/typescript/module/src/NativeNitroBSX.d.ts.map +1 -0
  898. package/lib/typescript/module/src/format.d.ts +3 -0
  899. package/lib/typescript/module/src/format.d.ts.map +1 -0
  900. package/lib/typescript/module/src/index.d.ts +5 -0
  901. package/lib/typescript/module/src/index.d.ts.map +1 -0
  902. package/lib/typescript/module/src/service.d.ts +28 -0
  903. package/lib/typescript/module/src/service.d.ts.map +1 -0
  904. package/lib/typescript/module/src/specs/NitroBSX.nitro.d.ts +251 -0
  905. package/lib/typescript/module/src/specs/NitroBSX.nitro.d.ts.map +1 -0
  906. package/nitro.json +19 -0
  907. package/nitrogen/generated/android/NitroBSX+autolinking.cmake +81 -0
  908. package/nitrogen/generated/android/NitroBSX+autolinking.gradle +27 -0
  909. package/nitrogen/generated/android/NitroBSXOnLoad.cpp +49 -0
  910. package/nitrogen/generated/android/NitroBSXOnLoad.hpp +34 -0
  911. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitrobsx/NitroBSXOnLoad.kt +35 -0
  912. package/nitrogen/generated/ios/NitroBSX+autolinking.rb +62 -0
  913. package/nitrogen/generated/ios/NitroBSX-Swift-Cxx-Bridge.cpp +17 -0
  914. package/nitrogen/generated/ios/NitroBSX-Swift-Cxx-Bridge.hpp +27 -0
  915. package/nitrogen/generated/ios/NitroBSX-Swift-Cxx-Umbrella.hpp +38 -0
  916. package/nitrogen/generated/ios/NitroBSXAutolinking.mm +35 -0
  917. package/nitrogen/generated/ios/NitroBSXAutolinking.swift +16 -0
  918. package/nitrogen/generated/shared/c++/HybridNitroBSXSpec.cpp +91 -0
  919. package/nitrogen/generated/shared/c++/HybridNitroBSXSpec.hpp +142 -0
  920. package/nitrogen/generated/shared/c++/NotificationPair.hpp +87 -0
  921. package/nitrogen/generated/shared/c++/SignOutcome.hpp +80 -0
  922. package/nitrogen/generated/shared/c++/SignTxResult.hpp +89 -0
  923. package/package.json +172 -4
  924. package/react-native-nitro-bsx.podspec +33 -0
  925. package/react-native.config.js +26 -0
  926. package/scripts/build-ios.sh +49 -0
  927. package/src/NativeNitroBSX.ts +203 -0
  928. package/src/format.ts +14 -0
  929. package/src/index.ts +4 -0
  930. package/src/service.ts +77 -0
  931. package/src/specs/NitroBSX.nitro.ts +300 -0
@@ -0,0 +1,3278 @@
1
+ /*
2
+ * X.509 certificate parsing and verification
3
+ *
4
+ * Copyright The Mbed TLS Contributors
5
+ * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6
+ */
7
+ /*
8
+ * The ITU-T X.509 standard defines a certificate format for PKI.
9
+ *
10
+ * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
11
+ * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
12
+ * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
13
+ *
14
+ * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
15
+ * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
16
+ *
17
+ * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
18
+ */
19
+
20
+ #include "x509_internal.h"
21
+
22
+ #if defined(MBEDTLS_X509_CRT_PARSE_C)
23
+
24
+ #include "mbedtls/x509_crt.h"
25
+ #include "mbedtls/error.h"
26
+ #include "mbedtls/oid.h"
27
+ #include "x509_oid.h"
28
+ #include "mbedtls/platform_util.h"
29
+
30
+ #include <limits.h>
31
+ #include <string.h>
32
+
33
+ #if defined(MBEDTLS_PEM_PARSE_C)
34
+ #include "mbedtls/pem.h"
35
+ #endif
36
+
37
+ #include "psa/crypto.h"
38
+ #include "mbedtls/psa_util.h"
39
+
40
+ #include "mbedtls/platform.h"
41
+
42
+ #if defined(MBEDTLS_THREADING_C)
43
+ #include "mbedtls/threading.h"
44
+ #endif
45
+
46
+ #include "mbedtls_utils.h"
47
+
48
+ #if defined(MBEDTLS_HAVE_TIME)
49
+ #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
50
+ #ifndef WIN32_LEAN_AND_MEAN
51
+ #define WIN32_LEAN_AND_MEAN
52
+ #endif
53
+ #include <windows.h>
54
+ #else
55
+ #include <time.h>
56
+ #endif
57
+ #endif
58
+
59
+ #if defined(MBEDTLS_FS_IO)
60
+ #include <stdio.h>
61
+ #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
62
+ #include <sys/types.h>
63
+ #include <sys/stat.h>
64
+ #if defined(__MBED__)
65
+ #include <platform/mbed_retarget.h>
66
+ #else
67
+ #include <dirent.h>
68
+ #endif /* __MBED__ */
69
+ #include <errno.h>
70
+ #endif /* !_WIN32 || EFIX64 || EFI32 */
71
+ #endif
72
+
73
+ /*
74
+ * Item in a verification chain: cert and flags for it
75
+ */
76
+ typedef struct {
77
+ mbedtls_x509_crt *crt;
78
+ uint32_t flags;
79
+ } x509_crt_verify_chain_item;
80
+
81
+ /*
82
+ * Max size of verification chain: end-entity + intermediates + trusted root
83
+ */
84
+ #define X509_MAX_VERIFY_CHAIN_SIZE (MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2)
85
+
86
+ /* Default profile. Do not remove items unless there are serious security
87
+ * concerns. */
88
+ const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
89
+ {
90
+ /* Hashes from SHA-256 and above. */
91
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
92
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
93
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512) |
94
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA3_256) |
95
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA3_384) |
96
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA3_512),
97
+ 0xFFFFFFF, /* Any PK alg */
98
+ #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
99
+ /* Curves at or above 128-bit security level. Note that this selection
100
+ * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
101
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
102
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
103
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
104
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
105
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
106
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
107
+ 0,
108
+ #else /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
109
+ 0,
110
+ #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
111
+ 2048,
112
+ };
113
+
114
+ /* Next-generation profile. Currently identical to the default, but may
115
+ * be tightened at any time. */
116
+ const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
117
+ {
118
+ /* Hashes from SHA-256 and above. */
119
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
120
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
121
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
122
+ 0xFFFFFFF, /* Any PK alg */
123
+ #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
124
+ /* Curves at or above 128-bit security level. */
125
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
126
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
127
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
128
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
129
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
130
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
131
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
132
+ #else
133
+ 0,
134
+ #endif
135
+ 2048,
136
+ };
137
+
138
+ /*
139
+ * NSA Suite B Profile
140
+ */
141
+ const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
142
+ {
143
+ /* Only SHA-256 and 384 */
144
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
145
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
146
+ /* Only ECDSA */
147
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
148
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
149
+ #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
150
+ /* Only NIST P-256 and P-384 */
151
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
152
+ MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
153
+ #else /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
154
+ 0,
155
+ #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
156
+ 0,
157
+ };
158
+
159
+ /*
160
+ * Empty / all-forbidden profile
161
+ */
162
+ const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
163
+ {
164
+ 0,
165
+ 0,
166
+ 0,
167
+ (uint32_t) -1,
168
+ };
169
+
170
+ int mbedtls_x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
171
+ mbedtls_md_type_t md_alg)
172
+ {
173
+ if (md_alg == MBEDTLS_MD_NONE) {
174
+ return -1;
175
+ }
176
+
177
+ if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0) {
178
+ return 0;
179
+ }
180
+
181
+ return -1;
182
+ }
183
+
184
+ int mbedtls_x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
185
+ mbedtls_pk_sigalg_t pk_alg)
186
+ {
187
+ if (pk_alg == MBEDTLS_PK_SIGALG_NONE) {
188
+ return -1;
189
+ }
190
+
191
+ if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0) {
192
+ return 0;
193
+ }
194
+
195
+ return -1;
196
+ }
197
+
198
+ /*
199
+ * Check key against profile
200
+ * Return 0 if pk is acceptable for this profile, -1 otherwise
201
+ */
202
+ static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
203
+ const mbedtls_pk_context *pk)
204
+ {
205
+ const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
206
+
207
+ #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
208
+ if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
209
+ if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen) {
210
+ return 0;
211
+ }
212
+
213
+ return -1;
214
+ }
215
+ #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
216
+
217
+ #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
218
+ if (pk_alg == MBEDTLS_PK_ECDSA ||
219
+ pk_alg == MBEDTLS_PK_ECKEY ||
220
+ pk_alg == MBEDTLS_PK_ECKEY_DH) {
221
+ const mbedtls_ecp_group_id gid = mbedtls_pk_get_ec_group_id(pk);
222
+
223
+ if (gid == MBEDTLS_ECP_DP_NONE) {
224
+ return -1;
225
+ }
226
+
227
+ if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0) {
228
+ return 0;
229
+ }
230
+
231
+ return -1;
232
+ }
233
+ #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
234
+
235
+ return -1;
236
+ }
237
+
238
+ /*
239
+ * Like memcmp, but case-insensitive and always returns -1 if different
240
+ */
241
+ static int x509_memcasecmp(const void *s1, const void *s2, size_t len)
242
+ {
243
+ size_t i;
244
+ unsigned char diff;
245
+ const unsigned char *n1 = s1, *n2 = s2;
246
+
247
+ for (i = 0; i < len; i++) {
248
+ diff = n1[i] ^ n2[i];
249
+
250
+ if (diff == 0) {
251
+ continue;
252
+ }
253
+
254
+ if (diff == 32 &&
255
+ ((n1[i] >= 'a' && n1[i] <= 'z') ||
256
+ (n1[i] >= 'A' && n1[i] <= 'Z'))) {
257
+ continue;
258
+ }
259
+
260
+ return -1;
261
+ }
262
+
263
+ return 0;
264
+ }
265
+
266
+ /*
267
+ * Return 0 if name matches wildcard, -1 otherwise
268
+ */
269
+ static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name)
270
+ {
271
+ size_t i;
272
+ size_t cn_idx = 0, cn_len = strlen(cn);
273
+
274
+ /* We can't have a match if there is no wildcard to match */
275
+ if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.') {
276
+ return -1;
277
+ }
278
+
279
+ for (i = 0; i < cn_len; ++i) {
280
+ if (cn[i] == '.') {
281
+ cn_idx = i;
282
+ break;
283
+ }
284
+ }
285
+
286
+ if (cn_idx == 0) {
287
+ return -1;
288
+ }
289
+
290
+ if (cn_len - cn_idx == name->len - 1 &&
291
+ x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
292
+ return 0;
293
+ }
294
+
295
+ return -1;
296
+ }
297
+
298
+ /*
299
+ * Compare two X.509 strings, case-insensitive, and allowing for some encoding
300
+ * variations (but not all).
301
+ *
302
+ * Return 0 if equal, -1 otherwise.
303
+ */
304
+ static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b)
305
+ {
306
+ if (a->tag == b->tag &&
307
+ a->len == b->len &&
308
+ memcmp(a->p, b->p, b->len) == 0) {
309
+ return 0;
310
+ }
311
+
312
+ if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
313
+ (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
314
+ a->len == b->len &&
315
+ x509_memcasecmp(a->p, b->p, b->len) == 0) {
316
+ return 0;
317
+ }
318
+
319
+ return -1;
320
+ }
321
+
322
+ /*
323
+ * Compare two X.509 Names (aka rdnSequence).
324
+ *
325
+ * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
326
+ * we sometimes return unequal when the full algorithm would return equal,
327
+ * but never the other way. (In particular, we don't do Unicode normalisation
328
+ * or space folding.)
329
+ *
330
+ * Return 0 if equal, -1 otherwise.
331
+ */
332
+ static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b)
333
+ {
334
+ /* Avoid recursion, it might not be optimised by the compiler */
335
+ while (a != NULL || b != NULL) {
336
+ if (a == NULL || b == NULL) {
337
+ return -1;
338
+ }
339
+
340
+ /* type */
341
+ if (a->oid.tag != b->oid.tag ||
342
+ a->oid.len != b->oid.len ||
343
+ memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
344
+ return -1;
345
+ }
346
+
347
+ /* value */
348
+ if (x509_string_cmp(&a->val, &b->val) != 0) {
349
+ return -1;
350
+ }
351
+
352
+ /* structure of the list of sets */
353
+ if (a->next_merged != b->next_merged) {
354
+ return -1;
355
+ }
356
+
357
+ a = a->next;
358
+ b = b->next;
359
+ }
360
+
361
+ /* a == NULL == b */
362
+ return 0;
363
+ }
364
+
365
+ /*
366
+ * Reset (init or clear) a verify_chain
367
+ */
368
+ static void x509_crt_verify_chain_reset(
369
+ mbedtls_x509_crt_verify_chain *ver_chain)
370
+ {
371
+ size_t i;
372
+
373
+ for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
374
+ ver_chain->items[i].crt = NULL;
375
+ ver_chain->items[i].flags = (uint32_t) -1;
376
+ }
377
+
378
+ ver_chain->len = 0;
379
+
380
+ #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
381
+ ver_chain->trust_ca_cb_result = NULL;
382
+ #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
383
+ }
384
+
385
+ /*
386
+ * Version ::= INTEGER { v1(0), v2(1), v3(2) }
387
+ */
388
+ static int x509_get_version(unsigned char **p,
389
+ const unsigned char *end,
390
+ int *ver)
391
+ {
392
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
393
+ size_t len;
394
+
395
+ if ((ret = mbedtls_asn1_get_tag(p, end, &len,
396
+ MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
397
+ 0)) != 0) {
398
+ if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
399
+ *ver = 0;
400
+ return 0;
401
+ }
402
+
403
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
404
+ }
405
+
406
+ end = *p + len;
407
+
408
+ if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
409
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, ret);
410
+ }
411
+
412
+ if (*p != end) {
413
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION,
414
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
415
+ }
416
+
417
+ return 0;
418
+ }
419
+
420
+ /*
421
+ * Validity ::= SEQUENCE {
422
+ * notBefore Time,
423
+ * notAfter Time }
424
+ */
425
+ static int x509_get_dates(unsigned char **p,
426
+ const unsigned char *end,
427
+ mbedtls_x509_time *from,
428
+ mbedtls_x509_time *to)
429
+ {
430
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
431
+ size_t len;
432
+
433
+ if ((ret = mbedtls_asn1_get_tag(p, end, &len,
434
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
435
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
436
+ }
437
+
438
+ end = *p + len;
439
+
440
+ if ((ret = mbedtls_x509_get_time(p, end, from)) != 0) {
441
+ return ret;
442
+ }
443
+
444
+ if ((ret = mbedtls_x509_get_time(p, end, to)) != 0) {
445
+ return ret;
446
+ }
447
+
448
+ if (*p != end) {
449
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
450
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
451
+ }
452
+
453
+ return 0;
454
+ }
455
+
456
+ /*
457
+ * X.509 v2/v3 unique identifier (not parsed)
458
+ */
459
+ static int x509_get_uid(unsigned char **p,
460
+ const unsigned char *end,
461
+ mbedtls_x509_buf *uid, int n)
462
+ {
463
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
464
+
465
+ if (*p == end) {
466
+ return 0;
467
+ }
468
+
469
+ uid->tag = **p;
470
+
471
+ if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
472
+ MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
473
+ n)) != 0) {
474
+ if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
475
+ return 0;
476
+ }
477
+
478
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
479
+ }
480
+
481
+ uid->p = *p;
482
+ *p += uid->len;
483
+
484
+ return 0;
485
+ }
486
+
487
+ static int x509_get_basic_constraints(unsigned char **p,
488
+ const unsigned char *end,
489
+ int *ca_istrue,
490
+ int *max_pathlen)
491
+ {
492
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
493
+ size_t len;
494
+
495
+ /*
496
+ * BasicConstraints ::= SEQUENCE {
497
+ * cA BOOLEAN DEFAULT FALSE,
498
+ * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
499
+ */
500
+ *ca_istrue = 0; /* DEFAULT FALSE */
501
+ *max_pathlen = 0; /* endless */
502
+
503
+ if ((ret = mbedtls_asn1_get_tag(p, end, &len,
504
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
505
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
506
+ }
507
+
508
+ if (*p == end) {
509
+ /* Empty basicConstraints: valid, not a CA. */
510
+ return 0;
511
+ }
512
+
513
+ if ((size_t) (end - *p) != len) {
514
+ /* Reject junk after the SEQUENCE inside the extension (which is
515
+ * probably benign), and reject a SEQUENCE that extends beyond
516
+ * the extension (could be very dangerous). */
517
+ return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
518
+ }
519
+
520
+ if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
521
+ /* If the SEQUENCE starts with an INTEGER and not a BOOLEAN,
522
+ * according to RFC 5280, it's syntactically valid, but it's
523
+ * something that a CA MUST NOT produce. So we reject it.
524
+ *
525
+ * Note: historically, from XySSL 0.9 to Mbed TLS 3.6.6/4.1.0,
526
+ * `SEQUENCE { INTEGER n }` was interpreted as cA=FALSE if n == 0
527
+ * and cA=TRUE if n != 0. This was dangerous since
528
+ * `SEQUENCE { INTEGER n }` should be parsed as cA=FALSE
529
+ * according to RFC 5280, so we stopped doing it.
530
+ */
531
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
532
+ }
533
+ /* `SEQUENCE { BOOLEAN FALSE }` is not DER since default-value fields
534
+ * must be omitted in DER. But it seems harmless, and Mbed TLS has
535
+ * always accepted it, so we continue to accept it. */
536
+
537
+ if (*p == end) {
538
+ return 0;
539
+ }
540
+
541
+ if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0) {
542
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
543
+ }
544
+
545
+ if (*p != end) {
546
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
547
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
548
+ }
549
+
550
+ /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
551
+ * overflow, which is an undefined behavior. */
552
+ if (*max_pathlen == INT_MAX) {
553
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
554
+ MBEDTLS_ERR_ASN1_INVALID_LENGTH);
555
+ }
556
+
557
+ (*max_pathlen)++;
558
+
559
+ return 0;
560
+ }
561
+
562
+ /*
563
+ * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
564
+ *
565
+ * KeyPurposeId ::= OBJECT IDENTIFIER
566
+ */
567
+ static int x509_get_ext_key_usage(unsigned char **p,
568
+ const unsigned char *end,
569
+ mbedtls_x509_sequence *ext_key_usage)
570
+ {
571
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
572
+
573
+ if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0) {
574
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
575
+ }
576
+
577
+ /* Sequence length must be >= 1 */
578
+ if (ext_key_usage->buf.p == NULL) {
579
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
580
+ MBEDTLS_ERR_ASN1_INVALID_LENGTH);
581
+ }
582
+
583
+ return 0;
584
+ }
585
+
586
+ /*
587
+ * SubjectKeyIdentifier ::= KeyIdentifier
588
+ *
589
+ * KeyIdentifier ::= OCTET STRING
590
+ */
591
+ static int x509_get_subject_key_id(unsigned char **p,
592
+ const unsigned char *end,
593
+ mbedtls_x509_buf *subject_key_id)
594
+ {
595
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
596
+ size_t len = 0u;
597
+
598
+ if ((ret = mbedtls_asn1_get_tag(p, end, &len,
599
+ MBEDTLS_ASN1_OCTET_STRING)) != 0) {
600
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
601
+ }
602
+
603
+ subject_key_id->len = len;
604
+ subject_key_id->tag = MBEDTLS_ASN1_OCTET_STRING;
605
+ subject_key_id->p = *p;
606
+ *p += len;
607
+
608
+ if (*p != end) {
609
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
610
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
611
+ }
612
+
613
+ return 0;
614
+ }
615
+
616
+ /*
617
+ * AuthorityKeyIdentifier ::= SEQUENCE {
618
+ * keyIdentifier [0] KeyIdentifier OPTIONAL,
619
+ * authorityCertIssuer [1] GeneralNames OPTIONAL,
620
+ * authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
621
+ *
622
+ * KeyIdentifier ::= OCTET STRING
623
+ */
624
+ static int x509_get_authority_key_id(unsigned char **p,
625
+ unsigned char *end,
626
+ mbedtls_x509_authority *authority_key_id)
627
+ {
628
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
629
+ size_t len = 0u;
630
+
631
+ if ((ret = mbedtls_asn1_get_tag(p, end, &len,
632
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
633
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
634
+ }
635
+
636
+ if (*p + len != end) {
637
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
638
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
639
+ }
640
+
641
+ ret = mbedtls_asn1_get_tag(p, end, &len,
642
+ MBEDTLS_ASN1_CONTEXT_SPECIFIC);
643
+
644
+ /* KeyIdentifier is an OPTIONAL field */
645
+ if (ret == 0) {
646
+ authority_key_id->keyIdentifier.len = len;
647
+ authority_key_id->keyIdentifier.p = *p;
648
+ /* Setting tag of the keyIdentfier intentionally to 0x04.
649
+ * Although the .keyIdentfier field is CONTEXT_SPECIFIC ([0] OPTIONAL),
650
+ * its tag with the content is the payload of on OCTET STRING primitive */
651
+ authority_key_id->keyIdentifier.tag = MBEDTLS_ASN1_OCTET_STRING;
652
+
653
+ *p += len;
654
+ } else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
655
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
656
+ }
657
+
658
+ if (*p < end) {
659
+ /* Getting authorityCertIssuer using the required specific class tag [1] */
660
+ if ((ret = mbedtls_asn1_get_tag(p, end, &len,
661
+ MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
662
+ 1)) != 0) {
663
+ /* authorityCertIssuer and authorityCertSerialNumber MUST both
664
+ be present or both be absent. At this point we expect to have both. */
665
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
666
+ }
667
+ /* "end" also includes the CertSerialNumber field so "len" shall be used */
668
+ ret = mbedtls_x509_get_subject_alt_name_ext(p,
669
+ (*p+len),
670
+ &authority_key_id->authorityCertIssuer);
671
+ if (ret != 0) {
672
+ return ret;
673
+ }
674
+
675
+ /* Getting authorityCertSerialNumber using the required specific class tag [2] */
676
+ if ((ret = mbedtls_asn1_get_tag(p, end, &len,
677
+ MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2)) != 0) {
678
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
679
+ }
680
+ authority_key_id->authorityCertSerialNumber.len = len;
681
+ authority_key_id->authorityCertSerialNumber.p = *p;
682
+ authority_key_id->authorityCertSerialNumber.tag = MBEDTLS_ASN1_INTEGER;
683
+ *p += len;
684
+ }
685
+
686
+ if (*p != end) {
687
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
688
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
689
+ }
690
+
691
+ return 0;
692
+ }
693
+
694
+ /*
695
+ * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
696
+ *
697
+ * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
698
+ *
699
+ * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
700
+ *
701
+ * PolicyInformation ::= SEQUENCE {
702
+ * policyIdentifier CertPolicyId,
703
+ * policyQualifiers SEQUENCE SIZE (1..MAX) OF
704
+ * PolicyQualifierInfo OPTIONAL }
705
+ *
706
+ * CertPolicyId ::= OBJECT IDENTIFIER
707
+ *
708
+ * PolicyQualifierInfo ::= SEQUENCE {
709
+ * policyQualifierId PolicyQualifierId,
710
+ * qualifier ANY DEFINED BY policyQualifierId }
711
+ *
712
+ * -- policyQualifierIds for Internet policy qualifiers
713
+ *
714
+ * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
715
+ * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
716
+ * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
717
+ *
718
+ * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
719
+ *
720
+ * Qualifier ::= CHOICE {
721
+ * cPSuri CPSuri,
722
+ * userNotice UserNotice }
723
+ *
724
+ * CPSuri ::= IA5String
725
+ *
726
+ * UserNotice ::= SEQUENCE {
727
+ * noticeRef NoticeReference OPTIONAL,
728
+ * explicitText DisplayText OPTIONAL }
729
+ *
730
+ * NoticeReference ::= SEQUENCE {
731
+ * organization DisplayText,
732
+ * noticeNumbers SEQUENCE OF INTEGER }
733
+ *
734
+ * DisplayText ::= CHOICE {
735
+ * ia5String IA5String (SIZE (1..200)),
736
+ * visibleString VisibleString (SIZE (1..200)),
737
+ * bmpString BMPString (SIZE (1..200)),
738
+ * utf8String UTF8String (SIZE (1..200)) }
739
+ *
740
+ * NOTE: we only parse and use anyPolicy without qualifiers at this point
741
+ * as defined in RFC 5280.
742
+ */
743
+ static int x509_get_certificate_policies(unsigned char **p,
744
+ const unsigned char *end,
745
+ mbedtls_x509_sequence *certificate_policies)
746
+ {
747
+ int ret, parse_ret = 0;
748
+ size_t len;
749
+ mbedtls_asn1_buf *buf;
750
+ mbedtls_asn1_sequence *cur = certificate_policies;
751
+
752
+ /* Get main sequence tag */
753
+ ret = mbedtls_asn1_get_tag(p, end, &len,
754
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
755
+ if (ret != 0) {
756
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
757
+ }
758
+
759
+ if (*p + len != end) {
760
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
761
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
762
+ }
763
+
764
+ /*
765
+ * Cannot be an empty sequence.
766
+ */
767
+ if (len == 0) {
768
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
769
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
770
+ }
771
+
772
+ while (*p < end) {
773
+ mbedtls_x509_buf policy_oid;
774
+ const unsigned char *policy_end;
775
+
776
+ /*
777
+ * Get the policy sequence
778
+ */
779
+ if ((ret = mbedtls_asn1_get_tag(p, end, &len,
780
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
781
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
782
+ }
783
+
784
+ policy_end = *p + len;
785
+
786
+ if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
787
+ MBEDTLS_ASN1_OID)) != 0) {
788
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
789
+ }
790
+
791
+ policy_oid.tag = MBEDTLS_ASN1_OID;
792
+ policy_oid.len = len;
793
+ policy_oid.p = *p;
794
+
795
+ /*
796
+ * Only AnyPolicy is currently supported when enforcing policy.
797
+ */
798
+ if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
799
+ /*
800
+ * Set the parsing return code but continue parsing, in case this
801
+ * extension is critical.
802
+ */
803
+ parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
804
+ }
805
+
806
+ /* Allocate and assign next pointer */
807
+ if (cur->buf.p != NULL) {
808
+ if (cur->next != NULL) {
809
+ return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
810
+ }
811
+
812
+ cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
813
+
814
+ if (cur->next == NULL) {
815
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
816
+ MBEDTLS_ERR_ASN1_ALLOC_FAILED);
817
+ }
818
+
819
+ cur = cur->next;
820
+ }
821
+
822
+ buf = &(cur->buf);
823
+ buf->tag = policy_oid.tag;
824
+ buf->p = policy_oid.p;
825
+ buf->len = policy_oid.len;
826
+
827
+ *p += len;
828
+
829
+ /*
830
+ * If there is an optional qualifier, then *p < policy_end
831
+ * Check the Qualifier len to verify it doesn't exceed policy_end.
832
+ */
833
+ if (*p < policy_end) {
834
+ if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
835
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
836
+ 0) {
837
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
838
+ }
839
+ /*
840
+ * Skip the optional policy qualifiers.
841
+ */
842
+ *p += len;
843
+ }
844
+
845
+ if (*p != policy_end) {
846
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
847
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
848
+ }
849
+ }
850
+
851
+ /* Set final sequence entry's next pointer to NULL */
852
+ cur->next = NULL;
853
+
854
+ if (*p != end) {
855
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
856
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
857
+ }
858
+
859
+ return parse_ret;
860
+ }
861
+
862
+ /*
863
+ * X.509 v3 extensions
864
+ *
865
+ */
866
+ static int x509_get_crt_ext(unsigned char **p,
867
+ const unsigned char *end,
868
+ mbedtls_x509_crt *crt,
869
+ mbedtls_x509_crt_ext_cb_t cb,
870
+ void *p_ctx)
871
+ {
872
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
873
+ size_t len;
874
+ unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
875
+
876
+ if (*p == end) {
877
+ return 0;
878
+ }
879
+
880
+ if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0) {
881
+ return ret;
882
+ }
883
+
884
+ end = crt->v3_ext.p + crt->v3_ext.len;
885
+ while (*p < end) {
886
+ /*
887
+ * Extension ::= SEQUENCE {
888
+ * extnID OBJECT IDENTIFIER,
889
+ * critical BOOLEAN DEFAULT FALSE,
890
+ * extnValue OCTET STRING }
891
+ */
892
+ mbedtls_x509_buf extn_oid = { 0, 0, NULL };
893
+ int is_critical = 0; /* DEFAULT FALSE */
894
+ int ext_type = 0;
895
+
896
+ if ((ret = mbedtls_asn1_get_tag(p, end, &len,
897
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
898
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
899
+ }
900
+
901
+ end_ext_data = *p + len;
902
+
903
+ /* Get extension ID */
904
+ if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
905
+ MBEDTLS_ASN1_OID)) != 0) {
906
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
907
+ }
908
+
909
+ extn_oid.tag = MBEDTLS_ASN1_OID;
910
+ extn_oid.p = *p;
911
+ *p += extn_oid.len;
912
+
913
+ /* Get optional critical */
914
+ if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
915
+ (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
916
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
917
+ }
918
+
919
+ /* Data should be octet string type */
920
+ if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
921
+ MBEDTLS_ASN1_OCTET_STRING)) != 0) {
922
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
923
+ }
924
+
925
+ start_ext_octet = *p;
926
+ end_ext_octet = *p + len;
927
+
928
+ if (end_ext_octet != end_ext_data) {
929
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
930
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
931
+ }
932
+
933
+ /*
934
+ * Detect supported extensions
935
+ */
936
+ ret = mbedtls_x509_oid_get_x509_ext_type(&extn_oid, &ext_type);
937
+
938
+ if (ret != 0) {
939
+ /* Give the callback (if any) a chance to handle the extension */
940
+ if (cb != NULL) {
941
+ ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
942
+ if (ret != 0 && is_critical) {
943
+ return ret;
944
+ }
945
+ *p = end_ext_octet;
946
+ continue;
947
+ }
948
+
949
+ /* No parser found, skip extension */
950
+ *p = end_ext_octet;
951
+
952
+ if (is_critical) {
953
+ /* Data is marked as critical: fail */
954
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
955
+ MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
956
+ }
957
+ continue;
958
+ }
959
+
960
+ /* Forbid repeated extensions */
961
+ if ((crt->ext_types & ext_type) != 0) {
962
+ return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
963
+ }
964
+
965
+ crt->ext_types |= ext_type;
966
+
967
+ switch (ext_type) {
968
+ case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
969
+ /* Parse basic constraints */
970
+ if ((ret = x509_get_basic_constraints(p, end_ext_octet,
971
+ &crt->ca_istrue, &crt->max_pathlen)) != 0) {
972
+ return ret;
973
+ }
974
+ break;
975
+
976
+ case MBEDTLS_X509_EXT_KEY_USAGE:
977
+ /* Parse key usage */
978
+ if ((ret = mbedtls_x509_get_key_usage(p, end_ext_octet,
979
+ &crt->key_usage)) != 0) {
980
+ return ret;
981
+ }
982
+ break;
983
+
984
+ case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
985
+ /* Parse extended key usage */
986
+ if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
987
+ &crt->ext_key_usage)) != 0) {
988
+ return ret;
989
+ }
990
+ break;
991
+
992
+ case MBEDTLS_X509_EXT_SUBJECT_KEY_IDENTIFIER:
993
+ /* Parse subject key identifier */
994
+ if ((ret = x509_get_subject_key_id(p, end_ext_data,
995
+ &crt->subject_key_id)) != 0) {
996
+ return ret;
997
+ }
998
+ break;
999
+
1000
+ case MBEDTLS_X509_EXT_AUTHORITY_KEY_IDENTIFIER:
1001
+ /* Parse authority key identifier */
1002
+ if ((ret = x509_get_authority_key_id(p, end_ext_octet,
1003
+ &crt->authority_key_id)) != 0) {
1004
+ return ret;
1005
+ }
1006
+ break;
1007
+ case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
1008
+ /* Parse subject alt name
1009
+ * SubjectAltName ::= GeneralNames
1010
+ */
1011
+ if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_octet,
1012
+ &crt->subject_alt_names)) != 0) {
1013
+ return ret;
1014
+ }
1015
+ break;
1016
+
1017
+ case MBEDTLS_X509_EXT_NS_CERT_TYPE:
1018
+ /* Parse netscape certificate type */
1019
+ if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_octet,
1020
+ &crt->ns_cert_type)) != 0) {
1021
+ return ret;
1022
+ }
1023
+ break;
1024
+
1025
+ case MBEDTLS_X509_EXT_CERTIFICATE_POLICIES:
1026
+ /* Parse certificate policies type */
1027
+ if ((ret = x509_get_certificate_policies(p, end_ext_octet,
1028
+ &crt->certificate_policies)) != 0) {
1029
+ /* Give the callback (if any) a chance to handle the extension
1030
+ * if it contains unsupported policies */
1031
+ if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
1032
+ cb(p_ctx, crt, &extn_oid, is_critical,
1033
+ start_ext_octet, end_ext_octet) == 0) {
1034
+ break;
1035
+ }
1036
+
1037
+ if (is_critical) {
1038
+ return ret;
1039
+ } else
1040
+ /*
1041
+ * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
1042
+ * cannot interpret or enforce the policy. However, it is up to
1043
+ * the user to choose how to enforce the policies,
1044
+ * unless the extension is critical.
1045
+ */
1046
+ if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1047
+ return ret;
1048
+ }
1049
+ }
1050
+ break;
1051
+
1052
+ default:
1053
+ /*
1054
+ * If this is a non-critical extension, which the oid layer
1055
+ * supports, but there isn't an x509 parser for it,
1056
+ * skip the extension.
1057
+ */
1058
+ if (is_critical) {
1059
+ return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
1060
+ } else {
1061
+ *p = end_ext_octet;
1062
+ }
1063
+ }
1064
+ }
1065
+
1066
+ if (*p != end) {
1067
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
1068
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1069
+ }
1070
+
1071
+ return 0;
1072
+ }
1073
+
1074
+ /*
1075
+ * Parse and fill a single X.509 certificate in DER format
1076
+ */
1077
+ static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1078
+ const unsigned char *buf,
1079
+ size_t buflen,
1080
+ int make_copy,
1081
+ mbedtls_x509_crt_ext_cb_t cb,
1082
+ void *p_ctx)
1083
+ {
1084
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1085
+ size_t len;
1086
+ unsigned char *p, *end, *crt_end;
1087
+ mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
1088
+
1089
+ memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1090
+ memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1091
+ memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
1092
+
1093
+ /*
1094
+ * Check for valid input
1095
+ */
1096
+ if (crt == NULL || buf == NULL) {
1097
+ return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1098
+ }
1099
+
1100
+ /* Use the original buffer until we figure out actual length. */
1101
+ p = (unsigned char *) buf;
1102
+ len = buflen;
1103
+ end = p + len;
1104
+
1105
+ /*
1106
+ * Certificate ::= SEQUENCE {
1107
+ * tbsCertificate TBSCertificate,
1108
+ * signatureAlgorithm AlgorithmIdentifier,
1109
+ * signatureValue BIT STRING }
1110
+ */
1111
+ if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1112
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1113
+ mbedtls_x509_crt_free(crt);
1114
+ return MBEDTLS_ERR_X509_INVALID_FORMAT;
1115
+ }
1116
+
1117
+ end = crt_end = p + len;
1118
+ crt->raw.len = (size_t) (crt_end - buf);
1119
+ if (make_copy != 0) {
1120
+ /* Create and populate a new buffer for the raw field. */
1121
+ crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1122
+ if (crt->raw.p == NULL) {
1123
+ return MBEDTLS_ERR_X509_ALLOC_FAILED;
1124
+ }
1125
+
1126
+ memcpy(crt->raw.p, buf, crt->raw.len);
1127
+ crt->own_buffer = 1;
1128
+
1129
+ p += crt->raw.len - len;
1130
+ end = crt_end = p + len;
1131
+ } else {
1132
+ crt->raw.p = (unsigned char *) buf;
1133
+ crt->own_buffer = 0;
1134
+ }
1135
+
1136
+ /*
1137
+ * TBSCertificate ::= SEQUENCE {
1138
+ */
1139
+ crt->tbs.p = p;
1140
+
1141
+ if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1142
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1143
+ mbedtls_x509_crt_free(crt);
1144
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
1145
+ }
1146
+
1147
+ end = p + len;
1148
+ crt->tbs.len = (size_t) (end - crt->tbs.p);
1149
+
1150
+ /*
1151
+ * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1152
+ *
1153
+ * CertificateSerialNumber ::= INTEGER
1154
+ *
1155
+ * signature AlgorithmIdentifier
1156
+ */
1157
+ if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1158
+ (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1159
+ (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1160
+ &sig_params1)) != 0) {
1161
+ mbedtls_x509_crt_free(crt);
1162
+ return ret;
1163
+ }
1164
+
1165
+ if (crt->version < 0 || crt->version > 2) {
1166
+ mbedtls_x509_crt_free(crt);
1167
+ return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
1168
+ }
1169
+
1170
+ crt->version++;
1171
+
1172
+ if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1173
+ &crt->sig_md, &crt->sig_pk)) != 0) {
1174
+ mbedtls_x509_crt_free(crt);
1175
+ return ret;
1176
+ }
1177
+
1178
+ /*
1179
+ * issuer Name
1180
+ */
1181
+ crt->issuer_raw.p = p;
1182
+
1183
+ if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1184
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1185
+ mbedtls_x509_crt_free(crt);
1186
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
1187
+ }
1188
+
1189
+ if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1190
+ mbedtls_x509_crt_free(crt);
1191
+ return ret;
1192
+ }
1193
+
1194
+ crt->issuer_raw.len = (size_t) (p - crt->issuer_raw.p);
1195
+
1196
+ /*
1197
+ * Validity ::= SEQUENCE {
1198
+ * notBefore Time,
1199
+ * notAfter Time }
1200
+ *
1201
+ */
1202
+ if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1203
+ &crt->valid_to)) != 0) {
1204
+ mbedtls_x509_crt_free(crt);
1205
+ return ret;
1206
+ }
1207
+
1208
+ /*
1209
+ * subject Name
1210
+ */
1211
+ crt->subject_raw.p = p;
1212
+
1213
+ if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1214
+ MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1215
+ mbedtls_x509_crt_free(crt);
1216
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
1217
+ }
1218
+
1219
+ if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1220
+ mbedtls_x509_crt_free(crt);
1221
+ return ret;
1222
+ }
1223
+
1224
+ crt->subject_raw.len = (size_t) (p - crt->subject_raw.p);
1225
+
1226
+ /*
1227
+ * SubjectPublicKeyInfo
1228
+ */
1229
+ crt->pk_raw.p = p;
1230
+ if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1231
+ mbedtls_x509_crt_free(crt);
1232
+ return ret;
1233
+ }
1234
+ crt->pk_raw.len = (size_t) (p - crt->pk_raw.p);
1235
+
1236
+ /*
1237
+ * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1238
+ * -- If present, version shall be v2 or v3
1239
+ * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1240
+ * -- If present, version shall be v2 or v3
1241
+ * extensions [3] EXPLICIT Extensions OPTIONAL
1242
+ * -- If present, version shall be v3
1243
+ */
1244
+ if (crt->version == 2 || crt->version == 3) {
1245
+ ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1246
+ if (ret != 0) {
1247
+ mbedtls_x509_crt_free(crt);
1248
+ return ret;
1249
+ }
1250
+ }
1251
+
1252
+ if (crt->version == 2 || crt->version == 3) {
1253
+ ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1254
+ if (ret != 0) {
1255
+ mbedtls_x509_crt_free(crt);
1256
+ return ret;
1257
+ }
1258
+ }
1259
+
1260
+ if (crt->version == 3) {
1261
+ ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1262
+ if (ret != 0) {
1263
+ mbedtls_x509_crt_free(crt);
1264
+ return ret;
1265
+ }
1266
+ }
1267
+
1268
+ if (p != end) {
1269
+ mbedtls_x509_crt_free(crt);
1270
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1271
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1272
+ }
1273
+
1274
+ end = crt_end;
1275
+
1276
+ /*
1277
+ * }
1278
+ * -- end of TBSCertificate
1279
+ *
1280
+ * signatureAlgorithm AlgorithmIdentifier,
1281
+ * signatureValue BIT STRING
1282
+ */
1283
+ if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1284
+ mbedtls_x509_crt_free(crt);
1285
+ return ret;
1286
+ }
1287
+
1288
+ if (crt->sig_oid.len != sig_oid2.len ||
1289
+ memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
1290
+ sig_params1.tag != sig_params2.tag ||
1291
+ sig_params1.len != sig_params2.len ||
1292
+ (sig_params1.len != 0 &&
1293
+ memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1294
+ mbedtls_x509_crt_free(crt);
1295
+ return MBEDTLS_ERR_X509_SIG_MISMATCH;
1296
+ }
1297
+
1298
+ if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1299
+ mbedtls_x509_crt_free(crt);
1300
+ return ret;
1301
+ }
1302
+
1303
+ if (p != end) {
1304
+ mbedtls_x509_crt_free(crt);
1305
+ return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
1306
+ MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1307
+ }
1308
+
1309
+ return 0;
1310
+ }
1311
+
1312
+ /*
1313
+ * Parse one X.509 certificate in DER format from a buffer and add them to a
1314
+ * chained list
1315
+ */
1316
+ static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1317
+ const unsigned char *buf,
1318
+ size_t buflen,
1319
+ int make_copy,
1320
+ mbedtls_x509_crt_ext_cb_t cb,
1321
+ void *p_ctx)
1322
+ {
1323
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1324
+ mbedtls_x509_crt *crt = chain, *prev = NULL;
1325
+
1326
+ /*
1327
+ * Check for valid input
1328
+ */
1329
+ if (crt == NULL || buf == NULL) {
1330
+ return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1331
+ }
1332
+
1333
+ while (crt->version != 0 && crt->next != NULL) {
1334
+ prev = crt;
1335
+ crt = crt->next;
1336
+ }
1337
+
1338
+ /*
1339
+ * Add new certificate on the end of the chain if needed.
1340
+ */
1341
+ if (crt->version != 0 && crt->next == NULL) {
1342
+ crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
1343
+
1344
+ if (crt->next == NULL) {
1345
+ return MBEDTLS_ERR_X509_ALLOC_FAILED;
1346
+ }
1347
+
1348
+ prev = crt;
1349
+ mbedtls_x509_crt_init(crt->next);
1350
+ crt = crt->next;
1351
+ }
1352
+
1353
+ ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1354
+ if (ret != 0) {
1355
+ if (prev) {
1356
+ prev->next = NULL;
1357
+ }
1358
+
1359
+ if (crt != chain) {
1360
+ mbedtls_free(crt);
1361
+ }
1362
+
1363
+ return ret;
1364
+ }
1365
+
1366
+ return 0;
1367
+ }
1368
+
1369
+ int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1370
+ const unsigned char *buf,
1371
+ size_t buflen)
1372
+ {
1373
+ return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL);
1374
+ }
1375
+
1376
+ int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1377
+ const unsigned char *buf,
1378
+ size_t buflen,
1379
+ int make_copy,
1380
+ mbedtls_x509_crt_ext_cb_t cb,
1381
+ void *p_ctx)
1382
+ {
1383
+ return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx);
1384
+ }
1385
+
1386
+ int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1387
+ const unsigned char *buf,
1388
+ size_t buflen)
1389
+ {
1390
+ return mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL);
1391
+ }
1392
+
1393
+ /*
1394
+ * Parse one or more PEM certificates from a buffer and add them to the chained
1395
+ * list
1396
+ */
1397
+ int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1398
+ const unsigned char *buf,
1399
+ size_t buflen)
1400
+ {
1401
+ #if defined(MBEDTLS_PEM_PARSE_C)
1402
+ int success = 0, first_error = 0, total_failed = 0;
1403
+ int buf_format = MBEDTLS_X509_FORMAT_DER;
1404
+ #endif
1405
+
1406
+ /*
1407
+ * Check for valid input
1408
+ */
1409
+ if (chain == NULL || buf == NULL) {
1410
+ return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1411
+ }
1412
+
1413
+ /*
1414
+ * Determine buffer content. Buffer contains either one DER certificate or
1415
+ * one or more PEM certificates.
1416
+ */
1417
+ #if defined(MBEDTLS_PEM_PARSE_C)
1418
+ if (buflen != 0 && buf[buflen - 1] == '\0' &&
1419
+ strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
1420
+ buf_format = MBEDTLS_X509_FORMAT_PEM;
1421
+ }
1422
+
1423
+ if (buf_format == MBEDTLS_X509_FORMAT_DER) {
1424
+ return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1425
+ }
1426
+ #else
1427
+ return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1428
+ #endif
1429
+
1430
+ #if defined(MBEDTLS_PEM_PARSE_C)
1431
+ if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
1432
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1433
+ mbedtls_pem_context pem;
1434
+
1435
+ /* 1 rather than 0 since the terminating NULL byte is counted in */
1436
+ while (buflen > 1) {
1437
+ size_t use_len;
1438
+ mbedtls_pem_init(&pem);
1439
+
1440
+ /* If we get there, we know the string is null-terminated */
1441
+ ret = mbedtls_pem_read_buffer(&pem,
1442
+ "-----BEGIN CERTIFICATE-----",
1443
+ "-----END CERTIFICATE-----",
1444
+ buf, NULL, 0, &use_len);
1445
+
1446
+ if (ret == 0) {
1447
+ /*
1448
+ * Was PEM encoded
1449
+ */
1450
+ buflen -= use_len;
1451
+ buf += use_len;
1452
+ } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1453
+ return ret;
1454
+ } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1455
+ mbedtls_pem_free(&pem);
1456
+
1457
+ /*
1458
+ * PEM header and footer were found
1459
+ */
1460
+ buflen -= use_len;
1461
+ buf += use_len;
1462
+
1463
+ if (first_error == 0) {
1464
+ first_error = ret;
1465
+ }
1466
+
1467
+ total_failed++;
1468
+ continue;
1469
+ } else {
1470
+ break;
1471
+ }
1472
+
1473
+ ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
1474
+
1475
+ mbedtls_pem_free(&pem);
1476
+
1477
+ if (ret != 0) {
1478
+ /*
1479
+ * Quit parsing on a memory error
1480
+ */
1481
+ if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED) {
1482
+ return ret;
1483
+ }
1484
+
1485
+ if (first_error == 0) {
1486
+ first_error = ret;
1487
+ }
1488
+
1489
+ total_failed++;
1490
+ continue;
1491
+ }
1492
+
1493
+ success = 1;
1494
+ }
1495
+ }
1496
+
1497
+ if (success) {
1498
+ return total_failed;
1499
+ } else if (first_error) {
1500
+ return first_error;
1501
+ } else {
1502
+ return MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT;
1503
+ }
1504
+ #endif /* MBEDTLS_PEM_PARSE_C */
1505
+ }
1506
+
1507
+ #if defined(MBEDTLS_FS_IO)
1508
+ /*
1509
+ * Load one or more certificates and add them to the chained list
1510
+ */
1511
+ int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path)
1512
+ {
1513
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1514
+ size_t n;
1515
+ unsigned char *buf;
1516
+
1517
+ if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
1518
+ return ret;
1519
+ }
1520
+
1521
+ ret = mbedtls_x509_crt_parse(chain, buf, n);
1522
+
1523
+ mbedtls_zeroize_and_free(buf, n);
1524
+
1525
+ return ret;
1526
+ }
1527
+
1528
+ int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path)
1529
+ {
1530
+ int ret = 0;
1531
+ #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
1532
+ int w_ret;
1533
+ WCHAR szDir[MAX_PATH];
1534
+ char filename[MAX_PATH];
1535
+ char *p;
1536
+ size_t len = strlen(path);
1537
+
1538
+ WIN32_FIND_DATAW file_data;
1539
+ HANDLE hFind;
1540
+
1541
+ if (len > MAX_PATH - 3) {
1542
+ return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1543
+ }
1544
+
1545
+ memset(szDir, 0, sizeof(szDir));
1546
+ memset(filename, 0, MAX_PATH);
1547
+ memcpy(filename, path, len);
1548
+ filename[len++] = '\\';
1549
+ p = filename + len;
1550
+ filename[len++] = '*';
1551
+
1552
+ /*
1553
+ * Note this function uses the code page CP_ACP which is the system default
1554
+ * ANSI codepage. The input string is always described in BYTES and the
1555
+ * output length is described in WCHARs.
1556
+ */
1557
+ w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int) len, szDir,
1558
+ MAX_PATH - 3);
1559
+ if (w_ret == 0) {
1560
+ return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1561
+ }
1562
+
1563
+ hFind = FindFirstFileW(szDir, &file_data);
1564
+ if (hFind == INVALID_HANDLE_VALUE) {
1565
+ return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1566
+ }
1567
+
1568
+ len = MAX_PATH - len;
1569
+ do {
1570
+ memset(p, 0, len);
1571
+
1572
+ if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1573
+ continue;
1574
+ }
1575
+ w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1576
+ -1, p, (int) len, NULL, NULL);
1577
+ if (w_ret == 0) {
1578
+ ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1579
+ goto cleanup;
1580
+ }
1581
+
1582
+ w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1583
+ if (w_ret < 0) {
1584
+ ret++;
1585
+ } else {
1586
+ ret += w_ret;
1587
+ }
1588
+ } while (FindNextFileW(hFind, &file_data) != 0);
1589
+
1590
+ if (GetLastError() != ERROR_NO_MORE_FILES) {
1591
+ ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1592
+ }
1593
+
1594
+ cleanup:
1595
+ FindClose(hFind);
1596
+ #else /* _WIN32 */
1597
+ int t_ret;
1598
+ int snp_ret;
1599
+ struct stat sb;
1600
+ struct dirent *entry;
1601
+ char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
1602
+ DIR *dir = opendir(path);
1603
+
1604
+ if (dir == NULL) {
1605
+ return MBEDTLS_ERR_X509_FILE_IO_ERROR;
1606
+ }
1607
+
1608
+ #if defined(MBEDTLS_THREADING_C)
1609
+ if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1610
+ closedir(dir);
1611
+ return ret;
1612
+ }
1613
+ #endif /* MBEDTLS_THREADING_C */
1614
+
1615
+ memset(&sb, 0, sizeof(sb));
1616
+
1617
+ while ((entry = readdir(dir)) != NULL) {
1618
+ snp_ret = mbedtls_snprintf(entry_name, sizeof(entry_name),
1619
+ "%s/%s", path, entry->d_name);
1620
+
1621
+ if (snp_ret < 0 || (size_t) snp_ret >= sizeof(entry_name)) {
1622
+ ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1623
+ goto cleanup;
1624
+ } else if (stat(entry_name, &sb) == -1) {
1625
+ if (errno == ENOENT) {
1626
+ /* Broken symbolic link - ignore this entry.
1627
+ stat(2) will return this error for either (a) a dangling
1628
+ symlink or (b) a missing file.
1629
+ Given that we have just obtained the filename from readdir,
1630
+ assume that it does exist and therefore treat this as a
1631
+ dangling symlink. */
1632
+ continue;
1633
+ } else {
1634
+ /* Some other file error; report the error. */
1635
+ ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1636
+ goto cleanup;
1637
+ }
1638
+ }
1639
+
1640
+ if (!S_ISREG(sb.st_mode)) {
1641
+ continue;
1642
+ }
1643
+
1644
+ // Ignore parse errors
1645
+ //
1646
+ t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1647
+ if (t_ret < 0) {
1648
+ ret++;
1649
+ } else {
1650
+ ret += t_ret;
1651
+ }
1652
+ }
1653
+
1654
+ cleanup:
1655
+ closedir(dir);
1656
+
1657
+ #if defined(MBEDTLS_THREADING_C)
1658
+ if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0) {
1659
+ ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1660
+ }
1661
+ #endif /* MBEDTLS_THREADING_C */
1662
+
1663
+ #endif /* _WIN32 */
1664
+
1665
+ return ret;
1666
+ }
1667
+ #endif /* MBEDTLS_FS_IO */
1668
+
1669
+ #if !defined(MBEDTLS_X509_REMOVE_INFO)
1670
+ #define PRINT_ITEM(i) \
1671
+ do { \
1672
+ ret = mbedtls_snprintf(p, n, "%s" i, sep); \
1673
+ MBEDTLS_X509_SAFE_SNPRINTF; \
1674
+ sep = ", "; \
1675
+ } while (0)
1676
+
1677
+ #define CERT_TYPE(type, name) \
1678
+ do { \
1679
+ if (ns_cert_type & (type)) { \
1680
+ PRINT_ITEM(name); \
1681
+ } \
1682
+ } while (0)
1683
+
1684
+ #define KEY_USAGE(code, name) \
1685
+ do { \
1686
+ if (key_usage & (code)) { \
1687
+ PRINT_ITEM(name); \
1688
+ } \
1689
+ } while (0)
1690
+
1691
+ static int x509_info_ext_key_usage(char **buf, size_t *size,
1692
+ const mbedtls_x509_sequence *extended_key_usage)
1693
+ {
1694
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1695
+ const char *desc;
1696
+ size_t n = *size;
1697
+ char *p = *buf;
1698
+ const mbedtls_x509_sequence *cur = extended_key_usage;
1699
+ const char *sep = "";
1700
+
1701
+ while (cur != NULL) {
1702
+ if (mbedtls_x509_oid_get_extended_key_usage(&cur->buf, &desc) != 0) {
1703
+ desc = "???";
1704
+ }
1705
+
1706
+ ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
1707
+ MBEDTLS_X509_SAFE_SNPRINTF;
1708
+
1709
+ sep = ", ";
1710
+
1711
+ cur = cur->next;
1712
+ }
1713
+
1714
+ *size = n;
1715
+ *buf = p;
1716
+
1717
+ return 0;
1718
+ }
1719
+
1720
+ static int x509_info_cert_policies(char **buf, size_t *size,
1721
+ const mbedtls_x509_sequence *certificate_policies)
1722
+ {
1723
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1724
+ const char *desc;
1725
+ size_t n = *size;
1726
+ char *p = *buf;
1727
+ const mbedtls_x509_sequence *cur = certificate_policies;
1728
+ const char *sep = "";
1729
+
1730
+ while (cur != NULL) {
1731
+ if (mbedtls_x509_oid_get_certificate_policies(&cur->buf, &desc) != 0) {
1732
+ desc = "???";
1733
+ }
1734
+
1735
+ ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
1736
+ MBEDTLS_X509_SAFE_SNPRINTF;
1737
+
1738
+ sep = ", ";
1739
+
1740
+ cur = cur->next;
1741
+ }
1742
+
1743
+ *size = n;
1744
+ *buf = p;
1745
+
1746
+ return 0;
1747
+ }
1748
+
1749
+ /*
1750
+ * Return an informational string about the certificate.
1751
+ */
1752
+ #define MBEDTLS_BEFORE_COLON 18
1753
+ #define MBEDTLS_BEFORE_COLON_STR "18"
1754
+ int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1755
+ const mbedtls_x509_crt *crt)
1756
+ {
1757
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1758
+ size_t n;
1759
+ char *p;
1760
+ char key_size_str[MBEDTLS_BEFORE_COLON];
1761
+
1762
+ p = buf;
1763
+ n = size;
1764
+
1765
+ if (NULL == crt) {
1766
+ ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
1767
+ MBEDTLS_X509_SAFE_SNPRINTF;
1768
+
1769
+ return (int) (size - n);
1770
+ }
1771
+
1772
+ ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1773
+ prefix, crt->version);
1774
+ MBEDTLS_X509_SAFE_SNPRINTF;
1775
+ ret = mbedtls_snprintf(p, n, "%sserial number : ",
1776
+ prefix);
1777
+ MBEDTLS_X509_SAFE_SNPRINTF;
1778
+
1779
+ ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
1780
+ MBEDTLS_X509_SAFE_SNPRINTF;
1781
+
1782
+ ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
1783
+ MBEDTLS_X509_SAFE_SNPRINTF;
1784
+ ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
1785
+ MBEDTLS_X509_SAFE_SNPRINTF;
1786
+
1787
+ ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
1788
+ MBEDTLS_X509_SAFE_SNPRINTF;
1789
+ ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
1790
+ MBEDTLS_X509_SAFE_SNPRINTF;
1791
+
1792
+ ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1793
+ "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1794
+ crt->valid_from.year, crt->valid_from.mon,
1795
+ crt->valid_from.day, crt->valid_from.hour,
1796
+ crt->valid_from.min, crt->valid_from.sec);
1797
+ MBEDTLS_X509_SAFE_SNPRINTF;
1798
+
1799
+ ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1800
+ "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1801
+ crt->valid_to.year, crt->valid_to.mon,
1802
+ crt->valid_to.day, crt->valid_to.hour,
1803
+ crt->valid_to.min, crt->valid_to.sec);
1804
+ MBEDTLS_X509_SAFE_SNPRINTF;
1805
+
1806
+ ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
1807
+ MBEDTLS_X509_SAFE_SNPRINTF;
1808
+
1809
+ ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk, crt->sig_md);
1810
+ MBEDTLS_X509_SAFE_SNPRINTF;
1811
+
1812
+ /* Key size */
1813
+ if ((ret = mbedtls_x509_key_size_helper(key_size_str, MBEDTLS_BEFORE_COLON,
1814
+ mbedtls_x509_pk_type_as_string(&crt->pk))) != 0) {
1815
+ return ret;
1816
+ }
1817
+
1818
+ ret = mbedtls_snprintf(p, n, "\n%s%-" MBEDTLS_BEFORE_COLON_STR "s: %d bits",
1819
+ prefix, key_size_str, (int) mbedtls_pk_get_bitlen(&crt->pk));
1820
+ MBEDTLS_X509_SAFE_SNPRINTF;
1821
+
1822
+ /*
1823
+ * Optional extensions
1824
+ */
1825
+
1826
+ if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1827
+ ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1828
+ crt->ca_istrue ? "true" : "false");
1829
+ MBEDTLS_X509_SAFE_SNPRINTF;
1830
+
1831
+ if (crt->max_pathlen > 0) {
1832
+ ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
1833
+ MBEDTLS_X509_SAFE_SNPRINTF;
1834
+ }
1835
+ }
1836
+
1837
+ if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
1838
+ ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
1839
+ MBEDTLS_X509_SAFE_SNPRINTF;
1840
+
1841
+ if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
1842
+ &crt->subject_alt_names,
1843
+ prefix)) != 0) {
1844
+ return ret;
1845
+ }
1846
+ }
1847
+
1848
+ if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
1849
+ ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
1850
+ MBEDTLS_X509_SAFE_SNPRINTF;
1851
+
1852
+ if ((ret = mbedtls_x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0) {
1853
+ return ret;
1854
+ }
1855
+ }
1856
+
1857
+ if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
1858
+ ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
1859
+ MBEDTLS_X509_SAFE_SNPRINTF;
1860
+
1861
+ if ((ret = mbedtls_x509_info_key_usage(&p, &n, crt->key_usage)) != 0) {
1862
+ return ret;
1863
+ }
1864
+ }
1865
+
1866
+ if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
1867
+ ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
1868
+ MBEDTLS_X509_SAFE_SNPRINTF;
1869
+
1870
+ if ((ret = x509_info_ext_key_usage(&p, &n,
1871
+ &crt->ext_key_usage)) != 0) {
1872
+ return ret;
1873
+ }
1874
+ }
1875
+
1876
+ if (crt->ext_types & MBEDTLS_X509_EXT_CERTIFICATE_POLICIES) {
1877
+ ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
1878
+ MBEDTLS_X509_SAFE_SNPRINTF;
1879
+
1880
+ if ((ret = x509_info_cert_policies(&p, &n,
1881
+ &crt->certificate_policies)) != 0) {
1882
+ return ret;
1883
+ }
1884
+ }
1885
+
1886
+ ret = mbedtls_snprintf(p, n, "\n");
1887
+ MBEDTLS_X509_SAFE_SNPRINTF;
1888
+
1889
+ return (int) (size - n);
1890
+ }
1891
+
1892
+ struct x509_crt_verify_string {
1893
+ int code;
1894
+ const char *string;
1895
+ };
1896
+
1897
+ #define X509_CRT_ERROR_INFO(err, err_str, info) { err, info },
1898
+ static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
1899
+ MBEDTLS_X509_CRT_ERROR_INFO_LIST
1900
+ { 0, NULL }
1901
+ };
1902
+ #undef X509_CRT_ERROR_INFO
1903
+
1904
+ int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
1905
+ uint32_t flags)
1906
+ {
1907
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1908
+ const struct x509_crt_verify_string *cur;
1909
+ char *p = buf;
1910
+ size_t n = size;
1911
+
1912
+ for (cur = x509_crt_verify_strings; cur->string != NULL; cur++) {
1913
+ if ((flags & cur->code) == 0) {
1914
+ continue;
1915
+ }
1916
+
1917
+ ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
1918
+ MBEDTLS_X509_SAFE_SNPRINTF;
1919
+ flags ^= cur->code;
1920
+ }
1921
+
1922
+ if (flags != 0) {
1923
+ ret = mbedtls_snprintf(p, n, "%sUnknown reason "
1924
+ "(this should not happen)\n", prefix);
1925
+ MBEDTLS_X509_SAFE_SNPRINTF;
1926
+ }
1927
+
1928
+ return (int) (size - n);
1929
+ }
1930
+ #endif /* MBEDTLS_X509_REMOVE_INFO */
1931
+
1932
+ int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
1933
+ unsigned int usage)
1934
+ {
1935
+ unsigned int usage_must, usage_may;
1936
+ unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1937
+ | MBEDTLS_X509_KU_DECIPHER_ONLY;
1938
+
1939
+ if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0) {
1940
+ return 0;
1941
+ }
1942
+
1943
+ usage_must = usage & ~may_mask;
1944
+
1945
+ if (((crt->key_usage & ~may_mask) & usage_must) != usage_must) {
1946
+ return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1947
+ }
1948
+
1949
+ usage_may = usage & may_mask;
1950
+
1951
+ if (((crt->key_usage & may_mask) | usage_may) != usage_may) {
1952
+ return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1953
+ }
1954
+
1955
+ return 0;
1956
+ }
1957
+
1958
+ int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
1959
+ const char *usage_oid,
1960
+ size_t usage_len)
1961
+ {
1962
+ const mbedtls_x509_sequence *cur;
1963
+
1964
+ /* Extension is not mandatory, absent means no restriction */
1965
+ if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0) {
1966
+ return 0;
1967
+ }
1968
+
1969
+ /*
1970
+ * Look for the requested usage (or wildcard ANY) in our list
1971
+ */
1972
+ for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
1973
+ const mbedtls_x509_buf *cur_oid = &cur->buf;
1974
+
1975
+ if (cur_oid->len == usage_len &&
1976
+ memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
1977
+ return 0;
1978
+ }
1979
+
1980
+ if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0) {
1981
+ return 0;
1982
+ }
1983
+ }
1984
+
1985
+ return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
1986
+ }
1987
+
1988
+ #if defined(MBEDTLS_X509_CRL_PARSE_C)
1989
+ /*
1990
+ * Return 1 if the certificate is revoked, or 0 otherwise.
1991
+ */
1992
+ int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl)
1993
+ {
1994
+ const mbedtls_x509_crl_entry *cur = &crl->entry;
1995
+
1996
+ while (cur != NULL && cur->serial.len != 0) {
1997
+ if (crt->serial.len == cur->serial.len &&
1998
+ memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
1999
+ return 1;
2000
+ }
2001
+
2002
+ cur = cur->next;
2003
+ }
2004
+
2005
+ return 0;
2006
+ }
2007
+
2008
+ /*
2009
+ * Check that the given certificate is not revoked according to the CRL.
2010
+ * Skip validation if no CRL for the given CA is present.
2011
+ */
2012
+ static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2013
+ mbedtls_x509_crl *crl_list,
2014
+ const mbedtls_x509_crt_profile *profile,
2015
+ const mbedtls_x509_time *now)
2016
+ {
2017
+ int flags = 0;
2018
+ unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2019
+ psa_algorithm_t psa_algorithm;
2020
+ size_t hash_length;
2021
+
2022
+ if (ca == NULL) {
2023
+ return flags;
2024
+ }
2025
+
2026
+ while (crl_list != NULL) {
2027
+ if (crl_list->version == 0 ||
2028
+ x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
2029
+ crl_list = crl_list->next;
2030
+ continue;
2031
+ }
2032
+
2033
+ /*
2034
+ * Check if the CA is configured to sign CRLs
2035
+ */
2036
+ if (mbedtls_x509_crt_check_key_usage(ca,
2037
+ MBEDTLS_X509_KU_CRL_SIGN) != 0) {
2038
+ flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2039
+ break;
2040
+ }
2041
+
2042
+ /*
2043
+ * Check if CRL is correctly signed by the trusted CA
2044
+ */
2045
+ if (mbedtls_x509_profile_check_md_alg(profile, crl_list->sig_md) != 0) {
2046
+ flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2047
+ }
2048
+
2049
+ if (mbedtls_x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0) {
2050
+ flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2051
+ }
2052
+
2053
+ psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
2054
+ if (psa_hash_compute(psa_algorithm,
2055
+ crl_list->tbs.p,
2056
+ crl_list->tbs.len,
2057
+ hash,
2058
+ sizeof(hash),
2059
+ &hash_length) != PSA_SUCCESS) {
2060
+ /* Note: this can't happen except after an internal error */
2061
+ flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2062
+ break;
2063
+ }
2064
+
2065
+ if (x509_profile_check_key(profile, &ca->pk) != 0) {
2066
+ flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2067
+ }
2068
+
2069
+ if (mbedtls_pk_verify_ext(crl_list->sig_pk, &ca->pk,
2070
+ crl_list->sig_md, hash, hash_length,
2071
+ crl_list->sig.p, crl_list->sig.len) != 0) {
2072
+ flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2073
+ break;
2074
+ }
2075
+
2076
+ #if defined(MBEDTLS_HAVE_TIME_DATE)
2077
+ /*
2078
+ * Check for validity of CRL (Do not drop out)
2079
+ */
2080
+ if (mbedtls_x509_time_cmp(&crl_list->next_update, now) < 0) {
2081
+ flags |= MBEDTLS_X509_BADCRL_EXPIRED;
2082
+ }
2083
+
2084
+ if (mbedtls_x509_time_cmp(&crl_list->this_update, now) > 0) {
2085
+ flags |= MBEDTLS_X509_BADCRL_FUTURE;
2086
+ }
2087
+ #else
2088
+ ((void) now);
2089
+ #endif
2090
+
2091
+ /*
2092
+ * Check if certificate is revoked
2093
+ */
2094
+ if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
2095
+ flags |= MBEDTLS_X509_BADCERT_REVOKED;
2096
+ break;
2097
+ }
2098
+
2099
+ crl_list = crl_list->next;
2100
+ }
2101
+
2102
+ return flags;
2103
+ }
2104
+ #endif /* MBEDTLS_X509_CRL_PARSE_C */
2105
+
2106
+ /*
2107
+ * Check the signature of a certificate by its parent
2108
+ */
2109
+ static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2110
+ mbedtls_x509_crt *parent,
2111
+ mbedtls_x509_crt_restart_ctx *rs_ctx)
2112
+ {
2113
+ size_t hash_len;
2114
+ unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2115
+ psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
2116
+ psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2117
+
2118
+ /* Skip expensive computation on obvious mismatch */
2119
+ if (!mbedtls_pk_can_do_psa(&parent->pk,
2120
+ mbedtls_psa_alg_from_pk_sigalg(child->sig_pk, hash_alg),
2121
+ PSA_KEY_USAGE_VERIFY_HASH)) {
2122
+ return -1;
2123
+ }
2124
+
2125
+ status = psa_hash_compute(hash_alg,
2126
+ child->tbs.p,
2127
+ child->tbs.len,
2128
+ hash,
2129
+ sizeof(hash),
2130
+ &hash_len);
2131
+ if (status != PSA_SUCCESS) {
2132
+ return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
2133
+ }
2134
+
2135
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2136
+ if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_SIGALG_ECDSA) {
2137
+ return mbedtls_pk_verify_restartable(&parent->pk,
2138
+ child->sig_md, hash, hash_len,
2139
+ child->sig.p, child->sig.len, &rs_ctx->pk);
2140
+ }
2141
+ #else
2142
+ (void) rs_ctx;
2143
+ #endif
2144
+
2145
+ return mbedtls_pk_verify_ext(child->sig_pk, &parent->pk,
2146
+ child->sig_md, hash, hash_len,
2147
+ child->sig.p, child->sig.len);
2148
+ }
2149
+
2150
+ /*
2151
+ * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2152
+ * Return 0 if yes, -1 if not.
2153
+ *
2154
+ * top means parent is a locally-trusted certificate
2155
+ */
2156
+ static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2157
+ const mbedtls_x509_crt *parent,
2158
+ int top)
2159
+ {
2160
+ int need_ca_bit;
2161
+
2162
+ /* Parent must be the issuer */
2163
+ if (x509_name_cmp(&child->issuer, &parent->subject) != 0) {
2164
+ return -1;
2165
+ }
2166
+
2167
+ /* Parent must have the basicConstraints CA bit set as a general rule */
2168
+ need_ca_bit = 1;
2169
+
2170
+ /* Exception: v1/v2 certificates that are locally trusted. */
2171
+ if (top && parent->version < 3) {
2172
+ need_ca_bit = 0;
2173
+ }
2174
+
2175
+ if (need_ca_bit && !parent->ca_istrue) {
2176
+ return -1;
2177
+ }
2178
+
2179
+ if (need_ca_bit &&
2180
+ mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2181
+ return -1;
2182
+ }
2183
+
2184
+ return 0;
2185
+ }
2186
+
2187
+ /*
2188
+ * Find a suitable parent for child in candidates, or return NULL.
2189
+ *
2190
+ * Here suitable is defined as:
2191
+ * 1. subject name matches child's issuer
2192
+ * 2. if necessary, the CA bit is set and key usage allows signing certs
2193
+ * 3. for trusted roots, the signature is correct
2194
+ * (for intermediates, the signature is checked and the result reported)
2195
+ * 4. pathlen constraints are satisfied
2196
+ *
2197
+ * If there's a suitable candidate which is also time-valid, return the first
2198
+ * such. Otherwise, return the first suitable candidate (or NULL if there is
2199
+ * none).
2200
+ *
2201
+ * The rationale for this rule is that someone could have a list of trusted
2202
+ * roots with two versions on the same root with different validity periods.
2203
+ * (At least one user reported having such a list and wanted it to just work.)
2204
+ * The reason we don't just require time-validity is that generally there is
2205
+ * only one version, and if it's expired we want the flags to state that
2206
+ * rather than NOT_TRUSTED, as would be the case if we required it here.
2207
+ *
2208
+ * The rationale for rule 3 (signature for trusted roots) is that users might
2209
+ * have two versions of the same CA with different keys in their list, and the
2210
+ * way we select the correct one is by checking the signature (as we don't
2211
+ * rely on key identifier extensions). (This is one way users might choose to
2212
+ * handle key rollover, another relies on self-issued certs, see [SIRO].)
2213
+ *
2214
+ * Arguments:
2215
+ * - [in] child: certificate for which we're looking for a parent
2216
+ * - [in] candidates: chained list of potential parents
2217
+ * - [out] r_parent: parent found (or NULL)
2218
+ * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
2219
+ * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2220
+ * of the chain, 0 otherwise
2221
+ * - [in] path_cnt: number of intermediates seen so far
2222
+ * - [in] self_cnt: number of self-signed intermediates seen so far
2223
+ * (will never be greater than path_cnt)
2224
+ * - [in-out] rs_ctx: context for restarting operations
2225
+ *
2226
+ * Return value:
2227
+ * - 0 on success
2228
+ * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
2229
+ */
2230
+ static int x509_crt_find_parent_in(
2231
+ mbedtls_x509_crt *child,
2232
+ mbedtls_x509_crt *candidates,
2233
+ mbedtls_x509_crt **r_parent,
2234
+ int *r_signature_is_good,
2235
+ int top,
2236
+ unsigned path_cnt,
2237
+ unsigned self_cnt,
2238
+ mbedtls_x509_crt_restart_ctx *rs_ctx,
2239
+ const mbedtls_x509_time *now)
2240
+ {
2241
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2242
+ mbedtls_x509_crt *parent, *fallback_parent;
2243
+ int signature_is_good = 0, fallback_signature_is_good;
2244
+
2245
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2246
+ /* did we have something in progress? */
2247
+ if (rs_ctx != NULL && rs_ctx->parent != NULL) {
2248
+ /* restore saved state */
2249
+ parent = rs_ctx->parent;
2250
+ fallback_parent = rs_ctx->fallback_parent;
2251
+ fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
2252
+
2253
+ /* clear saved state */
2254
+ rs_ctx->parent = NULL;
2255
+ rs_ctx->fallback_parent = NULL;
2256
+ rs_ctx->fallback_signature_is_good = 0;
2257
+
2258
+ /* resume where we left */
2259
+ goto check_signature;
2260
+ }
2261
+ #endif
2262
+
2263
+ fallback_parent = NULL;
2264
+ fallback_signature_is_good = 0;
2265
+
2266
+ for (parent = candidates; parent != NULL; parent = parent->next) {
2267
+ /* basic parenting skills (name, CA bit, key usage) */
2268
+ if (x509_crt_check_parent(child, parent, top) != 0) {
2269
+ continue;
2270
+ }
2271
+
2272
+ /* +1 because stored max_pathlen is 1 higher that the actual value */
2273
+ if (parent->max_pathlen > 0 &&
2274
+ (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
2275
+ continue;
2276
+ }
2277
+
2278
+ /* Signature */
2279
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2280
+ check_signature:
2281
+ #endif
2282
+ ret = x509_crt_check_signature(child, parent, rs_ctx);
2283
+
2284
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2285
+ if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2286
+ /* save state */
2287
+ rs_ctx->parent = parent;
2288
+ rs_ctx->fallback_parent = fallback_parent;
2289
+ rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
2290
+
2291
+ return ret;
2292
+ }
2293
+ #else
2294
+ (void) ret;
2295
+ #endif
2296
+
2297
+ signature_is_good = ret == 0;
2298
+ if (top && !signature_is_good) {
2299
+ continue;
2300
+ }
2301
+
2302
+ #if defined(MBEDTLS_HAVE_TIME_DATE)
2303
+ /* optional time check */
2304
+ if (mbedtls_x509_time_cmp(&parent->valid_to, now) < 0 || /* past */
2305
+ mbedtls_x509_time_cmp(&parent->valid_from, now) > 0) { /* future */
2306
+ if (fallback_parent == NULL) {
2307
+ fallback_parent = parent;
2308
+ fallback_signature_is_good = signature_is_good;
2309
+ }
2310
+
2311
+ continue;
2312
+ }
2313
+ #else
2314
+ ((void) now);
2315
+ #endif
2316
+
2317
+ *r_parent = parent;
2318
+ *r_signature_is_good = signature_is_good;
2319
+
2320
+ break;
2321
+ }
2322
+
2323
+ if (parent == NULL) {
2324
+ *r_parent = fallback_parent;
2325
+ *r_signature_is_good = fallback_signature_is_good;
2326
+ }
2327
+
2328
+ return 0;
2329
+ }
2330
+
2331
+ /*
2332
+ * Find a parent in trusted CAs or the provided chain, or return NULL.
2333
+ *
2334
+ * Searches in trusted CAs first, and return the first suitable parent found
2335
+ * (see find_parent_in() for definition of suitable).
2336
+ *
2337
+ * Arguments:
2338
+ * - [in] child: certificate for which we're looking for a parent, followed
2339
+ * by a chain of possible intermediates
2340
+ * - [in] trust_ca: list of locally trusted certificates
2341
+ * - [out] parent: parent found (or NULL)
2342
+ * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2343
+ * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2344
+ * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2345
+ * - [in] self_cnt: number of self-signed certs in the chain so far
2346
+ * (will always be no greater than path_cnt)
2347
+ * - [in-out] rs_ctx: context for restarting operations
2348
+ *
2349
+ * Return value:
2350
+ * - 0 on success
2351
+ * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
2352
+ */
2353
+ static int x509_crt_find_parent(
2354
+ mbedtls_x509_crt *child,
2355
+ mbedtls_x509_crt *trust_ca,
2356
+ mbedtls_x509_crt **parent,
2357
+ int *parent_is_trusted,
2358
+ int *signature_is_good,
2359
+ unsigned path_cnt,
2360
+ unsigned self_cnt,
2361
+ mbedtls_x509_crt_restart_ctx *rs_ctx,
2362
+ const mbedtls_x509_time *now)
2363
+ {
2364
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2365
+ mbedtls_x509_crt *search_list;
2366
+
2367
+ *parent_is_trusted = 1;
2368
+
2369
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2370
+ /* restore then clear saved state if we have some stored */
2371
+ if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
2372
+ *parent_is_trusted = rs_ctx->parent_is_trusted;
2373
+ rs_ctx->parent_is_trusted = -1;
2374
+ }
2375
+ #endif
2376
+
2377
+ while (1) {
2378
+ search_list = *parent_is_trusted ? trust_ca : child->next;
2379
+
2380
+ ret = x509_crt_find_parent_in(child, search_list,
2381
+ parent, signature_is_good,
2382
+ *parent_is_trusted,
2383
+ path_cnt, self_cnt, rs_ctx, now);
2384
+
2385
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2386
+ if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2387
+ /* save state */
2388
+ rs_ctx->parent_is_trusted = *parent_is_trusted;
2389
+ return ret;
2390
+ }
2391
+ #else
2392
+ (void) ret;
2393
+ #endif
2394
+
2395
+ /* stop here if found or already in second iteration */
2396
+ if (*parent != NULL || *parent_is_trusted == 0) {
2397
+ break;
2398
+ }
2399
+
2400
+ /* prepare second iteration */
2401
+ *parent_is_trusted = 0;
2402
+ }
2403
+
2404
+ /* extra precaution against mistakes in the caller */
2405
+ if (*parent == NULL) {
2406
+ *parent_is_trusted = 0;
2407
+ *signature_is_good = 0;
2408
+ }
2409
+
2410
+ return 0;
2411
+ }
2412
+
2413
+ /*
2414
+ * Check if an end-entity certificate is locally trusted
2415
+ *
2416
+ * Currently we require such certificates to be self-signed (actually only
2417
+ * check for self-issued as self-signatures are not checked)
2418
+ */
2419
+ static int x509_crt_check_ee_locally_trusted(
2420
+ mbedtls_x509_crt *crt,
2421
+ mbedtls_x509_crt *trust_ca)
2422
+ {
2423
+ mbedtls_x509_crt *cur;
2424
+
2425
+ /* must be self-issued */
2426
+ if (x509_name_cmp(&crt->issuer, &crt->subject) != 0) {
2427
+ return -1;
2428
+ }
2429
+
2430
+ /* look for an exact match with trusted cert */
2431
+ for (cur = trust_ca; cur != NULL; cur = cur->next) {
2432
+ if (crt->raw.len == cur->raw.len &&
2433
+ memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2434
+ return 0;
2435
+ }
2436
+ }
2437
+
2438
+ /* too bad */
2439
+ return -1;
2440
+ }
2441
+
2442
+ /*
2443
+ * Build and verify a certificate chain
2444
+ *
2445
+ * Given a peer-provided list of certificates EE, C1, ..., Cn and
2446
+ * a list of trusted certs R1, ... Rp, try to build and verify a chain
2447
+ * EE, Ci1, ... Ciq [, Rj]
2448
+ * such that every cert in the chain is a child of the next one,
2449
+ * jumping to a trusted root as early as possible.
2450
+ *
2451
+ * Verify that chain and return it with flags for all issues found.
2452
+ *
2453
+ * Special cases:
2454
+ * - EE == Rj -> return a one-element list containing it
2455
+ * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2456
+ * -> return that chain with NOT_TRUSTED set on Ciq
2457
+ *
2458
+ * Tests for (aspects of) this function should include at least:
2459
+ * - trusted EE
2460
+ * - EE -> trusted root
2461
+ * - EE -> intermediate CA -> trusted root
2462
+ * - if relevant: EE untrusted
2463
+ * - if relevant: EE -> intermediate, untrusted
2464
+ * with the aspect under test checked at each relevant level (EE, int, root).
2465
+ * For some aspects longer chains are required, but usually length 2 is
2466
+ * enough (but length 1 is not in general).
2467
+ *
2468
+ * Arguments:
2469
+ * - [in] crt: the cert list EE, C1, ..., Cn
2470
+ * - [in] trust_ca: the trusted list R1, ..., Rp
2471
+ * - [in] ca_crl, profile: as in verify_with_profile()
2472
+ * - [out] ver_chain: the built and verified chain
2473
+ * Only valid when return value is 0, may contain garbage otherwise!
2474
+ * Restart note: need not be the same when calling again to resume.
2475
+ * - [in-out] rs_ctx: context for restarting operations
2476
+ *
2477
+ * Return value:
2478
+ * - non-zero if the chain could not be fully built and examined
2479
+ * - 0 is the chain was successfully built and examined,
2480
+ * even if it was found to be invalid
2481
+ */
2482
+ static int x509_crt_verify_chain(
2483
+ mbedtls_x509_crt *crt,
2484
+ mbedtls_x509_crt *trust_ca,
2485
+ mbedtls_x509_crl *ca_crl,
2486
+ mbedtls_x509_crt_ca_cb_t f_ca_cb,
2487
+ void *p_ca_cb,
2488
+ const mbedtls_x509_crt_profile *profile,
2489
+ mbedtls_x509_crt_verify_chain *ver_chain,
2490
+ mbedtls_x509_crt_restart_ctx *rs_ctx)
2491
+ {
2492
+ /* Don't initialize any of those variables here, so that the compiler can
2493
+ * catch potential issues with jumping ahead when restarting */
2494
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2495
+ uint32_t *flags;
2496
+ mbedtls_x509_crt_verify_chain_item *cur;
2497
+ mbedtls_x509_crt *child;
2498
+ mbedtls_x509_crt *parent;
2499
+ int parent_is_trusted;
2500
+ int child_is_trusted;
2501
+ int signature_is_good;
2502
+ unsigned self_cnt;
2503
+ mbedtls_x509_crt *cur_trust_ca = NULL;
2504
+ mbedtls_x509_time now;
2505
+
2506
+ #if defined(MBEDTLS_HAVE_TIME_DATE)
2507
+ if (mbedtls_x509_time_gmtime(mbedtls_time(NULL), &now) != 0) {
2508
+ return MBEDTLS_ERR_X509_FATAL_ERROR;
2509
+ }
2510
+ #endif
2511
+
2512
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2513
+ /* resume if we had an operation in progress */
2514
+ if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
2515
+ /* restore saved state */
2516
+ *ver_chain = rs_ctx->ver_chain; /* struct copy */
2517
+ self_cnt = rs_ctx->self_cnt;
2518
+
2519
+ /* restore derived state */
2520
+ cur = &ver_chain->items[ver_chain->len - 1];
2521
+ child = cur->crt;
2522
+ flags = &cur->flags;
2523
+
2524
+ goto find_parent;
2525
+ }
2526
+ #endif /* MBEDTLS_ECP_RESTARTABLE */
2527
+
2528
+ child = crt;
2529
+ self_cnt = 0;
2530
+ parent_is_trusted = 0;
2531
+ child_is_trusted = 0;
2532
+
2533
+ while (1) {
2534
+ /* Add certificate to the verification chain */
2535
+ cur = &ver_chain->items[ver_chain->len];
2536
+ cur->crt = child;
2537
+ cur->flags = 0;
2538
+ ver_chain->len++;
2539
+ flags = &cur->flags;
2540
+
2541
+ #if defined(MBEDTLS_HAVE_TIME_DATE)
2542
+ /* Check time-validity (all certificates) */
2543
+ if (mbedtls_x509_time_cmp(&child->valid_to, &now) < 0) {
2544
+ *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2545
+ }
2546
+
2547
+ if (mbedtls_x509_time_cmp(&child->valid_from, &now) > 0) {
2548
+ *flags |= MBEDTLS_X509_BADCERT_FUTURE;
2549
+ }
2550
+ #endif
2551
+
2552
+ /* Stop here for trusted roots (but not for trusted EE certs) */
2553
+ if (child_is_trusted) {
2554
+ return 0;
2555
+ }
2556
+
2557
+ /* Check signature algorithm: MD & PK algs */
2558
+ if (mbedtls_x509_profile_check_md_alg(profile, child->sig_md) != 0) {
2559
+ *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2560
+ }
2561
+
2562
+ if (mbedtls_x509_profile_check_pk_alg(profile, child->sig_pk) != 0) {
2563
+ *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2564
+ }
2565
+
2566
+ /* Special case: EE certs that are locally trusted */
2567
+ if (ver_chain->len == 1 &&
2568
+ x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2569
+ return 0;
2570
+ }
2571
+
2572
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2573
+ find_parent:
2574
+ #endif
2575
+
2576
+ /* Obtain list of potential trusted signers from CA callback,
2577
+ * or use statically provided list. */
2578
+ #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2579
+ if (f_ca_cb != NULL) {
2580
+ mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2581
+ mbedtls_free(ver_chain->trust_ca_cb_result);
2582
+ ver_chain->trust_ca_cb_result = NULL;
2583
+
2584
+ ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2585
+ if (ret != 0) {
2586
+ return MBEDTLS_ERR_X509_FATAL_ERROR;
2587
+ }
2588
+
2589
+ cur_trust_ca = ver_chain->trust_ca_cb_result;
2590
+ } else
2591
+ #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2592
+ {
2593
+ ((void) f_ca_cb);
2594
+ ((void) p_ca_cb);
2595
+ cur_trust_ca = trust_ca;
2596
+ }
2597
+
2598
+ /* Look for a parent in trusted CAs or up the chain */
2599
+ ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2600
+ &parent_is_trusted, &signature_is_good,
2601
+ ver_chain->len - 1, self_cnt, rs_ctx,
2602
+ &now);
2603
+
2604
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
2605
+ if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2606
+ /* save state */
2607
+ rs_ctx->in_progress = x509_crt_rs_find_parent;
2608
+ rs_ctx->self_cnt = self_cnt;
2609
+ rs_ctx->ver_chain = *ver_chain; /* struct copy */
2610
+
2611
+ return ret;
2612
+ }
2613
+ #else
2614
+ (void) ret;
2615
+ #endif
2616
+
2617
+ /* No parent? We're done here */
2618
+ if (parent == NULL) {
2619
+ *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2620
+ return 0;
2621
+ }
2622
+
2623
+ /* Count intermediate self-issued (not necessarily self-signed) certs.
2624
+ * These can occur with some strategies for key rollover, see [SIRO],
2625
+ * and should be excluded from max_pathlen checks. */
2626
+ if (ver_chain->len != 1 &&
2627
+ x509_name_cmp(&child->issuer, &child->subject) == 0) {
2628
+ self_cnt++;
2629
+ }
2630
+
2631
+ /* path_cnt is 0 for the first intermediate CA,
2632
+ * and if parent is trusted it's not an intermediate CA */
2633
+ if (!parent_is_trusted &&
2634
+ ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
2635
+ /* return immediately to avoid overflow the chain array */
2636
+ return MBEDTLS_ERR_X509_FATAL_ERROR;
2637
+ }
2638
+
2639
+ /* signature was checked while searching parent */
2640
+ if (!signature_is_good) {
2641
+ *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2642
+ }
2643
+
2644
+ /* check size of signing key */
2645
+ if (x509_profile_check_key(profile, &parent->pk) != 0) {
2646
+ *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2647
+ }
2648
+
2649
+ #if defined(MBEDTLS_X509_CRL_PARSE_C)
2650
+ /* Check trusted CA's CRL for the given crt */
2651
+ *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile, &now);
2652
+ #else
2653
+ (void) ca_crl;
2654
+ #endif
2655
+
2656
+ /* prepare for next iteration */
2657
+ child = parent;
2658
+ parent = NULL;
2659
+ child_is_trusted = parent_is_trusted;
2660
+ signature_is_good = 0;
2661
+ }
2662
+ }
2663
+
2664
+ #ifdef _WIN32
2665
+ #ifdef _MSC_VER
2666
+ #pragma comment(lib, "ws2_32.lib")
2667
+ #include <winsock2.h>
2668
+ #include <ws2tcpip.h>
2669
+ #elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600
2670
+ #include <winsock2.h>
2671
+ #include <ws2tcpip.h>
2672
+ #else
2673
+ /* inet_pton() is not supported, fallback to software version */
2674
+ #define MBEDTLS_TEST_SW_INET_PTON
2675
+ #endif
2676
+ #elif defined(__sun)
2677
+ /* Solaris requires -lsocket -lnsl for inet_pton() */
2678
+ #elif defined(__has_include)
2679
+ #if __has_include(<sys/socket.h>)
2680
+ #include <sys/socket.h>
2681
+ #endif
2682
+ #if __has_include(<arpa/inet.h>)
2683
+ #include <arpa/inet.h>
2684
+ #endif
2685
+ #endif
2686
+
2687
+ /* Use whether or not AF_INET6 is defined to indicate whether or not to use
2688
+ * the platform inet_pton() or a local implementation (below). The local
2689
+ * implementation may be used even in cases where the platform provides
2690
+ * inet_pton(), e.g. when there are different includes required and/or the
2691
+ * platform implementation requires dependencies on additional libraries.
2692
+ * Specifically, Windows requires custom includes and additional link
2693
+ * dependencies, and Solaris requires additional link dependencies.
2694
+ * Also, as a coarse heuristic, use the local implementation if the compiler
2695
+ * does not support __has_include(), or if the definition of AF_INET6 is not
2696
+ * provided by headers included (or not) via __has_include() above.
2697
+ * MBEDTLS_TEST_SW_INET_PTON is a bypass define to force testing of this code //no-check-names
2698
+ * despite having a platform that has inet_pton. */
2699
+ #if !defined(AF_INET6) || defined(MBEDTLS_TEST_SW_INET_PTON) //no-check-names
2700
+ /* Definition located further below to possibly reduce compiler inlining */
2701
+ static int x509_inet_pton_ipv4(const char *src, void *dst);
2702
+
2703
+ #define li_cton(c, n) \
2704
+ (((n) = (c) - '0') <= 9 || (((n) = ((c)&0xdf) - 'A') <= 5 ? ((n) += 10) : 0))
2705
+
2706
+ static int x509_inet_pton_ipv6(const char *src, void *dst)
2707
+ {
2708
+ const unsigned char *p = (const unsigned char *) src;
2709
+ int nonzero_groups = 0, num_digits, zero_group_start = -1;
2710
+ uint16_t addr[8];
2711
+ do {
2712
+ /* note: allows excess leading 0's, e.g. 1:0002:3:... */
2713
+ uint16_t group = num_digits = 0;
2714
+ for (uint8_t digit; num_digits < 4; num_digits++) {
2715
+ if (li_cton(*p, digit) == 0) {
2716
+ break;
2717
+ }
2718
+ group = (group << 4) | digit;
2719
+ p++;
2720
+ }
2721
+ if (num_digits != 0) {
2722
+ MBEDTLS_PUT_UINT16_BE(group, addr, nonzero_groups);
2723
+ nonzero_groups++;
2724
+ if (*p == '\0') {
2725
+ break;
2726
+ } else if (*p == '.') {
2727
+ /* Don't accept IPv4 too early or late:
2728
+ * - The first 6 nonzero groups must be 16 bit pieces of address delimited by ':'
2729
+ * - This might be fully or partially represented with compressed syntax (a zero
2730
+ * group "::")
2731
+ */
2732
+ if ((nonzero_groups < 6 && zero_group_start == -1) ||
2733
+ nonzero_groups >= 7) {
2734
+ break;
2735
+ }
2736
+
2737
+ /* Walk back to prior ':', then parse as IPv4-mapped.
2738
+ * At this point nonzero_groups == 6 or zero_group_start >= 0. Either way we have a
2739
+ * ':' before the current position and still inside the buffer. Thus it is safe to
2740
+ * search back for that ':' without any further checks.
2741
+ */
2742
+ do {
2743
+ p--;
2744
+ } while (*p != ':');
2745
+
2746
+ p++;
2747
+ nonzero_groups--;
2748
+ if (x509_inet_pton_ipv4((const char *) p,
2749
+ addr + nonzero_groups) != 0) {
2750
+ break;
2751
+ }
2752
+
2753
+ nonzero_groups += 2;
2754
+ p = (const unsigned char *) "";
2755
+ break;
2756
+ } else if (*p != ':') {
2757
+ return -1;
2758
+ }
2759
+ } else {
2760
+ /* Don't accept a second zero group or an invalid delimiter */
2761
+ if (zero_group_start != -1 || *p != ':') {
2762
+ return -1;
2763
+ }
2764
+ zero_group_start = nonzero_groups;
2765
+
2766
+ /* Accept a zero group at start, but it has to be a double colon */
2767
+ if (zero_group_start == 0 && *++p != ':') {
2768
+ return -1;
2769
+ }
2770
+
2771
+ if (p[1] == '\0') {
2772
+ ++p;
2773
+ break;
2774
+ }
2775
+ }
2776
+ ++p;
2777
+ } while (nonzero_groups < 8);
2778
+
2779
+ if (*p != '\0') {
2780
+ return -1;
2781
+ }
2782
+
2783
+ if (zero_group_start != -1) {
2784
+ if (nonzero_groups > 6) {
2785
+ return -1;
2786
+ }
2787
+ int zero_groups = 8 - nonzero_groups;
2788
+ int groups_after_zero = nonzero_groups - zero_group_start;
2789
+
2790
+ /* Move the non-zero part to after the zeroes */
2791
+ if (groups_after_zero) {
2792
+ memmove(addr + zero_group_start + zero_groups,
2793
+ addr + zero_group_start,
2794
+ groups_after_zero * sizeof(*addr));
2795
+ }
2796
+ memset(addr + zero_group_start, 0, zero_groups * sizeof(*addr));
2797
+ } else {
2798
+ if (nonzero_groups != 8) {
2799
+ return -1;
2800
+ }
2801
+ }
2802
+ memcpy(dst, addr, sizeof(addr));
2803
+ return 0;
2804
+ }
2805
+
2806
+ static int x509_inet_pton_ipv4(const char *src, void *dst)
2807
+ {
2808
+ const unsigned char *p = (const unsigned char *) src;
2809
+ uint8_t *res = (uint8_t *) dst;
2810
+ uint8_t digit, num_digits = 0;
2811
+ uint8_t num_octets = 0;
2812
+ uint16_t octet;
2813
+
2814
+ do {
2815
+ octet = num_digits = 0;
2816
+ do {
2817
+ digit = *p - '0';
2818
+ if (digit > 9) {
2819
+ break;
2820
+ }
2821
+
2822
+ /* Don't allow leading zeroes. These might mean octal format,
2823
+ * which this implementation does not support. */
2824
+ if (octet == 0 && num_digits > 0) {
2825
+ return -1;
2826
+ }
2827
+
2828
+ octet = octet * 10 + digit;
2829
+ num_digits++;
2830
+ p++;
2831
+ } while (num_digits < 3);
2832
+
2833
+ if (octet >= 256 || num_digits > 3 || num_digits == 0) {
2834
+ return -1;
2835
+ }
2836
+ *res++ = (uint8_t) octet;
2837
+ num_octets++;
2838
+ } while (num_octets < 4 && *p++ == '.');
2839
+ return num_octets == 4 && *p == '\0' ? 0 : -1;
2840
+ }
2841
+
2842
+ #else
2843
+
2844
+ static int x509_inet_pton_ipv6(const char *src, void *dst)
2845
+ {
2846
+ return inet_pton(AF_INET6, src, dst) == 1 ? 0 : -1;
2847
+ }
2848
+
2849
+ static int x509_inet_pton_ipv4(const char *src, void *dst)
2850
+ {
2851
+ return inet_pton(AF_INET, src, dst) == 1 ? 0 : -1;
2852
+ }
2853
+
2854
+ #endif /* !AF_INET6 || MBEDTLS_TEST_SW_INET_PTON */ //no-check-names
2855
+
2856
+ size_t mbedtls_x509_crt_parse_cn_inet_pton(const char *cn, void *dst)
2857
+ {
2858
+ return strchr(cn, ':') == NULL
2859
+ ? x509_inet_pton_ipv4(cn, dst) == 0 ? 4 : 0
2860
+ : x509_inet_pton_ipv6(cn, dst) == 0 ? 16 : 0;
2861
+ }
2862
+
2863
+ /*
2864
+ * Check for CN match
2865
+ */
2866
+ static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2867
+ const char *cn, size_t cn_len)
2868
+ {
2869
+ /* try exact match */
2870
+ if (name->len == cn_len &&
2871
+ x509_memcasecmp(cn, name->p, cn_len) == 0) {
2872
+ return 0;
2873
+ }
2874
+
2875
+ /* try wildcard match */
2876
+ if (x509_check_wildcard(cn, name) == 0) {
2877
+ return 0;
2878
+ }
2879
+
2880
+ return -1;
2881
+ }
2882
+
2883
+ static int x509_crt_check_san_ip(const mbedtls_x509_sequence *san,
2884
+ const char *cn, size_t cn_len)
2885
+ {
2886
+ uint32_t ip[4];
2887
+ cn_len = mbedtls_x509_crt_parse_cn_inet_pton(cn, ip);
2888
+ if (cn_len == 0) {
2889
+ return -1;
2890
+ }
2891
+
2892
+ for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2893
+ const unsigned char san_type = (unsigned char) cur->buf.tag &
2894
+ MBEDTLS_ASN1_TAG_VALUE_MASK;
2895
+ if (san_type == MBEDTLS_X509_SAN_IP_ADDRESS &&
2896
+ cur->buf.len == cn_len && memcmp(cur->buf.p, ip, cn_len) == 0) {
2897
+ return 0;
2898
+ }
2899
+ }
2900
+
2901
+ return -1;
2902
+ }
2903
+
2904
+ static int x509_crt_check_san_uri(const mbedtls_x509_sequence *san,
2905
+ const char *cn, size_t cn_len)
2906
+ {
2907
+ for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2908
+ const unsigned char san_type = (unsigned char) cur->buf.tag &
2909
+ MBEDTLS_ASN1_TAG_VALUE_MASK;
2910
+ if (san_type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER &&
2911
+ cur->buf.len == cn_len && memcmp(cur->buf.p, cn, cn_len) == 0) {
2912
+ return 0;
2913
+ }
2914
+ }
2915
+
2916
+ return -1;
2917
+ }
2918
+
2919
+ /*
2920
+ * Check for SAN match, see RFC 5280 Section 4.2.1.6
2921
+ */
2922
+ static int x509_crt_check_san(const mbedtls_x509_sequence *san,
2923
+ const char *cn, size_t cn_len)
2924
+ {
2925
+ int san_ip = 0;
2926
+ int san_uri = 0;
2927
+ /* Prioritize DNS name over other subtypes due to popularity */
2928
+ for (const mbedtls_x509_sequence *cur = san; cur != NULL; cur = cur->next) {
2929
+ switch ((unsigned char) cur->buf.tag & MBEDTLS_ASN1_TAG_VALUE_MASK) {
2930
+ case MBEDTLS_X509_SAN_DNS_NAME:
2931
+ if (x509_crt_check_cn(&cur->buf, cn, cn_len) == 0) {
2932
+ return 0;
2933
+ }
2934
+ break;
2935
+ case MBEDTLS_X509_SAN_IP_ADDRESS:
2936
+ san_ip = 1;
2937
+ break;
2938
+ case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
2939
+ san_uri = 1;
2940
+ break;
2941
+ /* (We may handle other types here later.) */
2942
+ default: /* Unrecognized type */
2943
+ break;
2944
+ }
2945
+ }
2946
+ if (san_ip) {
2947
+ if (x509_crt_check_san_ip(san, cn, cn_len) == 0) {
2948
+ return 0;
2949
+ }
2950
+ }
2951
+ if (san_uri) {
2952
+ if (x509_crt_check_san_uri(san, cn, cn_len) == 0) {
2953
+ return 0;
2954
+ }
2955
+ }
2956
+
2957
+ return -1;
2958
+ }
2959
+
2960
+ /*
2961
+ * Verify the requested CN - only call this if cn is not NULL!
2962
+ */
2963
+ static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2964
+ const char *cn,
2965
+ uint32_t *flags)
2966
+ {
2967
+ const mbedtls_x509_name *name;
2968
+ size_t cn_len = strlen(cn);
2969
+
2970
+ if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2971
+ if (x509_crt_check_san(&crt->subject_alt_names, cn, cn_len) == 0) {
2972
+ return;
2973
+ }
2974
+ } else {
2975
+ for (name = &crt->subject; name != NULL; name = name->next) {
2976
+ if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2977
+ x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
2978
+ return;
2979
+ }
2980
+ }
2981
+
2982
+ }
2983
+
2984
+ *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2985
+ }
2986
+
2987
+ /*
2988
+ * Merge the flags for all certs in the chain, after calling callback
2989
+ */
2990
+ static int x509_crt_merge_flags_with_cb(
2991
+ uint32_t *flags,
2992
+ const mbedtls_x509_crt_verify_chain *ver_chain,
2993
+ int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2994
+ void *p_vrfy)
2995
+ {
2996
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2997
+ unsigned i;
2998
+ uint32_t cur_flags;
2999
+ const mbedtls_x509_crt_verify_chain_item *cur;
3000
+
3001
+ for (i = ver_chain->len; i != 0; --i) {
3002
+ cur = &ver_chain->items[i-1];
3003
+ cur_flags = cur->flags;
3004
+
3005
+ if (NULL != f_vrfy) {
3006
+ if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i-1, &cur_flags)) != 0) {
3007
+ return ret;
3008
+ }
3009
+ }
3010
+
3011
+ *flags |= cur_flags;
3012
+ }
3013
+
3014
+ return 0;
3015
+ }
3016
+
3017
+ /*
3018
+ * Verify the certificate validity, with profile, restartable version
3019
+ *
3020
+ * This function:
3021
+ * - checks the requested CN (if any)
3022
+ * - checks the type and size of the EE cert's key,
3023
+ * as that isn't done as part of chain building/verification currently
3024
+ * - builds and verifies the chain
3025
+ * - then calls the callback and merges the flags
3026
+ *
3027
+ * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
3028
+ * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
3029
+ * verification routine to search for trusted signers, and CRLs will
3030
+ * be disabled. Otherwise, `trust_ca` will be used as the static list
3031
+ * of trusted signers, and `ca_crl` will be use as the static list
3032
+ * of CRLs.
3033
+ */
3034
+ static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
3035
+ mbedtls_x509_crt *trust_ca,
3036
+ mbedtls_x509_crl *ca_crl,
3037
+ mbedtls_x509_crt_ca_cb_t f_ca_cb,
3038
+ void *p_ca_cb,
3039
+ const mbedtls_x509_crt_profile *profile,
3040
+ const char *cn, uint32_t *flags,
3041
+ int (*f_vrfy)(void *,
3042
+ mbedtls_x509_crt *,
3043
+ int,
3044
+ uint32_t *),
3045
+ void *p_vrfy,
3046
+ mbedtls_x509_crt_restart_ctx *rs_ctx)
3047
+ {
3048
+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3049
+ mbedtls_pk_type_t pk_type;
3050
+ mbedtls_x509_crt_verify_chain ver_chain;
3051
+ uint32_t ee_flags;
3052
+
3053
+ *flags = 0;
3054
+ ee_flags = 0;
3055
+ x509_crt_verify_chain_reset(&ver_chain);
3056
+
3057
+ if (profile == NULL) {
3058
+ ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
3059
+ goto exit;
3060
+ }
3061
+
3062
+ /* check name if requested */
3063
+ if (cn != NULL) {
3064
+ x509_crt_verify_name(crt, cn, &ee_flags);
3065
+ }
3066
+
3067
+ /* Check the type and size of the key */
3068
+ pk_type = mbedtls_pk_get_type(&crt->pk);
3069
+
3070
+ if (mbedtls_x509_profile_check_pk_alg(profile, (mbedtls_pk_sigalg_t) pk_type) != 0) {
3071
+ ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
3072
+ }
3073
+
3074
+ if (x509_profile_check_key(profile, &crt->pk) != 0) {
3075
+ ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
3076
+ }
3077
+
3078
+ /* Check the chain */
3079
+ ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
3080
+ f_ca_cb, p_ca_cb, profile,
3081
+ &ver_chain, rs_ctx);
3082
+
3083
+ if (ret != 0) {
3084
+ goto exit;
3085
+ }
3086
+
3087
+ /* Merge end-entity flags */
3088
+ ver_chain.items[0].flags |= ee_flags;
3089
+
3090
+ /* Build final flags, calling callback on the way if any */
3091
+ ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
3092
+
3093
+ exit:
3094
+
3095
+ #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3096
+ mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
3097
+ mbedtls_free(ver_chain.trust_ca_cb_result);
3098
+ ver_chain.trust_ca_cb_result = NULL;
3099
+ #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3100
+
3101
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
3102
+ if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
3103
+ mbedtls_x509_crt_restart_free(rs_ctx);
3104
+ }
3105
+ #endif
3106
+
3107
+ /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
3108
+ * the SSL module for authmode optional, but non-zero return from the
3109
+ * callback means a fatal error so it shouldn't be ignored */
3110
+ if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
3111
+ ret = MBEDTLS_ERR_X509_FATAL_ERROR;
3112
+ }
3113
+
3114
+ if (ret != 0) {
3115
+ *flags = (uint32_t) -1;
3116
+ return ret;
3117
+ }
3118
+
3119
+ if (*flags != 0) {
3120
+ return MBEDTLS_ERR_X509_CERT_VERIFY_FAILED;
3121
+ }
3122
+
3123
+ return 0;
3124
+ }
3125
+
3126
+
3127
+ /*
3128
+ * Verify the certificate validity (default profile, not restartable)
3129
+ */
3130
+ int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
3131
+ mbedtls_x509_crt *trust_ca,
3132
+ mbedtls_x509_crl *ca_crl,
3133
+ const char *cn, uint32_t *flags,
3134
+ int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3135
+ void *p_vrfy)
3136
+ {
3137
+ return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3138
+ NULL, NULL,
3139
+ &mbedtls_x509_crt_profile_default,
3140
+ cn, flags,
3141
+ f_vrfy, p_vrfy, NULL);
3142
+ }
3143
+
3144
+ /*
3145
+ * Verify the certificate validity (user-chosen profile, not restartable)
3146
+ */
3147
+ int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3148
+ mbedtls_x509_crt *trust_ca,
3149
+ mbedtls_x509_crl *ca_crl,
3150
+ const mbedtls_x509_crt_profile *profile,
3151
+ const char *cn, uint32_t *flags,
3152
+ int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3153
+ void *p_vrfy)
3154
+ {
3155
+ return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3156
+ NULL, NULL,
3157
+ profile, cn, flags,
3158
+ f_vrfy, p_vrfy, NULL);
3159
+ }
3160
+
3161
+ #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3162
+ /*
3163
+ * Verify the certificate validity (user-chosen profile, CA callback,
3164
+ * not restartable).
3165
+ */
3166
+ int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3167
+ mbedtls_x509_crt_ca_cb_t f_ca_cb,
3168
+ void *p_ca_cb,
3169
+ const mbedtls_x509_crt_profile *profile,
3170
+ const char *cn, uint32_t *flags,
3171
+ int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3172
+ void *p_vrfy)
3173
+ {
3174
+ return x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3175
+ f_ca_cb, p_ca_cb,
3176
+ profile, cn, flags,
3177
+ f_vrfy, p_vrfy, NULL);
3178
+ }
3179
+ #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3180
+
3181
+ int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3182
+ mbedtls_x509_crt *trust_ca,
3183
+ mbedtls_x509_crl *ca_crl,
3184
+ const mbedtls_x509_crt_profile *profile,
3185
+ const char *cn, uint32_t *flags,
3186
+ int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3187
+ void *p_vrfy,
3188
+ mbedtls_x509_crt_restart_ctx *rs_ctx)
3189
+ {
3190
+ return x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3191
+ NULL, NULL,
3192
+ profile, cn, flags,
3193
+ f_vrfy, p_vrfy, rs_ctx);
3194
+ }
3195
+
3196
+
3197
+ /*
3198
+ * Initialize a certificate chain
3199
+ */
3200
+ void mbedtls_x509_crt_init(mbedtls_x509_crt *crt)
3201
+ {
3202
+ memset(crt, 0, sizeof(mbedtls_x509_crt));
3203
+ }
3204
+
3205
+ /*
3206
+ * Unallocate all certificate data
3207
+ */
3208
+ void mbedtls_x509_crt_free(mbedtls_x509_crt *crt)
3209
+ {
3210
+ mbedtls_x509_crt *cert_cur = crt;
3211
+ mbedtls_x509_crt *cert_prv;
3212
+
3213
+ while (cert_cur != NULL) {
3214
+ mbedtls_pk_free(&cert_cur->pk);
3215
+
3216
+ mbedtls_asn1_free_named_data_list_shallow(cert_cur->issuer.next);
3217
+ mbedtls_asn1_free_named_data_list_shallow(cert_cur->subject.next);
3218
+ mbedtls_asn1_sequence_free(cert_cur->ext_key_usage.next);
3219
+ mbedtls_asn1_sequence_free(cert_cur->subject_alt_names.next);
3220
+ mbedtls_asn1_sequence_free(cert_cur->certificate_policies.next);
3221
+ mbedtls_asn1_sequence_free(cert_cur->authority_key_id.authorityCertIssuer.next);
3222
+
3223
+ if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3224
+ mbedtls_zeroize_and_free(cert_cur->raw.p, cert_cur->raw.len);
3225
+ }
3226
+
3227
+ cert_prv = cert_cur;
3228
+ cert_cur = cert_cur->next;
3229
+
3230
+ mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3231
+ if (cert_prv != crt) {
3232
+ mbedtls_free(cert_prv);
3233
+ }
3234
+ }
3235
+ }
3236
+
3237
+ #if defined(MBEDTLS_ECP_RESTARTABLE)
3238
+ /*
3239
+ * Initialize a restart context
3240
+ */
3241
+ void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx)
3242
+ {
3243
+ mbedtls_pk_restart_init(&ctx->pk);
3244
+
3245
+ ctx->parent = NULL;
3246
+ ctx->fallback_parent = NULL;
3247
+ ctx->fallback_signature_is_good = 0;
3248
+
3249
+ ctx->parent_is_trusted = -1;
3250
+
3251
+ ctx->in_progress = x509_crt_rs_none;
3252
+ ctx->self_cnt = 0;
3253
+ x509_crt_verify_chain_reset(&ctx->ver_chain);
3254
+ }
3255
+
3256
+ /*
3257
+ * Free the components of a restart context
3258
+ */
3259
+ void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx)
3260
+ {
3261
+ if (ctx == NULL) {
3262
+ return;
3263
+ }
3264
+
3265
+ mbedtls_pk_restart_free(&ctx->pk);
3266
+ mbedtls_x509_crt_restart_init(ctx);
3267
+ }
3268
+ #endif /* MBEDTLS_ECP_RESTARTABLE */
3269
+
3270
+ int mbedtls_x509_crt_get_ca_istrue(const mbedtls_x509_crt *crt)
3271
+ {
3272
+ if ((crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) != 0) {
3273
+ return crt->MBEDTLS_PRIVATE(ca_istrue);
3274
+ }
3275
+ return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
3276
+ }
3277
+
3278
+ #endif /* MBEDTLS_X509_CRT_PARSE_C */