global:
# Functions:
yk_challenge_response;
+ ykp_set_ndef_access_code;
+ ykp_alloc_ndef;
+ ykp_free_ndef;
# Variables:
} LIBYKPERS_1.7;
#include <string.h>
#include <assert.h>
+#include "ykcore/ykcore_lcl.h"
#include <ykpers.h>
#include <ykdef.h>
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)
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;
/* 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)))
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;
/*************************************************************************
*
/* 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. */
#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"
/* 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 */
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;
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);
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;
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);
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),