From bd6e6db138939ca00c0f504a21f54348c0abe3b1 Mon Sep 17 00:00:00 2001 From: Alessio Di Mauro Date: Wed, 9 Sep 2015 16:31:25 +0200 Subject: [PATCH] Add parsing of model number. --- ykcs11/token_vendors.h | 2 +- ykcs11/utils.c | 15 +++++++++------ ykcs11/utils.h | 4 ++-- ykcs11/ykcs11.c | 2 +- ykcs11/yubico_token.c | 19 +++++++++++++++---- ykcs11/yubico_token.h | 2 +- 6 files changed, 29 insertions(+), 15 deletions(-) diff --git a/ykcs11/token_vendors.h b/ykcs11/token_vendors.h index a5b6175..791dc91 100644 --- a/ykcs11/token_vendors.h +++ b/ykcs11/token_vendors.h @@ -8,7 +8,7 @@ typedef CK_RV (*get_t_label_f)(CK_UTF8CHAR_PTR, CK_ULONG); typedef CK_RV (*get_t_manufacturer_f)(CK_UTF8CHAR_PTR, CK_ULONG); -typedef CK_RV (*get_t_model_f)(CK_UTF8CHAR_PTR, CK_ULONG); +typedef CK_RV (*get_t_model_f)(ykpiv_state *, CK_UTF8CHAR_PTR, CK_ULONG); typedef CK_RV (*get_t_flags_f)(CK_FLAGS_PTR); typedef CK_RV (*get_t_version_f)(CK_UTF8CHAR_PTR, CK_ULONG, CK_VERSION_PTR); typedef CK_RV (*get_t_serial_f)(CK_CHAR_PTR, CK_ULONG); diff --git a/ykcs11/utils.c b/ykcs11/utils.c index 756b77b..8efc89d 100644 --- a/ykcs11/utils.c +++ b/ykcs11/utils.c @@ -8,7 +8,7 @@ CK_BBOOL has_token(const ykcs11_slot_t *slot) { } -CK_RV parse_readers(const CK_BYTE_PTR readers, const CK_ULONG len, +CK_RV parse_readers(ykpiv_state *state, const CK_BYTE_PTR readers, const CK_ULONG len, ykcs11_slot_t *slots, CK_ULONG_PTR n_slots, CK_ULONG_PTR n_with_token) { CK_BYTE i; @@ -49,7 +49,7 @@ CK_RV parse_readers(const CK_BYTE_PTR readers, const CK_ULONG len, memset(slots[*n_slots].info.slotDescription, ' ', sizeof(slots[*n_slots].info.slotDescription)); s = slots[*n_slots].info.slotDescription; l = sizeof(slots[*n_slots].info.slotDescription); - strncpy((char *)s, (char *)p, l); + strncpy((char *)s, (char*)p, l); memset(slots[*n_slots].info.manufacturerID, ' ', sizeof(slots[*n_slots].info.manufacturerID)); s = slots[*n_slots].info.manufacturerID; @@ -71,7 +71,7 @@ CK_RV parse_readers(const CK_BYTE_PTR readers, const CK_ULONG len, // Save token information (*n_with_token)++; - if (create_token(p, slots + *n_slots) != CKR_OK) + if (create_token(state, p, slots + *n_slots) != CKR_OK) goto failure; } } @@ -90,7 +90,7 @@ failure: return CKR_FUNCTION_FAILED; } -CK_RV create_token(CK_BYTE_PTR p, ykcs11_slot_t *slot) { +CK_RV create_token(ykpiv_state *state, CK_BYTE_PTR p, ykcs11_slot_t *slot) { token_vendor_t token; CK_TOKEN_INFO_PTR t_info; @@ -112,9 +112,12 @@ CK_RV create_token(CK_BYTE_PTR p, ykcs11_slot_t *slot) { if(token.get_token_manufacturer(t_info->manufacturerID, sizeof(t_info->manufacturerID)) != CKR_OK) return CKR_FUNCTION_FAILED; - memset(t_info->model, ' ', sizeof(t_info->model)); - if(token.get_token_model(t_info->model, sizeof(t_info->model)) != CKR_OK) + if (ykpiv_connect(state, (char *)p) != YKPIV_OK) return CKR_FUNCTION_FAILED; + memset(t_info->model, ' ', sizeof(t_info->model)); + if(token.get_token_model(state, t_info->model, sizeof(t_info->model)) != CKR_OK) + return CKR_FUNCTION_FAILED; + ykpiv_disconnect(state); memset(t_info->serialNumber, ' ', sizeof(t_info->serialNumber)); if(token.get_token_serial(t_info->serialNumber, sizeof(t_info->serialNumber)) != CKR_OK) diff --git a/ykcs11/utils.h b/ykcs11/utils.h index af6f82a..509b2d5 100644 --- a/ykcs11/utils.h +++ b/ykcs11/utils.h @@ -4,9 +4,9 @@ #include "ykcs11.h" CK_BBOOL has_token(const ykcs11_slot_t *slot); -CK_RV parse_readers(const CK_BYTE_PTR readers, const CK_ULONG len, +CK_RV parse_readers(ykpiv_state *state,const CK_BYTE_PTR readers, const CK_ULONG len, ykcs11_slot_t *slots, CK_ULONG_PTR n_slots, CK_ULONG_PTR n_with_token); -CK_RV create_token(CK_BYTE_PTR p, ykcs11_slot_t *slot); +CK_RV create_token(ykpiv_state *state, CK_BYTE_PTR p, ykcs11_slot_t *slot); void destroy_token(ykcs11_slot_t *slot); CK_BBOOL is_valid_key_id(CK_BYTE id); diff --git a/ykcs11/ykcs11.c b/ykcs11/ykcs11.c index 3ba5ad3..1b11abd 100644 --- a/ykcs11/ykcs11.c +++ b/ykcs11/ykcs11.c @@ -65,7 +65,7 @@ CK_DEFINE_FUNCTION(CK_RV, C_Initialize)( return CKR_FUNCTION_FAILED; } - if (parse_readers(readers, len, slots, &n_slots, &n_slots_with_token) != CK_TRUE) + if (parse_readers(piv_state, readers, len, slots, &n_slots, &n_slots_with_token) != CK_TRUE) CKR_FUNCTION_FAILED; DBG(("Found %lu slot(s) of which %lu tokenless/unsupported", n_slots, n_slots - n_slots_with_token)); diff --git a/ykcs11/yubico_token.c b/ykcs11/yubico_token.c index b005d3a..f5848f5 100644 --- a/ykcs11/yubico_token.c +++ b/ykcs11/yubico_token.c @@ -10,9 +10,9 @@ #define MIN_ECC_KEY_SIZE 256 #define MAX_ECC_KEY_SIZE 384 -static const CK_UTF8CHAR_PTR token_label = "YubiKey PIV X"; -static const CK_UTF8CHAR_PTR token_manufacturer = "Yubico"; -static const CK_UTF8CHAR_PTR token_model = "YubiKey MODEL"; +static const char* token_label = "YubiKey PIV"; +static const char* token_manufacturer = "Yubico"; +static const char* token_model = "YubiKey XXX"; static const CK_FLAGS token_flags = CKF_RNG | CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_TOKEN_INITIALIZED; static const CK_BYTE_PTR token_serial = "1234"; static const CK_MECHANISM_TYPE token_mechanisms[] = { // KEEP ALIGNED WITH token_mechanism_infos @@ -124,13 +124,24 @@ CK_RV YUBICO_get_token_manufacturer(CK_UTF8CHAR_PTR str, CK_ULONG len) { return CKR_OK; } +#include "debug.h" +CK_RV YUBICO_get_token_model(ykpiv_state *state, CK_UTF8CHAR_PTR str, CK_ULONG len) { -CK_RV YUBICO_get_token_model(CK_UTF8CHAR_PTR str, CK_ULONG len) { + char buf[16]; if (strlen(token_model) > len) return CKR_BUFFER_TOO_SMALL; + if (ykpiv_get_version(state, buf, sizeof(buf)) != YKPIV_OK) + return CKR_FUNCTION_FAILED; + memcpy(str, token_model, strlen(token_model)); + + if (buf[0] >= '4') + memcpy(str + strlen(token_model) - 3, "YK4", 3); + else + memcpy(str + strlen(token_model) - 3, "NEO", 3); + return CKR_OK; } diff --git a/ykcs11/yubico_token.h b/ykcs11/yubico_token.h index 09e57ea..e95c8e3 100644 --- a/ykcs11/yubico_token.h +++ b/ykcs11/yubico_token.h @@ -7,7 +7,7 @@ CK_RV YUBICO_get_token_label(CK_UTF8CHAR_PTR str, CK_ULONG len); CK_RV YUBICO_get_token_manufacturer(CK_UTF8CHAR_PTR str, CK_ULONG len); -CK_RV YUBICO_get_token_model(CK_UTF8CHAR_PTR str, CK_ULONG len); +CK_RV YUBICO_get_token_model(ykpiv_state *state, CK_UTF8CHAR_PTR str, CK_ULONG len); CK_RV YUBICO_get_token_flags(CK_FLAGS_PTR flags); CK_RV YUBICO_get_token_serial(CK_CHAR_PTR str, CK_ULONG len); CK_RV YUBICO_get_token_version(CK_UTF8CHAR_PTR v_str, CK_ULONG v_str_len, CK_VERSION_PTR version);