diff options
| author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2014-05-15 10:18:14 +0300 |
|---|---|---|
| committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2014-05-28 22:42:55 +0300 |
| commit | 3bdb5b625285d2d924bb61742a2184008608f0dd (patch) | |
| tree | 57cbd2fe9af58b5780dda6a6e929c73d5923e0ff | |
| parent | ad5867b4c5c85ac4ade3cd2d4884ade0e1fae5a5 (diff) | |
| download | luaossl-3bdb5b625285d2d924bb61742a2184008608f0dd.tar.gz luaossl-3bdb5b625285d2d924bb61742a2184008608f0dd.tar.bz2 luaossl-3bdb5b625285d2d924bb61742a2184008608f0dd.zip | |
PKCS #12 module
| -rw-r--r-- | src/GNUmakefile | 1 | ||||
| -rw-r--r-- | src/openssl.c | 133 | ||||
| -rw-r--r-- | src/openssl.pkcs12.lua | 1 |
3 files changed, 135 insertions, 0 deletions
diff --git a/src/GNUmakefile b/src/GNUmakefile index 75e8c3a..240a773 100644 --- a/src/GNUmakefile +++ b/src/GNUmakefile | |||
| @@ -96,6 +96,7 @@ MODS$(1)_$(d) = \ | |||
| 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/store.lua \ | 98 | $$(DESTDIR)$(3)/openssl/x509/store.lua \ |
| 99 | $$(DESTDIR)$(3)/openssl/pkcs12.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 \ |
| 101 | $$(DESTDIR)$(3)/openssl/digest.lua \ | 102 | $$(DESTDIR)$(3)/openssl/digest.lua \ |
diff --git a/src/openssl.c b/src/openssl.c index c589d6c..2cdf1d4 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
| @@ -64,6 +64,7 @@ | |||
| 64 | #include <openssl/asn1.h> | 64 | #include <openssl/asn1.h> |
| 65 | #include <openssl/x509.h> | 65 | #include <openssl/x509.h> |
| 66 | #include <openssl/x509v3.h> | 66 | #include <openssl/x509v3.h> |
| 67 | #include <openssl/pkcs12.h> | ||
| 67 | #include <openssl/evp.h> | 68 | #include <openssl/evp.h> |
| 68 | #include <openssl/pem.h> | 69 | #include <openssl/pem.h> |
| 69 | #include <openssl/ssl.h> | 70 | #include <openssl/ssl.h> |
| @@ -88,6 +89,7 @@ | |||
| 88 | #define X509_CRL_CLASS "X509_CRL*" | 89 | #define X509_CRL_CLASS "X509_CRL*" |
| 89 | #define X509_STORE_CLASS "X509_STORE*" | 90 | #define X509_STORE_CLASS "X509_STORE*" |
| 90 | #define X509_STCTX_CLASS "X509_STORE_CTX*" | 91 | #define X509_STCTX_CLASS "X509_STORE_CTX*" |
| 92 | #define PKCS12_CLASS "PKCS12*" | ||
| 91 | #define SSL_CTX_CLASS "SSL_CTX*" | 93 | #define SSL_CTX_CLASS "SSL_CTX*" |
| 92 | #define SSL_CLASS "SSL*" | 94 | #define SSL_CLASS "SSL*" |
| 93 | #define DIGEST_CLASS "EVP_MD_CTX" /* not a pointer */ | 95 | #define DIGEST_CLASS "EVP_MD_CTX" /* not a pointer */ |
| @@ -363,6 +365,18 @@ static _Bool loadfield(lua_State *L, int index, const char *k, int type, void *p | |||
| 363 | } /* loadfield() */ | 365 | } /* loadfield() */ |
| 364 | 366 | ||
| 365 | 367 | ||
| 368 | static void *loadfield_udata(lua_State *L, int index, const char *k, const char *tname) { | ||
| 369 | if (!getfield(L, index, k)) | ||
| 370 | return NULL; | ||
| 371 | |||
| 372 | void **p = luaL_checkudata(L, -1, tname); | ||
| 373 | |||
| 374 | lua_pop(L, 1); /* table keeps reference */ | ||
| 375 | |||
| 376 | return *p; | ||
| 377 | } /* loadfield_udata() */ | ||
| 378 | |||
| 379 | |||
| 366 | static const char *pushnid(lua_State *L, int nid) { | 380 | static const char *pushnid(lua_State *L, int nid) { |
| 367 | const char *txt; | 381 | const char *txt; |
| 368 | ASN1_OBJECT *obj; | 382 | ASN1_OBJECT *obj; |
| @@ -3563,6 +3577,124 @@ int luaopen__openssl_x509_store_context(lua_State *L) { | |||
| 3563 | 3577 | ||
| 3564 | 3578 | ||
| 3565 | /* | 3579 | /* |
| 3580 | * PKCS12 - openssl.pkcs12 | ||
| 3581 | * | ||
| 3582 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
| 3583 | |||
| 3584 | static int p12_new(lua_State *L) { | ||
| 3585 | char *pass = NULL; | ||
| 3586 | loadfield(L, 1, "password", LUA_TSTRING, &pass); | ||
| 3587 | |||
| 3588 | EVP_PKEY *key = loadfield_udata(L, 1, "key", PKEY_CLASS); | ||
| 3589 | STACK_OF(X509) *certs = loadfield_udata(L, 1, "certs", X509_CHAIN_CLASS); | ||
| 3590 | |||
| 3591 | PKCS12 **ud = prepsimple(L, PKCS12_CLASS); | ||
| 3592 | |||
| 3593 | int i; | ||
| 3594 | int no_kcert = 0; | ||
| 3595 | X509 *cert = NULL; | ||
| 3596 | X509 *kcert = NULL; | ||
| 3597 | STACK_OF(X509) *ca; | ||
| 3598 | |||
| 3599 | if (!(ca = sk_X509_new_null())) | ||
| 3600 | goto error; | ||
| 3601 | |||
| 3602 | for (i = 0; i < sk_X509_num(certs); i++) { | ||
| 3603 | cert = sk_X509_value(certs, i); | ||
| 3604 | if (key && X509_check_private_key(cert, key)) { | ||
| 3605 | if (!(kcert = X509_dup(cert))) | ||
| 3606 | goto error; | ||
| 3607 | X509_keyid_set1(kcert, NULL, 0); | ||
| 3608 | X509_alias_set1(kcert, NULL, 0); | ||
| 3609 | } | ||
| 3610 | else sk_X509_push(ca, cert); | ||
| 3611 | } | ||
| 3612 | if (key && !kcert) { | ||
| 3613 | no_kcert = 1; | ||
| 3614 | goto error; | ||
| 3615 | } | ||
| 3616 | |||
| 3617 | if (!(*ud = PKCS12_create(pass, NULL, key, kcert, ca, 0, 0, 0, 0, 0))) | ||
| 3618 | goto error; | ||
| 3619 | |||
| 3620 | if (kcert) | ||
| 3621 | X509_free(kcert); | ||
| 3622 | sk_X509_free(ca); | ||
| 3623 | |||
| 3624 | return 1; | ||
| 3625 | |||
| 3626 | error: | ||
| 3627 | if (kcert) | ||
| 3628 | X509_free(kcert); | ||
| 3629 | if (ca) | ||
| 3630 | sk_X509_free(ca); | ||
| 3631 | |||
| 3632 | if (no_kcert) | ||
| 3633 | luaL_argerror(L, 1, lua_pushstring(L, "certificate matching the key not found")); | ||
| 3634 | |||
| 3635 | return throwssl(L, "pkcs12.new"); | ||
| 3636 | } /* p12_new() */ | ||
| 3637 | |||
| 3638 | |||
| 3639 | static int p12_interpose(lua_State *L) { | ||
| 3640 | return interpose(L, PKCS12_CLASS); | ||
| 3641 | } /* p12_interpose() */ | ||
| 3642 | |||
| 3643 | |||
| 3644 | static int p12__tostring(lua_State *L) { | ||
| 3645 | PKCS12 *p12 = checksimple(L, 1, PKCS12_CLASS); | ||
| 3646 | BIO *bio = getbio(L); | ||
| 3647 | char *data; | ||
| 3648 | long len; | ||
| 3649 | |||
| 3650 | if (!i2d_PKCS12_bio(bio, p12)) | ||
| 3651 | return throwssl(L, "pkcs12:__tostring"); | ||
| 3652 | |||
| 3653 | len = BIO_get_mem_data(bio, &data); | ||
| 3654 | |||
| 3655 | lua_pushlstring(L, data, len); | ||
| 3656 | |||
| 3657 | return 1; | ||
| 3658 | } /* p12__tostring() */ | ||
| 3659 | |||
| 3660 | |||
| 3661 | static int p12__gc(lua_State *L) { | ||
| 3662 | PKCS12 **ud = luaL_checkudata(L, 1, PKCS12_CLASS); | ||
| 3663 | |||
| 3664 | PKCS12_free(*ud); | ||
| 3665 | *ud = NULL; | ||
| 3666 | |||
| 3667 | return 0; | ||
| 3668 | } /* p12__gc() */ | ||
| 3669 | |||
| 3670 | |||
| 3671 | static const luaL_Reg p12_methods[] = { | ||
| 3672 | { "tostring", &p12__tostring }, | ||
| 3673 | { NULL, NULL }, | ||
| 3674 | }; | ||
| 3675 | |||
| 3676 | static const luaL_Reg p12_metatable[] = { | ||
| 3677 | { "__tostring", &p12__tostring }, | ||
| 3678 | { "__gc", &p12__gc }, | ||
| 3679 | { NULL, NULL }, | ||
| 3680 | }; | ||
| 3681 | |||
| 3682 | static const luaL_Reg p12_globals[] = { | ||
| 3683 | { "new", &p12_new }, | ||
| 3684 | { "interpose", &p12_interpose }, | ||
| 3685 | { NULL, NULL }, | ||
| 3686 | }; | ||
| 3687 | |||
| 3688 | int luaopen__openssl_pkcs12(lua_State *L) { | ||
| 3689 | initall(L); | ||
| 3690 | |||
| 3691 | luaL_newlib(L, p12_globals); | ||
| 3692 | |||
| 3693 | return 1; | ||
| 3694 | } /* luaopen__openssl_pkcs12() */ | ||
| 3695 | |||
| 3696 | |||
| 3697 | /* | ||
| 3566 | * SSL_CTX - openssl.ssl.context | 3698 | * SSL_CTX - openssl.ssl.context |
| 3567 | * | 3699 | * |
| 3568 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | 3700 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| @@ -4759,6 +4891,7 @@ static void initall(lua_State *L) { | |||
| 4759 | addclass(L, X509_CRL_CLASS, xx_methods, xx_metatable); | 4891 | addclass(L, X509_CRL_CLASS, xx_methods, xx_metatable); |
| 4760 | addclass(L, X509_CHAIN_CLASS, xl_methods, xl_metatable); | 4892 | addclass(L, X509_CHAIN_CLASS, xl_methods, xl_metatable); |
| 4761 | addclass(L, X509_STORE_CLASS, xs_methods, xs_metatable); | 4893 | addclass(L, X509_STORE_CLASS, xs_methods, xs_metatable); |
| 4894 | addclass(L, PKCS12_CLASS, p12_methods, p12_metatable); | ||
| 4762 | addclass(L, SSL_CTX_CLASS, sx_methods, sx_metatable); | 4895 | addclass(L, SSL_CTX_CLASS, sx_methods, sx_metatable); |
| 4763 | addclass(L, SSL_CLASS, ssl_methods, ssl_metatable); | 4896 | addclass(L, SSL_CLASS, ssl_methods, ssl_metatable); |
| 4764 | addclass(L, DIGEST_CLASS, md_methods, md_metatable); | 4897 | addclass(L, DIGEST_CLASS, md_methods, md_metatable); |
diff --git a/src/openssl.pkcs12.lua b/src/openssl.pkcs12.lua new file mode 100644 index 0000000..d8f70c2 --- /dev/null +++ b/src/openssl.pkcs12.lua | |||
| @@ -0,0 +1 @@ | |||
| return require('_openssl.pkcs12') | |||
