]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ssl: add notBefore and notAfter utility functions
authorWilliam Lallemand <wlallemand@haproxy.com>
Fri, 6 Dec 2024 16:42:19 +0000 (17:42 +0100)
committerWilliam Lallemand <wlallemand@haproxy.com>
Mon, 9 Dec 2024 17:29:23 +0000 (18:29 +0100)
Extracting notBefore and notAfter as a string can be bothersome,
add 2 utility functions that returns the value in a static buffer.

include/haproxy/ssl_utils.h
src/ssl_utils.c

index 3391efd38bdfcf5b527d9e76990405df3b5505d0..74426e0293848d299764828457279d34b2deacc2 100644 (file)
@@ -45,6 +45,8 @@ void exclude_tls_grease(char *input, int len, struct buffer *output);
 int x509_v_err_str_to_int(const char *str);
 const char *x509_v_err_int_to_str(int code);
 long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d);
+const char *x509_get_notbefore(X509 *cert);
+const char *x509_get_notafter(X509 *cert);
 
 #endif /* _HAPROXY_SSL_UTILS_H */
 #endif /* USE_OPENSSL */
index 4a85b89187ea05b71a29a5e9fce5a823a4056ca7..dfa069dd3e3f1e5fed45ded1b220fdf91d39a993 100644 (file)
@@ -700,3 +700,56 @@ nosec:
 
        return -1;
 }
+
+/* Return the nofAfter value as as string extracted from an X509 certificate
+ * The returned buffer is static and thread local.
+ */
+const char *x509_get_notafter(X509 *cert)
+{
+       BIO *bio = NULL;
+       int write;
+       static THREAD_LOCAL char buf[256];
+
+       memset(buf, 0, sizeof(buf));
+
+       if ((bio = BIO_new(BIO_s_mem())) ==  NULL)
+               goto end;
+       if (ASN1_TIME_print(bio, X509_getm_notAfter(cert)) == 0)
+               goto end;
+       write = BIO_read(bio, buf, sizeof(buf)-1);
+       buf[write] = '\0';
+       BIO_free(bio);
+
+       return buf;
+
+end:
+       BIO_free(bio);
+       return NULL;
+}
+
+/* Return the nofBefore value as as string extracted from an X509 certificate
+ * The returned buffer is static and thread local.
+ */
+const char *x509_get_notbefore(X509 *cert)
+{
+       BIO *bio = NULL;
+       int write;
+       static THREAD_LOCAL char buf[256];
+
+       memset(buf, 0, sizeof(buf));
+
+       if ((bio = BIO_new(BIO_s_mem())) ==  NULL)
+               goto end;
+       if (ASN1_TIME_print(bio, X509_getm_notBefore(cert)) == 0)
+               goto end;
+       write = BIO_read(bio, buf, sizeof(buf)-1);
+       buf[write] = '\0';
+       BIO_free(bio);
+
+       return buf;
+
+end:
+       BIO_free(bio);
+       return NULL;
+}
+