From: Klas Lindfors Date: Mon, 3 Dec 2012 13:42:30 +0000 (+0100) Subject: refactor different write commands into a common _yk_write() X-Git-Tag: v1.10.0~19 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8fe786d1ee81e1ad6222db76e1a7826258503fff;p=yubikey-personalization refactor different write commands into a common _yk_write() --- diff --git a/ykcore/ykcore.c b/ykcore/ykcore.c index 1c95fe7..5d8255f 100644 --- a/ykcore/ykcore.c +++ b/ykcore/ykcore.c @@ -176,10 +176,8 @@ int yk_get_serial(YK_KEY *yk, uint8_t slot, unsigned int flags, unsigned int *se return 1; } -int yk_write_command(YK_KEY *yk, YK_CONFIG *cfg, uint8_t command, - unsigned char *acc_code) +static int _yk_write(YK_KEY *yk, uint8_t yk_cmd, unsigned char *buf, size_t len) { - unsigned char buf[sizeof(YK_CONFIG) + ACC_CODE_SIZE]; YK_STATUS stat; int seq; @@ -190,24 +188,8 @@ int yk_write_command(YK_KEY *yk, YK_CONFIG *cfg, uint8_t command, seq = stat.pgmSeq; - /* Update checksum and insert config block in buffer if present */ - - memset(buf, 0, sizeof(buf)); - - if (cfg) { - cfg->crc = ~yubikey_crc16 ((unsigned char *) cfg, - sizeof(YK_CONFIG) - sizeof(cfg->crc)); - cfg->crc = yk_endian_swap_16(cfg->crc); - memcpy(buf, cfg, sizeof(YK_CONFIG)); - } - - /* Append current access code if present */ - - if (acc_code) - memcpy(buf + sizeof(YK_CONFIG), acc_code, ACC_CODE_SIZE); - /* Write to Yubikey */ - if (!yk_write_to_key(yk, command, buf, sizeof(buf))) + if (!yk_write_to_key(yk, yk_cmd, buf, len)) return 0; /* When the Yubikey clears the SLOT_WRITE_FLAG, it has processed the last write. @@ -215,7 +197,7 @@ int yk_write_command(YK_KEY *yk, YK_CONFIG *cfg, uint8_t command, * want to get the bytes in the status message, but when writing configuration * we don't expect any data back. */ - yk_wait_for_key_status(yk, command, 0, WAIT_FOR_WRITE_FLAG, false, SLOT_WRITE_FLAG, NULL); + yk_wait_for_key_status(yk, yk_cmd, 0, WAIT_FOR_WRITE_FLAG, false, SLOT_WRITE_FLAG, NULL); /* Verify update */ @@ -227,12 +209,37 @@ int yk_write_command(YK_KEY *yk, YK_CONFIG *cfg, uint8_t command, /* when both configurations from a YubiKey is erased it will return * pgmSeq 0, if one is still configured after an erase pgmSeq is * counted up as usual. */ - if(!cfg && stat.pgmSeq == 0) { + if(stat.pgmSeq == 0) { return 1; } return stat.pgmSeq != seq; } +int yk_write_command(YK_KEY *yk, YK_CONFIG *cfg, uint8_t command, + unsigned char *acc_code) +{ + unsigned char buf[sizeof(YK_CONFIG) + ACC_CODE_SIZE]; + + /* Update checksum and insert config block in buffer if present */ + + memset(buf, 0, sizeof(buf)); + + if (cfg) { + cfg->crc = ~yubikey_crc16 ((unsigned char *) cfg, + sizeof(YK_CONFIG) - sizeof(cfg->crc)); + cfg->crc = yk_endian_swap_16(cfg->crc); + memcpy(buf, cfg, sizeof(YK_CONFIG)); + } + + /* Append current access code if present */ + + if (acc_code) + memcpy(buf + sizeof(YK_CONFIG), acc_code, ACC_CODE_SIZE); + + return _yk_write(yk, command, buf, sizeof(buf)); + +} + int yk_write_config(YK_KEY *yk, YK_CONFIG *cfg, int confnum, unsigned char *acc_code) { @@ -263,17 +270,8 @@ int yk_write_ndef(YK_KEY *yk, YK_NDEF *ndef) int yk_write_ndef2(YK_KEY *yk, YK_NDEF *ndef, int confnum) { unsigned char buf[sizeof(YK_NDEF)]; - YK_STATUS stat; - int seq; uint8_t command; - /* Get current sequence # from status block */ - - if (!yk_get_status(yk, &stat)) - return 0; - - seq = stat.pgmSeq; - switch(confnum) { case 1: command = SLOT_NDEF; @@ -291,61 +289,17 @@ int yk_write_ndef2(YK_KEY *yk, YK_NDEF *ndef, int confnum) memset(buf, 0, sizeof(buf)); memcpy(buf, ndef, sizeof(YK_NDEF)); - /* Write to Yubikey */ - if (!yk_write_to_key(yk, command, buf, sizeof(buf))) - return 0; - - /* When the Yubikey clears the SLOT_WRITE_FLAG, it has processed the last write. - * This wait can't be done in yk_write_to_key since some users of that function - * want to get the bytes in the status message, but when writing configuration - * we don't expect any data back. - */ - yk_wait_for_key_status(yk, SLOT_NDEF, 0, WAIT_FOR_WRITE_FLAG, false, SLOT_WRITE_FLAG, NULL); - - /* Verify update */ - - if (!yk_get_status(yk, &stat /*, 0*/)) - return 0; - - yk_errno = YK_EWRITEERR; - return stat.pgmSeq != seq; + return _yk_write(yk, command, buf, sizeof(YK_NDEF)); } int yk_write_device_config(YK_KEY *yk, YK_DEVICE_CONFIG *device_config) { unsigned char buf[sizeof(YK_DEVICE_CONFIG)]; - YK_STATUS stat; - int seq; memset(buf, 0, sizeof(buf)); memcpy(buf, device_config, sizeof(YK_DEVICE_CONFIG)); - /* Get current sequence # from status block */ - if (!yk_get_status(yk, &stat)) - return 0; - - seq = stat.pgmSeq; - - /* Write to Yubikey */ - if(!yk_write_to_key(yk, SLOT_DEVICE_CONFIG, buf, sizeof(buf))) - return 0; - - /* When the Yubikey clears the SLOT_WRITE_FLAG, it has processed the last write. - * This wait can't be done in yk_write_to_key since some users of that function - * want to get the bytes in the status message, but when writing configuration - * we don't expect any data back. - */ - yk_wait_for_key_status(yk, SLOT_DEVICE_CONFIG, 0, WAIT_FOR_WRITE_FLAG, false, SLOT_WRITE_FLAG, NULL); - - /* Verify update */ - - if (!yk_get_status(yk, &stat /*, 0*/)) - return 0; - - yk_errno = YK_EWRITEERR; - return stat.pgmSeq != seq; - - return 1; + return _yk_write(yk, SLOT_DEVICE_CONFIG, buf, sizeof(YK_DEVICE_CONFIG)); } /*