亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
国产精品久久一卡二卡| 成人高清视频在线观看| 欧美巨大另类极品videosbest| 亚洲色图在线视频| 一本大道久久a久久综合| 亚洲乱码精品一二三四区日韩在线| 色综合视频在线观看| 一区二区三区日韩欧美| 欧美日韩国产高清一区二区| 日本视频中文字幕一区二区三区| 日韩欧美一级二级三级| 国产美女主播视频一区| 亚洲国产精华液网站w| av电影天堂一区二区在线观看| 亚洲特黄一级片| 欧美精品aⅴ在线视频| 久久国产精品色| 国产精品久久午夜夜伦鲁鲁| 91精品办公室少妇高潮对白| 天天免费综合色| 国产日韩欧美精品综合| 91视视频在线观看入口直接观看www| 亚洲综合在线视频| 日韩精品一区二区三区蜜臀 | 1区2区3区国产精品| 欧美中文字幕一区二区三区 | 国产精品一色哟哟哟| 亚洲图片激情小说| 91精品麻豆日日躁夜夜躁| 国产福利一区在线观看| 亚洲高清免费一级二级三级| 欧美精品一区二区三区四区| 在线观看日韩国产| 黑人巨大精品欧美一区| 一区二区三区不卡在线观看 | 国产精品毛片久久久久久久| 精品视频一区三区九区| 国产精品亚洲第一| 亚洲不卡av一区二区三区| 久久亚洲综合色| 欧美日韩一区二区三区四区五区| 风流少妇一区二区| 日韩精品乱码免费| 亚洲女同一区二区| 国产欧美一区二区精品性色超碰 | 一区二区三区在线免费观看| 精品粉嫩超白一线天av| 色久优优欧美色久优优| 国产精品一区二区在线观看网站| 五月天亚洲婷婷| 最新日韩在线视频| 久久亚区不卡日本| 欧美一级免费观看| 在线视频欧美精品| 福利电影一区二区三区| 九九视频精品免费| 日韩av电影免费观看高清完整版 | 亚洲成人第一页| 亚洲特级片在线| 中文字幕一区二区三区不卡在线| 日韩一级片在线观看| 欧美日韩亚洲丝袜制服| 99精品欧美一区二区三区小说| 国产又粗又猛又爽又黄91精品| 日本女人一区二区三区| 午夜精品123| 亚洲国产日韩综合久久精品| 亚洲精选免费视频| 自拍偷拍欧美激情| 国产精品久线观看视频| 国产精品理论在线观看| 亚洲国产精品成人综合色在线婷婷| 亚洲精品一线二线三线无人区| 欧美电视剧免费观看| 91精品福利在线一区二区三区| 欧美日韩你懂得| 欧美日本一道本| 91精品国产日韩91久久久久久| 7777精品伊人久久久大香线蕉的 | 亚洲精品午夜久久久| 亚洲精品成人悠悠色影视| 亚洲日本va在线观看| 最新不卡av在线| 亚洲欧美激情一区二区| 一区二区三区在线免费| 亚洲综合网站在线观看| 亚洲一区二区偷拍精品| 亚洲国产精品一区二区www在线 | 欧美国产成人精品| 欧美国产日韩亚洲一区| 亚洲欧洲国产专区| 亚洲综合免费观看高清完整版 | 成人美女视频在线观看| jvid福利写真一区二区三区| 91麻豆国产精品久久| 在线观看一区二区视频| 91精品啪在线观看国产60岁| 欧美xfplay| 国产精品色哟哟网站| 一区二区三区资源| 麻豆成人免费电影| 国产成人精品免费| 色老综合老女人久久久| 欧美日韩情趣电影| 久久久久国产一区二区三区四区| 国产精品麻豆视频| 午夜影院久久久| 国产成人午夜99999| 色激情天天射综合网| 日韩精品一区二区三区视频在线观看| 亚洲精品乱码久久久久久久久| 亚洲一区二区三区美女| 裸体健美xxxx欧美裸体表演| 成人高清在线视频| 欧美欧美午夜aⅴ在线观看| 欧美电影免费观看高清完整版在| 国产精品情趣视频| 三级一区在线视频先锋| 国产成人高清在线| 欧美午夜电影在线播放| 久久影音资源网| 午夜欧美大尺度福利影院在线看| 国产一区二区三区免费看| 色婷婷综合久久久中文字幕| 欧美精品一区二区高清在线观看 | 精品久久久久久最新网址| 国产精品视频免费| 热久久久久久久| 色噜噜偷拍精品综合在线| 欧美成人精品1314www| 亚洲欧洲av在线| 国产一区三区三区| 欧美男人的天堂一二区| 国产精品麻豆欧美日韩ww| 伦理电影国产精品| 欧美日韩精品是欧美日韩精品| 国产女人aaa级久久久级 | 岛国一区二区在线观看| 91麻豆精品国产91久久久资源速度| 国产精品网站一区| 久久精品国产99| 欧美午夜片在线观看| 中文天堂在线一区| 国精产品一区一区三区mba视频 | 成人手机电影网| 日韩一区二区免费视频| 亚洲国产视频网站| av综合在线播放| 国产亚洲精品超碰| 麻豆精品精品国产自在97香蕉 | 日韩你懂的电影在线观看| 亚洲欧美成aⅴ人在线观看| 成人高清伦理免费影院在线观看| 精品国产91亚洲一区二区三区婷婷| 性欧美大战久久久久久久久| 欧美综合久久久| 玉足女爽爽91| 色婷婷综合五月| 有码一区二区三区| 日本高清视频一区二区| 亚洲日本va午夜在线影院| 成人手机电影网| 国产精品久久久久久久久动漫| 国产精品性做久久久久久| 欧美成人性战久久| 国产在线日韩欧美| 久久蜜桃一区二区| 国产在线一区观看| 久久亚洲综合色| 国产精品99久久久久久久女警| www日韩大片| 国产成人在线影院 | 精品少妇一区二区三区视频免付费 | 亚洲欧洲99久久| 91麻豆国产自产在线观看| 亚洲欧美综合色| 欧美在线观看视频一区二区三区| 18欧美亚洲精品| 91久久一区二区| 午夜一区二区三区在线观看| 91.com在线观看| 免费一级片91| 久久这里只有精品视频网| 国产成人在线视频网址| 国产精品久久久久aaaa| 色综合亚洲欧洲| 日韩中文字幕1| 久久影院视频免费| 91一区在线观看| 亚洲午夜国产一区99re久久| 欧美二区在线观看| 久久不见久久见中文字幕免费| 精品国产免费人成电影在线观看四季 | 国产麻豆精品视频| 亚洲天堂a在线| 日韩欧美在线123| 粉嫩av一区二区三区在线播放| 日韩理论电影院| 日韩视频免费观看高清完整版 | 久久久影视传媒|