summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Ahern <william@server.local>2013-01-31 19:38:32 -0800
committerWilliam Ahern <william@server.local>2013-01-31 19:38:32 -0800
commit03f20058472fab1d81b9a3f694457b090ab8e9d0 (patch)
treeada354307b43c496dd0faa8c78492edd4d30c5eb
parent705995f5d19aea40324b5f5c8031f6c39852e65c (diff)
downloadluaossl-03f20058472fab1d81b9a3f694457b090ab8e9d0.tar.gz
luaossl-03f20058472fab1d81b9a3f694457b090ab8e9d0.tar.bz2
luaossl-03f20058472fab1d81b9a3f694457b090ab8e9d0.zip
-n
add openssl.digest and openssl.hmac
-rw-r--r--Makefile12
-rw-r--r--openssl.c203
-rw-r--r--openssl.digest.lua3
-rw-r--r--openssl.hmac.lua3
4 files changed, 220 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 0087323..28a9b88 100644
--- a/Makefile
+++ b/Makefile
@@ -53,7 +53,9 @@ install: $(DESTDIR)$(lua52cpath)/_openssl.so \
53 $(DESTDIR)$(lua52path)/openssl/x509/chain.lua \ 53 $(DESTDIR)$(lua52path)/openssl/x509/chain.lua \
54 $(DESTDIR)$(lua52path)/openssl/x509/store.lua \ 54 $(DESTDIR)$(lua52path)/openssl/x509/store.lua \
55 $(DESTDIR)$(lua52path)/openssl/ssl/context.lua \ 55 $(DESTDIR)$(lua52path)/openssl/ssl/context.lua \
56 $(DESTDIR)$(lua52path)/openssl/ssl.lua 56 $(DESTDIR)$(lua52path)/openssl/ssl.lua \
57 $(DESTDIR)$(lua52path)/openssl/digest.lua \
58 $(DESTDIR)$(lua52path)/openssl/hmac.lua
57 59
58$(DESTDIR)$(lua52cpath)/_openssl.so: openssl.so 60$(DESTDIR)$(lua52cpath)/_openssl.so: openssl.so
59 mkdir -p $(@D) 61 mkdir -p $(@D)
@@ -95,6 +97,14 @@ $(DESTDIR)$(lua52path)/openssl/ssl.lua: openssl.ssl.lua
95 mkdir -p $(@D) 97 mkdir -p $(@D)
96 cp -p $< $@ 98 cp -p $< $@
97 99
100$(DESTDIR)$(lua52path)/openssl/digest.lua: openssl.digest.lua
101 mkdir -p $(@D)
102 cp -p $< $@
103
104$(DESTDIR)$(lua52path)/openssl/hmac.lua: openssl.hmac.lua
105 mkdir -p $(@D)
106 cp -p $< $@
107
98 108
99.PHONY: clean clean~ 109.PHONY: clean clean~
100 110
diff --git a/openssl.c b/openssl.c
index 67883e6..145deef 100644
--- a/openssl.c
+++ b/openssl.c
@@ -47,6 +47,7 @@
47#include <openssl/evp.h> 47#include <openssl/evp.h>
48#include <openssl/pem.h> 48#include <openssl/pem.h>
49#include <openssl/ssl.h> 49#include <openssl/ssl.h>
50#include <openssl/hmac.h>
50 51
51#include <lua.h> 52#include <lua.h>
52#include <lualib.h> 53#include <lualib.h>
@@ -68,6 +69,8 @@
68#define X509_STCTX_CLASS "OpenSSL X.509 Store Context" 69#define X509_STCTX_CLASS "OpenSSL X.509 Store Context"
69#define SSL_CTX_CLASS "OpenSSL SSL Context" 70#define SSL_CTX_CLASS "OpenSSL SSL Context"
70#define SSL_CLASS "OpenSSL SSL" 71#define SSL_CLASS "OpenSSL SSL"
72#define DIGEST_CLASS "OpenSSL Digest"
73#define HMAC_CLASS "OpenSSL HMAC"
71 74
72 75
73#define countof(a) (sizeof (a) / sizeof *(a)) 76#define countof(a) (sizeof (a) / sizeof *(a))
@@ -3272,6 +3275,204 @@ int luaopen__openssl_ssl(lua_State *L) {
3272} /* luaopen__openssl_ssl() */ 3275} /* luaopen__openssl_ssl() */
3273 3276
3274 3277
3278/*
3279 * Digest - openssl.digest
3280 *
3281 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3282
3283static const EVP_MD *md_optdigest(lua_State *L, int index) {
3284 const char *name = luaL_optstring(L, index, "sha1");
3285 const EVP_MD *type;
3286
3287 if (!(type = EVP_get_digestbyname(name)))
3288 luaL_argerror(L, index, lua_pushfstring(L, "%s: invalid digest type", name));
3289
3290 return type;
3291} /* md_optdigest() */
3292
3293
3294static int md_new(lua_State *L) {
3295 const EVP_MD *type = md_optdigest(L, 1);
3296 EVP_MD_CTX *ctx;
3297
3298 ctx = prepudata(L, sizeof *ctx, DIGEST_CLASS, NULL);
3299
3300 EVP_MD_CTX_init(ctx);
3301
3302 if (!EVP_DigestInit_ex(ctx, type, NULL))
3303 return throwssl(L, "digest.new");
3304
3305 return 1;
3306} /* md_new() */
3307
3308
3309static int md_interpose(lua_State *L) {
3310 return interpose(L, DIGEST_CLASS);
3311} /* md_interpose() */
3312
3313
3314static int md_update(lua_State *L) {
3315 EVP_MD_CTX *ctx = luaL_checkudata(L, 1, DIGEST_CLASS);
3316 int i, top = lua_gettop(L);
3317
3318 for (i = 2; i < top; i++) {
3319 const void *p;
3320 size_t n;
3321
3322 p = luaL_checklstring(L, i, &n);
3323
3324 if (!EVP_DigestUpdate(ctx, p, n))
3325 return throwssl(L, "digest:update");
3326 }
3327
3328 lua_pushboolean(L, 1);
3329
3330 return 1;
3331} /* md_update() */
3332
3333
3334static int md_final(lua_State *L) {
3335 EVP_MD_CTX *ctx = luaL_checkudata(L, 1, DIGEST_CLASS);
3336 unsigned char md[EVP_MAX_MD_SIZE];
3337 unsigned len;
3338
3339 if (!EVP_DigestFinal_ex(ctx, md, &len))
3340 return throwssl(L, "digest:final");
3341
3342 lua_pushlstring(L, (char *)md, len);
3343
3344 return 1;
3345} /* md_final() */
3346
3347
3348static int md__gc(lua_State *L) {
3349 EVP_MD_CTX *ctx = luaL_checkudata(L, 1, DIGEST_CLASS);
3350
3351 EVP_MD_CTX_cleanup(ctx);
3352
3353 return 0;
3354} /* md__gc() */
3355
3356
3357static const luaL_Reg md_methods[] = {
3358 { "update", &md_update },
3359 { "final", &md_final },
3360 { NULL, NULL },
3361};
3362
3363static const luaL_Reg md_metatable[] = {
3364 { "__gc", &md__gc },
3365 { NULL, NULL },
3366};
3367
3368static const luaL_Reg md_globals[] = {
3369 { "new", &md_new },
3370 { "interpose", &md_interpose },
3371 { NULL, NULL },
3372};
3373
3374int luaopen__openssl_digest(lua_State *L) {
3375 initall(L);
3376
3377 luaL_newlib(L, md_globals);
3378
3379 return 1;
3380} /* luaopen__openssl_digest() */
3381
3382
3383/*
3384 * HMAC - openssl.hmac
3385 *
3386 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
3387
3388static int hmac_new(lua_State *L) {
3389 const void *key;
3390 size_t len;
3391 const EVP_MD *type;
3392 HMAC_CTX *ctx;
3393
3394 key = luaL_checklstring(L, 1, &len);
3395 type = md_optdigest(L, 2);
3396
3397 ctx = prepudata(L, sizeof *ctx, HMAC_CLASS, NULL);
3398
3399 HMAC_Init_ex(ctx, key, len, type, NULL);
3400
3401 return 1;
3402} /* hmac_new() */
3403
3404
3405static int hmac_interpose(lua_State *L) {
3406 return interpose(L, HMAC_CLASS);
3407} /* hmac_interpose() */
3408
3409
3410static int hmac_update(lua_State *L) {
3411 HMAC_CTX *ctx = luaL_checkudata(L, 1, HMAC_CLASS);
3412 int i, top = lua_gettop(L);
3413
3414 for (i = 2; i < top; i++) {
3415 const void *p;
3416 size_t n;
3417
3418 p = luaL_checklstring(L, i, &n);
3419 HMAC_Update(ctx, p, n);
3420 }
3421
3422 lua_pushboolean(L, 1);
3423
3424 return 1;
3425} /* hmac_update() */
3426
3427
3428static int hmac_final(lua_State *L) {
3429 HMAC_CTX *ctx = luaL_checkudata(L, 1, HMAC_CLASS);
3430 unsigned char hmac[EVP_MAX_MD_SIZE];
3431 unsigned len;
3432
3433 HMAC_Final(ctx, hmac, &len);
3434
3435 lua_pushlstring(L, (char *)hmac, len);
3436
3437 return 1;
3438} /* hmac_final() */
3439
3440
3441static int hmac__gc(lua_State *L) {
3442 HMAC_CTX *ctx = luaL_checkudata(L, 1, HMAC_CLASS);
3443
3444 HMAC_CTX_cleanup(ctx);
3445
3446 return 0;
3447} /* hmac__gc() */
3448
3449
3450static const luaL_Reg hmac_methods[] = {
3451 { "update", &hmac_update },
3452 { "final", &hmac_final },
3453 { NULL, NULL },
3454};
3455
3456static const luaL_Reg hmac_metatable[] = {
3457 { "__gc", &hmac__gc },
3458 { NULL, NULL },
3459};
3460
3461static const luaL_Reg hmac_globals[] = {
3462 { "new", &hmac_new },
3463 { "interpose", &hmac_interpose },
3464 { NULL, NULL },
3465};
3466
3467int luaopen__openssl_hmac(lua_State *L) {
3468 initall(L);
3469
3470 luaL_newlib(L, hmac_globals);
3471
3472 return 1;
3473} /* luaopen__openssl_hmac() */
3474
3475
3275static void initall(lua_State *L) { 3476static void initall(lua_State *L) {
3276 ERR_load_crypto_strings(); 3477 ERR_load_crypto_strings();
3277 OpenSSL_add_all_algorithms(); 3478 OpenSSL_add_all_algorithms();
@@ -3286,6 +3487,8 @@ static void initall(lua_State *L) {
3286 addclass(L, X509_STORE_CLASS, xs_methods, xs_metatable); 3487 addclass(L, X509_STORE_CLASS, xs_methods, xs_metatable);
3287 addclass(L, SSL_CTX_CLASS, sx_methods, sx_metatable); 3488 addclass(L, SSL_CTX_CLASS, sx_methods, sx_metatable);
3288 addclass(L, SSL_CLASS, ssl_methods, ssl_metatable); 3489 addclass(L, SSL_CLASS, ssl_methods, ssl_metatable);
3490 addclass(L, DIGEST_CLASS, md_methods, md_metatable);
3491 addclass(L, HMAC_CLASS, hmac_methods, hmac_metatable);
3289} /* initall() */ 3492} /* initall() */
3290 3493
3291 3494
diff --git a/openssl.digest.lua b/openssl.digest.lua
new file mode 100644
index 0000000..87cc958
--- /dev/null
+++ b/openssl.digest.lua
@@ -0,0 +1,3 @@
1local ctx = require"_openssl.digest"
2
3return ctx
diff --git a/openssl.hmac.lua b/openssl.hmac.lua
new file mode 100644
index 0000000..e622d1d
--- /dev/null
+++ b/openssl.hmac.lua
@@ -0,0 +1,3 @@
1local ctx = require"_openssl.hmac"
2
3return ctx