view simtool/grcard2.c @ 81:8c23d2a9c013

doc/GrcardSIM2-programming: update for fcsim1-program
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 09 Apr 2021 08:35:54 +0000
parents 4ebd67176b73
children
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
 *
 * FreeCalypso Community SIM model FCSIM1 is 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);
}