Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1 | From 20282b1cb0389553421c4c5b14de198c5dfff50b Mon Sep 17 00:00:00 2001 |
| 2 | From: Anna Henningsen <anna@addaleax.net> |
| 3 | Date: Sat, 20 Oct 2018 05:24:54 +0200 |
| 4 | Subject: [PATCH] src: use more explicit return type in Sign::SignFinal() |
| 5 | MIME-Version: 1.0 |
| 6 | Content-Type: text/plain; charset=UTF-8 |
| 7 | Content-Transfer-Encoding: 8bit |
| 8 | |
| 9 | Using the non-indexed variant of `std::get<>` broke Travis CI. |
| 10 | Also, this allows us to be a bit more concise when returning |
| 11 | from `SignFinal()` due to some error condition. |
| 12 | |
| 13 | Refs: https://github.com/nodejs/node/pull/23427 |
| 14 | |
| 15 | PR-URL: https://github.com/nodejs/node/pull/23779 |
| 16 | Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> |
| 17 | Reviewed-By: Tobias Nießen <tniessen@tnie.de> |
| 18 | Reviewed-By: Refael Ackermann <refack@gmail.com> |
| 19 | Reviewed-By: Colin Ihrig <cjihrig@gmail.com> |
| 20 | --- |
| 21 | src/node_crypto.cc | 23 +++++++++++------------ |
| 22 | src/node_crypto.h | 12 +++++++++++- |
| 23 | 2 files changed, 22 insertions(+), 13 deletions(-) |
| 24 | |
| 25 | diff --git a/src/node_crypto.cc b/src/node_crypto.cc |
| 26 | index bd8d9e032554..ec7d4f2bb5be 100644 |
| 27 | --- a/src/node_crypto.cc |
| 28 | +++ b/src/node_crypto.cc |
| 29 | @@ -3562,22 +3562,20 @@ static MallocedBuffer<unsigned char> Node_SignFinal(EVPMDPointer&& mdctx, |
| 30 | return MallocedBuffer<unsigned char>(); |
| 31 | } |
| 32 | |
| 33 | -std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal( |
| 34 | +Sign::SignResult Sign::SignFinal( |
| 35 | const char* key_pem, |
| 36 | int key_pem_len, |
| 37 | const char* passphrase, |
| 38 | int padding, |
| 39 | int salt_len) { |
| 40 | - MallocedBuffer<unsigned char> buffer; |
| 41 | - |
| 42 | if (!mdctx_) |
| 43 | - return std::make_pair(kSignNotInitialised, std::move(buffer)); |
| 44 | + return SignResult(kSignNotInitialised); |
| 45 | |
| 46 | EVPMDPointer mdctx = std::move(mdctx_); |
| 47 | |
| 48 | BIOPointer bp(BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len)); |
| 49 | if (!bp) |
| 50 | - return std::make_pair(kSignPrivateKey, std::move(buffer)); |
| 51 | + return SignResult(kSignPrivateKey); |
| 52 | |
| 53 | EVPKeyPointer pkey(PEM_read_bio_PrivateKey(bp.get(), |
| 54 | nullptr, |
| 55 | @@ -3588,7 +3586,7 @@ std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal( |
| 56 | // without `pkey` being set to nullptr; |
| 57 | // cf. the test of `test_bad_rsa_privkey.pem` for an example. |
| 58 | if (!pkey || 0 != ERR_peek_error()) |
| 59 | - return std::make_pair(kSignPrivateKey, std::move(buffer)); |
| 60 | + return SignResult(kSignPrivateKey); |
| 61 | |
| 62 | #ifdef NODE_FIPS_MODE |
| 63 | /* Validate DSA2 parameters from FIPS 186-4 */ |
| 64 | @@ -3612,9 +3610,10 @@ std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal( |
| 65 | } |
| 66 | #endif // NODE_FIPS_MODE |
| 67 | |
| 68 | - buffer = Node_SignFinal(std::move(mdctx), pkey, padding, salt_len); |
| 69 | + MallocedBuffer<unsigned char> buffer = |
| 70 | + Node_SignFinal(std::move(mdctx), pkey, padding, salt_len); |
| 71 | Error error = buffer.is_empty() ? kSignPrivateKey : kSignOk; |
| 72 | - return std::make_pair(error, std::move(buffer)); |
| 73 | + return SignResult(error, std::move(buffer)); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | @@ -3639,18 +3638,18 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) { |
| 78 | |
| 79 | ClearErrorOnReturn clear_error_on_return; |
| 80 | |
| 81 | - std::pair<Error, MallocedBuffer<unsigned char>> ret = sign->SignFinal( |
| 82 | + SignResult ret = sign->SignFinal( |
| 83 | buf, |
| 84 | buf_len, |
| 85 | len >= 2 && !args[1]->IsNull() ? *passphrase : nullptr, |
| 86 | padding, |
| 87 | salt_len); |
| 88 | |
| 89 | - if (std::get<Error>(ret) != kSignOk) |
| 90 | - return sign->CheckThrow(std::get<Error>(ret)); |
| 91 | + if (ret.error != kSignOk) |
| 92 | + return sign->CheckThrow(ret.error); |
| 93 | |
| 94 | MallocedBuffer<unsigned char> sig = |
| 95 | - std::move(std::get<MallocedBuffer<unsigned char>>(ret)); |
| 96 | + std::move(ret.signature); |
| 97 | |
| 98 | Local<Object> rc = |
| 99 | Buffer::New(env, reinterpret_cast<char*>(sig.release()), sig.size) |
| 100 | diff --git a/src/node_crypto.h b/src/node_crypto.h |
| 101 | index 6fcf737f6c43..0c26c1f6ff1d 100644 |
| 102 | --- a/src/node_crypto.h |
| 103 | +++ b/src/node_crypto.h |
| 104 | @@ -518,7 +518,17 @@ class Sign : public SignBase { |
| 105 | public: |
| 106 | static void Initialize(Environment* env, v8::Local<v8::Object> target); |
| 107 | |
| 108 | - std::pair<Error, MallocedBuffer<unsigned char>> SignFinal( |
| 109 | + struct SignResult { |
| 110 | + Error error; |
| 111 | + MallocedBuffer<unsigned char> signature; |
| 112 | + |
| 113 | + explicit SignResult( |
| 114 | + Error err, |
| 115 | + MallocedBuffer<unsigned char>&& sig = MallocedBuffer<unsigned char>()) |
| 116 | + : error(err), signature(std::move(sig)) {} |
| 117 | + }; |
| 118 | + |
| 119 | + SignResult SignFinal( |
| 120 | const char* key_pem, |
| 121 | int key_pem_len, |
| 122 | const char* passphrase, |