[Pkg-javascript-commits] [node-asn1.js] 46/202: Properly encode IMPLICIT tag

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:18:52 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 27b28f418b3499b5068a8d07a3e18dd03c1a1fab
Author: Ilya Petrov <ilya.muromec at gmail.com>
Date:   Thu Aug 7 22:50:48 2014 +0300

    Properly encode IMPLICIT tag
---
 lib/asn1/base/node.js |  5 +++--
 test/use-test.js      | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/lib/asn1/base/node.js b/lib/asn1/base/node.js
index 7a14cad..25943b6 100644
--- a/lib/asn1/base/node.js
+++ b/lib/asn1/base/node.js
@@ -489,7 +489,7 @@ Node.prototype._encode = function encode(data, reporter) {
           return this._getUse(state.args[0])._encode(item, reporter);
         }, this));
       } else if (state.use !== null) {
-        result = this._getUse(state.use)._encode(data, reporter);
+        return this._getUse(state.use)._encode(data, reporter);
       } else {
         content = this._encodePrimitive(state.tag, data);
         primitive = true;
@@ -503,12 +503,13 @@ Node.prototype._encode = function encode(data, reporter) {
   var result;
   if (!state.any && state.choice === null) {
     var tag = state.implicit !== null ? state.implicit : state.tag;
+    var cls = state.implicit === null ? 'universal' : 'context';
 
     if (tag === null) {
       if (state.use === null)
         reporter.error('Tag could be ommited only for .use()');
     } else {
-      result = this._encodeComposite(tag, primitive, 'universal', content);
+      result = this._encodeComposite(tag, primitive, cls, content);
     }
   }
 
diff --git a/test/use-test.js b/test/use-test.js
new file mode 100644
index 0000000..8827707
--- /dev/null
+++ b/test/use-test.js
@@ -0,0 +1,50 @@
+var assert = require('assert');
+var asn1 = require('..');
+
+var Buffer = require('buffer').Buffer;
+
+describe('asn1.js models', function() {
+  describe('plain use', function() {
+    it('should encode submodel', function() {
+      var SubModel = asn1.define('SubModel', function() {
+        this.seq().obj(
+          this.key('b').octstr()
+        );
+      });
+      var Model = asn1.define('Model', function() {
+        this.seq().obj(
+          this.key('a').int(),
+          this.key('sub').use(SubModel)
+        );
+      });
+
+      var data = {a: 1, sub: {b: new Buffer("XXX")}};
+      var wire = Model.encode(data, 'der');
+      assert.equal(wire.toString('hex'), '300a02010130050403585858');
+      var back = Model.decode(wire, 'der');
+      assert.deepEqual(back, data);
+    });
+
+    it('should honour implicit tag from parent', function() {
+      var SubModel = asn1.define('SubModel', function() {
+        this.seq().obj(
+          this.key('x').octstr()
+        )
+      });
+      var Model = asn1.define('Model', function() {
+        this.seq().obj(
+          this.key('a').int(),
+          this.key('sub').use(SubModel).implicit(0)
+        );
+      });
+
+      var data = {a: 1, sub: {x: new Buffer("123")}};
+      var wire = Model.encode(data, 'der');
+      assert.equal(wire.toString('hex'), '300a020101a0050403313233');
+      var back = Model.decode(wire, 'der');
+      assert.deepEqual(back, data);
+
+    });
+  });
+});
+

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