Index: signHash/cryptotoken.cpp
===================================================================
--- signHash/cryptotoken.cpp	(revision ce57b4529df835ba8d80a34aceea16e66d9bed88)
+++ signHash/cryptotoken.cpp	(revision 2506bdb3d48ecf603848082cc9694b1064e41ef5)
@@ -1,3 +1,5 @@
 #include "cryptotoken.h"
+
+#include <QSslCertificate>
 
 #include <assert.h>
@@ -117,5 +119,5 @@
             if ((rv == CKR_PIN_INCORRECT) || (rv == CKR_DATA_LEN_RANGE) || (rv == CKR_ARGUMENTS_BAD))
                 plus = "El PIN introducido es incorrecto!";
-            qDebug(qPrintable("C_login: "  + error + plus));
+            qDebug(qPrintable("C_login: "  + error + " " +plus));
             return CK_INVALID_HANDLE;
         }
@@ -456,4 +458,5 @@
 
     closeSession(hSession);
+    finalize();
 
     return signature;
@@ -482,4 +485,5 @@
 QStringList CryptoToken::getPrivateKeyLabels(QString pin)
 {
+    qDebug("...CryptoToken::getPrivateKeyLabels");
     QStringList list;
 
@@ -531,5 +535,6 @@
         error = returnErrorToQString(rv);
         qDebug("%s", qPrintable("C_FindObjectsInit: "+error));
-        return list;
+        //return list;
+        throw std::runtime_error(qPrintable(error));
     }
 
@@ -554,6 +559,6 @@
         error = returnErrorToQString(rv);
         qDebug(qPrintable("C_FindObjects: "+error));
-        return list;
-
+        //return list;
+        throw std::runtime_error(qPrintable(error));
     }
 
@@ -566,5 +571,6 @@
         error = returnErrorToQString(rv);
         qDebug(qPrintable("C_FindObjectsFinal: "+error));
-        return list;
+        //return list;
+        throw std::runtime_error(qPrintable(error));
     }
     objectHandle.resize(objectCount);
@@ -573,5 +579,5 @@
     if (objectHandle.size() == 0)
     {
-        qDebug (" \nNinguna clave privada encontrada!\n");
+        qDebug (" \nNingun objeto encontrado!\n");
         // cerrar la sesion
         /*rv = C_CloseAllSessions(slotID);
@@ -579,20 +585,318 @@
         qDebug ("\n");
         assert(rv==CKR_OK);*/
-        return list;
+        //return list;
+        throw std::runtime_error(qPrintable(error));
         }
     else
     {
         //qDebug (" \nObjeto encontrado (object=0x%04X)\n", hObject);
-        qDebug("se econtraron %d claves", objectHandle.size());
+        qDebug("se econtraron %d objetos", objectHandle.size());
         //qDebug (" \nClave privada encontrada:  (clave = 0x%4X)\n", hObject);
         for (int i=0; i< objectHandle.size(); i++)
         {
-            qDebug("clave encontrada: 0x%4X", objectHandle[i]);
+            qDebug("objeto encontrado: 0x%4X", objectHandle[i]);
+            list.append(getKeyLabel(hSession, objectHandle[i]));
         }
+    }
+
+    closeSession(hSession);
+    finalize();
+
+    return list;
+}
+
+QString CryptoToken::getKeyLabel(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE key)
+{
+    qDebug("CryptoToken::getKeyLabel");
+
+    CK_RV rv = CKR_OK;
+    CK_ATTRIBUTE keyid_attr[1] = {
+        { CKA_LABEL, NULL, 0 }
+        //{ CKA_VALUE, NULL, 0 }
+    };
+
+    if ((rv = C_GetAttributeValue(hSession, key, keyid_attr, 1)) != CKR_OK)
+    {
+        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion del atributo ID"));
+        qDebug("fallo la obtencion del atributo ID");
+        return "";
+    }
+
+    if ((keyid_attr[0].pValue = malloc(keyid_attr[0].ulValueLen)) == NULL)
+    {
+        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la asignacion de memoria del atributo ID"));
+        qDebug("fallo la asignacion de memoria del atributo ID");
+        return "";
+    }
+    if ((rv = C_GetAttributeValue(hSession, key, keyid_attr, 1)) != CKR_OK)
+    {
+        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion 2 del atributo ID"));
+        qDebug("fallo la obtencion 2 del atributo ID");
+        return "";
+    }
+
+    //qDebug("\n*-** VALOR DE ID: %s\n", keyid_attr[0].pValue);
+    qDebug("\n*-** VALOR DE atributo: %s\n", keyid_attr[0].pValue);
+    QString label = (const char *) keyid_attr[0].pValue;
+
+    qDebug(qPrintable(label));
+    qDebug(qPrintable(QString::number(label.size())));
+
+    return label;
+}
+
+
+QList<QStringList> CryptoToken::getCertificateCNandExpirationDate(QString pin)
+{
+    qDebug("...CryptoToken::getCertificateCNandExpirationDate");
+    QList<QStringList> list;
+
+    QString error("");
+    CK_RV rv;
+
+    CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
+    CK_SLOT_ID slotID;
+
+    // initialize criptoki
+    rv = C_Initialize(NULL_PTR);
+    if (rv != CKR_OK)
+    {
+        if (rv == CKR_DEVICE_ERROR)
+        {
+            qDebug("CKR_DEVICE_ERROR");
+            error = returnErrorToQString(rv);
+            qDebug(qPrintable(error));
+            throw std::runtime_error(qPrintable(error));
+        }
+        qDebug("****");
+        error = returnErrorToQString(rv);
+        qDebug(qPrintable(error));
+        throw std::runtime_error(qPrintable(error));
+    }
+
+    hSession = openSession((char *) qPrintable(pin), slotID);
+    if (hSession == CK_INVALID_HANDLE)
+    {
+        qDebug("Fallo ptr_SC->openSession");
+        rv = C_Finalize(NULL_PTR);
+        qDebug("C_Finalize: rv = %x",rv);
+        assert(rv == CKR_OK);
+        throw std::runtime_error("Error openning a session inside the device");
+    }
+
+    CK_OBJECT_CLASS objectClass = CKO_CERTIFICATE;
+
+    CK_OBJECT_HANDLE hObject = CK_INVALID_HANDLE;
+    CK_ATTRIBUTE searchAttribute = {CKA_CLASS, &objectClass, sizeof(objectClass)};
+    CK_ULONG ulCount = 0;
+
+    rv = C_FindObjectsInit(hSession, &searchAttribute, 1);
+    qDebug ("C_FindObjectsInit rv = %x",rv);
+    qDebug ("\n");
+    //assert(rv==CKR_OK);
+    if (rv != CKR_OK)
+    {
+        error = returnErrorToQString(rv);
+        qDebug("%s", qPrintable("C_FindObjectsInit: "+error));
+        //return list;
+        throw std::runtime_error(qPrintable(error));
+    }
+
+    CK_ULONG objectCount = 3;
+    std::vector<CK_OBJECT_HANDLE> objectHandle(objectCount);
+
+    // se realiza la busqueda
+    //rv = C_FindObjects(hSession, &hObject, 2, &ulCount);
+    rv = C_FindObjects(hSession, objectHandle.data(), objectHandle.size(), &objectCount);
+
+    qDebug ("C_FindObjects rv = %x",rv);
+    qDebug ("\n");
+    //assert(rv==CKR_OK);
+
+    qDebug("*-*-*-*-*getPrivateKeyLabels VALOR DE objectCount %u: ", objectCount);
+    //if (rv != CKR_OK)
+    if ((rv != CKR_OK) )//|| (ulCount == 0))
+    {
+        if (ulCount == 0)
+            qDebug("ulCount == 0");
+
+        error = returnErrorToQString(rv);
+        qDebug(qPrintable("C_FindObjects: "+error));
+        //return list;
+        throw std::runtime_error(qPrintable(error));
+    }
+
+    rv = C_FindObjectsFinal(hSession);
+    qDebug ("C_FindObjectsFinal rv = %x",rv);
+    qDebug ("\n");
+    //assert(rv==CKR_OK);
+    if (rv != CKR_OK)
+    {
+        error = returnErrorToQString(rv);
+        qDebug(qPrintable("C_FindObjectsFinal: "+error));
+        //return list;
+        throw std::runtime_error(qPrintable(error));
+    }
+    objectHandle.resize(objectCount);
+
+    //if (ulCount == 0)
+    if (objectHandle.size() == 0)
+    {
+        qDebug (" \nNingun objeto encontrado!\n");
+        // cerrar la sesion
+        /*rv = C_CloseAllSessions(slotID);
+        qDebug("C_CloseAllSessions rv = %x", rv);
+        qDebug ("\n");
+        assert(rv==CKR_OK);*/
+        //return list;
+        throw std::runtime_error(qPrintable(error));
+        }
+    else
+    {
+        //qDebug (" \nObjeto encontrado (object=0x%04X)\n", hObject);
+        qDebug("se econtraron %d objetos", objectHandle.size());
+        //qDebug (" \nClave privada encontrada:  (clave = 0x%4X)\n", hObject);
+
+        for (int i=0; i< objectHandle.size(); i++)
+        {
+            QStringList certInformation;
+            qDebug("objeto encontrado: 0x%4X", objectHandle[i]);
+            certInformation = getCertificateInformation(hSession, objectHandle[i]);
+            //list.append(getCertificateInformation(hSession, objectHandle[i])[0]);
+            //list.append(getCertificateInformation(hSession, objectHandle[i])[0]);
+            list.append(certInformation);
+        }
+    }
+
+    closeSession(hSession);
+    finalize();
+
+    return list;
+}
+
+QStringList CryptoToken::getCertificateInformation(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE certificate)
+{
+    qDebug("...getCertificateInformation...");
+
+    CK_RV rv = CKR_OK;
+    CK_ATTRIBUTE keyid_attr[2] = {
+        { CKA_LABEL, NULL, 0 },
+        { CKA_VALUE, NULL, 0 }
+    };
+
+    QStringList list;
+
+
+    if ((rv = C_GetAttributeValue(hSession, certificate, keyid_attr, 2)) != CKR_OK)
+    {
+        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion del atributo ID"));
+        qDebug("fallo la obtencion del atributo ID");
         return list;
     }
 
+    if ((keyid_attr[1].pValue = malloc(keyid_attr[1].ulValueLen)) == NULL)
+    {
+        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la asignacion de memoria del atributo ID"));
+        qDebug("fallo la asignacion de memoria del atributo ID");
+        return list;
+    }
+    if ((rv = C_GetAttributeValue(hSession, certificate, keyid_attr, 2)) != CKR_OK)
+    {
+        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion 2 del atributo ID"));
+        qDebug("fallo la obtencion 2 del atributo ID");
+        return list;
+    }
+
+    //qDebug("\n*-** VALOR DE ID: %s\n", keyid_attr[0].pValue);
+    //qDebug("\n*-** VALOR DE atributo: %s\n", keyid_attr[1].pValue);
+    //QString x = (const char *) keyid_attr[1].pValue;
+
+    //qDebug(qPrintable(x));
+    //qDebug(qPrintable(QString::number(x.size())));
+
+
+    //qDebug("prueba de generar un QSslCertificate");
+
+    QSslCertificate qcert(QByteArray((const char *) keyid_attr[1].pValue, (int) keyid_attr[1].ulValueLen), QSsl::Der);
+
+    if (qcert.isNull()){
+        qDebug("qcert es null");
+    }
+
+    //qDebug("mira el contenido qcert");
+
+    /*
+    QStringList list = qcert.subjectInfo(QSslCertificate::EmailAddress);
+    QStringList l2 = qcert.subjectInfo(QSslCertificate::DistinguishedNameQualifier);
+    QStringList l3 = qcert.subjectInfo( QSslCertificate::CommonName );
+    QStringList l4 = qcert.subjectInfo( QSslCertificate::Organization );
+    QStringList l5 = qcert.subjectInfo( QSslCertificate::LocalityName );
+    QStringList l6 = qcert.subjectInfo( QSslCertificate::OrganizationalUnitName );
+    QStringList l7 = qcert.subjectInfo( QSslCertificate::StateOrProvinceName );
+    */
+    list.append(qcert.subjectInfo( QSslCertificate::CommonName ));
+    list.append(qcert.expiryDate().date().toString("dd.MM.yyyy"));
+
+    //qDebug(qPrintable(qcert.effectiveDate().date().toString("dd.MM.yyyy")));
+    //qDebug(qPrintable(qcert.expiryDate().date().toString("dd.MM.yyyy")));
+
     return list;
 }
+
+
+QList<QStringList> CryptoToken::getDeviceCertificates(QString pin)
+{
+    qDebug("...getDeviceCertificates");
+
+
+    QStringList labelList = getPrivateKeyLabels(pin);
+
+    QList<QStringList> nameExpirationList = getCertificateCNandExpirationDate(pin);
+
+    QList<QStringList> certificateInformationList;
+
+    for (int i=0; i<labelList.size(); i++)
+    {
+        QStringList l;
+        qDebug("... antes de insertar label");
+        l.append(labelList[i]);
+        qDebug("... antes de insertar commonName");
+        l.append(nameExpirationList[i][0]);
+        qDebug("... antes de insertar expirationDate");
+        l.append(nameExpirationList[i][1]);
+
+        qDebug("... antes de insertar en certificateInformationList");
+        certificateInformationList.append(l);
+    }
+
+    return certificateInformationList;
+}
+
+
+void CryptoToken::getCertificateAttributes(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hCert)
+{
+   CK_ATTRIBUTE pTemplate[2] = {
+       //List your desired attributes here
+       { CKA_LABEL, NULL, 0 }
+       //{ CKA_LABEL, NULL, 0 }
+   };
+
+   CK_RV rv = C_GetAttributeValue(hSession, hCert, pTemplate, /*pTemplateLen*/ 2);
+   if (rv == CKR_OK) {
+       //here you have your desired certificate attributes
+       qDebug("chequea los atributos");
+
+   }
+}
+
+
+
+
+
+
+
+
+
+
 
 // slot para obtener informacion del modulo PKCS11
@@ -672,4 +976,7 @@
 }
 
+
+
+
 QString CryptoToken::returnErrorToQString(CK_RV rv)
 {
