From: Klas Lindfors Date: Thu, 26 Apr 2012 07:04:13 +0000 (+0200) Subject: support for writing ndef text and uri X-Git-Tag: v1.7.0~1^2~19 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5d08a845993583a20ad60430490d2a973b81e8d6;p=yubikey-personalization support for writing ndef text and uri let there be ykp_construct_ndef_uri() and ykp_construct_ndef_text() --- diff --git a/libykpers-1.map b/libykpers-1.map index 4d50c51..2407744 100644 --- a/libykpers-1.map +++ b/libykpers-1.map @@ -140,6 +140,7 @@ LIBYKPERS_1.7 { ykp_set_extflag_ALLOW_UPDATE; ykp_set_extflag_DORMANT; yk_write_ndef; - ykp_construct_ndef; + ykp_construct_ndef_uri; + ykp_construct_ndef_text; # Variables: } LIBYKPERS_1.6; diff --git a/tests/test_args_to_config.c b/tests/test_args_to_config.c index 1df817b..1af3bd0 100644 --- a/tests/test_args_to_config.c +++ b/tests/test_args_to_config.c @@ -112,6 +112,7 @@ int _test_config (YKP_CONFIG *cfg, YK_STATUS *st, int argc, char **argv) /* Options */ char *salt = NULL; char *ndef = NULL; + char ndef_type; int rc; @@ -127,7 +128,7 @@ int _test_config (YKP_CONFIG *cfg, YK_STATUS *st, int argc, char **argv) &autocommit, salt, st, &verbose, access_code, &use_access_code, - &aesviahash, ndef, + &aesviahash, &ndef_type, ndef, &exit_code); return rc; diff --git a/ykpers-args.c b/ykpers-args.c index 9074a4a..2500003 100644 --- a/ykpers-args.c +++ b/ykpers-args.c @@ -65,8 +65,8 @@ const char *usage = " char hex value (not modhex)\n" "-cXXX.. A 12 char hex value (not modhex) to use as access code for programming\n" " (this does NOT SET the access code, that's done with -oaccess=)\n" -"-nXXX.. The URI to prepend to the OTP when sending OTP over NFC as ndef-type2\n" -" smart-tag. Only available with the YubiKey NEO.\n" +"-nXXX.. Write NDEF type 2 URI to YubiKey NEO, must be used on it's own\n" +"-tXXX.. Write NDEF type 2 text to YubiKey NEO, must be used on it's own\n" "-oOPTION change configuration option. Possible OPTION arguments are:\n" " salt=ssssssss Salt to be used when deriving key from a\n" " password. If none is given, a unique random\n" @@ -140,7 +140,7 @@ const char *usage = "-v verbose\n" "-h help (this text)\n" ; -const char *optstring = "u12xa:c:n:hi:o:s:vy"; +const char *optstring = "u12xa:c:n:t:hi:o:s:vy"; static const YK_CONFIG default_config1 = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* fixed */ @@ -249,7 +249,7 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, bool *autocommit, char *salt, YK_STATUS *st, bool *verbose, unsigned char *access_code, bool *use_access_code, - bool *aesviahash, char *ndef, + bool *aesviahash, char *ndef_type, char *ndef, int *exit_code) { int c; @@ -415,9 +415,14 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, *use_access_code = true; break; } + case 't': + *ndef_type = 'T'; case 'n': + if(!*ndef_type) { + *ndef_type = 'U'; + } if (slot_chosen || swap_seen || update_seen || option_seen) { - fprintf(stderr, "Ndef (-n) must be used on it's own.\n"); + fprintf(stderr, "Ndef (-n/-t) must be used on it's own.\n"); *exit_code = 1; return 0; } diff --git a/ykpers-args.h b/ykpers-args.h index 14d3965..e070fad 100644 --- a/ykpers-args.h +++ b/ykpers-args.h @@ -38,7 +38,7 @@ int args_to_config(int argc, char **argv, YKP_CONFIG *cfg, bool *autocommit, char *salt, YK_STATUS *st, bool *verbose, unsigned char *access_code, bool *use_access_code, - bool *aesviahash, char *ndef, + bool *aesviahash, char *ndef_type, char *ndef, int *exit_code); void report_yk_error(); diff --git a/ykpers.c b/ykpers.c index bce6e97..6b827a0 100644 --- a/ykpers.c +++ b/ykpers.c @@ -372,7 +372,7 @@ int ykp_AES_key_from_passphrase(YKP_CONFIG *cfg, const char *passphrase, } /* Fill in the data and len parts of the YKNDEF struct based on supplied uri. */ -int ykp_construct_ndef(YKNDEF *ndef, const char *uri) +int ykp_construct_ndef_uri(YKNDEF *ndef, const char *uri) { int num_identifiers = sizeof(ndef_identifiers) / sizeof(char*); int index = 0; @@ -395,6 +395,21 @@ int ykp_construct_ndef(YKNDEF *ndef, const char *uri) } memcpy(ndef->data + 1, uri, data_length); ndef->len = data_length + 1; + ndef->type = 'U'; + return 0; +} + +/* 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) +{ + size_t data_length = strlen(text); + if(data_length > NDEF_DATA_SIZE) { + ykp_errno = YKP_EINVAL; + return 1; + } + memcpy(ndef->data, text, data_length); + ndef->len = data_length; + ndef->type = 'T'; return 0; } @@ -443,7 +458,7 @@ static bool vcheck_neo(const YKP_CONFIG *cfg) static bool vcheck_neo_before_5(const YKP_CONFIG *cfg) { - return vcheck_neo && cfg->yk_build_version < 5; + return vcheck_neo(cfg) && cfg->yk_build_version < 5; } static bool capability_has_hidtrig(const YKP_CONFIG *cfg) diff --git a/ykpers.h b/ykpers.h index 56aa428..a72a3ec 100644 --- a/ykpers.h +++ b/ykpers.h @@ -34,6 +34,7 @@ #include #include #include +#include # ifdef __cplusplus extern "C" { @@ -65,6 +66,10 @@ 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); + 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); int ykp_set_uid(YKP_CONFIG *cfg, unsigned char *uid, size_t len); diff --git a/ykpersonalize.c b/ykpersonalize.c index 2f441d0..c5320d2 100644 --- a/ykpersonalize.c +++ b/ykpersonalize.c @@ -65,7 +65,8 @@ int main(int argc, char **argv) /* Options */ char *salt = NULL; - char ndef_uri[128]; + char ndef_string[128]; + char ndef_type; bool error = false; int exit_code = 0; @@ -125,7 +126,7 @@ int main(int argc, char **argv) &autocommit, salt, st, &verbose, access_code, &use_access_code, - &aesviahash, ndef_uri, + &aesviahash, &ndef_type, ndef_string, &exit_code)) { goto err; } @@ -229,11 +230,14 @@ int main(int argc, char **argv) if(ykp_command(cfg) == SLOT_NDEF) { YKNDEF ndef; memset(&ndef, 0, sizeof(YKNDEF)); - ykp_construct_ndef(&ndef, ndef_uri); + if(ndef_type == 'U') { + ykp_construct_ndef_uri(&ndef, ndef_string); + } else if(ndef_type == 'T') { + ykp_construct_ndef_text(&ndef, ndef_string); + } if(use_access_code) { memcpy(ndef.curAccCode, access_code, ACC_CODE_SIZE); } - ndef.type = 'U'; if (!yk_write_ndef(yk, &ndef)) { if (verbose) printf(" failure\n");