GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: crypto/crypto_keygen.h Lines: 73 90 81.1 %
Date: 2022-10-02 04:22:39 Branches: 28 39 71.8 %

Line Branch Exec Source
1
#ifndef SRC_CRYPTO_CRYPTO_KEYGEN_H_
2
#define SRC_CRYPTO_CRYPTO_KEYGEN_H_
3
4
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
5
6
#include "async_wrap.h"
7
#include "base_object.h"
8
#include "crypto/crypto_keys.h"
9
#include "crypto/crypto_util.h"
10
#include "env.h"
11
#include "memory_tracker.h"
12
#include "v8.h"
13
14
namespace node {
15
namespace crypto {
16
namespace Keygen {
17
void Initialize(Environment* env, v8::Local<v8::Object> target);
18
void RegisterExternalReferences(ExternalReferenceRegistry* registry);
19
}  // namespace Keygen
20
21
enum class KeyGenJobStatus {
22
  OK,
23
  FAILED
24
};
25
26
// A Base CryptoJob for generating secret keys or key pairs.
27
// The KeyGenTraits is largely responsible for the details of
28
// the implementation, while KeyGenJob handles the common
29
// mechanisms.
30
template <typename KeyGenTraits>
31
class KeyGenJob final : public CryptoJob<KeyGenTraits> {
32
 public:
33
  using AdditionalParams = typename KeyGenTraits::AdditionalParameters;
34
35
3299
  static void New(const v8::FunctionCallbackInfo<v8::Value>& args) {
36
3299
    Environment* env = Environment::GetCurrent(args);
37
3299
    CHECK(args.IsConstructCall());
38
39
3299
    CryptoJobMode mode = GetCryptoJobMode(args[0]);
40
41
3299
    unsigned int offset = 1;
42
43
3299
    AdditionalParams params;
44
6598
    if (KeyGenTraits::AdditionalConfig(mode, args, &offset, &params)
45
            .IsNothing()) {
46
      // The KeyGenTraits::AdditionalConfig is responsible for
47
      // calling an appropriate THROW_CRYPTO_* variant reporting
48
      // whatever error caused initialization to fail.
49
      return;
50
    }
51
52
3299
    new KeyGenJob<KeyGenTraits>(env, args.This(), mode, std::move(params));
53
  }
54
55
6240
  static void Initialize(
56
      Environment* env,
57
      v8::Local<v8::Object> target) {
58
6240
    CryptoJob<KeyGenTraits>::Initialize(New, env, target);
59
6240
  }
60
61
44248
  static void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
62
44248
    CryptoJob<KeyGenTraits>::RegisterExternalReferences(New, registry);
63
44248
  }
64
65
3299
  KeyGenJob(
66
      Environment* env,
67
      v8::Local<v8::Object> object,
68
      CryptoJobMode mode,
69
      AdditionalParams&& params)
70
      : CryptoJob<KeyGenTraits>(
71
            env,
72
            object,
73
            KeyGenTraits::Provider,
74
            mode,
75
3299
            std::move(params)) {}
76
77
3294
  void DoThreadPoolWork() override {
78
3294
    AdditionalParams* params = CryptoJob<KeyGenTraits>::params();
79
80
3294
    switch (KeyGenTraits::DoKeyGen(AsyncWrap::env(), params)) {
81
3200
      case KeyGenJobStatus::OK:
82
3200
        status_ = KeyGenJobStatus::OK;
83
        // Success!
84
3200
        break;
85
94
      case KeyGenJobStatus::FAILED: {
86
94
        CryptoErrorStore* errors = CryptoJob<KeyGenTraits>::errors();
87
94
        errors->Capture();
88
94
        if (errors->Empty())
89
          errors->Insert(NodeCryptoError::KEY_GENERATION_JOB_FAILED);
90
      }
91
    }
92
3294
  }
93
94
3293
  v8::Maybe<bool> ToResult(
95
      v8::Local<v8::Value>* err,
96
      v8::Local<v8::Value>* result) override {
97
3293
    Environment* env = AsyncWrap::env();
98
3293
    CryptoErrorStore* errors = CryptoJob<KeyGenTraits>::errors();
99
3293
    AdditionalParams* params = CryptoJob<KeyGenTraits>::params();
100
101
3293
    if (status_ == KeyGenJobStatus::OK) {
102
3199
      v8::Maybe<bool> ret = KeyGenTraits::EncodeKey(env, params, result);
103

6393
      if (ret.IsJust() && ret.FromJust()) {
104
6388
        *err = Undefined(env->isolate());
105
      }
106
3199
      return ret;
107
    }
108
109
94
    if (errors->Empty())
110
      errors->Capture();
111
94
    CHECK(!errors->Empty());
112
188
    *result = Undefined(env->isolate());
113
188
    return v8::Just(errors->ToException(env).ToLocal(err));
114
  }
115
116
  SET_SELF_SIZE(KeyGenJob)
117
118
 private:
119
  KeyGenJobStatus status_ = KeyGenJobStatus::FAILED;
120
};
121
122
// A Base KeyGenTraits for Key Pair generation algorithms.
123
template <typename KeyPairAlgorithmTraits>
124
struct KeyPairGenTraits final {
125
  using AdditionalParameters =
126
      typename KeyPairAlgorithmTraits::AdditionalParameters;
127
128
  static const AsyncWrap::ProviderType Provider =
129
      AsyncWrap::PROVIDER_KEYPAIRGENREQUEST;
130
  static constexpr const char* JobName = KeyPairAlgorithmTraits::JobName;
131
132
779
  static v8::Maybe<bool> AdditionalConfig(
133
      CryptoJobMode mode,
134
      const v8::FunctionCallbackInfo<v8::Value>& args,
135
      unsigned int* offset,
136
      AdditionalParameters* params) {
137
    // Notice that offset is a pointer. Each of the AdditionalConfig,
138
    // GetPublicKeyEncodingFromJs, and GetPrivateKeyEncodingFromJs
139
    // functions will update the value of the offset as they successfully
140
    // process input parameters. This allows each job to have a variable
141
    // number of input parameters specific to each job type.
142
1558
    if (KeyPairAlgorithmTraits::AdditionalConfig(mode, args, offset, params)
143
            .IsNothing()) {
144
4
      return v8::Just(false);
145
    }
146
147
775
    params->public_key_encoding = ManagedEVPPKey::GetPublicKeyEncodingFromJs(
148
        args,
149
        offset,
150
        kKeyContextGenerate);
151
152
775
    auto private_key_encoding =
153
        ManagedEVPPKey::GetPrivateKeyEncodingFromJs(
154
            args,
155
            offset,
156
            kKeyContextGenerate);
157
158
775
    if (!private_key_encoding.IsEmpty())
159
774
      params->private_key_encoding = private_key_encoding.Release();
160
161
775
    return v8::Just(true);
162
  }
163
164
774
  static KeyGenJobStatus DoKeyGen(
165
      Environment* env,
166
      AdditionalParameters* params) {
167
1548
    EVPKeyCtxPointer ctx = KeyPairAlgorithmTraits::Setup(params);
168
169
774
    if (!ctx)
170
      return KeyGenJobStatus::FAILED;
171
172
    // Generate the key
173
774
    EVP_PKEY* pkey = nullptr;
174
774
    if (!EVP_PKEY_keygen(ctx.get(), &pkey))
175
94
      return KeyGenJobStatus::FAILED;
176
177
680
    params->key = ManagedEVPPKey(EVPKeyPointer(pkey));
178
680
    return KeyGenJobStatus::OK;
179
  }
180
181
679
  static v8::Maybe<bool> EncodeKey(
182
      Environment* env,
183
      AdditionalParameters* params,
184
      v8::Local<v8::Value>* result) {
185
2037
    v8::Local<v8::Value> keys[2];
186
679
    if (params->key
187
679
            .ToEncodedPublicKey(env, params->public_key_encoding, &keys[0])
188

2032
            .IsNothing() ||
189
        params->key
190
674
            .ToEncodedPrivateKey(env, params->private_key_encoding, &keys[1])
191
1353
            .IsNothing()) {
192
5
      return v8::Nothing<bool>();
193
    }
194
674
    *result = v8::Array::New(env->isolate(), keys, arraysize(keys));
195
674
    return v8::Just(true);
196
  }
197
};
198
199
struct SecretKeyGenConfig final : public MemoryRetainer {
200
  size_t length;        // In bytes.
201
  ByteSource out;       // Placeholder for the generated key bytes.
202
203
  void MemoryInfo(MemoryTracker* tracker) const override;
204
  SET_MEMORY_INFO_NAME(SecretKeyGenConfig)
205
  SET_SELF_SIZE(SecretKeyGenConfig)
206
};
207
208
struct SecretKeyGenTraits final {
209
  using AdditionalParameters = SecretKeyGenConfig;
210
  static const AsyncWrap::ProviderType Provider =
211
      AsyncWrap::PROVIDER_KEYGENREQUEST;
212
  static constexpr const char* JobName = "SecretKeyGenJob";
213
214
  static v8::Maybe<bool> AdditionalConfig(
215
      CryptoJobMode mode,
216
      const v8::FunctionCallbackInfo<v8::Value>& args,
217
      unsigned int* offset,
218
      SecretKeyGenConfig* params);
219
220
  static KeyGenJobStatus DoKeyGen(
221
      Environment* env,
222
      SecretKeyGenConfig* params);
223
224
  static v8::Maybe<bool> EncodeKey(
225
      Environment* env,
226
      SecretKeyGenConfig* params,
227
      v8::Local<v8::Value>* result);
228
};
229
230
template <typename AlgorithmParams>
231
struct KeyPairGenConfig final : public MemoryRetainer {
232
  PublicKeyEncodingConfig public_key_encoding;
233
  PrivateKeyEncodingConfig private_key_encoding;
234
  ManagedEVPPKey key;
235
  AlgorithmParams params;
236
237
779
  KeyPairGenConfig() = default;
238
3114
  ~KeyPairGenConfig() {
239
3114
    Mutex::ScopedLock priv_lock(*key.mutex());
240
  }
241
242
779
  explicit KeyPairGenConfig(KeyPairGenConfig&& other) noexcept
243
      : public_key_encoding(other.public_key_encoding),
244
        private_key_encoding(
245
            std::forward<PrivateKeyEncodingConfig>(
246
779
                other.private_key_encoding)),
247
779
        key(std::move(other.key)),
248
779
        params(std::move(other.params)) {}
249
250
  KeyPairGenConfig& operator=(KeyPairGenConfig&& other) noexcept {
251
    if (&other == this) return *this;
252
    this->~KeyPairGenConfig();
253
    return *new (this) KeyPairGenConfig(std::move(other));
254
  }
255
256
  void MemoryInfo(MemoryTracker* tracker) const override {
257
    tracker->TrackField("key", key);
258
    if (!private_key_encoding.passphrase_.IsEmpty()) {
259
      tracker->TrackFieldWithSize("private_key_encoding.passphrase",
260
                                  private_key_encoding.passphrase_->size());
261
    }
262
    tracker->TrackField("params", params);
263
  }
264
265
  SET_MEMORY_INFO_NAME(KeyPairGenConfig)
266
  SET_SELF_SIZE(KeyPairGenConfig)
267
};
268
269
struct NidKeyPairParams final : public MemoryRetainer {
270
  int id;
271
  SET_NO_MEMORY_INFO()
272
  SET_MEMORY_INFO_NAME(NidKeyPairParams)
273
  SET_SELF_SIZE(NidKeyPairParams)
274
};
275
276
using NidKeyPairGenConfig = KeyPairGenConfig<NidKeyPairParams>;
277
278
struct NidKeyPairGenTraits final {
279
  using AdditionalParameters = NidKeyPairGenConfig;
280
  static constexpr const char* JobName = "NidKeyPairGenJob";
281
282
  static EVPKeyCtxPointer Setup(NidKeyPairGenConfig* params);
283
284
  static v8::Maybe<bool> AdditionalConfig(
285
      CryptoJobMode mode,
286
      const v8::FunctionCallbackInfo<v8::Value>& args,
287
      unsigned int* offset,
288
      NidKeyPairGenConfig* params);
289
};
290
291
using NidKeyPairGenJob = KeyGenJob<KeyPairGenTraits<NidKeyPairGenTraits>>;
292
using SecretKeyGenJob = KeyGenJob<SecretKeyGenTraits>;
293
}  // namespace crypto
294
}  // namespace node
295
296
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
297
#endif  // SRC_CRYPTO_CRYPTO_KEYGEN_H_
298