@@ -159,15 +159,64 @@ void Decode(const v8::FunctionCallbackInfo<v8::Value>& args,
159
159
}
160
160
}
161
161
162
+ enum class NodeCryptoError {
163
+ CIPHER_JOB_FAILED,
164
+ DERIVING_BITS_FAILED,
165
+ ENGINE_NOT_FOUND,
166
+ INVALID_KEY_TYPE,
167
+ KEY_GENERATION_JOB_FAILED,
168
+ OK
169
+ };
170
+
162
171
// Utility struct used to harvest error information from openssl's error stack
163
- struct CryptoErrorVector : public std ::vector<std::string> {
172
+ struct CryptoErrorStore final : public MemoryRetainer {
173
+ public:
164
174
void Capture ();
165
175
176
+ bool Empty () const ;
177
+
178
+ template <typename ... Args>
179
+ void Insert (const NodeCryptoError error, Args&&... args);
180
+
166
181
v8::MaybeLocal<v8::Value> ToException (
167
182
Environment* env,
168
183
v8::Local<v8::String> exception_string = v8::Local<v8::String>()) const ;
184
+
185
+ SET_NO_MEMORY_INFO ()
186
+ SET_MEMORY_INFO_NAME (CryptoErrorStore);
187
+ SET_SELF_SIZE (CryptoErrorStore);
188
+
189
+ private:
190
+ std::vector<std::string> errors_;
169
191
};
170
192
193
+ template <typename ... Args>
194
+ void CryptoErrorStore::Insert (const NodeCryptoError error, Args&&... args) {
195
+ const char * error_string = nullptr ;
196
+ switch (error) {
197
+ case NodeCryptoError::CIPHER_JOB_FAILED:
198
+ error_string = " Cipher job failed" ;
199
+ break ;
200
+ case NodeCryptoError::DERIVING_BITS_FAILED:
201
+ error_string = " Deriving bits failed" ;
202
+ break ;
203
+ case NodeCryptoError::ENGINE_NOT_FOUND:
204
+ error_string = " Engine \" %s\" was not found" ;
205
+ break ;
206
+ case NodeCryptoError::INVALID_KEY_TYPE:
207
+ error_string = " Invalid key type" ;
208
+ break ;
209
+ case NodeCryptoError::KEY_GENERATION_JOB_FAILED:
210
+ error_string = " Key generation failed" ;
211
+ break ;
212
+ case NodeCryptoError::OK:
213
+ error_string = " Ok" ;
214
+ break ;
215
+ }
216
+ errors_.emplace_back (SPrintF (error_string,
217
+ std::forward<Args>(args)...));
218
+ }
219
+
171
220
template <typename T>
172
221
T* MallocOpenSSL (size_t count) {
173
222
void * mem = OPENSSL_malloc (MultiplyWithOverflowCheck (count, sizeof (T)));
@@ -320,7 +369,7 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
320
369
321
370
CryptoJobMode mode () const { return mode_; }
322
371
323
- CryptoErrorVector * errors () { return &errors_; }
372
+ CryptoErrorStore * errors () { return &errors_; }
324
373
325
374
AdditionalParams* params () { return ¶ms_; }
326
375
@@ -364,7 +413,7 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
364
413
365
414
private:
366
415
const CryptoJobMode mode_;
367
- CryptoErrorVector errors_;
416
+ CryptoErrorStore errors_;
368
417
AdditionalParams params_;
369
418
};
370
419
@@ -412,10 +461,10 @@ class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> {
412
461
if (!DeriveBitsTraits::DeriveBits (
413
462
AsyncWrap::env (),
414
463
*CryptoJob<DeriveBitsTraits>::params (), &out_)) {
415
- CryptoErrorVector * errors = CryptoJob<DeriveBitsTraits>::errors ();
464
+ CryptoErrorStore * errors = CryptoJob<DeriveBitsTraits>::errors ();
416
465
errors->Capture ();
417
- if (errors->empty ())
418
- errors->push_back ( " Deriving bits failed " );
466
+ if (errors->Empty ())
467
+ errors->Insert (NodeCryptoError::DERIVING_BITS_FAILED );
419
468
return ;
420
469
}
421
470
success_ = true ;
@@ -425,9 +474,9 @@ class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> {
425
474
v8::Local<v8::Value>* err,
426
475
v8::Local<v8::Value>* result) override {
427
476
Environment* env = AsyncWrap::env ();
428
- CryptoErrorVector * errors = CryptoJob<DeriveBitsTraits>::errors ();
477
+ CryptoErrorStore * errors = CryptoJob<DeriveBitsTraits>::errors ();
429
478
if (success_) {
430
- CHECK (errors->empty ());
479
+ CHECK (errors->Empty ());
431
480
*err = v8::Undefined (env->isolate ());
432
481
return DeriveBitsTraits::EncodeOutput (
433
482
env,
@@ -436,9 +485,9 @@ class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> {
436
485
result);
437
486
}
438
487
439
- if (errors->empty ())
488
+ if (errors->Empty ())
440
489
errors->Capture ();
441
- CHECK (!errors->empty ());
490
+ CHECK (!errors->Empty ());
442
491
*result = v8::Undefined (env->isolate ());
443
492
return v8::Just (errors->ToException (env).ToLocal (err));
444
493
}
@@ -505,12 +554,12 @@ struct EnginePointer {
505
554
}
506
555
};
507
556
508
- EnginePointer LoadEngineById (const char * id, CryptoErrorVector * errors);
557
+ EnginePointer LoadEngineById (const char * id, CryptoErrorStore * errors);
509
558
510
559
bool SetEngine (
511
560
const char * id,
512
561
uint32_t flags,
513
- CryptoErrorVector * errors = nullptr );
562
+ CryptoErrorStore * errors = nullptr );
514
563
515
564
void SetEngine (const v8::FunctionCallbackInfo<v8::Value>& args);
516
565
#endif // !OPENSSL_NO_ENGINE
0 commit comments