add ykpiv_parse_key()

This commit is contained in:
Klas Lindfors
2014-06-17 10:07:49 +02:00
parent 01c844905a
commit d1b2062721
5 changed files with 40 additions and 30 deletions
+1
View File
@@ -49,6 +49,7 @@ static const err_t errors[] = {
ERR (YKPIV_AUTHENTICATION_ERROR, "Error during authentication"),
ERR (YKPIV_RANDOMNESS_ERROR, "Error getting randomness"),
ERR (YKPIV_GENERIC_ERROR, "Something went wrong."),
ERR (YKPIV_KEY_ERROR, "Error in key"),
};
/**
+30
View File
@@ -383,3 +383,33 @@ ykpiv_rc ykpiv_set_mgmkey(ykpiv_state *state, const unsigned char *new_key) {
}
return YKPIV_GENERIC_ERROR;
}
ykpiv_rc ykpiv_parse_key(ykpiv_state *state,
const char *key_in, unsigned char *key_out) {
unsigned int i;
char key_part[4] = {0};
int key_len = strlen(key_in);
if(key_len != DES_KEY_SZ * 3 * 2) {
if(state->verbose) {
fprintf(stderr, "Wrong key size, should be %lu characters (was %d).\n", DES_KEY_SZ * 3 * 2, key_len);
}
return YKPIV_SIZE_ERROR;
}
for(i = 0; i < DES_KEY_SZ * 3; i++) {
key_part[0] = *key_in++;
key_part[1] = *key_in++;
if(sscanf(key_part, "%hhx", &key_out[i]) != 1) {
if(state->verbose) {
fprintf(stderr, "Failed parsing key at position %d.\n", i);
}
return YKPIV_KEY_ERROR;
}
}
if(state->verbose > 1) {
fprintf(stderr, "parsed key: ");
dump_hex(key_out, DES_KEY_SZ * 3);
fprintf(stderr, "\n");
}
return YKPIV_OK;
}
+3
View File
@@ -48,6 +48,7 @@ extern "C"
YKPIV_AUTHENTICATION_ERROR = -5,
YKPIV_RANDOMNESS_ERROR = -6,
YKPIV_GENERIC_ERROR = -7,
YKPIV_KEY_ERROR = -8,
} ykpiv_rc;
const char *ykpiv_strerror(ykpiv_rc err);
@@ -63,6 +64,8 @@ extern "C"
unsigned char *data, unsigned long *recv_len, int *sw);
ykpiv_rc ykpiv_authenticate(ykpiv_state *state, const unsigned char *key);
ykpiv_rc ykpiv_set_mgmkey(ykpiv_state *state, const unsigned char *new_key);
ykpiv_rc ykpiv_parse_key(ykpiv_state *state,
const char *key_in, unsigned char *key_out);
#define YKPIV_ALGO_3DES 0x03;
#define YKPIV_ALGO_RSA1024 0x06;
+1
View File
@@ -37,6 +37,7 @@ global:
ykpiv_transfer_data;
ykpiv_authenticate;
ykpiv_set_mgmkey;
ykpiv_parse_key;
local:
*;