[Pkg-javascript-commits] [node-asn1.js] 133/202: lib: `numstr` and `printstr`

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:19:01 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 3e1c3d60014b3bfe7ae826c80b6f2f243556c2e9
Author: Aleksey <flegmatik.bel at gmail.com>
Date:   Sat Nov 28 20:07:05 2015 +0300

    lib: `numstr` and `printstr`
---
 lib/asn1/base/node.js    | 15 ++++++++++++++-
 lib/asn1/decoders/der.js | 16 ++++++++++++++++
 lib/asn1/encoders/der.js | 31 +++++++++++++++++++++++++------
 test/error-test.js       | 16 ++++++++++++++++
 test/ping-pong-test.js   |  8 ++++++++
 5 files changed, 79 insertions(+), 7 deletions(-)

diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index b2275ab..057bf7d 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -5,7 +5,8 @@ var assert = require('minimalistic-assert');
 // Supported tags
 var tags = [
   'seq', 'seqof', 'set', 'setof', 'octstr', 'bitstr', 'objid', 'bool',
-  'gentime', 'utctime', 'null_', 'enum', 'int', 'ia5str', 'utf8str', 'bmpstr'
+  'gentime', 'utctime', 'null_', 'enum', 'int', 'ia5str', 'utf8str', 'bmpstr',
+  'numstr', 'printstr'
 ];
 
 // Public methods list
@@ -377,6 +378,8 @@ Node.prototype._decodeGeneric = function decodeGeneric(tag, input) {
     return this._decodeStr(input, tag);
   else if (tag === 'ia5str' || tag === 'utf8str' || tag === 'bmpstr')
     return this._decodeStr(input, tag);
+  else if (tag === 'numstr' || tag === 'printstr')
+    return this._decodeStr(input, tag);
   else if (tag === 'objid' && state.args)
     return this._decodeObjid(input, state.args[0], state.args[1]);
   else if (tag === 'objid')
@@ -581,6 +584,8 @@ Node.prototype._encodePrimitive = function encodePrimitive(tag, data) {
     return this._encodeStr(data, tag);
   else if (tag === 'utf8str' || tag === 'bmpstr')
     return this._encodeStr(data, tag);
+  else if (tag === 'numstr' || tag === 'printstr')
+    return this._encodeStr(data, tag);
   else if (tag === 'objid' && state.args)
     return this._encodeObjid(data, state.reverseArgs[0], state.args[1]);
   else if (tag === 'objid')
@@ -596,3 +601,11 @@ Node.prototype._encodePrimitive = function encodePrimitive(tag, data) {
   else
     throw new Error('Unsupported tag: ' + tag);
 };
+
+Node.prototype._isNumstr = function isNumstr(str) {
+  return /^[1-9 ]*$/.test(str);
+};
+
+Node.prototype._isPrintstr = function isPrintstr(str) {
+  return /^[A-Za-z0-9 '\(\)\+,\-\.\/:=\?]*$/.test(str);
+};
\ No newline at end of file
diff --git a/lib/asn1/decoders/der.js b/lib/asn1/decoders/der.js
index 18f87e0..9c420ef 100644
--- a/lib/asn1/decoders/der.js
+++ b/lib/asn1/decoders/der.js
@@ -133,6 +133,22 @@ DERNode.prototype._decodeStr = function decodeStr(buffer, tag) {
     return { unused: unused, data: buffer.raw() };
   } else if (tag === 'ia5str' || tag === 'utf8str') {
     return buffer.raw().toString();
+  } else if(tag === 'numstr') {
+    var numstr = buffer.raw().toString('ascii');
+    if (!this._isNumstr(numstr)) {
+      return buffer.error('Decoding of string type: ' +
+                          'numstr unsupported characters');
+    }
+
+    return numstr;
+  } else if (tag === 'printstr') {
+    var printstr = buffer.raw().toString('ascii');
+    if (!this._isPrintstr(printstr)) {
+      return buffer.error('Decoding of string type: ' +
+                          'printstr unsupported characters');
+    }
+
+    return printstr;
   } else if(tag === 'bmpstr') {
     var raw = buffer.raw();
     if (raw.length % 2 === 1)
diff --git a/lib/asn1/encoders/der.js b/lib/asn1/encoders/der.js
index 05959d3..ec6f833 100644
--- a/lib/asn1/encoders/der.js
+++ b/lib/asn1/encoders/der.js
@@ -61,21 +61,40 @@ DERNode.prototype._encodeComposite = function encodeComposite(tag,
 };
 
 DERNode.prototype._encodeStr = function encodeStr(str, tag) {
-  if (tag === 'octstr')
+  if (tag === 'octstr') {
     return this._createEncoderBuffer(str);
-  else if (tag === 'bitstr')
+  } else if (tag === 'bitstr') {
     return this._createEncoderBuffer([ str.unused | 0, str.data ]);
-  else if (tag === 'ia5str' || tag === 'utf8str')
+  } else if (tag === 'ia5str' || tag === 'utf8str') {
     return this._createEncoderBuffer(str);
-  else if (tag === 'bmpstr') {
+  } else if (tag === 'bmpstr') {
     var buf = new Buffer(str.length * 2);
     for (var i = 0; i < str.length; i++) {
       buf.writeUInt16BE(str.charCodeAt(i), i * 2);
     }
     return this._createEncoderBuffer(buf);
+  } else if (tag === 'numstr') {
+    if (!this._isNumstr(str)) {
+      return this.reporter.error('Encoding of string type: numstr supports ' +
+                                 'only digits and space');
+    }
+
+    return this._createEncoderBuffer(str);
+  } else if (tag === 'printstr') {
+    if (!this._isPrintstr(str)) {
+      return this.reporter.error('Encoding of string type: printstr supports ' +
+                                 'only latin upper and lower case letters, ' +
+                                 'digits, space, apostrophe, left and rigth ' +
+                                 'parenthesis, plus sign, comma, hyphen, ' +
+                                 'dot, slash, colon, equal sign, ' +
+                                 'question mark');
+    }
+
+    return this._createEncoderBuffer(str);
+  } else {
+    return this.reporter.error('Encoding of string type: ' + tag +
+                               ' unsupported');
   }
-  return this.reporter.error('Encoding of string type: ' + tag +
-                             ' unsupported');
 };
 
 DERNode.prototype._encodeObjid = function encodeObjid(id, values, relative) {
diff --git a/test/error-test.js b/test/error-test.js
index 5e62d92..9892905 100644
--- a/test/error-test.js
+++ b/test/error-test.js
@@ -40,6 +40,14 @@ describe('asn1.js error', function() {
       test('objid', function() {
         this.objid();
       }, 1, /objid\(\) should be either array or string, got: 1/);
+
+      test('numstr', function() {
+        this.numstr();
+      }, 'hello', /only digits and space/);
+
+      test('printstr', function() {
+        this.printstr();
+      }, 'hello!', /only latin upper and lower case letters/);
     });
 
     describe('composite', function() {
@@ -126,6 +134,14 @@ describe('asn1.js error', function() {
       test('bmpstr invalid length', function() {
         this.bmpstr();
       }, '1e0b041f04400438043204350442', /bmpstr length mismatch/);
+
+      test('numstr unsupported characters', function() {
+        this.numstr();
+      }, '12024141', /numstr unsupported characters/);
+
+      test('printstr unsupported characters', function() {
+        this.printstr();
+      }, '13024121', /printstr unsupported characters/);
     });
 
     describe('composite', function() {
diff --git a/test/ping-pong-test.js b/test/ping-pong-test.js
index 1f8eafb..709a9a7 100644
--- a/test/ping-pong-test.js
+++ b/test/ping-pong-test.js
@@ -45,6 +45,14 @@ describe('asn1.js ping/pong', function() {
       this.bmpstr();
     }, 'hello');
 
+    test('numstr', function() {
+      this.numstr();
+    }, '1234 5678');
+
+    test('printstr', function() {
+      this.printstr();
+    }, 'hello');
+
     test('gentime', function() {
       this.gentime();
     }, 1385921175000);

-- 
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