2 * px5g - Embedded x509 key and certificate generator based on PolarSSL
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License, version 2.1 as published by the Free Software Foundation.
11 local nixio = require "nixio"
12 local table = require "table"
17 key = "-----BEGIN RSA PRIVATE KEY-----",
18 cert = "-----BEGIN CERTIFICATE-----",
19 request = "-----BEGIN CERTIFICATE REQUEST-----"
23 key = "-----END RSA PRIVATE KEY-----",
24 cert = "-----END CERTIFICATE-----",
25 request = "-----END CERTIFICATE REQUEST-----"
28 function der2pem(data, type)
29 local b64 = nixio.bin.b64encode(data)
31 local outdata = {preamble[type]}
32 for i = 1, #b64, 64 do
33 outdata[#outdata + 1] = b64:sub(i, i + 63)
35 outdata[#outdata + 1] = postamble[type]
36 outdata[#outdata + 1] = ""
38 return table.concat(outdata, "\n")
41 function pem2der(data)
42 local b64 = data:gsub({["\n"] = "", ["%-%-%-%-%-.-%-%-%-%-%-"] = ""})
43 return nixio.bin.b64decode(b64)