changeset 1:f7a03e53bb2c

fc-pcsc-atr ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 01:09:23 +0000
parents f4479a0d4cea
children 11f4f8a8fa33
files .hgignore pcsc/Makefile pcsc/atrfunc.c pcsc/atrmain.c pcsc/connect.c pcsc/rdselect.c
diffstat 6 files changed, 138 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Mar 14 00:45:15 2021 +0000
+++ b/.hgignore	Sun Mar 14 01:09:23 2021 +0000
@@ -2,4 +2,5 @@
 
 \.[oa]$
 
+^pcsc/fc-pcsc-atr$
 ^pcsc/fc-pcsc-list$
--- a/pcsc/Makefile	Sun Mar 14 00:45:15 2021 +0000
+++ b/pcsc/Makefile	Sun Mar 14 01:09:23 2021 +0000
@@ -1,15 +1,19 @@
 CC=	gcc
 CFLAGS=	-O2 -I/usr/include/PCSC
-PROGS=	fc-pcsc-list
+PROGS=	fc-pcsc-atr fc-pcsc-list
 
 INSTALL_PREFIX=	/opt/freecalypso
 
 INSTBIN=${INSTALL_PREFIX}/bin
 
+ATR_OBJS=	atrfunc.o atrmain.o connect.o context.o rdselect.o
 LIST_OBJS=	context.o rdlist.o
 
 all:	${PROGS}
 
+fc-pcsc-atr:	${ATR_OBJS}
+	${CC} ${CFLAGS} -o $@ ${ATR_OBJS} -lpcsclite
+
 fc-pcsc-list:	${LIST_OBJS}
 	${CC} ${CFLAGS} -o $@ ${LIST_OBJS} -lpcsclite
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pcsc/atrfunc.c	Sun Mar 14 01:09:23 2021 +0000
@@ -0,0 +1,37 @@
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+#include <reader.h>
+
+extern SCARDCONTEXT hContext;
+extern SCARDHANDLE hCard;
+
+#define	MAX_ATR_BYTES	33
+
+retrieve_atr(decor)
+{
+	u_char atrbuf[MAX_ATR_BYTES];
+	LONG rv;
+	DWORD dwAttrLen;
+	unsigned n;
+
+	dwAttrLen = MAX_ATR_BYTES;
+	rv = SCardGetAttrib(hCard, SCARD_ATTR_ATR_STRING, atrbuf, &dwAttrLen);
+	if (rv != SCARD_S_SUCCESS) {
+		fprintf(stderr, "SCardGetAttrib for ATR: %s\n",
+			pcsc_stringify_error(rv));
+		SCardReleaseContext(hContext);
+		exit(1);
+	}
+	if (decor)
+		printf("ATR:");
+	for (n = 0; n < dwAttrLen; n++) {
+		if (decor)
+			putchar(' ');
+		printf("%02X", atrbuf[n]);
+	}
+	putchar('\n');
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pcsc/atrmain.c	Sun Mar 14 01:09:23 2021 +0000
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+
+extern SCARDCONTEXT hContext;
+extern SCARDHANDLE hCard;
+extern char *selected_reader;
+
+main(argc, argv)
+	char **argv;
+{
+	parse_reader_select_opt(argc, argv);
+	setup_pcsc_context();
+	get_reader_list();
+	select_reader_by_num();
+	printf("Card reader name: %s\n", selected_reader);
+	connect_to_card();
+	retrieve_atr(1);
+	SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
+	SCardReleaseContext(hContext);
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pcsc/connect.c	Sun Mar 14 01:09:23 2021 +0000
@@ -0,0 +1,47 @@
+#include <string.h>
+#include <strings.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <pcsclite.h>
+#include <winscard.h>
+
+extern SCARDCONTEXT hContext;
+extern char *reader_list;
+extern unsigned select_reader_num;
+
+SCARDHANDLE hCard;
+char *selected_reader;
+
+select_reader_by_num()
+{
+	char *cp;
+	unsigned num;
+
+	for (cp = reader_list, num = 0; *cp; num++) {
+		if (num == select_reader_num) {
+			selected_reader = cp;
+			return(0);
+		}
+		cp += strlen(cp) + 1;
+	}
+	fprintf(stderr,
+		"error: requested reader #%u, but only %u reader(s) found\n",
+		select_reader_num, num);
+	SCardReleaseContext(hContext);
+	exit(1);
+}
+
+connect_to_card()
+{
+	LONG rv;
+	DWORD dwActiveProtocol;
+
+	rv = SCardConnect(hContext, selected_reader, SCARD_SHARE_EXCLUSIVE,
+			  SCARD_PROTOCOL_T0, &hCard, &dwActiveProtocol);
+	if (rv != SCARD_S_SUCCESS) {
+		fprintf(stderr, "SCardConnect: %s\n", pcsc_stringify_error(rv));
+		SCardReleaseContext(hContext);
+		exit(1);
+	}
+	return(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pcsc/rdselect.c	Sun Mar 14 01:09:23 2021 +0000
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+unsigned select_reader_num;
+
+parse_reader_select_opt(argc, argv)
+	char **argv;
+{
+	extern char *optarg;
+	int c;
+
+	while ((c = getopt(argc, argv, "p:")) != EOF) {
+		switch (c) {
+		case 'p':
+			select_reader_num = atoi(optarg);
+			continue;
+		case '?':
+		default:
+			/* error msg already printed */
+			exit(1);
+		}
+	}
+	return(0);
+}