GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_util.h Lines: 233 242 96.3 %
Date: 2022-02-13 04:14:14 Branches: 79 119 66.4 %

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_external_reference.h"
11
#include "node_internals.h"
12
#include "util.h"
13
#include "v8.h"
14
#include "string_bytes.h"
15
16
#include <openssl/err.h>
17
#include <openssl/evp.h>
18
#include <openssl/ec.h>
19
#include <openssl/kdf.h>
20
#include <openssl/rsa.h>
21
#include <openssl/dsa.h>
22
#include <openssl/ssl.h>
23
#ifndef OPENSSL_NO_ENGINE
24
#  include <openssl/engine.h>
25
#endif  // !OPENSSL_NO_ENGINE
26
// The FIPS-related functions are only available
27
// when the OpenSSL itself was compiled with FIPS support.
28
#if defined(OPENSSL_FIPS) && OPENSSL_VERSION_MAJOR < 3
29
#  include <openssl/fips.h>
30
#endif  // OPENSSL_FIPS
31
32
#include <algorithm>
33
#include <memory>
34
#include <string>
35
#include <vector>
36
#include <climits>
37
#include <cstdio>
38
39
namespace node {
40
namespace crypto {
41
// Currently known sizes of commonly used OpenSSL struct sizes.
42
// OpenSSL considers it's various structs to be opaque and the
43
// sizes may change from one version of OpenSSL to another, so
44
// these values should not be trusted to remain static. These
45
// are provided to allow for some close to reasonable memory
46
// tracking.
47
constexpr size_t kSizeOf_DH = 144;
48
constexpr size_t kSizeOf_EC_KEY = 80;
49
constexpr size_t kSizeOf_EVP_CIPHER_CTX = 168;
50
constexpr size_t kSizeOf_EVP_MD_CTX = 48;
51
constexpr size_t kSizeOf_EVP_PKEY = 72;
52
constexpr size_t kSizeOf_EVP_PKEY_CTX = 80;
53
constexpr size_t kSizeOf_HMAC_CTX = 32;
54
55
// Define smart pointers for the most commonly used OpenSSL types:
56
using X509Pointer = DeleteFnPtr<X509, X509_free>;
57
using BIOPointer = DeleteFnPtr<BIO, BIO_free_all>;
58
using SSLCtxPointer = DeleteFnPtr<SSL_CTX, SSL_CTX_free>;
59
using SSLSessionPointer = DeleteFnPtr<SSL_SESSION, SSL_SESSION_free>;
60
using SSLPointer = DeleteFnPtr<SSL, SSL_free>;
61
using PKCS8Pointer = DeleteFnPtr<PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free>;
62
using EVPKeyPointer = DeleteFnPtr<EVP_PKEY, EVP_PKEY_free>;
63
using EVPKeyCtxPointer = DeleteFnPtr<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
64
using EVPMDPointer = DeleteFnPtr<EVP_MD_CTX, EVP_MD_CTX_free>;
65
using RSAPointer = DeleteFnPtr<RSA, RSA_free>;
66
using ECPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
67
using BignumPointer = DeleteFnPtr<BIGNUM, BN_free>;
68
using BignumCtxPointer = DeleteFnPtr<BN_CTX, BN_CTX_free>;
69
using NetscapeSPKIPointer = DeleteFnPtr<NETSCAPE_SPKI, NETSCAPE_SPKI_free>;
70
using ECGroupPointer = DeleteFnPtr<EC_GROUP, EC_GROUP_free>;
71
using ECPointPointer = DeleteFnPtr<EC_POINT, EC_POINT_free>;
72
using ECKeyPointer = DeleteFnPtr<EC_KEY, EC_KEY_free>;
73
using DHPointer = DeleteFnPtr<DH, DH_free>;
74
using ECDSASigPointer = DeleteFnPtr<ECDSA_SIG, ECDSA_SIG_free>;
75
using HMACCtxPointer = DeleteFnPtr<HMAC_CTX, HMAC_CTX_free>;
76
using CipherCtxPointer = DeleteFnPtr<EVP_CIPHER_CTX, EVP_CIPHER_CTX_free>;
77
using RsaPointer = DeleteFnPtr<RSA, RSA_free>;
78
using DsaPointer = DeleteFnPtr<DSA, DSA_free>;
79
using DsaSigPointer = DeleteFnPtr<DSA_SIG, DSA_SIG_free>;
80
81
// Our custom implementation of the certificate verify callback
82
// used when establishing a TLS handshake. Because we cannot perform
83
// I/O quickly enough with X509_STORE_CTX_ APIs in this callback,
84
// we ignore preverify_ok errors here and let the handshake continue.
85
// In other words, this VerifyCallback is a non-op. It is imperative
86
// that the user user Connection::VerifyError after the `secure`
87
// callback has been made.
88
extern int VerifyCallback(int preverify_ok, X509_STORE_CTX* ctx);
89
90
bool ProcessFipsOptions();
91
92
bool InitCryptoOnce(v8::Isolate* isolate);
93
void InitCryptoOnce();
94
95
void InitCrypto(v8::Local<v8::Object> target);
96
97
extern void UseExtraCaCerts(const std::string& file);
98
99
// Forcibly clear OpenSSL's error stack on return. This stops stale errors
100
// from popping up later in the lifecycle of crypto operations where they
101
// would cause spurious failures. It's a rather blunt method, though.
102
// ERR_clear_error() isn't necessarily cheap either.
103
struct ClearErrorOnReturn {
104
11618
  ~ClearErrorOnReturn() { ERR_clear_error(); }
105
};
106
107
// Pop errors from OpenSSL's error stack that were added
108
// between when this was constructed and destructed.
109
struct MarkPopErrorOnReturn {
110
22398
  MarkPopErrorOnReturn() { ERR_set_mark(); }
111
22397
  ~MarkPopErrorOnReturn() { ERR_pop_to_mark(); }
112
};
113
114
// Ensure that OpenSSL has enough entropy (at least 256 bits) for its PRNG.
115
// The entropy pool starts out empty and needs to fill up before the PRNG
116
// can be used securely.  Once the pool is filled, it never dries up again;
117
// its contents is stirred and reused when necessary.
118
//
119
// OpenSSL normally fills the pool automatically but not when someone starts
120
// generating random numbers before the pool is full: in that case OpenSSL
121
// keeps lowering the entropy estimate to thwart attackers trying to guess
122
// the initial state of the PRNG.
123
//
124
// When that happens, we will have to wait until enough entropy is available.
125
// That should normally never take longer than a few milliseconds.
126
//
127
// OpenSSL draws from /dev/random and /dev/urandom.  While /dev/random may
128
// block pending "true" randomness, /dev/urandom is a CSPRNG that doesn't
129
// block under normal circumstances.
130
//
131
// The only time when /dev/urandom may conceivably block is right after boot,
132
// when the whole system is still low on entropy.  That's not something we can
133
// do anything about.
134
void CheckEntropy();
135
136
// Generate length bytes of random data. If this returns false, the data
137
// may not be truly random but it's still generally good enough.
138
bool EntropySource(unsigned char* buffer, size_t length);
139
140
int PasswordCallback(char* buf, int size, int rwflag, void* u);
141
142
int NoPasswordCallback(char* buf, int size, int rwflag, void* u);
143
144
// Decode is used by the various stream-based crypto utilities to decode
145
// string input.
146
template <typename T>
147
3403
void Decode(const v8::FunctionCallbackInfo<v8::Value>& args,
148
            void (*callback)(T*, const v8::FunctionCallbackInfo<v8::Value>&,
149
                             const char*, size_t)) {
150
  T* ctx;
151
3403
  ASSIGN_OR_RETURN_UNWRAP(&ctx, args.Holder());
152
153
6806
  if (args[0]->IsString()) {
154
1815
    StringBytes::InlineDecoder decoder;
155
1815
    Environment* env = Environment::GetCurrent(args);
156
1815
    enum encoding enc = ParseEncoding(env->isolate(), args[1], UTF8);
157
5445
    if (decoder.Decode(env, args[0].As<v8::String>(), enc).IsNothing())
158
      return;
159
1815
    callback(ctx, args, decoder.out(), decoder.size());
160
  } else {
161
1588
    ArrayBufferViewContents<char> buf(args[0]);
162
1588
    callback(ctx, args, buf.data(), buf.length());
163
  }
164
}
165
166
#define NODE_CRYPTO_ERROR_CODES_MAP(V)                                        \
167
    V(CIPHER_JOB_FAILED, "Cipher job failed")                                 \
168
    V(DERIVING_BITS_FAILED, "Deriving bits failed")                           \
169
    V(ENGINE_NOT_FOUND, "Engine \"%s\" was not found")                        \
170
    V(INVALID_KEY_TYPE, "Invalid key type")                                   \
171
    V(KEY_GENERATION_JOB_FAILED, "Key generation job failed")                 \
172
    V(OK, "Ok")                                                               \
173
174
enum class NodeCryptoError {
175
#define V(CODE, DESCRIPTION) CODE,
176
  NODE_CRYPTO_ERROR_CODES_MAP(V)
177
#undef V
178
};
179
180
// Utility struct used to harvest error information from openssl's error stack
181
struct CryptoErrorStore final : public MemoryRetainer {
182
 public:
183
  void Capture();
184
185
  bool Empty() const;
186
187
  template <typename... Args>
188
  void Insert(const NodeCryptoError error, Args&&... args);
189
190
  v8::MaybeLocal<v8::Value> ToException(
191
      Environment* env,
192
      v8::Local<v8::String> exception_string = v8::Local<v8::String>()) const;
193
194
3
  SET_NO_MEMORY_INFO()
195
3
  SET_MEMORY_INFO_NAME(CryptoErrorStore)
196
3
  SET_SELF_SIZE(CryptoErrorStore)
197
198
 private:
199
  std::vector<std::string> errors_;
200
};
201
202
template <typename... Args>
203
14
void CryptoErrorStore::Insert(const NodeCryptoError error, Args&&... args) {
204
14
  const char* error_string = nullptr;
205

14
  switch (error) {
206
#define V(CODE, DESCRIPTION) \
207
    case NodeCryptoError::CODE: error_string = DESCRIPTION; break;
208
14
    NODE_CRYPTO_ERROR_CODES_MAP(V)
209
#undef V
210
  }
211
14
  errors_.emplace_back(SPrintF(error_string,
212
                               std::forward<Args>(args)...));
213
14
}
214
215
template <typename T>
216
15702
T* MallocOpenSSL(size_t count) {
217
15702
  void* mem = OPENSSL_malloc(MultiplyWithOverflowCheck(count, sizeof(T)));
218

15702
  CHECK_IMPLIES(mem == nullptr, count == 0);
219
15702
  return static_cast<T*>(mem);
220
}
221
222
template <typename T>
223
1419
T* ReallocOpenSSL(T* buf, size_t count) {
224
1419
  void* mem = OPENSSL_realloc(buf, MultiplyWithOverflowCheck(count, sizeof(T)));
225

1419
  CHECK_IMPLIES(mem == nullptr, count == 0);
226
1419
  return static_cast<T*>(mem);
227
}
228
229
// A helper class representing a read-only byte array. When deallocated, its
230
// contents are zeroed.
231
class ByteSource {
232
 public:
233
21664
  ByteSource() = default;
234
  ByteSource(ByteSource&& other) noexcept;
235
  ~ByteSource();
236
237
  ByteSource& operator=(ByteSource&& other) noexcept;
238
239
  const char* get() const;
240
241
  template <typename T>
242
6779
  const T* data() const { return reinterpret_cast<const T*>(get()); }
243
244
  size_t size() const;
245
246
4500
  operator bool() const { return data_ != nullptr; }
247
248
2841
  BignumPointer ToBN() const {
249
    return BignumPointer(BN_bin2bn(
250
2841
        reinterpret_cast<const unsigned char*>(get()),
251
2841
        size(),
252
2841
        nullptr));
253
  }
254
255
  // Creates a v8::BackingStore that takes over responsibility for
256
  // any allocated data. The ByteSource will be reset with size = 0
257
  // after being called.
258
  std::unique_ptr<v8::BackingStore> ReleaseToBackingStore();
259
260
  v8::Local<v8::ArrayBuffer> ToArrayBuffer(Environment* env);
261
262
  v8::MaybeLocal<v8::Uint8Array> ToBuffer(Environment* env);
263
264
  void reset();
265
266
  // Allows an Allocated ByteSource to be truncated.
267
1419
  void Resize(size_t newsize) {
268
1419
    CHECK_LE(newsize, size_);
269
1419
    CHECK_NOT_NULL(allocated_data_);
270
1419
    char* new_data_ = ReallocOpenSSL<char>(allocated_data_, newsize);
271
1419
    data_ = allocated_data_ = new_data_;
272
1419
    size_ = newsize;
273
1419
  }
274
275
  static ByteSource Allocated(char* data, size_t size);
276
  static ByteSource Foreign(const char* data, size_t size);
277
278
  static ByteSource FromEncodedString(Environment* env,
279
                                      v8::Local<v8::String> value,
280
                                      enum encoding enc = BASE64);
281
282
  static ByteSource FromStringOrBuffer(Environment* env,
283
                                       v8::Local<v8::Value> value);
284
285
  static ByteSource FromString(Environment* env,
286
                               v8::Local<v8::String> str,
287
                               bool ntc = false);
288
289
  static ByteSource FromBuffer(v8::Local<v8::Value> buffer,
290
                               bool ntc = false);
291
292
  static ByteSource FromBIO(const BIOPointer& bio);
293
294
  static ByteSource NullTerminatedCopy(Environment* env,
295
                                       v8::Local<v8::Value> value);
296
297
  static ByteSource FromSymmetricKeyObjectHandle(v8::Local<v8::Value> handle);
298
299
  ByteSource(const ByteSource&) = delete;
300
  ByteSource& operator=(const ByteSource&) = delete;
301
302
  static ByteSource FromSecretKeyBytes(
303
      Environment* env, v8::Local<v8::Value> value);
304
305
 private:
306
  const char* data_ = nullptr;
307
  char* allocated_data_ = nullptr;
308
  size_t size_ = 0;
309
310
  ByteSource(const char* data, char* allocated_data, size_t size);
311
};
312
313
enum CryptoJobMode {
314
  kCryptoJobAsync,
315
  kCryptoJobSync
316
};
317
318
CryptoJobMode GetCryptoJobMode(v8::Local<v8::Value> args);
319
320
template <typename CryptoJobTraits>
321
class CryptoJob : public AsyncWrap, public ThreadPoolWork {
322
 public:
323
  using AdditionalParams = typename CryptoJobTraits::AdditionalParameters;
324
325
7654
  explicit CryptoJob(
326
      Environment* env,
327
      v8::Local<v8::Object> object,
328
      AsyncWrap::ProviderType type,
329
      CryptoJobMode mode,
330
      AdditionalParams&& params)
331
      : AsyncWrap(env, object, type),
332
        ThreadPoolWork(env),
333
        mode_(mode),
334
7654
        params_(std::move(params)) {
335
    // If the CryptoJob is async, then the instance will be
336
    // cleaned up when AfterThreadPoolWork is called.
337
7654
    if (mode == kCryptoJobSync) MakeWeak();
338
7654
  }
339
340
  bool IsNotIndicativeOfMemoryLeakAtExit() const override {
341
    // CryptoJobs run a work in the libuv thread pool and may still
342
    // exist when the event loop empties and starts to exit.
343
    return true;
344
  }
345
346
5859
  void AfterThreadPoolWork(int status) override {
347
5859
    Environment* env = AsyncWrap::env();
348
5859
    CHECK_EQ(mode_, kCryptoJobAsync);
349

5859
    CHECK(status == 0 || status == UV_ECANCELED);
350
5859
    std::unique_ptr<CryptoJob> ptr(this);
351
    // If the job was canceled do not execute the callback.
352
    // TODO(@jasnell): We should likely revisit skipping the
353
    // callback on cancel as that could leave the JS in a pending
354
    // state (e.g. unresolved promises...)
355
5859
    if (status == UV_ECANCELED) return;
356
5859
    v8::HandleScope handle_scope(env->isolate());
357
11718
    v8::Context::Scope context_scope(env->context());
358
359
    // TODO(tniessen): Remove the exception handling logic here as soon as we
360
    // can verify that no code path in ToResult will ever throw an exception.
361
    v8::Local<v8::Value> exception;
362
17577
    v8::Local<v8::Value> args[2];
363
    {
364
5859
      node::errors::TryCatchScope try_catch(env);
365
5859
      v8::Maybe<bool> ret = ptr->ToResult(&args[0], &args[1]);
366
5859
      if (!ret.IsJust()) {
367
4
        CHECK(try_catch.HasCaught());
368
4
        exception = try_catch.Exception();
369
5855
      } else if (!ret.FromJust()) {
370
        return;
371
      }
372
    }
373
374
5859
    if (exception.IsEmpty()) {
375
5855
      ptr->MakeCallback(env->ondone_string(), arraysize(args), args);
376
    } else {
377
4
      ptr->MakeCallback(env->ondone_string(), 1, &exception);
378
    }
379
  }
380
381
  virtual v8::Maybe<bool> ToResult(
382
      v8::Local<v8::Value>* err,
383
      v8::Local<v8::Value>* result) = 0;
384
385
7640
  CryptoJobMode mode() const { return mode_; }
386
387
7715
  CryptoErrorStore* errors() { return &errors_; }
388
389
12493
  AdditionalParams* params() { return &params_; }
390
391
6
  std::string MemoryInfoName() const override {
392
6
    return CryptoJobTraits::JobName;
393
  }
394
395
6
  void MemoryInfo(MemoryTracker* tracker) const override {
396
6
    tracker->TrackField("params", params_);
397
6
    tracker->TrackField("errors", errors_);
398
  }
399
400
7640
  static void Run(const v8::FunctionCallbackInfo<v8::Value>& args) {
401
7640
    Environment* env = Environment::GetCurrent(args);
402
403
    CryptoJob<CryptoJobTraits>* job;
404
13501
    ASSIGN_OR_RETURN_UNWRAP(&job, args.Holder());
405
7640
    if (job->mode() == kCryptoJobAsync)
406
5861
      return job->ScheduleWork();
407
408
5337
    v8::Local<v8::Value> ret[2];
409
1779
    env->PrintSyncTrace();
410
1779
    job->DoThreadPoolWork();
411
1779
    v8::Maybe<bool> result = job->ToResult(&ret[0], &ret[1]);
412

3552
    if (result.IsJust() && result.FromJust()) {
413
3546
      args.GetReturnValue().Set(
414
          v8::Array::New(env->isolate(), ret, arraysize(ret)));
415
    }
416
  }
417
418
183798
  static void Initialize(
419
      v8::FunctionCallback new_fn,
420
      Environment* env,
421
      v8::Local<v8::Object> target) {
422
183798
    v8::Local<v8::FunctionTemplate> job = env->NewFunctionTemplate(new_fn);
423
183798
    job->Inherit(AsyncWrap::GetConstructorTemplate(env));
424
367596
    job->InstanceTemplate()->SetInternalFieldCount(
425
        AsyncWrap::kInternalFieldCount);
426
183798
    env->SetProtoMethod(job, "run", Run);
427
183798
    env->SetConstructorFunction(target, CryptoJobTraits::JobName, job);
428
183798
  }
429
430
207234
  static void RegisterExternalReferences(v8::FunctionCallback new_fn,
431
                                         ExternalReferenceRegistry* registry) {
432
207234
    registry->Register(new_fn);
433
207234
    registry->Register(Run);
434
207234
  }
435
436
 private:
437
  const CryptoJobMode mode_;
438
  CryptoErrorStore errors_;
439
  AdditionalParams params_;
440
};
441
442
template <typename DeriveBitsTraits>
443
class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> {
444
 public:
445
  using AdditionalParams = typename DeriveBitsTraits::AdditionalParameters;
446
447
3891
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args) {
448
3891
    Environment* env = Environment::GetCurrent(args);
449
450
3891
    CryptoJobMode mode = GetCryptoJobMode(args[0]);
451
452
3891
    AdditionalParams params;
453
7782
    if (DeriveBitsTraits::AdditionalConfig(mode, args, 1, &params)
454
            .IsNothing()) {
455
      // The DeriveBitsTraits::AdditionalConfig is responsible for
456
      // calling an appropriate THROW_CRYPTO_* variant reporting
457
      // whatever error caused initialization to fail.
458
71
      return;
459
    }
460
461
3820
    new DeriveBitsJob(env, args.This(), mode, std::move(params));
462
  }
463
464
74398
  static void Initialize(
465
      Environment* env,
466
      v8::Local<v8::Object> target) {
467
74398
    CryptoJob<DeriveBitsTraits>::Initialize(New, env, target);
468
74398
  }
469
470
83884
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
471
83884
    CryptoJob<DeriveBitsTraits>::RegisterExternalReferences(New, registry);
472
83884
  }
473
474
3820
  DeriveBitsJob(
475
      Environment* env,
476
      v8::Local<v8::Object> object,
477
      CryptoJobMode mode,
478
      AdditionalParams&& params)
479
      : CryptoJob<DeriveBitsTraits>(
480
            env,
481
            object,
482
            DeriveBitsTraits::Provider,
483
            mode,
484
3820
            std::move(params)) {}
485
486
3820
  void DoThreadPoolWork() override {
487
3820
    if (!DeriveBitsTraits::DeriveBits(
488
            AsyncWrap::env(),
489
3820
            *CryptoJob<DeriveBitsTraits>::params(), &out_)) {
490
14
      CryptoErrorStore* errors = CryptoJob<DeriveBitsTraits>::errors();
491
14
      errors->Capture();
492
14
      if (errors->Empty())
493
14
        errors->Insert(NodeCryptoError::DERIVING_BITS_FAILED);
494
14
      return;
495
    }
496
3806
    success_ = true;
497
  }
498
499
3820
  v8::Maybe<bool> ToResult(
500
      v8::Local<v8::Value>* err,
501
      v8::Local<v8::Value>* result) override {
502
3820
    Environment* env = AsyncWrap::env();
503
3820
    CryptoErrorStore* errors = CryptoJob<DeriveBitsTraits>::errors();
504
3820
    if (success_) {
505
3806
      CHECK(errors->Empty());
506
3806
      *err = v8::Undefined(env->isolate());
507
3806
      return DeriveBitsTraits::EncodeOutput(
508
          env,
509
3806
          *CryptoJob<DeriveBitsTraits>::params(),
510
          &out_,
511
3806
          result);
512
    }
513
514
14
    if (errors->Empty())
515
      errors->Capture();
516
14
    CHECK(!errors->Empty());
517
28
    *result = v8::Undefined(env->isolate());
518
28
    return v8::Just(errors->ToException(env).ToLocal(err));
519
  }
520
521
6
  SET_SELF_SIZE(DeriveBitsJob)
522
3
  void MemoryInfo(MemoryTracker* tracker) const override {
523
6
    tracker->TrackFieldWithSize("out", out_.size());
524
6
    CryptoJob<DeriveBitsTraits>::MemoryInfo(tracker);
525
  }
526
527
 private:
528
  ByteSource out_;
529
  bool success_ = false;
530
};
531
532
void ThrowCryptoError(Environment* env,
533
                      unsigned long err,  // NOLINT(runtime/int)
534
                      const char* message = nullptr);
535
536
#ifndef OPENSSL_NO_ENGINE
537
struct EnginePointer {
538
  ENGINE* engine = nullptr;
539
  bool finish_on_exit = false;
540
541
2480
  inline EnginePointer() = default;
542
543
13
  inline explicit EnginePointer(ENGINE* engine_, bool finish_on_exit_ = false)
544
13
    : engine(engine_),
545
13
      finish_on_exit(finish_on_exit_) {}
546
547
2
  inline EnginePointer(EnginePointer&& other) noexcept
548
2
      : engine(other.engine),
549
2
        finish_on_exit(other.finish_on_exit) {
550
2
    other.release();
551
2
  }
552
553
2486
  inline ~EnginePointer() { reset(); }
554
555
2
  inline EnginePointer& operator=(EnginePointer&& other) noexcept {
556
2
    if (this == &other) return *this;
557
2
    this->~EnginePointer();
558
2
    return *new (this) EnginePointer(std::move(other));
559
  }
560
561
35
  inline operator bool() const { return engine != nullptr; }
562
563
13
  inline ENGINE* get() { return engine; }
564
565
2488
  inline void reset(ENGINE* engine_ = nullptr, bool finish_on_exit_ = false) {
566
2488
    if (engine != nullptr) {
567
11
      if (finish_on_exit) {
568
        // This also does the equivalent of ENGINE_free.
569
        CHECK_EQ(ENGINE_finish(engine), 1);
570
      } else {
571
11
        CHECK_EQ(ENGINE_free(engine), 1);
572
      }
573
    }
574
2488
    engine = engine_;
575
2488
    finish_on_exit = finish_on_exit_;
576
2488
  }
577
578
2
  inline ENGINE* release() {
579
2
    ENGINE* ret = engine;
580
2
    engine = nullptr;
581
2
    finish_on_exit = false;
582
2
    return ret;
583
  }
584
};
585
586
EnginePointer LoadEngineById(const char* id, CryptoErrorStore* errors);
587
588
bool SetEngine(
589
    const char* id,
590
    uint32_t flags,
591
    CryptoErrorStore* errors = nullptr);
592
593
void SetEngine(const v8::FunctionCallbackInfo<v8::Value>& args);
594
#endif  // !OPENSSL_NO_ENGINE
595
596
void GetFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args);
597
598
void SetFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args);
599
600
void TestFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args);
601
602
class CipherPushContext {
603
 public:
604
7
  inline explicit CipherPushContext(Environment* env) : env_(env) {}
605
606
581
  inline void push_back(const char* str) {
607
581
    list_.emplace_back(OneByteString(env_->isolate(), str));
608
581
  }
609
610
7
  inline v8::Local<v8::Array> ToJSArray() {
611
7
    return v8::Array::New(env_->isolate(), list_.data(), list_.size());
612
  }
613
614
 private:
615
  std::vector<v8::Local<v8::Value>> list_;
616
  Environment* env_;
617
};
618
619
#if OPENSSL_VERSION_MAJOR >= 3
620
template <class TypeName,
621
          TypeName* fetch_type(OSSL_LIB_CTX*, const char*, const char*),
622
          void free_type(TypeName*),
623
          const TypeName* getbyname(const char*),
624
          const char* getname(const TypeName*)>
625
761
void array_push_back(const TypeName* evp_ref,
626
                     const char* from,
627
                     const char* to,
628
                     void* arg) {
629
761
  if (!from)
630
    return;
631
632
761
  const TypeName* real_instance = getbyname(from);
633
761
  if (!real_instance)
634
    return;
635
636
761
  const char* real_name = getname(real_instance);
637
761
  if (!real_name)
638
    return;
639
640
  // EVP_*_fetch() does not support alias names, so we need to pass it the
641
  // real/original algorithm name.
642
  // We use EVP_*_fetch() as a filter here because it will only return an
643
  // instance if the algorithm is supported by the public OpenSSL APIs (some
644
  // algorithms are used internally by OpenSSL and are also passed to this
645
  // callback).
646
761
  TypeName* fetched = fetch_type(nullptr, real_name, nullptr);
647
761
  if (!fetched)
648
180
    return;
649
650
581
  free_type(fetched);
651
581
  static_cast<CipherPushContext*>(arg)->push_back(from);
652
}
653
#else
654
template <class TypeName>
655
void array_push_back(const TypeName* evp_ref,
656
                     const char* from,
657
                     const char* to,
658
                     void* arg) {
659
  if (!from)
660
    return;
661
  static_cast<CipherPushContext*>(arg)->push_back(from);
662
}
663
#endif
664
665
129979
inline bool IsAnyByteSource(v8::Local<v8::Value> arg) {
666
137025
  return arg->IsArrayBufferView() ||
667

144071
         arg->IsArrayBuffer() ||
668
134411
         arg->IsSharedArrayBuffer();
669
}
670
671
template <typename T>
672
class ArrayBufferOrViewContents {
673
 public:
674
  ArrayBufferOrViewContents() = default;
675
676
218298
  inline explicit ArrayBufferOrViewContents(v8::Local<v8::Value> buf) {
677
218298
    CHECK(IsAnyByteSource(buf));
678
218298
    if (buf->IsArrayBufferView()) {
679
215952
      auto view = buf.As<v8::ArrayBufferView>();
680
215952
      offset_ = view->ByteOffset();
681
215952
      length_ = view->ByteLength();
682
431904
      store_ = view->Buffer()->GetBackingStore();
683
2346
    } else if (buf->IsArrayBuffer()) {
684
2330
      auto ab = buf.As<v8::ArrayBuffer>();
685
2330
      offset_ = 0;
686
2330
      length_ = ab->ByteLength();
687
2330
      store_ = ab->GetBackingStore();
688
    } else {
689
16
      auto sab = buf.As<v8::SharedArrayBuffer>();
690
16
      offset_ = 0;
691
16
      length_ = sab->ByteLength();
692
16
      store_ = sab->GetBackingStore();
693
    }
694
218298
  }
695
696
13431
  inline const T* data() const {
697
    // Ideally, these would return nullptr if IsEmpty() or length_ is zero,
698
    // but some of the openssl API react badly if given a nullptr even when
699
    // length is zero, so we have to return something.
700
13431
    if (size() == 0)
701
131
      return &buf;
702
13300
    return reinterpret_cast<T*>(store_->Data()) + offset_;
703
  }
704
705
103201
  inline T* data() {
706
    // Ideally, these would return nullptr if IsEmpty() or length_ is zero,
707
    // but some of the openssl API react badly if given a nullptr even when
708
    // length is zero, so we have to return something.
709
103201
    if (size() == 0)
710
3
      return &buf;
711
103198
    return reinterpret_cast<T*>(store_->Data()) + offset_;
712
  }
713
714
671855
  inline size_t size() const { return length_; }
715
716
  // In most cases, input buffer sizes passed in to openssl need to
717
  // be limited to <= INT_MAX. This utility method helps us check.
718
213466
  inline bool CheckSizeInt32() { return size() <= INT_MAX; }
719
720
4707
  inline ByteSource ToByteSource() const {
721
4707
    return ByteSource::Foreign(data(), size());
722
  }
723
724
6943
  inline ByteSource ToCopy() const {
725
6943
    if (size() == 0) return ByteSource();
726
6342
    char* buf = MallocOpenSSL<char>(size());
727
6342
    CHECK_NOT_NULL(buf);
728
6342
    memcpy(buf, data(), size());
729
6342
    return ByteSource::Allocated(buf, size());
730
  }
731
732
88
  inline ByteSource ToNullTerminatedCopy() const {
733
88
    if (size() == 0) return ByteSource();
734
88
    char* buf = MallocOpenSSL<char>(size() + 1);
735
88
    CHECK_NOT_NULL(buf);
736
88
    buf[size()] = 0;
737
88
    memcpy(buf, data(), size());
738
88
    return ByteSource::Allocated(buf, size());
739
  }
740
741
  template <typename M>
742
85
  void CopyTo(M* dest, size_t len) const {
743
    static_assert(sizeof(M) == 1, "sizeof(M) must equal 1");
744
85
    len = std::min(len, size());
745

85
    if (len > 0 && data() != nullptr)
746
85
      memcpy(dest, data(), len);
747
85
  }
748
749
 private:
750
  T buf = 0;
751
  size_t offset_ = 0;
752
  size_t length_ = 0;
753
  std::shared_ptr<v8::BackingStore> store_;
754
};
755
756
template <typename T>
757
std::vector<T> CopyBuffer(const ArrayBufferOrViewContents<T>& buf) {
758
  std::vector<T> vec;
759
  vec->resize(buf.size());
760
  if (vec->size() > 0 && buf.data() != nullptr)
761
    memcpy(vec->data(), buf.data(), vec->size());
762
  return vec;
763
}
764
765
template <typename T>
766
std::vector<T> CopyBuffer(v8::Local<v8::Value> buf) {
767
  return CopyBuffer(ArrayBufferOrViewContents<T>(buf));
768
}
769
770
v8::MaybeLocal<v8::Value> EncodeBignum(
771
    Environment* env,
772
    const BIGNUM* bn,
773
    int size,
774
    v8::Local<v8::Value>* error);
775
776
v8::Maybe<bool> SetEncodedValue(
777
    Environment* env,
778
    v8::Local<v8::Object> target,
779
    v8::Local<v8::String> name,
780
    const BIGNUM* bn,
781
    int size = 0);
782
783
namespace Util {
784
void Initialize(Environment* env, v8::Local<v8::Object> target);
785
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
786
}  // namespace Util
787
788
}  // namespace crypto
789
}  // namespace node
790
791
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
792
#endif  // SRC_CRYPTO_CRYPTO_UTIL_H_