1 |
|
|
#include "crypto/crypto_hmac.h" |
2 |
|
|
#include "async_wrap-inl.h" |
3 |
|
|
#include "base_object-inl.h" |
4 |
|
|
#include "crypto/crypto_keys.h" |
5 |
|
|
#include "crypto/crypto_sig.h" |
6 |
|
|
#include "crypto/crypto_util.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 |
|
|
namespace node { |
15 |
|
|
|
16 |
|
|
using v8::FunctionCallbackInfo; |
17 |
|
|
using v8::FunctionTemplate; |
18 |
|
|
using v8::HandleScope; |
19 |
|
|
using v8::Isolate; |
20 |
|
|
using v8::Just; |
21 |
|
|
using v8::Local; |
22 |
|
|
using v8::Maybe; |
23 |
|
|
using v8::MaybeLocal; |
24 |
|
|
using v8::Nothing; |
25 |
|
|
using v8::Object; |
26 |
|
|
using v8::Uint32; |
27 |
|
|
using v8::Value; |
28 |
|
|
|
29 |
|
|
namespace crypto { |
30 |
|
166 |
Hmac::Hmac(Environment* env, Local<Object> wrap) |
31 |
|
|
: BaseObject(env, wrap), |
32 |
|
166 |
ctx_(nullptr) { |
33 |
|
166 |
MakeWeak(); |
34 |
|
166 |
} |
35 |
|
|
|
36 |
|
|
void Hmac::MemoryInfo(MemoryTracker* tracker) const { |
37 |
|
|
tracker->TrackFieldWithSize("context", ctx_ ? kSizeOf_HMAC_CTX : 0); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
779 |
void Hmac::Initialize(Environment* env, Local<Object> target) { |
41 |
|
779 |
Isolate* isolate = env->isolate(); |
42 |
|
779 |
Local<FunctionTemplate> t = NewFunctionTemplate(isolate, New); |
43 |
|
|
|
44 |
|
1558 |
t->InstanceTemplate()->SetInternalFieldCount( |
45 |
|
|
Hmac::kInternalFieldCount); |
46 |
|
779 |
t->Inherit(BaseObject::GetConstructorTemplate(env)); |
47 |
|
|
|
48 |
|
779 |
SetProtoMethod(isolate, t, "init", HmacInit); |
49 |
|
779 |
SetProtoMethod(isolate, t, "update", HmacUpdate); |
50 |
|
779 |
SetProtoMethod(isolate, t, "digest", HmacDigest); |
51 |
|
|
|
52 |
|
779 |
SetConstructorFunction(env->context(), target, "Hmac", t); |
53 |
|
|
|
54 |
|
779 |
HmacJob::Initialize(env, target); |
55 |
|
779 |
} |
56 |
|
|
|
57 |
|
5419 |
void Hmac::RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
58 |
|
5419 |
registry->Register(New); |
59 |
|
5419 |
registry->Register(HmacInit); |
60 |
|
5419 |
registry->Register(HmacUpdate); |
61 |
|
5419 |
registry->Register(HmacDigest); |
62 |
|
5419 |
HmacJob::RegisterExternalReferences(registry); |
63 |
|
5419 |
} |
64 |
|
|
|
65 |
|
166 |
void Hmac::New(const FunctionCallbackInfo<Value>& args) { |
66 |
|
166 |
Environment* env = Environment::GetCurrent(args); |
67 |
|
166 |
new Hmac(env, args.This()); |
68 |
|
166 |
} |
69 |
|
|
|
70 |
|
166 |
void Hmac::HmacInit(const char* hash_type, const char* key, int key_len) { |
71 |
|
166 |
HandleScope scope(env()->isolate()); |
72 |
|
|
|
73 |
|
166 |
const EVP_MD* md = EVP_get_digestbyname(hash_type); |
74 |
✓✓ |
166 |
if (md == nullptr) |
75 |
|
1 |
return THROW_ERR_CRYPTO_INVALID_DIGEST(env()); |
76 |
✓✓ |
165 |
if (key_len == 0) { |
77 |
|
8 |
key = ""; |
78 |
|
|
} |
79 |
|
165 |
ctx_.reset(HMAC_CTX_new()); |
80 |
✓✗✗✓ ✗✓ |
165 |
if (!ctx_ || !HMAC_Init_ex(ctx_.get(), key, key_len, md, nullptr)) { |
81 |
|
|
ctx_.reset(); |
82 |
|
|
return ThrowCryptoError(env(), ERR_get_error()); |
83 |
|
|
} |
84 |
|
|
} |
85 |
|
|
|
86 |
|
166 |
void Hmac::HmacInit(const FunctionCallbackInfo<Value>& args) { |
87 |
|
|
Hmac* hmac; |
88 |
✗✓ |
166 |
ASSIGN_OR_RETURN_UNWRAP(&hmac, args.Holder()); |
89 |
|
166 |
Environment* env = hmac->env(); |
90 |
|
|
|
91 |
|
332 |
const node::Utf8Value hash_type(env->isolate(), args[0]); |
92 |
|
332 |
ByteSource key = ByteSource::FromSecretKeyBytes(env, args[1]); |
93 |
|
166 |
hmac->HmacInit(*hash_type, key.data<char>(), key.size()); |
94 |
|
|
} |
95 |
|
|
|
96 |
|
164 |
bool Hmac::HmacUpdate(const char* data, size_t len) { |
97 |
✓✗✓✗
|
164 |
return ctx_ && HMAC_Update(ctx_.get(), |
98 |
|
|
reinterpret_cast<const unsigned char*>(data), |
99 |
|
328 |
len) == 1; |
100 |
|
|
} |
101 |
|
|
|
102 |
|
164 |
void Hmac::HmacUpdate(const FunctionCallbackInfo<Value>& args) { |
103 |
|
164 |
Decode<Hmac>(args, [](Hmac* hmac, const FunctionCallbackInfo<Value>& args, |
104 |
|
164 |
const char* data, size_t size) { |
105 |
|
164 |
Environment* env = Environment::GetCurrent(args); |
106 |
✗✓ |
164 |
if (UNLIKELY(size > INT_MAX)) |
107 |
|
|
return THROW_ERR_OUT_OF_RANGE(env, "data is too long"); |
108 |
|
164 |
bool r = hmac->HmacUpdate(data, size); |
109 |
✓✗ |
328 |
args.GetReturnValue().Set(r); |
110 |
|
|
}); |
111 |
|
164 |
} |
112 |
|
|
|
113 |
|
159 |
void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) { |
114 |
|
159 |
Environment* env = Environment::GetCurrent(args); |
115 |
|
|
|
116 |
|
|
Hmac* hmac; |
117 |
✗✓ |
159 |
ASSIGN_OR_RETURN_UNWRAP(&hmac, args.Holder()); |
118 |
|
|
|
119 |
|
159 |
enum encoding encoding = BUFFER; |
120 |
✓✓ |
159 |
if (args.Length() >= 1) { |
121 |
|
131 |
encoding = ParseEncoding(env->isolate(), args[0], BUFFER); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
unsigned char md_value[EVP_MAX_MD_SIZE]; |
125 |
|
159 |
unsigned int md_len = 0; |
126 |
|
|
|
127 |
✓✗ |
159 |
if (hmac->ctx_) { |
128 |
|
159 |
bool ok = HMAC_Final(hmac->ctx_.get(), md_value, &md_len); |
129 |
|
159 |
hmac->ctx_.reset(); |
130 |
✗✓ |
159 |
if (!ok) { |
131 |
|
|
return ThrowCryptoError(env, ERR_get_error(), "Failed to finalize HMAC"); |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
Local<Value> error; |
136 |
|
|
MaybeLocal<Value> rc = |
137 |
|
|
StringBytes::Encode(env->isolate(), |
138 |
|
|
reinterpret_cast<const char*>(md_value), |
139 |
|
|
md_len, |
140 |
|
|
encoding, |
141 |
|
159 |
&error); |
142 |
✗✓ |
159 |
if (rc.IsEmpty()) { |
143 |
|
|
CHECK(!error.IsEmpty()); |
144 |
|
|
env->isolate()->ThrowException(error); |
145 |
|
|
return; |
146 |
|
|
} |
147 |
|
477 |
args.GetReturnValue().Set(rc.FromMaybe(Local<Value>())); |
148 |
|
|
} |
149 |
|
|
|
150 |
|
102 |
HmacConfig::HmacConfig(HmacConfig&& other) noexcept |
151 |
|
102 |
: job_mode(other.job_mode), |
152 |
|
102 |
mode(other.mode), |
153 |
|
102 |
key(std::move(other.key)), |
154 |
|
102 |
data(std::move(other.data)), |
155 |
|
102 |
signature(std::move(other.signature)), |
156 |
|
102 |
digest(other.digest) {} |
157 |
|
|
|
158 |
|
|
HmacConfig& HmacConfig::operator=(HmacConfig&& other) noexcept { |
159 |
|
|
if (&other == this) return *this; |
160 |
|
|
this->~HmacConfig(); |
161 |
|
|
return *new (this) HmacConfig(std::move(other)); |
162 |
|
|
} |
163 |
|
|
|
164 |
|
|
void HmacConfig::MemoryInfo(MemoryTracker* tracker) const { |
165 |
|
|
tracker->TrackField("key", key.get()); |
166 |
|
|
// If the job is sync, then the HmacConfig does not own the data |
167 |
|
|
if (job_mode == kCryptoJobAsync) { |
168 |
|
|
tracker->TrackFieldWithSize("data", data.size()); |
169 |
|
|
tracker->TrackFieldWithSize("signature", signature.size()); |
170 |
|
|
} |
171 |
|
|
} |
172 |
|
|
|
173 |
|
102 |
Maybe<bool> HmacTraits::AdditionalConfig( |
174 |
|
|
CryptoJobMode mode, |
175 |
|
|
const FunctionCallbackInfo<Value>& args, |
176 |
|
|
unsigned int offset, |
177 |
|
|
HmacConfig* params) { |
178 |
|
102 |
Environment* env = Environment::GetCurrent(args); |
179 |
|
|
|
180 |
|
102 |
params->job_mode = mode; |
181 |
|
|
|
182 |
✓✗✗✓
|
204 |
CHECK(args[offset]->IsUint32()); // SignConfiguration::Mode |
183 |
|
102 |
params->mode = |
184 |
✓✗ |
306 |
static_cast<SignConfiguration::Mode>(args[offset].As<Uint32>()->Value()); |
185 |
|
|
|
186 |
✓✗✗✓
|
306 |
CHECK(args[offset + 1]->IsString()); // Hash |
187 |
✓✗✗✓
|
204 |
CHECK(args[offset + 2]->IsObject()); // Key |
188 |
|
|
|
189 |
✓✗ |
306 |
Utf8Value digest(env->isolate(), args[offset + 1]); |
190 |
|
102 |
params->digest = EVP_get_digestbyname(*digest); |
191 |
✗✓ |
102 |
if (params->digest == nullptr) { |
192 |
|
|
THROW_ERR_CRYPTO_INVALID_DIGEST(env); |
193 |
|
|
return Nothing<bool>(); |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
KeyObjectHandle* key; |
197 |
✓✗✗✓
|
204 |
ASSIGN_OR_RETURN_UNWRAP(&key, args[offset + 2], Nothing<bool>()); |
198 |
|
102 |
params->key = key->Data(); |
199 |
|
|
|
200 |
✓✗ |
204 |
ArrayBufferOrViewContents<char> data(args[offset + 3]); |
201 |
✗✓ |
102 |
if (UNLIKELY(!data.CheckSizeInt32())) { |
202 |
|
|
THROW_ERR_OUT_OF_RANGE(env, "data is too big"); |
203 |
|
|
return Nothing<bool>(); |
204 |
|
|
} |
205 |
|
|
params->data = mode == kCryptoJobAsync |
206 |
✓✗ |
204 |
? data.ToCopy() |
207 |
|
102 |
: data.ToByteSource(); |
208 |
|
|
|
209 |
✓✗✓✓
|
306 |
if (!args[offset + 4]->IsUndefined()) { |
210 |
✓✗ |
148 |
ArrayBufferOrViewContents<char> signature(args[offset + 4]); |
211 |
✗✓ |
74 |
if (UNLIKELY(!signature.CheckSizeInt32())) { |
212 |
|
|
THROW_ERR_OUT_OF_RANGE(env, "signature is too big"); |
213 |
|
|
return Nothing<bool>(); |
214 |
|
|
} |
215 |
|
|
params->signature = mode == kCryptoJobAsync |
216 |
✓✗ |
148 |
? signature.ToCopy() |
217 |
|
74 |
: signature.ToByteSource(); |
218 |
|
|
} |
219 |
|
|
|
220 |
|
102 |
return Just(true); |
221 |
|
|
} |
222 |
|
|
|
223 |
|
102 |
bool HmacTraits::DeriveBits( |
224 |
|
|
Environment* env, |
225 |
|
|
const HmacConfig& params, |
226 |
|
|
ByteSource* out) { |
227 |
|
204 |
HMACCtxPointer ctx(HMAC_CTX_new()); |
228 |
|
|
|
229 |
✓✗✗✓ ✗✓ |
204 |
if (!ctx || |
230 |
|
102 |
!HMAC_Init_ex( |
231 |
|
|
ctx.get(), |
232 |
|
102 |
params.key->GetSymmetricKey(), |
233 |
|
102 |
params.key->GetSymmetricKeySize(), |
234 |
|
102 |
params.digest, |
235 |
|
|
nullptr)) { |
236 |
|
|
return false; |
237 |
|
|
} |
238 |
|
|
|
239 |
✗✓ |
102 |
if (!HMAC_Update( |
240 |
|
|
ctx.get(), |
241 |
|
|
params.data.data<unsigned char>(), |
242 |
|
|
params.data.size())) { |
243 |
|
|
return false; |
244 |
|
|
} |
245 |
|
|
|
246 |
|
204 |
ByteSource::Builder buf(EVP_MAX_MD_SIZE); |
247 |
|
|
unsigned int len; |
248 |
|
|
|
249 |
✗✓ |
102 |
if (!HMAC_Final(ctx.get(), buf.data<unsigned char>(), &len)) { |
250 |
|
|
return false; |
251 |
|
|
} |
252 |
|
|
|
253 |
|
102 |
*out = std::move(buf).release(len); |
254 |
|
|
|
255 |
|
102 |
return true; |
256 |
|
|
} |
257 |
|
|
|
258 |
|
102 |
Maybe<bool> HmacTraits::EncodeOutput( |
259 |
|
|
Environment* env, |
260 |
|
|
const HmacConfig& params, |
261 |
|
|
ByteSource* out, |
262 |
|
|
Local<Value>* result) { |
263 |
✓✓✗ |
102 |
switch (params.mode) { |
264 |
|
28 |
case SignConfiguration::kSign: |
265 |
|
28 |
*result = out->ToArrayBuffer(env); |
266 |
|
28 |
break; |
267 |
|
74 |
case SignConfiguration::kVerify: |
268 |
✓✗ |
120 |
*result = |
269 |
✓✓ |
148 |
out->size() > 0 && out->size() == params.signature.size() && |
270 |
✓✓ |
66 |
memcmp(out->data(), params.signature.data(), out->size()) == 0 |
271 |
|
46 |
? v8::True(env->isolate()) |
272 |
|
28 |
: v8::False(env->isolate()); |
273 |
|
74 |
break; |
274 |
|
|
default: |
275 |
|
|
UNREACHABLE(); |
276 |
|
|
} |
277 |
|
102 |
return Just(!result->IsEmpty()); |
278 |
|
|
} |
279 |
|
|
|
280 |
|
|
} // namespace crypto |
281 |
|
|
} // namespace node |