Decode the CA Certificate from binary, not from PEM format.
[moonshot-ui.git] / src / moonshot-crypto-utils.c
1 #include <string.h>
2 #include <openssl/bio.h>
3 #include <openssl/pem.h>
4
5 #include <stdio.h>
6
7 char* get_cert_valid_before(const unsigned char* buf, int len, char* datebuf, int datebuf_len)
8 {
9     datebuf[0]='\0';
10
11     unsigned char *p = (unsigned char*) buf;
12     X509* x = d2i_X509(NULL, &p, len);
13     if (x == NULL) {
14         return "Error calling d2i_X509()!";
15     }
16
17     BIO* out_bio = BIO_new(BIO_s_mem());
18     ASN1_TIME* time = X509_get_notAfter(x);
19
20     if (ASN1_TIME_print(out_bio, time)) {
21         int write = BIO_read(out_bio, datebuf, datebuf_len - 1);
22         datebuf[write]='\0';
23     }
24
25     datebuf[datebuf_len - 1] = '\0';
26     BIO_free(out_bio);
27     X509_free(x);
28     return "";
29 }