static struct emv_pk *emv_pk_get_ca_pk_from_file(const char *fname, const unsigned char *rid, unsigned char idx) { if (!fname) return NULL; FILE *f = fopen(fname, "r"); if (!f) { perror("fopen"); return NULL; } while (!feof(f)) { char buf[BUFSIZ]; if (fgets(buf, sizeof(buf), f) == NULL) break; struct emv_pk *pk = emv_pk_parse_pk(buf); if (!pk) continue; if (memcmp(pk->rid, rid, 5) || pk->index != idx) { emv_pk_free(pk); continue; } fclose(f); return pk; } fclose(f); return NULL; }
int main(int argc, char **argv) { FILE *f; const char *fname; int rc = 0; fname = openemv_config_get("capk"); f = fopen(fname, "r"); if (!f) { perror("fopen"); return 1; } while (!feof(f)) { char buf[BUFSIZ]; if (fgets(buf, sizeof(buf), f) == NULL) break; struct emv_pk *pk = emv_pk_parse_pk(buf); if (!pk) continue; fprintf(stderr, "Verifying CA PK for %02hhx:%02hhx:%02hhx:%02hhx:%02hhx IDX %02hhx %zd bits...", pk->rid[0], pk->rid[1], pk->rid[2], pk->rid[3], pk->rid[4], pk->index, pk->mlen * 8); if (emv_pk_verify(pk)) { fprintf(stderr, "OK\n"); if (argc > 2 && argv[2][0] == 'v') { unsigned char *c; c = emv_pk_dump_pk(pk); if (c) printf("%s\n", c); free(c); } } else { fprintf(stderr, "Failed!\n"); rc = 1; } emv_pk_free(pk); } fclose(f); return rc; }