GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "crypto/crypto_ec.h" |
||
2 |
#include "crypto/crypto_common.h" |
||
3 |
#include "crypto/crypto_util.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 "node_buffer.h" |
||
10 |
#include "string_bytes.h" |
||
11 |
#include "threadpoolwork-inl.h" |
||
12 |
#include "v8.h" |
||
13 |
|||
14 |
#include <openssl/bn.h> |
||
15 |
#include <openssl/ec.h> |
||
16 |
#include <openssl/ecdh.h> |
||
17 |
|||
18 |
#include <algorithm> |
||
19 |
|||
20 |
namespace node { |
||
21 |
|||
22 |
using v8::Array; |
||
23 |
using v8::FunctionCallbackInfo; |
||
24 |
using v8::FunctionTemplate; |
||
25 |
using v8::Int32; |
||
26 |
using v8::Just; |
||
27 |
using v8::JustVoid; |
||
28 |
using v8::Local; |
||
29 |
using v8::Maybe; |
||
30 |
using v8::Nothing; |
||
31 |
using v8::Object; |
||
32 |
using v8::String; |
||
33 |
using v8::Uint32; |
||
34 |
using v8::Value; |
||
35 |
|||
36 |
namespace crypto { |
||
37 |
|||
38 |
262 |
int GetCurveFromName(const char* name) { |
|
39 |
262 |
int nid = EC_curve_nist2nid(name); |
|
40 |
✓✓ | 262 |
if (nid == NID_undef) |
41 |
23 |
nid = OBJ_sn2nid(name); |
|
42 |
262 |
return nid; |
|
43 |
} |
||
44 |
|||
45 |
114 |
int GetOKPCurveFromName(const char* name) { |
|
46 |
int nid; |
||
47 |
✓✓ | 114 |
if (strcmp(name, "NODE-ED25519") == 0) { |
48 |
10 |
nid = EVP_PKEY_ED25519; |
|
49 |
✓✓ | 104 |
} else if (strcmp(name, "NODE-ED448") == 0) { |
50 |
8 |
nid = EVP_PKEY_ED448; |
|
51 |
✓✓ | 96 |
} else if (strcmp(name, "NODE-X25519") == 0) { |
52 |
12 |
nid = EVP_PKEY_X25519; |
|
53 |
✓✓ | 84 |
} else if (strcmp(name, "NODE-X448") == 0) { |
54 |
10 |
nid = EVP_PKEY_X448; |
|
55 |
} else { |
||
56 |
74 |
nid = NID_undef; |
|
57 |
} |
||
58 |
114 |
return nid; |
|
59 |
} |
||
60 |
|||
61 |
629 |
void ECDH::Initialize(Environment* env, Local<Object> target) { |
|
62 |
629 |
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); |
|
63 |
629 |
t->Inherit(BaseObject::GetConstructorTemplate(env)); |
|
64 |
|||
65 |
1258 |
t->InstanceTemplate()->SetInternalFieldCount(ECDH::kInternalFieldCount); |
|
66 |
|||
67 |
629 |
env->SetProtoMethod(t, "generateKeys", GenerateKeys); |
|
68 |
629 |
env->SetProtoMethod(t, "computeSecret", ComputeSecret); |
|
69 |
629 |
env->SetProtoMethodNoSideEffect(t, "getPublicKey", GetPublicKey); |
|
70 |
629 |
env->SetProtoMethodNoSideEffect(t, "getPrivateKey", GetPrivateKey); |
|
71 |
629 |
env->SetProtoMethod(t, "setPublicKey", SetPublicKey); |
|
72 |
629 |
env->SetProtoMethod(t, "setPrivateKey", SetPrivateKey); |
|
73 |
|||
74 |
629 |
env->SetConstructorFunction(target, "ECDH", t); |
|
75 |
|||
76 |
629 |
env->SetMethodNoSideEffect(target, "ECDHConvertKey", ECDH::ConvertKey); |
|
77 |
629 |
env->SetMethodNoSideEffect(target, "getCurves", ECDH::GetCurves); |
|
78 |
|||
79 |
629 |
ECDHBitsJob::Initialize(env, target); |
|
80 |
629 |
ECKeyPairGenJob::Initialize(env, target); |
|
81 |
629 |
ECKeyExportJob::Initialize(env, target); |
|
82 |
|||
83 |
1887 |
NODE_DEFINE_CONSTANT(target, OPENSSL_EC_NAMED_CURVE); |
|
84 |
1258 |
NODE_DEFINE_CONSTANT(target, OPENSSL_EC_EXPLICIT_CURVE); |
|
85 |
629 |
} |
|
86 |
|||
87 |
4990 |
void ECDH::RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
|
88 |
4990 |
registry->Register(New); |
|
89 |
4990 |
registry->Register(GenerateKeys); |
|
90 |
4990 |
registry->Register(ComputeSecret); |
|
91 |
4990 |
registry->Register(GetPublicKey); |
|
92 |
4990 |
registry->Register(GetPrivateKey); |
|
93 |
4990 |
registry->Register(SetPublicKey); |
|
94 |
4990 |
registry->Register(SetPrivateKey); |
|
95 |
4990 |
registry->Register(ECDH::ConvertKey); |
|
96 |
4990 |
registry->Register(ECDH::GetCurves); |
|
97 |
|||
98 |
4990 |
ECDHBitsJob::RegisterExternalReferences(registry); |
|
99 |
4990 |
ECKeyPairGenJob::RegisterExternalReferences(registry); |
|
100 |
4990 |
ECKeyExportJob::RegisterExternalReferences(registry); |
|
101 |
4990 |
} |
|
102 |
|||
103 |
6 |
void ECDH::GetCurves(const FunctionCallbackInfo<Value>& args) { |
|
104 |
6 |
Environment* env = Environment::GetCurrent(args); |
|
105 |
6 |
const size_t num_curves = EC_get_builtin_curves(nullptr, 0); |
|
106 |
|||
107 |
✓✗ | 6 |
if (num_curves) { |
108 |
6 |
std::vector<EC_builtin_curve> curves(num_curves); |
|
109 |
|||
110 |
✓✗ | 6 |
if (EC_get_builtin_curves(curves.data(), num_curves)) { |
111 |
6 |
std::vector<Local<Value>> arr(num_curves); |
|
112 |
|||
113 |
✓✓ | 498 |
for (size_t i = 0; i < num_curves; i++) |
114 |
984 |
arr[i] = OneByteString(env->isolate(), OBJ_nid2sn(curves[i].nid)); |
|
115 |
|||
116 |
6 |
args.GetReturnValue().Set( |
|
117 |
Array::New(env->isolate(), arr.data(), arr.size())); |
||
118 |
6 |
return; |
|
119 |
} |
||
120 |
} |
||
121 |
|||
122 |
args.GetReturnValue().Set(Array::New(env->isolate())); |
||
123 |
} |
||
124 |
|||
125 |
10 |
ECDH::ECDH(Environment* env, Local<Object> wrap, ECKeyPointer&& key) |
|
126 |
: BaseObject(env, wrap), |
||
127 |
10 |
key_(std::move(key)), |
|
128 |
10 |
group_(EC_KEY_get0_group(key_.get())) { |
|
129 |
10 |
MakeWeak(); |
|
130 |
✗✓ | 10 |
CHECK_NOT_NULL(group_); |
131 |
10 |
} |
|
132 |
|||
133 |
void ECDH::MemoryInfo(MemoryTracker* tracker) const { |
||
134 |
tracker->TrackFieldWithSize("key", key_ ? kSizeOf_EC_KEY : 0); |
||
135 |
} |
||
136 |
|||
137 |
40 |
ECDH::~ECDH() {} |
|
138 |
|||
139 |
10 |
void ECDH::New(const FunctionCallbackInfo<Value>& args) { |
|
140 |
10 |
Environment* env = Environment::GetCurrent(args); |
|
141 |
|||
142 |
10 |
MarkPopErrorOnReturn mark_pop_error_on_return; |
|
143 |
|||
144 |
// TODO(indutny): Support raw curves? |
||
145 |
✗✓ | 20 |
CHECK(args[0]->IsString()); |
146 |
10 |
node::Utf8Value curve(env->isolate(), args[0]); |
|
147 |
|||
148 |
10 |
int nid = OBJ_sn2nid(*curve); |
|
149 |
✗✓ | 10 |
if (nid == NID_undef) |
150 |
return THROW_ERR_CRYPTO_INVALID_CURVE(env); |
||
151 |
|||
152 |
10 |
ECKeyPointer key(EC_KEY_new_by_curve_name(nid)); |
|
153 |
✗✓ | 10 |
if (!key) |
154 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
||
155 |
"Failed to create key using named curve"); |
||
156 |
|||
157 |
10 |
new ECDH(env, args.This(), std::move(key)); |
|
158 |
} |
||
159 |
|||
160 |
5 |
void ECDH::GenerateKeys(const FunctionCallbackInfo<Value>& args) { |
|
161 |
5 |
Environment* env = Environment::GetCurrent(args); |
|
162 |
|||
163 |
ECDH* ecdh; |
||
164 |
✗✓ | 5 |
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); |
165 |
|||
166 |
✗✓ | 5 |
if (!EC_KEY_generate_key(ecdh->key_.get())) |
167 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to generate key"); |
||
168 |
} |
||
169 |
|||
170 |
18 |
ECPointPointer ECDH::BufferToPoint(Environment* env, |
|
171 |
const EC_GROUP* group, |
||
172 |
Local<Value> buf) { |
||
173 |
int r; |
||
174 |
|||
175 |
36 |
ECPointPointer pub(EC_POINT_new(group)); |
|
176 |
✗✓ | 18 |
if (!pub) { |
177 |
THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
||
178 |
"Failed to allocate EC_POINT for a public key"); |
||
179 |
return pub; |
||
180 |
} |
||
181 |
|||
182 |
36 |
ArrayBufferOrViewContents<unsigned char> input(buf); |
|
183 |
✗✓ | 18 |
if (UNLIKELY(!input.CheckSizeInt32())) { |
184 |
THROW_ERR_OUT_OF_RANGE(env, "buffer is too big"); |
||
185 |
return ECPointPointer(); |
||
186 |
} |
||
187 |
36 |
r = EC_POINT_oct2point( |
|
188 |
group, |
||
189 |
pub.get(), |
||
190 |
18 |
input.data(), |
|
191 |
input.size(), |
||
192 |
nullptr); |
||
193 |
✓✓ | 18 |
if (!r) |
194 |
4 |
return ECPointPointer(); |
|
195 |
|||
196 |
14 |
return pub; |
|
197 |
} |
||
198 |
|||
199 |
7 |
void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) { |
|
200 |
7 |
Environment* env = Environment::GetCurrent(args); |
|
201 |
|||
202 |
✗✓ | 7 |
CHECK(IsAnyByteSource(args[0])); |
203 |
|||
204 |
ECDH* ecdh; |
||
205 |
✗✓ | 10 |
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); |
206 |
|||
207 |
7 |
MarkPopErrorOnReturn mark_pop_error_on_return; |
|
208 |
|||
209 |
✓✓ | 7 |
if (!ecdh->IsKeyPairValid()) |
210 |
1 |
return THROW_ERR_CRYPTO_INVALID_KEYPAIR(env); |
|
211 |
|||
212 |
ECPointPointer pub( |
||
213 |
ECDH::BufferToPoint(env, |
||
214 |
ecdh->group_, |
||
215 |
6 |
args[0])); |
|
216 |
✓✓ | 6 |
if (!pub) { |
217 |
2 |
args.GetReturnValue().Set( |
|
218 |
FIXED_ONE_BYTE_STRING(env->isolate(), |
||
219 |
"ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY")); |
||
220 |
2 |
return; |
|
221 |
} |
||
222 |
|||
223 |
// NOTE: field_size is in bits |
||
224 |
4 |
int field_size = EC_GROUP_get_degree(ecdh->group_); |
|
225 |
4 |
size_t out_len = (field_size + 7) / 8; |
|
226 |
4 |
AllocatedBuffer out = AllocatedBuffer::AllocateManaged(env, out_len); |
|
227 |
|||
228 |
4 |
int r = ECDH_compute_key( |
|
229 |
4 |
out.data(), out_len, pub.get(), ecdh->key_.get(), nullptr); |
|
230 |
✗✓ | 4 |
if (!r) |
231 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to compute ECDH key"); |
||
232 |
|||
233 |
12 |
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>())); |
|
234 |
} |
||
235 |
|||
236 |
21 |
void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) { |
|
237 |
21 |
Environment* env = Environment::GetCurrent(args); |
|
238 |
|||
239 |
// Conversion form |
||
240 |
✗✓ | 21 |
CHECK_EQ(args.Length(), 1); |
241 |
|||
242 |
ECDH* ecdh; |
||
243 |
✗✓ | 22 |
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); |
244 |
|||
245 |
21 |
const EC_GROUP* group = EC_KEY_get0_group(ecdh->key_.get()); |
|
246 |
21 |
const EC_POINT* pub = EC_KEY_get0_public_key(ecdh->key_.get()); |
|
247 |
✓✓ | 21 |
if (pub == nullptr) |
248 |
1 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
|
249 |
1 |
"Failed to get ECDH public key"); |
|
250 |
|||
251 |
✗✓ | 20 |
CHECK(args[0]->IsUint32()); |
252 |
40 |
uint32_t val = args[0].As<Uint32>()->Value(); |
|
253 |
20 |
point_conversion_form_t form = static_cast<point_conversion_form_t>(val); |
|
254 |
|||
255 |
const char* error; |
||
256 |
Local<Object> buf; |
||
257 |
✗✓ | 40 |
if (!ECPointToBuffer(env, group, pub, form, &error).ToLocal(&buf)) |
258 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, error); |
||
259 |
40 |
args.GetReturnValue().Set(buf); |
|
260 |
} |
||
261 |
|||
262 |
7 |
void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) { |
|
263 |
7 |
Environment* env = Environment::GetCurrent(args); |
|
264 |
|||
265 |
ECDH* ecdh; |
||
266 |
✗✓ | 8 |
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); |
267 |
|||
268 |
7 |
const BIGNUM* b = EC_KEY_get0_private_key(ecdh->key_.get()); |
|
269 |
✓✓ | 7 |
if (b == nullptr) |
270 |
1 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
|
271 |
1 |
"Failed to get ECDH private key"); |
|
272 |
|||
273 |
6 |
const int size = BN_num_bytes(b); |
|
274 |
6 |
AllocatedBuffer out = AllocatedBuffer::AllocateManaged(env, size); |
|
275 |
✗✓ | 6 |
CHECK_EQ(size, BN_bn2binpad(b, |
276 |
reinterpret_cast<unsigned char*>(out.data()), |
||
277 |
size)); |
||
278 |
|||
279 |
18 |
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>())); |
|
280 |
} |
||
281 |
|||
282 |
7 |
void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) { |
|
283 |
7 |
Environment* env = Environment::GetCurrent(args); |
|
284 |
|||
285 |
ECDH* ecdh; |
||
286 |
✗✓ | 10 |
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); |
287 |
|||
288 |
7 |
ArrayBufferOrViewContents<unsigned char> priv_buffer(args[0]); |
|
289 |
✗✓ | 7 |
if (UNLIKELY(!priv_buffer.CheckSizeInt32())) |
290 |
return THROW_ERR_OUT_OF_RANGE(env, "key is too big"); |
||
291 |
|||
292 |
BignumPointer priv(BN_bin2bn( |
||
293 |
7 |
priv_buffer.data(), priv_buffer.size(), nullptr)); |
|
294 |
✗✓ | 7 |
if (!priv) { |
295 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
||
296 |
"Failed to convert Buffer to BN"); |
||
297 |
} |
||
298 |
|||
299 |
✓✓ | 7 |
if (!ecdh->IsKeyValidForCurve(priv)) { |
300 |
3 |
return THROW_ERR_CRYPTO_INVALID_KEYTYPE(env, |
|
301 |
3 |
"Private key is not valid for specified curve."); |
|
302 |
} |
||
303 |
|||
304 |
4 |
ECKeyPointer new_key(EC_KEY_dup(ecdh->key_.get())); |
|
305 |
✗✓ | 4 |
CHECK(new_key); |
306 |
|||
307 |
4 |
int result = EC_KEY_set_private_key(new_key.get(), priv.get()); |
|
308 |
4 |
priv.reset(); |
|
309 |
|||
310 |
✗✓ | 4 |
if (!result) { |
311 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
||
312 |
"Failed to convert BN to a private key"); |
||
313 |
} |
||
314 |
|||
315 |
4 |
MarkPopErrorOnReturn mark_pop_error_on_return; |
|
316 |
4 |
USE(&mark_pop_error_on_return); |
|
317 |
|||
318 |
4 |
const BIGNUM* priv_key = EC_KEY_get0_private_key(new_key.get()); |
|
319 |
✗✓ | 4 |
CHECK_NOT_NULL(priv_key); |
320 |
|||
321 |
4 |
ECPointPointer pub(EC_POINT_new(ecdh->group_)); |
|
322 |
✗✓ | 4 |
CHECK(pub); |
323 |
|||
324 |
✗✓ | 4 |
if (!EC_POINT_mul(ecdh->group_, pub.get(), priv_key, |
325 |
nullptr, nullptr, nullptr)) { |
||
326 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
||
327 |
"Failed to generate ECDH public key"); |
||
328 |
} |
||
329 |
|||
330 |
✗✓ | 4 |
if (!EC_KEY_set_public_key(new_key.get(), pub.get())) |
331 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
||
332 |
"Failed to set generated public key"); |
||
333 |
|||
334 |
4 |
ecdh->key_ = std::move(new_key); |
|
335 |
4 |
ecdh->group_ = EC_KEY_get0_group(ecdh->key_.get()); |
|
336 |
} |
||
337 |
|||
338 |
5 |
void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) { |
|
339 |
5 |
Environment* env = Environment::GetCurrent(args); |
|
340 |
|||
341 |
ECDH* ecdh; |
||
342 |
✗✓ | 6 |
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder()); |
343 |
|||
344 |
✗✓ | 5 |
CHECK(IsAnyByteSource(args[0])); |
345 |
|||
346 |
5 |
MarkPopErrorOnReturn mark_pop_error_on_return; |
|
347 |
|||
348 |
ECPointPointer pub( |
||
349 |
ECDH::BufferToPoint(env, |
||
350 |
ecdh->group_, |
||
351 |
5 |
args[0])); |
|
352 |
✓✓ | 5 |
if (!pub) { |
353 |
1 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
|
354 |
1 |
"Failed to convert Buffer to EC_POINT"); |
|
355 |
} |
||
356 |
|||
357 |
4 |
int r = EC_KEY_set_public_key(ecdh->key_.get(), pub.get()); |
|
358 |
✗✓ | 4 |
if (!r) { |
359 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
||
360 |
"Failed to set EC_POINT as the public key"); |
||
361 |
} |
||
362 |
} |
||
363 |
|||
364 |
7 |
bool ECDH::IsKeyValidForCurve(const BignumPointer& private_key) { |
|
365 |
✗✓ | 7 |
CHECK(group_); |
366 |
✗✓ | 7 |
CHECK(private_key); |
367 |
// Private keys must be in the range [1, n-1]. |
||
368 |
// Ref: Section 3.2.1 - http://www.secg.org/sec1-v2.pdf |
||
369 |
✓✓ | 7 |
if (BN_cmp(private_key.get(), BN_value_one()) < 0) { |
370 |
1 |
return false; |
|
371 |
} |
||
372 |
6 |
BignumPointer order(BN_new()); |
|
373 |
✗✓ | 6 |
CHECK(order); |
374 |
✓✗✓✓ |
12 |
return EC_GROUP_get_order(group_, order.get(), nullptr) && |
375 |
12 |
BN_cmp(private_key.get(), order.get()) < 0; |
|
376 |
} |
||
377 |
|||
378 |
7 |
bool ECDH::IsKeyPairValid() { |
|
379 |
7 |
MarkPopErrorOnReturn mark_pop_error_on_return; |
|
380 |
7 |
USE(&mark_pop_error_on_return); |
|
381 |
7 |
return 1 == EC_KEY_check_key(key_.get()); |
|
382 |
} |
||
383 |
|||
384 |
// Convert the input public key to compressed, uncompressed, or hybrid formats. |
||
385 |
8 |
void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) { |
|
386 |
8 |
MarkPopErrorOnReturn mark_pop_error_on_return; |
|
387 |
8 |
Environment* env = Environment::GetCurrent(args); |
|
388 |
|||
389 |
✗✓ | 8 |
CHECK_EQ(args.Length(), 3); |
390 |
✗✓ | 8 |
CHECK(IsAnyByteSource(args[0])); |
391 |
|||
392 |
8 |
ArrayBufferOrViewContents<char> args0(args[0]); |
|
393 |
✗✓ | 8 |
if (UNLIKELY(!args0.CheckSizeInt32())) |
394 |
return THROW_ERR_OUT_OF_RANGE(env, "key is too big"); |
||
395 |
✗✓ | 8 |
if (args0.size() == 0) |
396 |
return args.GetReturnValue().SetEmptyString(); |
||
397 |
|||
398 |
8 |
node::Utf8Value curve(env->isolate(), args[1]); |
|
399 |
|||
400 |
8 |
int nid = OBJ_sn2nid(*curve); |
|
401 |
✓✓ | 8 |
if (nid == NID_undef) |
402 |
1 |
return THROW_ERR_CRYPTO_INVALID_CURVE(env); |
|
403 |
|||
404 |
ECGroupPointer group( |
||
405 |
7 |
EC_GROUP_new_by_curve_name(nid)); |
|
406 |
✗✓ | 7 |
if (group == nullptr) |
407 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to get EC_GROUP"); |
||
408 |
|||
409 |
ECPointPointer pub( |
||
410 |
ECDH::BufferToPoint(env, |
||
411 |
7 |
group.get(), |
|
412 |
7 |
args[0])); |
|
413 |
|||
414 |
✓✓ | 7 |
if (pub == nullptr) { |
415 |
1 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, |
|
416 |
1 |
"Failed to convert Buffer to EC_POINT"); |
|
417 |
} |
||
418 |
|||
419 |
✗✓ | 6 |
CHECK(args[2]->IsUint32()); |
420 |
12 |
uint32_t val = args[2].As<Uint32>()->Value(); |
|
421 |
6 |
point_conversion_form_t form = static_cast<point_conversion_form_t>(val); |
|
422 |
|||
423 |
const char* error; |
||
424 |
Local<Object> buf; |
||
425 |
✗✓ | 12 |
if (!ECPointToBuffer(env, group.get(), pub.get(), form, &error).ToLocal(&buf)) |
426 |
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, error); |
||
427 |
12 |
args.GetReturnValue().Set(buf); |
|
428 |
} |
||
429 |
|||
430 |
void ECDHBitsConfig::MemoryInfo(MemoryTracker* tracker) const { |
||
431 |
tracker->TrackField("public", public_); |
||
432 |
tracker->TrackField("private", private_); |
||
433 |
} |
||
434 |
|||
435 |
82 |
Maybe<bool> ECDHBitsTraits::EncodeOutput( |
|
436 |
Environment* env, |
||
437 |
const ECDHBitsConfig& params, |
||
438 |
ByteSource* out, |
||
439 |
v8::Local<v8::Value>* result) { |
||
440 |
164 |
*result = out->ToArrayBuffer(env); |
|
441 |
82 |
return Just(!result->IsEmpty()); |
|
442 |
} |
||
443 |
|||
444 |
82 |
Maybe<bool> ECDHBitsTraits::AdditionalConfig( |
|
445 |
CryptoJobMode mode, |
||
446 |
const FunctionCallbackInfo<Value>& args, |
||
447 |
unsigned int offset, |
||
448 |
ECDHBitsConfig* params) { |
||
449 |
82 |
Environment* env = Environment::GetCurrent(args); |
|
450 |
|||
451 |
✓✗✗✓ |
246 |
CHECK(args[offset]->IsString()); // curve name |
452 |
✓✗✗✓ |
164 |
CHECK(args[offset + 1]->IsObject()); // public key |
453 |
✓✗✗✓ |
164 |
CHECK(args[offset + 2]->IsObject()); // private key |
454 |
|||
455 |
KeyObjectHandle* private_key; |
||
456 |
KeyObjectHandle* public_key; |
||
457 |
|||
458 |
✓✗ | 246 |
Utf8Value name(env->isolate(), args[offset]); |
459 |
|||
460 |
✓✗✗✓ |
164 |
ASSIGN_OR_RETURN_UNWRAP(&public_key, args[offset + 1], Nothing<bool>()); |
461 |
✓✗✗✓ |
164 |
ASSIGN_OR_RETURN_UNWRAP(&private_key, args[offset + 2], Nothing<bool>()); |
462 |
|||
463 |
✓✗✗✓ ✗✓ |
164 |
if (private_key->Data()->GetKeyType() != kKeyTypePrivate || |
464 |
82 |
public_key->Data()->GetKeyType() != kKeyTypePublic) { |
|
465 |
THROW_ERR_CRYPTO_INVALID_KEYTYPE(env); |
||
466 |
return Nothing<bool>(); |
||
467 |
} |
||
468 |
|||
469 |
82 |
params->id_ = GetOKPCurveFromName(*name); |
|
470 |
82 |
params->private_ = private_key->Data(); |
|
471 |
82 |
params->public_ = public_key->Data(); |
|
472 |
|||
473 |
82 |
return Just(true); |
|
474 |
} |
||
475 |
|||
476 |
82 |
bool ECDHBitsTraits::DeriveBits( |
|
477 |
Environment* env, |
||
478 |
const ECDHBitsConfig& params, |
||
479 |
ByteSource* out) { |
||
480 |
|||
481 |
82 |
char* data = nullptr; |
|
482 |
82 |
size_t len = 0; |
|
483 |
164 |
ManagedEVPPKey m_privkey = params.private_->GetAsymmetricKey(); |
|
484 |
164 |
ManagedEVPPKey m_pubkey = params.public_->GetAsymmetricKey(); |
|
485 |
|||
486 |
✓✓ | 82 |
switch (params.id_) { |
487 |
8 |
case EVP_PKEY_X25519: |
|
488 |
// Fall through |
||
489 |
case EVP_PKEY_X448: { |
||
490 |
8 |
EVPKeyCtxPointer ctx = nullptr; |
|
491 |
{ |
||
492 |
8 |
ctx.reset(EVP_PKEY_CTX_new(m_privkey.get(), nullptr)); |
|
493 |
} |
||
494 |
8 |
Mutex::ScopedLock pub_lock(*m_pubkey.mutex()); |
|
495 |
✓✗ | 16 |
if (EVP_PKEY_derive_init(ctx.get()) <= 0 || |
496 |
8 |
EVP_PKEY_derive_set_peer( |
|
497 |
ctx.get(), |
||
498 |
✓✗✗✓ ✗✓ |
16 |
m_pubkey.get()) <= 0 || |
499 |
8 |
EVP_PKEY_derive(ctx.get(), nullptr, &len) <= 0) { |
|
500 |
return false; |
||
501 |
} |
||
502 |
|||
503 |
8 |
data = MallocOpenSSL<char>(len); |
|
504 |
|||
505 |
8 |
if (EVP_PKEY_derive( |
|
506 |
ctx.get(), |
||
507 |
reinterpret_cast<unsigned char*>(data), |
||
508 |
✗✓ | 8 |
&len) <= 0) { |
509 |
return false; |
||
510 |
} |
||
511 |
|||
512 |
8 |
break; |
|
513 |
} |
||
514 |
74 |
default: { |
|
515 |
const EC_KEY* private_key; |
||
516 |
{ |
||
517 |
74 |
Mutex::ScopedLock priv_lock(*m_privkey.mutex()); |
|
518 |
74 |
private_key = EVP_PKEY_get0_EC_KEY(m_privkey.get()); |
|
519 |
} |
||
520 |
|||
521 |
74 |
Mutex::ScopedLock pub_lock(*m_pubkey.mutex()); |
|
522 |
74 |
const EC_KEY* public_key = EVP_PKEY_get0_EC_KEY(m_pubkey.get()); |
|
523 |
|||
524 |
74 |
const EC_GROUP* group = EC_KEY_get0_group(private_key); |
|
525 |
✗✓ | 74 |
if (group == nullptr) |
526 |
return false; |
||
527 |
|||
528 |
✗✓ | 74 |
CHECK_EQ(EC_KEY_check_key(private_key), 1); |
529 |
✗✓ | 74 |
CHECK_EQ(EC_KEY_check_key(public_key), 1); |
530 |
74 |
const EC_POINT* pub = EC_KEY_get0_public_key(public_key); |
|
531 |
74 |
int field_size = EC_GROUP_get_degree(group); |
|
532 |
74 |
len = (field_size + 7) / 8; |
|
533 |
74 |
data = MallocOpenSSL<char>(len); |
|
534 |
✗✓ | 74 |
CHECK_NOT_NULL(data); |
535 |
✗✓ | 74 |
CHECK_NOT_NULL(pub); |
536 |
✗✓ | 74 |
CHECK_NOT_NULL(private_key); |
537 |
74 |
if (ECDH_compute_key( |
|
538 |
data, |
||
539 |
len, |
||
540 |
pub, |
||
541 |
private_key, |
||
542 |
✗✓ | 74 |
nullptr) <= 0) { |
543 |
return false; |
||
544 |
74 |
} |
|
545 |
} |
||
546 |
} |
||
547 |
82 |
ByteSource buf = ByteSource::Allocated(data, len); |
|
548 |
82 |
*out = std::move(buf); |
|
549 |
82 |
return true; |
|
550 |
} |
||
551 |
|||
552 |
101 |
EVPKeyCtxPointer EcKeyGenTraits::Setup(EcKeyPairGenConfig* params) { |
|
553 |
101 |
EVPKeyCtxPointer key_ctx; |
|
554 |
✗✓ | 101 |
switch (params->params.curve_nid) { |
555 |
case EVP_PKEY_ED25519: |
||
556 |
// Fall through |
||
557 |
case EVP_PKEY_ED448: |
||
558 |
// Fall through |
||
559 |
case EVP_PKEY_X25519: |
||
560 |
// Fall through |
||
561 |
case EVP_PKEY_X448: |
||
562 |
key_ctx.reset(EVP_PKEY_CTX_new_id(params->params.curve_nid, nullptr)); |
||
563 |
break; |
||
564 |
101 |
default: { |
|
565 |
101 |
EVPKeyCtxPointer param_ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr)); |
|
566 |
101 |
EVP_PKEY* raw_params = nullptr; |
|
567 |
✓✗ | 202 |
if (!param_ctx || |
568 |
✓✗ | 202 |
EVP_PKEY_paramgen_init(param_ctx.get()) <= 0 || |
569 |
101 |
EVP_PKEY_CTX_set_ec_paramgen_curve_nid( |
|
570 |
✓✗ | 101 |
param_ctx.get(), params->params.curve_nid) <= 0 || |
571 |
101 |
EVP_PKEY_CTX_set_ec_param_enc( |
|
572 |
✓✗✗✓ ✗✓ |
202 |
param_ctx.get(), params->params.param_encoding) <= 0 || |
573 |
101 |
EVP_PKEY_paramgen(param_ctx.get(), &raw_params) <= 0) { |
|
574 |
return EVPKeyCtxPointer(); |
||
575 |
} |
||
576 |
202 |
EVPKeyPointer key_params(raw_params); |
|
577 |
202 |
key_ctx.reset(EVP_PKEY_CTX_new(key_params.get(), nullptr)); |
|
578 |
} |
||
579 |
} |
||
580 |
|||
581 |
✓✗✗✓ ✗✓ |
101 |
if (key_ctx && EVP_PKEY_keygen_init(key_ctx.get()) <= 0) |
582 |
key_ctx.reset(); |
||
583 |
|||
584 |
101 |
return key_ctx; |
|
585 |
} |
||
586 |
|||
587 |
// EcKeyPairGenJob input arguments |
||
588 |
// 1. CryptoJobMode |
||
589 |
// 2. Curve Name |
||
590 |
// 3. Param Encoding |
||
591 |
// 4. Public Format |
||
592 |
// 5. Public Type |
||
593 |
// 6. Private Format |
||
594 |
// 7. Private Type |
||
595 |
// 8. Cipher |
||
596 |
// 9. Passphrase |
||
597 |
106 |
Maybe<bool> EcKeyGenTraits::AdditionalConfig( |
|
598 |
CryptoJobMode mode, |
||
599 |
const FunctionCallbackInfo<Value>& args, |
||
600 |
unsigned int* offset, |
||
601 |
EcKeyPairGenConfig* params) { |
||
602 |
106 |
Environment* env = Environment::GetCurrent(args); |
|
603 |
✓✗✗✓ |
318 |
CHECK(args[*offset]->IsString()); // curve name |
604 |
✓✗✗✓ |
212 |
CHECK(args[*offset + 1]->IsInt32()); // param encoding |
605 |
|||
606 |
✓✗ | 318 |
Utf8Value curve_name(env->isolate(), args[*offset]); |
607 |
106 |
params->params.curve_nid = GetCurveFromName(*curve_name); |
|
608 |
✓✓ | 106 |
if (params->params.curve_nid == NID_undef) { |
609 |
5 |
THROW_ERR_CRYPTO_INVALID_CURVE(env); |
|
610 |
5 |
return Nothing<bool>(); |
|
611 |
} |
||
612 |
|||
613 |
✓✗ | 303 |
params->params.param_encoding = args[*offset + 1].As<Int32>()->Value(); |
614 |
✓✓ | 101 |
if (params->params.param_encoding != OPENSSL_EC_NAMED_CURVE && |
615 |
✗✓ | 3 |
params->params.param_encoding != OPENSSL_EC_EXPLICIT_CURVE) { |
616 |
THROW_ERR_OUT_OF_RANGE(env, "Invalid param_encoding specified"); |
||
617 |
return Nothing<bool>(); |
||
618 |
} |
||
619 |
|||
620 |
101 |
*offset += 2; |
|
621 |
|||
622 |
101 |
return Just(true); |
|
623 |
} |
||
624 |
|||
625 |
namespace { |
||
626 |
7 |
WebCryptoKeyExportStatus EC_Raw_Export( |
|
627 |
KeyObjectData* key_data, |
||
628 |
const ECKeyExportConfig& params, |
||
629 |
ByteSource* out) { |
||
630 |
14 |
ManagedEVPPKey m_pkey = key_data->GetAsymmetricKey(); |
|
631 |
✗✓ | 7 |
CHECK(m_pkey); |
632 |
14 |
Mutex::ScopedLock lock(*m_pkey.mutex()); |
|
633 |
|||
634 |
7 |
const EC_KEY* ec_key = EVP_PKEY_get0_EC_KEY(m_pkey.get()); |
|
635 |
|||
636 |
unsigned char* data; |
||
637 |
7 |
size_t len = 0; |
|
638 |
|||
639 |
✓✗ | 7 |
if (ec_key == nullptr) { |
640 |
typedef int (*export_fn)(const EVP_PKEY*, unsigned char*, size_t* len); |
||
641 |
7 |
export_fn fn = nullptr; |
|
642 |
✗✓✗✗ |
7 |
switch (key_data->GetKeyType()) { |
643 |
case kKeyTypePrivate: |
||
644 |
fn = EVP_PKEY_get_raw_private_key; |
||
645 |
break; |
||
646 |
7 |
case kKeyTypePublic: |
|
647 |
7 |
fn = EVP_PKEY_get_raw_public_key; |
|
648 |
7 |
break; |
|
649 |
case kKeyTypeSecret: |
||
650 |
UNREACHABLE(); |
||
651 |
} |
||
652 |
✗✓ | 7 |
CHECK_NOT_NULL(fn); |
653 |
// Get the size of the raw key data |
||
654 |
✗✓ | 7 |
if (fn(m_pkey.get(), nullptr, &len) == 0) |
655 |
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE; |
||
656 |
7 |
data = MallocOpenSSL<unsigned char>(len); |
|
657 |
✗✓ | 7 |
if (fn(m_pkey.get(), data, &len) == 0) |
658 |
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE; |
||
659 |
} else { |
||
660 |
if (key_data->GetKeyType() != kKeyTypePublic) |
||
661 |
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE; |
||
662 |
const EC_GROUP* group = EC_KEY_get0_group(ec_key); |
||
663 |
const EC_POINT* point = EC_KEY_get0_public_key(ec_key); |
||
664 |
point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED; |
||
665 |
|||
666 |
// Get the allocated data size... |
||
667 |
len = EC_POINT_point2oct(group, point, form, nullptr, 0, nullptr); |
||
668 |
if (len == 0) |
||
669 |
return WebCryptoKeyExportStatus::FAILED; |
||
670 |
data = MallocOpenSSL<unsigned char>(len); |
||
671 |
size_t check_len = |
||
672 |
EC_POINT_point2oct(group, point, form, data, len, nullptr); |
||
673 |
if (check_len == 0) |
||
674 |
return WebCryptoKeyExportStatus::FAILED; |
||
675 |
|||
676 |
CHECK_EQ(len, check_len); |
||
677 |
} |
||
678 |
|||
679 |
7 |
*out = ByteSource::Allocated(reinterpret_cast<char*>(data), len); |
|
680 |
|||
681 |
7 |
return WebCryptoKeyExportStatus::OK; |
|
682 |
} |
||
683 |
} // namespace |
||
684 |
|||
685 |
155 |
Maybe<bool> ECKeyExportTraits::AdditionalConfig( |
|
686 |
const FunctionCallbackInfo<Value>& args, |
||
687 |
unsigned int offset, |
||
688 |
ECKeyExportConfig* params) { |
||
689 |
155 |
return Just(true); |
|
690 |
} |
||
691 |
|||
692 |
155 |
WebCryptoKeyExportStatus ECKeyExportTraits::DoExport( |
|
693 |
std::shared_ptr<KeyObjectData> key_data, |
||
694 |
WebCryptoKeyFormat format, |
||
695 |
const ECKeyExportConfig& params, |
||
696 |
ByteSource* out) { |
||
697 |
✗✓ | 155 |
CHECK_NE(key_data->GetKeyType(), kKeyTypeSecret); |
698 |
|||
699 |
✓✓✓✗ |
155 |
switch (format) { |
700 |
7 |
case kWebCryptoKeyFormatRaw: |
|
701 |
7 |
return EC_Raw_Export(key_data.get(), params, out); |
|
702 |
79 |
case kWebCryptoKeyFormatPKCS8: |
|
703 |
✗✓ | 79 |
if (key_data->GetKeyType() != kKeyTypePrivate) |
704 |
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE; |
||
705 |
79 |
return PKEY_PKCS8_Export(key_data.get(), out); |
|
706 |
69 |
case kWebCryptoKeyFormatSPKI: |
|
707 |
✗✓ | 69 |
if (key_data->GetKeyType() != kKeyTypePublic) |
708 |
return WebCryptoKeyExportStatus::INVALID_KEY_TYPE; |
||
709 |
69 |
return PKEY_SPKI_Export(key_data.get(), out); |
|
710 |
default: |
||
711 |
UNREACHABLE(); |
||
712 |
} |
||
713 |
} |
||
714 |
|||
715 |
199 |
Maybe<void> ExportJWKEcKey( |
|
716 |
Environment* env, |
||
717 |
std::shared_ptr<KeyObjectData> key, |
||
718 |
Local<Object> target) { |
||
719 |
398 |
ManagedEVPPKey m_pkey = key->GetAsymmetricKey(); |
|
720 |
398 |
Mutex::ScopedLock lock(*m_pkey.mutex()); |
|
721 |
✗✓ | 199 |
CHECK_EQ(EVP_PKEY_id(m_pkey.get()), EVP_PKEY_EC); |
722 |
|||
723 |
199 |
const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(m_pkey.get()); |
|
724 |
✗✓ | 199 |
CHECK_NOT_NULL(ec); |
725 |
|||
726 |
199 |
const EC_POINT* pub = EC_KEY_get0_public_key(ec); |
|
727 |
199 |
const EC_GROUP* group = EC_KEY_get0_group(ec); |
|
728 |
|||
729 |
199 |
int degree_bits = EC_GROUP_get_degree(group); |
|
730 |
199 |
int degree_bytes = |
|
731 |
199 |
(degree_bits / CHAR_BIT) + (7 + (degree_bits % CHAR_BIT)) / 8; |
|
732 |
|||
733 |
398 |
BignumPointer x(BN_new()); |
|
734 |
398 |
BignumPointer y(BN_new()); |
|
735 |
|||
736 |
✗✓ | 199 |
if (!EC_POINT_get_affine_coordinates(group, pub, x.get(), y.get(), nullptr)) { |
737 |
ThrowCryptoError(env, ERR_get_error(), |
||
738 |
"Failed to get elliptic-curve point coordinates"); |
||
739 |
return Nothing<void>(); |
||
740 |
} |
||
741 |
|||
742 |
398 |
if (target->Set( |
|
743 |
env->context(), |
||
744 |
env->jwk_kty_string(), |
||
745 |
✗✓ | 796 |
env->jwk_ec_string()).IsNothing()) { |
746 |
return Nothing<void>(); |
||
747 |
} |
||
748 |
|||
749 |
199 |
if (SetEncodedValue( |
|
750 |
env, |
||
751 |
target, |
||
752 |
env->jwk_x_string(), |
||
753 |
199 |
x.get(), |
|
754 |
✓✗ | 597 |
degree_bytes).IsNothing() || |
755 |
✗✓ | 199 |
SetEncodedValue( |
756 |
env, |
||
757 |
target, |
||
758 |
env->jwk_y_string(), |
||
759 |
199 |
y.get(), |
|
760 |
✗✓ | 398 |
degree_bytes).IsNothing()) { |
761 |
return Nothing<void>(); |
||
762 |
} |
||
763 |
|||
764 |
Local<String> crv_name; |
||
765 |
199 |
const int nid = EC_GROUP_get_curve_name(group); |
|
766 |
✓✓✓✓ ✓ |
199 |
switch (nid) { |
767 |
97 |
case NID_X9_62_prime256v1: |
|
768 |
97 |
crv_name = OneByteString(env->isolate(), "P-256"); |
|
769 |
97 |
break; |
|
770 |
7 |
case NID_secp256k1: |
|
771 |
7 |
crv_name = OneByteString(env->isolate(), "secp256k1"); |
|
772 |
7 |
break; |
|
773 |
75 |
case NID_secp384r1: |
|
774 |
75 |
crv_name = OneByteString(env->isolate(), "P-384"); |
|
775 |
75 |
break; |
|
776 |
17 |
case NID_secp521r1: |
|
777 |
17 |
crv_name = OneByteString(env->isolate(), "P-521"); |
|
778 |
17 |
break; |
|
779 |
3 |
default: { |
|
780 |
3 |
THROW_ERR_CRYPTO_JWK_UNSUPPORTED_CURVE( |
|
781 |
3 |
env, "Unsupported JWK EC curve: %s.", OBJ_nid2sn(nid)); |
|
782 |
3 |
return Nothing<void>(); |
|
783 |
} |
||
784 |
} |
||
785 |
392 |
if (target->Set( |
|
786 |
env->context(), |
||
787 |
env->jwk_crv_string(), |
||
788 |
✗✓ | 588 |
crv_name).IsNothing()) { |
789 |
return Nothing<void>(); |
||
790 |
} |
||
791 |
|||
792 |
✓✓ | 196 |
if (key->GetKeyType() == kKeyTypePrivate) { |
793 |
111 |
const BIGNUM* pvt = EC_KEY_get0_private_key(ec); |
|
794 |
111 |
return SetEncodedValue( |
|
795 |
env, |
||
796 |
target, |
||
797 |
env->jwk_d_string(), |
||
798 |
pvt, |
||
799 |
✓✗ | 222 |
degree_bytes).IsJust() ? JustVoid() : Nothing<void>(); |
800 |
} |
||
801 |
|||
802 |
85 |
return JustVoid(); |
|
803 |
} |
||
804 |
|||
805 |
46 |
Maybe<bool> ExportJWKEdKey( |
|
806 |
Environment* env, |
||
807 |
std::shared_ptr<KeyObjectData> key, |
||
808 |
Local<Object> target) { |
||
809 |
92 |
ManagedEVPPKey pkey = key->GetAsymmetricKey(); |
|
810 |
92 |
Mutex::ScopedLock lock(*pkey.mutex()); |
|
811 |
|||
812 |
46 |
const char* curve = nullptr; |
|
813 |
✓✓✓✓ ✗ |
46 |
switch (EVP_PKEY_id(pkey.get())) { |
814 |
15 |
case EVP_PKEY_ED25519: |
|
815 |
15 |
curve = "Ed25519"; |
|
816 |
15 |
break; |
|
817 |
13 |
case EVP_PKEY_ED448: |
|
818 |
13 |
curve = "Ed448"; |
|
819 |
13 |
break; |
|
820 |
9 |
case EVP_PKEY_X25519: |
|
821 |
9 |
curve = "X25519"; |
|
822 |
9 |
break; |
|
823 |
9 |
case EVP_PKEY_X448: |
|
824 |
9 |
curve = "X448"; |
|
825 |
9 |
break; |
|
826 |
default: |
||
827 |
UNREACHABLE(); |
||
828 |
} |
||
829 |
92 |
if (target->Set( |
|
830 |
env->context(), |
||
831 |
env->jwk_crv_string(), |
||
832 |
✗✓ | 184 |
OneByteString(env->isolate(), curve)).IsNothing()) { |
833 |
return Nothing<bool>(); |
||
834 |
} |
||
835 |
|||
836 |
46 |
size_t len = 0; |
|
837 |
Local<Value> encoded; |
||
838 |
Local<Value> error; |
||
839 |
|||
840 |
✗✓ | 46 |
if (!EVP_PKEY_get_raw_public_key(pkey.get(), nullptr, &len)) |
841 |
return Nothing<bool>(); |
||
842 |
|||
843 |
46 |
unsigned char* data = MallocOpenSSL<unsigned char>(len); |
|
844 |
92 |
ByteSource out = ByteSource::Allocated(reinterpret_cast<char*>(data), len); |
|
845 |
|||
846 |
✓✓ | 46 |
if (key->GetKeyType() == kKeyTypePrivate) { |
847 |
21 |
if (!EVP_PKEY_get_raw_private_key(pkey.get(), data, &len) || |
|
848 |
42 |
!StringBytes::Encode( |
|
849 |
env->isolate(), |
||
850 |
reinterpret_cast<const char*>(data), |
||
851 |
len, |
||
852 |
BASE64URL, |
||
853 |
✓✗✓✗ |
63 |
&error).ToLocal(&encoded) || |
854 |
42 |
!target->Set( |
|
855 |
env->context(), |
||
856 |
env->jwk_d_string(), |
||
857 |
✗✓✗✓ |
84 |
encoded).IsJust()) { |
858 |
if (!error.IsEmpty()) |
||
859 |
env->isolate()->ThrowException(error); |
||
860 |
return Nothing<bool>(); |
||
861 |
} |
||
862 |
} |
||
863 |
|||
864 |
46 |
if (!EVP_PKEY_get_raw_public_key(pkey.get(), data, &len) || |
|
865 |
92 |
!StringBytes::Encode( |
|
866 |
env->isolate(), |
||
867 |
reinterpret_cast<const char*>(data), |
||
868 |
len, |
||
869 |
BASE64URL, |
||
870 |
✓✗✓✗ |
138 |
&error).ToLocal(&encoded) || |
871 |
92 |
!target->Set( |
|
872 |
env->context(), |
||
873 |
env->jwk_x_string(), |
||
874 |
✗✓✗✓ |
184 |
encoded).IsJust()) { |
875 |
if (!error.IsEmpty()) |
||
876 |
env->isolate()->ThrowException(error); |
||
877 |
return Nothing<bool>(); |
||
878 |
} |
||
879 |
|||
880 |
92 |
if (target->Set( |
|
881 |
env->context(), |
||
882 |
env->jwk_kty_string(), |
||
883 |
✗✓ | 184 |
env->jwk_okp_string()).IsNothing()) { |
884 |
return Nothing<bool>(); |
||
885 |
} |
||
886 |
|||
887 |
46 |
return Just(true); |
|
888 |
} |
||
889 |
|||
890 |
156 |
std::shared_ptr<KeyObjectData> ImportJWKEcKey( |
|
891 |
Environment* env, |
||
892 |
Local<Object> jwk, |
||
893 |
const FunctionCallbackInfo<Value>& args, |
||
894 |
unsigned int offset) { |
||
895 |
✓✗✗✓ |
468 |
CHECK(args[offset]->IsString()); // curve name |
896 |
✓✗ | 624 |
Utf8Value curve(env->isolate(), args[offset].As<String>()); |
897 |
|||
898 |
156 |
int nid = GetCurveFromName(*curve); |
|
899 |
✗✓ | 156 |
if (nid == NID_undef) { // Unknown curve |
900 |
THROW_ERR_CRYPTO_INVALID_CURVE(env); |
||
901 |
return std::shared_ptr<KeyObjectData>(); |
||
902 |
} |
||
903 |
|||
904 |
Local<Value> x_value; |
||
905 |
Local<Value> y_value; |
||
906 |
Local<Value> d_value; |
||
907 |
|||
908 |
312 |
if (!jwk->Get(env->context(), env->jwk_x_string()).ToLocal(&x_value) || |
|
909 |
✓✗✓✗ |
780 |
!jwk->Get(env->context(), env->jwk_y_string()).ToLocal(&y_value) || |
910 |
✗✓✗✓ |
624 |
!jwk->Get(env->context(), env->jwk_d_string()).ToLocal(&d_value)) { |
911 |
return std::shared_ptr<KeyObjectData>(); |
||
912 |
} |
||
913 |
|||
914 |
156 |
if (!x_value->IsString() || |
|
915 |
✓✗✓✗ ✗✓ |
624 |
!y_value->IsString() || |
916 |
✓✓✗✓ |
482 |
(!d_value->IsUndefined() && !d_value->IsString())) { |
917 |
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key"); |
||
918 |
return std::shared_ptr<KeyObjectData>(); |
||
919 |
} |
||
920 |
|||
921 |
✓✓ | 312 |
KeyType type = d_value->IsString() ? kKeyTypePrivate : kKeyTypePublic; |
922 |
|||
923 |
312 |
ECKeyPointer ec(EC_KEY_new_by_curve_name(nid)); |
|
924 |
✗✓ | 156 |
if (!ec) { |
925 |
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key"); |
||
926 |
return std::shared_ptr<KeyObjectData>(); |
||
927 |
} |
||
928 |
|||
929 |
312 |
ByteSource x = ByteSource::FromEncodedString(env, x_value.As<String>()); |
|
930 |
312 |
ByteSource y = ByteSource::FromEncodedString(env, y_value.As<String>()); |
|
931 |
|||
932 |
312 |
if (!EC_KEY_set_public_key_affine_coordinates( |
|
933 |
ec.get(), |
||
934 |
312 |
x.ToBN().get(), |
|
935 |
✗✓ | 312 |
y.ToBN().get())) { |
936 |
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key"); |
||
937 |
return std::shared_ptr<KeyObjectData>(); |
||
938 |
} |
||
939 |
|||
940 |
✓✓ | 156 |
if (type == kKeyTypePrivate) { |
941 |
85 |
ByteSource d = ByteSource::FromEncodedString(env, d_value.As<String>()); |
|
942 |
✗✓ | 85 |
if (!EC_KEY_set_private_key(ec.get(), d.ToBN().get())) { |
943 |
THROW_ERR_CRYPTO_INVALID_JWK(env, "Invalid JWK EC key"); |
||
944 |
return std::shared_ptr<KeyObjectData>(); |
||
945 |
} |
||
946 |
} |
||
947 |
|||
948 |
156 |
EVPKeyPointer pkey(EVP_PKEY_new()); |
|
949 |
✗✓ | 156 |
CHECK_EQ(EVP_PKEY_set1_EC_KEY(pkey.get(), ec.get()), 1); |
950 |
|||
951 |
156 |
return KeyObjectData::CreateAsymmetric(type, ManagedEVPPKey(std::move(pkey))); |
|
952 |
} |
||
953 |
|||
954 |
641 |
Maybe<bool> GetEcKeyDetail( |
|
955 |
Environment* env, |
||
956 |
std::shared_ptr<KeyObjectData> key, |
||
957 |
Local<Object> target) { |
||
958 |
1282 |
ManagedEVPPKey m_pkey = key->GetAsymmetricKey(); |
|
959 |
641 |
Mutex::ScopedLock lock(*m_pkey.mutex()); |
|
960 |
✗✓ | 641 |
CHECK_EQ(EVP_PKEY_id(m_pkey.get()), EVP_PKEY_EC); |
961 |
|||
962 |
641 |
const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(m_pkey.get()); |
|
963 |
✗✓ | 641 |
CHECK_NOT_NULL(ec); |
964 |
|||
965 |
641 |
const EC_GROUP* group = EC_KEY_get0_group(ec); |
|
966 |
641 |
int nid = EC_GROUP_get_curve_name(group); |
|
967 |
|||
968 |
return target->Set( |
||
969 |
env->context(), |
||
970 |
env->named_curve_string(), |
||
971 |
1923 |
OneByteString(env->isolate(), OBJ_nid2sn(nid))); |
|
972 |
} |
||
973 |
|||
974 |
// WebCrypto requires a different format for ECDSA signatures than |
||
975 |
// what OpenSSL produces, so we need to convert between them. The |
||
976 |
// implementation here is a adapted from Chromium's impl here: |
||
977 |
// https://github.com/chromium/chromium/blob/7af6cfd/components/webcrypto/algorithms/ecdsa.cc |
||
978 |
|||
979 |
size_t GroupOrderSize(const ManagedEVPPKey& key) { |
||
980 |
const EC_KEY* ec = EVP_PKEY_get0_EC_KEY(key.get()); |
||
981 |
CHECK_NOT_NULL(ec); |
||
982 |
const EC_GROUP* group = EC_KEY_get0_group(ec); |
||
983 |
BignumPointer order(BN_new()); |
||
984 |
CHECK(EC_GROUP_get_order(group, order.get(), nullptr)); |
||
985 |
return BN_num_bytes(order.get()); |
||
986 |
} |
||
987 |
} // namespace crypto |
||
988 |
} // namespace node |
Generated by: GCOVR (Version 4.2) |