FreeCalypso > hg > fc-pcsc-tools
annotate libcommon/apdu.c @ 87:bc862d41f96b
pb-restore command implemented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Wed, 17 Feb 2021 07:04:05 +0000 |
parents | f7145c77b7fb |
children |
rev | line source |
---|---|
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 #include <sys/types.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 #include <stdio.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 #include <stdlib.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 #include <pcsclite.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <winscard.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include "cardif.h" |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 u_char sim_resp_data[258]; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 unsigned sim_resp_data_len, sim_resp_sw; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 apdu_exchange(cmd_apdu, cmd_apdu_len) |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 u_char *cmd_apdu; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 unsigned cmd_apdu_len; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 LONG rv; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 DWORD dwRecvLength; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 u_char *sw; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 dwRecvLength = 258; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 rv = SCardTransmit(hCard, SCARD_PCI_T0, cmd_apdu, cmd_apdu_len, NULL, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 sim_resp_data, &dwRecvLength); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 if (rv != SCARD_S_SUCCESS) { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 fprintf(stderr, "SCardTransmit: %s\n", |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 pcsc_stringify_error(rv)); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 return(-1); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (dwRecvLength < 2) { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 fprintf(stderr, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 "error: SCardTransmit response is shorter than 2 SW bytes\n"); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 return(-1); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 sim_resp_data_len = dwRecvLength - 2; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 sw = sim_resp_data + sim_resp_data_len; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 sim_resp_sw = (sw[0] << 8) | sw[1]; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 return(0); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |