Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: add support for recursive attributes #624

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions common/attrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ attrs_build (CK_ATTRIBUTE *attrs,
/* The attribute exists and we're not overriding */
} else if (!override) {
if (take_values)
free (add->pValue);
p11_attr_clear (add);
continue;

/* The attribute exists but we're overriding */
} else {
free (attr->pValue);
p11_attr_clear (attr);
}

if (take_values) {
Expand Down
72 changes: 68 additions & 4 deletions common/mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,24 @@ module_reset_objects (CK_SLOT_ID slot_id)
{
CK_OBJECT_CLASS klass = CKO_PUBLIC_KEY;
char *label = "Public prefix key";
char *wrapped_label = "Wrapped label";
char *value = "value";
CK_MECHANISM_TYPE type = CKM_MOCK_PREFIX;
CK_BBOOL btrue = CK_TRUE;
CK_BBOOL bfalse = CK_FALSE;
CK_ATTRIBUTE wrap_template[] = {
{ CKA_LABEL, wrapped_label, strlen (wrapped_label) },
{ CKA_VERIFY, &btrue, sizeof (btrue) },
{ CKA_ENCRYPT, &btrue, sizeof (btrue) },
};
CK_ATTRIBUTE attrs[] = {
{ CKA_CLASS, &klass, sizeof (klass) },
{ CKA_LABEL, label, strlen (label) },
{ CKA_ALLOWED_MECHANISMS, &type, sizeof (type) },
{ CKA_VERIFY, &btrue, sizeof (btrue) },
{ CKA_PRIVATE, &bfalse, sizeof (bfalse) },
{ CKA_ALWAYS_AUTHENTICATE, &btrue, sizeof (btrue) },
{ CKA_WRAP_TEMPLATE, wrap_template, sizeof (wrap_template) },
{ CKA_VALUE, value, strlen (value) },
{ CKA_INVALID, NULL, 0 },
};
Expand Down Expand Up @@ -1618,6 +1625,51 @@ mock_X_GetObjectSize__invalid_handle (CK_X_FUNCTION_LIST *self,
return CKR_SESSION_HANDLE_INVALID;
}

static CK_RV
get_recursive_attribute_value (CK_ATTRIBUTE_PTR dst,
CK_ATTRIBUTE_PTR src,
CK_ULONG count)
{
CK_RV rv, ret = CKR_OK;
CK_ULONG i;
CK_ATTRIBUTE *result, *attr;

for (i = 0; i < count; ++i) {
result = dst + i;
attr = src + i;

result->type = attr->type;

if (result->pValue == NULL) {
result->ulValueLen = attr->ulValueLen;
continue;
}

if (result->ulValueLen < attr->ulValueLen) {
result->ulValueLen = (CK_ULONG)-1;
ret = CKR_BUFFER_TOO_SMALL;
continue;
}

if (IS_ATTRIBUTE_ARRAY (attr)) {
rv = get_recursive_attribute_value (result->pValue,
attr->pValue, attr->ulValueLen / sizeof (CK_ATTRIBUTE));
if (rv != CKR_OK) {
result->ulValueLen = (CK_ULONG)-1;
ret = rv;
continue;
}
result->ulValueLen = attr->ulValueLen;
continue;
}

memcpy (result->pValue, attr->pValue, attr->ulValueLen);
result->ulValueLen = attr->ulValueLen;
}

return ret;
}

CK_RV
mock_C_GetAttributeValue (CK_SESSION_HANDLE session,
CK_OBJECT_HANDLE object,
Expand Down Expand Up @@ -1654,14 +1706,26 @@ mock_C_GetAttributeValue (CK_SESSION_HANDLE session,
continue;
}

if (result->ulValueLen >= attr->ulValueLen) {
memcpy (result->pValue, attr->pValue, attr->ulValueLen);
if (result->ulValueLen < attr->ulValueLen) {
result->ulValueLen = (CK_ULONG)-1;
ret = CKR_BUFFER_TOO_SMALL;
continue;
}

if (IS_ATTRIBUTE_ARRAY (attr)) {
rv = get_recursive_attribute_value (result->pValue,
attr->pValue, attr->ulValueLen / sizeof (CK_ATTRIBUTE));
if (rv != CKR_OK) {
result->ulValueLen = (CK_ULONG)-1;
ret = rv;
continue;
}
result->ulValueLen = attr->ulValueLen;
continue;
}

result->ulValueLen = (CK_ULONG)-1;
ret = CKR_BUFFER_TOO_SMALL;
memcpy (result->pValue, attr->pValue, attr->ulValueLen);
result->ulValueLen = attr->ulValueLen;
}

return ret;
Expand Down
6 changes: 6 additions & 0 deletions common/persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,12 @@ field_to_attribute (p11_persist *persist,
return false;
}

/* Ignore recursive attributes */
if (IS_ATTRIBUTE_ARRAY (&attr)) {
ZoltanFridrich marked this conversation as resolved.
Show resolved Hide resolved
free (attr.pValue);
return true;
}

*attrs = p11_attrs_take (*attrs, attr.type,
attr.pValue, attr.ulValueLen);
return true;
Expand Down
Loading
Loading