From 6dcb6798e61d00db022b9c54ee61f45b5ab31251 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Tue, 17 Jun 2014 09:20:32 +0200 Subject: [PATCH] let the ykpiv_connect() function select as well --- lib/error.c | 1 + lib/ykpiv.c | 22 ++++++++++++++++++++++ lib/ykpiv.h | 1 + tool/yubico-piv-tool.c | 31 ------------------------------- 4 files changed, 24 insertions(+), 31 deletions(-) diff --git a/lib/error.c b/lib/error.c index c2e1c78..a4dd16f 100644 --- a/lib/error.c +++ b/lib/error.c @@ -45,6 +45,7 @@ static const err_t errors[] = { ERR (YKPIV_MEMORY_ERROR, "Error allocating memory"), ERR (YKPIV_PCSC_ERROR, "Error in PCSC call"), ERR (YKPIV_SIZE_ERROR, "Wrong buffer size"), + ERR (YKPIV_APPLET_ERROR, "No PIV applet found"), }; /** diff --git a/lib/ykpiv.c b/lib/ykpiv.c index 0391ec4..0db5dca 100644 --- a/lib/ykpiv.c +++ b/lib/ykpiv.c @@ -145,6 +145,28 @@ ykpiv_rc ykpiv_connect(ykpiv_state *state, const char *wanted) { return YKPIV_PCSC_ERROR; } + { + 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 = 0xa4; + apdu.st.p1 = 0x04; + apdu.st.lc = sizeof(aid); + memcpy(apdu.st.data, aid, sizeof(aid)); + + if((res = ykpiv_send_data(state, apdu.raw, data, &recv_len, &sw) != YKPIV_OK)) { + return res; + } else if(sw == 0x9000) { + return YKPIV_OK; + } + + return YKPIV_APPLET_ERROR; + } + return YKPIV_OK; } diff --git a/lib/ykpiv.h b/lib/ykpiv.h index a8111e5..5e65243 100644 --- a/lib/ykpiv.h +++ b/lib/ykpiv.h @@ -44,6 +44,7 @@ extern "C" YKPIV_MEMORY_ERROR = -1, YKPIV_PCSC_ERROR = -2, YKPIV_SIZE_ERROR = -3, + YKPIV_APPLET_ERROR = -4, } ykpiv_rc; const char *ykpiv_strerror(ykpiv_rc err); diff --git a/tool/yubico-piv-tool.c b/tool/yubico-piv-tool.c index d3106bb..6c73355 100644 --- a/tool/yubico-piv-tool.c +++ b/tool/yubico-piv-tool.c @@ -41,11 +41,6 @@ #include "cmdline.h" -unsigned const char aid[] = { - 0xa0, 0x00, 0x00, 0x03, 0x08 -}; - - /* FASC-N containing S9999F9999F999999F0F1F0000000000300001E encoded in * 4-bit BCD with 1 bit parity. run through the tools/fasc.pl script to get * bytes. */ @@ -95,27 +90,6 @@ static bool sign_data(ykpiv_state*, unsigned char*, int, unsigned char, unsigned ASN1_BIT_STRING*); static int get_object_id(enum enum_slot slot); -static bool select_applet(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 = 0xa4; - apdu.st.p1 = 0x04; - apdu.st.lc = sizeof(aid); - memcpy(apdu.st.data, aid, sizeof(aid)); - - if(ykpiv_send_data(state, apdu.raw, data, &recv_len, &sw) != YKPIV_OK) { - return false; - } else if(sw == 0x9000) { - return true; - } - - return false; -} - static bool authenticate(ykpiv_state *state, unsigned const char *key) { APDU apdu; unsigned char data[0xff]; @@ -1314,11 +1288,6 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - if(select_applet(state) == false) { - fprintf(stderr, "Failed to select applet.\n"); - return EXIT_FAILURE; - } - if(authenticate(state, key) == false) { fprintf(stderr, "Failed authentication with the applet.\n"); return EXIT_FAILURE;