Changeset 2506bdb in terepaima for signHash/cryptotoken.cpp


Ignore:
Timestamp:
Oct 4, 2017, 4:08:55 PM (7 years ago)
Author:
Antonio Araujo <aaraujo@…>
Branches:
master
Children:
2aa151c
Parents:
ce57b45
Message:

Agreadas funciones para obtener información de los certificados firmantes que se encuentran en un dispositivo criptográfico.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • signHash/cryptotoken.cpp

    rce57b45 r2506bdb  
    11#include "cryptotoken.h"
     2
     3#include <QSslCertificate>
    24
    35#include <assert.h>
     
    117119            if ((rv == CKR_PIN_INCORRECT) || (rv == CKR_DATA_LEN_RANGE) || (rv == CKR_ARGUMENTS_BAD))
    118120                plus = "El PIN introducido es incorrecto!";
    119             qDebug(qPrintable("C_login: "  + error + plus));
     121            qDebug(qPrintable("C_login: "  + error + " " +plus));
    120122            return CK_INVALID_HANDLE;
    121123        }
     
    456458
    457459    closeSession(hSession);
     460    finalize();
    458461
    459462    return signature;
     
    482485QStringList CryptoToken::getPrivateKeyLabels(QString pin)
    483486{
     487    qDebug("...CryptoToken::getPrivateKeyLabels");
    484488    QStringList list;
    485489
     
    531535        error = returnErrorToQString(rv);
    532536        qDebug("%s", qPrintable("C_FindObjectsInit: "+error));
    533         return list;
     537        //return list;
     538        throw std::runtime_error(qPrintable(error));
    534539    }
    535540
     
    554559        error = returnErrorToQString(rv);
    555560        qDebug(qPrintable("C_FindObjects: "+error));
    556         return list;
    557 
     561        //return list;
     562        throw std::runtime_error(qPrintable(error));
    558563    }
    559564
     
    566571        error = returnErrorToQString(rv);
    567572        qDebug(qPrintable("C_FindObjectsFinal: "+error));
    568         return list;
     573        //return list;
     574        throw std::runtime_error(qPrintable(error));
    569575    }
    570576    objectHandle.resize(objectCount);
     
    573579    if (objectHandle.size() == 0)
    574580    {
    575         qDebug (" \nNinguna clave privada encontrada!\n");
     581        qDebug (" \nNingun objeto encontrado!\n");
    576582        // cerrar la sesion
    577583        /*rv = C_CloseAllSessions(slotID);
     
    579585        qDebug ("\n");
    580586        assert(rv==CKR_OK);*/
    581         return list;
     587        //return list;
     588        throw std::runtime_error(qPrintable(error));
    582589        }
    583590    else
    584591    {
    585592        //qDebug (" \nObjeto encontrado (object=0x%04X)\n", hObject);
    586         qDebug("se econtraron %d claves", objectHandle.size());
     593        qDebug("se econtraron %d objetos", objectHandle.size());
    587594        //qDebug (" \nClave privada encontrada:  (clave = 0x%4X)\n", hObject);
    588595        for (int i=0; i< objectHandle.size(); i++)
    589596        {
    590             qDebug("clave encontrada: 0x%4X", objectHandle[i]);
     597            qDebug("objeto encontrado: 0x%4X", objectHandle[i]);
     598            list.append(getKeyLabel(hSession, objectHandle[i]));
    591599        }
     600    }
     601
     602    closeSession(hSession);
     603    finalize();
     604
     605    return list;
     606}
     607
     608QString CryptoToken::getKeyLabel(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE key)
     609{
     610    qDebug("CryptoToken::getKeyLabel");
     611
     612    CK_RV rv = CKR_OK;
     613    CK_ATTRIBUTE keyid_attr[1] = {
     614        { CKA_LABEL, NULL, 0 }
     615        //{ CKA_VALUE, NULL, 0 }
     616    };
     617
     618    if ((rv = C_GetAttributeValue(hSession, key, keyid_attr, 1)) != CKR_OK)
     619    {
     620        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion del atributo ID"));
     621        qDebug("fallo la obtencion del atributo ID");
     622        return "";
     623    }
     624
     625    if ((keyid_attr[0].pValue = malloc(keyid_attr[0].ulValueLen)) == NULL)
     626    {
     627        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la asignacion de memoria del atributo ID"));
     628        qDebug("fallo la asignacion de memoria del atributo ID");
     629        return "";
     630    }
     631    if ((rv = C_GetAttributeValue(hSession, key, keyid_attr, 1)) != CKR_OK)
     632    {
     633        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion 2 del atributo ID"));
     634        qDebug("fallo la obtencion 2 del atributo ID");
     635        return "";
     636    }
     637
     638    //qDebug("\n*-** VALOR DE ID: %s\n", keyid_attr[0].pValue);
     639    qDebug("\n*-** VALOR DE atributo: %s\n", keyid_attr[0].pValue);
     640    QString label = (const char *) keyid_attr[0].pValue;
     641
     642    qDebug(qPrintable(label));
     643    qDebug(qPrintable(QString::number(label.size())));
     644
     645    return label;
     646}
     647
     648
     649QList<QStringList> CryptoToken::getCertificateCNandExpirationDate(QString pin)
     650{
     651    qDebug("...CryptoToken::getCertificateCNandExpirationDate");
     652    QList<QStringList> list;
     653
     654    QString error("");
     655    CK_RV rv;
     656
     657    CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE;
     658    CK_SLOT_ID slotID;
     659
     660    // initialize criptoki
     661    rv = C_Initialize(NULL_PTR);
     662    if (rv != CKR_OK)
     663    {
     664        if (rv == CKR_DEVICE_ERROR)
     665        {
     666            qDebug("CKR_DEVICE_ERROR");
     667            error = returnErrorToQString(rv);
     668            qDebug(qPrintable(error));
     669            throw std::runtime_error(qPrintable(error));
     670        }
     671        qDebug("****");
     672        error = returnErrorToQString(rv);
     673        qDebug(qPrintable(error));
     674        throw std::runtime_error(qPrintable(error));
     675    }
     676
     677    hSession = openSession((char *) qPrintable(pin), slotID);
     678    if (hSession == CK_INVALID_HANDLE)
     679    {
     680        qDebug("Fallo ptr_SC->openSession");
     681        rv = C_Finalize(NULL_PTR);
     682        qDebug("C_Finalize: rv = %x",rv);
     683        assert(rv == CKR_OK);
     684        throw std::runtime_error("Error openning a session inside the device");
     685    }
     686
     687    CK_OBJECT_CLASS objectClass = CKO_CERTIFICATE;
     688
     689    CK_OBJECT_HANDLE hObject = CK_INVALID_HANDLE;
     690    CK_ATTRIBUTE searchAttribute = {CKA_CLASS, &objectClass, sizeof(objectClass)};
     691    CK_ULONG ulCount = 0;
     692
     693    rv = C_FindObjectsInit(hSession, &searchAttribute, 1);
     694    qDebug ("C_FindObjectsInit rv = %x",rv);
     695    qDebug ("\n");
     696    //assert(rv==CKR_OK);
     697    if (rv != CKR_OK)
     698    {
     699        error = returnErrorToQString(rv);
     700        qDebug("%s", qPrintable("C_FindObjectsInit: "+error));
     701        //return list;
     702        throw std::runtime_error(qPrintable(error));
     703    }
     704
     705    CK_ULONG objectCount = 3;
     706    std::vector<CK_OBJECT_HANDLE> objectHandle(objectCount);
     707
     708    // se realiza la busqueda
     709    //rv = C_FindObjects(hSession, &hObject, 2, &ulCount);
     710    rv = C_FindObjects(hSession, objectHandle.data(), objectHandle.size(), &objectCount);
     711
     712    qDebug ("C_FindObjects rv = %x",rv);
     713    qDebug ("\n");
     714    //assert(rv==CKR_OK);
     715
     716    qDebug("*-*-*-*-*getPrivateKeyLabels VALOR DE objectCount %u: ", objectCount);
     717    //if (rv != CKR_OK)
     718    if ((rv != CKR_OK) )//|| (ulCount == 0))
     719    {
     720        if (ulCount == 0)
     721            qDebug("ulCount == 0");
     722
     723        error = returnErrorToQString(rv);
     724        qDebug(qPrintable("C_FindObjects: "+error));
     725        //return list;
     726        throw std::runtime_error(qPrintable(error));
     727    }
     728
     729    rv = C_FindObjectsFinal(hSession);
     730    qDebug ("C_FindObjectsFinal rv = %x",rv);
     731    qDebug ("\n");
     732    //assert(rv==CKR_OK);
     733    if (rv != CKR_OK)
     734    {
     735        error = returnErrorToQString(rv);
     736        qDebug(qPrintable("C_FindObjectsFinal: "+error));
     737        //return list;
     738        throw std::runtime_error(qPrintable(error));
     739    }
     740    objectHandle.resize(objectCount);
     741
     742    //if (ulCount == 0)
     743    if (objectHandle.size() == 0)
     744    {
     745        qDebug (" \nNingun objeto encontrado!\n");
     746        // cerrar la sesion
     747        /*rv = C_CloseAllSessions(slotID);
     748        qDebug("C_CloseAllSessions rv = %x", rv);
     749        qDebug ("\n");
     750        assert(rv==CKR_OK);*/
     751        //return list;
     752        throw std::runtime_error(qPrintable(error));
     753        }
     754    else
     755    {
     756        //qDebug (" \nObjeto encontrado (object=0x%04X)\n", hObject);
     757        qDebug("se econtraron %d objetos", objectHandle.size());
     758        //qDebug (" \nClave privada encontrada:  (clave = 0x%4X)\n", hObject);
     759
     760        for (int i=0; i< objectHandle.size(); i++)
     761        {
     762            QStringList certInformation;
     763            qDebug("objeto encontrado: 0x%4X", objectHandle[i]);
     764            certInformation = getCertificateInformation(hSession, objectHandle[i]);
     765            //list.append(getCertificateInformation(hSession, objectHandle[i])[0]);
     766            //list.append(getCertificateInformation(hSession, objectHandle[i])[0]);
     767            list.append(certInformation);
     768        }
     769    }
     770
     771    closeSession(hSession);
     772    finalize();
     773
     774    return list;
     775}
     776
     777QStringList CryptoToken::getCertificateInformation(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE certificate)
     778{
     779    qDebug("...getCertificateInformation...");
     780
     781    CK_RV rv = CKR_OK;
     782    CK_ATTRIBUTE keyid_attr[2] = {
     783        { CKA_LABEL, NULL, 0 },
     784        { CKA_VALUE, NULL, 0 }
     785    };
     786
     787    QStringList list;
     788
     789
     790    if ((rv = C_GetAttributeValue(hSession, certificate, keyid_attr, 2)) != CKR_OK)
     791    {
     792        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion del atributo ID"));
     793        qDebug("fallo la obtencion del atributo ID");
    592794        return list;
    593795    }
    594796
     797    if ((keyid_attr[1].pValue = malloc(keyid_attr[1].ulValueLen)) == NULL)
     798    {
     799        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la asignacion de memoria del atributo ID"));
     800        qDebug("fallo la asignacion de memoria del atributo ID");
     801        return list;
     802    }
     803    if ((rv = C_GetAttributeValue(hSession, certificate, keyid_attr, 2)) != CKR_OK)
     804    {
     805        //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion 2 del atributo ID"));
     806        qDebug("fallo la obtencion 2 del atributo ID");
     807        return list;
     808    }
     809
     810    //qDebug("\n*-** VALOR DE ID: %s\n", keyid_attr[0].pValue);
     811    //qDebug("\n*-** VALOR DE atributo: %s\n", keyid_attr[1].pValue);
     812    //QString x = (const char *) keyid_attr[1].pValue;
     813
     814    //qDebug(qPrintable(x));
     815    //qDebug(qPrintable(QString::number(x.size())));
     816
     817
     818    //qDebug("prueba de generar un QSslCertificate");
     819
     820    QSslCertificate qcert(QByteArray((const char *) keyid_attr[1].pValue, (int) keyid_attr[1].ulValueLen), QSsl::Der);
     821
     822    if (qcert.isNull()){
     823        qDebug("qcert es null");
     824    }
     825
     826    //qDebug("mira el contenido qcert");
     827
     828    /*
     829    QStringList list = qcert.subjectInfo(QSslCertificate::EmailAddress);
     830    QStringList l2 = qcert.subjectInfo(QSslCertificate::DistinguishedNameQualifier);
     831    QStringList l3 = qcert.subjectInfo( QSslCertificate::CommonName );
     832    QStringList l4 = qcert.subjectInfo( QSslCertificate::Organization );
     833    QStringList l5 = qcert.subjectInfo( QSslCertificate::LocalityName );
     834    QStringList l6 = qcert.subjectInfo( QSslCertificate::OrganizationalUnitName );
     835    QStringList l7 = qcert.subjectInfo( QSslCertificate::StateOrProvinceName );
     836    */
     837    list.append(qcert.subjectInfo( QSslCertificate::CommonName ));
     838    list.append(qcert.expiryDate().date().toString("dd.MM.yyyy"));
     839
     840    //qDebug(qPrintable(qcert.effectiveDate().date().toString("dd.MM.yyyy")));
     841    //qDebug(qPrintable(qcert.expiryDate().date().toString("dd.MM.yyyy")));
     842
    595843    return list;
    596844}
     845
     846
     847QList<QStringList> CryptoToken::getDeviceCertificates(QString pin)
     848{
     849    qDebug("...getDeviceCertificates");
     850
     851
     852    QStringList labelList = getPrivateKeyLabels(pin);
     853
     854    QList<QStringList> nameExpirationList = getCertificateCNandExpirationDate(pin);
     855
     856    QList<QStringList> certificateInformationList;
     857
     858    for (int i=0; i<labelList.size(); i++)
     859    {
     860        QStringList l;
     861        qDebug("... antes de insertar label");
     862        l.append(labelList[i]);
     863        qDebug("... antes de insertar commonName");
     864        l.append(nameExpirationList[i][0]);
     865        qDebug("... antes de insertar expirationDate");
     866        l.append(nameExpirationList[i][1]);
     867
     868        qDebug("... antes de insertar en certificateInformationList");
     869        certificateInformationList.append(l);
     870    }
     871
     872    return certificateInformationList;
     873}
     874
     875
     876void CryptoToken::getCertificateAttributes(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hCert)
     877{
     878   CK_ATTRIBUTE pTemplate[2] = {
     879       //List your desired attributes here
     880       { CKA_LABEL, NULL, 0 }
     881       //{ CKA_LABEL, NULL, 0 }
     882   };
     883
     884   CK_RV rv = C_GetAttributeValue(hSession, hCert, pTemplate, /*pTemplateLen*/ 2);
     885   if (rv == CKR_OK) {
     886       //here you have your desired certificate attributes
     887       qDebug("chequea los atributos");
     888
     889   }
     890}
     891
     892
     893
     894
     895
     896
     897
     898
     899
     900
    597901
    598902// slot para obtener informacion del modulo PKCS11
     
    672976}
    673977
     978
     979
     980
    674981QString CryptoToken::returnErrorToQString(CK_RV rv)
    675982{
Note: See TracChangeset for help on using the changeset viewer.