简体   繁体   English

查找对象后如何更新特定属性。 得到错误方法C_SetAttributeValue返回CKR_ATTRIBUTE_READ_ONLY

[英]How to Update particular Attribute after finding an object. getting error Method C_SetAttributeValue returned CKR_ATTRIBUTE_READ_ONLY

I am trying to update an attribute after finding the object. 我尝试在找到对象后更新属性。 Have tried out different cases. 尝试了不同的情况。

My creation code: 我的创建代码:

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

My modifying code is: 我的修改代码是:

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

The behavior you are observing seems to be specific to SoftHSM implementation. 您观察到的行为似乎是特定于SoftHSM实现的。 You can find following comment in its source code: 您可以在其源代码中找到以下注释

// 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.

Check if CKA_MODIFIABLE is set to TRUE because this attribute indicates whether a Data Object is read-only or not. 检查CKA_MODIFIABLE是否设置为TRUE,因为此属性指示数据对象是否为只读。 CKA_MODIFIABLE is by default TRUE and can only be changed by copying the object. CKA_MODIFIABLE默认情况下为TRUE,并且只能通过复制对象进行更改。

Also, pay attention to this note from the PKCS#11 standard: 另外,请注意PKCS#11标准中的此注释:

attributes which Cryptoki specifies are modifiable may actually not be modifiable on some tokens. Cryptoki指定的可修改属性实际上可能在某些令牌上不可修改。 That is, if a Cryptoki attribute is described as being modifiable, that really means only that it is modifiable insofar as the Cryptoki specification is concerned. 也就是说,如果将Cryptoki属性描述为可修改的,那实际上仅意味着就Cryptoki规范而言,它是可修改的。 A particular token might not actually support modification of some such attributes. 特定令牌可能实际上不支持对某些此类属性的修改。

So, it could be the case that the token is not allowing you to change the attribute and that it's not a problem from your code. 因此,可能是令牌不允许您更改属性,并且代码中没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# ASP.NET:“对象引用未设置为 object 的实例。” System.Xml.Linq.XElement.Attribute(...) 返回 null - C# ASP.NET: 'Object reference not set to an instance of an object.' System.Xml.Linq.XElement.Attribute(…) returned null 如何修复方法 C_GenerateKeyPair 返回的 CKR_FUNCTION_FAILED - How to fix method C_GenerateKeyPair returned CKR_FUNCTION_FAILED 如何将属性设置为未设置为实例或对象的XmlNode对象。 C# - How to set attribute to an XmlNode object which is not set to an instance or object. c# 从C#中返回的对象访问属性 - Access an attribute from a returned object in c# Session.GetOperationState()方法引发异常“ SoftHSM中方法C_GetOperationState返回了CKR_FUNCTION_NOT_SUPPORTED” - Session.GetOperationState() method is throwing exception “Method C_GetOperationState returned CKR_FUNCTION_NOT_SUPPORTED in SoftHSM” 查找具有特定属性的所有类 - Finding all classes with a particular attribute 在Api方法中获取Route属性错误 - Getting error on Route attribute in Api method 在包含对象属性的列表中查找 - Finding in a list that contains an attribute of an object 将对象属性传递给方法参数C# - Passing object attribute to method parameter C# 读取方法属性的值 - Read the value of an attribute of a method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM