Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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;
        }