Skip to content

Commit 82d830e

Browse files
committed
crypto: add isValid field to the X509Certificate API
Added the `isValid` field, which checks the certificate's validity based on the current time.
1 parent 00c0644 commit 82d830e

7 files changed

Lines changed: 40 additions & 0 deletions

File tree

doc/api/crypto.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,12 @@ added: v15.6.0
28552855

28562856
Returns the PEM-encoded certificate.
28572857

2858+
### `x509.isValid`
2859+
2860+
* Type: {boolean}
2861+
2862+
Returns `true` if the certificate is valid based on the current time.
2863+
28582864
### `x509.validFrom`
28592865

28602866
<!-- YAML

lib/internal/crypto/x509.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class X509Certificate {
133133
subjectAltName: this.subjectAltName,
134134
issuer: this.issuer,
135135
infoAccess: this.infoAccess,
136+
isValid: this.isValid,
136137
validFrom: this.validFrom,
137138
validTo: this.validTo,
138139
fingerprint: this.fingerprint,
@@ -202,6 +203,15 @@ class X509Certificate {
202203
return value;
203204
}
204205

206+
get isValid() {
207+
let value = this[kInternalState].get('isValid');
208+
if (value === undefined) {
209+
value = this[kHandle].isValid();
210+
this[kInternalState].set('isValid', value);
211+
}
212+
return value;
213+
}
214+
205215
get validFrom() {
206216
let value = this[kInternalState].get('validFrom');
207217
if (value === undefined) {

src/crypto/crypto_common.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,18 @@ MaybeLocal<Value> GetFingerprintDigest(
564564
return Undefined(env->isolate());
565565
}
566566

567+
MaybeLocal<Value> GetIsValid(
568+
Environment* env,
569+
X509* cert) {
570+
const ASN1_TIME* not_before = X509_get0_notBefore(cert);
571+
const ASN1_TIME* not_after = X509_get0_notAfter(cert);
572+
573+
int is_valid = X509_cmp_timeframe(NULL, not_before, not_after);
574+
575+
return Boolean::New(env->isolate(), is_valid == 0 ? true : false);
576+
}
577+
578+
567579
MaybeLocal<Value> GetValidTo(
568580
Environment* env,
569581
X509* cert,

src/crypto/crypto_common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ v8::MaybeLocal<v8::Object> X509ToObject(
103103
Environment* env,
104104
X509* cert);
105105

106+
v8::MaybeLocal<v8::Value> GetIsValid(
107+
Environment* env,
108+
X509* cert
109+
);
110+
106111
v8::MaybeLocal<v8::Value> GetValidTo(
107112
Environment* env,
108113
X509* cert,

src/crypto/crypto_x509.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Local<FunctionTemplate> X509Certificate::GetConstructorTemplate(
7777
SetProtoMethod(isolate, tmpl, "subjectAltName", SubjectAltName);
7878
SetProtoMethod(isolate, tmpl, "infoAccess", InfoAccess);
7979
SetProtoMethod(isolate, tmpl, "issuer", Issuer);
80+
SetProtoMethod(isolate, tmpl, "isValid", IsValid);
8081
SetProtoMethod(isolate, tmpl, "validTo", ValidTo);
8182
SetProtoMethod(isolate, tmpl, "validFrom", ValidFrom);
8283
SetProtoMethod(isolate, tmpl, "fingerprint", Fingerprint<EVP_sha1>);
@@ -249,6 +250,10 @@ static void ReturnProperty(const FunctionCallbackInfo<Value>& args) {
249250
if (Property(env, cert->get()).ToLocal(&ret)) args.GetReturnValue().Set(ret);
250251
}
251252

253+
void X509Certificate::IsValid(const FunctionCallbackInfo<Value>& args) {
254+
ReturnProperty<GetIsValid>(args);
255+
}
256+
252257
void X509Certificate::KeyUsage(const FunctionCallbackInfo<Value>& args) {
253258
ReturnProperty<GetKeyUsage>(args);
254259
}

src/crypto/crypto_x509.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class X509Certificate : public BaseObject {
7777
static void SubjectAltName(const v8::FunctionCallbackInfo<v8::Value>& args);
7878
static void Issuer(const v8::FunctionCallbackInfo<v8::Value>& args);
7979
static void InfoAccess(const v8::FunctionCallbackInfo<v8::Value>& args);
80+
static void IsValid(const v8::FunctionCallbackInfo<v8::Value>& args);
8081
static void ValidFrom(const v8::FunctionCallbackInfo<v8::Value>& args);
8182
static void ValidTo(const v8::FunctionCallbackInfo<v8::Value>& args);
8283
static void KeyUsage(const v8::FunctionCallbackInfo<v8::Value>& args);

test/parallel/test-crypto-x509.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const der = Buffer.from(
9494
assert.strictEqual(x509.subjectAltName, undefined);
9595
assert.strictEqual(x509.issuer, issuerCheck);
9696
assert.strictEqual(x509.infoAccess, infoAccessCheck);
97+
assert.strictEqual(x509.isValid, true);
9798
assert.strictEqual(x509.validFrom, 'Sep 3 21:40:37 2022 GMT');
9899
assert.strictEqual(x509.validTo, 'Jun 17 21:40:37 2296 GMT');
99100
assert.strictEqual(

0 commit comments

Comments
 (0)