简体   繁体   中英

Read text file from smart card with pkcs#11

I have a blank smart card (SLE66CX322P, Cardos 4.3b) and a card reader/writer ( Gemalto CT 40) . Using a software I can initialize the card, generate keys, certificates etc. I also can create a simple ASCII file with some text which will be stored on the smart card. My qustion is, how can I read out the content of this ASCII file in C/C++ using the pkcs#11 API ?

Here comes what I have tried so far (using also Qt):

---------- cardreader.h ------------

#ifndef CARDREADER_H
#define CARDREADER_H

#include "cm-pkcs11.h"
#include <QCoreApplication>
#include <QObject>
#include <QtDebug>

class CardReader: public QObject
{
    Q_OBJECT

private:

    CK_RV rv;
    CK_ULONG slotCount;
    CK_SLOT_ID slotIds[10];
    CK_SLOT_ID slotId;
    CK_SESSION_HANDLE session;
    CK_TOKEN_INFO_PTR info;

    void readCard();

public:

    explicit CardReader(QObject *parent = 0);
};

#endif // CARDREADER_H

------- cardreader.cpp ----------

#include "cardreader.h"

CardReader::CardReader(QObject *parent) : QObject(parent)
{
    readCard();
}

/***********************************************************************/

void CardReader::readCard()
{
    rv = C_Initialize(NULL_PTR);

    slotCount = 10;
    rv = C_GetSlotList(CK_TRUE, slotIds, &slotCount);
    qWarning() << "Found" << slotCount << "slots";

    if (rv != CKR_OK || slotCount < 1)
    {
        qWarning() << "No slots found -> exit";
        return;
    }

    slotId = slotIds[0];

    rv = C_OpenSession(slotId, CKF_SERIAL_SESSION|CKF_RW_SESSION, NULL_PTR, NULL_PTR, &session);

    if (rv != CKR_OK)
    {
        qWarning() << "Sessions could not be opened -> exit";
        qWarning() << "RV (as hex value) = " << QString("%1").arg(rv, 0, 16);
        return;
    }

    /*********************************************************************/

    CK_OBJECT_CLASS dataClass = CKO_DATA;
    CK_OBJECT_HANDLE handleObject;
    CK_UTF8CHAR label[] = {"MyLabel"};
    CK_ULONG ulCount = 4ul;
    CK_CHAR application[] = {"TestApplication"};
    CK_BYTE dataValue[] = {"MyData"};
    CK_BBOOL valid = CK_TRUE;
    CK_ATTRIBUTE dataTemp[] =
    {
        {CKA_CLASS, &dataClass, sizeof(dataClass)},
        {CKA_VALUE, dataValue, sizeof(dataValue)},
        {CKA_LABEL, label, sizeof(label)-1},
        //{CKA_APPLICATION, application, sizeof(application)}
        {CKA_TOKEN, &valid, sizeof(true)}
    };

    rv = C_FindObjectsInit(session, dataTemp, 0);
    if (rv != CKR_OK)
    {
        qWarning() << "C_FindObjectsInit Error -> exit";
        qWarning() << "C_FindObjectsInit Error" << QString("%1").arg(rv, 0, 16);
        return;
    }


    while (1)
    {
        rv = C_FindObjects(session, &handleObject, 1, &ulCount);
        qWarning() << "C_FindObjects Result =" << QString("%1").arg(rv, 0, 16) << ", count =" << ulCount;
        if (rv != CKR_OK || ulCount == 0)
            break;

        rv = C_GetAttributeValue(session, handleObject, dataTemp, ulCount);

        if (rv != CKR_OK)
        {
            qWarning() << "C_GetAttributeValue error -> exit";
            qWarning() << "RV (as hex value) = " << QString("%1").arg(rv, 0, 16) << ", count = " << ulCount;
            return;
        }

        qWarning() << (const char *) dataTemp[0].pValue;
        qWarning() << (const char *) dataTemp[1].pValue;
        qWarning() << (const char *) dataTemp[2].pValue;
        qWarning() << (const char *) dataTemp[3].pValue;
    }
}

---------- main.cpp ----------

#include "cardreader.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    CardReader c;

    return a.exec();
}

The result is that the block

        qWarning() << (const char *) dataTemp[0].pValue;
        qWarning() << (const char *) dataTemp[1].pValue;
        qWarning() << (const char *) dataTemp[2].pValue;
        qWarning() << (const char *) dataTemp[3].pValue;

is printed four times in the console but always with MyLabel and MyData as result which I have specified at the beginning of CardReader::readCard() . The content of the text file on the smart card is different of course, but is not shown in the output unfortunately.

I'd like also to mention that I was also able to use the C_CreateObject() function. This generated a second ASCII file on the smart card. But this second file I could not read out either with my code.

The most probable cause:

  • Your ulCount variable gets overwritten by C_FindObjects() to 1 .
  • You need to re-assign it to 4 again before calling the C_GetAttributeValue .
  • This way your code reads only the first attribute in the template.

Some additional (random) notes:

  • As you do not call C_Login() , you can view/access only public objects (objects with set CKA_PRIVATE are hidden for you).
  • Zero ulCount argument for C_FindObjectsInit() causes that your code enumerates all token objects -- you probably wanted the CKA_CLASS and CKA_LABEL filter to be effective.
  • I would recommend not to use the same template for C_FindObjectsInit() and C_GetAttributeValue()
  • Your dataValue buffer is quite small -- are you sure that the value will fit?

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM