2016-09-09 39 views
0

我尝试为我们的机构项目使用Pkcs11Interop库。但问题是,当我尝试从令牌卡获取价值时,“尝试读取或写入受保护的内存。这通常表示其他内存已损坏”错误是从Pkcs11Interop获得的。我找不到任何解决方案。请帮助我,提前谢谢你。PDF使用Pkcs11Interop签名

项目是一个与.Net框架编写Windows窗体应用程序4.5

错误:system.accessviolationexception {"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."}

错误堆栈跟踪:

at Net.Pkcs11Interop.HighLevelAPI40.Session.GetAttributeValue(ObjectHandle objectHandle, List`1 attributes) 
    at Net.Pkcs11Interop.HighLevelAPI40.Session.GetAttributeValue(ObjectHandle objectHandle, List`1 attributes) 
    at EFinImza.Program.Main() in c:\HttpRoot\EFinImza\EFinImza\Program.cs:line 56 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 

代码是这样的:

static void Main() 
    { 
     try 
     { 
      string pkcs11Library = @"C:\Windows\System32\akisp11.dll"; 
      using (var pkcs11 = new Net.Pkcs11Interop.HighLevelAPI40.Pkcs11(pkcs11Library, false, false)) 
      { 
       LibraryInfo info = pkcs11.GetInfo(); 
       foreach (Slot slot in pkcs11.GetSlotList(false)) 
       { 
        SlotInfo slotInfo = slot.GetSlotInfo(); 
        if (slotInfo.SlotFlags.TokenPresent) 
        { 
         TokenInfo tokenInfo = slot.GetTokenInfo(); 

         Session session = slot.OpenSession(false); 
         String pin = "*****"; 
         session.Login(CKU.CKU_USER, pin); 

         // get all objects using empty ObjectAttributes list 
         List<ObjectHandle> handles = session.FindAllObjects(new List<ObjectAttribute>()); 
         List<CKA> attrs = new List<CKA>(); 
         attrs.Add(CKA.CKA_LABEL); 

         foreach (ObjectHandle handle in handles) 
         { 
          List<ObjectAttribute> oAttrs = session.GetAttributeValue(handle, attrs); **//Error is getting here** 
         } 

         session.CloseSession(); 
        } 
       } 

       pkcs11.Dispose(); 
      } 

      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

    } 

回答

0

official documentation建议你开始使用Pkcs11Interop你应该得到至少与熟悉之前“第2章 - 适用范围”“第6章 - 一般概述”“第10章 - 物” PKCS#11 v2.20 specification

您的代码首先查找所有对象,而不管它们的类型(键,证书等),然后尝试读取每个对象的属性。 不是所有对象类型的有效属性,我想这可能会导致你的问题。当然,良好行为的非托管PKCS#11库将返回CKR_ATTRIBUTE_TYPE_INVALID错误而不是段错误,但是有许多质量差的PKCS#11库不能很好地处理这种情况。

,我建议你到规范的第一次读提到的章节,然后更改传递给FindAllObjects()方法搜索模板来搜索只为你真正感兴趣的特定对象类型。