// returns Root Certificate on the eid. private byte[] GetCertificateFile(string certificateName) { byte[] value = null; if (m == null) { m = Module.GetInstance(moduleFileName); } try { // Get the first slot (cardreader) with a token Slot[] slotlist = m.GetSlotList(true); if (slotlist.Length > 0) { Slot slot = slotlist[0]; Session session = slot.Token.OpenSession(true); // Search for objects // First, define a search template // "The label attribute of the objects should equal ..." ByteArrayAttribute fileLabel = new ByteArrayAttribute(CKA.LABEL); ObjectClassAttribute certificateAttribute = new ObjectClassAttribute(CKO.CERTIFICATE); fileLabel.Value = Encoding.UTF8.GetBytes(certificateName); session.FindObjectsInit(new P11Attribute[] { certificateAttribute, fileLabel }); P11Object[] foundObjects = session.FindObjects(1); if (foundObjects.Length != 0) { X509PublicKeyCertificate cert = foundObjects[0] as X509PublicKeyCertificate; value = cert.Value.Value; } session.FindObjectsFinal(); } else { throw new EIDNotFoundException(); } } finally { m.Dispose(); m = null; } return(value); }
/// <summary> /// Return raw byte data from objects of object class Certificate /// </summary> /// <param name="Certificatename">Label value of the certificate object</param> /// <returns>byte array with certificate file</returns> private byte[] GetCertificateFile(String Certificatename) { // returns Root Certificate on the eid. byte[] value = null; // pkcs11 module init if (m == null) { m = Module.GetInstance(mFileName); } //m.Initialize(); try { // Get the first slot (cardreader) with a token Slot[] slotlist = m.GetSlotList(true); if (slotlist.Length > 0) { Slot slot = slotlist[0]; Session session = slot.Token.OpenSession(true); // Search for objects // First, define a search template // "The label attribute of the objects should equal ..." ByteArrayAttribute fileLabel = new ByteArrayAttribute(CKA.LABEL); ObjectClassAttribute certificateAttribute = new ObjectClassAttribute(CKO.CERTIFICATE); fileLabel.Value = System.Text.Encoding.UTF8.GetBytes(Certificatename); session.FindObjectsInit(new P11Attribute[] { certificateAttribute, fileLabel }); P11Object[] foundObjects = session.FindObjects(1); if (foundObjects.Length != 0) { X509PublicKeyCertificate cert = foundObjects[0] as X509PublicKeyCertificate; value = cert.Value.Value; } session.FindObjectsFinal(); } else { Console.WriteLine("No card found\n"); } } finally { // pkcs11 finalize m.Dispose();//m.Finalize_(); m = null; } return(value); }
internal bool Autenticar(int in_SlotIndex, string in_PIN, out string out_Error) { bool result = false; out_Error = "OK"; try { if (m_Module == null) { m_Module = Module.GetInstance(m_FileName); } if (m_Slots == null) { // GetSlotList. m_Slots = m_Module.GetSlotList(true); } if (m_Slots.Length > in_SlotIndex) { Slot slot = m_Slots[in_SlotIndex]; Session session = slot.Token.OpenSession(false); m_CurrentIndex = in_SlotIndex; session.Login(UserType.USER, in_PIN); try { ObjectClassAttribute certificateAttribute = new ObjectClassAttribute(CKO.CERTIFICATE); ByteArrayAttribute fileLabel = new ByteArrayAttribute(CKA.LABEL); fileLabel.Value = System.Text.Encoding.UTF8.GetBytes(m_AutenticacionLabel); session.FindObjectsInit(new P11Attribute[] { certificateAttribute, fileLabel } ); P11Object[] foundObjects = session.FindObjects(1) as P11Object[]; if (foundObjects.Length == 1) { X509PublicKeyCertificate cert = foundObjects[0] as X509PublicKeyCertificate; OcspClient oscpClient = new OcspClient(cert.Value.Encode()); if (oscpClient.PublicKeyCertificate.IsValidNow) { CertificateStatus status = oscpClient.ConsultarEstadoDeCertificado(oscpClient.PublicKeyCertificate, oscpClient.LeerCertificado(m_IssuerCertificate)); if (status == CertificateStatus.Good) { result = true; } else if (status == CertificateStatus.Revoked) { out_Error = "Certificado Revocado"; } else { out_Error = "Certificado Desconocido"; } } else { out_Error = "Certificado Expirado"; } } else { out_Error = "No se encontraron objetos en la tarjeta."; } session.FindObjectsFinal(); } catch (System.Net.WebException wex) { Console.WriteLine(wex.ToString()); out_Error = wex.Message; } catch (Exception e) { Console.WriteLine(e.ToString()); out_Error = e.Message; } finally { // Log out. session.Logout(); } } } catch (Exception e) { Console.WriteLine(e.ToString()); } return(result); }