GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "crypto/crypto_hash.h" |
||
2 |
#include "allocated_buffer-inl.h" |
||
3 |
#include "async_wrap-inl.h" |
||
4 |
#include "base_object-inl.h" |
||
5 |
#include "env-inl.h" |
||
6 |
#include "memory_tracker-inl.h" |
||
7 |
#include "string_bytes.h" |
||
8 |
#include "threadpoolwork-inl.h" |
||
9 |
#include "v8.h" |
||
10 |
|||
11 |
#include <cstdio> |
||
12 |
|||
13 |
namespace node { |
||
14 |
|||
15 |
using v8::FunctionCallbackInfo; |
||
16 |
using v8::FunctionTemplate; |
||
17 |
using v8::Just; |
||
18 |
using v8::Local; |
||
19 |
using v8::Maybe; |
||
20 |
using v8::MaybeLocal; |
||
21 |
using v8::Nothing; |
||
22 |
using v8::Object; |
||
23 |
using v8::Uint32; |
||
24 |
using v8::Value; |
||
25 |
|||
26 |
namespace crypto { |
||
27 |
1188 |
Hash::Hash(Environment* env, Local<Object> wrap) : BaseObject(env, wrap) { |
|
28 |
1188 |
MakeWeak(); |
|
29 |
1188 |
} |
|
30 |
|||
31 |
void Hash::MemoryInfo(MemoryTracker* tracker) const { |
||
32 |
tracker->TrackFieldWithSize("mdctx", mdctx_ ? kSizeOf_EVP_MD_CTX : 0); |
||
33 |
tracker->TrackFieldWithSize("md", digest_ ? md_len_ : 0); |
||
34 |
} |
||
35 |
|||
36 |
6 |
void Hash::GetHashes(const FunctionCallbackInfo<Value>& args) { |
|
37 |
6 |
Environment* env = Environment::GetCurrent(args); |
|
38 |
6 |
CipherPushContext ctx(env); |
|
39 |
6 |
EVP_MD_do_all_sorted(array_push_back<EVP_MD>, &ctx); |
|
40 |
12 |
args.GetReturnValue().Set(ctx.ToJSArray()); |
|
41 |
6 |
} |
|
42 |
|||
43 |
4298 |
void Hash::Initialize(Environment* env, Local<Object> target) { |
|
44 |
4298 |
Local<FunctionTemplate> t = env->NewFunctionTemplate(New); |
|
45 |
|||
46 |
8596 |
t->InstanceTemplate()->SetInternalFieldCount( |
|
47 |
Hash::kInternalFieldCount); |
||
48 |
4298 |
t->Inherit(BaseObject::GetConstructorTemplate(env)); |
|
49 |
|||
50 |
4298 |
env->SetProtoMethod(t, "update", HashUpdate); |
|
51 |
4298 |
env->SetProtoMethod(t, "digest", HashDigest); |
|
52 |
|||
53 |
4298 |
env->SetConstructorFunction(target, "Hash", t); |
|
54 |
|||
55 |
4298 |
env->SetMethodNoSideEffect(target, "getHashes", GetHashes); |
|
56 |
|||
57 |
4298 |
HashJob::Initialize(env, target); |
|
58 |
4298 |
} |
|
59 |
|||
60 |
1188 |
void Hash::New(const FunctionCallbackInfo<Value>& args) { |
|
61 |
1188 |
Environment* env = Environment::GetCurrent(args); |
|
62 |
|||
63 |
1188 |
const Hash* orig = nullptr; |
|
64 |
✓✗ | 1188 |
const EVP_MD* md = nullptr; |
65 |
|||
66 |
✓✓ | 1188 |
if (args[0]->IsObject()) { |
67 |
✗✓ | 13 |
ASSIGN_OR_RETURN_UNWRAP(&orig, args[0].As<Object>()); |
68 |
5 |
md = EVP_MD_CTX_md(orig->mdctx_.get()); |
|
69 |
} else { |
||
70 |
2366 |
const Utf8Value hash_type(env->isolate(), args[0]); |
|
71 |
1183 |
md = EVP_get_digestbyname(*hash_type); |
|
72 |
} |
||
73 |
|||
74 |
1188 |
Maybe<unsigned int> xof_md_len = Nothing<unsigned int>(); |
|
75 |
✓✓ | 2376 |
if (!args[1]->IsUndefined()) { |
76 |
✗✓ | 13 |
CHECK(args[1]->IsUint32()); |
77 |
26 |
xof_md_len = Just<unsigned int>(args[1].As<Uint32>()->Value()); |
|
78 |
} |
||
79 |
|||
80 |
1188 |
Hash* hash = new Hash(env, args.This()); |
|
81 |
✓✓✓✓ ✓✓ |
1188 |
if (md == nullptr || !hash->HashInit(md, xof_md_len)) { |
82 |
3 |
return ThrowCryptoError(env, ERR_get_error(), |
|
83 |
3 |
"Digest method not supported"); |
|
84 |
} |
||
85 |
|||
86 |
✓✓✗✓ ✗✓ |
1190 |
if (orig != nullptr && |
87 |
5 |
0 >= EVP_MD_CTX_copy(hash->mdctx_.get(), orig->mdctx_.get())) { |
|
88 |
return ThrowCryptoError(env, ERR_get_error(), "Digest copy error"); |
||
89 |
} |
||
90 |
} |
||
91 |
|||
92 |
1186 |
bool Hash::HashInit(const EVP_MD* md, Maybe<unsigned int> xof_md_len) { |
|
93 |
1186 |
mdctx_.reset(EVP_MD_CTX_new()); |
|
94 |
✓✗✗✓ ✗✓ |
1186 |
if (!mdctx_ || EVP_DigestInit_ex(mdctx_.get(), md, nullptr) <= 0) { |
95 |
mdctx_.reset(); |
||
96 |
return false; |
||
97 |
} |
||
98 |
|||
99 |
1186 |
md_len_ = EVP_MD_size(md); |
|
100 |
✓✓✓✓ ✓✓ |
1199 |
if (xof_md_len.IsJust() && xof_md_len.FromJust() != md_len_) { |
101 |
// This is a little hack to cause createHash to fail when an incorrect |
||
102 |
// hashSize option was passed for a non-XOF hash function. |
||
103 |
✓✓ | 12 |
if ((EVP_MD_flags(md) & EVP_MD_FLAG_XOF) == 0) { |
104 |
1 |
EVPerr(EVP_F_EVP_DIGESTFINALXOF, EVP_R_NOT_XOF_OR_INVALID_LENGTH); |
|
105 |
1 |
return false; |
|
106 |
} |
||
107 |
11 |
md_len_ = xof_md_len.FromJust(); |
|
108 |
} |
||
109 |
|||
110 |
1185 |
return true; |
|
111 |
} |
||
112 |
|||
113 |
1402 |
bool Hash::HashUpdate(const char* data, size_t len) { |
|
114 |
✗✓ | 1402 |
if (!mdctx_) |
115 |
return false; |
||
116 |
1402 |
EVP_DigestUpdate(mdctx_.get(), data, len); |
|
117 |
1402 |
return true; |
|
118 |
} |
||
119 |
|||
120 |
1402 |
void Hash::HashUpdate(const FunctionCallbackInfo<Value>& args) { |
|
121 |
1402 |
Decode<Hash>(args, [](Hash* hash, const FunctionCallbackInfo<Value>& args, |
|
122 |
1402 |
const char* data, size_t size) { |
|
123 |
1402 |
Environment* env = Environment::GetCurrent(args); |
|
124 |
✗✓ | 1402 |
if (UNLIKELY(size > INT_MAX)) |
125 |
return THROW_ERR_OUT_OF_RANGE(env, "data is too long"); |
||
126 |
1402 |
bool r = hash->HashUpdate(data, size); |
|
127 |
✓✗ | 2804 |
args.GetReturnValue().Set(r); |
128 |
}); |
||
129 |
1402 |
} |
|
130 |
|||
131 |
1169 |
void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) { |
|
132 |
1169 |
Environment* env = Environment::GetCurrent(args); |
|
133 |
|||
134 |
Hash* hash; |
||
135 |
✗✓ | 1169 |
ASSIGN_OR_RETURN_UNWRAP(&hash, args.Holder()); |
136 |
|||
137 |
1169 |
enum encoding encoding = BUFFER; |
|
138 |
✓✓ | 1169 |
if (args.Length() >= 1) { |
139 |
1157 |
encoding = ParseEncoding(env->isolate(), args[0], BUFFER); |
|
140 |
} |
||
141 |
|||
142 |
1169 |
unsigned int len = hash->md_len_; |
|
143 |
|||
144 |
// TODO(tniessen): SHA3_squeeze does not work for zero-length outputs on all |
||
145 |
// platforms and will cause a segmentation fault if called. This workaround |
||
146 |
// causes hash.digest() to correctly return an empty buffer / string. |
||
147 |
// See https://github.com/openssl/openssl/issues/9431. |
||
148 |
|||
149 |
✓✓✓✓ ✓✓ |
1169 |
if (!hash->digest_ && len > 0) { |
150 |
// Some hash algorithms such as SHA3 do not support calling |
||
151 |
// EVP_DigestFinal_ex more than once, however, Hash._flush |
||
152 |
// and Hash.digest can both be used to retrieve the digest, |
||
153 |
// so we need to cache it. |
||
154 |
// See https://github.com/nodejs/node/issues/28245. |
||
155 |
|||
156 |
1166 |
char* md_value = MallocOpenSSL<char>(len); |
|
157 |
1166 |
ByteSource digest = ByteSource::Allocated(md_value, len); |
|
158 |
|||
159 |
1166 |
size_t default_len = EVP_MD_CTX_size(hash->mdctx_.get()); |
|
160 |
int ret; |
||
161 |
✓✓ | 1166 |
if (len == default_len) { |
162 |
1160 |
ret = EVP_DigestFinal_ex( |
|
163 |
1160 |
hash->mdctx_.get(), |
|
164 |
reinterpret_cast<unsigned char*>(md_value), |
||
165 |
&len); |
||
166 |
// The output length should always equal hash->md_len_ |
||
167 |
✗✓ | 1160 |
CHECK_EQ(len, hash->md_len_); |
168 |
} else { |
||
169 |
6 |
ret = EVP_DigestFinalXOF( |
|
170 |
6 |
hash->mdctx_.get(), |
|
171 |
reinterpret_cast<unsigned char*>(md_value), |
||
172 |
len); |
||
173 |
} |
||
174 |
|||
175 |
✗✓ | 1166 |
if (ret != 1) |
176 |
return ThrowCryptoError(env, ERR_get_error()); |
||
177 |
|||
178 |
1166 |
hash->digest_ = std::move(digest); |
|
179 |
} |
||
180 |
|||
181 |
Local<Value> error; |
||
182 |
MaybeLocal<Value> rc = |
||
183 |
StringBytes::Encode(env->isolate(), |
||
184 |
1169 |
hash->digest_.get(), |
|
185 |
len, |
||
186 |
encoding, |
||
187 |
1169 |
&error); |
|
188 |
✗✓ | 1169 |
if (rc.IsEmpty()) { |
189 |
CHECK(!error.IsEmpty()); |
||
190 |
env->isolate()->ThrowException(error); |
||
191 |
return; |
||
192 |
} |
||
193 |
3507 |
args.GetReturnValue().Set(rc.FromMaybe(Local<Value>())); |
|
194 |
} |
||
195 |
|||
196 |
141 |
HashConfig::HashConfig(HashConfig&& other) noexcept |
|
197 |
141 |
: mode(other.mode), |
|
198 |
141 |
in(std::move(other.in)), |
|
199 |
141 |
digest(other.digest), |
|
200 |
141 |
length(other.length) {} |
|
201 |
|||
202 |
HashConfig& HashConfig::operator=(HashConfig&& other) noexcept { |
||
203 |
if (&other == this) return *this; |
||
204 |
this->~HashConfig(); |
||
205 |
return *new (this) HashConfig(std::move(other)); |
||
206 |
} |
||
207 |
|||
208 |
void HashConfig::MemoryInfo(MemoryTracker* tracker) const { |
||
209 |
// If the Job is sync, then the HashConfig does not own the data. |
||
210 |
if (mode == kCryptoJobAsync) |
||
211 |
tracker->TrackFieldWithSize("in", in.size()); |
||
212 |
} |
||
213 |
|||
214 |
141 |
Maybe<bool> HashTraits::EncodeOutput( |
|
215 |
Environment* env, |
||
216 |
const HashConfig& params, |
||
217 |
ByteSource* out, |
||
218 |
v8::Local<v8::Value>* result) { |
||
219 |
282 |
*result = out->ToArrayBuffer(env); |
|
220 |
141 |
return Just(!result->IsEmpty()); |
|
221 |
} |
||
222 |
|||
223 |
154 |
Maybe<bool> HashTraits::AdditionalConfig( |
|
224 |
CryptoJobMode mode, |
||
225 |
const FunctionCallbackInfo<Value>& args, |
||
226 |
unsigned int offset, |
||
227 |
HashConfig* params) { |
||
228 |
154 |
Environment* env = Environment::GetCurrent(args); |
|
229 |
|||
230 |
154 |
params->mode = mode; |
|
231 |
|||
232 |
✓✗✗✓ |
462 |
CHECK(args[offset]->IsString()); // Hash algorithm |
233 |
✓✗ | 462 |
Utf8Value digest(env->isolate(), args[offset]); |
234 |
154 |
params->digest = EVP_get_digestbyname(*digest); |
|
235 |
✓✓ | 154 |
if (UNLIKELY(params->digest == nullptr)) { |
236 |
12 |
THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Invalid digest: %s", *digest); |
|
237 |
12 |
return Nothing<bool>(); |
|
238 |
} |
||
239 |
|||
240 |
✓✗ | 426 |
ArrayBufferOrViewContents<char> data(args[offset + 1]); |
241 |
✗✓ | 142 |
if (UNLIKELY(!data.CheckSizeInt32())) { |
242 |
THROW_ERR_OUT_OF_RANGE(env, "data is too big"); |
||
243 |
return Nothing<bool>(); |
||
244 |
} |
||
245 |
params->in = mode == kCryptoJobAsync |
||
246 |
✓✗ | 284 |
? data.ToCopy() |
247 |
142 |
: data.ToByteSource(); |
|
248 |
|||
249 |
142 |
unsigned int expected = EVP_MD_size(params->digest); |
|
250 |
142 |
params->length = expected; |
|
251 |
✓✗✓✓ |
284 |
if (UNLIKELY(args[offset + 2]->IsUint32())) { |
252 |
// length is expressed in terms of bits |
||
253 |
5 |
params->length = |
|
254 |
✓✗ | 10 |
static_cast<uint32_t>(args[offset + 2] |
255 |
5 |
.As<Uint32>()->Value()) / CHAR_BIT; |
|
256 |
✓✓ | 5 |
if (params->length != expected) { |
257 |
✓✗ | 1 |
if ((EVP_MD_flags(params->digest) & EVP_MD_FLAG_XOF) == 0) { |
258 |
1 |
THROW_ERR_CRYPTO_INVALID_DIGEST(env, "Digest method not supported"); |
|
259 |
1 |
return Nothing<bool>(); |
|
260 |
} |
||
261 |
} |
||
262 |
} |
||
263 |
|||
264 |
141 |
return Just(true); |
|
265 |
} |
||
266 |
|||
267 |
141 |
bool HashTraits::DeriveBits( |
|
268 |
Environment* env, |
||
269 |
const HashConfig& params, |
||
270 |
ByteSource* out) { |
||
271 |
282 |
EVPMDPointer ctx(EVP_MD_CTX_new()); |
|
272 |
|||
273 |
423 |
if (UNLIKELY(!ctx || |
|
274 |
EVP_DigestInit_ex(ctx.get(), params.digest, nullptr) <= 0 || |
||
275 |
EVP_DigestUpdate( |
||
276 |
ctx.get(), |
||
277 |
params.in.get(), |
||
278 |
✓✗✗✓ ✓✗✗✓ ✗✓ |
423 |
params.in.size()) <= 0)) { |
279 |
return false; |
||
280 |
} |
||
281 |
|||
282 |
✓✗ | 141 |
if (LIKELY(params.length > 0)) { |
283 |
141 |
unsigned int length = params.length; |
|
284 |
141 |
char* data = MallocOpenSSL<char>(length); |
|
285 |
141 |
ByteSource buf = ByteSource::Allocated(data, length); |
|
286 |
141 |
unsigned char* ptr = reinterpret_cast<unsigned char*>(data); |
|
287 |
|||
288 |
141 |
size_t expected = EVP_MD_CTX_size(ctx.get()); |
|
289 |
|||
290 |
141 |
int ret = (length == expected) |
|
291 |
✓✗ | 141 |
? EVP_DigestFinal_ex(ctx.get(), ptr, &length) |
292 |
: EVP_DigestFinalXOF(ctx.get(), ptr, length); |
||
293 |
|||
294 |
✗✓ | 141 |
if (UNLIKELY(ret != 1)) |
295 |
return false; |
||
296 |
|||
297 |
141 |
*out = std::move(buf); |
|
298 |
} |
||
299 |
|||
300 |
141 |
return true; |
|
301 |
} |
||
302 |
|||
303 |
} // namespace crypto |
||
304 |
} // namespace node |
Generated by: GCOVR (Version 4.2) |