annotate pcsc/main.c @ 3:45ea06eaa9fd

fc-pcsc-backend main program put together
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 02:21:49 +0000
parents pcsc/atrmain.c@11f4f8a8fa33
children 60fd23186e2e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
1 #include <sys/types.h>
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
2 #include <ctype.h>
1
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <stdio.h>
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 #include <stdlib.h>
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <pcsclite.h>
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <winscard.h>
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 extern SCARDCONTEXT hContext;
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 extern SCARDHANDLE hCard;
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 extern char *selected_reader;
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
3
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
12 static
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
13 decode_hex_digit(c)
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
14 {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
15 if (isdigit(c))
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
16 return c - '0';
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
17 else if (islower(c))
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
18 return c - 'a' + 10;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
19 else
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
20 return c - 'A' + 10;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
21 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
22
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
23 static
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
24 parse_hex_input(inbuf, outbuf)
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
25 char *inbuf;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
26 u_char *outbuf;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
27 {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
28 char *cp;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
29 unsigned count;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
30
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
31 count = 0;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
32 for (cp = inbuf; ; ) {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
33 while (isspace(*cp))
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
34 cp++;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
35 if (!*cp)
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
36 break;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
37 if (!isxdigit(cp[0]) || !isxdigit(cp[1])) {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
38 printf("error: invalid hex APDU input\n");
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
39 return(-1);
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
40 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
41 if (count >= 260) {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
42 printf("error: command APDU is too long\n");
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
43 return(-1);
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
44 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
45 outbuf[count++] = (decode_hex_digit(cp[0]) << 4) |
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
46 decode_hex_digit(cp[1]);
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
47 cp += 2;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
48 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
49 return count;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
50 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
51
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
52 static void
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
53 apdu_exchange(cmd_apdu, cmd_apdu_len)
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
54 u_char *cmd_apdu;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
55 unsigned cmd_apdu_len;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
56 {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
57 LONG rv;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
58 DWORD dwRecvLength;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
59 u_char sim_resp_data[258];
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
60 unsigned n;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
61
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
62 dwRecvLength = 258;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
63 rv = SCardTransmit(hCard, SCARD_PCI_T0, cmd_apdu, cmd_apdu_len, NULL,
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
64 sim_resp_data, &dwRecvLength);
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
65 if (rv != SCARD_S_SUCCESS) {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
66 printf("SCardTransmit error: %s\n", pcsc_stringify_error(rv));
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
67 return;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
68 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
69 if (dwRecvLength < 2) {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
70 printf(
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
71 "error: SCardTransmit response is shorter than 2 SW bytes\n");
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
72 return;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
73 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
74 for (n = 0; n < dwRecvLength; n++)
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
75 printf("%02X", sim_resp_data[n]);
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
76 putchar('\n');
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
77 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
78
1
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 main(argc, argv)
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 char **argv;
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 {
3
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
82 char inbuf[576];
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
83 u_char cmd[260];
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
84 int rc;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
85
1
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 parse_reader_select_opt(argc, argv);
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 setup_pcsc_context();
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 get_reader_list();
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 select_reader_by_num();
3
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
90 printf("R %s\n", selected_reader);
1
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 connect_to_card();
3
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
92 retrieve_atr("A");
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
93 putchar('\n');
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
94
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
95 while (fgets(inbuf, sizeof inbuf, stdin)) {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
96 rc = parse_hex_input(inbuf, cmd);
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
97 if (rc < 0)
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
98 continue;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
99 if (rc < 5) {
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
100 printf("error: command APDU is too short\n");
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
101 continue;
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
102 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
103 apdu_exchange(cmd, rc);
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
104 }
45ea06eaa9fd fc-pcsc-backend main program put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
105
1
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 SCardDisconnect(hCard, SCARD_UNPOWER_CARD);
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 SCardReleaseContext(hContext);
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 exit(0);
f7a03e53bb2c fc-pcsc-atr ported over
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }