Skip to content

Commit

Permalink
Include uri in pkcs11-tool -L option
Browse files Browse the repository at this point in the history
Fixes: #3123

Signed-off-by: Sergio Arroutbi <sarroutb@redhat.com>
  • Loading branch information
sarroutbi committed Apr 19, 2024
1 parent 26b9067 commit c27e972
Showing 1 changed file with 150 additions and 0 deletions.
150 changes: 150 additions & 0 deletions src/tools/pkcs11-tool.c
Expand Up @@ -621,6 +621,7 @@ static void generate_random(CK_SESSION_HANDLE session);
static CK_RV find_object_with_attributes(CK_SESSION_HANDLE session, CK_OBJECT_HANDLE *out,
CK_ATTRIBUTE *attrs, CK_ULONG attrsLen, CK_ULONG obj_index);
static CK_ULONG get_private_key_length(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE prkey);
static const char *p11_utf8_to_local_percent_format(CK_UTF8CHAR *, size_t);

/* win32 needs this in open(2) */
#ifndef O_BINARY
Expand Down Expand Up @@ -1681,6 +1682,15 @@ static void show_token(CK_SLOT_ID slot)
printf(" serial num : %s\n", p11_utf8_to_local(info.serialNumber,
sizeof(info.serialNumber)));
printf(" pin min/max : %lu/%lu\n", info.ulMinPinLen, info.ulMaxPinLen);
printf(" uri : pkcs11:model=");
printf("%s", p11_utf8_to_local_percent_format(info.model, sizeof(info.model)));
printf(";manufacturer=");
printf("%s", p11_utf8_to_local_percent_format(info.manufacturerID, sizeof(info.manufacturerID)));
printf(";serial=");
printf("%s", p11_utf8_to_local_percent_format(info.serialNumber, sizeof(info.serialNumber)));
printf(";token=");
printf("%s", p11_utf8_to_local_percent_format(info.label, sizeof(info.label)));
printf("\n");
}

static void list_mechs(CK_SLOT_ID slot)
Expand Down Expand Up @@ -8293,6 +8303,146 @@ static const char *p11_utf8_to_local(CK_UTF8CHAR *string, size_t len)
return buffer;
}

static CK_BBOOL
p11_is_percent_format_reserved_char(CK_UTF8CHAR c)
{
switch (c) {
case ' ':
case '!':
case '"':
case '#':
case '$':
case '%':
case '&':
case '\'':
case '(':
case ')':
case '*':
case '+':
case ',':
case '/':
case ':':
case ';':
case '=':
case '?':
case '@':
case '[':
case ']':
return CK_TRUE;
}
return CK_FALSE;
}

static char
p11_percent_format_first(CK_UTF8CHAR c)
{
switch (c) {
case ' ':
case '!':
case '"':
case '#':
case '$':
case '%':
case '&':
case '\'':
case '(':
case ')':
case '*':
case '+':
case ',':
case '/':
return '2';
case ':':
case ';':
case '=':
case '?':
return '3';
case '@':
return '4';
case '[':
case ']':
return '5';
}
return c;
}

static char
p11_percent_format_second(CK_UTF8CHAR c)
{
switch (c) {
case ' ':
return '0';
case '!':
return '1';
case '"':
return '2';
case '#':
return '3';
case '$':
return '4';
case '%':
return '5';
case '&':
return '6';
case '\'':
return '7';
case '(':
return '8';
case ')':
return '9';
case '*':
return 'A';
case '+':
return 'B';
case ',':
return 'C';
case '/':
return 'F';
case ':':
return 'A';
case ';':
return 'B';
case '=':
return 'D';
case '?':
return 'F';
case '@':
return '0';
case '[':
return 'B';
case ']':
return 'D';
}
return c;
}

static const char *
p11_utf8_to_local_percent_format(CK_UTF8CHAR *string, size_t len)
{
static char buffer[1024];
size_t output_index, input_index;

while (len && string[len - 1] == ' ')
len--;

for (output_index = input_index = 0; output_index < sizeof(buffer) - 1;
output_index++) {
if (input_index >= len) {
break;
}
if (p11_is_percent_format_reserved_char(string[input_index])) {
buffer[output_index++] = '%';
buffer[output_index++] = p11_percent_format_first(string[input_index]);
buffer[output_index] = p11_percent_format_second(string[input_index]);
} else {
buffer[output_index] = string[input_index];
}
input_index++;
}
buffer[output_index] = '\0';
return buffer;
}

static void p11_fatal(const char *func, CK_RV rv)
{
if (p11)
Expand Down

0 comments on commit c27e972

Please sign in to comment.