亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? certmng.cpp

?? 實現證書的生成和驗證
?? CPP
?? 第 1 頁 / 共 4 頁
字號:
#include "stdafx.h"
#include "CertMng.h"

using namespace std;

CCertMng * CCertMng::m_pMng = 0;
CRWLock CCertMng::m_Cert_Lock;
volatile bool CCertMng::m_bCert_Locked = false;

CCertMng::CCertMng()
{
	m_pVoid              = 0;
	m_lpfnGenKey         = 0;
	m_lpfnPass_Callback  = 0;
	m_lpfnGet_Dn_Value   = 0;
}

int CCertMng::Initialize ( conv_tool::OPERATORS_MAP mapOpr,
						  LPGENERATEKEY pfnGenKey,
						  LPPASSWORD_CALLBACK pfnPass,
						  LPGET_DN_VALUE  pfnDnValue,
						  LPVERIFY_CALLBACK pfnVerify,
						  void * pVoid)
{
	m_pVoid               = pVoid;
	m_lpfnGenKey          = pfnGenKey;
	m_lpfnPass_Callback   = pfnPass;
	m_lpfnGet_Dn_Value    = pfnDnValue;
	m_lpfnVerify_Callback = pfnVerify;

	conv_tool::OPERATORS_MAP::iterator it = mapOpr.find ( RAND_FILE );
	if ( it != mapOpr.end () )
	{
		m_szRandFile = it->second;
	}
	if ( ( it = mapOpr.find ( CONF_FILE ) ) != mapOpr.end () )
		m_szConfFile = it->second;

	return 0;
}

void CCertMng::Uninitialize ()
{
}

int CCertMng::Generate_Key ( conv_tool::OPERATORS_MAP mapOpt, xmlChar ** ppszXML, int *pnLen )
{
	RSA *              rsa         = NULL;
	const EVP_CIPHER * enc         = NULL;
	unsigned long      exp         = RSA_F4;
	BIO *              bio_out     = NULL;
	int                ret         = -1;
	int                nSize       = 1024;
	int                nCipher     = RSA_CIPHER;
	int                nOut        = FILE_TYPE;
	std::string        szKeyFile;

	conv_tool::OPERATORS_MAP           mapRet;
	conv_tool::OPERATORS_MAP::iterator it;

	it = mapOpt.find ( KEY_LENGTH );
	if ( it != mapOpt.end () )
		nSize = atoi ( it->second.c_str () );

	it = mapOpt.find ( CIPHER_TYPE );
	if ( it != mapOpt.end () )
		nCipher = atoi ( it->second.c_str() );

	it = mapOpt.find ( OUT_TYPE );
	if ( it != mapOpt.end () )
		nOut = atoi ( it->second.c_str() );

	it = mapOpt.find ( KEY );
	if ( it != mapOpt.end () )
		szKeyFile = it->second;

	if ( nOut == FILE_TYPE && szKeyFile.length() == 0 )
	{
		CError::Interface ()->Handle_Error ( 0, "參數(輸出文件名)不能為NULL" );
		return -1;
	}

	switch ( nCipher )
	{
	case RSA_CIPHER:
		break;
	case DES_CIPHER:
		enc = EVP_des_cbc ();
		break;
	case DES3_CIPHER:
		enc = EVP_des_ede3_cbc ();
		break;
	default:
		break;
	}

	//生成輸出BIO
	if ( ( bio_out = Bio_Write ( nOut, szKeyFile.c_str() ) ) == NULL )
	{
		CError::Interface ()->Handle_Error ( 0, "生成密鑰文件失敗:%s", szKeyFile.c_str() );
		goto end;
	}

	CRandMng::Interface ()->Rand_Load_File ( m_szRandFile );
	rsa = RSA_generate_key ( nSize, exp, Process_Key, this );
	CRandMng::Interface()->Rand_Write_File ( m_szRandFile );
	if ( rsa == NULL )
		CError::Interface ()->Handle_Error ( 0, "生成密鑰失敗(返回值=NULL)" );
	else
	{
		if ( ! PEM_write_bio_RSAPrivateKey ( bio_out, rsa, enc, NULL, 0, ( pem_password_cb *)password_callback, this) )
			CError::Interface ()->Handle_Error ( 0, "寫密鑰信息失敗" );
		else
		{
			if ( nOut != FILE_TYPE )
			{
				std::string sout_put;
				int  nLen = 0;
				char chLen[127];

				if ( Get_BioStr ( bio_out, sout_put, &nLen ) < 0 )
				{
					CError::Interface()->Handle_Error( 0, "獲取輸出內容失敗");
					goto end;
				}
				itoa (  nLen, chLen, 10 );
				mapRet.insert ( std::make_pair ( std::string ( KEY ), sout_put ) );
				mapRet.insert ( std::make_pair ( std::string ( KEY_LENGTH ), std::string ( chLen ) ) );
			}
			ret = 0; //返回值,成功
		}
	}
	if ( conv_tool::Map2XML ( mapRet, ppszXML, pnLen ) < 0 )
	{
		CError::Interface()->Handle_Error ( 0, "轉換XML失敗" );
		ret = -1;
	}
end:
	if (rsa != NULL)
		RSA_free ( rsa );
	if (bio_out != NULL)
		BIO_free_all ( bio_out );
	
	return ret;
}
// 生成證書憑證
int CCertMng::Generate_Req ( conv_tool::OPERATORS_MAP mapOpt,
							 xmlChar ** ppszXML, int * pnLen )
{
	int            ret       = -1;
	char     *     p         = 0;
	X509     *     x509      = 0;
	X509_REQ *     x509_req  = 0;
	EVP_PKEY *     pKey      = 0;
	int            nKey_Type = TYPE_RSA;
	int            nKey_Fmt  = FORMAT_PEM;
	int            nInFmt    = FORMAT_PEM;
	int            nOutFmt   = FORMAT_PEM;
	int            nDays     = 30;
	long           nErrLine  = 0;
	int            out_type  = FILE_TYPE;
	int            in_type   = FILE_TYPE;
	ASN1_INTEGER * pSerial   = 0;
	CONF     *     req_conf  = NULL;
	BIO      *     bio_in    = 0;
	string         szKeyFile, szOutFile, szInFile, szExt, szReqExt;
	bool           pub_key   = 0, new_req = 0, if_x509 = 0, asn_kludge = false, verify = false;
	unsigned long  nCharset  = MBSTRING_UTF8;
	const EVP_MD * md_alg    = NULL, * md_digest = EVP_md5();

	conv_tool::OPERATORS_MAP::iterator it;

	if ( ( it = mapOpt.find ( IN_FORMAT ) ) != mapOpt.end () )
		nInFmt = atoi ( it->second.c_str() );
	if ( ( it = mapOpt.find ( OUT_FORMAT ) ) != mapOpt.end () )
		nOutFmt = atoi ( it->second.c_str() );
	if ( ( it = mapOpt.find ( KEY ) ) != mapOpt.end () )
		szKeyFile = it->second;
	if ( ( it = mapOpt.find ( IF_PUBKEY ) ) != mapOpt.end () )
		pub_key = atoi ( it->second.c_str() );
	if ( ( it = mapOpt.find ( IF_NEWREQ ) ) != mapOpt.end () )
		new_req = atoi ( it->second.c_str() );
	if ( ( it = mapOpt.find ( KEY_FORMAT ) ) != mapOpt.end () )
		nKey_Fmt = atoi ( it->second.c_str() );
	if ( ( it = mapOpt.find ( OUT_FILE ) ) != mapOpt.end () )
		szOutFile = it->second;
	if ( ( it = mapOpt.find ( IN_FILE ) ) != mapOpt.end () )
		szInFile = it->second;
	if ( ( it = mapOpt.find ( EXTENTIONS ) ) != mapOpt.end () )
		szExt = it->second;
	if ( ( it = mapOpt.find ( REQ_EXT ) ) != mapOpt.end () )
		szReqExt = it->second;
	if ( ( it = mapOpt.find ( CHAR_SET ) ) != mapOpt.end () )
		nCharset = atoi ( it->second.c_str() );
	if ( ( it = mapOpt.find ( IF_X509 ) ) != mapOpt.end () )
		if_x509 = atoi ( it->second.c_str() );
	if ( ( it = mapOpt.find ( DAYS ) ) != mapOpt.end () )
		nDays = atoi ( it->second.c_str() );
	if ( ( it = mapOpt.find ( SERIAL ) ) != mapOpt.end () )
		pSerial = s2i_ASN1_INTEGER ( NULL, (char*)it->second.c_str() );
	if ( ( it = mapOpt.find ( MD_DIGEST ) ) != mapOpt.end () )
		md_alg = EVP_get_digestbyname ( (char*)it->second.c_str() );
	if ( md_alg )
		md_digest = md_alg;
	if ( ( it = mapOpt.find ( ASN_KLUDGE ) ) != mapOpt.end () )
		asn_kludge = atoi ( it->second.c_str() );
	if ( ( it = mapOpt.find ( IF_VERIFY ) ) != mapOpt.end () )
		verify = atoi ( it->second.c_str() );

	if ( ( it = mapOpt.find ( OUT_TYPE ) ) != mapOpt.end () )
		out_type = atoi ( it->second.c_str() );

	if ( ( it = mapOpt.find ( IN_TYPE ) ) != mapOpt.end () )
		in_type = atoi ( it->second.c_str() );
	

	if ( m_szConfFile.length () == 0 )
	{
		CError::Interface ()->Handle_Error ( 0, "缺少配置文件" );
		goto end;
	}
	if ( szKeyFile.length () == 0 )
	{
		CError::Interface ()->Handle_Error ( 0, "缺少Key文件" );
		goto end;
	}
	req_conf = NCONF_new ( NULL );
	if ( NCONF_load ( req_conf, m_szConfFile.c_str(), &nErrLine ) == 0 )
	{
		CError::Interface()->Handle_Error ( 0, "讀取配置文件失敗:%s:%d", m_szConfFile.c_str(), nErrLine );
		goto end;
	}

	if ( Load_Config ( req_conf ) < 0 )
		goto end;
	p = NCONF_get_string ( req_conf, NULL, OID_FILE );
	if ( p != NULL)
	{
		BIO * bio_oid;
		bio_oid = BIO_new_file ( p, "r" );
		if ( bio_oid )
		{
			OBJ_create_objects ( bio_oid );
			BIO_free ( bio_oid );
		}
	}
	if ( Add_Oid_Section ( req_conf ) < 0 ) goto end;
	if ( md_alg == NULL)
	{
		p = NCONF_get_string ( req_conf, REQ_SECTION, DEFAULT_MD );
		if ( p != NULL )
		{
			if ( ( md_alg = EVP_get_digestbyname ( p ) ) != NULL )
				md_digest = md_alg;
		}
	}
	if ( szExt.length () == 0 )
	{
		p = NCONF_get_string ( req_conf, REQ_SECTION, V3_EXTENSIONS);
		if ( p )
			szExt = p;
	}
	if ( szExt.length () > 0 )
	{
		X509V3_CTX ctx;
		X509V3_set_ctx_test ( &ctx );
		X509V3_set_nconf ( &ctx, req_conf );
		if ( ! X509V3_EXT_add_nconf ( req_conf, &ctx, (char*)szExt.c_str(), NULL ) )
		{
			CError::Interface()->Handle_Error ( 0, "讀取:%s失敗%s", V3_EXTENSIONS, szExt.c_str() );
			goto end;
		}
	}
	p = NCONF_get_string ( req_conf, REQ_SECTION, STRING_MASK );
	if ( p && ! ASN1_STRING_set_default_mask_asc ( p ) )
	{
		CError::Interface()->Handle_Error ( 0, "不正確的字符串標識:%s", p);
		goto end;
	}
	if ( nCharset != MBSTRING_UTF8 )
	{
		p = NCONF_get_string ( req_conf, REQ_SECTION, UTF8_IN );
		if ( p )
		{
			if ( ! strcmp ( p, "yes" ) )
				nCharset = MBSTRING_UTF8;
		}
	}
	if ( szReqExt.length() == 0 ) 
	{
		p = NCONF_get_string ( req_conf, REQ_SECTION, REQ_EXTENSIONS );
		if ( p ) szReqExt = p;
	}
	if ( szReqExt.length() > 0 )
	{
		X509V3_CTX ctx;
		X509V3_set_ctx_test ( &ctx );
		X509V3_set_nconf ( &ctx, req_conf );
		if ( !X509V3_EXT_add_nconf ( req_conf, &ctx, (char*)szReqExt.c_str(), NULL ) )
		{
			CError::Interface ()->Handle_Error ( 0, "讀取%s失敗%s", REQ_EXTENSIONS, REQ_SECTION );
			goto end;
		}
	}
	
	if ( szKeyFile.length() > 0 )
	{
		pKey = Load_Key ( (char*)szKeyFile.c_str(), szKeyFile.length(), nKey_Fmt, in_type );
		if ( pKey == NULL ) goto end;
		if ( EVP_PKEY_type ( pKey->type ) == EVP_PKEY_DSA )
		{
			CRandMng::Interface ()->Rand_Load_File ( m_szRandFile );
		}
	}
	if ( ! new_req )
	{
		if ( szInFile.length() == 0 )
		{
			CError::Interface ()->Handle_Error ( 0, "沒有證書文件" );
			goto end;
		}
		//如果輸入為文件格式,則從文件中讀取
		//讀取KEY
		if ( ( bio_in = Bio_Read ( in_type, szInFile.c_str(), szInFile.length () ) ) == NULL )
		{
			CError::Interface ()->Handle_Error ( 0, "讀取證書文件失敗:%s", szInFile.c_str() );
			goto end;
		}
		
		// 根據輸入格式生成X509憑證
		switch ( nInFmt )
		{
		case FORMAT_ASN1:
			x509_req = d2i_X509_REQ_bio ( bio_in, NULL );
			break;
		case FORMAT_PEM:
			x509_req = PEM_read_bio_X509_REQ ( bio_in, NULL, NULL, NULL );
			break;
		default:
			CError::Interface ()->Handle_Error ( 0, "證書格式不正確:%d,%s", nInFmt, szInFile.c_str() );
			goto end;
			break;
		}
		if ( x509_req == NULL )
		{
			CError::Interface ()->Handle_Error ( 0, "讀取x509 證書文件失敗:%s", szInFile.c_str() );
			goto end;
		}
	}
	// 判斷是否要新建憑證,并且是否需要自簽名
	if ( new_req || if_x509 )
	{
		if ( pKey == NULL )
		{
			CError::Interface()->Handle_Error ( 0, "密鑰文件不存在" );
			goto end;
		}
		if ( x509_req == NULL )
		{
			if ( ( x509_req = X509_REQ_new() ) == NULL ) goto end;
			ret = Make_Request ( req_conf, x509_req, pKey, !if_x509, nCharset );
			if ( asn_kludge && ! sk_X509_ATTRIBUTE_num ( x509_req->req_info->attributes ) )
			{
				sk_X509_ATTRIBUTE_free ( x509_req->req_info->attributes );
				x509_req->req_info->attributes = 0;
			}
			if ( ret == -1 ) goto end;
		}
		// 如果要自簽名時進行下面操作
		if ( if_x509 )
		{
			if ( ( x509 = X509_new ( ) ) == NULL )
			{
				CError::Interface ()->Handle_Error ( 0, "函數X509_new出錯(指針為NULL)" );
				goto end;
			}
			if ( -1 == Sign_Request ( req_conf, x509, x509_req, pKey, szExt.c_str(),
				pSerial, nDays, md_digest ) )
				goto end;
		}
		else
		{
			X509V3_CTX ext_ctx;
			X509V3_set_ctx ( &ext_ctx, NULL, NULL, x509_req, NULL, 0 );
			X509V3_set_nconf ( &ext_ctx, req_conf );

			if ( ( szReqExt.length() > 0 ) &&
				! X509V3_EXT_REQ_add_nconf ( req_conf, &ext_ctx, (char*)szReqExt.c_str(), x509_req ) )
			{
				CError::Interface()->Handle_Error ( 0, "增加擴展屬性失敗:%s", szReqExt.c_str() );
				goto end;
			}
			if ( ! X509_REQ_sign ( x509_req, pKey, md_digest ) )
				goto end;
		}
	}
	// 如果沒有自簽名,并且需要證書驗證時,進行下面操作
	if ( verify && ! if_x509 )
	{
		if ( Verify_Request ( x509_req, pKey ) == -1 )
			goto end;
	}
	if ( Out_Request ( x509, x509_req, out_type, szOutFile.c_str(),
		if_x509, pub_key, nOutFmt, ppszXML, pnLen ) == -1 )
		goto end;

	ret = 0;
end:
	if ( req_conf != NULL ) NCONF_free(req_conf);
	if ( bio_in ) BIO_free ( bio_in );
	if ( pKey ) EVP_PKEY_free ( pKey );
	if ( x509_req )	X509_REQ_free ( x509_req );
	if ( x509 ) X509_free ( x509 );
	if ( pSerial ) ASN1_INTEGER_free ( pSerial );
	OBJ_cleanup();
	return ret;
}
//輸出憑證
int CCertMng::Out_Request ( X509 * x509, X509_REQ * req,
						   int out_type, const char *out,
						   bool is_x509, bool is_pub_key, int out_fmt,
						   xmlChar ** ppszXML, int * pnLen )
{
	BIO *     bio_out = 0;
	BUF_MEM * mem_out = 0;

	int  ret    = -1;
	bool result = false;

	conv_tool::OPERATORS_MAP mapret;

	if ( ( bio_out = Bio_Write ( out_type, out) ) == NULL )
		goto end;

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲免费高清视频在线| 91美女片黄在线观看91美女| 成人免费视频国产在线观看| 欧美视频自拍偷拍| 国产女主播在线一区二区| 日韩成人一级片| 色视频欧美一区二区三区| 国产亚洲精品久| 美腿丝袜亚洲综合| 欧美日韩一区二区三区四区五区| 国产欧美日韩中文久久| 青青草国产精品亚洲专区无| 91黄视频在线| 中文字幕在线不卡一区| 国模少妇一区二区三区| 4438x亚洲最大成人网| 午夜精品久久久久| 欧美日韩久久久久久| 中文字幕在线不卡国产视频| 国产伦精品一区二区三区免费迷| 欧美一级视频精品观看| 亚洲一区二区在线免费看| 一本一道久久a久久精品| 欧美韩日一区二区三区四区| 国产精品性做久久久久久| 欧美v日韩v国产v| 麻豆精品视频在线| 欧美v国产在线一区二区三区| 免费高清不卡av| 日韩亚洲欧美在线| 久久97超碰色| 久久精品男人的天堂| 国产精品123区| 欧美激情综合五月色丁香 | 蜜桃91丨九色丨蝌蚪91桃色| 欧美日韩久久一区二区| 热久久久久久久| 精品女同一区二区| 国产成人在线视频网址| 国产精品三级在线观看| 91在线porny国产在线看| 综合激情成人伊人| 欧美性一级生活| 午夜久久久久久| 欧美成人午夜电影| 极品销魂美女一区二区三区| 久久久久久黄色| 97精品久久久久中文字幕| 亚洲最新视频在线观看| 8v天堂国产在线一区二区| 麻豆国产欧美一区二区三区| 久久午夜色播影院免费高清| www.成人在线| 五月激情六月综合| 久久综合色鬼综合色| 99久久久免费精品国产一区二区| 亚洲综合在线视频| 欧美一级黄色片| 国产毛片精品一区| 亚洲乱码国产乱码精品精的特点| 欧美精品在欧美一区二区少妇| 免费av成人在线| 日本一区二区三区dvd视频在线| 99综合影院在线| 热久久国产精品| 亚洲欧洲精品一区二区三区| 欧美日韩成人综合在线一区二区| 国内一区二区视频| 一区二区三区四区五区视频在线观看| 欧美日韩国产综合视频在线观看| 国产激情视频一区二区三区欧美| 亚洲国产精品久久久男人的天堂| 久久蜜臀精品av| 欧美精品丝袜中出| av亚洲精华国产精华| 秋霞成人午夜伦在线观看| 国产精品电影一区二区| 欧美一级免费观看| 99视频热这里只有精品免费| 久久99精品久久久久久动态图 | 青娱乐精品视频| 亚洲欧美在线aaa| 精品国产免费一区二区三区四区 | 日韩视频一区二区三区| 91免费在线视频观看| 国产一区在线观看麻豆| 午夜久久久久久电影| 亚洲日本在线a| 国产欧美一区二区在线| 日韩欧美国产小视频| 欧美日韩情趣电影| 91热门视频在线观看| 成人免费福利片| 国产激情偷乱视频一区二区三区| 日韩激情在线观看| 亚洲亚洲人成综合网络| 亚洲日本成人在线观看| 亚洲国产成人午夜在线一区| 精品国产一区a| 精品久久久久久久久久久久久久久 | 99综合影院在线| 成人黄色片在线观看| 国产一区二区调教| 精品一二三四区| 美女脱光内衣内裤视频久久网站| 亚欧色一区w666天堂| 亚洲福利视频一区二区| 亚洲一卡二卡三卡四卡无卡久久| 亚洲欧洲成人自拍| 国产精品久久久久久久久免费相片 | 精东粉嫩av免费一区二区三区| 一区二区三国产精华液| 日韩理论片中文av| 亚洲视频免费观看| 亚洲精品国产无天堂网2021| 国产精品超碰97尤物18| 欧美国产成人在线| 中文字幕欧美日本乱码一线二线| 国产欧美久久久精品影院| 欧美精品一区二| 国产偷v国产偷v亚洲高清| 精品成人a区在线观看| 久久久亚洲高清| 日本一区二区三区免费乱视频| 欧美激情一区二区三区蜜桃视频 | 久久精品视频网| 中文字幕免费不卡在线| 亚洲天堂福利av| 一片黄亚洲嫩模| 亚洲成va人在线观看| 日韩av一级片| 国产成人精品三级| av电影一区二区| 欧美日韩卡一卡二| 精品成人在线观看| 国产精品久久久久久久久快鸭| 樱桃国产成人精品视频| 日本aⅴ免费视频一区二区三区| 九九在线精品视频| 成人精品小蝌蚪| 欧美日韩一级二级三级| 精品国产凹凸成av人网站| 国产精品免费看片| 午夜成人免费电影| 国产不卡视频在线播放| 欧美在线综合视频| 精品日韩在线观看| 亚洲免费三区一区二区| 日本亚洲欧美天堂免费| 福利一区二区在线| 欧美精品自拍偷拍动漫精品| 欧美国产精品久久| 日本视频一区二区| 不卡在线观看av| 日韩亚洲电影在线| 亚洲日本va午夜在线电影| 久久精品国产精品亚洲精品| 日韩三级免费观看| 亚洲同性gay激情无套| 丝袜美腿亚洲色图| 久久草av在线| 99vv1com这只有精品| 欧美精品在线观看一区二区| 中文字幕一区在线| 激情图区综合网| 欧美日韩高清一区二区| 国产欧美日韩精品一区| 另类小说图片综合网| 在线中文字幕不卡| 国产人妖乱国产精品人妖| 三级成人在线视频| 91成人看片片| 一区在线播放视频| 国产91丝袜在线播放九色| 欧美一区二区三区影视| 亚洲一区二区三区四区在线观看 | 中文字幕人成不卡一区| 精品一区二区综合| 欧美一级精品大片| 午夜精品久久久久久久99樱桃| 99久久er热在这里只有精品15| 欧美一级搡bbbb搡bbbb| 亚洲成在人线在线播放| 99热国产精品| 中文字幕欧美三区| 国产一区福利在线| 精品国产伦理网| 蜜臀a∨国产成人精品| 欧美另类高清zo欧美| 一区二区三区在线视频播放| 91丨九色丨尤物| 亚洲青青青在线视频| 99麻豆久久久国产精品免费优播| 国产精品美女久久久久久2018| 国产精品1024| 国产精品毛片久久久久久| 丁香网亚洲国际| 国产精品国产成人国产三级| 成人自拍视频在线观看| 国产精品萝li|