Skip to content

Commit

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

Signed-off-by: Sergio Arroutbi <sarroutb@redhat.com>
  • Loading branch information
sarroutbi committed Apr 19, 2024
1 parent 26b9067 commit 4e10f45
Showing 1 changed file with 142 additions and 0 deletions.
142 changes: 142 additions & 0 deletions src/tools/pkcs11-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ static void p11_warn(const char *, CK_RV);
static const char * p11_slot_info_flags(CK_FLAGS);
static const char * p11_token_info_flags(CK_FLAGS);
static const char * p11_utf8_to_local(CK_UTF8CHAR *, size_t);
static const char * p11_utf8_to_local_percent_format(CK_UTF8CHAR *, size_t);
static const char * p11_flag_names(struct flag_info *, CK_FLAGS);
static const char * p11_mechanism_to_name(CK_MECHANISM_TYPE);
static CK_MECHANISM_TYPE p11_name_to_mechanism(const char *);
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(info.manufacturerID, sizeof(info.manufacturerID)));
printf(";serial=");
printf("%s", p11_utf8_to_local(info.serialNumber, sizeof(info.serialNumber)));
printf(";token=");
printf("%s", p11_utf8_to_local(info.label, sizeof(info.label)));
printf("\n");
}

static void list_mechs(CK_SLOT_ID slot)
Expand Down Expand Up @@ -8293,6 +8303,138 @@ 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;

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 4e10f45

Please sign in to comment.