Skip to content

Commit 11c2f8b

Browse files
committed
[api] Added notBefore and notAfter properties to crypto.Certificate
1 parent b36b28d commit 11c2f8b

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/api/crypto.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242

4343
#include <stdexcept>
4444

45+
#include <ctime>
46+
4547
namespace pipy {
4648
namespace crypto {
4749

@@ -495,6 +497,20 @@ auto Certificate::subject_alt_names() -> pjs::Array* {
495497
return m_subject_alt_names;
496498
}
497499

500+
auto Certificate::not_before() -> double {
501+
std::tm tm;
502+
auto t = X509_get0_notBefore(m_x509);
503+
ASN1_TIME_to_tm(t, &tm);
504+
return (double)std::mktime(&tm) * 1000;
505+
}
506+
507+
auto Certificate::not_after() -> double {
508+
std::tm tm;
509+
auto t = X509_get0_notAfter(m_x509);
510+
ASN1_TIME_to_tm(t, &tm);
511+
return (double)std::mktime(&tm) * 1000;
512+
}
513+
498514
auto Certificate::read_pem(const void *data, size_t size) -> X509* {
499515
auto bio = BIO_new_mem_buf(data, size);
500516
auto x509 = PEM_read_bio_X509(bio, nullptr, nullptr, nullptr);
@@ -1417,6 +1433,8 @@ template<> void ClassDef<Certificate>::init() {
14171433
accessor("issuer", [](Object *obj, Value &ret) { ret.set(obj->as<Certificate>()->issuer()); });
14181434
accessor("subject", [](Object *obj, Value &ret) { ret.set(obj->as<Certificate>()->subject()); });
14191435
accessor("subjectAltNames", [](Object *obj, Value &ret) { ret.set(obj->as<Certificate>()->subject_alt_names()); });
1436+
accessor("notBefore", [](Object *obj, Value &ret) { ret.set(obj->as<Certificate>()->not_before()); });
1437+
accessor("notAfter", [](Object *obj, Value &ret) { ret.set(obj->as<Certificate>()->not_after()); });
14201438
}
14211439

14221440
template<> void ClassDef<Constructor<Certificate>>::init() {

src/api/crypto.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ class Certificate : public pjs::ObjectTemplate<Certificate> {
147147
auto issuer() -> pjs::Object*;
148148
auto subject() -> pjs::Object*;
149149
auto subject_alt_names() -> pjs::Array*;
150+
auto not_before() -> double;
151+
auto not_after() -> double;
150152

151153
private:
152154
Certificate(X509 *x509);

0 commit comments

Comments
 (0)