changeset 143:431194b772e1

fc-simtool: uicc-dump command ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 04 Feb 2021 05:12:24 +0000
parents 74c5dc7408e9
children 429a8f80426e
files simtool/Makefile simtool/dispatch.c simtool/dumpdir.c
diffstat 3 files changed, 159 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/Makefile	Thu Feb 04 05:06:28 2021 +0000
+++ b/simtool/Makefile	Thu Feb 04 05:12:24 2021 +0000
@@ -2,9 +2,10 @@
 CFLAGS=	-O2 -I/usr/include/PCSC
 PROG=	fc-simtool
 OBJS=	a38.o alpha_decode.o alpha_valid.o apdu.o atr.o cardconnect.o chv.o \
-	dispatch.o exit.o globals.o hexdump.o hexread.o hlread.o main.o names.o\
-	pbcommon.o pbdump.o pberase.o pbupdate.o readcmd.o readops.o \
-	saverestore.o script.o select.o sysmo.o telsum.o writecmd.o writeops.o
+	dispatch.o dumpdir.o exit.o globals.o hexdump.o hexread.o hlread.o \
+	main.o names.o pbcommon.o pbdump.o pberase.o pbupdate.o readcmd.o \
+	readops.o saverestore.o script.o select.o sysmo.o telsum.o writecmd.o \
+	writeops.o
 INSTBIN=/opt/freecalypso/bin
 
 all:	${PROG}
--- a/simtool/dispatch.c	Thu Feb 04 05:06:28 2021 +0000
+++ b/simtool/dispatch.c	Thu Feb 04 05:12:24 2021 +0000
@@ -33,6 +33,7 @@
 extern int cmd_select();
 extern int cmd_spn();
 extern int cmd_telecom_sum();
+extern int cmd_uicc_dir();
 extern int cmd_unblock_chv();
 extern int cmd_update_bin();
 extern int cmd_update_bin_imm();
@@ -82,6 +83,7 @@
 	{"sim-resp", 0, 0, display_sim_resp_in_hex},
 	{"spn", 0, 0, cmd_spn},
 	{"telecom-sum", 0, 0, cmd_telecom_sum},
+	{"uicc-dir", 0, 0, cmd_uicc_dir},
 	{"unblock-chv1", 2, 2, cmd_unblock_chv},
 	{"unblock-chv2", 2, 2, cmd_unblock_chv},
 	{"unblock-pin1", 2, 2, cmd_unblock_chv},
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/dumpdir.c	Thu Feb 04 05:12:24 2021 +0000
@@ -0,0 +1,153 @@
+/*
+ * This module implements the dump of EF_DIR.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+#include "globals.h"
+#include "file_id.h"
+
+static
+check_all_blank()
+{
+	u_char *dp, *endp;
+
+	dp = sim_resp_data;
+	endp = sim_resp_data + sim_resp_data_len;
+	while (dp < endp)
+		if (*dp++ != 0xFF)
+			return(0);
+	return(1);
+}
+
+static void
+dump_aid(tlv)
+	u_char *tlv;
+{
+	unsigned reclen, n;
+
+	reclen = tlv[1];
+	printf(" AID:");
+	for (n = 0; n < reclen; n++)
+		printf(" %02X", tlv[n+2]);
+	putchar('\n');
+}
+
+static void
+dump_label(tlv)
+	u_char *tlv;
+{
+	int rc;
+	unsigned textlen;
+
+	printf(" Label: ");
+	rc = validate_alpha_field(tlv + 2, tlv[1], &textlen);
+	if (rc < 0) {
+		printf("malformed\n");
+		return;
+	}
+	print_alpha_field(tlv + 2, textlen, stdout);
+	putchar('\n');
+}
+
+static void
+dump_unknown_tlv(tlv)
+	u_char *tlv;
+{
+	unsigned reclen, n;
+
+	reclen = tlv[1] + 2;
+	printf(" TLV:");
+	for (n = 0; n < reclen; n++)
+		printf(" %02X", tlv[n]);
+	putchar('\n');
+}
+
+static void
+dump_record(recno)
+	unsigned recno;
+{
+	unsigned totlen, reclen;
+	u_char *dp, *endp;
+
+	printf("Record #%u:\n", recno);
+	if (sim_resp_data[0] != 0x61) {
+		printf(" bad: first byte != 0x61\n");
+		return;
+	}
+	totlen = sim_resp_data[1];
+	if (totlen < 3 || totlen > 0x7F) {
+		printf(" bad: global length byte 0x%02X is invalid\n", totlen);
+		return;
+	}
+	if (totlen + 2 > sim_resp_data_len) {
+		printf(" bad: TLV global length exceeds EF record length\n");
+		return;
+	}
+	dp = sim_resp_data + 2;
+	endp = sim_resp_data + 2 + totlen;
+	while (dp < endp) {
+		if (endp - dp < 2) {
+trunc_error:		printf(" bad: truncated TLV record\n");
+			return;
+		}
+		if ((dp[0] & 0x1F) == 0x1F) {
+			printf(" bad: extended tag not supported\n");
+			return;
+		}
+		if (dp[1] & 0x80) {
+			printf(" bad: extended length not supported\n");
+			return;
+		}
+		reclen = dp[1] + 2;
+		if (endp - dp < reclen)
+			goto trunc_error;
+		switch (dp[0]) {
+		case 0x4F:
+			dump_aid(dp);
+			break;
+		case 0x50:
+			dump_label(dp);
+			break;
+		default:
+			dump_unknown_tlv(dp);
+		}
+		dp += reclen;
+	}
+}
+
+cmd_uicc_dir()
+{
+	int rc;
+	unsigned recno;
+
+	rc = select_op(FILEID_MF);
+	if (rc < 0)
+		return(rc);
+	rc = select_op(EF_DIR);
+	if (rc < 0)
+		return(rc);
+	rc = parse_ef_select_response();
+	if (rc < 0)
+		return(rc);
+	if (curfile_structure != 0x01) {
+		fprintf(stderr, "error: EF_DIR is not linear fixed\n");
+		return(-1);
+	}
+	if (curfile_record_len < 5) {
+		fprintf(stderr, "error: EF_DIR record length is too short\n");
+		return(-1);
+	}
+	for (recno = 1; recno <= curfile_record_count; recno++) {
+		rc = readrec_op(recno, 0x04, curfile_record_len);
+		if (rc < 0)
+			return(rc);
+		if (check_all_blank())
+			continue;
+		dump_record(recno);
+	}
+	return(0);
+}