annotate simtool/grcard2.c @ 154:ed34c8b7e2c9

fc-simtool: grcard2-set-pin[12] commands implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Feb 2021 02:29:07 +0000
parents
children 804a5b0797c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
154
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * I, Mother Mychaela, am hoping to get some SIM cards from Grcard
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * that follow the protocol which the Osmocom community has nicknamed
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * GrcardSIM2:
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 *
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * https://osmocom.org/projects/cellular-infrastructure/wiki/GrcardSIM2
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 *
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 * I haven't got these cards yet and may not get them for a long time,
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 * hence the following code has been written blindly, untested.
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 * If anyone in the community happens to have a sysmoSIM-GR2 card
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 * that was once (aeons ago) sold by Sysmocom, please test this code!
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 */
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <sys/types.h>
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <ctype.h>
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include <stdio.h>
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 #include <stdlib.h>
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 #include <pcsclite.h>
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 #include <winscard.h>
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 #include "globals.h"
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 cmd_grcard2_set_pin(argc, argv)
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 char **argv;
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 {
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 u_char cmd[13];
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 int rc;
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 /* Grcard2 proprietary command APDU */
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 cmd[0] = 0xA0;
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 cmd[1] = 0xD4;
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 cmd[2] = 0x3A;
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 switch (argv[0][15]) {
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 case '1':
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 cmd[3] = 0x01;
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 break;
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 case '2':
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 cmd[3] = 0x02;
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 break;
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 default:
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 fprintf(stderr, "BUG in grcard2-set-pinN command\n");
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 return(-1);
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 cmd[4] = 8;
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 rc = encode_pin_entry(argv[1], cmd + 5);
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (rc < 0)
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 return(rc);
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 rc = apdu_exchange(cmd, 13);
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 if (rc < 0)
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 return(rc);
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (sim_resp_sw != 0x9000) {
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw);
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 return(-1);
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 }
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return(0);
ed34c8b7e2c9 fc-simtool: grcard2-set-pin[12] commands implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }