From: Klas Lindfors Date: Wed, 25 Apr 2012 12:10:33 +0000 (+0200) Subject: function for writing ndef struct to yubikey X-Git-Tag: v1.7.0~1^2~29 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85c7467050567e831ab1642cd5a0cab0e5b40406;p=yubikey-personalization function for writing ndef struct to yubikey --- diff --git a/ykcore/ykcore.c b/ykcore/ykcore.c index 1b81341..eee3b37 100644 --- a/ykcore/ykcore.c +++ b/ykcore/ykcore.c @@ -250,6 +250,45 @@ int yk_write_config(YK_KEY *yk, YK_CONFIG *cfg, int confnum, } } +int yk_write_ndef(YK_KEY *yk, YKNDEF *ndef) +{ + unsigned char buf[sizeof(YKNDEF)]; + YK_STATUS stat; + int seq; + + /* Get current sequence # from status block */ + + if (!yk_get_status(yk, &stat)) + return 0; + + seq = stat.pgmSeq; + + /* Insert config block in buffer */ + + memset(buf, 0, sizeof(buf)); + memcpy(buf, ndef, sizeof(YKNDEF)); + + /* Write to Yubikey */ + if (!yk_write_to_key(yk, SLOT_NDEF, buf, sizeof(buf))) + return 0; + + /* When the Yubikey clears the SLOT_WRITE_FLAG, it has processed the last write. + * This wait can't be done in yk_write_to_key since some users of that function + * want to get the bytes in the status message, but when writing configuration + * we don't expect any data back. + */ + yk_wait_for_key_status(yk, SLOT_NDEF, 0, WAIT_FOR_WRITE_FLAG, false, SLOT_WRITE_FLAG, NULL); + + /* Verify update */ + + if (!yk_get_status(yk, &stat /*, 0*/)) + return 0; + + yk_errno = YK_EWRITEERR; + return stat.pgmSeq != seq; +} + + int * const _yk_errno_location(void) { static int tsd_init = 0;