File: engine.cc

package info (click to toggle)
nodejs 22.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 246,928 kB
  • sloc: cpp: 1,582,349; javascript: 582,017; ansic: 82,400; python: 60,561; sh: 4,009; makefile: 2,263; asm: 1,732; pascal: 1,565; perl: 248; lisp: 222; xml: 42
file content (93 lines) | stat: -rw-r--r-- 2,584 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "ncrypto.h"

namespace ncrypto {

// ============================================================================
// Engine

#ifndef OPENSSL_NO_ENGINE
EnginePointer::EnginePointer(ENGINE* engine_, bool finish_on_exit_)
    : engine(engine_), finish_on_exit(finish_on_exit_) {}

EnginePointer::EnginePointer(EnginePointer&& other) noexcept
    : engine(other.engine), finish_on_exit(other.finish_on_exit) {
  other.release();
}

EnginePointer::~EnginePointer() {
  reset();
}

EnginePointer& EnginePointer::operator=(EnginePointer&& other) noexcept {
  if (this == &other) return *this;
  this->~EnginePointer();
  return *new (this) EnginePointer(std::move(other));
}

void EnginePointer::reset(ENGINE* engine_, bool finish_on_exit_) {
  if (engine != nullptr) {
    if (finish_on_exit) {
      // This also does the equivalent of ENGINE_free.
      ENGINE_finish(engine);
    } else {
      ENGINE_free(engine);
    }
  }
  engine = engine_;
  finish_on_exit = finish_on_exit_;
}

ENGINE* EnginePointer::release() {
  ENGINE* ret = engine;
  engine = nullptr;
  finish_on_exit = false;
  return ret;
}

EnginePointer EnginePointer::getEngineByName(const std::string_view name,
                                             CryptoErrorList* errors) {
  MarkPopErrorOnReturn mark_pop_error_on_return(errors);
  EnginePointer engine(ENGINE_by_id(name.data()));
  if (!engine) {
    // Engine not found, try loading dynamically.
    engine = EnginePointer(ENGINE_by_id("dynamic"));
    if (engine) {
      if (!ENGINE_ctrl_cmd_string(engine.get(), "SO_PATH", name.data(), 0) ||
          !ENGINE_ctrl_cmd_string(engine.get(), "LOAD", nullptr, 0)) {
        engine.reset();
      }
    }
  }
  return engine;
}

bool EnginePointer::setAsDefault(uint32_t flags, CryptoErrorList* errors) {
  if (engine == nullptr) return false;
  ClearErrorOnReturn clear_error_on_return(errors);
  return ENGINE_set_default(engine, flags) != 0;
}

bool EnginePointer::init(bool finish_on_exit) {
  if (engine == nullptr) return false;
  if (finish_on_exit) setFinishOnExit();
  return ENGINE_init(engine) == 1;
}

EVPKeyPointer EnginePointer::loadPrivateKey(const std::string_view key_name) {
  if (engine == nullptr) return EVPKeyPointer();
  return EVPKeyPointer(
      ENGINE_load_private_key(engine, key_name.data(), nullptr, nullptr));
}

void EnginePointer::initEnginesOnce() {
  static bool initialized = false;
  if (!initialized) {
    ENGINE_load_builtin_engines();
    ENGINE_register_all_complete();
    initialized = true;
  }
}

#endif  // OPENSSL_NO_ENGINE

}  // namespace ncrypto