GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "crypto/crypto_dh.h" |
||
2 |
#include "crypto/crypto_keys.h" |
||
3 |
#include "crypto/crypto_groups.h" |
||
4 |
#include "allocated_buffer-inl.h" |
||
5 |
#include "async_wrap-inl.h" |
||
6 |
#include "base_object-inl.h" |
||
7 |
#include "env-inl.h" |
||
8 |
#include "memory_tracker-inl.h" |
||
9 |
#include "threadpoolwork-inl.h" |
||
10 |
#include "v8.h" |
||
11 |
|||
12 |
#include <variant> |
||
13 |
|||
14 |
namespace node { |
||
15 |
|||
16 |
using v8::ConstructorBehavior; |
||
17 |
using v8::DontDelete; |
||
18 |
using v8::FunctionCallback; |
||
19 |
using v8::FunctionCallbackInfo; |
||
20 |
using v8::FunctionTemplate; |
||
21 |
using v8::HandleScope; |
||
22 |
using v8::Int32; |
||
23 |
using v8::Just; |
||
24 |
using v8::Local; |
||
25 |
using v8::Maybe; |
||
26 |
using v8::Nothing; |
||
27 |
using v8::Object; |
||
28 |
using v8::PropertyAttribute; |
||
29 |
using v8::ReadOnly; |
||
30 |
using v8::SideEffectType; |
||
31 |
using v8::Signature; |
||
32 |
using v8::String; |
||
33 |
using v8::Uint8Array; |
||
34 |
using v8::Value; |
||
35 |
|||
36 |
namespace crypto { |
||
37 |
namespace { |
||
38 |
59 |
static void ZeroPadDiffieHellmanSecret(size_t remainder_size, |
|
39 |
char* data, |
||
40 |
size_t length) { |
||
41 |
// DH_size returns number of bytes in a prime number. |
||
42 |
// DH_compute_key returns number of bytes in a remainder of exponent, which |
||
43 |
// may have less bytes than a prime number. Therefore add 0-padding to the |
||
44 |
// allocated buffer. |
||
45 |
59 |
const size_t prime_size = length; |
|
46 |
✓✓ | 59 |
if (remainder_size != prime_size) { |
47 |
✗✓ | 2 |
CHECK_LT(remainder_size, prime_size); |
48 |
2 |
const size_t padding = prime_size - remainder_size; |
|
49 |
2 |
memmove(data + padding, data, remainder_size); |
|
50 |
2 |
memset(data, 0, padding); |
|
51 |
} |
||
52 |
59 |
} |
|
53 |
41 |
static void ZeroPadDiffieHellmanSecret(size_t remainder_size, |
|
54 |
AllocatedBuffer* ret) { |
||
55 |
41 |
ZeroPadDiffieHellmanSecret(remainder_size, ret->data(), ret->size()); |
|
56 |
41 |
} |
|
57 |
} // namespace |
||
58 |
|||
59 |
60 |
DiffieHellman::DiffieHellman(Environment* env, Local<Object> wrap) |
|
60 |
60 |
: BaseObject(env, wrap), verifyError_(0) { |
|
61 |
60 |
MakeWeak(); |
|
62 |
60 |
} |
|
63 |
|||
64 |
4383 |
void DiffieHellman::Initialize(Environment* env, Local<Object> target) { |
|
65 |
8766 |
auto make = [&] (Local<String> name, FunctionCallback callback) { |
|
66 |
17532 |
Local<FunctionTemplate> t = env->NewFunctionTemplate(callback); |
|
67 |
|||
68 |
8766 |
const PropertyAttribute attributes = |
|
69 |
static_cast<PropertyAttribute>(ReadOnly | DontDelete); |
||
70 |
|||
71 |
17532 |
t->InstanceTemplate()->SetInternalFieldCount( |
|
72 |
DiffieHellman::kInternalFieldCount); |
||
73 |
8766 |
t->Inherit(BaseObject::GetConstructorTemplate(env)); |
|
74 |
|||
75 |
8766 |
env->SetProtoMethod(t, "generateKeys", GenerateKeys); |
|
76 |
8766 |
env->SetProtoMethod(t, "computeSecret", ComputeSecret); |
|
77 |
8766 |
env->SetProtoMethodNoSideEffect(t, "getPrime", GetPrime); |
|
78 |
8766 |
env->SetProtoMethodNoSideEffect(t, "getGenerator", GetGenerator); |
|
79 |
8766 |
env->SetProtoMethodNoSideEffect(t, "getPublicKey", GetPublicKey); |
|
80 |
8766 |
env->SetProtoMethodNoSideEffect(t, "getPrivateKey", GetPrivateKey); |
|
81 |
8766 |
env->SetProtoMethod(t, "setPublicKey", SetPublicKey); |
|
82 |
8766 |
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey); |
|
83 |
|||
84 |
Local<FunctionTemplate> verify_error_getter_templ = |
||
85 |
FunctionTemplate::New(env->isolate(), |
||
86 |
DiffieHellman::VerifyErrorGetter, |
||
87 |
Local<Value>(), |
||
88 |
Signature::New(env->isolate(), t), |
||
89 |
/* length */ 0, |
||
90 |
ConstructorBehavior::kThrow, |
||
91 |
8766 |
SideEffectType::kHasNoSideEffect); |
|
92 |
|||
93 |
35064 |
t->InstanceTemplate()->SetAccessorProperty( |
|
94 |
env->verify_error_string(), |
||
95 |
verify_error_getter_templ, |
||
96 |
Local<FunctionTemplate>(), |
||
97 |
attributes); |
||
98 |
|||
99 |
8766 |
env->SetConstructorFunction(target, name, t); |
|
100 |
13149 |
}; |
|
101 |
|||
102 |
4383 |
make(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellman"), New); |
|
103 |
4383 |
make(FIXED_ONE_BYTE_STRING(env->isolate(), "DiffieHellmanGroup"), |
|
104 |
DiffieHellmanGroup); |
||
105 |
|||
106 |
4383 |
env->SetMethodNoSideEffect(target, "statelessDH", DiffieHellman::Stateless); |
|
107 |
4383 |
DHKeyPairGenJob::Initialize(env, target); |
|
108 |
4383 |
DHKeyExportJob::Initialize(env, target); |
|
109 |
4383 |
DHBitsJob::Initialize(env, target); |
|
110 |
4383 |
} |
|
111 |
|||
112 |
4941 |
void DiffieHellman::RegisterExternalReferences( |
|
113 |
ExternalReferenceRegistry* registry) { |
||
114 |
4941 |
registry->Register(New); |
|
115 |
4941 |
registry->Register(DiffieHellmanGroup); |
|
116 |
|||
117 |
4941 |
registry->Register(GenerateKeys); |
|
118 |
4941 |
registry->Register(ComputeSecret); |
|
119 |
4941 |
registry->Register(GetPrime); |
|
120 |
4941 |
registry->Register(GetGenerator); |
|
121 |
4941 |
registry->Register(GetPublicKey); |
|
122 |
4941 |
registry->Register(GetPrivateKey); |
|
123 |
4941 |
registry->Register(SetPublicKey); |
|
124 |
4941 |
registry->Register(SetPrivateKey); |
|
125 |
|||
126 |
4941 |
registry->Register(DiffieHellman::VerifyErrorGetter); |
|
127 |
4941 |
registry->Register(DiffieHellman::Stateless); |
|
128 |
|||
129 |
4941 |
DHKeyPairGenJob::RegisterExternalReferences(registry); |
|
130 |
4941 |
DHKeyExportJob::RegisterExternalReferences(registry); |
|
131 |
4941 |
DHBitsJob::RegisterExternalReferences(registry); |
|
132 |
4941 |
} |
|
133 |
|||
134 |
10 |
bool DiffieHellman::Init(int primeLength, int g) { |
|
135 |
10 |
dh_.reset(DH_new()); |
|
136 |
✓✓ | 10 |
if (!DH_generate_parameters_ex(dh_.get(), primeLength, g, nullptr)) |
137 |
3 |
return false; |
|
138 |
7 |
return VerifyContext(); |
|
139 |
} |
||
140 |
|||
141 |
void DiffieHellman::MemoryInfo(MemoryTracker* tracker) const { |
||
142 |
tracker->TrackFieldWithSize("dh", dh_ ? kSizeOf_DH : 0); |
||
143 |
} |
||
144 |
|||
145 |
28 |
bool DiffieHellman::Init(const char* p, int p_len, int g) { |
|
146 |
28 |
dh_.reset(DH_new()); |
|
147 |
✗✓ | 28 |
if (p_len <= 0) { |
148 |
ERR_put_error(ERR_LIB_BN, BN_F_BN_GENERATE_PRIME_EX, |
||
149 |
BN_R_BITS_TOO_SMALL, __FILE__, __LINE__); |
||
150 |
return false; |
||
151 |
} |
||
152 |
✓✓ | 28 |
if (g <= 1) { |
153 |
4 |
ERR_put_error(ERR_LIB_DH, DH_F_DH_BUILTIN_GENPARAMS, |
|
154 |
DH_R_BAD_GENERATOR, __FILE__, __LINE__); |
||
155 |
4 |
return false; |
|
156 |
} |
||
157 |
BIGNUM* bn_p = |
||
158 |
24 |
BN_bin2bn(reinterpret_cast<const unsigned char*>(p), p_len, nullptr); |
|
159 |
24 |
BIGNUM* bn_g = BN_new(); |
|
160 |
✓✗✗✓ ✗✓ |
48 |
if (!BN_set_word(bn_g, g) || |
161 |
24 |
!DH_set0_pqg(dh_.get(), bn_p, nullptr, bn_g)) { |
|
162 |
BN_free(bn_p); |
||
163 |
BN_free(bn_g); |
||
164 |
return false; |
||
165 |
} |
||
166 |
24 |
return VerifyContext(); |
|
167 |
} |
||
168 |
|||
169 |
21 |
bool DiffieHellman::Init(const char* p, int p_len, const char* g, int g_len) { |
|
170 |
21 |
dh_.reset(DH_new()); |
|
171 |
✗✓ | 21 |
if (p_len <= 0) { |
172 |
ERR_put_error(ERR_LIB_BN, BN_F_BN_GENERATE_PRIME_EX, |
||
173 |
BN_R_BITS_TOO_SMALL, __FILE__, __LINE__); |
||
174 |
return false; |
||
175 |
} |
||
176 |
✓✓ | 21 |
if (g_len <= 0) { |
177 |
2 |
ERR_put_error(ERR_LIB_DH, DH_F_DH_BUILTIN_GENPARAMS, |
|
178 |
DH_R_BAD_GENERATOR, __FILE__, __LINE__); |
||
179 |
2 |
return false; |
|
180 |
} |
||
181 |
BIGNUM* bn_g = |
||
182 |
19 |
BN_bin2bn(reinterpret_cast<const unsigned char*>(g), g_len, nullptr); |
|
183 |
✓✓✓✓ ✓✓ |
19 |
if (BN_is_zero(bn_g) || BN_is_one(bn_g)) { |
184 |
4 |
BN_free(bn_g); |
|
185 |
4 |
ERR_put_error(ERR_LIB_DH, DH_F_DH_BUILTIN_GENPARAMS, |
|
186 |
DH_R_BAD_GENERATOR, __FILE__, __LINE__); |
||
187 |
4 |
return false; |
|
188 |
} |
||
189 |
BIGNUM* bn_p = |
||
190 |
15 |
BN_bin2bn(reinterpret_cast<const unsigned char*>(p), p_len, nullptr); |
|
191 |
✗✓ | 15 |
if (!DH_set0_pqg(dh_.get(), bn_p, nullptr, bn_g)) { |
192 |
BN_free(bn_p); |
||
193 |
BN_free(bn_g); |
||
194 |
return false; |
||
195 |
} |
||
196 |
15 |
return VerifyContext(); |
|
197 |
} |
||
198 |
|||
199 |
18 |
inline const modp_group* FindDiffieHellmanGroup(const char* name) { |
|
200 |
✓✓ | 68 |
for (const modp_group& group : modp_groups) { |
201 |
✓✓ | 66 |
if (StringEqualNoCase(name, group.name)) |
202 |
16 |
return &group; |
|
203 |
} |
||
204 |
2 |
return nullptr; |
|
205 |
} |
||
206 |
|||
207 |
11 |
void DiffieHellman::DiffieHellmanGroup( |
|
208 |
const FunctionCallbackInfo<Value>& args) { |
||
209 |
11 |
Environment* env = Environment::GetCurrent(args); |
|
210 |
11 |
DiffieHellman* diffieHellman = new DiffieHellman(env, args.This()); |
|
211 |
|||
212 |
✗✓ | 11 |
CHECK_EQ(args.Length(), 1); |
213 |
✗✓ | 23 |
THROW_AND_RETURN_IF_NOT_STRING(env, args[0], "Group name"); |
214 |
|||
215 |
✓✗ | 11 |
bool initialized = false; |
216 |
|||
217 |
11 |
const node::Utf8Value group_name(env->isolate(), args[0]); |
|
218 |
11 |
const modp_group* group = FindDiffieHellmanGroup(*group_name); |
|
219 |
✓✓ | 11 |
if (group == nullptr) |
220 |
1 |
return THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env); |
|
221 |
|||
222 |
20 |
initialized = diffieHellman->Init(group->prime, |
|
223 |
10 |
group->prime_size, |
|
224 |
10 |
group->gen); |
|
225 |
✗✓ | 10 |
if (!initialized) |
226 |
THROW_ERR_CRYPTO_INITIALIZATION_FAILED(env); |
||
227 |
} |
||
228 |
|||
229 |
|||
230 |
49 |
void DiffieHellman::New(const FunctionCallbackInfo<Value>& args) { |
|
231 |
49 |
Environment* env = Environment::GetCurrent(args); |
|
232 |
DiffieHellman* diffieHellman = |
||
233 |
49 |
new DiffieHellman(env, args.This()); |
|
234 |
49 |
bool initialized = false; |
|
235 |
|||
236 |
✓✗ | 49 |
if (args.Length() == 2) { |
237 |
✓✓ | 49 |
if (args[0]->IsInt32()) { |
238 |
✓✗ | 10 |
if (args[1]->IsInt32()) { |
239 |
30 |
initialized = diffieHellman->Init(args[0].As<Int32>()->Value(), |
|
240 |
30 |
args[1].As<Int32>()->Value()); |
|
241 |
} |
||
242 |
} else { |
||
243 |
39 |
ArrayBufferOrViewContents<char> arg0(args[0]); |
|
244 |
✗✓ | 39 |
if (UNLIKELY(!arg0.CheckSizeInt32())) |
245 |
return THROW_ERR_OUT_OF_RANGE(env, "prime is too big"); |
||
246 |
✓✓ | 39 |
if (args[1]->IsInt32()) { |
247 |
36 |
initialized = diffieHellman->Init(arg0.data(), |
|
248 |
18 |
arg0.size(), |
|
249 |
54 |
args[1].As<Int32>()->Value()); |
|
250 |
} else { |
||
251 |
21 |
ArrayBufferOrViewContents<char> arg1(args[1]); |
|
252 |
✗✓ | 21 |
if (UNLIKELY(!arg1.CheckSizeInt32())) |
253 |
return THROW_ERR_OUT_OF_RANGE(env, "generator is too big"); |
||
254 |
21 |
initialized = diffieHellman->Init(arg0.data(), arg0.size(), |
|
255 |
21 |
arg1.data(), arg1.size()); |
|
256 |
} |
||
257 |
} |
||
258 |
} |
||
259 |
|||
260 |
✓✓ | 49 |
if (!initialized) { |
261 |
13 |
return ThrowCryptoError(env, ERR_get_error(), "Initialization failed"); |
|
262 |
} |
||
263 |
} |
||
264 |
|||
265 |
|||
266 |
36 |
void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) { |
|
267 |
36 |
Environment* env = Environment::GetCurrent(args); |
|
268 |
|||
269 |
DiffieHellman* diffieHellman; |
||
270 |
✗✓ | 36 |
ASSIGN_OR_RETURN_UNWRAP(&diffieHellman, args.Holder()); |
271 |
|||
272 |
✗✓ | 36 |
if (!DH_generate_key(diffieHellman->dh_.get())) { |
273 |
return ThrowCryptoError(env, ERR_get_error(), "Key generation failed"); |
||
274 |
} |
||
275 |
|||
276 |
const BIGNUM* pub_key; |
||
277 |
36 |
DH_get0_key(diffieHellman->dh_.get(), &pub_key, nullptr); |
|
278 |
36 |
const int size = BN_num_bytes(pub_key); |
|
279 |
✗✓ | 36 |
CHECK_GE(size, 0); |
280 |
36 |
AllocatedBuffer data = AllocatedBuffer::AllocateManaged(env, size); |
|
281 |
✗✓ | 36 |
CHECK_EQ(size, |
282 |
BN_bn2binpad( |
||
283 |
pub_key, reinterpret_cast<unsigned char*>(data.data()), size)); |
||
284 |
108 |
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>())); |
|
285 |
} |
||
286 |
|||
287 |
|||
288 |
63 |
void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args, |
|
289 |
const BIGNUM* (*get_field)(const DH*), |
||
290 |
const char* err_if_null) { |
||
291 |
63 |
Environment* env = Environment::GetCurrent(args); |
|
292 |
|||
293 |
DiffieHellman* dh; |
||
294 |
✗✓ | 63 |
ASSIGN_OR_RETURN_UNWRAP(&dh, args.Holder()); |
295 |
|||
296 |
63 |
const BIGNUM* num = get_field(dh->dh_.get()); |
|
297 |
✗✓ | 63 |
if (num == nullptr) |
298 |
return THROW_ERR_CRYPTO_INVALID_STATE(env, err_if_null); |
||
299 |
|||
300 |
63 |
const int size = BN_num_bytes(num); |
|
301 |
✗✓ | 63 |
CHECK_GE(size, 0); |
302 |
63 |
AllocatedBuffer data = AllocatedBuffer::AllocateManaged(env, size); |
|
303 |
✗✓ | 63 |
CHECK_EQ( |
304 |
size, |
||
305 |
BN_bn2binpad(num, reinterpret_cast<unsigned char*>(data.data()), size)); |
||
306 |
189 |
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>())); |
|
307 |
} |
||
308 |
|||
309 |
13 |
void DiffieHellman::GetPrime(const FunctionCallbackInfo<Value>& args) { |
|
310 |
13 |
GetField(args, [](const DH* dh) -> const BIGNUM* { |
|
311 |
const BIGNUM* p; |
||
312 |
13 |
DH_get0_pqg(dh, &p, nullptr, nullptr); |
|
313 |
13 |
return p; |
|
314 |
}, "p is null"); |
||
315 |
13 |
} |
|
316 |
|||
317 |
7 |
void DiffieHellman::GetGenerator(const FunctionCallbackInfo<Value>& args) { |
|
318 |
7 |
GetField(args, [](const DH* dh) -> const BIGNUM* { |
|
319 |
const BIGNUM* g; |
||
320 |
7 |
DH_get0_pqg(dh, nullptr, nullptr, &g); |
|
321 |
7 |
return g; |
|
322 |
}, "g is null"); |
||
323 |
7 |
} |
|
324 |
|||
325 |
34 |
void DiffieHellman::GetPublicKey(const FunctionCallbackInfo<Value>& args) { |
|
326 |
34 |
GetField(args, [](const DH* dh) -> const BIGNUM* { |
|
327 |
const BIGNUM* pub_key; |
||
328 |
34 |
DH_get0_key(dh, &pub_key, nullptr); |
|
329 |
34 |
return pub_key; |
|
330 |
}, "No public key - did you forget to generate one?"); |
||
331 |
34 |
} |
|
332 |
|||
333 |
9 |
void DiffieHellman::GetPrivateKey(const FunctionCallbackInfo<Value>& args) { |
|
334 |
9 |
GetField(args, [](const DH* dh) -> const BIGNUM* { |
|
335 |
const BIGNUM* priv_key; |
||
336 |
9 |
DH_get0_key(dh, nullptr, &priv_key); |
|
337 |
9 |
return priv_key; |
|
338 |
}, "No private key - did you forget to generate one?"); |
||
339 |
9 |
} |
|
340 |
|||
341 |
42 |
void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) { |
|
342 |
42 |
Environment* env = Environment::GetCurrent(args); |
|
343 |
|||
344 |
DiffieHellman* diffieHellman; |
||
345 |
✗✓ | 43 |
ASSIGN_OR_RETURN_UNWRAP(&diffieHellman, args.Holder()); |
346 |
|||
347 |
ClearErrorOnReturn clear_error_on_return; |
||
348 |
|||
349 |
✗✓ | 42 |
CHECK_EQ(args.Length(), 1); |
350 |
42 |
ArrayBufferOrViewContents<unsigned char> key_buf(args[0]); |
|
351 |
✗✓ | 42 |
if (UNLIKELY(!key_buf.CheckSizeInt32())) |
352 |
return THROW_ERR_OUT_OF_RANGE(env, "secret is too big"); |
||
353 |
42 |
BignumPointer key(BN_bin2bn(key_buf.data(), key_buf.size(), nullptr)); |
|
354 |
|||
355 |
AllocatedBuffer ret = |
||
356 |
42 |
AllocatedBuffer::AllocateManaged(env, DH_size(diffieHellman->dh_.get())); |
|
357 |
|||
358 |
42 |
int size = DH_compute_key(reinterpret_cast<unsigned char*>(ret.data()), |
|
359 |
42 |
key.get(), |
|
360 |
42 |
diffieHellman->dh_.get()); |
|
361 |
|||
362 |
✓✓ | 42 |
if (size == -1) { |
363 |
int checkResult; |
||
364 |
int checked; |
||
365 |
|||
366 |
1 |
checked = DH_check_pub_key(diffieHellman->dh_.get(), |
|
367 |
1 |
key.get(), |
|
368 |
&checkResult); |
||
369 |
|||
370 |
✓✗ | 1 |
if (!checked) { |
371 |
1 |
return ThrowCryptoError(env, ERR_get_error(), "Invalid Key"); |
|
372 |
} else if (checkResult) { |
||
373 |
if (checkResult & DH_CHECK_PUBKEY_TOO_SMALL) { |
||
374 |
return THROW_ERR_CRYPTO_INVALID_KEYLEN(env, |
||
375 |
"Supplied key is too small"); |
||
376 |
} else if (checkResult & DH_CHECK_PUBKEY_TOO_LARGE) { |
||
377 |
return THROW_ERR_CRYPTO_INVALID_KEYLEN(env, |
||
378 |
"Supplied key is too large"); |
||
379 |
} |
||
380 |
} |
||
381 |
|||
382 |
return THROW_ERR_CRYPTO_INVALID_KEYTYPE(env); |
||
383 |
} |
||
384 |
|||
385 |
✗✓ | 41 |
CHECK_GE(size, 0); |
386 |
41 |
ZeroPadDiffieHellmanSecret(static_cast<size_t>(size), &ret); |
|
387 |
|||
388 |
123 |
args.GetReturnValue().Set(ret.ToBuffer().FromMaybe(Local<Value>())); |
|
389 |
} |
||
390 |
|||
391 |
100009 |
void DiffieHellman::SetKey(const FunctionCallbackInfo<Value>& args, |
|
392 |
int (*set_field)(DH*, BIGNUM*), const char* what) { |
||
393 |
100009 |
Environment* env = Environment::GetCurrent(args); |
|
394 |
DiffieHellman* dh; |
||
395 |
✗✓ | 100009 |
ASSIGN_OR_RETURN_UNWRAP(&dh, args.Holder()); |
396 |
✗✓ | 100009 |
CHECK_EQ(args.Length(), 1); |
397 |
100009 |
ArrayBufferOrViewContents<unsigned char> buf(args[0]); |
|
398 |
✗✓ | 100009 |
if (UNLIKELY(!buf.CheckSizeInt32())) |
399 |
return THROW_ERR_OUT_OF_RANGE(env, "buf is too big"); |
||
400 |
100009 |
BIGNUM* num = BN_bin2bn(buf.data(), buf.size(), nullptr); |
|
401 |
✗✓ | 100009 |
CHECK_NOT_NULL(num); |
402 |
✗✓ | 100009 |
CHECK_EQ(1, set_field(dh->dh_.get(), num)); |
403 |
} |
||
404 |
|||
405 |
50003 |
void DiffieHellman::SetPublicKey(const FunctionCallbackInfo<Value>& args) { |
|
406 |
50003 |
SetKey(args, |
|
407 |
50003 |
[](DH* dh, BIGNUM* num) { return DH_set0_key(dh, num, nullptr); }, |
|
408 |
"Public key"); |
||
409 |
50003 |
} |
|
410 |
|||
411 |
50006 |
void DiffieHellman::SetPrivateKey(const FunctionCallbackInfo<Value>& args) { |
|
412 |
50006 |
SetKey(args, |
|
413 |
50006 |
[](DH* dh, BIGNUM* num) { return DH_set0_key(dh, nullptr, num); }, |
|
414 |
"Private key"); |
||
415 |
50006 |
} |
|
416 |
|||
417 |
46 |
void DiffieHellman::VerifyErrorGetter(const FunctionCallbackInfo<Value>& args) { |
|
418 |
46 |
HandleScope scope(args.GetIsolate()); |
|
419 |
|||
420 |
DiffieHellman* diffieHellman; |
||
421 |
✗✓ | 46 |
ASSIGN_OR_RETURN_UNWRAP(&diffieHellman, args.Holder()); |
422 |
|||
423 |
92 |
args.GetReturnValue().Set(diffieHellman->verifyError_); |
|
424 |
} |
||
425 |
|||
426 |
46 |
bool DiffieHellman::VerifyContext() { |
|
427 |
int codes; |
||
428 |
✗✓ | 46 |
if (!DH_check(dh_.get(), &codes)) |
429 |
return false; |
||
430 |
46 |
verifyError_ = codes; |
|
431 |
46 |
return true; |
|
432 |
} |
||
433 |
|||
434 |
// The input arguments to DhKeyPairGenJob can vary |
||
435 |
// 1. CryptoJobMode |
||
436 |
// and either |
||
437 |
// 2. Group name (as a string) |
||
438 |
// or |
||
439 |
// 2. Prime or Prime Length |
||
440 |
// 3. Generator |
||
441 |
// Followed by the public and private key encoding parameters: |
||
442 |
// * Public format |
||
443 |
// * Public type |
||
444 |
// * Private format |
||
445 |
// * Private type |
||
446 |
// * Cipher |
||
447 |
// * Passphrase |
||
448 |
9 |
Maybe<bool> DhKeyGenTraits::AdditionalConfig( |
|
449 |
CryptoJobMode mode, |
||
450 |
const FunctionCallbackInfo<Value>& args, |
||
451 |
unsigned int* offset, |
||
452 |
DhKeyPairGenConfig* params) { |
||
453 |
9 |
Environment* env = Environment::GetCurrent(args); |
|
454 |
|||
455 |
✓✗✓✓ |
27 |
if (args[*offset]->IsString()) { |
456 |
✓✗ | 14 |
Utf8Value group_name(env->isolate(), args[*offset]); |
457 |
7 |
const modp_group* group = FindDiffieHellmanGroup(*group_name); |
|
458 |
✓✓ | 7 |
if (group == nullptr) { |
459 |
1 |
THROW_ERR_CRYPTO_UNKNOWN_DH_GROUP(env); |
|
460 |
1 |
return Nothing<bool>(); |
|
461 |
} |
||
462 |
|||
463 |
12 |
params->params.prime = BignumPointer( |
|
464 |
6 |
BN_bin2bn(reinterpret_cast<const unsigned char*>(group->prime), |
|
465 |
12 |
group->prime_size, nullptr)); |
|
466 |
6 |
params->params.generator = group->gen; |
|
467 |
6 |
*offset += 1; |
|
468 |
} else { |
||
469 |
✓✗✓✓ |
4 |
if (args[*offset]->IsInt32()) { |
470 |
✓✗ | 3 |
int size = args[*offset].As<Int32>()->Value(); |
471 |
✗✓ | 1 |
if (size < 0) { |
472 |
THROW_ERR_OUT_OF_RANGE(env, "Invalid prime size"); |
||
473 |
return Nothing<bool>(); |
||
474 |
} |
||
475 |
1 |
params->params.prime = size; |
|
476 |
} else { |
||
477 |
✓✗ | 2 |
ArrayBufferOrViewContents<unsigned char> input(args[*offset]); |
478 |
✗✓ | 1 |
if (UNLIKELY(!input.CheckSizeInt32())) { |
479 |
THROW_ERR_OUT_OF_RANGE(env, "prime is too big"); |
||
480 |
return Nothing<bool>(); |
||
481 |
} |
||
482 |
2 |
params->params.prime = BignumPointer( |
|
483 |
2 |
BN_bin2bn(input.data(), input.size(), nullptr)); |
|
484 |
} |
||
485 |
|||
486 |
✓✗✗✓ |
4 |
CHECK(args[*offset + 1]->IsInt32()); |
487 |
✓✗ | 6 |
params->params.generator = args[*offset + 1].As<Int32>()->Value(); |
488 |
2 |
*offset += 2; |
|
489 |
} |
||
490 |
|||
491 |
8 |
return Just(true); |
|
492 |
} |
||
493 |
|||
494 |
8 |
EVPKeyCtxPointer DhKeyGenTraits::Setup(DhKeyPairGenConfig* params) { |
|
495 |
8 |
EVPKeyPointer key_params; |
|
496 |
✓✓ | 8 |
if (BignumPointer* prime_fixed_value = |
497 |
8 |
std::get_if<BignumPointer>(¶ms->params.prime)) { |
|
498 |
7 |
DHPointer dh(DH_new()); |
|
499 |
✗✓ | 7 |
if (!dh) |
500 |
return EVPKeyCtxPointer(); |
||
501 |
|||
502 |
7 |
BIGNUM* prime = prime_fixed_value->get(); |
|
503 |
7 |
BignumPointer bn_g(BN_new()); |
|
504 |
✓✗✗✓ ✗✓ |
14 |
if (!BN_set_word(bn_g.get(), params->params.generator) || |
505 |
7 |
!DH_set0_pqg(dh.get(), prime, nullptr, bn_g.get())) { |
|
506 |
return EVPKeyCtxPointer(); |
||
507 |
} |
||
508 |
|||
509 |
7 |
prime_fixed_value->release(); |
|
510 |
7 |
bn_g.release(); |
|
511 |
|||
512 |
7 |
key_params = EVPKeyPointer(EVP_PKEY_new()); |
|
513 |
✗✓ | 7 |
CHECK(key_params); |
514 |
✗✓ | 7 |
CHECK_EQ(EVP_PKEY_assign_DH(key_params.get(), dh.release()), 1); |
515 |
✓✗ | 1 |
} else if (int* prime_size = std::get_if<int>(¶ms->params.prime)) { |
516 |
1 |
EVPKeyCtxPointer param_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_DH, nullptr)); |
|
517 |
1 |
EVP_PKEY* raw_params = nullptr; |
|
518 |
✓✗ | 2 |
if (!param_ctx || |
519 |
✓✗ | 2 |
EVP_PKEY_paramgen_init(param_ctx.get()) <= 0 || |
520 |
1 |
EVP_PKEY_CTX_set_dh_paramgen_prime_len( |
|
521 |
param_ctx.get(), |
||
522 |
✓✗ | 1 |
*prime_size) <= 0 || |
523 |
1 |
EVP_PKEY_CTX_set_dh_paramgen_generator( |
|
524 |
param_ctx.get(), |
||
525 |
✓✗✗✓ ✗✓ |
3 |
params->params.generator) <= 0 || |
526 |
1 |
EVP_PKEY_paramgen(param_ctx.get(), &raw_params) <= 0) { |
|
527 |
return EVPKeyCtxPointer(); |
||
528 |
} |
||
529 |
|||
530 |
1 |
key_params = EVPKeyPointer(raw_params); |
|
531 |
} else { |
||
532 |
UNREACHABLE(); |
||
533 |
} |
||
534 |
|||
535 |
16 |
EVPKeyCtxPointer ctx(EVP_PKEY_CTX_new(key_params.get(), nullptr)); |
|
536 |
✓✗✗✓ ✗✓ |
8 |
if (!ctx || EVP_PKEY_keygen_init(ctx.get()) <= 0) |
537 |
return EVPKeyCtxPointer(); |
||
538 |
|||
539 |
8 |
return ctx; |
|
540 |
} |
||
541 |
|||
542 |
Maybe<bool> DHKeyExportTraits::AdditionalConfig( |
||
543 |
const FunctionCallbackInfo<Value>& args, |
||
544 |
unsigned int offset, |
||
545 |
DHKeyExportConfig* params) { |
||
546 |
return Just(true); |
||
547 |
} |
||
548 |
|||
549 |
WebCryptoKeyExportStatus DHKeyExportTraits::DoExport( |
||
550 |
std::shared_ptr<KeyObjectData> key_data, |
||
551 |
WebCryptoKeyFormat format, |
||
552 |
const DHKeyExportConfig& params, |
||
553 |
ByteSource* out) { |
||
554 |
CHECK_NE(key_data->GetKeyType(), kKeyTypeSecret); |
||
555 |
|||
556 |
switch (format) { |
||
557 |
case kWebCryptoKeyFormatPKCS8: |
||
558 |
if (key_data->GetKeyType() != kKeyTypePrivate) |
||
559 |
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE; |
||
560 |
return PKEY_PKCS8_Export(key_data.get(), out); |
||
561 |
case kWebCryptoKeyFormatSPKI: |
||
562 |
if (key_data->GetKeyType() != kKeyTypePublic) |
||
563 |
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE; |
||
564 |
return PKEY_SPKI_Export(key_data.get(), out); |
||
565 |
default: |
||
566 |
UNREACHABLE(); |
||
567 |
} |
||
568 |
} |
||
569 |
|||
570 |
namespace { |
||
571 |
20 |
ByteSource StatelessDiffieHellmanThreadsafe( |
|
572 |
const ManagedEVPPKey& our_key, |
||
573 |
const ManagedEVPPKey& their_key) { |
||
574 |
size_t out_size; |
||
575 |
|||
576 |
40 |
EVPKeyCtxPointer ctx(EVP_PKEY_CTX_new(our_key.get(), nullptr)); |
|
577 |
✓✗ | 40 |
if (!ctx || |
578 |
✓✓ | 40 |
EVP_PKEY_derive_init(ctx.get()) <= 0 || |
579 |
✓✗✗✓ ✓✓ |
60 |
EVP_PKEY_derive_set_peer(ctx.get(), their_key.get()) <= 0 || |
580 |
18 |
EVP_PKEY_derive(ctx.get(), nullptr, &out_size) <= 0) |
|
581 |
2 |
return ByteSource(); |
|
582 |
|||
583 |
18 |
char* buf = MallocOpenSSL<char>(out_size); |
|
584 |
36 |
ByteSource out = ByteSource::Allocated(buf, out_size); |
|
585 |
|||
586 |
18 |
if (EVP_PKEY_derive( |
|
587 |
ctx.get(), |
||
588 |
reinterpret_cast<unsigned char*>(buf), |
||
589 |
✗✓ | 18 |
&out_size) <= 0) { |
590 |
return ByteSource(); |
||
591 |
} |
||
592 |
|||
593 |
18 |
ZeroPadDiffieHellmanSecret(out_size, buf, out.size()); |
|
594 |
18 |
return out; |
|
595 |
} |
||
596 |
} // namespace |
||
597 |
|||
598 |
15 |
void DiffieHellman::Stateless(const FunctionCallbackInfo<Value>& args) { |
|
599 |
15 |
Environment* env = Environment::GetCurrent(args); |
|
600 |
|||
601 |
✓✗✗✓ ✗✓ |
30 |
CHECK(args[0]->IsObject() && args[1]->IsObject()); |
602 |
KeyObjectHandle* our_key_object; |
||
603 |
✗✓ | 32 |
ASSIGN_OR_RETURN_UNWRAP(&our_key_object, args[0].As<Object>()); |
604 |
✗✓ | 15 |
CHECK_EQ(our_key_object->Data()->GetKeyType(), kKeyTypePrivate); |
605 |
KeyObjectHandle* their_key_object; |
||
606 |
✗✓ | 30 |
ASSIGN_OR_RETURN_UNWRAP(&their_key_object, args[1].As<Object>()); |
607 |
✗✓ | 15 |
CHECK_NE(their_key_object->Data()->GetKeyType(), kKeyTypeSecret); |
608 |
|||
609 |
15 |
ManagedEVPPKey our_key = our_key_object->Data()->GetAsymmetricKey(); |
|
610 |
15 |
ManagedEVPPKey their_key = their_key_object->Data()->GetAsymmetricKey(); |
|
611 |
|||
612 |
15 |
Local<Value> out = StatelessDiffieHellmanThreadsafe(our_key, their_key) |
|
613 |
30 |
.ToBuffer(env).FromMaybe(Local<Uint8Array>()); |
|
614 |
|||
615 |
✓✓ | 15 |
if (Buffer::Length(out) == 0) |
616 |
2 |
return ThrowCryptoError(env, ERR_get_error(), "diffieHellman failed"); |
|
617 |
|||
618 |
26 |
args.GetReturnValue().Set(out); |
|
619 |
} |
||
620 |
|||
621 |
5 |
Maybe<bool> DHBitsTraits::AdditionalConfig( |
|
622 |
CryptoJobMode mode, |
||
623 |
const FunctionCallbackInfo<Value>& args, |
||
624 |
unsigned int offset, |
||
625 |
DHBitsConfig* params) { |
||
626 |
5 |
Environment* env = Environment::GetCurrent(args); |
|
627 |
|||
628 |
✓✗✗✓ |
10 |
CHECK(args[offset]->IsObject()); // public key |
629 |
✓✗✗✓ |
10 |
CHECK(args[offset + 1]->IsObject()); // private key |
630 |
|||
631 |
KeyObjectHandle* private_key; |
||
632 |
KeyObjectHandle* public_key; |
||
633 |
|||
634 |
✓✗✗✓ |
10 |
ASSIGN_OR_RETURN_UNWRAP(&public_key, args[offset], Nothing<bool>()); |
635 |
✓✗✗✓ |
10 |
ASSIGN_OR_RETURN_UNWRAP(&private_key, args[offset + 1], Nothing<bool>()); |
636 |
|||
637 |
✓✗✗✓ ✗✓ |
10 |
if (private_key->Data()->GetKeyType() != kKeyTypePrivate || |
638 |
5 |
public_key->Data()->GetKeyType() != kKeyTypePublic) { |
|
639 |
THROW_ERR_CRYPTO_INVALID_KEYTYPE(env); |
||
640 |
return Nothing<bool>(); |
||
641 |
} |
||
642 |
|||
643 |
5 |
params->public_key = public_key->Data(); |
|
644 |
5 |
params->private_key = private_key->Data(); |
|
645 |
|||
646 |
5 |
return Just(true); |
|
647 |
} |
||
648 |
|||
649 |
5 |
Maybe<bool> DHBitsTraits::EncodeOutput( |
|
650 |
Environment* env, |
||
651 |
const DHBitsConfig& params, |
||
652 |
ByteSource* out, |
||
653 |
v8::Local<v8::Value>* result) { |
||
654 |
10 |
*result = out->ToArrayBuffer(env); |
|
655 |
5 |
return Just(!result->IsEmpty()); |
|
656 |
} |
||
657 |
|||
658 |
5 |
bool DHBitsTraits::DeriveBits( |
|
659 |
Environment* env, |
||
660 |
const DHBitsConfig& params, |
||
661 |
ByteSource* out) { |
||
662 |
10 |
*out = StatelessDiffieHellmanThreadsafe( |
|
663 |
10 |
params.private_key->GetAsymmetricKey(), |
|
664 |
15 |
params.public_key->GetAsymmetricKey()); |
|
665 |
5 |
return true; |
|
666 |
} |
||
667 |
|||
668 |
3 |
Maybe<bool> GetDhKeyDetail( |
|
669 |
Environment* env, |
||
670 |
std::shared_ptr<KeyObjectData> key, |
||
671 |
Local<Object> target) { |
||
672 |
3 |
ManagedEVPPKey pkey = key->GetAsymmetricKey(); |
|
673 |
✗✓ | 3 |
CHECK_EQ(EVP_PKEY_id(pkey.get()), EVP_PKEY_DH); |
674 |
3 |
return Just(true); |
|
675 |
} |
||
676 |
|||
677 |
} // namespace crypto |
||
678 |
} // namespace node |
Generated by: GCOVR (Version 4.2) |