react-native-quick-crypto 0.7.3 → 1.0.0-beta.2

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 (309) hide show
  1. package/QuickCrypto.podspec +34 -0
  2. package/README.md +13 -46
  3. package/android/CMakeLists.txt +32 -64
  4. package/android/build.gradle +52 -125
  5. package/android/gradle.properties +4 -4
  6. package/android/src/main/cpp/cpp-adapter.cpp +9 -54
  7. package/android/src/main/java/com/margelo/nitro/quickcrypto/QuickCryptoPackage.java +38 -0
  8. package/cpp/random/HybridRandom.cpp +34 -0
  9. package/cpp/random/HybridRandom.hpp +44 -0
  10. package/cpp/utils/Utils.hpp +9 -0
  11. package/ios/QuickCryptoOnLoad.mm +19 -0
  12. package/lib/commonjs/index.js +39 -39
  13. package/lib/commonjs/index.js.map +1 -1
  14. package/lib/commonjs/random.js +26 -12
  15. package/lib/commonjs/random.js.map +1 -1
  16. package/lib/commonjs/{NativeQuickCrypto/rsa.js → specs/random.nitro.js} +1 -1
  17. package/lib/commonjs/{NativeQuickCrypto/aes.js.map → specs/random.nitro.js.map} +1 -1
  18. package/lib/commonjs/utils/conversion.js +22 -0
  19. package/lib/commonjs/utils/conversion.js.map +1 -0
  20. package/lib/commonjs/utils/index.js +12 -0
  21. package/lib/commonjs/utils/index.js.map +1 -0
  22. package/lib/commonjs/utils/types.js +2 -0
  23. package/lib/{module/NativeQuickCrypto/aes.js.map → commonjs/utils/types.js.map} +1 -1
  24. package/lib/module/index.js +38 -38
  25. package/lib/module/index.js.map +1 -1
  26. package/lib/module/random.js +26 -12
  27. package/lib/module/random.js.map +1 -1
  28. package/lib/module/specs/random.nitro.js +4 -0
  29. package/lib/module/{NativeQuickCrypto/rsa.js.map → specs/random.nitro.js.map} +1 -1
  30. package/lib/module/utils/conversion.js +16 -0
  31. package/lib/module/utils/conversion.js.map +1 -0
  32. package/lib/module/utils/index.js +8 -0
  33. package/lib/module/utils/index.js.map +1 -0
  34. package/lib/module/utils/types.js +2 -0
  35. package/lib/{commonjs/NativeQuickCrypto/rsa.js.map → module/utils/types.js.map} +1 -1
  36. package/lib/tsconfig.tsbuildinfo +1 -0
  37. package/lib/typescript/index.d.ts +10 -141
  38. package/lib/typescript/index.d.ts.map +1 -1
  39. package/lib/typescript/random.d.ts +4 -5
  40. package/lib/typescript/random.d.ts.map +1 -1
  41. package/lib/typescript/specs/random.nitro.d.ts +9 -0
  42. package/lib/typescript/specs/random.nitro.d.ts.map +1 -0
  43. package/lib/typescript/utils/conversion.d.ts +4 -0
  44. package/lib/typescript/utils/conversion.d.ts.map +1 -0
  45. package/lib/typescript/utils/index.d.ts +6 -0
  46. package/lib/typescript/utils/index.d.ts.map +1 -0
  47. package/lib/typescript/utils/types.d.ts +5 -0
  48. package/lib/typescript/utils/types.d.ts.map +1 -0
  49. package/nitrogen/generated/android/QuickCrypto+autolinking.cmake +45 -0
  50. package/nitrogen/generated/android/QuickCrypto+autolinking.gradle +26 -0
  51. package/nitrogen/generated/ios/QuickCrypto+autolinking.rb +57 -0
  52. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.cpp +9 -0
  53. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Bridge.hpp +25 -0
  54. package/nitrogen/generated/ios/QuickCrypto-Swift-Cxx-Umbrella.hpp +29 -0
  55. package/nitrogen/generated/shared/c++/HybridRandomSpec.cpp +23 -0
  56. package/nitrogen/generated/shared/c++/HybridRandomSpec.hpp +62 -0
  57. package/package.json +50 -106
  58. package/src/index.ts +38 -48
  59. package/src/random.ts +51 -48
  60. package/src/specs/random.nitro.ts +14 -0
  61. package/src/utils/conversion.ts +16 -0
  62. package/src/utils/index.ts +6 -0
  63. package/src/utils/types.ts +15 -0
  64. package/LICENSE +0 -27
  65. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +0 -70
  66. package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoPackage.java +0 -25
  67. package/cpp/Cipher/MGLCipherHostObject.cpp +0 -663
  68. package/cpp/Cipher/MGLCipherHostObject.h +0 -90
  69. package/cpp/Cipher/MGLCreateCipherInstaller.cpp +0 -75
  70. package/cpp/Cipher/MGLCreateCipherInstaller.h +0 -22
  71. package/cpp/Cipher/MGLCreateDecipherInstaller.cpp +0 -77
  72. package/cpp/Cipher/MGLCreateDecipherInstaller.h +0 -17
  73. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +0 -134
  74. package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +0 -35
  75. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +0 -63
  76. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +0 -37
  77. package/cpp/Cipher/MGLPublicCipher.h +0 -124
  78. package/cpp/Cipher/MGLPublicCipherInstaller.h +0 -107
  79. package/cpp/Cipher/MGLRsa.cpp +0 -573
  80. package/cpp/Cipher/MGLRsa.h +0 -109
  81. package/cpp/HMAC/MGLHmacHostObject.cpp +0 -95
  82. package/cpp/HMAC/MGLHmacHostObject.h +0 -39
  83. package/cpp/HMAC/MGLHmacInstaller.cpp +0 -43
  84. package/cpp/HMAC/MGLHmacInstaller.h +0 -20
  85. package/cpp/Hash/MGLHashHostObject.cpp +0 -147
  86. package/cpp/Hash/MGLHashHostObject.h +0 -53
  87. package/cpp/Hash/MGLHashInstaller.cpp +0 -44
  88. package/cpp/Hash/MGLHashInstaller.h +0 -20
  89. package/cpp/JSIUtils/MGLJSIMacros.h +0 -110
  90. package/cpp/JSIUtils/MGLJSIUtils.h +0 -41
  91. package/cpp/JSIUtils/MGLSmartHostObject.cpp +0 -43
  92. package/cpp/JSIUtils/MGLSmartHostObject.h +0 -46
  93. package/cpp/JSIUtils/MGLThreadAwareHostObject.cpp +0 -24
  94. package/cpp/JSIUtils/MGLThreadAwareHostObject.h +0 -43
  95. package/cpp/JSIUtils/MGLTypedArray.cpp +0 -325
  96. package/cpp/JSIUtils/MGLTypedArray.h +0 -160
  97. package/cpp/MGLKeys.cpp +0 -1426
  98. package/cpp/MGLKeys.h +0 -203
  99. package/cpp/MGLQuickCryptoHostObject.cpp +0 -127
  100. package/cpp/MGLQuickCryptoHostObject.h +0 -30
  101. package/cpp/Random/MGLRandomHostObject.cpp +0 -96
  102. package/cpp/Random/MGLRandomHostObject.h +0 -27
  103. package/cpp/Sig/MGLSignHostObjects.cpp +0 -757
  104. package/cpp/Sig/MGLSignHostObjects.h +0 -128
  105. package/cpp/Sig/MGLSignInstaller.cpp +0 -24
  106. package/cpp/Sig/MGLSignInstaller.h +0 -29
  107. package/cpp/Sig/MGLVerifyInstaller.cpp +0 -24
  108. package/cpp/Sig/MGLVerifyInstaller.h +0 -22
  109. package/cpp/Utils/MGLDispatchQueue.cpp +0 -75
  110. package/cpp/Utils/MGLDispatchQueue.h +0 -55
  111. package/cpp/Utils/MGLUtils.cpp +0 -295
  112. package/cpp/Utils/MGLUtils.h +0 -358
  113. package/cpp/Utils/base64.h +0 -320
  114. package/cpp/Utils/logs.h +0 -38
  115. package/cpp/Utils/node.h +0 -13
  116. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.cpp +0 -164
  117. package/cpp/fastpbkdf2/MGLPbkdf2HostObject.h +0 -29
  118. package/cpp/fastpbkdf2/fastpbkdf2.c +0 -352
  119. package/cpp/fastpbkdf2/fastpbkdf2.h +0 -68
  120. package/cpp/webcrypto/MGLWebCrypto.cpp +0 -125
  121. package/cpp/webcrypto/MGLWebCrypto.h +0 -32
  122. package/cpp/webcrypto/crypto_aes.cpp +0 -516
  123. package/cpp/webcrypto/crypto_aes.h +0 -79
  124. package/cpp/webcrypto/crypto_ec.cpp +0 -424
  125. package/cpp/webcrypto/crypto_ec.h +0 -78
  126. package/cpp/webcrypto/crypto_keygen.cpp +0 -86
  127. package/cpp/webcrypto/crypto_keygen.h +0 -38
  128. package/ios/QuickCrypto.xcodeproj/project.pbxproj +0 -274
  129. package/ios/QuickCryptoModule.h +0 -5
  130. package/ios/QuickCryptoModule.mm +0 -42
  131. package/lib/commonjs/Cipher.js +0 -502
  132. package/lib/commonjs/Cipher.js.map +0 -1
  133. package/lib/commonjs/Hash.js +0 -99
  134. package/lib/commonjs/Hash.js.map +0 -1
  135. package/lib/commonjs/Hashnames.js +0 -87
  136. package/lib/commonjs/Hashnames.js.map +0 -1
  137. package/lib/commonjs/Hmac.js +0 -79
  138. package/lib/commonjs/Hmac.js.map +0 -1
  139. package/lib/commonjs/NativeQuickCrypto/Cipher.js +0 -30
  140. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +0 -1
  141. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js +0 -52
  142. package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
  143. package/lib/commonjs/NativeQuickCrypto/aes.js +0 -6
  144. package/lib/commonjs/NativeQuickCrypto/hash.js +0 -2
  145. package/lib/commonjs/NativeQuickCrypto/hash.js.map +0 -1
  146. package/lib/commonjs/NativeQuickCrypto/hmac.js +0 -2
  147. package/lib/commonjs/NativeQuickCrypto/hmac.js.map +0 -1
  148. package/lib/commonjs/NativeQuickCrypto/keygen.js +0 -6
  149. package/lib/commonjs/NativeQuickCrypto/keygen.js.map +0 -1
  150. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js +0 -2
  151. package/lib/commonjs/NativeQuickCrypto/pbkdf2.js.map +0 -1
  152. package/lib/commonjs/NativeQuickCrypto/random.js +0 -2
  153. package/lib/commonjs/NativeQuickCrypto/random.js.map +0 -1
  154. package/lib/commonjs/NativeQuickCrypto/sig.js +0 -19
  155. package/lib/commonjs/NativeQuickCrypto/sig.js.map +0 -1
  156. package/lib/commonjs/NativeQuickCrypto/webcrypto.js +0 -6
  157. package/lib/commonjs/NativeQuickCrypto/webcrypto.js.map +0 -1
  158. package/lib/commonjs/Utils.js +0 -608
  159. package/lib/commonjs/Utils.js.map +0 -1
  160. package/lib/commonjs/aes.js +0 -281
  161. package/lib/commonjs/aes.js.map +0 -1
  162. package/lib/commonjs/constants.js +0 -85
  163. package/lib/commonjs/constants.js.map +0 -1
  164. package/lib/commonjs/ec.js +0 -276
  165. package/lib/commonjs/ec.js.map +0 -1
  166. package/lib/commonjs/keygen.js +0 -56
  167. package/lib/commonjs/keygen.js.map +0 -1
  168. package/lib/commonjs/keys.js +0 -492
  169. package/lib/commonjs/keys.js.map +0 -1
  170. package/lib/commonjs/pbkdf2.js +0 -90
  171. package/lib/commonjs/pbkdf2.js.map +0 -1
  172. package/lib/commonjs/rsa.js +0 -248
  173. package/lib/commonjs/rsa.js.map +0 -1
  174. package/lib/commonjs/sig.js +0 -129
  175. package/lib/commonjs/sig.js.map +0 -1
  176. package/lib/commonjs/subtle.js +0 -448
  177. package/lib/commonjs/subtle.js.map +0 -1
  178. package/lib/commonjs/webcrypto.js +0 -14
  179. package/lib/commonjs/webcrypto.js.map +0 -1
  180. package/lib/module/Cipher.js +0 -491
  181. package/lib/module/Cipher.js.map +0 -1
  182. package/lib/module/Hash.js +0 -93
  183. package/lib/module/Hash.js.map +0 -1
  184. package/lib/module/Hashnames.js +0 -85
  185. package/lib/module/Hashnames.js.map +0 -1
  186. package/lib/module/Hmac.js +0 -74
  187. package/lib/module/Hmac.js.map +0 -1
  188. package/lib/module/NativeQuickCrypto/Cipher.js +0 -26
  189. package/lib/module/NativeQuickCrypto/Cipher.js.map +0 -1
  190. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js +0 -49
  191. package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +0 -1
  192. package/lib/module/NativeQuickCrypto/aes.js +0 -4
  193. package/lib/module/NativeQuickCrypto/hash.js +0 -2
  194. package/lib/module/NativeQuickCrypto/hash.js.map +0 -1
  195. package/lib/module/NativeQuickCrypto/hmac.js +0 -2
  196. package/lib/module/NativeQuickCrypto/hmac.js.map +0 -1
  197. package/lib/module/NativeQuickCrypto/keygen.js +0 -4
  198. package/lib/module/NativeQuickCrypto/keygen.js.map +0 -1
  199. package/lib/module/NativeQuickCrypto/pbkdf2.js +0 -2
  200. package/lib/module/NativeQuickCrypto/pbkdf2.js.map +0 -1
  201. package/lib/module/NativeQuickCrypto/random.js +0 -2
  202. package/lib/module/NativeQuickCrypto/random.js.map +0 -1
  203. package/lib/module/NativeQuickCrypto/rsa.js +0 -4
  204. package/lib/module/NativeQuickCrypto/sig.js +0 -17
  205. package/lib/module/NativeQuickCrypto/sig.js.map +0 -1
  206. package/lib/module/NativeQuickCrypto/webcrypto.js +0 -4
  207. package/lib/module/NativeQuickCrypto/webcrypto.js.map +0 -1
  208. package/lib/module/Utils.js +0 -539
  209. package/lib/module/Utils.js.map +0 -1
  210. package/lib/module/aes.js +0 -274
  211. package/lib/module/aes.js.map +0 -1
  212. package/lib/module/constants.js +0 -81
  213. package/lib/module/constants.js.map +0 -1
  214. package/lib/module/ec.js +0 -267
  215. package/lib/module/ec.js.map +0 -1
  216. package/lib/module/keygen.js +0 -49
  217. package/lib/module/keygen.js.map +0 -1
  218. package/lib/module/keys.js +0 -477
  219. package/lib/module/keys.js.map +0 -1
  220. package/lib/module/pbkdf2.js +0 -84
  221. package/lib/module/pbkdf2.js.map +0 -1
  222. package/lib/module/rsa.js +0 -242
  223. package/lib/module/rsa.js.map +0 -1
  224. package/lib/module/sig.js +0 -124
  225. package/lib/module/sig.js.map +0 -1
  226. package/lib/module/subtle.js +0 -443
  227. package/lib/module/subtle.js.map +0 -1
  228. package/lib/module/webcrypto.js +0 -10
  229. package/lib/module/webcrypto.js.map +0 -1
  230. package/lib/typescript/Cipher.d.ts +0 -81
  231. package/lib/typescript/Cipher.d.ts.map +0 -1
  232. package/lib/typescript/Hash.d.ts +0 -44
  233. package/lib/typescript/Hash.d.ts.map +0 -1
  234. package/lib/typescript/Hashnames.d.ts +0 -11
  235. package/lib/typescript/Hashnames.d.ts.map +0 -1
  236. package/lib/typescript/Hmac.d.ts +0 -37
  237. package/lib/typescript/Hmac.d.ts.map +0 -1
  238. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +0 -44
  239. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +0 -1
  240. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +0 -33
  241. package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts.map +0 -1
  242. package/lib/typescript/NativeQuickCrypto/aes.d.ts +0 -5
  243. package/lib/typescript/NativeQuickCrypto/aes.d.ts.map +0 -1
  244. package/lib/typescript/NativeQuickCrypto/hash.d.ts +0 -7
  245. package/lib/typescript/NativeQuickCrypto/hash.d.ts.map +0 -1
  246. package/lib/typescript/NativeQuickCrypto/hmac.d.ts +0 -6
  247. package/lib/typescript/NativeQuickCrypto/hmac.d.ts.map +0 -1
  248. package/lib/typescript/NativeQuickCrypto/keygen.d.ts +0 -4
  249. package/lib/typescript/NativeQuickCrypto/keygen.d.ts.map +0 -1
  250. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts +0 -5
  251. package/lib/typescript/NativeQuickCrypto/pbkdf2.d.ts.map +0 -1
  252. package/lib/typescript/NativeQuickCrypto/random.d.ts +0 -5
  253. package/lib/typescript/NativeQuickCrypto/random.d.ts.map +0 -1
  254. package/lib/typescript/NativeQuickCrypto/rsa.d.ts +0 -5
  255. package/lib/typescript/NativeQuickCrypto/rsa.d.ts.map +0 -1
  256. package/lib/typescript/NativeQuickCrypto/sig.d.ts +0 -23
  257. package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +0 -1
  258. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +0 -39
  259. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +0 -1
  260. package/lib/typescript/Utils.d.ts +0 -48
  261. package/lib/typescript/Utils.d.ts.map +0 -1
  262. package/lib/typescript/aes.d.ts +0 -22
  263. package/lib/typescript/aes.d.ts.map +0 -1
  264. package/lib/typescript/constants.d.ts +0 -76
  265. package/lib/typescript/constants.d.ts.map +0 -1
  266. package/lib/typescript/ec.d.ts +0 -7
  267. package/lib/typescript/ec.d.ts.map +0 -1
  268. package/lib/typescript/keygen.d.ts +0 -6
  269. package/lib/typescript/keygen.d.ts.map +0 -1
  270. package/lib/typescript/keys.d.ts +0 -206
  271. package/lib/typescript/keys.d.ts.map +0 -1
  272. package/lib/typescript/pbkdf2.d.ts +0 -12
  273. package/lib/typescript/pbkdf2.d.ts.map +0 -1
  274. package/lib/typescript/rsa.d.ts +0 -12
  275. package/lib/typescript/rsa.d.ts.map +0 -1
  276. package/lib/typescript/sig.d.ts +0 -21
  277. package/lib/typescript/sig.d.ts.map +0 -1
  278. package/lib/typescript/subtle.d.ts +0 -15
  279. package/lib/typescript/subtle.d.ts.map +0 -1
  280. package/lib/typescript/webcrypto.d.ts +0 -9
  281. package/lib/typescript/webcrypto.d.ts.map +0 -1
  282. package/react-native-quick-crypto.podspec +0 -40
  283. package/src/Cipher.ts +0 -832
  284. package/src/Hash.ts +0 -132
  285. package/src/Hashnames.ts +0 -93
  286. package/src/Hmac.ts +0 -106
  287. package/src/NativeQuickCrypto/Cipher.ts +0 -102
  288. package/src/NativeQuickCrypto/NativeQuickCrypto.ts +0 -102
  289. package/src/NativeQuickCrypto/aes.ts +0 -14
  290. package/src/NativeQuickCrypto/hash.ts +0 -10
  291. package/src/NativeQuickCrypto/hmac.ts +0 -9
  292. package/src/NativeQuickCrypto/keygen.ts +0 -7
  293. package/src/NativeQuickCrypto/pbkdf2.ts +0 -16
  294. package/src/NativeQuickCrypto/random.ts +0 -12
  295. package/src/NativeQuickCrypto/rsa.ts +0 -12
  296. package/src/NativeQuickCrypto/sig.ts +0 -44
  297. package/src/NativeQuickCrypto/webcrypto.ts +0 -72
  298. package/src/Utils.ts +0 -777
  299. package/src/aes.ts +0 -402
  300. package/src/constants.ts +0 -79
  301. package/src/ec.ts +0 -375
  302. package/src/keygen.ts +0 -80
  303. package/src/keys.ts +0 -787
  304. package/src/pbkdf2.ts +0 -169
  305. package/src/rsa.ts +0 -370
  306. package/src/sig.ts +0 -164
  307. package/src/subtle.ts +0 -639
  308. package/src/webcrypto.ts +0 -8
  309. /package/android/src/main/{AndroidManifestNew.xml → AndroidManifest.xml} +0 -0
@@ -1,107 +0,0 @@
1
- //
2
- // MGLPrivateDecryptInstaller.h
3
- // react-native-quick-crypto
4
- //
5
- // Created by Oscar on 28.06.22.
6
- //
7
-
8
- #ifndef MGLPublicCipherInstaller_h
9
- #define MGLPublicCipherInstaller_h
10
-
11
- #include <jsi/jsi.h>
12
- #include <openssl/evp.h>
13
-
14
- #include <iostream>
15
- #include <memory>
16
- #include <optional>
17
- #include <string>
18
- #include <utility>
19
- #include <vector>
20
-
21
- #include "MGLKeys.h"
22
- #include "MGLPublicCipher.h"
23
-
24
- #ifdef ANDROID
25
- #include "JSIUtils/MGLJSIUtils.h"
26
- #include "JSIUtils/MGLSmartHostObject.h"
27
- #include "JSIUtils/MGLTypedArray.h"
28
- #else
29
- #include "MGLJSIUtils.h"
30
- #include "MGLSmartHostObject.h"
31
- #include "MGLTypedArray.h"
32
- #endif
33
-
34
- namespace margelo {
35
- namespace jsi = facebook::jsi;
36
-
37
- // "publicEncrypt", "publicDecrypt", "privateEncrypt", "privateDecrypt" all use
38
- // the same key extraction logic, only vary in the final openSSL call, so this
39
- // is a template that accepts and incoming template function, think of it as a
40
- // weird lambda before real lambdas Because this is a template, the
41
- // implementation needs to be in this header to prevent linker failure
42
- template <MGLPublicCipher::Operation operation,
43
- MGLPublicCipher::EVP_PKEY_cipher_init_t EVP_PKEY_cipher_init,
44
- MGLPublicCipher::EVP_PKEY_cipher_t EVP_PKEY_cipher>
45
- FieldDefinition getPublicCipherFieldDefinition(
46
- std::string name, std::shared_ptr<react::CallInvoker> jsCallInvoker,
47
- std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
48
- return buildPair(
49
- name, JSIF([=]) {
50
- // there is a variable amount of parameters passed depending on the
51
- // scheme therefore making param validation on this level makes little
52
- // sense everything should be done on JS, which makes this a bit unsafe
53
- // but it's acceptable
54
- unsigned int offset = 0;
55
-
56
- ManagedEVPPKey pkey = ManagedEVPPKey::GetPublicOrPrivateKeyFromJs(
57
- runtime, arguments, &offset);
58
-
59
- if (!pkey) {
60
- throw jsi::JSError(runtime, "Could not generate key");
61
- }
62
-
63
- auto buf = arguments[offset].asObject(runtime).getArrayBuffer(runtime);
64
- if (!CheckSizeInt32(runtime, buf)) {
65
- throw jsi::JSError(runtime, "Data buffer is too long");
66
- }
67
-
68
- uint32_t padding =
69
- static_cast<uint32_t>(arguments[offset + 1].getNumber());
70
- if (!padding) {
71
- throw jsi::JSError(runtime, "Invalid padding");
72
- }
73
-
74
- const EVP_MD* digest = nullptr;
75
- if (arguments[offset + 2].isString()) {
76
- auto oaep_str =
77
- arguments[offset + 2].getString(runtime).utf8(runtime);
78
-
79
- digest = EVP_get_digestbyname(oaep_str.c_str());
80
- if (digest == nullptr) {
81
- throw jsi::JSError(runtime, "Invalid digest (oaep_str)");
82
- }
83
- }
84
-
85
- if (!arguments[offset + 3].isUndefined()) {
86
- auto oaep_label_buffer =
87
- arguments[offset + 3].getObject(runtime).getArrayBuffer(runtime);
88
- if (!CheckSizeInt32(runtime, oaep_label_buffer)) {
89
- throw jsi::JSError(runtime, "oaep_label buffer is too long");
90
- }
91
- }
92
-
93
- std::optional<jsi::Value> out =
94
- MGLPublicCipher::Cipher<operation, EVP_PKEY_cipher_init,
95
- EVP_PKEY_cipher>(
96
- runtime, pkey, padding, digest, arguments[offset + 3], buf);
97
-
98
- if (!out.has_value()) {
99
- throw jsi::JSError(runtime, "Failed Cipher Operation - " + name);
100
- }
101
-
102
- return out.value().getObject(runtime);
103
- });
104
- }
105
- } // namespace margelo
106
-
107
- #endif /* MGLPublicCipherInstaller_h */
@@ -1,573 +0,0 @@
1
- //
2
- // MGLRsa.cpp
3
- // react-native-quick-crypto
4
- //
5
- // Created by Oscar on 22.06.22.
6
- //
7
-
8
- #include "MGLRsa.h"
9
- #ifdef ANDROID
10
- #include "Cipher/MGLPublicCipher.h"
11
- #include "JSIUtils/MGLJSIMacros.h"
12
- #include "JSIUtils/MGLJSIUtils.h"
13
- #include "Utils/MGLUtils.h"
14
- #else
15
- #include "MGLPublicCipher.h"
16
- #include "MGLJSIMacros.h"
17
- #include "MGLJSIUtils.h"
18
- #include "MGLUtils.h"
19
- #endif
20
-
21
- #include <string>
22
- #include <utility>
23
-
24
- namespace margelo {
25
-
26
- namespace jsi = facebook::jsi;
27
-
28
- EVPKeyCtxPointer setup(std::shared_ptr<RsaKeyPairGenConfig> config) {
29
- EVPKeyCtxPointer ctx(EVP_PKEY_CTX_new_id(
30
- config->variant == kvRSA_PSS ? EVP_PKEY_RSA_PSS : EVP_PKEY_RSA,
31
- nullptr));
32
-
33
- if (EVP_PKEY_keygen_init(ctx.get()) <= 0) return EVPKeyCtxPointer();
34
-
35
- if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx.get(), config->modulus_bits) <= 0) {
36
- return EVPKeyCtxPointer();
37
- }
38
-
39
- // 0x10001 is the default RSA exponent.
40
- if (config->exponent != 0x10001) {
41
- BignumPointer bn(BN_new());
42
- // CHECK_NOT_NULL(bn.get());
43
- BN_set_word(bn.get(), config->exponent);
44
- // EVP_CTX accepts ownership of bn on success.
45
- if (EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx.get(), bn.get()) <= 0) {
46
- return EVPKeyCtxPointer();
47
- }
48
-
49
- bn.release();
50
- }
51
-
52
- if (config->variant == kvRSA_PSS) {
53
- if (config->md != nullptr &&
54
- EVP_PKEY_CTX_set_rsa_pss_keygen_md(ctx.get(), config->md) <= 0) {
55
- return EVPKeyCtxPointer();
56
- }
57
-
58
- // TODO(tniessen): This appears to only be necessary in OpenSSL 3, while
59
- // OpenSSL 1.1.1 behaves as recommended by RFC 8017 and defaults the MGF1
60
- // hash algorithm to the RSA-PSS hashAlgorithm. Remove this code if the
61
- // behavior of OpenSSL 3 changes.
62
- const EVP_MD* mgf1_md = config->mgf1_md;
63
- if (mgf1_md == nullptr && config->md != nullptr) {
64
- mgf1_md = config->md;
65
- }
66
-
67
- if (mgf1_md != nullptr &&
68
- EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(ctx.get(), mgf1_md) <= 0) {
69
- return EVPKeyCtxPointer();
70
- }
71
-
72
- int saltlen = config->saltlen;
73
- if (saltlen < 0 && config->md != nullptr) {
74
- saltlen = EVP_MD_size(config->md);
75
- }
76
-
77
- if (saltlen >= 0 &&
78
- EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(ctx.get(), saltlen) <= 0) {
79
- return EVPKeyCtxPointer();
80
- }
81
- }
82
-
83
- return ctx;
84
- }
85
-
86
- RsaKeyPairGenConfig prepareRsaKeyGenConfig(jsi::Runtime& runtime,
87
- const jsi::Value* arguments) {
88
- RsaKeyPairGenConfig config = RsaKeyPairGenConfig();
89
-
90
- // This is a funky one: depending on which encryption scheme you are
91
- // using, there is a variable number of arguments that will need to be
92
- // parsed, therefore this pointer will be used by the internal functions
93
- // as they go reading the arguments based on the selected scheme. I
94
- // tried to keep as close to the node implementation to make future
95
- // debugging easier
96
- unsigned int offset = 0;
97
-
98
- // TODO(osp)
99
- // CHECK(args[*offset]->IsUint32()); // Variant
100
- // CHECK(args[*offset + 1]->IsUint32()); // Modulus bits
101
- // CHECK(args[*offset + 2]->IsUint32()); // Exponent
102
- config.variant =
103
- static_cast<KeyVariant>((int)arguments[offset].asNumber());
104
-
105
- // TODO(osp)
106
- // CHECK_IMPLIES(params->params.variant != kvRSA_PSS,
107
- // args.Length() == 10);
108
- // CHECK_IMPLIES(params->params.variant == kvRSA_PSS,
109
- // args.Length() == 13);
110
- config.modulus_bits =
111
- static_cast<unsigned int>(arguments[offset + 1].asNumber());
112
- config.exponent = static_cast<unsigned int>(arguments[offset + 2].asNumber());
113
-
114
- offset += 3;
115
-
116
- if (config.variant == kvRSA_PSS) {
117
- if (!arguments[offset].isUndefined()) {
118
- // TODO(osp) CHECK(string)
119
- config.md = EVP_get_digestbyname(
120
- arguments[offset].asString(runtime).utf8(runtime).c_str());
121
-
122
- if (config.md == nullptr) {
123
- throw jsi::JSError(runtime, "invalid digest");
124
- }
125
- }
126
-
127
- if (!arguments[offset + 1].isUndefined()) {
128
- // TODO(osp) CHECK(string)
129
- config.mgf1_md = EVP_get_digestbyname(
130
- arguments[offset + 1].asString(runtime).utf8(runtime).c_str());
131
-
132
- if (config.mgf1_md == nullptr) {
133
- throw jsi::JSError(runtime, "invalid digest");
134
- }
135
- }
136
-
137
- if (!arguments[offset + 2].isUndefined()) {
138
- // CHECK(args[*offset + 2]->IsInt32());
139
- config.saltlen = static_cast<int>(arguments[offset + 2].asNumber());
140
-
141
- if (config.saltlen < 0) {
142
- throw jsi::JSError(runtime, "salt length is out of range");
143
- }
144
- }
145
-
146
- offset += 3;
147
- }
148
-
149
- config.public_key_encoding = ManagedEVPPKey::GetPublicKeyEncodingFromJs(
150
- runtime, arguments, &offset, kKeyContextGenerate);
151
-
152
- auto private_key_encoding = ManagedEVPPKey::GetPrivateKeyEncodingFromJs(
153
- runtime, arguments, &offset, kKeyContextGenerate);
154
-
155
- if (!private_key_encoding.IsEmpty()) {
156
- config.private_key_encoding = private_key_encoding.Release();
157
- }
158
-
159
- return config;
160
- }
161
-
162
- std::pair<jsi::Value, jsi::Value> generateRsaKeyPair(
163
- jsi::Runtime& runtime, std::shared_ptr<RsaKeyPairGenConfig> config) {
164
- // TODO: this is all copied into crypto_ec.cpp - template it up like Node?
165
- CheckEntropy();
166
-
167
- EVPKeyCtxPointer ctx = setup(config);
168
-
169
- if (!ctx) {
170
- throw jsi::JSError(runtime, "Error on key generation job");
171
- }
172
-
173
- // Generate the key
174
- EVP_PKEY* pkey = nullptr;
175
- if (!EVP_PKEY_keygen(ctx.get(), &pkey)) {
176
- throw jsi::JSError(runtime, "Error generating key");
177
- }
178
-
179
- config->key = ManagedEVPPKey(EVPKeyPointer(pkey));
180
-
181
- jsi::Value publicBuffer =
182
- ManagedEVPPKey::ToEncodedPublicKey(runtime, std::move(config->key),
183
- config->public_key_encoding);
184
- jsi::Value privateBuffer =
185
- ManagedEVPPKey::ToEncodedPrivateKey(runtime, std::move(config->key),
186
- config->private_key_encoding);
187
-
188
- if (publicBuffer.isUndefined() || privateBuffer.isUndefined()) {
189
- throw jsi::JSError(runtime, "Failed to encode public and/or private key (RSA)");
190
- }
191
-
192
- return {std::move(publicBuffer), std::move(privateBuffer)};
193
- }
194
-
195
- template <MGLPublicCipher::EVP_PKEY_cipher_init_t init,
196
- MGLPublicCipher::EVP_PKEY_cipher_t cipher>
197
- WebCryptoCipherStatus RSA_Cipher(const RSACipherConfig& params, ByteSource* out) {
198
- CHECK_NE(params.key->GetKeyType(), kKeyTypeSecret);
199
- ManagedEVPPKey m_pkey = params.key->GetAsymmetricKey();
200
- // Mutex::ScopedLock lock(*m_pkey.mutex());
201
-
202
- EVPKeyCtxPointer ctx(EVP_PKEY_CTX_new(m_pkey.get(), nullptr));
203
-
204
- if (!ctx || init(ctx.get()) <= 0)
205
- return WebCryptoCipherStatus::FAILED;
206
-
207
- if (EVP_PKEY_CTX_set_rsa_padding(ctx.get(), params.padding) <= 0) {
208
- return WebCryptoCipherStatus::FAILED;
209
- }
210
-
211
- if (params.digest != nullptr &&
212
- (EVP_PKEY_CTX_set_rsa_oaep_md(ctx.get(), params.digest) <= 0 ||
213
- EVP_PKEY_CTX_set_rsa_mgf1_md(ctx.get(), params.digest) <= 0)) {
214
- return WebCryptoCipherStatus::FAILED;
215
- }
216
-
217
- if (!SetRsaOaepLabel(ctx, params.label)) return WebCryptoCipherStatus::FAILED;
218
-
219
- size_t out_len = 0;
220
- if (cipher(
221
- ctx.get(),
222
- nullptr,
223
- &out_len,
224
- params.data.data<unsigned char>(),
225
- params.data.size()) <= 0) {
226
- return WebCryptoCipherStatus::FAILED;
227
- }
228
-
229
- ByteSource::Builder buf(out_len);
230
-
231
- if (cipher(ctx.get(),
232
- buf.data<unsigned char>(),
233
- &out_len,
234
- params.data.data<unsigned char>(),
235
- params.data.size()) <= 0) {
236
- return WebCryptoCipherStatus::FAILED;
237
- }
238
-
239
- *out = std::move(buf).release(out_len);
240
- return WebCryptoCipherStatus::OK;
241
- }
242
-
243
- RSACipherConfig RSACipher::GetParamsFromJS(jsi::Runtime &rt,
244
- const jsi::Value *args) {
245
- RSACipherConfig params;
246
- unsigned int offset = 0;
247
-
248
- // padding
249
- params.padding = RSA_PKCS1_OAEP_PADDING;
250
-
251
- // mode (encrypt/decrypt)
252
- params.mode = static_cast<WebCryptoCipherMode>((int)args[offset].getNumber());
253
- offset++;
254
-
255
- // key (handle)
256
- if (!args[offset].isObject()) {
257
- throw std::runtime_error("arg is not a KeyObjectHandle: key");
258
- }
259
- std::shared_ptr<KeyObjectHandle> handle =
260
- std::static_pointer_cast<KeyObjectHandle>(
261
- args[offset].asObject(rt).getHostObject(rt));
262
- params.key = handle->Data();
263
- offset++;
264
-
265
- // data
266
- params.data = GetByteSourceFromJS(rt, args[offset], "data");
267
- offset++;
268
-
269
- // variant
270
- if (CheckIsInt32(args[offset])) {
271
- params.variant = static_cast<RSAKeyVariant>((int)args[offset].getNumber());
272
- }
273
- // offset++; // The below variant-dependent params advance offset themselves
274
-
275
- std::string digest;
276
- switch (params.variant) {
277
- case kKeyVariantRSA_OAEP:
278
- // hash (digest)
279
- CHECK(args[offset + 1].isString());
280
- digest = args[offset + 1].asString(rt).utf8(rt);
281
- params.digest = EVP_get_digestbyname(digest.c_str());
282
- if (params.digest == nullptr) {
283
- throw jsi::JSError(rt, "invalid digest: " + digest);
284
- return params;
285
- }
286
-
287
- // label
288
- if (args[offset + 2].isUndefined()) {
289
- params.label = ByteSource();
290
- } else {
291
- params.label = GetByteSourceFromJS(rt, args[offset + 2], "label");
292
- }
293
-
294
- break;
295
- default:
296
- throw jsi::JSError(rt, "Invalid RSA key variant");
297
- }
298
-
299
- return params;
300
- }
301
-
302
- WebCryptoCipherStatus RSACipher::DoCipher(const RSACipherConfig &params,
303
- ByteSource *out) {
304
- switch (params.mode) {
305
- case kEncrypt:
306
- CHECK_EQ(params.key->GetKeyType(), kKeyTypePublic);
307
- return RSA_Cipher<EVP_PKEY_encrypt_init, EVP_PKEY_encrypt>(params, out);
308
- case kDecrypt:
309
- CHECK_EQ(params.key->GetKeyType(), kKeyTypePrivate);
310
- return RSA_Cipher<EVP_PKEY_decrypt_init, EVP_PKEY_decrypt>(params, out);
311
- }
312
- }
313
-
314
- jsi::Value ExportJWKRsaKey(jsi::Runtime &rt,
315
- std::shared_ptr<KeyObjectData> key,
316
- jsi::Object &target) {
317
- ManagedEVPPKey m_pkey = key->GetAsymmetricKey();
318
- // std::scoped_lock lock(*m_pkey.mutex()); // TODO: mutex/lock required?
319
- int type = EVP_PKEY_id(m_pkey.get());
320
- CHECK(type == EVP_PKEY_RSA || type == EVP_PKEY_RSA_PSS);
321
-
322
- // TODO(tniessen): Remove the "else" branch once we drop support for OpenSSL
323
- // versions older than 1.1.1e via FIPS / dynamic linking.
324
- const RSA* rsa;
325
- if (OpenSSL_version_num() >= 0x1010105fL) {
326
- rsa = EVP_PKEY_get0_RSA(m_pkey.get());
327
- } else {
328
- rsa = static_cast<const RSA*>(EVP_PKEY_get0(m_pkey.get()));
329
- }
330
- CHECK_NOT_NULL(rsa);
331
-
332
- const BIGNUM* n;
333
- const BIGNUM* e;
334
- const BIGNUM* d;
335
- const BIGNUM* p;
336
- const BIGNUM* q;
337
- const BIGNUM* dp;
338
- const BIGNUM* dq;
339
- const BIGNUM* qi;
340
- RSA_get0_key(rsa, &n, &e, &d);
341
-
342
- target.setProperty(rt, "kty", "RSA");
343
- target.setProperty(rt, "n", EncodeBignum(n, 0, true));
344
- target.setProperty(rt, "e", EncodeBignum(e, 0, true));
345
-
346
- if (key->GetKeyType() == kKeyTypePrivate) {
347
- RSA_get0_factors(rsa, &p, &q);
348
- RSA_get0_crt_params(rsa, &dp, &dq, &qi);
349
- target.setProperty(rt, "d", EncodeBignum(d, 0, true));
350
- target.setProperty(rt, "p", EncodeBignum(p, 0, true));
351
- target.setProperty(rt, "q", EncodeBignum(q, 0, true));
352
- target.setProperty(rt, "dp", EncodeBignum(dp, 0, true));
353
- target.setProperty(rt, "dq", EncodeBignum(dq, 0, true));
354
- target.setProperty(rt, "qi", EncodeBignum(qi, 0, true));
355
- }
356
-
357
- return std::move(target);
358
- }
359
-
360
- std::shared_ptr<KeyObjectData> ImportJWKRsaKey(jsi::Runtime &rt,
361
- jsi::Object &jwk) {
362
- jsi::Value n_value = jwk.getProperty(rt, "n");
363
- jsi::Value e_value = jwk.getProperty(rt, "e");
364
- jsi::Value d_value = jwk.getProperty(rt, "d");
365
-
366
- if (!n_value.isString() ||
367
- !e_value.isString()) {
368
- throw jsi::JSError(rt, "Invalid JWK RSA key");
369
- return std::shared_ptr<KeyObjectData>();
370
- }
371
-
372
- if (!d_value.isUndefined() && !d_value.isString()) {
373
- throw jsi::JSError(rt, "Invalid JWK RSA key");
374
- return std::shared_ptr<KeyObjectData>();
375
- }
376
-
377
- KeyType type = d_value.isString() ? kKeyTypePrivate : kKeyTypePublic;
378
-
379
- RsaPointer rsa(RSA_new());
380
-
381
- ByteSource n = ByteSource::FromEncodedString(rt, n_value.asString(rt).utf8(rt));
382
- ByteSource e = ByteSource::FromEncodedString(rt, e_value.asString(rt).utf8(rt));
383
-
384
- if (!RSA_set0_key(
385
- rsa.get(),
386
- n.ToBN().release(),
387
- e.ToBN().release(),
388
- nullptr)) {
389
- throw jsi::JSError(rt, "Invalid JWK RSA key");
390
- return std::shared_ptr<KeyObjectData>();
391
- }
392
-
393
- if (type == kKeyTypePrivate) {
394
- jsi::Value p_value = jwk.getProperty(rt, "p");
395
- jsi::Value q_value = jwk.getProperty(rt, "q");
396
- jsi::Value dp_value = jwk.getProperty(rt, "dp");
397
- jsi::Value dq_value = jwk.getProperty(rt, "dq");
398
- jsi::Value qi_value = jwk.getProperty(rt, "qi");
399
-
400
- if (!p_value.isString() ||
401
- !q_value.isString() ||
402
- !dp_value.isString() ||
403
- !dq_value.isString() ||
404
- !qi_value.isString()) {
405
- throw jsi::JSError(rt, "Invalid JWK RSA key");
406
- return std::shared_ptr<KeyObjectData>();
407
- }
408
-
409
- ByteSource d = ByteSource::FromEncodedString(rt, d_value.asString(rt).utf8(rt));
410
- ByteSource q = ByteSource::FromEncodedString(rt, q_value.asString(rt).utf8(rt));
411
- ByteSource p = ByteSource::FromEncodedString(rt, p_value.asString(rt).utf8(rt));
412
- ByteSource dp = ByteSource::FromEncodedString(rt, dp_value.asString(rt).utf8(rt));
413
- ByteSource dq = ByteSource::FromEncodedString(rt, dq_value.asString(rt).utf8(rt));
414
- ByteSource qi = ByteSource::FromEncodedString(rt, qi_value.asString(rt).utf8(rt));
415
-
416
- if (!RSA_set0_key(rsa.get(), nullptr, nullptr, d.ToBN().release()) ||
417
- !RSA_set0_factors(rsa.get(), p.ToBN().release(), q.ToBN().release()) ||
418
- !RSA_set0_crt_params(
419
- rsa.get(),
420
- dp.ToBN().release(),
421
- dq.ToBN().release(),
422
- qi.ToBN().release())) {
423
- throw jsi::JSError(rt, "Invalid JWK RSA key");
424
- return std::shared_ptr<KeyObjectData>();
425
- }
426
- }
427
-
428
- EVPKeyPointer pkey(EVP_PKEY_new());
429
- CHECK_EQ(EVP_PKEY_set1_RSA(pkey.get(), rsa.get()), 1);
430
-
431
- return KeyObjectData::CreateAsymmetric(type, ManagedEVPPKey(std::move(pkey)));
432
- }
433
-
434
- jsi::Value GetRsaKeyDetail(jsi::Runtime &rt,
435
- std::shared_ptr<KeyObjectData> key) {
436
- jsi::Object target = jsi::Object(rt);
437
- const BIGNUM* e; // Public Exponent
438
- const BIGNUM* n; // Modulus
439
-
440
- ManagedEVPPKey m_pkey = key->GetAsymmetricKey();
441
- // std::scoped_lock lock(*m_pkey.mutex()); // TODO: mutex/lock required?
442
- int type = EVP_PKEY_id(m_pkey.get());
443
- CHECK(type == EVP_PKEY_RSA || type == EVP_PKEY_RSA_PSS);
444
-
445
- // TODO(tniessen): Remove the "else" branch once we drop support for OpenSSL
446
- // versions older than 1.1.1e via FIPS / dynamic linking.
447
- const RSA* rsa;
448
- if (OpenSSL_version_num() >= 0x1010105fL) {
449
- rsa = EVP_PKEY_get0_RSA(m_pkey.get());
450
- } else {
451
- rsa = static_cast<const RSA*>(EVP_PKEY_get0(m_pkey.get()));
452
- }
453
- CHECK_NOT_NULL(rsa);
454
-
455
- RSA_get0_key(rsa, &n, &e, nullptr);
456
-
457
- size_t modulus_length = BN_num_bits(n);
458
- target.setProperty(rt, "modulusLength", static_cast<double>(modulus_length));
459
-
460
- size_t exp_size = BN_num_bytes(e);
461
- ByteSource public_exponent = ByteSource::FromBN(e, exp_size);
462
- target.setProperty(rt, "publicExponent", toJSI(rt, std::move(public_exponent)));
463
-
464
- if (type == EVP_PKEY_RSA_PSS) {
465
- // Due to the way ASN.1 encoding works, default values are omitted when
466
- // encoding the data structure. However, there are also RSA-PSS keys for
467
- // which no parameters are set. In that case, the ASN.1 RSASSA-PSS-params
468
- // sequence will be missing entirely and RSA_get0_pss_params will return
469
- // nullptr. If parameters are present but all parameters are set to their
470
- // default values, an empty sequence will be stored in the ASN.1 structure.
471
- // In that case, RSA_get0_pss_params does not return nullptr but all fields
472
- // of the returned RSA_PSS_PARAMS will be set to nullptr.
473
-
474
- const RSA_PSS_PARAMS* params = RSA_get0_pss_params(rsa);
475
- if (params != nullptr) {
476
- int hash_nid = NID_sha1;
477
- int mgf_nid = NID_mgf1;
478
- int mgf1_hash_nid = NID_sha1;
479
- int64_t salt_length = 20;
480
-
481
- if (params->hashAlgorithm != nullptr) {
482
- const ASN1_OBJECT* hash_obj;
483
- X509_ALGOR_get0(&hash_obj, nullptr, nullptr, params->hashAlgorithm);
484
- hash_nid = OBJ_obj2nid(hash_obj);
485
- }
486
-
487
- target.setProperty(rt, "hashAlgorithm", std::string(OBJ_nid2ln(hash_nid)));
488
-
489
- if (params->maskGenAlgorithm != nullptr) {
490
- const ASN1_OBJECT* mgf_obj;
491
- X509_ALGOR_get0(&mgf_obj, nullptr, nullptr, params->maskGenAlgorithm);
492
- mgf_nid = OBJ_obj2nid(mgf_obj);
493
- if (mgf_nid == NID_mgf1) {
494
- const ASN1_OBJECT* mgf1_hash_obj;
495
- X509_ALGOR_get0(&mgf1_hash_obj, nullptr, nullptr, params->maskHash);
496
- mgf1_hash_nid = OBJ_obj2nid(mgf1_hash_obj);
497
- }
498
- }
499
-
500
- // If, for some reason, the MGF is not MGF1, then the MGF1 hash function
501
- // is intentionally not added to the object.
502
- if (mgf_nid == NID_mgf1) {
503
- target.setProperty(rt, "mgf1HashAlgorithm", std::string(OBJ_nid2ln(mgf1_hash_nid)));
504
- }
505
-
506
- if (params->saltLength != nullptr) {
507
- if (ASN1_INTEGER_get_int64(&salt_length, params->saltLength) != 1) {
508
- throw jsi::JSError(rt, "ASN1_INTEGER_get_in64 error: " +
509
- std::to_string(ERR_get_error()));
510
- return target;
511
- }
512
- }
513
-
514
- target.setProperty(rt, "saltLength", static_cast<double>(salt_length));
515
- }
516
- }
517
-
518
- return target;
519
- }
520
-
521
- bool RsaKeyExport::GetParamsFromJS(jsi::Runtime &rt, const jsi::Value *args) {
522
- RsaKeyExportConfig params;
523
- unsigned int offset = 0;
524
-
525
- // format
526
- params.format = static_cast<WebCryptoKeyFormat>((int)args[offset].getNumber());
527
- offset++;
528
-
529
- // key
530
- std::shared_ptr<KeyObjectHandle> handle =
531
- std::static_pointer_cast<KeyObjectHandle>(
532
- args[1].asObject(rt).getHostObject(rt));
533
- params.key_ = handle->Data();
534
- offset++;
535
-
536
- // variant
537
- params.variant = static_cast<KeyVariant>((int)args[offset].getNumber());
538
- offset++;
539
-
540
- this->params_ = std::move(params);
541
- return true;
542
- }
543
-
544
- WebCryptoKeyExportStatus RsaKeyExport::DoExport(ByteSource* out) {
545
- auto key_data = this->params_.key_;
546
- CHECK_NE(key_data->GetKeyType(), kKeyTypeSecret);
547
-
548
- switch (this->params_.format) {
549
- case kWebCryptoKeyFormatRaw:
550
- throw std::runtime_error("Raw format not supported for RSA keys");
551
- return WebCryptoKeyExportStatus::FAILED;
552
- case kWebCryptoKeyFormatJWK:
553
- throw std::runtime_error("JWK format not handled in C++ for RSA keys");
554
- return WebCryptoKeyExportStatus::FAILED;
555
- case kWebCryptoKeyFormatPKCS8:
556
- if (key_data->GetKeyType() != kKeyTypePrivate) {
557
- throw std::runtime_error("Invalid key type for PKCS8 export");
558
- return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
559
- }
560
- return PKEY_PKCS8_Export(key_data.get(), out);
561
- case kWebCryptoKeyFormatSPKI:
562
- if (key_data->GetKeyType() != kKeyTypePublic) {
563
- throw std::runtime_error("Invalid key type for SPKI export");
564
- return WebCryptoKeyExportStatus::INVALID_KEY_TYPE;
565
- }
566
- return PKEY_SPKI_Export(key_data.get(), out);
567
- default:
568
- throw std::runtime_error("Unrecognized format for RSA key export");
569
- return WebCryptoKeyExportStatus::FAILED;
570
- }
571
- }
572
-
573
- } // namespace margelo