GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "crypto/crypto_util.h" |
||
2 |
#include "async_wrap-inl.h" |
||
3 |
#include "crypto/crypto_bio.h" |
||
4 |
#include "crypto/crypto_keys.h" |
||
5 |
#include "env-inl.h" |
||
6 |
#include "memory_tracker-inl.h" |
||
7 |
#include "node_buffer.h" |
||
8 |
#include "node_options-inl.h" |
||
9 |
#include "string_bytes.h" |
||
10 |
#include "threadpoolwork-inl.h" |
||
11 |
#include "util-inl.h" |
||
12 |
#include "v8.h" |
||
13 |
|||
14 |
#include "math.h" |
||
15 |
|||
16 |
#if OPENSSL_VERSION_MAJOR >= 3 |
||
17 |
#include "openssl/provider.h" |
||
18 |
#endif |
||
19 |
|||
20 |
#include <openssl/rand.h> |
||
21 |
|||
22 |
namespace node { |
||
23 |
|||
24 |
using v8::ArrayBuffer; |
||
25 |
using v8::BackingStore; |
||
26 |
using v8::BigInt; |
||
27 |
using v8::Context; |
||
28 |
using v8::Exception; |
||
29 |
using v8::FunctionCallbackInfo; |
||
30 |
using v8::HandleScope; |
||
31 |
using v8::Isolate; |
||
32 |
using v8::Just; |
||
33 |
using v8::Local; |
||
34 |
using v8::Maybe; |
||
35 |
using v8::MaybeLocal; |
||
36 |
using v8::NewStringType; |
||
37 |
using v8::Nothing; |
||
38 |
using v8::Object; |
||
39 |
using v8::String; |
||
40 |
using v8::TryCatch; |
||
41 |
using v8::Uint32; |
||
42 |
using v8::Uint8Array; |
||
43 |
using v8::Value; |
||
44 |
|||
45 |
namespace crypto { |
||
46 |
2248 |
int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx) { |
|
47 |
// From https://www.openssl.org/docs/man1.1.1/man3/SSL_verify_cb: |
||
48 |
// |
||
49 |
// If VerifyCallback returns 1, the verification process is continued. If |
||
50 |
// VerifyCallback always returns 1, the TLS/SSL handshake will not be |
||
51 |
// terminated with respect to verification failures and the connection will |
||
52 |
// be established. The calling process can however retrieve the error code |
||
53 |
// of the last verification error using SSL_get_verify_result(3) or by |
||
54 |
// maintaining its own error storage managed by VerifyCallback. |
||
55 |
// |
||
56 |
// Since we cannot perform I/O quickly enough with X509_STORE_CTX_ APIs in |
||
57 |
// this callback, we ignore all preverify_ok errors and let the handshake |
||
58 |
// continue. It is imperative that the user use Connection::VerifyError after |
||
59 |
// the 'secure' callback has been made. |
||
60 |
2248 |
return 1; |
|
61 |
} |
||
62 |
|||
63 |
21175 |
void CheckEntropy() { |
|
64 |
for (;;) { |
||
65 |
21175 |
int status = RAND_status(); |
|
66 |
✗✓ | 21175 |
CHECK_GE(status, 0); // Cannot fail. |
67 |
✓✗ | 21175 |
if (status != 0) |
68 |
21175 |
break; |
|
69 |
|||
70 |
// Give up, RAND_poll() not supported. |
||
71 |
if (RAND_poll() == 0) |
||
72 |
break; |
||
73 |
} |
||
74 |
21175 |
} |
|
75 |
|||
76 |
18756 |
bool EntropySource(unsigned char* buffer, size_t length) { |
|
77 |
// Ensure that OpenSSL's PRNG is properly seeded. |
||
78 |
18756 |
CheckEntropy(); |
|
79 |
// RAND_bytes() can return 0 to indicate that the entropy data is not truly |
||
80 |
// random. That's okay, it's still better than V8's stock source of entropy, |
||
81 |
// which is /dev/urandom on UNIX platforms and the current time on Windows. |
||
82 |
18756 |
return RAND_bytes(buffer, length) != -1; |
|
83 |
} |
||
84 |
|||
85 |
111 |
int PasswordCallback(char* buf, int size, int rwflag, void* u) { |
|
86 |
111 |
const ByteSource* passphrase = *static_cast<const ByteSource**>(u); |
|
87 |
✓✓ | 111 |
if (passphrase != nullptr) { |
88 |
91 |
size_t buflen = static_cast<size_t>(size); |
|
89 |
91 |
size_t len = passphrase->size(); |
|
90 |
✓✓ | 91 |
if (buflen < len) |
91 |
2 |
return -1; |
|
92 |
89 |
memcpy(buf, passphrase->data(), len); |
|
93 |
89 |
return len; |
|
94 |
} |
||
95 |
|||
96 |
20 |
return -1; |
|
97 |
} |
||
98 |
|||
99 |
// This callback is used to avoid the default passphrase callback in OpenSSL |
||
100 |
// which will typically prompt for the passphrase. The prompting is designed |
||
101 |
// for the OpenSSL CLI, but works poorly for Node.js because it involves |
||
102 |
// synchronous interaction with the controlling terminal, something we never |
||
103 |
// want, and use this function to avoid it. |
||
104 |
int NoPasswordCallback(char* buf, int size, int rwflag, void* u) { |
||
105 |
return 0; |
||
106 |
} |
||
107 |
|||
108 |
5353 |
bool ProcessFipsOptions() { |
|
109 |
/* Override FIPS settings in configuration file, if needed. */ |
||
110 |
✓✓✓✓ |
10705 |
if (per_process::cli_options->enable_fips_crypto || |
111 |
✓✓ | 5352 |
per_process::cli_options->force_fips_crypto) { |
112 |
#if OPENSSL_VERSION_MAJOR >= 3 |
||
113 |
2 |
OSSL_PROVIDER* fips_provider = OSSL_PROVIDER_load(nullptr, "fips"); |
|
114 |
✓✗ | 2 |
if (fips_provider == nullptr) |
115 |
2 |
return false; |
|
116 |
OSSL_PROVIDER_unload(fips_provider); |
||
117 |
|||
118 |
return EVP_default_properties_enable_fips(nullptr, 1) && |
||
119 |
EVP_default_properties_is_fips_enabled(nullptr); |
||
120 |
#else |
||
121 |
return FIPS_mode() == 0 && FIPS_mode_set(1); |
||
122 |
#endif |
||
123 |
} |
||
124 |
5351 |
return true; |
|
125 |
} |
||
126 |
|||
127 |
6101 |
bool InitCryptoOnce(Isolate* isolate) { |
|
128 |
static uv_once_t init_once = UV_ONCE_INIT; |
||
129 |
12202 |
TryCatch try_catch{isolate}; |
|
130 |
6101 |
uv_once(&init_once, InitCryptoOnce); |
|
131 |
✗✓✗✗ ✗✓ |
6101 |
if (try_catch.HasCaught() && !try_catch.HasTerminated()) { |
132 |
try_catch.ReThrow(); |
||
133 |
return false; |
||
134 |
} |
||
135 |
6101 |
return true; |
|
136 |
} |
||
137 |
|||
138 |
// Protect accesses to FIPS state with a mutex. This should potentially |
||
139 |
// be part of a larger mutex for global OpenSSL state. |
||
140 |
static Mutex fips_mutex; |
||
141 |
|||
142 |
5348 |
void InitCryptoOnce() { |
|
143 |
10696 |
Mutex::ScopedLock lock(per_process::cli_options_mutex); |
|
144 |
10696 |
Mutex::ScopedLock fips_lock(fips_mutex); |
|
145 |
#ifndef OPENSSL_IS_BORINGSSL |
||
146 |
5348 |
OPENSSL_INIT_SETTINGS* settings = OPENSSL_INIT_new(); |
|
147 |
|||
148 |
#if OPENSSL_VERSION_MAJOR < 3 |
||
149 |
// --openssl-config=... |
||
150 |
if (!per_process::cli_options->openssl_config.empty()) { |
||
151 |
const char* conf = per_process::cli_options->openssl_config.c_str(); |
||
152 |
OPENSSL_INIT_set_config_filename(settings, conf); |
||
153 |
} |
||
154 |
#endif |
||
155 |
|||
156 |
#if OPENSSL_VERSION_MAJOR >= 3 |
||
157 |
// --openssl-legacy-provider |
||
158 |
✗✓ | 5348 |
if (per_process::cli_options->openssl_legacy_provider) { |
159 |
OSSL_PROVIDER* legacy_provider = OSSL_PROVIDER_load(nullptr, "legacy"); |
||
160 |
if (legacy_provider == nullptr) { |
||
161 |
fprintf(stderr, "Unable to load legacy provider.\n"); |
||
162 |
} |
||
163 |
} |
||
164 |
#endif |
||
165 |
|||
166 |
5348 |
OPENSSL_init_ssl(0, settings); |
|
167 |
5348 |
OPENSSL_INIT_free(settings); |
|
168 |
5348 |
settings = nullptr; |
|
169 |
|||
170 |
#ifndef _WIN32 |
||
171 |
✓✓ | 5348 |
if (per_process::cli_options->secure_heap != 0) { |
172 |
✗✗✓✗ |
1 |
switch (CRYPTO_secure_malloc_init( |
173 |
1 |
per_process::cli_options->secure_heap, |
|
174 |
1 |
static_cast<int>(per_process::cli_options->secure_heap_min))) { |
|
175 |
case 0: |
||
176 |
fprintf(stderr, "Unable to initialize openssl secure heap.\n"); |
||
177 |
break; |
||
178 |
case 2: |
||
179 |
// Not a fatal error but worthy of a warning. |
||
180 |
fprintf(stderr, "Unable to memory map openssl secure heap.\n"); |
||
181 |
break; |
||
182 |
1 |
case 1: |
|
183 |
// OK! |
||
184 |
1 |
break; |
|
185 |
} |
||
186 |
} |
||
187 |
#endif |
||
188 |
|||
189 |
#endif // OPENSSL_IS_BORINGSSL |
||
190 |
|||
191 |
// Turn off compression. Saves memory and protects against CRIME attacks. |
||
192 |
// No-op with OPENSSL_NO_COMP builds of OpenSSL. |
||
193 |
5348 |
sk_SSL_COMP_zero(SSL_COMP_get_compression_methods()); |
|
194 |
|||
195 |
#ifndef OPENSSL_NO_ENGINE |
||
196 |
5348 |
ERR_load_ENGINE_strings(); |
|
197 |
5348 |
ENGINE_load_builtin_engines(); |
|
198 |
#endif // !OPENSSL_NO_ENGINE |
||
199 |
|||
200 |
5348 |
NodeBIO::GetMethod(); |
|
201 |
5348 |
} |
|
202 |
|||
203 |
213 |
void GetFipsCrypto(const FunctionCallbackInfo<Value>& args) { |
|
204 |
426 |
Mutex::ScopedLock lock(per_process::cli_options_mutex); |
|
205 |
213 |
Mutex::ScopedLock fips_lock(fips_mutex); |
|
206 |
|||
207 |
#if OPENSSL_VERSION_MAJOR >= 3 |
||
208 |
✗✓ | 426 |
args.GetReturnValue().Set(EVP_default_properties_is_fips_enabled(nullptr) ? |
209 |
1 : 0); |
||
210 |
#else |
||
211 |
args.GetReturnValue().Set(FIPS_mode() ? 1 : 0); |
||
212 |
#endif |
||
213 |
213 |
} |
|
214 |
|||
215 |
void SetFipsCrypto(const FunctionCallbackInfo<Value>& args) { |
||
216 |
Mutex::ScopedLock lock(per_process::cli_options_mutex); |
||
217 |
Mutex::ScopedLock fips_lock(fips_mutex); |
||
218 |
|||
219 |
CHECK(!per_process::cli_options->force_fips_crypto); |
||
220 |
Environment* env = Environment::GetCurrent(args); |
||
221 |
CHECK(env->owns_process_state()); |
||
222 |
bool enable = args[0]->BooleanValue(env->isolate()); |
||
223 |
|||
224 |
#if OPENSSL_VERSION_MAJOR >= 3 |
||
225 |
if (enable == EVP_default_properties_is_fips_enabled(nullptr)) |
||
226 |
#else |
||
227 |
if (static_cast<int>(enable) == FIPS_mode()) |
||
228 |
#endif |
||
229 |
return; // No action needed. |
||
230 |
|||
231 |
#if OPENSSL_VERSION_MAJOR >= 3 |
||
232 |
if (!EVP_default_properties_enable_fips(nullptr, enable)) { |
||
233 |
#else |
||
234 |
if (!FIPS_mode_set(enable)) { |
||
235 |
#endif |
||
236 |
unsigned long err = ERR_get_error(); // NOLINT(runtime/int) |
||
237 |
return ThrowCryptoError(env, err); |
||
238 |
} |
||
239 |
} |
||
240 |
|||
241 |
5 |
void TestFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args) { |
|
242 |
10 |
Mutex::ScopedLock lock(per_process::cli_options_mutex); |
|
243 |
5 |
Mutex::ScopedLock fips_lock(fips_mutex); |
|
244 |
|||
245 |
#ifdef OPENSSL_FIPS |
||
246 |
#if OPENSSL_VERSION_MAJOR >= 3 |
||
247 |
OSSL_PROVIDER* fips_provider = nullptr; |
||
248 |
if (OSSL_PROVIDER_available(nullptr, "fips")) { |
||
249 |
fips_provider = OSSL_PROVIDER_load(nullptr, "fips"); |
||
250 |
} |
||
251 |
const auto enabled = fips_provider == nullptr ? 0 : |
||
252 |
OSSL_PROVIDER_self_test(fips_provider) ? 1 : 0; |
||
253 |
#else |
||
254 |
const auto enabled = FIPS_selftest() ? 1 : 0; |
||
255 |
#endif |
||
256 |
#else // OPENSSL_FIPS |
||
257 |
5 |
const auto enabled = 0; |
|
258 |
#endif // OPENSSL_FIPS |
||
259 |
|||
260 |
10 |
args.GetReturnValue().Set(enabled); |
|
261 |
5 |
} |
|
262 |
|||
263 |
1606 |
void CryptoErrorStore::Capture() { |
|
264 |
1606 |
errors_.clear(); |
|
265 |
✓✓ | 1793 |
while (const uint32_t err = ERR_get_error()) { |
266 |
char buf[256]; |
||
267 |
187 |
ERR_error_string_n(err, buf, sizeof(buf)); |
|
268 |
187 |
errors_.emplace_back(buf); |
|
269 |
187 |
} |
|
270 |
1606 |
std::reverse(std::begin(errors_), std::end(errors_)); |
|
271 |
1606 |
} |
|
272 |
|||
273 |
9572 |
bool CryptoErrorStore::Empty() const { |
|
274 |
9572 |
return errors_.empty(); |
|
275 |
} |
||
276 |
|||
277 |
398 |
MaybeLocal<Value> CryptoErrorStore::ToException( |
|
278 |
Environment* env, |
||
279 |
Local<String> exception_string) const { |
||
280 |
✓✓ | 398 |
if (exception_string.IsEmpty()) { |
281 |
294 |
CryptoErrorStore copy(*this); |
|
282 |
✗✓ | 147 |
if (copy.Empty()) { |
283 |
// But possibly a bug... |
||
284 |
copy.Insert(NodeCryptoError::OK); |
||
285 |
} |
||
286 |
// Use last element as the error message, everything else goes |
||
287 |
// into the .opensslErrorStack property on the exception object. |
||
288 |
147 |
const std::string& last_error_string = copy.errors_.back(); |
|
289 |
Local<String> exception_string; |
||
290 |
147 |
if (!String::NewFromUtf8( |
|
291 |
env->isolate(), |
||
292 |
last_error_string.data(), |
||
293 |
NewStringType::kNormal, |
||
294 |
✗✓ | 294 |
last_error_string.size()).ToLocal(&exception_string)) { |
295 |
return MaybeLocal<Value>(); |
||
296 |
} |
||
297 |
147 |
copy.errors_.pop_back(); |
|
298 |
147 |
return copy.ToException(env, exception_string); |
|
299 |
} |
||
300 |
|||
301 |
251 |
Local<Value> exception_v = Exception::Error(exception_string); |
|
302 |
✗✓ | 251 |
CHECK(!exception_v.IsEmpty()); |
303 |
|||
304 |
✓✓ | 251 |
if (!Empty()) { |
305 |
✗✓ | 26 |
CHECK(exception_v->IsObject()); |
306 |
26 |
Local<Object> exception = exception_v.As<Object>(); |
|
307 |
Local<Value> stack; |
||
308 |
✓✗ | 78 |
if (!ToV8Value(env->context(), errors_).ToLocal(&stack) || |
309 |
✗✓ | 78 |
exception->Set(env->context(), env->openssl_error_stack(), stack) |
310 |
✗✓ | 26 |
.IsNothing()) { |
311 |
return MaybeLocal<Value>(); |
||
312 |
} |
||
313 |
} |
||
314 |
|||
315 |
251 |
return exception_v; |
|
316 |
} |
||
317 |
|||
318 |
32736 |
ByteSource::ByteSource(ByteSource&& other) noexcept |
|
319 |
32736 |
: data_(other.data_), |
|
320 |
32736 |
allocated_data_(other.allocated_data_), |
|
321 |
65472 |
size_(other.size_) { |
|
322 |
32736 |
other.allocated_data_ = nullptr; |
|
323 |
32736 |
} |
|
324 |
|||
325 |
203338 |
ByteSource::~ByteSource() { |
|
326 |
101669 |
OPENSSL_clear_free(allocated_data_, size_); |
|
327 |
101669 |
} |
|
328 |
|||
329 |
23495 |
ByteSource& ByteSource::operator=(ByteSource&& other) noexcept { |
|
330 |
✓✗ | 23495 |
if (&other != this) { |
331 |
23495 |
OPENSSL_clear_free(allocated_data_, size_); |
|
332 |
23495 |
data_ = other.data_; |
|
333 |
23495 |
allocated_data_ = other.allocated_data_; |
|
334 |
23495 |
other.allocated_data_ = nullptr; |
|
335 |
23495 |
size_ = other.size_; |
|
336 |
} |
||
337 |
23495 |
return *this; |
|
338 |
} |
||
339 |
|||
340 |
5651 |
std::unique_ptr<BackingStore> ByteSource::ReleaseToBackingStore() { |
|
341 |
// It's ok for allocated_data_ to be nullptr but |
||
342 |
// only if size_ is zero. |
||
343 |
✓✓✗✓ |
5651 |
CHECK_IMPLIES(size_ > 0, allocated_data_ != nullptr); |
344 |
std::unique_ptr<BackingStore> ptr = ArrayBuffer::NewBackingStore( |
||
345 |
allocated_data_, |
||
346 |
size(), |
||
347 |
5646 |
[](void* data, size_t length, void* deleter_data) { |
|
348 |
5646 |
OPENSSL_clear_free(deleter_data, length); |
|
349 |
5651 |
}, allocated_data_); |
|
350 |
✗✓ | 5651 |
CHECK(ptr); |
351 |
5651 |
allocated_data_ = nullptr; |
|
352 |
5651 |
data_ = nullptr; |
|
353 |
5651 |
size_ = 0; |
|
354 |
5651 |
return ptr; |
|
355 |
} |
||
356 |
|||
357 |
5651 |
Local<ArrayBuffer> ByteSource::ToArrayBuffer(Environment* env) { |
|
358 |
5651 |
std::unique_ptr<BackingStore> store = ReleaseToBackingStore(); |
|
359 |
5651 |
return ArrayBuffer::New(env->isolate(), std::move(store)); |
|
360 |
} |
||
361 |
|||
362 |
17 |
MaybeLocal<Uint8Array> ByteSource::ToBuffer(Environment* env) { |
|
363 |
17 |
Local<ArrayBuffer> ab = ToArrayBuffer(env); |
|
364 |
17 |
return Buffer::New(env, ab, 0, ab->ByteLength()); |
|
365 |
} |
||
366 |
|||
367 |
517 |
ByteSource ByteSource::FromBIO(const BIOPointer& bio) { |
|
368 |
✗✓ | 517 |
CHECK(bio); |
369 |
BUF_MEM* bptr; |
||
370 |
517 |
BIO_get_mem_ptr(bio.get(), &bptr); |
|
371 |
517 |
ByteSource::Builder out(bptr->length); |
|
372 |
517 |
memcpy(out.data<void>(), bptr->data, bptr->length); |
|
373 |
517 |
return std::move(out).release(); |
|
374 |
} |
||
375 |
|||
376 |
3111 |
ByteSource ByteSource::FromEncodedString(Environment* env, |
|
377 |
Local<String> key, |
||
378 |
enum encoding enc) { |
||
379 |
3111 |
size_t length = 0; |
|
380 |
3111 |
ByteSource out; |
|
381 |
|||
382 |
✓✗✓✗ ✓✗ |
6222 |
if (StringBytes::Size(env->isolate(), key, enc).To(&length) && length > 0) { |
383 |
3111 |
ByteSource::Builder buf(length); |
|
384 |
size_t actual = |
||
385 |
3111 |
StringBytes::Write(env->isolate(), buf.data<char>(), length, key, enc); |
|
386 |
3111 |
out = std::move(buf).release(actual); |
|
387 |
} |
||
388 |
|||
389 |
3111 |
return out; |
|
390 |
} |
||
391 |
|||
392 |
2718 |
ByteSource ByteSource::FromStringOrBuffer(Environment* env, |
|
393 |
Local<Value> value) { |
||
394 |
2718 |
return IsAnyByteSource(value) ? FromBuffer(value) |
|
395 |
✓✗ | 2718 |
: FromString(env, value.As<String>()); |
396 |
} |
||
397 |
|||
398 |
24 |
ByteSource ByteSource::FromString(Environment* env, Local<String> str, |
|
399 |
bool ntc) { |
||
400 |
✗✓ | 48 |
CHECK(str->IsString()); |
401 |
24 |
size_t size = str->Utf8Length(env->isolate()); |
|
402 |
✗✓ | 24 |
size_t alloc_size = ntc ? size + 1 : size; |
403 |
24 |
ByteSource::Builder out(alloc_size); |
|
404 |
24 |
int opts = String::NO_OPTIONS; |
|
405 |
✓✗ | 24 |
if (!ntc) opts |= String::NO_NULL_TERMINATION; |
406 |
24 |
str->WriteUtf8(env->isolate(), out.data<char>(), alloc_size, nullptr, opts); |
|
407 |
24 |
return std::move(out).release(); |
|
408 |
} |
||
409 |
|||
410 |
2720 |
ByteSource ByteSource::FromBuffer(Local<Value> buffer, bool ntc) { |
|
411 |
2720 |
ArrayBufferOrViewContents<char> buf(buffer); |
|
412 |
✗✓ | 2720 |
return ntc ? buf.ToNullTerminatedCopy() : buf.ToByteSource(); |
413 |
} |
||
414 |
|||
415 |
1560 |
ByteSource ByteSource::FromSecretKeyBytes( |
|
416 |
Environment* env, |
||
417 |
Local<Value> value) { |
||
418 |
// A key can be passed as a string, buffer or KeyObject with type 'secret'. |
||
419 |
// If it is a string, we need to convert it to a buffer. We are not doing that |
||
420 |
// in JS to avoid creating an unprotected copy on the heap. |
||
421 |
✓✗✓✓ |
3120 |
return value->IsString() || IsAnyByteSource(value) ? |
422 |
ByteSource::FromStringOrBuffer(env, value) : |
||
423 |
1560 |
ByteSource::FromSymmetricKeyObjectHandle(value); |
|
424 |
} |
||
425 |
|||
426 |
ByteSource ByteSource::NullTerminatedCopy(Environment* env, |
||
427 |
Local<Value> value) { |
||
428 |
return Buffer::HasInstance(value) ? FromBuffer(value, true) |
||
429 |
: FromString(env, value.As<String>(), true); |
||
430 |
} |
||
431 |
|||
432 |
15 |
ByteSource ByteSource::FromSymmetricKeyObjectHandle(Local<Value> handle) { |
|
433 |
✗✓ | 15 |
CHECK(handle->IsObject()); |
434 |
15 |
KeyObjectHandle* key = Unwrap<KeyObjectHandle>(handle.As<Object>()); |
|
435 |
✗✓ | 15 |
CHECK_NOT_NULL(key); |
436 |
15 |
return Foreign(key->Data()->GetSymmetricKey(), |
|
437 |
30 |
key->Data()->GetSymmetricKeySize()); |
|
438 |
} |
||
439 |
|||
440 |
26183 |
ByteSource ByteSource::Allocated(void* data, size_t size) { |
|
441 |
26183 |
return ByteSource(data, data, size); |
|
442 |
} |
||
443 |
|||
444 |
5163 |
ByteSource ByteSource::Foreign(const void* data, size_t size) { |
|
445 |
5163 |
return ByteSource(data, nullptr, size); |
|
446 |
} |
||
447 |
|||
448 |
namespace error { |
||
449 |
104 |
Maybe<bool> Decorate(Environment* env, Local<Object> obj, |
|
450 |
unsigned long err) { // NOLINT(runtime/int) |
||
451 |
✓✓ | 104 |
if (err == 0) return Just(true); // No decoration necessary. |
452 |
|||
453 |
93 |
const char* ls = ERR_lib_error_string(err); |
|
454 |
93 |
const char* fs = ERR_func_error_string(err); |
|
455 |
93 |
const char* rs = ERR_reason_error_string(err); |
|
456 |
|||
457 |
93 |
Isolate* isolate = env->isolate(); |
|
458 |
93 |
Local<Context> context = isolate->GetCurrentContext(); |
|
459 |
|||
460 |
✓✗ | 93 |
if (ls != nullptr) { |
461 |
186 |
if (obj->Set(context, env->library_string(), |
|
462 |
✓✓ | 372 |
OneByteString(isolate, ls)).IsNothing()) { |
463 |
1 |
return Nothing<bool>(); |
|
464 |
} |
||
465 |
} |
||
466 |
✗✓ | 92 |
if (fs != nullptr) { |
467 |
if (obj->Set(context, env->function_string(), |
||
468 |
OneByteString(isolate, fs)).IsNothing()) { |
||
469 |
return Nothing<bool>(); |
||
470 |
} |
||
471 |
} |
||
472 |
✓✗ | 92 |
if (rs != nullptr) { |
473 |
184 |
if (obj->Set(context, env->reason_string(), |
|
474 |
✗✓ | 368 |
OneByteString(isolate, rs)).IsNothing()) { |
475 |
return Nothing<bool>(); |
||
476 |
} |
||
477 |
|||
478 |
// SSL has no API to recover the error name from the number, so we |
||
479 |
// transform reason strings like "this error" to "ERR_SSL_THIS_ERROR", |
||
480 |
// which ends up being close to the original error macro name. |
||
481 |
92 |
std::string reason(rs); |
|
482 |
|||
483 |
✓✓ | 1754 |
for (auto& c : reason) { |
484 |
✓✓ | 1662 |
if (c == ' ') |
485 |
183 |
c = '_'; |
|
486 |
else |
||
487 |
1479 |
c = ToUpper(c); |
|
488 |
} |
||
489 |
|||
490 |
#define OSSL_ERROR_CODES_MAP(V) \ |
||
491 |
V(SYS) \ |
||
492 |
V(BN) \ |
||
493 |
V(RSA) \ |
||
494 |
V(DH) \ |
||
495 |
V(EVP) \ |
||
496 |
V(BUF) \ |
||
497 |
V(OBJ) \ |
||
498 |
V(PEM) \ |
||
499 |
V(DSA) \ |
||
500 |
V(X509) \ |
||
501 |
V(ASN1) \ |
||
502 |
V(CONF) \ |
||
503 |
V(CRYPTO) \ |
||
504 |
V(EC) \ |
||
505 |
V(SSL) \ |
||
506 |
V(BIO) \ |
||
507 |
V(PKCS7) \ |
||
508 |
V(X509V3) \ |
||
509 |
V(PKCS12) \ |
||
510 |
V(RAND) \ |
||
511 |
V(DSO) \ |
||
512 |
V(ENGINE) \ |
||
513 |
V(OCSP) \ |
||
514 |
V(UI) \ |
||
515 |
V(COMP) \ |
||
516 |
V(ECDSA) \ |
||
517 |
V(ECDH) \ |
||
518 |
V(OSSL_STORE) \ |
||
519 |
V(FIPS) \ |
||
520 |
V(CMS) \ |
||
521 |
V(TS) \ |
||
522 |
V(HMAC) \ |
||
523 |
V(CT) \ |
||
524 |
V(ASYNC) \ |
||
525 |
V(KDF) \ |
||
526 |
V(SM2) \ |
||
527 |
V(USER) \ |
||
528 |
|||
529 |
#define V(name) case ERR_LIB_##name: lib = #name "_"; break; |
||
530 |
92 |
const char* lib = ""; |
|
531 |
92 |
const char* prefix = "OSSL_"; |
|
532 |
✗✗✓✓ ✓✗✗✗ ✗✓✗✗ ✓✗✓✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✗✗✗ ✗✓ |
92 |
switch (ERR_GET_LIB(err)) { OSSL_ERROR_CODES_MAP(V) } |
533 |
#undef V |
||
534 |
#undef OSSL_ERROR_CODES_MAP |
||
535 |
// Don't generate codes like "ERR_OSSL_SSL_". |
||
536 |
✓✗✓✓ |
92 |
if (lib && strcmp(lib, "SSL_") == 0) |
537 |
6 |
prefix = ""; |
|
538 |
|||
539 |
// All OpenSSL reason strings fit in a single 80-column macro definition, |
||
540 |
// all prefix lengths are <= 10, and ERR_OSSL_ is 9, so 128 is more than |
||
541 |
// sufficient. |
||
542 |
char code[128]; |
||
543 |
92 |
snprintf(code, sizeof(code), "ERR_%s%s%s", prefix, lib, reason.c_str()); |
|
544 |
|||
545 |
184 |
if (obj->Set(env->isolate()->GetCurrentContext(), |
|
546 |
env->code_string(), |
||
547 |
✗✓ | 368 |
OneByteString(env->isolate(), code)).IsNothing()) |
548 |
return Nothing<bool>(); |
||
549 |
} |
||
550 |
|||
551 |
92 |
return Just(true); |
|
552 |
} |
||
553 |
} // namespace error |
||
554 |
|||
555 |
104 |
void ThrowCryptoError(Environment* env, |
|
556 |
unsigned long err, // NOLINT(runtime/int) |
||
557 |
// Default, only used if there is no SSL `err` which can |
||
558 |
// be used to create a long-style message string. |
||
559 |
const char* message) { |
||
560 |
104 |
char message_buffer[128] = {0}; |
|
561 |
✓✓✗✓ |
104 |
if (err != 0 || message == nullptr) { |
562 |
93 |
ERR_error_string_n(err, message_buffer, sizeof(message_buffer)); |
|
563 |
93 |
message = message_buffer; |
|
564 |
} |
||
565 |
104 |
HandleScope scope(env->isolate()); |
|
566 |
Local<String> exception_string; |
||
567 |
Local<Value> exception; |
||
568 |
Local<Object> obj; |
||
569 |
✗✓ | 208 |
if (!String::NewFromUtf8(env->isolate(), message).ToLocal(&exception_string)) |
570 |
return; |
||
571 |
104 |
CryptoErrorStore errors; |
|
572 |
104 |
errors.Capture(); |
|
573 |
104 |
if (!errors.ToException(env, exception_string).ToLocal(&exception) || |
|
574 |
✓✗✓✗ |
416 |
!exception->ToObject(env->context()).ToLocal(&obj) || |
575 |
✓✓✓✓ |
312 |
error::Decorate(env, obj, err).IsNothing()) { |
576 |
1 |
return; |
|
577 |
} |
||
578 |
103 |
env->isolate()->ThrowException(exception); |
|
579 |
} |
||
580 |
|||
581 |
#ifndef OPENSSL_NO_ENGINE |
||
582 |
11 |
EnginePointer LoadEngineById(const char* id, CryptoErrorStore* errors) { |
|
583 |
22 |
MarkPopErrorOnReturn mark_pop_error_on_return; |
|
584 |
|||
585 |
11 |
EnginePointer engine(ENGINE_by_id(id)); |
|
586 |
✓✓ | 11 |
if (!engine) { |
587 |
// Engine not found, try loading dynamically. |
||
588 |
2 |
engine = EnginePointer(ENGINE_by_id("dynamic")); |
|
589 |
✓✗ | 2 |
if (engine) { |
590 |
✓✗✓✗ ✓✗ |
4 |
if (!ENGINE_ctrl_cmd_string(engine.get(), "SO_PATH", id, 0) || |
591 |
2 |
!ENGINE_ctrl_cmd_string(engine.get(), "LOAD", nullptr, 0)) { |
|
592 |
2 |
engine.reset(); |
|
593 |
} |
||
594 |
} |
||
595 |
} |
||
596 |
|||
597 |
✓✓✗✓ ✗✓ |
11 |
if (!engine && errors != nullptr) { |
598 |
errors->Capture(); |
||
599 |
if (errors->Empty()) { |
||
600 |
errors->Insert(NodeCryptoError::ENGINE_NOT_FOUND, id); |
||
601 |
} |
||
602 |
} |
||
603 |
|||
604 |
11 |
return engine; |
|
605 |
} |
||
606 |
|||
607 |
11 |
bool SetEngine(const char* id, uint32_t flags, CryptoErrorStore* errors) { |
|
608 |
11 |
ClearErrorOnReturn clear_error_on_return; |
|
609 |
22 |
EnginePointer engine = LoadEngineById(id, errors); |
|
610 |
✓✓ | 11 |
if (!engine) |
611 |
2 |
return false; |
|
612 |
|||
613 |
✗✓ | 9 |
if (!ENGINE_set_default(engine.get(), flags)) { |
614 |
if (errors != nullptr) |
||
615 |
errors->Capture(); |
||
616 |
return false; |
||
617 |
} |
||
618 |
|||
619 |
9 |
return true; |
|
620 |
} |
||
621 |
|||
622 |
11 |
void SetEngine(const FunctionCallbackInfo<Value>& args) { |
|
623 |
11 |
Environment* env = Environment::GetCurrent(args); |
|
624 |
✓✗✗✓ ✗✓ |
33 |
CHECK(args.Length() >= 2 && args[0]->IsString()); |
625 |
uint32_t flags; |
||
626 |
✗✓ | 22 |
if (!args[1]->Uint32Value(env->context()).To(&flags)) return; |
627 |
|||
628 |
11 |
const node::Utf8Value engine_id(env->isolate(), args[0]); |
|
629 |
|||
630 |
✓✓ | 22 |
args.GetReturnValue().Set(SetEngine(*engine_id, flags)); |
631 |
} |
||
632 |
#endif // !OPENSSL_NO_ENGINE |
||
633 |
|||
634 |
2593 |
MaybeLocal<Value> EncodeBignum( |
|
635 |
Environment* env, |
||
636 |
const BIGNUM* bn, |
||
637 |
int size, |
||
638 |
Local<Value>* error) { |
||
639 |
5186 |
std::vector<uint8_t> buf(size); |
|
640 |
✗✓ | 2593 |
CHECK_EQ(BN_bn2binpad(bn, buf.data(), size), size); |
641 |
return StringBytes::Encode( |
||
642 |
env->isolate(), |
||
643 |
2593 |
reinterpret_cast<const char*>(buf.data()), |
|
644 |
buf.size(), |
||
645 |
BASE64URL, |
||
646 |
5186 |
error); |
|
647 |
} |
||
648 |
|||
649 |
✗✓ | 2593 |
Maybe<bool> SetEncodedValue( |
650 |
Environment* env, |
||
651 |
Local<Object> target, |
||
652 |
Local<String> name, |
||
653 |
const BIGNUM* bn, |
||
654 |
int size) { |
||
655 |
Local<Value> value; |
||
656 |
Local<Value> error; |
||
657 |
✗✓ | 2593 |
CHECK_NOT_NULL(bn); |
658 |
✓✓ | 2593 |
if (size == 0) |
659 |
2084 |
size = BN_num_bytes(bn); |
|
660 |
✗✓ | 5186 |
if (!EncodeBignum(env, bn, size, &error).ToLocal(&value)) { |
661 |
if (!error.IsEmpty()) |
||
662 |
env->isolate()->ThrowException(error); |
||
663 |
return Nothing<bool>(); |
||
664 |
} |
||
665 |
2593 |
return target->Set(env->context(), name, value); |
|
666 |
} |
||
667 |
|||
668 |
9438 |
CryptoJobMode GetCryptoJobMode(v8::Local<v8::Value> args) { |
|
669 |
✗✓ | 9438 |
CHECK(args->IsUint32()); |
670 |
9438 |
uint32_t mode = args.As<v8::Uint32>()->Value(); |
|
671 |
✗✓ | 9438 |
CHECK_LE(mode, kCryptoJobSync); |
672 |
9438 |
return static_cast<CryptoJobMode>(mode); |
|
673 |
} |
||
674 |
|||
675 |
namespace { |
||
676 |
// SecureBuffer uses openssl to allocate a Uint8Array using |
||
677 |
// OPENSSL_secure_malloc. Because we do not yet actually |
||
678 |
// make use of secure heap, this has the same semantics as |
||
679 |
// using OPENSSL_malloc. However, if the secure heap is |
||
680 |
// initialized, SecureBuffer will automatically use it. |
||
681 |
✓✗ | 4 |
void SecureBuffer(const FunctionCallbackInfo<Value>& args) { |
682 |
✗✓ | 4 |
CHECK(args[0]->IsUint32()); |
683 |
4 |
Environment* env = Environment::GetCurrent(args); |
|
684 |
8 |
uint32_t len = args[0].As<Uint32>()->Value(); |
|
685 |
4 |
char* data = static_cast<char*>(OPENSSL_secure_malloc(len)); |
|
686 |
✗✓ | 4 |
if (data == nullptr) { |
687 |
// There's no memory available for the allocation. |
||
688 |
// Return nothing. |
||
689 |
return; |
||
690 |
} |
||
691 |
4 |
memset(data, 0, len); |
|
692 |
std::shared_ptr<BackingStore> store = |
||
693 |
4 |
ArrayBuffer::NewBackingStore( |
|
694 |
data, |
||
695 |
len, |
||
696 |
4 |
[](void* data, size_t len, void* deleter_data) { |
|
697 |
4 |
OPENSSL_secure_clear_free(data, len); |
|
698 |
4 |
}, |
|
699 |
4 |
data); |
|
700 |
4 |
Local<ArrayBuffer> buffer = ArrayBuffer::New(env->isolate(), store); |
|
701 |
8 |
args.GetReturnValue().Set(Uint8Array::New(buffer, 0, len)); |
|
702 |
} |
||
703 |
|||
704 |
4 |
void SecureHeapUsed(const FunctionCallbackInfo<Value>& args) { |
|
705 |
4 |
Environment* env = Environment::GetCurrent(args); |
|
706 |
✓✗ | 4 |
if (CRYPTO_secure_malloc_initialized()) |
707 |
8 |
args.GetReturnValue().Set( |
|
708 |
4 |
BigInt::New(env->isolate(), CRYPTO_secure_used())); |
|
709 |
4 |
} |
|
710 |
} // namespace |
||
711 |
|||
712 |
namespace Util { |
||
713 |
770 |
void Initialize(Environment* env, Local<Object> target) { |
|
714 |
770 |
Local<Context> context = env->context(); |
|
715 |
#ifndef OPENSSL_NO_ENGINE |
||
716 |
770 |
SetMethod(context, target, "setEngine", SetEngine); |
|
717 |
#endif // !OPENSSL_NO_ENGINE |
||
718 |
|||
719 |
770 |
SetMethodNoSideEffect(context, target, "getFipsCrypto", GetFipsCrypto); |
|
720 |
770 |
SetMethod(context, target, "setFipsCrypto", SetFipsCrypto); |
|
721 |
770 |
SetMethodNoSideEffect(context, target, "testFipsCrypto", TestFipsCrypto); |
|
722 |
|||
723 |
2310 |
NODE_DEFINE_CONSTANT(target, kCryptoJobAsync); |
|
724 |
1540 |
NODE_DEFINE_CONSTANT(target, kCryptoJobSync); |
|
725 |
|||
726 |
770 |
SetMethod(context, target, "secureBuffer", SecureBuffer); |
|
727 |
770 |
SetMethod(context, target, "secureHeapUsed", SecureHeapUsed); |
|
728 |
770 |
} |
|
729 |
5338 |
void RegisterExternalReferences(ExternalReferenceRegistry* registry) { |
|
730 |
#ifndef OPENSSL_NO_ENGINE |
||
731 |
5338 |
registry->Register(SetEngine); |
|
732 |
#endif // !OPENSSL_NO_ENGINE |
||
733 |
|||
734 |
5338 |
registry->Register(GetFipsCrypto); |
|
735 |
5338 |
registry->Register(SetFipsCrypto); |
|
736 |
5338 |
registry->Register(TestFipsCrypto); |
|
737 |
5338 |
registry->Register(SecureBuffer); |
|
738 |
5338 |
registry->Register(SecureHeapUsed); |
|
739 |
5338 |
} |
|
740 |
|||
741 |
} // namespace Util |
||
742 |
|||
743 |
} // namespace crypto |
||
744 |
} // namespace node |
Generated by: GCOVR (Version 4.2) |