view uicc/pins.c @ 86:de23872796cb

fc-uicc-tool: update-rec-imm and update-rec-fill ported over from fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 11 Apr 2021 03:19:22 +0000
parents 97646b363eaa
children
line wrap: on
line source

/*
 * This module implements the standard set of commands for working
 * with UICC PINs; because all of these commands take a user-specified
 * P2 key ID, they should work with ADM PINs as well.
 */

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

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

	/* VERIFY PIN command APDU */
	cmd[0] = 0x00;
	cmd[1] = 0x20;
	cmd[2] = 0x00;
	cmd[3] = strtoul(argv[1], 0, 0);
	cmd[4] = 8;
	rc = encode_pin_entry(argv[2], 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_verify_hex(argc, argv)
	char **argv;
{
	u_char cmd[13];
	int rc;

	/* VERIFY PIN command APDU */
	cmd[0] = 0x00;
	cmd[1] = 0x20;
	cmd[2] = 0x00;
	cmd[3] = strtoul(argv[1], 0, 0);
	cmd[4] = 8;
	rc = decode_hex_data_from_string(argv[2], 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);
}

verify_pin_func(p2, pin)
	unsigned p2;
	char *pin;
{
	u_char cmd[13];
	int rc;

	/* VERIFY PIN command APDU */
	cmd[0] = 0x00;
	cmd[1] = 0x20;
	cmd[2] = 0x00;
	cmd[3] = p2;
	cmd[4] = 8;
	rc = encode_pin_entry(pin, 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 to VERIFY PIN: %04X\n",
			sim_resp_sw);
		return(-1);
	}
	return(0);
}

cmd_change_pin(argc, argv)
	char **argv;
{
	u_char cmd[21];
	int rc;

	/* CHANGE PIN command APDU */
	cmd[0] = 0x00;
	cmd[1] = 0x24;
	cmd[2] = 0x00;
	cmd[3] = strtoul(argv[1], 0, 0);
	cmd[4] = 16;
	rc = encode_pin_entry(argv[2], cmd + 5);
	if (rc < 0)
		return(rc);
	rc = encode_pin_entry(argv[3], cmd + 13);
	if (rc < 0)
		return(rc);
	rc = apdu_exchange(cmd, 21);
	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_disable_pin(argc, argv)
	char **argv;
{
	u_char cmd[13];
	int rc;

	/* DISABLE PIN command APDU */
	cmd[0] = 0x00;
	cmd[1] = 0x26;
	cmd[2] = 0x00;
	cmd[3] = strtoul(argv[1], 0, 0);
	cmd[4] = 8;
	rc = encode_pin_entry(argv[2], 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_enable_pin(argc, argv)
	char **argv;
{
	u_char cmd[13];
	int rc;

	/* ENABLE PIN command APDU */
	cmd[0] = 0x00;
	cmd[1] = 0x28;
	cmd[2] = 0x00;
	cmd[3] = strtoul(argv[1], 0, 0);
	cmd[4] = 8;
	rc = encode_pin_entry(argv[2], 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_unblock_pin(argc, argv)
	char **argv;
{
	u_char cmd[21];
	int rc;

	/* UNBLOCK PIN command APDU */
	cmd[0] = 0x00;
	cmd[1] = 0x2C;
	cmd[2] = 0x00;
	cmd[3] = strtoul(argv[1], 0, 0);
	cmd[4] = 16;
	rc = encode_pin_entry(argv[2], cmd + 5);
	if (rc < 0)
		return(rc);
	rc = encode_pin_entry(argv[3], cmd + 13);
	if (rc < 0)
		return(rc);
	rc = apdu_exchange(cmd, 21);
	if (rc < 0)
		return(rc);
	if (sim_resp_sw != 0x9000) {
		fprintf(stderr, "bad SW response: %04X\n", sim_resp_sw);
		return(-1);
	}
	return(0);
}

/* retrieving PIN attempt counters from the card */

cmd_pin_attempt_cnt(argc, argv)
	char **argv;
{
	u_char cmd[5];
	int rc;

	/* VERIFY PIN command APDU */
	cmd[0] = 0x00;
	cmd[1] = 0x20;
	cmd[2] = 0x00;
	cmd[3] = strtoul(argv[1], 0, 0);
	cmd[4] = 0;
	rc = apdu_exchange(cmd, 5);
	if (rc < 0)
		return(rc);
	printf("%04X\n", sim_resp_sw);
	return(0);
}

cmd_puk_attempt_cnt(argc, argv)
	char **argv;
{
	u_char cmd[5];
	int rc;

	/* UNBLOCK PIN command APDU */
	cmd[0] = 0x00;
	cmd[1] = 0x2C;
	cmd[2] = 0x00;
	cmd[3] = strtoul(argv[1], 0, 0);
	cmd[4] = 0;
	rc = apdu_exchange(cmd, 5);
	if (rc < 0)
		return(rc);
	printf("%04X\n", sim_resp_sw);
	return(0);
}