ssh.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2015 Joyent, Inc.
  2. module.exports = {
  3. read: read,
  4. write: write
  5. };
  6. var assert = require('assert-plus');
  7. var rfc4253 = require('./rfc4253');
  8. var utils = require('../utils');
  9. var Key = require('../key');
  10. var PrivateKey = require('../private-key');
  11. var sshpriv = require('./ssh-private');
  12. /*JSSTYLED*/
  13. var SSHKEY_RE = /^([a-z0-9-]+)[ \t]+([a-zA-Z0-9+\/]+[=]*)([\n \t]+([^\n]+))?$/;
  14. /*JSSTYLED*/
  15. var SSHKEY_RE2 = /^([a-z0-9-]+)[ \t]+([a-zA-Z0-9+\/ \t\n]+[=]*)(.*)$/;
  16. function read(buf, options) {
  17. if (typeof (buf) !== 'string') {
  18. assert.buffer(buf, 'buf');
  19. buf = buf.toString('ascii');
  20. }
  21. var trimmed = buf.trim().replace(/[\\\r]/g, '');
  22. var m = trimmed.match(SSHKEY_RE);
  23. if (!m)
  24. m = trimmed.match(SSHKEY_RE2);
  25. assert.ok(m, 'key must match regex');
  26. var type = rfc4253.algToKeyType(m[1]);
  27. var kbuf = new Buffer(m[2], 'base64');
  28. /*
  29. * This is a bit tricky. If we managed to parse the key and locate the
  30. * key comment with the regex, then do a non-partial read and assert
  31. * that we have consumed all bytes. If we couldn't locate the key
  32. * comment, though, there may be whitespace shenanigans going on that
  33. * have conjoined the comment to the rest of the key. We do a partial
  34. * read in this case to try to make the best out of a sorry situation.
  35. */
  36. var key;
  37. var ret = {};
  38. if (m[4]) {
  39. try {
  40. key = rfc4253.read(kbuf);
  41. } catch (e) {
  42. m = trimmed.match(SSHKEY_RE2);
  43. assert.ok(m, 'key must match regex');
  44. kbuf = new Buffer(m[2], 'base64');
  45. key = rfc4253.readInternal(ret, 'public', kbuf);
  46. }
  47. } else {
  48. key = rfc4253.readInternal(ret, 'public', kbuf);
  49. }
  50. assert.strictEqual(type, key.type);
  51. if (m[4] && m[4].length > 0) {
  52. key.comment = m[4];
  53. } else if (ret.consumed) {
  54. /*
  55. * Now the magic: trying to recover the key comment when it's
  56. * gotten conjoined to the key or otherwise shenanigan'd.
  57. *
  58. * Work out how much base64 we used, then drop all non-base64
  59. * chars from the beginning up to this point in the the string.
  60. * Then offset in this and try to make up for missing = chars.
  61. */
  62. var data = m[2] + m[3];
  63. var realOffset = Math.ceil(ret.consumed / 3) * 4;
  64. data = data.slice(0, realOffset - 2). /*JSSTYLED*/
  65. replace(/[^a-zA-Z0-9+\/=]/g, '') +
  66. data.slice(realOffset - 2);
  67. var padding = ret.consumed % 3;
  68. if (padding > 0 &&
  69. data.slice(realOffset - 1, realOffset) !== '=')
  70. realOffset--;
  71. while (data.slice(realOffset, realOffset + 1) === '=')
  72. realOffset++;
  73. /* Finally, grab what we think is the comment & clean it up. */
  74. var trailer = data.slice(realOffset);
  75. trailer = trailer.replace(/[\r\n]/g, ' ').
  76. replace(/^\s+/, '');
  77. if (trailer.match(/^[a-zA-Z0-9]/))
  78. key.comment = trailer;
  79. }
  80. return (key);
  81. }
  82. function write(key, options) {
  83. assert.object(key);
  84. if (!Key.isKey(key))
  85. throw (new Error('Must be a public key'));
  86. var parts = [];
  87. var alg = rfc4253.keyTypeToAlg(key);
  88. parts.push(alg);
  89. var buf = rfc4253.write(key);
  90. parts.push(buf.toString('base64'));
  91. if (key.comment)
  92. parts.push(key.comment);
  93. return (new Buffer(parts.join(' ')));
  94. }