Skip to content

Commit efa1845

Browse files
committed
add methods on csr to deal with attributes
getAttribute(name, startpos=-1) => (first attribute with that name after startpos) and pos as multiple values getAttributeTypes() => list of all the attribute names addAttribute(name, values, type = MBSTRING_ASC) values is an array type is one of MBSTRING_ASC, MBSTRING_UTF8 etc only stringish types are implemented
1 parent 7c7e3f4 commit efa1845

1 file changed

Lines changed: 135 additions & 1 deletion

File tree

src/openssl.c

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7827,6 +7827,137 @@ static int xr_modifyRequestedExtension(X509_REQ *csr, int target_nid, int crit,
78277827
} /* xr_modifyRequestedExtension() */
78287828

78297829

7830+
static int xr_getAttribute(lua_State *L) {
7831+
X509_REQ *csr = checksimple(L, 1, X509_CSR_CLASS);
7832+
const char *attr_name = luaL_checkstring(L, 2);
7833+
int nid = OBJ_txt2nid(attr_name);
7834+
int lastpos = luaL_optinteger(L, 3, -1);
7835+
const char *err;
7836+
7837+
if(nid==0)
7838+
return luaL_error(L, "no oid for attribute '%s'", attr_name);
7839+
7840+
7841+
int index = X509_REQ_get_attr_by_NID(csr, nid, lastpos);
7842+
X509_ATTRIBUTE *a = X509_REQ_get_attr(csr, index);
7843+
ASN1_OBJECT *aobj = X509_ATTRIBUTE_get0_object(a);
7844+
7845+
int val_count = X509_ATTRIBUTE_count(a);
7846+
ASN1_BIT_STRING *bs = NULL;
7847+
7848+
if (val_count == 0)
7849+
return luaL_error(L, "x509_r_invalid_attributes");
7850+
7851+
lua_createtable(L, val_count, 0);
7852+
7853+
for(int i=0; i < val_count; i++) {
7854+
ASN1_TYPE *at = X509_ATTRIBUTE_get0_type(a, i);
7855+
int type = at->type;
7856+
bs = at->value.asn1_string;
7857+
7858+
switch (type) {
7859+
case V_ASN1_PRINTABLESTRING:
7860+
case V_ASN1_T61STRING:
7861+
case V_ASN1_NUMERICSTRING:
7862+
case V_ASN1_UTF8STRING:
7863+
case V_ASN1_IA5STRING:
7864+
lua_pushlstring(L, (char *)bs->data, bs->length);
7865+
break;
7866+
default:
7867+
lua_pushnil(L);
7868+
break;
7869+
}
7870+
lua_seti(L, -2, i + 1);
7871+
}
7872+
7873+
lua_pushinteger(L, index);
7874+
return 2;
7875+
}
7876+
7877+
static int xr_getAttributeTypes(lua_State *L) {
7878+
X509_REQ *csr = checksimple(L, 1, X509_CSR_CLASS);
7879+
7880+
int buf_len = 80;
7881+
char * buf = 0;
7882+
char * err = 0;
7883+
int attr_count = X509_REQ_get_attr_count(csr);
7884+
7885+
buf = malloc(buf_len);
7886+
7887+
lua_createtable(L, attr_count, 0);
7888+
int table = lua_gettop(L);
7889+
7890+
for (int i = 0; i < attr_count; i++) {
7891+
X509_ATTRIBUTE *a;
7892+
ASN1_BIT_STRING *bs = NULL;
7893+
ASN1_OBJECT *aobj;
7894+
int name_len, val_count = 1;
7895+
7896+
a = X509_REQ_get_attr(csr, i);
7897+
aobj = X509_ATTRIBUTE_get0_object(a);
7898+
7899+
name_len = OBJ_obj2txt(buf, buf_len, aobj, 0);
7900+
if(name_len <= 0) continue;
7901+
if(name_len >= buf_len) {
7902+
buf_len = name_len;
7903+
buf = realloc(buf, buf_len);
7904+
OBJ_obj2txt(buf, buf_len, aobj, 0);
7905+
}
7906+
lua_pushnumber(L, i + 1);
7907+
lua_pushlstring(L, buf, name_len);
7908+
lua_settable(L, table);
7909+
}
7910+
if(buf) free(buf);
7911+
return 1;
7912+
7913+
failed:
7914+
if(buf) free(buf);
7915+
return luaL_error(L, "%s", err);
7916+
}
7917+
7918+
static int xr_addAttribute(lua_State *L) {
7919+
X509_REQ *csr = checksimple(L, 1, X509_CSR_CLASS);
7920+
const char *attr_name = luaL_checkstring(L, 2);
7921+
int num_values;
7922+
unsigned long val_type = luaL_optinteger(L, 4, MBSTRING_ASC);
7923+
7924+
lua_len(L, 3);
7925+
num_values = lua_tointeger(L, -1);
7926+
7927+
int nid = OBJ_txt2nid(attr_name);
7928+
if(nid==0) return 0;
7929+
7930+
X509_ATTRIBUTE *attr = X509_ATTRIBUTE_new();
7931+
if(!attr)
7932+
return luaL_error(L, "X509_ATTRIBUTE_new failed");
7933+
7934+
if(!X509_ATTRIBUTE_set1_object(attr, OBJ_txt2obj(attr_name, 0)))
7935+
return luaL_error(L, "X509_ATTRIBUTE_set1_object failed");
7936+
7937+
for(int i = 1; i <= num_values; i++) {
7938+
lua_geti(L, 3, i);
7939+
size_t data_len = 0;
7940+
char *data = lua_tolstring(L, -1, &data_len);
7941+
if(! X509_ATTRIBUTE_set1_data(attr, val_type, data, data_len))
7942+
return luaL_error(L, "X509_ATTRIBUTE_set1_data failed");
7943+
}
7944+
7945+
if(! X509_REQ_add1_attr(csr, attr))
7946+
return luaL_error(L, "X509_REQ_add1_attr failed");
7947+
7948+
lua_pushboolean(L, 1);
7949+
return 1;
7950+
}
7951+
7952+
static int xr_deleteAttribute(lua_State *L) {
7953+
X509_REQ *csr = checksimple(L, 1, X509_CSR_CLASS);
7954+
int index = luaL_checkinteger(L, 2);
7955+
7956+
lua_pushboolean(L, !! X509_REQ_delete_attr(csr, index));
7957+
return 1;
7958+
}
7959+
7960+
78307961
static int xr_setSubjectAlt(lua_State *L) {
78317962
X509_REQ *csr = checksimple(L, 1, X509_CSR_CLASS);
78327963
GENERAL_NAMES *gens = checksimple(L, 2, X509_GENS_CLASS);
@@ -8031,6 +8162,10 @@ static const auxL_Reg xr_methods[] = {
80318162
{ "setSubject", &xr_setSubject },
80328163
{ "getPublicKey", &xr_getPublicKey },
80338164
{ "setPublicKey", &xr_setPublicKey },
8165+
{ "getAttributeTypes", &xr_getAttributeTypes },
8166+
{ "getAttribute", &xr_getAttribute },
8167+
{ "addAttribute", &xr_addAttribute },
8168+
{ "deleteAttribute", &xr_deleteAttribute },
80348169
{ "getSubjectAlt", &xr_getSubjectAlt },
80358170
{ "setSubjectAlt", &xr_setSubjectAlt },
80368171
{ "getRequestedExtension", &xr_getRequestedExtension },
@@ -13242,4 +13377,3 @@ static void initall(lua_State *L) {
1324213377
}
1324313378
lua_pop(L, 1);
1324413379
} /* initall() */
13245-

0 commit comments

Comments
 (0)