From a595d683a96de006cdd31663697ca010a176f0dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Obernd=C3=B6rfer?= Date: Wed, 5 Feb 2014 18:09:54 +0100 Subject: [PATCH 1/2] Add getExpirationTime method to Key and SubKey --- src/key.js | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/key.js b/src/key.js index 9bf95f89..66625a47 100644 --- a/src/key.js +++ b/src/key.js @@ -440,12 +440,42 @@ Key.prototype.verifyPrimaryKey = function() { } // check V4 expiration time if (this.primaryKey.version == 4 && primaryUser.selfCertificate.keyNeverExpires === false && - Date.now() > (primaryUser.selfCertificate.created.getTime() + primaryUser.selfCertificate.keyExpirationTime*1000)) { + Date.now() > (this.primaryKey.created.getTime() + primaryUser.selfCertificate.keyExpirationTime*1000)) { return enums.keyStatus.expired; } return enums.keyStatus.valid; }; +/** + * Returns the expiration time of the primary key or null if key does not expire + * @return {Date|null} + */ +Key.prototype.getExpirationTime = function() { + if (this.primaryKey.version == 3) { + return getExpirationTime(this.primaryKey); + } + if (this.primaryKey.version == 4) { + var primaryUser = this.getPrimaryUser(); + if (!primaryUser) { + return null; + } + return getExpirationTime(this.primaryKey, primaryUser.selfCertificate); + } +}; + + +function getExpirationTime(keyPacket, selfCertificate) { + // check V3 expiration time + if (keyPacket.version == 3 && keyPacket.expirationTimeV3 !== 0) { + return new Date(keyPacket.created.getTime() + keyPacket.expirationTimeV3*24*3600*1000); + } + // check V4 expiration time + if (this.primaryKey.version == 4 && selfCertificate.keyNeverExpires === false) { + return new Date(keyPacket.created.getTime() + selfCertificate.keyExpirationTime*1000); + } + return null; +}; + /** * Returns primary user and most significant (latest valid) self signature * - if multiple users are marked as primary users returns the one with the latest self signature @@ -668,6 +698,14 @@ SubKey.prototype.verify = function(primaryKey) { return enums.keyStatus.valid; }; +/** + * Returns the expiration time of the subkey or null if key does not expire + * @return {Date|null} + */ +SubKey.prototype.getExpirationTime = function() { + return getExpirationTime(this.subKey, this.bindingSignature); +}; + /** * Reads an OpenPGP armored text and returns one or multiple key objects * @param {String} armoredText text to be parsed From b1e8c9ec52c57eb32c817da278cca96ab50d46a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Obernd=C3=B6rfer?= Date: Thu, 6 Feb 2014 12:28:36 +0100 Subject: [PATCH 2/2] Add unit tests for getExpirationTime Key method --- src/key.js | 2 +- test/general/key.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/key.js b/src/key.js index 66625a47..85cf5ea1 100644 --- a/src/key.js +++ b/src/key.js @@ -470,7 +470,7 @@ function getExpirationTime(keyPacket, selfCertificate) { return new Date(keyPacket.created.getTime() + keyPacket.expirationTimeV3*24*3600*1000); } // check V4 expiration time - if (this.primaryKey.version == 4 && selfCertificate.keyNeverExpires === false) { + if (keyPacket.version == 4 && selfCertificate.keyNeverExpires === false) { return new Date(keyPacket.created.getTime() + selfCertificate.keyExpirationTime*1000); } return null; diff --git a/test/general/key.js b/test/general/key.js index 75657025..16d9757c 100644 --- a/test/general/key.js +++ b/test/general/key.js @@ -293,5 +293,20 @@ describe('Key', function() { expect(status).to.equal(openpgp.enums.keyStatus.revoked); done(); }); + + it('Method getExpirationTime V4 Key', function() { + var pubKey = openpgp.key.readArmored(twoKeys).keys[1]; + expect(pubKey).to.exist; + expect(pubKey).to.be.an.instanceof(openpgp.key.Key); + expect(pubKey.getExpirationTime().toISOString()).to.be.equal('2018-11-26T10:58:29.000Z'); + }); + + it('Method getExpirationTime V4 SubKey', function() { + var pubKey = openpgp.key.readArmored(twoKeys).keys[1]; + expect(pubKey).to.exist; + expect(pubKey).to.be.an.instanceof(openpgp.key.Key); + expect(pubKey.subKeys[0].getExpirationTime().toISOString()).to.be.equal('2018-11-26T10:58:29.000Z'); + }); + });