SignatureConfig.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. if (kmiAx.Cert_Status(userId, dn) == 2) {
  95. return "-35";
  96. }
  97. // 비밀번호 입력
  98. string sPassword = string.Empty;
  99. SignaturePasswordForm signPasswordForm = new SignaturePasswordForm(userName, userId, dualViewer);
  100. if(signPasswordForm.ShowDialog() == DialogResult.OK) {
  101. sPassword = signPasswordForm.GetPassword();
  102. signPasswordForm.Close();
  103. }
  104. if(string.IsNullOrEmpty(sPassword)) {
  105. return "-50";
  106. }
  107. bool passwordState = false;
  108. for(int j = 0; j < passwordLimitcnt; j++) {
  109. //if (CertManX.SetMatchedContextExt(dn, "ou=EMR,ou=대구파티마병원,ou=의료,o=SignKorea,c=KR", sPassword, 256 + 0 + 1) == null)
  110. if(CertManX.SetMatchedContextExt(dn, "", sPassword, 256 + 0 + 1) == null) {
  111. if(CertManX.GetLastErrorCode() == 2417) {
  112. i++;
  113. MessageBoxDlg.Show(true, "비밀번호 오류 : " + i + "회"
  114. , string.Format(Properties.Resources.msg_caption_confirm)
  115. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  116. sPassword = string.Empty;
  117. if(i < passwordLimitcnt) {
  118. signPasswordForm = new SignaturePasswordForm(userName, userId, dualViewer);
  119. if(signPasswordForm.ShowDialog() == DialogResult.OK) {
  120. sPassword = signPasswordForm.GetPassword();
  121. signPasswordForm.Close();
  122. }
  123. if(string.IsNullOrEmpty(sPassword)) {
  124. return "-50";
  125. }
  126. }
  127. }
  128. }
  129. else {
  130. passwordState = true;
  131. break;
  132. }
  133. CertManX.UnsetMatchedContext();//암호를 새로 물음
  134. }
  135. // 공인인증서 비밀번호 입력 최종 실패시 리턴
  136. if(!passwordState) {
  137. return "-50";
  138. }
  139. i = 0;
  140. string plain;
  141. plain = sCertTarget;
  142. string signdata = CertManX.SignDataB64("", plain, 0);
  143. if(signdata == null) {
  144. MessageBoxDlg.Show(true, "SignDataB64 실패 : [" + CertManX.GetLastErrorCode() + "]" + CertManX.GetLastErrorMsg()
  145. , string.Format(Properties.Resources.msg_caption_confirm)
  146. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  147. return "-50";
  148. }
  149. return signdata;
  150. }
  151. catch(Exception ex) {
  152. MessageBoxDlg.Show(true, "인증서 실행 실패 " + ex
  153. , string.Format(Properties.Resources.msg_caption_confirm)
  154. , MessageBoxButtons.OK, MessageBoxIcon.Information);
  155. return "-1";
  156. }
  157. }
  158. public string getSHA256ImageHash(String fileList, string delimiter) {
  159. byte[] hashValue;
  160. string hashStr = string.Empty;
  161. try {
  162. string[] sFullPath = fileList.Split('^');
  163. if(sFullPath != null && sFullPath.Length > 0) {
  164. FileStream fileStream = null;
  165. SHA256 mySHA256 = SHA256Managed.Create();
  166. foreach(string sPath in sFullPath) {
  167. // Create a fileStream for the file.
  168. fileStream = new FileStream(sPath, FileMode.Open);
  169. // Be sure it's positioned to the beginning of the stream.
  170. fileStream.Position = 0;
  171. // Compute the hash of the fileStream.
  172. hashValue = mySHA256.ComputeHash(fileStream);
  173. // Close the file.
  174. fileStream.Close();
  175. if(string.IsNullOrEmpty(delimiter)) hashStr += byteArrayToString(hashValue);
  176. else hashStr += string.Format("{0}{1}", byteArrayToString(hashValue), "^");
  177. }
  178. }
  179. if(!string.IsNullOrEmpty(hashStr) && hashStr.Length > 0) {
  180. hashStr = hashStr.Substring(0, hashStr.Length - 1);
  181. }
  182. }
  183. catch(DirectoryNotFoundException) {
  184. Console.WriteLine("Error: The directory specified could not be found.");
  185. }
  186. catch(IOException) {
  187. Console.WriteLine("Error: A file in the directory could not be accessed.");
  188. }
  189. return hashStr;
  190. }
  191. private string byteArrayToString(byte[] bStr) {
  192. string hexOutput = string.Empty;
  193. foreach(char letter in bStr) {
  194. // Get the integral value of the character.
  195. int value = Convert.ToInt32(letter);
  196. // Convert the decimal value to a hexadecimal value in string form.
  197. hexOutput += String.Format("{0:x}", value);
  198. }
  199. return hexOutput;
  200. }
  201. }
  202. }