summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwilliam <william@25tandclement.com>2013-12-09 21:26:39 -0800
committerwilliam <william@25tandclement.com>2013-12-09 21:26:39 -0800
commite3ec2e4f949267ca48fe9fe983dd00f41010c2a8 (patch)
tree744fc7d57d94e044699c0f7ece7cb47cb56cfaff
parent9db41e05d9a00eb906b530b38bcaaa068d40c88b (diff)
downloadluaossl-e3ec2e4f949267ca48fe9fe983dd00f41010c2a8.tar.gz
luaossl-e3ec2e4f949267ca48fe9fe983dd00f41010c2a8.tar.bz2
luaossl-e3ec2e4f949267ca48fe9fe983dd00f41010c2a8.zip
copy over our examples/
-rwxr-xr-xexamples/self.x50954
-rwxr-xr-xexamples/vrfy.sig35
2 files changed, 89 insertions, 0 deletions
diff --git a/examples/self.x509 b/examples/self.x509
new file mode 100755
index 0000000..39525d0
--- /dev/null
+++ b/examples/self.x509
@@ -0,0 +1,54 @@
1#!/usr/local/lua52/bin/lua
2--
3-- Example self-signed X.509 certificate generation.
4--
5-- Skips intermediate CSR object, which is just an antiquated way for
6-- specifying subject DN and public key to CAs. See API documentation for
7-- CSR generation.
8--
9
10local pubkey = require"openssl.pubkey"
11local x509 = require"openssl.x509"
12local name = require"openssl.x509.name"
13local altname = require"openssl.x509.altname"
14
15-- generate our public/private key pair
16--local key = pubkey.new{ type = "RSA", bits = 1024 }
17--local key = pubkey.new{ type = "DSA", bits = 1024 }
18local key = pubkey.new{ type = "EC", curve = "prime192v1" }
19
20-- our Subject and Issuer DN (self-signed, so same)
21local dn = name.new()
22dn:add("C", "US")
23dn:add("ST", "California")
24dn:add("L", "San Francisco")
25dn:add("O", "Acme, Inc")
26dn:add("CN", "acme.inc")
27
28-- our Alternative Names
29local alt = altname.new()
30alt:add("DNS", "acme.inc")
31alt:add("DNS", "*.acme.inc")
32
33-- build our certificate
34local crt = x509.new()
35
36crt:setVersion(3)
37crt:setSerial(47)
38
39crt:setSubject(dn)
40crt:setIssuer(crt:getSubject())
41crt:setSubjectAlt(alt)
42
43local issued, expires = crt:getLifetime()
44crt:setLifetime(issued, expires + 60) -- good for 60 seconds
45
46crt:setBasicConstraints{ CA = true, pathLen = 2 }
47crt:setBasicConstraintsCritical(true)
48
49crt:setPublicKey(key)
50crt:sign(key)
51
52-- pretty-print using openssl command-line utility.
53io.popen("openssl x509 -text -noout", "w"):write(tostring(crt))
54
diff --git a/examples/vrfy.sig b/examples/vrfy.sig
new file mode 100755
index 0000000..94daf43
--- /dev/null
+++ b/examples/vrfy.sig
@@ -0,0 +1,35 @@
1#!/usr/local/lua52/bin/lua
2--
3-- Example public-key signature verification.
4--
5
6local pubkey = require"openssl.pubkey"
7local digest = require"openssl.digest"
8
9-- generate a public/private key pair
10local key = pubkey.new{ type = "EC", curve = "prime192v1" }
11
12-- digest our message using an appropriate digest ("ecdsa-with-SHA1" for EC;
13-- "dss1" for DSA; and "sha1", "sha256", etc for RSA).
14local data = digest.new"ecdsa-with-SHA1"
15data:update(... or "hello world")
16
17-- generate a signature for our data
18local sig = key:sign(data)
19
20-- to prove verification works, instantiate a new object holding just
21-- the public key
22local pub = pubkey.new(key:toPEM"public")
23
24-- a utility routine to output our signature
25local function tohex(b)
26 local x = ""
27 for i = 1, #b do
28 x = x .. string.format("%.2x", string.byte(b, i))
29 end
30 return x
31end
32
33print("okay", pub:verify(sig, data))
34print("type", pub:type())
35print("sig", tohex(sig))