-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
crypto: reduce memory usage of SignFinal #23427
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4b4b71b
crypto: reduce memory usage of SignFinal
tniessen e5bd9d0
fixup! crypto: reduce memory usage of SignFinal
tniessen 202d506
fixup! crypto: reduce memory usage of SignFinal
tniessen 27f5947
fixup! crypto: reduce memory usage of SignFinal
tniessen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
|
||
#include <algorithm> | ||
#include <memory> | ||
#include <utility> | ||
#include <vector> | ||
|
||
static const int X509_NAME_FLAGS = ASN1_STRFLGS_ESC_CTRL | ||
|
@@ -3539,26 +3540,29 @@ static MallocedBuffer<unsigned char> Node_SignFinal(EVPMDPointer&& mdctx, | |
EVP_PKEY_CTX_set_signature_md(pkctx.get(), | ||
EVP_MD_CTX_md(mdctx.get())) > 0 && | ||
EVP_PKEY_sign(pkctx.get(), sig.data, &sig_len, m, m_len) > 0) { | ||
return MallocedBuffer<unsigned char>(sig.release(), sig_len); | ||
sig.Truncate(sig_len); | ||
return sig; | ||
} | ||
|
||
return MallocedBuffer<unsigned char>(); | ||
} | ||
|
||
SignBase::Error Sign::SignFinal(const char* key_pem, | ||
int key_pem_len, | ||
const char* passphrase, | ||
MallocedBuffer<unsigned char>* buffer, | ||
int padding, | ||
int salt_len) { | ||
std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal( | ||
const char* key_pem, | ||
int key_pem_len, | ||
const char* passphrase, | ||
int padding, | ||
int salt_len) { | ||
MallocedBuffer<unsigned char> buffer; | ||
|
||
if (!mdctx_) | ||
return kSignNotInitialised; | ||
return std::make_pair(kSignNotInitialised, std::move(buffer)); | ||
|
||
EVPMDPointer mdctx = std::move(mdctx_); | ||
|
||
BIOPointer bp(BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len)); | ||
if (!bp) | ||
return kSignPrivateKey; | ||
return std::make_pair(kSignPrivateKey, std::move(buffer)); | ||
|
||
EVPKeyPointer pkey(PEM_read_bio_PrivateKey(bp.get(), | ||
nullptr, | ||
|
@@ -3569,7 +3573,7 @@ SignBase::Error Sign::SignFinal(const char* key_pem, | |
// 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 kSignPrivateKey; | ||
return std::make_pair(kSignPrivateKey, std::move(buffer)); | ||
|
||
#ifdef NODE_FIPS_MODE | ||
/* Validate DSA2 parameters from FIPS 186-4 */ | ||
|
@@ -3593,8 +3597,9 @@ SignBase::Error Sign::SignFinal(const char* key_pem, | |
} | ||
#endif // NODE_FIPS_MODE | ||
|
||
*buffer = Node_SignFinal(std::move(mdctx), pkey, padding, salt_len); | ||
return buffer->is_empty() ? kSignPrivateKey : kSignOk; | ||
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)); | ||
} | ||
|
||
|
||
|
@@ -3618,17 +3623,19 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) { | |
int salt_len = args[3].As<Int32>()->Value(); | ||
|
||
ClearErrorOnReturn clear_error_on_return; | ||
MallocedBuffer<unsigned char> sig; | ||
|
||
Error err = sign->SignFinal( | ||
std::pair<Error, MallocedBuffer<unsigned char>> ret = sign->SignFinal( | ||
buf, | ||
buf_len, | ||
len >= 2 && !args[1]->IsNull() ? *passphrase : nullptr, | ||
&sig, | ||
padding, | ||
salt_len); | ||
if (err != kSignOk) | ||
return sign->CheckThrow(err); | ||
|
||
if (std::get<Error>(ret) != kSignOk) | ||
return sign->CheckThrow(std::get<Error>(ret)); | ||
|
||
MallocedBuffer<unsigned char> sig = | ||
std::get<MallocedBuffer<unsigned char>>(std::move(ret)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to move the buffer not the ret: MallocedBuffer<unsigned char> sig = std::move(std::get<MallocedBuffer<unsigned char>>(ret)); Or: MallocedBuffer<unsigned char> sig(std::get<MallocedBuffer<unsigned char>>(ret)); Or my favorite: const auto& sig = std::get<MallocedBuffer<unsigned char>>(ret); |
||
|
||
Local<Object> rc = | ||
Buffer::New(env, reinterpret_cast<char*>(sig.release()), sig.size) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also possible:
then later:
std::get<Error>(ret) = kSignNotInitialised;
etc;