Skip to content

Commit 8bdf37e

Browse files
committed
crypto: add internal error codes
1 parent 52ca115 commit 8bdf37e

6 files changed

+107
-54
lines changed

src/crypto/crypto_cipher.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -227,18 +227,18 @@ class CipherJob final : public CryptoJob<CipherTraits> {
227227
// Success!
228228
return;
229229
}
230-
CryptoErrorVector* errors = CryptoJob<CipherTraits>::errors();
230+
CryptoErrorStore* errors = CryptoJob<CipherTraits>::errors();
231231
errors->Capture();
232-
if (errors->empty()) {
232+
if (errors->Empty()) {
233233
switch (status) {
234234
case WebCryptoCipherStatus::OK:
235235
UNREACHABLE();
236236
break;
237237
case WebCryptoCipherStatus::INVALID_KEY_TYPE:
238-
errors->emplace_back("Invalid key type.");
238+
errors->Insert(NodeCryptoError::INVALID_KEY_TYPE);
239239
break;
240240
case WebCryptoCipherStatus::FAILED:
241-
errors->emplace_back("Cipher job failed.");
241+
errors->Insert(NodeCryptoError::CIPHER_JOB_FAILED);
242242
break;
243243
}
244244
}
@@ -248,9 +248,9 @@ class CipherJob final : public CryptoJob<CipherTraits> {
248248
v8::Local<v8::Value>* err,
249249
v8::Local<v8::Value>* result) override {
250250
Environment* env = AsyncWrap::env();
251-
CryptoErrorVector* errors = CryptoJob<CipherTraits>::errors();
251+
CryptoErrorStore* errors = CryptoJob<CipherTraits>::errors();
252252
if (out_.size() > 0) {
253-
CHECK(errors->empty());
253+
CHECK(errors->Empty());
254254
*err = v8::Undefined(env->isolate());
255255
*result = out_.ToArrayBuffer(env);
256256
return v8::Just(!result->IsEmpty());

src/crypto/crypto_context.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ void SecureContext::SetEngineKey(const FunctionCallbackInfo<Value>& args) {
551551

552552
CHECK_EQ(args.Length(), 2);
553553

554-
CryptoErrorVector errors;
554+
CryptoErrorStore errors;
555555
Utf8Value engine_id(env->isolate(), args[1]);
556556
EnginePointer engine = LoadEngineById(*engine_id, &errors);
557557
if (!engine) {
@@ -987,7 +987,7 @@ void SecureContext::SetClientCertEngine(
987987
// support multiple calls to SetClientCertEngine.
988988
CHECK(!sc->client_cert_engine_provided_);
989989

990-
CryptoErrorVector errors;
990+
CryptoErrorStore errors;
991991
const Utf8Value engine_id(env->isolate(), args[0]);
992992
EnginePointer engine = LoadEngineById(*engine_id, &errors);
993993
if (!engine) {

src/crypto/crypto_keygen.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ class KeyGenJob final : public CryptoJob<KeyGenTraits> {
8282
// Success!
8383
break;
8484
case KeyGenJobStatus::FAILED: {
85-
CryptoErrorVector* errors = CryptoJob<KeyGenTraits>::errors();
85+
CryptoErrorStore* errors = CryptoJob<KeyGenTraits>::errors();
8686
errors->Capture();
87-
if (errors->empty())
88-
errors->push_back(std::string("Key generation job failed"));
87+
if (errors->Empty())
88+
errors->Insert(NodeCryptoError::KEY_GENERATION_JOB_FAILED);
8989
}
9090
}
9191
}
@@ -94,17 +94,17 @@ class KeyGenJob final : public CryptoJob<KeyGenTraits> {
9494
v8::Local<v8::Value>* err,
9595
v8::Local<v8::Value>* result) override {
9696
Environment* env = AsyncWrap::env();
97-
CryptoErrorVector* errors = CryptoJob<KeyGenTraits>::errors();
97+
CryptoErrorStore* errors = CryptoJob<KeyGenTraits>::errors();
9898
AdditionalParams* params = CryptoJob<KeyGenTraits>::params();
9999
if (status_ == KeyGenJobStatus::OK &&
100100
LIKELY(!KeyGenTraits::EncodeKey(env, params, result).IsNothing())) {
101101
*err = Undefined(env->isolate());
102102
return v8::Just(true);
103103
}
104104

105-
if (errors->empty())
105+
if (errors->Empty())
106106
errors->Capture();
107-
CHECK(!errors->empty());
107+
CHECK(!errors->Empty());
108108
*result = Undefined(env->isolate());
109109
return v8::Just(errors->ToException(env).ToLocal(err));
110110
}

src/crypto/crypto_keys.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -347,18 +347,18 @@ class KeyExportJob final : public CryptoJob<KeyExportTraits> {
347347
// Success!
348348
return;
349349
}
350-
CryptoErrorVector* errors = CryptoJob<KeyExportTraits>::errors();
350+
CryptoErrorStore* errors = CryptoJob<KeyExportTraits>::errors();
351351
errors->Capture();
352-
if (errors->empty()) {
352+
if (errors->Empty()) {
353353
switch (status) {
354354
case WebCryptoKeyExportStatus::OK:
355355
UNREACHABLE();
356356
break;
357357
case WebCryptoKeyExportStatus::INVALID_KEY_TYPE:
358-
errors->emplace_back("Invalid key type.");
358+
errors->Insert(NodeCryptoError::INVALID_KEY_TYPE);
359359
break;
360360
case WebCryptoKeyExportStatus::FAILED:
361-
errors->emplace_back("Cipher job failed.");
361+
errors->Insert(NodeCryptoError::CIPHER_JOB_FAILED);
362362
break;
363363
}
364364
}
@@ -368,17 +368,17 @@ class KeyExportJob final : public CryptoJob<KeyExportTraits> {
368368
v8::Local<v8::Value>* err,
369369
v8::Local<v8::Value>* result) override {
370370
Environment* env = AsyncWrap::env();
371-
CryptoErrorVector* errors = CryptoJob<KeyExportTraits>::errors();
371+
CryptoErrorStore* errors = CryptoJob<KeyExportTraits>::errors();
372372
if (out_.size() > 0) {
373-
CHECK(errors->empty());
373+
CHECK(errors->Empty());
374374
*err = v8::Undefined(env->isolate());
375375
*result = out_.ToArrayBuffer(env);
376376
return v8::Just(!result->IsEmpty());
377377
}
378378

379-
if (errors->empty())
379+
if (errors->Empty())
380380
errors->Capture();
381-
CHECK(!errors->empty());
381+
CHECK(!errors->Empty());
382382
*result = v8::Undefined(env->isolate());
383383
return v8::Just(errors->ToException(env).ToLocal(err));
384384
}

src/crypto/crypto_util.cc

+60-25
Original file line numberDiff line numberDiff line change
@@ -187,51 +187,86 @@ void TestFipsCrypto(const v8::FunctionCallbackInfo<v8::Value>& args) {
187187
args.GetReturnValue().Set(enabled);
188188
}
189189

190-
void CryptoErrorVector::Capture() {
191-
clear();
192-
while (auto err = ERR_get_error()) {
190+
void CryptoErrorStore::Capture() {
191+
errors_.clear();
192+
while (const uint32_t err = ERR_get_error()) {
193193
char buf[256];
194194
ERR_error_string_n(err, buf, sizeof(buf));
195-
push_back(buf);
195+
errors_.emplace_back(buf);
196196
}
197-
std::reverse(begin(), end());
197+
std::reverse(std::begin(errors_), std::end(errors_));
198198
}
199199

200-
MaybeLocal<Value> CryptoErrorVector::ToException(
200+
bool CryptoErrorStore::Empty() const {
201+
return errors_.empty();
202+
}
203+
204+
template<typename... Args>
205+
void CryptoErrorStore::Insert(const NodeCryptoError error, Args&&... args) {
206+
const char* error_string;
207+
switch(error) {
208+
case NodeCryptoError::CIPHER_JOB_FAILED:
209+
error_string = "Cipher job failed";
210+
break;
211+
case NodeCryptoError::DERIVING_BITS_FAILED:
212+
error_string = "Deriving bits failed";
213+
break;
214+
case NodeCryptoError::ENGINE_NOT_FOUND:
215+
error_string = "Engine \"%s\" was not found";
216+
break;
217+
case NodeCryptoError::INVALID_KEY_TYPE:
218+
error_string = "Invalid key type";
219+
break;
220+
case NodeCryptoError::KEY_GENERATION_JOB_FAILED:
221+
error_string = "Key generation failed";
222+
break;
223+
case NodeCryptoError::NO_ERROR:
224+
error_string = "No error";
225+
break;
226+
}
227+
errors.emplace_back(SPrintF(error_string,
228+
std::forward<Args>(args)...));
229+
}
230+
231+
MaybeLocal<Value> CryptoErrorStore::ToException(
201232
Environment* env,
202233
Local<String> exception_string) const {
203234
if (exception_string.IsEmpty()) {
204-
CryptoErrorVector copy(*this);
205-
if (copy.empty()) copy.push_back("no error"); // But possibly a bug...
235+
CryptoErrorStore copy(*this);
236+
if (copy.Empty()) {
237+
// But possibly a bug...
238+
copy.Insert(NodeCryptoError::NO_ERROR);
239+
}
206240
// Use last element as the error message, everything else goes
207241
// into the .opensslErrorStack property on the exception object.
242+
const std::string& last_error_string = copy.errors_.back();
208243
Local<String> exception_string;
209244
if (!String::NewFromUtf8(
210245
env->isolate(),
211-
copy.back().data(),
246+
last_error_string,
212247
NewStringType::kNormal,
213-
copy.back().size()).ToLocal(&exception_string)) {
248+
last_error_string.size()).ToLocal(&exception_string)) {
214249
return MaybeLocal<Value>();
215250
}
216-
copy.pop_back();
251+
copy.errors_.pop_back();
217252
return copy.ToException(env, exception_string);
218253
}
219254

220255
Local<Value> exception_v = Exception::Error(exception_string);
221256
CHECK(!exception_v.IsEmpty());
222257

223-
if (!empty()) {
224-
CHECK(exception_v->IsObject());
225-
Local<Object> exception = exception_v.As<Object>();
226-
Local<Value> stack;
227-
if (!ToV8Value(env->context(), *this).ToLocal(&stack) ||
228-
exception->Set(env->context(), env->openssl_error_stack(), stack)
229-
.IsNothing()) {
230-
return MaybeLocal<Value>();
231-
}
258+
if (Empty()) {
259+
return exception_v;
232260
}
233261

234-
return exception_v;
262+
CHECK(exception_v->IsObject());
263+
Local<Object> exception = exception_v.As<Object>();
264+
Local<Value> stack;
265+
if (!ToV8Value(env->context(), *this).ToLocal(&stack) ||
266+
exception->Set(env->context(), env->openssl_error_stack(), stack)
267+
.IsNothing()) {
268+
return MaybeLocal<Value>();
269+
}
235270
}
236271

237272
ByteSource::ByteSource(ByteSource&& other) noexcept
@@ -509,7 +544,7 @@ void ThrowCryptoError(Environment* env,
509544
Local<Object> obj;
510545
if (!String::NewFromUtf8(env->isolate(), message).ToLocal(&exception_string))
511546
return;
512-
CryptoErrorVector errors;
547+
CryptoErrorStore errors;
513548
errors.Capture();
514549
if (!errors.ToException(env, exception_string).ToLocal(&exception) ||
515550
!exception->ToObject(env->context()).ToLocal(&obj) ||
@@ -520,7 +555,7 @@ void ThrowCryptoError(Environment* env,
520555
}
521556

522557
#ifndef OPENSSL_NO_ENGINE
523-
EnginePointer LoadEngineById(const char* id, CryptoErrorVector* errors) {
558+
EnginePointer LoadEngineById(const char* id, CryptoErrorStore* errors) {
524559
MarkPopErrorOnReturn mark_pop_error_on_return;
525560

526561
EnginePointer engine(ENGINE_by_id(id));
@@ -539,14 +574,14 @@ EnginePointer LoadEngineById(const char* id, CryptoErrorVector* errors) {
539574
if (ERR_get_error() != 0) {
540575
errors->Capture();
541576
} else {
542-
errors->push_back(std::string("Engine \"") + id + "\" was not found");
577+
errors->Insert(NodeCryptoError::ENGINE_NOT_FOUND, id);
543578
}
544579
}
545580

546581
return engine;
547582
}
548583

549-
bool SetEngine(const char* id, uint32_t flags, CryptoErrorVector* errors) {
584+
bool SetEngine(const char* id, uint32_t flags, CryptoErrorStore* errors) {
550585
ClearErrorOnReturn clear_error_on_return;
551586
EnginePointer engine = LoadEngineById(id, errors);
552587
if (!engine)

src/crypto/crypto_util.h

+25-7
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,31 @@ void Decode(const v8::FunctionCallbackInfo<v8::Value>& args,
159159
}
160160
}
161161

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+
NO_ERROR,
169+
};
170+
162171
// Utility struct used to harvest error information from openssl's error stack
163-
struct CryptoErrorVector : public std::vector<std::string> {
172+
struct CryptoErrorStore {
173+
public:
164174
void Capture();
165175

176+
bool Empty() const;
177+
178+
template<typename... Args>
179+
void Insert(const NodeCryptoError error, Args&&... args);
180+
166181
v8::MaybeLocal<v8::Value> ToException(
167182
Environment* env,
168183
v8::Local<v8::String> exception_string = v8::Local<v8::String>()) const;
184+
185+
private:
186+
std::vector<std::string> errors_;
169187
};
170188

171189
template <typename T>
@@ -320,7 +338,7 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
320338

321339
CryptoJobMode mode() const { return mode_; }
322340

323-
CryptoErrorVector* errors() { return &errors_; }
341+
CryptoErrorStore* errors() { return &errors_; }
324342

325343
AdditionalParams* params() { return &params_; }
326344

@@ -364,7 +382,7 @@ class CryptoJob : public AsyncWrap, public ThreadPoolWork {
364382

365383
private:
366384
const CryptoJobMode mode_;
367-
CryptoErrorVector errors_;
385+
CryptoErrorStore errors_;
368386
AdditionalParams params_;
369387
};
370388

@@ -412,7 +430,7 @@ class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> {
412430
if (!DeriveBitsTraits::DeriveBits(
413431
AsyncWrap::env(),
414432
*CryptoJob<DeriveBitsTraits>::params(), &out_)) {
415-
CryptoErrorVector* errors = CryptoJob<DeriveBitsTraits>::errors();
433+
CryptoErrorStore* errors = CryptoJob<DeriveBitsTraits>::errors();
416434
errors->Capture();
417435
if (errors->empty())
418436
errors->push_back("Deriving bits failed");
@@ -425,7 +443,7 @@ class DeriveBitsJob final : public CryptoJob<DeriveBitsTraits> {
425443
v8::Local<v8::Value>* err,
426444
v8::Local<v8::Value>* result) override {
427445
Environment* env = AsyncWrap::env();
428-
CryptoErrorVector* errors = CryptoJob<DeriveBitsTraits>::errors();
446+
CryptoErrorStore* errors = CryptoJob<DeriveBitsTraits>::errors();
429447
if (success_) {
430448
CHECK(errors->empty());
431449
*err = v8::Undefined(env->isolate());
@@ -505,12 +523,12 @@ struct EnginePointer {
505523
}
506524
};
507525

508-
EnginePointer LoadEngineById(const char* id, CryptoErrorVector* errors);
526+
EnginePointer LoadEngineById(const char* id, CryptoErrorStore* errors);
509527

510528
bool SetEngine(
511529
const char* id,
512530
uint32_t flags,
513-
CryptoErrorVector* errors = nullptr);
531+
CryptoErrorStore* errors = nullptr);
514532

515533
void SetEngine(const v8::FunctionCallbackInfo<v8::Value>& args);
516534
#endif // !OPENSSL_NO_ENGINE

0 commit comments

Comments
 (0)