Skip to content

Commit c9bb05e

Browse files
committed
fix: fixes attribute reading on private objects
According to the PKCS#11 spec, all key objects have defined a semantic for CKA_START_DATE and CKA_END_DATE, although, for key objects that are private (e.g. CKO_PRIVATE_KEY, CKO_SECRET_KEY), the retrieval of their values fail. This is happening when an attribute with a specialized class, e.g. P11AttrStartDate, gets added to a private object, its value is always written in clear, due to the updateAttr method overload, although, upon retrieving the value, due to the retrieve method not being symmetrically overloaded, and because the object is private, the attribute value is decrypted and fails. This change adds protected virtual method `retrieveAttrByteString` to allow overloading the default behavior, namely for public attributes (written in clear), from private object. Also add a base class `P11NonPrivateAttribute` to be extended by public attributes specializations.
1 parent 1a39ad7 commit c9bb05e

2 files changed

Lines changed: 52 additions & 28 deletions

File tree

src/lib/P11Attributes.cpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ CK_RV P11Attribute::updateAttr(Token *token, bool isPrivate, CK_VOID_PTR pValue,
6969
return CKR_OK;
7070
}
7171

72+
CK_RV P11Attribute::retrieveAttrByteString(Token *token, bool isPrivate, OSAttribute *attr, ByteString &value)
73+
{
74+
if (isPrivate && attr->getByteStringValue().size() != 0)
75+
{
76+
if (!token->decrypt(attr->getByteStringValue(), value))
77+
{
78+
ERROR_MSG("Internal error: failed to decrypt private attribute value");
79+
return CKR_GENERAL_ERROR;
80+
}
81+
}
82+
else
83+
{
84+
value = attr->getByteStringValue();
85+
}
86+
return CKR_OK;
87+
}
88+
7289
bool P11Attribute::isModifiable()
7390
{
7491
// Get the CKA_MODIFIABLE attribute, when the attribute is
@@ -273,18 +290,13 @@ CK_RV P11Attribute::retrieve(Token *token, bool isPrivate, CK_VOID_PTR pValue, C
273290
// Lower level attribute has to be variable sized.
274291
if (attr.isByteStringAttribute())
275292
{
276-
if (isPrivate && attr.getByteStringValue().size() != 0)
293+
ByteString value;
294+
CK_RV res = retrieveAttrByteString(token, isPrivate, &attr, value);
295+
if (res != CKR_OK)
277296
{
278-
ByteString value;
279-
if (!token->decrypt(attr.getByteStringValue(),value))
280-
{
281-
ERROR_MSG("Internal error: failed to decrypt private attribute value");
282-
return CKR_GENERAL_ERROR;
283-
}
284-
attrSize = value.size();
297+
return res;
285298
}
286-
else
287-
attrSize = attr.getByteStringValue().size();
299+
attrSize = value.size();
288300
}
289301
else if (attr.isMechanismTypeSetAttribute())
290302
{
@@ -332,22 +344,15 @@ CK_RV P11Attribute::retrieve(Token *token, bool isPrivate, CK_VOID_PTR pValue, C
332344
}
333345
else if (attr.isByteStringAttribute())
334346
{
335-
if (isPrivate && attr.getByteStringValue().size() != 0)
347+
ByteString value;
348+
CK_RV res = retrieveAttrByteString(token, isPrivate, &attr, value);
349+
if (res != CKR_OK)
336350
{
337-
ByteString value;
338-
if (!token->decrypt(attr.getByteStringValue(),value))
339-
{
340-
ERROR_MSG("Internal error: failed to decrypt private attribute value");
341-
return CKR_GENERAL_ERROR;
342-
}
343-
if (value.size() != 0) {
344-
const unsigned char* attrPtr = value.const_byte_str();
345-
memcpy(pValue,attrPtr,attrSize);
346-
}
351+
return res;
347352
}
348-
else if (attr.getByteStringValue().size() != 0)
349-
{
350-
const unsigned char* attrPtr = attr.getByteStringValue().const_byte_str();
353+
354+
if (value.size() != 0) {
355+
const unsigned char* attrPtr = value.const_byte_str();
351356
memcpy(pValue,attrPtr,attrSize);
352357
}
353358
}
@@ -495,6 +500,12 @@ CK_RV P11Attribute::update(Token* token, bool isPrivate, CK_VOID_PTR pValue, CK_
495500
return CKR_ATTRIBUTE_READ_ONLY;
496501
}
497502

503+
CK_RV P11NonPrivateAttribute::retrieveAttrByteString(Token *token, bool isPrivate, OSAttribute *attr, ByteString &value)
504+
{
505+
value = attr->getByteStringValue();
506+
return CKR_OK;
507+
}
508+
498509
/*****************************************
499510
* CKA_CLASS
500511
*****************************************/

src/lib/P11Attributes.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,26 @@ class P11Attribute
122122
// Update the value if allowed
123123
virtual CK_RV updateAttr(Token *token, bool isPrivate, CK_VOID_PTR pValue, CK_ULONG ulValueLen, int op);
124124

125+
// Retrie the value if allowed
126+
virtual CK_RV retrieveAttrByteString(Token *token, bool isPrivate, OSAttribute *attr, ByteString &value);
127+
125128
// Helper functions
126129
bool isModifiable();
127130
bool isSensitive();
128131
bool isExtractable();
129132
bool isTrusted();
130133
};
131134

135+
class P11NonPrivateAttribute : public P11Attribute
136+
{
137+
protected:
138+
// Constructor
139+
P11NonPrivateAttribute(OSObject* inobject) : P11Attribute(inobject) {};
140+
141+
// Retrie the value if allowed
142+
virtual CK_RV retrieveAttrByteString(Token *token, bool isPrivate, OSAttribute *attr, ByteString &value);
143+
};
144+
132145
/*****************************************
133146
* CKA_CLASS
134147
*****************************************/
@@ -455,11 +468,11 @@ class P11AttrCertificateCategory : public P11Attribute
455468
* CKA_START_DATE
456469
*****************************************/
457470

458-
class P11AttrStartDate : public P11Attribute
471+
class P11AttrStartDate : public P11NonPrivateAttribute
459472
{
460473
public:
461474
// Constructor
462-
P11AttrStartDate(OSObject* inobject, CK_ULONG inchecks) : P11Attribute(inobject) { type = CKA_START_DATE; checks = inchecks; }
475+
P11AttrStartDate(OSObject* inobject, CK_ULONG inchecks) : P11NonPrivateAttribute(inobject) { type = CKA_START_DATE; checks = inchecks; }
463476

464477
protected:
465478
// Set the default value of the attribute
@@ -473,11 +486,11 @@ class P11AttrStartDate : public P11Attribute
473486
* CKA_END_DATE
474487
*****************************************/
475488

476-
class P11AttrEndDate : public P11Attribute
489+
class P11AttrEndDate : public P11NonPrivateAttribute
477490
{
478491
public:
479492
// Constructor
480-
P11AttrEndDate(OSObject* inobject, CK_ULONG inchecks) : P11Attribute(inobject) { type = CKA_END_DATE; checks = inchecks; }
493+
P11AttrEndDate(OSObject* inobject, CK_ULONG inchecks) : P11NonPrivateAttribute(inobject) { type = CKA_END_DATE; checks = inchecks; }
481494

482495
protected:
483496
// Set the default value of the attribute

0 commit comments

Comments
 (0)