react-native-quick-crypto 0.7.0-rc.9 → 0.7.0

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 (76) hide show
  1. package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +51 -14
  2. package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +25 -9
  3. package/cpp/Cipher/MGLRsa.cpp +13 -12
  4. package/cpp/Cipher/MGLRsa.h +2 -8
  5. package/cpp/JSIUtils/MGLJSIUtils.h +9 -0
  6. package/cpp/MGLKeys.cpp +174 -149
  7. package/cpp/MGLKeys.h +18 -13
  8. package/cpp/Sig/MGLSignHostObjects.cpp +284 -421
  9. package/cpp/Sig/MGLSignHostObjects.h +40 -0
  10. package/cpp/Utils/MGLUtils.cpp +0 -41
  11. package/cpp/Utils/MGLUtils.h +27 -6
  12. package/cpp/webcrypto/MGLWebCrypto.cpp +14 -4
  13. package/cpp/webcrypto/crypto_ec.cpp +106 -0
  14. package/cpp/webcrypto/crypto_ec.h +18 -0
  15. package/lib/commonjs/Cipher.js +138 -95
  16. package/lib/commonjs/Cipher.js.map +1 -1
  17. package/lib/commonjs/NativeQuickCrypto/Cipher.js +11 -8
  18. package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
  19. package/lib/commonjs/NativeQuickCrypto/sig.js +17 -0
  20. package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
  21. package/lib/commonjs/Utils.js +15 -1
  22. package/lib/commonjs/Utils.js.map +1 -1
  23. package/lib/commonjs/ec.js +79 -91
  24. package/lib/commonjs/ec.js.map +1 -1
  25. package/lib/commonjs/keys.js +10 -24
  26. package/lib/commonjs/keys.js.map +1 -1
  27. package/lib/commonjs/random.js +6 -0
  28. package/lib/commonjs/random.js.map +1 -1
  29. package/lib/commonjs/subtle.js +114 -0
  30. package/lib/commonjs/subtle.js.map +1 -1
  31. package/lib/module/Cipher.js +136 -93
  32. package/lib/module/Cipher.js.map +1 -1
  33. package/lib/module/NativeQuickCrypto/Cipher.js +10 -7
  34. package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
  35. package/lib/module/NativeQuickCrypto/sig.js +13 -0
  36. package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
  37. package/lib/module/Utils.js +12 -0
  38. package/lib/module/Utils.js.map +1 -1
  39. package/lib/module/ec.js +76 -93
  40. package/lib/module/ec.js.map +1 -1
  41. package/lib/module/keys.js +8 -24
  42. package/lib/module/keys.js.map +1 -1
  43. package/lib/module/random.js +6 -0
  44. package/lib/module/random.js.map +1 -1
  45. package/lib/module/subtle.js +115 -1
  46. package/lib/module/subtle.js.map +1 -1
  47. package/lib/typescript/Cipher.d.ts +23 -13
  48. package/lib/typescript/Cipher.d.ts.map +1 -1
  49. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +11 -6
  50. package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +1 -1
  51. package/lib/typescript/NativeQuickCrypto/sig.d.ts +10 -0
  52. package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +1 -1
  53. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +2 -0
  54. package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +1 -1
  55. package/lib/typescript/Utils.d.ts +1 -0
  56. package/lib/typescript/Utils.d.ts.map +1 -1
  57. package/lib/typescript/ec.d.ts +3 -1
  58. package/lib/typescript/ec.d.ts.map +1 -1
  59. package/lib/typescript/index.d.ts +11 -8
  60. package/lib/typescript/index.d.ts.map +1 -1
  61. package/lib/typescript/keys.d.ts +12 -1
  62. package/lib/typescript/keys.d.ts.map +1 -1
  63. package/lib/typescript/random.d.ts +2 -1
  64. package/lib/typescript/random.d.ts.map +1 -1
  65. package/lib/typescript/subtle.d.ts +4 -1
  66. package/lib/typescript/subtle.d.ts.map +1 -1
  67. package/package.json +1 -1
  68. package/src/Cipher.ts +139 -75
  69. package/src/NativeQuickCrypto/Cipher.ts +14 -14
  70. package/src/NativeQuickCrypto/sig.ts +27 -0
  71. package/src/NativeQuickCrypto/webcrypto.ts +2 -0
  72. package/src/Utils.ts +12 -0
  73. package/src/ec.ts +114 -90
  74. package/src/keys.ts +26 -31
  75. package/src/random.ts +12 -1
  76. package/src/subtle.ts +157 -1
package/cpp/MGLKeys.cpp CHANGED
@@ -251,24 +251,26 @@ ParseKeyResult ParsePrivateKey(EVPKeyPointer* pkey,
251
251
  return ParseKeyResult::kParseKeyFailed;
252
252
  }
253
253
 
254
- OptionJSVariant BIOToStringOrBuffer(jsi::Runtime& rt, BIO* bio, PKFormatType format) {
254
+ jsi::Value BIOToStringOrBuffer(jsi::Runtime& rt, BIO* bio, PKFormatType format) {
255
255
  BUF_MEM* bptr;
256
256
  BIO_get_mem_ptr(bio, &bptr);
257
257
  if (format == kKeyFormatPEM) {
258
258
  // PEM is an ASCII format, so we will return it as a string.
259
- return JSVariant(std::string(bptr->data, bptr->length));
259
+ return toJSI(rt, std::string(bptr->data, bptr->length));
260
260
  } else {
261
- // CHECK_EQ(format, kKeyFormatDER);
261
+ CHECK_EQ(format, kKeyFormatDER);
262
262
  // DER is binary, return it as a buffer.
263
- return JSVariant(ByteSource::Allocated(bptr->data, bptr->length));
263
+ ByteSource::Builder out(bptr->length);
264
+ memcpy(out.data<void>(), bptr->data, bptr->length);
265
+ return toJSI(rt, std::move(out).release());
264
266
  }
265
267
  }
266
268
 
267
- OptionJSVariant WritePrivateKey(
269
+ jsi::Value WritePrivateKey(
268
270
  jsi::Runtime& runtime, EVP_PKEY* pkey,
269
271
  const PrivateKeyEncodingConfig& config) {
270
272
  BIOPointer bio(BIO_new(BIO_s_mem()));
271
- // CHECK(bio);
273
+ CHECK(bio);
272
274
 
273
275
  // If an empty string was passed as the passphrase, the ByteSource might
274
276
  // contain a null pointer, which OpenSSL will ignore, causing it to invoke its
@@ -293,8 +295,15 @@ OptionJSVariant WritePrivateKey(
293
295
  }
294
296
 
295
297
  bool err = false;
298
+ PKEncodingType encoding_type;
299
+
300
+ if (config.type_.has_value()) {
301
+ encoding_type = config.type_.value();
302
+ } else {
303
+ // default for no value in std::option `config.type_`
304
+ encoding_type = kKeyEncodingSEC1;
305
+ }
296
306
 
297
- PKEncodingType encoding_type = config.type_.value();
298
307
  if (encoding_type == kKeyEncodingPKCS1) {
299
308
  // PKCS#1 is only permitted for RSA keys.
300
309
  // CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_RSA);
@@ -307,8 +316,8 @@ OptionJSVariant WritePrivateKey(
307
316
  pass_len, nullptr, nullptr) != 1;
308
317
  } else {
309
318
  // Encode PKCS#1 as DER. This does not permit encryption.
310
- // CHECK_EQ(config.format_, kKeyFormatDER);
311
- // CHECK_NULL(config.cipher_);
319
+ CHECK_EQ(config.format_, kKeyFormatDER);
320
+ CHECK_NULL(config.cipher_);
312
321
  err = i2d_RSAPrivateKey_bio(bio.get(), rsa.get()) != 1;
313
322
  }
314
323
  } else if (encoding_type == kKeyEncodingPKCS8) {
@@ -318,31 +327,28 @@ OptionJSVariant WritePrivateKey(
318
327
  pass_len, nullptr, nullptr) != 1;
319
328
  } else {
320
329
  // Encode PKCS#8 as DER.
321
- // CHECK_EQ(config.format_, kKeyFormatDER);
330
+ CHECK_EQ(config.format_, kKeyFormatDER);
322
331
  err = i2d_PKCS8PrivateKey_bio(bio.get(), pkey, config.cipher_, pass,
323
332
  pass_len, nullptr, nullptr) != 1;
324
333
  }
325
334
  } else {
326
- // CHECK_EQ(encoding_type, kKeyEncodingSEC1);
335
+ CHECK_EQ(encoding_type, kKeyEncodingSEC1);
327
336
 
328
337
  // SEC1 is only permitted for EC keys.
329
- // CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_EC);
330
-
331
- // ECKeyPointer ec_key(EVP_PKEY_get1_EC_KEY(pkey));
332
- // if (config.format_ == kKeyFormatPEM) {
333
- // // Encode SEC1 as PEM.
334
- // err = PEM_write_bio_ECPrivateKey(
335
- // bio.get(), ec_key.get(),
336
- // config.cipher_,
337
- // reinterpret_cast<unsigned
338
- // char*>(pass), pass_len, nullptr,
339
- // nullptr) != 1;
340
- // } else {
341
- // // Encode SEC1 as DER. This does not permit encryption.
342
- // CHECK_EQ(config.format_, kKeyFormatDER);
343
- // CHECK_NULL(config.cipher_);
344
- // err = i2d_ECPrivateKey_bio(bio.get(), ec_key.get()) != 1;
345
- // }
338
+ CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_EC);
339
+
340
+ ECKeyPointer ec_key(EVP_PKEY_get1_EC_KEY(pkey));
341
+ if (config.format_ == kKeyFormatPEM) {
342
+ // Encode SEC1 as PEM.
343
+ err = PEM_write_bio_ECPrivateKey(bio.get(),ec_key.get(), config.cipher_,
344
+ reinterpret_cast<unsigned char*>(pass),
345
+ pass_len, nullptr, nullptr) != 1;
346
+ } else {
347
+ // Encode SEC1 as DER. This does not permit encryption.
348
+ CHECK_EQ(config.format_, kKeyFormatDER);
349
+ // CHECK_NULL(config.cipher_);
350
+ err = i2d_ECPrivateKey_bio(bio.get(), ec_key.get()) != 1;
351
+ }
346
352
  }
347
353
 
348
354
  if (err) {
@@ -354,36 +360,37 @@ OptionJSVariant WritePrivateKey(
354
360
 
355
361
  bool WritePublicKeyInner(EVP_PKEY* pkey, const BIOPointer& bio,
356
362
  const PublicKeyEncodingConfig& config) {
363
+ if (!config.type_.has_value()) return false;
357
364
  if (config.type_.value() == kKeyEncodingPKCS1) {
358
365
  // PKCS#1 is only valid for RSA keys.
359
- // CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_RSA);
366
+ CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_RSA);
360
367
  RsaPointer rsa(EVP_PKEY_get1_RSA(pkey));
361
368
  if (config.format_ == kKeyFormatPEM) {
362
369
  // Encode PKCS#1 as PEM.
363
370
  return PEM_write_bio_RSAPublicKey(bio.get(), rsa.get()) == 1;
364
371
  } else {
365
372
  // Encode PKCS#1 as DER.
366
- // CHECK_EQ(config.format_, kKeyFormatDER);
373
+ CHECK_EQ(config.format_, kKeyFormatDER);
367
374
  return i2d_RSAPublicKey_bio(bio.get(), rsa.get()) == 1;
368
375
  }
369
376
  } else {
370
- // CHECK_EQ(config.type_.ToChecked(), kKeyEncodingSPKI);
377
+ CHECK_EQ(config.type_.value(), kKeyEncodingSPKI);
371
378
  if (config.format_ == kKeyFormatPEM) {
372
379
  // Encode SPKI as PEM.
373
380
  return PEM_write_bio_PUBKEY(bio.get(), pkey) == 1;
374
381
  } else {
375
382
  // Encode SPKI as DER.
376
- // CHECK_EQ(config.format_, kKeyFormatDER);
383
+ CHECK_EQ(config.format_, kKeyFormatDER);
377
384
  return i2d_PUBKEY_bio(bio.get(), pkey) == 1;
378
385
  }
379
386
  }
380
387
  }
381
388
 
382
- OptionJSVariant WritePublicKey(
389
+ jsi::Value WritePublicKey(
383
390
  jsi::Runtime& runtime, EVP_PKEY* pkey,
384
391
  const PublicKeyEncodingConfig& config) {
385
392
  BIOPointer bio(BIO_new(BIO_s_mem()));
386
- // CHECK(bio);
393
+ CHECK(bio);
387
394
 
388
395
  if (!WritePublicKeyInner(pkey, bio, config)) {
389
396
  throw jsi::JSError(runtime, "Failed to encode public key");
@@ -518,20 +525,18 @@ EVP_PKEY* ManagedEVPPKey::get() const { return pkey_.get(); }
518
525
  // size_of_private_key() +
519
526
  // size_of_public_key());
520
527
  //}
521
- //
522
- // size_t ManagedEVPPKey::size_of_private_key() const {
523
- // size_t len = 0;
524
- // return (pkey_ && EVP_PKEY_get_raw_private_key(
525
- // pkey_.get(), nullptr, &len)
526
- // == 1) ? len : 0;
527
- //}
528
- //
529
- // size_t ManagedEVPPKey::size_of_public_key() const {
530
- // size_t len = 0;
531
- // return (pkey_ && EVP_PKEY_get_raw_public_key(
532
- // pkey_.get(), nullptr, &len)
533
- // == 1) ? len : 0;
534
- //}
528
+
529
+ size_t ManagedEVPPKey::size_of_private_key() const {
530
+ size_t len = 0;
531
+ return (pkey_ && EVP_PKEY_get_raw_private_key(pkey_.get(), nullptr, &len) == 1)
532
+ ? len : 0;
533
+ }
534
+
535
+ size_t ManagedEVPPKey::size_of_public_key() const {
536
+ size_t len = 0;
537
+ return (pkey_ && EVP_PKEY_get_raw_public_key(pkey_.get(), nullptr, &len) == 1)
538
+ ? len : 0;
539
+ }
535
540
 
536
541
  jsi::Value ExportJWKInner(jsi::Runtime &rt,
537
542
  std::shared_ptr<KeyObjectData> key,
@@ -549,45 +554,48 @@ jsi::Value ExportJWKInner(jsi::Runtime &rt,
549
554
  }
550
555
  }
551
556
 
552
- OptionJSVariant ManagedEVPPKey::ToEncodedPublicKey(
553
- jsi::Runtime& runtime, ManagedEVPPKey key,
554
- const PublicKeyEncodingConfig& config) {
557
+ jsi::Value ManagedEVPPKey::ToEncodedPublicKey(jsi::Runtime& rt,
558
+ ManagedEVPPKey key,
559
+ const PublicKeyEncodingConfig& config) {
555
560
  if (!key) return {};
556
- // TODO(osp) ignore all this for now
557
- // if (config.output_key_object_) {
558
- // // Note that this has the downside of containing sensitive data of the
559
- // // private key.
560
- // std::shared_ptr<KeyObjectData> data =
561
- // KeyObjectData::CreateAsymmetric(kKeyTypePublic, std::move(key));
562
- // TODO(osp) Replaced tristate for std::optional
563
- // return Tristate(KeyObjectHandle::Create(env, data).ToLocal(out));
564
- // } else if (config.format_ == kKeyFormatJWK) {
565
- // std::shared_ptr<KeyObjectData> data =
566
- // KeyObjectData::CreateAsymmetric(kKeyTypePublic, std::move(key));
567
- // *out = Object::New(env->isolate());
568
- // return ExportJWKInner(env, data, *out, false);
569
- // }
570
-
571
- return WritePublicKey(runtime, key.get(), config);
561
+ if (config.output_key_object_) {
562
+ // Note that this has the downside of containing sensitive data of the
563
+ // private key.
564
+ auto data = KeyObjectData::CreateAsymmetric(kKeyTypePublic, std::move(key));
565
+ auto handle = KeyObjectHandle::Create(rt, data);
566
+ auto out = jsi::Object::createFromHostObject(rt, handle);
567
+ return jsi::Value(std::move(out));
568
+ } else
569
+ if (config.format_ == kKeyFormatJWK) {
570
+ throw std::runtime_error("ToEncodedPublicKey 2 (JWK) not implemented from node");
571
+ // std::shared_ptr<KeyObjectData> data =
572
+ // KeyObjectData::CreateAsymmetric(kKeyTypePublic, std::move(key));
573
+ // *out = Object::New(env->isolate());
574
+ // return ExportJWKInner(env, data, *out, false);
575
+ }
576
+
577
+ return WritePublicKey(rt, key.get(), config);
572
578
  }
573
579
 
574
- OptionJSVariant ManagedEVPPKey::ToEncodedPrivateKey(
575
- jsi::Runtime& runtime, ManagedEVPPKey key,
576
- const PrivateKeyEncodingConfig& config) {
580
+ jsi::Value ManagedEVPPKey::ToEncodedPrivateKey(jsi::Runtime& rt,
581
+ ManagedEVPPKey key,
582
+ const PrivateKeyEncodingConfig& config) {
577
583
  if (!key) return {};
578
- // if (config.output_key_object_) {
579
- // std::shared_ptr<KeyObjectData> data =
580
- // KeyObjectData::CreateAsymmetric(kKeyTypePrivate, std::move(key));
581
- // TODO(osp) replaced tristate for std::optional
582
- // return Tristate(KeyObjectHandle::Create(env, data).ToLocal(out));
583
- // } else if (config.format_ == kKeyFormatJWK) {
584
- // std::shared_ptr<KeyObjectData> data =
585
- // KeyObjectData::CreateAsymmetric(kKeyTypePrivate, std::move(key));
586
- // *out = Object::New(env->isolate());
587
- // return ExportJWKInner(env, data, *out, false);
588
- // }
589
-
590
- return WritePrivateKey(runtime, key.get(), config);
584
+ if (config.output_key_object_) {
585
+ auto data = KeyObjectData::CreateAsymmetric(kKeyTypePrivate, std::move(key));
586
+ auto handle = KeyObjectHandle::Create(rt, data);
587
+ auto out = jsi::Object::createFromHostObject(rt, handle);
588
+ return jsi::Value(std::move(out));
589
+ } else
590
+ if (config.format_ == kKeyFormatJWK) {
591
+ throw std::runtime_error("ToEncodedPrivateKey 2 (JWK) not implemented from node");
592
+ // std::shared_ptr<KeyObjectData> data =
593
+ // KeyObjectData::CreateAsymmetric(kKeyTypePrivate, std::move(key));
594
+ // *out = Object::New(env->isolate());
595
+ // return ExportJWKInner(env, data, *out, false);
596
+ }
597
+
598
+ return WritePrivateKey(rt, key.get(), config);
591
599
  }
592
600
 
593
601
  NonCopyableMaybe<PrivateKeyEncodingConfig>
@@ -668,19 +676,25 @@ ManagedEVPPKey ManagedEVPPKey::GetPrivateKeyFromJs(jsi::Runtime& runtime,
668
676
  return GetParsedKey(runtime, std::move(pkey), ret,
669
677
  "Failed to read private key");
670
678
  } else {
671
- // CHECK(args[*offset]->IsObject() && allow_key_object);
672
- // KeyObjectHandle* key;
673
- // ASSIGN_OR_RETURN_UNWRAP(&key, args[*offset].As<Object>(),
674
- // ManagedEVPPKey()); CHECK_EQ(key->Data()->GetKeyType(),
675
- // kKeyTypePrivate);
676
- // (*offset) += 4;
677
- // return key->Data()->GetAsymmetricKey();
678
- throw jsi::JSError(runtime, "KeyObject are not currently supported");
679
+ if( !(args[*offset].isObject() && allow_key_object)) {
680
+ throw jsi::JSError(runtime,
681
+ "ManagedEVPPKey::GetPrivateKeyFromJs: First argument "
682
+ "must be object (CryptoKey) and caller must pass "
683
+ "allow_key_object as true");
684
+ }
685
+ std::shared_ptr<KeyObjectHandle> handle =
686
+ std::static_pointer_cast<KeyObjectHandle>(
687
+ args[*offset].asObject(runtime).getHostObject(runtime));
688
+ CHECK_EQ(handle->Data()->GetKeyType(), kKeyTypePrivate);
689
+ (*offset) += 4;
690
+ return handle->Data()->GetAsymmetricKey();
679
691
  }
680
692
  }
681
693
 
682
694
  ManagedEVPPKey ManagedEVPPKey::GetPublicOrPrivateKeyFromJs(
683
- jsi::Runtime& runtime, const jsi::Value* args, unsigned int* offset) {
695
+ jsi::Runtime& runtime,
696
+ const jsi::Value* args,
697
+ unsigned int* offset) {
684
698
  if (args[*offset].asObject(runtime).isArrayBuffer(runtime)) {
685
699
  auto dataArrayBuffer =
686
700
  args[(*offset)++].asObject(runtime).getArrayBuffer(runtime);
@@ -741,15 +755,19 @@ ManagedEVPPKey ManagedEVPPKey::GetPublicOrPrivateKeyFromJs(
741
755
  return ManagedEVPPKey::GetParsedKey(runtime, std::move(pkey), ret,
742
756
  "Failed to read asymmetric key");
743
757
  } else {
744
- throw jsi::JSError(
745
- runtime, "public encrypt only supports ArrayBuffer at the moment");
746
- // CHECK(args[*offset]->IsObject());
747
- // KeyObjectHandle* key =
748
- // Unwrap<KeyObjectHandle>(args[*offset].As<Object>());
749
- // CHECK_NOT_NULL(key);
750
- // CHECK_NE(key->Data()->GetKeyType(), kKeyTypeSecret);
751
- // (*offset) += 4;
752
- // return key->Data()->GetAsymmetricKey();
758
+ if( !(args[*offset].isObject()) ) {
759
+ throw jsi::JSError(runtime,
760
+ "ManagedEVPPKey::GetPublicOrPrivateKeyFromJs: First "
761
+ "argument not ArrayBuffer or object (CryptoKey)");
762
+ }
763
+ std::shared_ptr<KeyObjectHandle> handle =
764
+ std::static_pointer_cast<KeyObjectHandle>(
765
+ args[*offset].asObject(runtime).getHostObject(runtime));
766
+ // note: below check is kKeyTypeSecret in Node.js
767
+ // but kKeyTypePublic in RNQC when testing verify() on ECDSA
768
+ CHECK_EQ(handle->Data()->GetKeyType(), kKeyTypePublic);
769
+ (*offset) += 4;
770
+ return handle->Data()->GetAsymmetricKey();
753
771
  }
754
772
  }
755
773
 
@@ -838,6 +856,8 @@ jsi::Value KeyObjectHandle::get(
838
856
  return this->InitJWK(rt);
839
857
  } else if (name == "keyDetail") {
840
858
  return this->GetKeyDetail(rt);
859
+ } else if (name == "getAsymmetricKeyType") {
860
+ return this->GetAsymmetricKeyType(rt);
841
861
  }
842
862
 
843
863
  return {};
@@ -886,23 +906,14 @@ jsi::Value KeyObjectHandle::get(
886
906
  // registry->Register(GetKeyDetail);
887
907
  // registry->Register(Equals);
888
908
  // }
889
- //
890
- // MaybeLocal<Object> KeyObjectHandle::Create(
891
- // Environment* env,
892
- // std::shared_ptr<KeyObjectData>
893
- // data) {
894
- // Local<Object> obj;
895
- // Local<Function> ctor = KeyObjectHandle::Initialize(env);
896
- // CHECK(!env->crypto_key_object_handle_constructor().IsEmpty());
897
- // if (!ctor->NewInstance(env->context(), 0, nullptr).ToLocal(&obj))
898
- // return MaybeLocal<Object>();
899
- //
900
- // KeyObjectHandle* key = Unwrap<KeyObjectHandle>(obj);
901
- // CHECK_NOT_NULL(key);
902
- // key->data_ = data;
903
- // return obj;
904
- // }
905
- //
909
+
910
+ std::shared_ptr<KeyObjectHandle> KeyObjectHandle::Create(jsi::Runtime &rt,
911
+ std::shared_ptr<KeyObjectData> data) {
912
+ auto handle = std::make_shared<KeyObjectHandle>();
913
+ handle->data_ = data;
914
+ return handle;
915
+ }
916
+
906
917
  const std::shared_ptr<KeyObjectData>& KeyObjectHandle::Data() {
907
918
  return this->data_;
908
919
  }
@@ -1150,31 +1161,45 @@ jsi::Value KeyObjectHandle::GetKeyDetail(jsi::Runtime &rt) {
1150
1161
  });
1151
1162
  }
1152
1163
 
1153
- // Local<Value> KeyObjectHandle::GetAsymmetricKeyType() const {
1154
- // const ManagedEVPPKey& key = data_->GetAsymmetricKey();
1155
- // switch (EVP_PKEY_id(key.get())) {
1156
- // case EVP_PKEY_RSA:
1157
- // return env()->crypto_rsa_string();
1158
- // case EVP_PKEY_RSA_PSS:
1159
- // return env()->crypto_rsa_pss_string();
1160
- // case EVP_PKEY_DSA:
1161
- // return env()->crypto_dsa_string();
1162
- // case EVP_PKEY_DH:
1163
- // return env()->crypto_dh_string();
1164
- // case EVP_PKEY_EC:
1165
- // return env()->crypto_ec_string();
1166
- // case EVP_PKEY_ED25519:
1167
- // return env()->crypto_ed25519_string();
1168
- // case EVP_PKEY_ED448:
1169
- // return env()->crypto_ed448_string();
1170
- // case EVP_PKEY_X25519:
1171
- // return env()->crypto_x25519_string();
1172
- // case EVP_PKEY_X448:
1173
- // return env()->crypto_x448_string();
1174
- // default:
1175
- // return Undefined(env()->isolate());
1176
- // }
1177
- //}
1164
+ jsi::Value KeyObjectHandle::GetAsymmetricKeyType(jsi::Runtime &rt) const {
1165
+ return HOSTFN("getAsymmetricKeyType", 0) {
1166
+ const ManagedEVPPKey& key = this->data_->GetAsymmetricKey();
1167
+ std::string ret;
1168
+ switch (EVP_PKEY_id(key.get())) {
1169
+ case EVP_PKEY_RSA:
1170
+ ret = "rss";
1171
+ break;
1172
+ case EVP_PKEY_RSA_PSS:
1173
+ ret = "rsa_pss";
1174
+ break;
1175
+ case EVP_PKEY_DSA:
1176
+ ret = "dsa";
1177
+ break;
1178
+ case EVP_PKEY_DH:
1179
+ ret = "dh";
1180
+ break;
1181
+ case EVP_PKEY_EC:
1182
+ ret = "ec";
1183
+ break;
1184
+ case EVP_PKEY_ED25519:
1185
+ ret = "ed25519";
1186
+ break;
1187
+ case EVP_PKEY_ED448:
1188
+ ret = "ed448";
1189
+ break;
1190
+ case EVP_PKEY_X25519:
1191
+ ret = "x25519";
1192
+ break;
1193
+ case EVP_PKEY_X448:
1194
+ ret = "x448";
1195
+ break;
1196
+ default:
1197
+ throw jsi::JSError(rt, "unknown KeyType in GetAsymmetricKeyType");
1198
+ }
1199
+ return jsi::String::createFromUtf8(rt, ret);
1200
+ });
1201
+ }
1202
+
1178
1203
  //
1179
1204
  // void KeyObjectHandle::GetAsymmetricKeyType(
1180
1205
  // const
@@ -1198,7 +1223,7 @@ jsi::Value KeyObjectHandle::GetKeyDetail(jsi::Runtime &rt) {
1198
1223
  jsi::Value KeyObjectHandle::Export(jsi::Runtime &rt) {
1199
1224
  return HOSTFN("export", 2) {
1200
1225
  KeyType type = this->data_->GetKeyType();
1201
- OptionJSVariant result;
1226
+ jsi::Value result;
1202
1227
  if (type == kKeyTypeSecret) {
1203
1228
  result = this->ExportSecretKey(rt);
1204
1229
  }
@@ -1218,16 +1243,16 @@ jsi::Value KeyObjectHandle::Export(jsi::Runtime &rt) {
1218
1243
  result = this->ExportPrivateKey(rt, config.Release());
1219
1244
  }
1220
1245
  }
1221
- return toJSI(rt, result);
1246
+ return result;
1222
1247
  });
1223
1248
  }
1224
1249
 
1225
- OptionJSVariant KeyObjectHandle::ExportSecretKey(jsi::Runtime &rt) const {
1250
+ jsi::Value KeyObjectHandle::ExportSecretKey(jsi::Runtime &rt) const {
1226
1251
  std::string ret = data_->GetSymmetricKey();
1227
- return JSVariant(ByteSource::FromString(ret));
1252
+ return toJSI(rt, ByteSource::FromString(ret));
1228
1253
  }
1229
1254
 
1230
- OptionJSVariant KeyObjectHandle::ExportPublicKey(
1255
+ jsi::Value KeyObjectHandle::ExportPublicKey(
1231
1256
  jsi::Runtime& rt,
1232
1257
  const PublicKeyEncodingConfig& config) const {
1233
1258
  return WritePublicKey(rt,
@@ -1235,7 +1260,7 @@ OptionJSVariant KeyObjectHandle::ExportPublicKey(
1235
1260
  config);
1236
1261
  }
1237
1262
 
1238
- OptionJSVariant KeyObjectHandle::ExportPrivateKey(
1263
+ jsi::Value KeyObjectHandle::ExportPrivateKey(
1239
1264
  jsi::Runtime &rt,
1240
1265
  const PrivateKeyEncodingConfig& config) const {
1241
1266
  return WritePrivateKey(rt,
package/cpp/MGLKeys.h CHANGED
@@ -101,7 +101,8 @@ class ManagedEVPPKey {
101
101
  KeyEncodingContext context);
102
102
  //
103
103
  static ManagedEVPPKey GetParsedKey(jsi::Runtime &runtime,
104
- EVPKeyPointer &&pkey, ParseKeyResult ret,
104
+ EVPKeyPointer &&pkey,
105
+ ParseKeyResult ret,
105
106
  const char *default_msg);
106
107
 
107
108
  static ManagedEVPPKey GetPublicOrPrivateKeyFromJs(jsi::Runtime &runtime,
@@ -113,17 +114,17 @@ class ManagedEVPPKey {
113
114
  unsigned int *offset,
114
115
  bool allow_key_object);
115
116
 
116
- static OptionJSVariant ToEncodedPublicKey(
117
- jsi::Runtime &runtime, ManagedEVPPKey key,
118
- const PublicKeyEncodingConfig &config);
117
+ static jsi::Value ToEncodedPublicKey(jsi::Runtime &runtime,
118
+ ManagedEVPPKey key,
119
+ const PublicKeyEncodingConfig &config);
119
120
 
120
- static OptionJSVariant ToEncodedPrivateKey(
121
- jsi::Runtime &runtime, ManagedEVPPKey key,
122
- const PrivateKeyEncodingConfig &config);
121
+ static jsi::Value ToEncodedPrivateKey(jsi::Runtime &runtime,
122
+ ManagedEVPPKey key,
123
+ const PrivateKeyEncodingConfig &config);
123
124
 
124
125
  private:
125
- // size_t size_of_private_key() const;
126
- // size_t size_of_public_key() const;
126
+ size_t size_of_private_key() const;
127
+ size_t size_of_public_key() const;
127
128
 
128
129
  EVPKeyPointer pkey_;
129
130
  };
@@ -167,20 +168,24 @@ class JSI_EXPORT KeyObjectHandle: public jsi::HostObject {
167
168
  jsi::Value get(jsi::Runtime &rt, const jsi::PropNameID &propNameID);
168
169
  const std::shared_ptr<KeyObjectData>& Data();
169
170
 
171
+ static std::shared_ptr<KeyObjectHandle> Create(jsi::Runtime &rt,
172
+ std::shared_ptr<KeyObjectData> data);
173
+
170
174
  protected:
171
175
  jsi::Value Export(jsi::Runtime &rt);
172
176
  jsi::Value ExportJWK(jsi::Runtime &rt);
173
- OptionJSVariant ExportPublicKey(
177
+ jsi::Value ExportPublicKey(
174
178
  jsi::Runtime& rt,
175
179
  const PublicKeyEncodingConfig& config) const;
176
- OptionJSVariant ExportPrivateKey(
180
+ jsi::Value ExportPrivateKey(
177
181
  jsi::Runtime& rt,
178
182
  const PrivateKeyEncodingConfig& config) const;
179
- OptionJSVariant ExportSecretKey(jsi::Runtime& rt) const;
183
+ jsi::Value ExportSecretKey(jsi::Runtime& rt) const;
184
+ jsi::Value GetKeyDetail(jsi::Runtime &rt);
185
+ jsi::Value GetAsymmetricKeyType(jsi::Runtime &rt) const;
180
186
  jsi::Value Init(jsi::Runtime &rt);
181
187
  jsi::Value InitECRaw(jsi::Runtime &rt);
182
188
  jsi::Value InitJWK(jsi::Runtime &rt);
183
- jsi::Value GetKeyDetail(jsi::Runtime &rt);
184
189
 
185
190
  private:
186
191
  std::shared_ptr<KeyObjectData> data_;