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