refactor send_len to calculate the send_len internally

This commit is contained in:
Klas Lindfors
2014-02-07 11:29:32 +01:00
parent 85a9dc4252
commit c96caa9ceb
+19 -18
View File
@@ -87,7 +87,7 @@ union u_APDU {
typedef union u_APDU APDU; typedef union u_APDU APDU;
static void dump_hex(unsigned const char*, unsigned int); static void dump_hex(unsigned const char*, unsigned int);
static int send_data(SCARDHANDLE*, APDU*, unsigned int, unsigned char*, unsigned long*, int); static int send_data(SCARDHANDLE*, APDU*, unsigned char*, unsigned long*, int);
static int set_length(unsigned char*, int); static int set_length(unsigned char*, int);
static int get_length(unsigned char*, int *); static int get_length(unsigned char*, int *);
static X509_NAME *parse_name(char*); static X509_NAME *parse_name(char*);
@@ -171,7 +171,7 @@ static bool select_applet(SCARDHANDLE *card, int verbose) {
apdu.st.lc = sizeof(aid); apdu.st.lc = sizeof(aid);
memcpy(apdu.st.data, aid, sizeof(aid)); memcpy(apdu.st.data, aid, sizeof(aid));
sw = send_data(card, &apdu, sizeof(aid) + 5, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw == 0x9000) { if(sw == 0x9000) {
return true; return true;
} }
@@ -207,7 +207,7 @@ static bool authenticate(SCARDHANDLE *card, unsigned const char *key, int verbos
apdu.st.data[0] = 0x7c; apdu.st.data[0] = 0x7c;
apdu.st.data[1] = 0x02; apdu.st.data[1] = 0x02;
apdu.st.data[2] = 0x80; apdu.st.data[2] = 0x80;
sw = send_data(card, &apdu, 9, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw != 0x9000) { if(sw != 0x9000) {
return false; return false;
} }
@@ -229,7 +229,7 @@ static bool authenticate(SCARDHANDLE *card, unsigned const char *key, int verbos
apdu.st.data[2] = 0x80; apdu.st.data[2] = 0x80;
apdu.st.data[3] = 8; apdu.st.data[3] = 8;
memcpy(apdu.st.data + 4, response, 8); memcpy(apdu.st.data + 4, response, 8);
sw = send_data(card, &apdu, 17, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
} }
if(sw == 0x9000) { if(sw == 0x9000) {
@@ -246,7 +246,7 @@ static void print_version(SCARDHANDLE *card, int verbose) {
memset(apdu.raw, 0, sizeof(apdu)); memset(apdu.raw, 0, sizeof(apdu));
apdu.st.ins = 0xfd; apdu.st.ins = 0xfd;
sw = send_data(card, &apdu, 4, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw == 0x9000) { if(sw == 0x9000) {
printf("Applet version %d.%d.%d found.\n", data[0], data[1], data[2]); printf("Applet version %d.%d.%d found.\n", data[0], data[1], data[2]);
} else { } else {
@@ -307,7 +307,7 @@ static bool generate_key(SCARDHANDLE *card, const char *slot, enum enum_algorith
ret = false; ret = false;
goto generate_out; goto generate_out;
} }
sw = send_data(card, &apdu, 10, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
/* chained response */ /* chained response */
if((sw & 0x6100) == 0x6100) { if((sw & 0x6100) == 0x6100) {
@@ -315,7 +315,7 @@ static bool generate_key(SCARDHANDLE *card, const char *slot, enum enum_algorith
recv_len = 0xff; recv_len = 0xff;
memset(apdu.raw, 0, sizeof(apdu)); memset(apdu.raw, 0, sizeof(apdu));
apdu.st.ins = 0xc0; apdu.st.ins = 0xc0;
sw = send_data(card, &apdu, 4, data + received, &recv_len, verbose); sw = send_data(card, &apdu, data + received, &recv_len, verbose);
} }
if(sw != 0x9000) { if(sw != 0x9000) {
fprintf(stderr, "Failed to generate new key.\n"); fprintf(stderr, "Failed to generate new key.\n");
@@ -440,7 +440,7 @@ static bool set_mgm_key(SCARDHANDLE *card, unsigned const char *new_key, int ver
apdu.st.data[1] = 0x9b; apdu.st.data[1] = 0x9b;
apdu.st.data[2] = KEY_LEN; apdu.st.data[2] = KEY_LEN;
memcpy(apdu.st.data + 3, new_key, KEY_LEN); memcpy(apdu.st.data + 3, new_key, KEY_LEN);
sw = send_data(card, &apdu, KEY_LEN + 8, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw == 0x9000) { if(sw == 0x9000) {
return true; return true;
@@ -457,7 +457,7 @@ static bool reset(SCARDHANDLE *card, int verbose) {
memset(apdu.raw, 0, sizeof(apdu)); memset(apdu.raw, 0, sizeof(apdu));
/* note: the reset function is only available when both pins are blocked. */ /* note: the reset function is only available when both pins are blocked. */
apdu.st.ins = 0xfb; apdu.st.ins = 0xfb;
sw = send_data(card, &apdu, 4, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw == 0x9000) { if(sw == 0x9000) {
return true; return true;
@@ -484,7 +484,7 @@ static bool set_pin_retries(SCARDHANDLE *card, int pin_retries, int puk_retries,
apdu.st.ins = 0xfa; apdu.st.ins = 0xfa;
apdu.st.p1 = pin_retries; apdu.st.p1 = pin_retries;
apdu.st.p2 = puk_retries; apdu.st.p2 = puk_retries;
sw = send_data(card, &apdu, 4, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw == 0x9000) { if(sw == 0x9000) {
return true; return true;
@@ -598,7 +598,7 @@ static bool import_key(SCARDHANDLE *card, enum enum_key_format key_format,
apdu.st.p2 = key; apdu.st.p2 = key;
apdu.st.lc = this_size; apdu.st.lc = this_size;
memcpy(apdu.st.data, in_ptr, this_size); memcpy(apdu.st.data, in_ptr, this_size);
sw = send_data(card, &apdu, this_size + 5, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw != 0x9000) { if(sw != 0x9000) {
fprintf(stderr, "Failed import command with code %x.", sw); fprintf(stderr, "Failed import command with code %x.", sw);
ret = false; ret = false;
@@ -747,7 +747,7 @@ static bool import_cert(SCARDHANDLE *card, enum enum_key_format cert_format,
apdu.st.p2 = 0xff; apdu.st.p2 = 0xff;
apdu.st.lc = this_size; apdu.st.lc = this_size;
memcpy(apdu.st.data, certptr, this_size); memcpy(apdu.st.data, certptr, this_size);
sw = send_data(card, &apdu, this_size + 5, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw != 0x9000) { if(sw != 0x9000) {
fprintf(stderr, "Failed import command with code %x.", sw); fprintf(stderr, "Failed import command with code %x.", sw);
ret = false; ret = false;
@@ -797,7 +797,7 @@ static bool set_chuid(SCARDHANDLE *card, int verbose) {
apdu.st.p1 = 0x3f; apdu.st.p1 = 0x3f;
apdu.st.p2 = 0xff; apdu.st.p2 = 0xff;
apdu.st.lc = sizeof(chuid_tmpl); apdu.st.lc = sizeof(chuid_tmpl);
sw = send_data(card, &apdu, sizeof(chuid_tmpl) + 5, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw != 0x9000) { if(sw != 0x9000) {
fprintf(stderr, "Failed setting CHUID.\n"); fprintf(stderr, "Failed setting CHUID.\n");
return false; return false;
@@ -971,13 +971,13 @@ static bool request_certificate(SCARDHANDLE *card, enum enum_key_format key_form
apdu.st.p2 = key; apdu.st.p2 = key;
apdu.st.lc = this_size; apdu.st.lc = this_size;
memcpy(apdu.st.data, dataptr, this_size); memcpy(apdu.st.data, dataptr, this_size);
sw = send_data(card, &apdu, apdu.st.lc + 5, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if((sw & 0x6100) == 0x6100) { if((sw & 0x6100) == 0x6100) {
received += recv_len - 2; received += recv_len - 2;
recv_len = 0xff; recv_len = 0xff;
memset(apdu.raw, 0, sizeof(apdu)); memset(apdu.raw, 0, sizeof(apdu));
apdu.st.ins = 0xc0; apdu.st.ins = 0xc0;
sw = send_data(card, &apdu, 4, data + received, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw == 0x9000) { if(sw == 0x9000) {
received += recv_len - 2; received += recv_len - 2;
} else { } else {
@@ -1064,7 +1064,7 @@ static bool verify_pin(SCARDHANDLE *card, const char *pin, int verbose) {
if(len < 8) { if(len < 8) {
memset(apdu.st.data + len, 0xff, 8 - len); memset(apdu.st.data + len, 0xff, 8 - len);
} }
sw = send_data(card, &apdu, apdu.st.lc + 5, data, &recv_len, verbose); sw = send_data(card, &apdu, data, &recv_len, verbose);
if(sw != 0x9000) { if(sw != 0x9000) {
return false; return false;
} }
@@ -1148,10 +1148,11 @@ parse_err:
return NULL; return NULL;
} }
static int send_data(SCARDHANDLE *card, APDU *apdu, unsigned int send_len, static int send_data(SCARDHANDLE *card, APDU *apdu, unsigned char *data,
unsigned char *data, unsigned long *recv_len, int verbose) { unsigned long *recv_len, int verbose) {
long rc; long rc;
int sw; int sw;
unsigned int send_len = (unsigned int)(apdu->st.lc + 5);
if(verbose > 1) { if(verbose > 1) {
fprintf(stderr, "> "); fprintf(stderr, "> ");