Skip to content

Commit ac641bf

Browse files
committed
src/openssl.c: Add getAttributes method on x509.csr
returns a table of attribute name as string => [val1 val2 .... valn]
1 parent 8e9622c commit ac641bf

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/openssl.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7818,6 +7818,76 @@ static int xr_modifyRequestedExtension(X509_REQ *csr, int target_nid, int crit,
78187818
return 1;
78197819
} /* xr_modifyRequestedExtension() */
78207820

7821+
static int xr_getAttributes(lua_State *L) {
7822+
X509_REQ *csr = checksimple(L, 1, X509_CSR_CLASS);
7823+
7824+
int buf_len = 80;
7825+
char * buf = 0;
7826+
char * err = 0;
7827+
int attr_count = X509_REQ_get_attr_count(csr);
7828+
7829+
buf = malloc(buf_len);
7830+
7831+
lua_createtable(L, attr_count, 0);
7832+
int table = lua_gettop(L);
7833+
7834+
for (int i = 0; i < attr_count; i++) {
7835+
X509_ATTRIBUTE *a;
7836+
ASN1_BIT_STRING *bs = NULL;
7837+
ASN1_OBJECT *aobj;
7838+
int name_len, val_count = 1;
7839+
7840+
a = X509_REQ_get_attr(csr, i);
7841+
aobj = X509_ATTRIBUTE_get0_object(a);
7842+
7843+
name_len = OBJ_obj2txt(buf, buf_len, aobj, 0);
7844+
if(name_len <= 0) continue;
7845+
if(name_len >= buf_len) {
7846+
buf_len = name_len;
7847+
buf = realloc(buf, buf_len);
7848+
OBJ_obj2txt(buf, buf_len, aobj, 0);
7849+
}
7850+
lua_pushlstring(L, buf, name_len);
7851+
7852+
val_count = X509_ATTRIBUTE_count(a);
7853+
if (val_count == 0) {
7854+
err = "x509_r_invalid_attributes"; goto failed;
7855+
}
7856+
7857+
lua_createtable(L, val_count, 0);
7858+
7859+
for(int ii=0; ii < val_count; ii++) {
7860+
ASN1_TYPE *at = X509_ATTRIBUTE_get0_type(a, ii);
7861+
int type = at->type;
7862+
bs = at->value.asn1_string;
7863+
7864+
switch (type) {
7865+
case V_ASN1_PRINTABLESTRING:
7866+
case V_ASN1_T61STRING:
7867+
case V_ASN1_NUMERICSTRING:
7868+
case V_ASN1_UTF8STRING:
7869+
case V_ASN1_IA5STRING:
7870+
lua_pushlstring(L, (char *)bs->data, bs->length);
7871+
break;
7872+
default:
7873+
lua_pushnil(L);
7874+
break;
7875+
}
7876+
lua_seti(L, -2, ii + 1);
7877+
}
7878+
lua_settable(L, table);
7879+
}
7880+
if(buf) free(buf);
7881+
return 1;
7882+
7883+
failed:
7884+
if(buf) free(buf);
7885+
lua_pushnil(L);
7886+
lua_pushstring(L, err);
7887+
return 2;
7888+
}
7889+
7890+
78217891

78227892
static int xr_setSubjectAlt(lua_State *L) {
78237893
X509_REQ *csr = checksimple(L, 1, X509_CSR_CLASS);
@@ -8023,6 +8093,7 @@ static const auxL_Reg xr_methods[] = {
80238093
{ "setSubject", &xr_setSubject },
80248094
{ "getPublicKey", &xr_getPublicKey },
80258095
{ "setPublicKey", &xr_setPublicKey },
8096+
{ "getAttributes", &xr_getAttributes },
80268097
{ "getSubjectAlt", &xr_getSubjectAlt },
80278098
{ "setSubjectAlt", &xr_setSubjectAlt },
80288099
{ "getRequestedExtension", &xr_getRequestedExtension },

0 commit comments

Comments
 (0)