Permalink
Browse files

pkey: to_pem: check return codes

check the return codes of wrote_bio_PrivateKey for any error.
  • Loading branch information...
1 parent be18148 commit 90539d70d0fc6d6286966a708a76307e2ae73fc4 Brandon Philips committed Mar 20, 2012
Showing with 14 additions and 7 deletions.
  1. +14 −7 src/lcrypto.c
View
@@ -987,26 +987,33 @@ static int pkey_to_pem(lua_State *L)
EVP_PKEY **pkey = (EVP_PKEY **)luaL_checkudata(L, 1, LUACRYPTO_PKEYNAME);
int private = lua_isboolean(L, 2) && lua_toboolean(L, 2);
struct evp_pkey_st *pkey_st = *pkey;
+ int ret;
long len;
BUF_MEM *buf;
BIO *mem = BIO_new(BIO_s_mem());
if (private && pkey_st->type == EVP_PKEY_DSA)
- PEM_write_bio_DSAPrivateKey(mem, pkey_st->pkey.dsa, NULL, NULL, 0, NULL, NULL);
+ ret = PEM_write_bio_DSAPrivateKey(mem, pkey_st->pkey.dsa, NULL, NULL, 0, NULL, NULL);
else if (private && pkey_st->type == EVP_PKEY_RSA)
- PEM_write_bio_RSAPrivateKey(mem, pkey_st->pkey.rsa, NULL, NULL, 0, NULL, NULL);
+ ret = PEM_write_bio_RSAPrivateKey(mem, pkey_st->pkey.rsa, NULL, NULL, 0, NULL, NULL);
else if (private)
- PEM_write_bio_PrivateKey(mem, *pkey, NULL, NULL, 0, NULL, NULL);
+ ret = PEM_write_bio_PrivateKey(mem, *pkey, NULL, NULL, 0, NULL, NULL);
else
- PEM_write_bio_PUBKEY(mem, *pkey);
+ ret = PEM_write_bio_PUBKEY(mem, *pkey);
- len = BIO_get_mem_ptr(mem, &buf);
+ if (ret == 0) {
+ ret = crypto_error(L);
+ goto error;
+ }
+ len = BIO_get_mem_ptr(mem, &buf);
lua_pushlstring(L, buf->data, buf->length);
- BIO_free(mem);
+ ret = 1;
- return 1;
+error:
+ BIO_free(mem);
+ return ret;
}
static int pkey_read(lua_State *L)

0 comments on commit 90539d7

Please sign in to comment.