diff options
| author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2014-05-31 23:54:06 +0300 |
|---|---|---|
| committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2014-06-01 00:05:22 +0300 |
| commit | 934e32e0416ae1da69aa1295837b93369c9f7aca (patch) | |
| tree | f9461f79f983c04d42ea70f3b440174c2d5426f9 | |
| parent | 1caa952f22f06ef58cd153e7bcffacef660e423d (diff) | |
| download | luaossl-934e32e0416ae1da69aa1295837b93369c9f7aca.tar.gz luaossl-934e32e0416ae1da69aa1295837b93369c9f7aca.tar.bz2 luaossl-934e32e0416ae1da69aa1295837b93369c9f7aca.zip | |
module for custom certificate extensions
| -rw-r--r-- | src/GNUmakefile | 1 | ||||
| -rw-r--r-- | src/openssl.c | 135 | ||||
| -rw-r--r-- | src/openssl.x509.extension.lua | 1 |
3 files changed, 101 insertions, 36 deletions
diff --git a/src/GNUmakefile b/src/GNUmakefile index 75e8c3a..c3cddf0 100644 --- a/src/GNUmakefile +++ b/src/GNUmakefile | |||
| @@ -95,6 +95,7 @@ MODS$(1)_$(d) = \ | |||
| 95 | $$(DESTDIR)$(3)/openssl/x509/altname.lua \ | 95 | $$(DESTDIR)$(3)/openssl/x509/altname.lua \ |
| 96 | $$(DESTDIR)$(3)/openssl/x509/chain.lua \ | 96 | $$(DESTDIR)$(3)/openssl/x509/chain.lua \ |
| 97 | $$(DESTDIR)$(3)/openssl/x509/crl.lua \ | 97 | $$(DESTDIR)$(3)/openssl/x509/crl.lua \ |
| 98 | $$(DESTDIR)$(3)/openssl/x509/extension.lua \ | ||
| 98 | $$(DESTDIR)$(3)/openssl/x509/store.lua \ | 99 | $$(DESTDIR)$(3)/openssl/x509/store.lua \ |
| 99 | $$(DESTDIR)$(3)/openssl/ssl/context.lua \ | 100 | $$(DESTDIR)$(3)/openssl/ssl/context.lua \ |
| 100 | $$(DESTDIR)$(3)/openssl/ssl.lua \ | 101 | $$(DESTDIR)$(3)/openssl/ssl.lua \ |
diff --git a/src/openssl.c b/src/openssl.c index a0af882..b72b28e 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
| @@ -82,6 +82,7 @@ | |||
| 82 | #define PKEY_CLASS "EVP_PKEY*" | 82 | #define PKEY_CLASS "EVP_PKEY*" |
| 83 | #define X509_NAME_CLASS "X509_NAME*" | 83 | #define X509_NAME_CLASS "X509_NAME*" |
| 84 | #define X509_GENS_CLASS "GENERAL_NAMES*" | 84 | #define X509_GENS_CLASS "GENERAL_NAMES*" |
| 85 | #define X509_EXT_CLASS "X509_EXTENSION*" | ||
| 85 | #define X509_CERT_CLASS "X509*" | 86 | #define X509_CERT_CLASS "X509*" |
| 86 | #define X509_CHAIN_CLASS "STACK_OF(X509)*" | 87 | #define X509_CHAIN_CLASS "STACK_OF(X509)*" |
| 87 | #define X509_CSR_CLASS "X509_REQ*" | 88 | #define X509_CSR_CLASS "X509_REQ*" |
| @@ -1804,6 +1805,98 @@ int luaopen__openssl_x509_altname(lua_State *L) { | |||
| 1804 | 1805 | ||
| 1805 | 1806 | ||
| 1806 | /* | 1807 | /* |
| 1808 | * X509_EXTENSION - openssl.x509.extension | ||
| 1809 | * | ||
| 1810 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
| 1811 | |||
| 1812 | static int xe_new(lua_State *L) { | ||
| 1813 | lua_settop(L, 3); | ||
| 1814 | |||
| 1815 | X509_EXTENSION **ud = prepsimple(L, X509_EXT_CLASS); | ||
| 1816 | |||
| 1817 | char *name = (char *) luaL_checkstring(L, 1); | ||
| 1818 | char *value = (char *) luaL_checkstring(L, 2); | ||
| 1819 | |||
| 1820 | CONF *conf = NULL; | ||
| 1821 | X509V3_CTX *ctx = NULL; | ||
| 1822 | X509_EXTENSION *ext = NULL; | ||
| 1823 | |||
| 1824 | if (!lua_isnil(L, 3)) { | ||
| 1825 | char *cdata = (char *) luaL_checkstring(L, 3); | ||
| 1826 | BIO *bio = getbio(L); | ||
| 1827 | if (BIO_puts(bio, cdata) < 0) | ||
| 1828 | goto error; | ||
| 1829 | |||
| 1830 | if (!(conf = NCONF_new(NULL))) | ||
| 1831 | goto error; | ||
| 1832 | if (!NCONF_load_bio(conf, bio, NULL)) | ||
| 1833 | goto error; | ||
| 1834 | |||
| 1835 | ctx = (X509V3_CTX *) malloc(sizeof (X509V3_CTX)); | ||
| 1836 | X509V3_set_nconf(ctx, conf); | ||
| 1837 | } | ||
| 1838 | |||
| 1839 | if (!(*ud = X509V3_EXT_nconf(conf, ctx, name, value))) | ||
| 1840 | goto error; | ||
| 1841 | |||
| 1842 | if (conf) { | ||
| 1843 | free(ctx); | ||
| 1844 | NCONF_free(conf); | ||
| 1845 | } | ||
| 1846 | |||
| 1847 | return 1; | ||
| 1848 | |||
| 1849 | error: | ||
| 1850 | if (ctx) | ||
| 1851 | free(ctx); | ||
| 1852 | if (conf) | ||
| 1853 | NCONF_free(conf); | ||
| 1854 | |||
| 1855 | return throwssl(L, "x509.extension.new"); | ||
| 1856 | } /* xe_new() */ | ||
| 1857 | |||
| 1858 | |||
| 1859 | static int xe_interpose(lua_State *L) { | ||
| 1860 | return interpose(L, X509_EXT_CLASS); | ||
| 1861 | } /* xe_interpose() */ | ||
| 1862 | |||
| 1863 | |||
| 1864 | static int xe__gc(lua_State *L) { | ||
| 1865 | X509_EXTENSION **ud = luaL_checkudata(L, 1, X509_EXT_CLASS); | ||
| 1866 | |||
| 1867 | X509_EXTENSION_free(*ud); | ||
| 1868 | *ud = NULL; | ||
| 1869 | |||
| 1870 | return 0; | ||
| 1871 | } /* xe__gc() */ | ||
| 1872 | |||
| 1873 | |||
| 1874 | static const luaL_Reg xe_methods[] = { | ||
| 1875 | { NULL, NULL }, | ||
| 1876 | }; | ||
| 1877 | |||
| 1878 | static const luaL_Reg xe_metatable[] = { | ||
| 1879 | { "__gc", &xe__gc }, | ||
| 1880 | { NULL, NULL }, | ||
| 1881 | }; | ||
| 1882 | |||
| 1883 | |||
| 1884 | static const luaL_Reg xe_globals[] = { | ||
| 1885 | { "new", &xe_new }, | ||
| 1886 | { "interpose", &xe_interpose }, | ||
| 1887 | { NULL, NULL }, | ||
| 1888 | }; | ||
| 1889 | |||
| 1890 | int luaopen__openssl_x509_extension(lua_State *L) { | ||
| 1891 | initall(L); | ||
| 1892 | |||
| 1893 | luaL_newlib(L, xe_globals); | ||
| 1894 | |||
| 1895 | return 1; | ||
| 1896 | } /* luaopen__openssl_x509_extension() */ | ||
| 1897 | |||
| 1898 | |||
| 1899 | /* | ||
| 1807 | * X509 - openssl.x509.cert | 1900 | * X509 - openssl.x509.cert |
| 1808 | * | 1901 | * |
| 1809 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | 1902 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| @@ -2498,45 +2591,14 @@ static int xc_setBasicConstraintsCritical(lua_State *L) { | |||
| 2498 | 2591 | ||
| 2499 | static int xc_addExtension(lua_State *L) { | 2592 | static int xc_addExtension(lua_State *L) { |
| 2500 | X509 *crt = checksimple(L, 1, X509_CERT_CLASS); | 2593 | X509 *crt = checksimple(L, 1, X509_CERT_CLASS); |
| 2501 | char *name = (char *) luaL_checkstring(L, 2); | 2594 | X509_EXTENSION *ext = checksimple(L, 2, X509_EXT_CLASS); |
| 2502 | char *value = (char *) luaL_checkstring(L, 3); | ||
| 2503 | |||
| 2504 | int ok = 1; | ||
| 2505 | |||
| 2506 | BIO *bio = NULL; | ||
| 2507 | CONF *conf = NULL; | ||
| 2508 | X509V3_CTX *ctx = NULL; | ||
| 2509 | X509_EXTENSION *ext = NULL; | ||
| 2510 | |||
| 2511 | if (lua_gettop(L) > 3) { | ||
| 2512 | char *cdata = (char *) luaL_checkstring(L, 4); | ||
| 2513 | |||
| 2514 | bio = BIO_new(BIO_s_mem()); | ||
| 2515 | if (!bio) goto error; | ||
| 2516 | if (BIO_puts(bio, cdata) < 0) goto error; | ||
| 2517 | |||
| 2518 | conf = NCONF_new(NULL); | ||
| 2519 | if (!conf) goto error; | ||
| 2520 | if (!NCONF_load_bio(conf, bio, NULL)) goto error; | ||
| 2521 | 2595 | ||
| 2522 | ctx = (X509V3_CTX *) malloc(sizeof (X509V3_CTX)); | 2596 | if (!X509_add_ext(crt, ext, -1)) |
| 2523 | X509V3_set_nconf(ctx, conf); | 2597 | throwssl(L, "x509.cert:addExtension"); |
| 2524 | } | ||
| 2525 | |||
| 2526 | ext = X509V3_EXT_nconf(conf, ctx, name, value); | ||
| 2527 | |||
| 2528 | if (ext && X509_add_ext(crt, ext, -1)) goto done; | ||
| 2529 | 2598 | ||
| 2530 | error: | 2599 | lua_pushboolean(L, 1); |
| 2531 | ok = 0; | ||
| 2532 | |||
| 2533 | done: | ||
| 2534 | if (ext) X509_EXTENSION_free(ext); | ||
| 2535 | if (ctx) free(ctx); | ||
| 2536 | if (conf) NCONF_free(conf); | ||
| 2537 | if (bio) BIO_free(bio); | ||
| 2538 | 2600 | ||
| 2539 | return ok ? 0 : throwssl(L, "x509.cert:addExtension"); | 2601 | return 1; |
| 2540 | } /* xc_addExtension() */ | 2602 | } /* xc_addExtension() */ |
| 2541 | 2603 | ||
| 2542 | 2604 | ||
| @@ -4799,6 +4861,7 @@ static void initall(lua_State *L) { | |||
| 4799 | addclass(L, PKEY_CLASS, pk_methods, pk_metatable); | 4861 | addclass(L, PKEY_CLASS, pk_methods, pk_metatable); |
| 4800 | addclass(L, X509_NAME_CLASS, xn_methods, xn_metatable); | 4862 | addclass(L, X509_NAME_CLASS, xn_methods, xn_metatable); |
| 4801 | addclass(L, X509_GENS_CLASS, gn_methods, gn_metatable); | 4863 | addclass(L, X509_GENS_CLASS, gn_methods, gn_metatable); |
| 4864 | addclass(L, X509_EXT_CLASS, xe_methods, xe_metatable); | ||
| 4802 | addclass(L, X509_CERT_CLASS, xc_methods, xc_metatable); | 4865 | addclass(L, X509_CERT_CLASS, xc_methods, xc_metatable); |
| 4803 | addclass(L, X509_CSR_CLASS, xr_methods, xr_metatable); | 4866 | addclass(L, X509_CSR_CLASS, xr_methods, xr_metatable); |
| 4804 | addclass(L, X509_CRL_CLASS, xx_methods, xx_metatable); | 4867 | addclass(L, X509_CRL_CLASS, xx_methods, xx_metatable); |
diff --git a/src/openssl.x509.extension.lua b/src/openssl.x509.extension.lua new file mode 100644 index 0000000..7043f45 --- /dev/null +++ b/src/openssl.x509.extension.lua | |||
| @@ -0,0 +1 @@ | |||
| return require('_openssl.x509.extension') | |||
