Skip to content

Commit 39f3f9e

Browse files
committed
pkcs11-tool.c - fixup
Error in printing ec point caused segfault On branch X25519-improvements-2 Changes to be committed: modified: tools/pkcs11-tool.c
1 parent 7fbd967 commit 39f3f9e

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/tools/pkcs11-tool.c

+26-15
Original file line numberDiff line numberDiff line change
@@ -5621,27 +5621,36 @@ show_key(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
56215621
}
56225622
if (pub) {
56235623
unsigned char *bytes = NULL;
5624+
unsigned char *body;
5625+
56245626
unsigned long ksize = 0;
56255627
unsigned int n;
56265628
unsigned long body_len = 0;
56275629

5628-
bytes = getEC_POINT(sess, obj, &ksize);
5630+
bytes = getEC_POINT(sess, obj, &size);
56295631
/*
56305632
* simple parse of DER BIT STRING 0x03 or OCTET STRING 0x04
56315633
* good to 65K bytes
56325634
*/
5633-
if (ksize > 3 && (bytes[0] == 0x03 || bytes[0] == 0x04)) {
5634-
if (bytes[1] <= 127 && ksize == (unsigned long)(bytes[1] + 2)) {
5635-
body_len = ksize - 2;
5635+
if (size > 3 && (bytes[0] == 0x03 || bytes[0] == 0x04)) {
5636+
if (bytes[1] <= 127 && size == (unsigned long)(bytes[1] + 2)) {
5637+
body_len = size - 2;
5638+
body = bytes + 2;
56365639
} else if (bytes[1] == 0x81 && size == ((unsigned long)bytes[2] + 3)) {
5637-
body_len = ksize - 3;
5640+
body_len = size - 3;
5641+
body = bytes + 3;
56385642
} else if (bytes[1] == 0x82 && size == ((unsigned long)(bytes[2] << 8) + (unsigned long)bytes[3] + 4)) {
5639-
body_len = ksize - 4;
5643+
body_len = size - 4;
5644+
body = bytes + 4;
5645+
} else {
5646+
body_len = 0; /* some problem with size */
56405647
}
56415648
}
56425649
/* With BIT STRING remove unused bits in last byte indicator */
5643-
if (body_len > 0 && bytes[0] == 0x03)
5650+
if (body_len > 0 && bytes[0] == 0x03) {
56445651
body_len--;
5652+
body++;
5653+
}
56455654

56465655
if (key_type == CKK_EC && body_len > 0) {
56475656
/*
@@ -5653,32 +5662,34 @@ show_key(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
56535662
* Do simple size calculation based on DER encoding
56545663
*/
56555664
ksize = (body_len - 1) * 4;
5665+
56565666
} else if (body_len > 0) {
56575667
/*
56585668
* EDDSA and XEDDSA in PKCS11 and only one coordinate
56595669
*/
5660-
/* TODO rebase on changes in master in this area */
5661-
ksize = (body_len) * 8 - 1;
5662-
size = body_len;
5670+
ksize = (body_len) * 8;
5671+
if (ksize == 256)
5672+
ksize--; /* as 25519 uses 255 as bits */
56635673
}
56645674

56655675
if (ksize)
56665676
printf(" EC_POINT %lu bits\n", ksize);
56675677
else
56685678
printf(" EC_POINT size unknown");
56695679

5670-
if (bytes) {
5680+
if (bytes && body) {
56715681
if ((CK_LONG)size > 0) { /* Will print the point here */
56725682
printf(" EC_POINT: ");
5673-
for (n = 0; n < size; n++)
5674-
printf("%02x", bytes[n]);
5683+
for (n = 0; n < body_len; n++)
5684+
printf("%02x", body[n]);
56755685
printf("\n");
56765686
}
5677-
free(bytes);
56785687
}
5688+
free(bytes);
56795689
bytes = NULL;
5690+
size = 0;
56805691
bytes = getEC_PARAMS(sess, obj, &size);
5681-
if (bytes){
5692+
if (bytes) {
56825693
if ((CK_LONG)size > 0) {
56835694
struct sc_object_id oid;
56845695

0 commit comments

Comments
 (0)