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
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/crypto/crypto_cipher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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>()));
});
}

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>();

Expand Down
94 changes: 58 additions & 36 deletions src/crypto/crypto_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ namespace node {

using v8::Array;
using v8::ArrayBufferView;
using v8::Boolean;
using v8::Context;
using v8::DontDelete;
using v8::Exception;
Expand Down Expand Up @@ -551,7 +550,9 @@ void SecureContext::SetEngineKey(const FunctionCallbackInfo<Value>& args) {
const Utf8Value engine_id(env->isolate(), args[1]);
EnginePointer engine = LoadEngineById(*engine_id, &errors);
if (!engine) {
env->isolate()->ThrowException(errors.ToException(env).ToLocalChecked());
Local<Value> exception;
if (errors.ToException(env).ToLocal(&exception))
env->isolate()->ThrowException(exception);
return;
}

Expand Down Expand Up @@ -1061,7 +1062,9 @@ void SecureContext::SetClientCertEngine(
const node::Utf8Value engine_id(env->isolate(), args[0]);
EnginePointer engine = LoadEngineById(*engine_id, &errors);
if (!engine) {
env->isolate()->ThrowException(errors.ToException(env).ToLocalChecked());
Local<Value> exception;
if (errors.ToException(env).ToLocal(&exception))
env->isolate()->ThrowException(exception);
return;
}

Expand All @@ -1079,7 +1082,10 @@ void SecureContext::GetTicketKeys(const FunctionCallbackInfo<Value>& args) {
SecureContext* wrap;
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());

Local<Object> buff = Buffer::New(wrap->env(), 48).ToLocalChecked();
Local<Object> buff;
if (!Buffer::New(wrap->env(), 48).ToLocal(&buff))
return;

memcpy(Buffer::Data(buff), wrap->ticket_key_name_, 16);
memcpy(Buffer::Data(buff) + 16, wrap->ticket_key_hmac_, 16);
memcpy(Buffer::Data(buff) + 32, wrap->ticket_key_aes_, 16);
Expand Down Expand Up @@ -1143,45 +1149,59 @@ int SecureContext::TicketKeyCallback(SSL* ssl,
HandleScope handle_scope(env->isolate());
Context::Scope context_scope(env->context());

Local<Value> argv[] = {
Buffer::Copy(env,
reinterpret_cast<char*>(name),
kTicketPartSize).ToLocalChecked(),
Buffer::Copy(env,
reinterpret_cast<char*>(iv),
kTicketPartSize).ToLocalChecked(),
Boolean::New(env->isolate(), enc != 0)
};

Local<Value> ret = node::MakeCallback(env->isolate(),
sc->object(),
env->ticketkeycallback_string(),
arraysize(argv),
argv,
{0, 0}).ToLocalChecked();
Local<Value> argv[3];

if (!Buffer::Copy(
env,
reinterpret_cast<char*>(name),
kTicketPartSize).ToLocal(&argv[0]) ||
!Buffer::Copy(
env,
reinterpret_cast<char*>(iv),
kTicketPartSize).ToLocal(&argv[1])) {
return -1;
}

argv[2] = env != 0 ? v8::True(env->isolate()) : v8::False(env->isolate());

Local<Value> ret;
if (!node::MakeCallback(
env->isolate(),
sc->object(),
env->ticketkeycallback_string(),
arraysize(argv),
argv,
{0, 0}).ToLocal(&ret) ||
!ret->IsArray()) {
return -1;
}
Local<Array> arr = ret.As<Array>();

int r =
arr->Get(env->context(),
kTicketKeyReturnIndex).ToLocalChecked()
->Int32Value(env->context()).FromJust();
Local<Value> val;
if (!arr->Get(env->context(), kTicketKeyReturnIndex).ToLocal(&val) ||
!val->IsInt32()) {
return -1;
}

int r = val.As<Int32>()->Value();
if (r < 0)
return r;

Local<Value> hmac = arr->Get(env->context(),
kTicketKeyHMACIndex).ToLocalChecked();
Local<Value> aes = arr->Get(env->context(),
kTicketKeyAESIndex).ToLocalChecked();
if (Buffer::Length(aes) != kTicketPartSize)
Local<Value> hmac;
Local<Value> aes;

if (!arr->Get(env->context(), kTicketKeyHMACIndex).ToLocal(&hmac) ||
!arr->Get(env->context(), kTicketKeyAESIndex).ToLocal(&aes) ||
Buffer::Length(aes) != kTicketPartSize) {
return -1;
}

if (enc) {
Local<Value> name_val = arr->Get(env->context(),
kTicketKeyNameIndex).ToLocalChecked();
Local<Value> iv_val = arr->Get(env->context(),
kTicketKeyIVIndex).ToLocalChecked();

if (Buffer::Length(name_val) != kTicketPartSize ||
Local<Value> name_val;
Local<Value> iv_val;
if (!arr->Get(env->context(), kTicketKeyNameIndex).ToLocal(&name_val) ||
!arr->Get(env->context(), kTicketKeyIVIndex).ToLocal(&iv_val) ||
Buffer::Length(name_val) != kTicketPartSize ||
Buffer::Length(iv_val) != kTicketPartSize) {
return -1;
}
Expand Down Expand Up @@ -1272,7 +1292,9 @@ void SecureContext::GetCertificate(const FunctionCallbackInfo<Value>& args) {
return args.GetReturnValue().SetNull();

int size = i2d_X509(cert, nullptr);
Local<Object> buff = Buffer::New(env, size).ToLocalChecked();
Local<Object> buff;
if (!Buffer::New(env, size).ToLocal(&buff))
return;
unsigned char* serialized = reinterpret_cast<unsigned char*>(
Buffer::Data(buff));
i2d_X509(cert, &serialized);
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/crypto_dh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>()));
}


Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions src/crypto/crypto_ecdh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_hash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_hmac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 21 additions & 12 deletions src/crypto/crypto_keys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>());
}
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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>()));
}

Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/crypto/crypto_sig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_spkac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading