http-signature
Advanced tools
Comparing version 0.9.4 to 0.9.5
221
lib/util.js
@@ -10,11 +10,2 @@ // Copyright 2011 Joyent, Inc. All rights reserved. | ||
///--- Globals | ||
var RSA_ENC_HDR = [0x30, 0x0d, | ||
0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, | ||
0x01, 0x01, 0x05, 0x00]; | ||
///--- Helpers | ||
@@ -46,4 +37,150 @@ | ||
function rsaToPEM(key) { | ||
var buffer; | ||
var der; | ||
var exponent; | ||
var i; | ||
var modulus; | ||
var newKey = ''; | ||
var offset = 0; | ||
var type; | ||
var tmp; | ||
try { | ||
buffer = new Buffer(key.split(' ')[1], 'base64'); | ||
tmp = readNext(buffer, offset); | ||
type = tmp.data.toString(); | ||
offset = tmp.offset; | ||
if (type !== 'ssh-rsa') | ||
throw new Error('Invalid ssh key type: ' + type); | ||
tmp = readNext(buffer, offset); | ||
exponent = tmp.data; | ||
offset = tmp.offset; | ||
tmp = readNext(buffer, offset); | ||
modulus = tmp.data; | ||
} catch (e) { | ||
throw new Error('Invalid ssh key: ' + key); | ||
} | ||
// DER is a subset of BER | ||
der = new asn1.BerWriter(); | ||
der.startSequence(); | ||
der.startSequence(); | ||
der.writeOID('1.2.840.113549.1.1.1'); | ||
der.writeNull(); | ||
der.endSequence(); | ||
der.startSequence(0x03); // bit string | ||
der.writeByte(0x00); | ||
// Actual key | ||
der.startSequence(); | ||
writeInt(der, modulus); | ||
writeInt(der, exponent); | ||
der.endSequence(); | ||
// bit string | ||
der.endSequence(); | ||
der.endSequence(); | ||
tmp = der.buffer.toString('base64'); | ||
for (i = 0; i < tmp.length; i++) { | ||
if ((i % 64) === 0) | ||
newKey += '\n'; | ||
newKey += tmp.charAt(i); | ||
} | ||
if (!/\\n$/.test(newKey)) | ||
newKey += '\n'; | ||
return '-----BEGIN PUBLIC KEY-----' + newKey + '-----END PUBLIC KEY-----\n'; | ||
} | ||
function dsaToPEM(key) { | ||
var buffer; | ||
var offset = 0; | ||
var tmp; | ||
var der; | ||
var newKey = ''; | ||
var type; | ||
var p; | ||
var q; | ||
var g; | ||
var y; | ||
try { | ||
buffer = new Buffer(key.split(' ')[1], 'base64'); | ||
tmp = readNext(buffer, offset); | ||
type = tmp.data.toString(); | ||
offset = tmp.offset; | ||
if (!/^ssh-ds[as].*/.test(type)) | ||
throw new Error('Invalid ssh key type: ' + type); | ||
tmp = readNext(buffer, offset); | ||
p = tmp.data; | ||
offset = tmp.offset; | ||
tmp = readNext(buffer, offset); | ||
q = tmp.data; | ||
offset = tmp.offset; | ||
tmp = readNext(buffer, offset); | ||
g = tmp.data; | ||
offset = tmp.offset; | ||
tmp = readNext(buffer, offset); | ||
y = tmp.data; | ||
} catch (e) { | ||
console.log(e.stack); | ||
throw new Error('Invalid ssh key: ' + key); | ||
} | ||
// DER is a subset of BER | ||
der = new asn1.BerWriter(); | ||
der.startSequence(); | ||
der.startSequence(); | ||
der.writeOID('1.2.840.10040.4.1'); | ||
der.startSequence(); | ||
writeInt(der, p); | ||
writeInt(der, q); | ||
writeInt(der, g); | ||
der.endSequence(); | ||
der.endSequence(); | ||
der.startSequence(0x03); // bit string | ||
der.writeByte(0x00); | ||
writeInt(der, y); | ||
der.endSequence(); | ||
der.endSequence(); | ||
tmp = der.buffer.toString('base64'); | ||
for (i = 0; i < tmp.length; i++) { | ||
if ((i % 64) === 0) | ||
newKey += '\n'; | ||
newKey += tmp.charAt(i); | ||
} | ||
if (!/\\n$/.test(newKey)) | ||
newKey += '\n'; | ||
return '-----BEGIN PUBLIC KEY-----' + newKey + '-----END PUBLIC KEY-----\n'; | ||
} | ||
///--- API | ||
@@ -68,64 +205,8 @@ | ||
var buffer; | ||
var der; | ||
var exponent; | ||
var i; | ||
var modulus; | ||
var newKey = ''; | ||
var offset = 0; | ||
var type; | ||
var tmp; | ||
if (/^ssh-rsa.*/.test(key)) | ||
return rsaToPEM(key); | ||
if (/^ssh-ds[as].*/.test(key)) | ||
return dsaToPEM(key); | ||
try { | ||
buffer = new Buffer(key.split(' ')[1], 'base64'); | ||
tmp = readNext(buffer, offset); | ||
type = tmp.data.toString(); | ||
offset = tmp.offset; | ||
if (type !== 'ssh-rsa') | ||
throw new Error('Invalid ssh key type: ' + type); | ||
tmp = readNext(buffer, offset); | ||
exponent = tmp.data; | ||
offset = tmp.offset; | ||
tmp = readNext(buffer, offset); | ||
modulus = tmp.data; | ||
} catch (e) { | ||
throw new Error('Invalid ssh key: ' + key); | ||
} | ||
// DER is a subset of BER | ||
der = new asn1.BerWriter(); | ||
der.startSequence(0x30); | ||
RSA_ENC_HDR.forEach(function(b) { | ||
der.writeByte(b); | ||
}); | ||
der.startSequence(0x03); // bit string | ||
der.writeByte(0x00); | ||
// Now the actual key | ||
der.startSequence(0x30); | ||
writeInt(der, modulus); | ||
writeInt(der, exponent); | ||
der.endSequence(); | ||
der.endSequence(); | ||
der.endSequence(); | ||
tmp = der.buffer.toString('base64'); | ||
for (i = 0; i < tmp.length; i++) { | ||
if ((i % 64) === 0) | ||
newKey += '\n'; | ||
newKey += tmp.charAt(i); | ||
} | ||
if (!/\\n$/.test(newKey)) | ||
newKey += '\n'; | ||
return '-----BEGIN PUBLIC KEY-----' + newKey + '-----END PUBLIC KEY-----\n'; | ||
throw new Error('Only RSA and DSA public keys are allowed'); | ||
}, | ||
@@ -132,0 +213,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"description": "Reference implementation of Joyent's HTTP Signature Scheme", | ||
"version": "0.9.4", | ||
"version": "0.9.5", | ||
"homepage": "http://www.joyent.com", | ||
@@ -21,3 +21,3 @@ "repository": { | ||
"dependencies": { | ||
"asn1": "0.1.5", | ||
"asn1": "0.1.7", | ||
"ctype": "0.1.0", | ||
@@ -24,0 +24,0 @@ "sprintf": "0.1.1" |
@@ -64,4 +64,24 @@ // Copyright 2011 Joyent, Inc. All rights reserved. | ||
var DSA_1024 = 'ssh-dss AAAAB3NzaC1kc3MAAACBAKK5sckoM05sOPajUcTWG0zPTvyRmj6' + | ||
'YQ1g2IgezUUrXgY+2PPy07+JrQi8SN9qr/CBP+0q0Ec48qVFf9LlkUBwu9Jf5HTUVNiKNj3c' + | ||
'SRPFH8HqZn+nxhVsOLhnHWxgDQ8OOm48Ma61NcYVo2B0Ne8cUs8xSqLqba2EG9ze87FQZAAA' + | ||
'AFQCVP/xpiAofZRD8L4QFwxOW9krikQAAAIACNv0EmKr+nIA13fjhpiqbYYyVXYOiWM4cmOD' + | ||
'G/d1J8/vR4YhWHWPbAEw7LD0DEwDIHLlRZr/1jsHbFcwt4tzRs95fyHzpucpGhocmjWx43qt' + | ||
'xEhDeJrxPlkIXHakciAEhoo+5YeRSSgRse5PrZDosdr5fA+DADs8tnto5Glf5owAAAIBHcEF' + | ||
'5ytvCRiKbsWKOgeMZ7JT/XGX+hMhS7aaJ2IspKj7YsWada1yBwoM6yYHtlpnGsq/PoPaZU8K' + | ||
'40f47psV6OhSh+/O/jgqLS/Ur2c0mQQqIb7vvkc7he/SPOQAqyDmyYFBuazuSf2s9Uy2hfvj' + | ||
'Wgb6X+vN9W8SOb2668IL7Vg== mark@bluesnoop.local'; | ||
var DSA_1024_PEM = '-----BEGIN PUBLIC KEY-----\n' + | ||
'MIIBtjCCASsGByqGSM44BAEwggEeAoGBAKK5sckoM05sOPajUcTWG0zPTvyRmj6Y\n' + | ||
'Q1g2IgezUUrXgY+2PPy07+JrQi8SN9qr/CBP+0q0Ec48qVFf9LlkUBwu9Jf5HTUV\n' + | ||
'NiKNj3cSRPFH8HqZn+nxhVsOLhnHWxgDQ8OOm48Ma61NcYVo2B0Ne8cUs8xSqLqb\n' + | ||
'a2EG9ze87FQZAhUAlT/8aYgKH2UQ/C+EBcMTlvZK4pECgYACNv0EmKr+nIA13fjh\n' + | ||
'piqbYYyVXYOiWM4cmODG/d1J8/vR4YhWHWPbAEw7LD0DEwDIHLlRZr/1jsHbFcwt\n' + | ||
'4tzRs95fyHzpucpGhocmjWx43qtxEhDeJrxPlkIXHakciAEhoo+5YeRSSgRse5Pr\n' + | ||
'ZDosdr5fA+DADs8tnto5Glf5owOBhAACgYBHcEF5ytvCRiKbsWKOgeMZ7JT/XGX+\n' + | ||
'hMhS7aaJ2IspKj7YsWada1yBwoM6yYHtlpnGsq/PoPaZU8K40f47psV6OhSh+/O/\n' + | ||
'jgqLS/Ur2c0mQQqIb7vvkc7he/SPOQAqyDmyYFBuazuSf2s9Uy2hfvjWgb6X+vN9\n' + | ||
'W8SOb2668IL7Vg==\n' + | ||
'-----END PUBLIC KEY-----\n'; | ||
///--- Tests | ||
@@ -87,2 +107,7 @@ | ||
test('1024b dsa ssh key', function(t) { | ||
t.equal(sshKeyToPEM(DSA_1024), DSA_1024_PEM); | ||
t.end(); | ||
}); | ||
test('fingerprint', function(t) { | ||
@@ -93,1 +118,3 @@ var fp = sshKeyFingerprint(SSH_1024); | ||
}); | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55670
1326
+ Addedasn1@0.1.7(transitive)
- Removedasn1@0.1.5(transitive)
Updatedasn1@0.1.7