[Pkg-javascript-commits] [node-asn1.js] 12/202: der: support utctime
Bastien Roucariès
rouca at moszumanska.debian.org
Thu Apr 20 19:18:49 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-asn1.js.
commit 01bc3c77e3bb5154b99508c997cda9e9055c26f5
Author: Fedor Indutny <fedor.indutny at gmail.com>
Date: Sun Dec 1 23:18:48 2013 +0400
der: support utctime
---
lib/asn1/base/node.js | 2 +-
lib/asn1/decoders/der.js | 30 +++++++++++++++++++++---------
lib/asn1/encoders/der.js | 16 ++++++++++++----
test/ping-pong-test.js | 4 ++++
4 files changed, 38 insertions(+), 14 deletions(-)
diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index f4945b2..4a30f64 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -318,7 +318,7 @@ Node.prototype._decodeGeneric = function decodeGeneric(tag, input) {
return this._decodeObjid(input, state.args[0], state.args[1]);
else if (tag === 'objid')
return this._decodeObjid(input, null, null);
- else if (tag === 'gentime')
+ else if (tag === 'gentime' || tag === 'utctime')
return this._decodeTime(input, tag);
else if (tag === 'null_')
return this._decodeNull(input);
diff --git a/lib/asn1/decoders/der.js b/lib/asn1/decoders/der.js
index 852b140..26943a9 100644
--- a/lib/asn1/decoders/der.js
+++ b/lib/asn1/decoders/der.js
@@ -126,16 +126,28 @@ DERNode.prototype._decodeObjid = function decodeObjid(buffer, values, relative)
};
DERNode.prototype._decodeTime = function decodeTime(buffer, tag) {
- assert.equal(tag, 'gentime');
-
- // TODO(indutny): verify in spec
var str = buffer.raw().toString();
- var year = str.slice(0, 4) | 0;
- var mon = str.slice(4, 6) | 0;
- var day = str.slice(6, 8) | 0;
- var hour = str.slice(8, 10) | 0;
- var min = str.slice(10, 12) | 0;
- var sec = str.slice(12, 14) | 0;
+ if (tag === 'gentime') {
+ var year = str.slice(0, 4) | 0;
+ var mon = str.slice(4, 6) | 0;
+ var day = str.slice(6, 8) | 0;
+ var hour = str.slice(8, 10) | 0;
+ var min = str.slice(10, 12) | 0;
+ var sec = str.slice(12, 14) | 0;
+ } else if (tag === 'utctime') {
+ var year = str.slice(0, 2) | 0;
+ var mon = str.slice(2, 4) | 0;
+ var day = str.slice(4, 6) | 0;
+ var hour = str.slice(6, 8) | 0;
+ var min = str.slice(8, 10) | 0;
+ var sec = str.slice(10, 12) | 0;
+ if (year < 70)
+ year = 2000 + year;
+ else
+ year = 1900 + year;
+ } else {
+ assert(0, 'Decoding ' + tag + ' time is not supported yet');
+ }
return Date.UTC(year, mon - 1, day, hour, min, sec, 0);
};
diff --git a/lib/asn1/encoders/der.js b/lib/asn1/encoders/der.js
index bb5082e..54b4b8b 100644
--- a/lib/asn1/encoders/der.js
+++ b/lib/asn1/encoders/der.js
@@ -112,11 +112,9 @@ function two(num) {
DERNode.prototype._encodeTime = function encodeTime(time, tag) {
var str;
+ var date = new Date(time);
- // TODO(indutny): verify in spec
if (tag === 'gentime') {
- var date = new Date(time);
-
str = [
date.getFullYear(),
two(date.getUTCMonth() + 1),
@@ -126,8 +124,18 @@ DERNode.prototype._encodeTime = function encodeTime(time, tag) {
two(date.getUTCSeconds()),
'Z'
].join('');
+ } else if (tag === 'utctime') {
+ str = [
+ date.getFullYear() % 100,
+ two(date.getUTCMonth() + 1),
+ two(date.getUTCDate()),
+ two(date.getUTCHours()),
+ two(date.getUTCMinutes()),
+ two(date.getUTCSeconds()),
+ 'Z'
+ ].join('');
} else {
- assert(0, tag + ' time is not supported yet');
+ assert(0, 'Encoding ' + tag + ' time is not supported yet');
}
return this._encodeStr(str, 'octstr');
diff --git a/test/ping-pong-test.js b/test/ping-pong-test.js
index fb652c7..c5d0cfb 100644
--- a/test/ping-pong-test.js
+++ b/test/ping-pong-test.js
@@ -31,6 +31,10 @@ describe('asn1.js ping/pong', function() {
this.gentime();
}, 1385921175000);
+ test('utctime', function() {
+ this.utctime();
+ }, 1385921175000);
+
test('null', function() {
this.null_();
}, null);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-asn1.js.git
More information about the Pkg-javascript-commits
mailing list