-
Notifications
You must be signed in to change notification settings - Fork 143
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
AES get error message #65
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,6 +78,9 @@ int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *outm, int *outl); | |
int EVP_BytesToKey(const EVP_CIPHER *type,const EVP_MD *md, | ||
const unsigned char *salt, const unsigned char *data, int datal, | ||
int count, unsigned char *key,unsigned char *iv); | ||
|
||
unsigned long ERR_get_error(void); | ||
void ERR_error_string_n(unsigned long e, char *buf, size_t len); | ||
]] | ||
|
||
local hash | ||
|
@@ -104,6 +107,18 @@ cipher = function (size, _cipher) | |
end | ||
_M.cipher = cipher | ||
|
||
local function get_error() | ||
local errno = C.ERR_get_error() | ||
if errno == 0 then | ||
return nil | ||
end | ||
|
||
local msg = ffi_new("char[?]", 256) | ||
C.ERR_error_string_n(errno, msg, 256) | ||
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. One OpenSSL call may cause several error messages. Therefore, to handle the error message properly, we need to draw the whole error messages out like: 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 suspected it but couldn't find confirmation in the OpenSSL documentation. Does the documentation say it clearly or you know it from source code (or another source)? 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. attention with
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. @toruneko 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. @un-def 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. BTW, sometimes OpenSSL don't even return an error message when the call is failed. (Perhaps it is a bug): openssl/openssl#11962 So strictly speaking, we need to provide a default one if no error is found: 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. @spacewander I've seen it, but this is not the official OpenSSL wiki and has not been updated for a long time. I meant the official docs hosted at https://www.openssl.org/ or official wiki https://wiki.openssl.org/ Anyway, I agree that it's worth popping all entries from the error stack, it's safe and future-compatible. |
||
|
||
return ffi_str(msg) | ||
end | ||
|
||
function _M.new(self, key, salt, _cipher, _hash, hash_rounds) | ||
local encrypt_ctx = C.EVP_CIPHER_CTX_new() | ||
if encrypt_ctx == nil then | ||
|
@@ -158,15 +173,15 @@ function _M.new(self, key, salt, _cipher, _hash, hash_rounds) | |
hash_rounds, gen_key, gen_iv) | ||
~= _cipherLength | ||
then | ||
return nil | ||
return nil, get_error() | ||
end | ||
end | ||
|
||
if C.EVP_EncryptInit_ex(encrypt_ctx, _cipher.method, nil, | ||
gen_key, gen_iv) == 0 or | ||
C.EVP_DecryptInit_ex(decrypt_ctx, _cipher.method, nil, | ||
gen_key, gen_iv) == 0 then | ||
return nil | ||
return nil, get_error() | ||
end | ||
|
||
return setmetatable({ | ||
|
@@ -185,15 +200,15 @@ function _M.encrypt(self, s) | |
local ctx = self._encrypt_ctx | ||
|
||
if C.EVP_EncryptInit_ex(ctx, nil, nil, nil, nil) == 0 then | ||
return nil | ||
return nil, get_error() | ||
end | ||
|
||
if C.EVP_EncryptUpdate(ctx, buf, out_len, s, s_len) == 0 then | ||
return nil | ||
return nil, get_error() | ||
end | ||
|
||
if C.EVP_EncryptFinal_ex(ctx, buf + out_len[0], tmp_len) == 0 then | ||
return nil | ||
return nil, get_error() | ||
end | ||
|
||
return ffi_str(buf, out_len[0] + tmp_len[0]) | ||
|
@@ -208,15 +223,15 @@ function _M.decrypt(self, s) | |
local ctx = self._decrypt_ctx | ||
|
||
if C.EVP_DecryptInit_ex(ctx, nil, nil, nil, nil) == 0 then | ||
return nil | ||
return nil, get_error() | ||
end | ||
|
||
if C.EVP_DecryptUpdate(ctx, buf, out_len, s, s_len) == 0 then | ||
return nil | ||
return nil, get_error() | ||
end | ||
|
||
if C.EVP_DecryptFinal_ex(ctx, buf + out_len[0], tmp_len) == 0 then | ||
return nil | ||
return nil, get_error() | ||
end | ||
|
||
return ffi_str(buf, out_len[0] + tmp_len[0]) | ||
|
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.
hope don’t usage magic number