Skip to content

Commit

Permalink
Tests and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondralukes committed Jul 21, 2020
1 parent f62726c commit d92a4d5
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 5 deletions.
18 changes: 14 additions & 4 deletions src/formats/openssh.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ module.exports = {
if(options.type === 'der'){
return writer.buf
} else {
return PRIVATE_OPENING_BOUNDARY + '\n' + utils.linebrk(buf.toString('base64'), 70) + '\n' + PRIVATE_CLOSING_BOUNDARY;
return PRIVATE_OPENING_BOUNDARY + '\n' + utils.linebrk(buf.toString('base64'), 70) + '\n' + PRIVATE_CLOSING_BOUNDARY + '\n';
}
},

Expand Down Expand Up @@ -202,7 +202,7 @@ module.exports = {
if(options.type === 'der'){
return writer.buf
} else {
return 'ssh-rsa ' + buf.toString('base64') + ' ' + comment;
return 'ssh-rsa ' + buf.toString('base64') + ' ' + comment + '\n';
}
},

Expand All @@ -218,7 +218,17 @@ module.exports = {
if (_.isString(data)) {
if(data.substring(0, 8) !== 'ssh-rsa ')
throw Error('Unsupported key format');
var pem = data.substring(8, data.indexOf(' ', 8))
let pemEnd = data.indexOf(' ', 8);

//Handle keys with no comment
if(pemEnd === -1){
pemEnd = data.length;
} else {
key.sshcomment = data.substring(pemEnd + 1)
.replace(/\s+|\n\r|\n|\r$/gm, '');
}

const pem = data.substring(8, pemEnd)
.replace(/\s+|\n\r|\n|\r$/gm, '');
buffer = Buffer.from(pem, 'base64');
} else {
Expand All @@ -235,7 +245,7 @@ module.exports = {
const type = readOpenSSHKeyString(reader).toString('ascii');

if(type !== 'ssh-rsa')
throw Error('Invalid key type');
throw Error('Invalid key type: '+ type);

const e = readOpenSSHKeyString(reader);
const n = readOpenSSHKeyString(reader);
Expand Down
16 changes: 16 additions & 0 deletions test/keys/id_rsa
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAIEAgnWPhKQwv7z2t9+Ze+dUgTPHeS3+M+EGV+G3Pg6k2Pc1WP65jxm0
5ArnipyCgKbjhjFyTmgsDR/cFH7Tbe09H6BMI5deriAvJuIcEo4zS+UyqjFXsksr9OKAbo
nb++rucjYiwuCfOZW5lt1gMmJEwm5v1SWQFzSbqgpuwFVpkDEAAAH4AAAAAAAAAAAAAAAH
c3NoLXJzYQAAAIEAgnWPhKQwv7z2t9+Ze+dUgTPHeS3+M+EGV+G3Pg6k2Pc1WP65jxm05A
rnipyCgKbjhjFyTmgsDR/cFH7Tbe09H6BMI5deriAvJuIcEo4zS+UyqjFXsksr9OKAbonb
++rucjYiwuCfOZW5lt1gMmJEwm5v1SWQFzSbqgpuwFVpkDEAAAADAQABAAAAgBGEd6D36x
PT680E2Tcp+M7ghQhghKGytYdXZ6ONk9UOXLt2eLQeX4u/axfRrDRaNHLwcMjWdBPPE14t
KXa5RFupnT4EBXdwhcazoCrfQBqTrSNc81Tm9VHXcsKv5lgT8hE8BCs6u5QtpwHDFK9K5R
a6w5lE9nWnx3rlpxTGf9WBAAAAQANIyhoJ33ughNzbCIknkMPKtgvLOUARnbya/bkfRexL
icyYzXPNuqZDY8JZQHlshN8cCcZcYjGPYYscd2LKB6oAAABBAMK1+2wf3+mtuC5DgXaU5a
wvP3pqLH+OcjwWEGa6QqZ8sxeMJlhi/4OyvxMiX+KuIapxKAaQEcegZ7WeYtRngQcAAABB
AKuGHAfE/QyyGFHmkviUVsCzno1Ov62HYMQSGhp9ptC3af8+5SzO4G9B+QJfuzzmo5AHZw
3JQQ4csaiJxzuFbwcAAAAAAQID
-----END OPENSSH PRIVATE KEY-----
1 change: 1 addition & 0 deletions test/keys/id_rsa.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCCdY+EpDC/vPa335l751SBM8d5Lf4z4QZX4bc+DqTY9zVY/rmPGbTkCueKnIKApuOGMXJOaCwNH9wUftNt7T0foEwjl16uIC8m4hwSjjNL5TKqMVeySyv04oBuidv76u5yNiLC4J85lbmW3WAyYkTCbm/VJZAXNJuqCm7AVWmQMQ==
16 changes: 16 additions & 0 deletions test/keys/id_rsa_comment
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAlwAAAAdzc2gtcn
NhAAAAAwEAAQAAAIEAs3U7i0VIEqz3K8gh67sVeM10KPoL+MmQGR9wTI0XVMb5hVzDwfTf
kbMSgE2oeQQJ1A+z/m1dPANEQsKmB+4+WyexnofCTVkyaRhC4GqbWPv4J332X1MeTYs01D
UMJZI/fAT9Cvq8LSDuRW02M6f+b2rAEtqHD+fxyekaBmxyjLcAAAIIAAAAAAAAAAAAAAAH
c3NoLXJzYQAAAIEAs3U7i0VIEqz3K8gh67sVeM10KPoL+MmQGR9wTI0XVMb5hVzDwfTfkb
MSgE2oeQQJ1A+z/m1dPANEQsKmB+4+WyexnofCTVkyaRhC4GqbWPv4J332X1MeTYs01DUM
JZI/fAT9Cvq8LSDuRW02M6f+b2rAEtqHD+fxyekaBmxyjLcAAAADAQABAAAAgH63VOgub4
ngYFel5W3SmILIcDFO/o0ZpopWzLEBH2xZY29r5T5bblIvI+086K0q0NXQkMQi7SanF9gc
IaiP7a65Tx7lKSrAmrsnSCrZ3k+dE/+MsqGwhlDA+cxf7Ti11xSBcilcp+/KpSIEaUM8W2
GWcCSRl9gY6A8rfl7bsxpBAAAAQBIPInX4FCtwwASCJVb45eMVx3+HWnMIzCW6cCn24scY
mXw4AaO/ykDpcMtyDRv8T6id7fkR+XKqZ6lKP+HxaC4AAABBANhJFHqKlpbN0PTfqjyyBM
ZWzKyzHEjwPvUcrPIrSsuQNGz/+Ync0zte0nQXMBYSQxIiSJ32fvwVdcE/hv9rWa8AAABB
ANRpALkbvpU4pjNYfWX/74eu9cbUDhHbu74cJq0mmU3jd4Uv4X7wUijkG4lVfsrdpXzJRv
aMkt1MrDSzj7kMp3kAAAAQb25kcmFAb25kcmFsdWtlcwECAw==
-----END OPENSSH PRIVATE KEY-----
1 change: 1 addition & 0 deletions test/keys/id_rsa_comment.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCzdTuLRUgSrPcryCHruxV4zXQo+gv4yZAZH3BMjRdUxvmFXMPB9N+RsxKATah5BAnUD7P+bV08A0RCwqYH7j5bJ7Geh8JNWTJpGELgaptY+/gnffZfUx5NizTUNQwlkj98BP0K+rwtIO5FbTYzp/5vasAS2ocP5/HJ6RoGbHKMtw== ondra@ondralukes
35 changes: 34 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ describe('NodeRSA', function () {
'pkcs8-private': {public: false, der: false, file: 'private_pkcs8.pem'},
'pkcs8-der': {public: false, der: true, file: 'private_pkcs8.der'},
'pkcs1-public': {public: true, der: false, file: 'public_pkcs1.pem'},
'pkcs8-public': {public: true, der: false, file: 'public_pkcs8.pem'}
'pkcs8-public': {public: true, der: false, file: 'public_pkcs8.pem'},

'openssh-public': {public: true, der: false, file: 'id_rsa.pub'},
'openssh-private': {public: false, der: false, file: 'id_rsa'}
};

describe('Good cases', function () {
Expand Down Expand Up @@ -483,6 +486,36 @@ describe('NodeRSA', function () {
})(format);
}
});

describe('OpenSSH keys', function () {
/*
* Warning!
* OpenSSH private key contains unused 64bit value, this value is set by ssh-keygen,
* but it's not used. NodeRSA does NOT store this value, so importing and exporting key sets this value to 0.
* This value is 0 in test files, so the tests pass.
*/
it('key export should preserve key data including comment', function(){
const opensshPrivateKey = fs.readFileSync(keysFolder + 'id_rsa_comment').toString();
const opensshPublicKey = fs.readFileSync(keysFolder + 'id_rsa_comment.pub').toString();
const opensshPriv = new NodeRSA(opensshPrivateKey);
const opensshPub = new NodeRSA(opensshPublicKey);

assert.equal(
opensshPriv.exportKey('openssh-private'),
opensshPrivateKey
);

assert.equal(
opensshPriv.exportKey('openssh-public'),
opensshPublicKey
);

assert.equal(
opensshPub.exportKey('openssh-public'),
opensshPublicKey
);
});
})
});

describe('Bad cases', function () {
Expand Down

0 comments on commit d92a4d5

Please sign in to comment.