Skip to content

Commit 5493770

Browse files
committed
pkcs11-tool: Simplify printing EC keys parameters
Most of the code for edwards/montgomery curves was already same as the default EC code, but it had interpretation of the EC_PARAMS (only OID so I added a printable string).
1 parent 0bc425e commit 5493770

File tree

1 file changed

+34
-56
lines changed

1 file changed

+34
-56
lines changed

src/tools/pkcs11-tool.c

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5042,20 +5042,40 @@ show_key(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
50425042
}
50435043
}
50445044
break;
5045+
case CKK_EC:
50455046
case CKK_EC_EDWARDS:
50465047
case CKK_EC_MONTGOMERY:
50475048
if (key_type == CKK_EC_EDWARDS) {
50485049
printf("; EC_EDWARDS");
5049-
} else {
5050+
} else if (key_type == CKK_EC_MONTGOMERY) {
50505051
printf("; EC_MONTGOMERY");
5052+
} else {
5053+
printf("; EC");
50515054
}
50525055
if (pub) {
50535056
unsigned char *bytes = NULL;
50545057
int ksize;
50555058
unsigned int n;
50565059

50575060
bytes = getEC_POINT(sess, obj, &size);
5058-
ksize = 255; /* for now, we support only 255b curves */
5061+
if (key_type == CKK_EC) {
5062+
/*
5063+
* (We only support uncompressed for now)
5064+
* Uncompressed EC_POINT is DER OCTET STRING of "04||x||y"
5065+
* So a "256" bit key has x and y of 32 bytes each
5066+
* something like: "04 41 04||x||y"
5067+
* Do simple size calculation based on DER encoding
5068+
*/
5069+
if ((size - 2) <= 127)
5070+
ksize = (size - 3) * 4;
5071+
else if ((size - 3) <= 255)
5072+
ksize = (size - 4) * 4;
5073+
else
5074+
ksize = (size - 5) * 4;
5075+
} else {
5076+
/* This should be 255 for ed25519 and 448 for ed448 curves so roughly */
5077+
ksize = size * 8;
5078+
}
50595079

50605080
printf(" EC_POINT %u bits\n", ksize);
50615081
if (bytes) {
@@ -5077,14 +5097,18 @@ show_key(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
50775097
for (n = 0; n < size; n++)
50785098
printf("%02x", bytes[n]);
50795099

5080-
sc_init_oid(&oid);
5081-
if (size > 2 && sc_asn1_decode_object_id(bytes + 2, size - 2, &oid) == SC_SUCCESS) {
5082-
printf(" (OID %i", oid.value[0]);
5083-
if (oid.value[0] >= 0)
5084-
for (n = 1; (n < SC_MAX_OBJECT_ID_OCTETS)
5085-
&& (oid.value[n] >= 0); n++)
5086-
printf(".%i", oid.value[n]);
5087-
printf(")");
5100+
if (size > 2 && bytes[0] == 0x06) { // OID
5101+
sc_init_oid(&oid);
5102+
if (sc_asn1_decode_object_id(bytes + 2, size - 2, &oid) == SC_SUCCESS) {
5103+
printf(" (OID %i", oid.value[0]);
5104+
if (oid.value[0] >= 0)
5105+
for (n = 1; (n < SC_MAX_OBJECT_ID_OCTETS)
5106+
&& (oid.value[n] >= 0); n++)
5107+
printf(".%i", oid.value[n]);
5108+
printf(")");
5109+
}
5110+
} else if (size > 2 && bytes[0] == 0x13) { // Printable string
5111+
printf(" (PrintableString %.*s)", bytes[1], bytes+2);
50885112
}
50895113
printf("\n");
50905114

@@ -5095,52 +5119,6 @@ show_key(CK_SESSION_HANDLE sess, CK_OBJECT_HANDLE obj)
50955119
printf("\n");
50965120
}
50975121
break;
5098-
case CKK_EC:
5099-
printf("; EC");
5100-
if (pub) {
5101-
unsigned char *bytes = NULL;
5102-
unsigned int n;
5103-
int ksize;
5104-
5105-
bytes = getEC_POINT(sess, obj, &size);
5106-
/*
5107-
* (We only support uncompressed for now)
5108-
* Uncompressed EC_POINT is DER OCTET STRING of "04||x||y"
5109-
* So a "256" bit key has x and y of 32 bytes each
5110-
* something like: "04 41 04||x||y"
5111-
* Do simple size calculation based on DER encoding
5112-
*/
5113-
if ((size - 2) <= 127)
5114-
ksize = (size - 3) * 4;
5115-
else if ((size - 3) <= 255)
5116-
ksize = (size - 4) * 4;
5117-
else
5118-
ksize = (size - 5) * 4;
5119-
5120-
printf(" EC_POINT %d bits\n", ksize);
5121-
if (bytes) {
5122-
if ((CK_LONG)size > 0) { /* Will print the point here */
5123-
printf(" EC_POINT: ");
5124-
for (n = 0; n < size; n++)
5125-
printf("%02x", bytes[n]);
5126-
printf("\n");
5127-
}
5128-
free(bytes);
5129-
}
5130-
bytes = NULL;
5131-
bytes = getEC_PARAMS(sess, obj, &size);
5132-
if (bytes){
5133-
if ((CK_LONG)size > 0) {
5134-
printf(" EC_PARAMS: ");
5135-
for (n = 0; n < size; n++)
5136-
printf("%02x", bytes[n]);
5137-
printf("\n");
5138-
}
5139-
free(bytes);
5140-
}
5141-
} else
5142-
printf("\n");
5143-
break;
51445122
case CKK_GENERIC_SECRET:
51455123
case CKK_AES:
51465124
case CKK_DES:

0 commit comments

Comments
 (0)