GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#ifndef SRC_CRYPTO_CRYPTO_UTIL_H_ |
||
2 |
#define SRC_CRYPTO_CRYPTO_UTIL_H_ |
||
3 |
|||
4 |
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
||
5 |
|||
6 |
#include "env.h" |
||
7 |
#include "async_wrap.h" |
||
8 |
#include "allocated_buffer.h" |
||
9 |
#include "node_errors.h" |
||
10 |
#include "node_internals.h" |
||
11 |
#include "util.h" |
||
12 |
#include "v8.h" |
||
13 |
#include "string_bytes.h" |
||
14 |
|||
15 |
#include <openssl/err.h> |
||
16 |
#include <openssl/evp.h> |
||
17 |
#include <openssl/ec.h> |
||
18 |
#include <openssl/kdf.h> |
||
19 |
#include <openssl/rsa.h> |
||
20 |
#include <openssl/dsa.h> |
||
21 |
#include <openssl/ssl.h> |
||
22 |
#ifndef OPENSSL_NO_ENGINE |
||
23 |
# include <openssl/engine.h> |
||
24 |
#endif // !OPENSSL_NO_ENGINE |
||
25 |
|||
26 |
#include <algorithm> |
||
27 |
#include <memory> |
||
28 |
#include <string> |
||
29 |
#include <vector> |
||
30 |
#include <climits> |
||
31 |
#include <cstdio> |
||
32 |
|||
33 |
namespace node { |
||
34 |
namespace crypto { |
||
35 |
// Currently known sizes of commonly used OpenSSL struct sizes. |
||
36 |
// OpenSSL considers it's various structs to be opaque and the |
||
37 |
// sizes may change from one version of OpenSSL to another, so |
||
38 |
// these values should not be trusted to remain static. These |
||
39 |
// are provided to allow for some close to reasonable memory |
||
40 |
// tracking. |
||
41 |
constexpr size_t kSizeOf_DH = 144; |
||
42 |
constexpr size_t kSizeOf_EC_KEY = 80; |
||
43 |
constexpr size_t kSizeOf_EVP_CIPHER_CTX = 168; |
||
44 |
constexpr size_t kSizeOf_EVP_MD_CTX = 48; |
||
45 |
constexpr size_t kSizeOf_EVP_PKEY = 72; |
||
46 |
constexpr size_t kSizeOf_EVP_PKEY_CTX = 80; |
||
47 |
constexpr size_t kSizeOf_HMAC_CTX = 32; |
||
48 |
|||
49 |
// Define smart pointers for the most commonly used OpenSSL types: |
||
50 |
using X509Pointer = DeleteFnPtr<X509, X509_free>; |
||
51 |
using BIOPointer = DeleteFnPtr<BIO, BIO_free_all>; |
||
52 |
using SSLCtxPointer = DeleteFnPtr<SSL_CTX, SSL_CTX_free>; |
||
53 |
using SSLSessionPointer = DeleteFnPtr<SSL_SESSION, SSL_SESSION_free>; |
||
54 |
using SSLPointer = DeleteFnPtr<SSL, SSL_free>; |
||
55 |
using PKCS8Pointer = DeleteFnPtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>; |
||
56 |
using EVPKeyPointer = DeleteFnPtr<EVP_PKEY, EVP_PKEY_free>; |
||
57 |
using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>; |
||
58 |
using EVPMDPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>; |
||
59 |
using RSAPointer = DeleteFnPtr<RSA, RSA_free>; |
||
60 |
using ECPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>; |
||
61 |
using BignumPointer = DeleteFnPtr<BIGNUM, BN_free>; |
||
62 |
using NetscapeSPKIPointer = DeleteFnPtr<NETSCAPE_SPKI, NETSCAPE_SPKI_free>; |
||
63 |
using ECGroupPointer = DeleteFnPtr<EC_GROUP, EC_GROUP_free>; |
||
64 |
using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>; |
||
65 |
using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>; |
||
66 |
using DHPointer = DeleteFnPtr<DH, DH_free>; |
||
67 |
using ECDSASigPointer = DeleteFnPtr<ECDSA_SIG, ECDSA_SIG_free>; |
||
68 |
using HMACCtxPointer = DeleteFnPtr<HMAC_CTX, HMAC_CTX_free>; |
||
69 |
using CipherCtxPointer = DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free>; |
||
70 |
using RsaPointer = DeleteFnPtr<RSA, RSA_free>; |
||
71 |
using DsaPointer = DeleteFnPtr<DSA, DSA_free>; |
||
72 |
using EcdsaSigPointer = DeleteFnPtr<ECDSA_SIG, ECDSA_SIG_free>; |
||
73 |
|||
74 |
// Our custom implementation of the certificate verify callback |
||
75 |
// used when establishing a TLS handshake. Because we cannot perform |
||
76 |
// I/O quickly enough with X509_STORE_CTX_ APIs in this callback, |
||
77 |
// we ignore preverify_ok errors here and let the handshake continue. |
||
78 |
// In other words, this VerifyCallback is a non-op. It is imperative |
||
79 |
// that the user user Connection::VerifyError after the `secure` |
||
80 |
// callback has been made. |
||
81 |
extern int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx); |
||
82 |
|||
83 |
void InitCryptoOnce(); |
||
84 |
|||
85 |
void InitCrypto(v8::Local<v8::Object> target); |
||
86 |
|||
87 |
extern void UseExtraCaCerts(const std::string& file); |
||
88 |
|||
89 |
// Forcibly clear OpenSSL's error stack on return. This stops stale errors |
||
90 |
// from popping up later in the lifecycle of crypto operations where they |
||
91 |
// would cause spurious failures. It's a rather blunt method, though. |
||
92 |
// ERR_clear_error() isn't necessarily cheap either. |
||
93 |
struct ClearErrorOnReturn { |
||
94 |
8602 |
~ClearErrorOnReturn() { ERR_clear_error(); } |
|
95 |
}; |
||
96 |
|||
97 |
// Pop errors from OpenSSL's error stack that were added |
||
98 |
// between when this was constructed and destructed. |
||
99 |
struct MarkPopErrorOnReturn { |
||
100 |
20186 |
MarkPopErrorOnReturn() { ERR_set_mark(); } |
|
101 |
20185 |
~MarkPopErrorOnReturn() { ERR_pop_to_mark(); } |
|
102 |
}; |
||
103 |
|||
104 |
// Ensure that OpenSSL has enough entropy (at least 256 bits) for its PRNG. |
||
105 |
// The entropy pool starts out empty and needs to fill up before the PRNG |
||
106 |
// can be used securely. Once the pool is filled, it never dries up again; |
||
107 |
// its contents is stirred and reused when necessary. |
||
108 |
// |
||
109 |
// OpenSSL normally fills the pool automatically but not when someone starts |
||
110 |
// generating random numbers before the pool is full: in that case OpenSSL |
||
111 |
// keeps lowering the entropy estimate to thwart attackers trying to guess |
||
112 |
// the initial state of the PRNG. |
||
113 |
// |
||
114 |
// When that happens, we will have to wait until enough entropy is available. |
||
115 |
// That should normally never take longer than a few milliseconds. |
||
116 |
// |
||
117 |
// OpenSSL draws from /dev/random and /dev/urandom. While /dev/random may |
||
118 |
// block pending "true" randomness, /dev/urandom is a CSPRNG that doesn't |
||
119 |
// block under normal circumstances. |
||
120 |
// |
||
121 |
// The only time when /dev/urandom may conceivably block is right after boot, |
||
122 |
// when the whole system is still low on entropy. That's not something we can |
||
123 |
// do anything about. |
||
124 |
void CheckEntropy(); |
||
125 |
|||
126 |
// Generate length bytes of random data. If this returns false, the data |
||
127 |
// may not be truly random but it's still generally good enough. |
||
128 |
bool EntropySource(unsigned char* buffer, size_t length); |
||
129 |
|||
130 |
int PasswordCallback(char* buf, int size, int rwflag, void* u); |
||
131 |
|||
132 |
int NoPasswordCallback(char* buf, int size, int rwflag, void* u); |
||
133 |
|||
134 |
// Decode is used by the various stream-based crypto utilities to decode |
||
135 |
// string input. |
||
136 |
template <typename T> |
||
137 |
11691 |
void Decode(const v8::FunctionCallbackInfo<v8::Value>& args, |
|
138 |
void (*callback)(T*, const v8::FunctionCallbackInfo<v8::Value>&, |
||
139 |
const char*, size_t)) { |
||
140 |
T* ctx; |
||
141 |
✗✓✗✓ |
11691 |
ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder()); |
142 |
|||
143 |
✓✓✓✓ |
35073 |
if (args[0]->IsString()) { |
144 |
20634 |
StringBytes::InlineDecoder decoder; |
|
145 |
10317 |
Environment* env = Environment::GetCurrent(args); |
|
146 |
10317 |
enum encoding enc = ParseEncoding(env->isolate(), args[1], UTF8); |
|
147 |
✗✓✗✓ |
30951 |
if (decoder.Decode(env, args[0].As<v8::String>(), enc).IsNothing()) |
148 |
return; |
||
149 |
✓✗✓✗ |
10317 |
callback(ctx, args, decoder.out(), decoder.size()); |
150 |
} else { |
||
151 |
1374 |
ArrayBufferViewContents<char> buf(args[0]); |
|
152 |
1374 |
callback(ctx, args, buf.data(), buf.length()); |
|
153 |
} |
||
154 |
} |
||
155 |
|||
156 |
// Utility struct used to harvest error information from openssl's error stack |
||
157 |
7111 |
struct CryptoErrorVector : public std::vector<std::string> { |
|
158 |
void Capture(); |
||
159 |
|||
160 |
v8::MaybeLocal<v8::Value> ToException( |
||
161 |
Environment* env, |
||
162 |
v8::Local<v8::String> exception_string = v8::Local<v8::String>()) const; |
||
163 |
}; |
||
164 |
|||
165 |
template <typename T> |
||
166 |
19625 |
T* MallocOpenSSL(size_t count) { |
|
167 |
19625 |
void* mem = OPENSSL_malloc(MultiplyWithOverflowCheck(count, sizeof(T))); |
|
168 |
✓✓✗✓ ✗✗✗✗ |
19623 |
CHECK_IMPLIES(mem == nullptr, count == 0); |
169 |
19623 |
return static_cast<T*>(mem); |
|
170 |
} |
||
171 |
|||
172 |
template <typename T> |
||
173 |
439 |
T* ReallocOpenSSL(T* buf, size_t count) { |
|
174 |
439 |
void* mem = OPENSSL_realloc(buf, MultiplyWithOverflowCheck(count, sizeof(T))); |
|
175 |
✗✓✗✗ |
439 |
CHECK_IMPLIES(mem == nullptr, count == 0); |
176 |
439 |
return static_cast<T*>(mem); |
|
177 |
} |
||
178 |
|||
179 |
// A helper class representing a read-only byte array. When deallocated, its |
||
180 |
// contents are zeroed. |
||
181 |
class ByteSource { |
||
182 |
public: |
||
183 |
22654 |
ByteSource() = default; |
|
184 |
ByteSource(ByteSource&& other) noexcept; |
||
185 |
~ByteSource(); |
||
186 |
|||
187 |
ByteSource& operator=(ByteSource&& other) noexcept; |
||
188 |
|||
189 |
const char* get() const; |
||
190 |
|||
191 |
template <typename T> |
||
192 |
3816 |
const T* data() const { return reinterpret_cast<const T*>(get()); } |
|
193 |
|||
194 |
size_t size() const; |
||
195 |
|||
196 |
13177 |
operator bool() const { return data_ != nullptr; } |
|
197 |
|||
198 |
1073 |
BignumPointer ToBN() const { |
|
199 |
return BignumPointer(BN_bin2bn( |
||
200 |
1073 |
reinterpret_cast<const unsigned char*>(get()), |
|
201 |
1073 |
size(), |
|
202 |
2146 |
nullptr)); |
|
203 |
} |
||
204 |
|||
205 |
// Creates a v8::BackingStore that takes over responsibility for |
||
206 |
// any allocated data. The ByteSource will be reset with size = 0 |
||
207 |
// after being called. |
||
208 |
std::unique_ptr<v8::BackingStore> ReleaseToBackingStore(); |
||
209 |
|||
210 |
v8::Local<v8::ArrayBuffer> ToArrayBuffer(Environment* env); |
||
211 |
|||
212 |
void reset(); |
||
213 |
|||
214 |
// Allows an Allocated ByteSource to be truncated. |
||
215 |
439 |
void Resize(size_t newsize) { |
|
216 |
✗✓ | 439 |
CHECK_LE(newsize, size_); |
217 |
✗✓ | 439 |
CHECK_NOT_NULL(allocated_data_); |
218 |
439 |
char* new_data_ = ReallocOpenSSL<char>(allocated_data_, newsize); |
|
219 |
439 |
data_ = allocated_data_ = new_data_; |
|
220 |
439 |
size_ = newsize; |
|
221 |
439 |
} |
|
222 |
|||
223 |
static ByteSource Allocated(char* data, size_t size); |
||
224 |
static ByteSource Foreign(const char* data, size_t size); |
||
225 |
|||
226 |
static ByteSource FromEncodedString(Environment* env, |
||
227 |
v8::Local<v8::String> value, |
||
228 |
enum encoding enc = BASE64); |
||
229 |
|||
230 |
static ByteSource FromStringOrBuffer(Environment* env, |
||
231 |
v8::Local<v8::Value> value); |
||
232 |
|||
233 |
static ByteSource FromString(Environment* env, |
||
234 |
v8::Local<v8::String> str, |
||
235 |
bool ntc = false); |
||
236 |
|||
237 |
static ByteSource FromBuffer(v8::Local<v8::Value> buffer, |
||
238 |
bool ntc = false); |
||
239 |
|||
240 |
static ByteSource FromBIO(const BIOPointer& bio); |
||
241 |
|||
242 |
static ByteSource NullTerminatedCopy(Environment* env, |
||
243 |
v8::Local<v8::Value> value); |
||
244 |
|||
245 |
static ByteSource FromSymmetricKeyObjectHandle(v8::Local<v8::Value> handle); |
||
246 |
|||
247 |
ByteSource(const ByteSource&) = delete; |
||
248 |
ByteSource& operator=(const ByteSource&) = delete; |
||
249 |
|||
250 |
static ByteSource FromSecretKeyBytes( |
||
251 |
Environment* env, v8::Local<v8::Value> value); |
||
252 |
|||
253 |
private: |
||
254 |
const char* data_ = nullptr; |
||
255 |
char* allocated_data_ = nullptr; |
||
256 |
size_t size_ = 0; |
||
257 |
|||
258 |
ByteSource(const char* data, char* allocated_data, size_t size); |
||
259 |
}; |
||
260 |
|||
261 |
enum CryptoJobMode { |
||
262 |
kCryptoJobAsync, |
||
263 |
kCryptoJobSync |
||
264 |
}; |
||
265 |
|||
266 |
CryptoJobMode GetCryptoJobMode(v8::Local<v8::Value> args); |
||
267 |
|||
268 |
template <typename CryptoJobTraits> |
||
269 |
3459 |
class CryptoJob : public AsyncWrap, public ThreadPoolWork { |
|
270 |
public: |
||
271 |
using AdditionalParams = typename CryptoJobTraits::AdditionalParameters; |
||
272 |
|||
273 |
3460 |
explicit CryptoJob( |
|
274 |
Environment* env, |
||
275 |
v8::Local<v8::Object> object, |
||
276 |
AsyncWrap::ProviderType type, |
||
277 |
CryptoJobMode mode, |
||
278 |
AdditionalParams&& params) |
||
279 |
: AsyncWrap(env, object, type), |
||
280 |
ThreadPoolWork(env), |
||
281 |
mode_(mode), |
||
282 |
3460 |
params_(std::move(params)) { |
|
283 |
// If the CryptoJob is async, then the instance will be |
||
284 |
// cleaned up when AfterThreadPoolWork is called. |
||
285 |
✓✓✓✓ ✓✓ |
3460 |
if (mode == kCryptoJobSync) MakeWeak(); |
286 |
3460 |
} |
|
287 |
|||
288 |
bool IsNotIndicativeOfMemoryLeakAtExit() const override { |
||
289 |
// CryptoJobs run a work in the libuv thread pool and may still |
||
290 |
// exist when the event loop empties and starts to exit. |
||
291 |
return true; |
||
292 |
} |
||
293 |
|||
294 |
3068 |
void AfterThreadPoolWork(int status) override { |
|
295 |
3068 |
Environment* env = AsyncWrap::env(); |
|
296 |
✗✓✗✓ ✗✓ |
3068 |
CHECK_EQ(mode_, kCryptoJobAsync); |
297 |
✗✓✗✗ ✗✓✗✗ ✗✓✗✗ |
3068 |
CHECK(status == 0 || status == UV_ECANCELED); |
298 |
6136 |
std::unique_ptr<CryptoJob> ptr(this); |
|
299 |
// If the job was canceled do not execute the callback. |
||
300 |
// TODO(@jasnell): We should likely revisit skipping the |
||
301 |
// callback on cancel as that could leave the JS in a pending |
||
302 |
// state (e.g. unresolved promises...) |
||
303 |
✗✓✗✓ ✗✓ |
3068 |
if (status == UV_ECANCELED) return; |
304 |
✓✗✓✗ ✓✗ |
6136 |
v8::HandleScope handle_scope(env->isolate()); |
305 |
3068 |
v8::Context::Scope context_scope(env->context()); |
|
306 |
✓✓✓✓ ✓✓ |
9204 |
v8::Local<v8::Value> args[2]; |
307 |
✓✗✓✗ ✓✗ |
6136 |
if (ptr->ToResult(&args[0], &args[1]).FromJust()) |
308 |
3068 |
ptr->MakeCallback(env->ondone_string(), arraysize(args), args); |
|
309 |
} |
||
310 |
|||
311 |
virtual v8::Maybe<bool> ToResult( |
||
312 |
v8::Local<v8::Value>* err, |
||
313 |
v8::Local<v8::Value>* result) = 0; |
||
314 |
|||
315 |
3457 |
CryptoJobMode mode() const { return mode_; } |
|
316 |
|||
317 |
3474 |
CryptoErrorVector* errors() { return &errors_; } |
|
318 |
|||
319 |
6241 |
AdditionalParams* params() { return ¶ms_; } |
|
320 |
|||
321 |
3 |
std::string MemoryInfoName() const override { |
|
322 |
3 |
return CryptoJobTraits::JobName; |
|
323 |
} |
||
324 |
|||
325 |
3 |
void MemoryInfo(MemoryTracker* tracker) const override { |
|
326 |
3 |
tracker->TrackField("params", params_); |
|
327 |
3 |
tracker->TrackField("errors", errors_); |
|
328 |
3 |
} |
|
329 |
|||
330 |
3457 |
static void Run(const v8::FunctionCallbackInfo<v8::Value>& args) { |
|
331 |
3457 |
Environment* env = Environment::GetCurrent(args); |
|
332 |
|||
333 |
CryptoJob<CryptoJobTraits>* job; |
||
334 |
✗✓✗✓ ✗✓ |
6526 |
ASSIGN_OR_RETURN_UNWRAP(&job, args.Holder()); |
335 |
✓✓✓✓ ✓✓ |
3457 |
if (job->mode() == kCryptoJobAsync) |
336 |
3069 |
return job->ScheduleWork(); |
|
337 |
|||
338 |
✓✓✓✓ ✓✓ |
1164 |
v8::Local<v8::Value> ret[2]; |
339 |
388 |
env->PrintSyncTrace(); |
|
340 |
388 |
job->DoThreadPoolWork(); |
|
341 |
✓✗✓✗ ✓✗ |
776 |
if (job->ToResult(&ret[0], &ret[1]).FromJust()) { |
342 |
1164 |
args.GetReturnValue().Set( |
|
343 |
v8::Array::New(env->isolate(), ret, arraysize(ret))); |
||
344 |
} |
||
345 |
} |
||
346 |
|||
347 |
13923 |
static void Initialize( |
|
348 |
v8::FunctionCallback new_fn, |
||
349 |
Environment* env, |
||
350 |
v8::Local<v8::Object> target) { |
||
351 |
13923 |
v8::Local<v8::FunctionTemplate> job = env->NewFunctionTemplate(new_fn); |
|
352 |
27846 |
job->Inherit(AsyncWrap::GetConstructorTemplate(env)); |
|
353 |
27846 |
job->InstanceTemplate()->SetInternalFieldCount( |
|
354 |
AsyncWrap::kInternalFieldCount); |
||
355 |
13923 |
env->SetProtoMethod(job, "run", Run); |
|
356 |
13923 |
env->SetConstructorFunction(target, CryptoJobTraits::JobName, job); |
|
357 |
13923 |
} |
|
358 |
|||
359 |
private: |
||
360 |
const CryptoJobMode mode_; |
||
361 |
CryptoErrorVector errors_; |
||
362 |
AdditionalParams params_; |
||
363 |
}; |
||
364 |
|||
365 |
template <typename DeriveBitsTraits> |
||
366 |
5130 |
class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> { |
|
367 |
public: |
||
368 |
using AdditionalParams = typename DeriveBitsTraits::AdditionalParameters; |
||
369 |
|||
370 |
2584 |
static void New(const v8::FunctionCallbackInfo<v8::Value>& args) { |
|
371 |
2584 |
Environment* env = Environment::GetCurrent(args); |
|
372 |
|||
373 |
2584 |
CryptoJobMode mode = GetCryptoJobMode(args[0]); |
|
374 |
|||
375 |
5149 |
AdditionalParams params; |
|
376 |
✓✓✓✓ ✓✓ |
5168 |
if (DeriveBitsTraits::AdditionalConfig(mode, args, 1, ¶ms) |
377 |
.IsNothing()) { |
||
378 |
// The DeriveBitsTraits::AdditionalConfig is responsible for |
||
379 |
// calling an appropriate THROW_CRYPTO_* variant reporting |
||
380 |
// whatever error caused initialization to fail. |
||
381 |
19 |
return; |
|
382 |
} |
||
383 |
|||
384 |
✓✓✓✓ ✓✓ |
2565 |
new DeriveBitsJob(env, args.This(), mode, std::move(params)); |
385 |
} |
||
386 |
|||
387 |
5967 |
static void Initialize( |
|
388 |
Environment* env, |
||
389 |
v8::Local<v8::Object> target) { |
||
390 |
5967 |
CryptoJob<DeriveBitsTraits>::Initialize(New, env, target); |
|
391 |
5967 |
} |
|
392 |
|||
393 |
2565 |
DeriveBitsJob( |
|
394 |
Environment* env, |
||
395 |
v8::Local<v8::Object> object, |
||
396 |
CryptoJobMode mode, |
||
397 |
AdditionalParams&& params) |
||
398 |
: CryptoJob<DeriveBitsTraits>( |
||
399 |
env, |
||
400 |
object, |
||
401 |
DeriveBitsTraits::Provider, |
||
402 |
mode, |
||
403 |
2565 |
std::move(params)) {} |
|
404 |
|||
405 |
2565 |
void DoThreadPoolWork() override { |
|
406 |
✗✓✗✓ ✗✓ |
5130 |
if (!DeriveBitsTraits::DeriveBits( |
407 |
AsyncWrap::env(), |
||
408 |
2565 |
*CryptoJob<DeriveBitsTraits>::params(), &out_)) { |
|
409 |
CryptoErrorVector* errors = CryptoJob<DeriveBitsTraits>::errors(); |
||
410 |
errors->Capture(); |
||
411 |
if (errors->empty()) |
||
412 |
errors->push_back("Deriving bits failed"); |
||
413 |
return; |
||
414 |
} |
||
415 |
2565 |
success_ = true; |
|
416 |
} |
||
417 |
|||
418 |
2565 |
v8::Maybe<bool> ToResult( |
|
419 |
v8::Local<v8::Value>* err, |
||
420 |
v8::Local<v8::Value>* result) override { |
||
421 |
2565 |
Environment* env = AsyncWrap::env(); |
|
422 |
2565 |
CryptoErrorVector* errors = CryptoJob<DeriveBitsTraits>::errors(); |
|
423 |
✓✗✓✗ ✓✗ |
2565 |
if (success_) { |
424 |
✗✓✗✓ ✗✓ |
2565 |
CHECK(errors->empty()); |
425 |
5130 |
*err = v8::Undefined(env->isolate()); |
|
426 |
2565 |
return DeriveBitsTraits::EncodeOutput( |
|
427 |
env, |
||
428 |
2565 |
*CryptoJob<DeriveBitsTraits>::params(), |
|
429 |
&out_, |
||
430 |
2565 |
result); |
|
431 |
} |
||
432 |
|||
433 |
if (errors->empty()) |
||
434 |
errors->Capture(); |
||
435 |
CHECK(!errors->empty()); |
||
436 |
*result = v8::Undefined(env->isolate()); |
||
437 |
return v8::Just(errors->ToException(env).ToLocal(err)); |
||
438 |
} |
||
439 |
|||
440 |
3 |
SET_SELF_SIZE(DeriveBitsJob); |
|
441 |
3 |
void MemoryInfo(MemoryTracker* tracker) const override { |
|
442 |
3 |
tracker->TrackFieldWithSize("out", out_.size()); |
|
443 |
3 |
CryptoJob<DeriveBitsTraits>::MemoryInfo(tracker); |
|
444 |
3 |
} |
|
445 |
|||
446 |
private: |
||
447 |
ByteSource out_; |
||
448 |
bool success_ = false; |
||
449 |
}; |
||
450 |
|||
451 |
void ThrowCryptoError(Environment* env, |
||
452 |
unsigned long err, // NOLINT(runtime/int) |
||
453 |
const char* message = nullptr); |
||
454 |
|||
455 |
#ifndef OPENSSL_NO_ENGINE |
||
456 |
struct EnginePointer { |
||
457 |
ENGINE* engine = nullptr; |
||
458 |
bool finish_on_exit = false; |
||
459 |
|||
460 |
2332 |
inline EnginePointer() = default; |
|
461 |
|||
462 |
4 |
inline explicit EnginePointer(ENGINE* engine_, bool finish_on_exit_ = false) |
|
463 |
4 |
: engine(engine_), |
|
464 |
4 |
finish_on_exit(finish_on_exit_) {} |
|
465 |
|||
466 |
2 |
inline EnginePointer(EnginePointer&& other) noexcept |
|
467 |
2 |
: engine(other.engine), |
|
468 |
2 |
finish_on_exit(other.finish_on_exit) { |
|
469 |
2 |
other.release(); |
|
470 |
2 |
} |
|
471 |
|||
472 |
2316 |
inline ~EnginePointer() { reset(); } |
|
473 |
|||
474 |
2 |
inline EnginePointer& operator=(EnginePointer&& other) noexcept { |
|
475 |
✗✓ | 2 |
if (this == &other) return *this; |
476 |
2 |
this->~EnginePointer(); |
|
477 |
✓✗ | 2 |
return *new (this) EnginePointer(std::move(other)); |
478 |
} |
||
479 |
|||
480 |
8 |
inline operator bool() const { return engine != nullptr; } |
|
481 |
|||
482 |
4 |
inline ENGINE* get() { return engine; } |
|
483 |
|||
484 |
2318 |
inline void reset(ENGINE* engine_ = nullptr, bool finish_on_exit_ = false) { |
|
485 |
✓✓ | 2318 |
if (engine != nullptr) { |
486 |
✗✓ | 2 |
if (finish_on_exit) |
487 |
ENGINE_finish(engine); |
||
488 |
2 |
ENGINE_free(engine); |
|
489 |
} |
||
490 |
2318 |
engine = engine_; |
|
491 |
2318 |
finish_on_exit = finish_on_exit_; |
|
492 |
2318 |
} |
|
493 |
|||
494 |
2 |
inline ENGINE* release() { |
|
495 |
2 |
ENGINE* ret = engine; |
|
496 |
2 |
engine = nullptr; |
|
497 |
2 |
finish_on_exit = false; |
|
498 |
2 |
return ret; |
|
499 |
} |
||
500 |
}; |
||
501 |
|||
502 |
EnginePointer LoadEngineById(const char* id, CryptoErrorVector* errors); |
||
503 |
|||
504 |
bool SetEngine( |
||
505 |
const char* id, |
||
506 |
uint32_t flags, |
||
507 |
CryptoErrorVector* errors = nullptr); |
||
508 |
|||
509 |
void SetEngine(const v8::FunctionCallbackInfo<v8::Value>& args); |
||
510 |
#endif // !OPENSSL_NO_ENGINE |
||
511 |
|||
512 |
#ifdef NODE_FIPS_MODE |
||
513 |
void GetFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args); |
||
514 |
|||
515 |
void SetFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args); |
||
516 |
#endif /* NODE_FIPS_MODE */ |
||
517 |
|||
518 |
10 |
class CipherPushContext { |
|
519 |
public: |
||
520 |
10 |
inline explicit CipherPushContext(Environment* env) : env_(env) {} |
|
521 |
|||
522 |
938 |
inline void push_back(const char* str) { |
|
523 |
938 |
list_.emplace_back(OneByteString(env_->isolate(), str)); |
|
524 |
938 |
} |
|
525 |
|||
526 |
10 |
inline v8::Local<v8::Array> ToJSArray() { |
|
527 |
10 |
return v8::Array::New(env_->isolate(), list_.data(), list_.size()); |
|
528 |
} |
||
529 |
|||
530 |
private: |
||
531 |
std::vector<v8::Local<v8::Value>> list_; |
||
532 |
Environment* env_; |
||
533 |
}; |
||
534 |
|||
535 |
template <class TypeName> |
||
536 |
938 |
void array_push_back(const TypeName* md, |
|
537 |
const char* from, |
||
538 |
const char* to, |
||
539 |
void* arg) { |
||
540 |
938 |
static_cast<CipherPushContext*>(arg)->push_back(from); |
|
541 |
938 |
} |
|
542 |
|||
543 |
122983 |
inline bool IsAnyByteSource(v8::Local<v8::Value> arg) { |
|
544 |
✓✓ | 126997 |
return arg->IsArrayBufferView() || |
545 |
✓✓✓✓ |
129086 |
arg->IsArrayBuffer() || |
546 |
125072 |
arg->IsSharedArrayBuffer(); |
|
547 |
} |
||
548 |
|||
549 |
template <typename T> |
||
550 |
116392 |
class ArrayBufferOrViewContents { |
|
551 |
public: |
||
552 |
ArrayBufferOrViewContents() = default; |
||
553 |
|||
554 |
113890 |
inline explicit ArrayBufferOrViewContents(v8::Local<v8::Value> buf) { |
|
555 |
✗✓✗✓ |
113890 |
CHECK(IsAnyByteSource(buf)); |
556 |
✓✓✓✓ |
113890 |
if (buf->IsArrayBufferView()) { |
557 |
112549 |
auto view = buf.As<v8::ArrayBufferView>(); |
|
558 |
112549 |
offset_ = view->ByteOffset(); |
|
559 |
112549 |
length_ = view->ByteLength(); |
|
560 |
225098 |
store_ = view->Buffer()->GetBackingStore(); |
|
561 |
✓✓✓✓ |
1341 |
} else if (buf->IsArrayBuffer()) { |
562 |
1323 |
auto ab = buf.As<v8::ArrayBuffer>(); |
|
563 |
1323 |
offset_ = 0; |
|
564 |
1323 |
length_ = ab->ByteLength(); |
|
565 |
1323 |
store_ = ab->GetBackingStore(); |
|
566 |
} else { |
||
567 |
18 |
auto sab = buf.As<v8::SharedArrayBuffer>(); |
|
568 |
18 |
offset_ = 0; |
|
569 |
18 |
length_ = sab->ByteLength(); |
|
570 |
18 |
store_ = sab->GetBackingStore(); |
|
571 |
} |
||
572 |
113890 |
} |
|
573 |
|||
574 |
9577 |
inline const T* data() const { |
|
575 |
// Ideally, these would return nullptr if IsEmpty() or length_ is zero, |
||
576 |
// but some of the openssl API react badly if given a nullptr even when |
||
577 |
// length is zero, so we have to return something. |
||
578 |
✓✓✓✓ |
9577 |
if (size() == 0) |
579 |
54 |
return &buf; |
|
580 |
9523 |
return reinterpret_cast<T*>(store_->Data()) + offset_; |
|
581 |
} |
||
582 |
|||
583 |
103373 |
inline T* data() { |
|
584 |
// Ideally, these would return nullptr if IsEmpty() or length_ is zero, |
||
585 |
// but some of the openssl API react badly if given a nullptr even when |
||
586 |
// length is zero, so we have to return something. |
||
587 |
✓✓✗✗ |
103373 |
if (size() == 0) |
588 |
33 |
return &buf; |
|
589 |
103340 |
return reinterpret_cast<T*>(store_->Data()) + offset_; |
|
590 |
} |
||
591 |
|||
592 |
355056 |
inline size_t size() const { return length_; } |
|
593 |
|||
594 |
// In most cases, input buffer sizes passed in to openssl need to |
||
595 |
// be limited to <= INT_MAX. This utility method helps us check. |
||
596 |
109853 |
inline bool CheckSizeInt32() { return size() <= INT_MAX; } |
|
597 |
|||
598 |
2826 |
inline ByteSource ToByteSource() const { |
|
599 |
2826 |
return ByteSource::Foreign(data(), size()); |
|
600 |
} |
||
601 |
|||
602 |
6164 |
inline ByteSource ToCopy() const { |
|
603 |
✓✓ | 6164 |
if (size() == 0) return ByteSource(); |
604 |
5370 |
char* buf = MallocOpenSSL<char>(size()); |
|
605 |
✗✓ | 5370 |
CHECK_NOT_NULL(buf); |
606 |
5370 |
memcpy(buf, data(), size()); |
|
607 |
5370 |
return ByteSource::Allocated(buf, size()); |
|
608 |
} |
||
609 |
|||
610 |
94 |
inline ByteSource ToNullTerminatedCopy() const { |
|
611 |
✓✓ | 94 |
if (size() == 0) return ByteSource(); |
612 |
88 |
char* buf = MallocOpenSSL<char>(size() + 1); |
|
613 |
✗✓ | 88 |
CHECK_NOT_NULL(buf); |
614 |
88 |
buf[size()] = 0; |
|
615 |
88 |
memcpy(buf, data(), size()); |
|
616 |
88 |
return ByteSource::Allocated(buf, size()); |
|
617 |
} |
||
618 |
|||
619 |
template <typename M> |
||
620 |
85 |
void CopyTo(M* dest, size_t len) const { |
|
621 |
static_assert(sizeof(M) == 1, "sizeof(M) must equal 1"); |
||
622 |
85 |
len = std::min(len, size()); |
|
623 |
✓✗✓✗ ✓✗ |
85 |
if (len > 0 && data() != nullptr) |
624 |
85 |
memcpy(dest, data(), len); |
|
625 |
85 |
} |
|
626 |
|||
627 |
private: |
||
628 |
T buf = 0; |
||
629 |
size_t offset_ = 0; |
||
630 |
size_t length_ = 0; |
||
631 |
std::shared_ptr<v8::BackingStore> store_; |
||
632 |
}; |
||
633 |
|||
634 |
template <typename T> |
||
635 |
std::vector<T> CopyBuffer(const ArrayBufferOrViewContents<T>& buf) { |
||
636 |
std::vector<T> vec; |
||
637 |
vec->resize(buf.size()); |
||
638 |
if (vec->size() > 0 && buf.data() != nullptr) |
||
639 |
memcpy(vec->data(), buf.data(), vec->size()); |
||
640 |
return vec; |
||
641 |
} |
||
642 |
|||
643 |
template <typename T> |
||
644 |
std::vector<T> CopyBuffer(v8::Local<v8::Value> buf) { |
||
645 |
return CopyBuffer(ArrayBufferOrViewContents<T>(buf)); |
||
646 |
} |
||
647 |
|||
648 |
v8::MaybeLocal<v8::Value> EncodeBignum( |
||
649 |
Environment* env, |
||
650 |
const BIGNUM* bn, |
||
651 |
v8::Local<v8::Value>* error); |
||
652 |
|||
653 |
v8::Maybe<bool> SetEncodedValue( |
||
654 |
Environment* env, |
||
655 |
v8::Local<v8::Object> target, |
||
656 |
v8::Local<v8::String> name, |
||
657 |
const BIGNUM* bn, |
||
658 |
int size = 0); |
||
659 |
|||
660 |
namespace Util { |
||
661 |
void Initialize(Environment* env, v8::Local<v8::Object> target); |
||
662 |
} // namespace Util |
||
663 |
|||
664 |
} // namespace crypto |
||
665 |
} // namespace node |
||
666 |
|||
667 |
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
||
668 |
#endif // SRC_CRYPTO_CRYPTO_UTIL_H_ |
Generated by: GCOVR (Version 3.4) |