Skip to content

Commit 400fbbc

Browse files
committed
add module for create certificate to SSL
1 parent 8a10644 commit 400fbbc

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

PS/Cert/init.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
local scriptDir = debug.getinfo(1, "S").source:match("@(.*/)") or "./"
2+
3+
if not _G.log then _G.log = require("loglua") end
4+
local openssl = require("openssl")
5+
6+
local Cert = {}
7+
8+
function Cert:CreateKey(keyCfg)
9+
keyCfg = keyCfg or { type="rsa", bits=4096 }
10+
11+
local CK = log.inSection("Create Key Certificate")
12+
13+
local pkey = openssl.pkey.new(keyCfg)
14+
15+
CK.debug("create pkey", pkey)
16+
17+
-- exporta para PEM
18+
local key_pem = pkey:export("pem")
19+
CK.debug("pkey exported to PEM", key_pem)
20+
21+
local f = assert(io.open(scriptDir.."server.key","w"))
22+
CK.debug("saving Server.key")
23+
f:write(key_pem)
24+
f:close()
25+
26+
return pkey
27+
end
28+
29+
function Cert:CreateCertificate(subjectCfg, keyCfg)
30+
local subjectCfg = subjectCfg or {
31+
CN = "pudim.local",
32+
C = "BR",
33+
ST = "RN",
34+
}
35+
36+
local pkey = self:CreateKey(keyCfg)
37+
local x509 = openssl.x509.new()
38+
39+
x509:set_version(2)
40+
x509:set_serial(os.time())
41+
42+
x509:set_subject_name(subjectCfg)
43+
x509:set_issuer_name(subjectCfg)
44+
x509:set_pubkey(pkey)
45+
46+
x509:set_not_before(os.time())
47+
x509:set_not_after(os.time() + 365*24*60*60)
48+
49+
x509:sign(pkey, "sha256")
50+
51+
local cert_pem = x509:export("pem")
52+
local f = assert(io.open(scriptDir.."server.crt","w"))
53+
f:write(cert_pem)
54+
f:close()
55+
end
56+
57+
58+
59+
return Cert

0 commit comments

Comments
 (0)