fileid change/cleanup

This commit is contained in:
NIIBE Yutaka
2012-05-17 12:15:24 +09:00
parent a921d408c5
commit 429c6f3d8b
6 changed files with 54 additions and 55 deletions

View File

@@ -1,3 +1,16 @@
2012-05-16 Niibe Yutaka <gniibe@fsij.org>
* tool/gnuk_put_binary.py (main): Fix fileid.
* tool/gnuk_put_binary_libusb.py: Ditto.
* src/openpgp.c (FILE_EF_RANDOM): Remove.
(cmd_update_binary, cmds): ifdef CERTDO_SUPPORT.
(cmd_write_binary): Fix fileid.
* src/flash.c (flash_check_blank): Always enable.
(flash_erase_binary): ifdef CERTDO_SUPPORT.
(flash_write_binary): Call flash_check_blank.
2012-05-15 Niibe Yutaka <gniibe@fsij.org> 2012-05-15 Niibe Yutaka <gniibe@fsij.org>
* Version 0.18. * Version 0.18.

View File

@@ -579,25 +579,23 @@ flash_cnt123_clear (const uint8_t **addr_p)
} }
#if defined(CERTDO_SUPPORT)
static int static int
flash_check_blank (const uint8_t *page, int size) flash_check_blank (const uint8_t *p_start, int size)
{ {
const uint8_t *p; const uint8_t *p;
for (p = page; p < page + size; p++) for (p = p_start; p < p_start + size; p++)
if (*p != 0xff) if (*p != 0xff)
return 0; return 0;
return 1; return 1;
} }
#endif
#if defined(CERTDO_SUPPORT)
#define FLASH_CH_CERTIFICATE_SIZE 2048 #define FLASH_CH_CERTIFICATE_SIZE 2048
int int
flash_erase_binary (uint8_t file_id) flash_erase_binary (uint8_t file_id)
{ {
#if defined(CERTDO_SUPPORT)
if (file_id == FILEID_CH_CERTIFICATE) if (file_id == FILEID_CH_CERTIFICATE)
{ {
const uint8_t *p = &ch_certificate_start; const uint8_t *p = &ch_certificate_start;
@@ -611,12 +609,10 @@ flash_erase_binary (uint8_t file_id)
return 0; return 0;
} }
else
#else return -1;
(void)file_id;
#endif
return -1;
} }
#endif
int int
@@ -626,19 +622,18 @@ flash_write_binary (uint8_t file_id, const uint8_t *data,
uint16_t maxsize; uint16_t maxsize;
const uint8_t *p; const uint8_t *p;
#if defined(CERTDO_SUPPORT)
if (file_id == FILEID_CH_CERTIFICATE)
{
maxsize = FLASH_CH_CERTIFICATE_SIZE;
p = &ch_certificate_start;
}
else
#endif
if (file_id == FILEID_SERIAL_NO) if (file_id == FILEID_SERIAL_NO)
{ {
maxsize = 6; maxsize = 6;
p = &openpgpcard_aid[8]; p = &openpgpcard_aid[8];
} }
#if defined(CERTDO_SUPPORT)
else if (file_id == FILEID_CH_CERTIFICATE)
{
maxsize = FLASH_CH_CERTIFICATE_SIZE;
p = &ch_certificate_start;
}
#endif
else else
return -1; return -1;
@@ -650,6 +645,9 @@ flash_write_binary (uint8_t file_id, const uint8_t *data,
uint32_t addr; uint32_t addr;
int i; int i;
if (flash_check_blank (p, len) == 0)
return -1;
addr = (uint32_t)p + offset; addr = (uint32_t)p + offset;
for (i = 0; i < len/2; i++) for (i = 0; i < len/2; i++)
{ {

View File

@@ -137,9 +137,8 @@ extern void flash_clear_halfword (uint32_t addr);
extern void flash_increment_counter (uint8_t counter_tag_nr); extern void flash_increment_counter (uint8_t counter_tag_nr);
extern void flash_reset_counter (uint8_t counter_tag_nr); extern void flash_reset_counter (uint8_t counter_tag_nr);
#define FILEID_CH_CERTIFICATE 0 #define FILEID_SERIAL_NO 0
#define FILEID_RANDOM 1 #define FILEID_CH_CERTIFICATE 1
#define FILEID_SERIAL_NO 2
extern int flash_erase_binary (uint8_t file_id); extern int flash_erase_binary (uint8_t file_id);
extern int flash_write_binary (uint8_t file_id, const uint8_t *data, uint16_t len, uint16_t offset); extern int flash_write_binary (uint8_t file_id, const uint8_t *data, uint16_t len, uint16_t offset);

View File

@@ -78,7 +78,6 @@ set_res_sw (uint8_t sw1, uint8_t sw2)
#define FILE_EF_DIR 3 #define FILE_EF_DIR 3
#define FILE_EF_SERIAL 4 #define FILE_EF_SERIAL 4
#define FILE_EF_CH_CERTIFICATE 5 #define FILE_EF_CH_CERTIFICATE 5
#define FILE_EF_RANDOM 6
static uint8_t file_selection; static uint8_t file_selection;
@@ -701,6 +700,7 @@ cmd_internal_authenticate (void)
} }
#if defined(CERTDO_SUPPORT)
static void static void
cmd_update_binary (void) cmd_update_binary (void)
{ {
@@ -718,10 +718,10 @@ cmd_update_binary (void)
} }
if ((P1 (apdu) & 0x80)) if ((P1 (apdu) & 0x80))
if ((P1 (apdu) & 0x7f) <= FILEID_RANDOM) if ((P1 (apdu) & 0x7f) == FILEID_CH_CERTIFICATE)
{ {
file_selection = FILE_EF_CH_CERTIFICATE + (P1 (apdu) & 0x7f); file_selection = FILE_EF_CH_CERTIFICATE;
r = flash_erase_binary (file_selection - FILE_EF_CH_CERTIFICATE); r = flash_erase_binary (FILEID_CH_CERTIFICATE);
if (r < 0) if (r < 0)
{ {
DEBUG_INFO ("memory error.\r\n"); DEBUG_INFO ("memory error.\r\n");
@@ -738,8 +738,7 @@ cmd_update_binary (void)
} }
else else
{ {
if (file_selection != FILE_EF_CH_CERTIFICATE if (file_selection != FILE_EF_CH_CERTIFICATE)
&& file_selection != FILE_EF_RANDOM)
{ {
GPG_COMMAND_NOT_ALLOWED (); GPG_COMMAND_NOT_ALLOWED ();
return; return;
@@ -751,7 +750,7 @@ cmd_update_binary (void)
DEBUG_SHORT (len); DEBUG_SHORT (len);
DEBUG_SHORT (offset); DEBUG_SHORT (offset);
r = flash_write_binary (file_selection - FILE_EF_CH_CERTIFICATE, r = flash_write_binary (FILEID_CH_CERTIFICATE,
apdu.cmd_apdu_data, len, offset); apdu.cmd_apdu_data, len, offset);
if (r < 0) if (r < 0)
{ {
@@ -763,6 +762,7 @@ cmd_update_binary (void)
GPG_SUCCESS (); GPG_SUCCESS ();
DEBUG_INFO ("UPDATE BINARY done.\r\n"); DEBUG_INFO ("UPDATE BINARY done.\r\n");
} }
#endif
static void static void
@@ -782,9 +782,9 @@ cmd_write_binary (void)
} }
if ((P1 (apdu) & 0x80)) if ((P1 (apdu) & 0x80))
if ((P1 (apdu) & 0x7f) <= FILEID_SERIAL_NO) if ((P1 (apdu) & 0x7f) <= FILEID_CH_CERTIFICATE)
{ {
file_selection = FILE_EF_CH_CERTIFICATE + (P1 (apdu) & 0x7f); file_selection = FILE_EF_SERIAL + (P1 (apdu) & 0x7f);
offset = 0; offset = 0;
} }
else else
@@ -794,9 +794,8 @@ cmd_write_binary (void)
} }
else else
{ {
if (file_selection != FILE_EF_CH_CERTIFICATE if (file_selection != FILE_EF_SERIAL
&& file_selection != FILE_EF_RANDOM && file_selection != FILE_EF_CH_CERTIFICATE)
&& file_selection != FILE_EF_SERIAL)
{ {
GPG_COMMAND_NOT_ALLOWED (); GPG_COMMAND_NOT_ALLOWED ();
return; return;
@@ -808,7 +807,7 @@ cmd_write_binary (void)
DEBUG_SHORT (len); DEBUG_SHORT (len);
DEBUG_SHORT (offset); DEBUG_SHORT (offset);
r = flash_write_binary (file_selection - FILE_EF_CH_CERTIFICATE, r = flash_write_binary (file_selection - FILE_EF_SERIAL,
apdu.cmd_apdu_data, len, offset); apdu.cmd_apdu_data, len, offset);
if (r < 0) if (r < 0)
{ {
@@ -839,7 +838,9 @@ const struct command cmds[] = {
{ INS_READ_BINARY, cmd_read_binary }, { INS_READ_BINARY, cmd_read_binary },
{ INS_GET_DATA, cmd_get_data }, { INS_GET_DATA, cmd_get_data },
{ INS_WRITE_BINARY, cmd_write_binary}, /* Not in OpenPGP card protocol */ { INS_WRITE_BINARY, cmd_write_binary}, /* Not in OpenPGP card protocol */
#if defined(CERTDO_SUPPORT)
{ INS_UPDATE_BINARY, cmd_update_binary }, /* Not in OpenPGP card protocol */ { INS_UPDATE_BINARY, cmd_update_binary }, /* Not in OpenPGP card protocol */
#endif
{ INS_PUT_DATA, cmd_put_data }, { INS_PUT_DATA, cmd_put_data },
{ INS_PUT_DATA_ODD, cmd_put_data }, { INS_PUT_DATA_ODD, cmd_put_data },
}; };

View File

@@ -125,16 +125,16 @@ def main(fileid, is_update, data, passwd):
gnuk.cmd_verify(BY_ADMIN, passwd) gnuk.cmd_verify(BY_ADMIN, passwd)
gnuk.cmd_write_binary(fileid, data, is_update) gnuk.cmd_write_binary(fileid, data, is_update)
if fileid == 0: if fileid == 0:
gnuk.cmd_select_openpgp()
data_in_device = gnuk.cmd_get_data(0x7f, 0x21)
compare(data[:-2], data_in_device)
elif fileid == 2:
gnuk.cmd_select_openpgp() gnuk.cmd_select_openpgp()
data_in_device = gnuk.cmd_get_data(0x00, 0x4f) data_in_device = gnuk.cmd_get_data(0x00, 0x4f)
for d in data_in_device: for d in data_in_device:
print "%02x" % d, print "%02x" % d,
print print
compare(data, data_in_device[8:]) compare(data, data_in_device[8:])
elif fileid == 1:
gnuk.cmd_select_openpgp()
data_in_device = gnuk.cmd_get_data(0x7f, 0x21)
compare(data[:-2], data_in_device)
gnuk.connection.disconnect() gnuk.connection.disconnect()
return 0 return 0
@@ -152,7 +152,7 @@ if __name__ == '__main__':
else: else:
is_update = False is_update = False
if sys.argv[1] == '-s': if sys.argv[1] == '-s':
fileid = 2 # serial number fileid = 0 # serial number
filename = sys.argv[2] filename = sys.argv[2]
f = open(filename) f = open(filename)
email = os.environ['EMAIL'] email = os.environ['EMAIL']
@@ -168,7 +168,7 @@ if __name__ == '__main__':
print "Writing serial number" print "Writing serial number"
data = binascii.unhexlify(serial_data_hex) data = binascii.unhexlify(serial_data_hex)
else: else:
fileid = 0 # Card holder certificate fileid = 1 # Card holder certificate
filename = sys.argv[1] filename = sys.argv[1]
f = open(filename) f = open(filename)
data = f.read() data = f.read()

View File

@@ -241,7 +241,7 @@ def main(fileid, is_update, data):
else: else:
icc.cmd_write_binary(fileid, data) icc.cmd_write_binary(fileid, data)
icc.cmd_select_openpgp() icc.cmd_select_openpgp()
if fileid == 0: if fileid == 1:
data = data[:-2] data = data[:-2]
data_in_device = icc.cmd_get_data(0x7f, 0x21) data_in_device = icc.cmd_get_data(0x7f, 0x21)
compare(data, data_in_device) compare(data, data_in_device)
@@ -255,7 +255,7 @@ if __name__ == '__main__':
else: else:
is_update = False is_update = False
if sys.argv[1] == '-s': if sys.argv[1] == '-s':
fileid = 2 # serial number fileid = 0 # serial number
filename = sys.argv[2] filename = sys.argv[2]
f = open(filename) f = open(filename)
email = os.environ['EMAIL'] email = os.environ['EMAIL']
@@ -270,20 +270,8 @@ if __name__ == '__main__':
exit(1) exit(1)
print "Writing serial number" print "Writing serial number"
data = binascii.unhexlify(serial_data_hex) data = binascii.unhexlify(serial_data_hex)
elif sys.argv[1] == '-r':
fileid = 1 # Random number bits
if len(sys.argv) == 3:
filename = sys.argv[2]
f = open(filename)
else:
filename = stdin
f = sys.stdin
data = f.read()
f.close()
print "%s: %d" % (filename, len(data))
print "Updating random bits"
else: else:
fileid = 0 # Card holder certificate fileid = 1 # Card holder certificate
filename = sys.argv[1] filename = sys.argv[1]
f = open(filename) f = open(filename)
data = f.read() data = f.read()