From 805a837b3e5476bad85116718d3be3a4c213ee20 Mon Sep 17 00:00:00 2001 From: william Date: Wed, 26 Mar 2014 20:10:43 -0700 Subject: add DER parameter to pk__tostring for symmetry, but we still need to refactor stringification APIs because it's unuseable as-is --- src/openssl.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 0a6c996..7d62796 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1194,16 +1194,26 @@ static int pk_toPEM(lua_State *L) { static int pk__tostring(lua_State *L) { EVP_PKEY *key = checksimple(L, 1, PUBKEY_CLASS); + int type = optencoding(L, 2, "pem", X509_PEM|X509_DER); BIO *bio = getbio(L); - char *pem; + char *data; long len; - int ok; + int ok = 0; - if (!PEM_write_bio_PUBKEY(bio, key)) - return throwssl(L, "pubkey:__tostring"); + switch (type) { + case X509_PEM: + if (!PEM_write_bio_PUBKEY(bio, key)) + return throwssl(L, "pubkey:__tostring"); + break; + case X509_DER: + if (!i2d_PUBKEY_bio(bio, key)) + return throwssl(L, "pubkey:__tostring"); + break; + } /* switch() */ - len = BIO_get_mem_data(bio, &pem); - lua_pushlstring(L, pem, len); + len = BIO_get_mem_data(bio, &data); + + lua_pushlstring(L, data, len); return 1; } /* pk__tostring() */ -- cgit v1.2.3-59-g8ed1b