Skip to content

Commit

Permalink
helper function: pushbiostring
Browse files Browse the repository at this point in the history
  • Loading branch information
kunkku committed Apr 30, 2018
1 parent 220d94f commit 88c535f
Showing 1 changed file with 31 additions and 63 deletions.
94 changes: 31 additions & 63 deletions src/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -3227,7 +3227,7 @@ static int bio__gc(lua_State *L) {
return 0;
} /* bio__gc() */

static BIO *getbio(lua_State *L) {
static BIO *getbioinstance(lua_State *L) {
BIO **bio;

lua_pushlightuserdata(L, (void *)&bio__gc);
Expand All @@ -3249,12 +3249,26 @@ static BIO *getbio(lua_State *L) {
bio = lua_touserdata(L, -1);
lua_pop(L, 1);

BIO_reset(*bio);

return *bio;
} /* getbioinstance() */


static BIO *getbio(lua_State *L) {
BIO *bio = getbioinstance(L);
BIO_reset(bio);
return bio;
} /* getbio() */


static void pushbiostring(lua_State *L) {
BIO *bio = getbioinstance(L);
char *str;
long len = BIO_get_mem_data(bio, &str);
lua_pushlstring(L, str, len);
BIO_reset(bio);
} /* pushbiostring() */


static int pk_new(lua_State *L) {
EVP_PKEY **ud;

Expand Down Expand Up @@ -3835,8 +3849,6 @@ static int pk_toPEM(lua_State *L) {
EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
int top, i, ok;
BIO *bio;
char *pem;
long len;

if (1 == (top = lua_gettop(L))) {
lua_pushstring(L, "publickey");
Expand All @@ -3858,18 +3870,14 @@ static int pk_toPEM(lua_State *L) {
if (!PEM_write_bio_PUBKEY(bio, key))
return auxL_error(L, auxL_EOPENSSL, "pkey:__tostring");

len = BIO_get_mem_data(bio, &pem);
lua_pushlstring(L, pem, len);
BIO_reset(bio);
pushbiostring(L);

break;
case 2: case 3: /* private, PrivateKey */
if (!PEM_write_bio_PrivateKey(bio, key, 0, 0, 0, 0, 0))
return auxL_error(L, auxL_EOPENSSL, "pkey:__tostring");

len = BIO_get_mem_data(bio, &pem);
lua_pushlstring(L, pem, len);
BIO_reset(bio);
pushbiostring(L);

break;
#if 0
Expand Down Expand Up @@ -4417,8 +4425,6 @@ static int pk__tostring(lua_State *L) {
EVP_PKEY *key = checksimple(L, 1, PKEY_CLASS);
int type = optencoding(L, 2, "pem", X509_PEM|X509_DER);
BIO *bio = getbio(L);
char *data;
long len;

switch (type) {
case X509_PEM:
Expand All @@ -4431,9 +4437,7 @@ static int pk__tostring(lua_State *L) {
break;
} /* switch() */

len = BIO_get_mem_data(bio, &data);

lua_pushlstring(L, data, len);
pushbiostring(L);

return 1;
} /* pk__tostring() */
Expand Down Expand Up @@ -4686,8 +4690,7 @@ static int ecg_tostring(lua_State *L) {
EC_GROUP *group = checksimple(L, 1, EC_GROUP_CLASS);
int how = optencoding(L, 2, "pem", X509_PEM|X509_DER|X509_TXT);
BIO *bio = getbio(L);
char *bytes;
int len, indent;
int indent;

switch (how) {
case X509_PEM:
Expand All @@ -4705,8 +4708,7 @@ static int ecg_tostring(lua_State *L) {
break;
}

len = BIO_get_mem_data(bio, &bytes);
lua_pushlstring(L, bytes, len);
pushbiostring(L);

return 1;
sslerr:
Expand Down Expand Up @@ -5452,15 +5454,11 @@ static int xe_text(lua_State *L) {
unsigned long flags = auxL_optunsigned(L, 2, 0, 0, ULONG_MAX);
int indent = auxL_optinteger(L, 3, 0, 0, INT_MAX);
BIO *bio = getbio(L);
char *data;
size_t len;

if (!X509V3_EXT_print(bio, ext, flags, indent))
return auxL_error(L, auxL_EOPENSSL, "x509.extension.text");

len = BIO_get_mem_data(bio, &data);

lua_pushlstring(L, data, len);
pushbiostring(L);

return 1;
} /* xe_text() */
Expand Down Expand Up @@ -6440,8 +6438,6 @@ static int xc_text(lua_State *L) {
unsigned int i;

BIO *bio = getbio(L);
char *data;
long len;

if (!lua_isnil(L, 2)) {
lua_pushnil(L);
Expand All @@ -6462,9 +6458,7 @@ static int xc_text(lua_State *L) {
if (!X509_print_ex(bio, crt, 0, flags))
return auxL_error(L, auxL_EOPENSSL, "x509.cert:text");

len = BIO_get_mem_data(bio, &data);

lua_pushlstring(L, data, len);
pushbiostring(L);

return 1;
} /* xc_text() */
Expand All @@ -6474,8 +6468,6 @@ static int xc__tostring(lua_State *L) {
X509 *crt = checksimple(L, 1, X509_CERT_CLASS);
int type = optencoding(L, 2, "pem", X509_PEM|X509_DER);
BIO *bio = getbio(L);
char *data;
long len;

switch (type) {
case X509_PEM:
Expand All @@ -6488,9 +6480,7 @@ static int xc__tostring(lua_State *L) {
break;
} /* switch() */

len = BIO_get_mem_data(bio, &data);

lua_pushlstring(L, data, len);
pushbiostring(L);

return 1;
} /* xc__tostring() */
Expand Down Expand Up @@ -6809,8 +6799,6 @@ static int xr__tostring(lua_State *L) {
X509_REQ *csr = checksimple(L, 1, X509_CSR_CLASS);
int type = optencoding(L, 2, "pem", X509_PEM|X509_DER);
BIO *bio = getbio(L);
char *data;
long len;

switch (type) {
case X509_PEM:
Expand All @@ -6823,9 +6811,7 @@ static int xr__tostring(lua_State *L) {
break;
} /* switch() */

len = BIO_get_mem_data(bio, &data);

lua_pushlstring(L, data, len);
pushbiostring(L);

return 1;
} /* xr__tostring() */
Expand Down Expand Up @@ -7204,15 +7190,11 @@ static int xx_text(lua_State *L) {
X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS);

BIO *bio = getbio(L);
char *data;
long len;

if (!X509_CRL_print(bio, crl))
return auxL_error(L, auxL_EOPENSSL, "x509.crl:text");

len = BIO_get_mem_data(bio, &data);

lua_pushlstring(L, data, len);
pushbiostring(L);

return 1;
} /* xx_text() */
Expand All @@ -7222,8 +7204,6 @@ static int xx__tostring(lua_State *L) {
X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS);
int type = optencoding(L, 2, "pem", X509_PEM|X509_DER);
BIO *bio = getbio(L);
char *data;
long len;

switch (type) {
case X509_PEM:
Expand All @@ -7236,9 +7216,7 @@ static int xx__tostring(lua_State *L) {
break;
} /* switch() */

len = BIO_get_mem_data(bio, &data);

lua_pushlstring(L, data, len);
pushbiostring(L);

return 1;
} /* xx__tostring() */
Expand Down Expand Up @@ -7848,15 +7826,11 @@ static int p12_parse(lua_State *L) {
static int p12__tostring(lua_State *L) {
PKCS12 *p12 = checksimple(L, 1, PKCS12_CLASS);
BIO *bio = getbio(L);
char *data;
long len;

if (!i2d_PKCS12_bio(bio, p12))
return auxL_error(L, auxL_EOPENSSL, "pkcs12:__tostring");

len = BIO_get_mem_data(bio, &data);

lua_pushlstring(L, data, len);
pushbiostring(L);

return 1;
} /* p12__tostring() */
Expand Down Expand Up @@ -9902,14 +9876,11 @@ EXPORT int luaopen__openssl_cipher(lua_State *L) {
static int or_tostring(lua_State *L) {
OCSP_RESPONSE *resp = checksimple(L, 1, OCSP_RESPONSE_CLASS);
BIO *bio = getbio(L);
size_t len;
char *bytes;

if (!OCSP_RESPONSE_print(bio, resp, 0))
return auxL_error(L, auxL_EOPENSSL, "OCSP_RESPONSE:tostring");

len = BIO_get_mem_data(bio, &bytes);
lua_pushlstring(L, bytes, len);
pushbiostring(L);

return 1;
} /* or__tostring() */
Expand All @@ -9918,14 +9889,11 @@ static int or_tostring(lua_State *L) {
static int or_toPEM(lua_State *L) {
OCSP_RESPONSE *resp = checksimple(L, 1, OCSP_RESPONSE_CLASS);
BIO *bio = getbio(L);
size_t len;
char *bytes;

if (!PEM_write_bio_OCSP_RESPONSE(bio, resp))
return auxL_error(L, auxL_EOPENSSL, "OCSP_RESPONSE:toPEM");

len = BIO_get_mem_data(bio, &bytes);
lua_pushlstring(L, bytes, len);
pushbiostring(L);

return 1;
} /* or_toPEM() */
Expand Down

0 comments on commit 88c535f

Please sign in to comment.