parse authentication key

This commit is contained in:
Klas Lindfors
2014-02-03 13:40:41 +01:00
parent 26f171873c
commit 8069baa283
2 changed files with 35 additions and 10 deletions
+1
View File
@@ -26,3 +26,4 @@
option "verbose" v "Print more information" flag off
option "reader" r "Only use a matching reader" string optional
option "key" k "Authentication key to use" string optional default="010203040506070801020304050607080102030405060708"
+31 -7
View File
@@ -40,18 +40,13 @@
#include "cmdline.h"
unsigned const char default_key[] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
};
#define KEY_LEN 24
unsigned const char aid[] = {
0xa0, 0x00, 0x00, 0x03, 0x08
};
#define AID_LEN 5
#define KEY_LEN 24
union u_APDU {
struct {
unsigned char cla;
@@ -193,15 +188,44 @@ void dump_hex(const unsigned char *buf, unsigned int len) {
printf("\n");
}
static bool parse_key(char *key_arg, unsigned char *key, int verbose) {
int i;
char key_part[2];
int key_len = strlen(key_arg);
if(key_len != KEY_LEN * 2) {
fprintf(stderr, "Wrong key size, should be %d characters (was %d).\n", KEY_LEN * 2, key_len);
return false;
}
for(i = 0; i < KEY_LEN; i++) {
key_part[0] = *key_arg++;
key_part[1] = *key_arg++;
if(sscanf(key_part, "%hhx", &key[i]) != 1) {
fprintf(stderr, "Failed parsing key at position %d.\n", i);
return false;
}
}
if(verbose) {
printf("parsed key:\n");
dump_hex(key, KEY_LEN);
}
return true;
}
int main(int argc, char *argv[]) {
struct gengetopt_args_info args_info;
SCARDHANDLE card;
SCARDCONTEXT context;
unsigned char key[KEY_LEN];
if(cmdline_parser(argc, argv, &args_info) != 0) {
return EXIT_FAILURE;
}
if(parse_key(args_info.key_arg, key, args_info.verbose_flag) == false) {
return EXIT_FAILURE;
}
if(connect_reader(&card, &context, args_info.reader_arg, args_info.verbose_flag) == false) {
return EXIT_FAILURE;
}