SignatureConfig.cs 8.4 KB

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