[Pkg-javascript-commits] [node-asn1.js] 111/202: lib: introduce PEM encoding support

Bastien Roucariès rouca at moszumanska.debian.org
Thu Apr 20 19:18:59 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 2ac0a3a42ecb639e93062df689455a476f076073
Author: Fedor Indutny <fedor at indutny.com>
Date:   Tue Jun 23 19:05:16 2015 -0700

    lib: introduce PEM encoding support
    
    Fix: #44
---
 lib/asn1/decoders/index.js |  1 +
 lib/asn1/decoders/pem.js   | 50 ++++++++++++++++++++++++++++++++++++++++++
 lib/asn1/encoders/index.js |  1 +
 lib/asn1/encoders/pem.js   | 23 ++++++++++++++++++++
 test/pem-test.js           | 54 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 129 insertions(+)

diff --git a/lib/asn1/decoders/index.js b/lib/asn1/decoders/index.js
index 7f431ca..e2583aa 100644
--- a/lib/asn1/decoders/index.js
+++ b/lib/asn1/decoders/index.js
@@ -1,3 +1,4 @@
 var decoders = exports;
 
 decoders.der = require('./der');
+decoders.pem = require('./pem');
diff --git a/lib/asn1/decoders/pem.js b/lib/asn1/decoders/pem.js
new file mode 100644
index 0000000..a7d5225
--- /dev/null
+++ b/lib/asn1/decoders/pem.js
@@ -0,0 +1,50 @@
+var inherits = require('inherits');
+var Buffer = require('buffer').Buffer;
+
+var asn1 = require('../../asn1');
+var DERDecoder = require('./der');
+
+function PEMDecoder(entity) {
+  DERDecoder.call(this, entity);
+  this.enc = 'pem';
+};
+inherits(PEMDecoder, DERDecoder);
+module.exports = PEMDecoder;
+
+PEMDecoder.prototype.decode = function decode(data, options) {
+  var lines = data.toString().split(/[\r\n]+/g);
+
+  var label = options.label.toUpperCase();
+
+  var re = /^-----(BEGIN|END) ([^-]+)-----$/;
+  var start = -1;
+  var end = -1;
+  for (var i = 0; i < lines.length; i++) {
+    var match = lines[i].match(re);
+    if (match === null)
+      continue;
+
+    if (match[2] !== label)
+      continue;
+
+    if (start === -1) {
+      if (match[1] !== 'BEGIN')
+        break;
+      start = i;
+    } else {
+      if (match[1] !== 'END')
+        break;
+      end = i;
+      break;
+    }
+  }
+  if (start === -1 || end === -1)
+    throw new Error('PEM section not found for: ' + label);
+
+  var base64 = lines.slice(start + 1, end).join('');
+  // Remove excessive symbols
+  base64.replace(/[^a-z0-9\+\/=]+/gi, '');
+
+  var input = new Buffer(base64, 'base64');
+  return DERDecoder.prototype.decode.call(this, input, options);
+};
diff --git a/lib/asn1/encoders/index.js b/lib/asn1/encoders/index.js
index 5b3cc38..6a5d29e 100644
--- a/lib/asn1/encoders/index.js
+++ b/lib/asn1/encoders/index.js
@@ -1,3 +1,4 @@
 var encoders = exports;
 
 encoders.der = require('./der');
+encoders.pem = require('./pem');
diff --git a/lib/asn1/encoders/pem.js b/lib/asn1/encoders/pem.js
new file mode 100644
index 0000000..40298b8
--- /dev/null
+++ b/lib/asn1/encoders/pem.js
@@ -0,0 +1,23 @@
+var inherits = require('inherits');
+var Buffer = require('buffer').Buffer;
+
+var asn1 = require('../../asn1');
+var DEREncoder = require('./der');
+
+function PEMEncoder(entity) {
+  DEREncoder.call(this, entity);
+  this.enc = 'pem';
+};
+inherits(PEMEncoder, DEREncoder);
+module.exports = PEMEncoder;
+
+PEMEncoder.prototype.encode = function encode(data, options) {
+  var buf = DEREncoder.prototype.encode.call(this, data);
+
+  var p = buf.toString('base64');
+  var out = [ '-----BEGIN ' + options.label + '-----' ];
+  for (var i = 0; i < p.length; i += 64)
+    out.push(p.slice(i, i + 64));
+  out.push('-----END ' + options.label + '-----');
+  return out.join('\n');
+};
diff --git a/test/pem-test.js b/test/pem-test.js
new file mode 100644
index 0000000..dfbe7db
--- /dev/null
+++ b/test/pem-test.js
@@ -0,0 +1,54 @@
+var assert = require('assert');
+var asn1 = require('..');
+var BN = require('bn.js');
+
+var Buffer = require('buffer').Buffer;
+
+describe('asn1.js PEM encoder/decoder', function() {
+  var model = asn1.define('Model', function() {
+    this.seq().obj(
+      this.key('a').int(),
+      this.key('b').bitstr(),
+      this.key('c').int()
+    );
+  });
+
+  var hundred = new Buffer(100);
+  hundred.fill('A');
+
+  it('should encode PEM', function() {
+
+    var out = model.encode({
+      a: new BN(123),
+      b: {
+        data: hundred,
+        unused: 0
+      },
+      c: new BN(456)
+    }, 'pem', {
+      label: 'MODEL'
+    });
+
+    var expected =
+        '-----BEGIN MODEL-----\n' +
+        'MG4CAXsDZQBBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\n' +
+        'QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\n' +
+        'QUFBQUFBQUFBQUFBAgIByA==\n' +
+        '-----END MODEL-----';
+    assert.equal(out, expected);
+  });
+
+  it('should decode PEM', function() {
+    var expected =
+        '-----BEGIN MODEL-----\n' +
+        'MG4CAXsDZQBBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\n' +
+        'QUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFB\n' +
+        'QUFBQUFBQUFBQUFBAgIByA==\n' +
+        '-----END MODEL-----';
+
+    var out = model.decode(expected, 'pem', { label: 'MODEL' });
+    assert.equal(out.a.toString(), '123');
+    assert.equal(out.b.data.toString(), hundred.toString());
+    assert.equal(out.c.toString(), '456');
+  });
+});

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