0

I want to install all I need to use libp11 and use libp11.

What is my environment and needs:

I work on Windows 10, 64 bits. I add package with pacman linux command on my mingw32 terminal (msys64 version of 2022/09/04). I work on a Qt Creator editor and I have a .pro file to configure my Qt project. I want to develop a C++ module for my application which use libp11 to get the Yubikey bin number and decrypt file. Why I use a mingw32 terminal and not the mingw64, because, for the moment, the project is always in develop in QT 4.8.

What I do:

I read the README file and follow the installation step of the INSTALL file (first I follow the MinGW / MSYS chapter, then I follow the MSYS2 chapter.) I do much more, but I don't remember every thing and I go in many wrong ways.

My problems and questions

I try to follow the examples find in GitHub. I help me with the site cpp.hotexemples.com to find the second parameter for the PKCS11_CTX_load function. I find the st_engine_ctx structure on this project.

The project file:

TEMPLATE = app
CONFIG += console c++17
CONFIG -= app_bundle
CONFIG += qt

LIBS += \
    -lp11 \
    -lssl \
    -lcrypto

SOURCES += \
    TestYubikey.cpp \
    main.cpp

HEADERS += \
    TestYubikey.h

The header file:

#ifndef TESTYUBIKEY_H
#define TESTYUBIKEY_H

#include <libp11.h>

#include <cstdio>
#include <cstring>

#include <openssl/err.h>
#include <openssl/crypto.h>
#include <openssl/objects.h>
#include <openssl/engine.h>
#include <openssl/ui.h>

/* Engine configuration */
/* The PIN used for login. Cache for the ctx_get_pin function.
 * The memory for this PIN is always owned internally,
 * and may be freed as necessary. Before freeing, the PIN
 * must be whitened, to prevent security holes.
 */
struct st_engine_ctx
{
    char *pin               = nullptr;
    size_t pin_length       = 0;
    int verbose             = 0;
    char *module            = nullptr;
    char *init_args         = nullptr;
    UI_METHOD *ui_method    = nullptr;
    void *callback_data     = nullptr;
    int force_login         = 0;

    /* Engine initialization mutex */
#if OPENSSL_VERSION_NUMBER >= 0x10100004L && !defined(LIBRESSL_VERSION_NUMBER)
    CRYPTO_RWLOCK *rwlock   = nullptr;
#else
    int rwlock;
#endif

    /* Current operations */
    PKCS11_CTX *pkcs11_ctx  = nullptr;
    PKCS11_SLOT *slot_list  = nullptr;
    unsigned int slot_count = 0;
};

class TestYubikey
{
public:
    TestYubikey();
};

#endif // TESTYUBIKEY_H

The source file:

//libp11 is a wrapper library for PKCS#11 modules with OpenSSL interface

#include "TestYubikey.h"

#include <iostream>

TestYubikey::TestYubikey()
{
    // Create a new libp11 context
    PKCS11_CTX *ctx = PKCS11_CTX_new();
    std::cout << "ctx = " << ctx << std::endl;

    /* load pkcs #11 module */
    int rc = PKCS11_CTX_load(ctx, "C:\\msys64\\mingw32\\lib\\engines-1_1\\pkcs11.dll"); //I test with "libpkcs11.dll" and "pkcs11.dll" too.
    std::cout << "rc = " << rc << std::endl;

    if (rc == -1)
    {

        std::cout << "Loading pkcs11 engine failed";

        unsigned long error_code = ERR_get_error();
        const char* error_detail = ERR_reason_error_string(error_code);
        
        std::cout << " (" << error_code << ") : " << std::string(error_detail) << std::endl;
    }
    else
    {
        std::cout << "Loading pkcs11 engine worked !" << std::endl;
    }
}

My output console show:

11:59:27: Starting C:/Users/jgomez/Documents/build-SandBox-Desktop_Qt_4_8_7_MinGW_32_bit-Release/release/SandBox.exe...
ctx = 0x2ca8f50
rc = -1
Loading pkcs11 engine failed (0) : terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string: construction from null is not valid
11:59:29: C:/Users/jgomez/Documents/build-SandBox-Desktop_Qt_4_8_7_MinGW_32_bit-Release/release/SandBox.exe exited with code 3

My problem:

rc = -1

1 Answers1

0

Solution : use the Dll called opensc-pkcs11.dll provided with OpenSC project and it should work. ( https://github.com/OpenSC/OpenSC , after installing , it should be found here by default C:\Program Files (x86)\OpenSC Project\OpenSC\pkcs11)

Explantation : I have encountered the same problem, and after messing around with the files I figured out that this error due the PKCS11_CTX_Load function.

PKCS11_CTX_Load , tries to load the pkcs11.dll , and then tries getting the address of "c_get_function_list" from this dll, which fails since it doesn't have that function.