view simtool/grcard2.c @ 47:b0cf75d0bb2d

doc/Serial-SIM-readers article written
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Mar 2021 04:32:18 +0000
parents ddd767f6e15b
children 4ebd67176b73
line wrap: on
line source

/*
 * This module implements special commands for the grcard.cn card model
 * which is known in the Osmocom community as GrcardSIM2:
 *
 * https://osmocom.org/projects/cellular-infrastructure/wiki/GrcardSIM2
 *
 * The sample cards which Mother Mychaela received from Grcard in 2021-02
 * are GrcardSIM2, and so are historical sysmoSIM-GR2 and 30C3 cards.
 */

#include <sys/types.h>
#include <stdio.h>
#include "simresp.h"
#include "curfile.h"
#include "file_id.h"

cmd_grcard2_set_pin(argc, argv)
	char **argv;
{
	u_char cmd[13];
	int rc;

	/* Grcard2 proprietary command APDU */
	cmd[0] = 0xA0;
	cmd[1] = 0xD4;
	cmd[2] = 0x3A;
	switch (argv[0][15]) {
	case '1':
		cmd[3] = 0x01;
		break;
	case '2':
		cmd[3] = 0x02;
		break;
	default:
		fprintf(stderr, "BUG in grcard2-set-pinN command\n");
		return(-1);
	}
	cmd[4] = 8;
	rc = encode_pin_entry(argv[1], cmd + 5);
	if (rc < 0)
		return(rc);
	rc = apdu_exchange(cmd, 13);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw);
		return(-1);
	}
	return(0);
}

cmd_grcard2_set_puk(argc, argv)
	char **argv;
{
	u_char cmd[13];
	int rc;

	/* Grcard2 proprietary command APDU */
	cmd[0] = 0xA0;
	cmd[1] = 0xD4;
	cmd[2] = 0x3B;
	switch (argv[0][15]) {
	case '1':
		cmd[3] = 0x00;
		break;
	case '2':
		cmd[3] = 0x02;
		break;
	default:
		fprintf(stderr, "BUG in grcard2-set-pukN command\n");
		return(-1);
	}
	cmd[4] = 8;
	rc = encode_pin_entry(argv[1], cmd + 5);
	if (rc < 0)
		return(rc);
	rc = apdu_exchange(cmd, 13);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw);
		return(-1);
	}
	return(0);
}

cmd_grcard2_set_adm5(argc, argv)
	char **argv;
{
	u_char cmd[13];
	int rc;

	/* Grcard2 proprietary command APDU */
	cmd[0] = 0xA0;
	cmd[1] = 0xD4;
	cmd[2] = 0x3A;
	cmd[3] = 0x05;
	cmd[4] = 8;
	rc = encode_pin_entry(argv[1], cmd + 5);
	if (rc < 0)
		return(rc);
	rc = apdu_exchange(cmd, 13);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw);
		return(-1);
	}
	return(0);
}

cmd_grcard2_set_adm5_hex(argc, argv)
	char **argv;
{
	u_char cmd[13];
	int rc;

	/* Grcard2 proprietary command APDU */
	cmd[0] = 0xA0;
	cmd[1] = 0xD4;
	cmd[2] = 0x3A;
	cmd[3] = 0x05;
	cmd[4] = 8;
	rc = decode_hex_data_from_string(argv[1], cmd + 5, 8, 8);
	if (rc < 0)
		return(rc);
	rc = apdu_exchange(cmd, 13);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw);
		return(-1);
	}
	return(0);
}

cmd_grcard2_set_super(argc, argv)
	char **argv;
{
	u_char cmd[13];
	int rc;

	/* Grcard2 proprietary command APDU */
	cmd[0] = 0xA0;
	cmd[1] = 0xD4;
	cmd[2] = 0x3A;
	cmd[3] = 0x0B;
	cmd[4] = 8;
	rc = encode_pin_entry(argv[1], cmd + 5);
	if (rc < 0)
		return(rc);
	rc = apdu_exchange(cmd, 13);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw);
		return(-1);
	}
	return(0);
}

cmd_grcard2_set_super_hex(argc, argv)
	char **argv;
{
	u_char cmd[13];
	int rc;

	/* Grcard2 proprietary command APDU */
	cmd[0] = 0xA0;
	cmd[1] = 0xD4;
	cmd[2] = 0x3A;
	cmd[3] = 0x0B;
	cmd[4] = 8;
	rc = decode_hex_data_from_string(argv[1], cmd + 5, 8, 8);
	if (rc < 0)
		return(rc);
	rc = apdu_exchange(cmd, 13);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw);
		return(-1);
	}
	return(0);
}

static
select_ef_weki()
{
	int rc;

	rc = select_op(DF_GSM);
	if (rc < 0)
		return(rc);
	rc = select_op(0x0001);		/* proprietary EF */
	if (rc < 0)
		return(rc);
	rc = parse_ef_select_response();
	if (rc < 0)
		return(rc);
	if (curfile_structure != 0x00 || curfile_total_size != 35) {
		fprintf(stderr,
			"error: EF_WEKI is not a transparent EF of 35 bytes\n");
		return(-1);
	}
	return(0);
}

cmd_grcard2_set_comp128(argc, argv)
	char **argv;
{
	int rc;
	unsigned code;
	u_char magic_byte;

	if (argv[1][0] < '1' || argv[1][0] > '3' || argv[1][1]) {
		fprintf(stderr, "error: invalid argument\n");
		return(-1);
	}
	code = argv[1][0] - '1';
	rc = select_ef_weki();
	if (rc < 0)
		return(rc);
	rc = readbin_op(2, 1);
	if (rc < 0)
		return(rc);
	magic_byte = sim_resp_data[0];
	magic_byte &= 0xFC;
	magic_byte |= code;
	return update_bin_op(2, &magic_byte, 1);
}

cmd_grcard2_set_ki(argc, argv)
	char **argv;
{
	u_char ki[16];
	int rc;

	rc = decode_hex_data_from_string(argv[1], ki, 16, 16);
	if (rc < 0)
		return(rc);
	rc = select_ef_weki();
	if (rc < 0)
		return(rc);
	return update_bin_op(3, ki, 16);
}