Skip to content

Commit 705fefd

Browse files
authored
src: refactor to avoid using a moved object
KeyPairGenTraits::EncodeKey should not use an object that it previously moved. Make ManagedEVPPKey::ToEncoded(Public|Private)Key non-static members of ManagedEVPPKey and call them on the key object instead. PR-URL: #44269 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Feng Yu <F3n67u@outlook.com>
1 parent 27ea62a commit 705fefd

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

src/crypto/crypto_keygen.h

+6-10
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,12 @@ struct KeyPairGenTraits final {
186186
AdditionalParameters* params,
187187
v8::Local<v8::Value>* result) {
188188
v8::Local<v8::Value> keys[2];
189-
if (ManagedEVPPKey::ToEncodedPublicKey(
190-
env,
191-
std::move(params->key),
192-
params->public_key_encoding,
193-
&keys[0]).IsNothing() ||
194-
ManagedEVPPKey::ToEncodedPrivateKey(
195-
env,
196-
std::move(params->key),
197-
params->private_key_encoding,
198-
&keys[1]).IsNothing()) {
189+
if (params->key
190+
.ToEncodedPublicKey(env, params->public_key_encoding, &keys[0])
191+
.IsNothing() ||
192+
params->key
193+
.ToEncodedPrivateKey(env, params->private_key_encoding, &keys[1])
194+
.IsNothing()) {
199195
return v8::Nothing<bool>();
200196
}
201197
*result = v8::Array::New(env->isolate(), keys, arraysize(keys));

src/crypto/crypto_keys.cc

+8-10
Original file line numberDiff line numberDiff line change
@@ -633,44 +633,42 @@ Maybe<bool> ExportJWKInner(Environment* env,
633633

634634
Maybe<bool> ManagedEVPPKey::ToEncodedPublicKey(
635635
Environment* env,
636-
ManagedEVPPKey key,
637636
const PublicKeyEncodingConfig& config,
638637
Local<Value>* out) {
639-
if (!key) return Nothing<bool>();
638+
if (!*this) return Nothing<bool>();
640639
if (config.output_key_object_) {
641640
// Note that this has the downside of containing sensitive data of the
642641
// private key.
643642
std::shared_ptr<KeyObjectData> data =
644-
KeyObjectData::CreateAsymmetric(kKeyTypePublic, std::move(key));
643+
KeyObjectData::CreateAsymmetric(kKeyTypePublic, *this);
645644
return Tristate(KeyObjectHandle::Create(env, data).ToLocal(out));
646645
} else if (config.format_ == kKeyFormatJWK) {
647646
std::shared_ptr<KeyObjectData> data =
648-
KeyObjectData::CreateAsymmetric(kKeyTypePublic, std::move(key));
647+
KeyObjectData::CreateAsymmetric(kKeyTypePublic, *this);
649648
*out = Object::New(env->isolate());
650649
return ExportJWKInner(env, data, *out, false);
651650
}
652651

653-
return Tristate(WritePublicKey(env, key.get(), config).ToLocal(out));
652+
return Tristate(WritePublicKey(env, get(), config).ToLocal(out));
654653
}
655654

656655
Maybe<bool> ManagedEVPPKey::ToEncodedPrivateKey(
657656
Environment* env,
658-
ManagedEVPPKey key,
659657
const PrivateKeyEncodingConfig& config,
660658
Local<Value>* out) {
661-
if (!key) return Nothing<bool>();
659+
if (!*this) return Nothing<bool>();
662660
if (config.output_key_object_) {
663661
std::shared_ptr<KeyObjectData> data =
664-
KeyObjectData::CreateAsymmetric(kKeyTypePrivate, std::move(key));
662+
KeyObjectData::CreateAsymmetric(kKeyTypePrivate, *this);
665663
return Tristate(KeyObjectHandle::Create(env, data).ToLocal(out));
666664
} else if (config.format_ == kKeyFormatJWK) {
667665
std::shared_ptr<KeyObjectData> data =
668-
KeyObjectData::CreateAsymmetric(kKeyTypePrivate, std::move(key));
666+
KeyObjectData::CreateAsymmetric(kKeyTypePrivate, *this);
669667
*out = Object::New(env->isolate());
670668
return ExportJWKInner(env, data, *out, false);
671669
}
672670

673-
return Tristate(WritePrivateKey(env, key.get(), config).ToLocal(out));
671+
return Tristate(WritePrivateKey(env, get(), config).ToLocal(out));
674672
}
675673

676674
NonCopyableMaybe<PrivateKeyEncodingConfig>

src/crypto/crypto_keys.h

+6-10
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,13 @@ class ManagedEVPPKey : public MemoryRetainer {
112112
unsigned int* offset,
113113
bool allow_key_object);
114114

115-
static v8::Maybe<bool> ToEncodedPublicKey(
116-
Environment* env,
117-
ManagedEVPPKey key,
118-
const PublicKeyEncodingConfig& config,
119-
v8::Local<v8::Value>* out);
115+
v8::Maybe<bool> ToEncodedPublicKey(Environment* env,
116+
const PublicKeyEncodingConfig& config,
117+
v8::Local<v8::Value>* out);
120118

121-
static v8::Maybe<bool> ToEncodedPrivateKey(
122-
Environment* env,
123-
ManagedEVPPKey key,
124-
const PrivateKeyEncodingConfig& config,
125-
v8::Local<v8::Value>* out);
119+
v8::Maybe<bool> ToEncodedPrivateKey(Environment* env,
120+
const PrivateKeyEncodingConfig& config,
121+
v8::Local<v8::Value>* out);
126122

127123
private:
128124
size_t size_of_private_key() const;

0 commit comments

Comments
 (0)