Search code examples
c++visual-studiocryptographypkcs#11botan

std::bad_alloc using Botan for PKCS#11


I'm new in C++ and I really stuck using Botan to connect to a hardware cryptography token. I don't know If I missed any setups for libs or dlls.

I built Botan library based on Building Botan library in Windows 10. botan.lib and botan.dll is created in lib folder after building.

Then I create a consoleApplication in Visual Studio 2019 with this simple code:

#include <iostream>
#include <botan/botan.h>

#include <botan/p11.h>
#include <botan/p11_slot.h>
#include <botan/p11_session.h>
#include <botan/p11_module.h>
#include <botan/p11_object.h>
#include <botan/p11_randomgenerator.h>

#include <botan/p11_x509.h>
#include <botan/x509_dn.h>

using namespace Botan; 
using namespace PKCS11;

int main()
{
    Botan::PKCS11::Module module("C:\\Windows\\System32\\ShuttleCsp11_3003.dll");
    // Sometimes useful if a newly connected token is not detected by the PKCS#11 module
    module.reload();

    Botan::PKCS11::Info info = module.get_info();

    // print library version
    std::cout << std::to_string(info.libraryVersion.major) << "."
        << std::to_string(info.libraryVersion.minor) << std::endl;
}

This is the settings I prepared to run:

Configuration Properties→VC++ Directories:

  1. Include Directories → add C:\Botan\include\botan-2;
  2. Executable Directories → add C:\Botan\bin;
  3. Library Directory → add C:\Botan\lib;
  4. Source Directory → add C:\Botan\src;
  5. Additional Include Library → add C:\Botan\include\botan-2

Linker

  1. Additional Library Directory → add C:\Botan\lib;
  2. Input → Additional Dependencies → add C:\Botan\lib\botan.lib

Also I installed token driver which dll is in System32 folder;

As I build the Botan Library with x86 so I debug the project with this config:

enter image description here

The error which I need your help to solve is:

Unhandled exception at 0x74CD2CF2 in ConsoleApplication1.exe: Microsoft C++ exception:
std::bad_alloc at memory location 0x004FF1AC.

This error occurred in this line of Code:

Botan::PKCS11::Module module("C:\\Windows\\System32\\ShuttleCsp11_3003.dll");

And this is the call stack

enter image description here

Note that I copied botan.dll and ShuttleCsp11_3003.dll in debug folder.

Somebody please help, thanks


Solution

  • Use Vcpkg, which is a tool created by Microsoft that helps acquire and build open source C and C++ libraries, to install botan automatically using a one liner shell command line and integrate to your VS 2019 project.

    After installing vcpkg from GitHub, type the following command from a PowerShell prompt to download and install the library including all the dependencies:

    .\vcpkg install botan:x86-windows
    

    Use this to automatically (or you can do it manually) integrate the library to your VS project.

    .\vcpkg integrate install
    

    This is serious error here, which is the reason why the namespace and include files are not recognized by your project, Include Directories → add C:\Botan\include\botan-2 is incorrect Check the directory/file name botan-2, it should not exist.

    • should be C:\Botan\include; as your program includes botan in the folder path (eg #include "botan/botan.h")

    Copy the dll files to your project directory( for debug testing) and to your application folder (debug or release version) and don't forget to correct the dll folder path while loading PKCS#11 shared library.