Skip to content

Commit

Permalink
rpc: Handle special cases for buffer and length
Browse files Browse the repository at this point in the history
When the buffer is not NULL, but the length is zero then treat this as an
empty message. Serialize this in a special way so that the server can
restore the same situation.

Example: Terminate an operation via C_XxxFinal, but there is no more
data for the final part. A call to C_XxxFinal with buffer=NULL and length=0
would be treated as a size query, and would not terminate the operation.
So the way to terminate the operation without more data is to specify
buffer!=NULL but length=0.

When sending a byte array, and the buffer is NULL, and the length is
zero, don't treat this is invalid, but as empty message.

Example: C_XxxUpdate with an empty message.

Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
  • Loading branch information
ifranzki authored and ueno committed May 16, 2022
1 parent b72aa47 commit 506b941
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion p11-kit/rpc-client.c
Expand Up @@ -584,7 +584,7 @@ proto_read_sesssion_info (p11_rpc_message *msg,
#define IN_BYTE_BUFFER(arr, len) \
if (len == NULL) \
{ _ret = CKR_ARGUMENTS_BAD; goto _cleanup; } \
if (!p11_rpc_message_write_byte_buffer (&_msg, arr ? *len : 0)) \
if (!p11_rpc_message_write_byte_buffer (&_msg, arr ? (*len > 0 ? *len : (uint32_t)-1) : 0)) \
{ _ret = CKR_HOST_MEMORY; goto _cleanup; }

#define IN_BYTE_ARRAY(arr, len) \
Expand Down
2 changes: 1 addition & 1 deletion p11-kit/rpc-message.c
Expand Up @@ -379,7 +379,7 @@ p11_rpc_message_write_byte_array (p11_rpc_message *msg,
assert (!msg->signature || p11_rpc_message_verify_part (msg, "ay"));

/* No array, no data, just length */
if (!arr) {
if (!arr && num != 0) {
p11_rpc_buffer_add_byte (msg->output, 0);
p11_rpc_buffer_add_uint32 (msg->output, num);
} else {
Expand Down
6 changes: 6 additions & 0 deletions p11-kit/rpc-server.c
Expand Up @@ -97,6 +97,12 @@ proto_read_byte_buffer (p11_rpc_message *msg,
*n_buffer = length;
*buffer = NULL;

/* length = -1 indicates length = 0, but buffer not NULL */
if (length == (uint32_t)-1) {
*n_buffer = 0;
length = 1; /*allocate 1 dummy byte */
}

/* If set to zero, then they just want the length */
if (length == 0)
return CKR_OK;
Expand Down

0 comments on commit 506b941

Please sign in to comment.