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.
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.cpp +51 -14
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.cpp +25 -9
- package/cpp/Cipher/MGLRsa.cpp +13 -12
- package/cpp/Cipher/MGLRsa.h +2 -8
- package/cpp/JSIUtils/MGLJSIUtils.h +9 -0
- package/cpp/MGLKeys.cpp +174 -149
- package/cpp/MGLKeys.h +18 -13
- package/cpp/Sig/MGLSignHostObjects.cpp +284 -421
- package/cpp/Sig/MGLSignHostObjects.h +40 -0
- package/cpp/Utils/MGLUtils.cpp +0 -41
- package/cpp/Utils/MGLUtils.h +27 -6
- package/cpp/webcrypto/MGLWebCrypto.cpp +14 -4
- package/cpp/webcrypto/crypto_ec.cpp +106 -0
- package/cpp/webcrypto/crypto_ec.h +18 -0
- package/lib/commonjs/Cipher.js +138 -95
- package/lib/commonjs/Cipher.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/Cipher.js +11 -8
- package/lib/commonjs/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/sig.js +17 -0
- package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/commonjs/Utils.js +15 -1
- package/lib/commonjs/Utils.js.map +1 -1
- package/lib/commonjs/ec.js +79 -91
- package/lib/commonjs/ec.js.map +1 -1
- package/lib/commonjs/keys.js +10 -24
- package/lib/commonjs/keys.js.map +1 -1
- package/lib/commonjs/random.js +6 -0
- package/lib/commonjs/random.js.map +1 -1
- package/lib/commonjs/subtle.js +114 -0
- package/lib/commonjs/subtle.js.map +1 -1
- package/lib/module/Cipher.js +136 -93
- package/lib/module/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/Cipher.js +10 -7
- package/lib/module/NativeQuickCrypto/Cipher.js.map +1 -1
- package/lib/module/NativeQuickCrypto/sig.js +13 -0
- package/lib/module/NativeQuickCrypto/sig.js.map +1 -1
- package/lib/module/Utils.js +12 -0
- package/lib/module/Utils.js.map +1 -1
- package/lib/module/ec.js +76 -93
- package/lib/module/ec.js.map +1 -1
- package/lib/module/keys.js +8 -24
- package/lib/module/keys.js.map +1 -1
- package/lib/module/random.js +6 -0
- package/lib/module/random.js.map +1 -1
- package/lib/module/subtle.js +115 -1
- package/lib/module/subtle.js.map +1 -1
- package/lib/typescript/Cipher.d.ts +23 -13
- package/lib/typescript/Cipher.d.ts.map +1 -1
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts +11 -6
- package/lib/typescript/NativeQuickCrypto/Cipher.d.ts.map +1 -1
- package/lib/typescript/NativeQuickCrypto/sig.d.ts +10 -0
- package/lib/typescript/NativeQuickCrypto/sig.d.ts.map +1 -1
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts +2 -0
- package/lib/typescript/NativeQuickCrypto/webcrypto.d.ts.map +1 -1
- package/lib/typescript/Utils.d.ts +1 -0
- package/lib/typescript/Utils.d.ts.map +1 -1
- package/lib/typescript/ec.d.ts +3 -1
- package/lib/typescript/ec.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +11 -8
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/keys.d.ts +12 -1
- package/lib/typescript/keys.d.ts.map +1 -1
- package/lib/typescript/random.d.ts +2 -1
- package/lib/typescript/random.d.ts.map +1 -1
- package/lib/typescript/subtle.d.ts +4 -1
- package/lib/typescript/subtle.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/Cipher.ts +139 -75
- package/src/NativeQuickCrypto/Cipher.ts +14 -14
- package/src/NativeQuickCrypto/sig.ts +27 -0
- package/src/NativeQuickCrypto/webcrypto.ts +2 -0
- package/src/Utils.ts +12 -0
- package/src/ec.ts +114 -90
- package/src/keys.ts +26 -31
- package/src/random.ts +12 -1
- 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
|
-
|
|
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
|
|
259
|
+
return toJSI(rt, std::string(bptr->data, bptr->length));
|
|
260
260
|
} else {
|
|
261
|
-
|
|
261
|
+
CHECK_EQ(format, kKeyFormatDER);
|
|
262
262
|
// DER is binary, return it as a buffer.
|
|
263
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
311
|
-
|
|
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
|
-
|
|
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
|
-
|
|
335
|
+
CHECK_EQ(encoding_type, kKeyEncodingSEC1);
|
|
327
336
|
|
|
328
337
|
// SEC1 is only permitted for EC keys.
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
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
|
-
|
|
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
|
-
|
|
373
|
+
CHECK_EQ(config.format_, kKeyFormatDER);
|
|
367
374
|
return i2d_RSAPublicKey_bio(bio.get(), rsa.get()) == 1;
|
|
368
375
|
}
|
|
369
376
|
} else {
|
|
370
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
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
|
-
|
|
553
|
-
|
|
554
|
-
|
|
557
|
+
jsi::Value ManagedEVPPKey::ToEncodedPublicKey(jsi::Runtime& rt,
|
|
558
|
+
ManagedEVPPKey key,
|
|
559
|
+
const PublicKeyEncodingConfig& config) {
|
|
555
560
|
if (!key) return {};
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
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
|
-
|
|
575
|
-
|
|
576
|
-
|
|
580
|
+
jsi::Value ManagedEVPPKey::ToEncodedPrivateKey(jsi::Runtime& rt,
|
|
581
|
+
ManagedEVPPKey key,
|
|
582
|
+
const PrivateKeyEncodingConfig& config) {
|
|
577
583
|
if (!key) return {};
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
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
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
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,
|
|
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
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
//
|
|
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
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
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
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
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
|
-
|
|
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
|
|
1246
|
+
return result;
|
|
1222
1247
|
});
|
|
1223
1248
|
}
|
|
1224
1249
|
|
|
1225
|
-
|
|
1250
|
+
jsi::Value KeyObjectHandle::ExportSecretKey(jsi::Runtime &rt) const {
|
|
1226
1251
|
std::string ret = data_->GetSymmetricKey();
|
|
1227
|
-
return
|
|
1252
|
+
return toJSI(rt, ByteSource::FromString(ret));
|
|
1228
1253
|
}
|
|
1229
1254
|
|
|
1230
|
-
|
|
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
|
-
|
|
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,
|
|
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
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
static jsi::Value ToEncodedPublicKey(jsi::Runtime &runtime,
|
|
118
|
+
ManagedEVPPKey key,
|
|
119
|
+
const PublicKeyEncodingConfig &config);
|
|
119
120
|
|
|
120
|
-
static
|
|
121
|
-
|
|
122
|
-
|
|
121
|
+
static jsi::Value ToEncodedPrivateKey(jsi::Runtime &runtime,
|
|
122
|
+
ManagedEVPPKey key,
|
|
123
|
+
const PrivateKeyEncodingConfig &config);
|
|
123
124
|
|
|
124
125
|
private:
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
177
|
+
jsi::Value ExportPublicKey(
|
|
174
178
|
jsi::Runtime& rt,
|
|
175
179
|
const PublicKeyEncodingConfig& config) const;
|
|
176
|
-
|
|
180
|
+
jsi::Value ExportPrivateKey(
|
|
177
181
|
jsi::Runtime& rt,
|
|
178
182
|
const PrivateKeyEncodingConfig& config) const;
|
|
179
|
-
|
|
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_;
|