/** * A simple program that loads certificate and dumps it to the * stdout. * * Written by Stjepan Gros . Use this * code as it suites you. */ #include #include #include #include gnutls_datum_t *load_file(const char *file) { FILE *f; size_t filelen, size; void *ptr = NULL; gnutls_datum_t *loaded_file = NULL; if ((f = fopen(file, "r")) != NULL) { if (fseek(f, 0, SEEK_END) != 0) { perror("fseek"); goto out; } if ((filelen = ftell(f)) < 0) { perror("ftell"); goto out; } if (fseek(f, 0, SEEK_SET) != 0) { perror("fseek"); goto out; } ptr = g_malloc(filelen); if ((size = fread(ptr, 1, filelen, f)) < filelen) { if (size < 0) perror("fread"); goto out; } loaded_file = g_malloc0(sizeof(gnutls_datum_t)); loaded_file->data = ptr; loaded_file->size = filelen; out: fclose(f); } else perror ("fopen"); return loaded_file; } void unload_file(gnutls_datum_t *file) { g_free(file->data); g_free(file); } int main(int argc, char **argv) { gnutls_x509_crt_t crt; gnutls_datum_t *crt_file = NULL; int ret; gnutls_datum_t txt_cert; if (argc != 2) { fprintf (stderr, "Usage: %s \n", argv[0]); return 1; } if ((crt_file = load_file(argv[1])) == NULL) return 1; gnutls_global_init(); gnutls_x509_crt_init(&crt); ret = gnutls_x509_crt_import(crt, crt_file, GNUTLS_X509_FMT_PEM); unload_file(crt_file); if (ret < 0) { fprintf(stderr, "Error: %s\n", gnutls_strerror(ret)); return 1; } gnutls_x509_crt_print(crt, GNUTLS_CRT_PRINT_FULL, &txt_cert); gnutls_x509_crt_deinit(crt); printf("%s\n", txt_cert.data); return 0; }