From: Klas Lindfors Date: Wed, 19 Sep 2012 14:18:22 +0000 (+0200) Subject: fix the NFC interfaces to work like the rest of our api X-Git-Tag: v1.8.0~29 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4a89eacbf8dfd6f614333b6b4f91a4242f793e2c;p=yubikey-personalization fix the NFC interfaces to work like the rest of our api --- diff --git a/libykpers-1.map b/libykpers-1.map index 5786fe9..6f73987 100644 --- a/libykpers-1.map +++ b/libykpers-1.map @@ -149,5 +149,8 @@ LIBYKPERS_1.8 { global: # Functions: yk_challenge_response; + ykp_set_ndef_access_code; + ykp_alloc_ndef; + ykp_free_ndef; # Variables: } LIBYKPERS_1.7; diff --git a/tests/test_ndef_construction.c b/tests/test_ndef_construction.c index dce4dda..9ce029c 100644 --- a/tests/test_ndef_construction.c +++ b/tests/test_ndef_construction.c @@ -32,73 +32,74 @@ #include #include +#include "ykcore/ykcore_lcl.h" #include #include void _test_https_uri() { - YKNDEF ndef; - memset(&ndef, 0, sizeof(YKNDEF)); + YK_NDEF *ndef = ykp_alloc_ndef(); char uri[] = "https://example.com/foo"; - int rc = ykp_construct_ndef_uri(&ndef, uri); + int rc = ykp_construct_ndef_uri(ndef, uri); assert(rc == 1); - assert(ndef.type == 'U'); - assert(ndef.data[0] == 0x04); - assert(strncmp(ndef.data + 1, "example.com/foo", 15) == 0); - assert(ndef.len == 16); + assert(ndef->type == 'U'); + assert(ndef->data[0] == 0x04); + assert(strncmp(ndef->data + 1, "example.com/foo", 15) == 0); + assert(ndef->len == 16); + ykp_free_ndef(ndef); } void _test_to_long_uri() { - YKNDEF ndef; - memset(&ndef, 0, sizeof(YKNDEF)); + YK_NDEF *ndef = ykp_alloc_ndef(); char uri[] = "https://example.example.example.example.com/foo/kaka/bar/blahonga"; - int rc = ykp_construct_ndef_uri(&ndef, uri); + int rc = ykp_construct_ndef_uri(ndef, uri); assert(rc == 0); - assert(ndef.type == 0); - assert(ndef.len == 0); + assert(ndef->type == 0); + assert(ndef->len == 0); + ykp_free_ndef(ndef); } void _test_exact_uri() { - YKNDEF ndef; - memset(&ndef, 0, sizeof(YKNDEF)); + YK_NDEF *ndef = ykp_alloc_ndef(); char uri[] = "https://www.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - int rc = ykp_construct_ndef_uri(&ndef, uri); + int rc = ykp_construct_ndef_uri(ndef, uri); assert(rc == 1); - assert(ndef.type == 'U'); - assert(ndef.data[0] == 0x02); - assert(strncmp(ndef.data + 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", NDEF_DATA_SIZE -1) == 0); - assert(ndef.len == NDEF_DATA_SIZE); + assert(ndef->type == 'U'); + assert(ndef->data[0] == 0x02); + assert(strncmp(ndef->data + 1, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", NDEF_DATA_SIZE -1) == 0); + assert(ndef->len == NDEF_DATA_SIZE); + ykp_free_ndef(ndef); } void _test_exact_text() { - YKNDEF ndef; - memset(&ndef, 0, sizeof(YKNDEF)); + YK_NDEF *ndef = ykp_alloc_ndef(); char text[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - int rc = ykp_construct_ndef_text(&ndef, text, "en", false); + int rc = ykp_construct_ndef_text(ndef, text, "en", false); assert(rc == 1); - assert(ndef.type == 'T'); - assert(ndef.data[0] == 2); - assert(strncmp(ndef.data + 1, "en", 2) == 0); - assert(strncmp(ndef.data + 3, text, NDEF_DATA_SIZE - 3) == 0); - assert(ndef.len == NDEF_DATA_SIZE); + assert(ndef->type == 'T'); + assert(ndef->data[0] == 2); + assert(strncmp(ndef->data + 1, "en", 2) == 0); + assert(strncmp(ndef->data + 3, text, NDEF_DATA_SIZE - 3) == 0); + assert(ndef->len == NDEF_DATA_SIZE); + ykp_free_ndef(ndef); } void _test_other_lang_text() { - YKNDEF ndef; - memset(&ndef, 0, sizeof(YKNDEF)); + YK_NDEF *ndef = ykp_alloc_ndef(); char text[] = "aaaaaaaaaaaaaaa"; size_t text_len = strlen(text); - int rc = ykp_construct_ndef_text(&ndef, text, "sv-SE", true); + int rc = ykp_construct_ndef_text(ndef, text, "sv-SE", true); assert(rc == 1); - assert(ndef.type == 'T'); - assert(ndef.data[0] == (0x80 & 5)); - assert(strncmp(ndef.data + 1, "sv-SE", 5) == 0); - assert(strncmp(ndef.data + 6, text, text_len) == 0); - assert(ndef.len == text_len + 6); + assert(ndef->type == 'T'); + assert(ndef->data[0] == (0x80 & 5)); + assert(strncmp(ndef->data + 1, "sv-SE", 5) == 0); + assert(strncmp(ndef->data + 6, text, text_len) == 0); + assert(ndef->len == text_len + 6); + ykp_free_ndef(ndef); } int main (void) diff --git a/ykcore/ykcore.c b/ykcore/ykcore.c index a06a894..f7a2201 100644 --- a/ykcore/ykcore.c +++ b/ykcore/ykcore.c @@ -239,9 +239,9 @@ int yk_write_config(YK_KEY *yk, YK_CONFIG *cfg, int confnum, return 1; } -int yk_write_ndef(YK_KEY *yk, YKNDEF *ndef) +int yk_write_ndef(YK_KEY *yk, YK_NDEF *ndef) { - unsigned char buf[sizeof(YKNDEF)]; + unsigned char buf[sizeof(YK_NDEF)]; YK_STATUS stat; int seq; @@ -255,7 +255,7 @@ int yk_write_ndef(YK_KEY *yk, YKNDEF *ndef) /* Insert config block in buffer */ memset(buf, 0, sizeof(buf)); - memcpy(buf, ndef, sizeof(YKNDEF)); + memcpy(buf, ndef, sizeof(YK_NDEF)); /* Write to Yubikey */ if (!yk_write_to_key(yk, SLOT_NDEF, buf, sizeof(buf))) diff --git a/ykcore/ykcore.h b/ykcore/ykcore.h index 776d30b..4c70b36 100644 --- a/ykcore/ykcore.h +++ b/ykcore/ykcore.h @@ -62,6 +62,7 @@ typedef struct yk_config_st YK_CONFIG; /* Configuration structure. typedef struct yk_nav_st YK_NAV; /* Navigation structure. Other libraries provide access. */ typedef struct yk_frame_st YK_FRAME; /* Data frame for write operation */ +typedef struct yk_ndef_st YK_NDEF; /************************************************************************* * @@ -114,6 +115,8 @@ extern int yk_write_command(YK_KEY *k, YK_CONFIG *cfg, uint8_t command, /* wrapper function of yk_write_command */ extern int yk_write_config(YK_KEY *k, YK_CONFIG *cfg, int confnum, unsigned char *acc_code); +/* writes the given ndef to the key. */ +int yk_write_ndef(YK_KEY *yk, YK_NDEF *ndef); /* Write something to the YubiKey (a command that is). */ extern int yk_write_to_key(YK_KEY *yk, uint8_t slot, const void *buf, int bufcount); /* Do a challenge-response round with the key. */ diff --git a/ykcore/ykcore_lcl.h b/ykcore/ykcore_lcl.h index 131fbc4..483e7c2 100644 --- a/ykcore/ykcore_lcl.h +++ b/ykcore/ykcore_lcl.h @@ -39,6 +39,7 @@ #define yk_config_st config_st #define yk_nav_st nav_st #define yk_frame_st frame_st +#define yk_ndef_st ndef_st #include "ykcore.h" #include "ykdef.h" diff --git a/ykcore/ykdef.h b/ykcore/ykdef.h index ca23af9..65dfa1e 100644 --- a/ykcore/ykdef.h +++ b/ykcore/ykdef.h @@ -177,12 +177,12 @@ struct config_st { /* NDEF structure */ #define NDEF_DATA_SIZE 54 -typedef struct { +struct ndef_st { unsigned char len; /* Payload length */ unsigned char type; /* NDEF type specifier */ unsigned char data[NDEF_DATA_SIZE]; /* Payload size */ unsigned char curAccCode[ACC_CODE_SIZE]; /* Access code */ -} YKNDEF; +}; /* Navigation */ diff --git a/ykpers.c b/ykpers.c index f8d449d..6ff8086 100644 --- a/ykpers.c +++ b/ykpers.c @@ -371,8 +371,28 @@ int ykp_AES_key_from_passphrase(YKP_CONFIG *cfg, const char *passphrase, return 0; } -/* Fill in the data and len parts of the YKNDEF struct based on supplied uri. */ -int ykp_construct_ndef_uri(YKNDEF *ndef, const char *uri) +YK_NDEF *ykp_alloc_ndef(void) +{ + YK_NDEF *ndef = malloc(sizeof(YK_NDEF)); + if(ndef) { + memset(ndef, 0, sizeof(YK_NDEF)); + return ndef; + } + return 0; +} + +void ykp_free_ndef(YK_NDEF *ndef) +{ + if(ndef) + { + free(ndef); + return 1; + } + return 0; +} + +/* Fill in the data and len parts of the YK_NDEF struct based on supplied uri. */ +int ykp_construct_ndef_uri(YK_NDEF *ndef, const char *uri) { int num_identifiers = sizeof(ndef_identifiers) / sizeof(char*); size_t data_length; @@ -400,8 +420,8 @@ int ykp_construct_ndef_uri(YKNDEF *ndef, const char *uri) return 1; } -/* Fill in the data and len parts of the YKNDEF struct based on supplied text. */ -int ykp_construct_ndef_text(YKNDEF *ndef, const char *text, const char *lang, bool isutf16) +/* Fill in the data and len parts of the YK_NDEF struct based on supplied text. */ +int ykp_construct_ndef_text(YK_NDEF *ndef, const char *text, const char *lang, bool isutf16) { size_t data_length = strlen(text); size_t lang_length = strlen(lang); @@ -421,6 +441,16 @@ int ykp_construct_ndef_text(YKNDEF *ndef, const char *text, const char *lang, bo return 1; } +int ykp_set_ndef_access_code(YK_NDEF *ndef, unsigned char *access_code) +{ + if(ndef) + { + memcpy(ndef->curAccCode, access_code, ACC_CODE_SIZE); + return 0; + } + return 1; +} + static bool vcheck_all(const YKP_CONFIG *cfg) { return true; diff --git a/ykpers.h b/ykpers.h index 4c67da8..a2c410b 100644 --- a/ykpers.h +++ b/ykpers.h @@ -66,9 +66,12 @@ int ykp_AES_key_from_passphrase(YKP_CONFIG *cfg, const char *passphrase, const char *salt); int ykp_HMAC_key_from_hex(YKP_CONFIG *cfg, const char *hexkey); -/* Functions for constructing the YKNDEF struct before writing it to a neo */ -int ykp_construct_ndef_uri(YKNDEF *ndef, const char *uri); -int ykp_construct_ndef_text(YKNDEF *ndef, const char *text, const char *lang, bool isutf16); +/* Functions for constructing the YK_NDEF struct before writing it to a neo */ +YK_NDEF *ykp_alloc_ndef(void); +void ykp_free_ndef(YK_NDEF *ndef); +int ykp_construct_ndef_uri(YK_NDEF *ndef, const char *uri); +int ykp_construct_ndef_text(YK_NDEF *ndef, const char *text, const char *lang, bool isutf16); +int ykp_set_ndef_access_code(YK_NDEF *ndef, unsigned char *access_code); int ykp_set_access_code(YKP_CONFIG *cfg, unsigned char *access_code, size_t len); int ykp_set_fixed(YKP_CONFIG *cfg, unsigned char *fixed, size_t len); diff --git a/ykpersonalize.c b/ykpersonalize.c index 0ec0784..9f2b7a9 100644 --- a/ykpersonalize.c +++ b/ykpersonalize.c @@ -228,21 +228,21 @@ int main(int argc, char **argv) if (verbose) printf("Attempting to write configuration to the yubikey..."); if(ykp_command(cfg) == SLOT_NDEF) { - YKNDEF ndef; - memset(&ndef, 0, sizeof(YKNDEF)); + YK_NDEF *ndef = ykp_alloc_ndef(); if(ndef_type == 'U') { - ykp_construct_ndef_uri(&ndef, ndef_string); + ykp_construct_ndef_uri(ndef, ndef_string); } else if(ndef_type == 'T') { - ykp_construct_ndef_text(&ndef, ndef_string, "en", false); + ykp_construct_ndef_text(ndef, ndef_string, "en", false); } if(use_access_code) { - memcpy(ndef.curAccCode, access_code, ACC_CODE_SIZE); + ykp_set_ndef_access_code(ndef, access_code); } - if (!yk_write_ndef(yk, &ndef)) { + if (!yk_write_ndef(yk, ndef)) { if (verbose) printf(" failure\n"); goto err; } + ykp_free_ndef(ndef); } else { if (!yk_write_command(yk, ykp_core_config(cfg), ykp_command(cfg),