putty.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2018 Joyent, Inc.
  2. module.exports = {
  3. read: read,
  4. write: write
  5. };
  6. var assert = require('assert-plus');
  7. var Buffer = require('safer-buffer').Buffer;
  8. var rfc4253 = require('./rfc4253');
  9. var Key = require('../key');
  10. var errors = require('../errors');
  11. function read(buf, options) {
  12. var lines = buf.toString('ascii').split(/[\r\n]+/);
  13. var found = false;
  14. var parts;
  15. var si = 0;
  16. while (si < lines.length) {
  17. parts = splitHeader(lines[si++]);
  18. if (parts &&
  19. parts[0].toLowerCase() === 'putty-user-key-file-2') {
  20. found = true;
  21. break;
  22. }
  23. }
  24. if (!found) {
  25. throw (new Error('No PuTTY format first line found'));
  26. }
  27. var alg = parts[1];
  28. parts = splitHeader(lines[si++]);
  29. assert.equal(parts[0].toLowerCase(), 'encryption');
  30. parts = splitHeader(lines[si++]);
  31. assert.equal(parts[0].toLowerCase(), 'comment');
  32. var comment = parts[1];
  33. parts = splitHeader(lines[si++]);
  34. assert.equal(parts[0].toLowerCase(), 'public-lines');
  35. var publicLines = parseInt(parts[1], 10);
  36. if (!isFinite(publicLines) || publicLines < 0 ||
  37. publicLines > lines.length) {
  38. throw (new Error('Invalid public-lines count'));
  39. }
  40. var publicBuf = Buffer.from(
  41. lines.slice(si, si + publicLines).join(''), 'base64');
  42. var keyType = rfc4253.algToKeyType(alg);
  43. var key = rfc4253.read(publicBuf);
  44. if (key.type !== keyType) {
  45. throw (new Error('Outer key algorithm mismatch'));
  46. }
  47. key.comment = comment;
  48. return (key);
  49. }
  50. function splitHeader(line) {
  51. var idx = line.indexOf(':');
  52. if (idx === -1)
  53. return (null);
  54. var header = line.slice(0, idx);
  55. ++idx;
  56. while (line[idx] === ' ')
  57. ++idx;
  58. var rest = line.slice(idx);
  59. return ([header, rest]);
  60. }
  61. function write(key, options) {
  62. assert.object(key);
  63. if (!Key.isKey(key))
  64. throw (new Error('Must be a public key'));
  65. var alg = rfc4253.keyTypeToAlg(key);
  66. var buf = rfc4253.write(key);
  67. var comment = key.comment || '';
  68. var b64 = buf.toString('base64');
  69. var lines = wrap(b64, 64);
  70. lines.unshift('Public-Lines: ' + lines.length);
  71. lines.unshift('Comment: ' + comment);
  72. lines.unshift('Encryption: none');
  73. lines.unshift('PuTTY-User-Key-File-2: ' + alg);
  74. return (Buffer.from(lines.join('\n') + '\n'));
  75. }
  76. function wrap(txt, len) {
  77. var lines = [];
  78. var pos = 0;
  79. while (pos < txt.length) {
  80. lines.push(txt.slice(pos, pos + 64));
  81. pos += 64;
  82. }
  83. return (lines);
  84. }