refactor dump_hex to drop some redundant code

This commit is contained in:
Klas Lindfors
2015-03-18 15:04:19 +01:00
parent cd1410a950
commit 0f26a7c1e3
3 changed files with 10 additions and 20 deletions
+3 -2
View File
@@ -144,11 +144,12 @@ parse_err:
return NULL; return NULL;
} }
void dump_hex(const unsigned char *buf, unsigned int len) { void dump_hex(const unsigned char *buf, unsigned int len, FILE *output, bool space) {
unsigned int i; unsigned int i;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
fprintf(stderr, "%02x ", buf[i]); fprintf(output, "%02x%s", buf[i], space == true ? " " : "");
} }
fprintf(output, "\n");
} }
int get_length(const unsigned char *buffer, int *len) { int get_length(const unsigned char *buffer, int *len) {
+1 -1
View File
@@ -37,7 +37,7 @@
#define INPUT 1 #define INPUT 1
#define OUTPUT 2 #define OUTPUT 2
void dump_hex(unsigned const char*, unsigned int); void dump_hex(unsigned const char*, unsigned int, FILE*, bool);
int set_length(unsigned char*, int); int set_length(unsigned char*, int);
int get_length(const unsigned char*, int*); int get_length(const unsigned char*, int*);
X509_NAME *parse_name(const char*); X509_NAME *parse_name(const char*);
+6 -17
View File
@@ -524,8 +524,7 @@ static bool set_chuid(ykpiv_state *state, int verbose) {
} }
if(verbose) { if(verbose) {
fprintf(stderr, "Setting the CHUID to: "); fprintf(stderr, "Setting the CHUID to: ");
dump_hex(chuid, sizeof(chuid)); dump_hex(chuid, sizeof(chuid), stderr, true);
fprintf(stderr, "\n");
} }
if((res = ykpiv_save_object(state, YKPIV_OBJ_CHUID, chuid, sizeof(chuid))) != YKPIV_OK) { if((res = ykpiv_save_object(state, YKPIV_OBJ_CHUID, chuid, sizeof(chuid))) != YKPIV_OK) {
fprintf(stderr, "Failed communicating with device: %s\n", ykpiv_strerror(res)); fprintf(stderr, "Failed communicating with device: %s\n", ykpiv_strerror(res));
@@ -1145,8 +1144,7 @@ static bool sign_file(ykpiv_state *state, const char *input, const char *output,
if(verbosity) { if(verbosity) {
fprintf(stderr, "file hashed as: "); fprintf(stderr, "file hashed as: ");
dump_hex(hashed, hash_len); dump_hex(hashed, hash_len, stderr, true);
fprintf(stderr, "\n");
} }
EVP_MD_CTX_destroy(mdctx); EVP_MD_CTX_destroy(mdctx);
} }
@@ -1183,8 +1181,7 @@ static bool sign_file(ykpiv_state *state, const char *input, const char *output,
if(verbosity) { if(verbosity) {
fprintf(stderr, "file signed as: "); fprintf(stderr, "file signed as: ");
dump_hex(buf, len); dump_hex(buf, len, stderr, true);
fprintf(stderr, "\n");
} }
fwrite(buf, 1, len, output_file); fwrite(buf, 1, len, output_file);
ret = true; ret = true;
@@ -1219,7 +1216,6 @@ static void print_cert_info(ykpiv_state *state, enum enum_slot slot, const EVP_M
} }
if(*ptr++ == 0x70) { if(*ptr++ == 0x70) {
unsigned int i;
unsigned int md_len = sizeof(data); unsigned int md_len = sizeof(data);
ASN1_TIME *not_before, *not_after; ASN1_TIME *not_before, *not_after;
@@ -1288,10 +1284,7 @@ static void print_cert_info(ykpiv_state *state, enum enum_slot slot, const EVP_M
fprintf(output, "\n"); fprintf(output, "\n");
X509_digest(x509, md, data, &md_len); X509_digest(x509, md, data, &md_len);
fprintf(output, "\tFingerprint:\t"); fprintf(output, "\tFingerprint:\t");
for(i = 0; i < md_len; i++) { dump_hex(data, md_len, output, false);
fprintf(output, "%02x", data[i]);
}
fprintf(output, "\n");
bio = BIO_new_fp(output, BIO_NOCLOSE | BIO_FP_TEXT); bio = BIO_new_fp(output, BIO_NOCLOSE | BIO_FP_TEXT);
not_before = X509_get_notBefore(x509); not_before = X509_get_notBefore(x509);
@@ -1346,14 +1339,10 @@ static bool status(ykpiv_state *state, enum enum_hash hash,
fprintf(output_file, "CHUID:\t"); fprintf(output_file, "CHUID:\t");
if(ykpiv_fetch_object(state, YKPIV_OBJ_CHUID, chuid, &len) != YKPIV_OK) { if(ykpiv_fetch_object(state, YKPIV_OBJ_CHUID, chuid, &len) != YKPIV_OK) {
fprintf(output_file, "No data available"); fprintf(output_file, "No data available\n");
} else { } else {
long unsigned i; dump_hex(chuid, len, output_file, false);
for(i = 0; i < len; i++) {
fprintf(output_file, "%02x", chuid[i]);
} }
}
fprintf(output_file, "\n");
fprintf(output_file, "Slot 9a:\t"); fprintf(output_file, "Slot 9a:\t");
print_cert_info(state, slot_arg_9a, md, output_file); print_cert_info(state, slot_arg_9a, md, output_file);