From 1bb51029998c7a53b78828a7a0c2f312768fc71e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Sat, 20 Oct 2018 05:24:54 +0200 Subject: [PATCH] src: use more explicit return type in Sign::SignFinal() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using the non-indexed variant of `std::get<>` broke Travis CI. Also, this allows us to be a bit more concise when returning from `SignFinal()` due to some error condition. Refs: https://github.com/nodejs/node/pull/23427 PR-URL: https://github.com/nodejs/node/pull/23779 Reviewed-By: Joyee Cheung Reviewed-By: Tobias Nießen Reviewed-By: Refael Ackermann Reviewed-By: Colin Ihrig --- src/node_crypto.cc | 23 +++++++++++------------ src/node_crypto.h | 12 +++++++++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index de3e28cfe9c539..3ddda9a77449db 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3572,22 +3572,20 @@ static MallocedBuffer Node_SignFinal(EVPMDPointer&& mdctx, return MallocedBuffer(); } -std::pair> Sign::SignFinal( +Sign::SignResult Sign::SignFinal( const char* key_pem, int key_pem_len, const char* passphrase, int padding, int salt_len) { - MallocedBuffer buffer; - if (!mdctx_) - return std::make_pair(kSignNotInitialised, std::move(buffer)); + return SignResult(kSignNotInitialised); EVPMDPointer mdctx = std::move(mdctx_); BIOPointer bp(BIO_new_mem_buf(const_cast(key_pem), key_pem_len)); if (!bp) - return std::make_pair(kSignPrivateKey, std::move(buffer)); + return SignResult(kSignPrivateKey); EVPKeyPointer pkey(PEM_read_bio_PrivateKey(bp.get(), nullptr, @@ -3598,7 +3596,7 @@ std::pair> Sign::SignFinal( // without `pkey` being set to nullptr; // cf. the test of `test_bad_rsa_privkey.pem` for an example. if (!pkey || 0 != ERR_peek_error()) - return std::make_pair(kSignPrivateKey, std::move(buffer)); + return SignResult(kSignPrivateKey); #ifdef NODE_FIPS_MODE /* Validate DSA2 parameters from FIPS 186-4 */ @@ -3622,9 +3620,10 @@ std::pair> Sign::SignFinal( } #endif // NODE_FIPS_MODE - buffer = Node_SignFinal(std::move(mdctx), pkey, padding, salt_len); + MallocedBuffer buffer = + Node_SignFinal(std::move(mdctx), pkey, padding, salt_len); Error error = buffer.is_empty() ? kSignPrivateKey : kSignOk; - return std::make_pair(error, std::move(buffer)); + return SignResult(error, std::move(buffer)); } @@ -3649,18 +3648,18 @@ void Sign::SignFinal(const FunctionCallbackInfo& args) { ClearErrorOnReturn clear_error_on_return; - std::pair> ret = sign->SignFinal( + SignResult ret = sign->SignFinal( buf, buf_len, len >= 2 && !args[1]->IsNull() ? *passphrase : nullptr, padding, salt_len); - if (std::get(ret) != kSignOk) - return sign->CheckThrow(std::get(ret)); + if (ret.error != kSignOk) + return sign->CheckThrow(ret.error); MallocedBuffer sig = - std::move(std::get>(ret)); + std::move(ret.signature); Local rc = Buffer::New(env, reinterpret_cast(sig.release()), sig.size) diff --git a/src/node_crypto.h b/src/node_crypto.h index b64a8c249a1303..e3627d47613781 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -516,7 +516,17 @@ class Sign : public SignBase { public: static void Initialize(Environment* env, v8::Local target); - std::pair> SignFinal( + struct SignResult { + Error error; + MallocedBuffer signature; + + explicit SignResult( + Error err, + MallocedBuffer&& sig = MallocedBuffer()) + : error(err), signature(std::move(sig)) {} + }; + + SignResult SignFinal( const char* key_pem, int key_pem_len, const char* passphrase,