aboutsummaryrefslogtreecommitdiffstats
path: root/src/sim/main.c
blob: fc7a7efd43fbda7a219550a95842e6c9fd85c773 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* main function
 *
 * (C) 2020 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef ARDUINO

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <math.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/time.h>
#include "../libdebug/debug.h"
#include "../liboptions/options.h"
#include "../libserial/serial.h"
#include "../libaaimage/aaimage.h"
#include "sim.h"
#include "sniffer.h"
#include "eeprom.h"

int num_kanal = 1;
sim_sniffer_t sim_sniffer;
sim_sim_t sim_sim;
const char *write_pdu_file = NULL;
static int quit = 0;
static const char *serialdev = "/dev/ttyUSB0";
static int baudrate = 9600;

static const char *eeprom_name = NULL;
static const char *futln = NULL;
static const char *sicherung = NULL;
static const char *karten = NULL;
static const char *sonder = NULL;
static const char *wartung = NULL;
static const char *pin = NULL;
#define MAX_DIR_COUNT 64
static int dir_count = 0;
static int dir_location[MAX_DIR_COUNT];
static const char *dir_number[MAX_DIR_COUNT];
static const char *dir_name[MAX_DIR_COUNT];
static const char *auth = NULL;

#define TIMEOUT	0.2

void print_help(const char *arg0)
{
	printf("Usage: %s [options] <command>\n", arg0);
	/*      -                                                                             - */
	printf("General options:\n");
	printf(" -h --help\n");
	printf("        This help\n");
	printf(" --config [~/]<path to config file>\n");
	printf("        Give a config file to use. If it starts with '~/', path is at home dir.\n");
	printf("        Each line in config file is one option, '-' or '--' must not be given!\n");
	debug_print_help();
	printf(" -s --serial-device <device>\n");
	printf("        Serial device (default = '%s')\n", serialdev);
	printf(" -b --baud-rate <baud>\n");
	printf("        Serial baud rate (default = %d)\n", baudrate);
	printf(" -w --write-pdu <filename>\n");
	printf("        Write PDU to file (sniffer only)\n");
	printf("\nSIM card simulator options:\n");
	printf(" -E --eeprom <name>\n");
	printf("        Stores and reads EEPROM data to/from file. The file is stored at\n");
	printf("        \"~/osmocom/analog/sim_<name>.eeprom\". If the file dos not exit yet,\n");
	printf("        the default values are used. Values are always overwritten with card\n");
	printf("        data, if defined.\n");
	printf(" -F --futln <phone number>\n");
	printf("        Give 7 digits subscriber ID (default = '%s')\n", FUTLN_DEFAULT);
	printf(" --sicherung <security code>\n");
	printf("        Card's security code for simple authentication (default = '%s')\n", SICHERUNG_DEFAULT);
	printf(" --kartenkennung <card ID>\n");
	printf("        Card's ID. Not relevant! (default = '%s')\n", KARTEN_DEFAULT);
	printf(" --sonder <special code>\n");
	printf("        Special codes are used for service cards (default = '%s')\n", SONDER_DEFAULT);
	printf(" --wartung <maitenance code>\n");
	printf("        May define features of service cards (default = '%s')\n", WARTUNG_DEFAULT);
	printf(" -P --pin <pin> | 0000\n");
	printf("        Give 4 .. 8 digits of pin. Use '0000' to disable. (default = '%s')\n", PIN_DEFAULT);
	printf("        This will also reset the PIN error counter and unlocks the card.\n");
	printf(" -D --directory <location> <number> <name> [--directory ...]\n");
	printf("        Give storage location '01' .. '%02d'. To erase give \"\" as number\n", directory_size() - 1);
	printf("        and name. This option can be given multiple times for more entries.\n");
	printf(" -A --authenticate 0x...\n");
	printf("        Give 64 Bit value for authentication response. (default = all bits 1)\n");
	printf("\nCommands are:\n");
	printf("        sniff - To passively sniff SIM card communication\n");
	printf("        sim - To simulate a SIM card\n");
}

#define OPT_SICHERUNG	256
#define OPT_KARTEN	257
#define OPT_SONDER	258
#define OPT_WARTUNG	259

void add_options(void)
{
	option_add('h', "help", 0);
	option_add('v', "debug", 1);
	option_add('s', "serial-device", 1);
	option_add('b', "baud-rate", 1);
	option_add('w', "write-pdu", 1);
	option_add('E', "eeprom", 1);
	option_add('F', "futln", 1);
	option_add(OPT_SICHERUNG, "sicherung", 1);
	option_add(OPT_KARTEN, "kartenkennung", 1);
	option_add(OPT_SONDER, "sonder", 1);
	option_add(OPT_WARTUNG, "wartung", 1);
	option_add('P', "pin", 1);
	option_add('D', "directory", 3);
	option_add('A', "auth", 1);
};

int handle_options(int short_option, int argi, char **argv)
{
	int rc;

	switch (short_option) {
	case 'h':
		print_help(argv[0]);
		return 0;
	case 'v':
		if (!strcasecmp(argv[argi], "list")) {
	                debug_list_cat();
			return 0;
		}
		rc = parse_debug_opt(argv[argi]);
		if (rc < 0) {
			fprintf(stderr, "Failed to parse debug option, please use -h for help.\n");
			return rc;
		}
		break;
	case 's':
		serialdev = options_strdup(argv[argi]);
		break;
	case 'b':
		baudrate = atoi(argv[argi]);
		break;
	case 'w':
		write_pdu_file = options_strdup(argv[argi]);
		break;
	case 'E':
		eeprom_name = options_strdup(argv[argi]);
		break;
	case 'F':
		futln = options_strdup(argv[argi]);
		break;
	case OPT_SICHERUNG:
		sicherung = options_strdup(argv[argi]);
		break;
	case OPT_KARTEN:
		karten = options_strdup(argv[argi]);
		break;
	case OPT_SONDER:
		sonder = options_strdup(argv[argi]);
		break;
	case OPT_WARTUNG:
		wartung = options_strdup(argv[argi]);
		break;
	case 'P':
		pin = options_strdup(argv[argi]);
		break;
	case 'D':
		if (dir_count == MAX_DIR_COUNT)
			break;
		dir_location[dir_count] = atoi(argv[argi + 0]);
		dir_number[dir_count] = options_strdup(argv[argi + 1]);
		dir_name[dir_count] = options_strdup(argv[argi + 2]);
		dir_count++;
		break;
	case 'A':
		auth = options_strdup(argv[argi]);
		break;
	default:
		return -EINVAL;
	}

	return 1;
}

/* EERPOM emulation */

static uint8_t eeprom[2048];

uint8_t eeprom_read(enum eeprom_locations loc)
{
	if (loc >= sizeof(eeprom))
		abort();

	return eeprom[loc];
}

void eeprom_write(enum eeprom_locations loc, uint8_t value)
{
	if (loc >= sizeof(eeprom))
		abort();

	eeprom[loc] = value;
}

uint8_t *eeprom_memory(void)
{
	return eeprom;
}

size_t eeprom_length(void)
{
	return sizeof(eeprom);
}

/* main loop for interfacing serial with sim / sniffer */

int main_loop(serial_t *serial, int sniffer)
{
	int rc, cts = 0, last_cts = 0;
	uint8_t byte;
	int skip_bytes = 0;
	int work = 0;

	struct timeval tv;
	double now, timer = 0;

	quit = 0;

	while (!quit) {
		gettimeofday(&tv, NULL);
		now = (double)tv.tv_usec * 0.000001 + tv.tv_sec;

		/* only check CTS when no work was done
		 * this is because USB query may take some time
		 * and we don't want to block transfer
		 */
		if (!work) {
			cts = serial_cts(serial);
			/* initially AND when CTS becomes 1 (pulled to low by reset line) */
			if (last_cts != cts) {
				if (sniffer == 1)
					sniffer_reset(&sim_sniffer);
				else
					sim_reset(&sim_sim, cts);
				timer = 0;
			}
			last_cts = cts;
		}
		work = 0;

		if (sniffer == 0) {
			rc = sim_tx(&sim_sim);
			if (rc >= 0) {
				byte = rc;
				serial_write(serial, &byte, 1);
				work = 1;
				skip_bytes++;
			}
		}

		rc = serial_read(serial, &byte, 1);
		if (rc > 0)
			work = 1;
		/* ignore while reset is low */
		if (cts)
			continue;
		if (rc == 1) {
			timer = now;
			/* count length, to remove echo from transmission */
			if (!skip_bytes) {
				if (sniffer == 1)
					sniffer_rx(&sim_sniffer, byte);
				else
					sim_rx(&sim_sim, byte);
			} else {
				/* done eliminating TX data, so we reset timer */
				if (--skip_bytes == 0)
					timer = 0;
			}
		} else {
			rc = -1;
			if (timer && now - timer > 12.0 * 5.0 / (double)baudrate) {
				if (sniffer == 1)
					sniffer_timeout(&sim_sniffer);
				else
					sim_timeout(&sim_sim);
				timer = 0;
				skip_bytes = 0;
			}
		}

		if (!work) {
			/* sleep some time if nothing was received */
			usleep(100);
		}
	}

	return quit;
}

void sighandler(int sigset)
{
	if (sigset == SIGHUP)
		return;
	if (sigset == SIGPIPE)
		return;

	printf("Signal received: %d\n", sigset);

	quit = -1;
}

int main(int argc, char *argv[])
{
	const char *home;
	char eeprom_file[128];
	FILE *fp;
	serial_t *serial = NULL;
	uint8_t ebdt_data[9];
	int rc, argi;
	int sniffer = 0;
	int i;

	debuglevel = DEBUG_INFO;

	add_options();
	rc = options_config_file(argc, argv, "~/.osmocom/analog/sim.conf", handle_options);
	if (rc < 0)
		return 0;

	rc = sim_init_eeprom();
	if (rc < 0)
		return rc;

	/* parse command line */
	argi = options_command_line(argc, argv, handle_options);
	if (argi <= 0)
		return argi;

	/* read from eeprom file, if defined and exists */
	if (eeprom_name) {
		/* open config file */
		home = getenv("HOME");
		if (home == NULL)
			return 1;
		sprintf(eeprom_file, "%s/.osmocom/analog/sim_%s.eeprom", home, eeprom_name);
		
		fp = fopen(eeprom_file, "r");
		if (fp) {
			rc = fread(eeprom_memory(), eeprom_length(), 1, fp);
			fclose(fp);
		} else
			PDEBUG(DOPTIONS, DEBUG_INFO, "EEPROM file '%s' does not exist yet.\n", eeprom_file);

	}

	/* check version */
	if (eeprom_read(EEPROM_MAGIC + 0) != 'C' || eeprom_read(EEPROM_MAGIC + 1) != '0' + EEPROM_VERSION) {
		PDEBUG(DOPTIONS, DEBUG_ERROR, "EEPROM file '%s' is not compatible with this version of program, please remove it!\n", eeprom_file);
		return 1;
	}

	/* apply config to eeprom, if defined */
	ebdt_data[0] = eeprom_read(EEPROM_FUTLN_H);
	ebdt_data[1] = eeprom_read(EEPROM_FUTLN_M);
	ebdt_data[2] = eeprom_read(EEPROM_FUTLN_L);
	ebdt_data[3] = eeprom_read(EEPROM_SICH_H);
	ebdt_data[4] = eeprom_read(EEPROM_SICH_L);
	ebdt_data[5] = eeprom_read(EEPROM_SONDER_H);
	ebdt_data[6] = eeprom_read(EEPROM_SONDER_L);
	ebdt_data[7] = eeprom_read(EEPROM_WARTUNG_H);
	ebdt_data[8] = eeprom_read(EEPROM_WARTUNG_L);
	rc = encode_ebdt(ebdt_data, futln, sicherung, karten, sonder, wartung);
	if (rc < 0)
		return 0;
	eeprom_write(EEPROM_FUTLN_H, ebdt_data[0]);
	eeprom_write(EEPROM_FUTLN_M, ebdt_data[1]);
	eeprom_write(EEPROM_FUTLN_L, ebdt_data[2]);
	eeprom_write(EEPROM_SICH_H, ebdt_data[3]);
	eeprom_write(EEPROM_SICH_L, ebdt_data[4]);
	eeprom_write(EEPROM_SONDER_H, ebdt_data[5]);
	eeprom_write(EEPROM_SONDER_L, ebdt_data[6]);
	eeprom_write(EEPROM_WARTUNG_H, ebdt_data[7]);
	eeprom_write(EEPROM_WARTUNG_L, ebdt_data[8]);
	if (pin) {
		if (strlen(pin) < 4 || strlen(pin) > 8) {
			PDEBUG(DSIM7, DEBUG_NOTICE, "Given PIN '%s' has invalid length. (Must be 4 .. 8)\n", pin);
			return 0;
		}
		eeprom_write(EEPROM_FLAGS, (strlen(pin) << EEPROM_FLAG_PIN_LEN) | (MAX_PIN_TRY << EEPROM_FLAG_PIN_TRY));
		for (i = 0; i < (int)strlen(pin); i++)
			eeprom_write(EEPROM_PIN_DATA + i, pin[i]);
	}
	for (i = 0; i < dir_count; i++) {
		uint8_t data[24];
		rc = encode_directory(data, dir_number[i], dir_name[i]);
		if (rc < 0)
			return 0;
		rc = save_directory(dir_location[i], data);
		if (rc < 0)
			return 0;
	}
	if (auth) {
		uint64_t value = strtoull(auth, NULL, 0);
		for (i = 0; i < 8; i++)
			eeprom_write(EEPROM_AUTH_DATA, value >> (8 * (7 - i)));
	}

	if (argi >= argc) {
		fprintf(stderr, "Expecting command, use '-h' for help!\n");
		return 0;
	} else if (!strcmp(argv[argi], "sniff")) {
		sniffer = 1;
	} else if (!strcmp(argv[argi], "sim")) {
		sniffer = 0;
	} else {
		print_help(argv[0]);
		return -EINVAL;
	}

	/* open serial device */
	serial = serial_open(serialdev, baudrate, 8, 'e', 2, 'd', 'd', 0, 1.0, 0.0);
	if (!serial) {
		printf("Serial failed: %s\n", serial_errnostr);
		goto error;
	}

	if (sniffer == 1)
		printf("SIM analyzer ready, please start the phone!\n");
	else {
		char temp[5][16];
		print_aaimage();
		decode_ebdt(ebdt_data, temp[0], temp[1], temp[2], temp[3], temp[4]);
		printf("FUTLN=%s, Sicherungscode=%s, Kartekennung=%s, Sonderheitenschluessel=%s, Wartungsschluessel=%s\n", temp[0], temp[1], temp[2], temp[3], temp[4]);
		for (i = 0; i < 8; i++)
			temp[0][i] = eeprom_read(EEPROM_PIN_DATA + i);
		temp[0][(eeprom_read(EEPROM_FLAGS) >> EEPROM_FLAG_PIN_LEN) & 0xf] = '\0';
		uint64_t auth_value = 0;
		for (i = 0; i < 8; i++)
			auth_value |= eeprom_read(EEPROM_AUTH_DATA + i) << (8 * (7 - i));
		printf("PIN=%s, auth response=0x%016" PRIx64 "\n", temp[0], auth_value);
		printf("Telephone directory has %d entries.\n", directory_size() - 1);
		for (i = 0; i < directory_size() - 1; i++) {
			uint8_t data[24];
			char number[32], name[32];
			load_directory(i + 1, data);
			decode_directory(data, number, name);
			if (number[0])
				printf(" -> %02d %16s %s\n", i + 1, number, name);
		}
		printf("SIM emulator ready, please start the phone!\n");
	}

	/* catch signals */
	signal(SIGINT, sighandler);
	signal(SIGHUP, sighandler);
	signal(SIGTERM, sighandler);
	signal(SIGPIPE, sighandler);

	/* run main loop until terminated by user */
	main_loop(serial, sniffer);

	/* reset signals */
	signal(SIGINT, SIG_DFL);
	signal(SIGHUP, SIG_DFL);
	signal(SIGTERM, SIG_DFL);
	signal(SIGPIPE, SIG_DFL);

	/* write to eeprom file, if defined */
	if (eeprom_name) {
		fp = fopen(eeprom_file, "w");
		if (fp) {
			fwrite(eeprom_memory(), eeprom_length(), 1, fp);
			fclose(fp);
			PDEBUG(DOPTIONS, DEBUG_INFO, "EEPROM file '%s' written.\n", eeprom_file);
		} else
			PDEBUG(DOPTIONS, DEBUG_INFO, "EEPROM file '%s' cannot be written. (errno = %d)\n", eeprom_file, errno);
	}

error:
	if (serial)
		serial_close(serial);

	options_free();

	return 0;
}

#endif /* ARDUINO */