CertificateService.cs
1 #region Usings
2 using sage.ew.global;
3 using sage.ew.interficies;
4 using System;
5 using System.Collections.Generic;
6 using System.IO;
7 using System.Linq;
8 using System.Security;
9 using System.Security.Cryptography.X509Certificates;
10 using System.Text;
11 #endregion Using
12 
13 namespace sage.ew.functions.Clases
14 {
19  {
20  #region Propiedades privadas
21  List<string> _lstFileNameCert = new List<string>();
22  private string _cMensajeError = string.Empty;
23  #endregion _cMensajeError
24 
25  #region Constructor
26  public CertificateService()
30  {
31  }
32  #endregion Constructor
33 
34  #region Propiedades públicas
35  public string _MensajeError
39  {
40  get { return _cMensajeError; }
41  }
42  #endregion Propiedades públicas
43 
44  #region Destructor
49  {
50  CloseService();
51  }
52  #endregion Destructor
53 
54  #region Métodos públicos
55  public string _GetFileCertificate(string tcCertificateName, string tcCertificatePassword = "")
62  {
63  X509Certificate2 loCertX509 = null;
64  string lcFileNameCert = string.Empty;
65 
66  try
67  {
68  byte[] fileContent = null;
69 
70  if (tcCertificateName == "ENTIDAD_FACEB2B")
71  {
72  fileContent = RawCertificate(tcCertificateName);
73  }
74  else
75  {
76  loCertX509 = GetCertificate(tcCertificateName, tcCertificatePassword);
77 
78  if (loCertX509 != null)
79  fileContent = loCertX509.Export(X509ContentType.Pfx, tcCertificatePassword);
80  }
81 
82  if (fileContent.Length > 0)
83  {
84  lcFileNameCert = Path.GetTempFileName();
85  File.WriteAllBytes(lcFileNameCert, fileContent);
86  _lstFileNameCert.Add(lcFileNameCert);
87  }
88  else
89  {
90  lcFileNameCert = string.Empty;
91  _cMensajeError = string.Format("El certificado '{0}' no se ha importado correctamente. ", tcCertificateName);
92  }
93  }
94  catch (SecurityException ex)
95  {
96  _cMensajeError = string.Format("El certificado '{0}' no se ha importado correctamente. {1}", tcCertificateName, ex.Message);
97  }
98  catch (Exception ex)
99  {
100  _cMensajeError = string.Format("El certificado '{0}' no se ha importado correctamente. {1}", tcCertificateName, ex);
101  }
102 
103  return lcFileNameCert;
104  }
105 
109  public void _CloseService()
110  {
111  CloseService();
112  }
113  #endregion Métodos públicos
114 
115  #region Métodos privados
116  private void CloseService()
120  {
121  if (_lstFileNameCert.Count > 0)
122  {
123  foreach (string lcFile in _lstFileNameCert)
124  DeleteFile(lcFile);
125 
126  _lstFileNameCert.Clear();
127  }
128  }
129 
134  private void DeleteFile(string tcFileName)
135  {
136  try
137  {
138  if (File.Exists(tcFileName))
139  File.Delete(tcFileName);
140  }
141  catch (Exception)
142  {
143  }
144  }
145 
151  private Byte[] RawCertificate(string tcCertificateName)
152  {
153  Byte[] loRawCertif = null;
154 
155  switch (tcCertificateName)
156  {
157  case "SAGESPAIN_SHA1":
158  if (FUNCTIONS._EntornoFaceB2B() == eEntornos.Pre)
159  loRawCertif = Properties.Resources.SAGESPAIN_PF_PRUEBAS;
160  else
161  loRawCertif = Properties.Resources.SAGESPAIN_SHA1;
162  break;
163 
164  case "SAGESPAIN":
165  if (FUNCTIONS._EntornoFaceB2B() == eEntornos.Pre)
166  loRawCertif = Properties.Resources.SAGESPAIN_PF_PRUEBAS;
167  else
168  loRawCertif = Properties.Resources.SAGESPAIN;
169  break;
170 
171  case "ENTIDAD_FACEB2B":
172  if (FUNCTIONS._EntornoFaceB2B() == eEntornos.Pre)
173  loRawCertif = Properties.Resources.SELLO_ENTIDAD_SGAD_PRUEBAS;
174  else
175  loRawCertif = Properties.Resources.SELLO_DE_ENTIDAD_SGAD;
176  break;
177 
178  default:
179  break;
180  }
181 
182  return loRawCertif;
183  }
184 
191  private X509Certificate2 GetCertificate(string tcCertificateName, string tcCertificatePassword)
192  {
193  if (String.IsNullOrEmpty(tcCertificateName))
194  throw new SecurityException("Nombre del certificado no informado.");
195 
196  string lcError = string.Empty;
197 
198  X509Certificate2 loCertX509 = null;
199 
200  try
201  {
202  if (tcCertificateName == "SAGESPAIN")
203  {
204  loCertX509 = GetCertificateFromResources(tcCertificateName, tcCertificatePassword, ref lcError);
205 
206  if (loCertX509 == null)
207  loCertX509 = GetCertificateFromResources("SAGESPAIN_SHA1", tcCertificatePassword, ref lcError);
208  }
209  else
210  {
211  Byte[] loRawCertif = RawCertificate(tcCertificateName);
212 
213  if (loRawCertif.Length > 0)
214  {
215  loCertX509 = new X509Certificate2();
216  loCertX509.Import(loRawCertif, tcCertificatePassword, X509KeyStorageFlags.Exportable);
217  }
218  }
219  }
220  catch (Exception ex)
221  {
222  _cMensajeError = string.Format("El certificado '{0}' no se ha importado correctamente. {1}", tcCertificateName, ex.Message);
223  }
224 
225  if (loCertX509 == null)
226  {
227  if (string.IsNullOrWhiteSpace(lcError))
228  _cMensajeError = string.Format("El certificado '{0}' indicado no se encuentra o está caducado.", tcCertificateName);
229  else
230  _cMensajeError = lcError;
231  }
232 
233  return loCertX509;
234  }
235 
243  private X509Certificate2 GetCertificateFromResources(string tcCertificateName, string tcCertificatePassword, ref string tcError)
244  {
245  X509Certificate2 certX509 = null;
246  tcError = "";
247 
248  try
249  {
250  Byte[] raw = RawCertificate(tcCertificateName);
251 
252  if (raw.Length > 0)
253  {
254  certX509 = new X509Certificate2();
255  certX509.Import(raw, tcCertificatePassword, X509KeyStorageFlags.Exportable);
256  }
257  }
258  catch (Exception ex)
259  {
260  tcError = string.Format("El certificado '{0}' no se ha importado correctamente. {1}", tcCertificateName, ex);
261  certX509 = null;
262  }
263 
264  return certX509;
265  }
266  #endregion Métodos privados
267  }
268 }
Interfaz para obtener certificados
void _CloseService()
Close Certificate Service
eEntornos
Entornos de trabajo
Definition: General.cs:135