react-native-quick-crypto 0.3.1 → 0.4.1
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/README.md +10 -6
- package/android/CMakeLists.txt +10 -2
- package/android/gradle.properties +1 -1
- package/android/src/main/AndroidManifest.xml +1 -1
- package/android/src/main/cpp/cpp-adapter.cpp +1 -1
- package/android/src/main/java/com/margelo/quickcrypto/QuickCryptoModule.java +70 -0
- package/android/src/main/java/com/{reactnativequickcrypto → margelo/quickcrypto}/QuickCryptoPackage.java +11 -12
- package/cpp/Cipher/MGLCipherHostObject.cpp +4 -5
- package/cpp/Cipher/MGLCreateCipherInstaller.cpp +1 -3
- package/cpp/Cipher/MGLGenerateKeyPairInstaller.h +6 -3
- package/cpp/Cipher/MGLGenerateKeyPairSyncInstaller.h +5 -3
- package/cpp/Cipher/MGLPublicCipher.h +1 -1
- package/cpp/Cipher/MGLPublicCipherInstaller.h +1 -1
- package/cpp/Cipher/MGLRsa.h +5 -1
- package/cpp/JSIUtils/MGLJSIMacros.h +69 -6
- package/cpp/{Cipher/MGLCipherKeys.cpp → MGLKeys.cpp} +47 -49
- package/cpp/{Cipher/MGLCipherKeys.h → MGLKeys.h} +29 -30
- package/cpp/MGLQuickCryptoHostObject.cpp +12 -0
- package/cpp/Sig/MGLSignHostObjects.cpp +889 -0
- package/cpp/Sig/MGLSignHostObjects.h +88 -0
- package/cpp/Sig/MGLSignInstaller.cpp +24 -0
- package/cpp/Sig/MGLSignInstaller.h +29 -0
- package/cpp/Sig/MGLVerifyInstaller.cpp +24 -0
- package/cpp/Sig/MGLVerifyInstaller.h +22 -0
- package/cpp/Utils/MGLUtils.cpp +67 -29
- package/cpp/Utils/MGLUtils.h +17 -17
- package/lib/commonjs/@types/crypto-browserify.d.js +2 -0
- package/lib/commonjs/@types/crypto-browserify.d.js.map +1 -0
- package/lib/commonjs/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/commonjs/NativeQuickCrypto/sig.js +2 -0
- package/lib/commonjs/NativeQuickCrypto/sig.js.map +1 -0
- package/lib/commonjs/QuickCrypto.js +4 -0
- package/lib/commonjs/QuickCrypto.js.map +1 -1
- package/lib/commonjs/index.js +7 -7
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/keys.js +1 -4
- package/lib/commonjs/keys.js.map +1 -1
- package/lib/commonjs/sig.js +170 -0
- package/lib/commonjs/sig.js.map +1 -0
- package/lib/module/@types/crypto-browserify.d.js +2 -0
- package/lib/module/@types/crypto-browserify.d.js.map +1 -0
- package/lib/module/NativeQuickCrypto/NativeQuickCrypto.js.map +1 -1
- package/lib/module/NativeQuickCrypto/sig.js +2 -0
- package/lib/module/NativeQuickCrypto/sig.js.map +1 -0
- package/lib/module/QuickCrypto.js +3 -0
- package/lib/module/QuickCrypto.js.map +1 -1
- package/lib/module/index.js +6 -9
- package/lib/module/index.js.map +1 -1
- package/lib/module/keys.js +1 -4
- package/lib/module/keys.js.map +1 -1
- package/lib/module/sig.js +155 -0
- package/lib/module/sig.js.map +1 -0
- package/lib/typescript/NativeQuickCrypto/NativeQuickCrypto.d.ts +3 -0
- package/lib/typescript/NativeQuickCrypto/sig.d.ts +12 -0
- package/lib/typescript/QuickCrypto.d.ts +3 -0
- package/lib/typescript/index.d.ts +206 -1
- package/lib/typescript/sig.d.ts +35 -0
- package/package.json +3 -2
- package/src/@types/crypto-browserify.d.ts +4 -0
- package/src/NativeQuickCrypto/NativeQuickCrypto.ts +3 -0
- package/src/NativeQuickCrypto/sig.ts +17 -0
- package/src/QuickCrypto.ts +3 -0
- package/src/index.ts +6 -5
- package/src/keys.ts +18 -13
- package/src/sig.ts +179 -0
- package/android/src/main/java/com/reactnativequickcrypto/QuickCryptoModule.java +0 -70
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
#ifndef MGLSignHostObjects_h
|
|
2
|
+
#define MGLSignHostObjects_h
|
|
3
|
+
|
|
4
|
+
#include <jsi/jsi.h>
|
|
5
|
+
#include <openssl/evp.h>
|
|
6
|
+
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <optional>
|
|
9
|
+
#include <string>
|
|
10
|
+
#include <utility>
|
|
11
|
+
|
|
12
|
+
#include "MGLKeys.h"
|
|
13
|
+
#ifdef ANDROID
|
|
14
|
+
#include "JSIUtils/MGLSmartHostObject.h"
|
|
15
|
+
#include "Utils/MGLUtils.h"
|
|
16
|
+
#else
|
|
17
|
+
#include "MGLSmartHostObject.h"
|
|
18
|
+
#include "MGLUtils.h"
|
|
19
|
+
#endif
|
|
20
|
+
|
|
21
|
+
namespace margelo {
|
|
22
|
+
|
|
23
|
+
namespace jsi = facebook::jsi;
|
|
24
|
+
|
|
25
|
+
static const unsigned int kNoDsaSignature = static_cast<unsigned int>(-1);
|
|
26
|
+
|
|
27
|
+
enum mode { kModeSign, kModeVerify };
|
|
28
|
+
|
|
29
|
+
enum DSASigEnc {
|
|
30
|
+
kSigEncDER,
|
|
31
|
+
kSigEncP1363,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
class SignBase : public MGLSmartHostObject {
|
|
35
|
+
public:
|
|
36
|
+
SignBase(std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
37
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
|
|
38
|
+
|
|
39
|
+
typedef enum {
|
|
40
|
+
kSignOk,
|
|
41
|
+
kSignUnknownDigest,
|
|
42
|
+
kSignInit,
|
|
43
|
+
kSignNotInitialised,
|
|
44
|
+
kSignUpdate,
|
|
45
|
+
kSignPrivateKey,
|
|
46
|
+
kSignPublicKey,
|
|
47
|
+
kSignMalformedSignature
|
|
48
|
+
} Error;
|
|
49
|
+
|
|
50
|
+
struct SignResult {
|
|
51
|
+
Error error;
|
|
52
|
+
std::optional<jsi::Value> signature;
|
|
53
|
+
|
|
54
|
+
explicit SignResult(Error err, std::optional<jsi::Value> sig = std::nullopt)
|
|
55
|
+
: error(err), signature(std::move(sig)) {}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
void InstallMethods(mode);
|
|
59
|
+
|
|
60
|
+
SignResult SignFinal(jsi::Runtime& runtime, const ManagedEVPPKey& pkey,
|
|
61
|
+
int padding, std::optional<int>& salt_len,
|
|
62
|
+
DSASigEnc dsa_sig_enc);
|
|
63
|
+
|
|
64
|
+
Error VerifyFinal(const ManagedEVPPKey& pkey, const ByteSource& sig,
|
|
65
|
+
int padding, std::optional<int>& saltlen,
|
|
66
|
+
bool* verify_result);
|
|
67
|
+
|
|
68
|
+
protected:
|
|
69
|
+
EVPMDPointer mdctx_;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
class MGLSignHostObject : public SignBase {
|
|
73
|
+
public:
|
|
74
|
+
explicit MGLSignHostObject(
|
|
75
|
+
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
76
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
class MGLVerifyHostObject : public SignBase {
|
|
80
|
+
public:
|
|
81
|
+
explicit MGLVerifyHostObject(
|
|
82
|
+
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
83
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
} // namespace margelo
|
|
87
|
+
|
|
88
|
+
#endif /* MGLSignHostObjects_h */
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#include "MGLSignInstaller.h"
|
|
2
|
+
|
|
3
|
+
#include "MGLSignHostObjects.h"
|
|
4
|
+
#ifdef ANDROID
|
|
5
|
+
#include "JSIUtils/MGLJSIMacros.h"
|
|
6
|
+
#else
|
|
7
|
+
#include "MGLJSIMacros.h"
|
|
8
|
+
#include "logs.h"
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
namespace margelo {
|
|
12
|
+
|
|
13
|
+
FieldDefinition getSignFieldDefinition(
|
|
14
|
+
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
15
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
|
|
16
|
+
return buildPair(
|
|
17
|
+
"createSign", JSIF([=]) {
|
|
18
|
+
auto hostObject =
|
|
19
|
+
std::make_shared<MGLSignHostObject>(jsCallInvoker, workerQueue);
|
|
20
|
+
return jsi::Object::createFromHostObject(runtime, hostObject);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
} // namespace margelo
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
//
|
|
2
|
+
// MGLSignInstaller.hpp
|
|
3
|
+
// DoubleConversion
|
|
4
|
+
//
|
|
5
|
+
// Created by Oscar on 30.06.22.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
#ifndef MGLSignInstaller_h
|
|
9
|
+
#define MGLSignInstaller_h
|
|
10
|
+
|
|
11
|
+
#include <jsi/jsi.h>
|
|
12
|
+
|
|
13
|
+
#include <memory>
|
|
14
|
+
|
|
15
|
+
#ifdef ANDROID
|
|
16
|
+
#include "JSIUtils/MGLSmartHostObject.h"
|
|
17
|
+
#else
|
|
18
|
+
#include "MGLSmartHostObject.h"
|
|
19
|
+
#endif
|
|
20
|
+
|
|
21
|
+
namespace margelo {
|
|
22
|
+
namespace jsi = facebook::jsi;
|
|
23
|
+
|
|
24
|
+
FieldDefinition getSignFieldDefinition(
|
|
25
|
+
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
26
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
|
|
27
|
+
} // namespace margelo
|
|
28
|
+
|
|
29
|
+
#endif /* MGLSignInstaller_h */
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#include "MGLVerifyInstaller.h"
|
|
2
|
+
|
|
3
|
+
#include "MGLSignHostObjects.h"
|
|
4
|
+
#ifdef ANDROID
|
|
5
|
+
#include "JSIUtils/MGLJSIMacros.h"
|
|
6
|
+
#else
|
|
7
|
+
#include "MGLJSIMacros.h"
|
|
8
|
+
#include "logs.h"
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
namespace margelo {
|
|
12
|
+
|
|
13
|
+
FieldDefinition getVerifyFieldDefinition(
|
|
14
|
+
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
15
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue) {
|
|
16
|
+
return buildPair(
|
|
17
|
+
"createVerify", JSIF([=]) {
|
|
18
|
+
auto hostObject =
|
|
19
|
+
std::make_shared<MGLVerifyHostObject>(jsCallInvoker, workerQueue);
|
|
20
|
+
return jsi::Object::createFromHostObject(runtime, hostObject);
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
} // namespace margelo
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#ifndef MGLVerifyInstaller_h
|
|
2
|
+
#define MGLVerifyInstaller_h
|
|
3
|
+
|
|
4
|
+
#include <jsi/jsi.h>
|
|
5
|
+
|
|
6
|
+
#include <memory>
|
|
7
|
+
|
|
8
|
+
#ifdef ANDROID
|
|
9
|
+
#include "JSIUtils/MGLSmartHostObject.h"
|
|
10
|
+
#else
|
|
11
|
+
#include "MGLSmartHostObject.h"
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
namespace margelo {
|
|
15
|
+
namespace jsi = facebook::jsi;
|
|
16
|
+
|
|
17
|
+
FieldDefinition getVerifyFieldDefinition(
|
|
18
|
+
std::shared_ptr<react::CallInvoker> jsCallInvoker,
|
|
19
|
+
std::shared_ptr<DispatchQueue::dispatch_queue> workerQueue);
|
|
20
|
+
} // namespace margelo
|
|
21
|
+
|
|
22
|
+
#endif /* MGLVerifyInstaller_h */
|
package/cpp/Utils/MGLUtils.cpp
CHANGED
|
@@ -1,18 +1,46 @@
|
|
|
1
|
-
//
|
|
2
|
-
// MGLUtils.cpp
|
|
3
|
-
// react-native-quick-crypto
|
|
4
|
-
//
|
|
5
|
-
// Created by Oscar on 21.06.22.
|
|
6
|
-
//
|
|
7
|
-
|
|
8
1
|
#include "MGLUtils.h"
|
|
9
2
|
|
|
10
3
|
#include <jsi/jsi.h>
|
|
11
4
|
|
|
5
|
+
#include <string>
|
|
6
|
+
|
|
7
|
+
#ifdef ANDROID
|
|
8
|
+
#include "JSIUtils/MGLJSIMacros.h"
|
|
9
|
+
#else
|
|
10
|
+
#include "MGLJSIMacros.h"
|
|
11
|
+
#endif
|
|
12
|
+
|
|
12
13
|
namespace margelo {
|
|
13
14
|
|
|
14
15
|
namespace jsi = facebook::jsi;
|
|
15
16
|
|
|
17
|
+
ByteSource ArrayBufferToByteSource(jsi::Runtime& runtime,
|
|
18
|
+
const jsi::ArrayBuffer& buffer) {
|
|
19
|
+
if (buffer.size(runtime) == 0) return ByteSource();
|
|
20
|
+
char* buf = MallocOpenSSL<char>(buffer.size(runtime));
|
|
21
|
+
CHECK_NOT_NULL(buf);
|
|
22
|
+
// const cast artificially removes the const qualifier, but you cannot still
|
|
23
|
+
// modify the data in this case, this is safe because we are just memcopying
|
|
24
|
+
// to the buffer
|
|
25
|
+
memcpy(buf, const_cast<jsi::ArrayBuffer&>(buffer).data(runtime),
|
|
26
|
+
buffer.size(runtime));
|
|
27
|
+
return ByteSource::Allocated(buf, buffer.size(runtime));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
ByteSource ArrayBufferToNTCByteSource(jsi::Runtime& runtime,
|
|
31
|
+
const jsi::ArrayBuffer& buffer) {
|
|
32
|
+
if (buffer.size(runtime) == 0) return ByteSource();
|
|
33
|
+
char* buf = MallocOpenSSL<char>(buffer.size(runtime) + 1);
|
|
34
|
+
CHECK_NOT_NULL(buf);
|
|
35
|
+
buf[buffer.size(runtime)] = 0;
|
|
36
|
+
// const cast artificially removes the const qualifier, but you cannot still
|
|
37
|
+
// modify the data in this case, this is safe because we are just memcopying
|
|
38
|
+
// to the buffer
|
|
39
|
+
memcpy(buf, const_cast<jsi::ArrayBuffer&>(buffer).data(runtime),
|
|
40
|
+
buffer.size(runtime));
|
|
41
|
+
return ByteSource::Allocated(buf, buffer.size(runtime));
|
|
42
|
+
}
|
|
43
|
+
|
|
16
44
|
ByteSource::ByteSource(ByteSource&& other) noexcept
|
|
17
45
|
: data_(other.data_),
|
|
18
46
|
allocated_data_(other.allocated_data_),
|
|
@@ -95,28 +123,38 @@ ByteSource& ByteSource::operator=(ByteSource&& other) noexcept {
|
|
|
95
123
|
// return out;
|
|
96
124
|
// }
|
|
97
125
|
//
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
//
|
|
107
|
-
|
|
108
|
-
//
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
//
|
|
113
|
-
//
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
126
|
+
ByteSource ByteSource::FromStringOrBuffer(jsi::Runtime& runtime,
|
|
127
|
+
const jsi::Value& value) {
|
|
128
|
+
return value.isString()
|
|
129
|
+
? FromString(value.asString(runtime).utf8(runtime))
|
|
130
|
+
: FromBuffer(runtime,
|
|
131
|
+
value.asObject(runtime).getArrayBuffer(runtime));
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// ntc = null terminated copy
|
|
135
|
+
ByteSource ByteSource::FromString(std::string str, bool ntc) {
|
|
136
|
+
// CHECK(str->IsString());
|
|
137
|
+
size_t size = str.size();
|
|
138
|
+
size_t alloc_size = ntc ? size + 1 : size;
|
|
139
|
+
ByteSource::Builder out(alloc_size);
|
|
140
|
+
// int opts = String::NO_OPTIONS;
|
|
141
|
+
// if (!ntc) opts |= String::NO_NULL_TERMINATION;
|
|
142
|
+
// str->WriteUtf8(env->isolate(), out.data<char>(), alloc_size, nullptr,
|
|
143
|
+
// opts);
|
|
144
|
+
if (ntc) {
|
|
145
|
+
strcpy(out.data<char>(), str.data());
|
|
146
|
+
} else {
|
|
147
|
+
strncpy(out.data<char>(), str.data(), alloc_size);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return std::move(out).release();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
ByteSource ByteSource::FromBuffer(jsi::Runtime& runtime,
|
|
154
|
+
const jsi::ArrayBuffer& buffer, bool ntc) {
|
|
155
|
+
return ntc ? ArrayBufferToNTCByteSource(runtime, buffer)
|
|
156
|
+
: ArrayBufferToByteSource(runtime, buffer);
|
|
157
|
+
}
|
|
120
158
|
//
|
|
121
159
|
// ByteSource ByteSource::FromSecretKeyBytes(
|
|
122
160
|
// Environment* env,
|
package/cpp/Utils/MGLUtils.h
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
//
|
|
2
|
-
// MGLUtils.h
|
|
3
|
-
// Pods
|
|
4
|
-
//
|
|
5
|
-
// Created by Oscar on 20.06.22.
|
|
6
|
-
//
|
|
7
|
-
|
|
8
1
|
#ifndef MGLUtils_h
|
|
9
2
|
#define MGLUtils_h
|
|
10
3
|
|
|
@@ -53,6 +46,8 @@ using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
|
|
|
53
46
|
using EVPKeyPointer = DeleteFnPtr<EVP_PKEY, EVP_PKEY_free>;
|
|
54
47
|
using BignumPointer = DeleteFnPtr<BIGNUM, BN_free>;
|
|
55
48
|
using RSAPointer = DeleteFnPtr<RSA, RSA_free>;
|
|
49
|
+
using EVPMDPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>;
|
|
50
|
+
using ECDSASigPointer = DeleteFnPtr<ECDSA_SIG, ECDSA_SIG_free>;
|
|
56
51
|
|
|
57
52
|
template <typename T>
|
|
58
53
|
class NonCopyableMaybe {
|
|
@@ -183,15 +178,14 @@ class ByteSource {
|
|
|
183
178
|
// v8::Local<v8::String> value,
|
|
184
179
|
// enum encoding enc = BASE64);
|
|
185
180
|
//
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
//
|
|
189
|
-
// static ByteSource FromString(Environment* env,
|
|
190
|
-
// v8::Local<v8::String> str,
|
|
191
|
-
// bool ntc = false);
|
|
181
|
+
static ByteSource FromStringOrBuffer(jsi::Runtime& runtime,
|
|
182
|
+
const jsi::Value& value);
|
|
192
183
|
|
|
193
|
-
|
|
194
|
-
|
|
184
|
+
static ByteSource FromString(std::string str, bool ntc = false);
|
|
185
|
+
|
|
186
|
+
static ByteSource FromBuffer(jsi::Runtime& runtime,
|
|
187
|
+
const jsi::ArrayBuffer& buffer,
|
|
188
|
+
bool ntc = false);
|
|
195
189
|
|
|
196
190
|
// static ByteSource FromBIO(const BIOPointer& bio);
|
|
197
191
|
//
|
|
@@ -214,6 +208,12 @@ class ByteSource {
|
|
|
214
208
|
: data_(data), allocated_data_(allocated_data), size_(size) {}
|
|
215
209
|
};
|
|
216
210
|
|
|
211
|
+
ByteSource ArrayBufferToByteSource(jsi::Runtime& runtime,
|
|
212
|
+
const jsi::ArrayBuffer& buffer);
|
|
213
|
+
|
|
214
|
+
ByteSource ArrayBufferToNTCByteSource(jsi::Runtime& runtime,
|
|
215
|
+
const jsi::ArrayBuffer& buffer);
|
|
216
|
+
|
|
217
217
|
// Originally part of the ArrayBufferContentOrView class
|
|
218
218
|
inline ByteSource ToNullTerminatedByteSource(jsi::Runtime& runtime,
|
|
219
219
|
jsi::ArrayBuffer& buffer) {
|
|
@@ -225,7 +225,7 @@ inline ByteSource ToNullTerminatedByteSource(jsi::Runtime& runtime,
|
|
|
225
225
|
return ByteSource::Allocated(buf, buffer.size(runtime));
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
-
int PasswordCallback(char* buf, int size, int rwflag, void* u) {
|
|
228
|
+
inline int PasswordCallback(char* buf, int size, int rwflag, void* u) {
|
|
229
229
|
const ByteSource* passphrase = *static_cast<const ByteSource**>(u);
|
|
230
230
|
if (passphrase != nullptr) {
|
|
231
231
|
size_t buflen = static_cast<size_t>(size);
|
|
@@ -238,7 +238,7 @@ int PasswordCallback(char* buf, int size, int rwflag, void* u) {
|
|
|
238
238
|
return -1;
|
|
239
239
|
}
|
|
240
240
|
|
|
241
|
-
void CheckEntropy() {
|
|
241
|
+
inline void CheckEntropy() {
|
|
242
242
|
for (;;) {
|
|
243
243
|
int status = RAND_status();
|
|
244
244
|
// CHECK_GE(status, 0); // Cannot fail.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["NativeQuickCrypto.ts"],"names":["global","__QuickCryptoProxy","QuickCryptoModule","NativeModules","QuickCrypto","message","Platform","OS","ExpoConstants","NativeUnimoduleProxy","modulesConstants","ExponentConstants","appOwnership","Error","nativeCallSyncHook","install","result","proxy","NativeQuickCrypto"],"mappings":";;;;;;;AAAA;;
|
|
1
|
+
{"version":3,"sources":["NativeQuickCrypto.ts"],"names":["global","__QuickCryptoProxy","QuickCryptoModule","NativeModules","QuickCrypto","message","Platform","OS","ExpoConstants","NativeUnimoduleProxy","modulesConstants","ExponentConstants","appOwnership","Error","nativeCallSyncHook","install","result","proxy","NativeQuickCrypto"],"mappings":";;;;;;;AAAA;;AAqCA;AACA,IAAIA,MAAM,CAACC,kBAAP,IAA6B,IAAjC,EAAuC;AACrC;AACA,QAAMC,iBAAiB,GAAGC,2BAAcC,WAAxC;;AACA,MAAIF,iBAAiB,IAAI,IAAzB,EAA+B;AAAA;;AAC7B,QAAIG,OAAO,GACT,kGADF;AAEAA,IAAAA,OAAO,IACL,2GADF;;AAEA,QAAIC,sBAASC,EAAT,KAAgB,KAAhB,IAAyBD,sBAASC,EAAT,KAAgB,OAA7C,EAAsD;AACpDF,MAAAA,OAAO,IAAI,4DAAX;AACD;;AACD,QAAIC,sBAASC,EAAT,KAAgB,SAApB,EAA+B;AAC7BF,MAAAA,OAAO,IAAI,iCAAX;AACD,KAV4B,CAW7B;;;AACA,UAAMG,aAAa,4BACjBL,2BAAcM,oBADG,oFACjB,sBAAoCC,gBADnB,2DACjB,uBAAsDC,iBADxD;;AAEA,QAAIH,aAAa,IAAI,IAArB,EAA2B;AACzB,UAAIA,aAAa,CAACI,YAAd,KAA+B,MAAnC,EAA2C;AACzC;AACA,cAAM,IAAIC,KAAJ,CACJ,uHADI,CAAN;AAGD,OALD,MAKO;AACL;AACAR,QAAAA,OAAO,IAAI,wCAAX;AACD;AACF;;AAEDA,IAAAA,OAAO,IAAI,oCAAX;AACA,UAAM,IAAIQ,KAAJ,CAAUR,OAAV,CAAN;AACD,GA/BoC,CAiCrC;;;AACA,MAAIL,MAAM,CAACc,kBAAP,IAA6B,IAA7B,IAAqCZ,iBAAiB,CAACa,OAAlB,IAA6B,IAAtE,EAA4E;AAC1E,UAAM,IAAIF,KAAJ,CACJ,oRADI,CAAN;AAGD,GAtCoC,CAwCrC;;;AACA,QAAMG,MAAM,GAAGd,iBAAiB,CAACa,OAAlB,EAAf;AACA,MAAIC,MAAM,KAAK,IAAf,EACE,MAAM,IAAIH,KAAJ,CACH,oKAAmKG,MAAO,EADvK,CAAN,CA3CmC,CA+CrC;;AACA,MAAIhB,MAAM,CAACC,kBAAP,IAA6B,IAAjC,EACE,MAAM,IAAIY,KAAJ,CACJ,4JADI,CAAN;AAGH;;AAED,MAAMI,KAAK,GAAGjB,MAAM,CAACC,kBAArB;AACO,MAAMiB,iBAAiB,GAAGD,KAA1B","sourcesContent":["import { NativeModules, Platform } from 'react-native';\nimport type { CreateHmacMethod } from './hmac';\nimport type { CreateHashMethod } from './hash';\nimport type { Pbkdf2Object } from './pbkdf2';\nimport type { RandomObject } from './random';\nimport type {\n CreateCipherMethod,\n CreateDecipherMethod,\n PublicEncryptMethod,\n PrivateDecryptMethod,\n GenerateKeyPairMethod,\n GenerateKeyPairSyncMethod,\n} from './Cipher';\nimport type { CreateSignMethod, CreateVerifyMethod } from './sig';\n\ninterface NativeQuickCryptoSpec {\n createHmac: CreateHmacMethod;\n pbkdf2: Pbkdf2Object;\n random: RandomObject;\n createHash: CreateHashMethod;\n createCipher: CreateCipherMethod;\n createDecipher: CreateDecipherMethod;\n publicEncrypt: PublicEncryptMethod;\n publicDecrypt: PublicEncryptMethod;\n privateDecrypt: PrivateDecryptMethod;\n generateKeyPair: GenerateKeyPairMethod;\n generateKeyPairSync: GenerateKeyPairSyncMethod;\n createSign: CreateSignMethod;\n createVerify: CreateVerifyMethod;\n}\n\n// global func declaration for JSI functions\ndeclare global {\n function nativeCallSyncHook(): unknown;\n var __QuickCryptoProxy: object | undefined;\n}\n\n// Check if the constructor exists. If not, try installing the JSI bindings.\nif (global.__QuickCryptoProxy == null) {\n // Get the native QuickCrypto ReactModule\n const QuickCryptoModule = NativeModules.QuickCrypto;\n if (QuickCryptoModule == null) {\n let message =\n 'Failed to install react-native-quick-crypto: The native `QuickCrypto` Module could not be found.';\n message +=\n '\\n* Make sure react-native-quick-crypto is correctly autolinked (run `npx react-native config` to verify)';\n if (Platform.OS === 'ios' || Platform.OS === 'macos') {\n message += '\\n* Make sure you ran `pod install` in the ios/ directory.';\n }\n if (Platform.OS === 'android') {\n message += '\\n* Make sure gradle is synced.';\n }\n // check if Expo\n const ExpoConstants =\n NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;\n if (ExpoConstants != null) {\n if (ExpoConstants.appOwnership === 'expo') {\n // We're running Expo Go\n throw new Error(\n 'react-native-quick-crypto is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'\n );\n } else {\n // We're running Expo bare / standalone\n message += '\\n* Make sure you ran `expo prebuild`.';\n }\n }\n\n message += '\\n* Make sure you rebuilt the app.';\n throw new Error(message);\n }\n\n // Check if we are running on-device (JSI)\n if (global.nativeCallSyncHook == null || QuickCryptoModule.install == null) {\n throw new Error(\n 'Failed to install react-native-quick-crypto: React Native is not running on-device. QuickCrypto can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'\n );\n }\n\n // Call the synchronous blocking install() function\n const result = QuickCryptoModule.install();\n if (result !== true)\n throw new Error(\n `Failed to install react-native-quick-crypto: The native QuickCrypto Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`\n );\n\n // Check again if the constructor now exists. If not, throw an error.\n if (global.__QuickCryptoProxy == null)\n throw new Error(\n 'Failed to install react-native-quick-crypto, the native initializer function does not exist. Are you trying to use QuickCrypto from different JS Runtimes?'\n );\n}\n\nconst proxy = global.__QuickCryptoProxy;\nexport const NativeQuickCrypto = proxy as any as NativeQuickCryptoSpec;\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
|
|
@@ -11,6 +11,8 @@ var random = _interopRequireWildcard(require("./random"));
|
|
|
11
11
|
|
|
12
12
|
var _Cipher = require("./Cipher");
|
|
13
13
|
|
|
14
|
+
var _sig = require("./sig");
|
|
15
|
+
|
|
14
16
|
var _Hmac = require("./Hmac");
|
|
15
17
|
|
|
16
18
|
var _Hash = require("./Hash");
|
|
@@ -35,6 +37,8 @@ const QuickCrypto = {
|
|
|
35
37
|
privateDecrypt: _Cipher.privateDecrypt,
|
|
36
38
|
generateKeyPair: _Cipher.generateKeyPair,
|
|
37
39
|
generateKeyPairSync: _Cipher.generateKeyPairSync,
|
|
40
|
+
createSign: _sig.createSign,
|
|
41
|
+
createVerify: _sig.createVerify,
|
|
38
42
|
constants: _constants.constants,
|
|
39
43
|
...pbkdf2,
|
|
40
44
|
...random
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["QuickCrypto.ts"],"names":["QuickCrypto","createHmac","Hmac","Hash","createHash","createCipher","createCipheriv","createDecipher","createDecipheriv","publicEncrypt","publicDecrypt","privateDecrypt","generateKeyPair","generateKeyPairSync","constants","pbkdf2","random"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AAWA;;AACA;;AACA;;;;;;AAEO,MAAMA,WAAW,GAAG;AACzBC,EAAAA,UAAU,EAAVA,gBADyB;AAEzBC,EAAAA,IAAI,EAAED,gBAFmB;AAGzBE,EAAAA,IAAI,EAAEC,gBAHmB;AAIzBA,EAAAA,UAAU,EAAVA,gBAJyB;AAKzBC,EAAAA,YAAY,EAAZA,oBALyB;AAMzBC,EAAAA,cAAc,EAAdA,sBANyB;AAOzBC,EAAAA,cAAc,EAAdA,sBAPyB;AAQzBC,EAAAA,gBAAgB,EAAhBA,wBARyB;AASzBC,EAAAA,aAAa,EAAbA,qBATyB;AAUzBC,EAAAA,aAAa,EAAbA,qBAVyB;AAWzBC,EAAAA,cAAc,EAAdA,sBAXyB;AAYzBC,EAAAA,eAAe,EAAfA,uBAZyB;AAazBC,EAAAA,mBAAmB,EAAnBA,2BAbyB;AAczBC,EAAAA,SAAS,EAATA,
|
|
1
|
+
{"version":3,"sources":["QuickCrypto.ts"],"names":["QuickCrypto","createHmac","Hmac","Hash","createHash","createCipher","createCipheriv","createDecipher","createDecipheriv","publicEncrypt","publicDecrypt","privateDecrypt","generateKeyPair","generateKeyPairSync","createSign","createVerify","constants","pbkdf2","random"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AAWA;;AACA;;AACA;;AACA;;;;;;AAEO,MAAMA,WAAW,GAAG;AACzBC,EAAAA,UAAU,EAAVA,gBADyB;AAEzBC,EAAAA,IAAI,EAAED,gBAFmB;AAGzBE,EAAAA,IAAI,EAAEC,gBAHmB;AAIzBA,EAAAA,UAAU,EAAVA,gBAJyB;AAKzBC,EAAAA,YAAY,EAAZA,oBALyB;AAMzBC,EAAAA,cAAc,EAAdA,sBANyB;AAOzBC,EAAAA,cAAc,EAAdA,sBAPyB;AAQzBC,EAAAA,gBAAgB,EAAhBA,wBARyB;AASzBC,EAAAA,aAAa,EAAbA,qBATyB;AAUzBC,EAAAA,aAAa,EAAbA,qBAVyB;AAWzBC,EAAAA,cAAc,EAAdA,sBAXyB;AAYzBC,EAAAA,eAAe,EAAfA,uBAZyB;AAazBC,EAAAA,mBAAmB,EAAnBA,2BAbyB;AAczBC,EAAAA,UAAU,EAAVA,eAdyB;AAezBC,EAAAA,YAAY,EAAZA,iBAfyB;AAgBzBC,EAAAA,SAAS,EAATA,oBAhByB;AAiBzB,KAAGC,MAjBsB;AAkBzB,KAAGC;AAlBsB,CAApB","sourcesContent":["import * as pbkdf2 from './pbkdf2';\nimport * as random from './random';\nimport {\n createCipher,\n createCipheriv,\n createDecipher,\n createDecipheriv,\n publicEncrypt,\n publicDecrypt,\n privateDecrypt,\n generateKeyPair,\n generateKeyPairSync,\n} from './Cipher';\nimport { createSign, createVerify } from './sig';\nimport { createHmac } from './Hmac';\nimport { createHash } from './Hash';\nimport { constants } from './constants';\n\nexport const QuickCrypto = {\n createHmac,\n Hmac: createHmac,\n Hash: createHash,\n createHash,\n createCipher,\n createCipheriv,\n createDecipher,\n createDecipheriv,\n publicEncrypt,\n publicDecrypt,\n privateDecrypt,\n generateKeyPair,\n generateKeyPairSync,\n createSign,\n createVerify,\n constants,\n ...pbkdf2,\n ...random,\n};\n"]}
|
package/lib/commonjs/index.js
CHANGED
|
@@ -9,16 +9,16 @@ var _reactNativeBuffer = require("@craftzdog/react-native-buffer");
|
|
|
9
9
|
|
|
10
10
|
var _QuickCrypto = require("./QuickCrypto");
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
global.Buffer = _reactNativeBuffer.Buffer; // @ts-expect-error
|
|
12
|
+
var _cryptoBrowserify = _interopRequireDefault(require("crypto-browserify"));
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const crypto = { ...
|
|
16
|
+
// @ts-expect-error Buffer does not match exact same type definition.
|
|
17
|
+
global.Buffer = _reactNativeBuffer.Buffer;
|
|
18
|
+
const crypto = { ..._cryptoBrowserify.default,
|
|
20
19
|
..._QuickCrypto.QuickCrypto
|
|
21
|
-
}; //
|
|
20
|
+
}; // for randombytes https://github.com/crypto-browserify/randombytes/blob/master/browser.js#L16
|
|
21
|
+
// @ts-expect-error QuickCrypto is missing `subtle` and `randomUUID`
|
|
22
22
|
|
|
23
23
|
global.crypto = crypto;
|
|
24
24
|
module.exports = crypto;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.ts"],"names":["global","Buffer","crypto","
|
|
1
|
+
{"version":3,"sources":["index.ts"],"names":["global","Buffer","crypto","FallbackCrypto","QuickCrypto","module","exports"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;;;AAEA;AACAA,MAAM,CAACC,MAAP,GAAgBA,yBAAhB;AAEA,MAAMC,MAAM,GAAG,EAAE,GAAGC,yBAAL;AAAqB,KAAGC;AAAxB,CAAf,C,CAEA;AACA;;AACAJ,MAAM,CAACE,MAAP,GAAgBA,MAAhB;AAEAG,MAAM,CAACC,OAAP,GAAiBJ,MAAjB;eACeA,M","sourcesContent":["import { Buffer } from '@craftzdog/react-native-buffer';\nimport { QuickCrypto } from './QuickCrypto';\nimport FallbackCrypto from 'crypto-browserify';\n\n// @ts-expect-error Buffer does not match exact same type definition.\nglobal.Buffer = Buffer;\n\nconst crypto = { ...FallbackCrypto, ...QuickCrypto };\n\n// for randombytes https://github.com/crypto-browserify/randombytes/blob/master/browser.js#L16\n// @ts-expect-error QuickCrypto is missing `subtle` and `randomUUID`\nglobal.crypto = crypto;\n\nmodule.exports = crypto;\nexport default crypto;\n"]}
|
package/lib/commonjs/keys.js
CHANGED
|
@@ -59,10 +59,7 @@ function parseKeyType(typeStr, required, keyType, isPublic, optionName) {
|
|
|
59
59
|
return undefined;
|
|
60
60
|
} else if (typeStr === 'pkcs1') {
|
|
61
61
|
if (keyType !== undefined && keyType !== 'rsa') {
|
|
62
|
-
throw new Error(`Crypto incompatible key options: ${typeStr} can only be used for RSA keys`);
|
|
63
|
-
// typeStr,
|
|
64
|
-
// 'can only be used for RSA keys'
|
|
65
|
-
// );
|
|
62
|
+
throw new Error(`Crypto incompatible key options: ${typeStr} can only be used for RSA keys`);
|
|
66
63
|
}
|
|
67
64
|
|
|
68
65
|
return KeyEncoding.kKeyEncodingPKCS1;
|
package/lib/commonjs/keys.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["keys.ts"],"names":["KFormatType","KeyInputContext","KeyEncoding","encodingNames","kKeyEncodingPKCS1","kKeyEncodingPKCS8","kKeyEncodingSPKI","kKeyEncodingSEC1","option","name","objName","undefined","parseKeyFormat","formatStr","defaultFormat","optionName","kKeyFormatPEM","kKeyFormatDER","kKeyFormatJWK","Error","parseKeyType","typeStr","required","keyType","isPublic","parseKeyFormatAndType","enc","format","type","isInput","isRequired","parseKeyEncoding","cipher","passphrase","encoding","prepareAsymmetricKey","key","ctx","data","kConsumePrivate","kCreatePrivate","preparePrivateKey","preparePublicOrPrivateKey","kConsumePublic","parsePublicKeyEncoding","parsePrivateKeyEncoding"],"mappings":";;;;;;;;;;AAAA;;AAEA;AACA;IACKA,W;;WAAAA,W;AAAAA,EAAAA,W,CAAAA,W;AAAAA,EAAAA,W,CAAAA,W;AAAAA,EAAAA,W,CAAAA,W;GAAAA,W,KAAAA,W;;IAMAC,e;;WAAAA,e;AAAAA,EAAAA,e,CAAAA,e;AAAAA,EAAAA,e,CAAAA,e;AAAAA,EAAAA,e,CAAAA,e;AAAAA,EAAAA,e,CAAAA,e;GAAAA,e,KAAAA,e;;IAOAC,W;;WAAAA,W;AAAAA,EAAAA,W,CAAAA,W;AAAAA,EAAAA,W,CAAAA,W;AAAAA,EAAAA,W,CAAAA,W;AAAAA,EAAAA,W,CAAAA,W;GAAAA,W,KAAAA,W;;AAOL,MAAMC,aAAa,GAAG;AACpB,GAACD,WAAW,CAACE,iBAAb,GAAiC,OADb;AAEpB,GAACF,WAAW,CAACG,iBAAb,GAAiC,OAFb;AAGpB,GAACH,WAAW,CAACI,gBAAb,GAAgC,MAHZ;AAIpB,GAACJ,WAAW,CAACK,gBAAb,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SAAOD,oBAAoB,CAACC,GAAD,EAAMnC,eAAe,CAAC0C,cAAtB,CAA3B;AACD,C,CAED;AACA;AACA;;;AACO,SAASC,sBAAT,CACLlB,GADK,EAQLH,OARK,EASLb,OATK,EAUL;AACA,SAAOqB,gBAAgB,CAACL,GAAD,EAAMH,OAAN,EAAeA,OAAO,GAAG,IAAH,GAAUZ,SAAhC,EAA2CD,OAA3C,CAAvB;AACD,C,CAED;AACA;AACA;;;AACO,SAASmC,uBAAT,CACLnB,GADK,EAQLH,OARK,EASLb,OATK,EAUL;AACA,SAAOqB,gBAAgB,CAACL,GAAD,EAAMH,OAAN,EAAe,KAAf,EAAsBb,OAAtB,CAAvB;AACD","sourcesContent":["import { BinaryLike, binaryLikeToArrayBuffer, isStringOrBuffer } from './Utils';\n\n// On node this value is defined on the native side, for now I'm just creating it here in JS\n// TODO(osp) move this into native side to make sure they always match\nenum KFormatType {\n kKeyFormatDER,\n kKeyFormatPEM,\n kKeyFormatJWK,\n}\n\nenum KeyInputContext {\n kConsumePublic,\n kConsumePrivate,\n kCreatePublic,\n kCreatePrivate,\n}\n\nenum KeyEncoding {\n kKeyEncodingPKCS1,\n kKeyEncodingPKCS8,\n kKeyEncodingSPKI,\n kKeyEncodingSEC1,\n}\n\nconst encodingNames = {\n [KeyEncoding.kKeyEncodingPKCS1]: 'pkcs1',\n [KeyEncoding.kKeyEncodingPKCS8]: 'pkcs8',\n [KeyEncoding.kKeyEncodingSPKI]: 'spki',\n [KeyEncoding.kKeyEncodingSEC1]: 'sec1',\n};\n\nfunction option(name: string, objName: string | undefined) {\n return objName === undefined\n ? `options.${name}`\n : `options.${objName}.${name}`;\n}\n\nfunction parseKeyFormat(\n formatStr: string,\n defaultFormat: KFormatType | undefined,\n optionName?: string\n) {\n if (formatStr === undefined && defaultFormat !== undefined)\n return defaultFormat;\n else if (formatStr === 'pem') return KFormatType.kKeyFormatPEM;\n else if (formatStr === 'der') return KFormatType.kKeyFormatDER;\n else if (formatStr === 'jwk') return KFormatType.kKeyFormatJWK;\n throw new Error(`Invalid key format str: ${optionName}`);\n // throw new ERR_INVALID_ARG_VALUE(optionName, formatStr);\n}\n\nfunction parseKeyType(\n typeStr: string,\n required: boolean,\n keyType: string,\n isPublic: boolean,\n optionName: string\n) {\n if (typeStr === undefined && !required) {\n return undefined;\n } else if (typeStr === 'pkcs1') {\n if (keyType !== undefined && keyType !== 'rsa') {\n throw new Error(\n `Crypto incompatible key options: ${typeStr} can only be used for RSA keys`\n );\n // throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(\n // typeStr,\n // 'can only be used for RSA keys'\n // );\n }\n return KeyEncoding.kKeyEncodingPKCS1;\n } else if (typeStr === 'spki' && isPublic !== false) {\n return KeyEncoding.kKeyEncodingSPKI;\n } else if (typeStr === 'pkcs8' && isPublic !== true) {\n return KeyEncoding.kKeyEncodingPKCS8;\n } else if (typeStr === 'sec1' && isPublic !== true) {\n if (keyType !== undefined && keyType !== 'ec') {\n throw new Error(\n `Incompatible key options ${typeStr} can only be used for EC keys`\n );\n }\n return KeyEncoding.kKeyEncodingSEC1;\n }\n\n throw new Error(`Invalid option ${optionName} - ${typeStr}`);\n}\n\nfunction parseKeyFormatAndType(\n enc: any,\n keyType: any,\n isPublic: any,\n objName: any\n) {\n const { format: formatStr, type: typeStr } = enc;\n\n const isInput = keyType === undefined;\n const format = parseKeyFormat(\n formatStr,\n isInput ? KFormatType.kKeyFormatPEM : undefined,\n option('format', objName)\n );\n\n const isRequired =\n (!isInput || format === KFormatType.kKeyFormatDER) &&\n format !== KFormatType.kKeyFormatJWK;\n const type = parseKeyType(\n typeStr,\n isRequired,\n keyType,\n isPublic,\n option('type', objName)\n );\n return { format, type };\n}\n\nfunction parseKeyEncoding(\n enc: {\n key: any;\n encoding?: string;\n format?: string;\n cipher?: string;\n passphrase?: string;\n },\n keyType: string | undefined,\n isPublic: boolean | undefined,\n objName?: string\n) {\n // validateObject(enc, 'options');\n\n const isInput = keyType === undefined;\n\n const { format, type } = parseKeyFormatAndType(\n enc,\n keyType,\n isPublic,\n objName\n );\n\n let cipher, passphrase, encoding;\n if (isPublic !== true) {\n ({ cipher, passphrase, encoding } = enc);\n\n if (!isInput) {\n if (cipher != null) {\n if (typeof cipher !== 'string')\n throw new Error(\n `Invalid argument ${option('cipher', objName)}: ${cipher}`\n );\n if (\n format === KFormatType.kKeyFormatDER &&\n (type === KeyEncoding.kKeyEncodingPKCS1 ||\n type === KeyEncoding.kKeyEncodingSEC1)\n ) {\n throw new Error(\n `Incompatible key options ${encodingNames[type]} does not support encryption`\n );\n }\n } else if (passphrase !== undefined) {\n throw new Error(\n `invalid argument ${option('cipher', objName)}: ${cipher}`\n );\n }\n }\n\n if (\n (isInput && passphrase !== undefined && !isStringOrBuffer(passphrase)) ||\n (!isInput && cipher != null && !isStringOrBuffer(passphrase))\n ) {\n throw new Error(\n `Invalid argument value ${option('passphrase', objName)}: ${passphrase}`\n );\n }\n }\n\n if (passphrase !== undefined)\n passphrase = binaryLikeToArrayBuffer(passphrase, encoding);\n\n return { format, type, cipher, passphrase };\n}\n\nfunction prepareAsymmetricKey(\n key:\n | BinaryLike\n | { key: any; encoding?: string; format?: any; passphrase?: string },\n ctx: KeyInputContext\n): {\n format: KFormatType;\n data: ArrayBuffer;\n type?: any;\n passphrase?: any;\n} {\n // TODO(osp) check, KeyObject some node object\n // if (isKeyObject(key)) {\n // // Best case: A key object, as simple as that.\n // return { data: getKeyObjectHandle(key, ctx) };\n // } else\n // if (isCryptoKey(key)) {\n // return { data: getKeyObjectHandle(key[kKeyObject], ctx) };\n // } else\n if (isStringOrBuffer(key)) {\n // Expect PEM by default, mostly for backward compatibility.\n return {\n format: KFormatType.kKeyFormatPEM,\n data: binaryLikeToArrayBuffer(key),\n };\n } else if (typeof key === 'object') {\n const {\n key: data,\n encoding,\n // format\n } = key;\n // // The 'key' property can be a KeyObject as well to allow specifying\n // // additional options such as padding along with the key.\n // if (isKeyObject(data)) return { data: getKeyObjectHandle(data, ctx) };\n // else if (isCryptoKey(data))\n // return { data: getKeyObjectHandle(data[kKeyObject], ctx) };\n // else if (isJwk(data) && format === 'jwk')\n // return { data: getKeyObjectHandleFromJwk(data, ctx), format: 'jwk' };\n // Either PEM or DER using PKCS#1 or SPKI.\n if (!isStringOrBuffer(data)) {\n throw new Error(\n 'prepareAsymmetricKey: key is not a string or ArrayBuffer'\n );\n }\n\n const isPublic =\n ctx === KeyInputContext.kConsumePrivate ||\n ctx === KeyInputContext.kCreatePrivate\n ? false\n : undefined;\n\n return {\n data: binaryLikeToArrayBuffer(data, encoding),\n ...parseKeyEncoding(key, undefined, isPublic),\n };\n }\n\n throw new Error('[prepareAsymetricKey] Invalid argument key: ${key}');\n}\n\n// TODO(osp) any here is a node KeyObject\nexport function preparePrivateKey(\n key:\n | BinaryLike\n | {\n key: any;\n encoding?: string;\n format?: any;\n padding?: number;\n passphrase?: string;\n }\n) {\n return prepareAsymmetricKey(key, KeyInputContext.kConsumePrivate);\n}\n\n// TODO(osp) any here is a node KeyObject\nexport function preparePublicOrPrivateKey(\n key:\n | BinaryLike\n | { key: any; encoding?: string; format?: any; padding?: number }\n) {\n return prepareAsymmetricKey(key, KeyInputContext.kConsumePublic);\n}\n\n// Parses the public key encoding based on an object. keyType must be undefined\n// when this is used to parse an input encoding and must be a valid key type if\n// used to parse an output encoding.\nexport function parsePublicKeyEncoding(\n enc: {\n key: any;\n encoding?: string;\n format?: string;\n cipher?: string;\n passphrase?: string;\n },\n keyType: string | undefined,\n objName?: string\n) {\n return parseKeyEncoding(enc, keyType, keyType ? true : undefined, objName);\n}\n\n// Parses the private key encoding based on an object. keyType must be undefined\n// when this is used to parse an input encoding and must be a valid key type if\n// used to parse an output encoding.\nexport function parsePrivateKeyEncoding(\n enc: {\n key: any;\n encoding?: string;\n format?: string;\n cipher?: string;\n passphrase?: string;\n },\n keyType: string | undefined,\n objName?: string\n) {\n return parseKeyEncoding(enc, keyType, false, objName);\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["keys.ts"],"names":["KFormatType","KeyInputContext","KeyEncoding","encodingNames","kKeyEncodingPKCS1","kKeyEncodingPKCS8","kKeyEncodingSPKI","kKeyEncodingSEC1","option","name","objName","undefined","parseKeyFormat","formatStr","defaultFormat","optionName","kKeyFormatPEM","kKeyFormatDER","kKeyFormatJWK","Error","parseKeyType","typeStr","required","keyType","isPublic","parseKeyFormatAndType","enc","format","type","isInput","isRequired","parseKeyEncoding","cipher","passphrase","encoding","prepareAsymmetricKey","key","ctx","data","kConsumePrivate","kCreatePrivate","preparePrivateKey","preparePublicOrPrivateKey","kConsumePublic","parsePublicKeyEncoding","parsePrivateKeyEncoding"],"mappings":";;;;;;;;;;AAAA;;AAEA;AACA;IACKA,W;;WAAAA,W;AAAAA,EAAAA,W,CAAAA,W;AAAAA,EAAAA,W,CAAAA,W;AAAAA,EAAAA,W,CAAAA,W;GAAAA,W,KAAAA,W;;IAMAC,e;;WAAAA,e;AAAAA,EAAAA,e,CAAAA,e;AAAAA,EAAAA,e,CAAAA,e;AAAAA,EAAAA,e,CAAAA,e;AAAAA,EAAAA,e,CAAAA,e;GAAAA,e,KAAAA,e;;IAOAC,W;;WAAAA,W;AAAAA,EAAAA,W,CAAAA,W;AAAAA,EAAAA,W,CAAAA,W;AAAAA,EAAAA,W,CAAAA,W;AAAAA,EAAAA,W,CAAAA,W;GAAAA,W,KAAAA,W;;AAOL,MAAMC,aAAa,GAAG;AACpB,GAACD,WAAW,CAACE,iBAAb,GAAiC,OADb;AAEpB,GAACF,WAAW,CAACG,iBAAb,GAAiC,OAFb;AAGpB,GAACH,WAAW,CAACI,gBAAb,GAAgC,MAHZ;AAIpB,GAACJ,WAAW,CAACK,gBAAb,GAAgC;AAJZ,CAAtB;;AAOA,SAASC,MAAT,CAAgBC,IAAhB,EAA8BC,OAA9B,EAA2D;AACzD,SAAOA,OAAO,KAAKC,SAAZ,GACF,WAAUF,IAAK,EADb,GAEF,WAAUC,OAAQ,IAAGD,IAAK,EAF/B;AAGD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AG,IAAH,GAAUZ,SAAhC,EAA2CD,OAA3C,CAAvB;AACD,C,CAED;AACA;AACA;;;AACO,SAASmC,uBAAT,CACLnB,GADK,EAQLH,OARK,EASLb,OATK,EAUL;AACA,SAAOqB,gBAAgB,CAACL,GAAD,EAAMH,OAAN,EAAe,KAAf,EAAsBb,OAAtB,CAAvB;AACD","sourcesContent":["import { BinaryLike, binaryLikeToArrayBuffer, isStringOrBuffer } from './Utils';\n\n// On node this value is defined on the native side, for now I'm just creating it here in JS\n// TODO(osp) move this into native side to make sure they always match\nenum KFormatType {\n kKeyFormatDER,\n kKeyFormatPEM,\n kKeyFormatJWK,\n}\n\nenum KeyInputContext {\n kConsumePublic,\n kConsumePrivate,\n kCreatePublic,\n kCreatePrivate,\n}\n\nenum KeyEncoding {\n kKeyEncodingPKCS1,\n kKeyEncodingPKCS8,\n kKeyEncodingSPKI,\n kKeyEncodingSEC1,\n}\n\nconst encodingNames = {\n [KeyEncoding.kKeyEncodingPKCS1]: 'pkcs1',\n [KeyEncoding.kKeyEncodingPKCS8]: 'pkcs8',\n [KeyEncoding.kKeyEncodingSPKI]: 'spki',\n [KeyEncoding.kKeyEncodingSEC1]: 'sec1',\n};\n\nfunction option(name: string, objName: string | undefined) {\n return objName === undefined\n ? `options.${name}`\n : `options.${objName}.${name}`;\n}\n\nfunction parseKeyFormat(\n formatStr: string | undefined,\n defaultFormat: KFormatType | undefined,\n optionName?: string\n) {\n if (formatStr === undefined && defaultFormat !== undefined)\n return defaultFormat;\n else if (formatStr === 'pem') return KFormatType.kKeyFormatPEM;\n else if (formatStr === 'der') return KFormatType.kKeyFormatDER;\n else if (formatStr === 'jwk') return KFormatType.kKeyFormatJWK;\n throw new Error(`Invalid key format str: ${optionName}`);\n // throw new ERR_INVALID_ARG_VALUE(optionName, formatStr);\n}\n\nfunction parseKeyType(\n typeStr: string | undefined,\n required: boolean,\n keyType: string | undefined,\n isPublic: boolean | undefined,\n optionName: string\n) {\n if (typeStr === undefined && !required) {\n return undefined;\n } else if (typeStr === 'pkcs1') {\n if (keyType !== undefined && keyType !== 'rsa') {\n throw new Error(\n `Crypto incompatible key options: ${typeStr} can only be used for RSA keys`\n );\n }\n return KeyEncoding.kKeyEncodingPKCS1;\n } else if (typeStr === 'spki' && isPublic !== false) {\n return KeyEncoding.kKeyEncodingSPKI;\n } else if (typeStr === 'pkcs8' && isPublic !== true) {\n return KeyEncoding.kKeyEncodingPKCS8;\n } else if (typeStr === 'sec1' && isPublic !== true) {\n if (keyType !== undefined && keyType !== 'ec') {\n throw new Error(\n `Incompatible key options ${typeStr} can only be used for EC keys`\n );\n }\n return KeyEncoding.kKeyEncodingSEC1;\n }\n\n throw new Error(`Invalid option ${optionName} - ${typeStr}`);\n}\n\nfunction parseKeyFormatAndType(\n enc: {\n key: any;\n type?: string;\n encoding?: string;\n format?: string;\n cipher?: string;\n passphrase?: string;\n },\n keyType: string | undefined,\n isPublic: boolean | undefined,\n objName: string | undefined\n) {\n const { format: formatStr, type: typeStr } = enc;\n\n const isInput = keyType === undefined;\n const format = parseKeyFormat(\n formatStr,\n isInput ? KFormatType.kKeyFormatPEM : undefined,\n option('format', objName)\n );\n\n const isRequired =\n (!isInput || format === KFormatType.kKeyFormatDER) &&\n format !== KFormatType.kKeyFormatJWK;\n\n const type = parseKeyType(\n typeStr,\n isRequired,\n keyType,\n isPublic,\n option('type', objName)\n );\n return { format, type };\n}\n\nfunction parseKeyEncoding(\n enc: {\n key: any;\n type?: string;\n encoding?: string;\n format?: string;\n cipher?: string;\n passphrase?: string;\n },\n keyType: string | undefined,\n isPublic: boolean | undefined,\n objName?: string | undefined\n) {\n // validateObject(enc, 'options');\n\n const isInput = keyType === undefined;\n\n const { format, type } = parseKeyFormatAndType(\n enc,\n keyType,\n isPublic,\n objName\n );\n\n let cipher, passphrase, encoding;\n if (isPublic !== true) {\n ({ cipher, passphrase, encoding } = enc);\n\n if (!isInput) {\n if (cipher != null) {\n if (typeof cipher !== 'string')\n throw new Error(\n `Invalid argument ${option('cipher', objName)}: ${cipher}`\n );\n if (\n format === KFormatType.kKeyFormatDER &&\n (type === KeyEncoding.kKeyEncodingPKCS1 ||\n type === KeyEncoding.kKeyEncodingSEC1)\n ) {\n throw new Error(\n `Incompatible key options ${encodingNames[type]} does not support encryption`\n );\n }\n } else if (passphrase !== undefined) {\n throw new Error(\n `invalid argument ${option('cipher', objName)}: ${cipher}`\n );\n }\n }\n\n if (\n (isInput && passphrase !== undefined && !isStringOrBuffer(passphrase)) ||\n (!isInput && cipher != null && !isStringOrBuffer(passphrase))\n ) {\n throw new Error(\n `Invalid argument value ${option('passphrase', objName)}: ${passphrase}`\n );\n }\n }\n\n if (passphrase !== undefined)\n passphrase = binaryLikeToArrayBuffer(passphrase, encoding);\n\n return { format, type, cipher, passphrase };\n}\n\nfunction prepareAsymmetricKey(\n key:\n | BinaryLike\n | { key: any; encoding?: string; format?: any; passphrase?: string },\n ctx: KeyInputContext\n): {\n format: KFormatType;\n data: ArrayBuffer;\n type?: any;\n passphrase?: any;\n} {\n // TODO(osp) check, KeyObject some node object\n // if (isKeyObject(key)) {\n // // Best case: A key object, as simple as that.\n // return { data: getKeyObjectHandle(key, ctx) };\n // } else\n // if (isCryptoKey(key)) {\n // return { data: getKeyObjectHandle(key[kKeyObject], ctx) };\n // } else\n if (isStringOrBuffer(key)) {\n // Expect PEM by default, mostly for backward compatibility.\n return {\n format: KFormatType.kKeyFormatPEM,\n data: binaryLikeToArrayBuffer(key),\n };\n } else if (typeof key === 'object') {\n const {\n key: data,\n encoding,\n // format\n } = key;\n // // The 'key' property can be a KeyObject as well to allow specifying\n // // additional options such as padding along with the key.\n // if (isKeyObject(data)) return { data: getKeyObjectHandle(data, ctx) };\n // else if (isCryptoKey(data))\n // return { data: getKeyObjectHandle(data[kKeyObject], ctx) };\n // else if (isJwk(data) && format === 'jwk')\n // return { data: getKeyObjectHandleFromJwk(data, ctx), format: 'jwk' };\n // Either PEM or DER using PKCS#1 or SPKI.\n if (!isStringOrBuffer(data)) {\n throw new Error(\n 'prepareAsymmetricKey: key is not a string or ArrayBuffer'\n );\n }\n\n const isPublic =\n ctx === KeyInputContext.kConsumePrivate ||\n ctx === KeyInputContext.kCreatePrivate\n ? false\n : undefined;\n\n return {\n data: binaryLikeToArrayBuffer(data, encoding),\n ...parseKeyEncoding(key, undefined, isPublic),\n };\n }\n\n throw new Error('[prepareAsymetricKey] Invalid argument key: ${key}');\n}\n\n// TODO(osp) any here is a node KeyObject\nexport function preparePrivateKey(\n key:\n | BinaryLike\n | {\n key: any;\n encoding?: string;\n format?: any;\n padding?: number;\n passphrase?: string;\n }\n) {\n return prepareAsymmetricKey(key, KeyInputContext.kConsumePrivate);\n}\n\n// TODO(osp) any here is a node KeyObject\nexport function preparePublicOrPrivateKey(\n key:\n | BinaryLike\n | { key: any; encoding?: string; format?: any; padding?: number }\n) {\n return prepareAsymmetricKey(key, KeyInputContext.kConsumePublic);\n}\n\n// Parses the public key encoding based on an object. keyType must be undefined\n// when this is used to parse an input encoding and must be a valid key type if\n// used to parse an output encoding.\nexport function parsePublicKeyEncoding(\n enc: {\n key: any;\n encoding?: string;\n format?: string;\n cipher?: string;\n passphrase?: string;\n },\n keyType: string | undefined,\n objName?: string\n) {\n return parseKeyEncoding(enc, keyType, keyType ? true : undefined, objName);\n}\n\n// Parses the private key encoding based on an object. keyType must be undefined\n// when this is used to parse an input encoding and must be a valid key type if\n// used to parse an output encoding.\nexport function parsePrivateKeyEncoding(\n enc: {\n key: any;\n encoding?: string;\n format?: string;\n cipher?: string;\n passphrase?: string;\n },\n keyType: string | undefined,\n objName?: string\n) {\n return parseKeyEncoding(enc, keyType, false, objName);\n}\n"]}
|