Ejemplo n.º 1
0
        // Sign data with a named private key
        // param name="data": Data to be signed
        // param name="privatekeylabel": Label for private key. (Can be "Signature" or "Authentication")
        // returns Signed data
        public byte[] DoSign(byte[] data, string privatekeylabel)
        {
            byte[]  encryptedData = null;
            Session session       = null;

            if (m == null)
            {
                m = Module.GetInstance(moduleFileName);
            }

            try
            {
                // Get the first slot (cardreader) with a token (eid)
                Slot slot = m.GetSlotList(true)[0];
                session = slot.Token.OpenSession(true);
                ObjectClassAttribute classAttribute    = new ObjectClassAttribute(CKO.PRIVATE_KEY);
                ByteArrayAttribute   keyLabelAttribute = new ByteArrayAttribute(CKA.LABEL);
                keyLabelAttribute.Value = Encoding.UTF8.GetBytes(privatekeylabel);

                session.FindObjectsInit(new P11Attribute[] {
                    classAttribute,
                    keyLabelAttribute
                }
                                        );

                P11Object[] privatekeys = session.FindObjects(1);
                session.FindObjectsFinal();

                if (privatekeys.Length >= 1)
                {
                    session.SignInit(new Mechanism(CKM.SHA1_RSA_PKCS), (PrivateKey)privatekeys[0]);
                    encryptedData = session.Sign(data);
                }
            }
            catch (TokenException)
            {
                if (session == null)
                {
                    throw new EIDNotFoundException();
                }
                else if (encryptedData == null)
                {
                    throw new SignatureCanceledException();
                }
            }
            finally
            {
                m.Dispose();
                m = null;
            }

            return(encryptedData);
        }
Ejemplo n.º 2
0
        // 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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sign data with a named private key
        /// </summary>
        /// <param name="data">Data to be signed</param>
        /// <param name="privatekeylabel">Label for private key. Can be "Signature" or "Authentication"</param>
        /// <returns>Signed data.</returns>
        public byte[] DoSign(byte[] data, string privatekeylabel)
        {
            if (m == null)
            {
                // link with the pkcs11 DLL
                m = Module.GetInstance(mFileName);
            } //m.Initialize();

            byte[] encryptedData = null;
            try
            {
                Slot    slot    = m.GetSlotList(true)[0];
                Session session = slot.Token.OpenSession(true);
                ObjectClassAttribute classAttribute    = new ObjectClassAttribute(CKO.PRIVATE_KEY);
                ByteArrayAttribute   keyLabelAttribute = new ByteArrayAttribute(CKA.LABEL);
                keyLabelAttribute.Value = System.Text.Encoding.UTF8.GetBytes(privatekeylabel);

                session.FindObjectsInit(new P11Attribute[] {
                    classAttribute,
                    keyLabelAttribute
                }
                                        );
                P11Object[] privatekeys = session.FindObjects(1) as P11Object[];
                session.FindObjectsFinal();

                if (privatekeys.Length >= 1)
                {
                    if (privatekeys[0] != null)
                    {
                        PrivateKey key = (PrivateKey)privatekeys[0];
                        if (key.KeyType.KeyType == CKK.EC)
                        {
                            SHA384 sha       = new SHA384CryptoServiceProvider();
                            byte[] HashValue = sha.ComputeHash(data);
                            session.SignInit(new Mechanism(CKM.ECDSA), (PrivateKey)privatekeys[0]);
                            encryptedData = session.Sign(HashValue);
                        }
                        else if (key.KeyType.KeyType == CKK.RSA)
                        {
                            session.SignInit(new Mechanism(CKM.SHA1_RSA_PKCS), (PrivateKey)privatekeys[0]);
                            encryptedData = session.Sign(data);
                        }
                    }
                }
            }
            finally
            {
                m.Dispose();
                m = null;
            }
            return(encryptedData);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Return raw byte data from objects of object class Public Key
        /// </summary>
        /// <param name="PubKeyName">Label value of the key object</param>
        /// <returns>ECPublicKey object of the public key found</returns>
        public ECPublicKey GetPublicKey(String PubKeyName)
        {
            ECPublicKey eCPublicKey = null;

            // pkcs11 module init
            if (m == null)
            {
                m = Module.GetInstance(mFileName);
            }
            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 PubKeyName
                    ObjectClassAttribute classAttribute    = new ObjectClassAttribute(CKO.PUBLIC_KEY);
                    ByteArrayAttribute   keyLabelAttribute = new ByteArrayAttribute(CKA.LABEL);
                    keyLabelAttribute.Value = System.Text.Encoding.UTF8.GetBytes(PubKeyName);

                    session.FindObjectsInit(new P11Attribute[] { classAttribute, keyLabelAttribute });
                    //P11Object[] pubkeys = session.FindObjects(1) as P11Object[];
                    P11Object[] pubkeys = session.FindObjects(1);
                    session.FindObjectsFinal();

                    if ((pubkeys.Length == 0) || (pubkeys[0] == null))
                    {
                        Console.WriteLine("Public Key Object not found");
                        return(eCPublicKey);
                    }
                    eCPublicKey = (ECPublicKey)pubkeys[0];
                    //  session.FindObjectsFinal();
                }
                else
                {
                    Console.WriteLine("No card found\n");
                }
            }
            finally
            {
                // pkcs11 finalize
                m.Dispose();//m.Finalize_();
                m = null;
            }
            return(eCPublicKey);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Return raw byte data from objects
        /// </summary>
        /// <param name="Filename">Label value of the object</param>
        /// <returns>byte array with file</returns>
        private byte[] GetFile(String Filename)
        {
            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);
                    fileLabel.Value = System.Text.Encoding.UTF8.GetBytes(Filename);
                    ByteArrayAttribute fileData = new ByteArrayAttribute(CKA.CLASS);
                    fileData.Value = BitConverter.GetBytes((uint)Net.Sf.Pkcs11.Wrapper.CKO.DATA);
                    session.FindObjectsInit(new P11Attribute[] {
                        fileLabel, fileData
                    });
                    P11Object[] foundObjects = session.FindObjects(1);
                    if (foundObjects.Length != 0)
                    {
                        Data file = foundObjects[0] as Data;
                        value = file.Value.Value;
                        // Console.WriteLine(System.Text.Encoding.UTF8.GetString(value));
                    }
                    session.FindObjectsFinal();
                }
                else
                {
                    Console.WriteLine("No card found\n");
                }
            }
            finally
            {
                // pkcs11 finalize
                m.Dispose();//m.Finalize_();
                m = null;
            }
            return(value);
        }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 7
0
        public byte[] GetFileHamid()
        {
            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)
                {
                    int     slotLength = slotlist.Length;
                    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 classAttribute = new ByteArrayAttribute(CKA.CLASS);
                    classAttribute.Value = BitConverter.GetBytes((uint)Net.Sf.Pkcs11.Wrapper.CKO.DATA);
                    session.FindObjectsInit(new P11Attribute[] { classAttribute });

                    P11Object[] foundObjects = session.FindObjects(40);

                    if (foundObjects.Length != 0)
                    {
                        Data file = foundObjects[0] as Data;
                        value = file.Value.Value;
                    }
                    session.FindObjectsFinal();
                }
                else
                {
                    Console.WriteLine("No card found\n");
                }
            }
            finally
            {
                // pkcs11 finalize
                m.Dispose();//m.Finalize_();
                m = null;
            }

            return(value);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Challenge an applet 1.8 card
        /// </summary>
        /// <param name="data">Data to be signed</param>
        /// <returns>Signed challenge data.</returns>
        public byte[] DoChallenge(byte[] data)
        {
            if (m == null)
            {
                // link with the pkcs11 DLL
                m = Module.GetInstance(mFileName);
            }

            byte[] encryptedData = null;
            try
            {
                Slot slot = m.GetSlotList(true)[0];

                if (slot == null)
                {
                    Console.WriteLine("No card reader found");
                }
                if (slot.Token == null)
                {
                    Console.WriteLine("No card Found");
                }

                Session session = slot.Token.OpenSession(true);
                ObjectClassAttribute classAttribute    = new ObjectClassAttribute(CKO.PRIVATE_KEY);
                ByteArrayAttribute   keyLabelAttribute = new ByteArrayAttribute(CKA.LABEL);
                keyLabelAttribute.Value = System.Text.Encoding.UTF8.GetBytes("Card");

                session.FindObjectsInit(new P11Attribute[] { classAttribute, keyLabelAttribute }
                                        );
                P11Object[] privatekeys = session.FindObjects(1) as P11Object[];
                session.FindObjectsFinal();

                if (privatekeys.Length >= 1)
                {
                    SHA384 sha       = new SHA384CryptoServiceProvider();
                    byte[] HashValue = sha.ComputeHash(data);
                    session.SignInit(new Mechanism(CKM.ECDSA), (PrivateKey)privatekeys[0]);
                    encryptedData = session.Sign(HashValue);
                }
            }
            finally
            {
                m.Dispose();
                m = null;
            }
            return(encryptedData);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Sign data with a named private key
        /// </summary>
        /// <param name="data">Data to be signed</param>
        /// <param name="privatekeylabel">Label for private key. Can be "Signature" or "Authentication"</param>
        /// <returns>Signed data.</returns>
        public byte[] DoSign(byte[] data, string privatekeylabel)
        {
            if (m == null)
            {
                // link with the pkcs11 DLL
                m = Module.GetInstance(mFileName);
            } //m.Initialize();

            byte[] encryptedData = null;
            try
            {
                Slot slot = m.GetSlotList(true)[0];
                Session session = slot.Token.OpenSession(true);
                ObjectClassAttribute classAttribute = new ObjectClassAttribute(CKO.PRIVATE_KEY);
                ByteArrayAttribute keyLabelAttribute = new ByteArrayAttribute(CKA.LABEL);
                keyLabelAttribute.Value = System.Text.Encoding.UTF8.GetBytes(privatekeylabel);

                session.FindObjectsInit(new P11Attribute[] {
                     classAttribute,
                     keyLabelAttribute
                    }
                );
                P11Object[] privatekeys = session.FindObjects(1) as P11Object[];
                session.FindObjectsFinal();

                if (privatekeys.Length >= 1)
                {
                    session.SignInit(new Mechanism(CKM.SHA1_RSA_PKCS), (PrivateKey)privatekeys[0]);
                    encryptedData = session.Sign(data);
                }

            }
            finally
            {
                m.Dispose();
            }
            return encryptedData;
        }
Ejemplo n.º 10
0
        /// <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_();
            }
            return value;
        }
Ejemplo n.º 11
0
        internal bool Firmar(int in_SlotIndex, string in_PIN, byte[] in_Data, out byte[] out_encryptedData)
        {
            bool result = false;
            out_encryptedData = null;
            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 classAttribute = new ObjectClassAttribute(CKO.PRIVATE_KEY);
                        ByteArrayAttribute keyLabelAttribute = new ByteArrayAttribute(CKA.LABEL);
                        keyLabelAttribute.Value = System.Text.Encoding.UTF8.GetBytes(m_SignLabel);

                        session.FindObjectsInit(new P11Attribute[] {
                                 classAttribute,
                                 keyLabelAttribute
                                }
                                );
                        P11Object[] privatekeys = session.FindObjects(1) as P11Object[];
                        session.FindObjectsFinal();

                        if (privatekeys.Length >= 1)
                        {
                            session.SignInit(new Mechanism(CKM.SHA1_RSA_PKCS), (PrivateKey)privatekeys[0]);
                            out_encryptedData = session.Sign(in_Data);
                        }
                        result = true;
                    }
                    finally
                    {
                        // Log out.
                        session.Logout();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return result;
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Generic function to get string data objects from label
        /// </summary>
        /// <param name="label">Value of label attribute of the object</param>
        /// <returns></returns>
        public string GetData(String label, Boolean displayBytes)
        {
            String value = "";
            if (m == null)
            {
                m = Module.GetInstance(mFileName);
            }
            // pkcs11 module init
            //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 classAttribute = new ByteArrayAttribute(CKA.CLASS);
                    classAttribute.Value = BitConverter.GetBytes((uint)Net.Sf.Pkcs11.Wrapper.CKO.DATA);

                    ByteArrayAttribute labelAttribute = new ByteArrayAttribute(CKA.LABEL);
                    labelAttribute.Value = System.Text.Encoding.UTF8.GetBytes(label);

                    session.FindObjectsInit(new P11Attribute[] { classAttribute, labelAttribute });
                    P11Object[] foundObjects = session.FindObjects(50);
                    int counter = foundObjects.Length;
                    Data data;
                    while (counter > 0)
                    {
                        //foundObjects[counter-1].ReadAttributes(session);
                        //public static BooleanAttribute ReadAttribute(Session session, uint hObj, BooleanAttribute attr)
                        data = foundObjects[counter - 1] as Data;
                        //label = data.Label.ToString();
                        if (label != null)
                            Console.WriteLine(label);
                        if (data.Value.Value != null)
                        {

                            if (displayBytes == true)
                            {
                                value = BitConverter.ToString(data.Value.Value);
                                value = value.Replace("-", "");
                            }
                            else
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            Console.WriteLine(value);
                        }
                        counter--;
                    }
                    session.FindObjectsFinal();
                }
                else
                {
                    Console.WriteLine("No card found\n");
                }
            }
            finally
            {
                // pkcs11 finalize
                m.Dispose();//m.Finalize_();
            }
            return value;
        }
Ejemplo n.º 13
0
        // Generic function to get string data objects from label
        // param name="label": Value of label attribute to the object
        private string GetData(string label)
        {
            string value = "";

            if (m == null)
            {
                m = Module.GetInstance(moduleFileName);
            }

            try
            {
                // Get the slots (cardreader) with a token (eid)
                Slot[] slotlist = m.GetSlotList(true);

                if (slotlist.Length > 0)
                {
                    Slot    slot    = slotlist[0];
                    Session session = CreateSession(slot);

                    if (session != null)
                    {
                        // Search for objects
                        // First, define a search template

                        // "The label attribute of the objects should equal ..."
                        ByteArrayAttribute classAttribute = new ByteArrayAttribute(CKA.CLASS);
                        classAttribute.Value = BitConverter.GetBytes((uint)CKO.DATA);

                        ByteArrayAttribute labelAttribute = new ByteArrayAttribute(CKA.LABEL);
                        labelAttribute.Value = Encoding.UTF8.GetBytes(label);

                        session.FindObjectsInit(new P11Attribute[] { classAttribute, labelAttribute });
                        P11Object[] foundObjects = session.FindObjects(50);
                        int         counter      = foundObjects.Length;
                        Data        data;

                        while (counter > 0)
                        {
                            data  = foundObjects[counter - 1] as Data;
                            label = data.Label.ToString();

                            if (data.Value.Value != null)
                            {
                                value = Encoding.UTF8.GetString(data.Value.Value);
                            }
                            counter--;
                        }

                        session.FindObjectsFinal();
                        session.Dispose();
                    }
                }
                else
                {
                    throw new EIDNotFoundException();
                }
            }
            finally
            {
                m.Dispose();
                m = null;
            }

            return(value);
        }
Ejemplo n.º 14
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.º 15
0
        /// <summary>
        /// Generic function to get string data objects from label
        /// </summary>
        /// <param name="label">Value of label attribute of the object</param>
        /// <returns></returns>
        public string GetData(String label)
        {
            String value = "";

            if (m == null)
            {
                m = Module.GetInstance(mFileName);
            }
            // pkcs11 module init
            //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);
                    Session session = CreateSession(slot);
                    if (session != null)
                    {
                        // Search for objects
                        // First, define a search template

                        // "The label attribute of the objects should equal ..."
                        ByteArrayAttribute classAttribute = new ByteArrayAttribute(CKA.CLASS);
                        classAttribute.Value = BitConverter.GetBytes((uint)Net.Sf.Pkcs11.Wrapper.CKO.DATA);


                        ByteArrayAttribute labelAttribute = new ByteArrayAttribute(CKA.LABEL);
                        labelAttribute.Value = System.Text.Encoding.UTF8.GetBytes(label);


                        session.FindObjectsInit(new P11Attribute[] { classAttribute, labelAttribute });
                        P11Object[] foundObjects = session.FindObjects(50);
                        int         counter      = foundObjects.Length;
                        Data        data;
                        while (counter > 0)
                        {
                            //foundObjects[counter-1].ReadAttributes(session);
                            //public static BooleanAttribute ReadAttribute(Session session, uint hObj, BooleanAttribute attr)
                            data  = foundObjects[counter - 1] as Data;
                            label = data.Label.ToString();
                            if (label != null)
                            {
                                Console.WriteLine(label);
                            }
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                Console.WriteLine(value);
                            }
                            counter--;
                        }
                        session.FindObjectsFinal();
                        session.Dispose();
                    }
                }
                else
                {
                    Console.WriteLine("No card found\n");
                }
            }
            finally
            {
                // pkcs11 finalize
                m.Dispose();//m.Finalize_();
                m = null;
            }
            return(value);
        }
Ejemplo n.º 16
0
        private void getData()
        {
            if (m == null)
            {
                m = Module.GetInstance(mFileName);
            }
            try
            {
                Slot[] slotlist = m.GetSlotList(true);
                if (slotlist.Length > 0)
                {
                    Slot               slot           = slotlist[0];
                    Session            session        = slot.Token.OpenSession(true);
                    ByteArrayAttribute classAttribute = new ByteArrayAttribute(CKA.CLASS);
                    classAttribute.Value = BitConverter.GetBytes(Convert.ToUInt32(CKO.DATA));
                    session.FindObjectsInit(new P11Attribute[] { classAttribute });
                    //aantal objecten om in te lezen
                    P11Object[] foundObjects = session.FindObjects(50);
                    //FOR LOOP om alle gegevens uit te lezen
                    for (int i = 0; i < foundObjects.Count() - 1; i++)
                    {
                        Net.Sf.Pkcs11.Objects.Data data = (Net.Sf.Pkcs11.Objects.Data)foundObjects[i];
                        String label = data.Label.ToString();
                        switch (label.ToLower())
                        {
                        case "[chararrayattribute value=surname]":
                            naam = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=firstnames]":
                            voornaam = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=date_of_birth]":
                            geboortedatum = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=location_of_birth]":
                            geboorteplaats = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=gender]":
                            geslacht = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=national_number]":
                            nationaalnummer = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=address_street_and_number]":
                            straat = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=address_country]":
                            land = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=address_zip]":
                            postcode = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=address_municipality]":
                            gemeente = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=nationality]":
                            nationaliteit = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            break;

                        case "[chararrayattribute value=photo_file]":
                            pasfoto = data.Value.Value;
                            break;
                        }
                    }
                    session.FindObjectsFinal();
                    IDingelezen = true;
                    m.P11Module.Finalize_();
                    m = null;
                }
                else
                {
                    throw new Exception("Controleer de kaartlezer, werd de identiteitskaart correct geplaatst");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Controleer de kaartlezer, werd de identiteitskaart correct geplaatst", "Melding");
            }
        }
Ejemplo n.º 17
0
        public void GetAllData()
        {
            String label = "";
            String value = "";

            byte[] file;
            if (m == null)
            {
                m = Module.GetInstance(mFileName);
            }
            try
            {
                Slot[] slotlist = m.GetSlotList(true);
                if (slotlist.Length > 0)
                {
                    Slot               slot           = slotlist[0];
                    Session            session        = slot.Token.OpenSession(true);
                    ByteArrayAttribute classAttribute = new ByteArrayAttribute(CKA.CLASS);
                    classAttribute.Value = BitConverter.GetBytes((uint)Net.Sf.Pkcs11.Wrapper.CKO.DATA);
                    ByteArrayAttribute labelAttribute = new ByteArrayAttribute(CKA.LABEL);
                    session.FindObjectsInit(new P11Attribute[] { classAttribute });
                    P11Object[] foundObjects = session.FindObjects(50);
                    Data        data;
                    for (int i = 5; i < foundObjects.Length; i++)
                    {
                        data  = foundObjects[i] as Data;
                        label = data.Label.ToString();
                        if (label == null)
                        {
                            label = "";
                        }
                        value = "";
                        switch (label)
                        {
                        case "[CharArrayAttribute Value=DATA_FILE]":
                            break;

                        case "[CharArrayAttribute Value=carddata_serialnumber]":
                            break;

                        case "[CharArrayAttribute Value=carddata_comp_code]":
                            break;

                        case "[CharArrayAttribute Value=carddata_os_number]":
                            break;

                        case "[CharArrayAttribute Value=carddata_os_version]":
                            break;

                        case "[CharArrayAttribute Value=carddata_soft_mask_number]":
                            break;

                        case "[CharArrayAttribute Value=carddata_soft_mask_version]":
                            break;

                        case "[CharArrayAttribute Value=carddata_appl_version]":
                            break;

                        case "[CharArrayAttribute Value=carddata_glob_os_version]":
                            break;

                        case "[CharArrayAttribute Value=carddata_appl_int_version]":
                            break;

                        case "[CharArrayAttribute Value=carddata_pkcs1_support]":
                            break;

                        case "[CharArrayAttribute Value=carddata_key_exchange_version]":
                            break;

                        case "[CharArrayAttribute Value=carddata_appl_lifecycle]":
                            break;

                        case "[CharArrayAttribute Value=card_number]":
                            break;

                        case "[CharArrayAttribute Value=chip_number]":
                            break;

                        case "[CharArrayAttribute Value=validity_begin_date]":
                            break;

                        case "[CharArrayAttribute Value=validity_end_date]":
                            break;

                        case "[CharArrayAttribute Value=issuing_municipality]":
                            break;

                        case "[CharArrayAttribute Value=national_number]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=surname]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=firstnames]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=first_letter_of_third_given_name]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=nationality]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=location_of_birth]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=date_of_birth]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=gender]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=nobility]":
                            break;

                        case "[CharArrayAttribute Value=document_type]":
                            break;

                        case "[CharArrayAttribute Value=special_status]":
                            break;

                        case "[CharArrayAttribute Value=photo_hash]":
                            break;

                        case "[CharArrayAttribute Value=duplicata]":
                            break;

                        case "[CharArrayAttribute Value=special_organization]":
                            break;

                        case "[CharArrayAttribute Value=member_of_family]":
                            break;

                        case "[CharArrayAttribute Value=ADDRESS_FILE]":
                            break;

                        case "[CharArrayAttribute Value=address_street_and_number]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=address_zip]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=address_municipality]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                            }
                            break;

                        case "[CharArrayAttribute Value=PHOTO_FILE]":
                            file = data.Value.Value;
                            break;

                        case "[CharArrayAttribute Value=rncert]":
                            break;

                        case "[CharArrayAttribute Value=SIGN_DATA_FILE]":
                            break;

                        case "[CharArrayAttribute Value=SIGN_ADDRESS_FILE]":
                            break;

                        default:
                            break;
                        }
                        Console.WriteLine(i + " -> " + label + " : " + value);
                    }
                    session.FindObjectsFinal();
                }
                else
                {
                    Console.WriteLine("No card found\n");
                }
            }
            finally
            {
                m.Dispose();
            }
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Generic function to get string data objects from labels and files
        /// </summary>
        /// <param name="labels">Values of label attribute of the object</param>
        /// <param name="files"> Values of file attribute of the object</param>
        /// <param name="outL">  Out value for the labels</param>
        /// <param name="outF">  Out value for the files</param>
        /// <returns></returns>
        public void GetData(String[] labels, String[] files, String[] outL, byte[][] outF)
        {
            if (m == null)
            {
                m = Module.GetInstance(mFileName);
            }
            // pkcs11 module init
            //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 classAttribute = new ByteArrayAttribute(CKA.CLASS);
                    classAttribute.Value = BitConverter.GetBytes((uint)Net.Sf.Pkcs11.Wrapper.CKO.DATA);

                    ByteArrayAttribute labelAttribute = new ByteArrayAttribute(CKA.LABEL);

                    Data        data;
                    int         counter, i = 0;
                    P11Object[] foundObjects;

                    // Get all labels
                    if (labels == null)
                    {
                        labels = new String[] { }
                    }
                    ;
                    foreach (String lab in labels)
                    {
                        Console.WriteLine("Getting >>   " + lab);

                        labelAttribute.Value = System.Text.Encoding.UTF8.GetBytes(lab);

                        session.FindObjectsInit(new P11Attribute[] { classAttribute, labelAttribute });

                        foundObjects = session.FindObjects(50);
                        counter      = foundObjects.Length;

                        while (counter > 0)
                        {
                            //foundObjects[counter-1].ReadAttributes(session);
                            //public static BooleanAttribute ReadAttribute(Session session, uint hObj, BooleanAttribute attr)
                            data = foundObjects[counter - 1] as Data;

                            /*String label = data.Label.ToString();
                             * if (label != null)
                             *  Console.WriteLine(label); */
                            if (data.Value.Value != null)
                            {
                                outL[i] = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                Console.WriteLine("\t" + outL[i]);
                            }
                            counter--;
                        }
                        i++;
                        session.FindObjectsFinal();
                    }


                    // Get all files asked as once
                    if (files == null)
                    {
                        files = new String[] { }
                    }
                    ;

                    i = 0;
                    foreach (String file in files)
                    {
                        Console.WriteLine("Getting FILE >>   " + file);

                        labelAttribute.Value = System.Text.Encoding.UTF8.GetBytes(file);

                        session.FindObjectsInit(new P11Attribute[] { classAttribute, labelAttribute });

                        foundObjects = session.FindObjects(1);
                        if (foundObjects.Length != 0)
                        {
                            data    = foundObjects[0] as Data;
                            outF[i] = data.Value.Value;
                        }

                        i++;
                        session.FindObjectsFinal();
                    }

                    session.Dispose();
                }
                else
                {
                    Console.WriteLine("No card found\n");
                }
            }
            finally
            {
                // pkcs11 finalize
                m.Dispose();//m.Finalize_();
                m = null;
            }
            return;
        }
Ejemplo n.º 19
0
        public Dictionary <string, string> GetDataAll(String [] labels)
        {
            byte[] signatureFile = GetPhotoFile();
            //Console.WriteLine(signatureFile);

            String value = "";
            Dictionary <string, string> map = new Dictionary <string, string>();

            if (m == null)
            {
                m = Module.GetInstance(mFileName);
            }

            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);
                    Session session = CreateSession(slot);
                    if (session != null)
                    {
                        ByteArrayAttribute classAttribute = new ByteArrayAttribute(CKA.CLASS);
                        classAttribute.Value = BitConverter.GetBytes((uint)Net.Sf.Pkcs11.Wrapper.CKO.DATA);
                        ByteArrayAttribute labelAttribute = new ByteArrayAttribute(CKA.LABEL);

                        foreach (string label in labels)
                        {
                            labelAttribute.Value = System.Text.Encoding.UTF8.GetBytes(label);
                            session.FindObjectsInit(new P11Attribute[] { classAttribute, labelAttribute });
                            P11Object[] foundObjects = session.FindObjects(50);
                            int         counter      = foundObjects.Length;
                            Data        data;
                            while (counter > 0)
                            {
                                data = foundObjects[counter - 1] as Data;
                                var labelLocal = data.Label;
                                if (labelLocal != null)
                                {
                                    Console.WriteLine(labelLocal);
                                }
                                if (data.Value.Value != null)
                                {
                                    if (label == "chip_number")
                                    {
                                        value      = BitConverter.ToString(data.Value.Value).Replace("-", "");
                                        map[label] = value;
                                        Console.WriteLine(BitConverter.ToString(data.Value.Value).Replace("-", ""));
                                    }
                                    else if (label == "photo_hash")
                                    {
                                        try
                                        {
                                            value      = Convert.ToBase64String(signatureFile);
                                            map[label] = value;
                                            Console.WriteLine(value);
                                        }
                                        catch (Exception e) { }
                                    }

                                    else
                                    {
                                        value      = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                        map[label] = value;
                                        Console.WriteLine(value);
                                    }
                                }
                                counter--;
                            }
                            session.FindObjectsFinal();
                        }
                        session.Dispose();
                    }
                }
                else

                {
                    Console.WriteLine("No card found\n");
                }
            }
            finally
            {
                // pkcs11 finalize
                m.Dispose();//m.Finalize_();
                m = null;
            }
            return(map);
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Return raw byte data from objects
        /// </summary>
        /// <param name="Filename">Label value of the object</param>
        /// <returns>byte array with file</returns>
        private byte[] GetFile(String Filename)
        {
            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);
                    fileLabel.Value = System.Text.Encoding.UTF8.GetBytes(Filename);
                    ByteArrayAttribute fileData = new ByteArrayAttribute(CKA.CLASS);
                    fileData.Value = BitConverter.GetBytes((uint)Net.Sf.Pkcs11.Wrapper.CKO.DATA);
                    session.FindObjectsInit(new P11Attribute[] {
                        fileLabel,fileData
                    });
                    P11Object[] foundObjects = session.FindObjects(1);
                    if (foundObjects.Length != 0)
                    {
                        Data file = foundObjects[0] as Data;
                        value = file.Value.Value;
                    }
                    session.FindObjectsFinal();
                }
                else
                {
                    Console.WriteLine("No card found\n");
                }
            }
            finally
            {
                // pkcs11 finalize
                m.Dispose();//m.Finalize_();
                m = null;
            }
            return value;
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Gets the description of the first slot (cardreader) found
        /// </summary>
        /// <returns>Description of the first slot found</returns>

        public void GetAllData()
        {
            String label = "";
            String value = "";

            byte[] file;
            if (m == null)
            {
                m = Module.GetInstance(mFileName);
            }
            try
            {
                Slot[] slotlist = m.GetSlotList(true);
                if (slotlist.Length > 0)
                {
                    Slot               slot           = slotlist[0];
                    Session            session        = slot.Token.OpenSession(true);
                    ByteArrayAttribute classAttribute = new ByteArrayAttribute(CKA.CLASS);
                    classAttribute.Value = BitConverter.GetBytes((uint)Net.Sf.Pkcs11.Wrapper.CKO.DATA);
                    ByteArrayAttribute labelAttribute = new ByteArrayAttribute(CKA.LABEL);
                    session.FindObjectsInit(new P11Attribute[] { classAttribute });
                    P11Object[] foundObjects = session.FindObjects(50);
                    Data        data;
                    for (int i = 17; i < foundObjects.Length; i++)
                    {
                        data  = foundObjects[i] as Data;
                        label = data.Label.ToString();
                        if (label == null)
                        {
                            label = "";
                        }
                        value = "";
                        switch (label)
                        {
                        case "[CharArrayAttribute Value=surname]":
                            if (data.Value.Value != null)
                            {
                                value   = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                surName = value;
                            }
                            break;

                        case "[CharArrayAttribute Value=firstnames]":
                            if (data.Value.Value != null)
                            {
                                value    = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                lastName = value;
                            }
                            break;

                        case "[CharArrayAttribute Value=nationality]":
                            if (data.Value.Value != null)
                            {
                                value       = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                nationality = value;
                            }
                            break;

                        case "[CharArrayAttribute Value=national_number]":
                            if (data.Value.Value != null)
                            {
                                value          = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                nationalNumber = value;
                            }
                            break;

                        case "[CharArrayAttribute Value=date_of_birth]":
                            if (data.Value.Value != null)
                            {
                                value = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                dob   = value;
                            }
                            break;

                        case "[CharArrayAttribute Value=gender]":
                            if (data.Value.Value != null)
                            {
                                value  = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                gender = value;
                            }
                            break;

                        case "[CharArrayAttribute Value=address_street_and_number]":
                            if (data.Value.Value != null)
                            {
                                value           = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                streetAndNumber = value;
                            }
                            break;

                        case "[CharArrayAttribute Value=address_zip]":
                            if (data.Value.Value != null)
                            {
                                value    = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                postCode = value;
                            }
                            break;

                        case "[CharArrayAttribute Value=address_municipality]":
                            if (data.Value.Value != null)
                            {
                                value        = System.Text.Encoding.UTF8.GetString(data.Value.Value);
                                municipality = value;
                            }
                            break;

                        default:
                            break;
                        }
                        //Console.WriteLine(i + " -> " + label + " : " + value);
                    }
                    session.FindObjectsFinal();
                }
                else
                {
                    Console.WriteLine("No card found\n");
                }
            }
            finally
            {
                m.Dispose();
            }
        }
Ejemplo n.º 22
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.º 23
0
        public ICardData GetCardData()
        {
            CardData result = new CardData();

            using (var m = Module.GetInstance(mFileName))
            {
                Slot[] slotlist = null;
                try
                {
                    //get cardreaders with a token
                    //this is a very expensive call, hence the ICardData-result object has properties indicating weather or not this call worked
                    slotlist = m.GetSlotList(true);
                    result.CardDataStatus = CardDataStatus.Ready;
                }
                catch (Exception ex)
                {
                    result.CardDataStatus = CardDataStatus.Error;
                    result.Error          = ex;
                }

                if (slotlist != null)
                {
                    foreach (var slot in slotlist)
                    {
                        var card = new Card();
                        card.CardSlot = slot.SlotInfo.SlotDescription;

                        try
                        {
                            // Search for objects
                            // First, define a search template

                            Session session = slot.Token.OpenSession(true);

                            IDictionary <string, byte[]> cardData = new Dictionary <string, byte[]>();

                            // "The label attribute of the objects should equal ..."
                            ByteArrayAttribute classAttribute = new ByteArrayAttribute(CKA.CLASS);
                            classAttribute.Value = BitConverter.GetBytes((uint)Net.Sf.Pkcs11.Wrapper.CKO.DATA);

                            session.FindObjectsInit(new P11Attribute[] { classAttribute });

                            P11Object[] foundObjects = session.FindObjects(50);
                            int         counter      = foundObjects.Length;
                            Data        data;
                            while (counter > 0)
                            {
                                //foundObjects[counter-1].ReadAttributes(session);
                                //public static BooleanAttribute ReadAttribute(Session session, uint hObj, BooleanAttribute attr)
                                data = foundObjects[counter - 1] as Data;
                                //label = data.Label.ToString();
                                if (data.Value.Value != null)
                                {
                                    var label = new string(data.Label.Value).ToLower();
                                    var value = data.Value.Value;

                                    if (!cardData.ContainsKey(label))
                                    {
                                        cardData.Add(label, value);
                                    }
                                    else
                                    {
                                        cardData[label] = value;
                                    }
                                }
                                counter--;
                            }

                            session.FindObjectsFinal();

                            card.ReadDataFrom(cardData);
                            card.CardStatus = CardStatus.Available;
                        }
                        catch (Exception ex)
                        {
                            card.CardStatus = CardStatus.Error;
                            card.Error      = ex;
                        }

                        result.AddCard(card);
                    }
                }
            }

            return(result);
        }