-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use OpenSSL ERR_get_error() to pop an error
This addition fixes the following alert in nginx logs: [alert] ignoring stale global SSL error See: openresty/lua-resty-string#65
- Loading branch information
Showing
4 changed files
with
64 additions
and
11 deletions.
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 |
---|---|---|
@@ -1,9 +1,56 @@ | ||
local aes = require('resty.aes') | ||
local ffi = require('ffi') | ||
|
||
local aes = require('resty.aes') | ||
local aes_params = require('app.config').aes | ||
|
||
local string_format = string.format | ||
|
||
local ffi_new = ffi.new | ||
local ffi_string = ffi.string | ||
local C = ffi.C | ||
|
||
|
||
ffi.cdef[[ | ||
unsigned long ERR_get_error(void); | ||
void ERR_error_string_n(unsigned long e, char *buf, size_t len); | ||
]] | ||
|
||
|
||
local _M = {} | ||
|
||
-- https://github.com/openresty/lua-resty-string/pull/65 | ||
local get_error = function(op) | ||
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) | ||
return string_format('AES %s error: %s', op, ffi_string(msg)) | ||
end | ||
|
||
local aes_obj = aes:new( | ||
aes_params.key, | ||
aes_params.salt, | ||
aes.cipher(aes_params.size, aes_params.mode), | ||
aes_params.hash and aes.hash[aes_params.hash], | ||
aes_params.hash_rounds | ||
) | ||
|
||
_M.encrypt = function(data) | ||
data = aes_obj:encrypt(data) | ||
if not data then | ||
return nil, get_error('encrypt') | ||
end | ||
return data | ||
end | ||
|
||
_M.decrypt = function(data) | ||
data = aes_obj:decrypt(data) | ||
if not data then | ||
return nil, get_error('decrypt') | ||
end | ||
return data | ||
end | ||
|
||
local hash = aes_params.hash and aes.hash[aes_params.hash] | ||
local cipher = aes.cipher(aes_params.size, aes_params.mode) | ||
return aes:new(aes_params.key, aes_params.salt, cipher, | ||
hash, aes_params.hash_rounds) | ||
return _M |
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
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