diff --git a/src/index.js b/src/index.js index 7472326b..64c95c8c 100644 --- a/src/index.js +++ b/src/index.js @@ -77,6 +77,12 @@ export { default as S2K } from './type/s2k'; */ export { default as Keyid } from './type/keyid'; +/** + * @see module:type/oid + * @name module:openpgp.Oid + */ +export { default as Oid } from './type/oid'; + /** * @see module:encoding/armor * @name module:openpgp.armor diff --git a/src/type/oid.js b/src/type/oid.js new file mode 100644 index 00000000..a8fd1899 --- /dev/null +++ b/src/type/oid.js @@ -0,0 +1,69 @@ +// OpenPGP.js - An OpenPGP implementation in javascript +// Copyright (C) 2015-2016 Decentral +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3.0 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +/** + * Wrapper to an Oid value
+ *
+ * An object identifier type from {@link https://tools.ietf.org/html/rfc6637#section-11|RFC6637, section 11}. + * @requires util + * @module type/oid + */ + +'use strict'; + +import util from '../util.js'; + +module.exports = Oid; + +/** + * @constructor + */ +function Oid(oid) { + if (typeof oid === 'undefined') { + oid = ''; + } else if (util.isArray(oid)) { + oid = util.bin2str(oid); + } else if (util.isUint8Array(oid)) { + oid = util.Uint8Array2str(oid); + } + this.oid = oid; +} + +/** + * Method to read an Oid object + * @param {Uint8Array} input Where to read the Oid from + * @return {Number} Number of read bytes + */ +Oid.prototype.read = function (input) { + if (input.length >= 1) { + var length = input[0]; + if (input.length >= 1+length) { + this.oid = util.Uint8Array2str(input.subarray(1, 1+length)); + return 1+this.oid.length; + } + } + throw new Error('Invalid oid'); +}; + +/** + * Serialize an Oid object + * @return {Uint8Array} Array with the serialized value the Oid + */ +Oid.prototype.write = function () { + return util.str2Uint8Array( + String.fromCharCode(this.oid.length)+this.oid); +}; diff --git a/test/general/index.js b/test/general/index.js index 61de4d72..aed972e7 100644 --- a/test/general/index.js +++ b/test/general/index.js @@ -7,5 +7,6 @@ describe('General', function () { require('./key.js'); require('./openpgp.js'); require('./hkp.js'); + require('./oid.js'); }); diff --git a/test/general/oid.js b/test/general/oid.js new file mode 100644 index 00000000..e093f407 --- /dev/null +++ b/test/general/oid.js @@ -0,0 +1,38 @@ +'use strict'; + +var openpgp = typeof window !== 'undefined' && window.openpgp ? window.openpgp : require('../../dist/openpgp'); + +var expect = require('chai').expect; + +describe('Oid tests', function() { + var Oid = openpgp.Oid; + var p256_oid = new Uint8Array([0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07]); + var p384_oid = new Uint8Array([0x2B, 0x81, 0x04, 0x00, 0x22]); + var p521_oid = new Uint8Array([0x2B, 0x81, 0x04, 0x00, 0x23]); + it('Constructing', function() { + var oids = [p256_oid, p384_oid, p521_oid]; + oids.forEach(function (data) { + var oid = new Oid(data); + expect(oid).to.exist; + expect(oid.oid).to.exist; + expect(oid.oid).to.have.length(data.length); + expect(oid.oid).to.equal(openpgp.util.Uint8Array2str(data)); + }); + }); + it('Reading and writing', function() { + var oids = [p256_oid, p384_oid, p521_oid]; + oids.forEach(function (data) { + data = openpgp.util.concatUint8Array([new Uint8Array([data.length]), data]); + var oid = new Oid(); + expect(oid.read(data)).to.equal(data.length); + expect(oid.oid).to.exist; + expect(oid.oid).to.have.length(data.length-1); + expect(oid.oid).to.equal(openpgp.util.Uint8Array2str(data.subarray(1))); + var result = oid.write(); + expect(result).to.exist; + expect(result).to.have.length(data.length); + expect(result[0]).to.equal(data.length-1); + expect(openpgp.util.Uint8Array2str(result.subarray(1))).to.equal(openpgp.util.Uint8Array2str(data.subarray(1))); + }); + }); +});