[Pkg-javascript-commits] [node-asn1.js] 131/202: lib: `bmpstr`

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 4abc5675466fac8feeb6da270677cfe90e3612cb
Author: Aleksey <flegmatik.bel at gmail.com>
Date:   Wed Nov 25 21:20:12 2015 +0300

    lib: `bmpstr`
---
 lib/asn1/base/node.js    |  6 +++---
 lib/asn1/decoders/der.js | 14 ++++++++++++--
 lib/asn1/encoders/der.js |  7 +++++++
 test/der-decode-test.js  |  9 +++++++++
 test/der-encode-test.js  |  9 +++++++++
 test/error-test.js       |  4 ++++
 test/ping-pong-test.js   |  4 ++++
 7 files changed, 48 insertions(+), 5 deletions(-)

diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index 92344a9..b2275ab 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -5,7 +5,7 @@ var assert = require('minimalistic-assert');
 // Supported tags
 var tags = [
   'seq', 'seqof', 'set', 'setof', 'octstr', 'bitstr', 'objid', 'bool',
-  'gentime', 'utctime', 'null_', 'enum', 'int', 'ia5str', 'utf8str'
+  'gentime', 'utctime', 'null_', 'enum', 'int', 'ia5str', 'utf8str', 'bmpstr'
 ];
 
 // Public methods list
@@ -375,7 +375,7 @@ Node.prototype._decodeGeneric = function decodeGeneric(tag, input) {
     return this._decodeList(input, tag, state.args[0]);
   else if (tag === 'octstr' || tag === 'bitstr')
     return this._decodeStr(input, tag);
-  else if (tag === 'ia5str' || tag === 'utf8str')
+  else if (tag === 'ia5str' || tag === 'utf8str' || tag === 'bmpstr')
     return this._decodeStr(input, tag);
   else if (tag === 'objid' && state.args)
     return this._decodeObjid(input, state.args[0], state.args[1]);
@@ -579,7 +579,7 @@ Node.prototype._encodePrimitive = function encodePrimitive(tag, data) {
 
   if (tag === 'octstr' || tag === 'bitstr' || tag === 'ia5str')
     return this._encodeStr(data, tag);
-  else if (tag === 'utf8str')
+  else if (tag === 'utf8str' || tag === 'bmpstr')
     return this._encodeStr(data, tag);
   else if (tag === 'objid' && state.args)
     return this._encodeObjid(data, state.reverseArgs[0], state.args[1]);
diff --git a/lib/asn1/decoders/der.js b/lib/asn1/decoders/der.js
index 7df8509..18f87e0 100644
--- a/lib/asn1/decoders/der.js
+++ b/lib/asn1/decoders/der.js
@@ -133,8 +133,18 @@ 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 === 'bmpstr') {
+    var raw = buffer.raw();
+    if (raw.length % 2 === 1)
+      return buffer.error('Decoding of string type: bmpstr length mismatch');
+
+    var str = '';
+    for (var i = 0; i < raw.length / 2; i++) {
+      str += String.fromCharCode(raw.readUInt16BE(i * 2));
+    }
+    return str;
   } else {
-    return this.error('Decoding of string type: ' + tag + ' unsupported');
+    return buffer.error('Decoding of string type: ' + tag + ' unsupported');
   }
 };
 
@@ -188,7 +198,7 @@ DERNode.prototype._decodeTime = function decodeTime(buffer, tag) {
     else
       year = 1900 + year;
   } else {
-    return this.error('Decoding ' + tag + ' time is not supported yet');
+    return buffer.error('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 f954603..05959d3 100644
--- a/lib/asn1/encoders/der.js
+++ b/lib/asn1/encoders/der.js
@@ -67,6 +67,13 @@ DERNode.prototype._encodeStr = function encodeStr(str, tag) {
     return this._createEncoderBuffer([ str.unused | 0, str.data ]);
   else if (tag === 'ia5str' || tag === 'utf8str')
     return this._createEncoderBuffer(str);
+  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);
+  }
   return this.reporter.error('Encoding of string type: ' + tag +
                              ' unsupported');
 };
diff --git a/test/der-decode-test.js b/test/der-decode-test.js
index a43033b..277ad36 100644
--- a/test/der-decode-test.js
+++ b/test/der-decode-test.js
@@ -75,4 +75,13 @@ describe('asn1.js DER decoder', function() {
       this.key('key').bool()
     );
   }, '30800101ff0000', { 'key': true });
+
+  test('should decode bmpstr', function() {
+    this.bmpstr();
+  }, '1e26004300650072007400690066006900630061' +
+     '0074006500540065006d0070006c006100740065', 'CertificateTemplate');
+
+  test('should decode bmpstr with cyrillic chars', function() {
+    this.bmpstr();
+  }, '1e0c041f04400438043204350442', 'Привет');
 });
diff --git a/test/der-encode-test.js b/test/der-encode-test.js
index e198037..0700867 100644
--- a/test/der-encode-test.js
+++ b/test/der-encode-test.js
@@ -91,4 +91,13 @@ describe('asn1.js DER encoder', function() {
   test('should properly encode objid as array of strings', function() {
     this.objid();
   }, '1.2.398.3.10.1.1.1.2.2'.split('.'), '060a2a830e030a0101010202');
+
+  test('should properly encode bmpstr', function() {
+    this.bmpstr();
+  }, 'CertificateTemplate', '1e26004300650072007400690066006900630061' +
+                            '0074006500540065006d0070006c006100740065');
+
+  test('should properly encode bmpstr with cyrillic chars', function() {
+    this.bmpstr();
+  }, 'Привет', '1e0c041f04400438043204350442');
 });
diff --git a/test/error-test.js b/test/error-test.js
index 8bda36a..5e62d92 100644
--- a/test/error-test.js
+++ b/test/error-test.js
@@ -122,6 +122,10 @@ describe('asn1.js error', function() {
       test('int', function() {
         this.int();
       }, '', /tag of "int"/);
+
+      test('bmpstr invalid length', function() {
+        this.bmpstr();
+      }, '1e0b041f04400438043204350442', /bmpstr length mismatch/);
     });
 
     describe('composite', function() {
diff --git a/test/ping-pong-test.js b/test/ping-pong-test.js
index 874629b..1f8eafb 100644
--- a/test/ping-pong-test.js
+++ b/test/ping-pong-test.js
@@ -41,6 +41,10 @@ describe('asn1.js ping/pong', function() {
       this.utf8str();
     }, 'hello');
 
+    test('bmpstr', function() {
+      this.bmpstr();
+    }, '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