summaryrefslogtreecommitdiff
path: root/openssl.c
diff options
context:
space:
mode:
authorWilliam Ahern <william@server.local>2012-10-09 13:42:21 -0700
committerWilliam Ahern <william@server.local>2012-10-09 13:42:21 -0700
commit479b80ccc1c5357ca46ebf6afdb6227200186ca7 (patch)
treea218182417992b254a28d6000a2c92c84faf19bc /openssl.c
parent2248a54bd21498c95447584cd764193090dd8209 (diff)
downloadluaossl-479b80ccc1c5357ca46ebf6afdb6227200186ca7.tar.gz
luaossl-479b80ccc1c5357ca46ebf6afdb6227200186ca7.tar.bz2
luaossl-479b80ccc1c5357ca46ebf6afdb6227200186ca7.zip
-n
wrap X509_STORE and STACK_OF(X509) objects
Diffstat (limited to 'openssl.c')
-rw-r--r--openssl.c313
1 files changed, 306 insertions, 7 deletions
diff --git a/openssl.c b/openssl.c
index e003eac..2d0f6ed 100644
--- a/openssl.c
+++ b/openssl.c
@@ -48,12 +48,16 @@
48#include <lauxlib.h> 48#include <lauxlib.h>
49 49
50 50
51#define BIGNUM_CLASS "OpenSSL BN" 51#define BIGNUM_CLASS "OpenSSL Bignum"
52#define PUBKEY_CLASS "OpenSSL PK" 52#define PUBKEY_CLASS "OpenSSL Pubkey"
53#define X509_NAME_CLASS "OpenSSL X.509 Name" 53#define X509_NAME_CLASS "OpenSSL X.509 Name"
54#define X509_GENS_CLASS "OpenSSL X.509 AltName" 54#define X509_GENS_CLASS "OpenSSL X.509 AltName"
55#define X509_CERT_CLASS "OpenSSL X.509 Cert" 55#define X509_CERT_CLASS "OpenSSL X.509"
56#define X509_CSR_CLASS "OpenSSL X.509 Request" 56#define X509_CHAIN_CLASS "OpenSSL X.509 Chain"
57#define X509_CSR_CLASS "OpenSSL X.509 Request"
58#define X509_CHAIN_CLASS "OpenSSL X.509 Chain"
59#define X509_STORE_CLASS "OpenSSL X.509 Store"
60#define X509_STCTX_CLASS "OpenSSL X.509 Store Context"
57 61
58 62
59#define countof(a) (sizeof (a) / sizeof *(a)) 63#define countof(a) (sizeof (a) / sizeof *(a))
@@ -2202,6 +2206,43 @@ static int xc_setBasicConstraintsCritical(lua_State *L) {
2202} /* xc_setBasicConstraintsCritical() */ 2206} /* xc_setBasicConstraintsCritical() */
2203 2207
2204 2208
2209static int xc_isIssuedBy(lua_State *L) {
2210 X509 *crt = checksimple(L, 1, X509_CERT_CLASS);
2211 X509 *issuer = checksimple(L, 2, X509_CERT_CLASS);
2212 EVP_PKEY *key;
2213 int ok, why = 0;
2214
2215 ERR_clear_error();
2216
2217 if (X509_V_OK != (why = X509_check_issued(issuer, crt)))
2218 goto done;
2219
2220 if (!(key = X509_get_pubkey(issuer))) {
2221 why = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
2222 goto done;
2223 }
2224
2225 ok = (1 == X509_verify(crt, key));
2226
2227 EVP_PKEY_free(key);
2228
2229 if (!ok)
2230 why = X509_V_ERR_CERT_SIGNATURE_FAILURE;
2231
2232done:
2233 if (why != X509_V_OK) {
2234 lua_pushboolean(L, 0);
2235 lua_pushstring(L, X509_verify_cert_error_string(why));
2236
2237 return 2;
2238 } else {
2239 lua_pushboolean(L, 1);
2240
2241 return 1;
2242 }
2243} /* xc_isIssuedBy() */
2244
2245
2205static int xc_getPublicKey(lua_State *L) { 2246static int xc_getPublicKey(lua_State *L) {
2206 X509 *crt = checksimple(L, 1, X509_CERT_CLASS); 2247 X509 *crt = checksimple(L, 1, X509_CERT_CLASS);
2207 EVP_PKEY **key = prepsimple(L, PUBKEY_CLASS); 2248 EVP_PKEY **key = prepsimple(L, PUBKEY_CLASS);
@@ -2312,6 +2353,7 @@ static const luaL_Reg xc_methods[] = {
2312 { "setBasicConstraint", &xc_setBasicConstraint }, 2353 { "setBasicConstraint", &xc_setBasicConstraint },
2313 { "getBasicConstraintsCritical", &xc_getBasicConstraintsCritical }, 2354 { "getBasicConstraintsCritical", &xc_getBasicConstraintsCritical },
2314 { "setBasicConstraintsCritical", &xc_setBasicConstraintsCritical }, 2355 { "setBasicConstraintsCritical", &xc_setBasicConstraintsCritical },
2356 { "isIssuedBy", &xc_isIssuedBy },
2315 { "getPublicKey", &xc_getPublicKey }, 2357 { "getPublicKey", &xc_getPublicKey },
2316 { "setPublicKey", &xc_setPublicKey }, 2358 { "setPublicKey", &xc_setPublicKey },
2317 { "sign", &xc_sign }, 2359 { "sign", &xc_sign },
@@ -2530,6 +2572,261 @@ int luaopen__openssl_x509_csr(lua_State *L) {
2530} /* luaopen__openssl_x509_csr() */ 2572} /* luaopen__openssl_x509_csr() */
2531 2573
2532 2574
2575/*
2576 * STACK_OF(X509) - openssl.x509.chain
2577 *
2578 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2579
2580static int xl_new(lua_State *L) {
2581 STACK_OF(X509) **chain = prepsimple(L, X509_CHAIN_CLASS);
2582
2583 if (!(*chain = sk_X509_new_null()))
2584 return throwssl(L, "x509.chain.new");
2585
2586 return 1;
2587} /* xl_new() */
2588
2589
2590static int xl_interpose(lua_State *L) {
2591 return interpose(L, X509_CHAIN_CLASS);
2592} /* xl_interpose() */
2593
2594
2595static int xl_add(lua_State *L) {
2596 STACK_OF(X509) *chain = checksimple(L, 1, X509_CHAIN_CLASS);
2597 X509 *crt = checksimple(L, 2, X509_CERT_CLASS);
2598 X509 *dup;
2599
2600 if (!(dup = X509_dup(crt)))
2601 return throwssl(L, "x509.chain:add");
2602
2603 if (!sk_X509_push(chain, dup)) {
2604 X509_free(dup);
2605 return throwssl(L, "x509.chain:add");
2606 }
2607
2608 lua_pushboolean(L, 1);
2609
2610 return 1;
2611} /* xl_add() */
2612
2613
2614static int xl__next(lua_State *L) {
2615 STACK_OF(X509) *chain = checksimple(L, lua_upvalueindex(1), X509_CHAIN_CLASS);
2616 int i = lua_tointeger(L, lua_upvalueindex(2));
2617 int n = sk_X509_num(chain);
2618
2619 lua_settop(L, 0);
2620
2621 while (i < n) {
2622 X509 *crt, **ret;
2623
2624 if (!(crt = sk_X509_value(chain, i++)))
2625 continue;
2626
2627 ret = prepsimple(L, X509_CHAIN_CLASS);
2628
2629 if (!(*ret = X509_dup(crt)))
2630 return throwssl(L, "x509.chain:__next");
2631
2632 break;
2633 }
2634
2635 lua_pushinteger(L, i);
2636 lua_replace(L, lua_upvalueindex(2));
2637
2638 return lua_gettop(L);
2639} /* xl__next() */
2640
2641static int xl__pairs(lua_State *L) {
2642 lua_settop(L, 1);
2643 lua_pushinteger(L, 0);
2644 lua_pushcclosure(L, &xl__next, 2);
2645
2646 return 1;
2647} /* xl__pairs() */
2648
2649
2650static int xl__gc(lua_State *L) {
2651 STACK_OF(X509) **chain = luaL_checkudata(L, 1, X509_CHAIN_CLASS);
2652
2653 sk_X509_pop_free(*chain, X509_free);
2654 *chain = NULL;
2655
2656 return 0;
2657} /* xl__gc() */
2658
2659
2660static const luaL_Reg xl_methods[] = {
2661 { "add", &xl_add },
2662 { NULL, NULL },
2663};
2664
2665static const luaL_Reg xl_metatable[] = {
2666 { "__pairs", &xl__pairs },
2667 { "__gc", &xl__gc },
2668 { NULL, NULL },
2669};
2670
2671static const luaL_Reg xl_globals[] = {
2672 { "new", &xl_new },
2673 { "interpose", &xl_interpose },
2674 { NULL, NULL },
2675};
2676
2677int luaopen__openssl_x509_chain(lua_State *L) {
2678 initall(L);
2679
2680 luaL_newlib(L, xl_globals);
2681
2682 return 1;
2683} /* luaopen__openssl_x509_chain() */
2684
2685
2686/*
2687 * X509_STORE - openssl.x509.store
2688 *
2689 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2690
2691static int xs_new(lua_State *L) {
2692 X509_STORE **ud = prepsimple(L, X509_STORE_CLASS);
2693
2694 if (!(*ud = X509_STORE_new()))
2695 return throwssl(L, "x509.store");
2696
2697 return 1;
2698} /* xs_new() */
2699
2700
2701static int xs_interpose(lua_State *L) {
2702 return interpose(L, X509_STORE_CLASS);
2703} /* xs_interpose() */
2704
2705
2706static int xs_add(lua_State *L) {
2707 X509_STORE *store = checksimple(L, 1, X509_STORE_CLASS);
2708 X509 *crt = checksimple(L, 2, X509_CERT_CLASS);
2709 X509 *dup;
2710
2711 if (!(dup = X509_dup(crt)))
2712 return throwssl(L, "x509.store:add");
2713
2714 if (!X509_STORE_add_cert(store, dup)) {
2715 X509_free(dup);
2716 return throwssl(L, "x509.store:add");
2717 }
2718
2719 lua_pushboolean(L, 1);
2720
2721 return 1;
2722} /* xs_add() */
2723
2724
2725static int xs__gc(lua_State *L) {
2726 X509_STORE **ud = luaL_checkudata(L, 1, X509_STORE_CLASS);
2727
2728 X509_STORE_free(*ud);
2729 *ud = NULL;
2730
2731 return 0;
2732} /* xs__gc() */
2733
2734
2735static const luaL_Reg xs_methods[] = {
2736 { "add", &xs_add },
2737 { NULL, NULL },
2738};
2739
2740static const luaL_Reg xs_metatable[] = {
2741 { "__gc", &xs__gc },
2742 { NULL, NULL },
2743};
2744
2745static const luaL_Reg xs_globals[] = {
2746 { "new", &xs_new },
2747 { "interpose", &xs_interpose },
2748 { NULL, NULL },
2749};
2750
2751int luaopen__openssl_x509_store(lua_State *L) {
2752 initall(L);
2753
2754 luaL_newlib(L, xs_globals);
2755
2756 return 1;
2757} /* luaopen__openssl_x509_store() */
2758
2759
2760/*
2761 * X509_STORE_CTX - openssl.x509.store.context
2762 *
2763 * This object is intended to be a temporary container in OpenSSL, so the
2764 * memory management is quite clumsy. In particular, it doesn't take
2765 * ownership of the X509_STORE object, which means the reference must be
2766 * held externally for the life of the X509_STORE_CTX object.
2767 *
2768 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2769
2770static int sx_new(lua_State *L) {
2771 X509_STORE_CTX **ud = prepsimple(L, X509_STCTX_CLASS);
2772 STACK_OF(X509) *chain;
2773
2774 if (!(*ud = X509_STORE_CTX_new()))
2775 return throwssl(L, "x509.store.context");
2776
2777 return 1;
2778} /* sx_new() */
2779
2780
2781static int sx_interpose(lua_State *L) {
2782 return interpose(L, X509_STCTX_CLASS);
2783} /* sx_interpose() */
2784
2785
2786static int sx_add(lua_State *L) {
2787 X509_STORE_CTX *ctx = checksimple(L, 1, X509_STCTX_CLASS);
2788
2789
2790
2791 return 0;
2792} /* sx_add() */
2793
2794
2795static int sx__gc(lua_State *L) {
2796 X509_STORE **ud = luaL_checkudata(L, 1, X509_STORE_CLASS);
2797
2798 X509_STORE_free(*ud);
2799 *ud = NULL;
2800
2801 return 0;
2802} /* sx__gc() */
2803
2804
2805static const luaL_Reg sx_methods[] = {
2806 { "add", &sx_add },
2807 { NULL, NULL },
2808};
2809
2810static const luaL_Reg sx_metatable[] = {
2811 { "__gc", &sx__gc },
2812 { NULL, NULL },
2813};
2814
2815static const luaL_Reg sx_globals[] = {
2816 { "new", &sx_new },
2817 { "interpose", &sx_interpose },
2818 { NULL, NULL },
2819};
2820
2821int luaopen__openssl_x509_store_context(lua_State *L) {
2822 initall(L);
2823
2824 luaL_newlib(L, sx_globals);
2825
2826 return 1;
2827} /* luaopen__openssl_x509_store_context() */
2828
2829
2533 2830
2534static void initall(lua_State *L) { 2831static void initall(lua_State *L) {
2535 ERR_load_crypto_strings(); 2832 ERR_load_crypto_strings();
@@ -2540,7 +2837,9 @@ static void initall(lua_State *L) {
2540 addclass(L, X509_NAME_CLASS, xn_methods, xn_metatable); 2837 addclass(L, X509_NAME_CLASS, xn_methods, xn_metatable);
2541 addclass(L, X509_GENS_CLASS, gn_methods, gn_metatable); 2838 addclass(L, X509_GENS_CLASS, gn_methods, gn_metatable);
2542 addclass(L, X509_CERT_CLASS, xc_methods, xc_metatable); 2839 addclass(L, X509_CERT_CLASS, xc_methods, xc_metatable);
2543 addclass(L, X509_CSR_CLASS, xr_methods, xr_metatable); 2840 addclass(L, X509_CSR_CLASS, xr_methods, xr_metatable);
2841 addclass(L, X509_CHAIN_CLASS, xl_methods, xl_metatable);
2842 addclass(L, X509_STORE_CLASS, xs_methods, xs_metatable);
2544} /* initall() */ 2843} /* initall() */
2545 2844
2546 2845