SignatureConfig.cs.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.IO;
  3. using System.Security.Cryptography;
  4. using System.Windows.Forms;
  5. //using KMCLIENTAXLib;
  6. using ClipSoft.eForm.Base.Dialog;
  7. namespace CLIP.eForm.Consent.UI
  8. {
  9. class SignatureConfig
  10. {
  11. //KMClientAX kmiAx = new KMClientAX();
  12. KMCLIENTAXLib.KMClientAX kmiAx = new KMCLIENTAXLib.KMClientAX();
  13. SKCOMMAXLib.SKCommAX CertManX = new SKCOMMAXLib.SKCommAX();
  14. public void SignAllClear()
  15. {
  16. try
  17. {
  18. int nRet;
  19. /*
  20. * 특정 DN 체계를 가진 인증서를 일괄 삭제 합니다.
  21. */
  22. //nRet = kmiAx.CertBatchDel("ou=EMR,ou=대구파티마병원,ou=의료,o=SignKorea,c=KR");
  23. nRet = kmiAx.CertBatchDel("ou=EMR,ou=경북대학교병원,ou=의료,o=SignKorea,c=KR");
  24. nRet = kmiAx.CertBatchDel("ou=EMR,ou=칠곡경북대학교병원,ou=의료,o=SignKorea,c=KR");
  25. //if (nRet == 0)
  26. //{
  27. // MessageBoxDlg.Show(true, "로컬 PC 인증서 일괄 삭제 실패-" + kmiAx.Error_MSG()
  28. // , string.Format(Properties.Resources.msg_caption_confirm)
  29. // , MessageBoxButtons.OK, MessageBoxIcon.Information);
  30. //}
  31. }
  32. catch (Exception ex)
  33. {
  34. MessageBoxDlg.Show(true, "인증서 삭제 실패 " + ex
  35. , string.Format(Properties.Resources.msg_caption_confirm)
  36. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  37. }
  38. }
  39. public string SetSignServerInfo(string ip, int port, string id)
  40. {
  41. try
  42. {
  43. int nRet;
  44. string strRet;
  45. SignAllClear();
  46. nRet = kmiAx.kmsConnect(ip, Convert.ToInt32(port));
  47. if (nRet == 0)
  48. {
  49. MessageBoxDlg.Show(true, "kmi 서버 접속 실패-" + kmiAx.Error_MSG()
  50. , string.Format(Properties.Resources.msg_caption_confirm)
  51. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  52. return string.Format("{0}", nRet);
  53. }
  54. strRet = kmiAx.kmsInit();
  55. if (strRet == "")
  56. {
  57. MessageBoxDlg.Show(true, "kmi 초기화 실패-" + kmiAx.Error_MSG()
  58. , string.Format(Properties.Resources.msg_caption_confirm)
  59. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  60. kmiAx.kmsDisconnect();
  61. }
  62. strRet = kmiAx.GetKeyAndCert(id);
  63. if (strRet == "")
  64. {
  65. MessageBoxDlg.Show(true, "인증서 다운로드 실패" + kmiAx.Error_MSG()
  66. , string.Format(Properties.Resources.msg_caption_confirm)
  67. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  68. kmiAx.kmsDisconnect();
  69. }
  70. kmiAx.kmsDisconnect();
  71. return strRet;
  72. }
  73. catch (Exception ex)
  74. {
  75. MessageBoxDlg.Show(true, "인증서 다운로드 실패 " + ex
  76. , string.Format(Properties.Resources.msg_caption_confirm)
  77. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  78. return "-1";
  79. }
  80. }
  81. public string SignatureExec(string sCertTarget, string dn, string userName, string userId, bool dualViewer)
  82. {
  83. try
  84. {
  85. int i = 0;
  86. //bool checkState = true;
  87. //BSTR pszUserID : 인증서 DN 또는 DN의 userid
  88. //BSTR pszSuffix: 검색할 인증서의 DN suffix(예: ou = test,O = test,c = KR )
  89. //BSTR pszPassword : 인증서 비밀번호
  90. //long Type : 인증서 선택/ 나열 방법을 정의
  91. //string singKoreaDn = CertManX.SetMatchedContextExt("", "", "", 256 + 0 + 1);
  92. //if (!dn.Equals(singKoreaDn))
  93. //{
  94. // // 공인인증서 초기화
  95. // CertManX.UnsetMatchedContext();
  96. // checkState = false;
  97. //}
  98. //if(!checkState)
  99. //{
  100. // 공인인증서 초기화
  101. int passwordLimitcnt = 5;
  102. CertManX.UnsetMatchedContext();//암호를 새로 물음
  103. CertManX.SetWrongPasswordLimit(passwordLimitcnt);
  104. // 비밀번호 입력
  105. string sPassword = string.Empty;
  106. SignaturePasswordForm signPasswordForm = new SignaturePasswordForm(userName, userId, dualViewer);
  107. if (signPasswordForm.ShowDialog() == DialogResult.OK)
  108. {
  109. sPassword = signPasswordForm.GetPassword();
  110. signPasswordForm.Close();
  111. }
  112. if (string.IsNullOrEmpty(sPassword))
  113. {
  114. return "-50";
  115. }
  116. bool passwordState = false;
  117. for (int j = 0; j < passwordLimitcnt; j++)
  118. {
  119. //if (CertManX.SetMatchedContextExt(dn, "ou=EMR,ou=대구파티마병원,ou=의료,o=SignKorea,c=KR", sPassword, 256 + 0 + 1) == null)
  120. if (CertManX.SetMatchedContextExt(dn, "", sPassword, 256 + 0 + 1) == null)
  121. {
  122. if (CertManX.GetLastErrorCode() == 2417)
  123. {
  124. i++;
  125. MessageBoxDlg.Show(true, "비밀번호 오류 : " + i + "회"
  126. , string.Format(Properties.Resources.msg_caption_confirm)
  127. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  128. sPassword = string.Empty;
  129. if (i < passwordLimitcnt)
  130. {
  131. signPasswordForm = new SignaturePasswordForm(userName, userId, dualViewer);
  132. if (signPasswordForm.ShowDialog() == DialogResult.OK)
  133. {
  134. sPassword = signPasswordForm.GetPassword();
  135. signPasswordForm.Close();
  136. }
  137. if (string.IsNullOrEmpty(sPassword))
  138. {
  139. return "-50";
  140. }
  141. }
  142. }
  143. }
  144. else
  145. {
  146. passwordState = true;
  147. break;
  148. }
  149. CertManX.UnsetMatchedContext();//암호를 새로 물음
  150. }
  151. // 공인인증서 비밀번호 입력 최종 실패시 리턴
  152. if(!passwordState)
  153. {
  154. return "-50";
  155. }
  156. i = 0;
  157. string plain;
  158. plain = sCertTarget;
  159. string signdata = CertManX.SignDataB64("", plain, 0);
  160. if (signdata == null)
  161. {
  162. MessageBoxDlg.Show(true, "SignDataB64 실패 : [" + CertManX.GetLastErrorCode() + "]" + CertManX.GetLastErrorMsg()
  163. , string.Format(Properties.Resources.msg_caption_confirm)
  164. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  165. return "-50";
  166. }
  167. return signdata;
  168. }
  169. catch (Exception ex)
  170. {
  171. MessageBoxDlg.Show(true, "인증서 실행 실패 " + ex
  172. , string.Format(Properties.Resources.msg_caption_confirm)
  173. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  174. return "-1";
  175. }
  176. }
  177. public string getSHA256ImageHash(String fileList, string delimiter)
  178. {
  179. byte[] hashValue;
  180. string hashStr = string.Empty;
  181. try
  182. {
  183. string[] sFullPath = fileList.Split('^');
  184. if (sFullPath != null && sFullPath.Length > 0)
  185. {
  186. FileStream fileStream = null;
  187. SHA256 mySHA256 = SHA256Managed.Create();
  188. foreach (string sPath in sFullPath)
  189. {
  190. // Create a fileStream for the file.
  191. fileStream = new FileStream(sPath, FileMode.Open);
  192. // Be sure it's positioned to the beginning of the stream.
  193. fileStream.Position = 0;
  194. // Compute the hash of the fileStream.
  195. hashValue = mySHA256.ComputeHash(fileStream);
  196. // Close the file.
  197. fileStream.Close();
  198. if(string.IsNullOrEmpty(delimiter)) hashStr += byteArrayToString(hashValue);
  199. else hashStr += string.Format("{0}{1}", byteArrayToString(hashValue), "^");
  200. }
  201. }
  202. if(!string.IsNullOrEmpty(hashStr) && hashStr.Length > 0)
  203. {
  204. hashStr = hashStr.Substring(0, hashStr.Length - 1);
  205. }
  206. }
  207. catch (DirectoryNotFoundException)
  208. {
  209. Console.WriteLine("Error: The directory specified could not be found.");
  210. }
  211. catch (IOException)
  212. {
  213. Console.WriteLine("Error: A file in the directory could not be accessed.");
  214. }
  215. return hashStr;
  216. }
  217. private string byteArrayToString(byte[] bStr)
  218. {
  219. string hexOutput = string.Empty;
  220. foreach (char letter in bStr)
  221. {
  222. // Get the integral value of the character.
  223. int value = Convert.ToInt32(letter);
  224. // Convert the decimal value to a hexadecimal value in string form.
  225. hexOutput += String.Format("{0:x}", value);
  226. }
  227. return hexOutput;
  228. }
  229. }
  230. }