summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-07-04 15:44:54 +0300
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2014-07-04 15:47:42 +0300
commitab27845444a153c8a9c402fab947e8997fc9d2da (patch)
tree4706e8e3cd448867471215c78dafd899c58bc30c /src
parentad5867b4c5c85ac4ade3cd2d4884ade0e1fae5a5 (diff)
downloadluaossl-ab27845444a153c8a9c402fab947e8997fc9d2da.tar.gz
luaossl-ab27845444a153c8a9c402fab947e8997fc9d2da.tar.bz2
luaossl-ab27845444a153c8a9c402fab947e8997fc9d2da.zip
text form conversion for certificates and CRLs
Diffstat (limited to 'src')
-rw-r--r--src/openssl.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/openssl.c b/src/openssl.c
index c589d6c..c0d93dd 100644
--- a/src/openssl.c
+++ b/src/openssl.c
@@ -2589,6 +2589,66 @@ static int xc_sign(lua_State *L) {
2589} /* xc_sign() */ 2589} /* xc_sign() */
2590 2590
2591 2591
2592static int xc_text(lua_State *L) {
2593 static const struct { const char *kw; unsigned int flag; } map[] = {
2594 { "no_header", X509_FLAG_NO_HEADER },
2595 { "no_version", X509_FLAG_NO_VERSION },
2596 { "no_serial", X509_FLAG_NO_SERIAL },
2597 { "no_signame", X509_FLAG_NO_SIGNAME },
2598 { "no_validity", X509_FLAG_NO_VALIDITY },
2599 { "no_subject", X509_FLAG_NO_SUBJECT },
2600 { "no_issuer", X509_FLAG_NO_ISSUER },
2601 { "no_pubkey", X509_FLAG_NO_PUBKEY },
2602 { "no_extensions", X509_FLAG_NO_EXTENSIONS },
2603 { "no_sigdump", X509_FLAG_NO_SIGDUMP },
2604 { "no_aux", X509_FLAG_NO_AUX },
2605 { "no_attributes", X509_FLAG_NO_ATTRIBUTES },
2606 { "ext_default", X509V3_EXT_DEFAULT },
2607 { "ext_error", X509V3_EXT_ERROR_UNKNOWN },
2608 { "ext_parse", X509V3_EXT_PARSE_UNKNOWN },
2609 { "ext_dump", X509V3_EXT_DUMP_UNKNOWN }
2610 };
2611
2612 lua_settop(L, 2);
2613
2614 X509 *crt = checksimple(L, 1, X509_CERT_CLASS);
2615
2616 unsigned int flags = 0;
2617 const char *kw;
2618 int found;
2619 unsigned int i;
2620
2621 BIO *bio = getbio(L);
2622 char *data;
2623 long len;
2624
2625 if (!lua_isnil(L, 2)) {
2626 lua_pushnil(L);
2627 while (lua_next(L, 2)) {
2628 kw = luaL_checkstring(L, -1);
2629 found = 0;
2630 for (i = 0; i < countof(map); i++)
2631 if (!strcmp(kw, map[i].kw)) {
2632 flags |= map[i].flag;
2633 found = 1;
2634 }
2635 if (!found)
2636 luaL_argerror(L, 2, lua_pushfstring(L, "invalid flag: %s", kw));
2637 lua_pop(L, 1);
2638 }
2639 }
2640
2641 if (!X509_print_ex(bio, crt, 0, flags))
2642 return throwssl(L, "x509.cert:text");
2643
2644 len = BIO_get_mem_data(bio, &data);
2645
2646 lua_pushlstring(L, data, len);
2647
2648 return 1;
2649} /* xc_text() */
2650
2651
2592static int xc__tostring(lua_State *L) { 2652static int xc__tostring(lua_State *L) {
2593 X509 *crt = checksimple(L, 1, X509_CERT_CLASS); 2653 X509 *crt = checksimple(L, 1, X509_CERT_CLASS);
2594 int type = optencoding(L, 2, "pem", X509_PEM|X509_DER); 2654 int type = optencoding(L, 2, "pem", X509_PEM|X509_DER);
@@ -2655,6 +2715,7 @@ static const luaL_Reg xc_methods[] = {
2655 { "getPublicKey", &xc_getPublicKey }, 2715 { "getPublicKey", &xc_getPublicKey },
2656 { "setPublicKey", &xc_setPublicKey }, 2716 { "setPublicKey", &xc_setPublicKey },
2657 { "sign", &xc_sign }, 2717 { "sign", &xc_sign },
2718 { "text", &xc_text },
2658 { "tostring", &xc__tostring }, 2719 { "tostring", &xc__tostring },
2659 { NULL, NULL }, 2720 { NULL, NULL },
2660}; 2721};
@@ -3103,6 +3164,24 @@ static int xx_sign(lua_State *L) {
3103} /* xx_sign() */ 3164} /* xx_sign() */
3104 3165
3105 3166
3167static int xx_text(lua_State *L) {
3168 X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS);
3169
3170 BIO *bio = getbio(L);
3171 char *data;
3172 long len;
3173
3174 if (!X509_CRL_print(bio, crl))
3175 return throwssl(L, "x509.crl:text");
3176
3177 len = BIO_get_mem_data(bio, &data);
3178
3179 lua_pushlstring(L, data, len);
3180
3181 return 1;
3182} /* xx_text() */
3183
3184
3106static int xx__tostring(lua_State *L) { 3185static int xx__tostring(lua_State *L) {
3107 X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS); 3186 X509_CRL *crl = checksimple(L, 1, X509_CRL_CLASS);
3108 int type = optencoding(L, 2, "pem", X509_PEM|X509_DER); 3187 int type = optencoding(L, 2, "pem", X509_PEM|X509_DER);
@@ -3149,6 +3228,7 @@ static const luaL_Reg xx_methods[] = {
3149 { "setIssuer", &xx_setIssuer }, 3228 { "setIssuer", &xx_setIssuer },
3150 { "add", &xx_add }, 3229 { "add", &xx_add },
3151 { "sign", &xx_sign }, 3230 { "sign", &xx_sign },
3231 { "text", &xx_text },
3152 { "tostring", &xx__tostring }, 3232 { "tostring", &xx__tostring },
3153 { NULL, NULL }, 3233 { NULL, NULL },
3154}; 3234};