0
votes

I have following code for softhsm2 test via pkcs11interop library

using Net.Pkcs11Interop.Common;
using Net.Pkcs11Interop.HighLevelAPI;
using Net.Pkcs11Interop.HighLevelAPI80;
using System;
using System.Collections.Generic;
namespace ExportTest
{
    public class Softhsmtest
    {
        public static Pkcs11InteropFactories Factories = new Pkcs11InteropFactories();
        public static void Test()
        {

            using (Pkcs11 pkcs11 = new Pkcs11(Factories, @"C:\SoftHSM2\lib\softhsm2.dll",AppType.MultiThreaded))
            {
                ISlot slot = pkcs11.GetSlotList(SlotsType.WithTokenPresent)[0];
                using (ISession session = slot.OpenSession(SessionType.ReadWrite))
                {
                    session.Login(CKU.CKU_USER, "1111");                    
                    session.Logout();
                }
            }
        }
    }
}

Error Stacktrace i am getting is

   at System.Convert.ToUInt32(UInt64 value)
   at Net.Pkcs11Interop.Common.ConvertUtils.UInt64ToCKR(UInt64 value)
   at Net.Pkcs11Interop.LowLevelAPI80.Delegates.InitializeWithGetFunctionList(IntPtr libraryHandle)
   at Net.Pkcs11Interop.LowLevelAPI80.Delegates..ctor(IntPtr libraryHandle, Boolean useGetFunctionList)
   at Net.Pkcs11Interop.LowLevelAPI80.Pkcs11..ctor(String libraryPath)
   at Net.Pkcs11Interop.HighLevelAPI80.Pkcs11..ctor(Pkcs11InteropFactories factories, String libraryPath, AppType appType)
   at ExportTest.Softhsmtest.Test() in C:\Users\kashahid\Downloads

\Pkcs11Interop-master\Pkcs11Interop-master\src\PkcsTestWindowsApp\Class1.cs:line 15 at PkcsTestWindowsApp.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\kashahid\Downloads\Pkcs11Interop-master\Pkcs11Interop-master\src\PkcsTestWindowsApp\Form1.cs:line 27 at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ButtonBase.WndProc(Message& m) at System.Windows.Forms.Button.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at PkcsTestWindowsApp.Program.Main() in C:\Users\kashahid\Downloads\Pkcs11Interop-master\Pkcs11Interop-master\src\PkcsTestWindowsApp\Program.cs:line 19

https://github.com/Pkcs11Interop/Pkcs11Interop

1

1 Answers

2
votes

I see two problems there:

  1. You're using the current development version of Pkcs11Interop (master branch) that contains several API breaking changes and will change a LOT before it becomes stable version.

  2. You're directly referencing types from Net.Pkcs11Interop.HighLevelAPI80 namespace which are supposed to be used only on Unix systems.

The solution:

You should use the current stable version - Pkcs11Interop 4.1.0 - with the following code:

using Net.Pkcs11Interop.Common;
using Net.Pkcs11Interop.HighLevelAPI;

namespace ExportTest
{
    public class Softhsmtest
    {
        public static void Test()
        {
            using (Pkcs11 pkcs11 = new Pkcs11(@"C:\SoftHSM2\lib\softhsm2.dll", AppType.MultiThreaded))
            {
                Slot slot = pkcs11.GetSlotList(SlotsType.WithTokenPresent)[0];
                using (Session session = slot.OpenSession(SessionType.ReadWrite))
                {
                    session.Login(CKU.CKU_USER, "1111");
                    session.Logout();
                }
            }
        }
    }
}