-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
crypto: handle OpenSSL error queue in CipherBase #21288
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2595,8 +2595,11 @@ void CipherBase::Init(const char* cipher_type, | |
|
|
||
| ctx_.reset(EVP_CIPHER_CTX_new()); | ||
| const bool encrypt = (kind_ == kCipher); | ||
| CHECK(EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, | ||
| nullptr, nullptr, encrypt)); | ||
| if (1 != EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, | ||
|
Contributor
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. Small style nit, but I was curious why you would do this over the regular |
||
| nullptr, nullptr, encrypt)) { | ||
| return ThrowCryptoError(env(), ERR_get_error(), | ||
| "Failed to initialize cipher"); | ||
| } | ||
|
|
||
| int mode = EVP_CIPHER_CTX_mode(ctx_.get()); | ||
| if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE || | ||
|
|
@@ -2619,12 +2622,15 @@ void CipherBase::Init(const char* cipher_type, | |
|
|
||
| CHECK_EQ(1, EVP_CIPHER_CTX_set_key_length(ctx_.get(), key_len)); | ||
|
|
||
| CHECK(EVP_CipherInit_ex(ctx_.get(), | ||
| nullptr, | ||
| nullptr, | ||
| reinterpret_cast<unsigned char*>(key), | ||
| reinterpret_cast<unsigned char*>(iv), | ||
| encrypt)); | ||
| if (1 != EVP_CipherInit_ex(ctx_.get(), | ||
|
Contributor
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. Same as above.
Contributor
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. Same as above. |
||
| nullptr, | ||
| nullptr, | ||
| reinterpret_cast<unsigned char*>(key), | ||
| reinterpret_cast<unsigned char*>(iv), | ||
| encrypt)) { | ||
| return ThrowCryptoError(env(), ERR_get_error(), | ||
| "Failed to initialize cipher"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -2690,8 +2696,11 @@ void CipherBase::InitIv(const char* cipher_type, | |
| EVP_CIPHER_CTX_set_flags(ctx_.get(), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); | ||
|
|
||
| const bool encrypt = (kind_ == kCipher); | ||
| CHECK(EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, | ||
| nullptr, nullptr, encrypt)); | ||
| if (1 != EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, | ||
|
Contributor
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. Same
Contributor
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. Same as above. |
||
| nullptr, nullptr, encrypt)) { | ||
| return ThrowCryptoError(env(), ERR_get_error(), | ||
| "Failed to initialize cipher"); | ||
| } | ||
|
|
||
| if (IsAuthenticatedMode()) { | ||
| CHECK(has_iv); | ||
|
|
@@ -2704,12 +2713,15 @@ void CipherBase::InitIv(const char* cipher_type, | |
| return env()->ThrowError("Invalid key length"); | ||
| } | ||
|
|
||
| CHECK(EVP_CipherInit_ex(ctx_.get(), | ||
| nullptr, | ||
| nullptr, | ||
| reinterpret_cast<const unsigned char*>(key), | ||
| reinterpret_cast<const unsigned char*>(iv), | ||
| encrypt)); | ||
| if (1 != EVP_CipherInit_ex(ctx_.get(), | ||
|
Contributor
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. This is getting boring, isn't it? 😛
Contributor
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. God, isn't this repetitive 😅 |
||
| nullptr, | ||
| nullptr, | ||
| reinterpret_cast<const unsigned char*>(key), | ||
| reinterpret_cast<const unsigned char*>(iv), | ||
| encrypt)) { | ||
| return ThrowCryptoError(env(), ERR_get_error(), | ||
| "Failed to initialize cipher"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
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.
Style nit, but shouldn't this be done the other way around? Eg:
EVP_CipherInit_ex(...) != 1? Would love to hear why you chose this over the former.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.
Didn't know we had a preference. I usually do the opposite, but I think having
!= 1at the end of a multi-line expression looks weird.Fun fact, this style was commonly used to prevent accidental assignment of variables / pointers in boolean expressions.
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.
Because people would accidentally type
=instead of==all the times? Sounds more likely in the era of butterfly keyboards, heh. If it makes sense for you to keep this, feel free to do so. I agree wholeheartedly that!= 1looks weird when in a multiline statement, just wanted to make sure we're not going against the style guide here.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.
Yes, even though I personally still prefer
var == constant, the only reason here is that it looks weird. If no one has a strong opinion against this, I'd prefer to keep it this way.