diff options
| author | William Ahern <william@server.local> | 2013-03-14 15:48:27 -0700 |
|---|---|---|
| committer | William Ahern <william@server.local> | 2013-03-14 15:48:27 -0700 |
| commit | dff7b083b113bbed0de3e6233467cfccf0e7a80c (patch) | |
| tree | a29779560fa4ee8c77559301f14a55a49850ad4f | |
| parent | 926d819effb9f9e7bfb8c5172911c7a3951994ba (diff) | |
| download | luaossl-dff7b083b113bbed0de3e6233467cfccf0e7a80c.tar.gz luaossl-dff7b083b113bbed0de3e6233467cfccf0e7a80c.tar.bz2 luaossl-dff7b083b113bbed0de3e6233467cfccf0e7a80c.zip | |
-n
don't throw errors in cipher:update and :final
| -rw-r--r-- | openssl.c | 40 |
1 files changed, 35 insertions, 5 deletions
| @@ -146,13 +146,14 @@ static void *testsimple(lua_State *L, int index, const char *tname) { | |||
| 146 | } /* testsimple() */ | 146 | } /* testsimple() */ |
| 147 | 147 | ||
| 148 | 148 | ||
| 149 | static int throwssl(lua_State *L, const char *fun) { | 149 | static const char *pusherror(lua_State *L, const char *fun) { |
| 150 | unsigned long code; | 150 | unsigned long code; |
| 151 | const char *path, *file; | 151 | const char *path, *file; |
| 152 | int line; | 152 | int line; |
| 153 | char txt[256]; | 153 | char txt[256]; |
| 154 | 154 | ||
| 155 | code = ERR_get_error_line(&path, &line); | 155 | code = ERR_get_error_line(&path, &line); |
| 156 | |||
| 156 | if ((file = strrchr(path, '/'))) | 157 | if ((file = strrchr(path, '/'))) |
| 157 | ++file; | 158 | ++file; |
| 158 | else | 159 | else |
| @@ -162,7 +163,17 @@ static int throwssl(lua_State *L, const char *fun) { | |||
| 162 | 163 | ||
| 163 | ERR_error_string_n(code, txt, sizeof txt); | 164 | ERR_error_string_n(code, txt, sizeof txt); |
| 164 | 165 | ||
| 165 | return luaL_error(L, "%s: %s:%d:%s", fun, file, line, txt); | 166 | if (fun) |
| 167 | return lua_pushfstring(L, "%s: %s:%d:%s", fun, file, line, txt); | ||
| 168 | else | ||
| 169 | return lua_pushfstring(L, "%s:%d:%s", file, line, txt); | ||
| 170 | } /* pusherror() */ | ||
| 171 | |||
| 172 | |||
| 173 | static int throwssl(lua_State *L, const char *fun) { | ||
| 174 | pusherror(L, fun); | ||
| 175 | |||
| 176 | return lua_error(L); | ||
| 166 | } /* throwssl() */ | 177 | } /* throwssl() */ |
| 167 | 178 | ||
| 168 | 179 | ||
| @@ -3576,11 +3587,20 @@ static int cipher_init(lua_State *L, _Bool encrypt) { | |||
| 3576 | luaL_argcheck(L, 3, n == m, lua_pushfstring(L, "%u: invalid IV length (should be %u)", (unsigned)n, (unsigned)m)); | 3587 | luaL_argcheck(L, 3, n == m, lua_pushfstring(L, "%u: invalid IV length (should be %u)", (unsigned)n, (unsigned)m)); |
| 3577 | 3588 | ||
| 3578 | if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, encrypt)) | 3589 | if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, encrypt)) |
| 3579 | return throwssl(L, (encrypt)? "cipher:encrypt" : "cipher:decrypt"); | 3590 | goto sslerr; |
| 3591 | |||
| 3592 | if (!lua_isnoneornil(L, 4)) { | ||
| 3593 | luaL_checktype(L, 4, LUA_TBOOLEAN); | ||
| 3594 | |||
| 3595 | if (!EVP_CIPHER_CTX_set_padding(ctx, lua_toboolean(L, 4))) | ||
| 3596 | goto sslerr; | ||
| 3597 | } | ||
| 3580 | 3598 | ||
| 3581 | lua_settop(L, 1); | 3599 | lua_settop(L, 1); |
| 3582 | 3600 | ||
| 3583 | return 1; | 3601 | return 1; |
| 3602 | sslerr: | ||
| 3603 | return throwssl(L, (encrypt)? "cipher:encrypt" : "cipher:decrypt"); | ||
| 3584 | } /* cipher_init() */ | 3604 | } /* cipher_init() */ |
| 3585 | 3605 | ||
| 3586 | 3606 | ||
| @@ -3616,7 +3636,7 @@ static int cipher_update(lua_State *L) { | |||
| 3616 | int in = (int)MIN((size_t)(pe - p), step), out; | 3636 | int in = (int)MIN((size_t)(pe - p), step), out; |
| 3617 | 3637 | ||
| 3618 | if (!EVP_CipherUpdate(ctx, (void *)luaL_prepbuffer(&B), &out, p, in)) | 3638 | if (!EVP_CipherUpdate(ctx, (void *)luaL_prepbuffer(&B), &out, p, in)) |
| 3619 | return throwssl(L, "cipher:update"); | 3639 | goto sslerr; |
| 3620 | 3640 | ||
| 3621 | p += in; | 3641 | p += in; |
| 3622 | luaL_addsize(&B, out); | 3642 | luaL_addsize(&B, out); |
| @@ -3625,6 +3645,11 @@ static int cipher_update(lua_State *L) { | |||
| 3625 | luaL_pushresult(&B); | 3645 | luaL_pushresult(&B); |
| 3626 | 3646 | ||
| 3627 | return 1; | 3647 | return 1; |
| 3648 | sslerr: | ||
| 3649 | lua_pushnil(L); | ||
| 3650 | pusherror(L, NULL); | ||
| 3651 | |||
| 3652 | return 2; | ||
| 3628 | } /* cipher_update() */ | 3653 | } /* cipher_update() */ |
| 3629 | 3654 | ||
| 3630 | 3655 | ||
| @@ -3642,12 +3667,17 @@ static int cipher_final(lua_State *L) { | |||
| 3642 | luaL_buffinit(L, &B); | 3667 | luaL_buffinit(L, &B); |
| 3643 | 3668 | ||
| 3644 | if (!EVP_CipherFinal(ctx, (void *)luaL_prepbuffer(&B), &out)) | 3669 | if (!EVP_CipherFinal(ctx, (void *)luaL_prepbuffer(&B), &out)) |
| 3645 | return throwssl(L, "cipher:final"); | 3670 | goto sslerr; |
| 3646 | 3671 | ||
| 3647 | luaL_addsize(&B, out); | 3672 | luaL_addsize(&B, out); |
| 3648 | luaL_pushresult(&B); | 3673 | luaL_pushresult(&B); |
| 3649 | 3674 | ||
| 3650 | return 1; | 3675 | return 1; |
| 3676 | sslerr: | ||
| 3677 | lua_pushnil(L); | ||
| 3678 | pusherror(L, NULL); | ||
| 3679 | |||
| 3680 | return 2; | ||
| 3651 | } /* cipher_final() */ | 3681 | } /* cipher_final() */ |
| 3652 | 3682 | ||
| 3653 | 3683 | ||
