1 |
|
|
#include "crypto/crypto_keygen.h" |
2 |
|
|
#include "async_wrap-inl.h" |
3 |
|
|
#include "base_object-inl.h" |
4 |
|
|
#include "debug_utils-inl.h" |
5 |
|
|
#include "env-inl.h" |
6 |
|
|
#include "memory_tracker-inl.h" |
7 |
|
|
#include "threadpoolwork-inl.h" |
8 |
|
|
#include "v8.h" |
9 |
|
|
|
10 |
|
|
#include <cmath> |
11 |
|
|
|
12 |
|
|
namespace node { |
13 |
|
|
|
14 |
|
|
using v8::FunctionCallbackInfo; |
15 |
|
|
using v8::Int32; |
16 |
|
|
using v8::Just; |
17 |
|
|
using v8::Local; |
18 |
|
|
using v8::Maybe; |
19 |
|
|
using v8::Nothing; |
20 |
|
|
using v8::Object; |
21 |
|
|
using v8::Uint32; |
22 |
|
|
using v8::Value; |
23 |
|
|
|
24 |
|
|
namespace crypto { |
25 |
|
|
// NidKeyPairGenJob input arguments: |
26 |
|
|
// 1. CryptoJobMode |
27 |
|
|
// 2. NID |
28 |
|
|
// 3. Public Format |
29 |
|
|
// 4. Public Type |
30 |
|
|
// 5. Private Format |
31 |
|
|
// 6. Private Type |
32 |
|
|
// 7. Cipher |
33 |
|
|
// 8. Passphrase |
34 |
|
130 |
Maybe<bool> NidKeyPairGenTraits::AdditionalConfig( |
35 |
|
|
CryptoJobMode mode, |
36 |
|
|
const FunctionCallbackInfo<Value>& args, |
37 |
|
|
unsigned int* offset, |
38 |
|
|
NidKeyPairGenConfig* params) { |
39 |
✓✗✗✓
|
260 |
CHECK(args[*offset]->IsInt32()); |
40 |
✓✗ |
390 |
params->params.id = args[*offset].As<Int32>()->Value(); |
41 |
|
|
|
42 |
|
130 |
*offset += 1; |
43 |
|
|
|
44 |
|
130 |
return Just(true); |
45 |
|
|
} |
46 |
|
|
|
47 |
|
130 |
EVPKeyCtxPointer NidKeyPairGenTraits::Setup(NidKeyPairGenConfig* params) { |
48 |
|
|
EVPKeyCtxPointer ctx = |
49 |
|
260 |
EVPKeyCtxPointer(EVP_PKEY_CTX_new_id(params->params.id, nullptr)); |
50 |
✓✗✗✓ ✗✓ |
130 |
if (!ctx || EVP_PKEY_keygen_init(ctx.get()) <= 0) |
51 |
|
|
return EVPKeyCtxPointer(); |
52 |
|
|
|
53 |
|
130 |
return ctx; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
void SecretKeyGenConfig::MemoryInfo(MemoryTracker* tracker) const { |
57 |
|
|
if (out) tracker->TrackFieldWithSize("out", length); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
1195 |
Maybe<bool> SecretKeyGenTraits::AdditionalConfig( |
61 |
|
|
CryptoJobMode mode, |
62 |
|
|
const FunctionCallbackInfo<Value>& args, |
63 |
|
|
unsigned int* offset, |
64 |
|
|
SecretKeyGenConfig* params) { |
65 |
|
1195 |
Environment* env = Environment::GetCurrent(args); |
66 |
✓✗✗✓
|
2390 |
CHECK(args[*offset]->IsUint32()); |
67 |
✓✗ |
3585 |
params->length = args[*offset].As<Uint32>()->Value() / CHAR_BIT; |
68 |
✗✓ |
1195 |
if (params->length > INT_MAX) { |
69 |
|
|
THROW_ERR_OUT_OF_RANGE(env, |
70 |
|
|
"length must be less than or equal to %u bits", |
71 |
|
|
static_cast<uint64_t>(INT_MAX) * CHAR_BIT); |
72 |
|
|
return Nothing<bool>(); |
73 |
|
|
} |
74 |
|
1195 |
*offset += 1; |
75 |
|
1195 |
return Just(true); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
1195 |
KeyGenJobStatus SecretKeyGenTraits::DoKeyGen( |
79 |
|
|
Environment* env, |
80 |
|
|
SecretKeyGenConfig* params) { |
81 |
✗✓ |
1195 |
CHECK_LE(params->length, INT_MAX); |
82 |
|
1195 |
ByteSource::Builder bytes(params->length); |
83 |
|
1195 |
EntropySource(bytes.data<unsigned char>(), params->length); |
84 |
|
1195 |
params->out = std::move(bytes).release(); |
85 |
|
1195 |
return KeyGenJobStatus::OK; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
1195 |
Maybe<bool> SecretKeyGenTraits::EncodeKey(Environment* env, |
89 |
|
|
SecretKeyGenConfig* params, |
90 |
|
|
Local<Value>* result) { |
91 |
|
|
std::shared_ptr<KeyObjectData> data = |
92 |
|
1195 |
KeyObjectData::CreateSecret(std::move(params->out)); |
93 |
|
2390 |
return Just(KeyObjectHandle::Create(env, data).ToLocal(result)); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
namespace Keygen { |
97 |
|
788 |
void Initialize(Environment* env, Local<Object> target) { |
98 |
|
788 |
NidKeyPairGenJob::Initialize(env, target); |
99 |
|
788 |
SecretKeyGenJob::Initialize(env, target); |
100 |
|
788 |
} |
101 |
|
|
|
102 |
|
5528 |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
103 |
|
5528 |
NidKeyPairGenJob::RegisterExternalReferences(registry); |
104 |
|
5528 |
SecretKeyGenJob::RegisterExternalReferences(registry); |
105 |
|
5528 |
} |
106 |
|
|
|
107 |
|
|
} // namespace Keygen |
108 |
|
|
} // namespace crypto |
109 |
|
|
} // namespace node |