Commit a479f7c1 authored by Medicean's avatar Medicean

(Vendor: node-rsa) 添加 node-rsa 依赖包

parent 4985a39c
Copyright (c) 2011 Mark Cavage, All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE
node-asn1 is a library for encoding and decoding ASN.1 datatypes in pure JS.
Currently BER encoding is supported; at some point I'll likely have to do DER.
## Usage
Mostly, if you're *actually* needing to read and write ASN.1, you probably don't
need this readme to explain what and why. If you have no idea what ASN.1 is,
see this: ftp://ftp.rsa.com/pub/pkcs/ascii/layman.asc
The source is pretty much self-explanatory, and has read/write methods for the
common types out there.
### Decoding
The following reads an ASN.1 sequence with a boolean.
var Ber = require('asn1').Ber;
var reader = new Ber.Reader(Buffer.from([0x30, 0x03, 0x01, 0x01, 0xff]));
reader.readSequence();
console.log('Sequence len: ' + reader.length);
if (reader.peek() === Ber.Boolean)
console.log(reader.readBoolean());
### Encoding
The following generates the same payload as above.
var Ber = require('asn1').Ber;
var writer = new Ber.Writer();
writer.startSequence();
writer.writeBoolean(true);
writer.endSequence();
console.log(writer.buffer);
## Installation
npm install asn1
## License
MIT.
## Bugs
See <https://github.com/joyent/node-asn1/issues>.
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
module.exports = {
newInvalidAsn1Error: function (msg) {
var e = new Error();
e.name = 'InvalidAsn1Error';
e.message = msg || '';
return e;
}
};
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
var errors = require('./errors');
var types = require('./types');
var Reader = require('./reader');
var Writer = require('./writer');
// --- Exports
module.exports = {
Reader: Reader,
Writer: Writer
};
for (var t in types) {
if (types.hasOwnProperty(t))
module.exports[t] = types[t];
}
for (var e in errors) {
if (errors.hasOwnProperty(e))
module.exports[e] = errors[e];
}
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
var assert = require('assert');
var Buffer = require('safer-buffer').Buffer;
var ASN1 = require('./types');
var errors = require('./errors');
// --- Globals
var newInvalidAsn1Error = errors.newInvalidAsn1Error;
// --- API
function Reader(data) {
if (!data || !Buffer.isBuffer(data))
throw new TypeError('data must be a node Buffer');
this._buf = data;
this._size = data.length;
// These hold the "current" state
this._len = 0;
this._offset = 0;
}
Object.defineProperty(Reader.prototype, 'length', {
enumerable: true,
get: function () { return (this._len); }
});
Object.defineProperty(Reader.prototype, 'offset', {
enumerable: true,
get: function () { return (this._offset); }
});
Object.defineProperty(Reader.prototype, 'remain', {
get: function () { return (this._size - this._offset); }
});
Object.defineProperty(Reader.prototype, 'buffer', {
get: function () { return (this._buf.slice(this._offset)); }
});
/**
* Reads a single byte and advances offset; you can pass in `true` to make this
* a "peek" operation (i.e., get the byte, but don't advance the offset).
*
* @param {Boolean} peek true means don't move offset.
* @return {Number} the next byte, null if not enough data.
*/
Reader.prototype.readByte = function (peek) {
if (this._size - this._offset < 1)
return null;
var b = this._buf[this._offset] & 0xff;
if (!peek)
this._offset += 1;
return b;
};
Reader.prototype.peek = function () {
return this.readByte(true);
};
/**
* Reads a (potentially) variable length off the BER buffer. This call is
* not really meant to be called directly, as callers have to manipulate
* the internal buffer afterwards.
*
* As a result of this call, you can call `Reader.length`, until the
* next thing called that does a readLength.
*
* @return {Number} the amount of offset to advance the buffer.
* @throws {InvalidAsn1Error} on bad ASN.1
*/
Reader.prototype.readLength = function (offset) {
if (offset === undefined)
offset = this._offset;
if (offset >= this._size)
return null;
var lenB = this._buf[offset++] & 0xff;
if (lenB === null)
return null;
if ((lenB & 0x80) === 0x80) {
lenB &= 0x7f;
if (lenB === 0)
throw newInvalidAsn1Error('Indefinite length not supported');
if (lenB > 4)
throw newInvalidAsn1Error('encoding too long');
if (this._size - offset < lenB)
return null;
this._len = 0;
for (var i = 0; i < lenB; i++)
this._len = (this._len << 8) + (this._buf[offset++] & 0xff);
} else {
// Wasn't a variable length
this._len = lenB;
}
return offset;
};
/**
* Parses the next sequence in this BER buffer.
*
* To get the length of the sequence, call `Reader.length`.
*
* @return {Number} the sequence's tag.
*/
Reader.prototype.readSequence = function (tag) {
var seq = this.peek();
if (seq === null)
return null;
if (tag !== undefined && tag !== seq)
throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
': got 0x' + seq.toString(16));
var o = this.readLength(this._offset + 1); // stored in `length`
if (o === null)
return null;
this._offset = o;
return seq;
};
Reader.prototype.readInt = function () {
return this._readTag(ASN1.Integer);
};
Reader.prototype.readBoolean = function () {
return (this._readTag(ASN1.Boolean) === 0 ? false : true);
};
Reader.prototype.readEnumeration = function () {
return this._readTag(ASN1.Enumeration);
};
Reader.prototype.readString = function (tag, retbuf) {
if (!tag)
tag = ASN1.OctetString;
var b = this.peek();
if (b === null)
return null;
if (b !== tag)
throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
': got 0x' + b.toString(16));
var o = this.readLength(this._offset + 1); // stored in `length`
if (o === null)
return null;
if (this.length > this._size - o)
return null;
this._offset = o;
if (this.length === 0)
return retbuf ? Buffer.alloc(0) : '';
var str = this._buf.slice(this._offset, this._offset + this.length);
this._offset += this.length;
return retbuf ? str : str.toString('utf8');
};
Reader.prototype.readOID = function (tag) {
if (!tag)
tag = ASN1.OID;
var b = this.readString(tag, true);
if (b === null)
return null;
var values = [];
var value = 0;
for (var i = 0; i < b.length; i++) {
var byte = b[i] & 0xff;
value <<= 7;
value += byte & 0x7f;
if ((byte & 0x80) === 0) {
values.push(value);
value = 0;
}
}
value = values.shift();
values.unshift(value % 40);
values.unshift((value / 40) >> 0);
return values.join('.');
};
Reader.prototype._readTag = function (tag) {
assert.ok(tag !== undefined);
var b = this.peek();
if (b === null)
return null;
if (b !== tag)
throw newInvalidAsn1Error('Expected 0x' + tag.toString(16) +
': got 0x' + b.toString(16));
var o = this.readLength(this._offset + 1); // stored in `length`
if (o === null)
return null;
if (this.length > 4)
throw newInvalidAsn1Error('Integer too long: ' + this.length);
if (this.length > this._size - o)
return null;
this._offset = o;
var fb = this._buf[this._offset];
var value = 0;
for (var i = 0; i < this.length; i++) {
value <<= 8;
value |= (this._buf[this._offset++] & 0xff);
}
if ((fb & 0x80) === 0x80 && i !== 4)
value -= (1 << (i * 8));
return value >> 0;
};
// --- Exported API
module.exports = Reader;
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
module.exports = {
EOC: 0,
Boolean: 1,
Integer: 2,
BitString: 3,
OctetString: 4,
Null: 5,
OID: 6,
ObjectDescriptor: 7,
External: 8,
Real: 9, // float
Enumeration: 10,
PDV: 11,
Utf8String: 12,
RelativeOID: 13,
Sequence: 16,
Set: 17,
NumericString: 18,
PrintableString: 19,
T61String: 20,
VideotexString: 21,
IA5String: 22,
UTCTime: 23,
GeneralizedTime: 24,
GraphicString: 25,
VisibleString: 26,
GeneralString: 28,
UniversalString: 29,
CharacterString: 30,
BMPString: 31,
Constructor: 32,
Context: 128
};
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
var assert = require('assert');
var Buffer = require('safer-buffer').Buffer;
var ASN1 = require('./types');
var errors = require('./errors');
// --- Globals
var newInvalidAsn1Error = errors.newInvalidAsn1Error;
var DEFAULT_OPTS = {
size: 1024,
growthFactor: 8
};
// --- Helpers
function merge(from, to) {
assert.ok(from);
assert.equal(typeof (from), 'object');
assert.ok(to);
assert.equal(typeof (to), 'object');
var keys = Object.getOwnPropertyNames(from);
keys.forEach(function (key) {
if (to[key])
return;
var value = Object.getOwnPropertyDescriptor(from, key);
Object.defineProperty(to, key, value);
});
return to;
}
// --- API
function Writer(options) {
options = merge(DEFAULT_OPTS, options || {});
this._buf = Buffer.alloc(options.size || 1024);
this._size = this._buf.length;
this._offset = 0;
this._options = options;
// A list of offsets in the buffer where we need to insert
// sequence tag/len pairs.
this._seq = [];
}
Object.defineProperty(Writer.prototype, 'buffer', {
get: function () {
if (this._seq.length)
throw newInvalidAsn1Error(this._seq.length + ' unended sequence(s)');
return (this._buf.slice(0, this._offset));
}
});
Writer.prototype.writeByte = function (b) {
if (typeof (b) !== 'number')
throw new TypeError('argument must be a Number');
this._ensure(1);
this._buf[this._offset++] = b;
};
Writer.prototype.writeInt = function (i, tag) {
if (typeof (i) !== 'number')
throw new TypeError('argument must be a Number');
if (typeof (tag) !== 'number')
tag = ASN1.Integer;
var sz = 4;
while ((((i & 0xff800000) === 0) || ((i & 0xff800000) === 0xff800000 >> 0)) &&
(sz > 1)) {
sz--;
i <<= 8;
}
if (sz > 4)
throw newInvalidAsn1Error('BER ints cannot be > 0xffffffff');
this._ensure(2 + sz);
this._buf[this._offset++] = tag;
this._buf[this._offset++] = sz;
while (sz-- > 0) {
this._buf[this._offset++] = ((i & 0xff000000) >>> 24);
i <<= 8;
}
};
Writer.prototype.writeNull = function () {
this.writeByte(ASN1.Null);
this.writeByte(0x00);
};
Writer.prototype.writeEnumeration = function (i, tag) {
if (typeof (i) !== 'number')
throw new TypeError('argument must be a Number');
if (typeof (tag) !== 'number')
tag = ASN1.Enumeration;
return this.writeInt(i, tag);
};
Writer.prototype.writeBoolean = function (b, tag) {
if (typeof (b) !== 'boolean')
throw new TypeError('argument must be a Boolean');
if (typeof (tag) !== 'number')
tag = ASN1.Boolean;
this._ensure(3);
this._buf[this._offset++] = tag;
this._buf[this._offset++] = 0x01;
this._buf[this._offset++] = b ? 0xff : 0x00;
};
Writer.prototype.writeString = function (s, tag) {
if (typeof (s) !== 'string')
throw new TypeError('argument must be a string (was: ' + typeof (s) + ')');
if (typeof (tag) !== 'number')
tag = ASN1.OctetString;
var len = Buffer.byteLength(s);
this.writeByte(tag);
this.writeLength(len);
if (len) {
this._ensure(len);
this._buf.write(s, this._offset);
this._offset += len;
}
};
Writer.prototype.writeBuffer = function (buf, tag) {
if (typeof (tag) !== 'number')
throw new TypeError('tag must be a number');
if (!Buffer.isBuffer(buf))
throw new TypeError('argument must be a buffer');
this.writeByte(tag);
this.writeLength(buf.length);
this._ensure(buf.length);
buf.copy(this._buf, this._offset, 0, buf.length);
this._offset += buf.length;
};
Writer.prototype.writeStringArray = function (strings) {
if ((!strings instanceof Array))
throw new TypeError('argument must be an Array[String]');
var self = this;
strings.forEach(function (s) {
self.writeString(s);
});
};
// This is really to solve DER cases, but whatever for now
Writer.prototype.writeOID = function (s, tag) {
if (typeof (s) !== 'string')
throw new TypeError('argument must be a string');
if (typeof (tag) !== 'number')
tag = ASN1.OID;
if (!/^([0-9]+\.){3,}[0-9]+$/.test(s))
throw new Error('argument is not a valid OID string');
function encodeOctet(bytes, octet) {
if (octet < 128) {
bytes.push(octet);
} else if (octet < 16384) {
bytes.push((octet >>> 7) | 0x80);
bytes.push(octet & 0x7F);
} else if (octet < 2097152) {
bytes.push((octet >>> 14) | 0x80);
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
bytes.push(octet & 0x7F);
} else if (octet < 268435456) {
bytes.push((octet >>> 21) | 0x80);
bytes.push(((octet >>> 14) | 0x80) & 0xFF);
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
bytes.push(octet & 0x7F);
} else {
bytes.push(((octet >>> 28) | 0x80) & 0xFF);
bytes.push(((octet >>> 21) | 0x80) & 0xFF);
bytes.push(((octet >>> 14) | 0x80) & 0xFF);
bytes.push(((octet >>> 7) | 0x80) & 0xFF);
bytes.push(octet & 0x7F);
}
}
var tmp = s.split('.');
var bytes = [];
bytes.push(parseInt(tmp[0], 10) * 40 + parseInt(tmp[1], 10));
tmp.slice(2).forEach(function (b) {
encodeOctet(bytes, parseInt(b, 10));
});
var self = this;
this._ensure(2 + bytes.length);
this.writeByte(tag);
this.writeLength(bytes.length);
bytes.forEach(function (b) {
self.writeByte(b);
});
};
Writer.prototype.writeLength = function (len) {
if (typeof (len) !== 'number')
throw new TypeError('argument must be a Number');
this._ensure(4);
if (len <= 0x7f) {
this._buf[this._offset++] = len;
} else if (len <= 0xff) {
this._buf[this._offset++] = 0x81;
this._buf[this._offset++] = len;
} else if (len <= 0xffff) {
this._buf[this._offset++] = 0x82;
this._buf[this._offset++] = len >> 8;
this._buf[this._offset++] = len;
} else if (len <= 0xffffff) {
this._buf[this._offset++] = 0x83;
this._buf[this._offset++] = len >> 16;
this._buf[this._offset++] = len >> 8;
this._buf[this._offset++] = len;
} else {
throw newInvalidAsn1Error('Length too long (> 4 bytes)');
}
};
Writer.prototype.startSequence = function (tag) {
if (typeof (tag) !== 'number')
tag = ASN1.Sequence | ASN1.Constructor;
this.writeByte(tag);
this._seq.push(this._offset);
this._ensure(3);
this._offset += 3;
};
Writer.prototype.endSequence = function () {
var seq = this._seq.pop();
var start = seq + 3;
var len = this._offset - start;
if (len <= 0x7f) {
this._shift(start, len, -2);
this._buf[seq] = len;
} else if (len <= 0xff) {
this._shift(start, len, -1);
this._buf[seq] = 0x81;
this._buf[seq + 1] = len;
} else if (len <= 0xffff) {
this._buf[seq] = 0x82;
this._buf[seq + 1] = len >> 8;
this._buf[seq + 2] = len;
} else if (len <= 0xffffff) {
this._shift(start, len, 1);
this._buf[seq] = 0x83;
this._buf[seq + 1] = len >> 16;
this._buf[seq + 2] = len >> 8;
this._buf[seq + 3] = len;
} else {
throw newInvalidAsn1Error('Sequence too long');
}
};
Writer.prototype._shift = function (start, len, shift) {
assert.ok(start !== undefined);
assert.ok(len !== undefined);
assert.ok(shift);
this._buf.copy(this._buf, start + shift, start, start + len);
this._offset += shift;
};
Writer.prototype._ensure = function (len) {
assert.ok(len);
if (this._size - this._offset < len) {
var sz = this._size * this._options.growthFactor;
if (sz - this._offset < len)
sz += len;
var buf = Buffer.alloc(sz);
this._buf.copy(buf, 0, 0, this._offset);
this._buf = buf;
this._size = sz;
}
};
// --- Exported API
module.exports = Writer;
// Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
// If you have no idea what ASN.1 or BER is, see this:
// ftp://ftp.rsa.com/pub/pkcs/ascii/layman.asc
var Ber = require('./ber/index');
// --- Exported API
module.exports = {
Ber: Ber,
BerReader: Ber.Reader,
BerWriter: Ber.Writer
};
{
"_from": "asn1@^0.2.4",
"_id": "asn1@0.2.4",
"_inBundle": false,
"_integrity": "sha1-jSR136tVO7M+d7VOWeiAu4ziMTY=",
"_location": "/asn1",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "asn1@^0.2.4",
"name": "asn1",
"escapedName": "asn1",
"rawSpec": "^0.2.4",
"saveSpec": null,
"fetchSpec": "^0.2.4"
},
"_requiredBy": [
"/node-rsa"
],
"_resolved": "http://registry.npm.taobao.org/asn1/download/asn1-0.2.4.tgz",
"_shasum": "8d2475dfab553bb33e77b54e59e880bb8ce23136",
"_spec": "asn1@^0.2.4",
"_where": "/Users/medicean/workspace/antSword/node_modules/node-rsa",
"author": {
"name": "Joyent",
"url": "joyent.com"
},
"bugs": {
"url": "https://github.com/joyent/node-asn1/issues"
},
"bundleDependencies": false,
"contributors": [
{
"name": "Mark Cavage",
"email": "mcavage@gmail.com"
},
{
"name": "David Gwynne",
"email": "loki@animata.net"
},
{
"name": "Yunong Xiao",
"email": "yunong@joyent.com"
},
{
"name": "Alex Wilson",
"email": "alex.wilson@joyent.com"
}
],
"dependencies": {
"safer-buffer": "~2.1.0"
},
"deprecated": false,
"description": "Contains parsers and serializers for ASN.1 (currently BER only)",
"devDependencies": {
"eslint": "2.13.1",
"eslint-plugin-joyent": "~1.3.0",
"faucet": "0.0.1",
"istanbul": "^0.3.6",
"tape": "^3.5.0"
},
"homepage": "https://github.com/joyent/node-asn1#readme",
"license": "MIT",
"main": "lib/index.js",
"name": "asn1",
"repository": {
"type": "git",
"url": "git://github.com/joyent/node-asn1.git"
},
"scripts": {
"test": "tape ./test/ber/*.test.js"
},
"version": "0.2.4"
}
This diff is collapsed.
module.exports = function (grunt) {
grunt.initConfig({
jshint: {
options: {},
default: {
files: {
src: ['gruntfile.js', 'src/**/*.js', '!src/libs/jsbn.js']
}
},
libs: {
files: {
src: ['src/libs/**/*']
}
}
},
simplemocha: {
options: {
reporter: 'list'
},
all: {src: ['test/**/*.js']}
}
});
require('jit-grunt')(grunt, {
'simplemocha': 'grunt-simple-mocha'
});
grunt.registerTask('lint', ['jshint:default']);
grunt.registerTask('test', ['simplemocha']);
grunt.registerTask('default', ['lint', 'test']);
};
\ No newline at end of file
{
"_from": "node-rsa@^1.0.5",
"_id": "node-rsa@1.0.5",
"_inBundle": false,
"_integrity": "sha1-hU3BsnVynWm8JYg/g8qAcF25Ji4=",
"_location": "/node-rsa",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "node-rsa@^1.0.5",
"name": "node-rsa",
"escapedName": "node-rsa",
"rawSpec": "^1.0.5",
"saveSpec": null,
"fetchSpec": "^1.0.5"
},
"_requiredBy": [
"#USER",
"/"
],
"_resolved": "http://registry.npm.taobao.org/node-rsa/download/node-rsa-1.0.5.tgz",
"_shasum": "854dc1b275729d69bc25883f83ca80705db9262e",
"_spec": "node-rsa@^1.0.5",
"_where": "/Users/medicean/workspace/antSword",
"author": {
"name": "rzcoder"
},
"bugs": {
"url": "https://github.com/rzcoder/node-rsa/issues"
},
"bundleDependencies": false,
"dependencies": {
"asn1": "^0.2.4"
},
"deprecated": false,
"description": "Node.js RSA library",
"devDependencies": {
"chai": "^4.2.0",
"grunt": "^1.0.3",
"grunt-contrib-jshint": "^2.0.0",
"grunt-simple-mocha": "0.4.1",
"jit-grunt": "0.10.0",
"lodash": "^4.17.11",
"nyc": "^13.1.0"
},
"homepage": "https://github.com/rzcoder/node-rsa",
"keywords": [
"node",
"rsa",
"crypto",
"assymetric",
"encryption",
"decryption",
"sign",
"verify",
"pkcs1",
"oaep",
"pss"
],
"license": "MIT",
"main": "src/NodeRSA.js",
"name": "node-rsa",
"repository": {
"type": "git",
"url": "git+https://github.com/rzcoder/node-rsa.git"
},
"scripts": {
"test": "grunt test"
},
"version": "1.0.5"
}
This diff is collapsed.
var crypt = require('crypto');
module.exports = {
getEngine: function (keyPair, options) {
var engine = require('./js.js');
if (options.environment === 'node') {
if (typeof crypt.publicEncrypt === 'function' && typeof crypt.privateDecrypt === 'function') {
if (typeof crypt.privateEncrypt === 'function' && typeof crypt.publicDecrypt === 'function') {
engine = require('./io.js');
} else {
engine = require('./node12.js');
}
}
}
return engine(keyPair, options);
}
};
\ No newline at end of file
var crypto = require('crypto');
var constants = require('constants');
var schemes = require('../schemes/schemes.js');
module.exports = function (keyPair, options) {
var pkcs1Scheme = schemes.pkcs1.makeScheme(keyPair, options);
return {
encrypt: function (buffer, usePrivate) {
var padding;
if (usePrivate) {
padding = constants.RSA_PKCS1_PADDING;
if (options.encryptionSchemeOptions && options.encryptionSchemeOptions.padding) {
padding = options.encryptionSchemeOptions.padding;
}
return crypto.privateEncrypt({
key: options.rsaUtils.exportKey('private'),
padding: padding
}, buffer);
} else {
padding = constants.RSA_PKCS1_OAEP_PADDING;
if (options.encryptionScheme === 'pkcs1') {
padding = constants.RSA_PKCS1_PADDING;
}
if (options.encryptionSchemeOptions && options.encryptionSchemeOptions.padding) {
padding = options.encryptionSchemeOptions.padding;
}
var data = buffer;
if (padding === constants.RSA_NO_PADDING) {
data = pkcs1Scheme.pkcs0pad(buffer);
}
return crypto.publicEncrypt({
key: options.rsaUtils.exportKey('public'),
padding: padding
}, data);
}
},
decrypt: function (buffer, usePublic) {
var padding;
if (usePublic) {
padding = constants.RSA_PKCS1_PADDING;
if (options.encryptionSchemeOptions && options.encryptionSchemeOptions.padding) {
padding = options.encryptionSchemeOptions.padding;
}
return crypto.publicDecrypt({
key: options.rsaUtils.exportKey('public'),
padding: padding
}, buffer);
} else {
padding = constants.RSA_PKCS1_OAEP_PADDING;
if (options.encryptionScheme === 'pkcs1') {
padding = constants.RSA_PKCS1_PADDING;
}
if (options.encryptionSchemeOptions && options.encryptionSchemeOptions.padding) {
padding = options.encryptionSchemeOptions.padding;
}
var res = crypto.privateDecrypt({
key: options.rsaUtils.exportKey('private'),
padding: padding
}, buffer);
if (padding === constants.RSA_NO_PADDING) {
return pkcs1Scheme.pkcs0unpad(res);
}
return res;
}
}
};
};
\ No newline at end of file
var BigInteger = require('../libs/jsbn.js');
var schemes = require('../schemes/schemes.js');
module.exports = function (keyPair, options) {
var pkcs1Scheme = schemes.pkcs1.makeScheme(keyPair, options);
return {
encrypt: function (buffer, usePrivate) {
var m, c;
if (usePrivate) {
/* Type 1: zeros padding for private key encrypt */
m = new BigInteger(pkcs1Scheme.encPad(buffer, {type: 1}));
c = keyPair.$doPrivate(m);
} else {
m = new BigInteger(keyPair.encryptionScheme.encPad(buffer));
c = keyPair.$doPublic(m);
}
return c.toBuffer(keyPair.encryptedDataLength);
},
decrypt: function (buffer, usePublic) {
var m, c = new BigInteger(buffer);
if (usePublic) {
m = keyPair.$doPublic(c);
/* Type 1: zeros padding for private key decrypt */
return pkcs1Scheme.encUnPad(m.toBuffer(keyPair.encryptedDataLength), {type: 1});
} else {
m = keyPair.$doPrivate(c);
return keyPair.encryptionScheme.encUnPad(m.toBuffer(keyPair.encryptedDataLength));
}
}
};
};
\ No newline at end of file
var crypto = require('crypto');
var constants = require('constants');
var schemes = require('../schemes/schemes.js');
module.exports = function (keyPair, options) {
var jsEngine = require('./js.js')(keyPair, options);
var pkcs1Scheme = schemes.pkcs1.makeScheme(keyPair, options);
return {
encrypt: function (buffer, usePrivate) {
if (usePrivate) {
return jsEngine.encrypt(buffer, usePrivate);
}
var padding = constants.RSA_PKCS1_OAEP_PADDING;
if (options.encryptionScheme === 'pkcs1') {
padding = constants.RSA_PKCS1_PADDING;
}
if (options.encryptionSchemeOptions && options.encryptionSchemeOptions.padding) {
padding = options.encryptionSchemeOptions.padding;
}
var data = buffer;
if (padding === constants.RSA_NO_PADDING) {
data = pkcs1Scheme.pkcs0pad(buffer);
}
return crypto.publicEncrypt({
key: options.rsaUtils.exportKey('public'),
padding: padding
}, data);
},
decrypt: function (buffer, usePublic) {
if (usePublic) {
return jsEngine.decrypt(buffer, usePublic);
}
var padding = constants.RSA_PKCS1_OAEP_PADDING;
if (options.encryptionScheme === 'pkcs1') {
padding = constants.RSA_PKCS1_PADDING;
}
if (options.encryptionSchemeOptions && options.encryptionSchemeOptions.padding) {
padding = options.encryptionSchemeOptions.padding;
}
var res = crypto.privateDecrypt({
key: options.rsaUtils.exportKey('private'),
padding: padding
}, buffer);
if (padding === constants.RSA_NO_PADDING) {
return pkcs1Scheme.pkcs0unpad(res);
}
return res;
}
};
};
\ No newline at end of file
var _ = require('../utils')._;
var utils = require('../utils');
module.exports = {
privateExport: function (key, options) {
return {
n: key.n.toBuffer(),
e: key.e,
d: key.d.toBuffer(),
p: key.p.toBuffer(),
q: key.q.toBuffer(),
dmp1: key.dmp1.toBuffer(),
dmq1: key.dmq1.toBuffer(),
coeff: key.coeff.toBuffer()
};
},
privateImport: function (key, data, options) {
if (data.n && data.e && data.d && data.p && data.q && data.dmp1 && data.dmq1 && data.coeff) {
key.setPrivate(
data.n,
data.e,
data.d,
data.p,
data.q,
data.dmp1,
data.dmq1,
data.coeff
);
} else {
throw Error("Invalid key data");
}
},
publicExport: function (key, options) {
return {
n: key.n.toBuffer(),
e: key.e
};
},
publicImport: function (key, data, options) {
if (data.n && data.e) {
key.setPublic(
data.n,
data.e
);
} else {
throw Error("Invalid key data");
}
},
/**
* Trying autodetect and import key
* @param key
* @param data
*/
autoImport: function (key, data) {
if (data.n && data.e) {
if (data.d && data.p && data.q && data.dmp1 && data.dmq1 && data.coeff) {
module.exports.privateImport(key, data);
return true;
} else {
module.exports.publicImport(key, data);
return true;
}
}
return false;
}
};
var _ = require('../utils')._;
function formatParse(format) {
format = format.split('-');
var keyType = 'private';
var keyOpt = {type: 'default'};
for (var i = 1; i < format.length; i++) {
if (format[i]) {
switch (format[i]) {
case 'public':
keyType = format[i];
break;
case 'private':
keyType = format[i];
break;
case 'pem':
keyOpt.type = format[i];
break;
case 'der':
keyOpt.type = format[i];
break;
}
}
}
return {scheme: format[0], keyType: keyType, keyOpt: keyOpt};
}
module.exports = {
pkcs1: require('./pkcs1'),
pkcs8: require('./pkcs8'),
components: require('./components'),
isPrivateExport: function (format) {
return module.exports[format] && typeof module.exports[format].privateExport === 'function';
},
isPrivateImport: function (format) {
return module.exports[format] && typeof module.exports[format].privateImport === 'function';
},
isPublicExport: function (format) {
return module.exports[format] && typeof module.exports[format].publicExport === 'function';
},
isPublicImport: function (format) {
return module.exports[format] && typeof module.exports[format].publicImport === 'function';
},
detectAndImport: function (key, data, format) {
if (format === undefined) {
for (var scheme in module.exports) {
if (typeof module.exports[scheme].autoImport === 'function' && module.exports[scheme].autoImport(key, data)) {
return true;
}
}
} else if (format) {
var fmt = formatParse(format);
if (module.exports[fmt.scheme]) {
if (fmt.keyType === 'private') {
module.exports[fmt.scheme].privateImport(key, data, fmt.keyOpt);
} else {
module.exports[fmt.scheme].publicImport(key, data, fmt.keyOpt);
}
} else {
throw Error('Unsupported key format');
}
}
return false;
},
detectAndExport: function (key, format) {
if (format) {
var fmt = formatParse(format);
if (module.exports[fmt.scheme]) {
if (fmt.keyType === 'private') {
if (!key.isPrivate()) {
throw Error("This is not private key");
}
return module.exports[fmt.scheme].privateExport(key, fmt.keyOpt);
} else {
if (!key.isPublic()) {
throw Error("This is not public key");
}
return module.exports[fmt.scheme].publicExport(key, fmt.keyOpt);
}
} else {
throw Error('Unsupported key format');
}
}
}
};
\ No newline at end of file
var ber = require('asn1').Ber;
var _ = require('../utils')._;
var utils = require('../utils');
const PRIVATE_OPENING_BOUNDARY = '-----BEGIN RSA PRIVATE KEY-----';
const PRIVATE_CLOSING_BOUNDARY = '-----END RSA PRIVATE KEY-----';
const PUBLIC_OPENING_BOUNDARY = '-----BEGIN RSA PUBLIC KEY-----';
const PUBLIC_CLOSING_BOUNDARY = '-----END RSA PUBLIC KEY-----';
module.exports = {
privateExport: function (key, options) {
options = options || {};
var n = key.n.toBuffer();
var d = key.d.toBuffer();
var p = key.p.toBuffer();
var q = key.q.toBuffer();
var dmp1 = key.dmp1.toBuffer();
var dmq1 = key.dmq1.toBuffer();
var coeff = key.coeff.toBuffer();
var length = n.length + d.length + p.length + q.length + dmp1.length + dmq1.length + coeff.length + 512; // magic
var writer = new ber.Writer({size: length});
writer.startSequence();
writer.writeInt(0);
writer.writeBuffer(n, 2);
writer.writeInt(key.e);
writer.writeBuffer(d, 2);
writer.writeBuffer(p, 2);
writer.writeBuffer(q, 2);
writer.writeBuffer(dmp1, 2);
writer.writeBuffer(dmq1, 2);
writer.writeBuffer(coeff, 2);
writer.endSequence();
if (options.type === 'der') {
return writer.buffer;
} else {
return PRIVATE_OPENING_BOUNDARY + '\n' + utils.linebrk(writer.buffer.toString('base64'), 64) + '\n' + PRIVATE_CLOSING_BOUNDARY;
}
},
privateImport: function (key, data, options) {
options = options || {};
var buffer;
if (options.type !== 'der') {
if (Buffer.isBuffer(data)) {
data = data.toString('utf8');
}
if (_.isString(data)) {
var pem = utils.trimSurroundingText(data, PRIVATE_OPENING_BOUNDARY, PRIVATE_CLOSING_BOUNDARY)
.replace(/\s+|\n\r|\n|\r$/gm, '');
buffer = Buffer.from(pem, 'base64');
} else {
throw Error('Unsupported key format');
}
} else if (Buffer.isBuffer(data)) {
buffer = data;
} else {
throw Error('Unsupported key format');
}
var reader = new ber.Reader(buffer);
reader.readSequence();
reader.readString(2, true); // just zero
key.setPrivate(
reader.readString(2, true), // modulus
reader.readString(2, true), // publicExponent
reader.readString(2, true), // privateExponent
reader.readString(2, true), // prime1
reader.readString(2, true), // prime2
reader.readString(2, true), // exponent1 -- d mod (p1)
reader.readString(2, true), // exponent2 -- d mod (q-1)
reader.readString(2, true) // coefficient -- (inverse of q) mod p
);
},
publicExport: function (key, options) {
options = options || {};
var n = key.n.toBuffer();
var length = n.length + 512; // magic
var bodyWriter = new ber.Writer({size: length});
bodyWriter.startSequence();
bodyWriter.writeBuffer(n, 2);
bodyWriter.writeInt(key.e);
bodyWriter.endSequence();
if (options.type === 'der') {
return bodyWriter.buffer;
} else {
return PUBLIC_OPENING_BOUNDARY + '\n' + utils.linebrk(bodyWriter.buffer.toString('base64'), 64) + '\n' + PUBLIC_CLOSING_BOUNDARY;
}
},
publicImport: function (key, data, options) {
options = options || {};
var buffer;
if (options.type !== 'der') {
if (Buffer.isBuffer(data)) {
data = data.toString('utf8');
}
if (_.isString(data)) {
var pem = utils.trimSurroundingText(data, PUBLIC_OPENING_BOUNDARY, PUBLIC_CLOSING_BOUNDARY)
.replace(/\s+|\n\r|\n|\r$/gm, '');
buffer = Buffer.from(pem, 'base64');
}
} else if (Buffer.isBuffer(data)) {
buffer = data;
} else {
throw Error('Unsupported key format');
}
var body = new ber.Reader(buffer);
body.readSequence();
key.setPublic(
body.readString(0x02, true), // modulus
body.readString(0x02, true) // publicExponent
);
},
/**
* Trying autodetect and import key
* @param key
* @param data
*/
autoImport: function (key, data) {
// [\S\s]* matches zero or more of any character
if (/^[\S\s]*-----BEGIN RSA PRIVATE KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END RSA PRIVATE KEY-----[\S\s]*$/g.test(data)) {
module.exports.privateImport(key, data);
return true;
}
if (/^[\S\s]*-----BEGIN RSA PUBLIC KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END RSA PUBLIC KEY-----[\S\s]*$/g.test(data)) {
module.exports.publicImport(key, data);
return true;
}
return false;
}
};
\ No newline at end of file
var ber = require('asn1').Ber;
var _ = require('../utils')._;
var PUBLIC_RSA_OID = '1.2.840.113549.1.1.1';
var utils = require('../utils');
const PRIVATE_OPENING_BOUNDARY = '-----BEGIN PRIVATE KEY-----';
const PRIVATE_CLOSING_BOUNDARY = '-----END PRIVATE KEY-----';
const PUBLIC_OPENING_BOUNDARY = '-----BEGIN PUBLIC KEY-----';
const PUBLIC_CLOSING_BOUNDARY = '-----END PUBLIC KEY-----';
module.exports = {
privateExport: function (key, options) {
options = options || {};
var n = key.n.toBuffer();
var d = key.d.toBuffer();
var p = key.p.toBuffer();
var q = key.q.toBuffer();
var dmp1 = key.dmp1.toBuffer();
var dmq1 = key.dmq1.toBuffer();
var coeff = key.coeff.toBuffer();
var length = n.length + d.length + p.length + q.length + dmp1.length + dmq1.length + coeff.length + 512; // magic
var bodyWriter = new ber.Writer({size: length});
bodyWriter.startSequence();
bodyWriter.writeInt(0);
bodyWriter.writeBuffer(n, 2);
bodyWriter.writeInt(key.e);
bodyWriter.writeBuffer(d, 2);
bodyWriter.writeBuffer(p, 2);
bodyWriter.writeBuffer(q, 2);
bodyWriter.writeBuffer(dmp1, 2);
bodyWriter.writeBuffer(dmq1, 2);
bodyWriter.writeBuffer(coeff, 2);
bodyWriter.endSequence();
var writer = new ber.Writer({size: length});
writer.startSequence();
writer.writeInt(0);
writer.startSequence();
writer.writeOID(PUBLIC_RSA_OID);
writer.writeNull();
writer.endSequence();
writer.writeBuffer(bodyWriter.buffer, 4);
writer.endSequence();
if (options.type === 'der') {
return writer.buffer;
} else {
return PRIVATE_OPENING_BOUNDARY + '\n' + utils.linebrk(writer.buffer.toString('base64'), 64) + '\n' + PRIVATE_CLOSING_BOUNDARY;
}
},
privateImport: function (key, data, options) {
options = options || {};
var buffer;
if (options.type !== 'der') {
if (Buffer.isBuffer(data)) {
data = data.toString('utf8');
}
if (_.isString(data)) {
var pem = utils.trimSurroundingText(data, PRIVATE_OPENING_BOUNDARY, PRIVATE_CLOSING_BOUNDARY)
.replace('-----END PRIVATE KEY-----', '')
.replace(/\s+|\n\r|\n|\r$/gm, '');
buffer = Buffer.from(pem, 'base64');
} else {
throw Error('Unsupported key format');
}
} else if (Buffer.isBuffer(data)) {
buffer = data;
} else {
throw Error('Unsupported key format');
}
var reader = new ber.Reader(buffer);
reader.readSequence();
reader.readInt(0);
var header = new ber.Reader(reader.readString(0x30, true));
if (header.readOID(0x06, true) !== PUBLIC_RSA_OID) {
throw Error('Invalid Public key format');
}
var body = new ber.Reader(reader.readString(0x04, true));
body.readSequence();
body.readString(2, true); // just zero
key.setPrivate(
body.readString(2, true), // modulus
body.readString(2, true), // publicExponent
body.readString(2, true), // privateExponent
body.readString(2, true), // prime1
body.readString(2, true), // prime2
body.readString(2, true), // exponent1 -- d mod (p1)
body.readString(2, true), // exponent2 -- d mod (q-1)
body.readString(2, true) // coefficient -- (inverse of q) mod p
);
},
publicExport: function (key, options) {
options = options || {};
var n = key.n.toBuffer();
var length = n.length + 512; // magic
var bodyWriter = new ber.Writer({size: length});
bodyWriter.writeByte(0);
bodyWriter.startSequence();
bodyWriter.writeBuffer(n, 2);
bodyWriter.writeInt(key.e);
bodyWriter.endSequence();
var writer = new ber.Writer({size: length});
writer.startSequence();
writer.startSequence();
writer.writeOID(PUBLIC_RSA_OID);
writer.writeNull();
writer.endSequence();
writer.writeBuffer(bodyWriter.buffer, 3);
writer.endSequence();
if (options.type === 'der') {
return writer.buffer;
} else {
return PUBLIC_OPENING_BOUNDARY + '\n' + utils.linebrk(writer.buffer.toString('base64'), 64) + '\n' + PUBLIC_CLOSING_BOUNDARY;
}
},
publicImport: function (key, data, options) {
options = options || {};
var buffer;
if (options.type !== 'der') {
if (Buffer.isBuffer(data)) {
data = data.toString('utf8');
}
if (_.isString(data)) {
var pem = utils.trimSurroundingText(data, PUBLIC_OPENING_BOUNDARY, PUBLIC_CLOSING_BOUNDARY)
.replace(/\s+|\n\r|\n|\r$/gm, '');
buffer = Buffer.from(pem, 'base64');
}
} else if (Buffer.isBuffer(data)) {
buffer = data;
} else {
throw Error('Unsupported key format');
}
var reader = new ber.Reader(buffer);
reader.readSequence();
var header = new ber.Reader(reader.readString(0x30, true));
if (header.readOID(0x06, true) !== PUBLIC_RSA_OID) {
throw Error('Invalid Public key format');
}
var body = new ber.Reader(reader.readString(0x03, true));
body.readByte();
body.readSequence();
key.setPublic(
body.readString(0x02, true), // modulus
body.readString(0x02, true) // publicExponent
);
},
/**
* Trying autodetect and import key
* @param key
* @param data
*/
autoImport: function (key, data) {
if (/^[\S\s]*-----BEGIN PRIVATE KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END PRIVATE KEY-----[\S\s]*$/g.test(data)) {
module.exports.privateImport(key, data);
return true;
}
if (/^[\S\s]*-----BEGIN PUBLIC KEY-----\s*(?=(([A-Za-z0-9+/=]+\s*)+))\1-----END PUBLIC KEY-----[\S\s]*$/g.test(data)) {
module.exports.publicImport(key, data);
return true;
}
return false;
}
};
This diff is collapsed.
This diff is collapsed.
/**
* PKCS_OAEP signature scheme
*/
var BigInteger = require('../libs/jsbn');
var crypt = require('crypto');
module.exports = {
isEncryption: true,
isSignature: false
};
module.exports.digestLength = {
md4: 16,
md5: 16,
ripemd160: 20,
rmd160: 20,
sha1: 20,
sha224: 28,
sha256: 32,
sha384: 48,
sha512: 64
};
var DEFAULT_HASH_FUNCTION = 'sha1';
/*
* OAEP Mask Generation Function 1
* Generates a buffer full of pseudorandom bytes given seed and maskLength.
* Giving the same seed, maskLength, and hashFunction will result in the same exact byte values in the buffer.
*
* https://tools.ietf.org/html/rfc3447#appendix-B.2.1
*
* Parameters:
* seed [Buffer] The pseudo random seed for this function
* maskLength [int] The length of the output
* hashFunction [String] The hashing function to use. Will accept any valid crypto hash. Default "sha1"
* Supports "sha1" and "sha256".
* To add another algorythm the algorythem must be accepted by crypto.createHash, and then the length of the output of the hash function (the digest) must be added to the digestLength object below.
* Most RSA implementations will be expecting sha1
*/
module.exports.eme_oaep_mgf1 = function (seed, maskLength, hashFunction) {
hashFunction = hashFunction || DEFAULT_HASH_FUNCTION;
var hLen = module.exports.digestLength[hashFunction];
var count = Math.ceil(maskLength / hLen);
var T = Buffer.alloc(hLen * count);
var c = Buffer.alloc(4);
for (var i = 0; i < count; ++i) {
var hash = crypt.createHash(hashFunction);
hash.update(seed);
c.writeUInt32BE(i, 0);
hash.update(c);
hash.digest().copy(T, i * hLen);
}
return T.slice(0, maskLength);
};
module.exports.makeScheme = function (key, options) {
function Scheme(key, options) {
this.key = key;
this.options = options;
}
Scheme.prototype.maxMessageLength = function () {
return this.key.encryptedDataLength - 2 * module.exports.digestLength[this.options.encryptionSchemeOptions.hash || DEFAULT_HASH_FUNCTION] - 2;
};
/**
* Pad input
* alg: PKCS1_OAEP
*
* https://tools.ietf.org/html/rfc3447#section-7.1.1
*/
Scheme.prototype.encPad = function (buffer) {
var hash = this.options.encryptionSchemeOptions.hash || DEFAULT_HASH_FUNCTION;
var mgf = this.options.encryptionSchemeOptions.mgf || module.exports.eme_oaep_mgf1;
var label = this.options.encryptionSchemeOptions.label || Buffer.alloc(0);
var emLen = this.key.encryptedDataLength;
var hLen = module.exports.digestLength[hash];
// Make sure we can put message into an encoded message of emLen bytes
if (buffer.length > emLen - 2 * hLen - 2) {
throw new Error("Message is too long to encode into an encoded message with a length of " + emLen + " bytes, increase" +
"emLen to fix this error (minimum value for given parameters and options: " + (emLen - 2 * hLen - 2) + ")");
}
var lHash = crypt.createHash(hash);
lHash.update(label);
lHash = lHash.digest();
var PS = Buffer.alloc(emLen - buffer.length - 2 * hLen - 1); // Padding "String"
PS.fill(0); // Fill the buffer with octets of 0
PS[PS.length - 1] = 1;
var DB = Buffer.concat([lHash, PS, buffer]);
var seed = crypt.randomBytes(hLen);
// mask = dbMask
var mask = mgf(seed, DB.length, hash);
// XOR DB and dbMask together.
for (var i = 0; i < DB.length; i++) {
DB[i] ^= mask[i];
}
// DB = maskedDB
// mask = seedMask
mask = mgf(DB, hLen, hash);
// XOR seed and seedMask together.
for (i = 0; i < seed.length; i++) {
seed[i] ^= mask[i];
}
// seed = maskedSeed
var em = Buffer.alloc(1 + seed.length + DB.length);
em[0] = 0;
seed.copy(em, 1);
DB.copy(em, 1 + seed.length);
return em;
};
/**
* Unpad input
* alg: PKCS1_OAEP
*
* Note: This method works within the buffer given and modifies the values. It also returns a slice of the EM as the return Message.
* If the implementation requires that the EM parameter be unmodified then the implementation should pass in a clone of the EM buffer.
*
* https://tools.ietf.org/html/rfc3447#section-7.1.2
*/
Scheme.prototype.encUnPad = function (buffer) {
var hash = this.options.encryptionSchemeOptions.hash || DEFAULT_HASH_FUNCTION;
var mgf = this.options.encryptionSchemeOptions.mgf || module.exports.eme_oaep_mgf1;
var label = this.options.encryptionSchemeOptions.label || Buffer.alloc(0);
var hLen = module.exports.digestLength[hash];
// Check to see if buffer is a properly encoded OAEP message
if (buffer.length < 2 * hLen + 2) {
throw new Error("Error decoding message, the supplied message is not long enough to be a valid OAEP encoded message");
}
var seed = buffer.slice(1, hLen + 1); // seed = maskedSeed
var DB = buffer.slice(1 + hLen); // DB = maskedDB
var mask = mgf(DB, hLen, hash); // seedMask
// XOR maskedSeed and seedMask together to get the original seed.
for (var i = 0; i < seed.length; i++) {
seed[i] ^= mask[i];
}
mask = mgf(seed, DB.length, hash); // dbMask
// XOR DB and dbMask together to get the original data block.
for (i = 0; i < DB.length; i++) {
DB[i] ^= mask[i];
}
var lHash = crypt.createHash(hash);
lHash.update(label);
lHash = lHash.digest();
var lHashEM = DB.slice(0, hLen);
if (lHashEM.toString("hex") != lHash.toString("hex")) {
throw new Error("Error decoding message, the lHash calculated from the label provided and the lHash in the encrypted data do not match.");
}
// Filter out padding
i = hLen;
while (DB[i++] === 0 && i < DB.length);
if (DB[i - 1] != 1) {
throw new Error("Error decoding message, there is no padding message separator byte");
}
return DB.slice(i); // Message
};
return new Scheme(key, options);
};
/**
* PKCS1 padding and signature scheme
*/
var BigInteger = require('../libs/jsbn');
var crypt = require('crypto');
var constants = require('constants');
var SIGN_INFO_HEAD = {
md2: Buffer.from('3020300c06082a864886f70d020205000410', 'hex'),
md5: Buffer.from('3020300c06082a864886f70d020505000410', 'hex'),
sha1: Buffer.from('3021300906052b0e03021a05000414', 'hex'),
sha224: Buffer.from('302d300d06096086480165030402040500041c', 'hex'),
sha256: Buffer.from('3031300d060960864801650304020105000420', 'hex'),
sha384: Buffer.from('3041300d060960864801650304020205000430', 'hex'),
sha512: Buffer.from('3051300d060960864801650304020305000440', 'hex'),
ripemd160: Buffer.from('3021300906052b2403020105000414', 'hex'),
rmd160: Buffer.from('3021300906052b2403020105000414', 'hex')
};
var SIGN_ALG_TO_HASH_ALIASES = {
'ripemd160': 'rmd160'
};
var DEFAULT_HASH_FUNCTION = 'sha256';
module.exports = {
isEncryption: true,
isSignature: true
};
module.exports.makeScheme = function (key, options) {
function Scheme(key, options) {
this.key = key;
this.options = options;
}
Scheme.prototype.maxMessageLength = function () {
if (this.options.encryptionSchemeOptions && this.options.encryptionSchemeOptions.padding == constants.RSA_NO_PADDING) {
return this.key.encryptedDataLength;
}
return this.key.encryptedDataLength - 11;
};
/**
* Pad input Buffer to encryptedDataLength bytes, and return Buffer.from
* alg: PKCS#1
* @param buffer
* @returns {Buffer}
*/
Scheme.prototype.encPad = function (buffer, options) {
options = options || {};
var filled;
if (buffer.length > this.key.maxMessageLength) {
throw new Error("Message too long for RSA (n=" + this.key.encryptedDataLength + ", l=" + buffer.length + ")");
}
if (this.options.encryptionSchemeOptions && this.options.encryptionSchemeOptions.padding == constants.RSA_NO_PADDING) {
//RSA_NO_PADDING treated like JAVA left pad with zero character
filled = Buffer.alloc(this.key.maxMessageLength - buffer.length);
filled.fill(0);
return Buffer.concat([filled, buffer]);
}
/* Type 1: zeros padding for private key encrypt */
if (options.type === 1) {
filled = Buffer.alloc(this.key.encryptedDataLength - buffer.length - 1);
filled.fill(0xff, 0, filled.length - 1);
filled[0] = 1;
filled[filled.length - 1] = 0;
return Buffer.concat([filled, buffer]);
} else {
/* random padding for public key encrypt */
filled = Buffer.alloc(this.key.encryptedDataLength - buffer.length);
filled[0] = 0;
filled[1] = 2;
var rand = crypt.randomBytes(filled.length - 3);
for (var i = 0; i < rand.length; i++) {
var r = rand[i];
while (r === 0) { // non-zero only
r = crypt.randomBytes(1)[0];
}
filled[i + 2] = r;
}
filled[filled.length - 1] = 0;
return Buffer.concat([filled, buffer]);
}
};
/**
* Unpad input Buffer and, if valid, return the Buffer object
* alg: PKCS#1 (type 2, random)
* @param buffer
* @returns {Buffer}
*/
Scheme.prototype.encUnPad = function (buffer, options) {
options = options || {};
var i = 0;
if (this.options.encryptionSchemeOptions && this.options.encryptionSchemeOptions.padding == constants.RSA_NO_PADDING) {
//RSA_NO_PADDING treated like JAVA left pad with zero character
var unPad;
if (typeof buffer.lastIndexOf == "function") { //patch for old node version
unPad = buffer.slice(buffer.lastIndexOf('\0') + 1, buffer.length);
} else {
unPad = buffer.slice(String.prototype.lastIndexOf.call(buffer, '\0') + 1, buffer.length);
}
return unPad;
}
if (buffer.length < 4) {
return null;
}
/* Type 1: zeros padding for private key decrypt */
if (options.type === 1) {
if (buffer[0] !== 0 && buffer[1] !== 1) {
return null;
}
i = 3;
while (buffer[i] !== 0) {
if (buffer[i] != 0xFF || ++i >= buffer.length) {
return null;
}
}
} else {
/* random padding for public key decrypt */
if (buffer[0] !== 0 && buffer[1] !== 2) {
return null;
}
i = 3;
while (buffer[i] !== 0) {
if (++i >= buffer.length) {
return null;
}
}
}
return buffer.slice(i + 1, buffer.length);
};
Scheme.prototype.sign = function (buffer) {
var hashAlgorithm = this.options.signingSchemeOptions.hash || DEFAULT_HASH_FUNCTION;
if (this.options.environment === 'browser') {
hashAlgorithm = SIGN_ALG_TO_HASH_ALIASES[hashAlgorithm] || hashAlgorithm;
var hasher = crypt.createHash(hashAlgorithm);
hasher.update(buffer);
var hash = this.pkcs1pad(hasher.digest(), hashAlgorithm);
var res = this.key.$doPrivate(new BigInteger(hash)).toBuffer(this.key.encryptedDataLength);
return res;
} else {
var signer = crypt.createSign('RSA-' + hashAlgorithm.toUpperCase());
signer.update(buffer);
return signer.sign(this.options.rsaUtils.exportKey('private'));
}
};
Scheme.prototype.verify = function (buffer, signature, signature_encoding) {
if (this.options.encryptionSchemeOptions && this.options.encryptionSchemeOptions.padding == constants.RSA_NO_PADDING) {
//RSA_NO_PADDING has no verify data
return false;
}
var hashAlgorithm = this.options.signingSchemeOptions.hash || DEFAULT_HASH_FUNCTION;
if (this.options.environment === 'browser') {
hashAlgorithm = SIGN_ALG_TO_HASH_ALIASES[hashAlgorithm] || hashAlgorithm;
if (signature_encoding) {
signature = Buffer.from(signature, signature_encoding);
}
var hasher = crypt.createHash(hashAlgorithm);
hasher.update(buffer);
var hash = this.pkcs1pad(hasher.digest(), hashAlgorithm);
var m = this.key.$doPublic(new BigInteger(signature));
return m.toBuffer().toString('hex') == hash.toString('hex');
} else {
var verifier = crypt.createVerify('RSA-' + hashAlgorithm.toUpperCase());
verifier.update(buffer);
return verifier.verify(this.options.rsaUtils.exportKey('public'), signature, signature_encoding);
}
};
/**
* PKCS#1 zero pad input buffer to max data length
* @param hashBuf
* @param hashAlgorithm
* @returns {*}
*/
Scheme.prototype.pkcs0pad = function (buffer) {
var filled = Buffer.alloc(this.key.maxMessageLength - buffer.length);
filled.fill(0);
return Buffer.concat([filled, buffer]);
};
Scheme.prototype.pkcs0unpad = function (buffer) {
var unPad;
if (typeof buffer.lastIndexOf == "function") { //patch for old node version
unPad = buffer.slice(buffer.lastIndexOf('\0') + 1, buffer.length);
} else {
unPad = buffer.slice(String.prototype.lastIndexOf.call(buffer, '\0') + 1, buffer.length);
}
return unPad;
};
/**
* PKCS#1 pad input buffer to max data length
* @param hashBuf
* @param hashAlgorithm
* @returns {*}
*/
Scheme.prototype.pkcs1pad = function (hashBuf, hashAlgorithm) {
var digest = SIGN_INFO_HEAD[hashAlgorithm];
if (!digest) {
throw Error('Unsupported hash algorithm');
}
var data = Buffer.concat([digest, hashBuf]);
if (data.length + 10 > this.key.encryptedDataLength) {
throw Error('Key is too short for signing algorithm (' + hashAlgorithm + ')');
}
var filled = Buffer.alloc(this.key.encryptedDataLength - data.length - 1);
filled.fill(0xff, 0, filled.length - 1);
filled[0] = 1;
filled[filled.length - 1] = 0;
var res = Buffer.concat([filled, data]);
return res;
};
return new Scheme(key, options);
};
/**
* PSS signature scheme
*/
var BigInteger = require('../libs/jsbn');
var crypt = require('crypto');
module.exports = {
isEncryption: false,
isSignature: true
};
var DEFAULT_HASH_FUNCTION = 'sha1';
var DEFAULT_SALT_LENGTH = 20;
module.exports.makeScheme = function (key, options) {
var OAEP = require('./schemes').pkcs1_oaep;
/**
* @param key
* @param options
* options [Object] An object that contains the following keys that specify certain options for encoding.
* └>signingSchemeOptions
* ├>hash [String] Hash function to use when encoding and generating masks. Must be a string accepted by node's crypto.createHash function. (default = "sha1")
* ├>mgf [function] The mask generation function to use when encoding. (default = mgf1SHA1)
* └>sLen [uint] The length of the salt to generate. (default = 20)
* @constructor
*/
function Scheme(key, options) {
this.key = key;
this.options = options;
}
Scheme.prototype.sign = function (buffer) {
var mHash = crypt.createHash(this.options.signingSchemeOptions.hash || DEFAULT_HASH_FUNCTION);
mHash.update(buffer);
var encoded = this.emsa_pss_encode(mHash.digest(), this.key.keySize - 1);
return this.key.$doPrivate(new BigInteger(encoded)).toBuffer(this.key.encryptedDataLength);
};
Scheme.prototype.verify = function (buffer, signature, signature_encoding) {
if (signature_encoding) {
signature = Buffer.from(signature, signature_encoding);
}
signature = new BigInteger(signature);
var emLen = Math.ceil((this.key.keySize - 1) / 8);
var m = this.key.$doPublic(signature).toBuffer(emLen);
var mHash = crypt.createHash(this.options.signingSchemeOptions.hash || DEFAULT_HASH_FUNCTION);
mHash.update(buffer);
return this.emsa_pss_verify(mHash.digest(), m, this.key.keySize - 1);
};
/*
* https://tools.ietf.org/html/rfc3447#section-9.1.1
*
* mHash [Buffer] Hashed message to encode
* emBits [uint] Maximum length of output in bits. Must be at least 8hLen + 8sLen + 9 (hLen = Hash digest length in bytes | sLen = length of salt in bytes)
* @returns {Buffer} The encoded message
*/
Scheme.prototype.emsa_pss_encode = function (mHash, emBits) {
var hash = this.options.signingSchemeOptions.hash || DEFAULT_HASH_FUNCTION;
var mgf = this.options.signingSchemeOptions.mgf || OAEP.eme_oaep_mgf1;
var sLen = this.options.signingSchemeOptions.saltLength || DEFAULT_SALT_LENGTH;
var hLen = OAEP.digestLength[hash];
var emLen = Math.ceil(emBits / 8);
if (emLen < hLen + sLen + 2) {
throw new Error("Output length passed to emBits(" + emBits + ") is too small for the options " +
"specified(" + hash + ", " + sLen + "). To fix this issue increase the value of emBits. (minimum size: " +
(8 * hLen + 8 * sLen + 9) + ")"
);
}
var salt = crypt.randomBytes(sLen);
var Mapostrophe = Buffer.alloc(8 + hLen + sLen);
Mapostrophe.fill(0, 0, 8);
mHash.copy(Mapostrophe, 8);
salt.copy(Mapostrophe, 8 + mHash.length);
var H = crypt.createHash(hash);
H.update(Mapostrophe);
H = H.digest();
var PS = Buffer.alloc(emLen - salt.length - hLen - 2);
PS.fill(0);
var DB = Buffer.alloc(PS.length + 1 + salt.length);
PS.copy(DB);
DB[PS.length] = 0x01;
salt.copy(DB, PS.length + 1);
var dbMask = mgf(H, DB.length, hash);
// XOR DB and dbMask together
var maskedDB = Buffer.alloc(DB.length);
for (var i = 0; i < dbMask.length; i++) {
maskedDB[i] = DB[i] ^ dbMask[i];
}
var bits = 8 * emLen - emBits;
var mask = 255 ^ (255 >> 8 - bits << 8 - bits);
maskedDB[0] = maskedDB[0] & mask;
var EM = Buffer.alloc(maskedDB.length + H.length + 1);
maskedDB.copy(EM, 0);
H.copy(EM, maskedDB.length);
EM[EM.length - 1] = 0xbc;
return EM;
};
/*
* https://tools.ietf.org/html/rfc3447#section-9.1.2
*
* mHash [Buffer] Hashed message
* EM [Buffer] Signature
* emBits [uint] Length of EM in bits. Must be at least 8hLen + 8sLen + 9 to be a valid signature. (hLen = Hash digest length in bytes | sLen = length of salt in bytes)
* @returns {Boolean} True if signature(EM) matches message(M)
*/
Scheme.prototype.emsa_pss_verify = function (mHash, EM, emBits) {
var hash = this.options.signingSchemeOptions.hash || DEFAULT_HASH_FUNCTION;
var mgf = this.options.signingSchemeOptions.mgf || OAEP.eme_oaep_mgf1;
var sLen = this.options.signingSchemeOptions.saltLength || DEFAULT_SALT_LENGTH;
var hLen = OAEP.digestLength[hash];
var emLen = Math.ceil(emBits / 8);
if (emLen < hLen + sLen + 2 || EM[EM.length - 1] != 0xbc) {
return false;
}
var DB = Buffer.alloc(emLen - hLen - 1);
EM.copy(DB, 0, 0, emLen - hLen - 1);
var mask = 0;
for (var i = 0, bits = 8 * emLen - emBits; i < bits; i++) {
mask |= 1 << (7 - i);
}
if ((DB[0] & mask) !== 0) {
return false;
}
var H = EM.slice(emLen - hLen - 1, emLen - 1);
var dbMask = mgf(H, DB.length, hash);
// Unmask DB
for (i = 0; i < DB.length; i++) {
DB[i] ^= dbMask[i];
}
bits = 8 * emLen - emBits;
mask = 255 ^ (255 >> 8 - bits << 8 - bits);
DB[0] = DB[0] & mask;
// Filter out padding
for (i = 0; DB[i] === 0 && i < DB.length; i++);
if (DB[i] != 1) {
return false;
}
var salt = DB.slice(DB.length - sLen);
var Mapostrophe = Buffer.alloc(8 + hLen + sLen);
Mapostrophe.fill(0, 0, 8);
mHash.copy(Mapostrophe, 8);
salt.copy(Mapostrophe, 8 + mHash.length);
var Hapostrophe = crypt.createHash(hash);
Hapostrophe.update(Mapostrophe);
Hapostrophe = Hapostrophe.digest();
return H.toString("hex") === Hapostrophe.toString("hex");
};
return new Scheme(key, options);
};
module.exports = {
pkcs1: require('./pkcs1'),
pkcs1_oaep: require('./oaep'),
pss: require('./pss'),
/**
* Check if scheme has padding methods
* @param scheme {string}
* @returns {Boolean}
*/
isEncryption: function (scheme) {
return module.exports[scheme] && module.exports[scheme].isEncryption;
},
/**
* Check if scheme has sign/verify methods
* @param scheme {string}
* @returns {Boolean}
*/
isSignature: function (scheme) {
return module.exports[scheme] && module.exports[scheme].isSignature;
}
};
\ No newline at end of file
/*
* Utils functions
*
*/
var crypt = require('crypto');
/**
* Break string str each maxLen symbols
* @param str
* @param maxLen
* @returns {string}
*/
module.exports.linebrk = function (str, maxLen) {
var res = '';
var i = 0;
while (i + maxLen < str.length) {
res += str.substring(i, i + maxLen) + "\n";
i += maxLen;
}
return res + str.substring(i, str.length);
};
module.exports.detectEnvironment = function () {
if (typeof(window) !== 'undefined' && window && !(process && process.title === 'node')) {
return 'browser';
}
return 'node';
};
/**
* Trying get a 32-bit unsigned integer from the partial buffer
* @param buffer
* @param offset
* @returns {Number}
*/
module.exports.get32IntFromBuffer = function (buffer, offset) {
offset = offset || 0;
var size = 0;
if ((size = buffer.length - offset) > 0) {
if (size >= 4) {
return buffer.readUInt32BE(offset);
} else {
var res = 0;
for (var i = offset + size, d = 0; i > offset; i--, d += 2) {
res += buffer[i - 1] * Math.pow(16, d);
}
return res;
}
} else {
return NaN;
}
};
module.exports._ = {
isObject: function (value) {
var type = typeof value;
return !!value && (type == 'object' || type == 'function');
},
isString: function (value) {
return typeof value == 'string' || value instanceof String;
},
isNumber: function (value) {
return typeof value == 'number' || !isNaN(parseFloat(value)) && isFinite(value);
},
/**
* Returns copy of `obj` without `removeProp` field.
* @param obj
* @param removeProp
* @returns Object
*/
omit: function (obj, removeProp) {
var newObj = {};
for (var prop in obj) {
if (!obj.hasOwnProperty(prop) || prop === removeProp) {
continue;
}
newObj[prop] = obj[prop];
}
return newObj;
}
};
/**
* Strips everything around the opening and closing lines, including the lines
* themselves.
*/
module.exports.trimSurroundingText = function (data, opening, closing) {
var trimStartIndex = 0;
var trimEndIndex = data.length;
var openingBoundaryIndex = data.indexOf(opening);
if (openingBoundaryIndex >= 0) {
trimStartIndex = openingBoundaryIndex + opening.length;
}
var closingBoundaryIndex = data.indexOf(closing, openingBoundaryIndex);
if (closingBoundaryIndex >= 0) {
trimEndIndex = closingBoundaryIndex;
}
return data.substring(trimStartIndex, trimEndIndex);
}
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment