static int openssl_xattr_data(lua_State*L) { X509_ATTRIBUTE* attr = CHECK_OBJECT(1, X509_ATTRIBUTE, "openssl.x509_attribute"); if (lua_type(L, 2) == LUA_TSTRING) { int attrtype = luaL_checkint(L, 2); size_t size; int ret; const char *data = luaL_checklstring(L, 3, &size); #if OPENSSL_VERSION_NUMBER < 0x10100000L if (X509_ATTRIBUTE_count(attr) == 1) ASN1_TYPE_free((ASN1_TYPE*)attr->value.ptr); else sk_ASN1_TYPE_pop_free(attr->value.set, ASN1_TYPE_free); attr->value.ptr = NULL; #else #endif ret = X509_ATTRIBUTE_set1_data(attr, attrtype, data, size); return openssl_pushresult(L, ret); } else { int idx = luaL_checkint(L, 2); int attrtype = luaL_checkint(L, 3); ASN1_STRING *as = (ASN1_STRING *)X509_ATTRIBUTE_get0_data(attr, idx, attrtype, NULL); PUSH_ASN1_STRING(L, as); return 1; } }
static int openssl_push_xname_entry(lua_State* L, X509_NAME_ENTRY* ne) { ASN1_OBJECT* object = X509_NAME_ENTRY_get_object(ne); lua_newtable(L); openssl_push_asn1object(L, object); PUSH_ASN1_STRING(L, X509_NAME_ENTRY_get_data(ne)); lua_settable(L, -3); return 1; }
/*** get index by give asn1_object or nid @function delete_entry @tparam integer location which name entry to delete @treturn[1] asn1_object object that delete name entry @treturn[1] asn1_string value that delete name entry @treturn[2] nil delete nothing */ static int openssl_xname_delete_entry(lua_State*L) { X509_NAME* xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name"); int loc = luaL_checkint(L, 2); X509_NAME_ENTRY *xe = X509_NAME_delete_entry(xn, loc); if (xe) { openssl_push_asn1object(L, X509_NAME_ENTRY_get_object(xe)); PUSH_ASN1_STRING(L, X509_NAME_ENTRY_get_data(xe)); X509_NAME_ENTRY_free(xe); return 2; } else lua_pushnil(L); return 1; };
static int openssl_push_xname_entry(lua_State* L, X509_NAME_ENTRY* ne, int obj) { ASN1_OBJECT* object = X509_NAME_ENTRY_get_object(ne); ASN1_STRING* value = X509_NAME_ENTRY_get_data(ne); lua_newtable(L); if(obj) { openssl_push_asn1object(L, object); PUSH_ASN1_STRING(L, value); } else { lua_pushstring(L, OBJ_nid2sn(OBJ_obj2nid(object))); lua_pushlstring(L, (const char*)ASN1_STRING_get0_data(value), ASN1_STRING_length(value)); } lua_settable(L, -3); return 1; }
static int openssl_xalgor_get(lua_State* L) { int type; void* val; ASN1_OBJECT *obj; X509_ALGOR* alg = CHECK_OBJECT(1, X509_ALGOR, "openssl.x509_algor"); X509_ALGOR_get0(&obj, &type, &val, alg); if (obj != NULL) { openssl_push_asn1object(L, obj); } else lua_pushnil(L); if (type == V_ASN1_UNDEF) lua_pushnil(L); else { PUSH_ASN1_STRING(L, val); } return 2; }
int openssl_push_general_name(lua_State*L, const GENERAL_NAME* general_name) { lua_newtable(L); switch (general_name->type) { case GEN_OTHERNAME: { OTHERNAME *otherName = general_name->d.otherName; lua_newtable(L); openssl_push_asn1object(L, otherName->type_id); PUSH_ASN1_STRING(L, otherName->value->value.asn1_string); lua_settable(L, -3); lua_setfield(L, -2, "otherName"); lua_pushstring(L, "otherName"); lua_setfield(L, -2, "type"); break; } case GEN_EMAIL: PUSH_ASN1_STRING(L, general_name->d.rfc822Name); lua_setfield(L, -2, "rfc822Name"); lua_pushstring(L, "rfc822Name"); lua_setfield(L, -2, "type"); break; case GEN_DNS: PUSH_ASN1_STRING(L, general_name->d.dNSName); lua_setfield(L, -2, "dNSName"); lua_pushstring(L, "dNSName"); lua_setfield(L, -2, "type"); break; case GEN_X400: openssl_push_asn1type(L, general_name->d.x400Address); lua_setfield(L, -2, "x400Address"); lua_pushstring(L, "x400Address"); lua_setfield(L, -2, "type"); break; case GEN_DIRNAME: { X509_NAME* xn = general_name->d.directoryName; openssl_push_xname_asobject(L, xn); lua_setfield(L, -2, "directoryName"); lua_pushstring(L, "directoryName"); lua_setfield(L, -2, "type"); } break; case GEN_URI: PUSH_ASN1_STRING(L, general_name->d.uniformResourceIdentifier); lua_setfield(L, -2, "uniformResourceIdentifier"); lua_pushstring(L, "uniformResourceIdentifier"); lua_setfield(L, -2, "type"); break; case GEN_IPADD: lua_newtable(L); PUSH_ASN1_OCTET_STRING(L, general_name->d.iPAddress); lua_setfield(L, -2, "iPAddress"); lua_pushstring(L, "iPAddress"); lua_setfield(L, -2, "type"); break; case GEN_EDIPARTY: lua_newtable(L); PUSH_ASN1_STRING(L, general_name->d.ediPartyName->nameAssigner); lua_setfield(L, -2, "nameAssigner"); PUSH_ASN1_STRING(L, general_name->d.ediPartyName->partyName); lua_setfield(L, -2, "partyName"); lua_setfield(L, -2, "ediPartyName"); lua_pushstring(L, "ediPartyName"); lua_setfield(L, -2, "type"); break; case GEN_RID: lua_newtable(L); openssl_push_asn1object(L, general_name->d.registeredID); lua_setfield(L, -2, "registeredID"); lua_pushstring(L, "registeredID"); lua_setfield(L, -2, "type"); break; default: lua_pushstring(L, "unsupport"); lua_setfield(L, -2, "type"); } return 1; };
int openssl_xext_totable(lua_State* L, X509_EXTENSION *x, int utf8) { lua_newtable(L); openssl_push_asn1object(L, x->object); lua_setfield(L, -2, "object"); PUSH_ASN1_OCTET_STRING(L, x->value); lua_setfield(L,-2, "value"); AUXILIAR_SET(L, -1, "critical", x->critical, boolean); switch (x->object->nid) { case NID_subject_alt_name: { int i; int n_general_names; STACK_OF(GENERAL_NAME) *values = X509V3_EXT_d2i(x); if (values == NULL) break; /* Push ret[oid] */ openssl_push_asn1object(L, x->object); lua_newtable(L); n_general_names = sk_GENERAL_NAME_num(values); for (i = 0; i < n_general_names; i++) { GENERAL_NAME *general_name = sk_GENERAL_NAME_value(values, i); switch (general_name->type) { case GEN_OTHERNAME: { OTHERNAME *otherName = general_name->d.otherName; lua_newtable(L); openssl_push_asn1object(L, otherName->type_id); PUSH_ASN1_STRING(L, otherName->value->value.asn1_string, utf8); lua_settable(L, -3); lua_setfield(L, -2, "otherName"); lua_pushstring(L, "otherName"); lua_rawseti(L, -2, i+1); break; } case GEN_EMAIL: lua_newtable(L); PUSH_ASN1_STRING(L, general_name->d.rfc822Name, utf8); lua_pushstring(L, "rfc822Name"); lua_settable(L, -3); lua_pushstring(L, "rfc822Name"); lua_rawseti(L, -2, i+1); break; case GEN_DNS: lua_newtable(L); PUSH_ASN1_STRING(L, general_name->d.dNSName, utf8); lua_setfield(L, -2, "dNSName"); lua_pushstring(L, "dNSName"); lua_rawseti(L, -2, i+1); break; case GEN_X400: lua_newtable(L); openssl_push_asn1type(L, general_name->d.x400Address); lua_setfield(L, -2, "x400Address"); lua_pushstring(L, "x400Address"); lua_rawseti(L, -2, i+1); break; case GEN_DIRNAME: { X509_NAME* xn = general_name->d.directoryName; lua_newtable(L); PUSH_OBJECT(X509_NAME_dup(xn), "openssl.x509_name"); lua_setfield(L, -2, "directoryName"); lua_pushstring(L, "directoryName"); lua_rawseti(L, -2, i+1); } break; case GEN_URI: lua_newtable(L); PUSH_ASN1_STRING(L, general_name->d.uniformResourceIdentifier, utf8); lua_setfield(L, -2, "uniformResourceIdentifier"); lua_pushstring(L, "uniformResourceIdentifier"); lua_rawseti(L, -2, i+1); break; case GEN_IPADD: lua_newtable(L); PUSH_ASN1_OCTET_STRING(L, general_name->d.iPAddress); lua_setfield(L, -2, "iPAddress"); lua_pushstring(L, "iPAddress"); lua_rawseti(L, -2, i+1); break; case GEN_EDIPARTY: lua_newtable(L); lua_newtable(L); PUSH_ASN1_STRING(L, general_name->d.ediPartyName->nameAssigner,utf8); lua_setfield(L, -2, "nameAssigner"); PUSH_ASN1_STRING(L, general_name->d.ediPartyName->partyName,utf8); lua_setfield(L, -2, "partyName"); lua_setfield(L, -2, "ediPartyName"); lua_pushstring(L, "ediPartyName"); lua_rawseti(L, -2, i+1); break; case GEN_RID: lua_newtable(L); openssl_push_asn1object(L, general_name->d.registeredID); lua_setfield(L, -2, "registeredID"); lua_pushstring(L, "registeredID"); lua_rawseti(L, -2, i+1); break; } } lua_settable(L, -3); } default: break; } return 1; };