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