【问题标题】:Softhsm2 with pkcs11interop error. Value was either too large or too small for a UInt32Softhsm2 出现 pkcs11interop 错误。对于 UInt32,值太大或太小
【发布时间】:2019-03-06 12:20:29
【问题描述】:

我有以下通过 pkcs11interop 库进行 softhsm2 测试的代码

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();
                }
            }
        }
    }
}

我得到的错误堆栈跟踪是

   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:第 15 行 在 C:\Users\kashahid\Downloads\Pkcs11Interop-master\Pkcs11Interop-master\src\PkcsTestWindowsApp\Form1.cs:line 27 中的 PkcsTestWindowsApp.Form1.button1_Click(Object sender, EventArgs e) 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 C:\Users\kashahid\Downloads\Pkcs11Interop-master\Pkcs11Interop-master\src\PkcsTestWindowsApp\Program.cs:line 19 中的 PkcsTestWindowsApp.Program.Main() 处

https://github.com/Pkcs11Interop/Pkcs11Interop

【问题讨论】:

    标签: c# .net-4.5 pkcs#11 pkcs11interop softhsm


    【解决方案1】:

    我发现有两个问题:

    1. 您正在使用 Pkcs11Interop(主分支)的当前开发版本,其中包含几个 API 重大更改,并且在成为稳定版本之前会进行大量更改。

    2. 您直接引用来自 Net.Pkcs11Interop.HighLevelAPI80 命名空间的类型,这些类型应该只在 Unix 系统上使用。

    解决办法:

    您应该使用当前的稳定版本 - Pkcs11Interop 4.1.0 - 使用以下代码:

    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();
                    }
                }
            }
        }
    }
    

    【讨论】:

    • 谢谢雅罗斯拉夫。知道将当前开发版本合并到主版本需要多长时间吗?
    • 至少几个月。
    • 糟糕。意味着相当多的时间:)
    猜你喜欢
    • 2016-09-21
    • 2020-09-21
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多