changeset 134:69628bcfec17

fc-uicc-tool: iccid command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 04 Feb 2021 02:28:52 +0000
parents f3bdefbeae38
children 51d6aaa43a7b
files uicc/Makefile uicc/dispatch.c uicc/hlread.c uicc/select.c
diffstat 4 files changed, 144 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/uicc/Makefile	Thu Feb 04 01:45:16 2021 +0000
+++ b/uicc/Makefile	Thu Feb 04 02:28:52 2021 +0000
@@ -1,8 +1,8 @@
 CC=	gcc
 CFLAGS=	-O2 -I/usr/include/PCSC
 PROG=	fc-uicc-tool
-OBJS=	apdu.o atr.o cardconnect.o dispatch.o exit.o globals.o hexdump.o main.o\
-	names.o readcmd.o readops.o script.o select.o
+OBJS=	apdu.o atr.o cardconnect.o dispatch.o exit.o globals.o hexdump.o \
+	hlread.o main.o names.o readcmd.o readops.o script.o select.o
 
 all:	${PROG}
 
--- a/uicc/dispatch.c	Thu Feb 04 01:45:16 2021 +0000
+++ b/uicc/dispatch.c	Thu Feb 04 02:28:52 2021 +0000
@@ -9,6 +9,7 @@
 #include <stdlib.h>
 
 extern int cmd_exec();
+extern int cmd_iccid();
 extern int cmd_readbin();
 extern int cmd_readrec();
 extern int cmd_select();
@@ -24,6 +25,7 @@
 } cmdtab[] = {
 	{"exec", 1, 1, cmd_exec},
 	{"exit", 0, 0, good_exit},
+	{"iccid", 0, 0, cmd_iccid},
 	{"quit", 0, 0, good_exit},
 	{"readbin", 2, 2, cmd_readbin},
 	{"readrec", 2, 2, cmd_readrec},
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uicc/hlread.c	Thu Feb 04 02:28:52 2021 +0000
@@ -0,0 +1,68 @@
+/*
+ * 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;
+	unsigned len;
+	char buf[21];
+
+	rc = select_op(FILEID_MF);
+	if (rc < 0)
+		return(rc);
+	rc = select_op(EF_ICCID);
+	if (rc < 0)
+		return(rc);
+	rc = select_resp_get_transparent(&len);
+	if (rc < 0)
+		return(rc);
+	if (len != 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);
+}
--- a/uicc/select.c	Thu Feb 04 01:45:16 2021 +0000
+++ b/uicc/select.c	Thu Feb 04 02:28:52 2021 +0000
@@ -151,3 +151,75 @@
 		return(rc);
 	return parse_and_display_select_response();
 }
+
+u_char *
+extract_select_resp_tag(sought_tag)
+	unsigned sought_tag;
+{
+	unsigned offset, totlen, reclen;
+	u_char *dp, *endp;
+	int rc;
+
+	rc = select_resp_header_check(&offset, &totlen);
+	if (rc < 0)
+		return(0);
+	dp = sim_resp_data + offset;
+	endp = sim_resp_data + offset + totlen;
+	while (dp < endp) {
+		if (endp - dp < 2) {
+trunc_error:		fprintf(stderr,
+			"error: truncated TLV record in SELECT response\n");
+			return(0);
+		}
+		if ((dp[0] & 0x1F) == 0x1F) {
+			fprintf(stderr,
+		"error: extended tag not supported in SELECT response\n");
+			return(0);
+		}
+		if (dp[1] & 0x80) {
+			fprintf(stderr,
+		"error: extended length not supported in SELECT response\n");
+			return(0);
+		}
+		reclen = dp[1] + 2;
+		if (endp - dp < reclen)
+			goto trunc_error;
+		if (dp[0] == sought_tag)
+			return(dp);
+		dp += reclen;
+	}
+	fprintf(stderr, "error: tag 0x%02X not found in SELECT response\n",
+		sought_tag);
+	return(0);
+}
+
+select_resp_get_transparent(lenp)
+	unsigned *lenp;
+{
+	u_char *tlv;
+
+	tlv = extract_select_resp_tag(0x82);
+	if (!tlv)
+		return(-1);
+	if (tlv[1] != 2) {
+bad_file_desc:	fprintf(stderr, "error: file type is not transparent EF\n");
+		return(-1);
+	}
+	if (tlv[2] & 0x80)
+		goto bad_file_desc;
+	if ((tlv[2] & 0x38) == 0x38)
+		goto bad_file_desc;
+	if ((tlv[2] & 0x07) != 0x01)
+		goto bad_file_desc;
+	tlv = extract_select_resp_tag(0x80);
+	if (!tlv)
+		return(-1);
+	if (tlv[1] != 2) {
+		fprintf(stderr,
+			"error: file size TLV element has wrong length\n");
+		return(-1);
+	}
+	if (lenp)
+		*lenp = (tlv[2] << 8) | tlv[3];
+	return(0);
+}