From a97010d5e3660e08ecc2c4825288a529169f7b0c Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 17 Jun 2014 15:26:48 +0200 Subject: [PATCH] add ykpiv_get_version() function --- lib/ykpiv.c | 22 ++++++++++++++++++++++ lib/ykpiv.h | 4 ++++ lib/ykpiv.map | 1 + tool/yubico-piv-tool.c | 16 ++++------------ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/ykpiv.c b/lib/ykpiv.c index 6c76f62..a35f515 100644 --- a/lib/ykpiv.c +++ b/lib/ykpiv.c @@ -535,3 +535,25 @@ ykpiv_rc ykpiv_sign_data(ykpiv_state *state, memcpy(sign_out, dataptr, len); return YKPIV_OK; } + +ykpiv_rc ykpiv_get_version(ykpiv_state *state, char *version, size_t len) { + APDU apdu; + unsigned char data[0xff]; + unsigned long recv_len = sizeof(data); + int sw; + ykpiv_rc res; + + memset(apdu.raw, 0, sizeof(apdu)); + apdu.st.ins = YKPIV_INS_GET_VERSION; + if((res = ykpiv_send_data(state, apdu.raw, data, &recv_len, &sw)) != YKPIV_OK) { + return res; + } else if(sw == 0x9000) { + int result = snprintf(version, len, "%d.%d.%d", data[0], data[1], data[2]); + if(result < 0) { + return YKPIV_SIZE_ERROR; + } + return YKPIV_OK; + } else { + return YKPIV_GENERIC_ERROR; + } +} diff --git a/lib/ykpiv.h b/lib/ykpiv.h index e0b1067..bf035b4 100644 --- a/lib/ykpiv.h +++ b/lib/ykpiv.h @@ -31,6 +31,8 @@ #define YKPIV_H #include +#include + #include #ifdef __cplusplus @@ -72,6 +74,7 @@ extern "C" ykpiv_rc ykpiv_sign_data(ykpiv_state *state, const unsigned char *sign_in, int in_len,unsigned char *sign_out, int *out_len, unsigned char algorithm, unsigned char key); + ykpiv_rc ykpiv_get_version(ykpiv_state *state, char *version, size_t len); #define YKPIV_ALGO_3DES 0x03 #define YKPIV_ALGO_RSA1024 0x06 @@ -88,6 +91,7 @@ extern "C" /* Yubico vendor specific instructions */ #define YKPIV_INS_SET_MGMKEY 0xff +#define YKPIV_INS_GET_VERSION 0xfd #ifdef __cplusplus } diff --git a/lib/ykpiv.map b/lib/ykpiv.map index eed8a19..5aed2ae 100644 --- a/lib/ykpiv.map +++ b/lib/ykpiv.map @@ -40,6 +40,7 @@ global: ykpiv_set_mgmkey; ykpiv_parse_key; ykpiv_sign_data; + ykpiv_get_version; local: *; diff --git a/tool/yubico-piv-tool.c b/tool/yubico-piv-tool.c index 6b73936..8aacddc 100644 --- a/tool/yubico-piv-tool.c +++ b/tool/yubico-piv-tool.c @@ -93,19 +93,11 @@ static FILE *open_file(const char*, int); static int get_object_id(enum enum_slot slot); static void print_version(ykpiv_state *state) { - APDU apdu; - unsigned char data[0xff]; - unsigned long recv_len = sizeof(data); - int sw; - - memset(apdu.raw, 0, sizeof(apdu)); - apdu.st.ins = 0xfd; - if(ykpiv_send_data(state, apdu.raw, data, &recv_len, &sw) != YKPIV_OK) { - printf("Failed to retreive apple version.\n"); - } else if(sw == 0x9000) { - printf("Applet version %d.%d.%d found.\n", data[0], data[1], data[2]); + char version[7]; + if(ykpiv_get_version(state, version, sizeof(version)) == YKPIV_OK) { + printf("Applet version %s found.\n", version); } else { - printf("Applet version not found. Status code: %x\n", sw); + printf("Failed to retreive apple version.\n"); } }