Support SSL_CTX_use_certificate_chain_file and SSL_CTX_use_private_key_file#194
Support SSL_CTX_use_certificate_chain_file and SSL_CTX_use_private_key_file#194Mehgugs wants to merge 3 commits intowahern:masterfrom
Conversation
- `ctx:setCertificteFromFile` calls `SSL_CTX_use_certificate_chain_file` to add a certificate chain from a pem encoded file specified by the string argument path. - `ctx:setPrivateKeyFromFile` calls `SSL_CTX_use_private_key_file` to add a private key from a PEM or ASN1 encoded file using the string argument path and filetype integer flag argument. The filetype is optional and will default to PEM if not specified. - `openssl.filetypes` is a new table in the openssl module which contains the two filetypes used by `setPrivateKeyFromFile`. The `.PEM` field is the value of `SSL_FILETYPE_PEM` and the `.ASN1` field is the value of `SSL_FILETYPE_ASN1`.
daurnimator
left a comment
There was a problem hiding this comment.
Have you tested this against old OpenSSL?
Do operations exist to do the same thing on an SSL object?
Please add new functions to docs
src/openssl.c
Outdated
| {"setCertificateChainFromFile", &sx_useCertificateChainFile}, | ||
| #endif | ||
| { "setPrivateKey", &sx_setPrivateKey }, | ||
| { "setPrivateKeyFromFile", &sx_usePrivateKeyFile}, |
There was a problem hiding this comment.
please realign the section
There was a problem hiding this comment.
Attempted to realign the reg declarations in keeping with the current style.
- Adds `ssl:setCertificateChainFromFile` and `ssl:setPrivateKeyFromFile` These both behave the same way as their context counterparts. - Attempt to improve formatting: - Added double newlines between the new code sections. - Tried to space out the reg declarations following the style of the code. - Added function end comments. - Renamed the c functions to match their lua registry name.
|
I'll do another commit adding tex when I'm finished with the code.
Will this need to be reflected with a version pre-req somehow? |
daurnimator
left a comment
There was a problem hiding this comment.
Could you add the new methods to the docs?
| #ifndef HAVE_USE_CERTIFICATE_CHAIN_FILE | ||
| #define HAVE_USE_CERTIFICATE_CHAIN_FILE (OPENSSL_PREREQ(0,9,4) || LIBRESSL_PREREQ(2,0,0)) | ||
| #endif | ||
|
|
There was a problem hiding this comment.
This is old enough we can likely count on it
src/openssl.c
Outdated
| static int sx_setPrivateKeyFromFile(lua_State* L) { | ||
| SSL_CTX *ctx = checksimple(L, 1, SSL_CTX_CLASS); | ||
| const char* filepath = luaL_checkstring(L, 2); | ||
| int typ = luaL_optinteger(L, 3, SSL_FILETYPE_PEM); |
There was a problem hiding this comment.
This should probably take a string rather than an integer option? (luaL_checkoption)
|
Hello, in the meantime how can one load a certificate and a key from files on the current version available from Luarocks? |
local Pkey = require "openssl.pkey"
local Crt = require "openssl.x509"
local Chain = require"openssl.x509.chain"
local function decode_fullchain(crtfile, iscontent)
local crtf = assert(io.open(crtfile, "r"))
local crttxt = crtf:read"a"
crtf:close()
local crts, pos = {}, 1
repeat
local st, ed = crttxt:find("-----BEGIN CERTIFICATE-----", pos, true)
if st then
local st2, ed2 = crttxt:find("-----END CERTIFICATE-----", ed + 1, true)
if st2 then
table.insert(crts, crttxt:sub(st, ed2))
pos = ed2+1
end
end
until st == nil
local chain = Chain.new()
local primary = asserts(Crt.new(crts[1]))
for i = 2, #crts do
local crt = asserts(Crt.new(crts[i]))
chain:add(crt)
end
return primary,chain
end
function example_usage(ctx, crtpath, keypath)
local keyfile = asserts(openf(keypath, "r"))
local primary,crt = decode_fullchain(crtpath)
asserts(ctx:setPrivateKey(Pkey.new(keyfile:read"a")))
asserts(ctx:setCertificate(primary))
asserts(ctx:setCertificateChain(crt))
keyfile:close()
endThis is my "good enough" solution but it doesnt really address all the situtations covered by these two functions. Apologies for not continuing to develop this PR further; I am a consumer of this library by way of lua-http and cqueues and it was easier for me to set up a reverse proxy to handle all the https and have the lua processes all run behind that. |
…ods, optional filetype is now a string (luaL_checkoption) (#1) - Update doc/luaossl.tex : document 'context:setCertificateChainFromFile', 'context:setPrivateKeyFromFile', 'ssl:setCertificateChainFromFile' and 'ssl:setPrivateKeyFromFile' methods
ctx:setCertificateFromFilecallsSSL_CTX_use_certificate_chain_fileto add a certificate chain from a pem encoded file specified by the string argument path.
ctx:setPrivateKeyFromFilecallsSSL_CTX_use_private_key_fileto add a private key from a PEM or ASN1 encoded file using the string argument path
and filetype integer flag argument. The filetype is optional and will default to PEM if not
specified.
openssl.filetypesis a new table in the openssl module which contains thetwo filetypes used by
setPrivateKeyFromFile. The.PEMfield is the value ofSSL_FILETYPE_PEMandthe
.ASN1field is the value ofSSL_FILETYPE_ASN1.