summaryrefslogtreecommitdiffhomepage
path: root/libs/px5g/lua/px5g/util.lua
diff options
context:
space:
mode:
Diffstat (limited to 'libs/px5g/lua/px5g/util.lua')
-rw-r--r--libs/px5g/lua/px5g/util.lua39
1 files changed, 39 insertions, 0 deletions
diff --git a/libs/px5g/lua/px5g/util.lua b/libs/px5g/lua/px5g/util.lua
new file mode 100644
index 000000000..e94fb6507
--- /dev/null
+++ b/libs/px5g/lua/px5g/util.lua
@@ -0,0 +1,39 @@
+--[[
+ * px5g - Embedded x509 key and certificate generator based on PolarSSL
+ *
+ * Copyright (C) 2009 Steven Barth <steven@midlink.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License, version 2.1 as published by the Free Software Foundation.
+]]--
+
+local nixio = require "nixio"
+local table = require "table"
+
+module "px5g.util"
+
+local preamble = {
+ key = "-----BEGIN RSA PRIVATE KEY-----",
+ cert = "-----BEGIN CERTIFICATE-----",
+ request = "-----BEGIN CERTIFICATE REQUEST-----"
+}
+
+local postamble = {
+ key = "-----END RSA PRIVATE KEY-----",
+ cert = "-----END CERTIFICATE-----",
+ request = "-----END CERTIFICATE REQUEST-----"
+}
+
+function der2pem(data, type)
+ local b64 = nixio.bin.b64encode(data)
+
+ local outdata = {preamble[type]}
+ for i = 1, 64, #b64 + 63 do
+ outdata[#outdata + 1] = b64:sub(i, i + 63)
+ end
+ outdata[#outdata + 1] = postamble[type]
+ outdata[#outdata + 1] = ""
+
+ return table.concat(outdata, "\n")
+end