Skip to content

Commit

Permalink
pkcs11-tool.c - fixup
Browse files Browse the repository at this point in the history
Error in printing ec point caused segfault

 On branch X25519-improvements-2
 Changes to be committed:
	modified:   tools/pkcs11-tool.c
  • Loading branch information
dengert committed Aug 23, 2024
1 parent 7fbd967 commit 39f3f9e
Showing 1 changed file with 26 additions and 15 deletions.
41 changes: 26 additions & 15 deletions src/tools/pkcs11-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -5621,27 +5621,36 @@ show_key(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
}
if (pub) {
unsigned char *bytes = NULL;
unsigned char *body;

unsigned long ksize = 0;
unsigned int n;
unsigned long body_len = 0;

bytes = getEC_POINT(sess, obj, &ksize);
bytes = getEC_POINT(sess, obj, &size);
/*
* simple parse of DER BIT STRING 0x03 or OCTET STRING 0x04
* good to 65K bytes
*/
if (ksize > 3 && (bytes[0] == 0x03 || bytes[0] == 0x04)) {
if (bytes[1] <= 127 && ksize == (unsigned long)(bytes[1] + 2)) {
body_len = ksize - 2;
if (size > 3 && (bytes[0] == 0x03 || bytes[0] == 0x04)) {
if (bytes[1] <= 127 && size == (unsigned long)(bytes[1] + 2)) {
body_len = size - 2;
body = bytes + 2;
} else if (bytes[1] == 0x81 && size == ((unsigned long)bytes[2] + 3)) {
body_len = ksize - 3;
body_len = size - 3;
body = bytes + 3;
} else if (bytes[1] == 0x82 && size == ((unsigned long)(bytes[2] << 8) + (unsigned long)bytes[3] + 4)) {
body_len = ksize - 4;
body_len = size - 4;
body = bytes + 4;
} else {
body_len = 0; /* some problem with size */
}
}
/* With BIT STRING remove unused bits in last byte indicator */
if (body_len > 0 && bytes[0] == 0x03)
if (body_len > 0 && bytes[0] == 0x03) {
body_len--;
body++;
}

if (key_type == CKK_EC && body_len > 0) {
/*
Expand All @@ -5653,32 +5662,34 @@ show_key(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
* Do simple size calculation based on DER encoding
*/
ksize = (body_len - 1) * 4;

} else if (body_len > 0) {
/*
* EDDSA and XEDDSA in PKCS11 and only one coordinate
*/
/* TODO rebase on changes in master in this area */
ksize = (body_len) * 8 - 1;
size = body_len;
ksize = (body_len) * 8;
if (ksize == 256)
ksize--; /* as 25519 uses 255 as bits */
}

if (ksize)
printf(" EC_POINT %lu bits\n", ksize);
else
printf(" EC_POINT size unknown");

if (bytes) {
if (bytes && body) {
if ((CK_LONG)size > 0) { /* Will print the point here */
printf(" EC_POINT: ");
for (n = 0; n < size; n++)
printf("%02x", bytes[n]);
for (n = 0; n < body_len; n++)
printf("%02x", body[n]);
printf("\n");
}
free(bytes);
}
free(bytes);
bytes = NULL;
size = 0;
bytes = getEC_PARAMS(sess, obj, &size);
if (bytes){
if (bytes) {
if ((CK_LONG)size > 0) {
struct sc_object_id oid;

Expand Down

0 comments on commit 39f3f9e

Please sign in to comment.