int X509_NAME_get_index_by_NID(X509_NAME *name, int nid, int lastpos) { ASN1_OBJECT *obj; obj=OBJ_nid2obj(nid); if (obj == NULL) return(-2); return(X509_NAME_get_index_by_OBJ(name,obj,lastpos)); }
int X509_NAME_get_text_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, char *buf, int len) { int i; ASN1_STRING *data; i=X509_NAME_get_index_by_OBJ(name,obj,-1); if (i < 0) return(-1); data=X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name,i)); i=(data->length > (len-1))?(len-1):data->length; if (buf == NULL) return(data->length); memcpy(buf,data->data,i); buf[i]='\0'; return(i); }
/*** get text by given asn1_object or nid @function get_text @tparam string|integer|asn1_object identid for asn1_object @tparam[opt=-1] number lastpos retrieve the next index after lastpos @treturn string text and followed by lastpos */ static int openssl_xname_get_text(lua_State*L) { X509_NAME* xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name"); ASN1_OBJECT *obj = openssl_get_asn1object(L, 2, 0); int lastpos = luaL_optint(L, 3, -1); X509_NAME_ENTRY *e; ASN1_STRING *s; lastpos = X509_NAME_get_index_by_OBJ(xn, obj, lastpos); ASN1_OBJECT_free(obj); if (lastpos == -1) return 0; e = X509_NAME_get_entry(xn, lastpos); s = X509_NAME_ENTRY_get_data(e); lua_pushlstring(L, (const char *)ASN1_STRING_get0_data(s), ASN1_STRING_length(s)); lua_pushinteger(L, lastpos); return 2; };
void add_assoc_name_entry(lua_State*L, char * key, X509_NAME * name, int shortname) /* {{{ */ { int i, j = -1, last = -1, obj_cnt = 0; char *sname; int nid; X509_NAME_ENTRY * ne; ASN1_STRING * str = NULL; ASN1_OBJECT * obj; char* p; lua_newtable(L); p=X509_NAME_oneline(name,NULL,0); lua_pushstring(L, p); lua_rawseti(L, -2, 0); OPENSSL_free(p); for (i = 0; i < X509_NAME_entry_count(name); i++) { unsigned char *to_add; int to_add_len; int tindex = 0; //int utf8 = 0; ne = X509_NAME_get_entry(name, i); obj = X509_NAME_ENTRY_get_object(ne); nid = OBJ_obj2nid(obj); obj_cnt = 0; if (shortname) { sname = (char *) OBJ_nid2sn(nid); } else { sname = (char *) OBJ_nid2ln(nid); } lua_newtable(L); last = -1; for (;;) { j = X509_NAME_get_index_by_OBJ(name, obj, last); if (j < 0) { if (last != -1) break; } else { obj_cnt++; ne = X509_NAME_get_entry(name, j); str = X509_NAME_ENTRY_get_data(ne); /* Some Certificate not stardand if (ASN1_STRING_type(str) != V_ASN1_UTF8STRING) { to_add_len = ASN1_STRING_to_UTF8(&to_add, str); } */ to_add = ASN1_STRING_data(str); to_add_len = ASN1_STRING_length(str); tindex++; lua_pushlstring(L,(char *)to_add, to_add_len); lua_rawseti(L,-2,tindex); } last = j; } i = last; if (obj_cnt > 1) { lua_setfield(L,-2,sname); } else { lua_pop(L,1); if (obj_cnt && str && to_add_len > -1) { lua_pushlstring(L,(char *)to_add, to_add_len); lua_setfield(L,-2, sname); } } } if (key != NULL) { lua_setfield(L,-2,key); } }
static bool IsNameObjPresent(X509_NAME *dn, ASN1_OBJECT *obj) { return X509_NAME_get_index_by_OBJ(dn, obj, -1) >= 0; }