Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull out evp #15

Merged
merged 2 commits into from
Sep 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions EVP_BytesToKey.js

This file was deleted.

88 changes: 44 additions & 44 deletions asn1.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// from https://github.com/indutny/self-signed/blob/gh-pages/lib/asn1.js
// Fedor, you are amazing.

var asn1 = require('asn1.js');
var asn1 = require('asn1.js')

var RSAPrivateKey = asn1.define('RSAPrivateKey', function() {
var RSAPrivateKey = asn1.define('RSAPrivateKey', function () {
this.seq().obj(
this.key('version').int(),
this.key('modulus').int(),
Expand All @@ -14,48 +14,48 @@ var RSAPrivateKey = asn1.define('RSAPrivateKey', function() {
this.key('exponent1').int(),
this.key('exponent2').int(),
this.key('coefficient').int()
);
});
exports.RSAPrivateKey = RSAPrivateKey;
)
})
exports.RSAPrivateKey = RSAPrivateKey

var RSAPublicKey = asn1.define('RSAPublicKey', function() {
var RSAPublicKey = asn1.define('RSAPublicKey', function () {
this.seq().obj(
this.key('modulus').int(),
this.key('publicExponent').int()
);
});
exports.RSAPublicKey = RSAPublicKey;
)
})
exports.RSAPublicKey = RSAPublicKey

var PublicKey = asn1.define('SubjectPublicKeyInfo', function() {
var PublicKey = asn1.define('SubjectPublicKeyInfo', function () {
this.seq().obj(
this.key('algorithm').use(AlgorithmIdentifier),
this.key('subjectPublicKey').bitstr()
);
});
exports.PublicKey = PublicKey;
)
})
exports.PublicKey = PublicKey

var AlgorithmIdentifier = asn1.define('AlgorithmIdentifier', function() {
var AlgorithmIdentifier = asn1.define('AlgorithmIdentifier', function () {
this.seq().obj(
this.key('algorithm').objid(),
this.key('none').null_().optional(),
this.key('curve').objid().optional(),
this.key('params').seq().obj(
this.key('p').int(),
this.key('q').int(),
this.key('g').int()
).optional()
);
});
this.key('p').int(),
this.key('q').int(),
this.key('g').int()
).optional()
)
})

var PrivateKeyInfo = asn1.define('PrivateKeyInfo', function() {
var PrivateKeyInfo = asn1.define('PrivateKeyInfo', function () {
this.seq().obj(
this.key('version').int(),
this.key('algorithm').use(AlgorithmIdentifier),
this.key('subjectPrivateKey').octstr()
);
});
exports.PrivateKey = PrivateKeyInfo;
var EncryptedPrivateKeyInfo = asn1.define('EncryptedPrivateKeyInfo', function() {
)
})
exports.PrivateKey = PrivateKeyInfo
var EncryptedPrivateKeyInfo = asn1.define('EncryptedPrivateKeyInfo', function () {
this.seq().obj(
this.key('algorithm').seq().obj(
this.key('id').objid(),
Expand All @@ -74,44 +74,44 @@ var EncryptedPrivateKeyInfo = asn1.define('EncryptedPrivateKeyInfo', function()
)
),
this.key('subjectPrivateKey').octstr()
);
});
)
})

exports.EncryptedPrivateKey = EncryptedPrivateKeyInfo;
exports.EncryptedPrivateKey = EncryptedPrivateKeyInfo

var DSAPrivateKey = asn1.define('DSAPrivateKey', function() {
var DSAPrivateKey = asn1.define('DSAPrivateKey', function () {
this.seq().obj(
this.key('version').int(),
this.key('p').int(),
this.key('q').int(),
this.key('g').int(),
this.key('pub_key').int(),
this.key('priv_key').int()
);
});
exports.DSAPrivateKey = DSAPrivateKey;
)
})
exports.DSAPrivateKey = DSAPrivateKey

exports.DSAparam = asn1.define('DSAparam', function () {
this.int();
});
var ECPrivateKey = asn1.define('ECPrivateKey', function() {
this.int()
})
var ECPrivateKey = asn1.define('ECPrivateKey', function () {
this.seq().obj(
this.key('version').int(),
this.key('privateKey').octstr(),
this.key('parameters').optional().explicit(0).use(ECParameters),
this.key('publicKey').optional().explicit(1).bitstr()
);
});
exports.ECPrivateKey = ECPrivateKey;
var ECParameters = asn1.define('ECParameters', function() {
)
})
exports.ECPrivateKey = ECPrivateKey
var ECParameters = asn1.define('ECParameters', function () {
this.choice({
namedCurve: this.objid()
});
});
})
})

exports.signature = asn1.define('signature', function() {
exports.signature = asn1.define('signature', function () {
this.seq().obj(
this.key('r').int(),
this.key('s').int()
);
});
)
})
50 changes: 20 additions & 30 deletions fixProc.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
// adapted from https://github.com/apatil/pemstrip
var findProc = /Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)\r?\n\r?\n([0-9A-z\n\r\+\/\=]+)\r?\n/m;
var startRegex =/^-----BEGIN (.*) KEY-----\r?\n/m;
var fullRegex = /^-----BEGIN (.*) KEY-----\r?\n([0-9A-z\n\r\+\/\=]+)\r?\n-----END \1 KEY-----$/m;
var evp = require('./EVP_BytesToKey');
var ciphers = require('browserify-aes');
var findProc = /Proc-Type: 4,ENCRYPTED\r?\nDEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)\r?\n\r?\n([0-9A-z\n\r\+\/\=]+)\r?\n/m
var startRegex = /^-----BEGIN (.*) KEY-----\r?\n/m
var fullRegex = /^-----BEGIN (.*) KEY-----\r?\n([0-9A-z\n\r\+\/\=]+)\r?\n-----END \1 KEY-----$/m
var evp = require('evp_bytestokey')
var ciphers = require('browserify-aes')
module.exports = function (okey, password) {
var key = okey.toString();
var match = key.match(findProc);
var decrypted;
var key = okey.toString()
var match = key.match(findProc)
var decrypted
if (!match) {
var match2 = key.match(fullRegex);
decrypted = new Buffer(match2[2].replace(/\r?\n/g, ''), 'base64');
var match2 = key.match(fullRegex)
decrypted = new Buffer(match2[2].replace(/\r?\n/g, ''), 'base64')
} else {
var suite = 'aes' + match[1];
var iv = new Buffer(match[2], 'hex');
var cipherText = new Buffer(match[3].replace(/\r?\n/g, ''), 'base64');
var cipherKey = evp(password, iv.slice(0,8), parseInt(match[1]));
var out = [];
var cipher = ciphers.createDecipheriv(suite, cipherKey, iv);
out.push(cipher.update(cipherText));
out.push(cipher.final());
decrypted = Buffer.concat(out);
var suite = 'aes' + match[1]
var iv = new Buffer(match[2], 'hex')
var cipherText = new Buffer(match[3].replace(/\r?\n/g, ''), 'base64')
var cipherKey = evp(password, iv.slice(0, 8), parseInt(match[1], 10)).key
var out = []
var cipher = ciphers.createDecipheriv(suite, cipherKey, iv)
out.push(cipher.update(cipherText))
out.push(cipher.final())
decrypted = Buffer.concat(out)
}
var tag = key.match(startRegex)[1] + ' KEY';
var tag = key.match(startRegex)[1] + ' KEY'
return {
tag: tag,
data: decrypted
};
};

// http://stackoverflow.com/a/7033705
function wrap (str) {
var chunks = []

for (var i = 0; i < str.length; i += 64) {
chunks.push(str.slice(i, i + 64))
}
return chunks.join("\n")
}
Loading