|
| 1 | +#if defined(HAVE_CONFIG_H) |
| 2 | + #include "config.h" |
| 3 | +#endif |
| 4 | + |
| 5 | +#include <err.h> |
| 6 | +#include <errno.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <unistd.h> |
| 10 | +#include <stdbool.h> |
| 11 | + |
| 12 | +#include <nfc/nfc.h> |
| 13 | + |
| 14 | +#include <freefare.h> |
| 15 | + |
| 16 | +uint8_t null_key_data[8]; |
| 17 | + |
| 18 | +uint8_t new_key_version = 0x00; |
| 19 | + |
| 20 | +MifareDESFireKey old_picc_key; |
| 21 | +MifareDESFireKey new_picc_key; |
| 22 | + |
| 23 | +#define NEW_KEY_VERSION new_key_version |
| 24 | + |
| 25 | +struct { |
| 26 | + bool interactive; |
| 27 | +} configure_options = { |
| 28 | + .interactive = true |
| 29 | +}; |
| 30 | + |
| 31 | +static void |
| 32 | +usage(char *progname) |
| 33 | +{ |
| 34 | + fprintf(stderr, "usage: %s [-y]\n", progname); |
| 35 | + fprintf(stderr, "\nOptions:\n"); |
| 36 | + fprintf(stderr, " -y Do not ask for confirmation\n"); |
| 37 | + fprintf(stderr, " -k Existing PICC key (Default is all zeros)\n"); |
| 38 | + fprintf(stderr, " -n New PICC key (Default is all zeros)\n"); |
| 39 | + fprintf(stderr, " -v New PICC key version (default is zero)\n"); |
| 40 | +} |
| 41 | + |
| 42 | +#define strnequal(x, y, n) (strncmp(x, y, n) == 0) |
| 43 | + |
| 44 | +static inline bool |
| 45 | +strhasprefix(const char* str, const char* prefix) |
| 46 | +{ |
| 47 | + return strnequal(str, prefix, strlen(prefix)); |
| 48 | +} |
| 49 | + |
| 50 | +MifareDESFireKey read_hex_desfire_key(const char* optarg) |
| 51 | +{ |
| 52 | + uint8_t buffer[24]; |
| 53 | + int i; |
| 54 | + uint64_t n; |
| 55 | + |
| 56 | + bool is_des = true; |
| 57 | + |
| 58 | + if (strhasprefix(optarg, "DES:")) { |
| 59 | + is_des = true; |
| 60 | + optarg += 4; |
| 61 | + } else if (strhasprefix(optarg, "AES:")) { |
| 62 | + is_des = false; |
| 63 | + optarg += 4; |
| 64 | + } |
| 65 | + |
| 66 | + size_t len = strlen(optarg); |
| 67 | + size_t div16 = len / 16; |
| 68 | + |
| 69 | + if (div16 < 1 || div16 > 3 || (len % 16) != 0) { |
| 70 | + fprintf(stderr,"Bad key length\n"); |
| 71 | + exit(EXIT_FAILURE); |
| 72 | + } |
| 73 | + |
| 74 | + if (div16 >= 1) { |
| 75 | + n = strtoull(optarg, NULL, 16); |
| 76 | + for (i = 7; i >= 0; i--) { |
| 77 | + buffer[i] = (uint8_t) n; |
| 78 | + n >>= 8; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if (div16 >= 2) { |
| 83 | + n = strtoull(optarg+8, NULL, 16); |
| 84 | + for (i = 7; i >= 0; i--) { |
| 85 | + buffer[i+8] = (uint8_t) n; |
| 86 | + n >>= 8; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + if (div16 == 3) { |
| 91 | + n = strtoull(optarg+16, NULL, 16); |
| 92 | + for (i = 7; i >= 0; i--) { |
| 93 | + buffer[i+16] = (uint8_t) n; |
| 94 | + n >>= 8; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (is_des && div16 == 1) { |
| 99 | + return mifare_desfire_des_key_new(buffer); |
| 100 | + } |
| 101 | + if (is_des && div16 == 2) { |
| 102 | + return mifare_desfire_3des_key_new(buffer); |
| 103 | + } |
| 104 | + if (is_des && div16 == 3) { |
| 105 | + return mifare_desfire_3k3des_key_new(buffer); |
| 106 | + } |
| 107 | + if (!is_des && div16 == 2) { |
| 108 | + return mifare_desfire_aes_key_new(buffer); |
| 109 | + } |
| 110 | + fprintf(stderr,"Bad key length\n"); |
| 111 | + exit(EXIT_FAILURE); |
| 112 | +} |
| 113 | + |
| 114 | +int |
| 115 | +main(int argc, char *argv[]) |
| 116 | +{ |
| 117 | + int ch; |
| 118 | + int error = EXIT_SUCCESS; |
| 119 | + nfc_device *device = NULL; |
| 120 | + FreefareTag *tags = NULL; |
| 121 | + bool should_diversify_new = false; |
| 122 | + |
| 123 | + old_picc_key = mifare_desfire_des_key_new(null_key_data); |
| 124 | + new_picc_key = mifare_desfire_des_key_new(null_key_data); |
| 125 | + |
| 126 | + while ((ch = getopt(argc, argv, "hyk:n:v:D")) != -1) { |
| 127 | + switch (ch) { |
| 128 | + case 'h': |
| 129 | + usage(argv[0]); |
| 130 | + exit(EXIT_SUCCESS); |
| 131 | + break; |
| 132 | + case 'y': |
| 133 | + configure_options.interactive = false; |
| 134 | + break; |
| 135 | + case 'k': |
| 136 | + mifare_desfire_key_free(old_picc_key); |
| 137 | + old_picc_key = read_hex_desfire_key(optarg); |
| 138 | + break; |
| 139 | + case 'n': |
| 140 | + mifare_desfire_key_free(new_picc_key); |
| 141 | + new_picc_key = read_hex_desfire_key(optarg); |
| 142 | + break; |
| 143 | + case 'v': |
| 144 | + errno = 0; |
| 145 | + new_key_version = (uint8_t)strtol(optarg, NULL, 0); |
| 146 | + if (errno != 0) { |
| 147 | + perror("strtol"); |
| 148 | + exit(EXIT_FAILURE); |
| 149 | + } |
| 150 | + break; |
| 151 | + case 'D': |
| 152 | + should_diversify_new = true; |
| 153 | + break; |
| 154 | + default: |
| 155 | + usage(argv[0]); |
| 156 | + exit(EXIT_FAILURE); |
| 157 | + } |
| 158 | + } |
| 159 | + // Remaining args, if any, are in argv[optind .. (argc-1)] |
| 160 | + |
| 161 | + mifare_desfire_key_set_version(new_picc_key, new_key_version); |
| 162 | + |
| 163 | + MifareKeyDeriver new_deriver = NULL; |
| 164 | + if (should_diversify_new) { |
| 165 | + new_deriver = mifare_key_deriver_new_an10922(new_picc_key, mifare_desfire_key_get_type(new_picc_key), AN10922_FLAG_DEFAULT); |
| 166 | + } |
| 167 | + |
| 168 | + nfc_connstring devices[8]; |
| 169 | + size_t device_count; |
| 170 | + |
| 171 | + nfc_context *context; |
| 172 | + nfc_init(&context); |
| 173 | + if (context == NULL) |
| 174 | + errx(EXIT_FAILURE, "Unable to init libnfc (malloc)"); |
| 175 | + |
| 176 | + device_count = nfc_list_devices(context, devices, 8); |
| 177 | + if (device_count <= 0) |
| 178 | + errx(EXIT_FAILURE, "No NFC device found."); |
| 179 | + |
| 180 | + for (size_t d = 0; (!error) && (d < device_count); d++) { |
| 181 | + device = nfc_open(context, devices[d]); |
| 182 | + if (!device) { |
| 183 | + warnx("nfc_open() failed."); |
| 184 | + error = EXIT_FAILURE; |
| 185 | + continue; |
| 186 | + } |
| 187 | + |
| 188 | + tags = freefare_get_tags(device); |
| 189 | + if (!tags) { |
| 190 | + nfc_close(device); |
| 191 | + errx(EXIT_FAILURE, "Error listing Mifare DESFire tags."); |
| 192 | + } |
| 193 | + |
| 194 | + for (int i = 0; (!error) && tags[i]; i++) { |
| 195 | + if (MIFARE_DESFIRE != freefare_get_tag_type(tags[i])) |
| 196 | + continue; |
| 197 | + |
| 198 | + char *tag_uid = freefare_get_tag_uid(tags[i]); |
| 199 | + char buffer[BUFSIZ]; |
| 200 | + |
| 201 | + int res; |
| 202 | + |
| 203 | + res = mifare_desfire_connect(tags[i]); |
| 204 | + if (res < 0) { |
| 205 | + warnx("Can't connect to Mifare DESFire target."); |
| 206 | + error = EXIT_FAILURE; |
| 207 | + break; |
| 208 | + } |
| 209 | + |
| 210 | + printf("Found %s with UID %s. ", freefare_get_tag_friendly_name(tags[i]), tag_uid); |
| 211 | + bool do_it = true; |
| 212 | + |
| 213 | + if (configure_options.interactive) { |
| 214 | + printf("Change PICC key? [yN] "); |
| 215 | + fgets(buffer, BUFSIZ, stdin); |
| 216 | + do_it = ((buffer[0] == 'y') || (buffer[0] == 'Y')); |
| 217 | + } else { |
| 218 | + printf("\n"); |
| 219 | + } |
| 220 | + |
| 221 | + if (do_it) { |
| 222 | + |
| 223 | + res = mifare_desfire_authenticate(tags[i], 0, old_picc_key); |
| 224 | + if (res < 0) { |
| 225 | + freefare_perror(tags[i], "mifare_desfire_authenticate"); |
| 226 | + error = EXIT_FAILURE; |
| 227 | + break; |
| 228 | + } |
| 229 | + |
| 230 | + res = mifare_desfire_change_key(tags[i], 0, new_picc_key, old_picc_key); |
| 231 | + if (res < 0) { |
| 232 | + freefare_perror(tags[i], "mifare_desfire_change_key"); |
| 233 | + error = EXIT_FAILURE; |
| 234 | + break; |
| 235 | + } |
| 236 | + |
| 237 | + } |
| 238 | + |
| 239 | + mifare_desfire_disconnect(tags[i]); |
| 240 | + free(tag_uid); |
| 241 | + } |
| 242 | + |
| 243 | + freefare_free_tags(tags); |
| 244 | + nfc_close(device); |
| 245 | + } |
| 246 | + |
| 247 | + mifare_desfire_key_free(old_picc_key); |
| 248 | + mifare_desfire_key_free(new_picc_key); |
| 249 | + |
| 250 | + nfc_exit(context); |
| 251 | + exit(error); |
| 252 | +} |
0 commit comments