Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: more crypto cleanup #35509

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
src: replace more toLocalCheckeds in crypto_*
Signed-off-by: James M Snell <jasnell@gmail.com>
jasnell committed Oct 8, 2020

Verified

This commit was signed with the committer’s verified signature.
jasnell James M Snell
commit e409e1b71863811cab74932ce9ac0ad82d4cd507
6 changes: 3 additions & 3 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
@@ -453,7 +453,7 @@ void CipherBase::GetAuthTag(const FunctionCallbackInfo<Value>& args) {

args.GetReturnValue().Set(
Buffer::Copy(env, cipher->auth_tag_, cipher->auth_tag_len_)
.ToLocalChecked());
.FromMaybe(Local<Value>()));
}

void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
@@ -643,7 +643,7 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
}

CHECK(out.data() != nullptr || out.size() == 0);
args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
});
}

@@ -734,7 +734,7 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
return ThrowCryptoError(env, ERR_get_error(), msg);
}

args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
}

template <PublicKeyCipher::Operation operation,
2 changes: 1 addition & 1 deletion src/crypto/crypto_common.cc
Original file line number Diff line number Diff line change
@@ -931,7 +931,7 @@ MaybeLocal<Value> GetPeerCert(
// First and main certificate.
X509Pointer first_cert(sk_X509_value(peer_certs.get(), 0));
CHECK(first_cert);
maybe_cert = X509ToObject(env, first_cert.release()).ToLocalChecked();
maybe_cert = X509ToObject(env, first_cert.release());
if (!maybe_cert.ToLocal(&result))
return MaybeLocal<Value>();

8 changes: 4 additions & 4 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
@@ -253,7 +253,7 @@ void DiffieHellman::GenerateKeys(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(size,
BN_bn2binpad(
pub_key, reinterpret_cast<unsigned char*>(data.data()), size));
args.GetReturnValue().Set(data.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>()));
}


@@ -275,7 +275,7 @@ void DiffieHellman::GetField(const FunctionCallbackInfo<Value>& args,
CHECK_EQ(
size,
BN_bn2binpad(num, reinterpret_cast<unsigned char*>(data.data()), size));
args.GetReturnValue().Set(data.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(data.ToBuffer().FromMaybe(Local<Value>()));
}

void DiffieHellman::GetPrime(const FunctionCallbackInfo<Value>& args) {
@@ -357,7 +357,7 @@ void DiffieHellman::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
CHECK_GE(size, 0);
ZeroPadDiffieHellmanSecret(static_cast<size_t>(size), &ret);

args.GetReturnValue().Set(ret.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(ret.ToBuffer().FromMaybe(Local<Value>()));
}

void DiffieHellman::SetKey(const FunctionCallbackInfo<Value>& args,
@@ -613,7 +613,7 @@ void DiffieHellman::Stateless(const FunctionCallbackInfo<Value>& args) {
if (out.size() == 0)
return ThrowCryptoError(env, ERR_get_error(), "diffieHellman failed");

args.GetReturnValue().Set(out.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
}

Maybe<bool> DHBitsTraits::AdditionalConfig(
6 changes: 2 additions & 4 deletions src/crypto/crypto_ecdh.cc
Original file line number Diff line number Diff line change
@@ -197,8 +197,7 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
if (!r)
return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to compute ECDH key");

Local<Object> buf = out.ToBuffer().ToLocalChecked();
args.GetReturnValue().Set(buf);
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
}

void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) {
@@ -244,8 +243,7 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) {
reinterpret_cast<unsigned char*>(out.data()),
size));

Local<Object> buf = out.ToBuffer().ToLocalChecked();
args.GetReturnValue().Set(buf);
args.GetReturnValue().Set(out.ToBuffer().FromMaybe(Local<Value>()));
}

void ECDH::SetPrivateKey(const FunctionCallbackInfo<Value>& args) {
2 changes: 1 addition & 1 deletion src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
@@ -198,7 +198,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
env->isolate()->ThrowException(error);
return;
}
args.GetReturnValue().Set(rc.ToLocalChecked());
args.GetReturnValue().Set(rc.FromMaybe(Local<Value>()));
}

HashConfig::HashConfig(HashConfig&& other) noexcept
2 changes: 1 addition & 1 deletion src/crypto/crypto_hmac.cc
Original file line number Diff line number Diff line change
@@ -134,7 +134,7 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
env->isolate()->ThrowException(error);
return;
}
args.GetReturnValue().Set(rc.ToLocalChecked());
args.GetReturnValue().Set(rc.FromMaybe(Local<Value>()));
}

HmacConfig::HmacConfig(HmacConfig&& other) noexcept
33 changes: 21 additions & 12 deletions src/crypto/crypto_keys.cc
Original file line number Diff line number Diff line change
@@ -266,20 +266,22 @@ ParseKeyResult ParsePrivateKey(EVPKeyPointer* pkey,
return ParseKeyResult::kParseKeyFailed;
}

Local<Value> BIOToStringOrBuffer(Environment* env,
BIO* bio,
PKFormatType format) {
MaybeLocal<Value> BIOToStringOrBuffer(
Environment* env,
BIO* bio,
PKFormatType format) {
BUF_MEM* bptr;
BIO_get_mem_ptr(bio, &bptr);
if (format == kKeyFormatPEM) {
// PEM is an ASCII format, so we will return it as a string.
return String::NewFromUtf8(env->isolate(), bptr->data,
NewStringType::kNormal,
bptr->length).ToLocalChecked();
bptr->length).FromMaybe(Local<Value>());
} else {
CHECK_EQ(format, kKeyFormatDER);
// DER is binary, return it as a buffer.
return Buffer::Copy(env, bptr->data, bptr->length).ToLocalChecked();
return Buffer::Copy(env, bptr->data, bptr->length)
.FromMaybe(Local<Value>());
}
}

@@ -1108,13 +1110,13 @@ void KeyObjectHandle::Export(const FunctionCallbackInfo<Value>& args) {
}

if (!result.IsEmpty())
args.GetReturnValue().Set(result.ToLocalChecked());
args.GetReturnValue().Set(result.FromMaybe(Local<Value>()));
}

Local<Value> KeyObjectHandle::ExportSecretKey() const {
MaybeLocal<Value> KeyObjectHandle::ExportSecretKey() const {
const char* buf = data_->GetSymmetricKey();
unsigned int len = data_->GetSymmetricKeySize();
return Buffer::Copy(env(), buf, len).ToLocalChecked();
return Buffer::Copy(env(), buf, len).FromMaybe(Local<Value>());
}

MaybeLocal<Value> KeyObjectHandle::ExportPublicKey(
@@ -1183,7 +1185,9 @@ void NativeKeyObject::CreateNativeKeyObjectClass(
KeyObjectHandle::kInternalFieldCount);
t->Inherit(BaseObject::GetConstructorTemplate(env));

Local<Value> ctor = t->GetFunction(env->context()).ToLocalChecked();
Local<Value> ctor;
if (!t->GetFunction(env->context()).ToLocal(&ctor))
return;

Local<Value> recv = Undefined(env->isolate());
Local<Value> ret_v;
@@ -1210,7 +1214,10 @@ BaseObjectPtr<BaseObject> NativeKeyObject::KeyObjectTransferData::Deserialize(
return {};
}

Local<Value> handle = KeyObjectHandle::Create(env, data_).ToLocalChecked();
Local<Value> handle;
if (!KeyObjectHandle::Create(env, data_).ToLocal(&handle))
return {};

Local<Function> key_ctor;
Local<Value> arg = FIXED_ONE_BYTE_STRING(env->isolate(),
"internal/crypto/keys");
@@ -1232,8 +1239,10 @@ BaseObjectPtr<BaseObject> NativeKeyObject::KeyObjectTransferData::Deserialize(
CHECK(false);
}

Local<Value> key =
key_ctor->NewInstance(context, 1, &handle).ToLocalChecked();
Local<Value> key;
if (!key_ctor->NewInstance(context, 1, &handle).ToLocal(&key))
return {};

return BaseObjectPtr<BaseObject>(Unwrap<KeyObjectHandle>(key.As<Object>()));
}

2 changes: 1 addition & 1 deletion src/crypto/crypto_keys.h
Original file line number Diff line number Diff line change
@@ -196,7 +196,7 @@ class KeyObjectHandle : public BaseObject {

static void Export(const v8::FunctionCallbackInfo<v8::Value>& args);

v8::Local<v8::Value> ExportSecretKey() const;
v8::MaybeLocal<v8::Value> ExportSecretKey() const;
v8::MaybeLocal<v8::Value> ExportPublicKey(
const PublicKeyEncodingConfig& config) const;
v8::MaybeLocal<v8::Value> ExportPrivateKey(
4 changes: 2 additions & 2 deletions src/crypto/crypto_sig.cc
Original file line number Diff line number Diff line change
@@ -377,7 +377,7 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
if (ret.error != kSignOk)
return crypto::CheckThrow(env, ret.error);

args.GetReturnValue().Set(ret.signature.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(ret.signature.ToBuffer().FromMaybe(Local<Value>()));
}

Verify::Verify(Environment* env, Local<Object> wrap)
@@ -581,7 +581,7 @@ void Sign::SignSync(const FunctionCallbackInfo<Value>& args) {
signature = ConvertSignatureToP1363(env, key, std::move(signature));
}

args.GetReturnValue().Set(signature.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(signature.ToBuffer().FromMaybe(Local<Value>()));
}

void Verify::VerifySync(const FunctionCallbackInfo<Value>& args) {
2 changes: 1 addition & 1 deletion src/crypto/crypto_spkac.cc
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
if (pkey.data() == nullptr)
return args.GetReturnValue().SetEmptyString();

args.GetReturnValue().Set(pkey.ToBuffer().ToLocalChecked());
args.GetReturnValue().Set(pkey.ToBuffer().FromMaybe(Local<Value>()));
}

OpenSSLBuffer ExportChallenge(const ArrayBufferOrViewContents<char>& input) {
37 changes: 21 additions & 16 deletions src/crypto/crypto_util.cc
Original file line number Diff line number Diff line change
@@ -178,10 +178,14 @@ MaybeLocal<Value> CryptoErrorVector::ToException(
if (copy.empty()) copy.push_back("no error"); // But possibly a bug...
// Use last element as the error message, everything else goes
// into the .opensslErrorStack property on the exception object.
auto exception_string =
String::NewFromUtf8(env->isolate(), copy.back().data(),
NewStringType::kNormal, copy.back().size())
.ToLocalChecked();
Local<String> exception_string;
if (!String::NewFromUtf8(
env->isolate(),
copy.back().data(),
NewStringType::kNormal,
copy.back().size()).ToLocal(&exception_string)) {
return MaybeLocal<Value>();
}
copy.pop_back();
return copy.ToException(env, exception_string);
}
@@ -192,11 +196,12 @@ MaybeLocal<Value> CryptoErrorVector::ToException(
if (!empty()) {
CHECK(exception_v->IsObject());
Local<Object> exception = exception_v.As<Object>();
Maybe<bool> ok = exception->Set(env->context(),
env->openssl_error_stack(),
ToV8Value(env->context(), *this).ToLocalChecked());
if (ok.IsNothing())
Local<Value> stack;
if (!ToV8Value(env->context(), *this).ToLocal(&stack) ||
exception->Set(env->context(), env->openssl_error_stack(), stack)
.IsNothing()) {
return MaybeLocal<Value>();
}
}

return exception_v;
@@ -470,18 +475,18 @@ void ThrowCryptoError(Environment* env,
message = message_buffer;
}
HandleScope scope(env->isolate());
Local<String> exception_string =
String::NewFromUtf8(env->isolate(), message).ToLocalChecked();
CryptoErrorVector errors;
errors.Capture();
Local<String> exception_string;
Local<Value> exception;
if (!errors.ToException(env, exception_string).ToLocal(&exception))
return;
Local<Object> obj;
if (!exception->ToObject(env->context()).ToLocal(&obj))
if (!String::NewFromUtf8(env->isolate(), message).ToLocal(&exception_string))
return;
if (error::Decorate(env, obj, err).IsNothing())
CryptoErrorVector errors;
errors.Capture();
if (!errors.ToException(env, exception_string).ToLocal(&exception) ||
!exception->ToObject(env->context()).ToLocal(&obj) ||
error::Decorate(env, obj, err).IsNothing()) {
return;
}
env->isolate()->ThrowException(exception);
}