diff options
| author | William Ahern <william@server.local> | 2012-09-29 22:44:00 -0700 |
|---|---|---|
| committer | William Ahern <william@server.local> | 2012-09-29 22:44:00 -0700 |
| commit | 468c9871b58856dda00149a9dc45ecfb7283d67f (patch) | |
| tree | 20bace130aeedfa232c9eb648873b05cddab1796 | |
| download | luaossl-468c9871b58856dda00149a9dc45ecfb7283d67f.tar.gz luaossl-468c9871b58856dda00149a9dc45ecfb7283d67f.tar.bz2 luaossl-468c9871b58856dda00149a9dc45ecfb7283d67f.zip | |
-n
start OpenSSL extension
| -rw-r--r-- | Makefile | 45 | ||||
| -rw-r--r-- | openssl.c | 182 |
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 @@ | |||
| 1 | prefix = /usr/local/lua52 | ||
| 2 | libdir = $(prefix)/lib | ||
| 3 | datadir = $(prefix)/share | ||
| 4 | includedir = $(prefix)/include | ||
| 5 | lua52include = $(includedir)/lua/5.2 | ||
| 6 | lua52path = $(datadir)/lua/5.2 | ||
| 7 | lua52cpath = $(libdir)/lua/5.2 | ||
| 8 | |||
| 9 | LUAC = $(prefix)/bin/luac | ||
| 10 | |||
| 11 | VENDOR.OS = $(shell ../mk/vendor.os) | ||
| 12 | VENDOR.CC = $(shell env CC="${CC}" ../mk/vendor.cc) | ||
| 13 | |||
| 14 | |||
| 15 | CPPFLAGS = -I$(DESTDIR)$(lua52include) | ||
| 16 | DFLAGS = -Wall -Wextra -Wno-deprecated-declarations -Wno-unused | ||
| 17 | CFLAGS = -fPIC $(DFLAGS) | ||
| 18 | LDFLAGS = -lssl -lcrypto | ||
| 19 | |||
| 20 | ifeq ($(VENDOR.OS), Darwin) | ||
| 21 | SOFLAGS = -bundle -undefined dynamic_lookup | ||
| 22 | else | ||
| 23 | SOFLAGS = -shared | ||
| 24 | endif | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | |||
| 30 | all: openssl.so | ||
| 31 | |||
| 32 | openssl.so: openssl.o | ||
| 33 | $(CC) -o $@ $^ $(SOFLAGS) $(LDFLAGS) | ||
| 34 | |||
| 35 | openssl.o: openssl.c | ||
| 36 | $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $< | ||
| 37 | |||
| 38 | |||
| 39 | .PHONY: clean clean~ | ||
| 40 | |||
| 41 | clean: | ||
| 42 | rm -f *.so *.o | ||
| 43 | |||
| 44 | clean~: 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 | |||
| 44 | static 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 | |||
| 51 | static void *prepsimple(lua_State *L, const char *tname) { | ||
| 52 | void **p = prepudata(L, tname, sizeof (void *)); | ||
| 53 | return *p; | ||
| 54 | } /* presimple() */ | ||
| 55 | |||
| 56 | |||
| 57 | static 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 | |||
| 63 | static 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 | |||
| 69 | static 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 | |||
| 84 | static 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 | |||
| 100 | static 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 | |||
| 110 | static 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 | |||
| 120 | static int xn_interpose(lua_State *L) { | ||
| 121 | return interpose(L, X509_NAME_CLASS); | ||
| 122 | } /* xn_interpose() */ | ||
| 123 | |||
| 124 | |||
| 125 | static 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 | |||
| 145 | static 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 | |||
| 155 | static const luaL_Reg xn_methods[] = { | ||
| 156 | { "add", &xn_add }, | ||
| 157 | { NULL, NULL }, | ||
| 158 | }; | ||
| 159 | |||
| 160 | static const luaL_Reg xn_metatable[] = { | ||
| 161 | { "__gc", &xn__gc }, | ||
| 162 | { NULL, NULL }, | ||
| 163 | }; | ||
| 164 | |||
| 165 | |||
| 166 | static const luaL_Reg xn_globals[] = { | ||
| 167 | { "new", &xn_new }, | ||
| 168 | { "interpose", &xn_interpose }, | ||
| 169 | { NULL, NULL }, | ||
| 170 | }; | ||
| 171 | |||
| 172 | int 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 */ | ||
