SignatureConfig.cs 10 KB

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