26 unsigned long *recv_len,
int *sw);
30 for (i = 0; i <
len; i++) {
31 fprintf(stderr,
"%02x ",
buf[i]);
38 fprintf(stderr,
"Unable to initialize: %s",
49 fprintf(stderr,
"Unable to initialize: %s",
58 s->context = SCARD_E_INVALID_HANDLE;
80 SCardDisconnect(
state->card, SCARD_RESET_CARD);
84 if (SCardIsValidContext(
state->
context) == SCARD_S_SUCCESS) {
93 unsigned long active_protocol;
94 char reader_buf[2048];
95 size_t num_readers =
sizeof(reader_buf);
105 if (
state->verbose) {
112 for (reader_ptr = reader_buf; *reader_ptr !=
'\0';
113 reader_ptr += strlen(reader_ptr) + 1) {
115 if (!strstr(reader_ptr, wanted)) {
116 if (
state->verbose) {
117 fprintf(stderr,
"skipping reader '%s' since it doesn't match '%s'\n",
124 if (
state->verbose) {
125 fprintf(stderr,
"trying to connect to reader '%s'\n", reader_ptr);
129 SCardConnect(
state->
context, reader_ptr, SCARD_SHARE_SHARED,
130 SCARD_PROTOCOL_T1, &
state->card, (LPDWORD) &active_protocol);
132 if (
rc != SCARD_S_SUCCESS) {
133 if (
state->verbose) {
134 fprintf(stderr,
"SCardConnect failed, rc=%08lx\n",
rc);
140 unsigned char data[0xff];
141 unsigned long recv_len =
sizeof(data);
148 apdu.
st.lc =
sizeof(
aid);
151 if ((res = send_data(
state, &apdu, data, &recv_len, &sw)) !=
153 if (
state->verbose) {
154 fprintf(stderr,
"Failed communicating with card: '%s'\n",
162 if (
state->verbose) {
163 fprintf(stderr,
"Failed selecting application: %04x\n", sw);
168 if (*reader_ptr ==
'\0') {
169 if (
state->verbose) {
170 fprintf(stderr,
"error: no usable reader found\n");
181 unsigned long num_readers = 0;
184 if (
state == NULL || readers == NULL) {
188 if (SCardIsValidContext(
state->
context) != SCARD_S_SUCCESS) {
189 rc = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &
state->
context);
190 if (
rc != SCARD_S_SUCCESS) {
191 if (
state->verbose) {
192 fprintf(stderr,
"error: SCardEstablishContext failed, rc=%08lx\n",
rc);
198 rc = SCardListReaders(
state->
context, NULL, NULL, (LPDWORD) &num_readers);
199 if (
rc != SCARD_S_SUCCESS) {
200 if (
state->verbose) {
201 fprintf(stderr,
"error: SCardListReaders failed, rc=%08lx\n",
rc);
208 if (num_readers > *
len) {
212 rc = SCardListReaders(
state->
context, NULL, readers, (LPDWORD) &num_readers);
213 if (
rc != SCARD_S_SUCCESS) {
214 if (
state->verbose) {
215 fprintf(stderr,
"error: SCardListReaders failed, rc=%08lx\n",
rc);
228 unsigned long *recv_len,
int *sw) {
230 unsigned int send_len = (
unsigned int) apdu->
st.lc + 5;
234 if (
state->verbose > 1) {
235 fprintf(stderr,
"> ");
237 fprintf(stderr,
"\n");
239 rc = SCardTransmit(
state->card, SCARD_PCI_T1, apdu->raw, send_len, NULL, data,
241 if (
rc != SCARD_S_SUCCESS) {
242 if (
state->verbose) {
243 fprintf(stderr,
"error: SCardTransmit failed, rc=%08lx\n",
rc);
248 if (
state->verbose > 1) {
249 fprintf(stderr,
"< ");
251 fprintf(stderr,
"\n");
253 if (*recv_len >= 2) {
254 *sw = (data[*recv_len - 2] << 8) | data[*recv_len - 1];
262 unsigned char data[261];
263 unsigned long recv_len =
sizeof(data);
267 if (
state == NULL || version == NULL) {
276 int result = snprintf(version,
len,
"%d.%d.%d", data[0], data[1], data[2]);
278 if (
state->verbose) {
279 fprintf(stderr,
"Version buffer too small\n");
290 size_t key_enc_len,
const uint8_t *key_mac,
size_t key_mac_len,
291 const char *pw,
const uint8_t touch_policy) {
294 unsigned char data[261];
295 unsigned long recv_len =
sizeof(data);
310 *(ptr++) = strlen(
name);
314 apdu.
st.lc += strlen(
name);
351 *(ptr++) = touch_policy ? 1 : 0;
367 unsigned char data[64];
368 unsigned long recv_len =
sizeof(data);
384 *(ptr++) = strlen(
name);
388 apdu.
st.lc += strlen(
name);
390 rc = send_data(
state, &apdu, data, &recv_len, &sw);
394 if (
state->verbose) {
395 fprintf(stderr,
"Unable to delete key: %04x\n", sw);
404 size_t context_len,
const char *pw,
uint8_t *key_s_enc,
405 size_t key_s_enc_len,
uint8_t *key_s_mac,
406 size_t key_s_mac_len,
uint8_t *key_s_rmac,
407 size_t key_s_rmac_len,
uint8_t *retries) {
410 unsigned char data[64];
411 unsigned long recv_len =
sizeof(data);
432 *(ptr++) = strlen(
name);
436 apdu.
st.lc += strlen(
name);
440 *(ptr++) = context_len;
444 apdu.
st.lc += context_len;
454 rc = send_data(
state, &apdu, data, &recv_len, &sw);
458 if (
state->verbose) {
459 fprintf(stderr,
"Unable to derive keys: %04x\n", sw);
462 if (retries != NULL) {
463 *retries = sw & ~SW_ERR_AUTHENTICATION_FAILED;
486 unsigned char data[8];
487 unsigned long recv_len =
sizeof(data);
500 res = send_data(
state, &apdu, data, &recv_len, &sw);
502 if (
state->verbose) {
503 fprintf(stderr,
"Unable to reset: %s\n",
ykyh_strerror(res));
511 size_t *list_items) {
514 unsigned char data[1024];
515 unsigned long recv_len =
sizeof(data);
519 if (
state == NULL || list_items == NULL) {
526 res = send_data(
state, &apdu, data, &recv_len, &sw);
528 if (
state->verbose) {
529 fprintf(stderr,
"Unable to list keys: %s\n",
ykyh_strerror(res));
535 *list_items = data[0];
540 if (*list_items < data[0]) {
543 *list_items = data[0];
546 for (
size_t j = 0;
j < *list_items;
j++) {
548 size_t len = data[i++];
549 list[
j].algo = data[i++];
553 list[
j].ctr = data[i++];
559 if (i != recv_len - 2) {
void YH_INTERNAL dump_hex(FILE *file, const uint8_t *ptr, uint16_t len)
unsigned char raw[0xff+5]
const char * ykyh_strerror(ykyh_rc err)
unsigned const char aid[]
ykyh_rc ykyh_put(ykyh_state *state, const char *name, const uint8_t *key_enc, size_t key_enc_len, const uint8_t *key_mac, size_t key_mac_len, const char *pw, const uint8_t touch_policy)
ykyh_rc ykyh_get_version(ykyh_state *state, char *version, size_t len)
ykyh_rc ykyh_get_challenge(ykyh_state *state)
ykyh_rc ykyh_done(ykyh_state *state)
ykyh_rc ykyh_connect(ykyh_state *state, const char *wanted)
ykyh_rc ykyh_list_keys(ykyh_state *state, ykyh_list_entry *list, size_t *list_items)
ykyh_rc ykyh_reset(ykyh_state *state)
ykyh_rc ykyh_list_readers(ykyh_state *state, char *readers, size_t *len)
ykyh_rc ykyh_init(ykyh_state **state, int verbose)
ykyh_rc ykyh_delete(ykyh_state *state, char *name)
ykyh_rc ykyh_calculate(ykyh_state *state, const char *name, uint8_t *context, size_t context_len, const char *pw, uint8_t *key_s_enc, size_t key_s_enc_len, uint8_t *key_s_mac, size_t key_s_mac_len, uint8_t *key_s_rmac, size_t key_s_rmac_len, uint8_t *retries)
ykyh_rc ykyh_disconnect(ykyh_state *state)
#define SW_ERR_AUTHENTICATION_FAILED
#define YKYH_INS_GET_VERSION
#define YKYH_INS_CALCULATE
#define YKYH_MAX_NAME_LEN
#define YKYH_MIN_NAME_LEN
#define YKYH_TAG_NAME_LIST
memset(pInfo->slotDescription, ' ', 64)
memcpy((char *) pInfo->slotDescription, s, l)