diff --git a/deps/ncrypto/ncrypto.cc b/deps/ncrypto/ncrypto.cc index df5bd6f33c43c1..19a30bdbd653ee 100644 --- a/deps/ncrypto/ncrypto.cc +++ b/deps/ncrypto/ncrypto.cc @@ -346,6 +346,21 @@ int PasswordCallback(char* buf, int size, int rwflag, void* u) { return -1; } +// provided time must be in UTC +time_t PortableTimeGM(struct tm* t) { + struct tm tmp = *t; + tmp.tm_isdst = 0; + + time_t local_ver = mktime(&tmp); + + struct tm* utc_tm = gmtime(&local_ver); + time_t utc_ver = mktime(utc_tm); + + double seconds = difftime(local_ver, utc_ver); + + return local_ver - static_cast(seconds); +} + // ============================================================================ // SPKAC @@ -816,6 +831,18 @@ BIOPointer X509View::getValidTo() const { return bio; } +time_t X509View::getValidToTime() const { + struct tm tp; + ASN1_TIME_to_tm(X509_get0_notAfter(cert_), &tp); + return PortableTimeGM(&tp); +} + +time_t X509View::getValidFromTime() const { + struct tm tp; + ASN1_TIME_to_tm(X509_get0_notBefore(cert_), &tp); + return PortableTimeGM(&tp); +} + DataPointer X509View::getSerialNumber() const { ClearErrorOnReturn clearErrorOnReturn; if (cert_ == nullptr) return {}; diff --git a/deps/ncrypto/ncrypto.h b/deps/ncrypto/ncrypto.h index 50e86538edda7c..919da4947770cc 100644 --- a/deps/ncrypto/ncrypto.h +++ b/deps/ncrypto/ncrypto.h @@ -338,6 +338,8 @@ class X509View final { BIOPointer getInfoAccess() const; BIOPointer getValidFrom() const; BIOPointer getValidTo() const; + time_t getValidFromTime() const; + time_t getValidToTime() const; DataPointer getSerialNumber() const; Result getPublicKey() const; StackOfASN1 getKeyUsage() const; diff --git a/doc/api/crypto.md b/doc/api/crypto.md index 435d5d495609b3..7cccbaffc20cb8 100644 --- a/doc/api/crypto.md +++ b/doc/api/crypto.md @@ -2865,6 +2865,16 @@ added: v15.6.0 The date/time from which this certificate is valid. +### `x509.validFromDate` + + + +* Type: {Date} + +The date/time from which this certificate is valid, encapsulated in a `Date` object. + ### `x509.validTo` + +* Type: {Date} + +The date/time until which this certificate is valid, encapsulated in a `Date` object. + ### `x509.verify(publicKey)`