2016-09-09 74 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()方法搜索模板來搜索只爲你真正感興趣的特定對象類型。