We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 00c0644 commit 82d830eCopy full SHA for 82d830e
7 files changed
doc/api/crypto.md
@@ -2855,6 +2855,12 @@ added: v15.6.0
2855
2856
Returns the PEM-encoded certificate.
2857
2858
+### `x509.isValid`
2859
+
2860
+* Type: {boolean}
2861
2862
+Returns `true` if the certificate is valid based on the current time.
2863
2864
### `x509.validFrom`
2865
2866
<!-- YAML
lib/internal/crypto/x509.js
@@ -133,6 +133,7 @@ class X509Certificate {
133
subjectAltName: this.subjectAltName,
134
issuer: this.issuer,
135
infoAccess: this.infoAccess,
136
+ isValid: this.isValid,
137
validFrom: this.validFrom,
138
validTo: this.validTo,
139
fingerprint: this.fingerprint,
@@ -202,6 +203,15 @@ class X509Certificate {
202
203
return value;
204
}
205
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
215
get validFrom() {
216
let value = this[kInternalState].get('validFrom');
217
if (value === undefined) {
src/crypto/crypto_common.cc
@@ -564,6 +564,18 @@ MaybeLocal<Value> GetFingerprintDigest(
564
return Undefined(env->isolate());
565
566
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
579
MaybeLocal<Value> GetValidTo(
580
Environment* env,
581
X509* cert,
src/crypto/crypto_common.h
@@ -103,6 +103,11 @@ v8::MaybeLocal<v8::Object> X509ToObject(
103
104
X509* cert);
105
106
+v8::MaybeLocal<v8::Value> GetIsValid(
107
108
+ X509* cert
109
+);
110
111
v8::MaybeLocal<v8::Value> GetValidTo(
112
113
src/crypto/crypto_x509.cc
@@ -77,6 +77,7 @@ Local<FunctionTemplate> X509Certificate::GetConstructorTemplate(
77
SetProtoMethod(isolate, tmpl, "subjectAltName", SubjectAltName);
78
SetProtoMethod(isolate, tmpl, "infoAccess", InfoAccess);
79
SetProtoMethod(isolate, tmpl, "issuer", Issuer);
80
+ SetProtoMethod(isolate, tmpl, "isValid", IsValid);
81
SetProtoMethod(isolate, tmpl, "validTo", ValidTo);
82
SetProtoMethod(isolate, tmpl, "validFrom", ValidFrom);
83
SetProtoMethod(isolate, tmpl, "fingerprint", Fingerprint<EVP_sha1>);
@@ -249,6 +250,10 @@ static void ReturnProperty(const FunctionCallbackInfo<Value>& args) {
249
250
if (Property(env, cert->get()).ToLocal(&ret)) args.GetReturnValue().Set(ret);
251
252
253
+void X509Certificate::IsValid(const FunctionCallbackInfo<Value>& args) {
254
+ ReturnProperty<GetIsValid>(args);
255
256
257
void X509Certificate::KeyUsage(const FunctionCallbackInfo<Value>& args) {
258
ReturnProperty<GetKeyUsage>(args);
259
src/crypto/crypto_x509.h
@@ -77,6 +77,7 @@ class X509Certificate : public BaseObject {
static void SubjectAltName(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Issuer(const v8::FunctionCallbackInfo<v8::Value>& args);
static void InfoAccess(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void IsValid(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ValidFrom(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ValidTo(const v8::FunctionCallbackInfo<v8::Value>& args);
static void KeyUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
test/parallel/test-crypto-x509.js
@@ -94,6 +94,7 @@ const der = Buffer.from(
94
assert.strictEqual(x509.subjectAltName, undefined);
95
assert.strictEqual(x509.issuer, issuerCheck);
96
assert.strictEqual(x509.infoAccess, infoAccessCheck);
97
+ assert.strictEqual(x509.isValid, true);
98
assert.strictEqual(x509.validFrom, 'Sep 3 21:40:37 2022 GMT');
99
assert.strictEqual(x509.validTo, 'Jun 17 21:40:37 2296 GMT');
100
assert.strictEqual(
0 commit comments