-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp.c
82 lines (68 loc) · 1.47 KB
/
exp.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Jonathan's certificate validation tool
*
* Exit code indicates if current system date is within certificate notBefore and notAfter.
*
* Usage: exp <path/to/cert/file.pem>
*
* Example:
*
* $ ./exp example.crt
* $ echo $?
* 0
* $
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/asn1.h>
#include <time.h>
#define PROGNAME "exp"
void exp_usage(void) {
printf("Certificate Validation Tool\n");
printf("Usage: %s <path/to/cert/file>\n", PROGNAME);
}
int main(int argc, char *argv[]) {
FILE *fp;
char *certname;
X509 *cert;
const ASN1_TIME *not_after, *not_before;
int result;
if (argc == 1) {
exp_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 2;
}
/* 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 2;
}
/* Get expiration date */
not_after = X509_get_notAfter(cert);
result = X509_cmp_current_time(not_after);
X509_free(cert);
fclose(fp);
switch (result) {
case 0:
/* Some other error */
return 2;
case -1:
/* Certificate expired */
return 1;
default:
/* Certificate hasn't expired */
return 0;
}
return 0;
}