/
main.c
63 lines (51 loc) · 1.44 KB
/
main.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
57
58
59
60
61
62
63
// Copyright (c) 2015 Alexander Teves <alexander.teves@gmail.com>
// Released under the terms of the MIT license
#define _XOPEN_SOURCE
#define BUFLEN 128
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <time.h>
int convertAsn1ToString(ASN1_TIME *notAfter, char buffer[]) {
BIO *bio = BIO_new(BIO_s_mem());
ASN1_TIME_print(bio, notAfter);
BIO_gets(bio, buffer, BUFLEN);
BIO_free(bio);
return 0;
}
int convertAsn1ToTimestamp(ASN1_TIME *notAfter, char buffer[]) {
convertAsn1ToString(notAfter, buffer);
struct tm timestruct;
memset(×truct, 0, sizeof(struct tm));
strptime(buffer, "%b %d %H:%M:%S %Y", ×truct);
strftime(buffer, BUFLEN, "%s", ×truct);
return 0;
}
int main(int argc, char* args[]) {
if(argc == 1) {
printf("Usage: %s [OPTIONS] [FILE]\n\n", args[0]);
printf("Options:\n\t-u\t Print UNIX timestamp\n\n");
return 1;
}
FILE *pemFile = fopen(args[argc-1], "r");
if(!pemFile) {
fprintf(stderr, "Could not open file \"%s\"\n", args[argc-1]);
return 1;
}
X509 *cert = PEM_read_X509(pemFile, NULL, NULL, NULL);
if(!cert) {
fprintf(stderr, "Could not read PEM format\n");
return 1;
}
char notAfterStr[BUFLEN];
ASN1_TIME *notAfter = X509_get_notAfter(cert);
if(strcmp(args[1], "-u") == 0) {
convertAsn1ToTimestamp(notAfter, notAfterStr);
} else {
convertAsn1ToString(notAfter, notAfterStr);
}
printf("%s\n", notAfterStr);
return 0;
}