C# 如何在找到对象后更新特定属性。获取错误方法C_SetAttributeValue返回CKR_ATTRIBUTE_READ_
我试图在找到对象后更新属性。我们试过不同的案例 我的创建代码:C# 如何在找到对象后更新特定属性。获取错误方法C_SetAttributeValue返回CKR_ATTRIBUTE_READ_,c#,.net,pkcs#11,pkcs11interop,softhsm,C#,.net,Pkcs#11,Pkcs11interop,Softhsm,我试图在找到对象后更新属性。我们试过不同的案例 我的创建代码: using (var pkcs11 = new Pkcs11(@"C:\SoftHSM2\lib\softhsm2.dll", AppType.SingleThreaded)) { var slot = pkcs11.GetSlotList(SlotsType.WithTokenPresent)[0]; using (var session = slot.OpenSession(SessionType.ReadWri
using (var pkcs11 = new Pkcs11(@"C:\SoftHSM2\lib\softhsm2.dll", AppType.SingleThreaded))
{
var slot = pkcs11.GetSlotList(SlotsType.WithTokenPresent)[0];
using (var session = slot.OpenSession(SessionType.ReadWrite))
{
session.Login(CKU.CKU_USER, "1111");
var objectAttributes = new List<ObjectAttribute>
{
new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_DATA),
new ObjectAttribute(CKA.CKA_TOKEN, true),
new ObjectAttribute(CKA.CKA_MODIFIABLE, true),
new ObjectAttribute(CKA.CKA_APPLICATION, txtTypeofData.Text),
new ObjectAttribute(CKA.CKA_LABEL,txtMsisdn.Text),
new ObjectAttribute(CKA.CKA_VALUE, "Data object content original " + DateTime.Now)
};
var result = session.CreateObject(objectAttributes);
session.Logout();
}
}
使用(var pkcs11=new pkcs11(@“C:\SoftHSM2\lib\SoftHSM2.dll”,AppType.SingleThreaded))
{
var slot=pkcs11.GetSlotList(SlotsType.WithTokenPresent)[0];
使用(var session=slot.OpenSession(SessionType.ReadWrite))
{
session.Login(CKU.CKU_用户,“1111”);
var objectAttributes=新列表
{
新的ObjectAttribute(CKA.CKA_类,CKO.CKO_数据),
新的ObjectAttribute(CKA.CKA_标记,true),
新的ObjectAttribute(CKA.CKA_可修改,true),
新的ObjectAttribute(CKA.CKA_应用程序,txtTypeofData.Text),
新的ObjectAttribute(CKA.CKA_标签,txtmissdn.Text),
新的ObjectAttribute(CKA.CKA_值,“原始数据对象内容”+DateTime.Now)
};
var result=session.CreateObject(objectAttributes);
session.Logout();
}
}
我的修改代码是:
using (Pkcs11 pkcs11 = new Pkcs11(@"C:\SoftHSM2\lib\softhsm2.dll", AppType.MultiThreaded))
{
var slot = pkcs11.GetSlotList(SlotsType.WithTokenPresent)[0];
using (Session session = slot.OpenSession(SessionType.ReadWrite))
{
session.Login(CKU.CKU_USER, "1111");
List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>();
objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_DATA));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_APPLICATION, txtTypeofData.Text));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, txtMsisdn.Text));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_MODIFIABLE, true));
var findA = session.FindAllObjects(objectAttributes);
if(findA != null && findA.Count > 0)
{
List<ObjectAttribute> objectAttributesNew = new List<ObjectAttribute>();
objectAttributesNew.Add(new ObjectAttribute(CKA.CKA_VALUE, "Data object content two changed " + DateTime.Now));
session.SetAttributeValue(findA[0], objectAttributesNew);
}
session.Logout();
}
}
使用(Pkcs11 Pkcs11=new Pkcs11(@“C:\SoftHSM2\lib\SoftHSM2.dll”,AppType.multi-threaded))
{
var slot=pkcs11.GetSlotList(SlotsType.WithTokenPresent)[0];
使用(Session Session=slot.OpenSession(SessionType.ReadWrite))
{
session.Login(CKU.CKU_用户,“1111”);
List objectAttributes=new List();
Add(新的ObjectAttribute(CKA.CKA_类,CKO.CKO_数据));
Add(新的ObjectAttribute(CKA.CKA_标记,true));
Add(新的ObjectAttribute(CKA.CKA_应用程序,txtypeofdata.Text));
添加(新的ObjectAttribute(CKA.CKA_标签,txtmissdn.Text));
添加(新的ObjectAttribute(CKA.CKA_MODIFIABLE,true));
var findA=session.FindAllObjects(objectAttributes);
如果(findA!=null&&findA.Count>0)
{
List objectAttributesNew=新列表();
objectAttributesNew.Add(新的ObjectAttribute(CKA.CKA_值,“数据对象内容2已更改”+DateTime.Now));
SetAttributeValue(findA[0],objectAttributesNew);
}
session.Logout();
}
}
检查CKA_MODIFIABLE是否设置为TRUE,因为此属性指示数据对象是否为只读。CKA_MODIFIABLE默认为TRUE,只能通过复制对象进行更改
另外,请注意PKCS#11标准中的注释:
Cryptoki指定的可修改属性实际上可能不可修改
可在某些令牌上修改。也就是说,如果Cryptoki属性被描述为可修改,那么
真正的意思是,就Cryptoki规范而言,它是可修改的。A.
特定的令牌实际上可能不支持修改某些这样的属性
因此,可能是令牌不允许您更改属性,并且代码中没有问题。您观察到的行为似乎特定于SoftHSM实现。您可以在其源代码中找到:
// NOTE: There is no mention in the PKCS#11 v2.40 spec that for a Data
// Object the CKA_VALUE attribute may be modified after creation!
// Therefore we assume it is not allowed to change the CKA_VALUE
// attribute of a Data Object.
请检查我更新的实现。在放置CKA_可修改的trueOk Jaroslav Imrich后仍然会出现相同的错误