//**************************************************************************************//
// 																						
// ÀÌ ¿¹Á¦´Â C Library °¡ ¾î¶»°Ô »ç¿ëµÇ¾î¾ßÇÏ´ÂÁö ±× Èå¸§À» ActiveX ·Î Ç¥ÇöÇÑ°ÍÀÔ´Ï´Ù.		
// ½ÇÁ¦ »ç¿ëµÇ´Â ActiveX ´Â °¢ »ç°¡ È¸»ç¿¡ ¸Â°Ô ÀÛ¼ºÇÏ¼Å¼­ »ç¿ëÇÏ½Ã¸é µË´Ï´Ù.				
// TradeSign Àº Library ¸¸À» Áö¿øÇÕ´Ï´Ù.													
// 																						
//**************************************************************************************//

//** ¼Ò½º º¸±â ÁÖÀÇ»çÇ× *****************************************************************//
// 																						
// 1. ActiveX ÀÇ ÇÔ¼ö Áß AxTS_ ·Î ½ÃÀÛÇÏ´Â ¸ðµç ÇÔ¼ö´Â 									
//		Ax ¶ó´Â Á¢µÎ¾î¸¦ »« ÇÔ¼öÀÌ¸§°ú µ¿ÀÏÇÑ C Library ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù.				
//		¿¹) ActiveX ÀÇ AxTS_SignData(...) Àº ³»ºÎ¿¡¼­ TS_SignData(...) ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù.	
// 2. ActiveX ÀÇ ÇÔ¼ö Áß AxTS_ ·Î ½ÃÀÛÇÏ´Â ¸ðµç ÇÔ¼ö´Â 									
//		ÀÔÃâ·ÂµÇ´Â µ¥ÀÌÅ¸ÀÇ Base64 Encoding, Decoding ¿Ü¿¡¼­ ¾Æ¹«·± µ¿ÀÛµµ ÇÏÁö ¾Ê´Â´Ù. 
// 3. ActiveX ÀÇ ÇÔ¼ö Áß AxTS_ ·Î ½ÃÀÛÇÏ´Â ¸ðµç ÇÔ¼ö´Â									
//		Á¢µÎ¾î¸¦ »« ÇÔ¼ö ÀÌ¸§ÀÇ C Library ÇÔ¼ö¿Í µ¿ÀÏÇÑ Input °ú OutPut À» °®Áö ¾Ê´Â´Ù.	
//		ÀÌ Â÷ÀÌ´Â C Library ´Â Binary Çü µ¥ÀÌÅ¸¿¡ ´ëÇØ¼­ µ¥ÀÌÅÍ¿Í ±æÀÌ¸¦ °°ÀÌ ÀÔ·Â¹Þ´Âµ¥,
//		ActiveX ´Â Binary Çü µ¥ÀÌÅ¸¸¦ Base64 Encoding ÇÏ¿© String À¸·Î Ã³¸®ÇÏ±â ¶§¹®ÀÌ´Ù.
//		¿¹) ActiveX ÀÇ AxTS_SignData() ÀÇ Input ÆÄ¶ó¹ÌÅÍ´Â 3°³.							
//			C Library ÀÇ TS_SignData() ÀÇ Input ÆÄ¶ó¹ÌÅÍ´Â 4°³. 						
// 4. ActiveX ÀÇ ¸ðµç ÇÔ¼ö´Â OutPut ÆÄ¸®¹ÌÅÍ¸¦ Property ¸¦ ÀÌ¿ëÇØ¼­ ¾ò´Â´Ù.				
//		¿¹) myKeyPwd = AxTSAPI2.OutData;												
// 																						
//**************************************************************************************//

//** È¯°æ ¼³Á¤½Ã ÁÖÀÇ»çÇ× ***************************************************************	//
// 																						
// 1. ¼­¹ö(»ó´ë¹æ)ÀÇ ¼­¸í¿ë ¹× Å°ºÐ¹è¿ë ÀÎÁõ¼­¸¦ ¼³Á¤ÇÑ´Ù.									
// 2. ´ëÄªÅ° ¾Ë°í¸®Áò°ú ¸ðµå¸¦ ¼³Á¤ÇÑ´Ù. 													
// 																						
//**************************************************************************************//

//** È¯°æ ¼³Á¤				************************************************************//
// DER Çü½ÄÀÇ ¼­¹ö(»ó´ë¹æ) ÀÎÁõ¼­µéÀ» Base64 Encoding ÇÏ¿© ¼³Á¤ÇÑ´Ù. 
// ÀüÀÚºÀÅõ°¡ »ç¿ëµÇ´Â ¸ðµç °÷¿¡¼­ »ç¿ëµÈ´Ù. Envelop, SignAndEnvelop, KeyExchange
// kmCert °¡ ¾ø´Â °æ¿ì¿¡ »ç¿ëÇÏ±â À§ÇØ¼­ ¼­¸í¿ë ÀÎÁõ¼­µµ ¼³Á¤ÇÑ´Ù. 
var svrKMCert = "MIIEHTCCA4agAwIBAgIEPpZ4uzANBgkqhkiG9w0BAQUFADBMMQswCQYDVQQGEwJLUjESMBAGA1UEChMJVHJhZGVTaWduMRMwEQYDVQQLEwpMaWNlbnNlZENBMRQwEgYDVQQLEwtUcmFkZVNpZ25DQTAeFw0wNTA3MDYwMzQxNDdaFw0wNjA3MDYwNDExNDdaMGExCzAJBgNVBAYTAktSMRIwEAYDVQQKEwlUcmFkZVNpZ24xEzARBgNVBAsTCkxpY2Vuc2VkQ0ExDjAMBgNVBAsTBUtUTkVUMRkwFwYDVQQDDBDthYzsiqTtirgoS1RORVQpMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC1Ojmob3bnIsZ3PlJD0iKwZ7fLagZuAc77f48IMR2/enU2pBQoXTY7pE6IJ4+xPy/rlmnHT5L6+qUXXF22EDDYsQ3SJzksoWMiW4iaKsBFGA+Ek7PvZE391aACk0YBB8XuHamQSvsWqJZQNL0u4ePZtsD7gP1WpJoC80HIuPKorwIDAQABo4IB9TCCAfEwHwYDVR0jBBgwFoAUzmcWRLJ+c/2Fp80NHtPw06UtJjkwHQYDVR0OBBYEFAnWJTbwP8XMF8mBC3voRwtHyf1iMA4GA1UdDwEB/wQEAwIAIDCBgwYDVR0gBHwwejAKBggqgxqMmkQCATBsBgkqgxqMmkwBAQQwXzAuBggrBgEFBQcCAjAiHiDHdAAgx3jJncEcspQAIKz1x3jHeMmdwRwAIMeFssiy5DAtBggrBgEFBQcCARYhaHR0cDovL3d3dy50cmFkZXNpZ24ubmV0L2Nwcy5odG1sMIHTBgNVHR8EgcswgcgwZ6BloGOkYTBfMQswCQYDVQQGEwJLUjESMBAGA1UEChMJVHJhZGVTaWduMRMwEQYDVQQLEwpMaWNlbnNlZENBMRQwEgYDVQQLEwtUcmFkZVNpZ25DQTERMA8GA1UEAxMIY3JsMWRwMjUwXaBboFmGV2xkYXA6Ly9sZGFwLnRyYWRlc2lnbi5uZXQ6Mzg5L2NuPWNybDFkcDI1LG91PVRyYWRlU2lnbkNBLG91PUxpY2Vuc2VkQ0Esbz1UcmFkZVNpZ24sYz1LUjBDBggrBgEFBQcBAQQ3MDUwMwYIKwYBBQUHMAGGJ2h0dHA6Ly9vY3NwLnRyYWRlc2lnbi5uZXQ6ODAvT0NTUFNlcnZlcjANBgkqhkiG9w0BAQUFAAOBgQAAd9+MzLuX/B5sJ9Qni7wz2+VJEyENeQPujrHJ4itHlEEZ2tSIAbGmorv2PqlPhp1NlywjqP8XOAgK9wEyKxoeYGFfIwSFcPqjLYR5kr4VlWYAy3ene11x926LXSsbkQw5Jj8vEdO1l1LG/tdNDWXTYX+MEAzLJpeM7xMS+HQ+mg==";

var svrSignCert = "MIIFETCCA/mgAwIBAgIEWWgzXjANBgkqhkiG9w0BAQUFADBOMQswCQYDVQQGEwJLUjESMBAGA1UECgwJVHJhZGVTaWduMRUwEwYDVQQLDAxBY2NyZWRpdGVkQ0ExFDASBgNVBAMMC1RyYWRlU2lnbkNBMB4XDTA2MDIwODA4NDU1M1oXDTA3MDIwODA5MTU1M1owdDELMAkGA1UEBhMCS1IxEjAQBgNVBAoMCVRyYWRlU2lnbjEVMBMGA1UECwwMQWNjcmVkaXRlZENBMQ0wCwYDVQQLDARURVNUMSswKQYDVQQDDCIzMjgw7YWM7Iqk7Yq4X+yduOymneyEnF8wMDAwMDUwNDM4MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD1wPnxNQsI/vV6j9lNXEcCn82z3hPTu/1ip+7xrhTebK3uPaQqdVBF5HBhEln880ei2YQ5JzCpuTdRardRwZ0IhPveY9fiUpClfuzAdSQqB5WNwT4kqh3XTdqM5E6qirmw+x0V6olJhY37OyJplhBly4Hqc7fn0bfItmqMyDZzcQIDAQABo4ICUzCCAk8wgY8GA1UdIwSBhzCBhIAUK3YCroJcfe6BkZ71iVu54plbqa+haKRmMGQxCzAJBgNVBAYTAktSMQ0wCwYDVQQKDARLSVNBMS4wLAYDVQQLDCVLb3JlYSBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eSBDZW50cmFsMRYwFAYDVQQDDA1LSVNBIFJvb3RDQSAxggInZDAdBgNVHQ4EFgQUT+bg1wMTWD7dutWkhA5ULATZX3cwDgYDVR0PAQH/BAQDAgbAMHoGA1UdIAEB/wRwMG4wbAYJKoMajJpMAQEDMF8wLgYIKwYBBQUHAgIwIh4gx3QAIMd4yZ3BHLKUACCs9cd4x3jJncEcACDHhbLIsuQwLQYIKwYBBQUHAgEWIWh0dHA6Ly93d3cudHJhZGVzaWduLm5ldC9jcHMuaHRtbDBmBgNVHREEXzBdoFsGCSqDGoyaRAoBAaBOMEwMFzMyODDthYzsiqTtirhf7J247Kad7IScMDEwLwYKKoMajJpECgEBATAhMAcGBSsOAwIaoBYEFGruj6KNaKj8V6VVXg7sRF4GFS/MMGMGA1UdHwRcMFowWKBWoFSGUmxkYXA6Ly9sZGFwLnRyYWRlc2lnbi5uZXQ6Mzg5L2NuPWNybDFkcDEsb3U9Y3JsZHAsb3U9QWNjcmVkaXRlZENBLG89VHJhZGVTaWduLGM9S1IwQwYIKwYBBQUHAQEENzA1MDMGCCsGAQUFBzABhidodHRwOi8vb2NzcC50cmFkZXNpZ24ubmV0OjgwL09DU1BTZXJ2ZXIwDQYJKoZIhvcNAQEFBQADggEBAFiwinu7tfpeExstxwcP3IohaY9rreczayxJ0FJj6usOv/8Tn+uK9BZnkNQ2vbAKwDWctYe+pSs3mZZscHGztZ3M6YLK2mdrts4U/EVMw2hAepzSkXels1RALerEeWutcUio2750FvPeWhCSh1NzCMAZCdYWR/lTBcP37V53eoGzwUjjnex04TFFFIGB8dIp3lC8PREr4CusFD/baAhWhwzajlAgytd4i2oaMIEe+m46JwJG3y8G0i25vZj5XQ5N0q9A7gqmOWGNcAzRsl0OoMOnrEBjImQQ338belIBLQHIWAEstxkwOSA5cRP7ha0XOdJCHTnPzIdri9DIl1yNu+Y=";

// Client ÀÚÃ¼ Å×½ºÆ®½Ã EnvelopVerify¸¦ À§ÇØ¼­ ÇÊ¿äÇÑ ºÎºÐ.	
var svrCertDirPath = "d://serverCert//";
var svrCertPwd = "12345678";

// ´ëÄªÅ° ¾Ë°í¸®Áò ¹× ¸ðµå
var SYSMMETRIC_ALGO = 2;	// 3 : SEED, 2 : 3DES
var SYSMMETRIC_MODE = 0;	// 0 : CBC

// ¼¼¼ÇÅ° °ü·Ã.
var csName = "TEST";		// »çÀÌÆ®¸í.
var nLifeTime = 0;			// ¼¼¼ÇÅ° À¯È¿±â°£. 1- 59 ºÐ. 0 : ÇÏ·çµ¿¾È ¹«Á¦ÇÑ.

// ÀÎÁõ¼­ ¼±ÅÃ °ü·Ã.
var selectCertType = 0;			// 0 : ÀÏ¹ÝÀûÀÎ ÀÎÁõ¼­ ¼±ÅÃ, 1 : ÀÔ·Â¹ÞÀº ºñ¹Ð¹øÈ£¸¦ ±âÁØÀ¸·Î °Ë»ö.
								// 1 Àº ·Î±×ÀÎ ¿¡¼­¸¸ »ç¿ëµÊ.
var certType = "111111";		// loginType ÀÌ 0 ÀÏ°æ¿ì »ç¿ë.
								// TradeSign, NCASign, CrossCert, SignGate, SignKorea, YesSign ¼ø¼­·Î Åä±Û. 
						
// **** »õ·Î Ãß°¡µÈ ºÎºÐ **** //
var CA_CERT_URL = "www.tradesign.net/cert/rootca.der:03|www.tradesign.net/cert/tradesign.der:2727|www.tradesign.net/cert/ncasign.der:273d|www.tradesign.net/cert/crosscert.der:2722|www.tradesign.net/cert/signgate.der:2748|www.tradesign.net/cert/signkorea.der:273b|www.tradesign.net/cert/yessign.der:2742|";
//var CA_CERT_URL = "www.rootca.or.kr/certs/rootca.der:03|www.rootca.or.kr/certs/tradesign.der:2727|www.rootca.or.kr/certs/ncasign.der:273d|www.rootca.or.kr/certs/CrossCert.der:2722|www.rootca.or.kr/certs/signgate.der:2748|www.rootca.or.kr/certs/signkorea.der:273b|www.rootca.or.kr/certs/yessign.der:2742|";
var CA_LDAP_INFO = "KISA:dirsys.rootca.or.kr:389|KICA:ldap.signgate.com:389|SignKorea:dir.signkorea.com:389|Yessign:203.233.91.35:389|CrossCert:dir.crosscert.com:389|TradeSign:ldap.tradesign.net:389|"

var crlCheckOption = 1;  	// 0 : CRL°ËÁõ¾ÈÇÔ, 1 : CRL °ËÁõ(±âº»), 2 : CRL °ËÁõ_Old CRL »ç¿ë(»ç¿ë¾ÈÇÔ)
var arlCheckOption = 0;  	// 0 : ARL°ËÁõ¾ÈÇÔ, 1 : ARL °ËÁõ(±âº»), 2 : ARL °ËÁõ_Old ARL »ç¿ë(»ç¿ë¾ÈÇÔ)
var storageType = 0;		// 0 : Hard, 	1 : Removable,	2 : SmartCard

var USING_CA_CERT_SIGN 		= 0;	// 0 : »óÀ§ÀÎÁõ¼­ Æ÷ÇÔ ¾ÈÇÔ.	1 : »óÀ§ÀÎÁõ¼­ Æ÷ÇÔ.
var USING_CRL_SIGN			= 0;	// 0 : CRL Æ÷ÇÔ ¾ÈÇÔ.		1 : CRL Æ÷ÇÔ.
var USING_SIGN_TIME_SIGN	= 1;	// 0 : ¼­¸í½Ã°£ Æ÷ÇÔ ¾ÈÇÔ. 	1 : ¼­¸í½Ã°£ Æ÷ÇÔ.

var CTLInfos	= "";
var InitPolMapInhibit	= 0;
var InitExplicitPol		= 0;
var InitAnyPolInhibit	= 0;
var Config1 = "1";
var Config2 = "1";
// **** »õ·Î Ãß°¡µÈ ºÎºÐ ³¡ **** //

// º¸°íÀÚÇÏ´Â ÀÎÁõ¼­ÀÇ Á¤Ã¥¸¸ ÀÔ·Â..
// ¸ðµç ÀÎÁõ¼­ º¸±â.
//var policies = "";
// ¹ýÀÎ »óÈ£¿¬µ¿¿ë ¹× TradeSign ÀüÀÚ¹«¿ª¿ë¸¸ º¸¿©ÁÜ.
//var policies = "1 2 410 200012 1 1 3|1 2 410 200012 1 1 4|1 2 410 200012 1 1 9|1 2 410 200012 1 1 10|1 2 410 200004 5 1 1 7|1 2 410 200005 1 1 5|1 2 410 200004 5 2 1 1|1 2 410 200004 5 2 1 4|1 2 410 200004 5 2 1 5|1 2 410 200004 5 4 1 2|";
// ¹ýÀÎ »óÈ£¿¬µ¿ ¹× TradeSign ¿ëµµÁ¦ÇÑ¿ë
//var policies = "1 2 410 200012 1 1 3|1 2 410 200012 1 1 4|1 2 410 200012 1 1 9|1 2 410 200012 1 1 10|1 2 410 200012 1 1 15|1 2 410 200012 1 1 16|1 2 410 200004 5 1 1 7|1 2 410 200005 1 1 5|1 2 410 200004 5 2 1 1|1 2 410 200004 5 2 1 4|1 2 410 200004 5 2 1 5|1 2 410 200004 5 4 1 2|";
// °³ÀÎ¿ë¸¸ º¸¿©ÁÜ.
//var policies = "1 2 410 200012 1 1 1|1 2 410 200012 1 1 2|1 2 410 200004 5 1 1 5|1 2 410 200005 1 1 1|1 2 410 200005 1 1 4|1 2 410 200004 5 2 1 11 2 410 200004 5 4 1 1|";

var policies = "1 2 410 200012 1 1 3|1 2 410 200012 1 1 4|1 2 410 200012 1 1 9|1 2 410 200012 1 1 10|1 2 410 200004 5 1 1 7|1 2 410 200005 1 1 5|1 2 410 200004 5 2 1 1|1 2 410 200004 5 2 1 4|1 2 410 200004 5 2 1 5|1 2 410 200004 5 4 1 2|1 2 410 200012 1 1 1|1 2 410 200012 1 1 2|1 2 410 200004 5 1 1 5|1 2 410 200005 1 1 1|1 2 410 200005 1 1 4|1 2 410 200004 5 2 1 1|1 2 410 200004 5 4 1 1|";

//**************************************************************************************//

//** Sign	(ÀüÀÚ¼­¸í)		************************************************************//
// 																			
// 1. ÀÚ½ÅÀÇ ÀÎÁõ¼­¸¦ ¼±ÅÃÇÏ¿© ¼­¸í¿ë ÀÎÁõ¼­¸¦ ÀÐ¾îµéÀÎ´Ù.					
// 2. ÀÐ¾îµé¾î ¼­¸í¿ë ÀÎÁõ¼­¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù. 						
// 3. ¼±ÅÃÇÑ ÀÚ½ÅÀÇ ¼­¸í¿ë ÀÎÁõ¼­ÀÇ °³ÀÎÅ°¸¦ ÀÐ¾îµéÀÎ´Ù.					
// 4. ÀÐ¾îµé¾î ¼­¸í¿ë ÀÎÁõ¼­ÀÇ °³ÀÎÅ°¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù.				
// 5. ¿ø¹®µ¥ÀÌÅ¸¿Í ¹Ì¸® ¼³Á¤µÈ ÀÎÁõ¼­¿Í °³ÀÎÅ°¸¦ ÀÌ¿ëÇÏ¿© ÀüÀÚ¼­¸íÀ» ÇÑ´Ù.	
// 																			
// ... ÇÊ¿äÇÑ °Í...															
// º»ÀÎÀÇ ¼­¸í¿ë ÀÎÁõ¼­¿Í ±× °³ÀÎÅ°.										
// 																			
//**************************************************************************************//
function AxSign(plainText) {
	// À§Ä¡ÇÒ µð·ºÅä¸®¸¦ ¸¸µê 
	var result, pVoid;
	
	// API ½ÃÀÛ.
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);
	
	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	// »ç¿ëÀÚ°¡ ÀÚ½ÅÀÇ ÀÎÁõ¼­¸¦ ¼±ÅÃ.
	var loginConf;
	var myCertDirPath, myCert, myPriKeyInfo;

	result = AxTSAPI2.Login(selectCertType, certType );
	if (result > 0)
	{
		alert(AxTSAPI2.ErrorMsg);
		return false;
	}
	else
	{
		myCertDirPath = AxTSAPI2.CertPath;
		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
		myCert = AxTSAPI2.Cert;
		myCertDN = AxTSAPI2.CertDN;
		alert(myCertDN);
		// ÀÐ¾îµéÀÎ Å°Áß ¾ÏÈ£È­ µÇÁö ¾ÊÀº Å° ¼³Á¤. - ¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignPriKey(pVoid, myPriKeyInfo );
		// ÀÐ¾îµéÀÎ ÀÎÁõ¼­ ¼³Á¤. - ¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignCert(pVoid, myCert);
	}
	
	// ¼­¸íÇÒ µ¥ÀÌÅ¸ °¡Á®¿À±â.
	//plainText = document.myform.data.value;	

	// ¼­¸íÇÏ±â.
	result = AxTSAPI2.AxTS_SignData(pVoid, "", plainText);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SignData Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// ¼­¸íµÈ µ¥ÀÌÅ¸ °¡Á®¿À±â.
		signMsg = AxTSAPI2.OutData;
		//alert(result + " : " + pVoid + "\r\nSignMsg : " + signMsg);
		
		//document.myform.data1.value = signMsg;
	}

	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		alert("Sign Success!!");
	}
	return signMsg;
}

//*************** Verify && ****************//
function AxVerify(signMsg) {
	var result, pVoid;
	// API ½ÃÀÛ 
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);
	
	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	// ¼­¸í¿ë µ¥ÀÌÅ¸ °ËÁõÇÏ±â.
	//signMsg = document.myform.data1.value;
	result = AxTSAPI2.AxTS_VerifySignature(pVoid, signMsg);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_VerifySignature Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// ¿ø¹® µ¥ÀÌÅ¸ °¡Á®¿À±â.
		verifyMsg = AxTSAPI2.OutData;
		loadedSign = AxTSAPI2.LoadedSign;
		//alert(result + " : " + pVoid + "\r\nverifyMsg : " + verifyMsg);
		//document.myform.data2.value = verifyMsg;
	}
	
	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		alert("Verify Success!!");
	}
	return verifyMsg;
}

//** Envelop 		****************************************************************//
// 																					
// 1. »ó´ë¹æ ÀÎÁõ¼­¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù.											
// 2. SingAndEnveloped Data »ý¼º½Ã ÇÊ¿äÇÑ ´ëÄªÅ° ¾Ë°í¸®ÁòÀ» ¼³Á¤ÇÑ´Ù. ´ëÄªÅ°¸¦ ¼³Á¤ÇÒ ¼öµµ ÀÖ´Ù.
// 3. Enveloped Data ¸¦ »ý¼ºÇÑ´Ù.			
// 																					
// ... ÇÊ¿äÇÑ °Í...																	
// »ó´ë¹æ(¼­¹ö) ÀÎÁõ¼­ (¾ÏÈ£¿ë, ¾ÏÈ£¿ëÀÌ ¾øÀ» ½Ã¿¡´Â ¼­¸í¿ë).		
// 																					
//**************************************************************************************//
function AxEnvelop(plainText) {
	// À§Ä¡ÇÒ µð·ºÅä¸®¸¦ ¸¸µê 
	var result, pVoid;
	// API ½ÃÀÛ.
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);
	
	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	// »ó´ëÆí (¼­¹ö)ÀÎÁõ¼­ ¼³Á¤ÇÏ±â.	
	AxTSAPI2.AxTS_SetPeerKMCert(pVoid, svrKMCert);
	AxTSAPI2.AxTS_SetPeerSignCert(pVoid, svrSignCert);
	
	// ¾Ë°í¸®Áò ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSymmetricAlgo(pVoid, SYSMMETRIC_ALGO, SYSMMETRIC_MODE);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}

	// envelop ÇÏ±â.
	var envelopMsg;
	result = AxTSAPI2.AxTS_EnvelopedData(pVoid, plainText);
	if (result > 0)
	{
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_EnvelopedData Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// envelop µÈ µ¥ÀÌÅ¸ °¡Á®¿À±â.
		envelopMsg = AxTSAPI2.OutData;
		//alert(result + " : " + pVoid + "\r\nEnvelopMsg : " + envelopMsg);
		//document.myform.data1.value = envelopMsg;
	}
	
	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		alert("Envelop Success!!");
	}
	return envelopMsg;
}

//*************** EnvelopVerify ****************//
function AxEnvelopVerify(envelopMsg) {
	// À§Ä¡ÇÒ µð·ºÅä¸®¸¦ ¸¸µê 
	var result, pVoid;
	// API ½ÃÀÛ.
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);

	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);
	
	var svrCertPath = svrCertDirPath + "kmCert.der";
	var svrKeyPath = svrCertDirPath + "kmPri.key";
	var svrKeyPwd = svrCertPwd;
	
	// ¾ÏÈ£¿ë °³ÀÎÅ° ÀÐ±â.
	result = AxTSAPI2.AxTS_ReadPrivateKeyInfo(pVoid, 0, svrKeyPath, svrKeyPwd);
	if (result > 0)
	{	
		var svrKeyPath = svrCertDirPath + "signPri.key";
		result = AxTSAPI2.AxTS_ReadPrivateKeyInfo(pVoid, 0, svrKeyPath, svrKeyPwd);
		{
	 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
			alert("AxTS_ReadPrivateKeyInfo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
			return false;
		}
		// ¼­¸í¿ë °³ÀÎÅ° °¡Á®¿À±â.
		myPriKey = AxTSAPI2.PriKey;
		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
		//alert(result + " : " + pVoid + "\r\nmyPriKey : " + myPriKey + "\r\nmyPriKeyInfo : " + myPriKeyInfo);
		// ¼­¸í¿ë °³ÀÎÅ° ¼³Á¤.
		AxTSAPI2.AxTS_SetMySignPriKey(pVoid, myPriKeyInfo);
	}
	else
	{
		// ¾ÏÈ£¿ë °³ÀÎÅ° °¡Á®¿À±â.
		myPriKey = AxTSAPI2.PriKey;
		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
		//alert(result + " : " + pVoid + "\r\nmyPriKey : " + myPriKey + "\r\nmyPriKeyInfo : " + myPriKeyInfo);
		// ¾ÏÈ£¿ë °³ÀÎÅ° ¼³Á¤.
		AxTSAPI2.AxTS_SetMyKMPriKey(pVoid, myPriKeyInfo);
	}
	
	// ¾ÏÈ£¿ë ÀÎÁõ¼­ ÀÐ±â.
	result = AxTSAPI2.AxTS_ReadCertificate(pVoid, 0, svrCertPath);
	if (result > 0)
	{	
		svrCertPath = svrCertDirPath + "signCert.der";
		result = AxTSAPI2.AxTS_ReadCertificate(pVoid, 0, svrCertPath);
		if (result > 0)
		{
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
			alert("AxTS_ReadCertificate Error : " + result + " : " + AxTSAPI2.ErrorMsg );
			return false;
		}
		// ÀÐ¾îµéÀÎ ÀÎÁõ¼­ °¡Á®¿À±â.
		myCert = AxTSAPI2.Cert;
		//alert(result + " : " + pVoid + "\r\nmyCert : " + myCert);
		// °¡Á®¿Â ÀÎÁõ¼­ ¼³Á¤ÇÏ±â.-¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignCert(pVoid, myCert);
	}
	else
	{
		// ¾ÏÈ£¿ë ÀÎÁõ¼­ °¡Á®¿À±â.
		myCert = AxTSAPI2.Cert;
		//alert(result + " : " + pVoid + "\r\nmyCert : " + myCert);
		// ¾ÏÈ£¿ë ÀÎÁõ¼­ ¼³Á¤.
		AxTSAPI2.AxTS_SetMyKMCert(pVoid, myCert);
	}
	
	// Develop
	result = AxTSAPI2.AxTS_DevelopedData(pVoid, "", envelopMsg);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_DevelopedData Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// ¿ø¹® µ¥ÀÌÅ¸ °¡Á®¿À±â.
		envelopVerifyMsg = AxTSAPI2.OutData;
		//alert(result + " : " + pVoid + "\r\nenvelopVerifyMsg : " + envelopVerifyMsg);
	}
	
	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		alert("EnvelopVerify Success!!");
	}
	return envelopVerifyMsg;
}


//** Sign And Envelop 	****************************************************************//
// 																					
// 1. ÀÚ½ÅÀÇ ÀÎÁõ¼­¸¦ ¼±ÅÃÇÑ´Ù. 					
// 2. ¼±ÅÃÇÑ ÀÚ½ÅÀÇ ¼­¸í¿ë ÀÎÁõ¼­ÀÇ °³ÀÎÅ°¸¦ ÀÐ¾îµéÀÎ´Ù.							
// 3. ÀÐ¾îµé¾î ¼­¸í¿ë ÀÎÁõ¼­ÀÇ °³ÀÎÅ°¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù.						
// 4. ¼±ÅÃÇÑ ÀÚ½ÅÀÇ ¼­¸í¿ë ÀÎÁõ¼­¸¦ ÀÐ¾îµéÀÎ´Ù.						
// 5. ÀÐ¾îµé¾î ¼­¸í¿ë ÀÎÁõ¼­¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù. 								
// 6. »ó´ë¹æ ÀÎÁõ¼­¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù.											
// 7. SingAndEnveloped Data »ý¼º½Ã ÇÊ¿äÇÑ ´ëÄªÅ° ¾Ë°í¸®ÁòÀ» ¼³Á¤ÇÑ´Ù. ´ëÄªÅ°¸¦ ¼³Á¤ÇÒ ¼öµµ ÀÖ´Ù.
// 8. SingAndEnveloped Data ¸¦ »ý¼ºÇÑ´Ù.			
// 																					
// ... ÇÊ¿äÇÑ °Í...																	
// º»ÀÎÀÇ ¼­¸í¿ë ÀÎÁõ¼­¿Í ±× °³ÀÎÅ°.												
// »ó´ë¹æ(¼­¹ö) ÀÎÁõ¼­ (¾ÏÈ£¿ë, ¾ÏÈ£¿ëÀÌ ¾øÀ» ½Ã¿¡´Â ¼­¸í¿ë).		
// 																					
//**************************************************************************************//
function AxSignnEnvelop(plainText) {
	var result, pVoid;
	// API ½ÃÀÛ
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);
	
	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	// »ç¿ëÀÚ°¡ ÀÚ½ÅÀÇ ÀÎÁõ¼­¸¦ ¼±ÅÃ.
	var myCertDirPath, myCert, myPriKeyInfo;
	result = AxTSAPI2.Login(selectCertType, certType );
	if (result > 0)
	{
		alert(AxTSAPI2.ErrorMsg);
		return false;
	}
	else
	{
		myCertDirPath = AxTSAPI2.CertPath;
		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
		myCert = AxTSAPI2.Cert;
		// ÀÐ¾îµéÀÎ Å°Áß ¾ÏÈ£È­ µÇÁö ¾ÊÀº Å° ¼³Á¤. - ¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignPriKey(pVoid, myPriKeyInfo );
		// ÀÐ¾îµéÀÎ ÀÎÁõ¼­ ¼³Á¤. - ¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignCert(pVoid, myCert);
	}

	// »ó´ëÆí (¼­¹ö)ÀÎÁõ¼­ ¼³Á¤ÇÏ±â.	
	AxTSAPI2.AxTS_SetPeerKMCert(pVoid, svrKMCert);
	AxTSAPI2.AxTS_SetPeerSignCert(pVoid, svrSignCert);

	// key °¡Á®¿À±â.
	// ¾Ë°í¸®Áò ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSymmetricAlgo(pVoid, SYSMMETRIC_ALGO, SYSMMETRIC_MODE);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	
	// keyExchangeMsg ¸¸µé±â.
	result = AxTSAPI2.AxTS_SignAndEnvelopedData(pVoid, "", plainText);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_KeyExchangeReqData Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// keyExchangeMsg °¡Á®¿À±â.
		signAndEnvelopMsg = AxTSAPI2.OutData;
		//alert(result + " : " + pVoid + "\r\keyExchangeMsg : " + keyExchangeMsg);
	}
	
	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		alert("Make KeyExchangeMsg Success!!");
	}
	return signAndEnvelopMsg;
}
//*************** DevleopnVerify && ****************//
function AxDevleopnVerify(signnEnvelopedMsg) {
	var result, pVoid;
	// API ½ÃÀÛ
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);

	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	
	var svrCertPath = svrCertDirPath + "kmCert.der";
	var svrKeyPath = svrCertDirPath + "kmPri.key";
	var svrKeyPwd = svrCertPwd;
	
	// ¾ÏÈ£¿ë °³ÀÎÅ° ÀÐ±â.
	result = AxTSAPI2.AxTS_ReadPrivateKeyInfo(pVoid, 0, svrKeyPath, svrKeyPwd);
	if (result > 0)
	{	
		var svrKeyPath = svrCertDirPath + "signPri.key";
		result = AxTSAPI2.AxTS_ReadPrivateKeyInfo(pVoid, 0, svrKeyPath, svrKeyPwd);
		{
	 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
			alert("AxTS_ReadPrivateKeyInfo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
			return false;
		}
		// ¼­¸í¿ë °³ÀÎÅ° °¡Á®¿À±â.
		myPriKey = AxTSAPI2.PriKey;
		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
		//alert(result + " : " + pVoid + "\r\nmyPriKey : " + myPriKey + "\r\nmyPriKeyInfo : " + myPriKeyInfo);
		// ¼­¸í¿ë °³ÀÎÅ° ¼³Á¤.
		AxTSAPI2.AxTS_SetMySignPriKey(pVoid, myPriKeyInfo);
	}
	else
	{
		// ¾ÏÈ£¿ë °³ÀÎÅ° °¡Á®¿À±â.
		myPriKey = AxTSAPI2.PriKey;
		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
		//alert(result + " : " + pVoid + "\r\nmyPriKey : " + myPriKey + "\r\nmyPriKeyInfo : " + myPriKeyInfo);
		// ¾ÏÈ£¿ë °³ÀÎÅ° ¼³Á¤.
		AxTSAPI2.AxTS_SetMyKMPriKey(pVoid, myPriKeyInfo);
	}
	
	// ¾ÏÈ£¿ë ÀÎÁõ¼­ ÀÐ±â.
	result = AxTSAPI2.AxTS_ReadCertificate(pVoid, 0, svrCertPath);
	if (result > 0)
	{	
		svrCertPath = svrCertDirPath + "signCert.der";
		result = AxTSAPI2.AxTS_ReadCertificate(pVoid, 0, svrCertPath);
		if (result > 0)
		{
	 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
			alert("AxTS_ReadCertificate Error : " + result + " : " + AxTSAPI2.ErrorMsg );
			return false;
		}
		// ÀÐ¾îµéÀÎ ÀÎÁõ¼­ °¡Á®¿À±â.
		myCert = AxTSAPI2.Cert;
		//alert(result + " : " + pVoid + "\r\nmyCert : " + myCert);
		// °¡Á®¿Â ÀÎÁõ¼­ ¼³Á¤ÇÏ±â.-¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignCert(pVoid, myCert);
	}
	else
	{
		// ¾ÏÈ£¿ë ÀÎÁõ¼­ °¡Á®¿À±â.
		myCert = AxTSAPI2.Cert;
		//alert(result + " : " + pVoid + "\r\nmyCert : " + myCert);
		// ¾ÏÈ£¿ë ÀÎÁõ¼­ ¼³Á¤.
		AxTSAPI2.AxTS_SetMyKMCert(pVoid, myCert);
	}
	
	// develop & Verify
	result = AxTSAPI2.AxTS_DevelopAndVerifyData(pVoid, "", signnEnvelopedMsg);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_KeyExchangeReqProcess Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// key ²¨³»¿À±â.
		// ±³È¯µÈ Å° °¡Á®¿À±â.
		plainMsg = AxTSAPI2.OutData;
	}
	
	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		alert("KeyExchange Success!!");
	}
	return plainMsg;
}


//** Make Key Exchage Message (Å°±³È¯ ¿äÃ» ¸Þ½ÃÁö »ý¼º)	********************************//
// 																					
// 1. ÀÚ½ÅÀÇ ÀÎÁõ¼­¸¦ ¼±ÅÃÇÑ´Ù. 					
// 2. ¼±ÅÃÇÑ ÀÚ½ÅÀÇ ¼­¸í¿ë ÀÎÁõ¼­ÀÇ °³ÀÎÅ°¸¦ ÀÐ¾îµéÀÎ´Ù.							
// 3. ÀÐ¾îµé¾î ¼­¸í¿ë ÀÎÁõ¼­ÀÇ °³ÀÎÅ°¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù.						
// 4. ¼±ÅÃÇÑ ÀÚ½ÅÀÇ ¼­¸í¿ë ÀÎÁõ¼­¸¦ ÀÐ¾îµéÀÎ´Ù.						
// 5. ÀÐ¾îµé¾î ¼­¸í¿ë ÀÎÁõ¼­¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù. 								
// 6. »ó´ë¹æ ÀÎÁõ¼­¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù.											
// 7. Å°±³È¯ ¿äÃ» ¸Þ½ÃÁö¸¦ »ý¼º½Ã ÇÊ¿äÇÑ ´ëÄªÅ° ¾Ë°í¸®Áò°ú ´ëÄªÅ°¸¦ ¼³Á¤ÇÑ´Ù.
// 8. Å°±³È¯ ¿äÃ» ¸Þ½ÃÁö¸¦ »ý¼ºÇÑ´Ù.			
// 																					
// ... ÇÊ¿äÇÑ °Í...																	
// º»ÀÎÀÇ ¼­¸í¿ë ÀÎÁõ¼­¿Í ±× °³ÀÎÅ°.												
// »ó´ë¹æ(¼­¹ö) ÀÎÁõ¼­ (¾ÏÈ£¿ë, ¾ÏÈ£¿ëÀÌ ¾øÀ» ½Ã¿¡´Â ¼­¸í¿ë).		
// Â÷ÈÄ »ç¿ëÇÒ (¼­¹ö¿¡ Àü´ÞÇÒ) ´ëÄªÅ°.				
// 																					
//**************************************************************************************//
function AxMakeKeyExchageMsg() {
	var result, pVoid;
	// API ½ÃÀÛ
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);
	
	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	// »ç¿ëÀÚ°¡ ÀÚ½ÅÀÇ ÀÎÁõ¼­¸¦ ¼±ÅÃ.
	var myCertDirPath, myCert, myPriKeyInfo;
	result = AxTSAPI2.Login(selectCertType, certType );
	if (result > 0)
	{
		alert(AxTSAPI2.ErrorMsg);
		return false;
	}
	else
	{
		myCertDirPath = AxTSAPI2.CertPath;
		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
		myCert = AxTSAPI2.Cert;
		// ÀÐ¾îµéÀÎ Å°Áß ¾ÏÈ£È­ µÇÁö ¾ÊÀº Å° ¼³Á¤. - ¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignPriKey(pVoid, myPriKeyInfo );
		// ÀÐ¾îµéÀÎ ÀÎÁõ¼­ ¼³Á¤. - ¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignCert(pVoid, myCert);
	}

	// »ó´ëÆí (¼­¹ö)ÀÎÁõ¼­ ¼³Á¤ÇÏ±â.	
	AxTSAPI2.AxTS_SetPeerKMCert(pVoid, svrKMCert);
	AxTSAPI2.AxTS_SetPeerSignCert(pVoid, svrSignCert);
	
	
	// key °¡Á®¿À±â.
	// ¾Ë°í¸®Áò ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSymmetricAlgo(pVoid, SYSMMETRIC_ALGO, SYSMMETRIC_MODE);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	

	// key °¡Á®¿À±â.
	var sessionKey, sessionIv;
//	result = AxTSAPI2.GetSessionKey();
	result = AxTSAPI2.GetSessionKey(csName, nLifeTime);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// ±³È¯ÇÒ Å° °¡Á®¿À±â.
		sessionKey = AxTSAPI2.SessionKey;
		sessionIv = AxTSAPI2.SessionIv;
		//alert(result + " : " + pVoid + "\r\nsessionKey : " + sessionKey + "\r\nsessionIv : " + sessionIv);
	}
	
	// ±³È¯ÇÒ Å° ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSessionKeyIv(pVoid, sessionKey, sessionIv);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		AxTSAPI2.AxTS_GetSessionKeyIv(pVoid);
		// ±³È¯ÇÒ Å° °¡Á®¿À±â.
		sessionKey = AxTSAPI2.SessionKey;
		sessionIv = AxTSAPI2.SessionIv;
		alert(result + " : " + pVoid + "\r\nsessionKey : " + sessionKey + "\r\nsessionIv : " + sessionIv);
	}

	// keyExchangeMsg ¸¸µé±â.
	result = AxTSAPI2.AxTS_KeyExchangeReqData(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_KeyExchangeReqData Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// keyExchangeMsg °¡Á®¿À±â.
		keyExchangeMsg = AxTSAPI2.OutData;
		//alert(result + " : " + pVoid + "\r\keyExchangeMsg : " + keyExchangeMsg);
	}
	
	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		alert("Make KeyExchangeMsg Success!!");
	}
	return keyExchangeMsg;
}

////*************** ParseKeyExchageMsg && ****************//
//
//function AxParseKeyExchageMsg(keyExchangeMsg) {
//	var result, pVoid;
//	// API ½ÃÀÛ
//	//alert("Init");
//	result = AxTSAPI2.AxTS_InitAPI();
//	pVoid = AxTSAPI2.pFHeHlib;
//	//alert(result + " : " + pVoid);
//	
//	var svrCertPath = svrCertDirPath + "kmCert.der";
//	var svrKeyPath = svrCertDirPath + "kmPri.key";
//	var svrKeyPwd = svrCertPwd;
//	
//	// ¾ÏÈ£¿ë °³ÀÎÅ° ÀÐ±â.
//	result = AxTSAPI2.AxTS_ReadPrivateKeyInfo(pVoid, 0, svrKeyPath, svrKeyPwd);
//	if (result > 0)
//	{	
//		var svrKeyPath = svrCertDirPath + "signPri.key";
//		result = AxTSAPI2.AxTS_ReadPrivateKeyInfo(pVoid, 0, svrKeyPath, svrKeyPwd);
//		{
// 			AxTSAPI2.AxTS_GetErrorInfo(pVoid);
//			alert("AxTS_ReadPrivateKeyInfo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
//			return false;
//		}
//		// ¼­¸í¿ë °³ÀÎÅ° °¡Á®¿À±â.
//		myPriKey = AxTSAPI2.PriKey;
//		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
//		//alert(result + " : " + pVoid + "\r\nmyPriKey : " + myPriKey + "\r\nmyPriKeyInfo : " + myPriKeyInfo);
//		// ¼­¸í¿ë °³ÀÎÅ° ¼³Á¤.
//		AxTSAPI2.AxTS_SetMySignPriKey(pVoid, myPriKeyInfo);
//	}
//	else
//	{
//		// ¾ÏÈ£¿ë °³ÀÎÅ° °¡Á®¿À±â.
//		myPriKey = AxTSAPI2.PriKey;
//		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
//		//alert(result + " : " + pVoid + "\r\nmyPriKey : " + myPriKey + "\r\nmyPriKeyInfo : " + myPriKeyInfo);
//		// ¾ÏÈ£¿ë °³ÀÎÅ° ¼³Á¤.
//		AxTSAPI2.AxTS_SetMyKMPriKey(pVoid, myPriKeyInfo);
//	}
//	
//	// ¾ÏÈ£¿ë ÀÎÁõ¼­ ÀÐ±â.
//	result = AxTSAPI2.AxTS_ReadCertificate(pVoid, 0, svrCertPath);
//	if (result > 0)
//	{	
//		svrCertPath = svrCertDirPath + "signCert.der";
//		result = AxTSAPI2.AxTS_ReadCertificate(pVoid, 0, svrCertPath);
//		if (result > 0)
//		{
// 			AxTSAPI2.AxTS_GetErrorInfo(pVoid);
//			alert("AxTS_ReadCertificate Error : " + result + " : " + AxTSAPI2.ErrorMsg );
//			return false;
//		}
//		// ÀÐ¾îµéÀÎ ÀÎÁõ¼­ °¡Á®¿À±â.
//		myCert = AxTSAPI2.Cert;
//		//alert(result + " : " + pVoid + "\r\nmyCert : " + myCert);
//		// °¡Á®¿Â ÀÎÁõ¼­ ¼³Á¤ÇÏ±â.-¼­¸í¿ë.
//		AxTSAPI2.AxTS_SetMySignCert(pVoid, myCert);
//	}
//	else
//	{
//		// ¾ÏÈ£¿ë ÀÎÁõ¼­ °¡Á®¿À±â.
//		myCert = AxTSAPI2.Cert;
//		//alert(result + " : " + pVoid + "\r\nmyCert : " + myCert);
//		// ¾ÏÈ£¿ë ÀÎÁõ¼­ ¼³Á¤.
//		AxTSAPI2.AxTS_SetMyKMCert(pVoid, myCert);
//	}
//	
//	var sessionAlgo, sessionMode;
//  var sessionKey, sessionIV; 
//
//	// keyExchangeMsg ºÐ¼®ÇÏ±â.
//	result = AxTSAPI2.AxTS_KeyExchangeReqProcess(pVoid, keyExchangeMsg);
//	if (result > 0)
//	{	
// 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
//		alert("AxTS_KeyExchangeReqProcess Error : " + result + " : " + AxTSAPI2.ErrorMsg );
//		return false;
//	}
//	else
//	{
//		// key ²¨³»¿À±â.
//		// ±³È¯µÈ Å° °¡Á®¿À±â.
//		sessionKey = AxTSAPI2.SessionKey;
//		sessionIv = AxTSAPI2.SessionIv;
//		sessionAlgo = AxTSAPI2.EncAlgo;
//		sessionMode = AxTSAPI2.EncMode;
//		
//	}
//	
//	// ¾Ë°í¸®Áò ¼³Á¤.
//	result = AxTSAPI2.AxTS_SetSymmetricAlgo(pVoid, sessionAlgo, sessionMode);
//	if (result > 0)
//	{	
// 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
//		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
//		return false;
//	}
//	
//	// ±³È¯µÈ Å° ¼³Á¤.
//	result = AxTSAPI2.AxTS_SetSessionKeyIv(pVoid, sessionKey, sessionIv);
//	if (result > 0)
//	{	
// 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
//		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
//		return false;
//	}
//
//	// API ¸¶¹«¸®.
//	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
//	if (result > 0)
//	{	
// 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
//		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
//		return false;
//	}
//	else
//	{
//		alert("KeyExchange Success!!");
//	}
//	return true;
//}
////*************** MakeKeyExchageResMsg ****************//

//*************** ParseKeyExchageResMsg ****************//
function AxParseKeyExchageResMsg(keyExchangeResMsg) {
	var result, ret, pVoid;
	// API ½ÃÀÛ
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);
	
	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	// key °¡Á®¿À±â.
	// ¾Ë°í¸®Áò ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSymmetricAlgo(pVoid, SYSMMETRIC_ALGO, SYSMMETRIC_MODE);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	
	
	// key °¡Á®¿À±â.
	var sessionKey, sessionIv;
	result = AxTSAPI2.GetSessionKey(csName, nLifeTime);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("GetSessionKey Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// ±³È¯ÇÒ Å° °¡Á®¿À±â.
		sessionKey = AxTSAPI2.SessionKey;
		sessionIv = AxTSAPI2.SessionIv;
		alert(result + " : " + pVoid + "\r\nsessionKey : " + sessionKey + "\r\nsessionIv : " + sessionIv);
	}
	
	// ±³È¯ÇÒ Å° ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSessionKeyIv(pVoid, sessionKey, sessionIv);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	{
		AxTSAPI2.AxTS_GetSessionKeyIv(pVoid);
		// ±³È¯ÇÒ Å° °¡Á®¿À±â.
		sessionKey = AxTSAPI2.SessionKey;
		sessionIv = AxTSAPI2.SessionIv;
		alert(result + " : " + pVoid + "\r\nsessionKey : " + sessionKey + "\r\nsessionIv : " + sessionIv);
	}
	ret = AxTSAPI2.AxTS_KeyExchangeResDataProcess(pVoid, keyExchangeResMsg);
 	if (ret > 0)
 	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_KeyExchangeResDataProcess Error : " + ret + " : " +  AxTSAPI2.ErrorMsg);
		return true;
	}
	
	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return true;
	}
	else
	{
		alert("KeyExchageRes Success!!");
	}
	return ret;
}

//*************** Encrypt ****************//
function AxEncrypt(plainText) {
	// À§Ä¡ÇÒ µð·ºÅä¸®¸¦ ¸¸µê 
	var result, pVoid;
	// API ½ÃÀÛ
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);
	
	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	// ¾Ë°í¸®Áò ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSymmetricAlgo(pVoid, SYSMMETRIC_ALGO, SYSMMETRIC_MODE);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		alert(AxTSAPI2.ErrorMsg);
		return false;
	}
	
	// key °¡Á®¿À±â.
	var sessionKey, sessionIv;
	result = AxTSAPI2.GetSessionKey(csName, nLifeTime);
	if (result > 0)
	{	
		alert("GetSessionKey Error : " + result);
		return false;
	}
	else
	{
		// ±³È¯ÇÒ Å° °¡Á®¿À±â.
		sessionKey = AxTSAPI2.SessionKey;
		sessionIv = AxTSAPI2.SessionIv;
		//alert(result + " : " + pVoid + "\r\nsessionKey : " + sessionKey + "\r\nsessionIv : " + sessionIv);

	}
	
	// ¼¼¼ÇÅ° ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSessionKeyIv(pVoid, sessionKey, sessionIv);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSessionKeyIv Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	//
	result = AxTSAPI2.AxTS_Encrypt(pVoid, plainText);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_Encrypt Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		//
		encryptMsg = AxTSAPI2.OutData;
		//alert(result + " : " + pVoid + "\r\nencryptMsg : " + encryptMsg);
	}
	
	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		alert("Encrypt Success!!");
	}
	return encryptMsg;
}

//*************** Decrypt ****************//
function AxDecrypt(encryptMsg) {
	// À§Ä¡ÇÒ µð·ºÅä¸®¸¦ ¸¸µê 
	var result, pVoid;
	//
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);
	
	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	// ¾Ë°í¸®Áò ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSymmetricAlgo(pVoid, SYSMMETRIC_ALGO, SYSMMETRIC_MODE);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	
	//
	result = AxTSAPI2.GetSessionKey(csName, nLifeTime);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// 
		sessionKey = AxTSAPI2.SessionKey;
		sessionIv  = AxTSAPI2.SessionIv;
		//alert(result + " : " + pVoid + "\r\nsessionKey : " + sessionKey + "\r\nsessionIv : " + sessionIv);
	}

	result = AxTSAPI2.AxTS_SetSessionKeyIv(pVoid, sessionKey, sessionIv);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	
	//
	result = AxTSAPI2.AxTS_Decrypt(pVoid, encryptMsg);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_Decrypt Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		//
		decryptMsg = AxTSAPI2.OutData;
		//alert(result + " : " + pVoid + "\r\ndecryptMsg : " + decryptMsg);
	}
	
	//
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		alert("Decrypt Success!!");
	}
	return decryptMsg;
}

//** Make Login Message	(·Î±×ÀÎ ¿äÃ» ¸Þ½ÃÁö »ý¼º)		************************************//
// 																					
// 1. ÀÚ½ÅÀÇ ÀÎÁõ¼­¸¦ ¼±ÅÃÇÏ¿© »ç¾÷ÀÚ(ÁÖ¹Îµî·Ï) ¹øÈ£¸¦ ÀÔ·Â¹Þ´Â´Ù.					
// 2. ¼±ÅÃÇÑ ÀÚ½ÅÀÇ ÀÎÁõ¼­ ¼ÂÆ® Áß ¼­¸í¿ë ÀÎÁõ¼­¸¦ ÀÐ¾îµéÀÎ´Ù.						
// 3. ÀÐ¾îµé¾î ¼­¸í¿ë ÀÎÁõ¼­¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù. 								
// 4. ¼±ÅÃÇÑ ÀÚ½ÅÀÇ ¼­¸í¿ë ÀÎÁõ¼­ÀÇ °³ÀÎÅ°¸¦ ÀÐ¾îµéÀÎ´Ù.							
// 5. ÀÐ¾îµé¾î ¼­¸í¿ë ÀÎÁõ¼­ÀÇ °³ÀÎÅ°¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù.						
// 6. »ó´ë¹æ ÀÎÁõ¼­¸¦ API ³»ºÎ¿¡ ¼³Á¤ÇÑ´Ù.											
// 7. ·Î±×ÀÎ ¿äÃ» ¸Þ½ÃÁö »ý¼º½Ã ÇÊ¿äÇÑ ´ëÄªÅ° ¾Ë°í¸®Áò ¹× ¸ðµå¸¦ ¼³Á¤ÇÑ´Ù. 										
// 8. »ç¾÷ÀÚ(ÁÖ¹Îµî·Ï)¹øÈ£ ¿Í tid, »ç¿ëÀÚ Á¤º¸ µîÀ» °¡Áö°í ·Î±×ÀÎ ¿äÃ» ¸Þ½ÃÁö¸¦ »ý¼ºÇÑ´Ù.
// 																					
// ... ÇÊ¿äÇÑ °Í...																	
// º»ÀÎÀÇ ¼­¸í¿ë ÀÎÁõ¼­¿Í ±× °³ÀÎÅ°.												
// »ó´ë¹æ(¼­¹ö) ÀÎÁõ¼­ (¾ÏÈ£¿ë, ¾ÏÈ£¿ëÀÌ ¾øÀ» ½Ã¿¡´Â ¼­¸í¿ë).						
// ·Î±×ÀÎ ¿äÃ»ÀÚ¸¦ ¾Ë¾Æº¼ ¼ö ÀÖ´Â transaction id(ÀÏ¸í tid).						
// ¼±ÅÃÇÑ ÀÎÁõ¼­ÀÇ ÁÖÀÎÀÇ »ç¾÷ÀÚ(ÁÖ¹Îµî·Ï)¹øÈ£.										
// °¢ ½Ã½ºÅÛ¿¡¼­ ÇÊ¿äÇÑ »ç¿ëÀÚ Á¤º¸.										
// 																					
//**************************************************************************************//
function AxMakeLoginMsg(tid, conf, userinfo) {
	var result, pVoid;
	// API ½ÃÀÛ
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);
	
	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	// »ç¿ëÀÚ°¡ ÀÚ½ÅÀÇ ÀÎÁõ¼­¸¦ ¼±ÅÃ.
	var loginConf, loginType;
	var myCertDirPath, myCert, myPriKeyInfo;


	if (conf != "")
	{
		loginType = "1";
		loginConf = conf;
	}	
	else
	{
		loginType = selectCertType;
		loginConf = certType;
	}
	result = AxTSAPI2.Login(loginType, loginConf );
	if (result > 0)
	{
		alert(AxTSAPI2.ErrorMsg);
		return false;
	}
	else
	{
		myCertDirPath = AxTSAPI2.CertPath;
		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
		myCert = AxTSAPI2.Cert;
		// ÀÐ¾îµéÀÎ Å°Áß ¾ÏÈ£È­ µÇÁö ¾ÊÀº Å° ¼³Á¤. - ¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignPriKey(pVoid, myPriKeyInfo );
		// ÀÐ¾îµéÀÎ ÀÎÁõ¼­ ¼³Á¤. - ¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignCert(pVoid, myCert);
	}
	// ¼±ÅÃÇÑ ÀÎÁõ¼­ÀÇ »ç¾÷ÀÚ ¹øÈ£ ¾ò±â.
	var ssn = 0;	

	// »ó´ëÆí (¼­¹ö)ÀÎÁõ¼­ ¼³Á¤ÇÏ±â.	
	AxTSAPI2.AxTS_SetPeerKMCert(pVoid, svrKMCert);
	AxTSAPI2.AxTS_SetPeerSignCert(pVoid, svrSignCert);
	

	// key °¡Á®¿À±â.
	// ¾Ë°í¸®Áò ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSymmetricAlgo(pVoid, SYSMMETRIC_ALGO, SYSMMETRIC_MODE);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	
	// Login Request Message ¸¸µé±â.
	result = AxTSAPI2.AxTS_LoginReq(pVoid, tid, ssn, userinfo);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
 		
		alert("AxTS_LoginReq Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// Login Request Message °¡Á®¿À±â.
		loginReqMsg = AxTSAPI2.OutData;
		//alert(result + " : " + pVoid + "\r\loginReqMsg : " + loginReqMsg);
	}
	
	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg  );
		return false;
	}
	else
	{
	//	alert("Make loginReqMsg Success!!");
	}
	
	return loginReqMsg;
}

function AxCertPwdChange(oldPwd, newPwd, plainText)
{
	var result, pVoid;
	// API ½ÃÀÛ
	//alert("Init");
	result = AxTSAPI2.AxTS_InitAPI();
	pVoid = AxTSAPI2.pFHeHlib;
	//alert(result + " : " + pVoid);
	
	// API ½ÇÇà¿¡ ÇÊ¿äÇÑ ¿É¼Ç ¼³Á¤.
	AxTSAPI2.SetInitConfig(CA_CERT_URL, CA_LDAP_INFO, policies, storageType, CTLInfos, InitPolMapInhibit, InitExplicitPol, InitAnyPolInhibit, crlCheckOption, arlCheckOption, USING_CA_CERT_SIGN , USING_CRL_SIGN, USING_SIGN_TIME_SIGN, Config1, Config2);

	// »ç¿ëÀÚ°¡ ÀÚ½ÅÀÇ ÀÎÁõ¼­¸¦ ¼±ÅÃ.
	var loginConf, loginType;
	var myCertDirPath, myCert, myPriKeyInfo;
	if (oldPwd != "")
	{
		loginType = "1";
		loginConf = oldPwd;
	}	
	else
	{
		loginType = selectCertType;
		loginConf = certType;
	}
	result = AxTSAPI2.Login(loginType, loginConf );
	if (result > 0)
	{
		alert(AxTSAPI2.ErrorMsg);
		return false;
	}
	else
	{
		myCertDirPath = AxTSAPI2.CertPath;
		myPriKeyInfo = AxTSAPI2.PriKeyInfo;
		myCert = AxTSAPI2.Cert;
		// ÀÐ¾îµéÀÎ Å°Áß ¾ÏÈ£È­ µÇÁö ¾ÊÀº Å° ¼³Á¤. - ¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignPriKey(pVoid, myPriKeyInfo );
		// ÀÐ¾îµéÀÎ ÀÎÁõ¼­ ¼³Á¤. - ¼­¸í¿ë.
		AxTSAPI2.AxTS_SetMySignCert(pVoid, myCert);
	}
	var mySignKeyPath = myCertDirPath + "\\signPri.key";
	var myKMKeyPath = myCertDirPath + "\\kmPri.key";
	
	result = AxTSAPI2.AxTS_PasswordChange(pVoid, mySignKeyPath, oldPwd, newPwd);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_PasswordChange Error : " + result + " : " + AxTSAPI2.ErrorMsg  );
		return false;
	}
	else
	{
		result = AxTSAPI2.AxTS_PasswordChange(pVoid, myKMKeyPath, oldPwd, newPwd);
		if (result > 0)
		{	
	 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
			alert("AxTS_PasswordChange  2 Error : " + result + " : " + AxTSAPI2.ErrorMsg  );
			return false;
		}
	}
	
	// »ó´ëÆí (¼­¹ö)ÀÎÁõ¼­ ¼³Á¤ÇÏ±â.	
	AxTSAPI2.AxTS_SetPeerKMCert(pVoid, svrKMCert);
	AxTSAPI2.AxTS_SetPeerSignCert(pVoid, svrSignCert);

	// key °¡Á®¿À±â.
	// ¾Ë°í¸®Áò ¼³Á¤.
	result = AxTSAPI2.AxTS_SetSymmetricAlgo(pVoid, SYSMMETRIC_ALGO, SYSMMETRIC_MODE);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_SetSymmetricAlgo Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	
	// keyExchangeMsg ¸¸µé±â.
	result = AxTSAPI2.AxTS_SignAndEnvelopedData(pVoid, "", plainText);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_KeyExchangeReqData Error : " + result + " : " + AxTSAPI2.ErrorMsg );
		return false;
	}
	else
	{
		// keyExchangeMsg °¡Á®¿À±â.
		pwdChangeMsg = AxTSAPI2.OutData;
		//alert(result + " : " + pVoid + "\r\keyExchangeMsg : " + keyExchangeMsg);
	}
	
	// API ¸¶¹«¸®.
	result = AxTSAPI2.AxTS_FinishAPI(pVoid);
	if (result > 0)
	{	
 		AxTSAPI2.AxTS_GetErrorInfo(pVoid);
		alert("AxTS_FinishAPI Error : " + result + " : " + AxTSAPI2.ErrorMsg  );
		return false;
	}
	else
	{
		alert("Make ChangePasswordMsg Success!!");
	}
	
	return pwdChangeMsg;
}










/********************************************
2004³â 2¿ù 14ÀÏ 
KTNET ÀÎÁõÆÀ ÀÌÀçÈÆ 
6000-2714, cathesis@ktnet.co.kr

Ãß°¡µÈ ÇÔ¼ö ¸®½ºÆ®
1. GatherValue
2. MakeSecureData
3. PrintObject

********************************************/

function GatherValue(form, start, bErase)
{
	var strResult = "";
	var name = "";
	var value = "";
	var sel=0;
					
	len = form.elements.length;
	for(i=start; i<len; i++) 
	{
		element = form.elements[i];
									
		if(element.name=="") continue;
		if(element.name=="INIpluginData") continue;
		if(element.name=="filedata") continue;
																	
		if (!((form.elements[i].type != "button") && (form.elements[i].type != "reset") && (form.elements[i].type != "submit"))) 
			continue;
																						
		if ( ((element.type == "radio") || (element.type == "checkbox")) && (element.checked!=true) ) 
			continue;
		// File Field´Â SKIPÇÑ´Ù.
		if(form.elements[i].name.indexOf('file_', 0) >= 0) {
			continue;
		}

		if (element.type == "select-one") 
		{
			sel = element.selectedIndex;
			if(sel<0) continue;
			if (element.options[sel].value != '') 
			{	
				value = element.options[sel].value;
			} 
			else 
			{
				value = element.options[sel].text;
			}
			if(bErase) element.selectedIndex = 0;
		}else{
			value = element.value;
			if(bErase) element.value = "";
		}
		if ((element.type == "checkbox") && (bErase)) element.checked = false;
	
		if (strResult!="") strResult += "&";
		strResult += element.name;
		strResult += "=";
		//		strResult += obj.URLEncode(value); 
		strResult += value;	
	}
	return strResult;
}



function MakeSecureData(form1, form2) 
{
	var p7Data = "";
	var eletemp = "";
	eletemp = GatherValue(form1, 0, false);
	
	if ((p7Data = AxMakeLoginMsg(12341234, 12341234, eletemp))== false)
		return false;
	alert(p7Data);
	
	form2.securedata.value = escape_url(p7Data);
	
	return true;
}


function PrintObject(){
	document.write('<OBJECT id=AxTSAPI2 classid=clsid:C2F50390-8033-4CC2-A0C6-DD7D3E6A9550 data=data:application/x-oleobject;base64,kAP1wjOAwkygxt19PmqVUAADAADYEwAA2BMAAA== style="LEFT: 0px; TOP: 0px" width="0" height="0"></OBJECT></P>');	
}

// URL Encoding
function escape_url(url) {
	var i;
	var ch;
	var out = '';
	var url_string = '';

	url_string = String(url);

	for (i = 0; i < url_string.length; i++) {
		ch = url_string.charAt(i);
		if (ch == ' ')
		    out += '%20';
		else if (ch == '%')
		    out += '%25';
		else if (ch == '&')
		    out += '%26';
		else if (ch == '+')
		    out += '%2B';
		else if (ch == '=')
		    out += '%3D';
		else if (ch == '?')
		    out += '%3F';
		else
		    out += ch;
	}
	return out;
}
