changeset 96:a5dfab380a90

fc-simtool: iccid high-level read command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 24 Jan 2021 22:11:34 +0000
parents 5f826e428641
children 597c4e87a1f4
files simtool/Makefile simtool/dispatch.c simtool/hlread.c
diffstat 3 files changed, 71 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/simtool/Makefile	Sun Jan 24 20:47:00 2021 +0000
+++ b/simtool/Makefile	Sun Jan 24 22:11:34 2021 +0000
@@ -1,8 +1,8 @@
 CC=	gcc
 CFLAGS=	-O2 -I/usr/include/PCSC
 PROG=	fc-simtool
-OBJS=	apdu.o atr.o cardconnect.o dispatch.o globals.o hexdump.o main.o \
-	names.o readcmd.o readops.o select.o
+OBJS=	apdu.o atr.o cardconnect.o dispatch.o globals.o hexdump.o hlread.o \
+	main.o names.o readcmd.o readops.o select.o
 
 all:	${PROG}
 
--- a/simtool/dispatch.c	Sun Jan 24 20:47:00 2021 +0000
+++ b/simtool/dispatch.c	Sun Jan 24 22:11:34 2021 +0000
@@ -12,6 +12,7 @@
 #include <winscard.h>
 #include "globals.h"
 
+extern int cmd_iccid();
 extern int cmd_readbin();
 extern int cmd_readef();
 extern int cmd_readrec();
@@ -33,6 +34,7 @@
 	int (*func)();
 } cmdtab[] = {
 	{"exit", 0, 0, cmd_exit},
+	{"iccid", 0, 0, cmd_iccid},
 	{"quit", 0, 0, cmd_exit},
 	{"readbin", 2, 2, cmd_readbin},
 	{"readef", 1, 1, cmd_readef},
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/simtool/hlread.c	Sun Jan 24 22:11:34 2021 +0000
@@ -0,0 +1,67 @@
+/*
+ * This module implements some high-level or user-friendly read commands.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+#include "globals.h"
+#include "file_id.h"
+
+encode_hex_digit(d)
+	unsigned d;
+{
+	if (d <= 9)
+		return(d + '0');
+	else
+		return(d - 10 + 'A');
+}
+
+decode_reversed_nibbles(bytes, nbytes, dest)
+	u_char *bytes;
+	unsigned nbytes;
+	char *dest;
+{
+	u_char *sp;
+	char *dp;
+	unsigned n, c;
+
+	sp = bytes;
+	dp = dest;
+	for (n = 0; n < nbytes; n++) {
+		c = *sp & 0xF;
+		*dp++ = encode_hex_digit(c);
+		c = *sp >> 4;
+		*dp++ = encode_hex_digit(c);
+		sp++;
+	}
+}
+
+cmd_iccid()
+{
+	int rc;
+	char buf[21];
+
+	rc = select_op(FILEID_MF);
+	if (rc < 0)
+		return(rc);
+	rc = select_op(EF_ICCID);
+	if (rc < 0)
+		return(rc);
+	rc = parse_ef_select_response();
+	if (rc < 0)
+		return(rc);
+	if (curfile_structure != 0x00 || curfile_total_size != 10) {
+		fprintf(stderr, "error: expected transparent EF of 10 bytes\n");
+		return(-1);
+	}
+	rc = readbin_op(0, 10);
+	if (rc < 0)
+		return(rc);
+	decode_reversed_nibbles(sim_resp_data, 10, buf);
+	buf[20] = '\0';
+	printf("%s\n", buf);
+	return(0);
+}