Support decrypting a server CA certificate so we can get its expiration (valid-before...
[moonshot-ui.git] / src / moonshot-crypto-utils.c
1 #include <string.h>
2 #include <openssl/bio.h>
3 #include <openssl/pem.h>
4
5
6 char* get_cert_valid_before(const char* cert_string, int cert_string_len, char* datebuf, int len)
7 {
8     datebuf[0]='\0';    
9
10     BIO* cert_bio = BIO_new_mem_buf(cert_string, cert_string_len);
11
12     if (cert_bio == NULL) {
13         return "Error calling PEM_new_mem_buf!";
14     }
15
16     X509 *x = PEM_read_bio_X509(cert_bio, NULL, 0, NULL);
17     if (x == NULL) {
18         return "Error calling PEM_read_bio_X509!";
19     }
20
21     BIO* out_bio = BIO_new(BIO_s_mem());
22     ASN1_TIME* time = X509_get_notAfter(x);
23
24     if (ASN1_TIME_print(out_bio, time)) {
25         int write = BIO_read(out_bio, datebuf, len - 1);
26         datebuf[write]='\0';
27     }
28
29     datebuf[len - 1] = '\0';
30     BIO_free(out_bio);
31     BIO_free(cert_bio);
32     X509_free(x);
33     return "";
34 }