Skip to content

Commit

Permalink
generic key files
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Metcalf committed Nov 15, 2014
1 parent 735a056 commit 7bd3f91
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 19 deletions.
10 changes: 10 additions & 0 deletions asn1.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ var RSAPublicKey = asn1.define('RSAPublicKey', function() {
});
exports.RSAPublicKey = RSAPublicKey;

var PublicKey = rfc3280.SubjectPublicKeyInfo;
exports.PublicKey = PublicKey;
var PrivateKeyInfo = asn1.define('PrivateKeyInfo', function() {
this.seq().obj(
this.key('version').int(),
this.key('algorithm').use(rfc3280.AlgorithmIdentifier),
this.key('subjectPrivateKey').octstr()
);
});
exports.PrivateKey = PrivateKeyInfo;
var GeneralName = asn1.define('GeneralName', function() {
this.choice({
dNSName: this.implicit(2).ia5str()
Expand Down
35 changes: 35 additions & 0 deletions parseKeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var pemstrip = require('pemstrip');
var asn1 = require('./asn1');
module.exports = parseKeys;

function parseKeys(buffer) {
var stripped = pemstrip.strip(buffer);
var type = stripped.tag;
var data = new Buffer(stripped.base64, 'base64');
var subtype
switch (type) {
case 'PUBLIC KEY':
data = asn1.PublicKey.decode(data, 'der');
subtype = data.algorithm.algorithm.join('.');
switch(subtype) {
case '1.2.840.113549.1.1.1':
return asn1.RSAPublicKey.decode(data.subjectPublicKey.data, 'der');
default: throw new Error('unknown key id ' + subtype);
}
throw new Error('unknown key type ' + type);
case 'PRIVATE KEY':
data = asn1.PrivateKey.decode(data, 'der');
subtype = data.algorithm.algorithm.join('.');
switch(subtype) {
case '1.2.840.113549.1.1.1':
return asn1.RSAPrivateKey.decode(data.subjectPrivateKey, 'der');
default: throw new Error('unknown key id ' + subtype);
}
throw new Error('unknown key type ' + type);
case 'RSA PUBLIC KEY':
return asn1.RSAPublicKey.decode(data, 'der');
case 'RSA PRIVATE KEY':
return asn1.RSAPrivateKey.decode(data, 'der');
default: throw new Error('unknown key type ' + type);
}
}
5 changes: 2 additions & 3 deletions sign.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
var pemstrip = require('pemstrip');
var asn1 = require('./asn1');
var parseKeys = require('./parseKeys');
var bn = require('bn.js');
module.exports = sign;
function sign(hash, key) {
var priv = asn1.RSAPrivateKey.decode(new Buffer(pemstrip.strip(key).base64, 'base64'), 'der');
var priv = parseKeys(key);
var len = priv.modulus.byteLength();
var pad = [ 0, 1 ];
while (hash.length + pad.length + 1 < len) {
Expand Down
16 changes: 16 additions & 0 deletions test/1024.priv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKulUTZ8B1qccZ8c
DXRGSY08gW8KvLlcxxxGC4gZHNT3CBUF8n5R4KE30aZyYZ/rtsQZu05juZJxaJ0q
mbe75dlQ5d+Xc9BMXeQg/MpTZw5TAN7OIdGYYpFBe+1PLZ6wEfjkYrMqMUcfq2Lq
hTLdAbvBJnuRcYZLqmBeOQ8FTrKrAgMBAAECgYEAnkHRbEPU3/WISSQrP36iyCb2
S/SBZwKkzmvCrBxDWhPeDswp9c/2JY76rNWfLzy8iXgUG8WUzvHje61Qh3gmBcKe
bUaTGl4Vy8Ha1YBADo5RfRrdm0FE4tvgvu/TkqFqpBBZweu54285hk5zlG7n/D7Y
dnNXUpu5MlNb5x3gW0kCQQDUL//cwcXUxY/evaJP4jSe+ZwEQZo+zXRLiPUulBoV
aw28CVMuxdgwqAo1X1IKefPeUaf7RQu8gCKaRnpGuEuXAkEAzxZTfMmvmCUDIew4
5Gk6bK265XQWdhcgiq254lpBGOYmDj9yCE7yA+zmASQwMsXTdQOi1hOCEyrXuSJ5
c++EDQJAFh3WrnzoEPByuYXMmET8tSFRWMQ5vpgNqh3haHR5b4gUC2hxaiunCBNL
1RpVY9AoUiDywGcG/SPh93CnKB3niwJBAKP7AtsifZgVXtiizB4aMThTjVYaSZrz
D0Kg9DuHylpkDChmFu77TGrNUQgAVuYtfhb/bRblVa/F0hJ4eQHT3JUCQBVT68tb
OgRUk0aP9tC3021VN82X6+klowSQN8oBPX8+TfDWSUilp/+j24Hky+Z29Do7yR/R
qutnL92CvBlVLV4=
-----END PRIVATE KEY-----
6 changes: 6 additions & 0 deletions test/1024.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrpVE2fAdanHGfHA10RkmNPIFv
Cry5XMccRguIGRzU9wgVBfJ+UeChN9GmcmGf67bEGbtOY7mScWidKpm3u+XZUOXf
l3PQTF3kIPzKU2cOUwDeziHRmGKRQXvtTy2esBH45GKzKjFHH6ti6oUy3QG7wSZ7
kXGGS6pgXjkPBU6yqwIDAQAB
-----END PUBLIC KEY-----
39 changes: 27 additions & 12 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
var test = require('tape');
var fs = require('fs');
var priv1024 = fs.readFileSync(__dirname + '/rsa.1024.priv');
var pub1024 = fs.readFileSync(__dirname + '/rsa.1024.pub');
var priv2028 = fs.readFileSync(__dirname + '/rsa.2028.priv');
var pub2028 = fs.readFileSync(__dirname + '/rsa.2028.pub');
var rsa1024 = {
private: fs.readFileSync(__dirname + '/rsa.1024.priv'),
public: fs.readFileSync(__dirname + '/rsa.1024.pub')
}
var rsa2028 = {
private: fs.readFileSync(__dirname + '/rsa.2028.priv'),
public: fs.readFileSync(__dirname + '/rsa.2028.pub')
}
var nonrsa1024 = {
private: fs.readFileSync(__dirname + '/1024.priv'),
public: fs.readFileSync(__dirname + '/1024.pub')
}
var nodeCrypto = require('crypto');
var myCrypto = require('../');
function testIt(pub, priv, message, scheme) {
function testIt(keys, message, scheme) {
var pub = keys.public;
var priv = keys.private;
test(message.toString(), function (t) {
t.plan(4);
var mySign = myCrypto.createSign(scheme);
Expand All @@ -21,11 +32,15 @@ function testIt(pub, priv, message, scheme) {
t.ok(myVer.update(message).verify(pub, nodeSig), 'me validate node sig');
});
}
testIt(pub1024, priv1024, new Buffer('sha224 with 1024 keys'), 'RSA-SHA224');
testIt(pub2028, priv2028, new Buffer('sha224 with 2028 keys'), 'RSA-SHA224');
testIt(pub1024, priv1024, new Buffer('SHA256 with 1024 keys'), 'RSA-SHA256');
testIt(pub2028, priv2028, new Buffer('SHA256 with 2028 keys'), 'RSA-SHA256');
testIt(pub1024, priv1024, new Buffer('SHA384 with 1024 keys'), 'RSA-SHA384');
testIt(pub2028, priv2028, new Buffer('SHA384 with 2028 keys'), 'RSA-SHA384');
testIt(pub1024, priv1024, new Buffer('SHA512 with 1024 keys'), 'RSA-SHA512');
testIt(pub2028, priv2028, new Buffer('SHA512 with 2028 keys'), 'RSA-SHA512');
testIt(rsa1024, new Buffer('sha224 with 1024 keys'), 'RSA-SHA224');
testIt(nonrsa1024, new Buffer('sha224 with 1024 keys non-rsa key'), 'RSA-SHA224');
testIt(rsa2028, new Buffer('sha224 with 2028 keys'), 'RSA-SHA224');
testIt(rsa1024, new Buffer('SHA256 with 1024 keys'), 'RSA-SHA256');
testIt(nonrsa1024, new Buffer('sha256 with 1024 keys non-rsa key'), 'RSA-SHA256');
testIt(rsa2028, new Buffer('SHA256 with 2028 keys'), 'RSA-SHA256');
testIt(rsa1024, new Buffer('SHA384 with 1024 keys'), 'RSA-SHA384');
testIt(nonrsa1024, new Buffer('sha384 with 1024 keys non-rsa key'), 'RSA-SHA384');
testIt(rsa2028, new Buffer('SHA384 with 2028 keys'), 'RSA-SHA384');
testIt(rsa1024, new Buffer('SHA512 with 1024 keys'), 'RSA-SHA512');
testIt(nonrsa1024, new Buffer('sha512 with 1024 keys non-rsa key'), 'RSA-SHA512');
testIt(rsa2028, new Buffer('SHA512 with 2028 keys'), 'RSA-SHA512');
7 changes: 3 additions & 4 deletions verify.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// much of this based on https://github.com/indutny/self-signed/blob/gh-pages/lib/rsa.js
var pemstrip = require('pemstrip');
var asn1 = require('./asn1');
var parseKeys = require('./parseKeys');

var bn = require('bn.js');
module.exports = verify;
function verify(sig, hash, key) {
var pub = asn1.RSAPublicKey.decode(new Buffer(pemstrip.strip(key).base64, 'base64'), 'der');

var pub = parseKeys(key);

var red = bn.mont(pub.modulus);
sig = new bn(sig).toRed(red);
Expand Down

0 comments on commit 7bd3f91

Please sign in to comment.