-
Notifications
You must be signed in to change notification settings - Fork 0
/
pem.c
56 lines (44 loc) · 1013 Bytes
/
pem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* Jonathan's certificate output tool
*
* Usage: pem <path/to/cert/file.pem>
*
* Reads X.509 certificate and prints details to stdout, same
* as openssl x509 -text...
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/pem.h>
#define PROGNAME "pem"
void pem_usage(void) {
printf("Certificate Output Tool\n");
printf("Usage: %s <path/to/cert/file.pem>\n", PROGNAME);
}
int main(int argc, char *argv[]) {
FILE *fp;
char *certname;
X509 *cert;
if (argc == 1) {
pem_usage();
exit(EXIT_SUCCESS);
}
certname = argv[1];
/* Open file for reading */
fp = fopen(certname, "r");
if (fp == NULL) {
fprintf(stderr, "fopen: failed to open %s\n", certname);
return 1;
}
/* Read PEM content */
cert = PEM_read_X509(fp, NULL, NULL, NULL);
if (cert == NULL) {
fprintf(stderr, "PEM_read_X509: failed to open %s\n", certname);
fclose(fp);
return 1;
}
X509_print_fp(stdout, cert);
X509_free(cert);
fclose(fp);
return 0;
}