Skip to content

pkcs15-tool, record based files handled incorrect in sc_pkcs15_read_file() #2504

@wcosnc

Description

@wcosnc

Reading certificates or public keys from record based files fails

The following commands fail:

pkcs15-tool --read-certificate ID
pkcs15-tool --read-public-key ID

Following situation

The card we are using has record based files to store certificates and public keys. Linear variable (0x04). One record per certificate or public key.

PKCS_15 V 1.1, chapter 6.1.5, states:

Path ::= SEQUENCE {
    path          OCTET STRING,
    index         INTEGER (0..pkcs15-ub-index) OPTIONAL,
    length    [0] INTEGER (0..pkcs15-ub-index) OPTIONAL
}...

In the Path case, identifiers index and length may specify a specific location within the file. If the file in question is a linear record file, index shall be the record number (in the ISO/IEC 7816-4 definition) and length can be set to 0 (if the card’s operating system allows an L e parameter equal to ‘0’ in a ‘READ RECORD’ command).

Bug 1

In "sc_pkcs15_read_file()" files with "SC_FILE_EF_LINEAR_VARIABLE_TLV" (0x05) are handled with the read record command, files with "SC_FILE_EF_LINEAR_VARIABLE" (0x04) not.

Bug 2

In "sc_pkcs15_read_file()", the record number, passed as "index" (see log, in our case 17) is not used. Instead all records are read, started by 1, see here:

if (file->ef_structure == SC_FILE_EF_LINEAR_VARIABLE_TLV) {
	unsigned int i;
	size_t l, record_len;
	unsigned char *head = data;

	for (i=1; ; i++) {
		l = len - (head - data);
		if (l > 256) {
			l = 256;
		}
		r = sc_read_record(p15card->card, i, head, l, SC_RECORD_BY_REC_NR);
		if (r == SC_ERROR_RECORD_NOT_FOUND)
			break;
		if (r < 0) {
			goto fail_unlock;
		}
		if (r < 2)
			break;
		record_len = head[1];
		if (record_len != 0xff) {
			memmove(head, head+2, r-2);
			head += (r-2);
		}
		else {
			if (r < 4)
				break;
			memmove(head, head+4, r-4);
			head += (r-4);
		}
	}
	len = head-data;
}
else {
	r = sc_read_binary(p15card->card, offset, data, len, 0);

Log

Public EC Key [External Key 17 (ECC)]
	Object Flags   : [0x02], modifiable
	Usage          : [0x40], verify
	Access Flags   : [0x00]
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] pkcs15-pubkey.c:926:sc_pkcs15_read_pubkey: called
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] pkcs15-pubkey.c:927:sc_pkcs15_read_pubkey: Public key type 0x204
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] pkcs15-pubkey.c:983:sc_pkcs15_read_pubkey: Read from EF and decode
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] pkcs15.c:2330:sc_pkcs15_read_file: called
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] pkcs15.c:2331:sc_pkcs15_read_file: path=3f00df084531, index=17, count=0
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] card.c:473:sc_lock: called
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] reader-pcsc.c:657:pcsc_lock: called
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] card.c:513:sc_lock: returning with: 0 (Success)
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] card.c:817:sc_select_file: called; type=2, path=3f00df084531
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] apdu.c:546:sc_transmit_apdu: called
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] card.c:473:sc_lock: called
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] card.c:513:sc_lock: returning with: 0 (Success)
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] apdu.c:513:sc_transmit: called
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] apdu.c:363:sc_single_transmit: called
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] apdu.c:367:sc_single_transmit: CLA:0, INS:A4, P1:8, P2:4, data(4) 0x7ffe555912a2
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] reader-pcsc.c:297:pcsc_transmit: reader 'SCM Microsystems Inc. SPR 532 [Vendor Interface] (21221326200958) 01 00'
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] reader-pcsc.c:298:pcsc_transmit: 
Outgoing APDU (10 bytes):
00 A4 08 04 04 DF 08 45 31 00 .......E1.
P:58580; T:0x140100510984256 16:18:34.498 [pkcs15-tool] reader-pcsc.c:216:pcsc_internal_transmit: called
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] reader-pcsc.c:307:pcsc_transmit: 
Incoming APDU (61 bytes):
62 39 80 02 19 00 82 05 04 41 01 90 20 83 02 45 b9.......A.. ..E
31 88 00 8A 01 05 AB 23 84 01 B2 84 01 B3 84 01 1......#........
A2 90 00 84 01 DC 84 01 DD A0 10 A4 06 95 01 08 ................
83 01 00 A4 06 95 01 80 83 01 01 90 00          .............
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] apdu.c:382:sc_single_transmit: returning with: 0 (Success)
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] apdu.c:535:sc_transmit: returning with: 0 (Success)
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] card.c:523:sc_unlock: called
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] iso7816.c:363:iso7816_process_fci:   bytes in file: 6400
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] iso7816.c:374:iso7816_process_fci:   shareable: no
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] iso7816.c:393:iso7816_process_fci:   type: working EF
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] iso7816.c:394:iso7816_process_fci:   EF structure: 4
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] iso7816.c:395:iso7816_process_fci:   tag 0x82: 0x04
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] iso7816.c:404:iso7816_process_fci:   file identifier: 0x4531
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] card.c:852:sc_select_file: returning with: 0 (Success)
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] card.c:628:sc_read_binary: called; 0 bytes at index 17
P:58580; T:0x140100510984256 16:18:34.512 [pkcs15-tool] card.c:523:sc_unlock: called
P:58580; T:0x140100510984256 16:18:34.513 [pkcs15-tool] reader-pcsc.c:709:pcsc_unlock: called
P:58580; T:0x140100510984256 16:18:34.521 [pkcs15-tool] pkcs15.c:2426:sc_pkcs15_read_file: returning with: 0 (Success)
P:58580; T:0x140100510984256 16:18:34.521 [pkcs15-tool] pkcs15-pubkey.c:735:sc_pkcs15_decode_pubkey_ec: called
P:58580; T:0x140100510984256 16:18:34.521 [pkcs15-tool] pkcs15-pubkey.c:740:sc_pkcs15_decode_pubkey_ec: ASN.1 decoding failed: -1402 (Required ASN.1 object not found)
P:58580; T:0x140100510984256 16:18:34.521 [pkcs15-tool] pkcs15-pubkey.c:991:sc_pkcs15_read_pubkey: Decode public key error: -1402 (Required ASN.1 object not found)
P:58580; T:0x140100510984256 16:18:34.521 [pkcs15-tool] pkcs15-pubkey.c:1005:sc_pkcs15_read_pubkey: returning with: -1402 (Required ASN.1 object not found)
	Key ref        : 144 (0x90)
	Native         : no
	Path           : 3f00df084531
	ID             : 4558544b2d3131

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions