summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Ahern <william@server.local>2013-02-26 19:32:59 -0800
committerWilliam Ahern <william@server.local>2013-02-26 19:32:59 -0800
commitbbf4aa5af2b44069eded0704c6f7b3d926af75c6 (patch)
tree49a77157d02332e1703a9ccab184c1fc6d2b8a19
parent03f20058472fab1d81b9a3f694457b090ab8e9d0 (diff)
downloadluaossl-bbf4aa5af2b44069eded0704c6f7b3d926af75c6.tar.gz
luaossl-bbf4aa5af2b44069eded0704c6f7b3d926af75c6.tar.bz2
luaossl-bbf4aa5af2b44069eded0704c6f7b3d926af75c6.zip
-n
wrap EVP_Sign and EVP_Verify
-rw-r--r--openssl.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/openssl.c b/openssl.c
index 145deef..bd65533 100644
--- a/openssl.c
+++ b/openssl.c
@@ -940,6 +940,52 @@ static int pk_setPrivateKey(lua_State *L) {
940} /* pk_setPrivateKEY() */ 940} /* pk_setPrivateKEY() */
941 941
942 942
943static int pk_sign(lua_State *L) {
944 EVP_PKEY *key = checksimple(L, 1, PUBKEY_CLASS);
945 EVP_MD_CTX *md = luaL_checkudata(L, 2, DIGEST_CLASS);
946 luaL_Buffer B;
947 unsigned n;
948
949 if (LUAL_BUFFERSIZE < EVP_PKEY_size(key))
950 return luaL_error(L, "pubkey:sign: LUAL_BUFFERSIZE(%zu) < EVP_PKEY_size(%zu)", (size_t)LUAL_BUFFERSIZE, (size_t)EVP_PKEY_size(key));
951
952 luaL_buffinit(L, &B);
953 n = LUAL_BUFFERSIZE;
954
955 if (!EVP_SignFinal(md, (void *)luaL_prepbuffer(&B), &n, key))
956 return throwssl(L, "pubkey:sign");
957
958 luaL_addsize(&B, n);
959 luaL_pushresult(&B);
960
961 return 1;
962} /* pk_sign() */
963
964
965static int pk_verify(lua_State *L) {
966 EVP_PKEY *key = checksimple(L, 1, PUBKEY_CLASS);
967 size_t len;
968 const void *sig = luaL_checklstring(L, 2, &len);
969 EVP_MD_CTX *md = luaL_checkudata(L, 3, DIGEST_CLASS);
970
971 switch (EVP_VerifyFinal(md, sig, len, key)) {
972 case 0: /* WRONG */
973 ERR_clear_error();
974 lua_pushboolean(L, 0);
975
976 break;
977 case 1: /* OK */
978 lua_pushboolean(L, 1);
979
980 break;
981 default:
982 return throwssl(L, "pubkey:verify");
983 }
984
985 return 1;
986} /* pk_verify() */
987
988
943static int pk_toPEM(lua_State *L) { 989static int pk_toPEM(lua_State *L) {
944 EVP_PKEY *key = checksimple(L, 1, PUBKEY_CLASS); 990 EVP_PKEY *key = checksimple(L, 1, PUBKEY_CLASS);
945 int top, i, ok; 991 int top, i, ok;
@@ -1074,6 +1120,8 @@ static const luaL_Reg pk_methods[] = {
1074 { "type", &pk_type }, 1120 { "type", &pk_type },
1075 { "setPublicKey", &pk_setPublicKey }, 1121 { "setPublicKey", &pk_setPublicKey },
1076 { "setPrivateKey", &pk_setPrivateKey }, 1122 { "setPrivateKey", &pk_setPrivateKey },
1123 { "sign", &pk_sign },
1124 { "verify", &pk_verify },
1077 { "toPEM", &pk_toPEM }, 1125 { "toPEM", &pk_toPEM },
1078 { NULL, NULL }, 1126 { NULL, NULL },
1079}; 1127};