diff options
| author | william <william+macosx@25thandclement.com> | 2015-06-04 18:27:35 -0700 |
|---|---|---|
| committer | william <william+macosx@25thandclement.com> | 2015-06-04 18:27:35 -0700 |
| commit | 5fa11721a7b3dddf5a7e41a164428749a60b7c44 (patch) | |
| tree | 57b0cd472c68900fb9f299cbcbb94ec44d22af5b /src | |
| parent | 53bcdd14c76157bac713414ca3df745c08b87916 (diff) | |
| download | luaossl-5fa11721a7b3dddf5a7e41a164428749a60b7c44.tar.gz luaossl-5fa11721a7b3dddf5a7e41a164428749a60b7c44.tar.bz2 luaossl-5fa11721a7b3dddf5a7e41a164428749a60b7c44.zip | |
permit getting extension by index (1-based indexing), and add x509:getExtensionCount and crl:getExtensionCount methods
Diffstat (limited to 'src')
| -rw-r--r-- | src/openssl.c | 120 |
1 files changed, 86 insertions, 34 deletions
diff --git a/src/openssl.c b/src/openssl.c index a4efcdc..108ae3d 100644 --- a/src/openssl.c +++ b/src/openssl.c | |||
| @@ -585,6 +585,24 @@ static size_t auxS_obj2txt(void *dst, size_t lim, const ASN1_OBJECT *obj) { | |||
| 585 | return auxS_obj2id(dst, lim, obj); | 585 | return auxS_obj2id(dst, lim, obj); |
| 586 | } /* auxS_obj2txt() */ | 586 | } /* auxS_obj2txt() */ |
| 587 | 587 | ||
| 588 | static _Bool auxS_isoid(const char *txt) { | ||
| 589 | return (*txt >= '0' && *txt <= '9'); | ||
| 590 | } /* auxS_isoid() */ | ||
| 591 | |||
| 592 | static _Bool auxS_txt2obj(ASN1_OBJECT **obj, const char *txt) { | ||
| 593 | int nid; | ||
| 594 | |||
| 595 | if ((nid = OBJ_sn2nid(txt)) != NID_undef | ||
| 596 | || (nid = OBJ_ln2nid(txt)) != NID_undef) { | ||
| 597 | return NULL != (*obj = OBJ_nid2obj(nid)); | ||
| 598 | } else if (auxS_isoid(txt)) { | ||
| 599 | return NULL != (*obj = OBJ_txt2obj(txt, 1)); | ||
| 600 | } else { | ||
| 601 | *obj = NULL; | ||
| 602 | return 1; | ||
| 603 | } | ||
| 604 | } /* auxS_txt2obj() */ | ||
| 605 | |||
| 588 | 606 | ||
| 589 | /* | 607 | /* |
| 590 | * Auxiliary Lua API routines | 608 | * Auxiliary Lua API routines |
| @@ -3867,35 +3885,51 @@ static int xc_addExtension(lua_State *L) { | |||
| 3867 | 3885 | ||
| 3868 | static int xc_getExtension(lua_State *L) { | 3886 | static int xc_getExtension(lua_State *L) { |
| 3869 | X509 *crt = checksimple(L, 1, X509_CERT_CLASS); | 3887 | X509 *crt = checksimple(L, 1, X509_CERT_CLASS); |
| 3870 | const char *name = luaL_checkstring(L, 2); | 3888 | X509_EXTENSION *ext = NULL, **ud; |
| 3871 | X509_EXTENSION *ext, **ud; | 3889 | int i; |
| 3872 | ASN1_OBJECT *obj = NULL; | ||
| 3873 | 3890 | ||
| 3874 | if (!(obj = OBJ_txt2obj(name, 0))) | 3891 | luaL_checkany(L, 2); |
| 3875 | goto error; | ||
| 3876 | 3892 | ||
| 3877 | int i = X509_get_ext_by_OBJ(crt, obj, -1); | 3893 | if (lua_type(L, 2) == LUA_TNUMBER) { |
| 3878 | if (i > -1) { | 3894 | /* NB: Lua 1-based indexing */ |
| 3879 | ud = prepsimple(L, X509_EXT_CLASS); | 3895 | i = auxL_checkinteger(L, 2, 1, INT_MAX) - 1; |
| 3880 | if (!(ext = X509_get0_ext(crt, i))) | ||
| 3881 | goto error; | ||
| 3882 | if (!(*ud = X509_EXTENSION_dup(ext))) | ||
| 3883 | goto error; | ||
| 3884 | } else { | 3896 | } else { |
| 3885 | lua_pushnil(L); | 3897 | ASN1_OBJECT *obj; |
| 3898 | |||
| 3899 | if (!auxS_txt2obj(&obj, luaL_checkstring(L, 2))) { | ||
| 3900 | goto error; | ||
| 3901 | } else if (!obj) { | ||
| 3902 | goto undef; | ||
| 3903 | } | ||
| 3904 | |||
| 3905 | i = X509_get_ext_by_OBJ(crt, obj, -1); | ||
| 3906 | |||
| 3907 | ASN1_OBJECT_free(obj); | ||
| 3886 | } | 3908 | } |
| 3887 | 3909 | ||
| 3888 | ASN1_OBJECT_free(obj); | 3910 | ud = prepsimple(L, X509_EXT_CLASS); |
| 3911 | |||
| 3912 | if (i < 0 || !(ext = X509_get0_ext(crt, i))) | ||
| 3913 | goto undef; | ||
| 3914 | |||
| 3915 | if (!(*ud = X509_EXTENSION_dup(ext))) | ||
| 3916 | goto error; | ||
| 3889 | 3917 | ||
| 3890 | return 1; | 3918 | return 1; |
| 3919 | undef: | ||
| 3920 | return 0; | ||
| 3891 | error: | 3921 | error: |
| 3892 | if (obj) | ||
| 3893 | ASN1_OBJECT_free(obj); | ||
| 3894 | |||
| 3895 | return auxL_error(L, auxL_EOPENSSL, "x509.cert:getExtension"); | 3922 | return auxL_error(L, auxL_EOPENSSL, "x509.cert:getExtension"); |
| 3896 | } /* xc_getExtension() */ | 3923 | } /* xc_getExtension() */ |
| 3897 | 3924 | ||
| 3898 | 3925 | ||
| 3926 | static int xc_getExtensionCount(lua_State *L) { | ||
| 3927 | auxL_pushinteger(L, X509_get_ext_count(checksimple(L, 1, X509_CERT_CLASS))); | ||
| 3928 | |||
| 3929 | return 1; | ||
| 3930 | } /* xc_getExtensionCount() */ | ||
| 3931 | |||
| 3932 | |||
| 3899 | static int xc_isIssuedBy(lua_State *L) { | 3933 | static int xc_isIssuedBy(lua_State *L) { |
| 3900 | X509 *crt = checksimple(L, 1, X509_CERT_CLASS); | 3934 | X509 *crt = checksimple(L, 1, X509_CERT_CLASS); |
| 3901 | X509 *issuer = checksimple(L, 2, X509_CERT_CLASS); | 3935 | X509 *issuer = checksimple(L, 2, X509_CERT_CLASS); |
| @@ -4134,6 +4168,7 @@ static const luaL_Reg xc_methods[] = { | |||
| 4134 | { "setBasicConstraintsCritical", &xc_setBasicConstraintsCritical }, | 4168 | { "setBasicConstraintsCritical", &xc_setBasicConstraintsCritical }, |
| 4135 | { "addExtension", &xc_addExtension }, | 4169 | { "addExtension", &xc_addExtension }, |
| 4136 | { "getExtension", &xc_getExtension }, | 4170 | { "getExtension", &xc_getExtension }, |
| 4171 | { "getExtensionCount", &xc_getExtensionCount }, | ||
| 4137 | { "isIssuedBy", &xc_isIssuedBy }, | 4172 | { "isIssuedBy", &xc_isIssuedBy }, |
| 4138 | { "getPublicKey", &xc_getPublicKey }, | 4173 | { "getPublicKey", &xc_getPublicKey }, |
| 4139 | { "setPublicKey", &xc_setPublicKey }, | 4174 | { "setPublicKey", &xc_setPublicKey }, |
| @@ -4618,35 +4653,51 @@ static int xx_addExtension(lua_State *L) { | |||
| 4618 | 4653 | ||
| 4619 | static int xx_getExtension(lua_State *L) { | 4654 | static int xx_getExtension(lua_State *L) { |
| 4620 | X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS); | 4655 | X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS); |
| 4621 | const char *name = luaL_checkstring(L, 2); | 4656 | X509_EXTENSION *ext = NULL, **ud; |
| 4622 | X509_EXTENSION *ext, **ud; | 4657 | int i; |
| 4623 | ASN1_OBJECT *obj = NULL; | ||
| 4624 | 4658 | ||
| 4625 | if (!(obj = OBJ_txt2obj(name, 0))) | 4659 | luaL_checkany(L, 2); |
| 4626 | goto error; | ||
| 4627 | 4660 | ||
| 4628 | int i = X509_CRL_get_ext_by_OBJ(crl, obj, -1); | 4661 | if (lua_type(L, 2) == LUA_TNUMBER) { |
| 4629 | if (i > -1) { | 4662 | /* NB: Lua 1-based indexing */ |
| 4630 | ud = prepsimple(L, X509_CRL_CLASS); | 4663 | i = auxL_checkinteger(L, 2, 1, INT_MAX) - 1; |
| 4631 | if (!(ext = X509_CRL_get0_ext(crl, i))) | ||
| 4632 | goto error; | ||
| 4633 | if (!(*ud = X509_EXTENSION_dup(ext))) | ||
| 4634 | goto error; | ||
| 4635 | } else { | 4664 | } else { |
| 4636 | lua_pushnil(L); | 4665 | ASN1_OBJECT *obj; |
| 4666 | |||
| 4667 | if (!auxS_txt2obj(&obj, luaL_checkstring(L, 2))) { | ||
| 4668 | goto error; | ||
| 4669 | } else if (!obj) { | ||
| 4670 | goto undef; | ||
| 4671 | } | ||
| 4672 | |||
| 4673 | i = X509_CRL_get_ext_by_OBJ(crl, obj, -1); | ||
| 4674 | |||
| 4675 | ASN1_OBJECT_free(obj); | ||
| 4637 | } | 4676 | } |
| 4638 | 4677 | ||
| 4639 | ASN1_OBJECT_free(obj); | 4678 | ud = prepsimple(L, X509_EXT_CLASS); |
| 4679 | |||
| 4680 | if (i < 0 || !(ext = X509_CRL_get0_ext(crl, i))) | ||
| 4681 | goto undef; | ||
| 4682 | |||
| 4683 | if (!(*ud = X509_EXTENSION_dup(ext))) | ||
| 4684 | goto error; | ||
| 4640 | 4685 | ||
| 4641 | return 1; | 4686 | return 1; |
| 4687 | undef: | ||
| 4688 | return 0; | ||
| 4642 | error: | 4689 | error: |
| 4643 | if (obj) | ||
| 4644 | ASN1_OBJECT_free(obj); | ||
| 4645 | |||
| 4646 | return auxL_error(L, auxL_EOPENSSL, "x509.crl:getExtension"); | 4690 | return auxL_error(L, auxL_EOPENSSL, "x509.crl:getExtension"); |
| 4647 | } /* xx_getExtension() */ | 4691 | } /* xx_getExtension() */ |
| 4648 | 4692 | ||
| 4649 | 4693 | ||
| 4694 | static int xx_getExtensionCount(lua_State *L) { | ||
| 4695 | auxL_pushinteger(L, X509_CRL_get_ext_count(checksimple(L, 1, X509_CRL_CLASS))); | ||
| 4696 | |||
| 4697 | return 1; | ||
| 4698 | } /* xx_getExtensionCount() */ | ||
| 4699 | |||
| 4700 | |||
| 4650 | static int xx_sign(lua_State *L) { | 4701 | static int xx_sign(lua_State *L) { |
| 4651 | X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS); | 4702 | X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS); |
| 4652 | EVP_PKEY *key = checksimple(L, 2, PKEY_CLASS); | 4703 | EVP_PKEY *key = checksimple(L, 2, PKEY_CLASS); |
| @@ -4727,6 +4778,7 @@ static const luaL_Reg xx_methods[] = { | |||
| 4727 | { "add", &xx_add }, | 4778 | { "add", &xx_add }, |
| 4728 | { "addExtension", &xx_addExtension }, | 4779 | { "addExtension", &xx_addExtension }, |
| 4729 | { "getExtension", &xx_getExtension }, | 4780 | { "getExtension", &xx_getExtension }, |
| 4781 | { "getExtensionCount", &xx_getExtensionCount }, | ||
| 4730 | { "sign", &xx_sign }, | 4782 | { "sign", &xx_sign }, |
| 4731 | { "text", &xx_text }, | 4783 | { "text", &xx_text }, |
| 4732 | { "tostring", &xx__tostring }, | 4784 | { "tostring", &xx__tostring }, |
