summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Ahern <william@server.local>2012-09-29 22:44:00 -0700
committerWilliam Ahern <william@server.local>2012-09-29 22:44:00 -0700
commit468c9871b58856dda00149a9dc45ecfb7283d67f (patch)
tree20bace130aeedfa232c9eb648873b05cddab1796
downloadluaossl-468c9871b58856dda00149a9dc45ecfb7283d67f.tar.gz
luaossl-468c9871b58856dda00149a9dc45ecfb7283d67f.tar.bz2
luaossl-468c9871b58856dda00149a9dc45ecfb7283d67f.zip
-n
start OpenSSL extension
-rw-r--r--Makefile45
-rw-r--r--openssl.c182
2 files changed, 227 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..a7f829b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,45 @@
1prefix = /usr/local/lua52
2libdir = $(prefix)/lib
3datadir = $(prefix)/share
4includedir = $(prefix)/include
5lua52include = $(includedir)/lua/5.2
6lua52path = $(datadir)/lua/5.2
7lua52cpath = $(libdir)/lua/5.2
8
9LUAC = $(prefix)/bin/luac
10
11VENDOR.OS = $(shell ../mk/vendor.os)
12VENDOR.CC = $(shell env CC="${CC}" ../mk/vendor.cc)
13
14
15CPPFLAGS = -I$(DESTDIR)$(lua52include)
16DFLAGS = -Wall -Wextra -Wno-deprecated-declarations -Wno-unused
17CFLAGS = -fPIC $(DFLAGS)
18LDFLAGS = -lssl -lcrypto
19
20ifeq ($(VENDOR.OS), Darwin)
21SOFLAGS = -bundle -undefined dynamic_lookup
22else
23SOFLAGS = -shared
24endif
25
26
27
28
29
30all: openssl.so
31
32openssl.so: openssl.o
33 $(CC) -o $@ $^ $(SOFLAGS) $(LDFLAGS)
34
35openssl.o: openssl.c
36 $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<
37
38
39.PHONY: clean clean~
40
41clean:
42 rm -f *.so *.o
43
44clean~: clean
45 rm -f *~
diff --git a/openssl.c b/openssl.c
new file mode 100644
index 0000000..3cbedec
--- /dev/null
+++ b/openssl.c
@@ -0,0 +1,182 @@
1/* ==========================================================================
2 * openssl.c - Lua OpenSSL
3 * --------------------------------------------------------------------------
4 * Copyright (c) 2012 William Ahern
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to permit
11 * persons to whom the Software is furnished to do so, subject to the
12 * following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
20 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * ==========================================================================
25 */
26#ifndef L_OPENSSL_H
27#define L_OPENSSH_H
28
29#include <string.h> /* memset(3) */
30
31#include <openssl/err.h>
32#include <openssl/x509.h>
33#include <openssl/x509v3.h>
34
35#include <lua.h>
36#include <lualib.h>
37#include <lauxlib.h>
38
39
40#define X509_NAME_CLASS "X.509 Name"
41#define X509_CERT_CLASS "X.509 Cert"
42
43
44static void *prepudata(lua_State *L, const char *tname, size_t size) {
45 void *p = memset(lua_newuserdata(L, size), 0, size);
46 luaL_setmetatable(L, tname);
47 return p;
48} /* prepudata() */
49
50
51static void *prepsimple(lua_State *L, const char *tname) {
52 void **p = prepudata(L, tname, sizeof (void *));
53 return *p;
54} /* presimple() */
55
56
57static void *checksimple(lua_State *L, int index, const char *tname) {
58 void **p = luaL_checkudata(L, index, tname);
59 return *p;
60} /* checksimple() */
61
62
63static int throwssl(lua_State *L, const char *func) {
64 /* FIXME */
65 return luaL_error(L, "%s: SSL error (%lu)", func, ERR_get_error());
66} /* throwssl() */
67
68
69static int interpose(lua_State *L, const char *mt) {
70 luaL_getmetatable(L, mt);
71 lua_getfield(L, -1, "__index");
72
73 lua_pushvalue(L, -4); /* push method name */
74 lua_gettable(L, -2); /* push old method */
75
76 lua_pushvalue(L, -5); /* push method name */
77 lua_pushvalue(L, -5); /* push new method */
78 lua_settable(L, -4); /* replace old method */
79
80 return 1; /* return old method */
81} /* interpose() */
82
83
84static void addclass(lua_State *L, const char *name, const luaL_Reg *methods, const luaL_Reg *metamethods) {
85 if (luaL_newmetatable(L, name)) {
86 luaL_setfuncs(L, metamethods, 0);
87 lua_newtable(L);
88 luaL_setfuncs(L, methods, 0);
89 lua_setfield(L, -2, "__index");
90 lua_pop(L, 1);
91 }
92} /* addclass() */
93
94
95/*
96 * X509_NAME - openssl.x509.name
97 *
98 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
99
100static X509_NAME *xn_dup(lua_State *L, X509_NAME *name) {
101 X509_NAME **ud = prepsimple(L, X509_NAME_CLASS);
102
103 if (!(*ud = X509_NAME_dup(name)))
104 throwssl(L, "x509.name.dup");
105
106 return *ud;
107} /* xn_dup() */
108
109
110static int xn_new(lua_State *L) {
111 X509_NAME **ud = prepsimple(L, X509_NAME_CLASS);
112
113 if (!(*ud = X509_NAME_new()))
114 return throwssl(L, "x509.name.new");
115
116 return 1;
117} /* xn_new() */
118
119
120static int xn_interpose(lua_State *L) {
121 return interpose(L, X509_NAME_CLASS);
122} /* xn_interpose() */
123
124
125static int xn_add(lua_State *L) {
126 X509_NAME *name = checksimple(L, 1, X509_NAME_CLASS);
127 int nid;
128 const char *txt;
129 size_t len;
130
131 if (NID_undef == (nid = OBJ_txt2nid(luaL_checkstring(L, 2))))
132 return luaL_error(L, "x509.name:add: %s: invalid NID", luaL_checkstring(L, 2));
133
134 txt = luaL_checklstring(L, 3, &len);
135
136 if (!(X509_NAME_add_entry_by_NID(name, nid, MBSTRING_ASC, (unsigned char *)txt, len, -1, 0)))
137 return throwssl(L, "x509.name:add");
138
139 lua_pushboolean(L, 1);
140
141 return 1;
142} /* xn_add() */
143
144
145static int xn__gc(lua_State *L) {
146 X509_NAME **ud = luaL_checkudata(L, 1, X509_NAME_CLASS);
147
148 X509_NAME_free(*ud);
149 *ud = NULL;
150
151 return 0;
152} /* xn__gc() */
153
154
155static const luaL_Reg xn_methods[] = {
156 { "add", &xn_add },
157 { NULL, NULL },
158};
159
160static const luaL_Reg xn_metatable[] = {
161 { "__gc", &xn__gc },
162 { NULL, NULL },
163};
164
165
166static const luaL_Reg xn_globals[] = {
167 { "new", &xn_new },
168 { "interpose", &xn_interpose },
169 { NULL, NULL },
170};
171
172int luaopen__openssl_x509_name_open(lua_State *L) {
173 addclass(L, X509_NAME_CLASS, xn_methods, xn_metatable);
174
175 luaL_newlib(L, xn_globals);
176
177 return 1;
178} /* luaopen__openssl_x509_name_open() */
179
180
181
182#endif /* L_OPENSSL_H */