]> err.no Git - yubikey-personalization/commitdiff
add -S flag for setting scanmap to the YubiKey NEO
authorKlas Lindfors <klas@yubico.com>
Tue, 4 Dec 2012 14:21:07 +0000 (15:21 +0100)
committerKlas Lindfors <klas@yubico.com>
Tue, 4 Dec 2012 14:21:07 +0000 (15:21 +0100)
libykpers-1.map
tests/test_args_to_config.c
ykcore/ykcore.c
ykcore/ykcore.h
ykpers-args.c
ykpers-args.h
ykpers.c
ykpersonalize.c

index 627d722c1b6725cd2c6189a3ffc7385b01b19d77..9ee32eca1dcb69a0d8deb49e9208fde9d04a65c3 100644 (file)
@@ -174,5 +174,6 @@ LIBYKPERS_1.10 {
   ykp_set_device_mode;
   ykp_set_device_chalresp_timeout;
   ykp_set_device_autoeject_time;
+  yk_write_scan_map;
 # Variables:
 } LIBYKPERS_1.9;
index 02ba388c79b45040173e61931cf2a28e8da726d1..fe0a9dfa7307b0632ace8d2d5fb63d2ee9cd6c63 100644 (file)
@@ -118,6 +118,8 @@ int _test_config (YKP_CONFIG *cfg, YK_STATUS *st, int argc, char **argv)
        unsigned char usb_mode = -1;
        bool zap = false;
 
+       unsigned char scan_map[strlen(SCAN_MAP)];
+
        int rc;
 
        ykp_errno = 0;
@@ -133,7 +135,7 @@ int _test_config (YKP_CONFIG *cfg, YK_STATUS *st, int argc, char **argv)
                            st, &verbose,
                            access_code, &use_access_code,
                            &aesviahash, &ndef_type, ndef, &usb_mode, &zap,
-                           &exit_code);
+                           scan_map, &exit_code);
 
        return rc;
 }
@@ -297,6 +299,8 @@ int _test_non_config_args(void)
        unsigned char usb_mode = -1;
        bool zap = false;
 
+       unsigned char scan_map[strlen(SCAN_MAP)];
+
        char *argv[] = {
                "unittest", "-1", "-sout", "-iin", "-c313233343536", "-y", "-v",
                NULL
@@ -317,7 +321,7 @@ int _test_non_config_args(void)
                            st, &verbose,
                            access_code, &use_access_code,
                            &aesviahash, &ndef_type, ndef, &usb_mode, &zap,
-                           &exit_code);
+                           scan_map, &exit_code);
        assert(rc == 1);
        i = strcmp(infname, "in"); assert(i == 0);
        i = strcmp(outfname, "out"); assert(i == 0);
index 5d8255f06bdc561fcee9a6ee3df824bb51ed4dd3..2ad4a72eb7deafe73cd47f5e01327e74f0a137d5 100644 (file)
@@ -302,6 +302,11 @@ int yk_write_device_config(YK_KEY *yk, YK_DEVICE_CONFIG *device_config)
        return _yk_write(yk, SLOT_DEVICE_CONFIG, buf, sizeof(YK_DEVICE_CONFIG));
 }
 
+int yk_write_scan_map(YK_KEY *yk, unsigned char *scan_map)
+{
+       return _yk_write(yk, SLOT_SCAN_MAP, scan_map, strlen(SCAN_MAP));
+}
+
 /*
  * This function is for doing HMAC-SHA1 or Yubico challenge-response with a key.
  */
index ea2fccb50c2a146325a918c3e54390c2b0745f4e..40c6f02723df27d5fa18b21713dcd57478e74584 100644 (file)
@@ -123,6 +123,8 @@ extern int yk_write_ndef(YK_KEY *yk, YK_NDEF *ndef);
 extern int yk_write_ndef2(YK_KEY *yk, YK_NDEF *ndef, int confnum);
 /* writes a device config block to the key. */
 extern int yk_write_device_config(YK_KEY *yk, YK_DEVICE_CONFIG *device_config);
+/* writes a scanmap to the key. */
+extern int yk_write_scan_map(YK_KEY *yk, unsigned char *scan_map);
 /* Write something to the YubiKey (a command that is). */
 extern int yk_write_to_key(YK_KEY *yk, uint8_t slot, const void *buf, int bufcount);
 /* Do a challenge-response round with the key. */
index 852cf8de2ca599c33cc3e92f8dcb124ebaf8558c..70e9f3ee39be1ba312f88e15134e101f430a3361 100644 (file)
@@ -77,6 +77,8 @@ const char *usage =
 "          1                   CCID device only.\n"
 "          2                   HID/CCID composite device.\n"
 "          Add 80 to set MODE_FLAG_EJECT, for example: 81\n"
+"-S0605..  Set the scanmap to use with the YubiKey NEO. Must be 44 unique bytes,\n"
+"          in hex.  Use with no argument to reset to the default.\n"
 "-oOPTION  change configuration option.  Possible OPTION arguments are:\n"
 "          salt=ssssssss       Salt to be used when deriving key from a\n"
 "                              password.  If none is given, a unique random\n"
@@ -151,7 +153,7 @@ const char *usage =
 "-v        verbose\n"
 "-h        help (this text)\n"
 ;
-const char *optstring = "u12xza:c:n:t:hi:o:s:vym:";
+const char *optstring = "u12xza:c:n:t:hi:o:s:vym:S::";
 
 static int _set_fixed(char *opt, YKP_CONFIG *cfg);
 static int _format_decimal_as_hex(uint8_t *dst, size_t dst_len, uint8_t *src);
@@ -225,8 +227,9 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
                   bool *autocommit, char *salt,
                   YK_STATUS *st, bool *verbose,
                   unsigned char *access_code, bool *use_access_code,
-                  bool *aesviahash, char *ndef_type, char *ndef, unsigned char *usb_mode, bool *zap,
-                  int *exit_code)
+                  bool *aesviahash, char *ndef_type, char *ndef,
+                  unsigned char *usb_mode, bool *zap,
+                  unsigned char *scan_bin, int *exit_code)
 {
        int c;
        const char *aeshash = NULL;
@@ -238,6 +241,7 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
        bool update_seen = false;
        bool ndef_seen = false;
        bool usb_mode_seen = false;
+       bool scan_map_seen = false;
        struct config_st *ycfg;
 
        ykp_configure_version(cfg, st);
@@ -463,6 +467,32 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
                                return 0;
 
                        break;
+               case 'S':
+                       {
+                               size_t scanlength = strlen(SCAN_MAP);
+                               if(optarg) {
+                                       size_t scanbinlen;
+                                       size_t scanlen = strlen (optarg);
+                                       int rc = hex_modhex_decode(scan_bin, &scanbinlen,
+                                                       optarg, scanlen,
+                                                       scanlength * 2, scanlength * 2,
+                                                       false);
+
+                                       if (rc <= 0) {
+                                               fprintf(stderr,
+                                                               "Invalid scanmap string %s\n",
+                                                               optarg);
+                                               *exit_code = 1;
+                                               return 0;
+                                       }
+                               } else {
+                                       memset(scan_bin, 0, scanlength);
+                               }
+                               scan_map_seen = true;
+                       }
+                       if (!ykp_configure_command(cfg, SLOT_SCAN_MAP))
+                               return 0;
+                       break;
                case 'o':
                        if (*zap) {
                                fprintf(stderr, "No options can be given with zap (-z).\n");
@@ -652,7 +682,7 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
                }
        }
 
-       if (!slot_chosen && !ndef_seen && !usb_mode_seen) {
+       if (!slot_chosen && !ndef_seen && !usb_mode_seen && !scan_map_seen) {
                fprintf(stderr, "A slot must be chosen with -1 or -2.\n");
                *exit_code = 1;
                return 0;
index 3f563b88826b194abb75d65f02ba519231104000..8ee82eb5fe5f0e20d249adbf4f0341860b00a3ad 100644 (file)
@@ -39,7 +39,7 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, YK_KEY *yk,
                   YK_STATUS *st, bool *verbose,
                   unsigned char *access_code, bool *use_access_code,
                   bool *aesviahash, char *ndef_type, char *ndef, unsigned char *usb_mode, 
-                  bool *zap, int *exit_code);
+                  bool *zap, unsigned char *scan_bin, int *exit_code);
 
 void report_yk_error(void);
 
index f800d485fe83d425d6b078fe729848291ceedfbb..00bb048d83ade95b48a7589ca0a6fa1d9f699068 100644 (file)
--- a/ykpers.c
+++ b/ykpers.c
@@ -184,19 +184,14 @@ int ykp_configure_command(YKP_CONFIG *cfg, uint8_t command)
                        return 0;
                }
                break;
-       /* TODO: Make sure this is correct */
        case SLOT_DEVICE_CONFIG:
-               /* For testing only */
+       case SLOT_SCAN_MAP:
+       case SLOT_NDEF2:
                if(!(cfg->yk_major_version >= 3)) {
                        ykp_errno = YKP_EYUBIKEYVER;
                        return 0;
                }
                break;
-       case SLOT_NDEF2:
-               if (!(cfg->yk_major_version == 3)) {
-                       ykp_errno = YKP_EYUBIKEYVER;
-                       return 0;
-               }
        case SLOT_NDEF:
                /* NDEF is available for neo, thus within 2.1 from build 4 */
                if (!((cfg->yk_major_version == 2 && cfg->yk_minor_version == 1 &&
index 6c4e1716dfa1a5afe95ecbc8d442acce2e9bb2ea..a130b67c9c90e083b0992be4ffc69a6b757730a1 100644 (file)
@@ -58,6 +58,7 @@ int main(int argc, char **argv)
        bool aesviahash = false;
        bool use_access_code = false;
        unsigned char access_code[256];
+       unsigned char scan_codes[strlen(SCAN_MAP)];
        YK_KEY *yk = 0;
        YKP_CONFIG *cfg = ykp_alloc();
        YK_STATUS *st = ykds_alloc();
@@ -128,8 +129,8 @@ int main(int argc, char **argv)
                             &autocommit, salt,
                             st, &verbose,
                             access_code, &use_access_code,
-                            &aesviahash, &ndef_type, ndef_string, 
-                                &usb_mode, &zap, &exit_code)) {
+                            &aesviahash, &ndef_type, ndef_string,
+                            &usb_mode, &zap, scan_codes, &exit_code)) {
                goto err;
        }
 
@@ -207,6 +208,8 @@ int main(int argc, char **argv)
                        fprintf(stderr, "New NDEF will be written as:\n%s\n", ndef_string);
                } else if(ykp_command(cfg) == SLOT_DEVICE_CONFIG) {
                        fprintf(stderr, "The USB mode will be set to: 0x%x\n", usb_mode);
+               } else if(ykp_command(cfg) == SLOT_SCAN_MAP) {
+                       fprintf(stderr, "A new scanmap will be written.\n");
                } else if(zap) {
                        fprintf(stderr, "Configuration in slot %d will be deleted\n", ykp_config_num(cfg));
                } else {
@@ -262,6 +265,14 @@ int main(int argc, char **argv)
                                        goto err;
                                }
                                ykp_free_device_config(device_config);
+
+
+                       } else if(ykp_command(cfg) == SLOT_SCAN_MAP) {
+                               if(!yk_write_scan_map(yk, scan_codes)) {
+                                       if(verbose)
+                                               printf(" failure\n");
+                                       goto err;
+                               }
                        } else {
                                YK_CONFIG *ycfg = NULL;
                                /* if we're deleting a slot we send the configuration as NULL */