2

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

Hana Bzh
  • 2,212
  • 3
  • 18
  • 38
  • Try to copy debug symbols from output directory of Botan library into Debug folder of your project and start debugging. This can explain some details of point of rising exception. – Alexander Aug 14 '19 at 14:05
  • @Alexander Would you please explain more what should I do, thanks – Hana Bzh Aug 14 '19 at 14:09
  • 3
    Build debug version of Botan. Copy `botan.dll` and `botan.pdb` file into Debug folder of your test project. When you start to debug your test application the exception point will not be the dummy address in memory. – Alexander Aug 14 '19 at 14:16
  • @Alexander I build debug version of Botan, copied those files in debug folder. But the problem is how I include in application. I mean in this case The '#include ' does not work because building in debug mode have different include path and there is no lib folder. botan.lib and botan.dll is created in the main root of botan folder. how can I deal with include and library directories? – Hana Bzh Aug 16 '19 at 13:20
  • @Alexander I would be appreciate if you help me choose the best cryptography library. The project I begin a web extension which should negotiate with a c++ native application that connects to a hardware token in order to sign a document. How I best implement it using C++. Thanks in advance – Hana Bzh Aug 16 '19 at 13:25
  • 1
    @BzH It might be worth trying with another pkcs#11 library (e.g. [SoftHSM](https://github.com/disig/SoftHSM2-for-Windows))...Good luck! – vlp Aug 16 '19 at 18:42
  • @vlp Botan is one of dependencies for SoftHSM. Should I configure Botan for SoftHSM. Can you provide a step by step configuration and using SoftHSM with VS2019? thanks – Hana Bzh Aug 18 '19 at 04:47
  • @BzH I try to keep myself as far as possible from VS -- so can't help here. AFAIK SoftHSM can work with OpenSSL backend (without Botan). The link I gave is to a precompiled version for Windows which you could try in place of your pkcs#11 (ShuttleCsp11_3003.dll) to avoid possible errors in this dll. Btw -- I tested your code on linux and it works correctly (i.e. no segfault) with my pkcs#11 library... – vlp Aug 18 '19 at 16:03

1 Answers1

2

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.

seccpur
  • 4,996
  • 2
  • 13
  • 21
  • How can I deal with includes after integrating. everything went fine with vcpkg but after I created a new console App in VS2019 how can I use Botan methods. Should I use the namespace or not. Should I set any configuration? Thanks alot – Hana Bzh Aug 18 '19 at 02:39
  • If you use integrate install botan libraries will be automatically linked to your vs 2019 projects. Otherwise you can manually linked from the vcpkg installed directories. – seccpur Aug 18 '19 at 03:52
  • I used integrate install successfully, but the line of code `Botan::PKCS11::Module module("C:\\Windows\\System32\\ShuttleCsp11_3003.dll");` has error which says it does not know Botan. Also I tried to write `#include ` but VS does not recognize it. 'Using namespace Botan;' also didn't work. I'm new in C++. maybe there is a easy mistake there. cause I'm sure everything went successful with VCPKG – Hana Bzh Aug 18 '19 at 04:12
  • 1
    Resort to manual linking. Include the header folder and lib folder under vcpkg\installed and copy the dll files to your application folder. May sure you select x64 project with multi-threaded DLL in the vs project settings. – seccpur Aug 18 '19 at 08:24
  • thanks a lot. You solved my problem. Would you please tell me how to debug Unhandled Exceptions in Botan. Does VCPKG had any option building the library in a way to debug these kinds of errors? Because I add another line in the source code `Botan::PKCS11::secure_string pin(4, '0');` and again there is another Unhadled Exception. thanks – Hana Bzh Aug 20 '19 at 09:38
  • 1
    vcpkg generates both release and debug versions of the libraries. So you can debug your code in VC by introducing suitable breakpoints. Several botan codes are available under github.com/randombit/botan for testing. So you can find out if the exception is rather during the session login through your specified pin. – seccpur Aug 20 '19 at 11:19